-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacket.py
More file actions
165 lines (125 loc) · 4.6 KB
/
packet.py
File metadata and controls
165 lines (125 loc) · 4.6 KB
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
import enum
from abc import abstractclassmethod
from datetime import datetime, date
from serial import Serial
SXT = 0x02
EXT = 0x03
DELIM = 0x1C
class Value:
def __init__(self, source):
self.source = source
def bool(self, bit):
return int(chr(self.source)) & 1 << bit
def string(self):
return "".join(chr(d) for d in self.source)
def int(self):
return int(chr(self.source[0]))
def date(self):
return datetime.strptime(self.string(), "%d%m%y")
def time(self):
return datetime.strptime(self.string(), "%H%M%S")
class Output:
def __init__(self, code: int):
self.code = code
self.params = []
def add_param(self, value) -> 'Output':
if type(value) == str:
self.params.append(x for x in value.encode("CP866"))
elif type(value) == bool:
if value:
self.params.append(0x31)
else:
self.params.append(0x30)
elif type(value) == datetime:
self.params.append(ord(x) for x in value.strftime("%d%m%y"))
self.params.append(ord(x) for x in value.strftime("%H%M%S"))
else:
self.params.append(value)
return self
def get_bytes(self, password, id):
result = [SXT]
result.extend(ord(x) for x in password)
result.append(id)
result.extend(ord(x) for x in "%02X" % self.code)
for param in self.params:
if type(param) == int:
result.append(param)
elif type(param) == str:
result.extend(ord(x) for x in password)
else:
result.extend(param)
result.append(DELIM)
result.append(EXT)
crc = 0
for x in result[1:]:
crc ^= x
for x in (ord(x) for x in str("%02x" % crc)):
result.append(x)
return result
class Input:
def __init__(self, port: Serial):
self.__port: Serial = port
self.__buffer = []
if ord(self.__read(1)) != SXT:
raise Exception("Wrong start byte")
self.id = ord(self.__read(1))
self.code = int("0x" + "".join(chr(x) for x in self.__read(2)), 16)
self.error = int("0x" + "".join(chr(x) for x in self.__read(2)), 16)
self.data = []
current = ord(self.__read(1))
element = []
while current != EXT:
if current == DELIM:
self.data.append(element)
element = []
else:
element.append(current)
current = ord(self.__read(1))
crc = 0
for x in self.__buffer[1:]:
crc ^= x
packet_crc = int("0x" + "".join(chr(x) for x in self.__read(2)), 16)
if crc != packet_crc:
raise Exception("Wrong CRC!")
def get_error(self):
if self.error == 0:
return None
if self.error == 3:
return "Неверный формат команды"
def value(self, index):
return Value(self.data[index])
def to_bool(self, index):
return self.to_string(index) == '1'
def to_string(self, index):
return bytearray(self.data[index]).decode("cp866")
def to_int(self, index):
return int(self.to_string(index))
def to_date(self, index):
return datetime.strptime(self.to_string(index), "%d%m%y")
def to_time(self, index):
return datetime.strptime(self.to_string(index), "%H%M%S")
def to_datetime(self, date_index, time_index):
if self.to_string(date_index) == "000000":
return datetime.min
return datetime.strptime(self.to_string(date_index)+self.to_string(time_index), "%d%m%y%H%M%S")
def __read(self, size: int):
result = self.__port.read(size)
self.__buffer.extend(result)
return result
def __str__(self):
s = "{ id:%i code:%02x error:%i} raw [%s]\n" % (self.id, self.code, self.error, " ".join("%02x" % x for x in self.__buffer))
for d in self.data:
s = s + " ".join("%02x" % x for x in d) + "\n"
return s
class Command:
def __init__(self, code: int, params: [], answer=None):
self.output = Output(code)
for param in params:
self.output.add_param(param)
def value(self, packet):
return None
class AnswerFNVersion(Command):
def __init__(self):
Command.__init__(self, 0x78, [14])
def value(self, packet):
pass