8000 Update _pybytes_config.py · embenix/pycom-micropython-sigfox@06dfad0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 06dfad0

Browse files
committed
Update _pybytes_config.py
Improve Sigfox activation robustness
1 parent 6d1f26c commit 06dfad0

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

esp32/frozen/Pybytes/_pybytes_config.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self):
2828
self.__force_update = False
2929

3030
def __write_config(self, filename='/flash/pybytes_config.json'):
31+
print_debug(2, 'Writing configuration to {}'.format(filename))
3132
try:
3233
cf = open(filename, 'w')
3334
cf.write(json.dumps(self.__pybytes_config))
@@ -131,16 +132,24 @@ def __process_sigfox_registration(self, activation_token):
131132
if hasattr(pycom, 'sigfox_info'):
132133
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:
133134
try:
135+
jsigfox = None
134136
from network import LoRa
135137
data = { "activationToken": activation_token['a'], "wmac": binascii.hexlify(machine.unique_id()).upper(), "smac": binascii.hexlify(LoRa(region=LoRa.EU868).mac())}
136138
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:
141140
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:
143141
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:
144153
try:
145154
self.__pybytes_sigfox_registration.close()
146155
except:
@@ -163,13 +172,14 @@ def __process_cli_activation(self, filename, activation_token):
163172
try:
164173
if not self.__pybytes_cli_activation.status_code == 200:
165174
print_debug(3, 'Activation request returned {}.'.format(self.__pybytes_cli_activation.status_code))
166-
self.__pybytes_cli_activation.close()
167175
else:
168176
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()
171177
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
173183
return self.__pybytes_config
174184
else:
175185
print('Unable to provision Sigfox! Please try again.')
@@ -190,7 +200,7 @@ def __process_activation(self, filename):
190200
else:
191201
self.__activation2config()
192202
self.__pybytes_activation.close()
193-
203+
print_debug(2, 'Checking and writing configuration in __process_activation')
194204
if self.__check_config() and self.__write_config(filename):
195205
return True
196206
return False
@@ -361,6 +371,7 @@ def __process_config(self, filename, configuration):
361371
self.__pybytes_config.update(sigfox_config)
362372
if ssl_params is not None:
363373
self.__pybytes_config.update(ssl_params)
374+
print_debug(2, 'Checking and writing configuration in __process_config')
364375
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):
365376
self.__pybytes_config['cfg_msg'] = "Configuration successfully converted to pybytes_config.json"
366377
return True
@@ -379,6 +390,7 @@ def __convert_legacy_config(self, filename):
379390
else:
380391
self.__pybytes_config.update(pybytes_legacy_config)
381392
del pybytes_legacy_config
393+
print_debug(2, 'Checking and writing configuration in __convert_legacy_config')
382394
if self.__write_config(filename):
383395
self.__pybytes_config['cfg_msg'] = 'Configuration successfully converted from config.py to {}'.format(filename)
384396
self.__force_update = False

0 commit comments

Comments
 (0)
0