-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathcode.py
More file actions
62 lines (51 loc) · 1.84 KB
/
code.py
File metadata and controls
62 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
import json
import time
import os
import random
import awsiot.greengrasscoreipc
import awsiot.greengrasscoreipc.model as model
ipc_client = awsiot.greengrasscoreipc.connect()
thing_name = os.environ["AWS_IOT_THING_NAME"]
def send_telemetry():
telemetry_data = {
"timestamp": int(time.time()),
"battery_state_of_charge": random.random() * 99.9,
"location": {
"longitude": 48.15743 + random.random() / 10.0,
"latitude": 11.57549 + random.random() / 10.0,
},
}
op = ipc_client.new_publish_to_iot_core()
op.activate(
model.PublishToIoTCoreRequest(
topic_name=f"my/iot/{thing_name}/telemetry",
qos=model.QOS.AT_LEAST_ONCE,
payload=json.dumps(telemetry_data).encode(),
)
)
try:
result = op.get_response().result(timeout=5.0)
print("successfully published message:", result)
except Exception as e:
print("failed to publish message:", e)
def main():
while True:
send_telemetry()
time.sleep(5)
if __name__ == "__main__":
# Once we enter here, we know:
# * all dependencies are available (imports succeeded)
# * IPC Client created
# * AWS_IOT_THING_NAME environment variable is available
# This should be sufficient to consider this component `running` and the deployment will be completed.
# If any of these failed, the component will be `broken`, and the deployment might roll-back or report the error.
# Once the component is `running`, we need to try as hard as possible to keep it alive and running.
while True:
try:
main()
except Exception as e:
print("ERROR", e)
time.sleep(5)