Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit 463a606

Browse files
committed
Universal .on decorator
1 parent cc98552 commit 463a606

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

BlynkLib.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (c) 2015-2019 Volodymyr Shymanskyy. See the file LICENSE for copying permission.
22

3-
_VERSION = "0.2.0"
3+
_VERSION = "0.2.1"
44

55
import struct
66
import time
@@ -21,7 +21,6 @@ def dummy(*args):
2121
MSG_PING = const(6)
2222

2323
MSG_TWEET = const(12)
24-
MSG_EMAIL = const(13)
2524
MSG_NOTIFY = const(14)
2625
MSG_BRIDGE = const(15)
2726
MSG_HW_SYNC = const(16)
@@ -58,37 +57,27 @@ def __init__(self, auth, heartbeat=10, buffin=1024, log=None):
5857
self.state = DISCONNECTED
5958
self.connect()
6059

60+
# These are mainly for backward-compatibility you can use "blynk.on()" instead
6161
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)
7163
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))
8065
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:
8274
def __init__(self, func):
8375
self.func = func
84-
blynk.callbacks["V"+str(pin)] = func
76+
blynk.callbacks[evt] = func
8577
def __call__(self):
8678
return self.func()
8779
return Decorator
8880

89-
def on(self, evt, func):
90-
self.callbacks[evt] = func
91-
9281
def emit(self, evt, *a, **kv):
9382
self.log("Event:", evt, "->", *a)
9483
if evt in self.callbacks:
@@ -116,8 +105,8 @@ def log_event(self, event, descr=None):
116105
self._send(MSG_EVENT_LOG, event, descr)
117106

118107
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')
121110
else:
122111
id = self.msg_id
123112
self.msg_id += 1
@@ -146,10 +135,11 @@ def connect(self):
146135

147136
def disconnect(self):
148137
if self.state == DISCONNECTED: return
138+
self.bin = b""
149139
self.state = DISCONNECTED
150140
self.emit('disconnected')
151141

152-
def process(self, data=b''):
142+
def process(self, data=None):
153143
if not (self.state == CONNECTING or self.state == CONNECTED): return
154144
now = gettime()
155145
if now - self.lastRecv > self.heartbeat+(self.heartbeat//2):
@@ -164,8 +154,9 @@ def process(self, data=b''):
164154
self.bin += data
165155

166156
while True:
167-
if len(self.bin) < 5: return
168-
157+
if len(self.bin) < 5:
158+
break
159+
169160
cmd, i, dlen = struct.unpack("!BHH", self.bin[:5])
170161
if i == 0: return self.disconnect()
171162

@@ -191,9 +182,10 @@ def process(self, data=b''):
191182
if dlen >= self.buffin:
192183
print("Cmd too big: ", dlen)
193184
return self.disconnect()
194-
195-
if len(self.bin) < 5+dlen: return
196-
185+
186+
if len(self.bin) < 5+dlen:
187+
break
188+
197189
data = self.bin[5:5+dlen]
198190
self.bin = self.bin[5+dlen:]
199191

0 commit comments

Comments
 (0)