Total Complexity | 385 |
Total Lines | 1922 |
Duplicated Lines | 97.76 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like core.combinedequipment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import falcon |
||
2 | import simplejson as json |
||
3 | import mysql.connector |
||
4 | import config |
||
5 | import uuid |
||
6 | |||
7 | |||
8 | View Code Duplication | class CombinedEquipmentCollection: |
|
|
|||
9 | @staticmethod |
||
10 | def __init__(): |
||
11 | pass |
||
12 | |||
13 | @staticmethod |
||
14 | def on_options(req, resp): |
||
15 | resp.status = falcon.HTTP_200 |
||
16 | |||
17 | @staticmethod |
||
18 | def on_get(req, resp): |
||
19 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
20 | cursor = cnx.cursor(dictionary=True) |
||
21 | |||
22 | query = (" SELECT id, name, uuid " |
||
23 | " FROM tbl_cost_centers ") |
||
24 | cursor.execute(query) |
||
25 | rows_cost_centers = cursor.fetchall() |
||
26 | |||
27 | cost_center_dict = dict() |
||
28 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
29 | for row in rows_cost_centers: |
||
30 | cost_center_dict[row['id']] = {"id": row['id'], |
||
31 | "name": row['name'], |
||
32 | "uuid": row['uuid']} |
||
33 | |||
34 | query = (" SELECT id, name, uuid, " |
||
35 | " is_input_counted, is_output_counted, " |
||
36 | " cost_center_id, description " |
||
37 | " FROM tbl_combined_equipments " |
||
38 | " ORDER BY id ") |
||
39 | cursor.execute(query) |
||
40 | rows_combined_equipments = cursor.fetchall() |
||
41 | |||
42 | result = list() |
||
43 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
44 | for row in rows_combined_equipments: |
||
45 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
46 | meta_result = {"id": row['id'], |
||
47 | "name": row['name'], |
||
48 | "uuid": row['uuid'], |
||
49 | "is_input_counted": bool(row['is_input_counted']), |
||
50 | "is_output_counted": bool(row['is_output_counted']), |
||
51 | "cost_center": cost_center, |
||
52 | "description": row['description']} |
||
53 | result.append(meta_result) |
||
54 | |||
55 | cursor.close() |
||
56 | cnx.disconnect() |
||
57 | resp.body = json.dumps(result) |
||
58 | |||
59 | @staticmethod |
||
60 | def on_post(req, resp): |
||
61 | """Handles POST requests""" |
||
62 | try: |
||
63 | raw_json = req.stream.read().decode('utf-8') |
||
64 | except Exception as ex: |
||
65 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
66 | |||
67 | new_values = json.loads(raw_json) |
||
68 | |||
69 | if 'name' not in new_values['data'].keys() or \ |
||
70 | not isinstance(new_values['data']['name'], str) or \ |
||
71 | len(str.strip(new_values['data']['name'])) == 0: |
||
72 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
73 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
74 | name = str.strip(new_values['data']['name']) |
||
75 | |||
76 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
77 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
78 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
79 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
80 | is_input_counted = new_values['data']['is_input_counted'] |
||
81 | |||
82 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
83 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
84 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
85 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
86 | is_output_counted = new_values['data']['is_output_counted'] |
||
87 | |||
88 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
89 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
90 | new_values['data']['cost_center_id'] <= 0: |
||
91 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
92 | description='API.INVALID_COST_CENTER_ID') |
||
93 | cost_center_id = new_values['data']['cost_center_id'] |
||
94 | |||
95 | if 'description' in new_values['data'].keys() and \ |
||
96 | new_values['data']['description'] is not None and \ |
||
97 | len(str(new_values['data']['description'])) > 0: |
||
98 | description = str.strip(new_values['data']['description']) |
||
99 | else: |
||
100 | description = None |
||
101 | |||
102 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
103 | cursor = cnx.cursor() |
||
104 | |||
105 | cursor.execute(" SELECT name " |
||
106 | " FROM tbl_combined_equipments " |
||
107 | " WHERE name = %s ", (name,)) |
||
108 | if cursor.fetchone() is not None: |
||
109 | cursor.close() |
||
110 | cnx.disconnect() |
||
111 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
112 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
113 | |||
114 | if cost_center_id is not None: |
||
115 | cursor.execute(" SELECT name " |
||
116 | " FROM tbl_cost_centers " |
||
117 | " WHERE id = %s ", |
||
118 | (new_values['data']['cost_center_id'],)) |
||
119 | row = cursor.fetchone() |
||
120 | if row is None: |
||
121 | cursor.close() |
||
122 | cnx.disconnect() |
||
123 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
124 | description='API.COST_CENTER_NOT_FOUND') |
||
125 | |||
126 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
127 | " (name, uuid, is_input_counted, is_output_counted, " |
||
128 | " cost_center_id, description) " |
||
129 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
130 | cursor.execute(add_values, (name, |
||
131 | str(uuid.uuid4()), |
||
132 | is_input_counted, |
||
133 | is_output_counted, |
||
134 | cost_center_id, |
||
135 | description)) |
||
136 | new_id = cursor.lastrowid |
||
137 | cnx.commit() |
||
138 | cursor.close() |
||
139 | cnx.disconnect() |
||
140 | |||
141 | resp.status = falcon.HTTP_201 |
||
142 | resp.location = '/combinedequipments/' + str(new_id) |
||
143 | |||
144 | |||
145 | class CombinedEquipmentItem: |
||
146 | @staticmethod |
||
147 | def __init__(): |
||
148 | pass |
||
149 | |||
150 | @staticmethod |
||
151 | def on_options(req, resp, id_): |
||
152 | resp.status = falcon.HTTP_200 |
||
153 | |||
154 | View Code Duplication | @staticmethod |
|
155 | def on_get(req, resp, id_): |
||
156 | if not id_.isdigit() or int(id_) <= 0: |
||
157 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
158 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
159 | |||
160 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
161 | cursor = cnx.cursor(dictionary=True) |
||
162 | |||
163 | query = (" SELECT id, name, uuid " |
||
164 | " FROM tbl_cost_centers ") |
||
165 | cursor.execute(query) |
||
166 | rows_cost_centers = cursor.fetchall() |
||
167 | |||
168 | cost_center_dict = dict() |
||
169 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
170 | for row in rows_cost_centers: |
||
171 | cost_center_dict[row['id']] = {"id": row['id'], |
||
172 | "name": row['name'], |
||
173 | "uuid": row['uuid']} |
||
174 | |||
175 | query = (" SELECT id, name, uuid, " |
||
176 | " is_input_counted, is_output_counted, " |
||
177 | " cost_center_id, description " |
||
178 | " FROM tbl_combined_equipments " |
||
179 | " WHERE id = %s ") |
||
180 | cursor.execute(query, (id_,)) |
||
181 | row = cursor.fetchone() |
||
182 | cursor.close() |
||
183 | cnx.disconnect() |
||
184 | |||
185 | if row is None: |
||
186 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
187 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
188 | else: |
||
189 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
190 | meta_result = {"id": row['id'], |
||
191 | "name": row['name'], |
||
192 | "uuid": row['uuid'], |
||
193 | "is_input_counted": bool(row['is_input_counted']), |
||
194 | "is_output_counted": bool(row['is_output_counted']), |
||
195 | "cost_center": cost_center, |
||
196 | "description": row['description']} |
||
197 | |||
198 | resp.body = json.dumps(meta_result) |
||
199 | |||
200 | View Code Duplication | @staticmethod |
|
201 | def on_delete(req, resp, id_): |
||
202 | if not id_.isdigit() or int(id_) <= 0: |
||
203 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
204 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
205 | |||
206 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
207 | cursor = cnx.cursor() |
||
208 | |||
209 | # check relation with space |
||
210 | cursor.execute(" SELECT space_id " |
||
211 | " FROM tbl_spaces_combined_equipments " |
||
212 | " WHERE combined_equipment_id = %s ", |
||
213 | (id_,)) |
||
214 | rows_combined_equipments = cursor.fetchall() |
||
215 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
216 | cursor.close() |
||
217 | cnx.disconnect() |
||
218 | raise falcon.HTTPError(falcon.HTTP_400, |
||
219 | title='API.BAD_REQUEST', |
||
220 | description='API.THERE_IS_RELATION_WITH_SPACE') |
||
221 | |||
222 | # check relation with meter |
||
223 | cursor.execute(" SELECT meter_id " |
||
224 | " FROM tbl_combined_equipments_meters " |
||
225 | " WHERE combined_equipment_id = %s ", |
||
226 | (id_,)) |
||
227 | rows_meters = cursor.fetchall() |
||
228 | if rows_meters is not None and len(rows_meters) > 0: |
||
229 | cursor.close() |
||
230 | cnx.disconnect() |
||
231 | raise falcon.HTTPError(falcon.HTTP_400, |
||
232 | title='API.BAD_REQUEST', |
||
233 | description='API.THERE_IS_RELATION_WITH_METER') |
||
234 | |||
235 | # check relation with offline meter |
||
236 | cursor.execute(" SELECT offline_meter_id " |
||
237 | " FROM tbl_combined_equipments_offline_meters " |
||
238 | " WHERE combined_equipment_id = %s ", |
||
239 | (id_,)) |
||
240 | rows_offline_meters = cursor.fetchall() |
||
241 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
242 | cursor.close() |
||
243 | cnx.disconnect() |
||
244 | raise falcon.HTTPError(falcon.HTTP_400, |
||
245 | title='API.BAD_REQUEST', |
||
246 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METER') |
||
247 | |||
248 | # check relation with virtual meter |
||
249 | cursor.execute(" SELECT virtual_meter_id " |
||
250 | " FROM tbl_combined_equipments_virtual_meters " |
||
251 | " WHERE combined_equipment_id = %s ", |
||
252 | (id_,)) |
||
253 | rows_virtual_meters = cursor.fetchall() |
||
254 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
255 | cursor.close() |
||
256 | cnx.disconnect() |
||
257 | raise falcon.HTTPError(falcon.HTTP_400, |
||
258 | title='API.BAD_REQUEST', |
||
259 | description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER') |
||
260 | |||
261 | # delete all associated parameters |
||
262 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters WHERE combined_equipment_id = %s ", (id_,)) |
||
263 | cnx.commit() |
||
264 | |||
265 | cursor.execute(" DELETE FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
266 | cnx.commit() |
||
267 | |||
268 | cursor.close() |
||
269 | cnx.disconnect() |
||
270 | |||
271 | resp.status = falcon.HTTP_204 |
||
272 | |||
273 | View Code Duplication | @staticmethod |
|
274 | def on_put(req, resp, id_): |
||
275 | """Handles PUT requests""" |
||
276 | if not id_.isdigit() or int(id_) <= 0: |
||
277 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
278 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
279 | try: |
||
280 | raw_json = req.stream.read().decode('utf-8') |
||
281 | except Exception as ex: |
||
282 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
283 | |||
284 | new_values = json.loads(raw_json) |
||
285 | |||
286 | if 'name' not in new_values['data'].keys() or \ |
||
287 | not isinstance(new_values['data']['name'], str) or \ |
||
288 | len(str.strip(new_values['data']['name'])) == 0: |
||
289 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
290 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
291 | name = str.strip(new_values['data']['name']) |
||
292 | |||
293 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
294 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
295 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
296 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
297 | is_input_counted = new_values['data']['is_input_counted'] |
||
298 | |||
299 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
300 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
301 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
302 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
303 | is_output_counted = new_values['data']['is_output_counted'] |
||
304 | |||
305 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
306 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
307 | new_values['data']['cost_center_id'] <= 0: |
||
308 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
309 | description='API.INVALID_COST_CENTER_ID') |
||
310 | cost_center_id = new_values['data']['cost_center_id'] |
||
311 | |||
312 | if 'description' in new_values['data'].keys() and \ |
||
313 | new_values['data']['description'] is not None and \ |
||
314 | len(str(new_values['data']['description'])) > 0: |
||
315 | description = str.strip(new_values['data']['description']) |
||
316 | else: |
||
317 | description = None |
||
318 | |||
319 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
320 | cursor = cnx.cursor() |
||
321 | |||
322 | cursor.execute(" SELECT name " |
||
323 | " FROM tbl_combined_equipments " |
||
324 | " WHERE id = %s ", (id_,)) |
||
325 | if cursor.fetchone() is None: |
||
326 | cursor.close() |
||
327 | cnx.disconnect() |
||
328 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
329 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
330 | |||
331 | cursor.execute(" SELECT name " |
||
332 | " FROM tbl_combined_equipments " |
||
333 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
334 | if cursor.fetchone() is not None: |
||
335 | cursor.close() |
||
336 | cnx.disconnect() |
||
337 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
338 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
339 | |||
340 | cursor.execute(" SELECT name " |
||
341 | " FROM tbl_cost_centers " |
||
342 | " WHERE id = %s ", |
||
343 | (new_values['data']['cost_center_id'],)) |
||
344 | row = cursor.fetchone() |
||
345 | if row is None: |
||
346 | cursor.close() |
||
347 | cnx.disconnect() |
||
348 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
349 | description='API.COST_CENTER_NOT_FOUND') |
||
350 | |||
351 | update_row = (" UPDATE tbl_combined_equipments " |
||
352 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
||
353 | " cost_center_id = %s, description = %s " |
||
354 | " WHERE id = %s ") |
||
355 | cursor.execute(update_row, (name, |
||
356 | is_input_counted, |
||
357 | is_output_counted, |
||
358 | cost_center_id, |
||
359 | description, |
||
360 | id_)) |
||
361 | cnx.commit() |
||
362 | |||
363 | cursor.close() |
||
364 | cnx.disconnect() |
||
365 | |||
366 | resp.status = falcon.HTTP_200 |
||
367 | |||
368 | # Clone a Combined Equipment |
||
369 | View Code Duplication | @staticmethod |
|
370 | def on_post(req, resp, id_): |
||
371 | """Handles PUT requests""" |
||
372 | if not id_.isdigit() or int(id_) <= 0: |
||
373 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
374 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
375 | try: |
||
376 | raw_json = req.stream.read().decode('utf-8') |
||
377 | except Exception as ex: |
||
378 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
379 | |||
380 | new_values = json.loads(raw_json) |
||
381 | |||
382 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
383 | cursor = cnx.cursor(dictionary=True) |
||
384 | cursor.execute(" SELECT name " |
||
385 | " FROM tbl_combined_equipments " |
||
386 | " WHERE id = %s ", (id_,)) |
||
387 | if cursor.fetchone() is None: |
||
388 | cursor.close() |
||
389 | cnx.disconnect() |
||
390 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
391 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
392 | |||
393 | query = (" SELECT name, is_input_counted, is_output_counted, " |
||
394 | " cost_center_id, description " |
||
395 | " FROM tbl_combined_equipments " |
||
396 | " WHERE id = %s ") |
||
397 | cursor.execute(query, (id_,)) |
||
398 | row = cursor.fetchone() |
||
399 | |||
400 | if row is None: |
||
401 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
402 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
403 | else: |
||
404 | |||
405 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
406 | " (name, uuid, is_input_counted, is_output_counted, " |
||
407 | " cost_center_id, description) " |
||
408 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
409 | cursor.execute(add_values, (row['name'] + ' Copy', |
||
410 | str(uuid.uuid4()), |
||
411 | row['is_input_counted'], |
||
412 | row['is_output_counted'], |
||
413 | row['cost_center_id'], |
||
414 | row['description'])) |
||
415 | new_id = cursor.lastrowid |
||
416 | cnx.commit() |
||
417 | |||
418 | # clone relation with meter |
||
419 | cursor.execute(" SELECT meter_id, is_output " |
||
420 | " FROM tbl_combined_equipments_meters " |
||
421 | " WHERE combined_equipment_id = %s ", |
||
422 | (id_,)) |
||
423 | rows_meters = cursor.fetchall() |
||
424 | if rows_meters is not None and len(rows_meters) > 0: |
||
425 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
||
426 | " VALUES ") |
||
427 | for row in rows_meters: |
||
428 | add_values += " (" + str(new_id) + "," |
||
429 | add_values += str(row['meter_id']) + "," |
||
430 | add_values += str(bool(row['is_output'])) + "), " |
||
431 | # trim ", " at the end of string and then execute |
||
432 | cursor.execute(add_values[:-2]) |
||
433 | cnx.commit() |
||
434 | |||
435 | # clone relation with offline meter |
||
436 | cursor.execute(" SELECT offline_meter_id, is_output " |
||
437 | " FROM tbl_combined_equipments_offline_meters " |
||
438 | " WHERE combined_equipment_id = %s ", |
||
439 | (id_,)) |
||
440 | rows_offline_meters = cursor.fetchall() |
||
441 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
442 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
443 | " (combined_equipment_id, offline_meter_id, is_output) " |
||
444 | " VALUES ") |
||
445 | for row in rows_offline_meters: |
||
446 | add_values += " (" + str(new_id) + "," |
||
447 | add_values += "'" + str(row['offline_meter_id']) + "'," |
||
448 | add_values += str(bool(row['is_output'])) + "), " |
||
449 | # trim ", " at the end of string and then execute |
||
450 | cursor.execute(add_values[:-2]) |
||
451 | cnx.commit() |
||
452 | |||
453 | # clone relation with virtual meter |
||
454 | cursor.execute(" SELECT virtual_meter_id, is_output " |
||
455 | " FROM tbl_combined_equipments_virtual_meters " |
||
456 | " WHERE combined_equipment_id = %s ", |
||
457 | (id_,)) |
||
458 | rows_virtual_meters = cursor.fetchall() |
||
459 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
460 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
461 | " (combined_equipment_id, virtual_meter_id, is_output) " |
||
462 | " VALUES ") |
||
463 | for row in rows_virtual_meters: |
||
464 | add_values += " (" + str(new_id) + "," |
||
465 | add_values += str(row['virtual_meter_id']) + "," |
||
466 | add_values += str(bool(row['is_output'])) + "), " |
||
467 | # trim ", " at the end of string and then execute |
||
468 | cursor.execute(add_values[:-2]) |
||
469 | cnx.commit() |
||
470 | |||
471 | # clone parameters |
||
472 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
473 | " FROM tbl_combined_equipments_parameters " |
||
474 | " WHERE combined_equipment_id = %s ", |
||
475 | (id_,)) |
||
476 | rows_parameters = cursor.fetchall() |
||
477 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
478 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
||
479 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
||
480 | " numerator_meter_uuid, denominator_meter_uuid) " |
||
481 | " VALUES ") |
||
482 | for row in rows_parameters: |
||
483 | add_values += " (" + str(new_id) + "," |
||
484 | add_values += "'" + str(row['name']) + "'," |
||
485 | add_values += "'" + str(row['parameter_type']) + "'," |
||
486 | if row['constant'] is not None: |
||
487 | add_values += "'" + str(row['constant']) + "'," |
||
488 | else: |
||
489 | add_values += "null, " |
||
490 | |||
491 | if row['point_id'] is not None: |
||
492 | add_values += str(row['point_id']) + "," |
||
493 | else: |
||
494 | add_values += "null, " |
||
495 | |||
496 | if row['numerator_meter_uuid'] is not None: |
||
497 | add_values += "'" + row['numerator_meter_uuid'] + "'," |
||
498 | else: |
||
499 | add_values += "null, " |
||
500 | if row['denominator_meter_uuid'] is not None: |
||
501 | add_values += "'" + row['denominator_meter_uuid'] + "'), " |
||
502 | else: |
||
503 | add_values += "null), " |
||
504 | |||
505 | # trim ", " at the end of string and then execute |
||
506 | cursor.execute(add_values[:-2]) |
||
507 | cnx.commit() |
||
508 | |||
509 | cursor.close() |
||
510 | cnx.disconnect() |
||
511 | resp.status = falcon.HTTP_201 |
||
512 | resp.location = '/combinedequipments/' + str(new_id) |
||
513 | |||
514 | |||
515 | View Code Duplication | class CombinedEquipmentEquipmentCollection: |
|
516 | @staticmethod |
||
517 | def __init__(): |
||
518 | pass |
||
519 | |||
520 | @staticmethod |
||
521 | def on_options(req, resp, id_): |
||
522 | resp.status = falcon.HTTP_200 |
||
523 | |||
524 | @staticmethod |
||
525 | def on_get(req, resp, id_): |
||
526 | if not id_.isdigit() or int(id_) <= 0: |
||
527 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
528 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
529 | |||
530 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
531 | cursor = cnx.cursor() |
||
532 | |||
533 | cursor.execute(" SELECT name " |
||
534 | " FROM tbl_combined_equipments " |
||
535 | " WHERE id = %s ", (id_,)) |
||
536 | if cursor.fetchone() is None: |
||
537 | cursor.close() |
||
538 | cnx.disconnect() |
||
539 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
540 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
541 | |||
542 | query = (" SELECT e.id, e.name, e.uuid " |
||
543 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
544 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
545 | " ORDER BY e.id ") |
||
546 | cursor.execute(query, (id_,)) |
||
547 | rows = cursor.fetchall() |
||
548 | |||
549 | result = list() |
||
550 | if rows is not None and len(rows) > 0: |
||
551 | for row in rows: |
||
552 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
553 | result.append(meta_result) |
||
554 | |||
555 | resp.body = json.dumps(result) |
||
556 | |||
557 | @staticmethod |
||
558 | def on_post(req, resp, id_): |
||
559 | """Handles POST requests""" |
||
560 | try: |
||
561 | raw_json = req.stream.read().decode('utf-8') |
||
562 | except Exception as ex: |
||
563 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
564 | |||
565 | if not id_.isdigit() or int(id_) <= 0: |
||
566 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
567 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
568 | |||
569 | new_values = json.loads(raw_json) |
||
570 | |||
571 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
572 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
573 | new_values['data']['equipment_id'] <= 0: |
||
574 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
575 | description='API.INVALID_EQUIPMENT_ID') |
||
576 | equipment_id = new_values['data']['equipment_id'] |
||
577 | |||
578 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
579 | cursor = cnx.cursor() |
||
580 | |||
581 | cursor.execute(" SELECT name " |
||
582 | " from tbl_combined_equipments " |
||
583 | " WHERE id = %s ", (id_,)) |
||
584 | if cursor.fetchone() is None: |
||
585 | cursor.close() |
||
586 | cnx.disconnect() |
||
587 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
588 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
589 | |||
590 | cursor.execute(" SELECT name " |
||
591 | " FROM tbl_equipments " |
||
592 | " WHERE id = %s ", (equipment_id,)) |
||
593 | if cursor.fetchone() is None: |
||
594 | cursor.close() |
||
595 | cnx.disconnect() |
||
596 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
597 | description='API.EQUIPMENT_NOT_FOUND') |
||
598 | |||
599 | query = (" SELECT id " |
||
600 | " FROM tbl_combined_equipments_equipments " |
||
601 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
602 | cursor.execute(query, (id_, equipment_id,)) |
||
603 | if cursor.fetchone() is not None: |
||
604 | cursor.close() |
||
605 | cnx.disconnect() |
||
606 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
607 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTED') |
||
608 | |||
609 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
610 | " VALUES (%s, %s) ") |
||
611 | cursor.execute(add_row, (id_, equipment_id,)) |
||
612 | new_id = cursor.lastrowid |
||
613 | cnx.commit() |
||
614 | cursor.close() |
||
615 | cnx.disconnect() |
||
616 | |||
617 | resp.status = falcon.HTTP_201 |
||
618 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
||
619 | |||
620 | |||
621 | View Code Duplication | class CombinedEquipmentEquipmentItem: |
|
622 | @staticmethod |
||
623 | def __init__(): |
||
624 | pass |
||
625 | |||
626 | @staticmethod |
||
627 | def on_options(req, resp, id_, eid): |
||
628 | resp.status = falcon.HTTP_200 |
||
629 | |||
630 | @staticmethod |
||
631 | def on_delete(req, resp, id_, eid): |
||
632 | if not id_.isdigit() or int(id_) <= 0: |
||
633 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
634 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
635 | |||
636 | if not eid.isdigit() or int(eid) <= 0: |
||
637 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
638 | description='API.INVALID_EQUIPMENT_ID') |
||
639 | |||
640 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
641 | cursor = cnx.cursor() |
||
642 | |||
643 | cursor.execute(" SELECT name " |
||
644 | " FROM tbl_combined_equipments " |
||
645 | " WHERE id = %s ", (id_,)) |
||
646 | if cursor.fetchone() is None: |
||
647 | cursor.close() |
||
648 | cnx.disconnect() |
||
649 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
650 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
651 | |||
652 | cursor.execute(" SELECT name " |
||
653 | " FROM tbl_equipments " |
||
654 | " WHERE id = %s ", (eid,)) |
||
655 | if cursor.fetchone() is None: |
||
656 | cursor.close() |
||
657 | cnx.disconnect() |
||
658 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
659 | description='API.EQUIPMENT_NOT_FOUND') |
||
660 | |||
661 | cursor.execute(" SELECT id " |
||
662 | " FROM tbl_combined_equipments_equipments " |
||
663 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
664 | if cursor.fetchone() is None: |
||
665 | cursor.close() |
||
666 | cnx.disconnect() |
||
667 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
668 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_NOT_FOUND') |
||
669 | |||
670 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments " |
||
671 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
672 | cnx.commit() |
||
673 | |||
674 | cursor.close() |
||
675 | cnx.disconnect() |
||
676 | |||
677 | resp.status = falcon.HTTP_204 |
||
678 | |||
679 | |||
680 | View Code Duplication | class CombinedEquipmentParameterCollection: |
|
681 | @staticmethod |
||
682 | def __init__(): |
||
683 | pass |
||
684 | |||
685 | @staticmethod |
||
686 | def on_options(req, resp, id_): |
||
687 | resp.status = falcon.HTTP_200 |
||
688 | |||
689 | @staticmethod |
||
690 | def on_get(req, resp, id_): |
||
691 | if not id_.isdigit() or int(id_) <= 0: |
||
692 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
693 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
694 | |||
695 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
696 | cursor = cnx.cursor(dictionary=True) |
||
697 | |||
698 | cursor.execute(" SELECT name " |
||
699 | " FROM tbl_combined_equipments " |
||
700 | " WHERE id = %s ", (id_,)) |
||
701 | if cursor.fetchone() is None: |
||
702 | cursor.close() |
||
703 | cnx.disconnect() |
||
704 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
705 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
706 | |||
707 | query = (" SELECT id, name " |
||
708 | " FROM tbl_points ") |
||
709 | cursor.execute(query) |
||
710 | rows_points = cursor.fetchall() |
||
711 | |||
712 | point_dict = dict() |
||
713 | if rows_points is not None and len(rows_points) > 0: |
||
714 | for row in rows_points: |
||
715 | point_dict[row['id']] = {"id": row['id'], |
||
716 | "name": row['name']} |
||
717 | |||
718 | query = (" SELECT id, name, uuid " |
||
719 | " FROM tbl_meters ") |
||
720 | cursor.execute(query) |
||
721 | rows_meters = cursor.fetchall() |
||
722 | |||
723 | meter_dict = dict() |
||
724 | if rows_meters is not None and len(rows_meters) > 0: |
||
725 | for row in rows_meters: |
||
726 | meter_dict[row['uuid']] = {"type": 'meter', |
||
727 | "id": row['id'], |
||
728 | "name": row['name'], |
||
729 | "uuid": row['uuid']} |
||
730 | |||
731 | query = (" SELECT id, name, uuid " |
||
732 | " FROM tbl_offline_meters ") |
||
733 | cursor.execute(query) |
||
734 | rows_offline_meters = cursor.fetchall() |
||
735 | |||
736 | offline_meter_dict = dict() |
||
737 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
738 | for row in rows_offline_meters: |
||
739 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
740 | "id": row['id'], |
||
741 | "name": row['name'], |
||
742 | "uuid": row['uuid']} |
||
743 | |||
744 | query = (" SELECT id, name, uuid " |
||
745 | " FROM tbl_virtual_meters ") |
||
746 | cursor.execute(query) |
||
747 | rows_virtual_meters = cursor.fetchall() |
||
748 | |||
749 | virtual_meter_dict = dict() |
||
750 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
751 | for row in rows_virtual_meters: |
||
752 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
753 | "id": row['id'], |
||
754 | "name": row['name'], |
||
755 | "uuid": row['uuid']} |
||
756 | |||
757 | query = (" SELECT id, name, parameter_type, " |
||
758 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
759 | " FROM tbl_combined_equipments_parameters " |
||
760 | " WHERE combined_equipment_id = %s " |
||
761 | " ORDER BY id ") |
||
762 | cursor.execute(query, (id_, )) |
||
763 | rows_parameters = cursor.fetchall() |
||
764 | |||
765 | result = list() |
||
766 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
767 | for row in rows_parameters: |
||
768 | constant = None |
||
769 | point = None |
||
770 | numerator_meter = None |
||
771 | denominator_meter = None |
||
772 | if row['parameter_type'] == 'point': |
||
773 | point = point_dict.get(row['point_id'], None) |
||
774 | constant = None |
||
775 | numerator_meter = None |
||
776 | denominator_meter = None |
||
777 | elif row['parameter_type'] == 'constant': |
||
778 | constant = row['constant'] |
||
779 | point = None |
||
780 | numerator_meter = None |
||
781 | denominator_meter = None |
||
782 | elif row['parameter_type'] == 'fraction': |
||
783 | constant = None |
||
784 | point = None |
||
785 | # find numerator meter by uuid |
||
786 | numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
||
787 | if numerator_meter is None: |
||
788 | numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
||
789 | if numerator_meter is None: |
||
790 | numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
||
791 | # find denominator meter by uuid |
||
792 | denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
||
793 | if denominator_meter is None: |
||
794 | denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
||
795 | if denominator_meter is None: |
||
796 | denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
||
797 | |||
798 | meta_result = {"id": row['id'], |
||
799 | "name": row['name'], |
||
800 | "parameter_type": row['parameter_type'], |
||
801 | "constant": constant, |
||
802 | "point": point, |
||
803 | "numerator_meter": numerator_meter, |
||
804 | "denominator_meter": denominator_meter} |
||
805 | result.append(meta_result) |
||
806 | |||
807 | cursor.close() |
||
808 | cnx.disconnect() |
||
809 | resp.body = json.dumps(result) |
||
810 | |||
811 | @staticmethod |
||
812 | def on_post(req, resp, id_): |
||
813 | """Handles POST requests""" |
||
814 | if not id_.isdigit() or int(id_) <= 0: |
||
815 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
816 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
817 | try: |
||
818 | raw_json = req.stream.read().decode('utf-8') |
||
819 | except Exception as ex: |
||
820 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
821 | |||
822 | new_values = json.loads(raw_json) |
||
823 | |||
824 | if 'name' not in new_values['data'].keys() or \ |
||
825 | not isinstance(new_values['data']['name'], str) or \ |
||
826 | len(str.strip(new_values['data']['name'])) == 0: |
||
827 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
828 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
829 | name = str.strip(new_values['data']['name']) |
||
830 | |||
831 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
832 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
833 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
834 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
835 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
836 | |||
837 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
838 | |||
839 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
840 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
841 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
842 | |||
843 | constant = None |
||
844 | if 'constant' in new_values['data'].keys(): |
||
845 | if new_values['data']['constant'] is not None and \ |
||
846 | isinstance(new_values['data']['constant'], str) and \ |
||
847 | len(str.strip(new_values['data']['constant'])) > 0: |
||
848 | constant = str.strip(new_values['data']['constant']) |
||
849 | |||
850 | point_id = None |
||
851 | if 'point_id' in new_values['data'].keys(): |
||
852 | if new_values['data']['point_id'] is not None and \ |
||
853 | new_values['data']['point_id'] <= 0: |
||
854 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
855 | description='API.INVALID_POINT_ID') |
||
856 | point_id = new_values['data']['point_id'] |
||
857 | |||
858 | numerator_meter_uuid = None |
||
859 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
860 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
861 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
862 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
863 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
864 | |||
865 | denominator_meter_uuid = None |
||
866 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
867 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
868 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
869 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
870 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
871 | |||
872 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
873 | cursor = cnx.cursor(dictionary=True) |
||
874 | |||
875 | cursor.execute(" SELECT name " |
||
876 | " FROM tbl_combined_equipments " |
||
877 | " WHERE id = %s ", (id_,)) |
||
878 | if cursor.fetchone() is None: |
||
879 | cursor.close() |
||
880 | cnx.disconnect() |
||
881 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
882 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
883 | |||
884 | cursor.execute(" SELECT name " |
||
885 | " FROM tbl_combined_equipments_parameters " |
||
886 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
||
887 | if cursor.fetchone() is not None: |
||
888 | cursor.close() |
||
889 | cnx.disconnect() |
||
890 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
891 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
892 | |||
893 | # validate by parameter type |
||
894 | if parameter_type == 'point': |
||
895 | if point_id is None: |
||
896 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
897 | description='API.INVALID_POINT_ID') |
||
898 | |||
899 | query = (" SELECT id, name " |
||
900 | " FROM tbl_points " |
||
901 | " WHERE id = %s ") |
||
902 | cursor.execute(query, (point_id, )) |
||
903 | if cursor.fetchone() is None: |
||
904 | cursor.close() |
||
905 | cnx.disconnect() |
||
906 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
907 | description='API.POINT_NOT_FOUND') |
||
908 | |||
909 | elif parameter_type == 'constant': |
||
910 | if constant is None: |
||
911 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
912 | description='API.INVALID_CONSTANT_VALUE') |
||
913 | |||
914 | elif parameter_type == 'fraction': |
||
915 | |||
916 | query = (" SELECT id, name, uuid " |
||
917 | " FROM tbl_meters ") |
||
918 | cursor.execute(query) |
||
919 | rows_meters = cursor.fetchall() |
||
920 | |||
921 | meter_dict = dict() |
||
922 | if rows_meters is not None and len(rows_meters) > 0: |
||
923 | for row in rows_meters: |
||
924 | meter_dict[row['uuid']] = {"type": 'meter', |
||
925 | "id": row['id'], |
||
926 | "name": row['name'], |
||
927 | "uuid": row['uuid']} |
||
928 | |||
929 | query = (" SELECT id, name, uuid " |
||
930 | " FROM tbl_offline_meters ") |
||
931 | cursor.execute(query) |
||
932 | rows_offline_meters = cursor.fetchall() |
||
933 | |||
934 | offline_meter_dict = dict() |
||
935 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
936 | for row in rows_offline_meters: |
||
937 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
938 | "id": row['id'], |
||
939 | "name": row['name'], |
||
940 | "uuid": row['uuid']} |
||
941 | |||
942 | query = (" SELECT id, name, uuid " |
||
943 | " FROM tbl_virtual_meters ") |
||
944 | cursor.execute(query) |
||
945 | rows_virtual_meters = cursor.fetchall() |
||
946 | |||
947 | virtual_meter_dict = dict() |
||
948 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
949 | for row in rows_virtual_meters: |
||
950 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
951 | "id": row['id'], |
||
952 | "name": row['name'], |
||
953 | "uuid": row['uuid']} |
||
954 | |||
955 | # validate numerator meter uuid |
||
956 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
957 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
958 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
959 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
960 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
961 | |||
962 | # validate denominator meter uuid |
||
963 | if denominator_meter_uuid == numerator_meter_uuid: |
||
964 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
965 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
966 | |||
967 | if denominator_meter_uuid not in meter_dict and \ |
||
968 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
969 | denominator_meter_uuid not in offline_meter_dict: |
||
970 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
971 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
972 | |||
973 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
974 | " (combined_equipment_id, name, parameter_type, constant, " |
||
975 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
976 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
977 | cursor.execute(add_values, (id_, |
||
978 | name, |
||
979 | parameter_type, |
||
980 | constant, |
||
981 | point_id, |
||
982 | numerator_meter_uuid, |
||
983 | denominator_meter_uuid)) |
||
984 | new_id = cursor.lastrowid |
||
985 | cnx.commit() |
||
986 | cursor.close() |
||
987 | cnx.disconnect() |
||
988 | |||
989 | resp.status = falcon.HTTP_201 |
||
990 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
||
991 | |||
992 | |||
993 | View Code Duplication | class CombinedEquipmentParameterItem: |
|
994 | @staticmethod |
||
995 | def __init__(): |
||
996 | pass |
||
997 | |||
998 | @staticmethod |
||
999 | def on_options(req, resp, id_, pid): |
||
1000 | resp.status = falcon.HTTP_200 |
||
1001 | |||
1002 | @staticmethod |
||
1003 | def on_get(req, resp, id_, pid): |
||
1004 | if not id_.isdigit() or int(id_) <= 0: |
||
1005 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1006 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1007 | |||
1008 | if not pid.isdigit() or int(pid) <= 0: |
||
1009 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1010 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1011 | |||
1012 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1013 | cursor = cnx.cursor(dictionary=True) |
||
1014 | |||
1015 | query = (" SELECT id, name " |
||
1016 | " FROM tbl_points ") |
||
1017 | cursor.execute(query) |
||
1018 | rows_points = cursor.fetchall() |
||
1019 | |||
1020 | point_dict = dict() |
||
1021 | if rows_points is not None and len(rows_points) > 0: |
||
1022 | for row in rows_points: |
||
1023 | point_dict[row['id']] = {"id": row['id'], |
||
1024 | "name": row['name']} |
||
1025 | |||
1026 | query = (" SELECT id, name, uuid " |
||
1027 | " FROM tbl_meters ") |
||
1028 | cursor.execute(query) |
||
1029 | rows_meters = cursor.fetchall() |
||
1030 | |||
1031 | meter_dict = dict() |
||
1032 | if rows_meters is not None and len(rows_meters) > 0: |
||
1033 | for row in rows_meters: |
||
1034 | meter_dict[row['uuid']] = {"type": 'meter', |
||
1035 | "id": row['id'], |
||
1036 | "name": row['name'], |
||
1037 | "uuid": row['uuid']} |
||
1038 | |||
1039 | query = (" SELECT id, name, uuid " |
||
1040 | " FROM tbl_offline_meters ") |
||
1041 | cursor.execute(query) |
||
1042 | rows_offline_meters = cursor.fetchall() |
||
1043 | |||
1044 | offline_meter_dict = dict() |
||
1045 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1046 | for row in rows_offline_meters: |
||
1047 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
1048 | "id": row['id'], |
||
1049 | "name": row['name'], |
||
1050 | "uuid": row['uuid']} |
||
1051 | |||
1052 | query = (" SELECT id, name, uuid " |
||
1053 | " FROM tbl_virtual_meters ") |
||
1054 | cursor.execute(query) |
||
1055 | rows_virtual_meters = cursor.fetchall() |
||
1056 | |||
1057 | virtual_meter_dict = dict() |
||
1058 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1059 | for row in rows_virtual_meters: |
||
1060 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
1061 | "id": row['id'], |
||
1062 | "name": row['name'], |
||
1063 | "uuid": row['uuid']} |
||
1064 | |||
1065 | query = (" SELECT id, name, parameter_type, " |
||
1066 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
1067 | " FROM tbl_combined_equipments_parameters " |
||
1068 | " WHERE combined_equipment_id = %s AND id = %s ") |
||
1069 | cursor.execute(query, (id_, pid)) |
||
1070 | row = cursor.fetchone() |
||
1071 | cursor.close() |
||
1072 | cnx.disconnect() |
||
1073 | |||
1074 | if row is None: |
||
1075 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1076 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1077 | else: |
||
1078 | constant = None |
||
1079 | point = None |
||
1080 | numerator_meter = None |
||
1081 | denominator_meter = None |
||
1082 | if row['parameter_type'] == 'point': |
||
1083 | point = point_dict.get(row['point_id'], None) |
||
1084 | constant = None |
||
1085 | numerator_meter = None |
||
1086 | denominator_meter = None |
||
1087 | elif row['parameter_type'] == 'constant': |
||
1088 | constant = row['constant'] |
||
1089 | point = None |
||
1090 | numerator_meter = None |
||
1091 | denominator_meter = None |
||
1092 | elif row['parameter_type'] == 'fraction': |
||
1093 | constant = None |
||
1094 | point = None |
||
1095 | # find numerator meter by uuid |
||
1096 | numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
||
1097 | if numerator_meter is None: |
||
1098 | numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
||
1099 | if numerator_meter is None: |
||
1100 | numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
||
1101 | # find denominator meter by uuid |
||
1102 | denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
||
1103 | if denominator_meter is None: |
||
1104 | denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
||
1105 | if denominator_meter is None: |
||
1106 | denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
||
1107 | |||
1108 | meta_result = {"id": row['id'], |
||
1109 | "name": row['name'], |
||
1110 | "parameter_type": row['parameter_type'], |
||
1111 | "constant": constant, |
||
1112 | "point": point, |
||
1113 | "numerator_meter": numerator_meter, |
||
1114 | "denominator_meter": denominator_meter} |
||
1115 | |||
1116 | resp.body = json.dumps(meta_result) |
||
1117 | |||
1118 | @staticmethod |
||
1119 | def on_delete(req, resp, id_, pid): |
||
1120 | if not id_.isdigit() or int(id_) <= 0: |
||
1121 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1122 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1123 | |||
1124 | if not pid.isdigit() or int(pid) <= 0: |
||
1125 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1126 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1127 | |||
1128 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1129 | cursor = cnx.cursor() |
||
1130 | |||
1131 | cursor.execute(" SELECT name " |
||
1132 | " FROM tbl_combined_equipments " |
||
1133 | " WHERE id = %s ", |
||
1134 | (id_,)) |
||
1135 | row = cursor.fetchone() |
||
1136 | if row is None: |
||
1137 | cursor.close() |
||
1138 | cnx.disconnect() |
||
1139 | raise falcon.HTTPError(falcon.HTTP_400, |
||
1140 | title='API.NOT_FOUND', |
||
1141 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1142 | |||
1143 | cursor.execute(" SELECT name " |
||
1144 | " FROM tbl_combined_equipments_parameters " |
||
1145 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
1146 | (id_, pid,)) |
||
1147 | row = cursor.fetchone() |
||
1148 | if row is None: |
||
1149 | cursor.close() |
||
1150 | cnx.disconnect() |
||
1151 | raise falcon.HTTPError(falcon.HTTP_400, |
||
1152 | title='API.NOT_FOUND', |
||
1153 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1154 | |||
1155 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
||
1156 | " WHERE id = %s ", (pid, )) |
||
1157 | cnx.commit() |
||
1158 | |||
1159 | cursor.close() |
||
1160 | cnx.disconnect() |
||
1161 | |||
1162 | resp.status = falcon.HTTP_204 |
||
1163 | |||
1164 | @staticmethod |
||
1165 | def on_put(req, resp, id_, pid): |
||
1166 | """Handles POST requests""" |
||
1167 | if not id_.isdigit() or int(id_) <= 0: |
||
1168 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1169 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1170 | |||
1171 | if not pid.isdigit() or int(pid) <= 0: |
||
1172 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1173 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
1174 | |||
1175 | try: |
||
1176 | raw_json = req.stream.read().decode('utf-8') |
||
1177 | except Exception as ex: |
||
1178 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
1179 | |||
1180 | new_values = json.loads(raw_json) |
||
1181 | |||
1182 | if 'name' not in new_values['data'].keys() or \ |
||
1183 | not isinstance(new_values['data']['name'], str) or \ |
||
1184 | len(str.strip(new_values['data']['name'])) == 0: |
||
1185 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1186 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
1187 | name = str.strip(new_values['data']['name']) |
||
1188 | |||
1189 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
1190 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
1191 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
1192 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1193 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
1194 | |||
1195 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
1196 | |||
1197 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
1198 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1199 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
1200 | |||
1201 | constant = None |
||
1202 | if 'constant' in new_values['data'].keys(): |
||
1203 | if new_values['data']['constant'] is not None and \ |
||
1204 | isinstance(new_values['data']['constant'], str) and \ |
||
1205 | len(str.strip(new_values['data']['constant'])) > 0: |
||
1206 | constant = str.strip(new_values['data']['constant']) |
||
1207 | |||
1208 | point_id = None |
||
1209 | if 'point_id' in new_values['data'].keys(): |
||
1210 | if new_values['data']['point_id'] is not None and \ |
||
1211 | new_values['data']['point_id'] <= 0: |
||
1212 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1213 | description='API.INVALID_POINT_ID') |
||
1214 | point_id = new_values['data']['point_id'] |
||
1215 | |||
1216 | numerator_meter_uuid = None |
||
1217 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
1218 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
1219 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
1220 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
1221 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
1222 | |||
1223 | denominator_meter_uuid = None |
||
1224 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
1225 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
1226 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
1227 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
1228 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
1229 | |||
1230 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1231 | cursor = cnx.cursor(dictionary=True) |
||
1232 | |||
1233 | cursor.execute(" SELECT name " |
||
1234 | " FROM tbl_combined_equipments " |
||
1235 | " WHERE id = %s ", (id_,)) |
||
1236 | if cursor.fetchone() is None: |
||
1237 | cursor.close() |
||
1238 | cnx.disconnect() |
||
1239 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
1240 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1241 | |||
1242 | cursor.execute(" SELECT name " |
||
1243 | " FROM tbl_combined_equipments_parameters " |
||
1244 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
1245 | (id_, pid,)) |
||
1246 | row = cursor.fetchone() |
||
1247 | if row is None: |
||
1248 | cursor.close() |
||
1249 | cnx.disconnect() |
||
1250 | raise falcon.HTTPError(falcon.HTTP_400, |
||
1251 | title='API.NOT_FOUND', |
||
1252 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
1253 | |||
1254 | cursor.execute(" SELECT name " |
||
1255 | " FROM tbl_combined_equipments_parameters " |
||
1256 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
||
1257 | row = cursor.fetchone() |
||
1258 | if row is not None: |
||
1259 | cursor.close() |
||
1260 | cnx.disconnect() |
||
1261 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1262 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
1263 | |||
1264 | # validate by parameter type |
||
1265 | if parameter_type == 'point': |
||
1266 | if point_id is None: |
||
1267 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1268 | description='API.INVALID_POINT_ID') |
||
1269 | |||
1270 | query = (" SELECT id, name " |
||
1271 | " FROM tbl_points " |
||
1272 | " WHERE id = %s ") |
||
1273 | cursor.execute(query, (point_id, )) |
||
1274 | row = cursor.fetchone() |
||
1275 | if row is None: |
||
1276 | cursor.close() |
||
1277 | cnx.disconnect() |
||
1278 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1279 | description='API.POINT_NOT_FOUND') |
||
1280 | |||
1281 | elif parameter_type == 'constant': |
||
1282 | if constant is None: |
||
1283 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1284 | description='API.INVALID_CONSTANT_VALUE') |
||
1285 | |||
1286 | elif parameter_type == 'fraction': |
||
1287 | |||
1288 | query = (" SELECT id, name, uuid " |
||
1289 | " FROM tbl_meters ") |
||
1290 | cursor.execute(query) |
||
1291 | rows_meters = cursor.fetchall() |
||
1292 | |||
1293 | meter_dict = dict() |
||
1294 | if rows_meters is not None and len(rows_meters) > 0: |
||
1295 | for row in rows_meters: |
||
1296 | meter_dict[row['uuid']] = {"type": 'meter', |
||
1297 | "id": row['id'], |
||
1298 | "name": row['name'], |
||
1299 | "uuid": row['uuid']} |
||
1300 | |||
1301 | query = (" SELECT id, name, uuid " |
||
1302 | " FROM tbl_offline_meters ") |
||
1303 | cursor.execute(query) |
||
1304 | rows_offline_meters = cursor.fetchall() |
||
1305 | |||
1306 | offline_meter_dict = dict() |
||
1307 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1308 | for row in rows_offline_meters: |
||
1309 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
1310 | "id": row['id'], |
||
1311 | "name": row['name'], |
||
1312 | "uuid": row['uuid']} |
||
1313 | |||
1314 | query = (" SELECT id, name, uuid " |
||
1315 | " FROM tbl_virtual_meters ") |
||
1316 | cursor.execute(query) |
||
1317 | rows_virtual_meters = cursor.fetchall() |
||
1318 | |||
1319 | virtual_meter_dict = dict() |
||
1320 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1321 | for row in rows_virtual_meters: |
||
1322 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
1323 | "id": row['id'], |
||
1324 | "name": row['name'], |
||
1325 | "uuid": row['uuid']} |
||
1326 | |||
1327 | # validate numerator meter uuid |
||
1328 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
1329 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
1330 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
1331 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1332 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
1333 | |||
1334 | # validate denominator meter uuid |
||
1335 | if denominator_meter_uuid == numerator_meter_uuid: |
||
1336 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1337 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1338 | |||
1339 | if denominator_meter_uuid not in meter_dict and \ |
||
1340 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
1341 | denominator_meter_uuid not in offline_meter_dict: |
||
1342 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1343 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
1344 | |||
1345 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
||
1346 | " SET name = %s , parameter_type = %s, constant = %s, " |
||
1347 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid =%s " |
||
1348 | " WHERE id = %s ") |
||
1349 | cursor.execute(add_values, (name, |
||
1350 | parameter_type, |
||
1351 | constant, |
||
1352 | point_id, |
||
1353 | numerator_meter_uuid, |
||
1354 | denominator_meter_uuid, |
||
1355 | pid)) |
||
1356 | cnx.commit() |
||
1357 | |||
1358 | cursor.close() |
||
1359 | cnx.disconnect() |
||
1360 | |||
1361 | resp.status = falcon.HTTP_200 |
||
1362 | |||
1363 | |||
1364 | View Code Duplication | class CombinedEquipmentMeterCollection: |
|
1365 | @staticmethod |
||
1366 | def __init__(): |
||
1367 | pass |
||
1368 | |||
1369 | @staticmethod |
||
1370 | def on_options(req, resp, id_): |
||
1371 | resp.status = falcon.HTTP_200 |
||
1372 | |||
1373 | @staticmethod |
||
1374 | def on_get(req, resp, id_): |
||
1375 | if not id_.isdigit() or int(id_) <= 0: |
||
1376 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1377 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1378 | |||
1379 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1380 | cursor = cnx.cursor(dictionary=True) |
||
1381 | |||
1382 | cursor.execute(" SELECT name " |
||
1383 | " FROM tbl_combined_equipments " |
||
1384 | " WHERE id = %s ", (id_,)) |
||
1385 | if cursor.fetchone() is None: |
||
1386 | cursor.close() |
||
1387 | cnx.disconnect() |
||
1388 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1389 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1390 | |||
1391 | query = (" SELECT id, name, uuid " |
||
1392 | " FROM tbl_energy_categories ") |
||
1393 | cursor.execute(query) |
||
1394 | rows_energy_categories = cursor.fetchall() |
||
1395 | |||
1396 | energy_category_dict = dict() |
||
1397 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1398 | for row in rows_energy_categories: |
||
1399 | energy_category_dict[row['id']] = {"id": row['id'], |
||
1400 | "name": row['name'], |
||
1401 | "uuid": row['uuid']} |
||
1402 | |||
1403 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1404 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
1405 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
1406 | " ORDER BY m.id ") |
||
1407 | cursor.execute(query, (id_,)) |
||
1408 | rows = cursor.fetchall() |
||
1409 | |||
1410 | result = list() |
||
1411 | if rows is not None and len(rows) > 0: |
||
1412 | for row in rows: |
||
1413 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
1414 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
1415 | "energy_category": energy_category, |
||
1416 | "is_output": bool(row['is_output'])} |
||
1417 | result.append(meta_result) |
||
1418 | |||
1419 | resp.body = json.dumps(result) |
||
1420 | |||
1421 | @staticmethod |
||
1422 | def on_post(req, resp, id_): |
||
1423 | """Handles POST requests""" |
||
1424 | try: |
||
1425 | raw_json = req.stream.read().decode('utf-8') |
||
1426 | except Exception as ex: |
||
1427 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
1428 | |||
1429 | if not id_.isdigit() or int(id_) <= 0: |
||
1430 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1431 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1432 | |||
1433 | new_values = json.loads(raw_json) |
||
1434 | |||
1435 | if 'meter_id' not in new_values['data'].keys() or \ |
||
1436 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
1437 | new_values['data']['meter_id'] <= 0: |
||
1438 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1439 | description='API.INVALID_METER_ID') |
||
1440 | meter_id = new_values['data']['meter_id'] |
||
1441 | |||
1442 | if 'is_output' not in new_values['data'].keys() or \ |
||
1443 | not isinstance(new_values['data']['is_output'], bool): |
||
1444 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1445 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
1446 | is_output = new_values['data']['is_output'] |
||
1447 | |||
1448 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1449 | cursor = cnx.cursor() |
||
1450 | |||
1451 | cursor.execute(" SELECT name " |
||
1452 | " from tbl_combined_equipments " |
||
1453 | " WHERE id = %s ", (id_,)) |
||
1454 | if cursor.fetchone() is None: |
||
1455 | cursor.close() |
||
1456 | cnx.disconnect() |
||
1457 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1458 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1459 | |||
1460 | cursor.execute(" SELECT name " |
||
1461 | " FROM tbl_meters " |
||
1462 | " WHERE id = %s ", (meter_id,)) |
||
1463 | if cursor.fetchone() is None: |
||
1464 | cursor.close() |
||
1465 | cnx.disconnect() |
||
1466 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1467 | description='API.METER_NOT_FOUND') |
||
1468 | |||
1469 | query = (" SELECT id " |
||
1470 | " FROM tbl_combined_equipments_meters " |
||
1471 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
1472 | cursor.execute(query, (id_, meter_id,)) |
||
1473 | if cursor.fetchone() is not None: |
||
1474 | cursor.close() |
||
1475 | cnx.disconnect() |
||
1476 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
1477 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTED') |
||
1478 | |||
1479 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
1480 | " VALUES (%s, %s, %s) ") |
||
1481 | cursor.execute(add_row, (id_, meter_id, is_output)) |
||
1482 | new_id = cursor.lastrowid |
||
1483 | cnx.commit() |
||
1484 | cursor.close() |
||
1485 | cnx.disconnect() |
||
1486 | |||
1487 | resp.status = falcon.HTTP_201 |
||
1488 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
||
1489 | |||
1490 | |||
1491 | View Code Duplication | class CombinedEquipmentMeterItem: |
|
1492 | @staticmethod |
||
1493 | def __init__(): |
||
1494 | pass |
||
1495 | |||
1496 | @staticmethod |
||
1497 | def on_options(req, resp, id_, mid): |
||
1498 | resp.status = falcon.HTTP_200 |
||
1499 | |||
1500 | @staticmethod |
||
1501 | def on_delete(req, resp, id_, mid): |
||
1502 | if not id_.isdigit() or int(id_) <= 0: |
||
1503 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1504 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1505 | |||
1506 | if not mid.isdigit() or int(mid) <= 0: |
||
1507 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1508 | description='API.INVALID_METER_ID') |
||
1509 | |||
1510 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1511 | cursor = cnx.cursor() |
||
1512 | |||
1513 | cursor.execute(" SELECT name " |
||
1514 | " FROM tbl_combined_equipments " |
||
1515 | " WHERE id = %s ", (id_,)) |
||
1516 | if cursor.fetchone() is None: |
||
1517 | cursor.close() |
||
1518 | cnx.disconnect() |
||
1519 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1520 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1521 | |||
1522 | cursor.execute(" SELECT name " |
||
1523 | " FROM tbl_meters " |
||
1524 | " WHERE id = %s ", (mid,)) |
||
1525 | if cursor.fetchone() is None: |
||
1526 | cursor.close() |
||
1527 | cnx.disconnect() |
||
1528 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1529 | description='API.METER_NOT_FOUND') |
||
1530 | |||
1531 | cursor.execute(" SELECT id " |
||
1532 | " FROM tbl_combined_equipments_meters " |
||
1533 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
1534 | if cursor.fetchone() is None: |
||
1535 | cursor.close() |
||
1536 | cnx.disconnect() |
||
1537 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1538 | description='API.COMBINED_EQUIPMENT_METER_RELATION_NOT_FOUND') |
||
1539 | |||
1540 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters " |
||
1541 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
1542 | cnx.commit() |
||
1543 | |||
1544 | cursor.close() |
||
1545 | cnx.disconnect() |
||
1546 | |||
1547 | resp.status = falcon.HTTP_204 |
||
1548 | |||
1549 | |||
1550 | View Code Duplication | class CombinedEquipmentOfflineMeterCollection: |
|
1551 | @staticmethod |
||
1552 | def __init__(): |
||
1553 | pass |
||
1554 | |||
1555 | @staticmethod |
||
1556 | def on_options(req, resp, id_): |
||
1557 | resp.status = falcon.HTTP_200 |
||
1558 | |||
1559 | @staticmethod |
||
1560 | def on_get(req, resp, id_): |
||
1561 | if not id_.isdigit() or int(id_) <= 0: |
||
1562 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1563 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1564 | |||
1565 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1566 | cursor = cnx.cursor(dictionary=True) |
||
1567 | |||
1568 | cursor.execute(" SELECT name " |
||
1569 | " FROM tbl_combined_equipments " |
||
1570 | " WHERE id = %s ", (id_,)) |
||
1571 | if cursor.fetchone() is None: |
||
1572 | cursor.close() |
||
1573 | cnx.disconnect() |
||
1574 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1575 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1576 | |||
1577 | query = (" SELECT id, name, uuid " |
||
1578 | " FROM tbl_energy_categories ") |
||
1579 | cursor.execute(query) |
||
1580 | rows_energy_categories = cursor.fetchall() |
||
1581 | |||
1582 | energy_category_dict = dict() |
||
1583 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1584 | for row in rows_energy_categories: |
||
1585 | energy_category_dict[row['id']] = {"id": row['id'], |
||
1586 | "name": row['name'], |
||
1587 | "uuid": row['uuid']} |
||
1588 | |||
1589 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1590 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
1591 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
1592 | " ORDER BY m.id ") |
||
1593 | cursor.execute(query, (id_,)) |
||
1594 | rows = cursor.fetchall() |
||
1595 | |||
1596 | result = list() |
||
1597 | if rows is not None and len(rows) > 0: |
||
1598 | for row in rows: |
||
1599 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
1600 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
1601 | "energy_category": energy_category, |
||
1602 | "is_output": bool(row['is_output'])} |
||
1603 | result.append(meta_result) |
||
1604 | |||
1605 | resp.body = json.dumps(result) |
||
1606 | |||
1607 | @staticmethod |
||
1608 | def on_post(req, resp, id_): |
||
1609 | """Handles POST requests""" |
||
1610 | try: |
||
1611 | raw_json = req.stream.read().decode('utf-8') |
||
1612 | except Exception as ex: |
||
1613 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
1614 | |||
1615 | if not id_.isdigit() or int(id_) <= 0: |
||
1616 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1617 | description='API.INVALID_COMBINED_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(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(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_combined_equipments " |
||
1639 | " WHERE id = %s ", (id_,)) |
||
1640 | if cursor.fetchone() is None: |
||
1641 | cursor.close() |
||
1642 | cnx.disconnect() |
||
1643 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1644 | description='API.COMBINED_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.disconnect() |
||
1652 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1653 | description='API.OFFLINE_METER_NOT_FOUND') |
||
1654 | |||
1655 | query = (" SELECT id " |
||
1656 | " FROM tbl_combined_equipments_offline_meters " |
||
1657 | " WHERE combined_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.disconnect() |
||
1662 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
1663 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTED') |
||
1664 | |||
1665 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
1666 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
1667 | " VALUES (%s, %s, %s) ") |
||
1668 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
||
1669 | new_id = cursor.lastrowid |
||
1670 | cnx.commit() |
||
1671 | cursor.close() |
||
1672 | cnx.disconnect() |
||
1673 | |||
1674 | resp.status = falcon.HTTP_201 |
||
1675 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
1676 | |||
1677 | |||
1678 | View Code Duplication | class CombinedEquipmentOfflineMeterItem: |
|
1679 | @staticmethod |
||
1680 | def __init__(): |
||
1681 | pass |
||
1682 | |||
1683 | @staticmethod |
||
1684 | def on_options(req, resp, id_, mid): |
||
1685 | resp.status = falcon.HTTP_200 |
||
1686 | |||
1687 | @staticmethod |
||
1688 | def on_delete(req, resp, id_, mid): |
||
1689 | if not id_.isdigit() or int(id_) <= 0: |
||
1690 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1691 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1692 | |||
1693 | if not mid.isdigit() or int(mid) <= 0: |
||
1694 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1695 | description='API.INVALID_OFFLINE_METER_ID') |
||
1696 | |||
1697 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1698 | cursor = cnx.cursor() |
||
1699 | |||
1700 | cursor.execute(" SELECT name " |
||
1701 | " FROM tbl_combined_equipments " |
||
1702 | " WHERE id = %s ", (id_,)) |
||
1703 | if cursor.fetchone() is None: |
||
1704 | cursor.close() |
||
1705 | cnx.disconnect() |
||
1706 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1707 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1708 | |||
1709 | cursor.execute(" SELECT name " |
||
1710 | " FROM tbl_offline_meters " |
||
1711 | " WHERE id = %s ", (mid,)) |
||
1712 | if cursor.fetchone() is None: |
||
1713 | cursor.close() |
||
1714 | cnx.disconnect() |
||
1715 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1716 | description='API.OFFLINE_METER_NOT_FOUND') |
||
1717 | |||
1718 | cursor.execute(" SELECT id " |
||
1719 | " FROM tbl_combined_equipments_offline_meters " |
||
1720 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
1721 | if cursor.fetchone() is None: |
||
1722 | cursor.close() |
||
1723 | cnx.disconnect() |
||
1724 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1725 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
||
1726 | |||
1727 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters " |
||
1728 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
1729 | cnx.commit() |
||
1730 | |||
1731 | cursor.close() |
||
1732 | cnx.disconnect() |
||
1733 | |||
1734 | resp.status = falcon.HTTP_204 |
||
1735 | |||
1736 | |||
1737 | View Code Duplication | class CombinedEquipmentVirtualMeterCollection: |
|
1738 | @staticmethod |
||
1739 | def __init__(): |
||
1740 | pass |
||
1741 | |||
1742 | @staticmethod |
||
1743 | def on_options(req, resp, id_): |
||
1744 | resp.status = falcon.HTTP_200 |
||
1745 | |||
1746 | @staticmethod |
||
1747 | def on_get(req, resp, id_): |
||
1748 | if not id_.isdigit() or int(id_) <= 0: |
||
1749 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1750 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1751 | |||
1752 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1753 | cursor = cnx.cursor(dictionary=True) |
||
1754 | |||
1755 | cursor.execute(" SELECT name " |
||
1756 | " FROM tbl_combined_equipments " |
||
1757 | " WHERE id = %s ", (id_,)) |
||
1758 | if cursor.fetchone() is None: |
||
1759 | cursor.close() |
||
1760 | cnx.disconnect() |
||
1761 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1762 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1763 | |||
1764 | query = (" SELECT id, name, uuid " |
||
1765 | " FROM tbl_energy_categories ") |
||
1766 | cursor.execute(query) |
||
1767 | rows_energy_categories = cursor.fetchall() |
||
1768 | |||
1769 | energy_category_dict = dict() |
||
1770 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
1771 | for row in rows_energy_categories: |
||
1772 | energy_category_dict[row['id']] = {"id": row['id'], |
||
1773 | "name": row['name'], |
||
1774 | "uuid": row['uuid']} |
||
1775 | |||
1776 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
1777 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
1778 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
1779 | " ORDER BY m.id ") |
||
1780 | cursor.execute(query, (id_,)) |
||
1781 | rows = cursor.fetchall() |
||
1782 | |||
1783 | result = list() |
||
1784 | if rows is not None and len(rows) > 0: |
||
1785 | for row in rows: |
||
1786 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
1787 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
1788 | "energy_category": energy_category, |
||
1789 | "is_output": bool(row['is_output'])} |
||
1790 | result.append(meta_result) |
||
1791 | |||
1792 | resp.body = json.dumps(result) |
||
1793 | |||
1794 | @staticmethod |
||
1795 | def on_post(req, resp, id_): |
||
1796 | """Handles POST requests""" |
||
1797 | try: |
||
1798 | raw_json = req.stream.read().decode('utf-8') |
||
1799 | except Exception as ex: |
||
1800 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
1801 | |||
1802 | if not id_.isdigit() or int(id_) <= 0: |
||
1803 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1804 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1805 | |||
1806 | new_values = json.loads(raw_json) |
||
1807 | |||
1808 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
1809 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
1810 | new_values['data']['virtual_meter_id'] <= 0: |
||
1811 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1812 | description='API.INVALID_VIRTUAL_METER_ID') |
||
1813 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
1814 | |||
1815 | if 'is_output' not in new_values['data'].keys() or \ |
||
1816 | not isinstance(new_values['data']['is_output'], bool): |
||
1817 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1818 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
1819 | is_output = new_values['data']['is_output'] |
||
1820 | |||
1821 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1822 | cursor = cnx.cursor() |
||
1823 | |||
1824 | cursor.execute(" SELECT name " |
||
1825 | " from tbl_combined_equipments " |
||
1826 | " WHERE id = %s ", (id_,)) |
||
1827 | if cursor.fetchone() is None: |
||
1828 | cursor.close() |
||
1829 | cnx.disconnect() |
||
1830 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1831 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1832 | |||
1833 | cursor.execute(" SELECT name " |
||
1834 | " FROM tbl_virtual_meters " |
||
1835 | " WHERE id = %s ", (virtual_meter_id,)) |
||
1836 | if cursor.fetchone() is None: |
||
1837 | cursor.close() |
||
1838 | cnx.disconnect() |
||
1839 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1840 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
1841 | |||
1842 | query = (" SELECT id " |
||
1843 | " FROM tbl_combined_equipments_virtual_meters " |
||
1844 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
1845 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
1846 | if cursor.fetchone() is not None: |
||
1847 | cursor.close() |
||
1848 | cnx.disconnect() |
||
1849 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
1850 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTED') |
||
1851 | |||
1852 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
1853 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
1854 | " VALUES (%s, %s, %s) ") |
||
1855 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
||
1856 | new_id = cursor.lastrowid |
||
1857 | cnx.commit() |
||
1858 | cursor.close() |
||
1859 | cnx.disconnect() |
||
1860 | |||
1861 | resp.status = falcon.HTTP_201 |
||
1862 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
1863 | |||
1864 | |||
1865 | View Code Duplication | class CombinedEquipmentVirtualMeterItem: |
|
1866 | @staticmethod |
||
1867 | def __init__(): |
||
1868 | pass |
||
1869 | |||
1870 | @staticmethod |
||
1871 | def on_options(req, resp, id_, mid): |
||
1872 | resp.status = falcon.HTTP_200 |
||
1873 | |||
1874 | @staticmethod |
||
1875 | def on_delete(req, resp, id_, mid): |
||
1876 | if not id_.isdigit() or int(id_) <= 0: |
||
1877 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1878 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
1879 | |||
1880 | if not mid.isdigit() or int(mid) <= 0: |
||
1881 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1882 | description='API.INVALID_VIRTUAL_METER_ID') |
||
1883 | |||
1884 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1885 | cursor = cnx.cursor() |
||
1886 | |||
1887 | cursor.execute(" SELECT name " |
||
1888 | " FROM tbl_combined_equipments " |
||
1889 | " WHERE id = %s ", (id_,)) |
||
1890 | if cursor.fetchone() is None: |
||
1891 | cursor.close() |
||
1892 | cnx.disconnect() |
||
1893 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1894 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
1895 | |||
1896 | cursor.execute(" SELECT name " |
||
1897 | " FROM tbl_virtual_meters " |
||
1898 | " WHERE id = %s ", (mid,)) |
||
1899 | if cursor.fetchone() is None: |
||
1900 | cursor.close() |
||
1901 | cnx.disconnect() |
||
1902 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1903 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
1904 | |||
1905 | cursor.execute(" SELECT id " |
||
1906 | " FROM tbl_combined_equipments_virtual_meters " |
||
1907 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
1908 | if cursor.fetchone() is None: |
||
1909 | cursor.close() |
||
1910 | cnx.disconnect() |
||
1911 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
1912 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
1913 | |||
1914 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters " |
||
1915 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
1916 | cnx.commit() |
||
1917 | |||
1918 | cursor.close() |
||
1919 | cnx.disconnect() |
||
1920 | |||
1921 | resp.status = falcon.HTTP_204 |
||
1922 |