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