@@ -63,7 +63,7 @@ def __init__(self, name, **kwargs):
63
63
self ._updated = False
64
64
self .on_write_scheduled = False
65
65
self .timestamp = timestamp ()
66
- self .last_run = timestamp_ms ()
66
+ self .last_poll = timestamp_ms ()
67
67
self .runnable = any ((self .on_run , self .on_read , self .on_write ))
68
68
callback = kwargs .pop ("callback" , self .senml_callback )
69
69
for key in kwargs : # kwargs should be empty by now, unless a wrong attr was used.
@@ -193,7 +193,6 @@ def __init__(
193
193
self .thing_id = None
194
194
self .keepalive = keepalive
195
195
self .last_ping = timestamp ()
196
- self .last_run = timestamp ()
197
196
self .senmlpack = SenmlPack ("" , self .senml_generic_callback )
198
197
self .ntp_server = ntp_server
199
198
self .ntp_timeout = ntp_timeout
@@ -322,8 +321,20 @@ def mqtt_callback(self, topic, message):
322
321
self .senmlpack .from_cbor (message )
323
322
self .senmlpack .clear ()
324
323
325
- def ts_expired (self , record , ts ):
326
- return (ts - record .last_run ) > int (record .interval * 1000 )
324
+ def ts_expired (self , ts , last_ts_ms , interval_s ):
325
+ return last_ts_ms == 0 or (ts - last_ts_ms ) > int (interval_s * 1000 )
326
+
327
+ def poll_records (self ):
328
+ ts = timestamp_ms ()
329
+ try :
330
+ for record in self .records .values ():
331
+ if record .runnable and self .ts_expired (ts , record .last_poll , record .interval ):
332
+ record .run_sync (self )
333
+ record .last_poll = ts
334
+ except Exception as e :
335
+ self .records .pop (record .name )
336
+ if log_level_enabled (logging .ERROR ):
337
+ logging .error (f"task: { record .name } raised exception: { str (e )} ." )
327
338
328
339
def poll_connect (self , aiot = None ):
329
340
logging .info ("Connecting to Arduino IoT cloud..." )
@@ -332,7 +343,7 @@ def poll_connect(self, aiot=None):
332
343
except Exception as e :
333
344
if log_level_enabled (logging .WARNING ):
334
345
logging .warning (f"Connection failed { e } , retrying..." )
335
- return False
346
+ return
336
347
337
348
if self .thing_id is None :
338
349
self .mqtt .subscribe (self .device_topic , qos = 1 )
@@ -341,10 +352,10 @@ def poll_connect(self, aiot=None):
341
352
342
353
if self .async_mode :
343
354
if self .thing_id is None :
344
- self .register ("discovery" , on_run = self .poll_discovery , interval = 0.100 )
355
+ self .register ("discovery" , on_run = self .poll_discovery , interval = 0.200 )
345
356
self .register ("mqtt_task" , on_run = self .poll_mqtt , interval = 0.100 )
346
357
raise DoneException ()
347
- return True
358
+ self . connected = True
348
359
349
360
def poll_discovery (self , aiot = None ):
350
361
self .mqtt .check_msg ()
@@ -429,17 +440,26 @@ async def run(self, interval, backoff):
429
440
def start (self , interval = 1.0 , backoff = 1.2 ):
430
441
if self .async_mode :
431
442
asyncio .run (self .run (interval , backoff ))
432
- else :
433
- # Synchronous mode.
434
- while not self .poll_connect ():
435
- time .sleep (interval )
436
- interval = min (interval * backoff , 5.0 )
443
+ return
437
444
438
- while self .thing_id is None :
445
+ last_conn_ms = 0
446
+ last_disc_ms = 0
447
+
448
+ while True :
449
+ ts = timestamp_ms ()
450
+ if not self .connected and self .ts_expired (ts , last_conn_ms , interval ):
451
+ self .poll_connect ()
452
+ if last_conn_ms != 0 :
453
+ interval = min (interval * backoff , 5.0 )
454
+ last_conn_ms = ts
455
+
456
+ if self .connected and self .thing_id is None and self .ts_expired (ts , last_disc_ms , 0.250 ):
439
457
self .poll_discovery ()
440
- time . sleep ( 0.100 )
458
+ last_disc_ms = ts
441
459
442
- self .connected = True
460
+ if self .connected and self .thing_id is not None :
461
+ break
462
+ self .poll_records ()
443
463
444
464
def update (self ):
445
465
if self .async_mode :
@@ -448,25 +468,14 @@ def update(self):
448
468
if not self .connected :
449
469
try :
450
470
self .start ()
451
- self .connected = True
452
471
except Exception as e :
453
472
raise e
454
473
455
- try :
456
- ts = timestamp_ms ()
457
- for record in self .records .values ():
458
- if record .runnable and self .ts_expired (record , ts ):
459
- record .run_sync (self )
460
- record .last_run = ts
461
- except Exception as e :
462
- self .records .pop (record .name )
463
- if log_level_enabled (logging .ERROR ):
464
- logging .error (f"task: { record .name } raised exception: { str (e )} ." )
474
+ self .poll_records ()
465
475
466
476
try :
467
477
self .poll_mqtt ()
468
478
except Exception as e :
469
479
self .connected = False
470
480
if log_level_enabled (logging .WARNING ):
471
481
logging .warning (f"Connection lost { e } " )
472
- raise e
0 commit comments