Skip to content

Commit 8dd84fe

Browse files
authored
Merge pull request #87 from arduino/add_task_args
ucloud: Add task args.
2 parents dc8bee1 + b548717 commit 8dd84fe

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

examples/example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def on_clight_changed(client, clight):
3131
logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}")
3232

3333

34-
def user_task(client):
34+
def user_task(client, args):
3535
# NOTE: this function should not block.
3636
# This is a user-defined task that updates the colored light. Note any registered
3737
# cloud object can be accessed using the client object passed to this function.

examples/micropython_basic.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def on_clight_changed(client, clight):
3232
logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}")
3333

3434

35-
def user_task(client):
35+
def user_task(client, args):
3636
# NOTE: this function should not block.
3737
# This is a user-defined task that updates the colored light. Note any registered
3838
# cloud object can be accessed using the client object passed to this function.
@@ -41,8 +41,7 @@ def user_task(client):
4141
client["clight"].bri = round(uniform(0, 100), 1)
4242

4343

44-
def wdt_task(client):
45-
global wdt
44+
def wdt_task(client, wdt):
4645
# Update the WDT to prevent it from resetting the system
4746
wdt.feed()
4847

@@ -130,7 +129,7 @@ def wifi_connect():
130129
from machine import WDT
131130
# Enable the WDT with a timeout of 5s (1s is the minimum)
132131
wdt = WDT(timeout=7500)
133-
client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0))
132+
client.register(Task("watchdog_task", on_run=wdt_task, interval=1.0, args=wdt))
134133
except (ImportError, AttributeError):
135134
pass
136135

src/arduino_iot_cloud/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self, name, **kwargs):
6060
self.active = False
6161
super().__init__(name, keys={"frm", "to", "len", "msk"}, **kwargs)
6262

63-
def on_run(self, aiot):
63+
def on_run(self, aiot, args=None):
6464
if self.initialized:
6565
ts = timestamp() + aiot.get("tz_offset", 0)
6666
if ts > self.frm and ts < (self.frm + self.len):
@@ -149,7 +149,7 @@ def __init__(self, name, **kwargs):
149149
)
150150

151151

152-
def async_wifi_connection(client=None, connecting=[False]):
152+
def async_wifi_connection(client=None, args=None, connecting=[False]):
153153
import time
154154
import network
155155
import logging

src/arduino_iot_cloud/ucloud.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, name, **kwargs):
5353
self.on_run = kwargs.pop("on_run", None)
5454
self.interval = kwargs.pop("interval", 1.0)
5555
self.backoff = kwargs.pop("backoff", None)
56+
self.args = kwargs.pop("args", None)
5657
value = kwargs.pop("value", None)
5758
if keys := kwargs.pop("keys", {}):
5859
value = { # Create a complex object (with sub-records).
@@ -165,7 +166,7 @@ async def run(self, client):
165166

166167
def run_sync(self, client):
167168
if self.on_run is not None:
168-
self.on_run(client)
169+
self.on_run(client, self.args)
169170
if self.on_read is not None:
170171
self.value = self.on_read(client)
171172
if self.on_write is not None and self.on_write_scheduled:
@@ -327,7 +328,7 @@ def poll_records(self):
327328
if log_level_enabled(logging.ERROR):
328329
logging.error(f"task: {record.name} raised exception: {str(e)}.")
329330

330-
def poll_connect(self, aiot=None):
331+
def poll_connect(self, aiot=None, args=None):
331332
logging.info("Connecting to Arduino IoT cloud...")
332333
try:
333334
self.mqtt.connect()
@@ -343,12 +344,12 @@ def poll_connect(self, aiot=None):
343344

344345
if self.async_mode:
345346
if self.thing_id is None:
346-
self.register("discovery", on_run=self.poll_discovery, interval=0.200)
347-
self.register("mqtt_task", on_run=self.poll_mqtt, interval=0.100)
347+
self.register("discovery", on_run=self.poll_discovery, interval=0.500)
348+
self.register("mqtt_task", on_run=self.poll_mqtt, interval=1.0)
348349
raise DoneException()
349350
self.connected = True
350351

351-
def poll_discovery(self, aiot=None):
352+
def poll_discovery(self, aiot=None, args=None):
352353
self.mqtt.check_msg()
353354
if self.records.get("thing_id").value is not None:
354355
self.thing_id = self.records.pop("thing_id").value
@@ -372,7 +373,7 @@ def poll_discovery(self, aiot=None):
372373
if self.async_mode:
373374
raise DoneException()
374375

375-
def poll_mqtt(self, aiot=None):
376+
def poll_mqtt(self, aiot=None, args=None):
376377
self.mqtt.check_msg()
377378
if self.thing_id is not None:
378379
self.senmlpack.clear()
@@ -405,6 +406,8 @@ async def run(self, interval, backoff):
405406
try:
406407
await asyncio.gather(*self.tasks.values(), return_exceptions=False)
407408
break # All tasks are done, not likely.
409+
except KeyboardInterrupt as e:
410+
raise e
408411
except Exception as e:
409412
task_except = e
410413
pass # import traceback; traceback.print_exc()

0 commit comments

Comments
 (0)