-
Notifications
You must be signed in to change notification settings - Fork 1
/
pellematic.py
152 lines (119 loc) · 4.63 KB
/
pellematic.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
# imports
from configparser import ConfigParser
from datetime import datetime
from influxdb import InfluxDBClient
import json
import os
import urllib.request
# configuration
path = os.path.dirname(__file__)
if '' != path:
path += '/'
config = ConfigParser()
config.read([path + '_config.ini', path + '_user.ini'])
# debug mode
debug = config.getboolean('source', 'debug', fallback = False)
# connect to influxDB to persist data
def connectToInfluxDB():
# influxDB configuration settings with fallback(s)
influxDatabase = config.get('influxDB', 'database', fallback = 'oekofen')
influxHost = config.get('influxDB', 'host', fallback = '127.0.0.1')
influxPassword = config.get('influxDB', 'password', fallback = '')
influxUser = config.get('influxDB', 'user', fallback = '')
# influxDB port configuratioin has to be an integer
try:
influxPort = config.getint('influxDB', 'port', fallback = 8086)
except ValueError as error:
print('Error: ' + str(error))
quit()
# check validity of influxDB connection concerning host and port only
try:
influxDBClient = InfluxDBClient(influxHost, influxPort, influxUser, influxPassword, influxDatabase)
influxDBClient.ping()
except Exception as error:
print('Error connecting to influxDB:')
print('-> ' + str(error))
quit()
return influxDBClient
# converts data for special keys so they have 'correct' values
def convertFieldValues(data):
for key in data:
for field in data[key]:
if isinstance(data[key][field], dict):
if 'factor' in data[key][field] and 0.1 == data[key][field]['factor']:
data[key][field] = float(data[key][field]['val'])/10
elif 'factor' in data[key][field] and 0.01 == data[key][field]['factor']:
data[key][field] = float(data[key][field]['val'])/100
else:
data[key][field] = data[key][field]['val']
return data
# collect defined data from AC ELWA-E
def getData():
ip = config.get('source', 'ip')
password = config.get('source', 'password')
port = config.getint('source', 'port', fallback = 4321)
# check for necessary config keys
if '' == ip:
print('Required config "ip" within [source] in _user.ini is missing!')
quit()
if '' == password:
print('Required config "password" within [source] in _user.ini is missing!')
quit()
fields = config.get('source', 'fields')
parts = config.get('source', 'parts')
if '' != fields and '' == parts:
print('If custom fields are defined, custom parts have to be defined as well!')
quit()
url = 'http://' + ip + ':' + str(port) + '/' + password + '/all?'
oekofenData = json.loads(urllib.request.urlopen(url).read().decode('cp1252'))
oekofenDataParts = dict()
# collect all fields of all parts
if '' == fields and '' == parts:
for key in oekofenData:
oekofenDataParts[key] = oekofenData[key]
return oekofenDataParts
# collect all fields of parts defined in _config.ini/_user.ini
if '' == fields and '' != parts:
for key in oekofenData:
if key in parts:
oekofenDataParts[key] = oekofenData[key]
return oekofenDataParts
# collect fields and parts defined in _config.ini/_user.ini
for key in oekofenData:
if key in parts:
oekofenFields = dict()
for field in oekofenData[key]:
if (key + '|' + field) in fields:
oekofenFields[field] = oekofenData[key][field]
oekofenDataParts[key] = oekofenFields
return oekofenDataParts
# write collected data to defined influxDB
def writeDataToInfluxDB(parts):
measurement = config.get('influxDB', 'measurement', fallback = 'pellematic')
for part in parts:
if 0 == len(parts[part]):
return
points = [{
'fields': parts[part],
'measurement': measurement,
'tags': {
'part': part
}
}]
try:
influxDBClient.write_points(points, time_precision = 's')
except Exception as error:
print('Error connecting to influxDB:')
print('-> ' + str(error))
quit()
# collect and persist data
influxDBClient = connectToInfluxDB()
data = getData()
data = convertFieldValues(data)
writeDataToInfluxDB(data)
if True == debug:
print('Debug: Pellematic Condens data:')
for part in data:
print('Part: ' + part)
for key in sorted(data[part]):
print('-> ' + key + ': ' + str(data[part][key]))