-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmonitor.py
More file actions
58 lines (45 loc) · 1.61 KB
/
monitor.py
File metadata and controls
58 lines (45 loc) · 1.61 KB
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
from influxdb import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS
import os
from ups import UPS, must_ep3000, must_pv1800, must_ph18_5248
SUPPORTED_INVERTERS = {
"must-pv1800": must_pv1800.MustPV1800,
"must-ep3000": must_ep3000.MustEP3000,
"must-ph18-5248": must_ph18_5248.MustPH185248
}
USB_DEVICE = os.environ.get("USB_DEVICE", "/dev/ttyUSB0")
DB_HOST = os.environ.get("DB_HOST", "influxdb")
DB_PORT = int(os.environ.get("DB_PORT", "8086"))
DB_USERNAME = os.environ.get("DB_USERNAME", "root")
DB_PASSWORD = os.environ.get("DB_PASSWORD", "root")
DB_NAME = os.environ.get("DB_NAME", "ups")
INVERTER_MODEL = os.environ.get("INVERTER_MODEL", "monitor-pv1800")
client = InfluxDBClient(DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME)
if INVERTER_MODEL not in SUPPORTED_INVERTERS:
print("Unknown inverter model model: {0}".format(INVERTER_MODEL))
exit(1)
inverter: UPS = SUPPORTED_INVERTERS[INVERTER_MODEL](USB_DEVICE)
sample = inverter.sample()
print("Measured: {0}".format(sample))
json_body = [
{
"measurement": "logs",
"tags": {
"host": INVERTER_MODEL,
"state": sample.state
},
"fields": {
"bat_volts": sample.bat_volts,
"bat_amps": sample.bat_amps,
"soc": sample.soc,
"ac": sample.ac,
"load_percent": sample.load_percent,
"output_va": sample.output_va,
"output_w": sample.output_w,
"temp": sample.temp,
"discharge": sample.discharge
}
}
]
print(json_body)
client.write_points(json_body)