-
Notifications
You must be signed in to change notification settings - Fork 0
/
openDTU.py
93 lines (80 loc) · 4.36 KB
/
openDTU.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
# openDTU client
# polling the info
import requests
from lib.measurementlist import MeasurementList
from lib.logger import Logger
logger = Logger()
def access_nested_dict(data, keys):
try:
value = data
for key in keys:
value = value[key]
return value
except (KeyError, TypeError):
# Handle cases where keys are not found or the structure is not a dictionary
return None
class OpenDTU:
def __init__(self, _ip_address):
self.ip_address = _ip_address
self.dat = MeasurementList()
self.dat.add_item(name="HOY Garage N Power",unit="W",send_min_diff=2, filter_time=60,filter_jump=1000,source=("112183227643","AC","0","Power","v"),filter_std_time=0)
self.dat.add_item(name="HOY Garage N Temp", unit="°C", send_min_diff=0.5, filter_time=60, filter_jump=1000, source=("112183227643", "INV","0","Temperature","v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage N Volt", unit="V", send_min_diff=0.5, filter_time=60, filter_jump=1000, source=("112183227643", "DC", "0", "Voltage", "v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage N Curr", unit="A", send_min_diff=0.1,filter_time=60, filter_jump=1000, source=("112183227643", "DC", "0", "Current", "v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage S Power", unit="W", send_min_diff=2, filter_time=60, filter_jump=1000, source=("112183213297", "AC", "0", "Power", "v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage S Temp", unit="°C", send_min_diff=0.5, filter_time=60, filter_jump=1000, source=("112183213297", "INV","0","Temperature","v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage S Volt", unit="V", send_min_diff=0.5, filter_time=60, filter_jump=1000, source=("112183213297", "DC", "0", "Voltage", "v"), filter_std_time=0)
self.dat.add_item(name="HOY Garage S Curr", unit="A", send_min_diff=0.1, filter_time=60, filter_jump=1000, source=("112183213297", "DC", "0", "Current", "v"), filter_std_time=0)
def update(self):
url = f"http://{self.ip_address}/api/livedata/status"
# some brain decided, that every converter must be polled individually. /api/livedata/status?inv=inverter-serialnumber
try:
# Send a GET request to the URL
response = requests.get(url, timeout= 5)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response as a dictionary
data = response.json()
#logger.log("JSON Response:")
#logger.log(data)
if data is None:
logger.log("DTU response is none")
return
else:
logger.log(f"DTU Failed to retrieve data. Status code: {response.status_code}")
response = None
return
except requests.exceptions.RequestException as e:
logger.log(f"DTU Request error: {e}")
return
except ValueError as e:
logger.log(f"DTU JSON decoding error: {e}")
return
# get the data for each inverter and hack them into the tree.
inverters = data["inverters"]
inverters_all = inverters
for inverter in inverters:
invser = inverter.get("serial")
try:
response = requests.get(url + "?inv=" + invser, timeout=5)
data = response.json()
# merge data into original tree
for i, da in enumerate(inverters_all):
if da["serial"] == inverter["serial"]:
inverters_all[i] = data["inverters"][0].copy()
except requests.exceptions.RequestException as e:
logger.log(f"DTU Request detail error: {e}")
return
# and now check for the values
for item in self.dat.get_name_list():
ser = self.dat.get_source(item)[0]
keys = self.dat.get_source(item)[1:]
for inverter in inverters_all:
if inverter["reachable"] == True and ser == inverter["serial"]:
nd = access_nested_dict(inverter,keys)
self.dat.update_value(item,nd)
self.dat.write_measurements()
if __name__ == "__main__":
# Example usage:
dtu = OpenDTU("192.168.1.69")
dtu.update()