-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS.py
65 lines (53 loc) · 2.1 KB
/
AWS.py
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
63
64
65
import AWSIoTPythonSDK.MQTTLib as AWSIoT
import json
class AWS:
# public
topic = "/Oven"
PATH_TO_CERT = "certificates/Oven.cert.pem"
PATH_TO_KEY = "certificates/Oven.private.key"
PATH_TO_ROOT = "certificates/root-CA.crt"
# private
client = None
# inject
main = None
def __init__(self, main):
self.main = main
#<static values>
ENDPOINT = "akyvbbf6sysh7-ats.iot.us-west-2.amazonaws.com"
PORT = 443
# AWSIoTMQTTClient credential configuration
self.client = AWSIoT.AWSIoTMQTTClient(main.CLIENT_ID, useWebsocket=True)
self.client.configureEndpoint(ENDPOINT, PORT)
self.client.configureCredentials(self.PATH_TO_ROOT, self.PATH_TO_KEY, self.PATH_TO_CERT)
# AWSIoTMQTTClient connection configuration
self.client.configureAutoReconnectBackoffTime(1, 32, 20)
self.client.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
self.client.configureDrainingFrequency(2) # Draining: 2 Hz
self.client.configureConnectDisconnectTimeout(6) # 6 sec
self.client.configureMQTTOperationTimeout(5) # 5 sec
# Connect AWSIoTMQTTClient
success = self.client.connect(10)
if success:
print("Connected...")
# AWSIoTMQTTClient subscribe to incoming events
self.client.subscribe(self.topic, 1, self.main.customCallback)
else:
print("Connection failed.")
pass
def publish(self, msg, val):
data_msg = "{}".format(msg)
data_val = "{}".format(val)
if val is None:
message = {"event" : data_msg, "value" : "{}".format(0)}
else:
message = {"event" : data_msg, "value" : data_val}
success = self.client.publish(self.topic, json.dumps(message), 1)
if success:
print('Published: "' + str(message) + '" to topic "' + "'" + str(self.topic) + "'")
else:
print('Error publishing')
def disconnect(self):
try:
self.client.disconnect()
except:
pass