forked from erazor83/pyblynk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.py
123 lines (105 loc) · 3.64 KB
/
tcp_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import time
import socket
#import hexdump
import pyblynk.common as common
#pylint: disable=w0511
class TCP_Client():
_Server = None
_Port = None
_Socket = None
_MessageID = None
_t_lastRX = None
_lastToken = None
t_Ping = 5
connected = False
def __init__(self, server = 'cloud.blynk.cc', port = 8442):
self._Server = server
self._Port = port
def connect(self, timeout = 3):
print('connected')
self.close()
self._MessageID = 0
self._Socket = socket.create_connection((self._Server, self._Port), timeout)
self._Socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if self._Socket:
self.connected = True
return self._Socket
def close(self):
if self._Socket:
self._Socket.close()
self.connected = False
def tx(self, data):
#print('tx', data)
if self._Socket:
try:
self._Socket.sendall(data)
except Exception:
self.connected = False
def rx(self, length):
if self._Socket:
data = []
#TODO can this be done better? need to look at the protocol properly
l = 0
while l < length:
r = ''
try:
r = self._Socket.recv(length-l)
self._t_lastRX = time.time()
except socket.timeout:
#print('rx-timeout')
return ''
except Exception as e:
print('rx exception', str(e))
self.connected = False
return ''
if not r:
self.connected = False
return ''
data.append(r)
#print(data)
l = l + len(r)
#hexdump.hexdump(str(data))
return ''.join(data)
def rxFrame(self):
response = self.rx(common.ProtocolHeader.size)
if response:
return common.ProtocolHeader.unpack(response)
def txFrame(self, msg_type, data):
self.tx(common.ProtocolHeader.pack(msg_type, self.newMessageID(), data))
def txFrameData(self, msg_type, data):
self.tx(common.ProtocolHeader.pack(msg_type, self.newMessageID(), len(data)) + data)
def newMessageID(self):
self._MessageID = self._MessageID+1
return self._MessageID
def auth(self, token = None):
if not token and self._lastToken:
token = self._lastToken
elif token:
self._lastToken = token
else:
return False
self.txFrame(common.MSG_LOGIN, len(token))
self.tx(token)
response = self.rxFrame()
if response:
# pylint: disable=W0612
msg_type, msg_id, msg_status = response
if (msg_status == common.MSG_STATUS_OK):
print("Auth successfull")
return True
def Ping(self):
print("Ping...")
self.txFrame(common.MSG_PING, 0)
rx_frame = self.rxFrame()
if rx_frame and (rx_frame[0] == common.MSG_RSP) and (rx_frame[1] == self._MessageID) and (rx_frame[2] == common.MSG_STATUS_OK):
print("...Pong")
return True
def keepConnection(self):
if not self.connected:
if self.connect() and self.auth():
return True
else:
time.sleep(1)
return False
if (self._t_lastRX+self.t_Ping)<time.time():
self.Ping()