@@ -40,6 +40,11 @@ def stub_log(*args):
40
40
class BlynkError (Exception ):
41
41
pass
42
42
43
+ class RedirectError (Exception ):
44
+ def __init__ (self , server , port ):
45
+ self .server = server
46
+ self .port = port
47
+
43
48
44
49
class Protocol (object ):
45
50
MSG_RSP = const (0 )
@@ -53,9 +58,11 @@ class Protocol(object):
53
58
MSG_INTERNAL = const (17 )
54
59
MSG_PROPERTY = const (19 )
55
60
MSG_HW = const (20 )
61
+ MSG_REDIRECT = const (41 )
56
62
MSG_HEAD_LEN = const (5 )
57
63
58
64
STATUS_INVALID_TOKEN = const (9 )
65
+ STATUS_NO_DATA = const (17 )
59
66
STATUS_OK = const (200 )
60
67
VPIN_MAX_NUM = const (32 )
61
68
@@ -83,7 +90,7 @@ def parse_response(self, rsp_data, msg_buffer):
83
90
raise BlynkError ('Command too long. Length = {}' .format (h_data ))
84
91
elif msg_type in (self .MSG_RSP , self .MSG_PING , self .MSG_INTERNAL ):
85
92
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 ):
87
94
msg_body = rsp_data [self .MSG_HEAD_LEN : self .MSG_HEAD_LEN + h_data ]
88
95
msg_args = [itm .decode ('utf-8' ) for itm in msg_body .split (b'\0 ' )]
89
96
else :
@@ -214,10 +221,12 @@ def _authenticate(self):
214
221
rsp_data = self .receive (self .rcv_buffer , self .SOCK_MAX_TIMEOUT )
215
222
if not rsp_data :
216
223
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 )
218
225
if status != self .STATUS_OK :
219
226
if status == self .STATUS_INVALID_TOKEN :
220
227
raise BlynkError ('Invalid Auth Token' )
228
+ if msg_type == self .MSG_REDIRECT :
229
+ raise RedirectError (* args )
221
230
raise BlynkError ('Auth stage failed. Status={}' .format (status ))
222
231
self ._state = self .AUTHENTICATED
223
232
self .log ('Access granted' )
@@ -271,6 +280,11 @@ def connect(self, timeout=_CONNECT_TIMEOUT):
271
280
except BlynkError as b_err :
272
281
self .disconnect (b_err )
273
282
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 )
274
288
if time .time () >= end_time :
275
289
return False
276
290
0 commit comments