-
Notifications
You must be signed in to change notification settings - Fork 0
/
fronius.py
354 lines (294 loc) · 13 KB
/
fronius.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
###
# fronius.py
# v1.1
# Api for fetching data from Fronius Symo
#
# Still very much a work in Progress, still working on the function parameters
# Todo:
# 1. Fix functions with Mandatory option and option combinations.
# eg. if Scope="Device" DeviceId is required
# 2. Option to return csv or raw json/dict data
# 3. Wrap into a module when bugs ironed out
#
###
# print("*************************************************")
# print("THIS IS NOT RUNNING YET - Check the CODE First :)")
# print("*************************************************")
# exit()
import requests
import json
import pprint
import time
# You might want to add ip.ip.ip.ip fronius to your hosts file
hostname = "fronius"
def getData(hostname,dataRequest):
"""
All Request's come via this function. It builds the url from args
hostname and dataRequest. It is advised to have a fronius hostname
entry in /etc/hosts. There is no authentication required, it is assumed
you are on a local, private network.
"""
try:
url = "http://" + hostname + dataRequest
r = requests.get(url,timeout=60)
r.raise_for_status()
return r.json()
except requests.exceptions.Timeout:
print("Request: {} failed ".format(url))
except requests.exceptions.RequestException as e:
print("Request failed with {}".format(e))
exit()
def GetInverterRealtimeData(DeviceId="0",Scope='System',DataCollection='CumulationInverterData'):
"""
:param Scope:
String ("Device" || "System"
System implies whole system and uses CumulationInverterData
:param DeviceId:
Mandatory - needs DeviceId
String ("Solar Net: 0 ...99")
:param DataCollection:
String:
"CumulationInverterData"
"CommonInverterData"
"3PInverterData"
"MinMaxInverterData"
:return:
json (dict)
"""
dataRq = '/solar_api/v1/GetInverterRealtimeData.cgi?'+'scope='+Scope+'&DataCollection='+DataCollection+'&DeviceId=' +DeviceId
return getData(hostname,dataRq)
def GetSensorRealtimeData(Scope='System',DeviceId='0',DataCollection='NowStringControlData',TimePeriod='Day'):
"""
This request provides data for all channels of a single Fronius Sensor Card.
Inactive channels and channels with damaged sensors are not included in the response.
:param Scope:
String "Device" or "System"
:param DeviceId:
String "0" .. "99"
:param DataCollection:
String.
"NowStringControlData" -> The presently measured currents of every channel.
"LastErrorStringControlData" -> Information about the last error which triggered a service message
"CurrentSumStringControlData" -> Current Sums of all channels for a selected time period
:param TimePeriod:
String.
"Day"
"Year"
"Total"
:return:
json (dict)
"""
dataRq = '/solar_api/v1/GetSensorRealtimeData.cgi?Scope='+Scope+'&DeviceId='+DeviceId+'&DataCollection='+DataCollection+'&TimePeriod='+TimePeriod
return getData(hostname,dataRq)
def GetStringRealtimeData(Scope='System',DeviceId='0',DataCollection='NowStringControlData',TimePeriod='Day'):
"""
:param Scope:
String "Device" or "System"
:param DeviceId:
String "0" .. "99"
:param DataCollection:
String.
"NowStringControlData" -> The presently measured currents of every channel.
"LastErrorStringControlData" -> Information about the last error which triggered a service message
"CurrentSumStringControlData" -> Current Sums of all channels for a selected time period
:param TimePeriod:
String.
"Day"
"Year"
"Total"
:return:
json (dict)
"""
dataRq = '/solar_api/v1/GetStringRealtimeData.cgi?Scope='+Scope+'&DeviceId='+DeviceId+'&DataCollection='+DataCollection+'&TimePeriod='+TimePeriod
return getData(hostname, dataRq)
def GetLoggerInfo():
"""
This request provides information about the logging device which provides this API
"""
dataRq = '/solar_api/v1/GetLoggerInfo.cgi'
return getData(hostname, dataRq)
def GetLoggerLEDInfo():
"""
This request provides information about the LED states and colors on the
device which provides this API
"""
dataRq = '/solar_api/v1/GetLoggerLEDInfo.cgi'
return getData(hostname, dataRq)
def InverterInfoStatusCode(code):
if code > 10:
return "Invalid Status"
if code < 7:
return "Startup"
elif code == 7:
return "Running"
elif code == 8:
return "Standby"
elif code == 9:
return "Bootloading"
elif code == 10:
return "Error"
def GetInverterInfo():
"""
This request provides information about all inverters that are currently
being monitored by the logging device. So this means that inverters which
are currently not online are also reported by this request, provided these
inverters have been seen by the logging device within the last 24 hours.
If information about devices currently online is needed,
the GetActiveDeviceInfo request should be used. This request also provides
information about device classes other than inverters
Returns:
StatusCode (Numeric)
0-6 Startup
7 Running
8 Standby
9 Bootloading
10 Error
"""
dataRq = '/solar_api/v1/GetInverterInfo.cgi'
return getData(hostname, dataRq)
def GetActiveDeviceInfo(DeviceClass='System'):
"""
This request provides information about which devices are currently online.
:parameter: DeviceClass
String:
"Inverter"
"Storage"
"Ohmpilot"
"SensorCard"
"StringControl"
"Meter"
"System"
"""
dataRq = '/solar_api/v1/GetActiveDeviceInfo.cgi?'+ 'DeviceClass=' + DeviceClass
return getData(hostname, dataRq)
def GetMeterRealtimeData(Scope='System',DeviceId='0',DeviceClass='System'):
"""
This request provides detailed information about Meter devices.
Inactive channels are not included in the response and may vary depended
on used metering device and software version. Take care about permanently
or temporary missing channels when processing this response.
Check the Enable,Visible fields if zero data may not be available.
:param Scope: String "Device" or "System"
:param DeviceId: String "0" .. "65535"
:parameter: DeviceClass String:
"Inverter"
"Storage"
"Ohmpilot"
"SensorCard"
"StringControl"
"Meter"
"System"
:return:
json (dict)
"""
dataRq = '/solar_api/v1/GetMeterRealtimeData.cgi?Scope='+Scope+'&DeviceId='+DeviceId+'&DeviceClass='+DeviceClass
return getData(hostname, dataRq)
def GetStorageRealtimeData(Scope='System',DeviceId='0'):
"""
This request provides detailed information about batteries. Inactive channels
are not included in the response and may vary depended on used battery and
software version. Take care about permanently or temporary missing channels
when processing this response.
***
I can't test this -
Get Request failed with 404 Client Error: Not Found for url:
***
:param Scope: String "Device" or "System"
:param DeviceId: String "0" .. "65535"
:return:
"""
dataRq ='/solar_api/v1/GetStorageRealtimeData.cgi?Scope='+Scope+'&DeviceId='+DeviceId
return getData(hostname, dataRq)
def GetOhmPilotRealtimeData(Scope='Device',DeviceId='0'):
"""
This request provides detailed information about OhmPilot. Inactive channels
are not included in the response and may vary depended on used hardware and
software version. Take care about permanently or temporary missing channels
when processing this response.
:param Scope: String "Device" or "System"
:param DeviceId: String "0" .. "65535"
"""
dataRq = '/solar_api/v1/GetOhmPilotRealtimeData.cgi?Scope=' + Scope + '&DeviceId=' + DeviceId
return getData(hostname, dataRq)
def GetArchiveData(startDate,endDate):
"""
Archive requests shall be provided whenever access to historic device-data
is possible and it makes sense to provide such a request.
Of course, the Datalogger Web can only provide what is stored in its internal
memory and has not been overwritten by newer data yet. It can loose data,
due to capacity reason. The number of days stored dependence on the
number of connected units to log. This limitation of is not present
for Solar.web, provided that the Datalogger has reliably uploaded the data.
The Request is populated with All Possible parameters by default.
"""
dataRq = "/solar_api/v1/GetArchiveData.cgi?Scope=System&SeriesType=Detail&HumanReadable=True&StartDate=1.9.2017&EndDate=3.9.2017&Channel=TimeSpanInSec&Channel=Digital_PowerManagementRelay_Out_1&Channel=EnergyReal_WAC_Sum_Produced&Channel=InverterEvents&Channel=InverterErrors&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2&Channel=Temperature_Powerstage&Channel=Voltage_AC_Phase_1&Channel=Voltage_AC_Phase_2&Channel=Voltage_AC_Phase_3&Channel=Current_AC_Phase_1&Channel=Current_AC_Phase_2&Channel=Current_AC_Phase_3&Channel=PowerReal_PAC_Sum&Channel=EnergyReal_WAC_Minus_Absolute&Channel=EnergyReal_WAC_Plus_Absolute&Channel=Meter_Location_Current&Channel=Temperature_Channel_1&Channel=Temperature_Channel_2&Channel=Digital_Channel_1&Channel=Digital_Channel_2&Channel=Radiation&Channel=Digital_PowerManagementRelay_Out_1&"
return getData(hostname,dataRq)
def GetPowerFlowRealtimeData():
"""
This request provides detailed information about the local energy grid.
The values replied represent the current state. Because of data has multiple
asynchrone origins it is a matter of facts that the sum of all
powers (grid, load and generate) will differ from zero.
"""
dataRq = '/solar_api/v1/GetPowerFlowRealtimeData.fcgi'
return getData(hostname,dataRq)
def PowerFlowRealtimeData(jPFRD):
# Collect the Inverter Data
# Does not include Optional Fields at this time
Inverters = dict()
Site = dict()
# There could be more than 1 inverter here - Bitcoin Miners :)
for i in jPFRD['Body']['Data']['Inverters']:
for i in jPFRD['Body']['Data']['Inverters']:
Inverters['DeviceId'] = i
Inverters['Version'] = jPFRD['Body']['Data']['Version']
Inverters['Timestamp'] = jPFRD['Head']['Timestamp']
Inverters['DT'] = jPFRD['Body']['Data']['Inverters'][i]['DT']
Inverters['E_Day'] = jPFRD['Body']['Data']['Inverters'][i]['E_Day']
Inverters['E_Total'] = jPFRD['Body']['Data']['Inverters'][i]['E_Total']
Inverters['E_Year'] = jPFRD['Body']['Data']['Inverters'][i]['E_Year']
Inverters['P'] = jPFRD['Body']['Data']['Inverters'][i]['P']
# Collect Site data (single row)
Site['Timestamp'] = jPFRD['Head']['Timestamp']
Site['Version'] = jPFRD['Body']['Data']['Version']
Site['E_Day'] = jPFRD['Body']['Data']['Site']['E_Day']
Site['E_Total'] = jPFRD['Body']['Data']['Site']['E_Total']
Site['E_Year'] = jPFRD['Body']['Data']['Site']['E_Year']
Site['Meter_Location'] = jPFRD['Body']['Data']['Site']['Meter_Location']
Site['Mode'] = jPFRD['Body']['Data']['Site']['Mode']
Site['P_Akku'] = jPFRD['Body']['Data']['Site']['P_Akku']
Site['P_Grid'] = jPFRD['Body']['Data']['Site']['P_Grid']
Site['P_Load'] = jPFRD['Body']['Data']['Site']['P_Load']
Site['P_PV'] = jPFRD['Body']['Data']['Site']['P_PV']
Site['rel_Autonomy'] = jPFRD['Body']['Data']['Site']['rel_Autonomy']
Site['rel_SelfConsumption'] = jPFRD['Body']['Data']['Site']['rel_SelfConsumption']
return [Site, Inverters]
### Just Initial Testing Code
def testPowerFlowRealtimeData():
pp = pprint.PrettyPrinter(indent=4)
cnt = 0
while cnt < 10:
cnt = cnt + 1
Site, Inverters = PowerFlowRealtimeData(GetPowerFlowRealtimeData())
pp.pprint(Site)
pp.pprint(Inverters)
time.sleep(10)
#testPowerFlowRealtimeData()
#jArchiveData = getArchiveData('1.9.2017','30.12.2017')
#jPowerFLowRealtimeData = GetPowerFlowRealtimeData()
#Site, Inverters = PowerFlowRealtimeData(jPowerFLowRealtimeData)
#jDeviceInfo = GetActiveDeviceInfo('Inverter')
#jInverterRealtimeData = GetInverterRealtimeData()
#jSensorRealtimeData = GetSensorRealtimeData()
#jStringRealtimeData = GetStringRealtimeData(DataCollection='LastErrorStringControlData')
#jStringRealtimeData = GetStringRealtimeData(DataCollection='CurrentSumStringControlData')
jLoggerInfo = GetLoggerInfo()
#jLoggerLEDInfo = GetLoggerLEDInfo()
#jInverterInfo = GetInverterInfo()
#for i in range(1,20):
# print(InverterInfoStatusCode(i))
#jMeterRealtimeData = GetMeterRealtimeData(Scope='System')
#jStorageRealtimeData = GetStorageRealtimeData(Scope='System')
#jOhmPilotRealtimeData = GetOhmPilotRealtimeData(Scope='System')
#jPowerFlowRealtimeData = GetPowerFlowRealtimeData()
print('Done')