-
Notifications
You must be signed in to change notification settings - Fork 2
/
gridrelay.py
73 lines (57 loc) · 3.06 KB
/
gridrelay.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
import time
from pvinflux import PvInflux
from pvoutputorg import PvOutputOrg
from pvconf import PvConf
from gridkenter import GridKenter
from pvmqtt import PvMqtt
class GridRelay:
def __init__(self, conf: PvConf, logger):
self.conf = conf
self.logger = logger
self.logger.debug("GridRelay class instantiated")
self.gridkenter = GridKenter(conf, logger)
self.pvoutput = PvOutputOrg(conf, logger)
self.pvmqtt = PvMqtt(conf, logger)
self.pvinflux = PvInflux(self.conf, self.logger)
self.pvinflux_initialized = False
self.logger.info("Starting GridRelay on separate thread")
self.start()
def start(self):
self.logger.debug("GridRelay waiting 5sec to initialize docker-compose containers")
time.sleep(5)
daystobackfill = self.conf.gridrelaydaystobackfill
while 1:
try:
for daysback in range(self.conf.gridrelaydaysback, self.conf.gridrelaydaysback + 1 + daystobackfill):
grid_measurement_data = self.gridkenter.fetch_gridkenter_data(self.conf.gridrelaysysname, self.conf.gridrelaykenterean, self.conf.gridrelaykentermeterid, daysback)
self.write_gridkenter_to_influxdb(grid_measurement_data)
self.write_gridkenter_to_pvoutput(grid_measurement_data)
if self.conf.gridrelaysys02enabled:
grid_measurement_data = self.gridkenter.fetch_gridkenter_data(self.conf.gridrelaysysname02, self.conf.gridrelaykenterean02, self.conf.gridrelaykentermeterid02, daysback)
self.write_gridkenter_to_influxdb(grid_measurement_data)
#No support for pvoutput on 2 EAN codes yet (needs summing of kenter data or pvoutput support for 2 distinct systems)
#self.write_gridkenter_to_pvoutput(grid_measurement_data)
# Wait 5 secs for next backfill day
if daystobackfill > 0: time.sleep(5);
# Don't backfill after initial backfill
daystobackfill = 0
except:
self.logger.exception(
"Uncaught exception in GridRelay data processing loop."
)
self.logger.debug("Waiting for next interval...")
time.sleep(self.conf.gridrelayinterval)
def write_gridkenter_to_pvoutput(self, grid_measurement_data):
if self.conf.pvoutput:
try:
self.pvoutput.write_griddata_to_pvoutput(grid_measurement_data)
except:
self.logger.exception("Error writing GridData to PVOutput.org")
def write_gridkenter_to_influxdb(self, grid_measurement_data):
if self.conf.influx:
if self.pvinflux_initialized == False:
self.pvinflux_initialized = self.pvinflux.initialize()
if self.pvinflux_initialized:
self.pvinflux.pvinflux_write_griddata(grid_measurement_data)
else:
self.logger.debug("Writing data to Influx skipped, not initialized yet.")