frame.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. # C:\Users\chx_s\AppData\Local\Programs\Python\Python310\Scripts\pip.exe
  2. # C:\Users\chx_s\AppData\Local\Programs\Python\Python310\python.exe
  3. import sys
  4. import struct
  5. from binascii import *
  6. # 临时注释掉crcmod导入,用于测试弹窗功能
  7. from crcmod import *
  8. import random
  9. import re
  10. # bytes解包
  11. def my_unpack(s, data, i):
  12. global info_msg
  13. global error_msg
  14. temp_msg = ""
  15. if type(s) == str:
  16. np = struct.calcsize(s)
  17. try:
  18. val = struct.unpack(s, data[i:(i + np)])
  19. return i + np, val[0]
  20. except:
  21. temp_msg = "fun:" + sys._getframe().f_code.co_name + \
  22. ",line:" + f"{sys._getframe().f_lineno}." + \
  23. f"\n出错了!!!,s={s},i={i},data={data}"
  24. error_msg = error_msg + temp_msg
  25. info_msg = info_msg + temp_msg
  26. return i + np, 0
  27. elif type(s) == int:
  28. val = data[i:(i + s)]
  29. return i + s, val
  30. def convert_to_24byte_bytes(s):
  31. """将字符串转换为24字节的bytes,不足补0x00"""
  32. bytes_data = s.encode('utf-8')[:24] # 取前24字节
  33. return bytes_data.ljust(24, b'\x00') # 不足24字节用0x00填充
  34. def get_msg_restart(device, data):
  35. print(f"this is get_msg_restart:")
  36. # body
  37. pub_body = bytearray(struct.pack("H", 0x2006)) # 注意 2006 是指十六进制 0x2006
  38. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(device['dev_sn'])))) # 注意 103 是指十进制 103
  39. # add something
  40. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  41. # header
  42. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  43. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  44. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  45. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 一级类型
  46. pub_header = pub_header + bytearray(struct.pack("H", 0x1021)) # 二级类型
  47. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  48. # 重新结算长度
  49. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  50. # 添加crc16
  51. pub_frame = pub_header + pub_body
  52. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  53. crc16 = crc16mod(pub_frame)
  54. # 完整消息
  55. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  56. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  57. print(f"pub_frame=%s\n" % pub_frame.hex().upper())
  58. return pub_frame
  59. def get_msg_upgrade_1001(dev_type,dev_sn):
  60. print(f"this is get_msg_upgrade_1001:")
  61. # body
  62. pub_body = bytearray(struct.pack("H", (int(dev_type,16)))) # 注意 2006 是指十六进制 0x2006
  63. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(dev_sn)))) # 注意 103 是指十进制 103
  64. pub_body = pub_body + bytearray(struct.pack("H", (int(dev_type,16)))) # 注意 2006 是指十六进制 0x2006
  65. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(dev_sn)))) # 注意 103 是指十进制 103
  66. pub_body = pub_body + bytearray(struct.pack("H", (int(dev_type,16)))) # 注意 2006 是指十六进制 0x2006
  67. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(dev_sn)))) # 注意 103 是指十进制 103
  68. pub_body = pub_body + bytearray(struct.pack("H", 0x1234))
  69. pub_body = pub_body + bytearray(struct.pack("B", 0x03))
  70. url = "http://123.bin"
  71. pub_body = pub_body + bytearray(struct.pack("B", len(url)))
  72. pub_body = pub_body + bytearray(url.encode())
  73. # add something
  74. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  75. # header
  76. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  77. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  78. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  79. pub_header = pub_header + bytearray(struct.pack("B", 0x04)) # 一级类型
  80. pub_header = pub_header + bytearray(struct.pack("H", 0x1001)) # 二级类型
  81. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  82. # 重新结算长度
  83. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  84. # 添加crc16
  85. pub_frame = pub_header + pub_body
  86. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  87. crc16 = crc16mod(pub_frame)
  88. # 完整消息
  89. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  90. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  91. return pub_frame
  92. def get_msg_upgrade_1030(dev_type, dev_sn, fileSum, maxPkgId, curPkgId, curPkgsize, data):
  93. print(f"this is get_msg_upgrade_1030:")
  94. # body
  95. pub_body = bytearray(struct.pack("H", int(dev_type,16))) # 注意 2006 是指十六进制 0x2006
  96. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(dev_sn)))) # 注意 103 是指十进制 103
  97. pub_body = pub_body + bytearray(struct.pack("B", 0x03))
  98. pub_body = pub_body + bytearray(struct.pack("H", 0x1234))
  99. pub_body = pub_body + bytearray(struct.pack("I", fileSum))
  100. pub_body = pub_body + bytearray(struct.pack("H", maxPkgId))
  101. pub_body = pub_body + bytearray(struct.pack("H", curPkgId))
  102. pub_body = pub_body + bytearray(struct.pack("I", 0))
  103. pub_body = pub_body + bytearray(struct.pack("H", curPkgsize))
  104. # add something
  105. pub_body = pub_body + bytearray(data)
  106. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  107. # header
  108. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  109. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  110. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  111. pub_header = pub_header + bytearray(struct.pack("B", 0x04)) # 一级类型
  112. pub_header = pub_header + bytearray(struct.pack("H", 0x1030)) # 二级类型
  113. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  114. # 重新结算长度
  115. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  116. # 添加crc16
  117. pub_frame = pub_header + pub_body
  118. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  119. crc16 = crc16mod(pub_frame)
  120. # 完整消息
  121. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  122. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  123. return pub_frame
  124. def get_msg_config_wifi(device, data):
  125. print(f"this is get_msg_config_wifi:")
  126. pub_body = bytearray(struct.pack("B", 0x02)) # WIFI配置文件编号索引
  127. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 仅配置WIFI
  128. pub_body = pub_body + bytearray(struct.pack("H", 1)) # 2.4G信道
  129. pub_body = pub_body + bytearray(struct.pack("H", 36)) # 5G信道
  130. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 使能5G
  131. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 功率等级
  132. pub_body = pub_body + bytearray([0x43, 0x4E, 0x00]) # CN国家代码
  133. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_ssid","未知")) # WIFI名称
  134. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # WIFI加密类型
  135. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_password","未知")) # WIFI密码
  136. if device.get("network_type","未知") == "WiFi":
  137. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 网络类型 0=未知,1=WIFI,2=有线网
  138. elif device.get("network_type","未知") == "有线网":
  139. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 网络类型 0=未知,1=WIFI,2=有线网
  140. else:
  141. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 网络类型 0=未知,1=WIFI,2=有线网
  142. if device.get("cloud_platform","未知") == "阿里云生产":
  143. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  144. elif device.get("cloud_platform","未知") == "测试环境":
  145. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  146. else:
  147. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  148. pub_body = pub_body + bytearray(struct.pack("H", 0x00)) # 预留
  149. # body
  150. # pub_body = bytearray(struct.pack("H", 0x2006)) # 注意 2006 是指十六进制 0x2006
  151. # pub_body = pub_body + bytearray(bytes(struct.pack("I", int(device['dev_sn'])))) # 注意 103 是指十进制 103
  152. # add something
  153. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  154. # header
  155. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  156. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  157. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  158. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 一级类型
  159. pub_header = pub_header + bytearray(struct.pack("H", 0x1006)) # 二级类型
  160. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  161. # 重新结算长度
  162. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  163. # 添加crc16
  164. pub_frame = pub_header + pub_body
  165. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  166. crc16 = crc16mod(pub_frame)
  167. # 完整消息
  168. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  169. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  170. print(f"pub_frame=%s\n" % pub_frame.hex().upper())
  171. return pub_frame
  172. def get_msg_config_platform(device, data):
  173. print(f"this is get_msg_config_platform:")
  174. # body
  175. pub_body = bytearray(struct.pack("B", 0x02)) # WIFI配置文件编号索引
  176. pub_body = pub_body + bytearray(struct.pack("B", 0x03)) # 仅配置平台
  177. pub_body = pub_body + bytearray(struct.pack("H", 1)) # 2.4G信道
  178. pub_body = pub_body + bytearray(struct.pack("H", 36)) # 5G信道
  179. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 使能5G
  180. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 功率等级
  181. pub_body = pub_body + bytearray([0x43, 0x4E, 0x00]) # CN国家代码
  182. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_ssid","未知")) # WIFI名称
  183. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # WIFI加密类型
  184. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_password","未知")) # WIFI密码
  185. if device.get("network_type","未知") == "WiFi":
  186. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 网络类型 0=未知,1=WIFI,2=有线网
  187. elif device.get("network_type","未知") == "有线网":
  188. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 网络类型 0=未知,1=WIFI,2=有线网
  189. else:
  190. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 网络类型 0=未知,1=WIFI,2=有线网
  191. if data == "阿里云生产":
  192. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  193. elif data == "测试环境":
  194. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  195. else:
  196. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  197. pub_body = pub_body + bytearray(struct.pack("H", 0x00)) # 预留
  198. # pub_body = pub_body + bytearray(struct.pack("H", 0x2006))
  199. # pub_body = pub_body + bytearray(bytes(struct.pack("I", int(device['dev_sn'])))) # 注意 103 是指十进制 103
  200. # add something
  201. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  202. # header
  203. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  204. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  205. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  206. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 一级类型
  207. pub_header = pub_header + bytearray(struct.pack("H", 0x1006)) # 二级类型
  208. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  209. # 重新结算长度
  210. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  211. # 添加crc16
  212. pub_frame = pub_header + pub_body
  213. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  214. crc16 = crc16mod(pub_frame)
  215. # 完整消息
  216. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  217. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  218. print(f"pub_frame=%s\n" % pub_frame.hex().upper())
  219. return pub_frame
  220. def get_msg_config_network(device, data):
  221. print(f"this is get_msg_config_network:")
  222. pub_body = bytearray(struct.pack("B", 0x02)) # WIFI配置文件编号索引
  223. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 仅配置设备
  224. pub_body = pub_body + bytearray(struct.pack("H", 1)) # 2.4G信道
  225. pub_body = pub_body + bytearray(struct.pack("H", 36)) # 5G信道
  226. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 使能5G
  227. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 功率等级
  228. pub_body = pub_body + bytearray([0x43, 0x4E, 0x00]) # CN国家代码
  229. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_ssid","未知")) # WIFI名称
  230. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # WIFI加密类型
  231. pub_body = pub_body + convert_to_24byte_bytes(device.get("wifi_password","未知")) # WIFI密码
  232. if device.get("network_type","未知") == "WiFi":
  233. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 网络类型 0=未知,1=WIFI,2=有线网
  234. elif device.get("network_type","未知") == "有线网":
  235. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 网络类型 0=未知,1=WIFI,2=有线网
  236. else:
  237. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 网络类型 0=未知,1=WIFI,2=有线网
  238. if device.get("cloud_platform","未知") == "阿里云生产":
  239. pub_body = pub_body + bytearray(struct.pack("B", 0x01)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  240. elif device.get("cloud_platform","未知") == "测试环境":
  241. pub_body = pub_body + bytearray(struct.pack("B", 0x02)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  242. else:
  243. pub_body = pub_body + bytearray(struct.pack("B", 0x00)) # 云平台 0=未知,1=阿里云生产,2=测试环境
  244. pub_body = pub_body + bytearray(struct.pack("H", 0x00)) # 预留
  245. # body
  246. # pub_body = bytearray(struct.pack("H", 0x2006)) # 注意 2006 是指十六进制 0x2006
  247. # pub_body = pub_body + bytearray(bytes(struct.pack("I", int(device['dev_sn'])))) # 注意 103 是指十进制 103
  248. # add something
  249. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  250. # header
  251. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  252. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  253. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  254. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 一级类型
  255. pub_header = pub_header + bytearray(struct.pack("H", 0x1006)) # 二级类型
  256. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body) + 2))) # 消息长度
  257. # 重新结算长度
  258. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  259. # 添加crc16
  260. pub_frame = pub_header + pub_body
  261. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  262. crc16 = crc16mod(pub_frame)
  263. # 完整消息
  264. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  265. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  266. print(f"pub_frame=%s\n" % pub_frame.hex().upper())
  267. return pub_frame
  268. def get_msg_config_all(device,data):
  269. print(f"this is get_msg_config_all:")
  270. # body
  271. pub_body = bytearray(struct.pack("H", 0x2006)) # 注意 2006 是指十六进制 0x2006
  272. pub_body = pub_body + bytearray(bytes(struct.pack("I", int(device['dev_sn'])))) # 注意 103 是指十进制 103
  273. # add something
  274. # pub_header = bytearray.fromhex("FEFE01390100000363206A00")
  275. # header
  276. pub_header = bytearray(struct.pack("H", 0xFEFE)) # 协议头
  277. pub_header = pub_header + bytearray(struct.pack("B", 0x01)) # 协议版本
  278. pub_header = pub_header + bytearray(struct.pack("I", random.randint(0, 1000000000))) # 消息ID
  279. pub_header = pub_header + bytearray(struct.pack("B", 0x03)) # 一级类型
  280. pub_header = pub_header + bytearray(struct.pack("H", 0x2001)) # 二级类型
  281. pub_header = pub_header + bytearray(struct.pack("H", (len(pub_body)+2))) # 消息长度
  282. # 重新结算长度
  283. # pub_header[10:12] = bytearray(struct.pack("H", (len(pub_body))))
  284. # 添加crc16
  285. pub_frame = pub_header + pub_body
  286. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  287. crc16 = crc16mod(pub_frame)
  288. # 完整消息
  289. pub_frame = pub_frame + bytearray(struct.pack("<H", crc16))
  290. print(f"\nlen(pub_frame)=%d," % len(pub_frame))
  291. print(f"pub_frame=%s\n" % pub_frame.hex().upper())
  292. return pub_frame
  293. info_msg = ""
  294. error_msg = ""
  295. def parse_data(client, data, topic):
  296. global info_msg
  297. global error_msg
  298. header = {}
  299. body = {}
  300. flag = True
  301. while flag:
  302. info_msg = ""
  303. error_msg = ""
  304. temp_msg = ""
  305. crc16mod = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0x0000, xorOut=0000)
  306. crc16 = crc16mod(data[:-2])
  307. crc16_f = struct.unpack("H",data[-2:])[0]
  308. if crc16 != crc16_f :
  309. temp_msg = sys._getframe().f_code.co_name + \
  310. f"\nerror:crc16 != data[-2:]\n"
  311. error_msg = error_msg + temp_msg
  312. info_msg = info_msg + temp_msg
  313. flag = False
  314. break
  315. if len(data) < 12:
  316. temp_msg = sys._getframe().f_code.co_name + \
  317. f"\nerror:len(data) < 12\n"
  318. error_msg = error_msg + temp_msg
  319. info_msg = info_msg + temp_msg
  320. flag = False
  321. break
  322. temp = 0
  323. info_msg = "解析结果:"
  324. cmd = {}
  325. np = 0
  326. np, header['start'] = my_unpack("<H", data, np)
  327. np, header['pro_ver'] = my_unpack("B", data, np)
  328. np, header['msg_id'] = my_unpack("<I", data, np)
  329. np, header['msg_type1'] = my_unpack("B", data, np)
  330. np, header['msg_type2'] = my_unpack("<H", data, np)
  331. np, header['msg_Len'] = my_unpack("<H", data, np)
  332. info_msg = info_msg + f"协议版本:0x%02x," % (header['pro_ver'])
  333. # if header['msg_type1'] == 0x03 and header['msg_type2'] == 0x2051:
  334. # outMsg = outMsg + "\r\n\r\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\r\n\r\n"
  335. # for k, v in header.items():
  336. # outMsg = outMsg + str(k)+':' + ' '*(16-len(k)) + (f"0x%02X" % v) + ' '*(16 - len(f"0x%02X" % v)) + (f"%u" % v)
  337. # outMsg = outMsg + str(k) + ':' + (f"0x%X," % v) + (f"%u" % v)
  338. if header['start'] != 0xFEFE:
  339. temp_msg = "fun:" + sys._getframe().f_code.co_name + \
  340. ",line:" + f"{sys._getframe().f_lineno}." + \
  341. f"\nerror: frame.start"
  342. error_msg = error_msg + temp_msg
  343. info_msg = info_msg + temp_msg
  344. flag = False
  345. break
  346. if header['msg_Len'] + 12 != len(data):
  347. temp_msg = "fun:" + sys._getframe().f_code.co_name + \
  348. ",line:" + f"{sys._getframe().f_lineno}." + \
  349. f"\nerror: frame.len"
  350. error_msg = error_msg + temp_msg
  351. info_msg = info_msg + temp_msg
  352. flag = False
  353. break
  354. if header['pro_ver'] == 0x01:
  355. if header['msg_type1'] == 0x02:
  356. info_msg = info_msg + f",MT1=0x%02X," % (header['msg_type1']) + "状态类"
  357. if header['msg_type2'] == 0x2003:
  358. info_msg = info_msg + f",MT2=0x%04X," % (header['msg_type2']) + "解码板心跳上行"
  359. np, body['master_type'] = my_unpack("<H", data, np);
  360. body['master_type'] = f"%04X" % (body['master_type'])
  361. np, body['master_sn'] = my_unpack("<I", data, np);
  362. body['master_sn'] = f"%u" % (body['master_sn'])
  363. info_msg = info_msg + f",主设备:%s-%s" % (body['master_type'].zfill(4), body['master_sn'].zfill(10))
  364. np, body['slave_type'] = my_unpack("<H", data, np);
  365. body['slave_type'] = f"%04X" % (body['slave_type'])
  366. np, body['slave_sn'] = my_unpack("<I", data, np);
  367. body['slave_sn'] = f"%u" % (body['slave_sn'])
  368. info_msg = info_msg + f",从设备:%s-%s" % (body['slave_type'].zfill(4), body['slave_sn'].zfill(10))
  369. np, body['app_version'] = my_unpack("<I", data, np);
  370. body['app_version'] = f"%08X" % (body['app_version'])
  371. info_msg = info_msg + f",APP版本:%s" % (body['app_version'])
  372. np, body['reset_times'] = my_unpack("<I", data, np);
  373. info_msg = info_msg + f",复位次数:%d" % (body['reset_times'])
  374. np, body['last_reset_type'] = my_unpack("B", data, np);
  375. body['last_reset_type'] = f"%02X" % (body['last_reset_type'])
  376. info_msg = info_msg + f",最后复位类型:0x%s" % (body['last_reset_type'])
  377. np, body['uuid'] = my_unpack(12, data, np);
  378. body['uuid'] = body['uuid'].hex().upper()
  379. info_msg = info_msg + f",uuid:%s" % (body['uuid'])
  380. np, body['run_time'] = my_unpack("<I", data, np);
  381. info_msg = info_msg + f",运行时长:%d秒" % (body['run_time'])
  382. np, body['receive_sum'] = my_unpack("<I", data, np);
  383. info_msg = info_msg + f",接收数据量:%d" % (body['receive_sum'])
  384. np, body['send_sum'] = my_unpack("<I", data, np);
  385. info_msg = info_msg + f",发送数据量:%d" % (body['send_sum'])
  386. np, body['network_type'] = my_unpack("B", data, np);
  387. info_msg = info_msg + f",网络类型:0x%02X" % (body['network_type'])
  388. np, body['cloud_platform'] = my_unpack("B", data, np);
  389. info_msg = info_msg + f",云平台:0x%02X" % (body['cloud_platform'])
  390. np, body['sim_status'] = my_unpack("B", data, np);
  391. info_msg = info_msg + f",SIM卡状态:0x%02X" % (body['sim_status'])
  392. np, body['free_fifo'] = my_unpack("B", data, np);
  393. info_msg = info_msg + f",剩余资源:0x%02X" % (body['free_fifo'])
  394. np, body_ip = my_unpack("16s", data, np);
  395. find_ip = body_ip.rstrip(b'\0').decode('utf-8')
  396. body['ip'] = find_ip
  397. info_msg = info_msg + f",IP地址:%s" % (str(body['ip']))
  398. np, body['reserve'] = my_unpack("<I", data, np);
  399. info_msg = info_msg + f",保留字段:%d" % (body['reserve'])
  400. elif header['msg_type1'] == 0x04:
  401. info_msg = info_msg + f",MT1=0x%02X," % (header['msg_type1']) + "状态类"
  402. if header['msg_type2'] == 0x2001:
  403. info_msg = info_msg + f",MT2=0x%04X," % (header['msg_type2']) + "升级状态上行"
  404. np, body['FirstLevelType'] = my_unpack("<H", data, np);
  405. body['FirstLevelType'] = f"%04X" % (body['FirstLevelType'])
  406. np, body['FirstLevelSn'] = my_unpack("<I", data, np);
  407. body['FirstLevelSn'] = f"%u" % (body['FirstLevelSn'])
  408. info_msg = info_msg + f",一级设备:%s-%s" % (body['FirstLevelType'].zfill(4), body['FirstLevelSn'].zfill(10))
  409. np, body['escalationDevType'] = my_unpack("<H", data, np);
  410. body['escalationDevType'] = f"%04X" % (body['escalationDevType'])
  411. np, body['escalationDevSn'] = my_unpack("<I", data, np);
  412. body['escalationDevSn'] = f"%u" % (body['escalationDevSn'])
  413. info_msg = info_msg + f",目标设备:%s-%s" % (body['escalationDevType'].zfill(4), body['escalationDevSn'].zfill(10))
  414. np, body['firmType'] = my_unpack("B", data, np);
  415. info_msg = info_msg + f",固件类型:0x%02X" % (body['firmType'])
  416. np, body['taskID'] = my_unpack("<H", data, np);
  417. info_msg = info_msg + f",任务ID:%u" % (body['taskID'])
  418. np, body['upgrade_status'] = my_unpack("B", data, np);
  419. info_msg = info_msg + f",升级状态:0x%02X" % (body['upgrade_status'])
  420. np, body['app_Version'] = my_unpack("<I", data, np);
  421. info_msg = info_msg + f",APP版本:0x%08X" % (body['app_Version'])
  422. flag = False
  423. # print(info_msg)
  424. return header, body, info_msg