42
42
class InvalidStateError (Exception ):
43
43
pass
44
44
45
+ # Server/port for basic auth.
46
+ _DEFAULT_UP_SERVER = ("mqtts-up.iot.oniudra.cc" , 8884 )
47
+
48
+ # Default server/port for key/cert auth.
49
+ _DEFAULT_SA_SERVER = ("mqtts-sa.iot.oniudra.cc" , 8883 )
50
+
45
51
46
52
def timestamp ():
47
53
return int (time .time ())
@@ -171,8 +177,8 @@ def __init__(
171
177
username = None ,
172
178
password = None ,
173
179
ssl_params = {},
174
- server = "mqtts-sa.iot.oniudra.cc" ,
175
- port = 8883 ,
180
+ server = None ,
181
+ port = None ,
176
182
keepalive = 10
177
183
):
178
184
self .tasks = {}
@@ -183,15 +189,28 @@ def __init__(
183
189
self .last_ping = timestamp ()
184
190
self .device_topic = b"/a/d/" + device_id + b"/e/i"
185
191
self .senmlpack = SenmlPack ("" , self .senml_generic_callback )
192
+
193
+ # MicroPython does not support secure elements yet, and key/cert
194
+ # must be loaded from DER files and passed as binary blobs.
186
195
if "keyfile" in ssl_params and "der" in ssl_params ["keyfile" ]:
187
- # MicroPython does not support secure elements yet, and key/cert
188
- # must be loaded from DER files and passed as binary blobs.
189
196
with open (ssl_params .pop ("keyfile" ), "rb" ) as f :
190
197
ssl_params ["key" ] = f .read ()
191
198
with open (ssl_params .pop ("certfile" ), "rb" ) as f :
192
199
ssl_params ["cert" ] = f .read ()
193
- self .mqtt = MQTTClient (device_id , server , port , ssl_params , username , password , keepalive , self .mqtt_callback )
194
- # Note: the following internal objects are initialized by the cloud.
200
+
201
+ # If no server/port were passed in args, set the default server/port
202
+ # based on authentication type.
203
+ if server is None :
204
+ server = _DEFAULT_SA_SERVER [0 ] if password is None else _DEFAULT_UP_SERVER [0 ]
205
+ if port is None :
206
+ port = _DEFAULT_SA_SERVER [1 ] if password is None else _DEFAULT_UP_SERVER [1 ]
207
+
208
+ # Create MQTT client.
209
+ self .mqtt = MQTTClient (
210
+ device_id , server , port , ssl_params , username , password , keepalive , self .mqtt_callback
211
+ )
212
+
213
+ # Add internal objects initialized by the cloud.
195
214
for name in ["thing_id" , "tz_offset" , "tz_dst_until" ]:
196
215
self .register (name , value = None )
197
216
0 commit comments