-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyasdi.py
285 lines (249 loc) · 7.5 KB
/
yasdi.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from subprocess import Popen, PIPE
import time
import traceback
import requests
from datetime import datetime
p = Popen(['yasdishell', 'yasdi.ini'],stdin = PIPE, stdout=PIPE, stderr=PIPE,bufsize=0,close_fds=True)
xs = bytearray(b'')
POLL_TIME = 30
API_ENDPOINT = "https://{YOUR URL HERE}/api/solar/data/grid/devices?systemId={YOUR SYSTEM ID HERE}"
HEADERS = {'clientToken':"{YOUR TOKEN HERE}"}
C_FINDDEVICES="e\n4\n"
#insert your device ids here
C_READ_1="a\n155\n"
C_READ_2="a\n385\n"
C_READ_3="a\n545\n"
def addToDTO(dto,target,value,inputId=None):
if dto == None:
dto = {"inputs":[]}
if inputId == None:
dto[target] = float(value)
return dto
input = None
for inp in dto["inputs"]:
if inp["id"] == inputId:
input = inp
break
if input == None:
input = {"id":inputId}
dto["inputs"].append(input)
input[target] = value
return dto
def parseLine(line):
try:
arr = line.split("|")
if len(arr) != 3:
return None
res = [None] * 2
res[0] = arr[1].split("'")[1].strip()
res[1] = float(arr[2].split("'")[1].strip())
return res
except:
return None
def handleFinish(cC,commands):
global devices
global ok
if cC == C_FINDDEVICES:
if "Sorry, no devices currently available..." in cC:
ok = False
else:
ok = True
#TODO late parse devices maby
return
dto = None
print(dto)
#example of parsing for charger with two configured inputs
if cC == C_READ_1:
for line in commands:
arr = parseLine(line)
if arr == None:
continue
if arr[0] == "Upv-Ist DC-A":
dto = addToDTO(dto,"voltage",arr[1],1)
if arr[0] == "Upv-Ist DC-B":
dto = addToDTO(dto,"voltage",arr[1],2)
if arr[0] == "PPV DC-A":
dto = addToDTO(dto,"watt",arr[1],1)
if arr[0] == "PPV DC-B":
dto = addToDTO(dto,"watt",arr[1],2)
if arr[0] == "Uac":
dto = addToDTO(dto,"gridVoltage",arr[1])
if arr[0] == "Pac":
dto = addToDTO(dto,"gridWatt",arr[1])
if arr[0] == "Fac":
dto = addToDTO(dto,"frequency",arr[1])
if arr[0] == "E-Total":
dto = addToDTO(dto,"totalKWH",arr[1])
if arr[0] == "h-Total":
dto = addToDTO(dto,"totalOH",arr[1])
if dto != None:
dto["id"]=155
#until backend is fixed
if dto["inputs"][0]["voltage"] <= 0:
dto["inputs"][0]["ampere"] = 0
else:
dto["inputs"][0]["ampere"] = dto["inputs"][0]["watt"] / dto["inputs"][0]["voltage"]
#utnil backend is fixed
if dto["inputs"][1]["voltage"] <= 0:
dto["inputs"][1]["ampere"] = 0
else:
dto["inputs"][1]["ampere"] = dto["inputs"][1]["watt"] / dto["inputs"][1]["voltage"]
#until backend is fixed
if dto["gridVoltage"] <= 0:
dto["gridAmpere"] = 0
else:
dto["gridAmpere"] = dto["gridWatt"] / dto["gridVoltage"]
devices.append(dto)
print("dto is",dto)
#example of parsing for charger with two configured inputs
elif cC == C_READ_2:
for line in commands:
arr = parseLine(line)
if arr == None:
continue
if arr[0] == "Upv-Ist":
dto = addToDTO(dto,"chargeVoltage",arr[1])
if arr[0] == "Ipv":
dto = addToDTO(dto,"chargeAmpere",arr[1]/1000)
if arr[0] == "Uac":
dto = addToDTO(dto,"gridVoltage",arr[1])
if arr[0] == "Pac":
dto = addToDTO(dto,"gridWatt",arr[1])
if arr[0] == "Fac":
dto = addToDTO(dto,"frequency",arr[1])
if arr[0] == "E-Total":
dto = addToDTO(dto,"totalKWH",arr[1])
if arr[0] == "h-Total":
dto = addToDTO(dto,"totalOH",arr[1])
if dto != None:
dto["id"]=385
#until backend is fixed
if dto["gridVoltage"] <= 0:
dto["gridAmpere"] = 0
else:
dto["gridAmpere"] = dto["gridWatt"] / dto["gridVoltage"]
devices.append(dto)
print("dto is",dto)
elif cC == C_READ_3:
for line in commands:
arr = parseLine(line)
if arr == None:
continue
if arr[0] == "Upv-Ist DC-A":
dto = addToDTO(dto,"voltage",arr[1],3)
if arr[0] == "Upv-Ist DC-B":
dto = addToDTO(dto,"voltage",arr[1],4)
if arr[0] == "PPV DC-A":
dto = addToDTO(dto,"watt",arr[1],3)
if arr[0] == "PPV DC-B":
dto = addToDTO(dto,"watt",arr[1],4)
if arr[0] == "Uac":
dto = addToDTO(dto,"gridVoltage",arr[1])
if arr[0] == "Pac":
dto = addToDTO(dto,"gridWatt",arr[1])
if arr[0] == "Fac":
dto = addToDTO(dto,"frequency",arr[1])
if arr[0] == "E-Total":
dto = addToDTO(dto,"totalKWH",arr[1])
if arr[0] == "h-Total":
dto = addToDTO(dto,"totalOH",arr[1])
if dto != None:
dto["id"]=454
#until backend is fixed
if dto["inputs"][0]["voltage"] <= 0:
dto["inputs"][0]["ampere"] = 0
else:
dto["inputs"][0]["ampere"] = dto["inputs"][0]["watt"] / dto["inputs"][0]["voltage"]
#utnil backend is fixed
if dto["inputs"][1]["voltage"] <= 0:
dto["inputs"][1]["ampere"] = 0
else:
dto["inputs"][1]["ampere"] = dto["inputs"][1]["watt"] / dto["inputs"][1]["voltage"]
#until backend is fixed
if dto["gridVoltage"] <= 0:
dto["gridAmpere"] = 0
else:
dto["gridAmpere"] = dto["gridWatt"] / dto["gridVoltage"]
devices.append(dto)
print("dto is",dto)
def current_milli_time():
return round(time.time() * 1000)
def sendData(devices):
global POLL_TIME
global API_ENDPOINT
global HEADERS
data = {
"duration": POLL_TIME,
"timestamp": current_milli_time(),
"devices": devices
}
try:
r = requests.post(url = API_ENDPOINT,headers = HEADERS, json = data)
print(r)
if r.status_code != 200:
print(r.content)
except:
print('requests fail')
print(data)
print(traceback.format_exc())
nextCommand=C_READ_1
currentCommand=""
summedLines = list()
stamp = datetime.now()
first = True
devices = list()
ok = False
while True:
#c=reader.read(1)
c = p.stdout.read(1)
xs += c
decoded = xs.decode()
if decoded.endswith('\n'):
summedLines.append(decoded)
print("-> ",decoded)
xs = bytearray(b'')
continue
if decoded == "Command ('?' for help): ":
print("Processing mext Command")
handleFinish(currentCommand,summedLines)
summedLines.clear()
#change last command when request should be send
if currentCommand == C_READ_3:
print("send devices")
print(devices)
if len(devices) == 0:
print("No data available on any Device so weit for 5 min and try again")
time.sleep(360)
else:
sendData(devices)
devices.clear()
now = datetime.now()
dif = now - stamp
stamp = now
timeToSleep = POLL_TIME - dif.total_seconds()
if(timeToSleep > 0):
print("Sleep for: ", timeToSleep, " Seconds")
time.sleep(timeToSleep)
#for testing
#ok = True
if ok == False:
if first == False:
print("No Devices available wait for 5 min and try again")
time.sleep(360)
first = False
#p.stdin.write(bytes(C_FINDDEVICES,'UTF-8'))
#p.stdin.flush()
nextCommand = C_FINDDEVICES
print("--> send command",nextCommand)
p.stdin.write(bytes(nextCommand,'UTF-8'))
p.stdin.flush()
currentCommand=nextCommand
#change commandorder if changes
if nextCommand == C_FINDDEVICES:
nextCommand=C_READ_1
elif nextCommand == C_READ_1:
nextCommand=C_READ_2
elif nextCommand == C_READ_2:
nextCommand=C_READ_3
elif nextCommand == C_READ_3:
nextCommand=C_READ_1