-
Notifications
You must be signed in to change notification settings - Fork 2
/
pvinflux.py
221 lines (197 loc) · 8.23 KB
/
pvinflux.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
from datetime import datetime
from pvconf import PvConf
class PvInflux:
def __init__(self, conf: PvConf, logger):
self.conf = conf
self.logger = logger
self.logger.debug("PvInflux class instantiated")
def initialize(self):
try:
if self.conf.influx2:
self.initialize_v2()
else:
self.initialize_v1()
self.logger.info("InfluxDB initialized")
return True
except Exception as e:
self.logger.exception(
"Error initializing InfluxDB: '{}', retrying next interval".format(
str(e)
)
)
return False
def initialize_v2(self):
self.logger.debug("InfluxDB v2 initialization started")
try:
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
except Exception as e:
raise Exception(
"Error importing InfluxDB client library: '{}'".format(str(e))
)
url = "{}://{}:{}".format(
self.conf.if2protocol, self.conf.ifhost, self.conf.ifport
)
self.logger.info("Connecting to InfluxDB v2 url: {}".format(url))
try:
self.logger.debug(
"Instantiating InfluxDBClient class from InfluxDB library"
)
self.influxclient = InfluxDBClient(
url=url,
org=self.conf.if2org,
token=self.conf.if2token,
)
self.if2bucket_api = self.influxclient.buckets_api()
self.if2organization_api = self.influxclient.organizations_api()
self.ifwrite_api = self.influxclient.write_api(write_options=SYNCHRONOUS)
except Exception as e:
raise Exception(
"Error instantiating InfluxDB v2 client library: '{}'".format(str(e))
)
try:
self.logger.debug("Fetching influxdb bucket by name")
buckets = self.if2bucket_api.find_bucket_by_name(self.conf.if2bucket)
if buckets == None:
raise Exception(
"InfluxDB v2 bucket {} not defined".format(self.conf.if2bucket)
)
except Exception as e:
raise Exception(
"Error getting InfluxDB bucket by name: '{}'".format(str(e))
)
try:
self.logger.debug("Fetching InfluxDB organizations")
organizations = self.if2organization_api.find_organizations()
orgfound = False
for org in organizations:
if org.name == self.conf.if2org:
orgfound = True
break
if not orgfound:
self.logger.warning(
"InfluxDB v2 organization {} not defined or no authorisation to check".format(
self.conf.if2org
)
)
except Exception as e:
self.logger.exception(
"Error getting InfluxDB organizations: '{}'".format(str(e))
)
def initialize_v1(self):
self.logger.debug("InfluxDB v1 initialization started")
try:
from influxdb import InfluxDBClient
except Exception as e:
raise Exception(
"Error importing InfluxDB client library: '{}'".format(str(e))
)
try:
self.logger.debug(
"Instantiating InfluxDBClient class from InfluxDB library"
)
self.influxclient = InfluxDBClient(
host=self.conf.ifhost,
port=self.conf.ifport,
timeout=3,
username=self.conf.if1user,
password=self.conf.if1passwd,
database=self.conf.if1dbname
)
except Exception as e:
raise Exception(
"Error instantiating InfluxDB v1 client library: '{}'".format(str(e))
)
'''
try:
self.logger.debug("Fetching influxdb database list")
databases = [db["name"] for db in self.influxclient.get_list_database()]
except Exception as e:
raise Exception(
"Cannot fetch list of databases from InfluxDB: '{}'".format(str(e))
)
if self.conf.if1dbname not in databases:
self.logger.info(
f"InfluxDB database {self.conf.if1dbname} not defined in InfluxDB, creating new database"
)
try:
self.influxclient.create_database(self.conf.if1dbname)
except Exception as e:
raise Exception(
"Unable create database: '{}': '{}'".format(self.conf.if1dbname),
str(e),
)
self.logger.debug("Switching to influxdb database {}", self.conf.if1dbname)
try:
self.influxclient.switch_database(self.conf.if1dbname)
except Exception as e:
raise Exception(
"Error switching to database {}: ''".format(self.conf.if1dbname), str(e)
)
self.logger.info(
"Succesfully switched to InfluxDB v1 database '{}'".format(
self.conf.if1dbname
)
)
'''
def pvinflux_write_pvdata(self, response_json_data):
ifjson = self.make_influx_pvdata_jsonrecord(response_json_data)
self.logger.info("Writing InfluxDB json record: {}".format(str(ifjson)))
try:
if self.conf.influx2:
self.logger.debug("Writing PvData to InfluxDB v2...")
self.ifwrite_api.write(
bucket=self.conf.if2bucket,
org=self.conf.if2org,
record=ifjson,
write_precision="s",
)
else:
self.logger.debug("Writing PvData to InfluxDB v1...")
self.influxclient.write_points(ifjson, time_precision="s")
except Exception as e:
self.logger.exception("InfluxDB PvData write error: '{}'".format(str(e)))
def make_influx_pvdata_jsonrecord(self, response_json_data):
ifobj = {
"measurement": self.conf.pvsysname,
"time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"fields": {},
}
# floatKey element existence already verified and converted to Watts in fetch_fusionsolar_status()
floatKeys = {"realTimePower", "cumulativeEnergy"}
for floatKey in floatKeys:
ifobj["fields"][floatKey] = response_json_data["realKpi"][floatKey]
floatKeys = {"currentPower"}
for floatKey in floatKeys:
ifobj["fields"][floatKey] = response_json_data["powerCurve"][floatKey]
ifjson = [ifobj]
return ifjson
def pvinflux_write_griddata(self, grid_data_obj):
ifjson = self.make_influx_griddata_jsonrecord(grid_data_obj)
self.logger.info("Writing GridData InfluxDB json records: {}".format(str(ifjson)))
try:
if self.conf.influx2:
self.logger.debug("Writing GridData to InfluxDB v2...")
self.ifwrite_api.write(
bucket=self.conf.if2bucket,
org=self.conf.if2org,
record=ifjson,
write_precision="s",
)
else:
self.logger.debug("Writing GridData to InfluxDB v1...")
self.influxclient.write_points(ifjson, time_precision="s")
except Exception as e:
self.logger.exception("InfluxDB GridData write error: '{}'".format(str(e)))
def make_influx_griddata_jsonrecord(self, grid_data_obj):
influx_measurement_list = []
for measurement in grid_data_obj["grid_net_consumption"]:
influx_measurement_list.append({
"measurement": grid_data_obj["sysname"],
"time": datetime.utcfromtimestamp(measurement["timestamp"]).strftime("%Y-%m-%dT%H:%M:%SZ"),
"fields": {
"interval_energy": measurement["interval_energy"],
"interval_power_avg": measurement["interval_power_avg"]
}
})
return influx_measurement_list