1111import time
1212import pycom
1313import sys
14+ import gc
1415from network import WLAN
1516from binascii import hexlify , a2b_base64
16- from machine import Timer , deepsleep , pin_sleep_wakeup , unique_id
17+ from machine import Timer , deepsleep , pin_sleep_wakeup , unique_id , pygate_init , RTC , pygate_debug_level , reset
1718
1819try :
1920 from periodical_pin import PeriodicalPin
2021except :
2122 from _periodical_pin import PeriodicalPin
2223
2324try :
24- from pybytes_debug import print_debug
25+ from pybytes_debug import print_debug , DEBUG
2526except :
26- from _pybytes_debug import print_debug
27+ from _pybytes_debug import print_debug , DEBUG
2728
2829try :
2930 from pybytes_config_reader import PybytesConfigReader
@@ -36,7 +37,7 @@ class Pybytes:
3637 WAKEUP_ALL_LOW = const (0 ) # noqa: F821
3738 WAKEUP_ANY_HIGH = const (1 ) # noqa: F821
3839
39- def __init__ (self , config , activation = False , autoconnect = False ):
40+ def __init__ (self , config , activation = False , autoconnect = False , user_callback = None ):
4041 self .__frozen = globals ().get ('__name__' ) == '_pybytes'
4142 self .__activation = activation
4243 self .__custom_message_callback = None
@@ -45,11 +46,15 @@ def __init__(self, config, activation=False, autoconnect=False):
4546 self .__smart_config = False
4647 self .__conf = {}
4748 self .__pymesh = None
49+ self .__user_callback = user_callback
4850
4951 if not self .__activation :
5052 self .__conf = config
5153 self .__conf_reader = PybytesConfigReader (config )
5254 pycom .wifi_on_boot (False , True )
55+ if pycom .lte_modem_en_on_boot ():
56+ pycom .lte_modem_en_on_boot (False )
57+ reset ()
5358
5459 self .__check_dump_ca ()
5560 self .__create_pybytes_connection (self .__conf )
@@ -66,7 +71,7 @@ def __create_pybytes_connection(self, conf):
6671 except :
6772 from _pybytes_connection import PybytesConnection
6873
69- self .__pybytes_connection = PybytesConnection (conf , self .__recv_message )
74+ self .__pybytes_connection = PybytesConnection (conf , self .__recv_message , user_callback = self . __user_callback )
7075
7176 def __check_config (self ):
7277 try :
@@ -155,9 +160,9 @@ def send_node_signal(self, signal_number, value, token):
155160 topic = 'br/{}' .format (token )
156161 self .__pybytes_connection .__pybytes_protocol .send_pybytes_custom_method_values (signal_number , [value ], topic )
157162
158- def send_signal (self , signal_number , value ):
163+ def send_signal (self , signal_number , value , nomesh = False ):
159164 self .__check_init ()
160- if self .__pymesh :
165+ if self .__pymesh and not nomesh :
161166 self .__pymesh .unpack_pymesh_message (signal_number , value )
162167 else :
163168 self .__pybytes_connection .__pybytes_protocol .send_pybytes_custom_method_values (signal_number , [value ])
@@ -280,11 +285,51 @@ def connect(self):
280285 from _pybytes_pymesh_config import PybytesPymeshConfig
281286 self .__pymesh = PybytesPymeshConfig (self )
282287 self .__pymesh .pymesh_init ()
288+ elif hasattr (os .uname (), 'pygate' ) and self .get_config ('gateway' ):
289+ # PYGATE FIRMWARE VERSION
290+ buf = None
291+ try :
292+ with open ('/flash/pybytes_pygate_config.json' ,'r' ) as fp :
293+ buf = fp .read ()
294+ except Exception as e :
295+ print_debug (5 , e )
296+ print ('pybytes_pygate_config.json file is missing or has wrong format' )
297+ return
298+
299+ try :
300+ print ('Syncing RTC via ntp...' , end = '' )
301+ rtc = RTC ()
302+ if not rtc .synced ():
303+ rtc .ntp_sync (server = "pool.ntp.org" )
304+ to_s = 20
305+ while not rtc .synced () and to_s > 0 :
306+ print ('.' , end = '' )
307+ time .sleep (1 )
308+ to_s -= 1
309+ if not rtc .synced ():
310+ print ('RTC sync failed. Gateway will not work' )
311+ return
312+ print (" RTC synced" )
313+
314+ except Exception as e :
315+ print_debug (5 , e )
316+ print ('RTC sync failed. Gateway will not work' )
317+ return
318+
319+ try :
320+ gc .collect ()
321+ if not DEBUG :
322+ pygate_debug_level (0 )
323+ pygate_init (buf )
324+ except Exception as e :
325+ print ('Pygate failed to start' , e )
326+ return
283327 else :
284328 print ('ERROR! Could not connect to Pybytes!' )
285329
286330 except Exception as ex :
287331 print ("Unable to connect to Pybytes: {}" .format (ex ))
332+ sys .print_exception (ex )
288333
289334 def write_config (self , file = '/flash/pybytes_config.json' , silent = False ):
290335 try :
@@ -428,6 +473,12 @@ def deepsleep(self, ms, pins=None, mode=None, enable_pull=None):
428473 self .disconnect ()
429474 deepsleep (ms )
430475
476+ def message_queue_len (self ):
477+ try :
478+ return len (pybytes .__pybytes_connection .__connection .__mqtt ._msgHandler ._output_queue )
479+ except :
480+ return None
481+
431482 def dump_ca (self , ca_file = '/flash/cert/pycom-ca.pem' ):
432483 try :
433484 from _pybytes_ca import PYBYTES_CA
0 commit comments