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