diff --git a/examples/example.py b/examples/example.py index a945c04..00823b8 100644 --- a/examples/example.py +++ b/examples/example.py @@ -31,7 +31,7 @@ def on_clight_changed(client, clight): logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}") -def user_task(client): +def user_task(client, args): # NOTE: this function should not block. # This is a user-defined task that updates the colored light. Note any registered # cloud object can be accessed using the client object passed to this function. diff --git a/examples/micropython_basic.py b/examples/micropython_basic.py index c2871de..b97cea8 100644 --- a/examples/micropython_basic.py +++ b/examples/micropython_basic.py @@ -32,7 +32,7 @@ def on_clight_changed(client, clight): logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}") -def user_task(client): +def user_task(client, args): # NOTE: this function should not block. # This is a user-defined task that updates the colored light. Note any registered # cloud object can be accessed using the client object passed to this function. @@ -41,8 +41,7 @@ def user_task(client): client["clight"].bri = round(uniform(0, 100), 1) -def wdt_task(client): - global wdt +def wdt_task(client, wdt): # Update the WDT to prevent it from resetting the system wdt.feed() @@ -130,7 +129,7 @@ def wifi_connect(): from machine import WDT # Enable the WDT with a timeout of 5s (1s is the minimum) wdt = WDT(timeout=7500) - client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0)) + client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0, args=wdt)) except (ImportError, AttributeError): pass diff --git a/src/arduino_iot_cloud/__init__.py b/src/arduino_iot_cloud/__init__.py index 02661c8..1ca60e8 100644 --- a/src/arduino_iot_cloud/__init__.py +++ b/src/arduino_iot_cloud/__init__.py @@ -60,7 +60,7 @@ def __init__(self, name, **kwargs): self.active = False super().__init__(name, keys={"frm", "to", "len", "msk"}, **kwargs) - def on_run(self, aiot): + def on_run(self, aiot, args=None): if self.initialized: ts = timestamp() + aiot.get("tz_offset", 0) if ts > self.frm and ts < (self.frm + self.len): @@ -149,7 +149,7 @@ def __init__(self, name, **kwargs): ) -def async_wifi_connection(client=None, connecting=[False]): +def async_wifi_connection(client=None, args=None, connecting=[False]): import time import network import logging diff --git a/src/arduino_iot_cloud/ucloud.py b/src/arduino_iot_cloud/ucloud.py index bc323d6..9c2b57f 100644 --- a/src/arduino_iot_cloud/ucloud.py +++ b/src/arduino_iot_cloud/ucloud.py @@ -53,6 +53,7 @@ def __init__(self, name, **kwargs): self.on_run = kwargs.pop("on_run", None) self.interval = kwargs.pop("interval", 1.0) self.backoff = kwargs.pop("backoff", None) + self.args = kwargs.pop("args", None) value = kwargs.pop("value", None) if keys := kwargs.pop("keys", {}): value = { # Create a complex object (with sub-records). @@ -165,7 +166,7 @@ async def run(self, client): def run_sync(self, client): if self.on_run is not None: - self.on_run(client) + self.on_run(client, self.args) if self.on_read is not None: self.value = self.on_read(client) if self.on_write is not None and self.on_write_scheduled: @@ -327,7 +328,7 @@ def poll_records(self): if log_level_enabled(logging.ERROR): logging.error(f"task: {record.name} raised exception: {str(e)}.") - def poll_connect(self, aiot=None): + def poll_connect(self, aiot=None, args=None): logging.info("Connecting to Arduino IoT cloud...") try: self.mqtt.connect() @@ -343,12 +344,12 @@ def poll_connect(self, aiot=None): if self.async_mode: if self.thing_id is None: - self.register("discovery", on_run=self.poll_discovery, interval=0.200) - self.register("mqtt_task", on_run=self.poll_mqtt, interval=0.100) + self.register("discovery", on_run=self.poll_discovery, interval=0.500) + self.register("mqtt_task", on_run=self.poll_mqtt, interval=1.0) raise DoneException() self.connected = True - def poll_discovery(self, aiot=None): + def poll_discovery(self, aiot=None, args=None): self.mqtt.check_msg() if self.records.get("thing_id").value is not None: self.thing_id = self.records.pop("thing_id").value @@ -372,7 +373,7 @@ def poll_discovery(self, aiot=None): if self.async_mode: raise DoneException() - def poll_mqtt(self, aiot=None): + def poll_mqtt(self, aiot=None, args=None): self.mqtt.check_msg() if self.thing_id is not None: self.senmlpack.clear() @@ -405,6 +406,8 @@ async def run(self, interval, backoff): try: await asyncio.gather(*self.tasks.values(), return_exceptions=False) break # All tasks are done, not likely. + except KeyboardInterrupt as e: + raise e except Exception as e: task_except = e pass # import traceback; traceback.print_exc()