8000 Added internal messages support.Added RTC example · RustyPixel/lib-python@f7ea75d · GitHub
[go: up one dir, main page]

Skip to content

Commit f7ea75d

Browse files
committed
Added internal messages support.Added RTC example
1 parent 4da4c27 commit f7ea75d

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ Examples can be found **[here][blynk-py-examples]** Check them all to get famili
160160
- [06_terminal_widget.py](https://github.com/blynkkk/lib-python/blob/master/examples/06_terminal_widget.py): Communication between hardware and app through Terminal widget)
161161
- [07_tweet_and_logging.py](https://github.com/blynkkk/lib-python/blob/master/examples/07_tweet_and_logging.py): How to post to Twitter and log events from your hardware
162162
- [08_blynk_timer.py](https://github.com/blynkkk/lib-python/blob/master/examples/08_blynk_timer.py): How send data periodically from hardware by using **[Blynk Timer][blynktimer-doc]**
163-
- [09_sync_virtual_pin.py](https://github.com/blynkkk/lib-python/blob/master/examples/09_sync_virtual_pin.py): How to sync virtual pin states and properties
163+
- [09_sync_virtual_pin.py](https://github.com/blynkkk/lib-python/blob/master/examples/09_sync_virtual_pin.py): How to sync virtual pin states and properties
164+
- [10_rtc_sync.py](https://github.com/blynkkk/lib-python/blob/master/examples/10_rtc_sync.py): How to perform RTC sync with blynk server
164165

165166
##### Raspberry Pi (any):
166167
Read [Raspberry Pi guide](https://github.com/blynkkk/lib-python/tree/master/examples/raspberry) first.

blynklib.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ def parse_response(self, rsp_data, msg_buffer):
8989
raise BlynkError('invalid msg_id == 0')
9090
elif h_data >= msg_buffer:
9191
raise BlynkError('Command too long. Length = {}'.format(h_data))
92-
elif msg_type in (self.MSG_RSP, self.MSG_PING, self.MSG_INTERNAL):
92+
elif msg_type in (self.MSG_RSP, self.MSG_PING):
9393
pass
94-
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_REDIRECT):
94+
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_INTERNAL, self.MSG_REDIRECT):
9595
msg_body = rsp_data[self.MSG_HEAD_LEN: self.MSG_HEAD_LEN + h_data]
9696
msg_args = [itm.decode('utf-8') for itm in msg_body.split(b'\0')]
9797
else:
@@ -129,6 +129,9 @@ def notify_msg(self, msg):
129129
def set_property_msg(self, pin, prop, *val):
130130
return self._pack_msg(self.MSG_PROPERTY, pin, prop, *val)
131131

132+
def internal_msg(self, *args):
133+
return self._pack_msg(self.MSG_INTERNAL, *args)
134+
132135

133136
class Connection(Protocol):
134137
SOCK_MAX_TIMEOUT = const(5)
@@ -316,6 +319,9 @@ def notify(self, msg):
316319
def set_property(self, v_pin, property_name, *val):
317320
return self.send(self.set_property_msg(v_pin, property_name, *val))
318321

322+
def internal(self, *args):
323+
return self.send(self.internal_msg(*args))
324+
319325
def handle_event(blynk, event_name):
320326
class Deco(object):
321327
def __init__(self, func):
@@ -344,8 +350,8 @@ def process(self, msg_type, msg_id, msg_len, msg_args):
344350
elif msg_type == self.MSG_PING:
345351
self.send(self.response_msg(self.STATUS_OK, msg_id=msg_id))
346352
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_INTERNAL):
347-
if msg_type == self.MSG_INTERNAL and len(msg_args) >= const(3):
348-
self.call_handler("{}{}".format(self._INTERNAL, msg_args[1]), msg_args[2:])
353+
if msg_type == self.MSG_INTERNAL and len(msg_args) >= const(2):
354+
self.call_handler("{}{}".format(self._INTERNAL, msg_args[0]), msg_args[1:])
349355
elif len(msg_args) >= const(3) and msg_args[0] == 'vw':
350356
self.call_handler("{}{}".format(self._VPIN_WRITE, msg_args[1]), int(msg_args[1]), msg_args[2:])
351357
elif len(msg_args) == const(2) and msg_args[0] == 'vr':

examples/10_rtc_sync.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
[RTC EXAMPLE] ========================================================================================================
3+
4+
Environment prepare:
5+
In your Blynk App project:
6+
- add "RTC" widget,
7+
- set required TimeZone,
8+
- Run the App (green triangle in the upper right corner).
9+
- define your auth token for current example and run it
10+
11+
12+
This started program on connect will send rtc_sync call to server.
13+
RTC reply will be captured by "internal_rtc" handler
14+
UTC time with required timezone correction will be printed
15+
16+
Schema:
17+
=====================================================================================================================
18+
+-----------+ +--------------+ +--------------+
19+
| | | | | |
20+
| blynk lib | | blynk server | | blynk app |
21+
| | | | | |
22+
| | | | | |
23+
+-----+-----+ +------+-------+ +-------+------+
24+
| | |
25+
connect handler | | |
26+
+-------+ | |
27+
| | | |
28+
| | rtc sync | |
29+
+------>------------------------------------->+ rtc widget present in app? |
30+
| +----------------------------------->+
31+
| | |
32+
| | yes rtc widget found |
33+
| rtc with timezone correction +<-----------------------------------+
34+
internal_rtc | | |
35+
handler +--------<------------------------------------+ |
36+
| | | |
37+
| | | |
38+
+------>+ | |
39+
| | |
40+
| | |
41+
+ + +
42+
=====================================================================================================================
43+
Additional blynk info you can find by examining such resources:
44+
45+
Downloads, docs, tutorials: https://blynk.io
46+
Sketch generator: http://examples.blynk.cc
47+
Blynk community: http://community.blynk.cc
48+
Social networks: http://www.fb.com/blynkapp
49+
http://twitter.com/blynk_app
50+
=====================================================================================================================
51+
"""
52+
53+
import blynklib
54+
from datetime import datetime
55+
56+
BLYNK_AUTH = 'YourAuthToken'
57+
blynk = blynklib.Blynk(BLYNK_AUTH)
58+
59+
60+
@blynk.handle_event("connect")
61+
def connect_handler():
62+
blynk.internal("rtc", "sync")
63+
print("RTC sync request was sent")
64+
65+
66+
@blynk.handle_event('internal_rtc')
67+
def rtc_handler(rtc_data_list):
68+
hr_rtc_value = datetime.utcfromtimestamp(int(rtc_data_list[0])).strftime('%Y-%m-%d %H:%M:%S')
69+
print('Raw RTC value from server: {}'.format(rtc_data_list[0]))
70+
print('Human readable RTC value: {}'.format(hr_rtc_value))
71+
72+
73+
###########################################################
74+
# infinite loop that waits for event
75+
###########################################################
76+
while True:
77+
blynk.run()

test/test_blynk_main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def test_set_property(self, bl, mocker):
8585
result = bl.set_property(1, '2', '3')
8686
assert result == 60
8787

88+
def test_internal(self, bl, mocker):
89+
with mocker.patch.object(bl, 'send', return_value=70):
90+
result = bl.internal('rtc', 'sync')
91+
assert result == 70
92+
8893
def test_hadle_event(self, bl):
8994
bl._events = {}
9095

@@ -143,13 +148,13 @@ def test_process_ping(self, bl, mocker):
143148
def test_process_internal(self, bl, mocker):
144149
bl._events = {}
145150

146-
@bl.handle_event('internal_5')
151+
@bl.handle_event('internal_xyz')
147152
def internal_handler(*args):
148-
bl._status = 'INTERNAL TEST{}'.format(*args)
153+
bl._status = 'INTERNAL TEST {}'.format(*args)
149154

150155
with mocker.patch.object(bl, 'send', return_value=None):
151-
bl.process(bl.MSG_INTERNAL, 100, 200, ['int', 5, 1, 2])
152-
assert bl._status == 'INTERNAL TEST[1, 2]'
156+
bl.process(bl.MSG_INTERNAL, 100, 20, ["xyz", "add", 2])
157+
assert bl._status == "INTERNAL TEST ['add', 2]"
153158

154159
def test_process_write(self, bl, mocker):
155160
bl._events = {}

test/test_blynk_protocol.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ def test_notify_msg(self, pb):
136136
def test_set_property_msg(self, pb):
137137
result = pb.set_property_msg(10, 'color', '#FF00EE')
138138
assert result == b'\x13\x00\x02\x00\x1010\x00color\x00#FF00EE'
139+
140+
def test_internal_msg(self, pb):
141+
result = pb.internal_msg('rtc', 'sync')
142+
assert result == b'\x11\x00\x02\x00\x08rtc\x00sync'

0 commit comments

Comments
 (0)
0