-
Notifications
You must be signed in to change notification settings - Fork 38
/
example_influxdb.py
executable file
·145 lines (119 loc) · 5.43 KB
/
example_influxdb.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
#!/usr/bin/env python3
import argparse
import sys
import time
from influxdb import InfluxDBClient
import requests
import solaredge_modbus
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("host", type=str, help="Modbus TCP address")
argparser.add_argument("port", type=int, help="Modbus TCP port")
argparser.add_argument("--timeout", type=int, default=1, help="Connection timeout")
argparser.add_argument("--unit", type=int, default=1, help="Modbus device address")
argparser.add_argument("--interval", type=int, default=10, help="Update interval")
argparser.add_argument("--influx_host", type=str, default="localhost", help="InfluxDB host")
argparser.add_argument("--influx_port", type=int, default=8086, help="InfluxDB port")
argparser.add_argument("--influx_db", type=str, default="solaredge", help="InfluxDB database")
argparser.add_argument("--influx_user", type=str, help="InfluxDB username")
argparser.add_argument("--influx_pass", type=str, help="InfluxDB password")
args = argparser.parse_args()
try:
if args.influx_user and args.influx_pass:
client = InfluxDBClient(
host=args.influx_host,
port=args.influx_port,
username=args.influx_user,
password=args.influx_pass
)
else:
client = InfluxDBClient(host=args.influx_host, port=args.influx_port)
client.switch_database(args.influx_db)
except (ConnectionRefusedError, requests.exceptions.ConnectionError):
print(f"database connection failed: {args.influx_host,}:{args.influx_port}/{args.influx_db}")
sys.exit()
inverter = solaredge_modbus.Inverter(
host=args.host,
port=args.port,
timeout=args.timeout,
unit=args.unit
)
while True:
values = {}
values = inverter.read_all()
meters = inverter.meters()
batteries = inverter.batteries()
json_body = []
current_time = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
inverter_data = {
"measurement": "inverter",
"tags": {
"c_manufacturer": values["c_manufacturer"],
"c_model": values["c_model"],
"c_version": values["c_version"],
"c_serialnumber": values["c_serialnumber"],
"c_deviceaddress": values["c_deviceaddress"],
"c_sunspec_did": values["c_sunspec_did"]
},
"time": current_time,
"fields": {}
}
for k, v in values.items():
if (isinstance(v, int) or isinstance(v, float)) and "_scale" not in k:
k_split = k.split("_")
scale = 0
if f"{k_split[len(k_split) - 1]}_scale" in values:
scale = values[f"{k_split[len(k_split) - 1]}_scale"]
elif f"{k}_scale" in values:
scale = values[f"{k}_scale"]
inverter_data["fields"].update({k: float(v * (10 ** scale))})
json_body.append(inverter_data)
for meter, params in meters.items():
meter_values = params.read_all()
meter_data = {
"measurement": "meter",
"tags": {
"c_manufacturer": meter_values["c_manufacturer"],
"c_model": meter_values["c_model"],
"c_option": meter_values["c_option"],
"c_version": meter_values["c_version"],
"c_serialnumber": meter_values["c_serialnumber"],
"c_deviceaddress": meter_values["c_deviceaddress"],
"c_sunspec_did": meter_values["c_sunspec_did"]
},
"time": current_time,
"fields": {}
}
for k, v in meter_values.items():
if (isinstance(v, int) or isinstance(v, float)) and "_scale" not in k:
k_split = k.split("_")
scale = 0
if f"{k_split[len(k_split) - 1]}_scale" in meter_values:
scale = meter_values[f"{k_split[len(k_split) - 1]}_scale"]
elif f"{k}_scale" in meter_values:
scale = meter_values[f"{k}_scale"]
meter_data["fields"].update({k: float(v * (10 ** scale))})
json_body.append(meter_data)
for battery, params in batteries.items():
battery_values = params.read_all()
if not battery_values["c_model"]:
continue
battery_data = {
"measurement": "battery",
"tags": {
"c_manufacturer": battery_values["c_manufacturer"],
"c_model": battery_values["c_model"],
"c_version": battery_values["c_version"],
"c_serialnumber": battery_values["c_serialnumber"],
"c_deviceaddress": battery_values["c_deviceaddress"],
"c_sunspec_did": battery_values["c_sunspec_did"]
},
"time": current_time,
"fields": {}
}
for k, v in battery_values.items():
if isinstance(v, int) or isinstance(v, float):
battery_data["fields"].update({k: v})
json_body.append(battery_data)
client.write_points(json_body)
time.sleep(args.interval)