forked from xiaokaizh/SmartDoorControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandle.py
More file actions
262 lines (235 loc) · 5.99 KB
/
MessageHandle.py
File metadata and controls
262 lines (235 loc) · 5.99 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
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
# coding=utf-8
__author__ = 'Administrator'
import SocketVisitor
# 下行报文设备控制报文的回调
CallbackDeviceControl = {}
# 下行报文猫眼请求的回调
CallbackCatEyeVisitor = {}
# 下行报文误报的回调
CallbackMisInformation = {}
# 下行报文程序状态获取的回调
CallbackGetProgramStatus = {}
# 接收到消息
def MessageRecive(data):
clusid = ord(data[4])
if (clusid == 0x21):
# 设备控制报文
return MessageDeviceControl(data)
else:
if (clusid == 0x22):
# 查看猫眼请求
return MessageCatEyeVisitor(data)
else:
if (clusid == 0x23):
# 误报
return MessageMisInformation(data)
else:
if (clusid == 0x24):
# 程序状态获取
return MessageGetProgramStatus(data)
else:
print("CantRecognizeMessage " + str(ord(data[4])))
return b"abc"
# 设备控制报文
def MessageDeviceControl(data):
print("MessageDeviceControl ")
DeviceCode = ord(data[19])
ControlCode = ord(data[20])
ControlResult = CallbackDeviceControl(DeviceCode, ControlCode)
result = UpMessageDeviceControl(DeviceCode, ControlResult)
result = AddPackgeHead(result)
return result
# 设备控制回复上行报文
def UpMessageDeviceControl(DeviceCode, ControlResult):
result = []
# 簇ID
result.append(0x13)
# 硬件ID
result.append(0x11)
result.append(0x22)
result.append(0x33)
result.append(0x44)
result.append(0x55)
result.append(0x66)
# 客户端ID
result.append(0x66)
result.append(0x55)
result.append(0x44)
result.append(0x33)
result.append(0x22)
result.append(0x11)
# 回复KEY
result.append(0x12)
result.append(0x34)
# 受控硬件码
result.append(DeviceCode)
# 控制结果
result.append(ControlResult)
# 硬件状态
result.append(0x01)
# 预留字段
result.append(0x00)
result.append(0x00)
return result
# 查看猫眼请求
def MessageCatEyeVisitor(data):
print("MessageCatEyeVisitor ")
Result = CallbackCatEyeVisitor()
result = UpMessageCatEyeVisitor()
result = AddPackgeHead(result)
return result
# 查看猫眼回复
def UpMessageCatEyeVisitor():
result = []
# 簇ID
result.append(0x12)
# 硬件ID
result.append(0x11)
result.append(0x22)
result.append(0x33)
result.append(0x44)
result.append(0x55)
result.append(0x66)
# 客户端ID
result.append(0x66)
result.append(0x55)
result.append(0x44)
result.append(0x33)
result.append(0x22)
result.append(0x11)
# 回复KEY
result.append(0x12)
result.append(0x34)
# 预留字段
result.append(0x00)
result.append(0x00)
return result
# 误报
def MessageMisInformation(data):
print("MessageMisInformation ")
AlarmCode = ord(data[21])
Result = CallbackMisInformation(AlarmCode)
result = UpMessageMisInformation(AlarmCode)
result = AddPackgeHead(result)
return result
# 误报上行回复
def UpMessageMisInformation(AlarmCode):
result = []
# 簇ID
result.append(0x14)
# 硬件ID
result.append(0x11)
result.append(0x22)
result.append(0x33)
result.append(0x44)
result.append(0x55)
result.append(0x66)
# 客户端ID
result.append(0x66)
result.append(0x55)
result.append(0x44)
result.append(0x33)
result.append(0x22)
result.append(0x11)
# 回复KEY
result.append(0x12)
result.append(0x34)
# 报警类型
result.append(AlarmCode)
# 预留字段
result.append(0x00)
result.append(0x00)
return result
# 程序状态获取
def MessageGetProgramStatus(data):
print("MessageGetProgramStatus ")
Result = CallbackGetProgramStatus()
result = UpMessageGetProgramStatus()
result = AddPackgeHead(result)
return result
def UpMessageGetProgramStatus():
result = []
# 簇ID
result.append(0x15)
# 硬件ID
result.append(0x11)
result.append(0x22)
result.append(0x33)
result.append(0x44)
result.append(0x55)
result.append(0x66)
# 客户端ID
result.append(0x66)
result.append(0x55)
result.append(0x44)
result.append(0x33)
result.append(0x22)
result.append(0x11)
# 回复KEY
result.append(0x12)
result.append(0x34)
# 状态保温网版本
result.append(0x01)
# 程序版本
result.append(0x01)
result.append(0x01)
# 振动传感器状态机状态
result.append(0x01)
# 红外传感器状态机状态
result.append(0x01)
# 预留字段
result.append(0x00)
result.append(0x00)
return result
# 添加包头
def AddPackgeHead(data):
result = []
# FE
result.append(0xFE)
# 版本
result.append(0x01)
# 字节数
result.append(0x00)
result.append(data.count(data))
# 添加数据包
for byte in data:
result.append(byte)
# 校验和
result.append(0x55)
return result
# 纯上行报警报文 报警码 1为敲门 2为撬门 3为徘徊报警
def CallAlarm(sock, addr, AlarmCode):
result = UpMessageAlarm(AlarmCode)
result = AddPackgeHead(result)
SocketVisitor.UdpSend(sock, addr, result)
def UpMessageAlarm(AlarmCode):
result = []
# 簇ID
result.append(0x11)
# 硬件ID
result.append(0x11)
result.append(0x22)
result.append(0x33)
result.append(0x44)
result.append(0x55)
result.append(0x66)
# 客户端ID
result.append(0x66)
result.append(0x55)
result.append(0x44)
result.append(0x33)
result.append(0x22)
result.append(0x11)
# 报警KEY
result.append(0x12)
result.append(0x34)
# 报警类型
result.append(AlarmCode)
# 预留字段
result.append(0x00)
result.append(0x00)
return result
# 开启监听程序
def linseningStart(sock, addr):
SocketVisitor.MessageRecive = MessageRecive
SocketVisitor.linseningStart(sock, addr)