Skip to content

ucloud: Add task args. #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions examples/micropython_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/arduino_iot_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/arduino_iot_cloud/ucloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Loading