-
Notifications
You must be signed in to change notification settings - Fork 5
/
requests_util.py
204 lines (178 loc) · 7.42 KB
/
requests_util.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
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
'''
Copyright 2024 philippoo66
Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import utils
import optolinkvs2
import onewire_util
import settings_ini
def get_value(data, frmat, signd:bool) -> any:
scale = utils.to_number(frmat)
if(scale is not None):
return utils.bytesval(data, scale, signd)
else:
#TODO hier evtl weitere Formate umsetzen
if(frmat == 'vdatetime'):
return utils.vdatetime2str(data)
elif(frmat == 'unixtime'):
return utils.unixtime2str(data)
elif(frmat == 'utf8'):
return utils.utf82str(data)
else:
#return raw
return utils.arr2hexstr(data)
def perform_bytebit_filter(data, item):
# item is poll list entry: (Name, DpAddr, Len, 'b:startbyte:lastbyte:bitmask:endian', Scale, Signed)
# may also be read request: ("read; DpAddr; Len; 'b:startbyte:lastbyte:bitmask:endian'; Scale; Signed)
bparts = item[3].split(':')
bstart = int(bparts[1])
bend = bstart
if(len(bparts) > 1):
if(bparts[2] != ''):
bend = int(bparts[2])
udata = data[bstart:(bend+1)]
# first apply mask if given
if(len(bparts) > 3):
if(bparts[3] != ''):
amask = utils.hexstr2arr(str(bparts[3]).replace('0x',''))
for i in range(len(udata)+1):
if(i > len(amask)):
break
udata = udata & amask
endian = 'little' # € ['little, 'big', 'raw']
if(len(bparts) > 4):
if(bparts[4] != ''):
endian = bparts[4]
if(len(item) > 4):
scal = item[4]
if(scal == 'raw'):
endian = 'raw'
else:
scal = None
if(endian == 'raw'):
return utils.arr2hexstr(udata)
else:
signd = False
if(len(item) > 5):
signd = utils.get_bool(item[5])
uvalue = int.from_bytes(udata, byteorder=endian, signed=signd)
if(scal is not None):
scal = float(scal)
if(scal != 1.0):
#print("H round called")
#print(f"{type(uvalue)} {uvalue} {type(scal)} {scal}")
uvalue = round(uvalue * scal, int(settings_ini.max_decimals))
#print("H round done")
return uvalue
def get_retstr(retcode, addr, val) -> str:
prefix = ''
if('x' in settings_ini.resp_addr_format.lower()):
prefix = '0x'
saddr = prefix + format(addr, settings_ini.resp_addr_format)
#retstr = str(retcode) + ';' + str(addr) + ';' + str(val)
return f"{retcode};{saddr};{val}"
# 'main' functions +++++++++++++++++++++++++++++
def response_to_request(request, serViDev) -> tuple[int, bytearray, any, str]: # retcode, data, value, string_to_pass
ispollitem = False
if(isinstance(request, str)):
# TCP, MQTT requests
parts = request.split(';')
else:
# poll item
ispollitem = True
parts = request
numelms = len(parts)
data = bytearray()
val = None
retstr = ''
retcode = 0
if(numelms == 1):
# full raw +++++++++++++++++++ "4105000100F80806"
bstr = bytes.fromhex(parts[0])
serViDev.reset_input_buffer()
serViDev.write(bstr)
#print("sent to OL:", bbbstr(bstr))
data = optolinkvs2.receive_fullraw(settings_ini.fullraw_eot_time,settings_ini.fullraw_timeout, serViDev)
val = utils.arr2hexstr(data)
retstr = str(val)
retcode = 0x01 # attention!
#print("recd fr OL:", bbbstr(data))
elif(numelms > 1):
cmnd = parts[0].lower()
if(cmnd == "raw"): # "raw;4105000100F80806"
# raw +++++++++++++++++++
bstr = bytes.fromhex(parts[1])
serViDev.reset_input_buffer()
serViDev.write(bstr)
#print("sent to OL:", bbbstr(retstr))
retcode, _, data = optolinkvs2.receive_vs2telegr(True, True, serViDev)
#print("recd fr OL:", ret, ',', bbbstr(data))
val = utils.arr2hexstr(data)
retstr = f"{retcode};{val}"
data = bytearray()
elif((cmnd in ["read", "r"]) or ispollitem): # "read;0x0804;1;0.1;False" or "r;0x2500;22;'b:0:1';0.1"
# read +++++++++++++++++++
addr = utils.get_int(parts[1])
if(addr in settings_ini.w1sensors):
# 1wire sensor
retcode, val = onewire_util.read_w1sensor(addr)
else:
# Optolink item
retcode, addr, data = optolinkvs2.read_datapoint_ext(addr, int(parts[2]), serViDev)
if(retcode==1):
if(numelms > 3):
if(str(parts[3]).startswith('b:')):
#print(f"H perform_bytebit_filter, ispollitem {ispollitem}")
val = perform_bytebit_filter(data, parts)
else:
signd = False
if(numelms > 4):
signd = utils.get_bool(parts[4])
val = get_value(data, parts[3], signd)
else:
#return raw
val = utils.arr2hexstr(data)
elif(data):
# probably error message
val = utils.arr2hexstr(data) #f"{int.from_bytes(data, 'little')} ({utils.bbbstr(data)})"
else:
val = "?"
retstr = get_retstr(retcode, addr, val)
elif(cmnd in ["write", "w"]): # "write;0x6300;1;48"
# write +++++++++++++++++++
#raise Exception("write noch nicht fertig") #TODO scaling und so
bval = (utils.get_int(parts[3])).to_bytes(int(parts[2]), 'little')
retcode, addr, data = optolinkvs2.write_datapoint_ext(utils.get_int(parts[1]), bval, serViDev)
if(retcode == 1):
val = int.from_bytes(bval, 'little')
elif(data):
# probably error message
val = utils.arr2hexstr(data) #f"{int.from_bytes(data, 'little')} ({utils.bbbstr(data)})"
else:
val = "?"
retstr = get_retstr(retcode, addr, val)
elif(cmnd in ["writeraw", "wraw"]): # "writeraw;0x6300;2A"
# write raw +++++++++++++++++++
hexstr = str(parts[2]).replace('0x','')
bval = utils.hexstr2arr(hexstr)
retcode, addr, data = optolinkvs2.write_datapoint_ext(utils.get_int(parts[1]), bval, serViDev)
if(retcode == 1):
val = hexstr #int.from_bytes(bval, 'big')
elif(data):
# probably error message
val = utils.arr2hexstr(data) #f"{int.from_bytes(data, 'little')} ({utils.bbbstr(data)})"
else:
val = "?"
retstr = get_retstr(retcode, addr, val)
else:
print("unknown command received:", cmnd)
# and finally return...
return retcode, data, val, retstr