@@ -28,6 +28,7 @@ def __init__(self):
28
28
self .__force_update = False
29
29
30
30
def __write_config (self , filename = '/flash/pybytes_config.json' ):
31
+ print_debug (2 , 'Writing configuration to {}' .format (filename ))
31
32
try :
32
33
cf = open (filename , 'w' )
33
34
cf .write (json .dumps (self .__pybytes_config ))
@@ -131,16 +132,24 @@ def __process_sigfox_registration(self, activation_token):
131
132
if hasattr (pycom , 'sigfox_info' ):
132
133
if pycom .sigfox_info ()[0 ] is None or pycom .sigfox_info ()[1 ] is None or pycom .sigfox_info ()[2 ] is None or pycom .sigfox_info ()[3 ] is None :
133
134
try :
135
+ jsigfox = None
134
136
from network import LoRa
135
137
data = { "activationToken" : activation_token ['a' ], "wmac" : binascii .hexlify (machine .unique_id ()).upper (), "smac" : binascii .hexlify (LoRa (region = LoRa .EU868 ).mac ())}
136
138
print_debug (99 ,'sigfox_registration: {}' .format (data ))
137
- self .__pybytes_sigfox_registration = urequest .post ('https://api.{}/v2/register-sigfox' .format (constants .__DEFAULT_DOMAIN ), json = data , headers = {'content-type' : 'application/json' })
138
- start_time = time .time ()
139
- while (self .__pybytes_sigfox_registration is None or self .__pybytes_sigfox_registration .status_code != 200 ) and time .time () - start_time < 600 :
140
- time .sleep (30 )
139
+ try :
141
140
self .__pybytes_sigfox_registration = urequest .post ('https://api.{}/v2/register-sigfox' .format (constants .__DEFAULT_DOMAIN ), json = data , headers = {'content-type' : 'application/json' })
142
- if self .__pybytes_sigfox_registration is not None and self .__pybytes_sigfox_registration .status_code == 200 :
143
141
jsigfox = self .__pybytes_sigfox_registration .json ()
142
+ except :
143
+ jsigfox = None
144
+ start_time = time .time ()
145
+ while jsigfox is None and time .time () - start_time < 300 :
146
+ time .sleep (15 )
147
+ try :
148
+ self .__pybytes_sigfox_registration = urequest .post ('https://api.{}/v2/register-sigfox' .format (constants .__DEFAULT_DOMAIN ), json = data , headers = {'content-type' : 'application/json' })
149
+ jsigfox = self .__pybytes_sigfox_registration .json ()
150
+ except :
151
+ jsigfox = None
152
+ if jsigfox is not None :
144
153
try :
145
154
self .__pybytes_sigfox_registration .close ()
146
155
except :
@@ -163,13 +172,14 @@ def __process_cli_activation(self, filename, activation_token):
163
172
try :
164
173
if not self .__pybytes_cli_activation .status_code == 200 :
165
174
print_debug (3 , 'Activation request returned {}.' .format (self .__pybytes_cli_activation .status_code ))
166
- self .__pybytes_cli_activation .close ()
167
175
else :
168
176
print_debug (99 , 'Activation response:\n {}' .format (self .__pybytes_cli_activation .json ()))
169
- self .__process_config (filename , self .__generate_cli_config ())
170
- self .__pybytes_cli_activation .close ()
171
177
if self .__process_sigfox_registration (activation_token ):
172
- if self .__check_config () and self .__write_config (filename ):
178
+ if self .__process_config (filename , self .__generate_cli_config ()):
179
+ try :
180
+ self .__pybytes_cli_activation .close ()
181
+ except :
182
+ pass
173
183
return self .__pybytes_config
174
184
else :
175
185
print ('Unable to provision Sigfox! Please try again.' )
@@ -190,7 +200,7 @@ def __process_activation(self, filename):
190
200
else :
191
201
self .__activation2config ()
192
202
self .__pybytes_activation .close ()
193
-
203
+ print_debug ( 2 , 'Checking and writing configuration in __process_activation' )
194
204
if self .__check_config () and self .__write_config (filename ):
195
205
return True
196
206
return False
@@ -361,6 +371,7 @@ def __process_config(self, filename, configuration):
361
371
self .__pybytes_config .update (sigfox_config )
362
372
if ssl_params is not None :
363
373
self .__pybytes_config .update (ssl_params )
374
+ print_debug (2 , 'Checking and writing configuration in __process_config' )
364
375
if (len (self .__pybytes_config ['username' ]) > 4 and len (self .__pybytes_config ['device_id' ]) >= 36 and len (self .__pybytes_config ['server' ]) > 4 ) and self .__write_config (filename ):
365
376
self .__pybytes_config ['cfg_msg' ] = "Configuration successfully converted to pybytes_config.json"
366
377
return True
@@ -379,6 +390,7 @@ def __convert_legacy_config(self, filename):
379
390
else :
380
391
self .__pybytes_config .update (pybytes_legacy_config )
381
392
del pybytes_legacy_config
393
+ print_debug (2 , 'Checking and writing configuration in __convert_legacy_config' )
382
394
if self .__write_config (filename ):
383
395
self .__pybytes_config ['cfg_msg' ] = 'Configuration successfully converted from config.py to {}' .format (filename )
384
396
self .__force_update = False
0 commit comments