| @@ 343-720 (lines=378) @@ | ||
| 340 | resp.location = '/hybridpowerstations/' + str(new_id) |
|
| 341 | ||
| 342 | ||
| 343 | class HybridPowerStationItem: |
|
| 344 | def __init__(self): |
|
| 345 | """"Initializes Class""" |
|
| 346 | pass |
|
| 347 | ||
| 348 | @staticmethod |
|
| 349 | def on_options(req, resp, id_): |
|
| 350 | resp.status = falcon.HTTP_200 |
|
| 351 | ||
| 352 | @staticmethod |
|
| 353 | def on_get(req, resp, id_): |
|
| 354 | access_control(req) |
|
| 355 | if not id_.isdigit() or int(id_) <= 0: |
|
| 356 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 357 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 358 | ||
| 359 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 360 | cursor = cnx.cursor() |
|
| 361 | ||
| 362 | contact_dict = dict() |
|
| 363 | query = (" SELECT id, name, uuid " |
|
| 364 | " FROM tbl_contacts ") |
|
| 365 | cursor.execute(query) |
|
| 366 | rows_contacts = cursor.fetchall() |
|
| 367 | if rows_contacts is not None and len(rows_contacts) > 0: |
|
| 368 | for row in rows_contacts: |
|
| 369 | contact_dict[row[0]] = {"id": row[0], |
|
| 370 | "name": row[1], |
|
| 371 | "uuid": row[2]} |
|
| 372 | ||
| 373 | cost_center_dict = dict() |
|
| 374 | query = (" SELECT id, name, uuid " |
|
| 375 | " FROM tbl_cost_centers ") |
|
| 376 | cursor.execute(query) |
|
| 377 | rows_cost_centers = cursor.fetchall() |
|
| 378 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
| 379 | for row in rows_cost_centers: |
|
| 380 | cost_center_dict[row[0]] = {"id": row[0], |
|
| 381 | "name": row[1], |
|
| 382 | "uuid": row[2]} |
|
| 383 | ||
| 384 | svg_dict = dict() |
|
| 385 | query = (" SELECT id, name, uuid " |
|
| 386 | " FROM tbl_svgs ") |
|
| 387 | cursor.execute(query) |
|
| 388 | rows_svgs = cursor.fetchall() |
|
| 389 | if rows_svgs is not None and len(rows_svgs) > 0: |
|
| 390 | for row in rows_svgs: |
|
| 391 | svg_dict[row[0]] = {"id": row[0], |
|
| 392 | "name": row[1], |
|
| 393 | "uuid": row[2]} |
|
| 394 | ||
| 395 | # query point dict |
|
| 396 | point_dict = dict() |
|
| 397 | query = (" SELECT id, name " |
|
| 398 | " FROM tbl_points ") |
|
| 399 | cursor.execute(query) |
|
| 400 | rows_points = cursor.fetchall() |
|
| 401 | if rows_points is not None and len(rows_points) > 0: |
|
| 402 | for row in rows_points: |
|
| 403 | point_dict[row[0]] = {"id": row[0], |
|
| 404 | "name": row[1]} |
|
| 405 | ||
| 406 | query = (" SELECT id, name, uuid, " |
|
| 407 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 408 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 409 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id " |
|
| 410 | " FROM tbl_hybrid_power_stations " |
|
| 411 | " WHERE id = %s ") |
|
| 412 | cursor.execute(query, (id_,)) |
|
| 413 | row = cursor.fetchone() |
|
| 414 | cursor.close() |
|
| 415 | cnx.close() |
|
| 416 | ||
| 417 | if row is None: |
|
| 418 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 419 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 420 | else: |
|
| 421 | meta_result = {"id": row[0], |
|
| 422 | "name": row[1], |
|
| 423 | "uuid": row[2], |
|
| 424 | "address": row[3], |
|
| 425 | "postal_code": row[4], |
|
| 426 | "latitude": row[5], |
|
| 427 | "longitude": row[6], |
|
| 428 | "rated_capacity": row[7], |
|
| 429 | "rated_power": row[8], |
|
| 430 | "contact": contact_dict.get(row[9], None), |
|
| 431 | "cost_center": cost_center_dict.get(row[10], None), |
|
| 432 | "svg": svg_dict.get(row[11], None), |
|
| 433 | "is_cost_data_displayed": bool(row[12]), |
|
| 434 | "phase_of_lifecycle": row[13], |
|
| 435 | "description": row[14], |
|
| 436 | "latitude_point": point_dict.get(row[15], None), |
|
| 437 | "longitude_point": point_dict.get(row[16], None), |
|
| 438 | "svg2": svg_dict.get(row[17], None), |
|
| 439 | "svg3": svg_dict.get(row[18], None), |
|
| 440 | "svg4": svg_dict.get(row[19], None), |
|
| 441 | "svg5": svg_dict.get(row[20], None), |
|
| 442 | "qrcode": 'hybridpowerstation:' + row[2]} |
|
| 443 | ||
| 444 | resp.text = json.dumps(meta_result) |
|
| 445 | ||
| 446 | @staticmethod |
|
| 447 | @user_logger |
|
| 448 | def on_delete(req, resp, id_): |
|
| 449 | admin_control(req) |
|
| 450 | if not id_.isdigit() or int(id_) <= 0: |
|
| 451 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 452 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 453 | ||
| 454 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 455 | cursor = cnx.cursor() |
|
| 456 | ||
| 457 | cursor.execute(" SELECT name " |
|
| 458 | " FROM tbl_hybrid_power_stations " |
|
| 459 | " WHERE id = %s ", (id_,)) |
|
| 460 | if cursor.fetchone() is None: |
|
| 461 | cursor.close() |
|
| 462 | cnx.close() |
|
| 463 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 464 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 465 | cursor.execute(" DELETE FROM tbl_hybrid_power_stations_containers " |
|
| 466 | " WHERE hybrid_power_station_id = %s ", (id_,)) |
|
| 467 | ||
| 468 | cursor.execute(" DELETE FROM tbl_hybrid_power_stations " |
|
| 469 | " WHERE id = %s ", (id_,)) |
|
| 470 | cnx.commit() |
|
| 471 | ||
| 472 | cursor.close() |
|
| 473 | cnx.close() |
|
| 474 | ||
| 475 | resp.status = falcon.HTTP_204 |
|
| 476 | ||
| 477 | @staticmethod |
|
| 478 | @user_logger |
|
| 479 | def on_put(req, resp, id_): |
|
| 480 | """Handles PUT requests""" |
|
| 481 | admin_control(req) |
|
| 482 | try: |
|
| 483 | raw_json = req.stream.read().decode('utf-8') |
|
| 484 | except Exception as ex: |
|
| 485 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 486 | title='API.BAD_REQUEST', |
|
| 487 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 488 | ||
| 489 | if not id_.isdigit() or int(id_) <= 0: |
|
| 490 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 491 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 492 | ||
| 493 | new_values = json.loads(raw_json) |
|
| 494 | ||
| 495 | if 'name' not in new_values['data'].keys() or \ |
|
| 496 | not isinstance(new_values['data']['name'], str) or \ |
|
| 497 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 498 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 499 | description='API.INVALID_HYBRID_POWER_STATION_NAME') |
|
| 500 | name = str.strip(new_values['data']['name']) |
|
| 501 | ||
| 502 | if 'address' not in new_values['data'].keys() or \ |
|
| 503 | not isinstance(new_values['data']['address'], str) or \ |
|
| 504 | len(str.strip(new_values['data']['address'])) == 0: |
|
| 505 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 506 | description='API.INVALID_ADDRESS_VALUE') |
|
| 507 | address = str.strip(new_values['data']['address']) |
|
| 508 | ||
| 509 | if 'postal_code' not in new_values['data'].keys() or \ |
|
| 510 | not isinstance(new_values['data']['postal_code'], str) or \ |
|
| 511 | len(str.strip(new_values['data']['postal_code'])) == 0: |
|
| 512 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 513 | description='API.INVALID_POSTAL_CODE_VALUE') |
|
| 514 | postal_code = str.strip(new_values['data']['postal_code']) |
|
| 515 | ||
| 516 | if 'latitude' not in new_values['data'].keys() or \ |
|
| 517 | not (isinstance(new_values['data']['latitude'], float) or |
|
| 518 | isinstance(new_values['data']['latitude'], int)) or \ |
|
| 519 | new_values['data']['latitude'] < -90.0 or \ |
|
| 520 | new_values['data']['latitude'] > 90.0: |
|
| 521 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 522 | description='API.INVALID_LATITUDE_VALUE') |
|
| 523 | latitude = new_values['data']['latitude'] |
|
| 524 | ||
| 525 | if 'longitude' not in new_values['data'].keys() or \ |
|
| 526 | not (isinstance(new_values['data']['longitude'], float) or |
|
| 527 | isinstance(new_values['data']['longitude'], int)) or \ |
|
| 528 | new_values['data']['longitude'] < -180.0 or \ |
|
| 529 | new_values['data']['longitude'] > 180.0: |
|
| 530 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 531 | description='API.INVALID_LONGITUDE_VALUE') |
|
| 532 | longitude = new_values['data']['longitude'] |
|
| 533 | ||
| 534 | if 'rated_capacity' not in new_values['data'].keys() or \ |
|
| 535 | not (isinstance(new_values['data']['rated_capacity'], float) or |
|
| 536 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
|
| 537 | new_values['data']['rated_capacity'] < 0.0: |
|
| 538 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 539 | description='API.INVALID_RATED_CAPACITY') |
|
| 540 | rated_capacity = new_values['data']['rated_capacity'] |
|
| 541 | ||
| 542 | if 'rated_power' not in new_values['data'].keys() or \ |
|
| 543 | not (isinstance(new_values['data']['rated_power'], float) or |
|
| 544 | isinstance(new_values['data']['rated_power'], int)) or \ |
|
| 545 | new_values['data']['rated_power'] < 0.0: |
|
| 546 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 547 | description='API.INVALID_RATED_POWER') |
|
| 548 | rated_power = new_values['data']['rated_power'] |
|
| 549 | ||
| 550 | if 'contact_id' not in new_values['data'].keys() or \ |
|
| 551 | not isinstance(new_values['data']['contact_id'], int) or \ |
|
| 552 | new_values['data']['contact_id'] <= 0: |
|
| 553 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 554 | description='API.INVALID_CONTACT_ID') |
|
| 555 | contact_id = new_values['data']['contact_id'] |
|
| 556 | ||
| 557 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 558 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 559 | new_values['data']['cost_center_id'] <= 0: |
|
| 560 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 561 | description='API.INVALID_COST_CENTER_ID') |
|
| 562 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 563 | ||
| 564 | if 'svg_id' not in new_values['data'].keys() or \ |
|
| 565 | not isinstance(new_values['data']['svg_id'], int) or \ |
|
| 566 | new_values['data']['svg_id'] <= 0: |
|
| 567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 568 | description='API.INVALID_SVG_ID') |
|
| 569 | svg_id = new_values['data']['svg_id'] |
|
| 570 | ||
| 571 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
|
| 572 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
|
| 573 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 574 | description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE') |
|
| 575 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
|
| 576 | ||
| 577 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
|
| 578 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
|
| 579 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
|
| 580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 581 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
|
| 582 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
|
| 583 | ||
| 584 | if 'description' in new_values['data'].keys() and \ |
|
| 585 | new_values['data']['description'] is not None and \ |
|
| 586 | len(str(new_values['data']['description'])) > 0: |
|
| 587 | description = str.strip(new_values['data']['description']) |
|
| 588 | else: |
|
| 589 | description = None |
|
| 590 | ||
| 591 | if 'latitude_point_id' in new_values['data'].keys() and \ |
|
| 592 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
|
| 593 | new_values['data']['latitude_point_id'] > 0: |
|
| 594 | latitude_point_id = new_values['data']['latitude_point_id'] |
|
| 595 | else: |
|
| 596 | latitude_point_id = None |
|
| 597 | ||
| 598 | if 'longitude_point_id' in new_values['data'].keys() and \ |
|
| 599 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
|
| 600 | new_values['data']['longitude_point_id'] > 0: |
|
| 601 | longitude_point_id = new_values['data']['longitude_point_id'] |
|
| 602 | else: |
|
| 603 | longitude_point_id = None |
|
| 604 | ||
| 605 | if 'svg2_id' in new_values['data'].keys() and \ |
|
| 606 | isinstance(new_values['data']['svg2_id'], int) and \ |
|
| 607 | new_values['data']['svg2_id'] > 0: |
|
| 608 | svg2_id = new_values['data']['svg2_id'] |
|
| 609 | else: |
|
| 610 | svg2_id = None |
|
| 611 | ||
| 612 | if 'svg3_id' in new_values['data'].keys() and \ |
|
| 613 | isinstance(new_values['data']['svg3_id'], int) and \ |
|
| 614 | new_values['data']['svg3_id'] > 0: |
|
| 615 | svg3_id = new_values['data']['svg3_id'] |
|
| 616 | else: |
|
| 617 | svg3_id = None |
|
| 618 | ||
| 619 | if 'svg4_id' in new_values['data'].keys() and \ |
|
| 620 | isinstance(new_values['data']['svg4_id'], int) and \ |
|
| 621 | new_values['data']['svg4_id'] > 0: |
|
| 622 | svg4_id = new_values['data']['svg4_id'] |
|
| 623 | else: |
|
| 624 | svg4_id = None |
|
| 625 | ||
| 626 | if 'svg5_id' in new_values['data'].keys() and \ |
|
| 627 | isinstance(new_values['data']['svg5_id'], int) and \ |
|
| 628 | new_values['data']['svg5_id'] > 0: |
|
| 629 | svg5_id = new_values['data']['svg5_id'] |
|
| 630 | else: |
|
| 631 | svg5_id = None |
|
| 632 | ||
| 633 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 634 | cursor = cnx.cursor() |
|
| 635 | ||
| 636 | cursor.execute(" SELECT name " |
|
| 637 | " FROM tbl_hybrid_power_stations " |
|
| 638 | " WHERE id = %s ", (id_,)) |
|
| 639 | if cursor.fetchone() is None: |
|
| 640 | cursor.close() |
|
| 641 | cnx.close() |
|
| 642 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 643 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 644 | ||
| 645 | cursor.execute(" SELECT name " |
|
| 646 | " FROM tbl_hybrid_power_stations " |
|
| 647 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
| 648 | if cursor.fetchone() is not None: |
|
| 649 | cursor.close() |
|
| 650 | cnx.close() |
|
| 651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 652 | description='API.HYBRID_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
|
| 653 | ||
| 654 | cursor.execute(" SELECT name " |
|
| 655 | " FROM tbl_contacts " |
|
| 656 | " WHERE id = %s ", |
|
| 657 | (new_values['data']['contact_id'],)) |
|
| 658 | row = cursor.fetchone() |
|
| 659 | if row is None: |
|
| 660 | cursor.close() |
|
| 661 | cnx.close() |
|
| 662 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 663 | description='API.CONTACT_NOT_FOUND') |
|
| 664 | ||
| 665 | cursor.execute(" SELECT name " |
|
| 666 | " FROM tbl_cost_centers " |
|
| 667 | " WHERE id = %s ", |
|
| 668 | (new_values['data']['cost_center_id'],)) |
|
| 669 | row = cursor.fetchone() |
|
| 670 | if row is None: |
|
| 671 | cursor.close() |
|
| 672 | cnx.close() |
|
| 673 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 674 | description='API.COST_CENTER_NOT_FOUND') |
|
| 675 | ||
| 676 | cursor.execute(" SELECT name " |
|
| 677 | " FROM tbl_svgs " |
|
| 678 | " WHERE id = %s ", |
|
| 679 | (new_values['data']['svg_id'],)) |
|
| 680 | row = cursor.fetchone() |
|
| 681 | if row is None: |
|
| 682 | cursor.close() |
|
| 683 | cnx.close() |
|
| 684 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 685 | description='API.SVG_NOT_FOUND') |
|
| 686 | ||
| 687 | update_row = (" UPDATE tbl_hybrid_power_stations " |
|
| 688 | " SET name = %s, address = %s, postal_code = %s, latitude = %s, longitude = %s, " |
|
| 689 | " rated_capacity = %s, rated_power = %s, " |
|
| 690 | " contact_id = %s, cost_center_id = %s, " |
|
| 691 | " svg_id = %s, is_cost_data_displayed = %s, phase_of_lifecycle = %s, description = %s, " |
|
| 692 | " latitude_point_id = %s, longitude_point_id = %s, " |
|
| 693 | " svg2_id = %s, svg3_id = %s, svg4_id = %s, svg5_id = %s " |
|
| 694 | " WHERE id = %s ") |
|
| 695 | cursor.execute(update_row, (name, |
|
| 696 | address, |
|
| 697 | postal_code, |
|
| 698 | latitude, |
|
| 699 | longitude, |
|
| 700 | rated_capacity, |
|
| 701 | rated_power, |
|
| 702 | contact_id, |
|
| 703 | cost_center_id, |
|
| 704 | svg_id, |
|
| 705 | is_cost_data_displayed, |
|
| 706 | phase_of_lifecycle, |
|
| 707 | description, |
|
| 708 | latitude_point_id, |
|
| 709 | longitude_point_id, |
|
| 710 | svg2_id, |
|
| 711 | svg3_id, |
|
| 712 | svg4_id, |
|
| 713 | svg5_id, |
|
| 714 | id_)) |
|
| 715 | cnx.commit() |
|
| 716 | ||
| 717 | cursor.close() |
|
| 718 | cnx.close() |
|
| 719 | ||
| 720 | resp.status = falcon.HTTP_200 |
|
| 721 | ||
| 722 | ||
| 723 | class HybridPowerStationBMSCollection: |
|
| @@ 342-719 (lines=378) @@ | ||
| 339 | resp.location = '/energystoragepowerstations/' + str(new_id) |
|
| 340 | ||
| 341 | ||
| 342 | class EnergyStoragePowerStationItem: |
|
| 343 | def __init__(self): |
|
| 344 | """"Initializes Class""" |
|
| 345 | pass |
|
| 346 | ||
| 347 | @staticmethod |
|
| 348 | def on_options(req, resp, id_): |
|
| 349 | resp.status = falcon.HTTP_200 |
|
| 350 | ||
| 351 | @staticmethod |
|
| 352 | def on_get(req, resp, id_): |
|
| 353 | access_control(req) |
|
| 354 | if not id_.isdigit() or int(id_) <= 0: |
|
| 355 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 356 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 357 | ||
| 358 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 359 | cursor = cnx.cursor() |
|
| 360 | ||
| 361 | contact_dict = dict() |
|
| 362 | query = (" SELECT id, name, uuid " |
|
| 363 | " FROM tbl_contacts ") |
|
| 364 | cursor.execute(query) |
|
| 365 | rows_contacts = cursor.fetchall() |
|
| 366 | if rows_contacts is not None and len(rows_contacts) > 0: |
|
| 367 | for row in rows_contacts: |
|
| 368 | contact_dict[row[0]] = {"id": row[0], |
|
| 369 | "name": row[1], |
|
| 370 | "uuid": row[2]} |
|
| 371 | ||
| 372 | cost_center_dict = dict() |
|
| 373 | query = (" SELECT id, name, uuid " |
|
| 374 | " FROM tbl_cost_centers ") |
|
| 375 | cursor.execute(query) |
|
| 376 | rows_cost_centers = cursor.fetchall() |
|
| 377 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
| 378 | for row in rows_cost_centers: |
|
| 379 | cost_center_dict[row[0]] = {"id": row[0], |
|
| 380 | "name": row[1], |
|
| 381 | "uuid": row[2]} |
|
| 382 | ||
| 383 | svg_dict = dict() |
|
| 384 | query = (" SELECT id, name, uuid " |
|
| 385 | " FROM tbl_svgs ") |
|
| 386 | cursor.execute(query) |
|
| 387 | rows_svgs = cursor.fetchall() |
|
| 388 | if rows_svgs is not None and len(rows_svgs) > 0: |
|
| 389 | for row in rows_svgs: |
|
| 390 | svg_dict[row[0]] = {"id": row[0], |
|
| 391 | "name": row[1], |
|
| 392 | "uuid": row[2]} |
|
| 393 | ||
| 394 | # query point dict |
|
| 395 | point_dict = dict() |
|
| 396 | query = (" SELECT id, name " |
|
| 397 | " FROM tbl_points ") |
|
| 398 | cursor.execute(query) |
|
| 399 | rows_points = cursor.fetchall() |
|
| 400 | if rows_points is not None and len(rows_points) > 0: |
|
| 401 | for row in rows_points: |
|
| 402 | point_dict[row[0]] = {"id": row[0], |
|
| 403 | "name": row[1]} |
|
| 404 | ||
| 405 | query = (" SELECT id, name, uuid, " |
|
| 406 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 407 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 408 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id " |
|
| 409 | " FROM tbl_energy_storage_power_stations " |
|
| 410 | " WHERE id = %s ") |
|
| 411 | cursor.execute(query, (id_,)) |
|
| 412 | row = cursor.fetchone() |
|
| 413 | cursor.close() |
|
| 414 | cnx.close() |
|
| 415 | ||
| 416 | if row is None: |
|
| 417 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 418 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 419 | else: |
|
| 420 | meta_result = {"id": row[0], |
|
| 421 | "name": row[1], |
|
| 422 | "uuid": row[2], |
|
| 423 | "address": row[3], |
|
| 424 | "postal_code": row[4], |
|
| 425 | "latitude": row[5], |
|
| 426 | "longitude": row[6], |
|
| 427 | "rated_capacity": row[7], |
|
| 428 | "rated_power": row[8], |
|
| 429 | "contact": contact_dict.get(row[9], None), |
|
| 430 | "cost_center": cost_center_dict.get(row[10], None), |
|
| 431 | "svg": svg_dict.get(row[11], None), |
|
| 432 | "is_cost_data_displayed": bool(row[12]), |
|
| 433 | "phase_of_lifecycle": row[13], |
|
| 434 | "description": row[14], |
|
| 435 | "latitude_point": point_dict.get(row[15], None), |
|
| 436 | "longitude_point": point_dict.get(row[16], None), |
|
| 437 | "svg2": svg_dict.get(row[17], None), |
|
| 438 | "svg3": svg_dict.get(row[18], None), |
|
| 439 | "svg4": svg_dict.get(row[19], None), |
|
| 440 | "svg5": svg_dict.get(row[20], None), |
|
| 441 | "qrcode": 'energystoragepowerstation:' + row[2]} |
|
| 442 | ||
| 443 | resp.text = json.dumps(meta_result) |
|
| 444 | ||
| 445 | @staticmethod |
|
| 446 | @user_logger |
|
| 447 | def on_delete(req, resp, id_): |
|
| 448 | admin_control(req) |
|
| 449 | if not id_.isdigit() or int(id_) <= 0: |
|
| 450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 451 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 452 | ||
| 453 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 454 | cursor = cnx.cursor() |
|
| 455 | ||
| 456 | cursor.execute(" SELECT name " |
|
| 457 | " FROM tbl_energy_storage_power_stations " |
|
| 458 | " WHERE id = %s ", (id_,)) |
|
| 459 | if cursor.fetchone() is None: |
|
| 460 | cursor.close() |
|
| 461 | cnx.close() |
|
| 462 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 463 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 464 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
|
| 465 | " WHERE energy_storage_power_station_id = %s ", (id_,)) |
|
| 466 | ||
| 467 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations " |
|
| 468 | " WHERE id = %s ", (id_,)) |
|
| 469 | cnx.commit() |
|
| 470 | ||
| 471 | cursor.close() |
|
| 472 | cnx.close() |
|
| 473 | ||
| 474 | resp.status = falcon.HTTP_204 |
|
| 475 | ||
| 476 | @staticmethod |
|
| 477 | @user_logger |
|
| 478 | def on_put(req, resp, id_): |
|
| 479 | """Handles PUT requests""" |
|
| 480 | admin_control(req) |
|
| 481 | try: |
|
| 482 | raw_json = req.stream.read().decode('utf-8') |
|
| 483 | except Exception as ex: |
|
| 484 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 485 | title='API.BAD_REQUEST', |
|
| 486 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 487 | ||
| 488 | if not id_.isdigit() or int(id_) <= 0: |
|
| 489 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 490 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 491 | ||
| 492 | new_values = json.loads(raw_json) |
|
| 493 | ||
| 494 | if 'name' not in new_values['data'].keys() or \ |
|
| 495 | not isinstance(new_values['data']['name'], str) or \ |
|
| 496 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 497 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 498 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
|
| 499 | name = str.strip(new_values['data']['name']) |
|
| 500 | ||
| 501 | if 'address' not in new_values['data'].keys() or \ |
|
| 502 | not isinstance(new_values['data']['address'], str) or \ |
|
| 503 | len(str.strip(new_values['data']['address'])) == 0: |
|
| 504 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 505 | description='API.INVALID_ADDRESS_VALUE') |
|
| 506 | address = str.strip(new_values['data']['address']) |
|
| 507 | ||
| 508 | if 'postal_code' not in new_values['data'].keys() or \ |
|
| 509 | not isinstance(new_values['data']['postal_code'], str) or \ |
|
| 510 | len(str.strip(new_values['data']['postal_code'])) == 0: |
|
| 511 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 512 | description='API.INVALID_POSTAL_CODE_VALUE') |
|
| 513 | postal_code = str.strip(new_values['data']['postal_code']) |
|
| 514 | ||
| 515 | if 'latitude' not in new_values['data'].keys() or \ |
|
| 516 | not (isinstance(new_values['data']['latitude'], float) or |
|
| 517 | isinstance(new_values['data']['latitude'], int)) or \ |
|
| 518 | new_values['data']['latitude'] < -90.0 or \ |
|
| 519 | new_values['data']['latitude'] > 90.0: |
|
| 520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 521 | description='API.INVALID_LATITUDE_VALUE') |
|
| 522 | latitude = new_values['data']['latitude'] |
|
| 523 | ||
| 524 | if 'longitude' not in new_values['data'].keys() or \ |
|
| 525 | not (isinstance(new_values['data']['longitude'], float) or |
|
| 526 | isinstance(new_values['data']['longitude'], int)) or \ |
|
| 527 | new_values['data']['longitude'] < -180.0 or \ |
|
| 528 | new_values['data']['longitude'] > 180.0: |
|
| 529 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 530 | description='API.INVALID_LONGITUDE_VALUE') |
|
| 531 | longitude = new_values['data']['longitude'] |
|
| 532 | ||
| 533 | if 'rated_capacity' not in new_values['data'].keys() or \ |
|
| 534 | not (isinstance(new_values['data']['rated_capacity'], float) or |
|
| 535 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
|
| 536 | new_values['data']['rated_capacity'] < 0.0: |
|
| 537 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 538 | description='API.INVALID_RATED_CAPACITY') |
|
| 539 | rated_capacity = new_values['data']['rated_capacity'] |
|
| 540 | ||
| 541 | if 'rated_power' not in new_values['data'].keys() or \ |
|
| 542 | not (isinstance(new_values['data']['rated_power'], float) or |
|
| 543 | isinstance(new_values['data']['rated_power'], int)) or \ |
|
| 544 | new_values['data']['rated_power'] < 0.0: |
|
| 545 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 546 | description='API.INVALID_RATED_POWER') |
|
| 547 | rated_power = new_values['data']['rated_power'] |
|
| 548 | ||
| 549 | if 'contact_id' not in new_values['data'].keys() or \ |
|
| 550 | not isinstance(new_values['data']['contact_id'], int) or \ |
|
| 551 | new_values['data']['contact_id'] <= 0: |
|
| 552 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 553 | description='API.INVALID_CONTACT_ID') |
|
| 554 | contact_id = new_values['data']['contact_id'] |
|
| 555 | ||
| 556 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 557 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 558 | new_values['data']['cost_center_id'] <= 0: |
|
| 559 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 560 | description='API.INVALID_COST_CENTER_ID') |
|
| 561 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 562 | ||
| 563 | if 'svg_id' not in new_values['data'].keys() or \ |
|
| 564 | not isinstance(new_values['data']['svg_id'], int) or \ |
|
| 565 | new_values['data']['svg_id'] <= 0: |
|
| 566 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 567 | description='API.INVALID_SVG_ID') |
|
| 568 | svg_id = new_values['data']['svg_id'] |
|
| 569 | ||
| 570 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
|
| 571 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
|
| 572 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 573 | description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE') |
|
| 574 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
|
| 575 | ||
| 576 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
|
| 577 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
|
| 578 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
|
| 579 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 580 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
|
| 581 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
|
| 582 | ||
| 583 | if 'description' in new_values['data'].keys() and \ |
|
| 584 | new_values['data']['description'] is not None and \ |
|
| 585 | len(str(new_values['data']['description'])) > 0: |
|
| 586 | description = str.strip(new_values['data']['description']) |
|
| 587 | else: |
|
| 588 | description = None |
|
| 589 | ||
| 590 | if 'latitude_point_id' in new_values['data'].keys() and \ |
|
| 591 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
|
| 592 | new_values['data']['latitude_point_id'] > 0: |
|
| 593 | latitude_point_id = new_values['data']['latitude_point_id'] |
|
| 594 | else: |
|
| 595 | latitude_point_id = None |
|
| 596 | ||
| 597 | if 'longitude_point_id' in new_values['data'].keys() and \ |
|
| 598 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
|
| 599 | new_values['data']['longitude_point_id'] > 0: |
|
| 600 | longitude_point_id = new_values['data']['longitude_point_id'] |
|
| 601 | else: |
|
| 602 | longitude_point_id = None |
|
| 603 | ||
| 604 | if 'svg2_id' in new_values['data'].keys() and \ |
|
| 605 | isinstance(new_values['data']['svg2_id'], int) and \ |
|
| 606 | new_values['data']['svg2_id'] > 0: |
|
| 607 | svg2_id = new_values['data']['svg2_id'] |
|
| 608 | else: |
|
| 609 | svg2_id = None |
|
| 610 | ||
| 611 | if 'svg3_id' in new_values['data'].keys() and \ |
|
| 612 | isinstance(new_values['data']['svg3_id'], int) and \ |
|
| 613 | new_values['data']['svg3_id'] > 0: |
|
| 614 | svg3_id = new_values['data']['svg3_id'] |
|
| 615 | else: |
|
| 616 | svg3_id = None |
|
| 617 | ||
| 618 | if 'svg4_id' in new_values['data'].keys() and \ |
|
| 619 | isinstance(new_values['data']['svg4_id'], int) and \ |
|
| 620 | new_values['data']['svg4_id'] > 0: |
|
| 621 | svg4_id = new_values['data']['svg4_id'] |
|
| 622 | else: |
|
| 623 | svg4_id = None |
|
| 624 | ||
| 625 | if 'svg5_id' in new_values['data'].keys() and \ |
|
| 626 | isinstance(new_values['data']['svg5_id'], int) and \ |
|
| 627 | new_values['data']['svg5_id'] > 0: |
|
| 628 | svg5_id = new_values['data']['svg5_id'] |
|
| 629 | else: |
|
| 630 | svg5_id = None |
|
| 631 | ||
| 632 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 633 | cursor = cnx.cursor() |
|
| 634 | ||
| 635 | cursor.execute(" SELECT name " |
|
| 636 | " FROM tbl_energy_storage_power_stations " |
|
| 637 | " WHERE id = %s ", (id_,)) |
|
| 638 | if cursor.fetchone() is None: |
|
| 639 | cursor.close() |
|
| 640 | cnx.close() |
|
| 641 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 642 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 643 | ||
| 644 | cursor.execute(" SELECT name " |
|
| 645 | " FROM tbl_energy_storage_power_stations " |
|
| 646 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
| 647 | if cursor.fetchone() is not None: |
|
| 648 | cursor.close() |
|
| 649 | cnx.close() |
|
| 650 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 651 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
|
| 652 | ||
| 653 | cursor.execute(" SELECT name " |
|
| 654 | " FROM tbl_contacts " |
|
| 655 | " WHERE id = %s ", |
|
| 656 | (new_values['data']['contact_id'],)) |
|
| 657 | row = cursor.fetchone() |
|
| 658 | if row is None: |
|
| 659 | cursor.close() |
|
| 660 | cnx.close() |
|
| 661 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 662 | description='API.CONTACT_NOT_FOUND') |
|
| 663 | ||
| 664 | cursor.execute(" SELECT name " |
|
| 665 | " FROM tbl_cost_centers " |
|
| 666 | " WHERE id = %s ", |
|
| 667 | (new_values['data']['cost_center_id'],)) |
|
| 668 | row = cursor.fetchone() |
|
| 669 | if row is None: |
|
| 670 | cursor.close() |
|
| 671 | cnx.close() |
|
| 672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 673 | description='API.COST_CENTER_NOT_FOUND') |
|
| 674 | ||
| 675 | cursor.execute(" SELECT name " |
|
| 676 | " FROM tbl_svgs " |
|
| 677 | " WHERE id = %s ", |
|
| 678 | (new_values['data']['svg_id'],)) |
|
| 679 | row = cursor.fetchone() |
|
| 680 | if row is None: |
|
| 681 | cursor.close() |
|
| 682 | cnx.close() |
|
| 683 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 684 | description='API.SVG_NOT_FOUND') |
|
| 685 | ||
| 686 | update_row = (" UPDATE tbl_energy_storage_power_stations " |
|
| 687 | " SET name = %s, address = %s, postal_code = %s, latitude = %s, longitude = %s, " |
|
| 688 | " rated_capacity = %s, rated_power = %s, " |
|
| 689 | " contact_id = %s, cost_center_id = %s, " |
|
| 690 | " svg_id = %s, is_cost_data_displayed = %s, phase_of_lifecycle = %s, description = %s, " |
|
| 691 | " latitude_point_id = %s, longitude_point_id = %s, " |
|
| 692 | " svg2_id = %s, svg3_id = %s, svg4_id = %s, svg5_id = %s " |
|
| 693 | " WHERE id = %s ") |
|
| 694 | cursor.execute(update_row, (name, |
|
| 695 | address, |
|
| 696 | postal_code, |
|
| 697 | latitude, |
|
| 698 | longitude, |
|
| 699 | rated_capacity, |
|
| 700 | rated_power, |
|
| 701 | contact_id, |
|
| 702 | cost_center_id, |
|
| 703 | svg_id, |
|
| 704 | is_cost_data_displayed, |
|
| 705 | phase_of_lifecycle, |
|
| 706 | description, |
|
| 707 | latitude_point_id, |
|
| 708 | longitude_point_id, |
|
| 709 | svg2_id, |
|
| 710 | svg3_id, |
|
| 711 | svg4_id, |
|
| 712 | svg5_id, |
|
| 713 | id_)) |
|
| 714 | cnx.commit() |
|
| 715 | ||
| 716 | cursor.close() |
|
| 717 | cnx.close() |
|
| 718 | ||
| 719 | resp.status = falcon.HTTP_200 |
|
| 720 | ||
| 721 | ||
| 722 | class EnergyStoragePowerStationContainerCollection: |
|