-
Notifications
You must be signed in to change notification settings - Fork 11
/
WIZMSGHandler.py
357 lines (298 loc) · 13 KB
/
WIZMSGHandler.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/python
# -*- coding: utf-8 -*-
import select
import codecs
import os
from utils import logger
from PyQt5.QtCore import QThread, pyqtSignal
from constants import Opcode
from wizcmdset import Wizcmdset
exitflag = 0
# PACKET_SIZE = 1024
# PACKET_SIZE = 2048
PACKET_SIZE = 4096
def timeout_func():
# print('timeout')
global exitflag
exitflag = 1
class WIZMSGHandler(QThread):
search_result = pyqtSignal(int)
set_result = pyqtSignal(int)
searched_data = pyqtSignal(bytes)
def __init__(self, udpsock, cmd_list, what_sock, op_code, timeout):
QThread.__init__(self)
self.logger = logger
self.sock = udpsock
self.msg = bytearray(PACKET_SIZE)
self.size = 0
try:
self.inputs = [self.sock.sock]
except Exception as e:
self.logger.error('socket error:', e)
self.terminate()
self.outputs = []
self.errors = []
self.opcode = None
self.iter = 0
self.dest_mac = None
self.isvalid = False
# self.timer1 = None
self.istimeout = False
self.reply = ''
self.setting_pw_wrong = False
self.mac_list = []
self.mode_list = []
self.mn_list = []
self.vr_list = []
self.getreply = []
self.rcv_list = []
self.st_list = []
self.what_sock = what_sock
self.cmd_list = cmd_list
self.opcode = op_code
self.timeout = timeout
self.cmdset = Wizcmdset('WIZ750SR')
def timeout_func(self):
self.istimeout = True
def makecommands(self):
self.size = 0
try:
for cmd in self.cmd_list:
# print('cmd[0]: %s, cmd[1]: %s' % (cmd[0], cmd[1]))
try:
self.msg[self.size:] = str.encode(cmd[0])
except Exception as e:
self.logger.error('[ERROR] makecommands() encode:', cmd[0], e)
self.size += len(cmd[0])
if cmd[0] == "MA":
# sys.stdout.write('cmd[1]: %r\r\n' % cmd[1])
cmd[1] = cmd[1].replace(":", "")
# print(cmd[1])
# hex_string = cmd[1].decode('hex')
try:
hex_string = codecs.decode(cmd[1], 'hex')
except Exception as e:
self.logger.error(
'[ERROR] makecommands() decode:', cmd[0], cmd[1], e)
self.msg[self.size:] = hex_string
self.dest_mac = hex_string
# self.dest_mac = (int(cmd[1], 16)).to_bytes(6, byteorder='big') # Hexadecimal string to hexadecimal binary
# self.msg[self.size:] = self.dest_mac
self.size += 6
else:
try:
self.msg[self.size:] = str.encode(cmd[1])
except Exception as e:
self.logger.error(
'[ERROR] makecommands() encode param:', cmd[0], cmd[1], e)
self.size += len(cmd[1])
if "\r\n" not in cmd[1]:
self.msg[self.size:] = str.encode("\r\n")
self.size += 2
# print(self.size, self.msg)
except Exception as e:
self.logger.error('[ERROR] WIZMSGHandler makecommands(): %r' % e)
def sendcommands(self):
self.sock.sendto(self.msg)
def sendcommandsTCP(self):
self.sock.write(self.msg)
def check_parameter(self, cmdset):
# print('check_parameter()', cmdset, cmdset[:2], cmdset[2:])
try:
if b'MA' not in cmdset:
# print('check_parameter() OK', cmdset, cmdset[:2], cmdset[2:])
if self.cmdset.isvalidparameter(cmdset[:2].decode(), cmdset[2:].decode()):
return True
else:
return False
else:
return False
except Exception as e:
self.logger.error('[ERROR] WIZMSGHandler check_parameter(): %r' % e)
def run(self):
try:
self.makecommands()
if self.what_sock == 'udp':
self.sendcommands()
elif self.what_sock == 'tcp':
self.sendcommandsTCP()
except Exception as e:
self.logger.error(f'[ERROR] WIZMSGHandler sendcommands: {e}')
try:
readready, writeready, errorready = select.select(
self.inputs, self.outputs, self.errors, self.timeout)
replylists = None
self.getreply = []
self.mac_list = []
self.mn_list = []
self.vr_list = []
self.st_list = []
self.rcv_list = []
# print('readready value: ', len(readready), readready)
if self.timeout < 2:
# Search each device
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
self.logger.debug(f'Each-search recv: {data}')
self.searched_data.emit(data)
# replylists = data.splitlines()
replylists = data.split(b"\r\n")
# print('replylists', replylists)
self.getreply = replylists
else:
# Pre search
while True:
self.iter += 1
# sys.stdout.write("iter count: %r " % self.iter)
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
self.logger.debug(f'Pre-search recv: {data}')
# self.searched_data.emit(data)
# check if data reduplication
if data in self.rcv_list:
replylists = []
else:
self.rcv_list.append(data) # received data backup
# replylists = data.splitlines()
replylists = data.split(b"\r\n")
# print('replylists', replylists)
self.getreply = replylists
if self.opcode == Opcode.OP_SEARCHALL:
try:
for i in range(0, len(replylists)):
if b'MC' in replylists[i]:
if self.check_parameter(replylists[i]):
self.mac_list.append(replylists[i][2:])
if b'MN' in replylists[i]:
if self.check_parameter(replylists[i]):
self.mn_list.append(replylists[i][2:])
if b'VR' in replylists[i]:
if self.check_parameter(replylists[i]):
self.vr_list.append(replylists[i][2:])
if b'OP' in replylists[i]:
if self.check_parameter(replylists[i]):
self.mode_list.append(replylists[i][2:])
if b'ST' in replylists[i]:
if self.check_parameter(replylists[i]):
self.st_list.append(replylists[i][2:])
except Exception as e:
self.logger.error('[ERROR] WIZMSGHandler makecommands(): %r' % e)
elif self.opcode == Opcode.OP_FWUP:
for i in range(0, len(replylists)):
if b'MA' in replylists[i][:2]:
pass
# self.isvalid = True
else:
self.isvalid = False
# sys.stdout.write("%r\r\n" % replylists[i][:2])
if b'FW' in replylists[i][:2]:
# sys.stdout.write('self.isvalid == True\r\n')
# param = replylists[i][2:].split(b':')
self.reply = replylists[i][2:]
elif self.opcode == Opcode.OP_SETCOMMAND:
for i in range(0, len(replylists)):
if b'AP' in replylists[i][:2]:
if replylists[i][2:] == b' ':
self.setting_pw_wrong = True
else:
self.setting_pw_wrong = False
readready, writeready, errorready = select.select(
self.inputs, self.outputs, self.errors, 1)
if not readready or not replylists:
break
if self.opcode == Opcode.OP_SEARCHALL:
self.msleep(500)
# print('Search device:', self.mac_list)
self.search_result.emit(len(self.mac_list))
# return len(self.mac_list)
if self.opcode == Opcode.OP_SETCOMMAND:
self.msleep(500)
if len(self.rcv_list) > 0:
if self.setting_pw_wrong:
self.set_result.emit(-3)
else:
self.set_result.emit(len(self.rcv_list[0]))
else:
self.set_result.emit(-1)
elif self.opcode == Opcode.OP_FWUP:
return self.reply
# sys.stdout.write("%s\r\n" % self.mac_list)
except Exception as e:
self.logger.error(f'[ERROR] WIZMSGHandler error: {e}')
class DataRefresh(QThread):
resp_check = pyqtSignal(int)
def __init__(self, sock, cmd_list, what_sock, interval):
QThread.__init__(self)
self.logger = logger
self.sock = sock
self.msg = bytearray(PACKET_SIZE)
self.size = 0
self.inputs = [self.sock.sock]
self.outputs = []
self.errors = []
self.iter = 0
self.dest_mac = None
self.reply = ''
self.mac_list = []
self.rcv_list = []
self.what_sock = what_sock
self.cmd_list = cmd_list
self.interval = interval * 1000
def makecommands(self):
self.size = 0
for cmd in self.cmd_list:
self.msg[self.size:] = str.encode(cmd[0])
self.size += len(cmd[0])
if cmd[0] == "MA":
cmd[1] = cmd[1].replace(":", "")
hex_string = codecs.decode(cmd[1], 'hex')
self.msg[self.size:] = hex_string
self.dest_mac = hex_string
self.size += 6
else:
self.msg[self.size:] = str.encode(cmd[1])
self.size += len(cmd[1])
if "\r\n" not in cmd[1]:
self.msg[self.size:] = str.encode("\r\n")
self.size += 2
def sendcommands(self):
self.sock.sendto(self.msg)
def sendcommandsTCP(self):
self.sock.write(self.msg)
def run(self):
try:
self.makecommands()
if self.what_sock == 'udp':
self.sendcommands()
elif self.what_sock == 'tcp':
self.sendcommandsTCP()
except Exception as e:
self.logger.error(str(e))
# replylists = None
checknum = 0
try:
while True:
self.rcv_list = []
readready, writeready, errorready = select.select(
self.inputs, self.outputs, self.errors, 2)
self.iter += 1
# sys.stdout.write("iter count: %r " % self.iter)
for sock in readready:
self.logger.info(f'DataRefresh: {checknum}')
if sock == self.sock.sock:
data = self.sock.recvfrom()
self.rcv_list.append(data) # 수신 데이터 저장
# replylists = data.splitlines()
# replylists = data.split(b"\r\n")
# print('replylists', replylists)
checknum += 1
self.resp_check.emit(checknum)
if self.interval == 0:
break
else:
self.msleep(self.interval)
self.sendcommands()
except Exception as e:
self.logger.error(f'[ERROR] DataRefresh error: {e}')