2020import logging
2121import time
2222import json
23- import getopt
23+ import argparse
2424
2525class shadowCallbackContainer :
2626 def __init__ (self , deviceShadowInstance ):
@@ -39,79 +39,32 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
3939 self .deviceShadowInstance .shadowUpdate (newPayload , None , 5 )
4040 print ("Sent." )
4141
42- # Usage
43- usageInfo = """Usage:
44-
45- Use certificate based mutual authentication:
46- python ThingShadowEcho.py -e <endpoint> -r <rootCAFilePath> -c <certFilePath> -k <privateKeyFilePath>
47-
48- Use MQTT over WebSocket:
49- python ThingShadowEcho.py -e <endpoint> -r <rootCAFilePath> -w
50- Type "python ThingShadowEcho.py -h" for available options.
51-
52-
53- """
54- # Help info
55- helpInfo = """-e, --endpoint
56- Your AWS IoT custom endpoint
57- -r, --rootCA
58- Root CA file path
59- -c, --cert
60- Certificate file path
61- -k, --key
62- Private key file path
63- -w, --websocket
64- Use MQTT over WebSocket
65- -h, --help
66- Help information
67-
68-
69- """
70-
7142# Read in command-line parameters
72- useWebsocket = False
73- host = ""
74- rootCAPath = ""
75- certificatePath = ""
76- privateKeyPath = ""
77- try :
78- opts , args = getopt .getopt (sys .argv [1 :], "hwe:k:c:r:" , ["help" , "endpoint=" , "key=" ,"cert=" ,"rootCA=" , "websocket" ])
79- if len (opts ) == 0 :
80- raise getopt .GetoptError ("No input parameters!" )
81- for opt , arg in opts :
82- if opt in ("-h" , "--help" ):
83- print (helpInfo )
84- exit (0 )
85- if opt in ("-e" , "--endpoint" ):
86- host = arg
87- if opt in ("-r" , "--rootCA" ):
88- rootCAPath = arg
89- if opt in ("-c" , "--cert" ):
90- certificatePath = arg
91- if opt in ("-k" , "--key" ):
92- privateKeyPath = arg
93- if opt in ("-w" , "--websocket" ):
94- useWebsocket = True
95- except getopt .GetoptError :
96- print (usageInfo )
97- exit (1 )
43+ parser = argparse .ArgumentParser ()
44+ parser .add_argument ("-e" , "--endpoint" , action = "store" , required = True , dest = "host" , help = "Your AWS IoT custom endpoint" )
45+ parser .add_argument ("-r" , "--rootCA" , action = "store" , required = True , dest = "rootCAPath" , help = "Root CA file path" )
46+ parser .add_argument ("-c" , "--cert" , action = "store" , dest = "certificatePath" , help = "Certificate file path" )
47+ parser .add_argument ("-k" , "--key" , action = "store" , dest = "privateKeyPath" , help = "Private key file path" )
48+ parser .add_argument ("-w" , "--websocket" , action = "store_true" , dest = "useWebsocket" , default = False ,
49+ help = "Use MQTT over WebSocket" )
50+ parser .add_argument ("-n" , "--thingName" , action = "store" , dest = "thingName" , default = "Bot" , help = "Targeted thing name" )
51+ parser .add_argument ("-id" , "--clientId" , action = "store" , dest = "clientId" , default = "ThingShadowEcho" , help = "Targeted client id" )
52+
53+ args = parser .parse_args ()
54+ host = args .host
55+ rootCAPath = args .rootCAPath
56+ certificatePath = args .certificatePath
57+ privateKeyPath = args .privateKeyPath
58+ useWebsocket = args .useWebsocket
59+ thingName = args .thingName
60+ clientId = args .clientId
61+
62+ if args .useWebsocket and args .certificatePath and args .privateKeyPath :
63+ parser .error ("X.509 cert authentication and WebSocket are mutual exclusive. Please pick one." )
64+ exit (2 )
9865
99- # Missing configuration notification
100- missingConfiguration = False
101- if not host :
102- print ("Missing '-e' or '--endpoint'" )
103- missingConfiguration = True
104- if not rootCAPath :
105- print ("Missing '-r' or '--rootCA'" )
106- missingConfiguration = True
107- if not useWebsocket :
108- if not certificatePath :
109- print ("Missing '-c' or '--cert'" )
110- missingConfiguration = True
111- if not privateKeyPath :
112- print ("Missing '-k' or '--key'" )
113- missingConfiguration = True
114- if missingConfiguration :
66+ if not args .useWebsocket and (not args .certificatePath or not args .privateKeyPath ):
67+ parser .error ("Missing credentials for authentication." )
11568 exit (2 )
11669
11770# Configure logging
@@ -125,11 +78,11 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
12578# Init AWSIoTMQTTShadowClient
12679myAWSIoTMQTTShadowClient = None
12780if useWebsocket :
128- myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient ("ThingShadowEcho" , useWebsocket = True )
81+ myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient (clientId , useWebsocket = True )
12982 myAWSIoTMQTTShadowClient .configureEndpoint (host , 443 )
13083 myAWSIoTMQTTShadowClient .configureCredentials (rootCAPath )
13184else :
132- myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient ("ThingShadowEcho" )
85+ myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient (clientId )
13386 myAWSIoTMQTTShadowClient .configureEndpoint (host , 8883 )
13487 myAWSIoTMQTTShadowClient .configureCredentials (rootCAPath , privateKeyPath , certificatePath )
13588
@@ -142,12 +95,12 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
14295myAWSIoTMQTTShadowClient .connect ()
14396
14497# Create a deviceShadow with persistent subscription
145- Bot = myAWSIoTMQTTShadowClient .createShadowHandlerWithName ("Bot" , True )
146- shadowCallbackContainer_Bot = shadowCallbackContainer (Bot )
98+ deviceShadowHandler = myAWSIoTMQTTShadowClient .createShadowHandlerWithName (thingName , True )
99+ shadowCallbackContainer_Bot = shadowCallbackContainer (deviceShadowHandler )
147100
148101# Listen on deltas
149- Bot .shadowRegisterDeltaCallback (shadowCallbackContainer_Bot .customShadowCallback_Delta )
102+ deviceShadowHandler .shadowRegisterDeltaCallback (shadowCallbackContainer_Bot .customShadowCallback_Delta )
150103
151104# Loop forever
152105while True :
153- pass
106+ time . sleep ( 1 )
0 commit comments