20
20
import logging
21
21
import time
22
22
import json
23
- import getopt
23
+ import argparse
24
24
25
25
class shadowCallbackContainer :
26
26
def __init__ (self , deviceShadowInstance ):
@@ -39,79 +39,32 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
39
39
self .deviceShadowInstance .shadowUpdate (newPayload , None , 5 )
40
40
print ("Sent." )
41
41
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
-
71
42
# 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 )
98
65
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." )
115
68
exit (2 )
116
69
117
70
# Configure logging
@@ -125,11 +78,11 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
125
78
# Init AWSIoTMQTTShadowClient
126
79
myAWSIoTMQTTShadowClient = None
127
80
if useWebsocket :
128
- myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient ("ThingShadowEcho" , useWebsocket = True )
81
+ myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient (clientId , useWebsocket = True )
129
82
myAWSIoTMQTTShadowClient .configureEndpoint (host , 443 )
130
83
myAWSIoTMQTTShadowClient .configureCredentials (rootCAPath )
131
84
else :
132
- myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient ("ThingShadowEcho" )
85
+ myAWSIoTMQTTShadowClient = AWSIoTMQTTShadowClient (clientId )
133
86
myAWSIoTMQTTShadowClient .configureEndpoint (host , 8883 )
134
87
myAWSIoTMQTTShadowClient .configureCredentials (rootCAPath , privateKeyPath , certificatePath )
135
88
@@ -142,12 +95,12 @@ def customShadowCallback_Delta(self, payload, responseStatus, token):
142
95
myAWSIoTMQTTShadowClient .connect ()
143
96
144
97
# 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 )
147
100
148
101
# Listen on deltas
149
- Bot .shadowRegisterDeltaCallback (shadowCallbackContainer_Bot .customShadowCallback_Delta )
102
+ deviceShadowHandler .shadowRegisterDeltaCallback (shadowCallbackContainer_Bot .customShadowCallback_Delta )
150
103
151
104
# Loop forever
152
105
while True :
153
- pass
106
+ time . sleep ( 1 )
0 commit comments