-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0-bridge.py
executable file
·116 lines (87 loc) · 3.45 KB
/
s0-bridge.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
#!/usr/bin/env python3
import time as t, math, sys
from datetime import datetime
import traceback
from util.config import Config
from util.database import Database
from util.serial import Serial
from util.network import Network
class S0_Bridge:
def __init__(self):
print('###########################\n# S0 SBFspot bridge #\n###########################')
self.cfg = Config()
self.db = Database(self.cfg)
self.ser = Serial(self.cfg)
self.ntwrk = Network(self.cfg)
def start(self):
self.cfg.log('started')
# initialize
self.cfg.log('adding inverters')
self.db.add_inverters()
self.cfg.log('starting timed data collection (every 5 minutes)')
try:
while True:
ts = datetime.now().timestamp()
# before minute of time is a multiple of 5 minutes
if (int(ts) % 300) == 299:
self.cfg.log('collecting new data')
self.collect_data(self.db, ts)
t.sleep(1)
except KeyboardInterrupt:
print('Shutting down')
except Exception as e:
print('Error occured', e)
print(traceback.format_exc())
finally:
self.db.close()
self.ser.close()
sys.exit(1)
def dry_run(self, cycles=1):
self.cfg.log('starting dry run with 5 seconds sleep after each run')
# initialize
self.cfg.log('adding inverters')
self.db.add_inverters()
self.cfg.log('doing ' + str(cycles) + ' dry runs:')
try:
for _ in range(cycles):
ts = datetime.now().timestamp()
self.cfg.log('collecting new data')
self.collect_data(self.db, ts, dry=True)
t.sleep(5)
except KeyboardInterrupt:
print('Shutting down')
except Exception as e:
print('Error occured', e)
finally:
self.db.close()
self.ser.close()
sys.exit(1)
def collect_data(self, db, ts, dry=False):
ts_log = self.roundup(ts)
# serial data
if self.ser.is_enabled:
new_serial_data = self.ser.get_power_since_last_request()
if len(new_serial_data) > 0:
if dry: self.cfg.log("serial data:", new_serial_data)
if not dry:
db.add_data(ts_log, new_serial_data)
self.cfg.log('added pv data from serial interface')
if self.ntwrk.is_enabled:
new_network_data = self.ntwrk.get_power_since_last_request()
if dry: self.cfg.log("network data:", new_network_data)
if len(new_network_data) > 0:
if not dry:
db.add_data(ts_log, new_network_data)
self.cfg.log('added pv data from network interfaces')
grid_in, grid_out = self.ntwrk.get_absolute_grid_meter_data(cfg=self.cfg)
if dry: self.cfg.log('grid meter data: in = {} Wh, out = {} Wh'.format(grid_in, grid_out))
self.db.add_grid_meter_data_row(ts_log, grid_in, grid_out)
self.cfg.log('added grid meter data from network interfaces')
def roundup(self, x):
return int(math.ceil(x / 100.0)) * 100
if __name__ == '__main__':
bridge = S0_Bridge()
if len(sys.argv) > 1 and sys.argv[1] == 'dry':
bridge.dry_run(cycles=3)
else:
bridge.start()