4
4
# License, v. 2.0. If a copy of the MPL was not distributed with this
5
5
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
6
7
- import asyncio
8
7
import binascii
9
8
from .ucloud import ArduinoCloudClient # noqa
10
9
from .ucloud import ArduinoCloudObject
10
+ from .ucloud import ArduinoCloudObject as Task # noqa
11
11
from .ucloud import timestamp
12
12
13
13
30
30
b"8d6444ffe82217304ff2b89aafca8ecf"
31
31
)
32
32
33
- async def coro (): # noqa
34
- pass
35
-
36
-
37
- def is_async (obj ):
38
- if hasattr (asyncio , "iscoroutinefunction" ):
39
- return asyncio .iscoroutinefunction (obj )
40
- else :
41
- return isinstance (obj , type (coro ))
42
-
43
-
44
- class Task (ArduinoCloudObject ):
45
- def __init__ (self , name , ** kwargs ):
46
- kwargs .update ({("runnable" , True )}) # Force task creation.
47
- self .on_run = kwargs .pop ("on_run" , None )
48
- if not callable (self .on_run ):
49
- raise TypeError ("Expected a callable object" )
50
- super ().__init__ (name , ** kwargs )
51
-
52
- async def run (self , aiot ):
53
- if is_async (self .on_run ):
54
- await self .on_run (aiot )
55
- else :
56
- while True :
57
- self .on_run (aiot )
58
- await asyncio .sleep (self .interval )
59
-
60
33
61
34
class Location (ArduinoCloudObject ):
62
35
def __init__ (self , name , ** kwargs ):
@@ -80,24 +53,22 @@ def __init__(self, name, **kwargs):
80
53
81
54
class Schedule (ArduinoCloudObject ):
82
55
def __init__ (self , name , ** kwargs ):
83
- kwargs .update ({("runnable " , True )}) # Force task creation.
56
+ kwargs .update ({("on_run " , self . on_run )})
84
57
self .on_active = kwargs .pop ("on_active" , None )
85
58
# Uncomment to allow the schedule to change in runtime.
86
59
# kwargs["on_write"] = kwargs.get("on_write", lambda aiot, value: None)
87
60
self .active = False
88
61
super ().__init__ (name , keys = {"frm" , "to" , "len" , "msk" }, ** kwargs )
89
62
90
- async def run (self , aiot ):
91
- while True :
92
- if self .initialized :
93
- ts = timestamp () + aiot .get ("tz_offset" , 0 )
94
- if ts > self .frm and ts < (self .frm + self .len ):
95
- if not self .active and self .on_active is not None :
96
- self .on_active (aiot , self .value )
97
- self .active = True
98
- else :
99
- self .active = False
100
- await asyncio .sleep (self .interval )
63
+ def on_run (self , aiot ):
64
+ if self .initialized :
65
+ ts = timestamp () + aiot .get ("tz_offset" , 0 )
66
+ if ts > self .frm and ts < (self .frm + self .len ):
67
+ if not self .active and self .on_active is not None :
68
+ self .on_active (aiot , self .value )
69
+ self .active = True
70
+ else :
71
+ self .active = False
101
72
102
73
103
74
class Television (ArduinoCloudObject ):
0 commit comments