def main(): print("free ram {!r}".format(gc.mem_free())) gc.collect() loop.create_task(_resetReason()) config.getMQTT().registerWifiCallback(start_services) print("Starting uasyncio loop") try: loop.run_forever() except Exception as e: try: config.getMQTT().close() except: pass if config.DEBUG_STOP_AFTER_EXCEPTION: # want to see the exception trace in debug mode if config.USE_SOFTWARE_WATCHDOG: wdt.deinit() # so it doesn't reset the board raise e # just log the exception and reset the microcontroller if sys.platform == "esp8266": try: rtc.memory("{!s}".format(e).encode()) except Exception as e: print(e) print("{!s}".format(e).encode()) else: with open("reset_reason.txt", "w") as f: f.write(e) machine.reset()
def main(): print("free ram {!r}".format(gc.mem_free())) gc.collect() loop.create_task(_resetReason()) config.getMQTT().registerWifiCallback(start_services) print("Starting uasyncio loop") try: loop.run_forever() except Exception as e: try: config.getMQTT().close() except: pass if config.DEBUG_STOP_AFTER_EXCEPTION: # should actually never happen that the uasyncio main loop runs into an exception if config.USE_SOFTWARE_WATCHDOG: wdt.deinit() # so it doesn't reset the board raise e # just log the exception and reset the microcontroller with open("reset_reason.txt", "w") as f: s = io.StringIO() sys.print_exception(e, s) f.write(s.getvalue()) machine.reset()
def _log(self, level, *message, local_only=False, timeout=None): print("[{!s}]".format(level), *message) if timeout == 0: return if config.getMQTT() and not local_only: message = (b"{} " * len(message)).format(*message) asyncio.create_task(config.getMQTT().publish( self.base_topic.format(level), message, qos=1, timeout=timeout, await_connection=True))
async def asyncLog(name, message, level): if config.getMQTT() is not None: base_topic = "{!s}/log/{!s}/{!s}".format(config.MQTT_HOME, "{!s}", sys_vars.getDeviceID()) # if level is before id other clients can subscribe to e.g. all critical logs await config.getMQTT().publish(base_topic.format(level), "[{!s}] {}".format(name, message), qos=1) else: print(level, message)
async def callRegularPublish(func, topic, interval=None, retain=None, qos=None): interval = interval or config.INTERVAL_SEND_SENSOR mqtt = config.getMQTT() while True: if type(func) == type(_callAsyncSafe): res = await func() else: res = func() await mqtt.publish(topic, res, retain, qos) await asyncio.sleep(interval)
async def asyncLog(name, level, *message, timeout=None, await_connection=True): if level == "debug" and not config.DEBUG: # ignore debug messages if debug is disabled return if config.getMQTT(): base_topic = "{!s}/log/{!s}/{!s}".format(config.MQTT_HOME, "{!s}", sys_vars.getDeviceID()) # if level is before id other clients can subscribe to e.g. all critical logs message = (b"{} " * (len(message) + 1)).format("[{}]".format(name), *message) gc.collect() await config.getMQTT().publish(base_topic.format(level), message, qos=1, timeout=timeout, await_connection=await_connection)
async def asyncLog(self, level, *message, timeout=None, await_connection=True): if level == "debug" and not config.DEBUG: # ignore debug messages if debug is disabled return print("[{!s}]".format(level), *message) if timeout == 0: return if config.getMQTT() is not None: await config.getMQTT().publish( self.base_topic.format(level), b"{}".format(message if len(message) > 1 else message[0]), qos=1, timeout=timeout, await_connection=await_connection)
async def _loop(self): mqtt = config.getMQTT() mqtt.registerWifiCallback(self._wifiChanged) mqtt.registerConnectedCallback(self._reconnected) await self._flash(500, 1) sta = network.WLAN(network.STA_IF) st = time.ticks_ms() while True: while self._next: await self._flash(*self._next.pop(0)) await asyncio.sleep(1) if time.ticks_diff(time.ticks_ms(), st) > 60000: # heartbeat st = time.ticks_ms() if sta.isconnected(): await self._flash(20, 1) await asyncio.sleep_ms(250) await self._flash(20, 1) else: await self._flash(500, 3) await asyncio.sleep_ms(500)
__version__ = "1.4" from pysmartnode import config import uasyncio as asyncio from pysmartnode.utils import sys_vars from .definitions import * import gc # This module is used to create components. # This could be sensors, switches, binary_sensors etc. # It provides a base class for linking components and subscribed topics and # provides the basis for homeassistant autodiscovery. # Helping components like arduino, i2c and similar that only provide helper objects (like Pins) # don't need to use this module as a basis. _mqtt = config.getMQTT() # prevent multiple discoveries from running concurrently and creating Out-Of-Memory errors # or queue overflow errors. This is a starting pointer using _next_component to progress. _init_queue_start = None _components = None # pointer list of all registered components, used for mqtt etc class Component: """ Use this class as a base for components. Subclass to extend. See the template for examples. """ def __init__(self, component_name, version,
async def asyncLog(self, level, message): print("[{!s}] {}".format(level, message)) if config.getMQTT() is not None: await config.getMQTT().publish(self.base_topic.format(level), "{}".format(message), qos=1)
def _log(self, message, level, local_only=False): print("[{!s}] {}".format(level, message)) if config.getMQTT() is not None and local_only is False: asyncio.get_event_loop().create_task(config.getMQTT().publish( self.base_topic.format(level), "{}".format(message)), qos=1)