forked from aws-samples/aws-iot-jobs-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobsSample.py
329 lines (270 loc) · 12.6 KB
/
jobsSample.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'''
/*
* Copyright 2019 Amazon.com, Inc. and its affiliates. All Rights Reserved.
*
* Licensed under the MIT License. See the LICENSE accompanying this file
* for the specific language governing permissions and limitations under
* the License.
*/
'''
from AWSIoTPythonSDK.MQTTLib import DROP_OLDEST
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTThingJobsClient
from AWSIoTPythonSDK.core.jobs.thingJobManager import jobExecutionTopicType
from AWSIoTPythonSDK.core.jobs.thingJobManager import jobExecutionTopicReplyType
from AWSIoTPythonSDK.core.jobs.thingJobManager import jobExecutionStatus
from jobExecutor import JobExecutor
import threading
import logging
import time
import datetime
import argparse
import json
class Config:
thingName = None
clientId = None
topic = None
mode = None
message = None
host = None
rootCAPath = None
certificatePath = None
privateKeyPath = None
useWebsocket = False
credentialsEndpoint = None
region = None
port = 8883
roleAlias = None
class JobsMessageProcessor(object):
def __init__(self, awsIoTMQTTThingJobsClient, clientToken, jobExecutor):
#keep track of this to correlate request/responses
self.clientToken = clientToken
self.awsIoTMQTTThingJobsClient = awsIoTMQTTThingJobsClient
self.done = False
self.jobsStarted = 0
self.jobsSucceeded = 0
self.jobsRejected = 0
self._setupCallbacks(self.awsIoTMQTTThingJobsClient)
self.jobExecutor = jobExecutor
def _setupCallbacks(self, awsIoTMQTTThingJobsClient):
self.awsIoTMQTTThingJobsClient.createJobSubscription(
self.newJobReceived, jobExecutionTopicType.JOB_NOTIFY_NEXT_TOPIC)
self.awsIoTMQTTThingJobsClient.createJobSubscription(
self.startNextJobSuccessfullyInProgress, jobExecutionTopicType.JOB_START_NEXT_TOPIC, jobExecutionTopicReplyType.JOB_ACCEPTED_REPLY_TYPE)
self.awsIoTMQTTThingJobsClient.createJobSubscription(
self.startNextRejected, jobExecutionTopicType.JOB_START_NEXT_TOPIC, jobExecutionTopicReplyType.JOB_REJECTED_REPLY_TYPE)
# '+' indicates a wildcard for jobId in the following subscriptions
self.awsIoTMQTTThingJobsClient.createJobSubscription(
self.updateJobSuccessful, jobExecutionTopicType.JOB_UPDATE_TOPIC, jobExecutionTopicReplyType.JOB_ACCEPTED_REPLY_TYPE, '+')
self.awsIoTMQTTThingJobsClient.createJobSubscription(
self.updateJobRejected, jobExecutionTopicType.JOB_UPDATE_TOPIC, jobExecutionTopicReplyType.JOB_REJECTED_REPLY_TYPE, '+')
#call back on successful job updates
def startNextJobSuccessfullyInProgress(self, client, userdata, message):
payload = json.loads(message.payload.decode('utf-8'))
if 'execution' in payload:
self.jobsStarted += 1
execution = payload['execution']
result = self.executeJob(execution)
result['HandledBy'] = 'ClientToken: {}'.format(self.clientToken)
if result['didSucceed'] == True:
threading.Thread(target=self.awsIoTMQTTThingJobsClient.sendJobsUpdate, kwargs={
'jobId': execution['jobId'], 'status': jobExecutionStatus.JOB_EXECUTION_SUCCEEDED, 'statusDetails': result, 'expectedVersion': execution['versionNumber'], 'executionNumber': execution['executionNumber']}).start()
else:
threading.Thread(target=self.awsIoTMQTTThingJobsClient.sendJobsUpdate, kwargs={
'jobId': execution['jobId'], 'status': jobExecutionStatus.JOB_EXECUTION_FAILED, 'statusDetails': result, 'expectedVersion': execution['versionNumber'], 'executionNumber': execution['executionNumber']}).start()
else:
print('Start next saw no execution: ' +
message.payload.decode('utf-8'))
self.done = True
def executeJob(self, execution):
print('Executing job ID, version, number: {}, {}, {}'.format(
execution['jobId'], execution['versionNumber'], execution['executionNumber']))
print('With jobDocument: ' + json.dumps(execution['jobDocument']))
return self.jobExecutor.executeJob(execution)
def newJobReceived(self, client, userdata, message):
payload = json.loads(message.payload.decode('utf-8'))
if 'execution' in payload:
self._attemptStartNextJob()
else:
print('Notify next saw no execution')
self.done = True
def processJobs(self):
self.done = False
self._attemptStartNextJob()
def startNextRejected(self, client, userdata, message):
print('Start next rejected:' + message.payload.decode('utf-8'))
self.jobsRejected += 1
def updateJobSuccessful(self, client, userdata, message):
self.jobsSucceeded += 1
def updateJobRejected(self, client, userdata, message):
self.jobsRejected += 1
def _attemptStartNextJob(self):
statusDetails = {'StartedBy': 'ClientToken: {} on {}'.format(
self.clientToken, datetime.datetime.now().isoformat())}
threading.Thread(target=self.awsIoTMQTTThingJobsClient.sendJobsStartNext, kwargs={
'statusDetails': statusDetails}).start()
def isDone(self):
return self.done
def getStats(self):
stats = {}
stats['jobsStarted'] = self.jobsStarted
stats['jobsSucceeded'] = self.jobsSucceeded
stats['jobsRejected'] = self.jobsRejected
return stats
def getConfig():
config = Config()
with open('config.json') as configFile:
jsonConfig = json.load(configFile)
config.thingName = jsonConfig['thingName']
config.clientId = config.thingName
config.topic = config.thingName
config.mode = 'both'
config.message = 'Hello World'
config.host = jsonConfig['endpoint']
config.rootCAPath = jsonConfig['rootCaPath']
config.certificatePath = jsonConfig['deviceCertificatePath']
config.privateKeyPath = jsonConfig['privateKeyPath']
config.credentialsEndpoint = jsonConfig['credentialsEndpoint']
config.region = jsonConfig['region']
config.roleAlias = jsonConfig['roleAlias']
useWebsocket = jsonConfig['useWebsocket']
config.useWebsocket = useWebsocket == 'true'
config.port = int(jsonConfig['port'])
return config
def getDefaultEnv(jsonConfig):
for env in jsonConfig['environments']:
if env['default']:
return env
def getDefaultRegion(jsonEnvConfig):
for region in jsonEnvConfig['regions']:
if region['default']:
return region
# Read in command-line parameters
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--config", action="store",
dest="configPath", help="JSON config file")
parser.add_argument("-n", "--thingName", action="store", dest="thingName",
help="Your AWS IoT ThingName to process jobs for")
parser.add_argument("-e", "--endpoint", action="store",
dest="host", help="Your AWS IoT custom endpoint")
parser.add_argument("-r", "--rootCA", action="store",
dest="rootCAPath", help="Root CA file path")
parser.add_argument("-c", "--cert", action="store",
dest="certificatePath", help="Certificate file path")
parser.add_argument("-k", "--key", action="store",
dest="privateKeyPath", help="Private key file path")
parser.add_argument("-p", "--port", action="store",
dest="port", type=int, help="Port number override")
parser.add_argument("-a", "--roleAlias", action="store",
dest="roleAlias", help="Role alias for device")
parser.add_argument("-m", "--region", action="store",
dest="region", default="us-east-1", help="Region")
parser.add_argument("-x", "--credentialsEndpoint", action="store",
dest="credentialsEndpoint", help="IoT Credentials endpoint")
parser.add_argument("-w", "--websocket", action="store_true", dest="useWebsocket", default=False,
help="Use MQTT over WebSocket")
parser.add_argument("-id", "--clientId", action="store", dest="clientId", default="basicJobsSampleClient",
help="Targeted client id")
config = None
args = parser.parse_args()
if args.configPath:
config = getConfig()
else:
config = Config()
config.host = args.host
config.rootCAPath = args.rootCAPath
config.certificatePath = args.certificatePath
config.privateKeyPath = args.privateKeyPath
config.port = args.port
config.useWebsocket = args.useWebsocket
config.clientId = args.clientId
config.thingName = args.thingName
config.region = args.region
config.credentialsEndpoint = args.credentialsEndpoint
config.roleAlias = args.roleAlias
# print(config.host)
# print(config.rootCAPath)
# print(config.certificatePath)
# print(config.privateKeyPath)
# print(config.port)
# print(config.useWebsocket)
# print(config.clientId)
# print(config.thingName)
# print(config.region)
# print(config.credentialsEndpoint)
# print(config.roleAlias)
if config.useWebsocket and config.certificatePath and config.privateKeyPath:
parser.error(
"X.509 cert authentication and WebSocket are mutual exclusive. Please pick one.")
exit(2)
if not config.useWebsocket and (not config.certificatePath or not config.privateKeyPath):
parser.error("Missing credentials for authentication.")
exit(2)
# Port defaults
if config.useWebsocket and not config.port: # When no port override for WebSocket, default to 443
port = 443
# When no port override for non-WebSocket, default to 8883
if not config.useWebsocket and not config.port:
port = 8883
# Configure logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
# Init AWSIoTMQTTClient
myAWSIoTMQTTClient = None
if config.useWebsocket:
myAWSIoTMQTTClient = AWSIoTMQTTClient(config.clientId, useWebsocket=True)
myAWSIoTMQTTClient.configureEndpoint(config.host, config.port)
myAWSIoTMQTTClient.configureCredentials(config.rootCAPath)
else:
myAWSIoTMQTTClient = AWSIoTMQTTClient(config.clientId)
myAWSIoTMQTTClient.configureEndpoint(config.host, config.port)
myAWSIoTMQTTClient.configureCredentials(
config.rootCAPath, config.privateKeyPath, config.certificatePath)
# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTClient.configureMQTTOperationTimeout(10) # 5 sec
jobsClient = AWSIoTMQTTThingJobsClient(
config.clientId, config.thingName, QoS=1, awsIoTMQTTClient=myAWSIoTMQTTClient)
# Init AWSIoTMQTTShadowClient
myAWSIoTMQTTShadowClient = None
if config.useWebsocket:
myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient(
config.clientId, useWebsocket=True)
myAWSIoTMQTTShadowClient.configureEndpoint(config.host, config.port)
myAWSIoTMQTTShadowClient.configureCredentials(config.rootCAPath)
else:
myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient(config.clientId)
myAWSIoTMQTTShadowClient.configureEndpoint(config.host, config.port)
myAWSIoTMQTTShadowClient.configureCredentials(
config.rootCAPath, config.privateKeyPath, config.certificatePath)
# AWSIoTMQTTShadowClient configuration
myAWSIoTMQTTShadowClient.configureAutoReconnectBackoffTime(1, 32, 20)
myAWSIoTMQTTShadowClient.configureConnectDisconnectTimeout(10) # 10 sec
myAWSIoTMQTTShadowClient.configureMQTTOperationTimeout(5) # 5 sec
MQTTClient = myAWSIoTMQTTShadowClient.getMQTTConnection()
MQTTClient.configureOfflinePublishQueueing(5, DROP_OLDEST)
# Connect to AWS IoT Shadow
myAWSIoTMQTTShadowClient.connect()
# Create a deviceShadow with persistent subscription
deviceShadowHandler = myAWSIoTMQTTShadowClient.createShadowHandlerWithName(
config.thingName, True)
print('Connecting to MQTT server and setting up callbacks...')
jobsClient.connect()
jobExecutor = JobExecutor(config, deviceShadowHandler)
jobsMsgProc = JobsMessageProcessor(jobsClient, config.clientId, jobExecutor)
print('Starting to process jobs...')
while True:
jobsMsgProc.processJobs()
while not jobsMsgProc.isDone():
time.sleep(2)
time.sleep(10)
print('Done processing jobs')
print('Stats: ' + json.dumps(jobsMsgProc.getStats()))
jobsClient.disconnect()