-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvartaElement2influxdb.py
executable file
·127 lines (114 loc) · 4.01 KB
/
vartaElement2influxdb.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
#!/usr/bin/python3
import pytz, datetime
import pprint
import urllib.request
from influxdb import InfluxDBClient
INFLUXDB_ADDRESS = '192.168.1.6'
INFLUXDB_PORT = 8086
INFLUXDB_USER = 'username'
INFLUXDB_PASSWORD = 'password'
INFLUXDB_DATABASE = 'varta_element6'
influxdb_client = InfluxDBClient(INFLUXDB_ADDRESS, INFLUXDB_PORT, INFLUXDB_USER, INFLUXDB_PASSWORD, None)
databases = influxdb_client.get_list_database()
if len(list(filter(lambda x: x['name'] == INFLUXDB_DATABASE, databases))) == 0:
influxdb_client.create_database(INFLUXDB_DATABASE)
print('INFO', 'initialized DB')
influxdb_client.switch_database(INFLUXDB_DATABASE)
baseUrl = "http://192.168.1.15/"
content = urllib.request.urlopen(baseUrl + "cgi/ems_conf.js").read()
#print(content)
exec(content,globals(),locals())
# Wechselrichter
log_values = {
'UVerbundL1' : 'netz_l1_u',
'UVerbundL2' : 'netz_l2_u',
'UVerbundL3' : 'netz_l3_u',
'IVerbundL1' : 'netz_l1_i',
'IVerbundL2' : 'netz_l2_i',
'IVerbundL3' : 'netz_l3_i',
'UInselL1' : 'in_l1_u',
'UInselL2' : 'in_l2_u',
'UInselL3' : 'in_l3_u',
'IInselL1' : 'in_l1_1',
'IInselL2' : 'in_l2_i',
'IInselL3' : 'in_l3_i',
'TempL1' : 'temp1',
'TempL2' : 'temp2',
'TempL3' : 'temp3',
'FNetz' : 'netzfreq',
'SystemState' : 'state',
# Charger 1
'U' : 'u',
'I' : 'i',
'THT' : 'temp',
# Batterie 1
'U_Rack' : 'u',
'I_Rack' : 'i',
# Batteriemodul 1
'Status' : 'status',
'U_Modul' : 'u',
'I_Modul' : 'i',
'UAvg_Modul' : 'u_avg',
'TempAvg' : 'temp',
'Cycles' : 'cycles',
'CapRemain' : 'remain'
}
content = urllib.request.urlopen(baseUrl + "cgi/ems_data.js").read()
#print(str(content))
exec(content,globals(),locals())
# record extracted data in here
data = dict()
json_body = []
def print_key_value(key, value, name=''):
#print(key, "=", value)
if key in log_values: data[name + log_values[key]] = value
#print()
naive = datetime.datetime.strptime(Zeit, '%d.%m.%Y %H:%M:%S')
local = pytz.timezone('Europe/Berlin')
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
timestamp = utc_dt.strftime('%Y-%m-%d %H:%M:%S')
print("Zeit", "=", Zeit, timestamp)
#print("--- Wechselrichter")
name = 'WR1'
all_data = dict()
for i, key in enumerate(WR_Conf):
print_key_value(key.replace(" ",""), WR_Data[i])
all_data[key.replace(" ","")] = WR_Data[i]
json_body.append({'time': timestamp, 'measurement': 'varta','tags':{'location':name}, 'fields':all_data})
#print()
for j in range(len(Charger_Data)):
#print("--- Charger ", j + 1)
name = 'C' + str(j+1)
all_data = dict()
for i, key in enumerate(Charger_Conf):
if key == "BattData":
BattData = Charger_Data[j][i];
else:
print_key_value(key, Charger_Data[j][i], 'charger' + str(j + 1) + "_")
all_data[key] = Charger_Data[j][i]
json_body.append({'time': timestamp, 'measurement':'varta', 'tags':{'location':name}, 'fields':all_data})
#print()
name = 'B' + str(j+1)
all_data = dict()
for i, key in enumerate(Batt_Conf):
#print("--- Batterie ", j + 1)
if key == "ModulData":
ModulData = BattData[i];
else:
print_key_value(key, BattData[i], 'batt' + str(j + 1) + "_")
all_data[key] = BattData[i]
json_body.append({'time': timestamp, 'measurement':'varta', 'tags': {'location':name}, 'fields':all_data})
#print()
all_data = dict()
for k in range(len(ModulData)):
#print("--- Batteriemodul", k + 1)
name = 'B' + str(j+1) + 'M' + str(k+1)
for i, key in enumerate(Modul_Conf):
print_key_value(key, ModulData[k][i], 'modul' + str(k + 1) + "_")
all_data[key] = ModulData[k][i]
json_body.append({'time': timestamp, 'measurement':'varta', 'tags': {'location':name}, 'fields':all_data})
## remove later
json_body.append({'time':timestamp, 'measurement':'battery', 'tags': {'location':'Varta'}, 'fields':data})
#pprint.pprint(json_body)
influxdb_client.write_points(json_body)