forked from cndingtek/NB-IoT_Gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
df400.py
97 lines (86 loc) · 4.37 KB
/
df400.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 json
from utility import utility
class DF400(object):
# Func: parse data to attr
# Param: req_data: input data string in upper format
# attr_result: output attr
# token_id: token for thingsboard, imei
# "800040021C015A00100000640000804FC40001186105004544355681" heart beat/alarm
# "" Param
def parse_data_DF400(req_data):
try:
data_type = req_data[6:8]
data_len = int(req_data[8:10], 16)
global attr_result
global token_id
if data_len == len(req_data) / 2:
if (data_type == "01" or data_type == "02") and (data_len == 28):
token_id = req_data[39:54]
data_volt = int(req_data[10:14], 16) / 100
data_volt_status = int(req_data[14:16], 16)
data_temperature = int(req_data[16:18], 16)
data_height = int(req_data[18:22], 16)
data_height_percent = int(req_data[22:24], 16)
data_height_status = int(req_data[24:26], 16)
data_rsrp_origin = req_data[26:34]
data_rsrp = int(utility.IEEE754_Hex_To_Float(data_rsrp_origin))
# data_timestamp=int(req_data[46:54],16)
data_frame_counter = int(req_data[34:38], 16)
# print("rsrp is "+str(data_rsrp))
attribute = {
"volt": data_volt,
"volt status": data_volt_status,
"temperature": data_temperature,
"height": data_height,
"height_percent": data_height_percent,
"height_status": data_height_status,
"rsrp": data_rsrp,
"frame_counter": data_frame_counter,
}
else:
token_id = req_data[23:38]
data_version = (
int(req_data[10:12], 16) + int(req_data[12:14], 16) / 100
)
data_upload_interval = int(req_data[14:16], 16)
data_detect_interval = int(req_data[16:18], 16)
data_battery_alarm_threshold = int(req_data[18:20], 16)
data_height_percent_alarm_threshold = int(req_data[20:22], 16)
data_server_mode = int(req_data[38:40], 16)
index_server_one = req_data.find("3B")
str_server1 = req_data[40:index_server_one]
str_asc_server1 = utility.hex_to_ascii(str_server1)
new_req_data = req_data[index_server_one + 2 : -1]
index_server_two = new_req_data.find("3B")
str_port1 = new_req_data[0:index_server_two]
port1_number = int(str_port1, 16)
new_req_data = new_req_data[index_server_two + 2 : -1]
index_server_three = new_req_data.find("3B")
str_server2 = new_req_data[0:index_server_three]
str_asc_server2 = utility.hex_to_ascii(str_server2)
new_req_data = new_req_data[index_server_three + 2 : -1]
index_server_four = new_req_data.find("3B")
str_port2 = new_req_data[0:index_server_four]
port2_number = int(str_port2, 16)
attribute = {
"version": data_version,
"upload interval": data_upload_interval,
"detect interval": data_detect_interval,
"battery_alarm_threshold": data_battery_alarm_threshold,
"height_percent_alarm_threshold": data_height_percent_alarm_threshold,
"server mode": data_server_mode,
"server IP1": str_asc_server1,
"server Port1": port1_number,
"server IP2": str_asc_server2,
"server Port2": port2_number,
}
attr_result = json.dumps(attribute)
else:
attr_result = json.dumps("")
token_id = ""
# str_org=str(byte_org)
except Exception as e:
print(e)
# log.logger.exception(e)
finally:
return attr_result, token_id