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