1 | import re |
||
2 | from datetime import datetime, timedelta, timezone |
||
3 | import falcon |
||
4 | import mysql.connector |
||
5 | import simplejson as json |
||
6 | import config |
||
7 | import excelexporters.metertrend |
||
8 | from core import utilities |
||
9 | from core.useractivity import access_control, api_key_control |
||
10 | |||
11 | |||
12 | class Reporting: |
||
13 | def __init__(self): |
||
14 | """"Initializes Reporting""" |
||
15 | pass |
||
16 | |||
17 | @staticmethod |
||
18 | def on_options(req, resp): |
||
19 | _ = req |
||
20 | resp.status = falcon.HTTP_200 |
||
21 | |||
22 | #################################################################################################################### |
||
23 | # PROCEDURES |
||
24 | # Step 1: valid parameters |
||
25 | # Step 2: query the meter and energy category |
||
26 | # Step 3: query associated points |
||
27 | # Step 4: query reporting period points trends |
||
28 | # Step 5: query tariff data |
||
29 | # Step 6: construct the report |
||
30 | #################################################################################################################### |
||
31 | @staticmethod |
||
32 | def on_get(req, resp): |
||
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 | print(req.params) |
||
40 | meter_id = req.params.get('meterid') |
||
41 | meter_uuid = req.params.get('meteruuid') |
||
42 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
43 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
44 | language = req.params.get('language') |
||
45 | quick_mode = req.params.get('quickmode') |
||
46 | |||
47 | ################################################################################################################ |
||
48 | # Step 1: valid parameters |
||
49 | ################################################################################################################ |
||
50 | if meter_id is None and meter_uuid is None: |
||
51 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') |
||
52 | |||
53 | if meter_id is not None: |
||
54 | meter_id = str.strip(meter_id) |
||
55 | if not meter_id.isdigit() or int(meter_id) <= 0: |
||
56 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
57 | description='API.INVALID_METER_ID') |
||
58 | |||
59 | if meter_uuid is not None: |
||
60 | regex = re.compile(r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) |
||
61 | match = regex.match(str.strip(meter_uuid)) |
||
62 | if not bool(match): |
||
63 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
64 | description='API.INVALID_METER_UUID') |
||
65 | |||
66 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
67 | if config.utc_offset[0] == '-': |
||
68 | timezone_offset = -timezone_offset |
||
69 | |||
70 | if reporting_period_start_datetime_local is None: |
||
71 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
72 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
73 | else: |
||
74 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
||
75 | try: |
||
76 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
||
77 | '%Y-%m-%dT%H:%M:%S') |
||
78 | except ValueError: |
||
79 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
80 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
81 | reporting_start_datetime_utc = \ |
||
82 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
||
83 | # nomalize the start datetime |
||
84 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
||
85 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
||
86 | else: |
||
87 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
||
88 | |||
89 | if reporting_period_end_datetime_local is None: |
||
90 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
91 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
92 | else: |
||
93 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
||
94 | try: |
||
95 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
||
96 | '%Y-%m-%dT%H:%M:%S') |
||
97 | except ValueError: |
||
98 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
99 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
100 | reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ |
||
101 | timedelta(minutes=timezone_offset) |
||
102 | |||
103 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
104 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
105 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
||
106 | |||
107 | # if turn quick mode on, do not return parameters data and excel file |
||
108 | is_quick_mode = False |
||
109 | if quick_mode is not None and \ |
||
110 | len(str.strip(quick_mode)) > 0 and \ |
||
111 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
||
112 | is_quick_mode = True |
||
113 | |||
114 | trans = utilities.get_translation(language) |
||
115 | trans.install() |
||
116 | _ = trans.gettext |
||
117 | |||
118 | ################################################################################################################ |
||
119 | # Step 2: query the meter and energy category |
||
120 | ################################################################################################################ |
||
121 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
||
122 | cursor_system = cnx_system.cursor() |
||
123 | |||
124 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
||
125 | cursor_historical = cnx_historical.cursor() |
||
126 | if meter_id is not None: |
||
127 | cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " |
||
128 | " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " |
||
129 | " FROM tbl_meters m, tbl_energy_categories ec " |
||
130 | " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id,)) |
||
131 | row_meter = cursor_system.fetchone() |
||
132 | elif meter_uuid is not None: |
||
133 | cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " |
||
134 | " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " |
||
135 | " FROM tbl_meters m, tbl_energy_categories ec " |
||
136 | " WHERE m.uuid = %s AND m.energy_category_id = ec.id ", (meter_uuid,)) |
||
137 | row_meter = cursor_system.fetchone() |
||
138 | |||
139 | if row_meter is None: |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
140 | if cursor_system: |
||
141 | cursor_system.close() |
||
142 | if cnx_system: |
||
143 | cnx_system.close() |
||
144 | |||
145 | if cursor_historical: |
||
146 | cursor_historical.close() |
||
147 | if cnx_historical: |
||
148 | cnx_historical.close() |
||
149 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') |
||
150 | meter = dict() |
||
151 | meter['id'] = row_meter[0] |
||
152 | meter['name'] = row_meter[1] |
||
153 | meter['cost_center_id'] = row_meter[2] |
||
154 | meter['energy_category_id'] = row_meter[3] |
||
155 | meter['energy_category_name'] = row_meter[4] |
||
156 | meter['unit_of_measure'] = row_meter[5] |
||
157 | meter['kgce'] = row_meter[6] |
||
158 | meter['kgco2e'] = row_meter[7] |
||
159 | |||
160 | ################################################################################################################ |
||
161 | # Step 3: query associated points |
||
162 | ################################################################################################################ |
||
163 | point_list = list() |
||
164 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
||
165 | " FROM tbl_meters m, tbl_meters_points mp, tbl_points p " |
||
166 | " WHERE m.id = %s AND m.id = mp.meter_id AND mp.point_id = p.id " |
||
167 | " ORDER BY p.id ", (meter['id'],)) |
||
168 | rows_points = cursor_system.fetchall() |
||
169 | if rows_points is not None and len(rows_points) > 0: |
||
170 | for row in rows_points: |
||
171 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
||
172 | |||
173 | ################################################################################################################ |
||
174 | # Step 4: query reporting period points trends |
||
175 | ################################################################################################################ |
||
176 | reporting = dict() |
||
177 | reporting['names'] = list() |
||
178 | reporting['timestamps'] = list() |
||
179 | reporting['values'] = list() |
||
180 | |||
181 | for point in point_list: |
||
182 | if is_quick_mode and point['object_type'] != 'ENERGY_VALUE': |
||
183 | continue |
||
184 | |||
185 | point_value_list = list() |
||
186 | point_timestamp_list = list() |
||
187 | if point['object_type'] == 'ENERGY_VALUE': |
||
188 | query = (" SELECT utc_date_time, actual_value " |
||
189 | " FROM tbl_energy_value " |
||
190 | " WHERE point_id = %s " |
||
191 | " AND utc_date_time BETWEEN %s AND %s " |
||
192 | " ORDER BY utc_date_time ") |
||
193 | cursor_historical.execute(query, (point['id'], |
||
194 | reporting_start_datetime_utc, |
||
195 | reporting_end_datetime_utc)) |
||
196 | rows = cursor_historical.fetchall() |
||
197 | |||
198 | if rows is not None and len(rows) > 0: |
||
199 | for row in rows: |
||
200 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
201 | timedelta(minutes=timezone_offset) |
||
202 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
203 | point_timestamp_list.append(current_datetime) |
||
204 | point_value_list.append(row[1]) |
||
205 | elif point['object_type'] == 'ANALOG_VALUE': |
||
206 | query = (" SELECT utc_date_time, actual_value " |
||
207 | " FROM tbl_analog_value " |
||
208 | " WHERE point_id = %s " |
||
209 | " AND utc_date_time BETWEEN %s AND %s " |
||
210 | " ORDER BY utc_date_time ") |
||
211 | cursor_historical.execute(query, (point['id'], |
||
212 | reporting_start_datetime_utc, |
||
213 | reporting_end_datetime_utc)) |
||
214 | rows = cursor_historical.fetchall() |
||
215 | |||
216 | if rows is not None and len(rows) > 0: |
||
217 | for row in rows: |
||
218 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
219 | timedelta(minutes=timezone_offset) |
||
220 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
221 | point_timestamp_list.append(current_datetime) |
||
222 | point_value_list.append(row[1]) |
||
223 | elif point['object_type'] == 'DIGITAL_VALUE': |
||
224 | query = (" SELECT utc_date_time, actual_value " |
||
225 | " FROM tbl_digital_value " |
||
226 | " WHERE point_id = %s " |
||
227 | " AND utc_date_time BETWEEN %s AND %s " |
||
228 | " ORDER BY utc_date_time ") |
||
229 | cursor_historical.execute(query, (point['id'], |
||
230 | reporting_start_datetime_utc, |
||
231 | reporting_end_datetime_utc)) |
||
232 | rows = cursor_historical.fetchall() |
||
233 | |||
234 | if rows is not None and len(rows) > 0: |
||
235 | for row in rows: |
||
236 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
||
237 | timedelta(minutes=timezone_offset) |
||
238 | current_datetime = current_datetime_local.isoformat()[0:19] |
||
239 | point_timestamp_list.append(current_datetime) |
||
240 | point_value_list.append(row[1]) |
||
241 | |||
242 | reporting['names'].append(point['name'] + ' (' + point['units'] + ')') |
||
243 | reporting['timestamps'].append(point_timestamp_list) |
||
244 | reporting['values'].append(point_value_list) |
||
245 | |||
246 | ################################################################################################################ |
||
247 | # Step 5: query tariff data |
||
248 | ################################################################################################################ |
||
249 | parameters_data = dict() |
||
250 | parameters_data['names'] = list() |
||
251 | parameters_data['timestamps'] = list() |
||
252 | parameters_data['values'] = list() |
||
253 | if config.is_tariff_appended and not is_quick_mode: |
||
254 | tariff_dict = utilities.get_energy_category_tariffs(meter['cost_center_id'], |
||
255 | meter['energy_category_id'], |
||
256 | reporting_start_datetime_utc, |
||
257 | reporting_end_datetime_utc) |
||
258 | tariff_timestamp_list = list() |
||
259 | tariff_value_list = list() |
||
260 | for k, v in tariff_dict.items(): |
||
261 | # convert k from utc to local |
||
262 | k = k + timedelta(minutes=timezone_offset) |
||
263 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
||
264 | tariff_value_list.append(v) |
||
265 | |||
266 | parameters_data['names'].append(_('Tariff') + '-' + meter['energy_category_name']) |
||
267 | parameters_data['timestamps'].append(tariff_timestamp_list) |
||
268 | parameters_data['values'].append(tariff_value_list) |
||
269 | |||
270 | ################################################################################################################ |
||
271 | # Step 6: construct the report |
||
272 | ################################################################################################################ |
||
273 | if cursor_system: |
||
274 | cursor_system.close() |
||
275 | if cnx_system: |
||
276 | cnx_system.close() |
||
277 | |||
278 | if cursor_historical: |
||
279 | cursor_historical.close() |
||
280 | if cnx_historical: |
||
281 | cnx_historical.close() |
||
282 | |||
283 | result = { |
||
284 | "meter": { |
||
285 | "cost_center_id": meter['cost_center_id'], |
||
286 | "energy_category_id": meter['energy_category_id'], |
||
287 | "energy_category_name": meter['energy_category_name'], |
||
288 | "unit_of_measure": meter['unit_of_measure'], |
||
289 | "kgce": meter['kgce'], |
||
290 | "kgco2e": meter['kgco2e'], |
||
291 | }, |
||
292 | "reporting_period": { |
||
293 | "names": reporting['names'], |
||
294 | "timestamps": reporting['timestamps'], |
||
295 | "values": reporting['values'], |
||
296 | }, |
||
297 | "parameters": { |
||
298 | "names": parameters_data['names'], |
||
299 | "timestamps": parameters_data['timestamps'], |
||
300 | "values": parameters_data['values'] |
||
301 | }, |
||
302 | "excel_bytes_base64": None |
||
303 | } |
||
304 | # export result to Excel file and then encode the file to base64 string |
||
305 | if not is_quick_mode: |
||
306 | result['excel_bytes_base64'] = excelexporters.metertrend.export(result, |
||
307 | meter['name'], |
||
308 | reporting_period_start_datetime_local, |
||
309 | reporting_period_end_datetime_local, |
||
310 | None, |
||
311 | language) |
||
312 | |||
313 | resp.text = json.dumps(result) |
||
314 |