1
1
# Copyright (c) 2015-2019 Volodymyr Shymanskyy. See the file LICENSE for copying permission.
2
2
3
- _VERSION = "0.2.0 "
3
+ _VERSION = "0.2.1 "
4
4
5
5
import struct
6
6
import time
@@ -21,7 +21,6 @@ def dummy(*args):
21
21
MSG_PING = const (6 )
22
22
23
23
MSG_TWEET = const (12 )
24
- MSG_EMAIL = const (13 )
25
24
MSG_NOTIFY = const (14 )
26
25
MSG_BRIDGE = const (15 )
27
26
MSG_HW_SYNC = const (16 )
@@ -58,37 +57,27 @@ def __init__(self, auth, heartbeat=10, buffin=1024, log=None):
58
57
self .state = DISCONNECTED
59
58
self .connect ()
60
59
60
+ # These are mainly for backward-compatibility you can use "blynk.on()" instead
61
61
def ON (blynk , evt ):
62
- class Decorator :
63
- def __init__ (self , func ):
64
- self .func = func
65
- blynk .callbacks [evt ] = func
66
- def __call__ (self ):
67
- return self .func ()
68
- return Decorator
69
-
70
- # These are mainly for backward-compatibility you can use "blynk.ON()" instead
62
+ return blynk .on (evt )
71
63
def VIRTUAL_READ (blynk , pin ):
72
- class Decorator ():
73
- def __init__ (self , func ):
74
- self .func = func
75
- blynk .callbacks ["readV" + str (pin )] = func
76
- def __call__ (self ):
77
- return self .func ()
78
- return Decorator
79
-
64
+ return blynk .on ("readV" + str (pin ))
80
65
def VIRTUAL_WRITE (blynk , pin ):
81
- class Decorator ():
66
+ return blynk .on ("V" + str (pin ))
67
+
68
+ def on (blynk , evt , func = None ):
69
+ if func :
70
+ blynk .callbacks [evt ] = func
71
+ return
72
+
73
+ class Decorator :
82
74
def __init__ (self , func ):
83
75
self .func = func
84
- blynk .callbacks ["V" + str ( pin ) ] = func
76
+ blynk .callbacks [evt ] = func
85
77
def __call__ (self ):
86
78
return self .func ()
87
79
return Decorator
88
80
89
- def on (self , evt , func ):
90
- self .callbacks [evt ] = func
91
-
92
81
def emit (self , evt , * a , ** kv ):
93
82
self .log ("Event:" , evt , "->" , * a )
94
83
if evt in self .callbacks :
@@ -116,8 +105,8 @@ def log_event(self, event, descr=None):
116
105
self ._send (MSG_EVENT_LOG , event , descr )
117
106
118
107
def _send (self , cmd , * args , ** kwargs ):
119
- if "id" in kwargs :
120
- id = kwargs .id
108
+ if 'id' in kwargs :
109
+ id = kwargs .get ( 'id' )
121
110
else :
122
111
id = self .msg_id
123
112
self .msg_id += 1
@@ -146,10 +135,11 @@ def connect(self):
146
135
147
136
def disconnect (self ):
148
137
if self .state == DISCONNECTED : return
138
+ self .bin = b""
149
139
self .state = DISCONNECTED
150
140
self .emit ('disconnected' )
151
141
152
- def process (self , data = b'' ):
142
+ def process (self , data = None ):
153
143
if not (self .state == CONNECTING or self .state == CONNECTED ): return
154
144
now = gettime ()
155
145
if now - self .lastRecv > self .heartbeat + (self .heartbeat // 2 ):
@@ -164,8 +154,9 @@ def process(self, data=b''):
164
154
self .bin += data
165
155
166
156
while True :
167
- if len (self .bin ) < 5 : return
168
-
157
+ if len (self .bin ) < 5 :
158
+ break
159
+
169
160
cmd , i , dlen = struct .unpack ("!BHH" , self .bin [:5 ])
170
161
if i == 0 : return self .disconnect ()
171
162
@@ -191,9 +182,10 @@ def process(self, data=b''):
191
182
if dlen >= self .buffin :
192
183
print ("Cmd too big: " , dlen )
193
184
return self .disconnect ()
194
-
195
- if len (self .bin ) < 5 + dlen : return
196
-
185
+
186
+ if len (self .bin ) < 5 + dlen :
187
+ break
188
+
197
189
data = self .bin [5 :5 + dlen ]
198
190
self .bin = self .bin [5 + dlen :]
199
191
0 commit comments