forked from jbuehl/solaredge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathse2state.py
41 lines (36 loc) · 1.12 KB
/
se2state.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
#!/usr/bin/python
# Maintain a file containing the current state of SolarEdge inverters and optimizers
import json
import getopt
import time
import sys
# state values
stateDict = {"inverters": {}, "optimizers": {}}
# get program arguments and options
(opts, args) = getopt.getopt(sys.argv[1:], "o:")
try:
inFile = open(args[0])
except:
inFile = sys.stdin
for opt in opts:
if opt[0] == "-o":
outFileName = opt[1]
# read the input forever
while True:
jsonStr = ""
# wait for data
while jsonStr == "":
time.sleep(.1)
jsonStr = inFile.readline()
inDict = json.loads(jsonStr)
# update the state values
stateDict["inverters"].update(inDict["inverters"])
stateDict["optimizers"].update(inDict["optimizers"])
# zero current energy and power when an event occurs
if len(inDict["events"]) != 0:
for inverter in stateDict["inverters"].keys():
stateDict["inverters"][inverter]["Eac"] = 0.0
stateDict["inverters"][inverter]["Pac"] = 0.0
# update the state file
with open(outFileName, "w") as outFile:
json.dump(stateDict, outFile)