| Total Complexity | 228 |
| Total Lines | 1171 |
| Duplicated Lines | 21.86 % |
| 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 falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | |||
| 7 | |||
| 8 | class EnergyFlowDiagramCollection: |
||
| 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, energy_flow_diagram_id, name " |
||
| 23 | " FROM tbl_energy_flow_diagrams_nodes") |
||
| 24 | cursor.execute(query) |
||
| 25 | rows_nodes = cursor.fetchall() |
||
| 26 | query = (" SELECT id, name, uuid " |
||
| 27 | " FROM tbl_meters ") |
||
| 28 | cursor.execute(query) |
||
| 29 | rows_meters = cursor.fetchall() |
||
| 30 | |||
| 31 | meter_dict = dict() |
||
| 32 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 33 | for row in rows_meters: |
||
| 34 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 35 | "id": row['id'], |
||
| 36 | "name": row['name'], |
||
| 37 | "uuid": row['uuid']} |
||
| 38 | |||
| 39 | query = (" SELECT id, name, uuid " |
||
| 40 | " FROM tbl_offline_meters ") |
||
| 41 | cursor.execute(query) |
||
| 42 | rows_offline_meters = cursor.fetchall() |
||
| 43 | |||
| 44 | offline_meter_dict = dict() |
||
| 45 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 46 | for row in rows_offline_meters: |
||
| 47 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 48 | "id": row['id'], |
||
| 49 | "name": row['name'], |
||
| 50 | "uuid": row['uuid']} |
||
| 51 | |||
| 52 | query = (" SELECT id, name, uuid " |
||
| 53 | " FROM tbl_virtual_meters ") |
||
| 54 | cursor.execute(query) |
||
| 55 | rows_virtual_meters = cursor.fetchall() |
||
| 56 | |||
| 57 | virtual_meter_dict = dict() |
||
| 58 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 59 | for row in rows_virtual_meters: |
||
| 60 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 61 | "id": row['id'], |
||
| 62 | "name": row['name'], |
||
| 63 | "uuid": row['uuid']} |
||
| 64 | |||
| 65 | node_dict = dict() |
||
| 66 | node_list_dict = dict() |
||
| 67 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
| 68 | for row in rows_nodes: |
||
| 69 | node_dict[row['id']] = row['name'] |
||
| 70 | if node_list_dict.get(row['energy_flow_diagram_id']) is None: |
||
| 71 | node_list_dict[row['energy_flow_diagram_id']] = list() |
||
| 72 | node_list_dict[row['energy_flow_diagram_id']].append({"id": row['id'], "name": row['name']}) |
||
| 73 | |||
| 74 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
| 75 | " FROM tbl_energy_flow_diagrams_links") |
||
| 76 | cursor.execute(query) |
||
| 77 | rows_links = cursor.fetchall() |
||
| 78 | |||
| 79 | link_list_dict = dict() |
||
| 80 | View Code Duplication | if rows_links is not None and len(rows_links) > 0: |
|
|
|
|||
| 81 | for row in rows_links: |
||
| 82 | # find meter by uuid |
||
| 83 | meter = meter_dict.get(row['meter_uuid'], None) |
||
| 84 | if meter is None: |
||
| 85 | meter = virtual_meter_dict.get(row['meter_uuid'], None) |
||
| 86 | if meter is None: |
||
| 87 | meter = offline_meter_dict.get(row['meter_uuid'], None) |
||
| 88 | |||
| 89 | if link_list_dict.get(row['energy_flow_diagram_id']) is None: |
||
| 90 | link_list_dict[row['energy_flow_diagram_id']] = list() |
||
| 91 | link_list_dict[row['energy_flow_diagram_id']].append({"id": row['id'], |
||
| 92 | "source_node": { |
||
| 93 | "id": row['source_node_id'], |
||
| 94 | "name": node_dict.get(row['source_node_id'])}, |
||
| 95 | "target_node": { |
||
| 96 | "id": row['target_node_id'], |
||
| 97 | "name": node_dict.get(row['target_node_id'])}, |
||
| 98 | "meter": meter}) |
||
| 99 | |||
| 100 | query = (" SELECT id, name, uuid " |
||
| 101 | " FROM tbl_energy_flow_diagrams " |
||
| 102 | " ORDER BY id ") |
||
| 103 | cursor.execute(query) |
||
| 104 | rows_energy_flow_diagrams = cursor.fetchall() |
||
| 105 | |||
| 106 | result = list() |
||
| 107 | if rows_energy_flow_diagrams is not None and len(rows_energy_flow_diagrams) > 0: |
||
| 108 | for row in rows_energy_flow_diagrams: |
||
| 109 | |||
| 110 | meta_result = {"id": row['id'], |
||
| 111 | "name": row['name'], |
||
| 112 | "uuid": row['uuid'], |
||
| 113 | "nodes": node_list_dict.get(row['id'], None), |
||
| 114 | "links": link_list_dict.get(row['id'], None), } |
||
| 115 | result.append(meta_result) |
||
| 116 | |||
| 117 | cursor.close() |
||
| 118 | cnx.disconnect() |
||
| 119 | resp.body = json.dumps(result) |
||
| 120 | |||
| 121 | View Code Duplication | @staticmethod |
|
| 122 | def on_post(req, resp): |
||
| 123 | """Handles POST requests""" |
||
| 124 | try: |
||
| 125 | raw_json = req.stream.read().decode('utf-8') |
||
| 126 | except Exception as ex: |
||
| 127 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 128 | |||
| 129 | new_values = json.loads(raw_json) |
||
| 130 | |||
| 131 | if 'name' not in new_values['data'].keys() or \ |
||
| 132 | not isinstance(new_values['data']['name'], str) or \ |
||
| 133 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 134 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 135 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
||
| 136 | name = str.strip(new_values['data']['name']) |
||
| 137 | |||
| 138 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 139 | cursor = cnx.cursor() |
||
| 140 | |||
| 141 | cursor.execute(" SELECT name " |
||
| 142 | " FROM tbl_energy_flow_diagrams " |
||
| 143 | " WHERE name = %s ", (name,)) |
||
| 144 | if cursor.fetchone() is not None: |
||
| 145 | cursor.close() |
||
| 146 | cnx.disconnect() |
||
| 147 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 148 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
| 149 | |||
| 150 | add_values = (" INSERT INTO tbl_energy_flow_diagrams " |
||
| 151 | " (name, uuid) " |
||
| 152 | " VALUES (%s, %s) ") |
||
| 153 | cursor.execute(add_values, (name, |
||
| 154 | str(uuid.uuid4()))) |
||
| 155 | new_id = cursor.lastrowid |
||
| 156 | cnx.commit() |
||
| 157 | cursor.close() |
||
| 158 | cnx.disconnect() |
||
| 159 | |||
| 160 | resp.status = falcon.HTTP_201 |
||
| 161 | resp.location = '/energyflowdiagrams/' + str(new_id) |
||
| 162 | |||
| 163 | |||
| 164 | class EnergyFlowDiagramItem: |
||
| 165 | @staticmethod |
||
| 166 | def __init__(): |
||
| 167 | pass |
||
| 168 | |||
| 169 | @staticmethod |
||
| 170 | def on_options(req, resp, id_): |
||
| 171 | resp.status = falcon.HTTP_200 |
||
| 172 | |||
| 173 | @staticmethod |
||
| 174 | def on_get(req, resp, id_): |
||
| 175 | if not id_.isdigit() or int(id_) <= 0: |
||
| 176 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 177 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 178 | |||
| 179 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 180 | cursor = cnx.cursor(dictionary=True) |
||
| 181 | |||
| 182 | query = (" SELECT id, energy_flow_diagram_id, name " |
||
| 183 | " FROM tbl_energy_flow_diagrams_nodes") |
||
| 184 | cursor.execute(query) |
||
| 185 | rows_nodes = cursor.fetchall() |
||
| 186 | query = (" SELECT id, name, uuid " |
||
| 187 | " FROM tbl_meters ") |
||
| 188 | cursor.execute(query) |
||
| 189 | rows_meters = cursor.fetchall() |
||
| 190 | |||
| 191 | meter_dict = dict() |
||
| 192 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 193 | for row in rows_meters: |
||
| 194 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 195 | "id": row['id'], |
||
| 196 | "name": row['name'], |
||
| 197 | "uuid": row['uuid']} |
||
| 198 | |||
| 199 | query = (" SELECT id, name, uuid " |
||
| 200 | " FROM tbl_offline_meters ") |
||
| 201 | cursor.execute(query) |
||
| 202 | rows_offline_meters = cursor.fetchall() |
||
| 203 | |||
| 204 | offline_meter_dict = dict() |
||
| 205 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 206 | for row in rows_offline_meters: |
||
| 207 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 208 | "id": row['id'], |
||
| 209 | "name": row['name'], |
||
| 210 | "uuid": row['uuid']} |
||
| 211 | |||
| 212 | query = (" SELECT id, name, uuid " |
||
| 213 | " FROM tbl_virtual_meters ") |
||
| 214 | cursor.execute(query) |
||
| 215 | rows_virtual_meters = cursor.fetchall() |
||
| 216 | |||
| 217 | virtual_meter_dict = dict() |
||
| 218 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 219 | for row in rows_virtual_meters: |
||
| 220 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 221 | "id": row['id'], |
||
| 222 | "name": row['name'], |
||
| 223 | "uuid": row['uuid']} |
||
| 224 | |||
| 225 | node_dict = dict() |
||
| 226 | node_list_dict = dict() |
||
| 227 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
| 228 | for row in rows_nodes: |
||
| 229 | node_dict[row['id']] = row['name'] |
||
| 230 | if node_list_dict.get(row['energy_flow_diagram_id']) is None: |
||
| 231 | node_list_dict[row['energy_flow_diagram_id']] = list() |
||
| 232 | node_list_dict[row['energy_flow_diagram_id']].append({"id": row['id'], "name": row['name']}) |
||
| 233 | |||
| 234 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
||
| 235 | " FROM tbl_energy_flow_diagrams_links") |
||
| 236 | cursor.execute(query) |
||
| 237 | rows_links = cursor.fetchall() |
||
| 238 | |||
| 239 | link_list_dict = dict() |
||
| 240 | View Code Duplication | if rows_links is not None and len(rows_links) > 0: |
|
| 241 | for row in rows_links: |
||
| 242 | # find meter by uuid |
||
| 243 | meter = meter_dict.get(row['meter_uuid'], None) |
||
| 244 | if meter is None: |
||
| 245 | meter = virtual_meter_dict.get(row['meter_uuid'], None) |
||
| 246 | if meter is None: |
||
| 247 | meter = offline_meter_dict.get(row['meter_uuid'], None) |
||
| 248 | |||
| 249 | if link_list_dict.get(row['energy_flow_diagram_id']) is None: |
||
| 250 | link_list_dict[row['energy_flow_diagram_id']] = list() |
||
| 251 | link_list_dict[row['energy_flow_diagram_id']].append({"id": row['id'], |
||
| 252 | "source_node": { |
||
| 253 | "id": row['source_node_id'], |
||
| 254 | "name": node_dict.get(row['source_node_id'])}, |
||
| 255 | "target_node": { |
||
| 256 | "id": row['target_node_id'], |
||
| 257 | "name": node_dict.get(row['target_node_id'])}, |
||
| 258 | "meter": meter}) |
||
| 259 | |||
| 260 | query = (" SELECT id, name, uuid " |
||
| 261 | " FROM tbl_energy_flow_diagrams " |
||
| 262 | " WHERE id = %s ") |
||
| 263 | cursor.execute(query, (id_,)) |
||
| 264 | row = cursor.fetchone() |
||
| 265 | cursor.close() |
||
| 266 | cnx.disconnect() |
||
| 267 | |||
| 268 | if row is None: |
||
| 269 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 270 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 271 | else: |
||
| 272 | meta_result = {"id": row['id'], |
||
| 273 | "name": row['name'], |
||
| 274 | "uuid": row['uuid'], |
||
| 275 | "nodes": node_list_dict.get(row['id'], None), |
||
| 276 | "links": link_list_dict.get(row['id'], None), |
||
| 277 | } |
||
| 278 | |||
| 279 | resp.body = json.dumps(meta_result) |
||
| 280 | |||
| 281 | View Code Duplication | @staticmethod |
|
| 282 | def on_delete(req, resp, id_): |
||
| 283 | if not id_.isdigit() or int(id_) <= 0: |
||
| 284 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 285 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 286 | |||
| 287 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 288 | cursor = cnx.cursor() |
||
| 289 | |||
| 290 | # delete all associated nodes |
||
| 291 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_nodes" |
||
| 292 | " WHERE energy_flow_diagram_id = %s ", (id_,)) |
||
| 293 | cnx.commit() |
||
| 294 | |||
| 295 | # delete all associated links |
||
| 296 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_links" |
||
| 297 | " WHERE energy_flow_diagram_id = %s ", (id_,)) |
||
| 298 | cnx.commit() |
||
| 299 | |||
| 300 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams" |
||
| 301 | " WHERE id = %s ", (id_,)) |
||
| 302 | cnx.commit() |
||
| 303 | |||
| 304 | cursor.close() |
||
| 305 | cnx.disconnect() |
||
| 306 | |||
| 307 | resp.status = falcon.HTTP_204 |
||
| 308 | |||
| 309 | View Code Duplication | @staticmethod |
|
| 310 | def on_put(req, resp, id_): |
||
| 311 | """Handles PUT requests""" |
||
| 312 | if not id_.isdigit() or int(id_) <= 0: |
||
| 313 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 314 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 315 | try: |
||
| 316 | raw_json = req.stream.read().decode('utf-8') |
||
| 317 | except Exception as ex: |
||
| 318 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 319 | |||
| 320 | new_values = json.loads(raw_json) |
||
| 321 | |||
| 322 | if 'name' not in new_values['data'].keys() or \ |
||
| 323 | not isinstance(new_values['data']['name'], str) or \ |
||
| 324 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 325 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 326 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
||
| 327 | name = str.strip(new_values['data']['name']) |
||
| 328 | |||
| 329 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 330 | cursor = cnx.cursor() |
||
| 331 | |||
| 332 | cursor.execute(" SELECT name " |
||
| 333 | " FROM tbl_energy_flow_diagrams " |
||
| 334 | " WHERE id = %s ", (id_,)) |
||
| 335 | if cursor.fetchone() is None: |
||
| 336 | cursor.close() |
||
| 337 | cnx.disconnect() |
||
| 338 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 339 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 340 | |||
| 341 | cursor.execute(" SELECT name " |
||
| 342 | " FROM tbl_energy_flow_diagrams " |
||
| 343 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 344 | if cursor.fetchone() is not None: |
||
| 345 | cursor.close() |
||
| 346 | cnx.disconnect() |
||
| 347 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 348 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
| 349 | |||
| 350 | update_row = (" UPDATE tbl_energy_flow_diagrams " |
||
| 351 | " SET name = %s " |
||
| 352 | " WHERE id = %s ") |
||
| 353 | cursor.execute(update_row, (name, |
||
| 354 | id_)) |
||
| 355 | cnx.commit() |
||
| 356 | |||
| 357 | cursor.close() |
||
| 358 | cnx.disconnect() |
||
| 359 | |||
| 360 | resp.status = falcon.HTTP_200 |
||
| 361 | |||
| 362 | |||
| 363 | class EnergyFlowDiagramLinkCollection: |
||
| 364 | @staticmethod |
||
| 365 | def __init__(): |
||
| 366 | pass |
||
| 367 | |||
| 368 | @staticmethod |
||
| 369 | def on_options(req, resp, id_): |
||
| 370 | resp.status = falcon.HTTP_200 |
||
| 371 | |||
| 372 | @staticmethod |
||
| 373 | def on_get(req, resp, id_): |
||
| 374 | if not id_.isdigit() or int(id_) <= 0: |
||
| 375 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 376 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 377 | |||
| 378 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 379 | cursor = cnx.cursor(dictionary=True) |
||
| 380 | |||
| 381 | query = (" SELECT id, name " |
||
| 382 | " FROM tbl_energy_flow_diagrams_nodes ") |
||
| 383 | cursor.execute(query) |
||
| 384 | rows_nodes = cursor.fetchall() |
||
| 385 | |||
| 386 | node_dict = dict() |
||
| 387 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
| 388 | for row in rows_nodes: |
||
| 389 | node_dict[row['id']] = {"id": row['id'], |
||
| 390 | "name": row['name']} |
||
| 391 | |||
| 392 | query = (" SELECT id, name, uuid " |
||
| 393 | " FROM tbl_meters ") |
||
| 394 | cursor.execute(query) |
||
| 395 | rows_meters = cursor.fetchall() |
||
| 396 | |||
| 397 | meter_dict = dict() |
||
| 398 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 399 | for row in rows_meters: |
||
| 400 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 401 | "id": row['id'], |
||
| 402 | "name": row['name'], |
||
| 403 | "uuid": row['uuid']} |
||
| 404 | |||
| 405 | query = (" SELECT id, name, uuid " |
||
| 406 | " FROM tbl_offline_meters ") |
||
| 407 | cursor.execute(query) |
||
| 408 | rows_offline_meters = cursor.fetchall() |
||
| 409 | |||
| 410 | offline_meter_dict = dict() |
||
| 411 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 412 | for row in rows_offline_meters: |
||
| 413 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 414 | "id": row['id'], |
||
| 415 | "name": row['name'], |
||
| 416 | "uuid": row['uuid']} |
||
| 417 | |||
| 418 | query = (" SELECT id, name, uuid " |
||
| 419 | " FROM tbl_virtual_meters ") |
||
| 420 | cursor.execute(query) |
||
| 421 | rows_virtual_meters = cursor.fetchall() |
||
| 422 | |||
| 423 | virtual_meter_dict = dict() |
||
| 424 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 425 | for row in rows_virtual_meters: |
||
| 426 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 427 | "id": row['id'], |
||
| 428 | "name": row['name'], |
||
| 429 | "uuid": row['uuid']} |
||
| 430 | |||
| 431 | cursor.execute(" SELECT name " |
||
| 432 | " FROM tbl_energy_flow_diagrams " |
||
| 433 | " WHERE id = %s ", (id_,)) |
||
| 434 | if cursor.fetchone() is None: |
||
| 435 | cursor.close() |
||
| 436 | cnx.disconnect() |
||
| 437 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 438 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 439 | |||
| 440 | query = (" SELECT id, source_node_id, target_node_id, meter_uuid " |
||
| 441 | " FROM tbl_energy_flow_diagrams_links " |
||
| 442 | " WHERE energy_flow_diagram_id = %s " |
||
| 443 | " ORDER BY id ") |
||
| 444 | cursor.execute(query, (id_, )) |
||
| 445 | rows_links = cursor.fetchall() |
||
| 446 | |||
| 447 | result = list() |
||
| 448 | if rows_links is not None and len(rows_links) > 0: |
||
| 449 | for row in rows_links: |
||
| 450 | source_node = node_dict.get(row['source_node_id'], None) |
||
| 451 | target_node = node_dict.get(row['target_node_id'], None) |
||
| 452 | # find meter by uuid |
||
| 453 | meter = meter_dict.get(row['meter_uuid'], None) |
||
| 454 | if meter is None: |
||
| 455 | meter = virtual_meter_dict.get(row['meter_uuid'], None) |
||
| 456 | if meter is None: |
||
| 457 | meter = offline_meter_dict.get(row['meter_uuid'], None) |
||
| 458 | |||
| 459 | meta_result = {"id": row['id'], |
||
| 460 | "source_node": source_node, |
||
| 461 | "target_node": target_node, |
||
| 462 | "meter": meter} |
||
| 463 | result.append(meta_result) |
||
| 464 | |||
| 465 | cursor.close() |
||
| 466 | cnx.disconnect() |
||
| 467 | resp.body = json.dumps(result) |
||
| 468 | |||
| 469 | @staticmethod |
||
| 470 | def on_post(req, resp, id_): |
||
| 471 | """Handles POST requests""" |
||
| 472 | if not id_.isdigit() or int(id_) <= 0: |
||
| 473 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 474 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 475 | try: |
||
| 476 | raw_json = req.stream.read().decode('utf-8') |
||
| 477 | except Exception as ex: |
||
| 478 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 479 | |||
| 480 | new_values = json.loads(raw_json) |
||
| 481 | |||
| 482 | source_node_id = None |
||
| 483 | if 'source_node_id' in new_values['data'].keys(): |
||
| 484 | if new_values['data']['source_node_id'] is not None and \ |
||
| 485 | new_values['data']['source_node_id'] <= 0: |
||
| 486 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 487 | description='API.INVALID_SOURCE_NODE_ID') |
||
| 488 | source_node_id = new_values['data']['source_node_id'] |
||
| 489 | |||
| 490 | target_node_id = None |
||
| 491 | if 'target_node_id' in new_values['data'].keys(): |
||
| 492 | if new_values['data']['target_node_id'] is not None and \ |
||
| 493 | new_values['data']['target_node_id'] <= 0: |
||
| 494 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 495 | description='API.INVALID_TARGET_NODE_ID') |
||
| 496 | target_node_id = new_values['data']['target_node_id'] |
||
| 497 | |||
| 498 | meter_uuid = None |
||
| 499 | if 'meter_uuid' in new_values['data'].keys(): |
||
| 500 | if new_values['data']['meter_uuid'] is not None and \ |
||
| 501 | isinstance(new_values['data']['meter_uuid'], str) and \ |
||
| 502 | len(str.strip(new_values['data']['meter_uuid'])) > 0: |
||
| 503 | meter_uuid = str.strip(new_values['data']['meter_uuid']) |
||
| 504 | |||
| 505 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 506 | cursor = cnx.cursor(dictionary=True) |
||
| 507 | |||
| 508 | cursor.execute(" SELECT name " |
||
| 509 | " FROM tbl_energy_flow_diagrams " |
||
| 510 | " WHERE id = %s ", (id_,)) |
||
| 511 | if cursor.fetchone() is None: |
||
| 512 | cursor.close() |
||
| 513 | cnx.disconnect() |
||
| 514 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 515 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 516 | |||
| 517 | cursor.execute(" SELECT id " |
||
| 518 | " FROM tbl_energy_flow_diagrams_links " |
||
| 519 | " WHERE energy_flow_diagram_id = %s AND " |
||
| 520 | " source_node_id = %s AND target_node_id = %s ", |
||
| 521 | (id_, source_node_id, target_node_id,)) |
||
| 522 | row = cursor.fetchone() |
||
| 523 | if row is not None: |
||
| 524 | cursor.close() |
||
| 525 | cnx.disconnect() |
||
| 526 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 527 | title='API.NOT_FOUND', |
||
| 528 | description='API.ENERGY_FLOW_DIAGRAM_LINK_IS_ALREADY_IN_USE') |
||
| 529 | |||
| 530 | query = (" SELECT id, name " |
||
| 531 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 532 | " WHERE id = %s ") |
||
| 533 | cursor.execute(query, (source_node_id,)) |
||
| 534 | if cursor.fetchone() is None: |
||
| 535 | cursor.close() |
||
| 536 | cnx.disconnect() |
||
| 537 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 538 | description='API.SOURCE_NODE_NOT_FOUND') |
||
| 539 | |||
| 540 | query = (" SELECT id, name " |
||
| 541 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 542 | " WHERE id = %s ") |
||
| 543 | cursor.execute(query, (target_node_id,)) |
||
| 544 | if cursor.fetchone() is None: |
||
| 545 | cursor.close() |
||
| 546 | cnx.disconnect() |
||
| 547 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 548 | description='API.TARGET_NODE_NOT_FOUND') |
||
| 549 | |||
| 550 | query = (" SELECT id, name, uuid " |
||
| 551 | " FROM tbl_meters ") |
||
| 552 | cursor.execute(query) |
||
| 553 | rows_meters = cursor.fetchall() |
||
| 554 | |||
| 555 | meter_dict = dict() |
||
| 556 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 557 | for row in rows_meters: |
||
| 558 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 559 | "id": row['id'], |
||
| 560 | "name": row['name'], |
||
| 561 | "uuid": row['uuid']} |
||
| 562 | |||
| 563 | query = (" SELECT id, name, uuid " |
||
| 564 | " FROM tbl_offline_meters ") |
||
| 565 | cursor.execute(query) |
||
| 566 | rows_offline_meters = cursor.fetchall() |
||
| 567 | |||
| 568 | offline_meter_dict = dict() |
||
| 569 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 570 | for row in rows_offline_meters: |
||
| 571 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 572 | "id": row['id'], |
||
| 573 | "name": row['name'], |
||
| 574 | "uuid": row['uuid']} |
||
| 575 | |||
| 576 | query = (" SELECT id, name, uuid " |
||
| 577 | " FROM tbl_virtual_meters ") |
||
| 578 | cursor.execute(query) |
||
| 579 | rows_virtual_meters = cursor.fetchall() |
||
| 580 | |||
| 581 | virtual_meter_dict = dict() |
||
| 582 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 583 | for row in rows_virtual_meters: |
||
| 584 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 585 | "id": row['id'], |
||
| 586 | "name": row['name'], |
||
| 587 | "uuid": row['uuid']} |
||
| 588 | |||
| 589 | # validate meter uuid |
||
| 590 | if meter_dict.get(meter_uuid) is None and \ |
||
| 591 | virtual_meter_dict.get(meter_uuid) is None and \ |
||
| 592 | offline_meter_dict.get(meter_uuid) is None: |
||
| 593 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 594 | description='API.INVALID_METER_UUID') |
||
| 595 | |||
| 596 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_links " |
||
| 597 | " (energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid) " |
||
| 598 | " VALUES (%s, %s, %s, %s) ") |
||
| 599 | cursor.execute(add_values, (id_, |
||
| 600 | source_node_id, |
||
| 601 | target_node_id, |
||
| 602 | meter_uuid)) |
||
| 603 | new_id = cursor.lastrowid |
||
| 604 | cnx.commit() |
||
| 605 | cursor.close() |
||
| 606 | cnx.disconnect() |
||
| 607 | |||
| 608 | resp.status = falcon.HTTP_201 |
||
| 609 | resp.location = '/energyflowdiagrams/' + str(id_) + 'links/' + str(new_id) |
||
| 610 | |||
| 611 | |||
| 612 | class EnergyFlowDiagramLinkItem: |
||
| 613 | @staticmethod |
||
| 614 | def __init__(): |
||
| 615 | pass |
||
| 616 | |||
| 617 | @staticmethod |
||
| 618 | def on_options(req, resp, id_, lid): |
||
| 619 | resp.status = falcon.HTTP_200 |
||
| 620 | |||
| 621 | @staticmethod |
||
| 622 | def on_get(req, resp, id_, lid): |
||
| 623 | if not id_.isdigit() or int(id_) <= 0: |
||
| 624 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 625 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 626 | |||
| 627 | if not lid.isdigit() or int(lid) <= 0: |
||
| 628 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 629 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
| 630 | |||
| 631 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 632 | cursor = cnx.cursor(dictionary=True) |
||
| 633 | |||
| 634 | query = (" SELECT id, name " |
||
| 635 | " FROM tbl_energy_flow_diagrams_nodes ") |
||
| 636 | cursor.execute(query) |
||
| 637 | rows_nodes = cursor.fetchall() |
||
| 638 | |||
| 639 | node_dict = dict() |
||
| 640 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
| 641 | for row in rows_nodes: |
||
| 642 | node_dict[row['id']] = {"id": row['id'], |
||
| 643 | "name": row['name']} |
||
| 644 | |||
| 645 | query = (" SELECT id, name, uuid " |
||
| 646 | " FROM tbl_meters ") |
||
| 647 | cursor.execute(query) |
||
| 648 | rows_meters = cursor.fetchall() |
||
| 649 | |||
| 650 | meter_dict = dict() |
||
| 651 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 652 | for row in rows_meters: |
||
| 653 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 654 | "id": row['id'], |
||
| 655 | "name": row['name'], |
||
| 656 | "uuid": row['uuid']} |
||
| 657 | |||
| 658 | query = (" SELECT id, name, uuid " |
||
| 659 | " FROM tbl_offline_meters ") |
||
| 660 | cursor.execute(query) |
||
| 661 | rows_offline_meters = cursor.fetchall() |
||
| 662 | |||
| 663 | offline_meter_dict = dict() |
||
| 664 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 665 | for row in rows_offline_meters: |
||
| 666 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 667 | "id": row['id'], |
||
| 668 | "name": row['name'], |
||
| 669 | "uuid": row['uuid']} |
||
| 670 | |||
| 671 | query = (" SELECT id, name, uuid " |
||
| 672 | " FROM tbl_virtual_meters ") |
||
| 673 | cursor.execute(query) |
||
| 674 | rows_virtual_meters = cursor.fetchall() |
||
| 675 | |||
| 676 | virtual_meter_dict = dict() |
||
| 677 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 678 | for row in rows_virtual_meters: |
||
| 679 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 680 | "id": row['id'], |
||
| 681 | "name": row['name'], |
||
| 682 | "uuid": row['uuid']} |
||
| 683 | |||
| 684 | query = (" SELECT id, source_node_id, target_node_id, meter_uuid " |
||
| 685 | " FROM tbl_energy_flow_diagrams_links " |
||
| 686 | " WHERE energy_flow_diagram_id = %s AND id = %s ") |
||
| 687 | cursor.execute(query, (id_, lid)) |
||
| 688 | row = cursor.fetchone() |
||
| 689 | cursor.close() |
||
| 690 | cnx.disconnect() |
||
| 691 | |||
| 692 | if row is None: |
||
| 693 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 694 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
| 695 | else: |
||
| 696 | source_node = node_dict.get(row['source_node_id'], None) |
||
| 697 | target_node = node_dict.get(row['target_node_id'], None) |
||
| 698 | # find meter by uuid |
||
| 699 | meter = meter_dict.get(row['meter_uuid'], None) |
||
| 700 | if meter is None: |
||
| 701 | meter = virtual_meter_dict.get(row['meter_uuid'], None) |
||
| 702 | if meter is None: |
||
| 703 | meter = offline_meter_dict.get(row['meter_uuid'], None) |
||
| 704 | |||
| 705 | meta_result = {"id": row['id'], |
||
| 706 | "source_node": source_node, |
||
| 707 | "target_node": target_node, |
||
| 708 | "meter": meter} |
||
| 709 | resp.body = json.dumps(meta_result) |
||
| 710 | |||
| 711 | @staticmethod |
||
| 712 | def on_delete(req, resp, id_, lid): |
||
| 713 | if not id_.isdigit() or int(id_) <= 0: |
||
| 714 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 715 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 716 | |||
| 717 | if not lid.isdigit() or int(lid) <= 0: |
||
| 718 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 719 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
| 720 | |||
| 721 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 722 | cursor = cnx.cursor() |
||
| 723 | |||
| 724 | cursor.execute(" SELECT name " |
||
| 725 | " FROM tbl_energy_flow_diagrams " |
||
| 726 | " WHERE id = %s ", |
||
| 727 | (id_,)) |
||
| 728 | row = cursor.fetchone() |
||
| 729 | if row is None: |
||
| 730 | cursor.close() |
||
| 731 | cnx.disconnect() |
||
| 732 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 733 | title='API.NOT_FOUND', |
||
| 734 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 735 | |||
| 736 | cursor.execute(" SELECT id " |
||
| 737 | " FROM tbl_energy_flow_diagrams_links " |
||
| 738 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
| 739 | (id_, lid,)) |
||
| 740 | row = cursor.fetchone() |
||
| 741 | if row is None: |
||
| 742 | cursor.close() |
||
| 743 | cnx.disconnect() |
||
| 744 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 745 | title='API.NOT_FOUND', |
||
| 746 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
| 747 | |||
| 748 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_links " |
||
| 749 | " WHERE id = %s ", (lid, )) |
||
| 750 | cnx.commit() |
||
| 751 | |||
| 752 | cursor.close() |
||
| 753 | cnx.disconnect() |
||
| 754 | |||
| 755 | resp.status = falcon.HTTP_204 |
||
| 756 | |||
| 757 | @staticmethod |
||
| 758 | def on_put(req, resp, id_, lid): |
||
| 759 | """Handles POST requests""" |
||
| 760 | if not id_.isdigit() or int(id_) <= 0: |
||
| 761 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 762 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 763 | |||
| 764 | if not lid.isdigit() or int(lid) <= 0: |
||
| 765 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 766 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_LINK_ID') |
||
| 767 | |||
| 768 | try: |
||
| 769 | raw_json = req.stream.read().decode('utf-8') |
||
| 770 | except Exception as ex: |
||
| 771 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 772 | |||
| 773 | new_values = json.loads(raw_json) |
||
| 774 | |||
| 775 | source_node_id = None |
||
| 776 | if 'source_node_id' in new_values['data'].keys(): |
||
| 777 | if new_values['data']['source_node_id'] is not None and \ |
||
| 778 | new_values['data']['source_node_id'] <= 0: |
||
| 779 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 780 | description='API.INVALID_SOURCE_NODE_ID') |
||
| 781 | source_node_id = new_values['data']['source_node_id'] |
||
| 782 | |||
| 783 | target_node_id = None |
||
| 784 | if 'target_node_id' in new_values['data'].keys(): |
||
| 785 | if new_values['data']['target_node_id'] is not None and \ |
||
| 786 | new_values['data']['target_node_id'] <= 0: |
||
| 787 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 788 | description='API.INVALID_TARGET_NODE_ID') |
||
| 789 | target_node_id = new_values['data']['target_node_id'] |
||
| 790 | |||
| 791 | meter_uuid = None |
||
| 792 | if 'meter_uuid' in new_values['data'].keys(): |
||
| 793 | if new_values['data']['meter_uuid'] is not None and \ |
||
| 794 | isinstance(new_values['data']['meter_uuid'], str) and \ |
||
| 795 | len(str.strip(new_values['data']['meter_uuid'])) > 0: |
||
| 796 | meter_uuid = str.strip(new_values['data']['meter_uuid']) |
||
| 797 | |||
| 798 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 799 | cursor = cnx.cursor(dictionary=True) |
||
| 800 | |||
| 801 | cursor.execute(" SELECT name " |
||
| 802 | " FROM tbl_energy_flow_diagrams " |
||
| 803 | " WHERE id = %s ", (id_,)) |
||
| 804 | if cursor.fetchone() is None: |
||
| 805 | cursor.close() |
||
| 806 | cnx.disconnect() |
||
| 807 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 808 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 809 | |||
| 810 | cursor.execute(" SELECT id " |
||
| 811 | " FROM tbl_energy_flow_diagrams_links " |
||
| 812 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
| 813 | (id_, lid,)) |
||
| 814 | row = cursor.fetchone() |
||
| 815 | if row is None: |
||
| 816 | cursor.close() |
||
| 817 | cnx.disconnect() |
||
| 818 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 819 | title='API.NOT_FOUND', |
||
| 820 | description='API.ENERGY_FLOW_DIAGRAM_LINK_NOT_FOUND_OR_NOT_MATCH') |
||
| 821 | |||
| 822 | cursor.execute(" SELECT id " |
||
| 823 | " FROM tbl_energy_flow_diagrams_links " |
||
| 824 | " WHERE energy_flow_diagram_id = %s AND id != %s " |
||
| 825 | " AND source_node_id = %s AND target_node_id = %s ", |
||
| 826 | (id_, lid, source_node_id, target_node_id,)) |
||
| 827 | row = cursor.fetchone() |
||
| 828 | if row is not None: |
||
| 829 | cursor.close() |
||
| 830 | cnx.disconnect() |
||
| 831 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 832 | title='API.NOT_FOUND', |
||
| 833 | description='API.ENERGY_FLOW_DIAGRAM_LINK_IS_ALREADY_IN_USE') |
||
| 834 | |||
| 835 | query = (" SELECT id, name " |
||
| 836 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 837 | " WHERE id = %s ") |
||
| 838 | cursor.execute(query, (source_node_id,)) |
||
| 839 | if cursor.fetchone() is None: |
||
| 840 | cursor.close() |
||
| 841 | cnx.disconnect() |
||
| 842 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 843 | description='API.SOURCE_NODE_NOT_FOUND') |
||
| 844 | |||
| 845 | query = (" SELECT id, name " |
||
| 846 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 847 | " WHERE id = %s ") |
||
| 848 | cursor.execute(query, (target_node_id,)) |
||
| 849 | if cursor.fetchone() is None: |
||
| 850 | cursor.close() |
||
| 851 | cnx.disconnect() |
||
| 852 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 853 | description='API.TARGET_NODE_NOT_FOUND') |
||
| 854 | |||
| 855 | query = (" SELECT id, name, uuid " |
||
| 856 | " FROM tbl_meters ") |
||
| 857 | cursor.execute(query) |
||
| 858 | rows_meters = cursor.fetchall() |
||
| 859 | |||
| 860 | meter_dict = dict() |
||
| 861 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 862 | for row in rows_meters: |
||
| 863 | meter_dict[row['uuid']] = {"type": 'meter', |
||
| 864 | "id": row['id'], |
||
| 865 | "name": row['name'], |
||
| 866 | "uuid": row['uuid']} |
||
| 867 | |||
| 868 | query = (" SELECT id, name, uuid " |
||
| 869 | " FROM tbl_offline_meters ") |
||
| 870 | cursor.execute(query) |
||
| 871 | rows_offline_meters = cursor.fetchall() |
||
| 872 | |||
| 873 | offline_meter_dict = dict() |
||
| 874 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 875 | for row in rows_offline_meters: |
||
| 876 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
||
| 877 | "id": row['id'], |
||
| 878 | "name": row['name'], |
||
| 879 | "uuid": row['uuid']} |
||
| 880 | |||
| 881 | query = (" SELECT id, name, uuid " |
||
| 882 | " FROM tbl_virtual_meters ") |
||
| 883 | cursor.execute(query) |
||
| 884 | rows_virtual_meters = cursor.fetchall() |
||
| 885 | |||
| 886 | virtual_meter_dict = dict() |
||
| 887 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 888 | for row in rows_virtual_meters: |
||
| 889 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
||
| 890 | "id": row['id'], |
||
| 891 | "name": row['name'], |
||
| 892 | "uuid": row['uuid']} |
||
| 893 | |||
| 894 | # validate meter uuid |
||
| 895 | if meter_dict.get(meter_uuid) is None and \ |
||
| 896 | virtual_meter_dict.get(meter_uuid) is None and \ |
||
| 897 | offline_meter_dict.get(meter_uuid) is None: |
||
| 898 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 899 | description='API.INVALID_METER_UUID') |
||
| 900 | |||
| 901 | add_values = (" UPDATE tbl_energy_flow_diagrams_links " |
||
| 902 | " SET source_node_id = %s, target_node_id = %s, meter_uuid = %s " |
||
| 903 | " WHERE id = %s ") |
||
| 904 | cursor.execute(add_values, (source_node_id, |
||
| 905 | target_node_id, |
||
| 906 | meter_uuid, |
||
| 907 | lid)) |
||
| 908 | cnx.commit() |
||
| 909 | |||
| 910 | cursor.close() |
||
| 911 | cnx.disconnect() |
||
| 912 | |||
| 913 | resp.status = falcon.HTTP_200 |
||
| 914 | |||
| 915 | |||
| 916 | View Code Duplication | class EnergyFlowDiagramNodeCollection: |
|
| 917 | @staticmethod |
||
| 918 | def __init__(): |
||
| 919 | pass |
||
| 920 | |||
| 921 | @staticmethod |
||
| 922 | def on_options(req, resp, id_): |
||
| 923 | resp.status = falcon.HTTP_200 |
||
| 924 | |||
| 925 | @staticmethod |
||
| 926 | def on_get(req, resp, id_): |
||
| 927 | if not id_.isdigit() or int(id_) <= 0: |
||
| 928 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 929 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 930 | |||
| 931 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 932 | cursor = cnx.cursor(dictionary=True) |
||
| 933 | |||
| 934 | cursor.execute(" SELECT name " |
||
| 935 | " FROM tbl_energy_flow_diagrams " |
||
| 936 | " WHERE id = %s ", (id_,)) |
||
| 937 | if cursor.fetchone() is None: |
||
| 938 | cursor.close() |
||
| 939 | cnx.disconnect() |
||
| 940 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 941 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 942 | |||
| 943 | query = (" SELECT id, name " |
||
| 944 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 945 | " WHERE energy_flow_diagram_id = %s " |
||
| 946 | " ORDER BY id ") |
||
| 947 | cursor.execute(query, (id_, )) |
||
| 948 | rows_nodes = cursor.fetchall() |
||
| 949 | |||
| 950 | result = list() |
||
| 951 | if rows_nodes is not None and len(rows_nodes) > 0: |
||
| 952 | for row in rows_nodes: |
||
| 953 | meta_result = {"id": row['id'], |
||
| 954 | "name": row['name']} |
||
| 955 | result.append(meta_result) |
||
| 956 | |||
| 957 | cursor.close() |
||
| 958 | cnx.disconnect() |
||
| 959 | resp.body = json.dumps(result) |
||
| 960 | |||
| 961 | @staticmethod |
||
| 962 | def on_post(req, resp, id_): |
||
| 963 | """Handles POST requests""" |
||
| 964 | if not id_.isdigit() or int(id_) <= 0: |
||
| 965 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 966 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 967 | try: |
||
| 968 | raw_json = req.stream.read().decode('utf-8') |
||
| 969 | except Exception as ex: |
||
| 970 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 971 | |||
| 972 | new_values = json.loads(raw_json) |
||
| 973 | |||
| 974 | if 'name' not in new_values['data'].keys() or \ |
||
| 975 | not isinstance(new_values['data']['name'], str) or \ |
||
| 976 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 977 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 978 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME') |
||
| 979 | name = str.strip(new_values['data']['name']) |
||
| 980 | |||
| 981 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 982 | cursor = cnx.cursor(dictionary=True) |
||
| 983 | |||
| 984 | cursor.execute(" SELECT name " |
||
| 985 | " FROM tbl_energy_flow_diagrams " |
||
| 986 | " WHERE id = %s ", (id_,)) |
||
| 987 | if cursor.fetchone() is None: |
||
| 988 | cursor.close() |
||
| 989 | cnx.disconnect() |
||
| 990 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 991 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 992 | |||
| 993 | cursor.execute(" SELECT name " |
||
| 994 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 995 | " WHERE name = %s AND energy_flow_diagram_id = %s ", (name, id_)) |
||
| 996 | if cursor.fetchone() is not None: |
||
| 997 | cursor.close() |
||
| 998 | cnx.disconnect() |
||
| 999 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1000 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
||
| 1001 | |||
| 1002 | add_values = (" INSERT INTO tbl_energy_flow_diagrams_nodes " |
||
| 1003 | " (energy_flow_diagram_id, name) " |
||
| 1004 | " VALUES (%s, %s) ") |
||
| 1005 | cursor.execute(add_values, (id_, |
||
| 1006 | name)) |
||
| 1007 | new_id = cursor.lastrowid |
||
| 1008 | cnx.commit() |
||
| 1009 | cursor.close() |
||
| 1010 | cnx.disconnect() |
||
| 1011 | |||
| 1012 | resp.status = falcon.HTTP_201 |
||
| 1013 | resp.location = '/energyflowdiagrams/' + str(id_) + 'nodes/' + str(new_id) |
||
| 1014 | |||
| 1015 | |||
| 1016 | class EnergyFlowDiagramNodeItem: |
||
| 1017 | @staticmethod |
||
| 1018 | def __init__(): |
||
| 1019 | pass |
||
| 1020 | |||
| 1021 | @staticmethod |
||
| 1022 | def on_options(req, resp, id_, nid): |
||
| 1023 | resp.status = falcon.HTTP_200 |
||
| 1024 | |||
| 1025 | @staticmethod |
||
| 1026 | def on_get(req, resp, id_, nid): |
||
| 1027 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1028 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1029 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 1030 | |||
| 1031 | if not nid.isdigit() or int(nid) <= 0: |
||
| 1032 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1033 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
| 1034 | |||
| 1035 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1036 | cursor = cnx.cursor(dictionary=True) |
||
| 1037 | |||
| 1038 | query = (" SELECT id, name " |
||
| 1039 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 1040 | " WHERE energy_flow_diagram_id = %s AND id = %s ") |
||
| 1041 | cursor.execute(query, (id_, nid)) |
||
| 1042 | row = cursor.fetchone() |
||
| 1043 | cursor.close() |
||
| 1044 | cnx.disconnect() |
||
| 1045 | |||
| 1046 | if row is None: |
||
| 1047 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1048 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
| 1049 | else: |
||
| 1050 | meta_result = {"id": row['id'], |
||
| 1051 | "name": row['name']} |
||
| 1052 | |||
| 1053 | resp.body = json.dumps(meta_result) |
||
| 1054 | |||
| 1055 | @staticmethod |
||
| 1056 | def on_delete(req, resp, id_, nid): |
||
| 1057 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1058 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1059 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 1060 | |||
| 1061 | if not nid.isdigit() or int(nid) <= 0: |
||
| 1062 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1063 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
| 1064 | |||
| 1065 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1066 | cursor = cnx.cursor() |
||
| 1067 | |||
| 1068 | cursor.execute(" SELECT name " |
||
| 1069 | " FROM tbl_energy_flow_diagrams " |
||
| 1070 | " WHERE id = %s ", |
||
| 1071 | (id_,)) |
||
| 1072 | row = cursor.fetchone() |
||
| 1073 | if row is None: |
||
| 1074 | cursor.close() |
||
| 1075 | cnx.disconnect() |
||
| 1076 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1077 | title='API.NOT_FOUND', |
||
| 1078 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 1079 | |||
| 1080 | cursor.execute(" SELECT name " |
||
| 1081 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 1082 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
| 1083 | (id_, nid,)) |
||
| 1084 | row = cursor.fetchone() |
||
| 1085 | if row is None: |
||
| 1086 | cursor.close() |
||
| 1087 | cnx.disconnect() |
||
| 1088 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1089 | title='API.NOT_FOUND', |
||
| 1090 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
| 1091 | |||
| 1092 | cursor.execute(" DELETE FROM tbl_energy_flow_diagrams_nodes " |
||
| 1093 | " WHERE id = %s ", (nid, )) |
||
| 1094 | cnx.commit() |
||
| 1095 | |||
| 1096 | cursor.close() |
||
| 1097 | cnx.disconnect() |
||
| 1098 | |||
| 1099 | resp.status = falcon.HTTP_204 |
||
| 1100 | |||
| 1101 | @staticmethod |
||
| 1102 | def on_put(req, resp, id_, nid): |
||
| 1103 | """Handles POST requests""" |
||
| 1104 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1105 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1106 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 1107 | |||
| 1108 | if not nid.isdigit() or int(nid) <= 0: |
||
| 1109 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1110 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_ID') |
||
| 1111 | |||
| 1112 | try: |
||
| 1113 | raw_json = req.stream.read().decode('utf-8') |
||
| 1114 | except Exception as ex: |
||
| 1115 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 1116 | |||
| 1117 | new_values = json.loads(raw_json) |
||
| 1118 | |||
| 1119 | if 'name' not in new_values['data'].keys() or \ |
||
| 1120 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1121 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1122 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1123 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME') |
||
| 1124 | name = str.strip(new_values['data']['name']) |
||
| 1125 | |||
| 1126 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1127 | cursor = cnx.cursor(dictionary=True) |
||
| 1128 | |||
| 1129 | cursor.execute(" SELECT name " |
||
| 1130 | " FROM tbl_energy_flow_diagrams " |
||
| 1131 | " WHERE id = %s ", (id_,)) |
||
| 1132 | if cursor.fetchone() is None: |
||
| 1133 | cursor.close() |
||
| 1134 | cnx.disconnect() |
||
| 1135 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1136 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 1137 | |||
| 1138 | cursor.execute(" SELECT name " |
||
| 1139 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 1140 | " WHERE energy_flow_diagram_id = %s AND id = %s ", |
||
| 1141 | (id_, nid,)) |
||
| 1142 | row = cursor.fetchone() |
||
| 1143 | if row is None: |
||
| 1144 | cursor.close() |
||
| 1145 | cnx.disconnect() |
||
| 1146 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1147 | title='API.NOT_FOUND', |
||
| 1148 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NOT_FOUND_OR_NOT_MATCH') |
||
| 1149 | |||
| 1150 | cursor.execute(" SELECT name " |
||
| 1151 | " FROM tbl_energy_flow_diagrams_nodes " |
||
| 1152 | " WHERE name = %s AND energy_flow_diagram_id = %s AND id != %s ", (name, id_, nid)) |
||
| 1153 | row = cursor.fetchone() |
||
| 1154 | if row is not None: |
||
| 1155 | cursor.close() |
||
| 1156 | cnx.disconnect() |
||
| 1157 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1158 | description='API.ENERGY_FLOW_DIAGRAM_NODE_NAME_IS_ALREADY_IN_USE') |
||
| 1159 | |||
| 1160 | add_values = (" UPDATE tbl_energy_flow_diagrams_nodes " |
||
| 1161 | " SET name = %s " |
||
| 1162 | " WHERE id = %s ") |
||
| 1163 | cursor.execute(add_values, (name, |
||
| 1164 | nid)) |
||
| 1165 | cnx.commit() |
||
| 1166 | |||
| 1167 | cursor.close() |
||
| 1168 | cnx.disconnect() |
||
| 1169 | |||
| 1170 | resp.status = falcon.HTTP_200 |
||
| 1171 | |||
| 1172 |