Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #573 from pycom/rel_5
Browse files Browse the repository at this point in the history
Rel 5
  • Loading branch information
peter-pycom authored Oct 27, 2021
2 parents 466e52e + b3b1575 commit 1b1e869
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 96 deletions.
6 changes: 4 additions & 2 deletions esp32/frozen/Pybytes/_OTA.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ def get_update_manifest(self, fwtype=None, token=None):
wmac = hexlify(machine.unique_id()).decode('ascii')
if fwtype == 'pymesh':
request_template = "manifest.json?current_ver={}&sysname={}&token={}&ota_slot={}&wmac={}&fwtype={}&current_fwtype={}"
req = request_template.format(current_version, sysname, token, hex(pycom.ota_slot()), wmac.upper(), fwtype, 'pymesh' if hasattr(os.uname(),'pymesh') else 'pybytes')
current_fwtype = 'pymesh' if hasattr(os.uname(), 'pymesh') else 'pybytes'
req = request_template.format(current_version, sysname, token, hex(pycom.ota_slot()), wmac.upper(), fwtype, current_fwtype)
elif fwtype == 'pygate':
request_template = "manifest.json?current_ver={}&sysname={}&ota_slot={}&wmac={}&fwtype={}&current_fwtype={}"
req = request_template.format(current_version, sysname, hex(pycom.ota_slot()), wmac.upper(), fwtype, 'pygate' if hasattr(os.uname(),'pygate') else 'pybytes')
current_fwtype = 'pygate' if hasattr(os.uname(), 'pygate') else 'pybytes'
req = request_template.format(current_version, sysname, hex(pycom.ota_slot()), wmac.upper(), fwtype, current_fwtype)
else:
request_template = "manifest.json?current_ver={}&sysname={}&wmac={}&ota_slot={}"
req = request_template.format(current_version, sysname, wmac, hex(pycom.ota_slot()))
Expand Down
26 changes: 18 additions & 8 deletions esp32/frozen/Pybytes/_flash_control_OTA.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ class FCOTA:
def __init__(self):
pass

def is_folder(self, name):
try:
os.listdir(name)
return True
except:
return False

def is_file(self, name):
return not self.is_folder(name)

def update_file_content(self, path, newContent):
print_debug(2, 'Updating file [{}]'.format(path))

if '.' in path:
if self.is_file(path):
listfDir = path.split('/')
currentPath = '/'
for value in listfDir:
Expand All @@ -34,7 +44,7 @@ def update_file_content(self, path, newContent):
# check if dir exists
if value not in parentList:
# create dir
if '.' in currentPath:
if self.is_file(currentPath):
continue
os.mkdir(currentPath)

Expand All @@ -52,7 +62,7 @@ def update_file_content(self, path, newContent):
def delete_file(self, path):
print_debug(2, 'FCOTA deleting file [{}]'.format(path))
try:
if ('.' in path):
if self.is_file(path):
os.remove(path)
else:
targetedFiles = []
Expand All @@ -67,7 +77,7 @@ def delete_file(self, path):
while maxDepth >= 0:
for elem in targetedFiles:
if elem.count('/') == maxDepth:
if '.' in elem:
if self.is_file(elem):
os.remove(elem)
else:
os.rmdir(elem)
Expand All @@ -87,7 +97,7 @@ def convert_bytes(self, num):

def get_file_size(self, path):
print_debug(2, 'FCOTA getting file infos [{}]'.format(path))
if '.' in path:
if self.is_file(path):
fileInfo = os.stat(path)
print_debug(2, 'printing fileInfo tupple: ' + str(fileInfo))
return self.convert_bytes(fileInfo[6])
Expand All @@ -96,7 +106,7 @@ def get_file_size(self, path):
def get_file_content(self, path):
print_debug(2, 'FCOTA reading file [{}]'.format(path))

if '.' in path:
if self.is_file(path):
f = open(path, 'r')
content = f.read()
f.close()
Expand All @@ -113,7 +123,7 @@ def get_flash_hierarchy(self):
hierarchy = os.listdir()
folders = []
for elem in hierarchy:
if '.' not in elem:
if self.is_folder(elem):
folders.append(elem)

while len(folders) > 0:
Expand All @@ -130,7 +140,7 @@ def get_flash_hierarchy(self):
path = folders[i] + '/' + subFolders[j]
hierarchy.append(path)

if '.' not in path:
if self.is_folder(path):
foldersToCheck.append(path)

j += 1
Expand Down
2 changes: 1 addition & 1 deletion esp32/frozen/Pybytes/_main_pybytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@
# def custom_print(params):
# print("Custom method called")
# return [255, 20]
# pybytes.add_custom_method(0, custom_print)
# pybytes.add_custom_method(0, custom_print)
6 changes: 4 additions & 2 deletions esp32/frozen/Pybytes/_msg_handl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(
receive_timeout=3000,
reconnectMethod=None
):
print_debug(5, 'starting new MsgHandler')
self._host = ""
self._port = -1
self._sock = None
Expand Down Expand Up @@ -152,7 +153,8 @@ def _receive_packet(self):
try:
self._sock.setblocking(False)
msg_type = self._sock.recv(1)
except socket.error:
except socket.error as err:
print_debug(2, '_receive_packet() socket error: {}'.format(err))
self.disconnect()
self.reconnectMethod()
return False
Expand Down Expand Up @@ -213,7 +215,7 @@ def _send_packet(self, packet):
else:
print_debug(2, 'Packet sent. (Length: %d)' % written)
except socket.error as err:
print_debug(2, 'Socket send error {0}'.format(err))
print_debug(2, '_send_packet() socket error {0}'.format(err))
return False

return True if len(packet) == written else False
Expand Down
65 changes: 58 additions & 7 deletions esp32/frozen/Pybytes/_pybytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
import time
import pycom
import sys
import gc
from network import WLAN
from binascii import hexlify, a2b_base64
from machine import Timer, deepsleep, pin_sleep_wakeup, unique_id
from machine import Timer, deepsleep, pin_sleep_wakeup, unique_id, pygate_init, RTC, pygate_debug_level, reset

try:
from periodical_pin import PeriodicalPin
except:
from _periodical_pin import PeriodicalPin

try:
from pybytes_debug import print_debug
from pybytes_debug import print_debug, DEBUG
except:
from _pybytes_debug import print_debug
from _pybytes_debug import print_debug, DEBUG

try:
from pybytes_config_reader import PybytesConfigReader
Expand All @@ -36,7 +37,7 @@ class Pybytes:
WAKEUP_ALL_LOW = const(0) # noqa: F821
WAKEUP_ANY_HIGH = const(1) # noqa: F821

def __init__(self, config, activation=False, autoconnect=False):
def __init__(self, config, activation=False, autoconnect=False, user_callback=None):
self.__frozen = globals().get('__name__') == '_pybytes'
self.__activation = activation
self.__custom_message_callback = None
Expand All @@ -45,11 +46,15 @@ def __init__(self, config, activation=False, autoconnect=False):
self.__smart_config = False
self.__conf = {}
self.__pymesh = None
self.__user_callback = user_callback

if not self.__activation:
self.__conf = config
self.__conf_reader = PybytesConfigReader(config)
pycom.wifi_on_boot(False, True)
if pycom.lte_modem_en_on_boot():
pycom.lte_modem_en_on_boot(False)
reset()

self.__check_dump_ca()
self.__create_pybytes_connection(self.__conf)
Expand All @@ -66,7 +71,7 @@ def __create_pybytes_connection(self, conf):
except:
from _pybytes_connection import PybytesConnection

self.__pybytes_connection = PybytesConnection(conf, self.__recv_message)
self.__pybytes_connection = PybytesConnection(conf, self.__recv_message, user_callback=self.__user_callback)

def __check_config(self):
try:
Expand Down Expand Up @@ -155,9 +160,9 @@ def send_node_signal(self, signal_number, value, token):
topic = 'br/{}'.format(token)
self.__pybytes_connection.__pybytes_protocol.send_pybytes_custom_method_values(signal_number, [value], topic)

def send_signal(self, signal_number, value):
def send_signal(self, signal_number, value, nomesh=False):
self.__check_init()
if self.__pymesh:
if self.__pymesh and not nomesh:
self.__pymesh.unpack_pymesh_message(signal_number, value)
else:
self.__pybytes_connection.__pybytes_protocol.send_pybytes_custom_method_values(signal_number, [value])
Expand Down Expand Up @@ -280,11 +285,51 @@ def connect(self):
from _pybytes_pymesh_config import PybytesPymeshConfig
self.__pymesh = PybytesPymeshConfig(self)
self.__pymesh.pymesh_init()
elif hasattr(os.uname(), 'pygate') and self.get_config('gateway'):
# PYGATE FIRMWARE VERSION
buf = None
try:
with open('/flash/pybytes_pygate_config.json','r') as fp:
buf = fp.read()
except Exception as e:
print_debug(5, e)
print('pybytes_pygate_config.json file is missing or has wrong format')
return

try:
print('Syncing RTC via ntp...', end='')
rtc = RTC()
if not rtc.synced():
rtc.ntp_sync(server="pool.ntp.org")
to_s = 20
while not rtc.synced() and to_s > 0:
print('.', end='')
time.sleep(1)
to_s -= 1
if not rtc.synced():
print('RTC sync failed. Gateway will not work')
return
print(" RTC synced")

except Exception as e:
print_debug(5, e)
print('RTC sync failed. Gateway will not work')
return

try:
gc.collect()
if not DEBUG:
pygate_debug_level(0)
pygate_init(buf)
except Exception as e:
print('Pygate failed to start', e)
return
else:
print('ERROR! Could not connect to Pybytes!')

except Exception as ex:
print("Unable to connect to Pybytes: {}".format(ex))
sys.print_exception(ex)

def write_config(self, file='/flash/pybytes_config.json', silent=False):
try:
Expand Down Expand Up @@ -428,6 +473,12 @@ def deepsleep(self, ms, pins=None, mode=None, enable_pull=None):
self.disconnect()
deepsleep(ms)

def message_queue_len(self):
try:
return len(pybytes.__pybytes_connection.__connection.__mqtt._msgHandler._output_queue)
except:
return None

def dump_ca(self, ca_file='/flash/cert/pycom-ca.pem'):
try:
from _pybytes_ca import PYBYTES_CA
Expand Down
5 changes: 3 additions & 2 deletions esp32/frozen/Pybytes/_pybytes_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


class PybytesConnection:
def __init__(self, config, message_callback):
def __init__(self, config, message_callback, user_callback=None):
if config is not None:
self.__conf = config
self.__conf_reader = PybytesConfigReader(config)
Expand All @@ -61,7 +61,8 @@ def __init__(self, config, message_callback):
self.__mqtt_download_topic = "d" + self.__device_id
self.__mqtt_upload_topic = "u" + self.__device_id
self.__pybytes_protocol = PybytesProtocol(
config, message_callback, pybytes_connection=self
config, message_callback, pybytes_connection=self,
user_callback=user_callback
)
self.__connection = None
self.__connection_status = constants.__CONNECTION_STATUS_DISCONNECTED
Expand Down
3 changes: 3 additions & 0 deletions esp32/frozen/Pybytes/_pybytes_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class constants:
__TYPE_PYMESH = 0x0D
__TYPE_PYBYTES = 0x0E
__TYPE_ML = 0x0F
__TYPE_USER = 0x08
__PYBYTES_PROTOCOL = ">B%ds"
__PYBYTES_PROTOCOL_PING = ">B"
__PYBYTES_INTERNAL_PROTOCOL = ">BBH"
Expand All @@ -100,6 +101,8 @@ class constants:
__FCOTA_PING = 0x03
__FCOTA_COMMAND_FILE_DELETE = 0x04
__FCOTA_COMMAND_FILE_UPDATE_NO_RESET = 0x05
__FCOTA_COMMAND_GATEWAY_DEPLOY = 0x06
__FCOTA_COMMAND_TOGGLE_GATEWAY = 0x07

__DEVICE_TYPE_WIPY = 0x00
__DEVICE_TYPE_LOPY = 0x01
Expand Down
8 changes: 7 additions & 1 deletion esp32/frozen/Pybytes/_pybytes_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import os
import pycom # pylint: disable=import-error
import _thread
import time

from machine import RTC
from time import timezone
Expand All @@ -22,7 +24,11 @@
def print_debug(level, msg):
"""Print log messages into console."""
if DEBUG is not None and level <= DEBUG:
print(msg)
print("[{:15.3f}] [{:10d}] {}".format(
time.ticks_ms() / 1000,
_thread.get_ident(),
msg
))


def print_debug_local(level, msg):
Expand Down
3 changes: 3 additions & 0 deletions esp32/frozen/Pybytes/_pybytes_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ def pack_info_message(self, releaseVersion=None):

if hasattr(os.uname(), 'pymesh'):
body.append(constants.__FWTYPE_PYMESH)
print_debug(4, "pymesh")
elif hasattr(os.uname(), 'pygate'):
body.append(constants.__FWTYPE_PYGATE)
print_debug(4, "pygate")
else:
body.append(constants.__FWTYPE_DEFAULT)
print_debug(4, "pybytes")

return self.__pack_message(constants.__TYPE_INFO, body)

Expand Down
Loading

0 comments on commit 1b1e869

Please sign in to comment.