-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbridge.py
221 lines (175 loc) · 5.93 KB
/
bridge.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
import threading
import queue
import os
import serial
import time
import socket
import socketserver
requests = queue.Queue()
response_vitoconnect = queue.Queue()
response_proxy = queue.Queue()
## open devices
ser_heating = serial.Serial("/dev/ttyUSB0",
baudrate=4800,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
timeout=1)
ser_vitoconnect = serial.Serial("/dev/ttyAMA0",
baudrate=4800,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
timeout=1)
def read(s, bytes):
try:
return s.read(bytes)
except AttributeError:
return s.recv(bytes)
def write(s, bytes):
try:
return s.write(bytes)
except AttributeError:
return s.send(bytearray(bytes))
def flush(s):
try:
s.flushInput
except AttributeError:
pass
def printbytes(type, bytes):
if bytes:
print (type, end="")
print (" ", end="")
for c in bytes:
print("%02x " % c, end="")
print()
else:
print(type)
def addrequest(s, name, responsequeue):
while True:
try:
header = s.read(1)
if not header:
continue
except AttributeError:
header = s.recv(1)
if not header:
break
#printbytes(name + " REQ HEADER", header)
if header == bytes([0x04]):
print(name + " P300 RESET")
write(s, [0x05])
elif header == bytes([0x16]):
p300 = read(s, 2)
if p300 != bytes([0x00, 0x00]):
flush(s)
continue
print(name + " P300 START")
write(s, [0x06])
else:
lenbyte = read(s, 1)
if not lenbyte:
continue
len = int.from_bytes(lenbyte, byteorder='little') + 1
msg = header + lenbyte + read(s, len)
#printbytes(name + " REQ BODY", msg)
with responsequeue.mutex:
responsequeue.queue.clear()
requests.put({"name": name, "msg": msg, "queue": responsequeue})
try:
response = responsequeue.get(block=True, timeout=1)
responsequeue.task_done()
write(s, response)
except queue.Empty:
print(name + "no response within 1 second")
def sendrequest(s):
init = False
while True:
if init == False:
print("INIT")
# maybe something broken and timeout has happened reading from heating
# still a response in buffer?
s.flushInput()
# reset
time.sleep(10)
s.write([0x04])
# avoid boot loop of heater, abort after 10 tries
for x in range(10):
initR = s.read(1)
# optolink periodically sends x05 every 2 seconds
if initR:
printbytes("INIT", initR)
# heater reset, time for sync required
if initR == bytes([0xFF]):
time.sleep(10)
continue
# we have an init responsn
if initR == bytes([0x06]):
s.flushInput()
init = True
print("INIT DONE")
break
else:
# hammer until we have a successful session
print("P300 KEEPALIVE")
s.write([0x16, 0x00, 0x00])
if init == False:
time.sleep(10)
continue
try:
request = requests.get(True, 1)
except queue.Empty:
print("REQ KEEPALIVE")
s.flushInput()
s.write([0x16, 0x00, 0x00])
keepR = s.read(1)
if not keepR or keepR != bytes([0x06]):
printbytes("REQ KEEPALIVE FAIL", keepR)
init = False
continue
requests.task_done()
if request['msg']:
start_time = time.strftime("%d.%m.%Y %H:%M:%S")
#print(request['name'], end="")
#print(" ", end="")
#print(start_time, end="")
#print(" ", end="")
#for c in request['msg']:
# print("%02x " % c, end="")
#print()
s.flushInput()
s.write(request['msg'])
header = s.read(2)
#printbytes("RESP HEADER", header)
lenbyte = s.read(1)
if not lenbyte:
print("INIT REQUIRED 2?")
init = False
continue
#printbytes("RESP LEN", lenbyte)
len = int.from_bytes(lenbyte, byteorder='little') + 1
msg = s.read(len)
if not msg:
print("INIT REQUIRED 3?")
init = False
continue
#printbytes("RESP BODY", msg)
request['queue'].put(header + lenbyte + msg)
thread1 = threading.Thread(target=addrequest, args=(ser_vitoconnect, "vitoconnect", response_vitoconnect,),)
thread2 = threading.Thread(target=sendrequest, args=(ser_heating,),)
print('START')
thread2.start()
thread1.start()
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
addrequest(self.request, "proxy" + str(self.client_address[1]), response_proxy)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
daemon_threads = True
pass
server = ThreadedTCPServer(('127.0.0.1', 12345), ThreadedTCPRequestHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
print('STARTED')
server_thread.join()