8000 Add support for redirect command (#14) · vicantec/lib-python@2ebcf31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ebcf31

Browse files
egueliantohaUa
authored andcommitted
Add support for redirect command (blynkkk#14)
* Add support for redirect command
1 parent f3d1b76 commit 2ebcf31

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

blynklib.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def stub_log(*args):
4040
class BlynkError(Exception):
4141
pass
4242

43+
class RedirectError(Exception):
44+
def __init__(self, server, port):
45+
self.server = server
46+
self.port = port
47+
4348

4449
class Protocol(object):
4550
MSG_RSP = const(0)
@@ -53,9 +58,11 @@ class Protocol(object):
5358
MSG_INTERNAL = const(17)
5459
MSG_PROPERTY = const(19)
5560
MSG_HW = const(20)
61+
MSG_REDIRECT = const(41)
5662
MSG_HEAD_LEN = const(5)
5763

5864
STATUS_INVALID_TOKEN = const(9)
65+
STATUS_NO_DATA = const(17)
5966
STATUS_OK = const(200)
6067
VPIN_MAX_NUM = const(32)
6168

@@ -83,7 +90,7 @@ def parse_response(self, rsp_data, msg_buffer):
8390
raise BlynkError('Command too long. Length = {}'.format(h_data))
8491
elif msg_type in (self.MSG_RSP, self.MSG_PING, self.MSG_INTERNAL):
8592
pass
86-
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE):
93+
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_REDIRECT):
8794
msg_body = rsp_data[self.MSG_HEAD_LEN: self.MSG_HEAD_LEN + h_data]
8895
msg_args = [itm.decode('utf-8') for itm in msg_body.split(b'\0')]
8996
else:
@@ -214,10 +221,12 @@ def _authenticate(self):
214221
rsp_data = self.receive(self.rcv_buffer, self.SOCK_MAX_TIMEOUT)
215222
if not rsp_data:
216223
raise BlynkError('Auth stage timeout')
217-
_, _, status, _ = self.parse_response(rsp_data, self.rcv_buffer)
224+
msg_type, _, status, args = self.parse_response(rsp_data, self.rcv_buffer)
218225
if status != self.STATUS_OK:
219226
if status == self.STATUS_INVALID_TOKEN:
220227
raise BlynkError('Invalid Auth Token')
228+
if msg_type == self.MSG_REDIRECT:
229+
raise RedirectError(*args)
221230
raise BlynkError('Auth stage failed. Status={}'.format(status))
222231
self._state = self.AUTHENTICATED
223232
self.log('Access granted')
@@ -271,6 +280,11 @@ def connect(self, timeout=_CONNECT_TIMEOUT):
271280
except BlynkError as b_err:
272281
self.disconnect(b_err)
273282
sleep_ms(self.TASK_PERIOD_RES)
283+
except RedirectError as r_err:
284+
self.disconnect()
285+
self.server = r_err.server
286+
self.port = r_err.port
287+
sleep_ms(self.TASK_PERIOD_RES)
274288
if time.time() >= end_time:
275289
return False
276290

0 commit comments

Comments
 (0)
0