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
+ import binascii
7
9
from .ucloud import ArduinoCloudClient # noqa
8
10
from .ucloud import ArduinoCloudObject
9
11
from .ucloud import timestamp
10
12
11
- try :
12
- import asyncio
13
- import binascii
14
- except ImportError :
15
- import uasyncio as asyncio
16
- import ubinascii as binascii
17
-
18
13
19
14
CADATA = binascii .unhexlify (
20
15
b"308201cf30820174a00302010202141f101deba7e125e727c1a391e3ec0d"
35
30
b"8d6444ffe82217304ff2b89aafca8ecf"
36
31
)
37
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
+
38
43
39
44
class Task (ArduinoCloudObject ):
40
45
def __init__ (self , name , ** kwargs ):
@@ -45,9 +50,12 @@ def __init__(self, name, **kwargs):
45
50
super ().__init__ (name , ** kwargs )
46
51
47
52
async def run (self , aiot ):
48
- while True :
49
- self .on_run (aiot )
50
- await asyncio .sleep (self .interval )
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 )
51
59
52
60
53
61
class Location (ArduinoCloudObject ):
0 commit comments