Total Complexity | 345 |
Total Lines | 1735 |
Duplicated Lines | 13.14 % |
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.energyflowdiagram often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import uuid |
||
2 | from datetime import datetime, timedelta |
||
3 | import falcon |
||
4 | import mysql.connector |
||
5 | import simplejson as json |
||
6 | from core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
7 | import config |
||
8 | |||
9 | |||
10 | class EnergyFlowDiagramCollection: |
||
11 | def __init__(self): |
||
12 | """"Initializes EnergyFlowDiagramCollection""" |
||
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_meters ") |
||
33 | cursor.execute(query) |
||
34 | rows_meters = cursor.fetchall() |
||
35 | |||
36 | meter_dict = dict() |
||
37 | if rows_meters is not None and len(rows_meters) > 0: |
||
38 | for row in rows_meters: |
||
39 | meter_dict[row[2]] = {"type": 'meter', |
||
40 | "id": row[0], |
||
41 | "name": row[1], |
||
42 | "uuid": row[2]} |
||
43 | |||
44 | query = (" SELECT id, name, uuid " |
||
45 | " FROM tbl_offline_meters ") |
||
46 | cursor.execute(query) |
||
47 | rows_offline_meters = cursor.fetchall() |
||
48 | |||
49 | offline_meter_dict = dict() |
||
50 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
51 | for row in rows_offline_meters: |
||
52 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
53 | "id": row[0], |
||
54 | "name": row[1], |
||
55 | "uuid": row[2]} |
||
56 | |||
57 | query = (" SELECT id, name, uuid " |
||
58 | " FROM tbl_virtual_meters ") |
||
59 | cursor.execute(query) |
||
60 | rows_virtual_meters = cursor.fetchall() |
||
61 | |||
62 | virtual_meter_dict = dict() |
||
63 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
64 | for row in rows_virtual_meters: |
||
65 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
66 | "id": row[0], |
||
67 | "name": row[1], |
||
68 | "uuid": row[2]} |
||
69 | |||
70 | query = (" SELECT id, energy_flow_diagram_id, name " |
||
71 | " FROM tbl_energy_flow_diagrams_nodes") |
||
72 | cursor.execute(query) |
||
73 | rows_nodes = cursor.fetchall() |
||
74 | |||
75 | node_dict = dict() |
||
76 | node_list_dict = dict() |
||
77 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
78 | for row in rows_nodes: |
||
79 | node_dict[row[0]] = row[2] |
||
80 | if node_list_dict.get(row[1]) is None: |
||
81 | node_list_dict[row[1]] = list() |
||
82 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
||
83 | |||
84 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
85 | " FROM tbl_energy_flow_diagrams_links") |
||
86 | cursor.execute(query) |
||
87 | rows_links = cursor.fetchall() |
||
88 | |||
89 | link_list_dict = dict() |
||
90 | if rows_links is not None and len(rows_links) > 0: |
||
91 | for row in rows_links: |
||
92 | # find meter by uuid |
||
93 | meter = meter_dict.get(row[4], None) |
||
94 | if meter is None: |
||
95 | meter = virtual_meter_dict.get(row[4], None) |
||
96 | if meter is None: |
||
97 | meter = offline_meter_dict.get(row[4], None) |
||
98 | |||
99 | if link_list_dict.get(row[1]) is None: |
||
100 | link_list_dict[row[1]] = list() |
||
101 | link_list_dict[row[1]].append({"id": row[0], |
||
102 | "source_node": { |
||
103 | "id": row[2], |
||
104 | "name": node_dict.get(row[2])}, |
||
105 | "target_node": { |
||
106 | "id": row[3], |
||
107 | "name": node_dict.get(row[3])}, |
||
108 | "meter": meter}) |
||
109 | |||
110 | query = (" SELECT id, name, uuid " |
||
111 | " FROM tbl_energy_flow_diagrams " |
||
112 | " ORDER BY id ") |
||
113 | cursor.execute(query) |
||
114 | rows_energy_flow_diagrams = cursor.fetchall() |
||
115 | |||
116 | result = list() |
||
117 | if rows_energy_flow_diagrams is not None and len(rows_energy_flow_diagrams) > 0: |
||
118 | for row in rows_energy_flow_diagrams: |
||
119 | |||
120 | meta_result = {"id": row[0], |
||
121 | "name": row[1], |
||
122 | "uuid": row[2], |
||
123 | "nodes": node_list_dict.get(row[0], None), |
||
124 | "links": link_list_dict.get(row[0], None), } |
||
125 | result.append(meta_result) |
||
126 | |||
127 | cursor.close() |
||
128 | cnx.close() |
||
129 | resp.text = json.dumps(result) |
||
130 | |||
131 | @staticmethod |
||
132 | @user_logger |
||
133 | def on_post(req, resp): |
||
134 | """Handles POST requests""" |
||
135 | admin_control(req) |
||
136 | try: |
||
137 | raw_json = req.stream.read().decode('utf-8') |
||
138 | except Exception as ex: |
||
139 | print(str(ex)) |
||
140 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
141 | title='API.BAD_REQUEST', |
||
142 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
143 | |||
144 | new_values = json.loads(raw_json) |
||
145 | |||
146 | if 'name' not in new_values['data'].keys() or \ |
||
147 | not isinstance(new_values['data']['name'], str) or \ |
||
148 | len(str.strip(new_values['data']['name'])) == 0: |
||
149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
150 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
||
151 | name = str.strip(new_values['data']['name']) |
||
152 | |||
153 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
154 | cursor = cnx.cursor() |
||
155 | |||
156 | cursor.execute(" SELECT name " |
||
157 | " FROM tbl_energy_flow_diagrams " |
||
158 | " WHERE name = %s ", (name,)) |
||
159 | if cursor.fetchone() is not None: |
||
160 | cursor.close() |
||
161 | cnx.close() |
||
162 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
163 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
164 | |||
165 | add_values = (" INSERT INTO tbl_energy_flow_diagrams " |
||
166 | " (name, uuid) " |
||
167 | " VALUES (%s, %s) ") |
||
168 | cursor.execute(add_values, (name, |
||
169 | str(uuid.uuid4()))) |
||
170 | new_id = cursor.lastrowid |
||
171 | cnx.commit() |
||
172 | cursor.close() |
||
173 | cnx.close() |
||
174 | |||
175 | resp.status = falcon.HTTP_201 |
||
176 | resp.location = '/energyflowdiagrams/' + str(new_id) |
||
177 | |||
178 | |||
179 | class EnergyFlowDiagramItem: |
||
180 | def __init__(self): |
||
181 | """"Initializes EnergyFlowDiagramItem""" |
||
182 | pass |
||
183 | |||
184 | @staticmethod |
||
185 | def on_options(req, resp, id_): |
||
186 | _=req |
||
187 | resp.status = falcon.HTTP_200 |
||
188 | _=id_ |
||
189 | View Code Duplication | @staticmethod |
|
|
|||
190 | def on_get(req, resp, id_): |
||
191 | if 'API-KEY' not in req.headers or \ |
||
192 | not isinstance(req.headers['API-KEY'], str) or \ |
||
193 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
194 | access_control(req) |
||
195 | else: |
||
196 | api_key_control(req) |
||
197 | if not id_.isdigit() or int(id_) <= 0: |
||
198 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
199 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
200 | |||
201 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
202 | cursor = cnx.cursor() |
||
203 | |||
204 | query = (" SELECT id, name, uuid " |
||
205 | " FROM tbl_meters ") |
||
206 | cursor.execute(query) |
||
207 | rows_meters = cursor.fetchall() |
||
208 | |||
209 | meter_dict = dict() |
||
210 | if rows_meters is not None and len(rows_meters) > 0: |
||
211 | for row in rows_meters: |
||
212 | meter_dict[row[2]] = {"type": 'meter', |
||
213 | "id": row[0], |
||
214 | "name": row[1], |
||
215 | "uuid": row[2]} |
||
216 | |||
217 | query = (" SELECT id, name, uuid " |
||
218 | " FROM tbl_offline_meters ") |
||
219 | cursor.execute(query) |
||
220 | rows_offline_meters = cursor.fetchall() |
||
221 | |||
222 | offline_meter_dict = dict() |
||
223 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
224 | for row in rows_offline_meters: |
||
225 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
226 | "id": row[0], |
||
227 | "name": row[1], |
||
228 | "uuid": row[2]} |
||
229 | |||
230 | query = (" SELECT id, name, uuid " |
||
231 | " FROM tbl_virtual_meters ") |
||
232 | cursor.execute(query) |
||
233 | rows_virtual_meters = cursor.fetchall() |
||
234 | |||
235 | virtual_meter_dict = dict() |
||
236 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
237 | for row in rows_virtual_meters: |
||
238 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
239 | "id": row[0], |
||
240 | "name": row[1], |
||
241 | "uuid": row[2]} |
||
242 | |||
243 | query = (" SELECT id, energy_flow_diagram_id, name " |
||
244 | " FROM tbl_energy_flow_diagrams_nodes") |
||
245 | cursor.execute(query) |
||
246 | rows_nodes = cursor.fetchall() |
||
247 | |||
248 | node_dict = dict() |
||
249 | node_list_dict = dict() |
||
250 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
251 | for row in rows_nodes: |
||
252 | node_dict[row[0]] = row[2] |
||
253 | if node_list_dict.get(row[1]) is None: |
||
254 | node_list_dict[row[1]] = list() |
||
255 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
||
256 | |||
257 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
258 | " FROM tbl_energy_flow_diagrams_links") |
||
259 | cursor.execute(query) |
||
260 | rows_links = cursor.fetchall() |
||
261 | |||
262 | link_list_dict = dict() |
||
263 | if rows_links is not None and len(rows_links) > 0: |
||
264 | for row in rows_links: |
||
265 | # find meter by uuid |
||
266 | meter = meter_dict.get(row[4], None) |
||
267 | if meter is None: |
||
268 | meter = virtual_meter_dict.get(row[4], None) |
||
269 | if meter is None: |
||
270 | meter = offline_meter_dict.get(row[4], None) |
||
271 | |||
272 | if link_list_dict.get(row[1]) is None: |
||
273 | link_list_dict[row[1]] = list() |
||
274 | link_list_dict[row[1]].append({"id": row[0], |
||
275 | "source_node": { |
||
276 | "id": row[2], |
||
277 | "name": node_dict.get(row[2])}, |
||
278 | "target_node": { |
||
279 | "id": row[3], |
||
280 | "name": node_dict.get(row[3])}, |
||
281 | "meter": meter}) |
||
282 | |||
283 | query = (" SELECT id, name, uuid " |
||
284 | " FROM tbl_energy_flow_diagrams " |
||
285 | " WHERE id = %s ") |
||
286 | cursor.execute(query, (id_,)) |
||
287 | row = cursor.fetchone() |
||
288 | cursor.close() |
||
289 | cnx.close() |
||
290 | |||
291 | if row is None: |
||
292 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
293 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
294 | else: |
||
295 | meta_result = {"id": row[0], |
||
296 | "name": row[1], |
||
297 | "uuid": row[2], |
||
298 | "nodes": node_list_dict.get(row[0], None), |
||
299 | "links": link_list_dict.get(row[0], None), |
||
300 | } |
||
301 | |||
302 | resp.text = json.dumps(meta_result) |
||
303 | |||
304 | @staticmethod |
||
305 | @user_logger |
||
306 | def on_delete(req, resp, id_): |
||
307 | admin_control(req) |
||
308 | if not id_.isdigit() or int(id_) <= 0: |
||
309 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
310 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
311 | |||
312 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
313 | cursor = cnx.cursor() |
||
314 | |||
315 | # delete all associated nodes |
||
316 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_nodes" |
||
317 | " WHERE energy_flow_diagram_id = %s ", (id_,)) |
||
318 | cnx.commit() |
||
319 | |||
320 | # delete all associated links |
||
321 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_links" |
||
322 | " WHERE energy_flow_diagram_id = %s ", (id_,)) |
||
323 | cnx.commit() |
||
324 | |||
325 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams" |
||
326 | " WHERE id = %s ", (id_,)) |
||
327 | cnx.commit() |
||
328 | |||
329 | cursor.close() |
||
330 | cnx.close() |
||
331 | |||
332 | resp.status = falcon.HTTP_204 |
||
333 | |||
334 | @staticmethod |
||
335 | @user_logger |
||
336 | def on_put(req, resp, id_): |
||
337 | """Handles PUT requests""" |
||
338 | admin_control(req) |
||
339 | if not id_.isdigit() or int(id_) <= 0: |
||
340 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
341 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
342 | try: |
||
343 | raw_json = req.stream.read().decode('utf-8') |
||
344 | except Exception as ex: |
||
345 | print(str(ex)) |
||
346 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
347 | title='API.BAD_REQUEST', |
||
348 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
349 | |||
350 | new_values = json.loads(raw_json) |
||
351 | |||
352 | if 'name' not in new_values['data'].keys() or \ |
||
353 | not isinstance(new_values['data']['name'], str) or \ |
||
354 | len(str.strip(new_values['data']['name'])) == 0: |
||
355 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
356 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
||
357 | name = str.strip(new_values['data']['name']) |
||
358 | |||
359 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
360 | cursor = cnx.cursor() |
||
361 | |||
362 | cursor.execute(" SELECT name " |
||
363 | " FROM tbl_energy_flow_diagrams " |
||
364 | " WHERE id = %s ", (id_,)) |
||
365 | if cursor.fetchone() is None: |
||
366 | cursor.close() |
||
367 | cnx.close() |
||
368 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
369 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
370 | |||
371 | cursor.execute(" SELECT name " |
||
372 | " FROM tbl_energy_flow_diagrams " |
||
373 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
374 | if cursor.fetchone() is not None: |
||
375 | cursor.close() |
||
376 | cnx.close() |
||
377 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
378 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
379 | |||
380 | update_row = (" UPDATE tbl_energy_flow_diagrams " |
||
381 | " SET name = %s " |
||
382 | " WHERE id = %s ") |
||
383 | cursor.execute(update_row, (name, |
||
384 | id_)) |
||
385 | cnx.commit() |
||
386 | |||
387 | cursor.close() |
||
388 | cnx.close() |
||
389 | |||
390 | resp.status = falcon.HTTP_200 |
||
391 | |||
392 | |||
393 | class EnergyFlowDiagramLinkCollection: |
||
394 | def __init__(self): |
||
395 | """"Initializes EnergyFlowDiagramLinkCollection""" |
||
396 | pass |
||
397 | |||
398 | @staticmethod |
||
399 | def on_options(req, resp, id_): |
||
400 | _=req |
||
401 | resp.status = falcon.HTTP_200 |
||
402 | _=id_ |
||
403 | @staticmethod |
||
404 | def on_get(req, resp, id_): |
||
405 | if 'API-KEY' not in req.headers or \ |
||
406 | not isinstance(req.headers['API-KEY'], str) or \ |
||
407 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
408 | access_control(req) |
||
409 | else: |
||
410 | api_key_control(req) |
||
411 | if not id_.isdigit() or int(id_) <= 0: |
||
412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
413 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
414 | |||
415 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
416 | cursor = cnx.cursor() |
||
417 | |||
418 | query = (" SELECT id, name " |
||
419 | " FROM tbl_energy_flow_diagrams_nodes ") |
||
420 | cursor.execute(query) |
||
421 | rows_nodes = cursor.fetchall() |
||
422 | |||
423 | node_dict = dict() |
||
424 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
425 | for row in rows_nodes: |
||
426 | node_dict[row[0]] = {"id": row[0], |
||
427 | "name": row[1]} |
||
428 | |||
429 | query = (" SELECT id, name, uuid " |
||
430 | " FROM tbl_meters ") |
||
431 | cursor.execute(query) |
||
432 | rows_meters = cursor.fetchall() |
||
433 | |||
434 | meter_dict = dict() |
||
435 | if rows_meters is not None and len(rows_meters) > 0: |
||
436 | for row in rows_meters: |
||
437 | meter_dict[row[2]] = {"type": 'meter', |
||
438 | "id": row[0], |
||
439 | "name": row[1], |
||
440 | "uuid": row[2]} |
||
441 | |||
442 | query = (" SELECT id, name, uuid " |
||
443 | " FROM tbl_offline_meters ") |
||
444 | cursor.execute(query) |
||
445 | rows_offline_meters = cursor.fetchall() |
||
446 | |||
447 | offline_meter_dict = dict() |
||
448 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
449 | for row in rows_offline_meters: |
||
450 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
451 | "id": row[0], |
||
452 | "name": row[1], |
||
453 | "uuid": row[2]} |
||
454 | |||
455 | query = (" SELECT id, name, uuid " |
||
456 | " FROM tbl_virtual_meters ") |
||
457 | cursor.execute(query) |
||
458 | rows_virtual_meters = cursor.fetchall() |
||
459 | |||
460 | virtual_meter_dict = dict() |
||
461 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
462 | for row in rows_virtual_meters: |
||
463 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
464 | "id": row[0], |
||
465 | "name": row[1], |
||
466 | "uuid": row[2]} |
||
467 | |||
468 | cursor.execute(" SELECT name " |
||
469 | " FROM tbl_energy_flow_diagrams " |
||
470 | " WHERE id = %s ", (id_,)) |
||
471 | if cursor.fetchone() is None: |
||
472 | cursor.close() |
||
473 | cnx.close() |
||
474 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
475 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
476 | |||
477 | query = (" SELECT id, source_node_id, target_node_id, meter_uuid " |
||
478 | " FROM tbl_energy_flow_diagrams_links " |
||
479 | " WHERE energy_flow_diagram_id = %s " |
||
480 | " ORDER BY id ") |
||
481 | cursor.execute(query, (id_, )) |
||
482 | rows_links = cursor.fetchall() |
||
483 | |||
484 | result = list() |
||
485 | if rows_links is not None and len(rows_links) > 0: |
||
486 | for row in rows_links: |
||
487 | source_node = node_dict.get(row[1], None) |
||
488 | target_node = node_dict.get(row[2], None) |
||
489 | # find meter by uuid |
||
490 | meter = meter_dict.get(row[3], None) |
||
491 | if meter is None: |
||
492 | meter = virtual_meter_dict.get(row[3], None) |
||
493 | if meter is None: |
||
494 | meter = offline_meter_dict.get(row[3], None) |
||
495 | |||
496 | meta_result = {"id": row[0], |
||
497 | "source_node": source_node, |
||
498 | "target_node": target_node, |
||
499 | "meter": meter} |
||
500 | result.append(meta_result) |
||
501 | |||
502 | cursor.close() |
||
503 | cnx.close() |
||
504 | resp.text = json.dumps(result) |
||
505 | |||
506 | @staticmethod |
||
507 | @user_logger |
||
508 | def on_post(req, resp, id_): |
||
509 | """Handles POST requests""" |
||
510 | admin_control(req) |
||
511 | if not id_.isdigit() or int(id_) <= 0: |
||
512 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
513 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
514 | try: |
||
515 | raw_json = req.stream.read().decode('utf-8') |
||
516 | except Exception as ex: |
||
517 | print(str(ex)) |
||
518 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
519 | title='API.BAD_REQUEST', |
||
520 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
521 | |||
522 | new_values = json.loads(raw_json) |
||
523 | |||
524 | source_node_id = None |
||
525 | if 'source_node_id' in new_values['data'].keys(): |
||
526 | if new_values['data']['source_node_id'] is not None and \ |
||
527 | new_values['data']['source_node_id'] <= 0: |
||
528 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
529 | description='API.INVALID_SOURCE_NODE_ID') |
||
530 | source_node_id = new_values['data']['source_node_id'] |
||
531 | |||
532 | target_node_id = None |
||
533 | if 'target_node_id' in new_values['data'].keys(): |
||
534 | if new_values['data']['target_node_id'] is not None and \ |
||
535 | new_values['data']['target_node_id'] <= 0: |
||
536 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
537 | description='API.INVALID_TARGET_NODE_ID') |
||
538 | target_node_id = new_values['data']['target_node_id'] |
||
539 | |||
540 | meter_uuid = None |
||
541 | if 'meter_uuid' in new_values['data'].keys(): |
||
542 | if new_values['data']['meter_uuid'] is not None and \ |
||
543 | isinstance(new_values['data']['meter_uuid'], str) and \ |
||
544 | len(str.strip(new_values['data']['meter_uuid'])) > 0: |
||
545 | meter_uuid = str.strip(new_values['data']['meter_uuid']) |
||
546 | |||
547 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
548 | cursor = cnx.cursor() |
||
549 | |||
550 | cursor.execute(" SELECT name " |
||
551 | " FROM tbl_energy_flow_diagrams " |
||
552 | " WHERE id = %s ", (id_,)) |
||
553 | if cursor.fetchone() is None: |
||
554 | cursor.close() |
||
555 | cnx.close() |
||
556 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
557 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
558 | |||
559 | cursor.execute(" SELECT id " |
||
560 | " FROM tbl_energy_flow_diagrams_links " |
||
561 | " WHERE energy_flow_diagram_id = %s AND " |
||
562 | " source_node_id = %s AND target_node_id = %s ", |
||
563 | (id_, source_node_id, target_node_id,)) |
||
564 | row = cursor.fetchone() |
||
565 | if row is not None: |
||
566 | cursor.close() |
||
567 | cnx.close() |
||
568 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
569 | title='API.NOT_FOUND', |
||
570 | description='API.ENERGY_FLOW_DIAGRAM_LINK_IS_ALREADY_IN_USE') |
||
571 | |||
572 | query = (" SELECT id, name " |
||
573 | " FROM tbl_energy_flow_diagrams_nodes " |
||
574 | " WHERE id = %s ") |
||
575 | cursor.execute(query, (source_node_id,)) |
||
576 | if cursor.fetchone() is None: |
||
577 | cursor.close() |
||
578 | cnx.close() |
||
579 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
580 | description='API.SOURCE_NODE_NOT_FOUND') |
||
581 | |||
582 | query = (" SELECT id, name " |
||
583 | " FROM tbl_energy_flow_diagrams_nodes " |
||
584 | " WHERE id = %s ") |
||
585 | cursor.execute(query, (target_node_id,)) |
||
586 | if cursor.fetchone() is None: |
||
587 | cursor.close() |
||
588 | cnx.close() |
||
589 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
590 | description='API.TARGET_NODE_NOT_FOUND') |
||
591 | |||
592 | query = (" SELECT id, name, uuid " |
||
593 | " FROM tbl_meters ") |
||
594 | cursor.execute(query) |
||
595 | rows_meters = cursor.fetchall() |
||
596 | |||
597 | meter_dict = dict() |
||
598 | if rows_meters is not None and len(rows_meters) > 0: |
||
599 | for row in rows_meters: |
||
600 | meter_dict[row[2]] = {"type": 'meter', |
||
601 | "id": row[0], |
||
602 | "name": row[1], |
||
603 | "uuid": row[2]} |
||
604 | |||
605 | query = (" SELECT id, name, uuid " |
||
606 | " FROM tbl_offline_meters ") |
||
607 | cursor.execute(query) |
||
608 | rows_offline_meters = cursor.fetchall() |
||
609 | |||
610 | offline_meter_dict = dict() |
||
611 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
612 | for row in rows_offline_meters: |
||
613 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
614 | "id": row[0], |
||
615 | "name": row[1], |
||
616 | "uuid": row[2]} |
||
617 | |||
618 | query = (" SELECT id, name, uuid " |
||
619 | " FROM tbl_virtual_meters ") |
||
620 | cursor.execute(query) |
||
621 | rows_virtual_meters = cursor.fetchall() |
||
622 | |||
623 | virtual_meter_dict = dict() |
||
624 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
625 | for row in rows_virtual_meters: |
||
626 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
627 | "id": row[0], |
||
628 | "name": row[1], |
||
629 | "uuid": row[2]} |
||
630 | |||
631 | # validate meter uuid |
||
632 | if meter_dict.get(meter_uuid) is None and \ |
||
633 | virtual_meter_dict.get(meter_uuid) is None and \ |
||
634 | offline_meter_dict.get(meter_uuid) is None: |
||
635 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
636 | description='API.INVALID_METER_UUID') |
||
637 | |||
638 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_links " |
||
639 | " (energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid) " |
||
640 | " VALUES (%s, %s, %s, %s) ") |
||
641 | cursor.execute(add_values, (id_, |
||
642 | source_node_id, |
||
643 | target_node_id, |
||
644 | meter_uuid)) |
||
645 | new_id = cursor.lastrowid |
||
646 | cnx.commit() |
||
647 | cursor.close() |
||
648 | cnx.close() |
||
649 | |||
650 | resp.status = falcon.HTTP_201 |
||
651 | resp.location = '/energyflowdiagrams/' + str(id_) + 'links/' + str(new_id) |
||
652 | |||
653 | |||
654 | class EnergyFlowDiagramLinkItem: |
||
655 | def __init__(self): |
||
656 | """"Initializes EnergyFlowDiagramLinkItem""" |
||
657 | pass |
||
658 | |||
659 | @staticmethod |
||
660 | def on_options(req, resp, id_, lid): |
||
661 | _=req |
||
662 | resp.status = falcon.HTTP_200 |
||
663 | _=id_ |
||
664 | @staticmethod |
||
665 | def on_get(req, resp, id_, lid): |
||
666 | access_control(req) |
||
667 | if not id_.isdigit() or int(id_) <= 0: |
||
668 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
669 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
670 | |||
671 | if not lid.isdigit() or int(lid) <= 0: |
||
672 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
673 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
674 | |||
675 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
676 | cursor = cnx.cursor() |
||
677 | |||
678 | query = (" SELECT id, name " |
||
679 | " FROM tbl_energy_flow_diagrams_nodes ") |
||
680 | cursor.execute(query) |
||
681 | rows_nodes = cursor.fetchall() |
||
682 | |||
683 | node_dict = dict() |
||
684 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
685 | for row in rows_nodes: |
||
686 | node_dict[row[0]] = {"id": row[0], |
||
687 | "name": row[1]} |
||
688 | |||
689 | query = (" SELECT id, name, uuid " |
||
690 | " FROM tbl_meters ") |
||
691 | cursor.execute(query) |
||
692 | rows_meters = cursor.fetchall() |
||
693 | |||
694 | meter_dict = dict() |
||
695 | if rows_meters is not None and len(rows_meters) > 0: |
||
696 | for row in rows_meters: |
||
697 | meter_dict[row[2]] = {"type": 'meter', |
||
698 | "id": row[0], |
||
699 | "name": row[1], |
||
700 | "uuid": row[2]} |
||
701 | |||
702 | query = (" SELECT id, name, uuid " |
||
703 | " FROM tbl_offline_meters ") |
||
704 | cursor.execute(query) |
||
705 | rows_offline_meters = cursor.fetchall() |
||
706 | |||
707 | offline_meter_dict = dict() |
||
708 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
709 | for row in rows_offline_meters: |
||
710 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
711 | "id": row[0], |
||
712 | "name": row[1], |
||
713 | "uuid": row[2]} |
||
714 | |||
715 | query = (" SELECT id, name, uuid " |
||
716 | " FROM tbl_virtual_meters ") |
||
717 | cursor.execute(query) |
||
718 | rows_virtual_meters = cursor.fetchall() |
||
719 | |||
720 | virtual_meter_dict = dict() |
||
721 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
722 | for row in rows_virtual_meters: |
||
723 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
724 | "id": row[0], |
||
725 | "name": row[1], |
||
726 | "uuid": row[2]} |
||
727 | |||
728 | query = (" SELECT id, source_node_id, target_node_id, meter_uuid " |
||
729 | " FROM tbl_energy_flow_diagrams_links " |
||
730 | " WHERE energy_flow_diagram_id = %s AND id = %s ") |
||
731 | cursor.execute(query, (id_, lid)) |
||
732 | row = cursor.fetchone() |
||
733 | cursor.close() |
||
734 | cnx.close() |
||
735 | |||
736 | if row is None: |
||
737 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
738 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
739 | else: |
||
740 | source_node = node_dict.get(row[1], None) |
||
741 | target_node = node_dict.get(row[2], None) |
||
742 | # find meter by uuid |
||
743 | meter = meter_dict.get(row[3], None) |
||
744 | if meter is None: |
||
745 | meter = virtual_meter_dict.get(row[3], None) |
||
746 | if meter is None: |
||
747 | meter = offline_meter_dict.get(row[3], None) |
||
748 | |||
749 | meta_result = {"id": row[0], |
||
750 | "source_node": source_node, |
||
751 | "target_node": target_node, |
||
752 | "meter": meter} |
||
753 | resp.text = json.dumps(meta_result) |
||
754 | |||
755 | @staticmethod |
||
756 | @user_logger |
||
757 | def on_delete(req, resp, id_, lid): |
||
758 | admin_control(req) |
||
759 | if not id_.isdigit() or int(id_) <= 0: |
||
760 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
761 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
762 | |||
763 | if not lid.isdigit() or int(lid) <= 0: |
||
764 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
765 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
766 | |||
767 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
768 | cursor = cnx.cursor() |
||
769 | |||
770 | cursor.execute(" SELECT name " |
||
771 | " FROM tbl_energy_flow_diagrams " |
||
772 | " WHERE id = %s ", |
||
773 | (id_,)) |
||
774 | row = cursor.fetchone() |
||
775 | if row is None: |
||
776 | cursor.close() |
||
777 | cnx.close() |
||
778 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
779 | title='API.NOT_FOUND', |
||
780 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
781 | |||
782 | cursor.execute(" SELECT id " |
||
783 | " FROM tbl_energy_flow_diagrams_links " |
||
784 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
785 | (id_, lid,)) |
||
786 | row = cursor.fetchone() |
||
787 | if row is None: |
||
788 | cursor.close() |
||
789 | cnx.close() |
||
790 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
791 | title='API.NOT_FOUND', |
||
792 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
793 | |||
794 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_links " |
||
795 | " WHERE id = %s ", (lid, )) |
||
796 | cnx.commit() |
||
797 | |||
798 | cursor.close() |
||
799 | cnx.close() |
||
800 | |||
801 | resp.status = falcon.HTTP_204 |
||
802 | |||
803 | @staticmethod |
||
804 | @user_logger |
||
805 | def on_put(req, resp, id_, lid): |
||
806 | """Handles PUT requests""" |
||
807 | admin_control(req) |
||
808 | if not id_.isdigit() or int(id_) <= 0: |
||
809 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
810 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
811 | |||
812 | if not lid.isdigit() or int(lid) <= 0: |
||
813 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
814 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
815 | |||
816 | try: |
||
817 | raw_json = req.stream.read().decode('utf-8') |
||
818 | except Exception as ex: |
||
819 | print(str(ex)) |
||
820 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
821 | title='API.BAD_REQUEST', |
||
822 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
823 | |||
824 | new_values = json.loads(raw_json) |
||
825 | |||
826 | source_node_id = None |
||
827 | if 'source_node_id' in new_values['data'].keys(): |
||
828 | if new_values['data']['source_node_id'] is not None and \ |
||
829 | new_values['data']['source_node_id'] <= 0: |
||
830 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
831 | description='API.INVALID_SOURCE_NODE_ID') |
||
832 | source_node_id = new_values['data']['source_node_id'] |
||
833 | |||
834 | target_node_id = None |
||
835 | if 'target_node_id' in new_values['data'].keys(): |
||
836 | if new_values['data']['target_node_id'] is not None and \ |
||
837 | new_values['data']['target_node_id'] <= 0: |
||
838 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
839 | description='API.INVALID_TARGET_NODE_ID') |
||
840 | target_node_id = new_values['data']['target_node_id'] |
||
841 | |||
842 | meter_uuid = None |
||
843 | if 'meter_uuid' in new_values['data'].keys(): |
||
844 | if new_values['data']['meter_uuid'] is not None and \ |
||
845 | isinstance(new_values['data']['meter_uuid'], str) and \ |
||
846 | len(str.strip(new_values['data']['meter_uuid'])) > 0: |
||
847 | meter_uuid = str.strip(new_values['data']['meter_uuid']) |
||
848 | |||
849 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
850 | cursor = cnx.cursor() |
||
851 | |||
852 | cursor.execute(" SELECT name " |
||
853 | " FROM tbl_energy_flow_diagrams " |
||
854 | " WHERE id = %s ", (id_,)) |
||
855 | if cursor.fetchone() is None: |
||
856 | cursor.close() |
||
857 | cnx.close() |
||
858 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
859 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
860 | |||
861 | cursor.execute(" SELECT id " |
||
862 | " FROM tbl_energy_flow_diagrams_links " |
||
863 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
864 | (id_, lid,)) |
||
865 | row = cursor.fetchone() |
||
866 | if row is None: |
||
867 | cursor.close() |
||
868 | cnx.close() |
||
869 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
870 | title='API.NOT_FOUND', |
||
871 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
872 | |||
873 | cursor.execute(" SELECT id " |
||
874 | " FROM tbl_energy_flow_diagrams_links " |
||
875 | " WHERE energy_flow_diagram_id = %s AND id != %s " |
||
876 | " AND source_node_id = %s AND target_node_id = %s ", |
||
877 | (id_, lid, source_node_id, target_node_id,)) |
||
878 | row = cursor.fetchone() |
||
879 | if row is not None: |
||
880 | cursor.close() |
||
881 | cnx.close() |
||
882 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
883 | title='API.NOT_FOUND', |
||
884 | description='API.ENERGY_FLOW_DIAGRAM_LINK_IS_ALREADY_IN_USE') |
||
885 | |||
886 | query = (" SELECT id, name " |
||
887 | " FROM tbl_energy_flow_diagrams_nodes " |
||
888 | " WHERE id = %s ") |
||
889 | cursor.execute(query, (source_node_id,)) |
||
890 | if cursor.fetchone() is None: |
||
891 | cursor.close() |
||
892 | cnx.close() |
||
893 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
894 | description='API.SOURCE_NODE_NOT_FOUND') |
||
895 | |||
896 | query = (" SELECT id, name " |
||
897 | " FROM tbl_energy_flow_diagrams_nodes " |
||
898 | " WHERE id = %s ") |
||
899 | cursor.execute(query, (target_node_id,)) |
||
900 | if cursor.fetchone() is None: |
||
901 | cursor.close() |
||
902 | cnx.close() |
||
903 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
904 | description='API.TARGET_NODE_NOT_FOUND') |
||
905 | |||
906 | query = (" SELECT id, name, uuid " |
||
907 | " FROM tbl_meters ") |
||
908 | cursor.execute(query) |
||
909 | rows_meters = cursor.fetchall() |
||
910 | |||
911 | meter_dict = dict() |
||
912 | if rows_meters is not None and len(rows_meters) > 0: |
||
913 | for row in rows_meters: |
||
914 | meter_dict[row[2]] = {"type": 'meter', |
||
915 | "id": row[0], |
||
916 | "name": row[1], |
||
917 | "uuid": row[2]} |
||
918 | |||
919 | query = (" SELECT id, name, uuid " |
||
920 | " FROM tbl_offline_meters ") |
||
921 | cursor.execute(query) |
||
922 | rows_offline_meters = cursor.fetchall() |
||
923 | |||
924 | offline_meter_dict = dict() |
||
925 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
926 | for row in rows_offline_meters: |
||
927 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
928 | "id": row[0], |
||
929 | "name": row[1], |
||
930 | "uuid": row[2]} |
||
931 | |||
932 | query = (" SELECT id, name, uuid " |
||
933 | " FROM tbl_virtual_meters ") |
||
934 | cursor.execute(query) |
||
935 | rows_virtual_meters = cursor.fetchall() |
||
936 | |||
937 | virtual_meter_dict = dict() |
||
938 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
939 | for row in rows_virtual_meters: |
||
940 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
941 | "id": row[0], |
||
942 | "name": row[1], |
||
943 | "uuid": row[2]} |
||
944 | |||
945 | # validate meter uuid |
||
946 | if meter_dict.get(meter_uuid) is None and \ |
||
947 | virtual_meter_dict.get(meter_uuid) is None and \ |
||
948 | offline_meter_dict.get(meter_uuid) is None: |
||
949 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
950 | description='API.INVALID_METER_UUID') |
||
951 | |||
952 | add_values = (" UPDATE tbl_energy_flow_diagrams_links " |
||
953 | " SET source_node_id = %s, target_node_id = %s, meter_uuid = %s " |
||
954 | " WHERE id = %s ") |
||
955 | cursor.execute(add_values, (source_node_id, |
||
956 | target_node_id, |
||
957 | meter_uuid, |
||
958 | lid)) |
||
959 | cnx.commit() |
||
960 | |||
961 | cursor.close() |
||
962 | cnx.close() |
||
963 | |||
964 | resp.status = falcon.HTTP_200 |
||
965 | |||
966 | |||
967 | class EnergyFlowDiagramNodeCollection: |
||
968 | def __init__(self): |
||
969 | """"Initializes EnergyFlowDiagramNodeCollection""" |
||
970 | pass |
||
971 | |||
972 | @staticmethod |
||
973 | def on_options(req, resp, id_): |
||
974 | _=req |
||
975 | resp.status = falcon.HTTP_200 |
||
976 | _=id_ |
||
977 | @staticmethod |
||
978 | def on_get(req, resp, id_): |
||
979 | if 'API-KEY' not in req.headers or \ |
||
980 | not isinstance(req.headers['API-KEY'], str) or \ |
||
981 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
982 | access_control(req) |
||
983 | else: |
||
984 | api_key_control(req) |
||
985 | if not id_.isdigit() or int(id_) <= 0: |
||
986 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
987 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
988 | |||
989 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
990 | cursor = cnx.cursor() |
||
991 | |||
992 | cursor.execute(" SELECT name " |
||
993 | " FROM tbl_energy_flow_diagrams " |
||
994 | " WHERE id = %s ", (id_,)) |
||
995 | if cursor.fetchone() is None: |
||
996 | cursor.close() |
||
997 | cnx.close() |
||
998 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
999 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1000 | |||
1001 | query = (" SELECT id, name " |
||
1002 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1003 | " WHERE energy_flow_diagram_id = %s " |
||
1004 | " ORDER BY id ") |
||
1005 | cursor.execute(query, (id_, )) |
||
1006 | rows_nodes = cursor.fetchall() |
||
1007 | |||
1008 | result = list() |
||
1009 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
1010 | for row in rows_nodes: |
||
1011 | meta_result = {"id": row[0], |
||
1012 | "name": row[1]} |
||
1013 | result.append(meta_result) |
||
1014 | |||
1015 | cursor.close() |
||
1016 | cnx.close() |
||
1017 | resp.text = json.dumps(result) |
||
1018 | |||
1019 | @staticmethod |
||
1020 | @user_logger |
||
1021 | def on_post(req, resp, id_): |
||
1022 | """Handles POST requests""" |
||
1023 | admin_control(req) |
||
1024 | if not id_.isdigit() or int(id_) <= 0: |
||
1025 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1026 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1027 | try: |
||
1028 | raw_json = req.stream.read().decode('utf-8') |
||
1029 | except Exception as ex: |
||
1030 | print(str(ex)) |
||
1031 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1032 | title='API.BAD_REQUEST', |
||
1033 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1034 | |||
1035 | new_values = json.loads(raw_json) |
||
1036 | |||
1037 | if 'name' not in new_values['data'].keys() or \ |
||
1038 | not isinstance(new_values['data']['name'], str) or \ |
||
1039 | len(str.strip(new_values['data']['name'])) == 0: |
||
1040 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1041 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME') |
||
1042 | name = str.strip(new_values['data']['name']) |
||
1043 | |||
1044 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1045 | cursor = cnx.cursor() |
||
1046 | |||
1047 | cursor.execute(" SELECT name " |
||
1048 | " FROM tbl_energy_flow_diagrams " |
||
1049 | " WHERE id = %s ", (id_,)) |
||
1050 | if cursor.fetchone() is None: |
||
1051 | cursor.close() |
||
1052 | cnx.close() |
||
1053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
1054 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1055 | |||
1056 | cursor.execute(" SELECT name " |
||
1057 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1058 | " WHERE name = %s AND energy_flow_diagram_id = %s ", (name, id_)) |
||
1059 | if cursor.fetchone() is not None: |
||
1060 | cursor.close() |
||
1061 | cnx.close() |
||
1062 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1063 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
1064 | |||
1065 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_nodes " |
||
1066 | " (energy_flow_diagram_id, name) " |
||
1067 | " VALUES (%s, %s) ") |
||
1068 | cursor.execute(add_values, (id_, |
||
1069 | name)) |
||
1070 | new_id = cursor.lastrowid |
||
1071 | cnx.commit() |
||
1072 | cursor.close() |
||
1073 | cnx.close() |
||
1074 | |||
1075 | resp.status = falcon.HTTP_201 |
||
1076 | resp.location = '/energyflowdiagrams/' + str(id_) + 'nodes/' + str(new_id) |
||
1077 | |||
1078 | |||
1079 | class EnergyFlowDiagramNodeItem: |
||
1080 | def __init__(self): |
||
1081 | """"Initializes EnergyFlowDiagramNodeItem""" |
||
1082 | pass |
||
1083 | |||
1084 | @staticmethod |
||
1085 | def on_options(req, resp, id_, nid): |
||
1086 | _=req |
||
1087 | resp.status = falcon.HTTP_200 |
||
1088 | _=id_ |
||
1089 | @staticmethod |
||
1090 | def on_get(req, resp, id_, nid): |
||
1091 | access_control(req) |
||
1092 | if not id_.isdigit() or int(id_) <= 0: |
||
1093 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1094 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1095 | |||
1096 | if not nid.isdigit() or int(nid) <= 0: |
||
1097 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1098 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
1099 | |||
1100 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1101 | cursor = cnx.cursor() |
||
1102 | |||
1103 | query = (" SELECT id, name " |
||
1104 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1105 | " WHERE energy_flow_diagram_id = %s AND id = %s ") |
||
1106 | cursor.execute(query, (id_, nid)) |
||
1107 | row = cursor.fetchone() |
||
1108 | cursor.close() |
||
1109 | cnx.close() |
||
1110 | |||
1111 | if row is None: |
||
1112 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1113 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
1114 | else: |
||
1115 | meta_result = {"id": row[0], |
||
1116 | "name": row[1]} |
||
1117 | |||
1118 | resp.text = json.dumps(meta_result) |
||
1119 | |||
1120 | @staticmethod |
||
1121 | @user_logger |
||
1122 | def on_delete(req, resp, id_, nid): |
||
1123 | admin_control(req) |
||
1124 | if not id_.isdigit() or int(id_) <= 0: |
||
1125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1126 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1127 | |||
1128 | if not nid.isdigit() or int(nid) <= 0: |
||
1129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1130 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
1131 | |||
1132 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1133 | cursor = cnx.cursor() |
||
1134 | |||
1135 | cursor.execute(" SELECT name " |
||
1136 | " FROM tbl_energy_flow_diagrams " |
||
1137 | " WHERE id = %s ", |
||
1138 | (id_,)) |
||
1139 | row = cursor.fetchone() |
||
1140 | if row is None: |
||
1141 | cursor.close() |
||
1142 | cnx.close() |
||
1143 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1144 | title='API.NOT_FOUND', |
||
1145 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1146 | |||
1147 | cursor.execute(" SELECT name " |
||
1148 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1149 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
1150 | (id_, nid,)) |
||
1151 | row = cursor.fetchone() |
||
1152 | if row is None: |
||
1153 | cursor.close() |
||
1154 | cnx.close() |
||
1155 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1156 | title='API.NOT_FOUND', |
||
1157 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
1158 | |||
1159 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_nodes " |
||
1160 | " WHERE id = %s ", (nid, )) |
||
1161 | cnx.commit() |
||
1162 | |||
1163 | cursor.close() |
||
1164 | cnx.close() |
||
1165 | |||
1166 | resp.status = falcon.HTTP_204 |
||
1167 | |||
1168 | @staticmethod |
||
1169 | @user_logger |
||
1170 | def on_put(req, resp, id_, nid): |
||
1171 | """Handles PUT requests""" |
||
1172 | admin_control(req) |
||
1173 | if not id_.isdigit() or int(id_) <= 0: |
||
1174 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1175 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1176 | |||
1177 | if not nid.isdigit() or int(nid) <= 0: |
||
1178 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1179 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
1180 | |||
1181 | try: |
||
1182 | raw_json = req.stream.read().decode('utf-8') |
||
1183 | except Exception as ex: |
||
1184 | print(str(ex)) |
||
1185 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1186 | title='API.BAD_REQUEST', |
||
1187 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1188 | |||
1189 | new_values = json.loads(raw_json) |
||
1190 | |||
1191 | if 'name' not in new_values['data'].keys() or \ |
||
1192 | not isinstance(new_values['data']['name'], str) or \ |
||
1193 | len(str.strip(new_values['data']['name'])) == 0: |
||
1194 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1195 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME') |
||
1196 | name = str.strip(new_values['data']['name']) |
||
1197 | |||
1198 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1199 | cursor = cnx.cursor() |
||
1200 | |||
1201 | cursor.execute(" SELECT name " |
||
1202 | " FROM tbl_energy_flow_diagrams " |
||
1203 | " WHERE id = %s ", (id_,)) |
||
1204 | if cursor.fetchone() is None: |
||
1205 | cursor.close() |
||
1206 | cnx.close() |
||
1207 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
1208 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1209 | |||
1210 | cursor.execute(" SELECT name " |
||
1211 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1212 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
1213 | (id_, nid,)) |
||
1214 | row = cursor.fetchone() |
||
1215 | if row is None: |
||
1216 | cursor.close() |
||
1217 | cnx.close() |
||
1218 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1219 | title='API.NOT_FOUND', |
||
1220 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
1221 | |||
1222 | cursor.execute(" SELECT name " |
||
1223 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1224 | " WHERE name = %s AND energy_flow_diagram_id = %s AND id != %s ", (name, id_, nid)) |
||
1225 | row = cursor.fetchone() |
||
1226 | if row is not None: |
||
1227 | cursor.close() |
||
1228 | cnx.close() |
||
1229 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1230 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NAME_IS_ALREADY_IN_USE') |
||
1231 | |||
1232 | add_values = (" UPDATE tbl_energy_flow_diagrams_nodes " |
||
1233 | " SET name = %s " |
||
1234 | " WHERE id = %s ") |
||
1235 | cursor.execute(add_values, (name, |
||
1236 | nid)) |
||
1237 | cnx.commit() |
||
1238 | |||
1239 | cursor.close() |
||
1240 | cnx.close() |
||
1241 | |||
1242 | resp.status = falcon.HTTP_200 |
||
1243 | |||
1244 | |||
1245 | class EnergyFlowDiagramExport: |
||
1246 | def __init__(self): |
||
1247 | """"Initializes EnergyFlowDiagramExport""" |
||
1248 | pass |
||
1249 | |||
1250 | @staticmethod |
||
1251 | def on_options(req, resp, id_): |
||
1252 | _=req |
||
1253 | resp.status = falcon.HTTP_200 |
||
1254 | _=id_ |
||
1255 | View Code Duplication | @staticmethod |
|
1256 | def on_get(req, resp, id_): |
||
1257 | if 'API-KEY' not in req.headers or \ |
||
1258 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1259 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1260 | access_control(req) |
||
1261 | else: |
||
1262 | api_key_control(req) |
||
1263 | if not id_.isdigit() or int(id_) <= 0: |
||
1264 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1265 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1266 | |||
1267 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1268 | cursor = cnx.cursor() |
||
1269 | |||
1270 | query = (" SELECT id, name, uuid " |
||
1271 | " FROM tbl_meters ") |
||
1272 | cursor.execute(query) |
||
1273 | rows_meters = cursor.fetchall() |
||
1274 | |||
1275 | meter_dict = dict() |
||
1276 | if rows_meters is not None and len(rows_meters) > 0: |
||
1277 | for row in rows_meters: |
||
1278 | meter_dict[row[2]] = {"type": 'meter', |
||
1279 | "id": row[0], |
||
1280 | "name": row[1], |
||
1281 | "uuid": row[2]} |
||
1282 | |||
1283 | query = (" SELECT id, name, uuid " |
||
1284 | " FROM tbl_offline_meters ") |
||
1285 | cursor.execute(query) |
||
1286 | rows_offline_meters = cursor.fetchall() |
||
1287 | |||
1288 | offline_meter_dict = dict() |
||
1289 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1290 | for row in rows_offline_meters: |
||
1291 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1292 | "id": row[0], |
||
1293 | "name": row[1], |
||
1294 | "uuid": row[2]} |
||
1295 | |||
1296 | query = (" SELECT id, name, uuid " |
||
1297 | " FROM tbl_virtual_meters ") |
||
1298 | cursor.execute(query) |
||
1299 | rows_virtual_meters = cursor.fetchall() |
||
1300 | |||
1301 | virtual_meter_dict = dict() |
||
1302 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1303 | for row in rows_virtual_meters: |
||
1304 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1305 | "id": row[0], |
||
1306 | "name": row[1], |
||
1307 | "uuid": row[2]} |
||
1308 | |||
1309 | query = (" SELECT id, energy_flow_diagram_id, name " |
||
1310 | " FROM tbl_energy_flow_diagrams_nodes") |
||
1311 | cursor.execute(query) |
||
1312 | rows_nodes = cursor.fetchall() |
||
1313 | |||
1314 | node_dict = dict() |
||
1315 | node_list_dict = dict() |
||
1316 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
1317 | for row in rows_nodes: |
||
1318 | node_dict[row[0]] = row[2] |
||
1319 | if node_list_dict.get(row[1]) is None: |
||
1320 | node_list_dict[row[1]] = list() |
||
1321 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
||
1322 | |||
1323 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
1324 | " FROM tbl_energy_flow_diagrams_links") |
||
1325 | cursor.execute(query) |
||
1326 | rows_links = cursor.fetchall() |
||
1327 | |||
1328 | link_list_dict = dict() |
||
1329 | if rows_links is not None and len(rows_links) > 0: |
||
1330 | for row in rows_links: |
||
1331 | # find meter by uuid |
||
1332 | meter = meter_dict.get(row[4], None) |
||
1333 | if meter is None: |
||
1334 | meter = virtual_meter_dict.get(row[4], None) |
||
1335 | if meter is None: |
||
1336 | meter = offline_meter_dict.get(row[4], None) |
||
1337 | |||
1338 | if link_list_dict.get(row[1]) is None: |
||
1339 | link_list_dict[row[1]] = list() |
||
1340 | link_list_dict[row[1]].append({"id": row[0], |
||
1341 | "source_node": { |
||
1342 | "id": row[2], |
||
1343 | "name": node_dict.get(row[2])}, |
||
1344 | "target_node": { |
||
1345 | "id": row[3], |
||
1346 | "name": node_dict.get(row[3])}, |
||
1347 | "meter": meter}) |
||
1348 | |||
1349 | query = (" SELECT id, name, uuid " |
||
1350 | " FROM tbl_energy_flow_diagrams " |
||
1351 | " WHERE id = %s ") |
||
1352 | cursor.execute(query, (id_,)) |
||
1353 | row = cursor.fetchone() |
||
1354 | cursor.close() |
||
1355 | cnx.close() |
||
1356 | |||
1357 | if row is None: |
||
1358 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1359 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1360 | else: |
||
1361 | meta_result = {"id": row[0], |
||
1362 | "name": row[1], |
||
1363 | "uuid": row[2], |
||
1364 | "nodes": node_list_dict.get(row[0], None), |
||
1365 | "links": link_list_dict.get(row[0], None), |
||
1366 | } |
||
1367 | |||
1368 | resp.text = json.dumps(meta_result) |
||
1369 | |||
1370 | |||
1371 | class EnergyFlowDiagramImport: |
||
1372 | def __init__(self): |
||
1373 | """"Initializes EnergyFlowDiagramImport""" |
||
1374 | pass |
||
1375 | |||
1376 | @staticmethod |
||
1377 | def on_options(req, resp): |
||
1378 | _=req |
||
1379 | resp.status = falcon.HTTP_200 |
||
1380 | |||
1381 | @staticmethod |
||
1382 | @user_logger |
||
1383 | def on_post(req, resp): |
||
1384 | """Handles POST requests""" |
||
1385 | admin_control(req) |
||
1386 | try: |
||
1387 | raw_json = req.stream.read().decode('utf-8') |
||
1388 | except Exception as ex: |
||
1389 | print(str(ex)) |
||
1390 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1391 | title='API.BAD_REQUEST', |
||
1392 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1393 | |||
1394 | new_values = json.loads(raw_json) |
||
1395 | |||
1396 | if 'name' not in new_values.keys() or \ |
||
1397 | not isinstance(new_values['name'], str) or \ |
||
1398 | len(str.strip(new_values['name'])) == 0: |
||
1399 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1400 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
||
1401 | name = str.strip(new_values['name']) |
||
1402 | |||
1403 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1404 | cursor = cnx.cursor() |
||
1405 | |||
1406 | cursor.execute(" SELECT name " |
||
1407 | " FROM tbl_energy_flow_diagrams " |
||
1408 | " WHERE name = %s ", (name,)) |
||
1409 | if cursor.fetchone() is not None: |
||
1410 | cursor.close() |
||
1411 | cnx.close() |
||
1412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1413 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
1414 | |||
1415 | add_values = (" INSERT INTO tbl_energy_flow_diagrams " |
||
1416 | " (name, uuid) " |
||
1417 | " VALUES (%s, %s) ") |
||
1418 | cursor.execute(add_values, (name, |
||
1419 | str(uuid.uuid4()))) |
||
1420 | new_id = cursor.lastrowid |
||
1421 | for node in new_values['nodes']: |
||
1422 | if 'name' not in node.keys() or \ |
||
1423 | not isinstance(node['name'], str) or \ |
||
1424 | len(str.strip(node['name'])) == 0: |
||
1425 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1426 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME') |
||
1427 | name = str.strip(node['name']) |
||
1428 | |||
1429 | cursor.execute(" SELECT name " |
||
1430 | " FROM tbl_energy_flow_diagrams " |
||
1431 | " WHERE id = %s ", (new_id,)) |
||
1432 | if cursor.fetchone() is None: |
||
1433 | cursor.close() |
||
1434 | cnx.close() |
||
1435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
1436 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1437 | |||
1438 | cursor.execute(" SELECT name " |
||
1439 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1440 | " WHERE name = %s AND energy_flow_diagram_id = %s ", (name, new_id)) |
||
1441 | if cursor.fetchone() is not None: |
||
1442 | cursor.close() |
||
1443 | cnx.close() |
||
1444 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1445 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
1446 | |||
1447 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_nodes " |
||
1448 | " (energy_flow_diagram_id, name) " |
||
1449 | " VALUES (%s, %s) ") |
||
1450 | cursor.execute(add_values, (new_id, |
||
1451 | name)) |
||
1452 | for link in new_values['links']: |
||
1453 | source_node_id = None |
||
1454 | if 'id' in link['source_node'].keys(): |
||
1455 | if link['source_node']['id'] is not None and \ |
||
1456 | link['source_node']['id'] <= 0: |
||
1457 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1458 | description='API.INVALID_SOURCE_NODE_ID') |
||
1459 | source_node_id = link['source_node']['id'] |
||
1460 | |||
1461 | target_node_id = None |
||
1462 | if 'id' in link['target_node'].keys(): |
||
1463 | if link['target_node']['id'] is not None and \ |
||
1464 | link['target_node']['id'] <= 0: |
||
1465 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1466 | description='API.INVALID_TARGET_NODE_ID') |
||
1467 | target_node_id = link['target_node']['id'] |
||
1468 | |||
1469 | meter_uuid = None |
||
1470 | if 'uuid' in link['meter'].keys(): |
||
1471 | if link['meter']['uuid'] is not None and \ |
||
1472 | isinstance(link['meter']['uuid'], str) and \ |
||
1473 | len(str.strip(link['meter']['uuid'])) > 0: |
||
1474 | meter_uuid = str.strip(link['meter']['uuid']) |
||
1475 | |||
1476 | cursor.execute(" SELECT name " |
||
1477 | " FROM tbl_energy_flow_diagrams " |
||
1478 | " WHERE id = %s ", (new_id,)) |
||
1479 | if cursor.fetchone() is None: |
||
1480 | cursor.close() |
||
1481 | cnx.close() |
||
1482 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
1483 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1484 | |||
1485 | cursor.execute(" SELECT id " |
||
1486 | " FROM tbl_energy_flow_diagrams_links " |
||
1487 | " WHERE energy_flow_diagram_id = %s AND " |
||
1488 | " source_node_id = %s AND target_node_id = %s ", |
||
1489 | (new_id, source_node_id, target_node_id,)) |
||
1490 | row = cursor.fetchone() |
||
1491 | if row is not None: |
||
1492 | cursor.close() |
||
1493 | cnx.close() |
||
1494 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1495 | title='API.NOT_FOUND', |
||
1496 | description='API.ENERGY_FLOW_DIAGRAM_LINK_IS_ALREADY_IN_USE') |
||
1497 | |||
1498 | query = (" SELECT id, name " |
||
1499 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1500 | " WHERE id = %s ") |
||
1501 | cursor.execute(query, (source_node_id,)) |
||
1502 | if cursor.fetchone() is None: |
||
1503 | cursor.close() |
||
1504 | cnx.close() |
||
1505 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1506 | description='API.SOURCE_NODE_NOT_FOUND') |
||
1507 | |||
1508 | query = (" SELECT id, name " |
||
1509 | " FROM tbl_energy_flow_diagrams_nodes " |
||
1510 | " WHERE id = %s ") |
||
1511 | cursor.execute(query, (target_node_id,)) |
||
1512 | if cursor.fetchone() is None: |
||
1513 | cursor.close() |
||
1514 | cnx.close() |
||
1515 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1516 | description='API.TARGET_NODE_NOT_FOUND') |
||
1517 | |||
1518 | query = (" SELECT id, name, uuid " |
||
1519 | " FROM tbl_meters ") |
||
1520 | cursor.execute(query) |
||
1521 | rows_meters = cursor.fetchall() |
||
1522 | |||
1523 | meter_dict = dict() |
||
1524 | if rows_meters is not None and len(rows_meters) > 0: |
||
1525 | for row in rows_meters: |
||
1526 | meter_dict[row[2]] = {"type": 'meter', |
||
1527 | "id": row[0], |
||
1528 | "name": row[1], |
||
1529 | "uuid": row[2]} |
||
1530 | |||
1531 | query = (" SELECT id, name, uuid " |
||
1532 | " FROM tbl_offline_meters ") |
||
1533 | cursor.execute(query) |
||
1534 | rows_offline_meters = cursor.fetchall() |
||
1535 | |||
1536 | offline_meter_dict = dict() |
||
1537 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1538 | for row in rows_offline_meters: |
||
1539 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1540 | "id": row[0], |
||
1541 | "name": row[1], |
||
1542 | "uuid": row[2]} |
||
1543 | |||
1544 | query = (" SELECT id, name, uuid " |
||
1545 | " FROM tbl_virtual_meters ") |
||
1546 | cursor.execute(query) |
||
1547 | rows_virtual_meters = cursor.fetchall() |
||
1548 | |||
1549 | virtual_meter_dict = dict() |
||
1550 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1551 | for row in rows_virtual_meters: |
||
1552 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1553 | "id": row[0], |
||
1554 | "name": row[1], |
||
1555 | "uuid": row[2]} |
||
1556 | |||
1557 | # validate meter uuid |
||
1558 | if meter_dict.get(meter_uuid) is None and \ |
||
1559 | virtual_meter_dict.get(meter_uuid) is None and \ |
||
1560 | offline_meter_dict.get(meter_uuid) is None: |
||
1561 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1562 | description='API.INVALID_METER_UUID') |
||
1563 | |||
1564 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_links " |
||
1565 | " (energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid) " |
||
1566 | " VALUES (%s, %s, %s, %s) ") |
||
1567 | cursor.execute(add_values, (new_id, |
||
1568 | source_node_id, |
||
1569 | target_node_id, |
||
1570 | meter_uuid)) |
||
1571 | cnx.commit() |
||
1572 | cursor.close() |
||
1573 | cnx.close() |
||
1574 | |||
1575 | resp.status = falcon.HTTP_201 |
||
1576 | resp.location = '/energyflowdiagrams/' + str(new_id) |
||
1577 | |||
1578 | |||
1579 | class EnergyFlowDiagramClone: |
||
1580 | def __init__(self): |
||
1581 | """"Initializes EnergyFlowDiagramClone""" |
||
1582 | pass |
||
1583 | |||
1584 | @staticmethod |
||
1585 | def on_options(req, resp, id_): |
||
1586 | _=req |
||
1587 | resp.status = falcon.HTTP_200 |
||
1588 | _=id_ |
||
1589 | @staticmethod |
||
1590 | @user_logger |
||
1591 | def on_post(req, resp, id_): |
||
1592 | if 'API-KEY' not in req.headers or \ |
||
1593 | not isinstance(req.headers['API-KEY'], str) or \ |
||
1594 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
1595 | access_control(req) |
||
1596 | else: |
||
1597 | api_key_control(req) |
||
1598 | if not id_.isdigit() or int(id_) <= 0: |
||
1599 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1600 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
1601 | |||
1602 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1603 | cursor = cnx.cursor() |
||
1604 | |||
1605 | query = (" SELECT id, name, uuid " |
||
1606 | " FROM tbl_meters ") |
||
1607 | cursor.execute(query) |
||
1608 | rows_meters = cursor.fetchall() |
||
1609 | |||
1610 | meter_dict = dict() |
||
1611 | if rows_meters is not None and len(rows_meters) > 0: |
||
1612 | for row in rows_meters: |
||
1613 | meter_dict[row[2]] = {"type": 'meter', |
||
1614 | "id": row[0], |
||
1615 | "name": row[1], |
||
1616 | "uuid": row[2]} |
||
1617 | |||
1618 | query = (" SELECT id, name, uuid " |
||
1619 | " FROM tbl_offline_meters ") |
||
1620 | cursor.execute(query) |
||
1621 | rows_offline_meters = cursor.fetchall() |
||
1622 | |||
1623 | offline_meter_dict = dict() |
||
1624 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
1625 | for row in rows_offline_meters: |
||
1626 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
1627 | "id": row[0], |
||
1628 | "name": row[1], |
||
1629 | "uuid": row[2]} |
||
1630 | |||
1631 | query = (" SELECT id, name, uuid " |
||
1632 | " FROM tbl_virtual_meters ") |
||
1633 | cursor.execute(query) |
||
1634 | rows_virtual_meters = cursor.fetchall() |
||
1635 | |||
1636 | virtual_meter_dict = dict() |
||
1637 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
1638 | for row in rows_virtual_meters: |
||
1639 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
1640 | "id": row[0], |
||
1641 | "name": row[1], |
||
1642 | "uuid": row[2]} |
||
1643 | |||
1644 | query = (" SELECT id, energy_flow_diagram_id, name " |
||
1645 | " FROM tbl_energy_flow_diagrams_nodes") |
||
1646 | cursor.execute(query) |
||
1647 | rows_nodes = cursor.fetchall() |
||
1648 | |||
1649 | node_dict = dict() |
||
1650 | node_list_dict = dict() |
||
1651 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
1652 | for row in rows_nodes: |
||
1653 | node_dict[row[0]] = row[2] |
||
1654 | if node_list_dict.get(row[1]) is None: |
||
1655 | node_list_dict[row[1]] = list() |
||
1656 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
||
1657 | |||
1658 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
1659 | " FROM tbl_energy_flow_diagrams_links") |
||
1660 | cursor.execute(query) |
||
1661 | rows_links = cursor.fetchall() |
||
1662 | |||
1663 | link_list_dict = dict() |
||
1664 | if rows_links is not None and len(rows_links) > 0: |
||
1665 | for row in rows_links: |
||
1666 | # find meter by uuid |
||
1667 | meter = meter_dict.get(row[4], None) |
||
1668 | if meter is None: |
||
1669 | meter = virtual_meter_dict.get(row[4], None) |
||
1670 | if meter is None: |
||
1671 | meter = offline_meter_dict.get(row[4], None) |
||
1672 | |||
1673 | if link_list_dict.get(row[1]) is None: |
||
1674 | link_list_dict[row[1]] = list() |
||
1675 | link_list_dict[row[1]].append({"id": row[0], |
||
1676 | "source_node": { |
||
1677 | "id": row[2], |
||
1678 | "name": node_dict.get(row[2])}, |
||
1679 | "target_node": { |
||
1680 | "id": row[3], |
||
1681 | "name": node_dict.get(row[3])}, |
||
1682 | "meter": meter}) |
||
1683 | |||
1684 | query = (" SELECT id, name, uuid " |
||
1685 | " FROM tbl_energy_flow_diagrams " |
||
1686 | " WHERE id = %s ") |
||
1687 | cursor.execute(query, (id_,)) |
||
1688 | row = cursor.fetchone() |
||
1689 | |||
1690 | if row is None: |
||
1691 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1692 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
1693 | else: |
||
1694 | meta_result = {"id": row[0], |
||
1695 | "name": row[1], |
||
1696 | "uuid": row[2], |
||
1697 | "nodes": node_list_dict.get(row[0], None), |
||
1698 | "links": link_list_dict.get(row[0], None), |
||
1699 | } |
||
1700 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
1701 | if config.utc_offset[0] == '-': |
||
1702 | timezone_offset = -timezone_offset |
||
1703 | new_name = (str.strip(meta_result['name']) + |
||
1704 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
1705 | add_values = (" INSERT INTO tbl_energy_flow_diagrams " |
||
1706 | " (name, uuid) " |
||
1707 | " VALUES (%s, %s) ") |
||
1708 | cursor.execute(add_values, (new_name, |
||
1709 | str(uuid.uuid4()))) |
||
1710 | new_id = cursor.lastrowid |
||
1711 | if meta_result['nodes'] is not None and len(meta_result['nodes']) > 0: |
||
1712 | for node in meta_result['nodes']: |
||
1713 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_nodes " |
||
1714 | " (energy_flow_diagram_id, name) " |
||
1715 | " VALUES (%s, %s) ") |
||
1716 | cursor.execute(add_values, (new_id, |
||
1717 | node['name'])) |
||
1718 | if meta_result['links'] is not None and len(meta_result['links']) > 0: |
||
1719 | for link in meta_result['links']: |
||
1720 | if link['meter'] is None: |
||
1721 | continue |
||
1722 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_links " |
||
1723 | " (energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid) " |
||
1724 | " VALUES (%s, %s, %s, %s) ") |
||
1725 | cursor.execute(add_values, (new_id, |
||
1726 | link['source_node']['id'], |
||
1727 | link['target_node']['id'], |
||
1728 | link['meter']['uuid'])) |
||
1729 | cnx.commit() |
||
1730 | cursor.close() |
||
1731 | cnx.close() |
||
1732 | |||
1733 | resp.status = falcon.HTTP_201 |
||
1734 | resp.location = '/energyflowdiagrams/' + str(new_id) |
||
1735 | |||
1736 |