forked from blakadder/nspanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nspanel-decode.py
97 lines (82 loc) · 2.06 KB
/
nspanel-decode.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
import binascii
import struct
import codecs
print("Calculate NSPanel hex command from JSON payload\n")
value = input("Enter JSON:\n")
def findtype(value):
if "relation" in value:
# 86
type = 134
elif "ATC" in value:
# 84
type = 132
elif "outlet" in value:
# 87
type = 135
elif "HMI_dimOpen" in value:
# 87
type = 135
elif "index" in value:
# 86
type = 134
elif 'id":"' in value:
# 86
type = 134
elif "params" in value:
# 86
type = 134
elif "wifiState" in value:
# 85
type = 133
elif "HMI_resources" in value:
# 84
type = 132
elif "resourcetype" in value:
# 84
type = 132
elif "temp" in value:
# 83
type = 131
elif "year" in value:
# 82
type = 130
elif "weather" in value:
# 81
type = 129
elif "queryInfo" in value:
# 80
type = 128
elif "ctype" in value:
# 80
type = 132
else:
print("Type not found")
type = 0
return type
def crc16(data:bytes, poly:hex=0xA001) -> str:
'''
CRC-16 MODBUS HASHING ALGORITHM
'''
crc = 0xFFFF
for b in data:
crc ^= b
for _ in range(8):
crc = ((crc >> 1) ^ poly
if (crc & 0x0001)
else crc >> 1)
return crc
print("ns_type:", findtype(value))
json_payload = bytes(value, 'ascii')
header = binascii.unhexlify('55AA')
btype = binascii.unhexlify('86')
nsp_type = (findtype(value)).to_bytes(1, 'big')
print("length:", len(value))
length = len(value).to_bytes(2, 'little')
bytes_payload = header + nsp_type + length + json_payload
#print("bytes_payload:", bytes_payload)
msg_crc = crc16(bytes_payload)
#print('{:04x}'.format(msg_crc))
crc=struct.pack('H', msg_crc)
command=binascii.hexlify(bytes_payload + crc)
print("\n\n")
print(bytes.decode(command))