1
|
|
|
from datetime import datetime, timedelta |
2
|
|
|
import falcon |
3
|
|
|
import mysql.connector |
4
|
|
|
import simplejson as json |
5
|
|
|
from core.useractivity import access_control, api_key_control |
6
|
|
|
import config |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Reporting: |
10
|
|
|
def __init__(self): |
11
|
|
|
"""Initializes Class""" |
12
|
|
|
pass |
13
|
|
|
|
14
|
|
|
@staticmethod |
15
|
|
|
def on_options(req, resp, id_): |
16
|
|
|
_ = req |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
_ = id_ |
19
|
|
|
|
20
|
|
|
#################################################################################################################### |
21
|
|
|
# PROCEDURES |
22
|
|
|
# Step 1: valid parameters |
23
|
|
|
# Step 2: query the energy storage power station |
24
|
|
|
# Step 3: query associated containers |
25
|
|
|
# Step 4: query analog points latest values |
26
|
|
|
# Step 5: query energy points latest values |
27
|
|
|
# Step 6: query digital points latest values |
28
|
|
|
# Step 7: query the points of meters |
29
|
|
|
# Step 8: construct the report |
30
|
|
|
#################################################################################################################### |
31
|
|
|
@staticmethod |
32
|
|
|
def on_get(req, resp, id_): |
33
|
|
|
if 'API-KEY' not in req.headers or \ |
34
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
35
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
36
|
|
|
access_control(req) |
37
|
|
|
else: |
38
|
|
|
api_key_control(req) |
39
|
|
|
|
40
|
|
|
################################################################################################################ |
41
|
|
|
# Step 1: valid parameters |
42
|
|
|
################################################################################################################ |
43
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
44
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
45
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
46
|
|
|
energy_storage_power_station_id = id_ |
47
|
|
|
################################################################################################################ |
48
|
|
|
# Step 2: query the energy storage power station |
49
|
|
|
################################################################################################################ |
50
|
|
|
cnx_system = mysql.connector.connect(**config.myems_system_db) |
51
|
|
|
cursor_system = cnx_system.cursor() |
52
|
|
|
|
53
|
|
|
cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
54
|
|
|
cursor_historical = cnx_historical.cursor() |
55
|
|
|
|
56
|
|
|
if energy_storage_power_station_id is not None: |
57
|
|
|
query = (" SELECT id, name, uuid " |
58
|
|
|
" FROM tbl_energy_storage_power_stations " |
59
|
|
|
" WHERE id = %s ") |
60
|
|
|
cursor_system.execute(query, (energy_storage_power_station_id,)) |
61
|
|
|
row = cursor_system.fetchone() |
62
|
|
|
if row is None: |
63
|
|
|
cursor_system.close() |
64
|
|
|
cnx_system.close() |
65
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
66
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
67
|
|
|
|
68
|
|
|
# query all points |
69
|
|
|
query = (" SELECT id, name, units, description " |
70
|
|
|
" FROM tbl_points ") |
71
|
|
|
cursor_system.execute(query) |
72
|
|
|
rows = cursor_system.fetchall() |
73
|
|
|
|
74
|
|
|
points_dict = dict() |
75
|
|
|
if rows is not None and len(rows) > 0: |
76
|
|
|
for row in rows: |
77
|
|
|
points_dict[row[0]] = [row[1], row[2], row[3]] |
78
|
|
|
################################################################################################################ |
79
|
|
|
# Step 3: query associated containers |
80
|
|
|
################################################################################################################ |
81
|
|
|
container_list = list() |
82
|
|
|
cursor_system.execute(" SELECT c.id, c.name, c.uuid " |
83
|
|
|
" FROM tbl_energy_storage_power_stations_containers espsc, " |
84
|
|
|
" tbl_energy_storage_containers c " |
85
|
|
|
" WHERE espsc.energy_storage_power_station_id = %s " |
86
|
|
|
" AND espsc.energy_storage_container_id = c.id ", |
87
|
|
|
(energy_storage_power_station_id,)) |
88
|
|
|
rows_containers = cursor_system.fetchall() |
89
|
|
|
if rows_containers is not None and len(rows_containers) > 0: |
90
|
|
|
for row_container in rows_containers: |
91
|
|
|
container_list.append({"id": row_container[0], |
92
|
|
|
"name": row_container[1], |
93
|
|
|
"uuid": row_container[2]}) |
94
|
|
|
print('container_list:' + str(container_list)) |
95
|
|
|
|
96
|
|
|
################################################################################################################ |
97
|
|
|
# Step 4: query analog points latest values |
98
|
|
|
################################################################################################################ |
99
|
|
|
latest_value_dict = dict() |
100
|
|
|
query = (" SELECT point_id, actual_value " |
101
|
|
|
" FROM tbl_analog_value_latest " |
102
|
|
|
" WHERE utc_date_time > %s ") |
103
|
|
|
cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
104
|
|
|
rows = cursor_historical.fetchall() |
105
|
|
|
if rows is not None and len(rows) > 0: |
106
|
|
|
for row in rows: |
107
|
|
|
latest_value_dict[row[0]] = [points_dict[row[0]][0], |
108
|
|
|
points_dict[row[0]][1], |
109
|
|
|
points_dict[row[0]][2], |
110
|
|
|
row[1]] |
111
|
|
|
|
112
|
|
|
################################################################################################################ |
113
|
|
|
# Step 5: query energy points latest values |
114
|
|
|
################################################################################################################ |
115
|
|
|
query = (" SELECT point_id, actual_value " |
116
|
|
|
" FROM tbl_energy_value_latest " |
117
|
|
|
" WHERE utc_date_time > %s ") |
118
|
|
|
cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
119
|
|
|
rows = cursor_historical.fetchall() |
120
|
|
|
if rows is not None and len(rows) > 0: |
121
|
|
|
for row in rows: |
122
|
|
|
latest_value_dict[row[0]] = [points_dict[row[0]][0], |
123
|
|
|
points_dict[row[0]][1], |
124
|
|
|
points_dict[row[0]][2], |
125
|
|
|
row[1]] |
126
|
|
|
|
127
|
|
|
################################################################################################################ |
128
|
|
|
# Step 6: query digital points latest values |
129
|
|
|
################################################################################################################ |
130
|
|
|
query = (" SELECT point_id, actual_value " |
131
|
|
|
" FROM tbl_digital_value_latest " |
132
|
|
|
" WHERE utc_date_time > %s ") |
133
|
|
|
cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
134
|
|
|
rows = cursor_historical.fetchall() |
135
|
|
|
if rows is not None and len(rows) > 0: |
136
|
|
|
for row in rows: |
137
|
|
|
latest_value_dict[row[0]] = [points_dict[row[0]][0], |
138
|
|
|
points_dict[row[0]][1], |
139
|
|
|
points_dict[row[0]][2], |
140
|
|
|
row[1]] |
141
|
|
|
|
142
|
|
|
################################################################################################################ |
143
|
|
|
# Step 7: query the points of meters |
144
|
|
|
################################################################################################################ |
145
|
|
|
# query all points with units |
146
|
|
|
query = (" SELECT id, units " |
147
|
|
|
" FROM tbl_points ") |
148
|
|
|
cursor_system.execute(query) |
149
|
|
|
rows = cursor_system.fetchall() |
150
|
|
|
|
151
|
|
|
units_dict = dict() |
152
|
|
|
if rows is not None and len(rows) > 0: |
153
|
|
|
for row in rows: |
154
|
|
|
units_dict[row[0]] = row[1] |
155
|
|
|
|
156
|
|
|
# query meter parameters |
157
|
|
|
meter_list = list() |
158
|
|
|
point_id_list = list() |
159
|
|
|
for container in container_list: |
160
|
|
|
cursor_system.execute(" SELECT charge_meter_id, discharge_meter_id " |
161
|
|
|
" FROM tbl_energy_storage_containers_batteries " |
162
|
|
|
" WHERE energy_storage_container_id = %s " |
163
|
|
|
" ORDER BY id ", |
164
|
|
|
(container['id'],)) |
165
|
|
|
rows_meters = cursor_system.fetchall() |
166
|
|
|
if rows_meters is not None and len(rows_meters) > 0: |
167
|
|
|
for row in rows_meters: |
168
|
|
|
charge_meter = dict() |
169
|
|
|
charge_meter['id'] = row[0] |
170
|
|
|
charge_meter['points'] = list() |
171
|
|
|
meter_list.append(charge_meter) |
172
|
|
|
discharge_meter = dict() |
173
|
|
|
discharge_meter['id'] = row[1] |
174
|
|
|
discharge_meter['points'] = list() |
175
|
|
|
meter_list.append(discharge_meter) |
176
|
|
|
|
177
|
|
|
for index, meter in enumerate(meter_list): |
178
|
|
|
cursor_system.execute(" SELECT p.id " |
179
|
|
|
" FROM tbl_meters_points mp, tbl_points p " |
180
|
|
|
" WHERE mp.meter_id = %s AND mp.point_id = p.id " |
181
|
|
|
" ORDER BY mp.id ", |
182
|
|
|
(meter['id'],)) |
183
|
|
|
rows_points = cursor_system.fetchall() |
184
|
|
|
if rows_points is not None and len(rows_points) > 0: |
185
|
|
|
point_list = list() |
186
|
|
|
for row in rows_points: |
187
|
|
|
# ignore duplicate point |
188
|
|
|
if row[0] in point_id_list: |
189
|
|
|
continue |
190
|
|
|
point_id_list.append(row[0]) |
191
|
|
|
point = latest_value_dict.get(row[0], None) |
192
|
|
|
if point is not None: |
193
|
|
|
point_list.append(point) |
194
|
|
|
meter_list[index]['points'] = point_list |
195
|
|
|
|
196
|
|
|
if cursor_system: |
197
|
|
|
cursor_system.close() |
198
|
|
|
if cnx_system: |
199
|
|
|
cnx_system.close() |
200
|
|
|
|
201
|
|
|
if cursor_historical: |
202
|
|
|
cursor_historical.close() |
203
|
|
|
if cnx_historical: |
204
|
|
|
cnx_historical.close() |
205
|
|
|
################################################################################################################ |
206
|
|
|
# Step 8: construct the report |
207
|
|
|
################################################################################################################ |
208
|
|
|
all_in_one_meter = dict() |
209
|
|
|
all_in_one_meter['points'] = list() |
210
|
|
|
for meter in meter_list: |
211
|
|
|
all_in_one_meter['points'] += meter['points'] |
212
|
|
|
|
213
|
|
|
resp.text = json.dumps([all_in_one_meter]) |
214
|
|
|
|