-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreengrassAwareConnection.py
283 lines (215 loc) · 9.33 KB
/
GreengrassAwareConnection.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# GreengrassAwareConnection.py
#
# class to connect to IoT core or gg based on discovery.
# methods to publish to topic, subscribe, shadow, etc.
#
# Based on v 1 of the Python SKD
#
import json
import logging
import os
import time
import uuid
from AWSIoTPythonSDK.core.greengrass.discovery.providers import DiscoveryInfoProvider
from AWSIoTPythonSDK.core.protocol.connection.cores import ProgressiveBackOffCore
from AWSIoTPythonSDK.exception.AWSIoTExceptions import DiscoveryFailure, DiscoveryInvalidRequestException, publishQueueFullException
from AWSIoTPythonSDK.core.protocol.paho.client import MQTT_ERR_SUCCESS
from AWSIoTPythonSDK.exception.AWSIoTExceptions import publishError
from AWSIoTPythonSDK.MQTTLib import *
class Obj(object):
pass
class GreengrassAwareConnection:
MAX_DISCOVERY_RETRIES = 10
GROUP_CA_PATH = "./groupCA/"
OFFLINE_QUEUE_DEPTH = 100
def __init__(self, host, rootCA, cert, key, thingName, stateChangeQueue = None):
self.logger = logging.getLogger("GreengrassAwareConnection")
self.logger.setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
self.logger.addHandler(streamHandler)
self.host = host
self.rootCA = rootCA
self.cert = cert
self.key = key
self.thingName = thingName
self.stateChangeQueue = stateChangeQueue
self.backOffCore = ProgressiveBackOffCore()
self.discovered = False
self.discoverBroker()
self.connected = False
self.connect()
self.shadowConnected = False
self.connectShadow()
self.published_ids = []
def hasDiscovered(self):
return self.discovered
def discoverBroker(self):
if self.hasDiscovered():
return
# Discover GGCs
discoveryInfoProvider = DiscoveryInfoProvider()
discoveryInfoProvider.configureEndpoint(self.host)
discoveryInfoProvider.configureCredentials(self.rootCA, self.cert, self.key)
discoveryInfoProvider.configureTimeout(10) # 10 sec
retryCount = self.MAX_DISCOVERY_RETRIES
self.groupCA = None
coreInfo = None
while retryCount != 0:
try:
discoveryInfo = discoveryInfoProvider.discover(self.thingName)
caList = discoveryInfo.getAllCas()
coreList = discoveryInfo.getAllCores()
# We only pick the first ca and core info
groupId, ca = caList[0]
self.coreInfo = coreList[0]
self.logger.info("Discovered GGC: %s from Group: %s" % (self.coreInfo.coreThingArn, groupId))
self.groupCA = self.GROUP_CA_PATH + groupId + "_CA_" + str(uuid.uuid4()) + ".crt"
if not os.path.exists(self.GROUP_CA_PATH):
os.makedirs(self.GROUP_CA_PATH)
groupCAFile = open(self.groupCA, "w")
groupCAFile.write(ca)
groupCAFile.close()
self.discovered = True
break
except DiscoveryFailure as e:
# device is not configured for greengrass, revert to IoT Core
cl = Obj()
cl.host = self.host
cl.port = 8883
self.coreInfo = Obj()
self.coreInfo.connectivityInfoList = [cl]
break
except DiscoveryInvalidRequestException as e:
print("Invalid discovery request detected!")
print("Type: %s" % str(type(e)))
print("Error message: %s" % e.message)
print("Stopping...")
break
except BaseException as e:
print("Error in discovery!")
print("Type: %s" % str(type(e)))
# print("Error message: %s" % e.message)
retryCount -= 1
print("\n%d/%d retries left\n" % (retryCount, self.MAX_DISCOVERY_RETRIES))
print("Backing off...\n")
self.backOffCore.backOff()
def isConnected(self):
return self.connected
def _getCA(self):
return self.groupCA if self.hasDiscovered() else self.rootCA
def onOnline(self):
print("online callback")
def onOffline(self):
print("offline callback")
def connect(self):
if self.isConnected():
return
self.client = AWSIoTMQTTClient(self.thingName)
self.client.configureCredentials(self._getCA(), self.key, self.cert)
for connectivityInfo in self.coreInfo.connectivityInfoList:
currentHost = connectivityInfo.host
currentPort = connectivityInfo.port
self.logger.info("Trying to connect to core at %s:%d" % (currentHost, currentPort))
self.client.configureEndpoint(currentHost, currentPort)
try:
self.client.configureAutoReconnectBackoffTime(1, 128, 20)
self.client.configureOfflinePublishQueueing(1000)
self.client.configureDrainingFrequency(50)
self.client.configureMQTTOperationTimeout(10)
self.client.onOnline = self.onOnline
self.client.onOffline = self.onOffline
self.client.connect()
self.connected = True
self.currentHost = currentHost
self.currentPort = currentPort
break
except BaseException as e:
self.logger.warn("Error in Connect: Type: %s" % str(type(e)))
def disconnect(self):
if not self.isConnected():
return
if self.shadowConnected:
self.disconnectShadow()
self.client.disconnect()
self.connected = False
def pubAck(self, mid):
print(f"puback: {mid}")
self.published_ids.remove(mid)
def publicationIsBlocked(self):
# return self.pubIsQueued
return False
def publishMessageOnTopic(self, message, topic, qos=0):
if not self.isConnected():
raise ConnectionError()
result = MQTT_ERR_SUCCESS
did_publish = False
try:
result = self.client.publishAsync(topic, message, qos, self.pubAck)
did_publish = True
# may be QUEUED or has ID
self.published_ids.append(int(result))
except ValueError as e:
print(f"message queued - {result}")
except publishError as e:
print(f"Publish Error: {e.message}")
except publishQueueFullException as e:
print(f"Publish Full Exception: {e.message}")
except Exception as e:
print(f"Another Exception: {type(e)}")
return did_publish
def isShadowConnected(self):
return self.shadowConnected
def deltaHandler(self, payload, responseStatus, token):
print("got a delta message " + payload)
payloadDict = json.loads(payload)
state = payloadDict['state']
try:
self.stateChangeQueue.append(state)
except Exception as e:
pass
def shadowUpdate_callback(self, payload, responseStatus, token):
if responseStatus != 'accepted':
print(f"\n Update Status: {responseStatus}")
print(json.dumps(payload))
print("\n")
def shadowDelete_callback(self, payload, responseStatus, token):
print("shadow deleted")
# print(json.dumps({'payload': payload, 'responseStatus': responseStatus, 'token':token}))
def connectShadow(self):
if not self.isConnected():
self.logger.warn("connect regula client first to get host and port")
raise ConnectionError
self.shadowClient = AWSIoTMQTTShadowClient(self.thingName)
self.shadowClient.configureEndpoint(self.currentHost, self.currentPort)
self.shadowClient.configureCredentials(self._getCA(), self.key, self.cert)
# AWSIoTMQTTShadowClient configuration
self.shadowClient.configureAutoReconnectBackoffTime(1, 32, 20)
self.shadowClient.configureConnectDisconnectTimeout(10) # 10 sec
self.shadowClient.configureMQTTOperationTimeout(5) # 5 sec
self.shadowClient._AWSIoTMQTTClient.configureOfflinePublishQueueing(self.OFFLINE_QUEUE_DEPTH, DROP_OLDEST)
self.shadowClient.connect()
# Create a deviceShadow with persistent subscription
self.deviceShadowHandler = self.shadowClient.createShadowHandlerWithName(self.thingName, True)
self.deviceShadowHandler.shadowRegisterDeltaCallback(self.deltaHandler)
self.shadowConnected = True
def disconnectShadow(self):
if not self.shadowConnected:
return
self.shadowClient.disconnect()
self.shadowConnected = False
def updateShadow(self, update):
if not self.isShadowConnected():
raise ConnectionError
state = {'state': {
'reported': update
}}
try:
self.deviceShadowHandler.shadowUpdate(json.dumps(state), self.shadowUpdate_callback, 10)
except Exception as e:
print("Exception updating shadow")
def deleteShadow(self):
if not self.isShadowConnected():
raise ConnectionError
self.deviceShadowHandler.shadowDelete(self.shadowDelete_callback, 15)