-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsma.py
337 lines (314 loc) · 16 KB
/
sma.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
# encoding: utf-8
import json
import time
from datetime import datetime, timedelta
import requests
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
from influxdb import InfluxDBClient
import argparse
import sys
import signal
import os
import logging
parser=argparse.ArgumentParser(
description='''Script for Query SMA Values.''')
parser.add_argument('--sma_ip', type=str, required=True, default="", help='IP of the SMA Device.')
parser.add_argument('--sma_pw', type=str, required=True, default="", help='Password of the SMA Device.')
parser.add_argument('--sma_mode', type=str, required=False, default="https", choices=["http", "https"], help='HTTP Mode for accessing the WebConnect Interface.')
parser.add_argument('--influx_ip', type=str, required=True, default="", help='IP of the Influx DB Server.')
parser.add_argument('--influx_port', type=str, required=True, default="", help='Port of the Influx DB Server.')
parser.add_argument('--influx_user', type=str, required=True, default="", help='User of the Influx DB Server.')
parser.add_argument('--influx_pw', type=str, required=True, default="", help='Password of the Influx DB Server.')
parser.add_argument('--influx_db', type=str, required=True, default="", help='DB name of the Influx DB Server.')
parser.add_argument('--interval', type=float, required=False, default="30.0", help='Interval in Seconds to query and save the data.')
parser.add_argument('--write', type=int, required=False, default=1, choices=[0, 1], help='Specify if Data should be written to InfluxDB or not.')
parser.add_argument('--log', type=str, required=False, default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR"], help='Specify Log output.')
args=parser.parse_args()
ip = args.sma_ip
pw = args.sma_pw
mode = args.sma_mode
# Log Output configuration
logging.basicConfig(level=getattr(logging, args.log), format='%(asctime)s.%(msecs)05d %(levelname)07s:\t%(message)s', datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)
# Load Measurement Configuration
# Definition of SMA Measurements
try:
for filename in ["config_measurements.json", "/config_measurements.json"]:
if os.path.isfile(filename):
with open(filename) as json_data_file:
measurements = json.load(json_data_file)
break
measurement_list = {}
for measurement in measurements:
if measurements[measurement]['active'] == True:
measurement_list[measurement] = measurements[measurement]
except Exception as e:
log.error("config_measurements.json file could not be opened or is not valid JSON file!")
log.error(e)
time.sleep(60)
sys.exit(1)
# Load Constant Queries Configuration
# definition of continous queries for faster statistics
try:
for filename in ["config_queries.json", "/config_queries.json"]:
if os.path.isfile(filename):
with open(filename) as json_data_file:
continuous_queries = json.load(json_data_file)
break
for query in continuous_queries:
continuous_queries[query] = continuous_queries[query].replace("+influx_db+", args.influx_db)
except:
log.error("config_queries.json file could not be opened or is not valid JSON file!")
time.sleep(60)
sys.exit(1)
measurement_groups = {}
for measurement in measurement_list:
if measurement_list[measurement]['group'] is not None:
measurement_groups[measurement_list[measurement]['group']] = {}
measurement_groups[measurement_list[measurement]['group']]['measurement'] = measurement_list[measurement]['group']
# Download of SMA Language File
url = mode + "://" + ip + "/data/l10n/en-US.json"
response = requests.request("GET", url, verify=False, timeout=(3, 10))
descriptions = response.json()
# Catch Stopping the Docker Container
def handler_stop_signals(signum, frame):
global sid
global ip
if logout(ip, sid, mode):
log.info("SMA Device Logout Successfull.")
else:
log.error("SMA Device Logout Failed.")
log.warning("Container Stopping...")
sys.exit(0)
signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)
# Login Function
def login(ip, pw, mode):
url = mode + "://" + ip + "/dyn/login.json"
payload = "{\"right\":\"usr\",\"pass\":\"" + pw + "\"}"
try:
response = requests.request("POST", url, data = payload, verify=False, timeout=(3, 10))
log.debug(response.json())
log.debug(response.status_code)
if response.status_code == 200:
if "result" in response.json():
return response.json()['result']['sid']
return None
except:
return None
# Logout Function
def logout(ip, sid, mode):
url = mode + "://" + ip + "/dyn/logout.json?sid=" + sid
response = requests.request("POST", url, data = "{}", verify=False, timeout=(3, 10))
if response.status_code == 200:
return True
else:
return False
# SMA Value Query
def query_values(ip, mode):
global sid
global pw
global descriptions
url = mode + "://" + ip + "/dyn/getValues.json?sid=" + sid
payload = {"destDev": [], "keys ": [] }
measurements = []
for measurement in measurement_list:
if measurement_list[measurement]['key'] not in measurements and measurement_list[measurement]['key'] is not None:
measurements.append(measurement_list[measurement]['key'])
payload = json.dumps({"destDev": [], "keys": measurements})
try:
response = requests.request("POST", url, data = payload, verify=False, timeout=(3, 10))
log.debug(response.json())
log.debug(response.status_code)
if "err" in response.json():
if response.json()['err'] == 401:
# Login on SMA Device
sid = login(ip, pw, mode)
while not sid:
log.error("Login on SMA Device (" + ip + ") failed.")
time.sleep(60)
sid = login(ip, pw, mode)
log.info("Login on SMA Device successfull.")
else:
for id in response.json()['result']:
sma_data = response.json()['result'][id]
except:
log.error("Query Failed...")
values = {}
for measurement in measurement_list:
if measurement_list[measurement]['key'] is not None:
key = measurement_list[measurement]['key']
typ = measurement_list[measurement]['type']
val = measurement_list[measurement]['val']
if typ == "int":
try:
for id in sma_data[key]:
values[measurement] = int(sma_data[key][id][val]['val'])
except:
values[measurement] = 0
elif typ == "calc":
values[measurement] = 0
elif typ == "tag":
try:
for id in sma_data[key]:
values[measurement] = descriptions[str(sma_data[key][id][val]['val'][0]['tag'])]
except:
values[measurement] = str("-")
else:
try:
for id in sma_data[key]:
values[measurement] = str(sma_data[key][id][val]['val'])
except:
values[measurement] = str("-")
for measurement in measurement_list:
typ = measurement_list[measurement]['type']
if typ == "calc":
try:
values[measurement] = values[measurement_list[measurement]['field1']]-values[measurement_list[measurement]['field2']]+values[measurement_list[measurement]['field3']]
except:
pass
return values
# Session Check Function
def session_check(ip, mode):
url = mode + "://" + ip + "/dyn/sessionCheck.json"
try:
response = requests.request("POST", url, data = "{}", verify=False, timeout=(3, 10))
except:
return (False, "No response from SMA Device (" + ip + ")!")
#log.debug(response.json())
if response.status_code == 200:
if "result" in response.json():
if "cntFreeSess" in response.json()['result']:
if response.json()['result']['cntFreeSess'] > 0:
return (True, "Sessions OK: " + str(response.json()['result']['cntFreeSess']))
else:
return (False, "No free Session on SMA Device (" + ip + ")!")
return (False, "Error in Response from SMA Device (" + ip + ")!")
# Time Rounding Function
def ceil_time(ct, delta):
return ct + (datetime.min - ct) % delta
# Check for Free Session on SMA Device
session_status = session_check(ip, mode)
while not session_status[0]:
log.error(session_status[1])
time.sleep(10)
session_status = session_check(ip, mode)
log.info(session_status[1])
now = datetime.now()
new_time = ceil_time(now, timedelta(seconds=int(args.interval)))
log.info("Actual Time: " + str(now) + " waiting for: " + str(new_time))
# Wait for Full Minute / Half Minute
while now < new_time:
time.sleep(0.5)
now = datetime.now()
client = InfluxDBClient(host=args.influx_ip, port=args.influx_port, username=args.influx_user, password=args.influx_pw)
# Login on SMA Device
sid = login(ip, pw, mode)
while not sid:
log.error("Login on SMA Device (" + ip + ") failed.")
time.sleep(60)
sid = login(ip, pw, mode)
log.info("Login on SMA Device successfull.")
# Execute Query every Xs
solar_values_last = {}
try:
while True:
solar_values = query_values(ip, mode)
log.debug("SMA Device Values: " + str(solar_values))
# Connect to InfluxDB and save Solar Values
try:
client = InfluxDBClient(host=args.influx_ip, port=args.influx_port, username=args.influx_user, password=args.influx_pw)
#log.debug(client.get_list_database())
if not {'name': args.influx_db} in client.get_list_database():
client.create_database(args.influx_db)
log.info("InfluxDB (" + str(args.influx_db) + ") created.")
client.switch_database(args.influx_db)
# check for correct continous query configuration
queries = client.get_list_continuous_queries()
for db in queries:
if list(db.keys())[0] == args.influx_db:
for query in db[args.influx_db]:
if query['name'] in continuous_queries:
if query['query'] == continuous_queries[query['name']]:
continuous_queries.pop(query['name'])
else:
client.drop_continuous_query(query['name'], database=args.influx_db)
log.info("Incorrect Continuous Query dropped: " + str(query['name']))
for query in continuous_queries:
influx_query = client.query(continuous_queries[query])
log.info("Added Continuous Query: " + str(query) + " Result: " + str(influx_query))
sel_start = continuous_queries[query].find("SELECT")
sel_end = continuous_queries[query].find("END")
sel_query = continuous_queries[query][sel_start:sel_end]
influx_query = client.query(sel_query)
log.info("Filled Statistical Tables for: " + str(query) + " Result: " + str(influx_query))
# Write Solar data to InfluxDB
try:
# Workaround for missing / wrong (lower) total values after SMA restart
if "solar_total" in measurement_list:
points = client.query('SELECT last(solar_total) FROM totals').get_points()
for point in points:
solar_values_last['solar_total'] = point['last']
if 'solar_total' in solar_values_last:
if solar_values['solar_total'] < solar_values_last['solar_total']:
solar_values['solar_total'] = solar_values_last['solar_total']
solar_values['consumption_total'] = solar_values['solar_total']-solar_values['einspeisung_total']+solar_values['bezug_total']
points = client.query('SELECT last(bezug_total) FROM totals').get_points()
for point in points:
solar_values_last['bezug_total'] = point['last']
if 'bezug_total' in solar_values_last:
if solar_values['bezug_total'] < solar_values_last['bezug_total']:
solar_values['bezug_total'] = solar_values_last['bezug_total']
solar_values['consumption_total'] = solar_values['solar_total']-solar_values['einspeisung_total']+solar_values['bezug_total']
points = client.query('SELECT last(einspeisung_total) FROM totals').get_points()
for point in points:
solar_values_last['einspeisung_total'] = point['last']
if 'einspeisung_total' in solar_values_last:
if solar_values['einspeisung_total'] < solar_values_last['einspeisung_total']:
solar_values['einspeisung_total'] = solar_values_last['einspeisung_total']
solar_values['consumption_total'] = solar_values['solar_total']-solar_values['einspeisung_total']+solar_values['bezug_total']
# Generating the JSON for writing the Influx DB data
json_body = []
for group in measurement_groups:
temp_body = {
"measurement": str(group),
"tags": {
"serial": str(solar_values['sma_sn']),
"device": str(solar_values['sma_type'])
},
"time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
"fields": {}
}
for measurement in measurement_list:
if measurement_list[measurement]['group'] == group:
if solar_values[measurement] is not None:
temp_body['fields'][measurement] = solar_values[measurement]
if str(solar_values['sma_sn']) != "unknown" and str(solar_values['sma_sn']) != "-" and str(solar_values['sma_type']) != "unknown" and str(solar_values['sma_type']) != "-":
json_body.append(temp_body)
else:
log.error("Incorrect Serial Number / Device Type, skipping data, SN: " + str(solar_values['sma_sn']) + ", Device: " + str(solar_values['sma_type']))
log.debug("InfluxDB write data DEBUG:" + str(json_body))
if args.write == 1:
influx_result = client.write_points(json_body)
if influx_result:
log.debug("InfluxDB write data successfull:" + str(json_body))
else:
log.error("InfluxDB write data FAILED:" + str(json_body))
log.error(influx_result)
#solar_values_last = solar_values
except Exception as e:
log.error("InfluxDB error writing the Solar Values")
log.error(e)
finally:
client.close()
except Exception as e:
log.error("InfluxDB connection failed (%s@%s)." % (args.influx_ip, args.influx_port))
time.sleep(args.interval - ((time.time() - new_time.timestamp()) % args.interval))
except KeyboardInterrupt:
log.warning("Script aborted...")
finally:
if logout(ip, sid, mode):
log.info("SMA Device Logout Successfull.")
else:
log.error("SMA Device Logout Failed.")