Total Complexity | 717 |
Total Lines | 3470 |
Duplicated Lines | 71.21 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like core.combinedequipment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import uuid |
||
2 | from datetime import datetime, timedelta |
||
3 | import falcon |
||
4 | import mysql.connector |
||
5 | import simplejson as json |
||
6 | from core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
7 | import config |
||
8 | |||
9 | |||
10 | View Code Duplication | class CombinedEquipmentCollection: |
|
|
|||
11 | @staticmethod |
||
12 | def __init__(): |
||
13 | """ Initializes CombinedEquipmentCollection""" |
||
14 | pass |
||
15 | |||
16 | @staticmethod |
||
17 | def on_options(req, resp): |
||
18 | resp.status = falcon.HTTP_200 |
||
19 | |||
20 | @staticmethod |
||
21 | def on_get(req, resp): |
||
22 | if 'API-KEY' not in req.headers or \ |
||
23 | not isinstance(req.headers['API-KEY'], str) or \ |
||
24 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
25 | access_control(req) |
||
26 | else: |
||
27 | api_key_control(req) |
||
28 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
29 | cursor = cnx.cursor() |
||
30 | |||
31 | query = (" SELECT id, name, uuid " |
||
32 | " FROM tbl_cost_centers ") |
||
33 | cursor.execute(query) |
||
34 | rows_cost_centers = cursor.fetchall() |
||
35 | |||
36 | cost_center_dict = dict() |
||
37 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
38 | for row in rows_cost_centers: |
||
39 | cost_center_dict[row[0]] = {"id": row[0], |
||
40 | "name": row[1], |
||
41 | "uuid": row[2]} |
||
42 | |||
43 | query = (" SELECT id, name, uuid, " |
||
44 | " is_input_counted, is_output_counted, " |
||
45 | " cost_center_id, svg, camera_url, description " |
||
46 | " FROM tbl_combined_equipments " |
||
47 | " ORDER BY id ") |
||
48 | cursor.execute(query) |
||
49 | rows_combined_equipments = cursor.fetchall() |
||
50 | |||
51 | result = list() |
||
52 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
53 | for row in rows_combined_equipments: |
||
54 | meta_result = {"id": row[0], |
||
55 | "name": row[1], |
||
56 | "uuid": row[2], |
||
57 | "is_input_counted": bool(row[3]), |
||
58 | "is_output_counted": bool(row[4]), |
||
59 | "cost_center": cost_center_dict.get(row[5], None), |
||
60 | "svg": row[6], |
||
61 | "camera_url": row[7], |
||
62 | "description": row[8], |
||
63 | "qrcode": 'combinedequipment:' + row[2]} |
||
64 | result.append(meta_result) |
||
65 | |||
66 | cursor.close() |
||
67 | cnx.close() |
||
68 | resp.text = json.dumps(result) |
||
69 | |||
70 | @staticmethod |
||
71 | @user_logger |
||
72 | def on_post(req, resp): |
||
73 | """Handles POST requests""" |
||
74 | admin_control(req) |
||
75 | try: |
||
76 | raw_json = req.stream.read().decode('utf-8') |
||
77 | except Exception as ex: |
||
78 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
79 | title='API.BAD_REQUEST', |
||
80 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
81 | |||
82 | new_values = json.loads(raw_json) |
||
83 | |||
84 | if 'name' not in new_values['data'].keys() or \ |
||
85 | not isinstance(new_values['data']['name'], str) or \ |
||
86 | len(str.strip(new_values['data']['name'])) == 0: |
||
87 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
88 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
89 | name = str.strip(new_values['data']['name']) |
||
90 | |||
91 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
92 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
93 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
94 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
95 | is_input_counted = new_values['data']['is_input_counted'] |
||
96 | |||
97 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
98 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
99 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
100 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
101 | is_output_counted = new_values['data']['is_output_counted'] |
||
102 | |||
103 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
104 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
105 | new_values['data']['cost_center_id'] <= 0: |
||
106 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
107 | description='API.INVALID_COST_CENTER_ID') |
||
108 | cost_center_id = new_values['data']['cost_center_id'] |
||
109 | |||
110 | if 'svg' not in new_values['data'].keys() or \ |
||
111 | not isinstance(new_values['data']['svg'], str) or \ |
||
112 | len(str.strip(new_values['data']['svg'])) == 0: |
||
113 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
114 | description='API.INVALID_SVG') |
||
115 | svg = str.strip(new_values['data']['svg']) |
||
116 | |||
117 | if 'description' in new_values['data'].keys() and \ |
||
118 | new_values['data']['description'] is not None and \ |
||
119 | len(str(new_values['data']['description'])) > 0: |
||
120 | description = str.strip(new_values['data']['description']) |
||
121 | else: |
||
122 | description = None |
||
123 | |||
124 | if 'camera_url' in new_values['data'].keys() and \ |
||
125 | new_values['data']['camera_url'] is not None and \ |
||
126 | len(str(new_values['data']['camera_url'])) > 0: |
||
127 | camera_url = str.strip(new_values['data']['camera_url']) |
||
128 | else: |
||
129 | camera_url = None |
||
130 | |||
131 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
132 | cursor = cnx.cursor() |
||
133 | |||
134 | cursor.execute(" SELECT name " |
||
135 | " FROM tbl_combined_equipments " |
||
136 | " WHERE name = %s ", (name,)) |
||
137 | if cursor.fetchone() is not None: |
||
138 | cursor.close() |
||
139 | cnx.close() |
||
140 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
141 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
142 | |||
143 | if cost_center_id is not None: |
||
144 | cursor.execute(" SELECT name " |
||
145 | " FROM tbl_cost_centers " |
||
146 | " WHERE id = %s ", |
||
147 | (new_values['data']['cost_center_id'],)) |
||
148 | row = cursor.fetchone() |
||
149 | if row is None: |
||
150 | cursor.close() |
||
151 | cnx.close() |
||
152 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
153 | description='API.COST_CENTER_NOT_FOUND') |
||
154 | |||
155 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
156 | " (name, uuid, is_input_counted, is_output_counted, " |
||
157 | " cost_center_id, svg, camera_url, description) " |
||
158 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
159 | cursor.execute(add_values, (name, |
||
160 | str(uuid.uuid4()), |
||
161 | is_input_counted, |
||
162 | is_output_counted, |
||
163 | cost_center_id, |
||
164 | svg, |
||
165 | camera_url, |
||
166 | description)) |
||
167 | new_id = cursor.lastrowid |
||
168 | cnx.commit() |
||
169 | cursor.close() |
||
170 | cnx.close() |
||
171 | |||
172 | resp.status = falcon.HTTP_201 |
||
173 | resp.location = '/combinedequipments/' + str(new_id) |
||
174 | |||
175 | |||
176 | class CombinedEquipmentItem: |
||
177 | @staticmethod |
||
178 | def __init__(): |
||
179 | """Initializes CombinedEquipmentItem""" |
||
180 | pass |
||
181 | |||
182 | @staticmethod |
||
183 | def on_options(req, resp, id_): |
||
184 | resp.status = falcon.HTTP_200 |
||
185 | |||
186 | View Code Duplication | @staticmethod |
|
187 | def on_get(req, resp, id_): |
||
188 | if 'API-KEY' not in req.headers or \ |
||
189 | not isinstance(req.headers['API-KEY'], str) or \ |
||
190 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
191 | access_control(req) |
||
192 | else: |
||
193 | api_key_control(req) |
||
194 | if not id_.isdigit() or int(id_) <= 0: |
||
195 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
196 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
197 | |||
198 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
199 | cursor = cnx.cursor() |
||
200 | |||
201 | query = (" SELECT id, name, uuid " |
||
202 | " FROM tbl_cost_centers ") |
||
203 | cursor.execute(query) |
||
204 | rows_cost_centers = cursor.fetchall() |
||
205 | |||
206 | cost_center_dict = dict() |
||
207 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
208 | for row in rows_cost_centers: |
||
209 | cost_center_dict[row[0]] = {"id": row[0], |
||
210 | "name": row[1], |
||
211 | "uuid": row[2]} |
||
212 | |||
213 | query = (" SELECT id, name, uuid, " |
||
214 | " is_input_counted, is_output_counted, " |
||
215 | " cost_center_id, svg, camera_url, description " |
||
216 | " FROM tbl_combined_equipments " |
||
217 | " WHERE id = %s ") |
||
218 | cursor.execute(query, (id_,)) |
||
219 | row = cursor.fetchone() |
||
220 | cursor.close() |
||
221 | cnx.close() |
||
222 | |||
223 | if row is None: |
||
224 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
225 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
226 | else: |
||
227 | meta_result = {"id": row[0], |
||
228 | "name": row[1], |
||
229 | "uuid": row[2], |
||
230 | "is_input_counted": bool(row[3]), |
||
231 | "is_output_counted": bool(row[4]), |
||
232 | "cost_center": cost_center_dict.get(row[5], None), |
||
233 | "svg": row[6], |
||
234 | "camera_url": row[7], |
||
235 | "description": row[8], |
||
236 | "qrcode": 'combinedequipment:' + row[2]} |
||
237 | |||
238 | resp.text = json.dumps(meta_result) |
||
239 | |||
240 | View Code Duplication | @staticmethod |
|
241 | @user_logger |
||
242 | def on_delete(req, resp, id_): |
||
243 | admin_control(req) |
||
244 | if not id_.isdigit() or int(id_) <= 0: |
||
245 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
246 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
247 | |||
248 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
249 | cursor = cnx.cursor() |
||
250 | |||
251 | # check relation with space |
||
252 | cursor.execute(" SELECT space_id " |
||
253 | " FROM tbl_spaces_combined_equipments " |
||
254 | " WHERE combined_equipment_id = %s ", |
||
255 | (id_,)) |
||
256 | rows_combined_equipments = cursor.fetchall() |
||
257 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
258 | cursor.close() |
||
259 | cnx.close() |
||
260 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
261 | title='API.BAD_REQUEST', |
||
262 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
263 | |||
264 | # delete relation with commands |
||
265 | cursor.execute(" DELETE FROM tbl_combined_equipments_commands WHERE combined_equipment_id = %s ", (id_,)) |
||
266 | |||
267 | # delete relation with equipments |
||
268 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments WHERE combined_equipment_id = %s ", (id_,)) |
||
269 | |||
270 | # delete relation with meters |
||
271 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
272 | |||
273 | # delete relation with offline meters |
||
274 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
275 | |||
276 | # delete all associated parameters |
||
277 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters WHERE combined_equipment_id = %s ", (id_,)) |
||
278 | |||
279 | # delete relation with virtual meter |
||
280 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
281 | cnx.commit() |
||
282 | |||
283 | cursor.execute(" DELETE FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
284 | cnx.commit() |
||
285 | |||
286 | cursor.close() |
||
287 | cnx.close() |
||
288 | |||
289 | resp.status = falcon.HTTP_204 |
||
290 | |||
291 | View Code Duplication | @staticmethod |
|
292 | @user_logger |
||
293 | def on_put(req, resp, id_): |
||
294 | """Handles PUT requests""" |
||
295 | admin_control(req) |
||
296 | if not id_.isdigit() or int(id_) <= 0: |
||
297 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
298 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
299 | try: |
||
300 | raw_json = req.stream.read().decode('utf-8') |
||
301 | except Exception as ex: |
||
302 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
303 | title='API.BAD_REQUEST', |
||
304 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
305 | |||
306 | new_values = json.loads(raw_json) |
||
307 | |||
308 | if 'name' not in new_values['data'].keys() or \ |
||
309 | not isinstance(new_values['data']['name'], str) or \ |
||
310 | len(str.strip(new_values['data']['name'])) == 0: |
||
311 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
312 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
313 | name = str.strip(new_values['data']['name']) |
||
314 | |||
315 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
316 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
317 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
318 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
319 | is_input_counted = new_values['data']['is_input_counted'] |
||
320 | |||
321 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
322 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
323 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
324 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
325 | is_output_counted = new_values['data']['is_output_counted'] |
||
326 | |||
327 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
328 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
329 | new_values['data']['cost_center_id'] <= 0: |
||
330 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
331 | description='API.INVALID_COST_CENTER_ID') |
||
332 | cost_center_id = new_values['data']['cost_center_id'] |
||
333 | |||
334 | if 'svg' not in new_values['data'].keys() or \ |
||
335 | not isinstance(new_values['data']['svg'], str) or \ |
||
336 | len(str.strip(new_values['data']['svg'])) == 0: |
||
337 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
338 | description='API.INVALID_SVG') |
||
339 | svg = str.strip(new_values['data']['svg']) |
||
340 | |||
341 | if 'description' in new_values['data'].keys() and \ |
||
342 | new_values['data']['description'] is not None and \ |
||
343 | len(str(new_values['data']['description'])) > 0: |
||
344 | description = str.strip(new_values['data']['description']) |
||
345 | else: |
||
346 | description = None |
||
347 | |||
348 | if 'camera_url' in new_values['data'].keys() and \ |
||
349 | new_values['data']['camera_url'] is not None and \ |
||
350 | len(str(new_values['data']['camera_url'])) > 0: |
||
351 | camera_url = str.strip(new_values['data']['camera_url']) |
||
352 | else: |
||
353 | camera_url = None |
||
354 | |||
355 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
356 | cursor = cnx.cursor() |
||
357 | |||
358 | cursor.execute(" SELECT name " |
||
359 | " FROM tbl_combined_equipments " |
||
360 | " WHERE id = %s ", (id_,)) |
||
361 | if cursor.fetchone() is None: |
||
362 | cursor.close() |
||
363 | cnx.close() |
||
364 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
365 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
366 | |||
367 | cursor.execute(" SELECT name " |
||
368 | " FROM tbl_combined_equipments " |
||
369 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
370 | if cursor.fetchone() is not None: |
||
371 | cursor.close() |
||
372 | cnx.close() |
||
373 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
374 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
375 | |||
376 | cursor.execute(" SELECT name " |
||
377 | " FROM tbl_cost_centers " |
||
378 | " WHERE id = %s ", |
||
379 | (new_values['data']['cost_center_id'],)) |
||
380 | row = cursor.fetchone() |
||
381 | if row is None: |
||
382 | cursor.close() |
||
383 | cnx.close() |
||
384 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
385 | description='API.COST_CENTER_NOT_FOUND') |
||
386 | |||
387 | update_row = (" UPDATE tbl_combined_equipments " |
||
388 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
||
389 | " cost_center_id = %s, svg = %s, camera_url = %s, description = %s " |
||
390 | " WHERE id = %s ") |
||
391 | cursor.execute(update_row, (name, |
||
392 | is_input_counted, |
||
393 | is_output_counted, |
||
394 | cost_center_id, |
||
395 | svg, |
||
396 | camera_url, |
||
397 | description, |
||
398 | id_)) |
||
399 | cnx.commit() |
||
400 | |||
401 | cursor.close() |
||
402 | cnx.close() |
||
403 | |||
404 | resp.status = falcon.HTTP_200 |
||
405 | |||
406 | # Clone a Combined Equipment |
||
407 | View Code Duplication | @staticmethod |
|
408 | @user_logger |
||
409 | def on_post(req, resp, id_): |
||
410 | """Handles POST requests""" |
||
411 | admin_control(req) |
||
412 | if not id_.isdigit() or int(id_) <= 0: |
||
413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
414 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
415 | |||
416 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
417 | cursor = cnx.cursor() |
||
418 | cursor.execute(" SELECT name " |
||
419 | " FROM tbl_combined_equipments " |
||
420 | " WHERE id = %s ", (id_,)) |
||
421 | if cursor.fetchone() is None: |
||
422 | cursor.close() |
||
423 | cnx.close() |
||
424 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
425 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
426 | |||
427 | query = (" SELECT name, is_input_counted, is_output_counted, " |
||
428 | " cost_center_id, svg, camera_url, description " |
||
429 | " FROM tbl_combined_equipments " |
||
430 | " WHERE id = %s ") |
||
431 | cursor.execute(query, (id_,)) |
||
432 | row = cursor.fetchone() |
||
433 | |||
434 | if row is None: |
||
435 | cursor.close() |
||
436 | cnx.close() |
||
437 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
438 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
439 | else: |
||
440 | |||
441 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
442 | " (name, uuid, is_input_counted, is_output_counted, " |
||
443 | " cost_center_id, svg, camera_url, description) " |
||
444 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
445 | cursor.execute(add_values, (row[0] + ' Copy', |
||
446 | str(uuid.uuid4()), |
||
447 | row[1], |
||
448 | row[2], |
||
449 | row[3], |
||
450 | row[4], |
||
451 | row[5], |
||
452 | row[6])) |
||
453 | new_id = cursor.lastrowid |
||
454 | cnx.commit() |
||
455 | |||
456 | # clone relation with meter |
||
457 | cursor.execute(" SELECT meter_id, is_output " |
||
458 | " FROM tbl_combined_equipments_meters " |
||
459 | " WHERE combined_equipment_id = %s ", |
||
460 | (id_,)) |
||
461 | rows_meters = cursor.fetchall() |
||
462 | if rows_meters is not None and len(rows_meters) > 0: |
||
463 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
||
464 | " VALUES ") |
||
465 | for row in rows_meters: |
||
466 | add_values += " (" + str(new_id) + "," |
||
467 | add_values += str(row[0]) + "," |
||
468 | add_values += str(bool(row[1])) + "), " |
||
469 | # trim ", " at the end of string and then execute |
||
470 | cursor.execute(add_values[:-2]) |
||
471 | cnx.commit() |
||
472 | |||
473 | # clone relation with offline meter |
||
474 | cursor.execute(" SELECT offline_meter_id, is_output " |
||
475 | " FROM tbl_combined_equipments_offline_meters " |
||
476 | " WHERE combined_equipment_id = %s ", |
||
477 | (id_,)) |
||
478 | rows_offline_meters = cursor.fetchall() |
||
479 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
480 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
481 | " (combined_equipment_id, offline_meter_id, is_output) " |
||
482 | " VALUES ") |
||
483 | for row in rows_offline_meters: |
||
484 | add_values += " (" + str(new_id) + "," |
||
485 | add_values += "'" + str(row[0]) + "'," |
||
486 | add_values += str(bool(row[1])) + "), " |
||
487 | # trim ", " at the end of string and then execute |
||
488 | cursor.execute(add_values[:-2]) |
||
489 | cnx.commit() |
||
490 | |||
491 | # clone relation with virtual meter |
||
492 | cursor.execute(" SELECT virtual_meter_id, is_output " |
||
493 | " FROM tbl_combined_equipments_virtual_meters " |
||
494 | " WHERE combined_equipment_id = %s ", |
||
495 | (id_,)) |
||
496 | rows_virtual_meters = cursor.fetchall() |
||
497 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
498 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
499 | " (combined_equipment_id, virtual_meter_id, is_output) " |
||
500 | " VALUES ") |
||
501 | for row in rows_virtual_meters: |
||
502 | add_values += " (" + str(new_id) + "," |
||
503 | add_values += str(row[0]) + "," |
||
504 | add_values += str(bool(row[1])) + "), " |
||
505 | # trim ", " at the end of string and then execute |
||
506 | cursor.execute(add_values[:-2]) |
||
507 | cnx.commit() |
||
508 | |||
509 | # clone parameters |
||
510 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
511 | " FROM tbl_combined_equipments_parameters " |
||
512 | " WHERE combined_equipment_id = %s ", |
||
513 | (id_,)) |
||
514 | rows_parameters = cursor.fetchall() |
||
515 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
516 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
||
517 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
||
518 | " numerator_meter_uuid, denominator_meter_uuid) " |
||
519 | " VALUES ") |
||
520 | for row in rows_parameters: |
||
521 | add_values += " (" + str(new_id) + "," |
||
522 | add_values += "'" + str(row[0]) + "'," |
||
523 | add_values += "'" + str(row[1]) + "'," |
||
524 | if row[2] is not None: |
||
525 | add_values += "'" + str(row[2]) + "'," |
||
526 | else: |
||
527 | add_values += "null, " |
||
528 | |||
529 | if row[3] is not None: |
||
530 | add_values += str(row[2]) + "," |
||
531 | else: |
||
532 | add_values += "null, " |
||
533 | |||
534 | if row[4] is not None: |
||
535 | add_values += "'" + row[4] + "'," |
||
536 | else: |
||
537 | add_values += "null, " |
||
538 | if row[5] is not None: |
||
539 | add_values += "'" + row[5] + "'), " |
||
540 | else: |
||
541 | add_values += "null), " |
||
542 | |||
543 | # trim ", " at the end of string and then execute |
||
544 | cursor.execute(add_values[:-2]) |
||
545 | cnx.commit() |
||
546 | |||
547 | cursor.close() |
||
548 | cnx.close() |
||
549 | resp.status = falcon.HTTP_201 |
||
550 | resp.location = '/combinedequipments/' + str(new_id) |
||
551 | |||
552 | |||
553 | View Code Duplication | class CombinedEquipmentEquipmentCollection: |
|
554 | @staticmethod |
||
555 | def __init__(): |
||
556 | """Initializes CombinedEquipmentEquipmentCollection""" |
||
557 | pass |
||
558 | |||
559 | @staticmethod |
||
560 | def on_options(req, resp, id_): |
||
561 | resp.status = falcon.HTTP_200 |
||
562 | |||
563 | @staticmethod |
||
564 | def on_get(req, resp, id_): |
||
565 | if 'API-KEY' not in req.headers or \ |
||
566 | not isinstance(req.headers['API-KEY'], str) or \ |
||
567 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
568 | access_control(req) |
||
569 | else: |
||
570 | api_key_control(req) |
||
571 | if not id_.isdigit() or int(id_) <= 0: |
||
572 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
573 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
574 | |||
575 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
576 | cursor = cnx.cursor() |
||
577 | |||
578 | cursor.execute(" SELECT name " |
||
579 | " FROM tbl_combined_equipments " |
||
580 | " WHERE id = %s ", (id_,)) |
||
581 | if cursor.fetchone() is None: |
||
582 | cursor.close() |
||
583 | cnx.close() |
||
584 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
585 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
586 | |||
587 | query = (" SELECT e.id, e.name, e.uuid " |
||
588 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
589 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
590 | " ORDER BY e.id ") |
||
591 | cursor.execute(query, (id_,)) |
||
592 | rows = cursor.fetchall() |
||
593 | cursor.close() |
||
594 | cnx.close() |
||
595 | |||
596 | result = list() |
||
597 | if rows is not None and len(rows) > 0: |
||
598 | for row in rows: |
||
599 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
600 | result.append(meta_result) |
||
601 | |||
602 | resp.text = json.dumps(result) |
||
603 | |||
604 | @staticmethod |
||
605 | @user_logger |
||
606 | def on_post(req, resp, id_): |
||
607 | """Handles POST requests""" |
||
608 | admin_control(req) |
||
609 | try: |
||
610 | raw_json = req.stream.read().decode('utf-8') |
||
611 | except Exception as ex: |
||
612 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
613 | title='API.BAD_REQUEST', |
||
614 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
615 | |||
616 | if not id_.isdigit() or int(id_) <= 0: |
||
617 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
618 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
619 | |||
620 | new_values = json.loads(raw_json) |
||
621 | |||
622 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
623 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
624 | new_values['data']['equipment_id'] <= 0: |
||
625 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
626 | description='API.INVALID_EQUIPMENT_ID') |
||
627 | equipment_id = new_values['data']['equipment_id'] |
||
628 | |||
629 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
630 | cursor = cnx.cursor() |
||
631 | |||
632 | cursor.execute(" SELECT name " |
||
633 | " from tbl_combined_equipments " |
||
634 | " WHERE id = %s ", (id_,)) |
||
635 | if cursor.fetchone() is None: |
||
636 | cursor.close() |
||
637 | cnx.close() |
||
638 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
639 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
640 | |||
641 | cursor.execute(" SELECT name " |
||
642 | " FROM tbl_equipments " |
||
643 | " WHERE id = %s ", (equipment_id,)) |
||
644 | if cursor.fetchone() is None: |
||
645 | cursor.close() |
||
646 | cnx.close() |
||
647 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
648 | description='API.EQUIPMENT_NOT_FOUND') |
||
649 | |||
650 | query = (" SELECT id " |
||
651 | " FROM tbl_combined_equipments_equipments " |
||
652 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
653 | cursor.execute(query, (id_, equipment_id,)) |
||
654 | if cursor.fetchone() is not None: |
||
655 | cursor.close() |
||
656 | cnx.close() |
||
657 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
658 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
659 | |||
660 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
661 | " VALUES (%s, %s) ") |
||
662 | cursor.execute(add_row, (id_, equipment_id,)) |
||
663 | cnx.commit() |
||
664 | cursor.close() |
||
665 | cnx.close() |
||
666 | |||
667 | resp.status = falcon.HTTP_201 |
||
668 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
||
669 | |||
670 | |||
671 | View Code Duplication | class CombinedEquipmentEquipmentItem: |
|
672 | @staticmethod |
||
673 | def __init__(): |
||
674 | """Initializes CombinedEquipmentEquipmentItem""" |
||
675 | pass |
||
676 | |||
677 | @staticmethod |
||
678 | def on_options(req, resp, id_, eid): |
||
679 | resp.status = falcon.HTTP_200 |
||
680 | |||
681 | @staticmethod |
||
682 | @user_logger |
||
683 | def on_delete(req, resp, id_, eid): |
||
684 | admin_control(req) |
||
685 | if not id_.isdigit() or int(id_) <= 0: |
||
686 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
687 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
688 | |||
689 | if not eid.isdigit() or int(eid) <= 0: |
||
690 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
691 | description='API.INVALID_EQUIPMENT_ID') |
||
692 | |||
693 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
694 | cursor = cnx.cursor() |
||
695 | |||
696 | cursor.execute(" SELECT name " |
||
697 | " FROM tbl_combined_equipments " |
||
698 | " WHERE id = %s ", (id_,)) |
||
699 | if cursor.fetchone() is None: |
||
700 | cursor.close() |
||
701 | cnx.close() |
||
702 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
703 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
704 | |||
705 | cursor.execute(" SELECT name " |
||
706 | " FROM tbl_equipments " |
||
707 | " WHERE id = %s ", (eid,)) |
||
708 | if cursor.fetchone() is None: |
||
709 | cursor.close() |
||
710 | cnx.close() |
||
711 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
712 | description='API.EQUIPMENT_NOT_FOUND') |
||
713 | |||
714 | cursor.execute(" SELECT id " |
||
715 | " FROM tbl_combined_equipments_equipments " |
||
716 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
717 | if cursor.fetchone() is None: |
||
718 | cursor.close() |
||
719 | cnx.close() |
||
720 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
721 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_NOT_FOUND') |
||
722 | |||
723 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments " |
||
724 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
725 | cnx.commit() |
||
726 | |||
727 | cursor.close() |
||
728 | cnx.close() |
||
729 | |||
730 | resp.status = falcon.HTTP_204 |
||
731 | |||
732 | |||
733 | View Code Duplication | class CombinedEquipmentParameterCollection: |
|
734 | @staticmethod |
||
735 | def __init__(): |
||
736 | """Initializes CombinedEquipmentParameterCollection""" |
||
737 | pass |
||
738 | |||
739 | @staticmethod |
||
740 | def on_options(req, resp, id_): |
||
741 | resp.status = falcon.HTTP_200 |
||
742 | |||
743 | @staticmethod |
||
744 | def on_get(req, resp, id_): |
||
745 | if 'API-KEY' not in req.headers or \ |
||
746 | not isinstance(req.headers['API-KEY'], str) or \ |
||
747 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
748 | access_control(req) |
||
749 | else: |
||
750 | api_key_control(req) |
||
751 | if not id_.isdigit() or int(id_) <= 0: |
||
752 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
753 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
754 | |||
755 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
756 | cursor = cnx.cursor() |
||
757 | |||
758 | cursor.execute(" SELECT name " |
||
759 | " FROM tbl_combined_equipments " |
||
760 | " WHERE id = %s ", (id_,)) |
||
761 | if cursor.fetchone() is None: |
||
762 | cursor.close() |
||
763 | cnx.close() |
||
764 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
765 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
766 | |||
767 | query = (" SELECT id, name " |
||
768 | " FROM tbl_points ") |
||
769 | cursor.execute(query) |
||
770 | rows_points = cursor.fetchall() |
||
771 | |||
772 | point_dict = dict() |
||
773 | if rows_points is not None and len(rows_points) > 0: |
||
774 | for row in rows_points: |
||
775 | point_dict[row[0]] = {"id": row[0], |
||
776 | "name": row[1]} |
||
777 | |||
778 | query = (" SELECT id, name, uuid " |
||
779 | " FROM tbl_meters ") |
||
780 | cursor.execute(query) |
||
781 | rows_meters = cursor.fetchall() |
||
782 | |||
783 | meter_dict = dict() |
||
784 | if rows_meters is not None and len(rows_meters) > 0: |
||
785 | for row in rows_meters: |
||
786 | meter_dict[row[2]] = {"type": 'meter', |
||
787 | "id": row[0], |
||
788 | "name": row[1], |
||
789 | "uuid": row[2]} |
||
790 | |||
791 | query = (" SELECT id, name, uuid " |
||
792 | " FROM tbl_offline_meters ") |
||
793 | cursor.execute(query) |
||
794 | rows_offline_meters = cursor.fetchall() |
||
795 | |||
796 | offline_meter_dict = dict() |
||
797 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
798 | for row in rows_offline_meters: |
||
799 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
800 | "id": row[0], |
||
801 | "name": row[1], |
||
802 | "uuid": row[2]} |
||
803 | |||
804 | query = (" SELECT id, name, uuid " |
||
805 | " FROM tbl_virtual_meters ") |
||
806 | cursor.execute(query) |
||
807 | rows_virtual_meters = cursor.fetchall() |
||
808 | |||
809 | virtual_meter_dict = dict() |
||
810 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
811 | for row in rows_virtual_meters: |
||
812 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
813 | "id": row[0], |
||
814 | "name": row[1], |
||
815 | "uuid": row[2]} |
||
816 | |||
817 | query = (" SELECT id, name, parameter_type, " |
||
818 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
819 | " FROM tbl_combined_equipments_parameters " |
||
820 | " WHERE combined_equipment_id = %s " |
||
821 | " ORDER BY id ") |
||
822 | cursor.execute(query, (id_,)) |
||
823 | rows_parameters = cursor.fetchall() |
||
824 | |||
825 | result = list() |
||
826 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
827 | for row in rows_parameters: |
||
828 | constant = None |
||
829 | point = None |
||
830 | numerator_meter = None |
||
831 | denominator_meter = None |
||
832 | if row[2] == 'point': |
||
833 | point = point_dict.get(row[4], None) |
||
834 | constant = None |
||
835 | numerator_meter = None |
||
836 | denominator_meter = None |
||
837 | elif row[2] == 'constant': |
||
838 | constant = row[3] |
||
839 | point = None |
||
840 | numerator_meter = None |
||
841 | denominator_meter = None |
||
842 | elif row[2] == 'fraction': |
||
843 | constant = None |
||
844 | point = None |
||
845 | # find numerator meter by uuid |
||
846 | numerator_meter = meter_dict.get(row[5], None) |
||
847 | if numerator_meter is None: |
||
848 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
849 | if numerator_meter is None: |
||
850 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
851 | # find denominator meter by uuid |
||
852 | denominator_meter = meter_dict.get(row[6], None) |
||
853 | if denominator_meter is None: |
||
854 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
855 | if denominator_meter is None: |
||
856 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
857 | |||
858 | meta_result = {"id": row[0], |
||
859 | "name": row[1], |
||
860 | "parameter_type": row[2], |
||
861 | "constant": constant, |
||
862 | "point": point, |
||
863 | "numerator_meter": numerator_meter, |
||
864 | "denominator_meter": denominator_meter} |
||
865 | result.append(meta_result) |
||
866 | |||
867 | cursor.close() |
||
868 | cnx.close() |
||
869 | resp.text = json.dumps(result) |
||
870 | |||
871 | @staticmethod |
||
872 | @user_logger |
||
873 | def on_post(req, resp, id_): |
||
874 | """Handles POST requests""" |
||
875 | admin_control(req) |
||
876 | if not id_.isdigit() or int(id_) <= 0: |
||
877 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
878 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
879 | try: |
||
880 | raw_json = req.stream.read().decode('utf-8') |
||
881 | except Exception as ex: |
||
882 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
883 | title='API.BAD_REQUEST', |
||
884 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
885 | |||
886 | new_values = json.loads(raw_json) |
||
887 | |||
888 | if 'name' not in new_values['data'].keys() or \ |
||
889 | not isinstance(new_values['data']['name'], str) or \ |
||
890 | len(str.strip(new_values['data']['name'])) == 0: |
||
891 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
892 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
893 | name = str.strip(new_values['data']['name']) |
||
894 | |||
895 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
896 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
897 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
898 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
899 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
900 | |||
901 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
902 | |||
903 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
904 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
905 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
906 | |||
907 | constant = None |
||
908 | if 'constant' in new_values['data'].keys(): |
||
909 | if new_values['data']['constant'] is not None and \ |
||
910 | isinstance(new_values['data']['constant'], str) and \ |
||
911 | len(str.strip(new_values['data']['constant'])) > 0: |
||
912 | constant = str.strip(new_values['data']['constant']) |
||
913 | |||
914 | point_id = None |
||
915 | if 'point_id' in new_values['data'].keys(): |
||
916 | if new_values['data']['point_id'] is not None and \ |
||
917 | new_values['data']['point_id'] <= 0: |
||
918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
919 | description='API.INVALID_POINT_ID') |
||
920 | point_id = new_values['data']['point_id'] |
||
921 | |||
922 | numerator_meter_uuid = None |
||
923 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
924 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
925 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
926 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
927 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
928 | |||
929 | denominator_meter_uuid = None |
||
930 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
931 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
932 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
933 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
934 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
935 | |||
936 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
937 | cursor = cnx.cursor() |
||
938 | |||
939 | cursor.execute(" SELECT name " |
||
940 | " FROM tbl_combined_equipments " |
||
941 | " WHERE id = %s ", (id_,)) |
||
942 | if cursor.fetchone() is None: |
||
943 | cursor.close() |
||
944 | cnx.close() |
||
945 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
946 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
947 | |||
948 | cursor.execute(" SELECT name " |
||
949 | " FROM tbl_combined_equipments_parameters " |
||
950 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
||
951 | if cursor.fetchone() is not None: |
||
952 | cursor.close() |
||
953 | cnx.close() |
||
954 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
955 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
956 | |||
957 | # validate by parameter type |
||
958 | if parameter_type == 'point': |
||
959 | if point_id is None: |
||
960 | cursor.close() |
||
961 | cnx.close() |
||
962 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
963 | description='API.INVALID_POINT_ID') |
||
964 | |||
965 | query = (" SELECT id, name " |
||
966 | " FROM tbl_points " |
||
967 | " WHERE id = %s ") |
||
968 | cursor.execute(query, (point_id,)) |
||
969 | if cursor.fetchone() is None: |
||
970 | cursor.close() |
||
971 | cnx.close() |
||
972 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
973 | description='API.POINT_NOT_FOUND') |
||
974 | |||
975 | elif parameter_type == 'constant': |
||
976 | if constant is None: |
||
977 | cursor.close() |
||
978 | cnx.close() |
||
979 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
980 | description='API.INVALID_CONSTANT_VALUE') |
||
981 | |||
982 | elif parameter_type == 'fraction': |
||
983 | |||
984 | query = (" SELECT id, name, uuid " |
||
985 | " FROM tbl_meters ") |
||
986 | cursor.execute(query) |
||
987 | rows_meters = cursor.fetchall() |
||
988 | |||
989 | meter_dict = dict() |
||
990 | if rows_meters is not None and len(rows_meters) > 0: |
||
991 | for row in rows_meters: |
||
992 | meter_dict[row[2]] = {"type": 'meter', |
||
993 | "id": row[0], |
||
994 | "name": row[1], |
||
995 | "uuid": row[2]} |
||
996 | |||
997 | query = (" SELECT id, name, uuid " |
||
998 | " FROM tbl_offline_meters ") |
||
999 | cursor.execute(query) |
||
1000 | rows_offline_meters = cursor.fetchall() |
||
1001 | |||
1002 | offline_meter_dict = dict() |
||
1003 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1004 | for row in rows_offline_meters: |
||
1005 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1006 | "id": row[0], |
||
1007 | "name": row[1], |
||
1008 | "uuid": row[2]} |
||
1009 | |||
1010 | query = (" SELECT id, name, uuid " |
||
1011 | " FROM tbl_virtual_meters ") |
||
1012 | cursor.execute(query) |
||
1013 | rows_virtual_meters = cursor.fetchall() |
||
1014 | |||
1015 | virtual_meter_dict = dict() |
||
1016 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1017 | for row in rows_virtual_meters: |
||
1018 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1019 | "id": row[0], |
||
1020 | "name": row[1], |
||
1021 | "uuid": row[2]} |
||
1022 | |||
1023 | # validate numerator meter uuid |
||
1024 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
1025 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
1026 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
1027 | cursor.close() |
||
1028 | cnx.close() |
||
1029 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1030 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
1031 | |||
1032 | # validate denominator meter uuid |
||
1033 | if denominator_meter_uuid == numerator_meter_uuid: |
||
1034 | cursor.close() |
||
1035 | cnx.close() |
||
1036 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1037 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1038 | |||
1039 | if denominator_meter_uuid not in meter_dict and \ |
||
1040 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
1041 | denominator_meter_uuid not in offline_meter_dict: |
||
1042 | cursor.close() |
||
1043 | cnx.close() |
||
1044 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1045 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1046 | |||
1047 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
1048 | " (combined_equipment_id, name, parameter_type, constant, " |
||
1049 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
1050 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
1051 | cursor.execute(add_values, (id_, |
||
1052 | name, |
||
1053 | parameter_type, |
||
1054 | constant, |
||
1055 | point_id, |
||
1056 | numerator_meter_uuid, |
||
1057 | denominator_meter_uuid)) |
||
1058 | new_id = cursor.lastrowid |
||
1059 | cnx.commit() |
||
1060 | cursor.close() |
||
1061 | cnx.close() |
||
1062 | |||
1063 | resp.status = falcon.HTTP_201 |
||
1064 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
||
1065 | |||
1066 | |||
1067 | View Code Duplication | class CombinedEquipmentParameterItem: |
|
1068 | @staticmethod |
||
1069 | def __init__(): |
||
1070 | """"Initializes CombinedEquipmentParameterItem""" |
||
1071 | pass |
||
1072 | |||
1073 | @staticmethod |
||
1074 | def on_options(req, resp, id_, pid): |
||
1075 | resp.status = falcon.HTTP_200 |
||
1076 | |||
1077 | @staticmethod |
||
1078 | def on_get(req, resp, id_, pid): |
||
1079 | if 'API-KEY' not in req.headers or \ |
||
1080 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1081 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1082 | access_control(req) |
||
1083 | else: |
||
1084 | api_key_control(req) |
||
1085 | if not id_.isdigit() or int(id_) <= 0: |
||
1086 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1087 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1088 | |||
1089 | if not pid.isdigit() or int(pid) <= 0: |
||
1090 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1091 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1092 | |||
1093 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1094 | cursor = cnx.cursor() |
||
1095 | |||
1096 | query = (" SELECT id, name " |
||
1097 | " FROM tbl_points ") |
||
1098 | cursor.execute(query) |
||
1099 | rows_points = cursor.fetchall() |
||
1100 | |||
1101 | point_dict = dict() |
||
1102 | if rows_points is not None and len(rows_points) > 0: |
||
1103 | for row in rows_points: |
||
1104 | point_dict[row[0]] = {"id": row[0], |
||
1105 | "name": row[1]} |
||
1106 | |||
1107 | query = (" SELECT id, name, uuid " |
||
1108 | " FROM tbl_meters ") |
||
1109 | cursor.execute(query) |
||
1110 | rows_meters = cursor.fetchall() |
||
1111 | |||
1112 | meter_dict = dict() |
||
1113 | if rows_meters is not None and len(rows_meters) > 0: |
||
1114 | for row in rows_meters: |
||
1115 | meter_dict[row[2]] = {"type": 'meter', |
||
1116 | "id": row[0], |
||
1117 | "name": row[1], |
||
1118 | "uuid": row[2]} |
||
1119 | |||
1120 | query = (" SELECT id, name, uuid " |
||
1121 | " FROM tbl_offline_meters ") |
||
1122 | cursor.execute(query) |
||
1123 | rows_offline_meters = cursor.fetchall() |
||
1124 | |||
1125 | offline_meter_dict = dict() |
||
1126 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1127 | for row in rows_offline_meters: |
||
1128 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1129 | "id": row[0], |
||
1130 | "name": row[1], |
||
1131 | "uuid": row[2]} |
||
1132 | |||
1133 | query = (" SELECT id, name, uuid " |
||
1134 | " FROM tbl_virtual_meters ") |
||
1135 | cursor.execute(query) |
||
1136 | rows_virtual_meters = cursor.fetchall() |
||
1137 | |||
1138 | virtual_meter_dict = dict() |
||
1139 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1140 | for row in rows_virtual_meters: |
||
1141 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1142 | "id": row[0], |
||
1143 | "name": row[1], |
||
1144 | "uuid": row[2]} |
||
1145 | |||
1146 | query = (" SELECT id, name, parameter_type, " |
||
1147 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
1148 | " FROM tbl_combined_equipments_parameters " |
||
1149 | " WHERE combined_equipment_id = %s AND id = %s ") |
||
1150 | cursor.execute(query, (id_, pid)) |
||
1151 | row = cursor.fetchone() |
||
1152 | cursor.close() |
||
1153 | cnx.close() |
||
1154 | |||
1155 | if row is None: |
||
1156 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1157 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1158 | else: |
||
1159 | constant = None |
||
1160 | point = None |
||
1161 | numerator_meter = None |
||
1162 | denominator_meter = None |
||
1163 | if row[2] == 'point': |
||
1164 | point = point_dict.get(row[4], None) |
||
1165 | constant = None |
||
1166 | numerator_meter = None |
||
1167 | denominator_meter = None |
||
1168 | elif row[2] == 'constant': |
||
1169 | constant = row[3] |
||
1170 | point = None |
||
1171 | numerator_meter = None |
||
1172 | denominator_meter = None |
||
1173 | elif row[2] == 'fraction': |
||
1174 | constant = None |
||
1175 | point = None |
||
1176 | # find numerator meter by uuid |
||
1177 | numerator_meter = meter_dict.get(row[5], None) |
||
1178 | if numerator_meter is None: |
||
1179 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
1180 | if numerator_meter is None: |
||
1181 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
1182 | # find denominator meter by uuid |
||
1183 | denominator_meter = meter_dict.get(row[6], None) |
||
1184 | if denominator_meter is None: |
||
1185 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
1186 | if denominator_meter is None: |
||
1187 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
1188 | |||
1189 | meta_result = {"id": row[0], |
||
1190 | "name": row[1], |
||
1191 | "parameter_type": row[2], |
||
1192 | "constant": constant, |
||
1193 | "point": point, |
||
1194 | "numerator_meter": numerator_meter, |
||
1195 | "denominator_meter": denominator_meter} |
||
1196 | |||
1197 | resp.text = json.dumps(meta_result) |
||
1198 | |||
1199 | @staticmethod |
||
1200 | @user_logger |
||
1201 | def on_delete(req, resp, id_, pid): |
||
1202 | admin_control(req) |
||
1203 | if not id_.isdigit() or int(id_) <= 0: |
||
1204 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1205 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1206 | |||
1207 | if not pid.isdigit() or int(pid) <= 0: |
||
1208 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1209 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1210 | |||
1211 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1212 | cursor = cnx.cursor() |
||
1213 | |||
1214 | cursor.execute(" SELECT name " |
||
1215 | " FROM tbl_combined_equipments " |
||
1216 | " WHERE id = %s ", |
||
1217 | (id_,)) |
||
1218 | row = cursor.fetchone() |
||
1219 | if row is None: |
||
1220 | cursor.close() |
||
1221 | cnx.close() |
||
1222 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1223 | title='API.NOT_FOUND', |
||
1224 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1225 | |||
1226 | cursor.execute(" SELECT name " |
||
1227 | " FROM tbl_combined_equipments_parameters " |
||
1228 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
1229 | (id_, pid,)) |
||
1230 | row = cursor.fetchone() |
||
1231 | if row is None: |
||
1232 | cursor.close() |
||
1233 | cnx.close() |
||
1234 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1235 | title='API.NOT_FOUND', |
||
1236 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1237 | |||
1238 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
||
1239 | " WHERE id = %s ", (pid,)) |
||
1240 | cnx.commit() |
||
1241 | |||
1242 | cursor.close() |
||
1243 | cnx.close() |
||
1244 | |||
1245 | resp.status = falcon.HTTP_204 |
||
1246 | |||
1247 | @staticmethod |
||
1248 | @user_logger |
||
1249 | def on_put(req, resp, id_, pid): |
||
1250 | """Handles PUT requests""" |
||
1251 | admin_control(req) |
||
1252 | if not id_.isdigit() or int(id_) <= 0: |
||
1253 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1254 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1255 | |||
1256 | if not pid.isdigit() or int(pid) <= 0: |
||
1257 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1258 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1259 | |||
1260 | try: |
||
1261 | raw_json = req.stream.read().decode('utf-8') |
||
1262 | except Exception as ex: |
||
1263 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1264 | title='API.BAD_REQUEST', |
||
1265 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1266 | |||
1267 | new_values = json.loads(raw_json) |
||
1268 | |||
1269 | if 'name' not in new_values['data'].keys() or \ |
||
1270 | not isinstance(new_values['data']['name'], str) or \ |
||
1271 | len(str.strip(new_values['data']['name'])) == 0: |
||
1272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1273 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
1274 | name = str.strip(new_values['data']['name']) |
||
1275 | |||
1276 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
1277 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
1278 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
1279 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1280 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
1281 | |||
1282 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
1283 | |||
1284 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
1285 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1286 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
1287 | |||
1288 | constant = None |
||
1289 | if 'constant' in new_values['data'].keys(): |
||
1290 | if new_values['data']['constant'] is not None and \ |
||
1291 | isinstance(new_values['data']['constant'], str) and \ |
||
1292 | len(str.strip(new_values['data']['constant'])) > 0: |
||
1293 | constant = str.strip(new_values['data']['constant']) |
||
1294 | |||
1295 | point_id = None |
||
1296 | if 'point_id' in new_values['data'].keys(): |
||
1297 | if new_values['data']['point_id'] is not None and \ |
||
1298 | new_values['data']['point_id'] <= 0: |
||
1299 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1300 | description='API.INVALID_POINT_ID') |
||
1301 | point_id = new_values['data']['point_id'] |
||
1302 | |||
1303 | numerator_meter_uuid = None |
||
1304 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
1305 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
1306 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
1307 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
1308 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
1309 | |||
1310 | denominator_meter_uuid = None |
||
1311 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
1312 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
1313 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
1314 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
1315 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
1316 | |||
1317 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1318 | cursor = cnx.cursor() |
||
1319 | |||
1320 | cursor.execute(" SELECT name " |
||
1321 | " FROM tbl_combined_equipments " |
||
1322 | " WHERE id = %s ", (id_,)) |
||
1323 | if cursor.fetchone() is None: |
||
1324 | cursor.close() |
||
1325 | cnx.close() |
||
1326 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
1327 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1328 | |||
1329 | cursor.execute(" SELECT name " |
||
1330 | " FROM tbl_combined_equipments_parameters " |
||
1331 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
1332 | (id_, pid,)) |
||
1333 | row = cursor.fetchone() |
||
1334 | if row is None: |
||
1335 | cursor.close() |
||
1336 | cnx.close() |
||
1337 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1338 | title='API.NOT_FOUND', |
||
1339 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1340 | |||
1341 | cursor.execute(" SELECT name " |
||
1342 | " FROM tbl_combined_equipments_parameters " |
||
1343 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
||
1344 | row = cursor.fetchone() |
||
1345 | if row is not None: |
||
1346 | cursor.close() |
||
1347 | cnx.close() |
||
1348 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1349 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
1350 | |||
1351 | # validate by parameter type |
||
1352 | if parameter_type == 'point': |
||
1353 | if point_id is None: |
||
1354 | cursor.close() |
||
1355 | cnx.close() |
||
1356 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1357 | description='API.INVALID_POINT_ID') |
||
1358 | |||
1359 | query = (" SELECT id, name " |
||
1360 | " FROM tbl_points " |
||
1361 | " WHERE id = %s ") |
||
1362 | cursor.execute(query, (point_id,)) |
||
1363 | row = cursor.fetchone() |
||
1364 | if row is None: |
||
1365 | cursor.close() |
||
1366 | cnx.close() |
||
1367 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1368 | description='API.POINT_NOT_FOUND') |
||
1369 | |||
1370 | elif parameter_type == 'constant': |
||
1371 | if constant is None: |
||
1372 | cursor.close() |
||
1373 | cnx.close() |
||
1374 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1375 | description='API.INVALID_CONSTANT_VALUE') |
||
1376 | |||
1377 | elif parameter_type == 'fraction': |
||
1378 | |||
1379 | query = (" SELECT id, name, uuid " |
||
1380 | " FROM tbl_meters ") |
||
1381 | cursor.execute(query) |
||
1382 | rows_meters = cursor.fetchall() |
||
1383 | |||
1384 | meter_dict = dict() |
||
1385 | if rows_meters is not None and len(rows_meters) > 0: |
||
1386 | for row in rows_meters: |
||
1387 | meter_dict[row[2]] = {"type": 'meter', |
||
1388 | "id": row[0], |
||
1389 | "name": row[1], |
||
1390 | "uuid": row[2]} |
||
1391 | |||
1392 | query = (" SELECT id, name, uuid " |
||
1393 | " FROM tbl_offline_meters ") |
||
1394 | cursor.execute(query) |
||
1395 | rows_offline_meters = cursor.fetchall() |
||
1396 | |||
1397 | offline_meter_dict = dict() |
||
1398 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1399 | for row in rows_offline_meters: |
||
1400 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1401 | "id": row[0], |
||
1402 | "name": row[1], |
||
1403 | "uuid": row[2]} |
||
1404 | |||
1405 | query = (" SELECT id, name, uuid " |
||
1406 | " FROM tbl_virtual_meters ") |
||
1407 | cursor.execute(query) |
||
1408 | rows_virtual_meters = cursor.fetchall() |
||
1409 | |||
1410 | virtual_meter_dict = dict() |
||
1411 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1412 | for row in rows_virtual_meters: |
||
1413 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1414 | "id": row[0], |
||
1415 | "name": row[1], |
||
1416 | "uuid": row[2]} |
||
1417 | |||
1418 | # validate numerator meter uuid |
||
1419 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
1420 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
1421 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
1422 | cursor.close() |
||
1423 | cnx.close() |
||
1424 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1425 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
1426 | |||
1427 | # validate denominator meter uuid |
||
1428 | if denominator_meter_uuid == numerator_meter_uuid: |
||
1429 | cursor.close() |
||
1430 | cnx.close() |
||
1431 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1432 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1433 | |||
1434 | if denominator_meter_uuid not in meter_dict and \ |
||
1435 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
1436 | denominator_meter_uuid not in offline_meter_dict: |
||
1437 | cursor.close() |
||
1438 | cnx.close() |
||
1439 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1440 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1441 | |||
1442 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
||
1443 | " SET name = %s , parameter_type = %s, constant = %s, " |
||
1444 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
||
1445 | " WHERE id = %s ") |
||
1446 | cursor.execute(add_values, (name, |
||
1447 | parameter_type, |
||
1448 | constant, |
||
1449 | point_id, |
||
1450 | numerator_meter_uuid, |
||
1451 | denominator_meter_uuid, |
||
1452 | pid)) |
||
1453 | cnx.commit() |
||
1454 | |||
1455 | cursor.close() |
||
1456 | cnx.close() |
||
1457 | |||
1458 | resp.status = falcon.HTTP_200 |
||
1459 | |||
1460 | |||
1461 | View Code Duplication | class CombinedEquipmentMeterCollection: |
|
1462 | @staticmethod |
||
1463 | def __init__(): |
||
1464 | """"Initializes CombinedEquipmentMeterCollection""" |
||
1465 | pass |
||
1466 | |||
1467 | @staticmethod |
||
1468 | def on_options(req, resp, id_): |
||
1469 | resp.status = falcon.HTTP_200 |
||
1470 | |||
1471 | @staticmethod |
||
1472 | def on_get(req, resp, id_): |
||
1473 | if 'API-KEY' not in req.headers or \ |
||
1474 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1475 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1476 | access_control(req) |
||
1477 | else: |
||
1478 | api_key_control(req) |
||
1479 | if not id_.isdigit() or int(id_) <= 0: |
||
1480 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1481 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1482 | |||
1483 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1484 | cursor = cnx.cursor() |
||
1485 | |||
1486 | cursor.execute(" SELECT name " |
||
1487 | " FROM tbl_combined_equipments " |
||
1488 | " WHERE id = %s ", (id_,)) |
||
1489 | if cursor.fetchone() is None: |
||
1490 | cursor.close() |
||
1491 | cnx.close() |
||
1492 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1493 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1494 | |||
1495 | query = (" SELECT id, name, uuid " |
||
1496 | " FROM tbl_energy_categories ") |
||
1497 | cursor.execute(query) |
||
1498 | rows_energy_categories = cursor.fetchall() |
||
1499 | |||
1500 | energy_category_dict = dict() |
||
1501 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1502 | for row in rows_energy_categories: |
||
1503 | energy_category_dict[row[0]] = {"id": row[0], |
||
1504 | "name": row[1], |
||
1505 | "uuid": row[2]} |
||
1506 | |||
1507 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1508 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
1509 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
1510 | " ORDER BY m.id ") |
||
1511 | cursor.execute(query, (id_,)) |
||
1512 | rows = cursor.fetchall() |
||
1513 | |||
1514 | result = list() |
||
1515 | if rows is not None and len(rows) > 0: |
||
1516 | for row in rows: |
||
1517 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
1518 | "energy_category": energy_category_dict.get(row[3], None), |
||
1519 | "is_output": bool(row[4])} |
||
1520 | result.append(meta_result) |
||
1521 | |||
1522 | cursor.close() |
||
1523 | cnx.close() |
||
1524 | |||
1525 | resp.text = json.dumps(result) |
||
1526 | |||
1527 | @staticmethod |
||
1528 | @user_logger |
||
1529 | def on_post(req, resp, id_): |
||
1530 | """Handles POST requests""" |
||
1531 | admin_control(req) |
||
1532 | try: |
||
1533 | raw_json = req.stream.read().decode('utf-8') |
||
1534 | except Exception as ex: |
||
1535 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1536 | title='API.BAD_REQUEST', |
||
1537 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1538 | |||
1539 | if not id_.isdigit() or int(id_) <= 0: |
||
1540 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1541 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1542 | |||
1543 | new_values = json.loads(raw_json) |
||
1544 | |||
1545 | if 'meter_id' not in new_values['data'].keys() or \ |
||
1546 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
1547 | new_values['data']['meter_id'] <= 0: |
||
1548 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1549 | description='API.INVALID_METER_ID') |
||
1550 | meter_id = new_values['data']['meter_id'] |
||
1551 | |||
1552 | if 'is_output' not in new_values['data'].keys() or \ |
||
1553 | not isinstance(new_values['data']['is_output'], bool): |
||
1554 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1555 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
1556 | is_output = new_values['data']['is_output'] |
||
1557 | |||
1558 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1559 | cursor = cnx.cursor() |
||
1560 | |||
1561 | cursor.execute(" SELECT name " |
||
1562 | " from tbl_combined_equipments " |
||
1563 | " WHERE id = %s ", (id_,)) |
||
1564 | if cursor.fetchone() is None: |
||
1565 | cursor.close() |
||
1566 | cnx.close() |
||
1567 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1568 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1569 | |||
1570 | cursor.execute(" SELECT name " |
||
1571 | " FROM tbl_meters " |
||
1572 | " WHERE id = %s ", (meter_id,)) |
||
1573 | if cursor.fetchone() is None: |
||
1574 | cursor.close() |
||
1575 | cnx.close() |
||
1576 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1577 | description='API.METER_NOT_FOUND') |
||
1578 | |||
1579 | query = (" SELECT id " |
||
1580 | " FROM tbl_combined_equipments_meters " |
||
1581 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
1582 | cursor.execute(query, (id_, meter_id,)) |
||
1583 | if cursor.fetchone() is not None: |
||
1584 | cursor.close() |
||
1585 | cnx.close() |
||
1586 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1587 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
1588 | |||
1589 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
1590 | " VALUES (%s, %s, %s) ") |
||
1591 | cursor.execute(add_row, (id_, meter_id, is_output)) |
||
1592 | cnx.commit() |
||
1593 | cursor.close() |
||
1594 | cnx.close() |
||
1595 | |||
1596 | resp.status = falcon.HTTP_201 |
||
1597 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
||
1598 | |||
1599 | |||
1600 | View Code Duplication | class CombinedEquipmentMeterItem: |
|
1601 | @staticmethod |
||
1602 | def __init__(): |
||
1603 | """"Initializes CombinedEquipmentMeterItem""" |
||
1604 | pass |
||
1605 | |||
1606 | @staticmethod |
||
1607 | def on_options(req, resp, id_, mid): |
||
1608 | resp.status = falcon.HTTP_200 |
||
1609 | |||
1610 | @staticmethod |
||
1611 | @user_logger |
||
1612 | def on_delete(req, resp, id_, mid): |
||
1613 | admin_control(req) |
||
1614 | if not id_.isdigit() or int(id_) <= 0: |
||
1615 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1616 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1617 | |||
1618 | if not mid.isdigit() or int(mid) <= 0: |
||
1619 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1620 | description='API.INVALID_METER_ID') |
||
1621 | |||
1622 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1623 | cursor = cnx.cursor() |
||
1624 | |||
1625 | cursor.execute(" SELECT name " |
||
1626 | " FROM tbl_combined_equipments " |
||
1627 | " WHERE id = %s ", (id_,)) |
||
1628 | if cursor.fetchone() is None: |
||
1629 | cursor.close() |
||
1630 | cnx.close() |
||
1631 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1632 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1633 | |||
1634 | cursor.execute(" SELECT name " |
||
1635 | " FROM tbl_meters " |
||
1636 | " WHERE id = %s ", (mid,)) |
||
1637 | if cursor.fetchone() is None: |
||
1638 | cursor.close() |
||
1639 | cnx.close() |
||
1640 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1641 | description='API.METER_NOT_FOUND') |
||
1642 | |||
1643 | cursor.execute(" SELECT id " |
||
1644 | " FROM tbl_combined_equipments_meters " |
||
1645 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
1646 | if cursor.fetchone() is None: |
||
1647 | cursor.close() |
||
1648 | cnx.close() |
||
1649 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1650 | description='API.COMBINED_EQUIPMENT_METER_RELATION_NOT_FOUND') |
||
1651 | |||
1652 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters " |
||
1653 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
1654 | cnx.commit() |
||
1655 | |||
1656 | cursor.close() |
||
1657 | cnx.close() |
||
1658 | |||
1659 | resp.status = falcon.HTTP_204 |
||
1660 | |||
1661 | |||
1662 | View Code Duplication | class CombinedEquipmentOfflineMeterCollection: |
|
1663 | @staticmethod |
||
1664 | def __init__(): |
||
1665 | """"Initializes CombinedEquipmentOfflineMeterCollection""" |
||
1666 | pass |
||
1667 | |||
1668 | @staticmethod |
||
1669 | def on_options(req, resp, id_): |
||
1670 | resp.status = falcon.HTTP_200 |
||
1671 | |||
1672 | @staticmethod |
||
1673 | def on_get(req, resp, id_): |
||
1674 | if 'API-KEY' not in req.headers or \ |
||
1675 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1676 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1677 | access_control(req) |
||
1678 | else: |
||
1679 | api_key_control(req) |
||
1680 | if not id_.isdigit() or int(id_) <= 0: |
||
1681 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1682 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1683 | |||
1684 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1685 | cursor = cnx.cursor() |
||
1686 | |||
1687 | cursor.execute(" SELECT name " |
||
1688 | " FROM tbl_combined_equipments " |
||
1689 | " WHERE id = %s ", (id_,)) |
||
1690 | if cursor.fetchone() is None: |
||
1691 | cursor.close() |
||
1692 | cnx.close() |
||
1693 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1694 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1695 | |||
1696 | query = (" SELECT id, name, uuid " |
||
1697 | " FROM tbl_energy_categories ") |
||
1698 | cursor.execute(query) |
||
1699 | rows_energy_categories = cursor.fetchall() |
||
1700 | |||
1701 | energy_category_dict = dict() |
||
1702 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1703 | for row in rows_energy_categories: |
||
1704 | energy_category_dict[row[0]] = {"id": row[0], |
||
1705 | "name": row[1], |
||
1706 | "uuid": row[2]} |
||
1707 | |||
1708 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1709 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
1710 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
1711 | " ORDER BY m.id ") |
||
1712 | cursor.execute(query, (id_,)) |
||
1713 | rows = cursor.fetchall() |
||
1714 | |||
1715 | result = list() |
||
1716 | if rows is not None and len(rows) > 0: |
||
1717 | for row in rows: |
||
1718 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
1719 | "energy_category": energy_category_dict.get(row[3], None), |
||
1720 | "is_output": bool(row[4])} |
||
1721 | result.append(meta_result) |
||
1722 | |||
1723 | cursor.close() |
||
1724 | cnx.close() |
||
1725 | |||
1726 | resp.text = json.dumps(result) |
||
1727 | |||
1728 | @staticmethod |
||
1729 | @user_logger |
||
1730 | def on_post(req, resp, id_): |
||
1731 | """Handles POST requests""" |
||
1732 | admin_control(req) |
||
1733 | try: |
||
1734 | raw_json = req.stream.read().decode('utf-8') |
||
1735 | except Exception as ex: |
||
1736 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1737 | title='API.BAD_REQUEST', |
||
1738 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1739 | |||
1740 | if not id_.isdigit() or int(id_) <= 0: |
||
1741 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1742 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1743 | |||
1744 | new_values = json.loads(raw_json) |
||
1745 | |||
1746 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
1747 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
1748 | new_values['data']['offline_meter_id'] <= 0: |
||
1749 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1750 | description='API.INVALID_OFFLINE_METER_ID') |
||
1751 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
1752 | |||
1753 | if 'is_output' not in new_values['data'].keys() or \ |
||
1754 | not isinstance(new_values['data']['is_output'], bool): |
||
1755 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1756 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
1757 | is_output = new_values['data']['is_output'] |
||
1758 | |||
1759 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1760 | cursor = cnx.cursor() |
||
1761 | |||
1762 | cursor.execute(" SELECT name " |
||
1763 | " from tbl_combined_equipments " |
||
1764 | " WHERE id = %s ", (id_,)) |
||
1765 | if cursor.fetchone() is None: |
||
1766 | cursor.close() |
||
1767 | cnx.close() |
||
1768 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1769 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1770 | |||
1771 | cursor.execute(" SELECT name " |
||
1772 | " FROM tbl_offline_meters " |
||
1773 | " WHERE id = %s ", (offline_meter_id,)) |
||
1774 | if cursor.fetchone() is None: |
||
1775 | cursor.close() |
||
1776 | cnx.close() |
||
1777 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1778 | description='API.OFFLINE_METER_NOT_FOUND') |
||
1779 | |||
1780 | query = (" SELECT id " |
||
1781 | " FROM tbl_combined_equipments_offline_meters " |
||
1782 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
1783 | cursor.execute(query, (id_, offline_meter_id,)) |
||
1784 | if cursor.fetchone() is not None: |
||
1785 | cursor.close() |
||
1786 | cnx.close() |
||
1787 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1788 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
1789 | |||
1790 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
1791 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
1792 | " VALUES (%s, %s, %s) ") |
||
1793 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
||
1794 | cnx.commit() |
||
1795 | cursor.close() |
||
1796 | cnx.close() |
||
1797 | |||
1798 | resp.status = falcon.HTTP_201 |
||
1799 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
1800 | |||
1801 | |||
1802 | View Code Duplication | class CombinedEquipmentOfflineMeterItem: |
|
1803 | @staticmethod |
||
1804 | def __init__(): |
||
1805 | """"Initializes CombinedEquipmentOfflineMeterItem""" |
||
1806 | pass |
||
1807 | |||
1808 | @staticmethod |
||
1809 | def on_options(req, resp, id_, mid): |
||
1810 | resp.status = falcon.HTTP_200 |
||
1811 | |||
1812 | @staticmethod |
||
1813 | @user_logger |
||
1814 | def on_delete(req, resp, id_, mid): |
||
1815 | admin_control(req) |
||
1816 | if not id_.isdigit() or int(id_) <= 0: |
||
1817 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1818 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1819 | |||
1820 | if not mid.isdigit() or int(mid) <= 0: |
||
1821 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1822 | description='API.INVALID_OFFLINE_METER_ID') |
||
1823 | |||
1824 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1825 | cursor = cnx.cursor() |
||
1826 | |||
1827 | cursor.execute(" SELECT name " |
||
1828 | " FROM tbl_combined_equipments " |
||
1829 | " WHERE id = %s ", (id_,)) |
||
1830 | if cursor.fetchone() is None: |
||
1831 | cursor.close() |
||
1832 | cnx.close() |
||
1833 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1834 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1835 | |||
1836 | cursor.execute(" SELECT name " |
||
1837 | " FROM tbl_offline_meters " |
||
1838 | " WHERE id = %s ", (mid,)) |
||
1839 | if cursor.fetchone() is None: |
||
1840 | cursor.close() |
||
1841 | cnx.close() |
||
1842 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1843 | description='API.OFFLINE_METER_NOT_FOUND') |
||
1844 | |||
1845 | cursor.execute(" SELECT id " |
||
1846 | " FROM tbl_combined_equipments_offline_meters " |
||
1847 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
1848 | if cursor.fetchone() is None: |
||
1849 | cursor.close() |
||
1850 | cnx.close() |
||
1851 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1852 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
||
1853 | |||
1854 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters " |
||
1855 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
1856 | cnx.commit() |
||
1857 | |||
1858 | cursor.close() |
||
1859 | cnx.close() |
||
1860 | |||
1861 | resp.status = falcon.HTTP_204 |
||
1862 | |||
1863 | |||
1864 | View Code Duplication | class CombinedEquipmentVirtualMeterCollection: |
|
1865 | @staticmethod |
||
1866 | def __init__(): |
||
1867 | """"Initializes CombinedEquipmentVirtualMeterCollection""" |
||
1868 | pass |
||
1869 | |||
1870 | @staticmethod |
||
1871 | def on_options(req, resp, id_): |
||
1872 | resp.status = falcon.HTTP_200 |
||
1873 | |||
1874 | @staticmethod |
||
1875 | def on_get(req, resp, id_): |
||
1876 | if 'API-KEY' not in req.headers or \ |
||
1877 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1878 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1879 | access_control(req) |
||
1880 | else: |
||
1881 | api_key_control(req) |
||
1882 | if not id_.isdigit() or int(id_) <= 0: |
||
1883 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1884 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1885 | |||
1886 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1887 | cursor = cnx.cursor() |
||
1888 | |||
1889 | cursor.execute(" SELECT name " |
||
1890 | " FROM tbl_combined_equipments " |
||
1891 | " WHERE id = %s ", (id_,)) |
||
1892 | if cursor.fetchone() is None: |
||
1893 | cursor.close() |
||
1894 | cnx.close() |
||
1895 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1896 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1897 | |||
1898 | query = (" SELECT id, name, uuid " |
||
1899 | " FROM tbl_energy_categories ") |
||
1900 | cursor.execute(query) |
||
1901 | rows_energy_categories = cursor.fetchall() |
||
1902 | |||
1903 | energy_category_dict = dict() |
||
1904 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1905 | for row in rows_energy_categories: |
||
1906 | energy_category_dict[row[0]] = {"id": row[0], |
||
1907 | "name": row[1], |
||
1908 | "uuid": row[2]} |
||
1909 | |||
1910 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1911 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
1912 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
1913 | " ORDER BY m.id ") |
||
1914 | cursor.execute(query, (id_,)) |
||
1915 | rows = cursor.fetchall() |
||
1916 | |||
1917 | result = list() |
||
1918 | if rows is not None and len(rows) > 0: |
||
1919 | for row in rows: |
||
1920 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
1921 | "energy_category": energy_category_dict.get(row[3], None), |
||
1922 | "is_output": bool(row[4])} |
||
1923 | result.append(meta_result) |
||
1924 | |||
1925 | cursor.close() |
||
1926 | cnx.close() |
||
1927 | resp.text = json.dumps(result) |
||
1928 | |||
1929 | @staticmethod |
||
1930 | @user_logger |
||
1931 | def on_post(req, resp, id_): |
||
1932 | """Handles POST requests""" |
||
1933 | admin_control(req) |
||
1934 | try: |
||
1935 | raw_json = req.stream.read().decode('utf-8') |
||
1936 | except Exception as ex: |
||
1937 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1938 | title='API.BAD_REQUEST', |
||
1939 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1940 | |||
1941 | if not id_.isdigit() or int(id_) <= 0: |
||
1942 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1943 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1944 | |||
1945 | new_values = json.loads(raw_json) |
||
1946 | |||
1947 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
1948 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
1949 | new_values['data']['virtual_meter_id'] <= 0: |
||
1950 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1951 | description='API.INVALID_VIRTUAL_METER_ID') |
||
1952 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
1953 | |||
1954 | if 'is_output' not in new_values['data'].keys() or \ |
||
1955 | not isinstance(new_values['data']['is_output'], bool): |
||
1956 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1957 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
1958 | is_output = new_values['data']['is_output'] |
||
1959 | |||
1960 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1961 | cursor = cnx.cursor() |
||
1962 | |||
1963 | cursor.execute(" SELECT name " |
||
1964 | " from tbl_combined_equipments " |
||
1965 | " WHERE id = %s ", (id_,)) |
||
1966 | if cursor.fetchone() is None: |
||
1967 | cursor.close() |
||
1968 | cnx.close() |
||
1969 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1970 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1971 | |||
1972 | cursor.execute(" SELECT name " |
||
1973 | " FROM tbl_virtual_meters " |
||
1974 | " WHERE id = %s ", (virtual_meter_id,)) |
||
1975 | if cursor.fetchone() is None: |
||
1976 | cursor.close() |
||
1977 | cnx.close() |
||
1978 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1979 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
1980 | |||
1981 | query = (" SELECT id " |
||
1982 | " FROM tbl_combined_equipments_virtual_meters " |
||
1983 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
1984 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
1985 | if cursor.fetchone() is not None: |
||
1986 | cursor.close() |
||
1987 | cnx.close() |
||
1988 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1989 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
1990 | |||
1991 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
1992 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
1993 | " VALUES (%s, %s, %s) ") |
||
1994 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
||
1995 | cnx.commit() |
||
1996 | cursor.close() |
||
1997 | cnx.close() |
||
1998 | |||
1999 | resp.status = falcon.HTTP_201 |
||
2000 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
2001 | |||
2002 | |||
2003 | View Code Duplication | class CombinedEquipmentVirtualMeterItem: |
|
2004 | @staticmethod |
||
2005 | def __init__(): |
||
2006 | """"Initializes CombinedEquipmentVirtualMeterItem""" |
||
2007 | pass |
||
2008 | |||
2009 | @staticmethod |
||
2010 | def on_options(req, resp, id_, mid): |
||
2011 | resp.status = falcon.HTTP_200 |
||
2012 | |||
2013 | @staticmethod |
||
2014 | @user_logger |
||
2015 | def on_delete(req, resp, id_, mid): |
||
2016 | admin_control(req) |
||
2017 | if not id_.isdigit() or int(id_) <= 0: |
||
2018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2019 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2020 | |||
2021 | if not mid.isdigit() or int(mid) <= 0: |
||
2022 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2023 | description='API.INVALID_VIRTUAL_METER_ID') |
||
2024 | |||
2025 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2026 | cursor = cnx.cursor() |
||
2027 | |||
2028 | cursor.execute(" SELECT name " |
||
2029 | " FROM tbl_combined_equipments " |
||
2030 | " WHERE id = %s ", (id_,)) |
||
2031 | if cursor.fetchone() is None: |
||
2032 | cursor.close() |
||
2033 | cnx.close() |
||
2034 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2035 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2036 | |||
2037 | cursor.execute(" SELECT name " |
||
2038 | " FROM tbl_virtual_meters " |
||
2039 | " WHERE id = %s ", (mid,)) |
||
2040 | if cursor.fetchone() is None: |
||
2041 | cursor.close() |
||
2042 | cnx.close() |
||
2043 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2044 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
2045 | |||
2046 | cursor.execute(" SELECT id " |
||
2047 | " FROM tbl_combined_equipments_virtual_meters " |
||
2048 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
2049 | if cursor.fetchone() is None: |
||
2050 | cursor.close() |
||
2051 | cnx.close() |
||
2052 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2053 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
2054 | |||
2055 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters " |
||
2056 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
2057 | cnx.commit() |
||
2058 | |||
2059 | cursor.close() |
||
2060 | cnx.close() |
||
2061 | |||
2062 | resp.status = falcon.HTTP_204 |
||
2063 | |||
2064 | |||
2065 | View Code Duplication | class CombinedEquipmentCommandCollection: |
|
2066 | @staticmethod |
||
2067 | def __init__(): |
||
2068 | """Initializes Class""" |
||
2069 | pass |
||
2070 | |||
2071 | @staticmethod |
||
2072 | def on_options(req, resp, id_): |
||
2073 | resp.status = falcon.HTTP_200 |
||
2074 | |||
2075 | @staticmethod |
||
2076 | def on_get(req, resp, id_): |
||
2077 | if 'API-KEY' not in req.headers or \ |
||
2078 | not isinstance(req.headers['API-KEY'], str) or \ |
||
2079 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
2080 | access_control(req) |
||
2081 | else: |
||
2082 | api_key_control(req) |
||
2083 | if not id_.isdigit() or int(id_) <= 0: |
||
2084 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2085 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2086 | |||
2087 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2088 | cursor = cnx.cursor() |
||
2089 | |||
2090 | cursor.execute(" SELECT name " |
||
2091 | " FROM tbl_combined_equipments " |
||
2092 | " WHERE id = %s ", (id_,)) |
||
2093 | if cursor.fetchone() is None: |
||
2094 | cursor.close() |
||
2095 | cnx.close() |
||
2096 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2097 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2098 | |||
2099 | query = (" SELECT c.id, c.name, c.uuid " |
||
2100 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
2101 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
2102 | " ORDER BY c.id ") |
||
2103 | cursor.execute(query, (id_,)) |
||
2104 | rows = cursor.fetchall() |
||
2105 | |||
2106 | result = list() |
||
2107 | if rows is not None and len(rows) > 0: |
||
2108 | for row in rows: |
||
2109 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2110 | result.append(meta_result) |
||
2111 | |||
2112 | resp.text = json.dumps(result) |
||
2113 | |||
2114 | @staticmethod |
||
2115 | @user_logger |
||
2116 | def on_post(req, resp, id_): |
||
2117 | """Handles POST requests""" |
||
2118 | admin_control(req) |
||
2119 | try: |
||
2120 | raw_json = req.stream.read().decode('utf-8') |
||
2121 | except Exception as ex: |
||
2122 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
2123 | title='API.BAD_REQUEST', |
||
2124 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
2125 | |||
2126 | if not id_.isdigit() or int(id_) <= 0: |
||
2127 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2128 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2129 | |||
2130 | new_values = json.loads(raw_json) |
||
2131 | |||
2132 | if 'command_id' not in new_values['data'].keys() or \ |
||
2133 | not isinstance(new_values['data']['command_id'], int) or \ |
||
2134 | new_values['data']['command_id'] <= 0: |
||
2135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2136 | description='API.INVALID_COMMAND_ID') |
||
2137 | command_id = new_values['data']['command_id'] |
||
2138 | |||
2139 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2140 | cursor = cnx.cursor() |
||
2141 | |||
2142 | cursor.execute(" SELECT name " |
||
2143 | " from tbl_combined_equipments " |
||
2144 | " WHERE id = %s ", (id_,)) |
||
2145 | if cursor.fetchone() is None: |
||
2146 | cursor.close() |
||
2147 | cnx.close() |
||
2148 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2149 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2150 | |||
2151 | cursor.execute(" SELECT name " |
||
2152 | " FROM tbl_commands " |
||
2153 | " WHERE id = %s ", (command_id,)) |
||
2154 | if cursor.fetchone() is None: |
||
2155 | cursor.close() |
||
2156 | cnx.close() |
||
2157 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2158 | description='API.COMMAND_NOT_FOUND') |
||
2159 | |||
2160 | query = (" SELECT id " |
||
2161 | " FROM tbl_combined_equipments_commands " |
||
2162 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
2163 | cursor.execute(query, (id_, command_id,)) |
||
2164 | if cursor.fetchone() is not None: |
||
2165 | cursor.close() |
||
2166 | cnx.close() |
||
2167 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2168 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
2169 | |||
2170 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
2171 | " VALUES (%s, %s) ") |
||
2172 | cursor.execute(add_row, (id_, command_id,)) |
||
2173 | cnx.commit() |
||
2174 | cursor.close() |
||
2175 | cnx.close() |
||
2176 | |||
2177 | resp.status = falcon.HTTP_201 |
||
2178 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
||
2179 | |||
2180 | |||
2181 | View Code Duplication | class CombinedEquipmentCommandItem: |
|
2182 | @staticmethod |
||
2183 | def __init__(): |
||
2184 | """Initializes Class""" |
||
2185 | pass |
||
2186 | |||
2187 | @staticmethod |
||
2188 | def on_options(req, resp, id_, cid): |
||
2189 | resp.status = falcon.HTTP_200 |
||
2190 | |||
2191 | @staticmethod |
||
2192 | @user_logger |
||
2193 | def on_delete(req, resp, id_, cid): |
||
2194 | admin_control(req) |
||
2195 | if not id_.isdigit() or int(id_) <= 0: |
||
2196 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2197 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2198 | |||
2199 | if not cid.isdigit() or int(cid) <= 0: |
||
2200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2201 | description='API.INVALID_COMMAND_ID') |
||
2202 | |||
2203 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2204 | cursor = cnx.cursor() |
||
2205 | |||
2206 | cursor.execute(" SELECT name " |
||
2207 | " FROM tbl_combined_equipments " |
||
2208 | " WHERE id = %s ", (id_,)) |
||
2209 | if cursor.fetchone() is None: |
||
2210 | cursor.close() |
||
2211 | cnx.close() |
||
2212 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2213 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2214 | |||
2215 | cursor.execute(" SELECT name " |
||
2216 | " FROM tbl_commands " |
||
2217 | " WHERE id = %s ", (cid,)) |
||
2218 | if cursor.fetchone() is None: |
||
2219 | cursor.close() |
||
2220 | cnx.close() |
||
2221 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2222 | description='API.COMMAND_NOT_FOUND') |
||
2223 | |||
2224 | cursor.execute(" SELECT id " |
||
2225 | " FROM tbl_combined_equipments_commands " |
||
2226 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
2227 | if cursor.fetchone() is None: |
||
2228 | cursor.close() |
||
2229 | cnx.close() |
||
2230 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2231 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_NOT_FOUND') |
||
2232 | |||
2233 | cursor.execute(" DELETE FROM tbl_combined_equipments_commands " |
||
2234 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
2235 | cnx.commit() |
||
2236 | |||
2237 | cursor.close() |
||
2238 | cnx.close() |
||
2239 | |||
2240 | resp.status = falcon.HTTP_204 |
||
2241 | |||
2242 | |||
2243 | class CombinedEquipmentExport: |
||
2244 | @staticmethod |
||
2245 | def __init__(): |
||
2246 | """Initializes CombinedEquipmentExport""" |
||
2247 | pass |
||
2248 | |||
2249 | @staticmethod |
||
2250 | def on_options(req, resp, id_): |
||
2251 | resp.status = falcon.HTTP_200 |
||
2252 | |||
2253 | @staticmethod |
||
2254 | def on_get(req, resp, id_): |
||
2255 | if 'API-KEY' not in req.headers or \ |
||
2256 | not isinstance(req.headers['API-KEY'], str) or \ |
||
2257 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
2258 | access_control(req) |
||
2259 | else: |
||
2260 | api_key_control(req) |
||
2261 | if not id_.isdigit() or int(id_) <= 0: |
||
2262 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2263 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2264 | |||
2265 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2266 | cursor = cnx.cursor() |
||
2267 | |||
2268 | query = (" SELECT id, name, uuid " |
||
2269 | " FROM tbl_cost_centers ") |
||
2270 | cursor.execute(query) |
||
2271 | rows_cost_centers = cursor.fetchall() |
||
2272 | |||
2273 | cost_center_dict = dict() |
||
2274 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
2275 | for row in rows_cost_centers: |
||
2276 | cost_center_dict[row[0]] = {"id": row[0], |
||
2277 | "name": row[1], |
||
2278 | "uuid": row[2]} |
||
2279 | |||
2280 | query = (" SELECT id, name, uuid, " |
||
2281 | " is_input_counted, is_output_counted, " |
||
2282 | " cost_center_id, svg, camera_url, description " |
||
2283 | " FROM tbl_combined_equipments " |
||
2284 | " WHERE id = %s ") |
||
2285 | cursor.execute(query, (id_,)) |
||
2286 | row = cursor.fetchone() |
||
2287 | |||
2288 | if row is None: |
||
2289 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2290 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2291 | else: |
||
2292 | meta_result = {"id": row[0], |
||
2293 | "name": row[1], |
||
2294 | "uuid": row[2], |
||
2295 | "is_input_counted": bool(row[3]), |
||
2296 | "is_output_counted": bool(row[4]), |
||
2297 | "cost_center": cost_center_dict.get(row[5], None), |
||
2298 | "svg": row[6], |
||
2299 | "camera_url": row[7], |
||
2300 | "description": row[8], |
||
2301 | "equipments": None, |
||
2302 | "commands": None, |
||
2303 | "meters": None, |
||
2304 | "offline_meters": None, |
||
2305 | "virtual_meters": None, |
||
2306 | "parameters": None} |
||
2307 | query = (" SELECT e.id, e.name, e.uuid " |
||
2308 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
2309 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
2310 | " ORDER BY e.id ") |
||
2311 | cursor.execute(query, (id_,)) |
||
2312 | rows = cursor.fetchall() |
||
2313 | |||
2314 | equipment_result = list() |
||
2315 | if rows is not None and len(rows) > 0: |
||
2316 | for row in rows: |
||
2317 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2318 | equipment_result.append(result) |
||
2319 | meta_result['equipments'] = equipment_result |
||
2320 | |||
2321 | query = (" SELECT c.id, c.name, c.uuid " |
||
2322 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
2323 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
2324 | " ORDER BY c.id ") |
||
2325 | cursor.execute(query, (id_,)) |
||
2326 | rows = cursor.fetchall() |
||
2327 | |||
2328 | command_result = list() |
||
2329 | if rows is not None and len(rows) > 0: |
||
2330 | for row in rows: |
||
2331 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2332 | command_result.append(result) |
||
2333 | meta_result['commands'] = command_result |
||
2334 | |||
2335 | query = (" SELECT id, name, uuid " |
||
2336 | " FROM tbl_energy_categories ") |
||
2337 | cursor.execute(query) |
||
2338 | rows_energy_categories = cursor.fetchall() |
||
2339 | |||
2340 | energy_category_dict = dict() |
||
2341 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2342 | for row in rows_energy_categories: |
||
2343 | energy_category_dict[row[0]] = {"id": row[0], |
||
2344 | "name": row[1], |
||
2345 | "uuid": row[2]} |
||
2346 | |||
2347 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
2348 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
2349 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
2350 | " ORDER BY m.id ") |
||
2351 | cursor.execute(query, (id_,)) |
||
2352 | rows = cursor.fetchall() |
||
2353 | |||
2354 | meter_result = list() |
||
2355 | if rows is not None and len(rows) > 0: |
||
2356 | for row in rows: |
||
2357 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2358 | "energy_category": energy_category_dict.get(row[3], None), |
||
2359 | "is_output": bool(row[4])} |
||
2360 | meter_result.append(result) |
||
2361 | meta_result['meters'] = meter_result |
||
2362 | |||
2363 | query = (" SELECT id, name, uuid " |
||
2364 | " FROM tbl_energy_categories ") |
||
2365 | cursor.execute(query) |
||
2366 | rows_energy_categories = cursor.fetchall() |
||
2367 | |||
2368 | energy_category_dict = dict() |
||
2369 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2370 | for row in rows_energy_categories: |
||
2371 | energy_category_dict[row[0]] = {"id": row[0], |
||
2372 | "name": row[1], |
||
2373 | "uuid": row[2]} |
||
2374 | |||
2375 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
2376 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
2377 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
2378 | " ORDER BY m.id ") |
||
2379 | cursor.execute(query, (id_,)) |
||
2380 | rows = cursor.fetchall() |
||
2381 | |||
2382 | offlinemeter_result = list() |
||
2383 | if rows is not None and len(rows) > 0: |
||
2384 | for row in rows: |
||
2385 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2386 | "energy_category": energy_category_dict.get(row[3], None), |
||
2387 | "is_output": bool(row[4])} |
||
2388 | offlinemeter_result.append(result) |
||
2389 | meta_result['offline_meters'] = offlinemeter_result |
||
2390 | |||
2391 | query = (" SELECT id, name, uuid " |
||
2392 | " FROM tbl_energy_categories ") |
||
2393 | cursor.execute(query) |
||
2394 | rows_energy_categories = cursor.fetchall() |
||
2395 | |||
2396 | energy_category_dict = dict() |
||
2397 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
2398 | for row in rows_energy_categories: |
||
2399 | energy_category_dict[row[0]] = {"id": row[0], |
||
2400 | "name": row[1], |
||
2401 | "uuid": row[2]} |
||
2402 | |||
2403 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
2404 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
2405 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
2406 | " ORDER BY m.id ") |
||
2407 | cursor.execute(query, (id_,)) |
||
2408 | rows = cursor.fetchall() |
||
2409 | |||
2410 | virtualmeter_result = list() |
||
2411 | if rows is not None and len(rows) > 0: |
||
2412 | for row in rows: |
||
2413 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
2414 | "energy_category": energy_category_dict.get(row[3], None), |
||
2415 | "is_output": bool(row[4])} |
||
2416 | virtualmeter_result.append(result) |
||
2417 | meta_result['virtual_meters'] = virtualmeter_result |
||
2418 | |||
2419 | query = (" SELECT id, name " |
||
2420 | " FROM tbl_points ") |
||
2421 | cursor.execute(query) |
||
2422 | rows_points = cursor.fetchall() |
||
2423 | |||
2424 | point_dict = dict() |
||
2425 | if rows_points is not None and len(rows_points) > 0: |
||
2426 | for row in rows_points: |
||
2427 | point_dict[row[0]] = {"id": row[0], |
||
2428 | "name": row[1]} |
||
2429 | |||
2430 | query = (" SELECT id, name, uuid " |
||
2431 | " FROM tbl_meters ") |
||
2432 | cursor.execute(query) |
||
2433 | rows_meters = cursor.fetchall() |
||
2434 | |||
2435 | meter_dict = dict() |
||
2436 | if rows_meters is not None and len(rows_meters) > 0: |
||
2437 | for row in rows_meters: |
||
2438 | meter_dict[row[2]] = {"type": 'meter', |
||
2439 | "id": row[0], |
||
2440 | "name": row[1], |
||
2441 | "uuid": row[2]} |
||
2442 | |||
2443 | query = (" SELECT id, name, uuid " |
||
2444 | " FROM tbl_offline_meters ") |
||
2445 | cursor.execute(query) |
||
2446 | rows_offline_meters = cursor.fetchall() |
||
2447 | |||
2448 | offline_meter_dict = dict() |
||
2449 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
2450 | for row in rows_offline_meters: |
||
2451 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
2452 | "id": row[0], |
||
2453 | "name": row[1], |
||
2454 | "uuid": row[2]} |
||
2455 | |||
2456 | query = (" SELECT id, name, uuid " |
||
2457 | " FROM tbl_virtual_meters ") |
||
2458 | cursor.execute(query) |
||
2459 | rows_virtual_meters = cursor.fetchall() |
||
2460 | |||
2461 | virtual_meter_dict = dict() |
||
2462 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
2463 | for row in rows_virtual_meters: |
||
2464 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
2465 | "id": row[0], |
||
2466 | "name": row[1], |
||
2467 | "uuid": row[2]} |
||
2468 | |||
2469 | query = (" SELECT id, name, parameter_type, " |
||
2470 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
2471 | " FROM tbl_combined_equipments_parameters " |
||
2472 | " WHERE combined_equipment_id = %s " |
||
2473 | " ORDER BY id ") |
||
2474 | cursor.execute(query, (id_,)) |
||
2475 | rows_parameters = cursor.fetchall() |
||
2476 | |||
2477 | parameter_result = list() |
||
2478 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
2479 | for row in rows_parameters: |
||
2480 | constant = None |
||
2481 | point = None |
||
2482 | numerator_meter = None |
||
2483 | denominator_meter = None |
||
2484 | if row[2] == 'point': |
||
2485 | point = point_dict.get(row[4], None) |
||
2486 | constant = None |
||
2487 | numerator_meter = None |
||
2488 | denominator_meter = None |
||
2489 | elif row[2] == 'constant': |
||
2490 | constant = row[3] |
||
2491 | point = None |
||
2492 | numerator_meter = None |
||
2493 | denominator_meter = None |
||
2494 | elif row[2] == 'fraction': |
||
2495 | constant = None |
||
2496 | point = None |
||
2497 | # find numerator meter by uuid |
||
2498 | numerator_meter = meter_dict.get(row[5], None) |
||
2499 | if numerator_meter is None: |
||
2500 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
2501 | if numerator_meter is None: |
||
2502 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
2503 | # find denominator meter by uuid |
||
2504 | denominator_meter = meter_dict.get(row[6], None) |
||
2505 | if denominator_meter is None: |
||
2506 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
2507 | if denominator_meter is None: |
||
2508 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
2509 | |||
2510 | result = {"id": row[0], |
||
2511 | "name": row[1], |
||
2512 | "parameter_type": row[2], |
||
2513 | "constant": constant, |
||
2514 | "point": point, |
||
2515 | "numerator_meter": numerator_meter, |
||
2516 | "denominator_meter": denominator_meter} |
||
2517 | parameter_result.append(result) |
||
2518 | meta_result['parameters'] = parameter_result |
||
2519 | |||
2520 | cursor.close() |
||
2521 | cnx.close() |
||
2522 | resp.text = json.dumps(meta_result) |
||
2523 | |||
2524 | |||
2525 | class CombinedEquipmentImport: |
||
2526 | @staticmethod |
||
2527 | def __init__(): |
||
2528 | """ Initializes CombinedEquipmentImport""" |
||
2529 | pass |
||
2530 | |||
2531 | @staticmethod |
||
2532 | def on_options(req, resp): |
||
2533 | resp.status = falcon.HTTP_200 |
||
2534 | |||
2535 | @staticmethod |
||
2536 | @user_logger |
||
2537 | def on_post(req, resp): |
||
2538 | """Handles POST requests""" |
||
2539 | admin_control(req) |
||
2540 | try: |
||
2541 | raw_json = req.stream.read().decode('utf-8') |
||
2542 | except Exception as ex: |
||
2543 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
2544 | title='API.BAD_REQUEST', |
||
2545 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
2546 | |||
2547 | new_values = json.loads(raw_json) |
||
2548 | |||
2549 | if 'name' not in new_values.keys() or \ |
||
2550 | not isinstance(new_values['name'], str) or \ |
||
2551 | len(str.strip(new_values['name'])) == 0: |
||
2552 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2553 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
2554 | name = str.strip(new_values['name']) |
||
2555 | |||
2556 | if 'is_input_counted' not in new_values.keys() or \ |
||
2557 | not isinstance(new_values['is_input_counted'], bool): |
||
2558 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2559 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
2560 | is_input_counted = new_values['is_input_counted'] |
||
2561 | |||
2562 | if 'is_output_counted' not in new_values.keys() or \ |
||
2563 | not isinstance(new_values['is_output_counted'], bool): |
||
2564 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2565 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
2566 | is_output_counted = new_values['is_output_counted'] |
||
2567 | |||
2568 | if 'id' not in new_values['cost_center'].keys() or \ |
||
2569 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
2570 | new_values['cost_center']['id'] <= 0: |
||
2571 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2572 | description='API.INVALID_COST_CENTER_ID') |
||
2573 | cost_center_id = new_values['cost_center']['id'] |
||
2574 | |||
2575 | if 'svg' not in new_values.keys() or \ |
||
2576 | not isinstance(new_values['svg'], str) or \ |
||
2577 | len(str.strip(new_values['svg'])) == 0: |
||
2578 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2579 | description='API.INVALID_SVG') |
||
2580 | svg = str.strip(new_values['svg']) |
||
2581 | |||
2582 | if 'description' in new_values.keys() and \ |
||
2583 | new_values['description'] is not None and \ |
||
2584 | len(str(new_values['description'])) > 0: |
||
2585 | description = str.strip(new_values['description']) |
||
2586 | else: |
||
2587 | description = None |
||
2588 | |||
2589 | if 'camera_url' in new_values.keys() and \ |
||
2590 | new_values['camera_url'] is not None and \ |
||
2591 | len(str(new_values['camera_url'])) > 0: |
||
2592 | camera_url = str.strip(new_values['camera_url']) |
||
2593 | else: |
||
2594 | camera_url = None |
||
2595 | |||
2596 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2597 | cursor = cnx.cursor() |
||
2598 | |||
2599 | cursor.execute(" SELECT name " |
||
2600 | " FROM tbl_combined_equipments " |
||
2601 | " WHERE name = %s ", (name,)) |
||
2602 | if cursor.fetchone() is not None: |
||
2603 | cursor.close() |
||
2604 | cnx.close() |
||
2605 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2606 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
2607 | |||
2608 | if cost_center_id is not None: |
||
2609 | cursor.execute(" SELECT name " |
||
2610 | " FROM tbl_cost_centers " |
||
2611 | " WHERE id = %s ", |
||
2612 | (new_values['cost_center']['id'],)) |
||
2613 | row = cursor.fetchone() |
||
2614 | if row is None: |
||
2615 | cursor.close() |
||
2616 | cnx.close() |
||
2617 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2618 | description='API.COST_CENTER_NOT_FOUND') |
||
2619 | |||
2620 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
2621 | " (name, uuid, is_input_counted, is_output_counted, " |
||
2622 | " cost_center_id, svg, camera_url, description) " |
||
2623 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
2624 | cursor.execute(add_values, (name, |
||
2625 | str(uuid.uuid4()), |
||
2626 | is_input_counted, |
||
2627 | is_output_counted, |
||
2628 | cost_center_id, |
||
2629 | svg, |
||
2630 | camera_url, |
||
2631 | description)) |
||
2632 | new_id = cursor.lastrowid |
||
2633 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
2634 | for equipment in new_values['equipments']: |
||
2635 | cursor.execute(" SELECT name " |
||
2636 | " FROM tbl_equipments " |
||
2637 | " WHERE id = %s ", (equipment['id'],)) |
||
2638 | if cursor.fetchone() is None: |
||
2639 | cursor.close() |
||
2640 | cnx.close() |
||
2641 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2642 | description='API.EQUIPMENT_NOT_FOUND') |
||
2643 | |||
2644 | query = (" SELECT id " |
||
2645 | " FROM tbl_combined_equipments_equipments " |
||
2646 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
2647 | cursor.execute(query, (new_id, equipment['id'],)) |
||
2648 | if cursor.fetchone() is not None: |
||
2649 | cursor.close() |
||
2650 | cnx.close() |
||
2651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2652 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
2653 | |||
2654 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
2655 | " VALUES (%s, %s) ") |
||
2656 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
2657 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
2658 | for command in new_values['commands']: |
||
2659 | cursor.execute(" SELECT name " |
||
2660 | " FROM tbl_commands " |
||
2661 | " WHERE id = %s ", (command['id'],)) |
||
2662 | if cursor.fetchone() is None: |
||
2663 | cursor.close() |
||
2664 | cnx.close() |
||
2665 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2666 | description='API.COMMAND_NOT_FOUND') |
||
2667 | |||
2668 | query = (" SELECT id " |
||
2669 | " FROM tbl_combined_equipments_commands " |
||
2670 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
2671 | cursor.execute(query, (new_id, command['id'],)) |
||
2672 | if cursor.fetchone() is not None: |
||
2673 | cursor.close() |
||
2674 | cnx.close() |
||
2675 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2676 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
2677 | |||
2678 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
2679 | " VALUES (%s, %s) ") |
||
2680 | cursor.execute(add_row, (new_id, command['id'],)) |
||
2681 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
2682 | for meter in new_values['meters']: |
||
2683 | cursor.execute(" SELECT name " |
||
2684 | " FROM tbl_meters " |
||
2685 | " WHERE id = %s ", (meter['id'],)) |
||
2686 | if cursor.fetchone() is None: |
||
2687 | cursor.close() |
||
2688 | cnx.close() |
||
2689 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2690 | description='API.METER_NOT_FOUND') |
||
2691 | |||
2692 | query = (" SELECT id " |
||
2693 | " FROM tbl_combined_equipments_meters " |
||
2694 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
2695 | cursor.execute(query, (new_id, meter['id'],)) |
||
2696 | if cursor.fetchone() is not None: |
||
2697 | cursor.close() |
||
2698 | cnx.close() |
||
2699 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2700 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
2701 | |||
2702 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
2703 | " VALUES (%s, %s, %s) ") |
||
2704 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
2705 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
2706 | for offline_meter in new_values['offline_meters']: |
||
2707 | cursor.execute(" SELECT name " |
||
2708 | " FROM tbl_offline_meters " |
||
2709 | " WHERE id = %s ", (offline_meter['id'],)) |
||
2710 | if cursor.fetchone() is None: |
||
2711 | cursor.close() |
||
2712 | cnx.close() |
||
2713 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2714 | description='API.OFFLINE_METER_NOT_FOUND') |
||
2715 | |||
2716 | query = (" SELECT id " |
||
2717 | " FROM tbl_combined_equipments_offline_meters " |
||
2718 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
2719 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
2720 | if cursor.fetchone() is not None: |
||
2721 | cursor.close() |
||
2722 | cnx.close() |
||
2723 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2724 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
2725 | |||
2726 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
2727 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
2728 | " VALUES (%s, %s, %s) ") |
||
2729 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
2730 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
2731 | for virtual_meter in new_values['virtual_meters']: |
||
2732 | cursor.execute(" SELECT name " |
||
2733 | " FROM tbl_virtual_meters " |
||
2734 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
2735 | if cursor.fetchone() is None: |
||
2736 | cursor.close() |
||
2737 | cnx.close() |
||
2738 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2739 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
2740 | |||
2741 | query = (" SELECT id " |
||
2742 | " FROM tbl_combined_equipments_virtual_meters " |
||
2743 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
2744 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
2745 | if cursor.fetchone() is not None: |
||
2746 | cursor.close() |
||
2747 | cnx.close() |
||
2748 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
2749 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
2750 | |||
2751 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
2752 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
2753 | " VALUES (%s, %s, %s) ") |
||
2754 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
2755 | View Code Duplication | if new_values['parameters'] is not None and len(new_values['parameters']) > 0: |
|
2756 | for parameters in new_values['parameters']: |
||
2757 | cursor.execute(" SELECT name " |
||
2758 | " FROM tbl_combined_equipments_parameters " |
||
2759 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
2760 | if cursor.fetchone() is not None: |
||
2761 | cursor.close() |
||
2762 | cnx.close() |
||
2763 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2764 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
2765 | if 'point' in parameters: |
||
2766 | if parameters['point'] is None: |
||
2767 | point_id = None |
||
2768 | elif parameters['point']['id'] is not None and \ |
||
2769 | parameters['point']['id'] <= 0: |
||
2770 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2771 | description='API.INVALID_POINT_ID') |
||
2772 | else: |
||
2773 | point_id = parameters['point']['id'] |
||
2774 | else: |
||
2775 | point_id = None |
||
2776 | numerator_meter_uuid = None |
||
2777 | if 'numerator_meter' in parameters: |
||
2778 | if parameters['numerator_meter'] is not None and \ |
||
2779 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
2780 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
2781 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
2782 | |||
2783 | denominator_meter_uuid = None |
||
2784 | if 'denominator_meter' in parameters: |
||
2785 | if parameters['denominator_meter'] is not None and \ |
||
2786 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
2787 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
2788 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
2789 | |||
2790 | # validate by parameter type |
||
2791 | if parameters['parameter_type'] == 'point': |
||
2792 | if point_id is None: |
||
2793 | cursor.close() |
||
2794 | cnx.close() |
||
2795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2796 | description='API.INVALID_POINT_ID') |
||
2797 | |||
2798 | query = (" SELECT id, name " |
||
2799 | " FROM tbl_points " |
||
2800 | " WHERE id = %s ") |
||
2801 | cursor.execute(query, (point_id,)) |
||
2802 | if cursor.fetchone() is None: |
||
2803 | cursor.close() |
||
2804 | cnx.close() |
||
2805 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2806 | description='API.POINT_NOT_FOUND') |
||
2807 | |||
2808 | elif parameters['parameter_type'] == 'constant': |
||
2809 | if parameters['constant'] is None: |
||
2810 | cursor.close() |
||
2811 | cnx.close() |
||
2812 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2813 | description='API.INVALID_CONSTANT_VALUE') |
||
2814 | |||
2815 | elif parameters['parameter_type'] == 'fraction': |
||
2816 | |||
2817 | query = (" SELECT id, name, uuid " |
||
2818 | " FROM tbl_meters ") |
||
2819 | cursor.execute(query) |
||
2820 | rows_meters = cursor.fetchall() |
||
2821 | |||
2822 | meter_dict = dict() |
||
2823 | if rows_meters is not None and len(rows_meters) > 0: |
||
2824 | for row in rows_meters: |
||
2825 | meter_dict[row[2]] = {"type": 'meter', |
||
2826 | "id": row[0], |
||
2827 | "name": row[1], |
||
2828 | "uuid": row[2]} |
||
2829 | |||
2830 | query = (" SELECT id, name, uuid " |
||
2831 | " FROM tbl_offline_meters ") |
||
2832 | cursor.execute(query) |
||
2833 | rows_offline_meters = cursor.fetchall() |
||
2834 | |||
2835 | offline_meter_dict = dict() |
||
2836 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
2837 | for row in rows_offline_meters: |
||
2838 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
2839 | "id": row[0], |
||
2840 | "name": row[1], |
||
2841 | "uuid": row[2]} |
||
2842 | |||
2843 | query = (" SELECT id, name, uuid " |
||
2844 | " FROM tbl_virtual_meters ") |
||
2845 | cursor.execute(query) |
||
2846 | rows_virtual_meters = cursor.fetchall() |
||
2847 | |||
2848 | virtual_meter_dict = dict() |
||
2849 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
2850 | for row in rows_virtual_meters: |
||
2851 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
2852 | "id": row[0], |
||
2853 | "name": row[1], |
||
2854 | "uuid": row[2]} |
||
2855 | |||
2856 | # validate numerator meter uuid |
||
2857 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
2858 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
2859 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
2860 | cursor.close() |
||
2861 | cnx.close() |
||
2862 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2863 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
2864 | |||
2865 | # validate denominator meter uuid |
||
2866 | if denominator_meter_uuid is None: |
||
2867 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2868 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
2869 | |||
2870 | if denominator_meter_uuid == numerator_meter_uuid: |
||
2871 | cursor.close() |
||
2872 | cnx.close() |
||
2873 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2874 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
2875 | |||
2876 | if denominator_meter_uuid not in meter_dict and \ |
||
2877 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
2878 | denominator_meter_uuid not in offline_meter_dict: |
||
2879 | cursor.close() |
||
2880 | cnx.close() |
||
2881 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2882 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
2883 | |||
2884 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
2885 | " (combined_equipment_id, name, parameter_type, constant, " |
||
2886 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
2887 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
2888 | cursor.execute(add_values, (new_id, |
||
2889 | parameters['name'], |
||
2890 | parameters['parameter_type'], |
||
2891 | parameters['constant'], |
||
2892 | point_id, |
||
2893 | numerator_meter_uuid, |
||
2894 | denominator_meter_uuid)) |
||
2895 | cnx.commit() |
||
2896 | cursor.close() |
||
2897 | cnx.close() |
||
2898 | |||
2899 | resp.status = falcon.HTTP_201 |
||
2900 | resp.location = '/combinedequipments/' + str(new_id) |
||
2901 | |||
2902 | |||
2903 | class CombinedEquipmentClone: |
||
2904 | @staticmethod |
||
2905 | def __init__(): |
||
2906 | """Initializes CombinedEquipmentClone""" |
||
2907 | pass |
||
2908 | |||
2909 | @staticmethod |
||
2910 | def on_options(req, resp, id_): |
||
2911 | resp.status = falcon.HTTP_200 |
||
2912 | |||
2913 | @staticmethod |
||
2914 | @user_logger |
||
2915 | def on_post(req, resp, id_): |
||
2916 | """Handles POST requests""" |
||
2917 | if 'API-KEY' not in req.headers or \ |
||
2918 | not isinstance(req.headers['API-KEY'], str) or \ |
||
2919 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
2920 | access_control(req) |
||
2921 | else: |
||
2922 | api_key_control(req) |
||
2923 | if not id_.isdigit() or int(id_) <= 0: |
||
2924 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
2925 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
2926 | |||
2927 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
2928 | cursor = cnx.cursor() |
||
2929 | |||
2930 | query = (" SELECT id, name, uuid " |
||
2931 | " FROM tbl_cost_centers ") |
||
2932 | cursor.execute(query) |
||
2933 | rows_cost_centers = cursor.fetchall() |
||
2934 | |||
2935 | cost_center_dict = dict() |
||
2936 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
2937 | for row in rows_cost_centers: |
||
2938 | cost_center_dict[row[0]] = {"id": row[0], |
||
2939 | "name": row[1], |
||
2940 | "uuid": row[2]} |
||
2941 | |||
2942 | query = (" SELECT id, name, uuid, " |
||
2943 | " is_input_counted, is_output_counted, " |
||
2944 | " cost_center_id, svg, camera_url, description " |
||
2945 | " FROM tbl_combined_equipments " |
||
2946 | " WHERE id = %s ") |
||
2947 | cursor.execute(query, (id_,)) |
||
2948 | row = cursor.fetchone() |
||
2949 | |||
2950 | if row is None: |
||
2951 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
2952 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
2953 | else: |
||
2954 | meta_result = {"id": row[0], |
||
2955 | "name": row[1], |
||
2956 | "uuid": row[2], |
||
2957 | "is_input_counted": bool(row[3]), |
||
2958 | "is_output_counted": bool(row[4]), |
||
2959 | "cost_center": cost_center_dict.get(row[5], None), |
||
2960 | "svg": row[6], |
||
2961 | "camera_url": row[7], |
||
2962 | "description": row[8], |
||
2963 | "equipments": None, |
||
2964 | "commands": None, |
||
2965 | "meters": None, |
||
2966 | "offline_meters": None, |
||
2967 | "virtual_meters": None, |
||
2968 | "parameters": None |
||
2969 | } |
||
2970 | query = (" SELECT e.id, e.name, e.uuid " |
||
2971 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
2972 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
2973 | " ORDER BY e.id ") |
||
2974 | cursor.execute(query, (id_,)) |
||
2975 | rows = cursor.fetchall() |
||
2976 | |||
2977 | equipment_result = list() |
||
2978 | if rows is not None and len(rows) > 0: |
||
2979 | for row in rows: |
||
2980 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2981 | equipment_result.append(result) |
||
2982 | meta_result['equipments'] = equipment_result |
||
2983 | |||
2984 | query = (" SELECT c.id, c.name, c.uuid " |
||
2985 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
2986 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
2987 | " ORDER BY c.id ") |
||
2988 | cursor.execute(query, (id_,)) |
||
2989 | rows = cursor.fetchall() |
||
2990 | |||
2991 | command_result = list() |
||
2992 | if rows is not None and len(rows) > 0: |
||
2993 | for row in rows: |
||
2994 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
2995 | command_result.append(result) |
||
2996 | meta_result['commands'] = command_result |
||
2997 | |||
2998 | query = (" SELECT id, name, uuid " |
||
2999 | " FROM tbl_energy_categories ") |
||
3000 | cursor.execute(query) |
||
3001 | rows_energy_categories = cursor.fetchall() |
||
3002 | |||
3003 | energy_category_dict = dict() |
||
3004 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
3005 | for row in rows_energy_categories: |
||
3006 | energy_category_dict[row[0]] = {"id": row[0], |
||
3007 | "name": row[1], |
||
3008 | "uuid": row[2]} |
||
3009 | |||
3010 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
3011 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
3012 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
3013 | " ORDER BY m.id ") |
||
3014 | cursor.execute(query, (id_,)) |
||
3015 | rows = cursor.fetchall() |
||
3016 | |||
3017 | meter_result = list() |
||
3018 | if rows is not None and len(rows) > 0: |
||
3019 | for row in rows: |
||
3020 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
3021 | "energy_category": energy_category_dict.get(row[3], None), |
||
3022 | "is_output": bool(row[4])} |
||
3023 | meter_result.append(result) |
||
3024 | meta_result['meters'] = meter_result |
||
3025 | |||
3026 | query = (" SELECT id, name, uuid " |
||
3027 | " FROM tbl_energy_categories ") |
||
3028 | cursor.execute(query) |
||
3029 | rows_energy_categories = cursor.fetchall() |
||
3030 | |||
3031 | energy_category_dict = dict() |
||
3032 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
3033 | for row in rows_energy_categories: |
||
3034 | energy_category_dict[row[0]] = {"id": row[0], |
||
3035 | "name": row[1], |
||
3036 | "uuid": row[2]} |
||
3037 | |||
3038 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
3039 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
3040 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
3041 | " ORDER BY m.id ") |
||
3042 | cursor.execute(query, (id_,)) |
||
3043 | rows = cursor.fetchall() |
||
3044 | |||
3045 | offlinemeter_result = list() |
||
3046 | if rows is not None and len(rows) > 0: |
||
3047 | for row in rows: |
||
3048 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
3049 | "energy_category": energy_category_dict.get(row[3], None), |
||
3050 | "is_output": bool(row[4])} |
||
3051 | offlinemeter_result.append(result) |
||
3052 | meta_result['offline_meters'] = offlinemeter_result |
||
3053 | |||
3054 | query = (" SELECT id, name, uuid " |
||
3055 | " FROM tbl_energy_categories ") |
||
3056 | cursor.execute(query) |
||
3057 | rows_energy_categories = cursor.fetchall() |
||
3058 | |||
3059 | energy_category_dict = dict() |
||
3060 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
3061 | for row in rows_energy_categories: |
||
3062 | energy_category_dict[row[0]] = {"id": row[0], |
||
3063 | "name": row[1], |
||
3064 | "uuid": row[2]} |
||
3065 | |||
3066 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
3067 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
3068 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
3069 | " ORDER BY m.id ") |
||
3070 | cursor.execute(query, (id_,)) |
||
3071 | rows = cursor.fetchall() |
||
3072 | |||
3073 | virtualmeter_result = list() |
||
3074 | if rows is not None and len(rows) > 0: |
||
3075 | for row in rows: |
||
3076 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
3077 | "energy_category": energy_category_dict.get(row[3], None), |
||
3078 | "is_output": bool(row[4])} |
||
3079 | virtualmeter_result.append(result) |
||
3080 | meta_result['virtual_meters'] = virtualmeter_result |
||
3081 | |||
3082 | query = (" SELECT id, name " |
||
3083 | " FROM tbl_points ") |
||
3084 | cursor.execute(query) |
||
3085 | rows_points = cursor.fetchall() |
||
3086 | |||
3087 | point_dict = dict() |
||
3088 | if rows_points is not None and len(rows_points) > 0: |
||
3089 | for row in rows_points: |
||
3090 | point_dict[row[0]] = {"id": row[0], |
||
3091 | "name": row[1]} |
||
3092 | |||
3093 | query = (" SELECT id, name, uuid " |
||
3094 | " FROM tbl_meters ") |
||
3095 | cursor.execute(query) |
||
3096 | rows_meters = cursor.fetchall() |
||
3097 | |||
3098 | meter_dict = dict() |
||
3099 | if rows_meters is not None and len(rows_meters) > 0: |
||
3100 | for row in rows_meters: |
||
3101 | meter_dict[row[2]] = {"type": 'meter', |
||
3102 | "id": row[0], |
||
3103 | "name": row[1], |
||
3104 | "uuid": row[2]} |
||
3105 | |||
3106 | query = (" SELECT id, name, uuid " |
||
3107 | " FROM tbl_offline_meters ") |
||
3108 | cursor.execute(query) |
||
3109 | rows_offline_meters = cursor.fetchall() |
||
3110 | |||
3111 | offline_meter_dict = dict() |
||
3112 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
3113 | for row in rows_offline_meters: |
||
3114 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
3115 | "id": row[0], |
||
3116 | "name": row[1], |
||
3117 | "uuid": row[2]} |
||
3118 | |||
3119 | query = (" SELECT id, name, uuid " |
||
3120 | " FROM tbl_virtual_meters ") |
||
3121 | cursor.execute(query) |
||
3122 | rows_virtual_meters = cursor.fetchall() |
||
3123 | |||
3124 | virtual_meter_dict = dict() |
||
3125 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
3126 | for row in rows_virtual_meters: |
||
3127 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
3128 | "id": row[0], |
||
3129 | "name": row[1], |
||
3130 | "uuid": row[2]} |
||
3131 | |||
3132 | query = (" SELECT id, name, parameter_type, " |
||
3133 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
3134 | " FROM tbl_combined_equipments_parameters " |
||
3135 | " WHERE combined_equipment_id = %s " |
||
3136 | " ORDER BY id ") |
||
3137 | cursor.execute(query, (id_,)) |
||
3138 | rows_parameters = cursor.fetchall() |
||
3139 | |||
3140 | parameter_result = list() |
||
3141 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
3142 | for row in rows_parameters: |
||
3143 | constant = None |
||
3144 | point = None |
||
3145 | numerator_meter = None |
||
3146 | denominator_meter = None |
||
3147 | if row[2] == 'point': |
||
3148 | point = point_dict.get(row[4], None) |
||
3149 | constant = None |
||
3150 | numerator_meter = None |
||
3151 | denominator_meter = None |
||
3152 | elif row[2] == 'constant': |
||
3153 | constant = row[3] |
||
3154 | point = None |
||
3155 | numerator_meter = None |
||
3156 | denominator_meter = None |
||
3157 | elif row[2] == 'fraction': |
||
3158 | constant = None |
||
3159 | point = None |
||
3160 | # find numerator meter by uuid |
||
3161 | numerator_meter = meter_dict.get(row[5], None) |
||
3162 | if numerator_meter is None: |
||
3163 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
3164 | if numerator_meter is None: |
||
3165 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
3166 | # find denominator meter by uuid |
||
3167 | denominator_meter = meter_dict.get(row[6], None) |
||
3168 | if denominator_meter is None: |
||
3169 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
3170 | if denominator_meter is None: |
||
3171 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
3172 | |||
3173 | result = {"id": row[0], |
||
3174 | "name": row[1], |
||
3175 | "parameter_type": row[2], |
||
3176 | "constant": constant, |
||
3177 | "point": point, |
||
3178 | "numerator_meter": numerator_meter, |
||
3179 | "denominator_meter": denominator_meter} |
||
3180 | parameter_result.append(result) |
||
3181 | meta_result['parameters'] = parameter_result |
||
3182 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
3183 | if config.utc_offset[0] == '-': |
||
3184 | timezone_offset = -timezone_offset |
||
3185 | new_name = (str.strip(meta_result['name']) |
||
3186 | + (datetime.now() |
||
3187 | + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
3188 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
3189 | " (name, uuid, is_input_counted, is_output_counted, " |
||
3190 | " cost_center_id, svg, camera_url, description) " |
||
3191 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
3192 | cursor.execute(add_values, (new_name, |
||
3193 | str(uuid.uuid4()), |
||
3194 | meta_result['is_input_counted'], |
||
3195 | meta_result['is_output_counted'], |
||
3196 | meta_result['cost_center']['id'], |
||
3197 | meta_result['svg'], |
||
3198 | meta_result['camera_url'], |
||
3199 | meta_result['description'])) |
||
3200 | new_id = cursor.lastrowid |
||
3201 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
3202 | for equipment in meta_result['equipments']: |
||
3203 | cursor.execute(" SELECT name " |
||
3204 | " FROM tbl_equipments " |
||
3205 | " WHERE id = %s ", (equipment['id'],)) |
||
3206 | if cursor.fetchone() is None: |
||
3207 | cursor.close() |
||
3208 | cnx.close() |
||
3209 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
3210 | description='API.EQUIPMENT_NOT_FOUND') |
||
3211 | |||
3212 | query = (" SELECT id " |
||
3213 | " FROM tbl_combined_equipments_equipments " |
||
3214 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
3215 | cursor.execute(query, (new_id, equipment['id'],)) |
||
3216 | if cursor.fetchone() is not None: |
||
3217 | cursor.close() |
||
3218 | cnx.close() |
||
3219 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
3220 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
3221 | |||
3222 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
3223 | " VALUES (%s, %s) ") |
||
3224 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
3225 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
3226 | for command in meta_result['commands']: |
||
3227 | cursor.execute(" SELECT name " |
||
3228 | " FROM tbl_commands " |
||
3229 | " WHERE id = %s ", (command['id'],)) |
||
3230 | if cursor.fetchone() is None: |
||
3231 | cursor.close() |
||
3232 | cnx.close() |
||
3233 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
3234 | description='API.COMMAND_NOT_FOUND') |
||
3235 | |||
3236 | query = (" SELECT id " |
||
3237 | " FROM tbl_combined_equipments_commands " |
||
3238 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
3239 | cursor.execute(query, (new_id, command['id'],)) |
||
3240 | if cursor.fetchone() is not None: |
||
3241 | cursor.close() |
||
3242 | cnx.close() |
||
3243 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
3244 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
3245 | |||
3246 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
3247 | " VALUES (%s, %s) ") |
||
3248 | cursor.execute(add_row, (new_id, command['id'],)) |
||
3249 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
3250 | for meter in meta_result['meters']: |
||
3251 | cursor.execute(" SELECT name " |
||
3252 | " FROM tbl_meters " |
||
3253 | " WHERE id = %s ", (meter['id'],)) |
||
3254 | if cursor.fetchone() is None: |
||
3255 | cursor.close() |
||
3256 | cnx.close() |
||
3257 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
3258 | description='API.METER_NOT_FOUND') |
||
3259 | |||
3260 | query = (" SELECT id " |
||
3261 | " FROM tbl_combined_equipments_meters " |
||
3262 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
3263 | cursor.execute(query, (new_id, meter['id'],)) |
||
3264 | if cursor.fetchone() is not None: |
||
3265 | cursor.close() |
||
3266 | cnx.close() |
||
3267 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
3268 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
3269 | |||
3270 | add_row = ( |
||
3271 | " INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
3272 | " VALUES (%s, %s, %s) ") |
||
3273 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
3274 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
3275 | for offline_meter in meta_result['offline_meters']: |
||
3276 | cursor.execute(" SELECT name " |
||
3277 | " FROM tbl_offline_meters " |
||
3278 | " WHERE id = %s ", (offline_meter['id'],)) |
||
3279 | if cursor.fetchone() is None: |
||
3280 | cursor.close() |
||
3281 | cnx.close() |
||
3282 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
3283 | description='API.OFFLINE_METER_NOT_FOUND') |
||
3284 | |||
3285 | query = (" SELECT id " |
||
3286 | " FROM tbl_combined_equipments_offline_meters " |
||
3287 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
3288 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
3289 | if cursor.fetchone() is not None: |
||
3290 | cursor.close() |
||
3291 | cnx.close() |
||
3292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
3293 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
3294 | |||
3295 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
3296 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
3297 | " VALUES (%s, %s, %s) ") |
||
3298 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
3299 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
3300 | for virtual_meter in meta_result['virtual_meters']: |
||
3301 | cursor.execute(" SELECT name " |
||
3302 | " FROM tbl_virtual_meters " |
||
3303 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
3304 | if cursor.fetchone() is None: |
||
3305 | cursor.close() |
||
3306 | cnx.close() |
||
3307 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
3308 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
3309 | |||
3310 | query = (" SELECT id " |
||
3311 | " FROM tbl_combined_equipments_virtual_meters " |
||
3312 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
3313 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
3314 | if cursor.fetchone() is not None: |
||
3315 | cursor.close() |
||
3316 | cnx.close() |
||
3317 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
3318 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
3319 | |||
3320 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
3321 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
3322 | " VALUES (%s, %s, %s) ") |
||
3323 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
3324 | View Code Duplication | if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0: |
|
3325 | for parameters in meta_result['parameters']: |
||
3326 | cursor.execute(" SELECT name " |
||
3327 | " FROM tbl_combined_equipments_parameters " |
||
3328 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
3329 | if cursor.fetchone() is not None: |
||
3330 | cursor.close() |
||
3331 | cnx.close() |
||
3332 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3333 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
3334 | if 'point' in parameters: |
||
3335 | if parameters['point'] is None: |
||
3336 | point_id = None |
||
3337 | elif parameters['point']['id'] is not None and \ |
||
3338 | parameters['point']['id'] <= 0: |
||
3339 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3340 | description='API.INVALID_POINT_ID') |
||
3341 | else: |
||
3342 | point_id = parameters['point']['id'] |
||
3343 | else: |
||
3344 | point_id = None |
||
3345 | numerator_meter_uuid = None |
||
3346 | if 'numerator_meter' in parameters: |
||
3347 | if parameters['numerator_meter'] is not None and \ |
||
3348 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
3349 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
3350 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
3351 | |||
3352 | denominator_meter_uuid = None |
||
3353 | if 'denominator_meter' in parameters: |
||
3354 | if parameters['denominator_meter'] is not None and \ |
||
3355 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
3356 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
3357 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
3358 | |||
3359 | # validate by parameter type |
||
3360 | if parameters['parameter_type'] == 'point': |
||
3361 | if point_id is None: |
||
3362 | cursor.close() |
||
3363 | cnx.close() |
||
3364 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3365 | description='API.INVALID_POINT_ID') |
||
3366 | |||
3367 | query = (" SELECT id, name " |
||
3368 | " FROM tbl_points " |
||
3369 | " WHERE id = %s ") |
||
3370 | cursor.execute(query, (point_id,)) |
||
3371 | if cursor.fetchone() is None: |
||
3372 | cursor.close() |
||
3373 | cnx.close() |
||
3374 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3375 | description='API.POINT_NOT_FOUND') |
||
3376 | |||
3377 | elif parameters['parameter_type'] == 'constant': |
||
3378 | if parameters['constant'] is None: |
||
3379 | cursor.close() |
||
3380 | cnx.close() |
||
3381 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3382 | description='API.INVALID_CONSTANT_VALUE') |
||
3383 | |||
3384 | elif parameters['parameter_type'] == 'fraction': |
||
3385 | |||
3386 | query = (" SELECT id, name, uuid " |
||
3387 | " FROM tbl_meters ") |
||
3388 | cursor.execute(query) |
||
3389 | rows_meters = cursor.fetchall() |
||
3390 | |||
3391 | meter_dict = dict() |
||
3392 | if rows_meters is not None and len(rows_meters) > 0: |
||
3393 | for row in rows_meters: |
||
3394 | meter_dict[row[2]] = {"type": 'meter', |
||
3395 | "id": row[0], |
||
3396 | "name": row[1], |
||
3397 | "uuid": row[2]} |
||
3398 | |||
3399 | query = (" SELECT id, name, uuid " |
||
3400 | " FROM tbl_offline_meters ") |
||
3401 | cursor.execute(query) |
||
3402 | rows_offline_meters = cursor.fetchall() |
||
3403 | |||
3404 | offline_meter_dict = dict() |
||
3405 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
3406 | for row in rows_offline_meters: |
||
3407 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
3408 | "id": row[0], |
||
3409 | "name": row[1], |
||
3410 | "uuid": row[2]} |
||
3411 | |||
3412 | query = (" SELECT id, name, uuid " |
||
3413 | " FROM tbl_virtual_meters ") |
||
3414 | cursor.execute(query) |
||
3415 | rows_virtual_meters = cursor.fetchall() |
||
3416 | |||
3417 | virtual_meter_dict = dict() |
||
3418 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
3419 | for row in rows_virtual_meters: |
||
3420 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
3421 | "id": row[0], |
||
3422 | "name": row[1], |
||
3423 | "uuid": row[2]} |
||
3424 | |||
3425 | # validate numerator meter uuid |
||
3426 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
3427 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
3428 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
3429 | cursor.close() |
||
3430 | cnx.close() |
||
3431 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3432 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
3433 | |||
3434 | # validate denominator meter uuid |
||
3435 | if denominator_meter_uuid is None: |
||
3436 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3437 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
3438 | |||
3439 | if denominator_meter_uuid == numerator_meter_uuid: |
||
3440 | cursor.close() |
||
3441 | cnx.close() |
||
3442 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3443 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
3444 | |||
3445 | if denominator_meter_uuid not in meter_dict and \ |
||
3446 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
3447 | denominator_meter_uuid not in offline_meter_dict: |
||
3448 | cursor.close() |
||
3449 | cnx.close() |
||
3450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
3451 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
3452 | |||
3453 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
3454 | " (combined_equipment_id, name, parameter_type, constant, " |
||
3455 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
3456 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
3457 | cursor.execute(add_values, (new_id, |
||
3458 | parameters['name'], |
||
3459 | parameters['parameter_type'], |
||
3460 | parameters['constant'], |
||
3461 | point_id, |
||
3462 | numerator_meter_uuid, |
||
3463 | denominator_meter_uuid)) |
||
3464 | cnx.commit() |
||
3465 | cursor.close() |
||
3466 | cnx.close() |
||
3467 | |||
3468 | resp.status = falcon.HTTP_201 |
||
3469 | resp.location = '/combinedequipments/' + str(new_id) |
||
3470 |