| @@ 505-648 (lines=144) @@ | ||
| 502 | resp.status = falcon.HTTP_200 |
|
| 503 | ||
| 504 | # Clone a Combined Equipment |
|
| 505 | @staticmethod |
|
| 506 | @user_logger |
|
| 507 | def on_post(req, resp, id_): |
|
| 508 | """Handles POST requests""" |
|
| 509 | admin_control(req) |
|
| 510 | if not id_.isdigit() or int(id_) <= 0: |
|
| 511 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 512 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 513 | ||
| 514 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 515 | cursor = cnx.cursor() |
|
| 516 | cursor.execute(" SELECT name " |
|
| 517 | " FROM tbl_combined_equipments " |
|
| 518 | " WHERE id = %s ", (id_,)) |
|
| 519 | if cursor.fetchone() is None: |
|
| 520 | cursor.close() |
|
| 521 | cnx.close() |
|
| 522 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 523 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 524 | ||
| 525 | query = (" SELECT name, is_input_counted, is_output_counted, " |
|
| 526 | " cost_center_id, svg_id, camera_url, description " |
|
| 527 | " FROM tbl_combined_equipments " |
|
| 528 | " WHERE id = %s ") |
|
| 529 | cursor.execute(query, (id_,)) |
|
| 530 | row = cursor.fetchone() |
|
| 531 | ||
| 532 | if row is None: |
|
| 533 | cursor.close() |
|
| 534 | cnx.close() |
|
| 535 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 536 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 537 | else: |
|
| 538 | add_values = (" INSERT INTO tbl_combined_equipments " |
|
| 539 | " (name, uuid, is_input_counted, is_output_counted, " |
|
| 540 | " cost_center_id, svg_id, camera_url, description) " |
|
| 541 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 542 | cursor.execute(add_values, (row[0] + ' Copy', |
|
| 543 | str(uuid.uuid4()), |
|
| 544 | row[1], |
|
| 545 | row[2], |
|
| 546 | row[3], |
|
| 547 | row[4], |
|
| 548 | row[5], |
|
| 549 | row[6])) |
|
| 550 | new_id = cursor.lastrowid |
|
| 551 | cnx.commit() |
|
| 552 | ||
| 553 | # clone relation with meter |
|
| 554 | cursor.execute(" SELECT meter_id, is_output " |
|
| 555 | " FROM tbl_combined_equipments_meters " |
|
| 556 | " WHERE combined_equipment_id = %s ", |
|
| 557 | (id_,)) |
|
| 558 | rows_meters = cursor.fetchall() |
|
| 559 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 560 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
|
| 561 | " VALUES ") |
|
| 562 | for row in rows_meters: |
|
| 563 | add_values += " (" + str(new_id) + "," |
|
| 564 | add_values += str(row[0]) + "," |
|
| 565 | add_values += str(bool(row[1])) + "), " |
|
| 566 | # trim ", " at the end of string and then execute |
|
| 567 | cursor.execute(add_values[:-2]) |
|
| 568 | cnx.commit() |
|
| 569 | ||
| 570 | # clone relation with offline meter |
|
| 571 | cursor.execute(" SELECT offline_meter_id, is_output " |
|
| 572 | " FROM tbl_combined_equipments_offline_meters " |
|
| 573 | " WHERE combined_equipment_id = %s ", |
|
| 574 | (id_,)) |
|
| 575 | rows_offline_meters = cursor.fetchall() |
|
| 576 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 577 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
|
| 578 | " (combined_equipment_id, offline_meter_id, is_output) " |
|
| 579 | " VALUES ") |
|
| 580 | for row in rows_offline_meters: |
|
| 581 | add_values += " (" + str(new_id) + "," |
|
| 582 | add_values += "'" + str(row[0]) + "'," |
|
| 583 | add_values += str(bool(row[1])) + "), " |
|
| 584 | # trim ", " at the end of string and then execute |
|
| 585 | cursor.execute(add_values[:-2]) |
|
| 586 | cnx.commit() |
|
| 587 | ||
| 588 | # clone relation with virtual meter |
|
| 589 | cursor.execute(" SELECT virtual_meter_id, is_output " |
|
| 590 | " FROM tbl_combined_equipments_virtual_meters " |
|
| 591 | " WHERE combined_equipment_id = %s ", |
|
| 592 | (id_,)) |
|
| 593 | rows_virtual_meters = cursor.fetchall() |
|
| 594 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 595 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
|
| 596 | " (combined_equipment_id, virtual_meter_id, is_output) " |
|
| 597 | " VALUES ") |
|
| 598 | for row in rows_virtual_meters: |
|
| 599 | add_values += " (" + str(new_id) + "," |
|
| 600 | add_values += str(row[0]) + "," |
|
| 601 | add_values += str(bool(row[1])) + "), " |
|
| 602 | # trim ", " at the end of string and then execute |
|
| 603 | cursor.execute(add_values[:-2]) |
|
| 604 | cnx.commit() |
|
| 605 | ||
| 606 | # clone parameters |
|
| 607 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
| 608 | " FROM tbl_combined_equipments_parameters " |
|
| 609 | " WHERE combined_equipment_id = %s ", |
|
| 610 | (id_,)) |
|
| 611 | rows_parameters = cursor.fetchall() |
|
| 612 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
| 613 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
|
| 614 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
|
| 615 | " numerator_meter_uuid, denominator_meter_uuid) " |
|
| 616 | " VALUES ") |
|
| 617 | for row in rows_parameters: |
|
| 618 | add_values += " (" + str(new_id) + "," |
|
| 619 | add_values += "'" + str(row[0]) + "'," |
|
| 620 | add_values += "'" + str(row[1]) + "'," |
|
| 621 | if row[2] is not None: |
|
| 622 | add_values += "'" + str(row[2]) + "'," |
|
| 623 | else: |
|
| 624 | add_values += "null, " |
|
| 625 | ||
| 626 | if row[3] is not None: |
|
| 627 | add_values += str(row[2]) + "," |
|
| 628 | else: |
|
| 629 | add_values += "null, " |
|
| 630 | ||
| 631 | if row[4] is not None: |
|
| 632 | add_values += "'" + row[4] + "'," |
|
| 633 | else: |
|
| 634 | add_values += "null, " |
|
| 635 | if row[5] is not None: |
|
| 636 | add_values += "'" + row[5] + "'), " |
|
| 637 | else: |
|
| 638 | add_values += "null), " |
|
| 639 | ||
| 640 | # trim ", " at the end of string and then execute |
|
| 641 | cursor.execute(add_values[:-2]) |
|
| 642 | cnx.commit() |
|
| 643 | ||
| 644 | cursor.close() |
|
| 645 | cnx.close() |
|
| 646 | resp.status = falcon.HTTP_201 |
|
| 647 | resp.location = '/combinedequipments/' + str(new_id) |
|
| 648 | ||
| 649 | ||
| 650 | class CombinedEquipmentEquipmentCollection: |
|
| 651 | def __init__(self): |
|
| @@ 497-635 (lines=139) @@ | ||
| 494 | resp.status = falcon.HTTP_200 |
|
| 495 | ||
| 496 | # Clone an Equipment |
|
| 497 | @staticmethod |
|
| 498 | @user_logger |
|
| 499 | def on_post(req, resp, id_): |
|
| 500 | admin_control(req) |
|
| 501 | """Handles POST requests""" |
|
| 502 | if not id_.isdigit() or int(id_) <= 0: |
|
| 503 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 504 | description='API.INVALID_EQUIPMENT_ID') |
|
| 505 | ||
| 506 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 507 | cursor = cnx.cursor() |
|
| 508 | cursor.execute(" SELECT name " |
|
| 509 | " FROM tbl_equipments " |
|
| 510 | " WHERE id = %s ", (id_,)) |
|
| 511 | if cursor.fetchone() is None: |
|
| 512 | cursor.close() |
|
| 513 | cnx.close() |
|
| 514 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 515 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 516 | ||
| 517 | query = (" SELECT name, is_input_counted, is_output_counted, " |
|
| 518 | " cost_center_id, svg_id, camera_url, description " |
|
| 519 | " FROM tbl_equipments " |
|
| 520 | " WHERE id = %s ") |
|
| 521 | cursor.execute(query, (id_,)) |
|
| 522 | row = cursor.fetchone() |
|
| 523 | ||
| 524 | if row is None: |
|
| 525 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 526 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 527 | else: |
|
| 528 | add_values = (" INSERT INTO tbl_equipments " |
|
| 529 | " (name, uuid, is_input_counted, is_output_counted, " |
|
| 530 | " cost_center_id, svg_id, camera_url, description) " |
|
| 531 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 532 | cursor.execute(add_values, (row[0] + ' Copy', |
|
| 533 | str(uuid.uuid4()), |
|
| 534 | row[1], |
|
| 535 | row[2], |
|
| 536 | row[3], |
|
| 537 | row[4], |
|
| 538 | row[5], |
|
| 539 | row[6])) |
|
| 540 | new_id = cursor.lastrowid |
|
| 541 | cnx.commit() |
|
| 542 | ||
| 543 | # clone relation with meter |
|
| 544 | cursor.execute(" SELECT meter_id, is_output " |
|
| 545 | " FROM tbl_equipments_meters " |
|
| 546 | " WHERE equipment_id = %s ", |
|
| 547 | (id_,)) |
|
| 548 | rows_meters = cursor.fetchall() |
|
| 549 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 550 | add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) " |
|
| 551 | " VALUES ") |
|
| 552 | for row in rows_meters: |
|
| 553 | add_values += " (" + str(new_id) + "," |
|
| 554 | add_values += str(row[0]) + "," |
|
| 555 | add_values += str(bool(row[1])) + "), " |
|
| 556 | # trim ", " at the end of string and then execute |
|
| 557 | cursor.execute(add_values[:-2]) |
|
| 558 | cnx.commit() |
|
| 559 | ||
| 560 | # clone relation with offline meter |
|
| 561 | cursor.execute(" SELECT offline_meter_id, is_output " |
|
| 562 | " FROM tbl_equipments_offline_meters " |
|
| 563 | " WHERE equipment_id = %s ", |
|
| 564 | (id_,)) |
|
| 565 | rows_offline_meters = cursor.fetchall() |
|
| 566 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 567 | add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) " |
|
| 568 | " VALUES ") |
|
| 569 | for row in rows_offline_meters: |
|
| 570 | add_values += " (" + str(new_id) + "," |
|
| 571 | add_values += "'" + str(row[0]) + "'," |
|
| 572 | add_values += str(bool(row[1])) + "), " |
|
| 573 | # trim ", " at the end of string and then execute |
|
| 574 | cursor.execute(add_values[:-2]) |
|
| 575 | cnx.commit() |
|
| 576 | ||
| 577 | # clone relation with virtual meter |
|
| 578 | cursor.execute(" SELECT virtual_meter_id, is_output " |
|
| 579 | " FROM tbl_equipments_virtual_meters " |
|
| 580 | " WHERE equipment_id = %s ", |
|
| 581 | (id_,)) |
|
| 582 | rows_virtual_meters = cursor.fetchall() |
|
| 583 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 584 | add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) " |
|
| 585 | " VALUES ") |
|
| 586 | for row in rows_virtual_meters: |
|
| 587 | add_values += " (" + str(new_id) + "," |
|
| 588 | add_values += str(row[0]) + "," |
|
| 589 | add_values += str(bool(row[1])) + "), " |
|
| 590 | # trim ", " at the end of string and then execute |
|
| 591 | cursor.execute(add_values[:-2]) |
|
| 592 | cnx.commit() |
|
| 593 | ||
| 594 | # clone parameters |
|
| 595 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
| 596 | " FROM tbl_equipments_parameters " |
|
| 597 | " WHERE equipment_id = %s ", |
|
| 598 | (id_,)) |
|
| 599 | rows_parameters = cursor.fetchall() |
|
| 600 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
| 601 | add_values = (" INSERT INTO tbl_equipments_parameters" |
|
| 602 | " (equipment_id, name, parameter_type, constant, point_id, " |
|
| 603 | " numerator_meter_uuid, denominator_meter_uuid) " |
|
| 604 | " VALUES ") |
|
| 605 | for row in rows_parameters: |
|
| 606 | add_values += " (" + str(new_id) + "," |
|
| 607 | add_values += "'" + str(row[0]) + "'," |
|
| 608 | add_values += "'" + str(row[1]) + "'," |
|
| 609 | if row[2] is not None: |
|
| 610 | add_values += "'" + str(row[2]) + "'," |
|
| 611 | else: |
|
| 612 | add_values += "null, " |
|
| 613 | ||
| 614 | if row[3] is not None: |
|
| 615 | add_values += str(row[3]) + "," |
|
| 616 | else: |
|
| 617 | add_values += "null, " |
|
| 618 | ||
| 619 | if row[4] is not None: |
|
| 620 | add_values += "'" + row[4] + "'," |
|
| 621 | else: |
|
| 622 | add_values += "null, " |
|
| 623 | if row[5] is not None: |
|
| 624 | add_values += "'" + row[5] + "'), " |
|
| 625 | else: |
|
| 626 | add_values += "null), " |
|
| 627 | ||
| 628 | # trim ", " at the end of string and then execute |
|
| 629 | cursor.execute(add_values[:-2]) |
|
| 630 | cnx.commit() |
|
| 631 | ||
| 632 | cursor.close() |
|
| 633 | cnx.close() |
|
| 634 | resp.status = falcon.HTTP_201 |
|
| 635 | resp.location = '/equipments/' + str(new_id) |
|
| 636 | ||
| 637 | ||
| 638 | class EquipmentParameterCollection: |
|