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