@@ 622-941 (lines=320) @@ | ||
619 | resp.location = '/equipments/' + str(new_id) |
|
620 | ||
621 | ||
622 | class EquipmentParameterCollection: |
|
623 | def __init__(self): |
|
624 | """Initializes EquipmentParameterCollection""" |
|
625 | pass |
|
626 | ||
627 | @staticmethod |
|
628 | def on_options(req, resp, id_): |
|
629 | _ = req |
|
630 | resp.status = falcon.HTTP_200 |
|
631 | _ = id_ |
|
632 | ||
633 | @staticmethod |
|
634 | def on_get(req, resp, id_): |
|
635 | if 'API-KEY' not in req.headers or \ |
|
636 | not isinstance(req.headers['API-KEY'], str) or \ |
|
637 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
638 | access_control(req) |
|
639 | else: |
|
640 | api_key_control(req) |
|
641 | if not id_.isdigit() or int(id_) <= 0: |
|
642 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
643 | description='API.INVALID_EQUIPMENT_ID') |
|
644 | ||
645 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
646 | cursor = cnx.cursor() |
|
647 | ||
648 | cursor.execute(" SELECT name " |
|
649 | " FROM tbl_equipments " |
|
650 | " WHERE id = %s ", (id_,)) |
|
651 | if cursor.fetchone() is None: |
|
652 | cursor.close() |
|
653 | cnx.close() |
|
654 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
655 | description='API.EQUIPMENT_NOT_FOUND') |
|
656 | ||
657 | query = (" SELECT id, name " |
|
658 | " FROM tbl_points ") |
|
659 | cursor.execute(query) |
|
660 | rows_points = cursor.fetchall() |
|
661 | ||
662 | point_dict = dict() |
|
663 | if rows_points is not None and len(rows_points) > 0: |
|
664 | for row in rows_points: |
|
665 | point_dict[row[0]] = {"id": row[0], |
|
666 | "name": row[1]} |
|
667 | ||
668 | query = (" SELECT id, name, uuid " |
|
669 | " FROM tbl_meters ") |
|
670 | cursor.execute(query) |
|
671 | rows_meters = cursor.fetchall() |
|
672 | ||
673 | meter_dict = dict() |
|
674 | if rows_meters is not None and len(rows_meters) > 0: |
|
675 | for row in rows_meters: |
|
676 | meter_dict[row[2]] = {"type": 'meter', |
|
677 | "id": row[0], |
|
678 | "name": row[1], |
|
679 | "uuid": row[2]} |
|
680 | ||
681 | query = (" SELECT id, name, uuid " |
|
682 | " FROM tbl_offline_meters ") |
|
683 | cursor.execute(query) |
|
684 | rows_offline_meters = cursor.fetchall() |
|
685 | ||
686 | offline_meter_dict = dict() |
|
687 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
688 | for row in rows_offline_meters: |
|
689 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
690 | "id": row[0], |
|
691 | "name": row[1], |
|
692 | "uuid": row[2]} |
|
693 | ||
694 | query = (" SELECT id, name, uuid " |
|
695 | " FROM tbl_virtual_meters ") |
|
696 | cursor.execute(query) |
|
697 | rows_virtual_meters = cursor.fetchall() |
|
698 | ||
699 | virtual_meter_dict = dict() |
|
700 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
701 | for row in rows_virtual_meters: |
|
702 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
703 | "id": row[0], |
|
704 | "name": row[1], |
|
705 | "uuid": row[2]} |
|
706 | ||
707 | query = (" SELECT id, name, parameter_type, " |
|
708 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
709 | " FROM tbl_equipments_parameters " |
|
710 | " WHERE equipment_id = %s " |
|
711 | " ORDER BY id ") |
|
712 | cursor.execute(query, (id_, )) |
|
713 | rows_parameters = cursor.fetchall() |
|
714 | ||
715 | result = list() |
|
716 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
717 | for row in rows_parameters: |
|
718 | constant = None |
|
719 | point = None |
|
720 | numerator_meter = None |
|
721 | denominator_meter = None |
|
722 | if row[2] == 'point': |
|
723 | point = point_dict.get(row[4], None) |
|
724 | constant = None |
|
725 | numerator_meter = None |
|
726 | denominator_meter = None |
|
727 | elif row[2] == 'constant': |
|
728 | constant = row[3] |
|
729 | point = None |
|
730 | numerator_meter = None |
|
731 | denominator_meter = None |
|
732 | elif row[2] == 'fraction': |
|
733 | constant = None |
|
734 | point = None |
|
735 | # find numerator meter by uuid |
|
736 | numerator_meter = meter_dict.get(row[5], None) |
|
737 | if numerator_meter is None: |
|
738 | numerator_meter = virtual_meter_dict.get(row[5], None) |
|
739 | if numerator_meter is None: |
|
740 | numerator_meter = offline_meter_dict.get(row[5], None) |
|
741 | # find denominator meter by uuid |
|
742 | denominator_meter = meter_dict.get(row[6], None) |
|
743 | if denominator_meter is None: |
|
744 | denominator_meter = virtual_meter_dict.get(row[6], None) |
|
745 | if denominator_meter is None: |
|
746 | denominator_meter = offline_meter_dict.get(row[6], None) |
|
747 | ||
748 | meta_result = {"id": row[0], |
|
749 | "name": row[1], |
|
750 | "parameter_type": row[2], |
|
751 | "constant": constant, |
|
752 | "point": point, |
|
753 | "numerator_meter": numerator_meter, |
|
754 | "denominator_meter": denominator_meter} |
|
755 | result.append(meta_result) |
|
756 | ||
757 | cursor.close() |
|
758 | cnx.close() |
|
759 | resp.text = json.dumps(result) |
|
760 | ||
761 | @staticmethod |
|
762 | @user_logger |
|
763 | def on_post(req, resp, id_): |
|
764 | """Handles POST requests""" |
|
765 | admin_control(req) |
|
766 | if not id_.isdigit() or int(id_) <= 0: |
|
767 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
768 | description='API.INVALID_EQUIPMENT_ID') |
|
769 | try: |
|
770 | raw_json = req.stream.read().decode('utf-8') |
|
771 | except Exception as ex: |
|
772 | print(str(ex)) |
|
773 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
774 | title='API.BAD_REQUEST', |
|
775 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
776 | ||
777 | new_values = json.loads(raw_json) |
|
778 | ||
779 | if 'name' not in new_values['data'].keys() or \ |
|
780 | not isinstance(new_values['data']['name'], str) or \ |
|
781 | len(str.strip(new_values['data']['name'])) == 0: |
|
782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
783 | description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
|
784 | name = str.strip(new_values['data']['name']) |
|
785 | ||
786 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
787 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
788 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
789 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
790 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
791 | ||
792 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
793 | ||
794 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
796 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
797 | ||
798 | constant = None |
|
799 | if 'constant' in new_values['data'].keys(): |
|
800 | if new_values['data']['constant'] is not None and \ |
|
801 | isinstance(new_values['data']['constant'], str) and \ |
|
802 | len(str.strip(new_values['data']['constant'])) > 0: |
|
803 | constant = str.strip(new_values['data']['constant']) |
|
804 | ||
805 | point_id = None |
|
806 | if 'point_id' in new_values['data'].keys(): |
|
807 | if new_values['data']['point_id'] is not None and \ |
|
808 | new_values['data']['point_id'] <= 0: |
|
809 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
810 | description='API.INVALID_POINT_ID') |
|
811 | point_id = new_values['data']['point_id'] |
|
812 | ||
813 | numerator_meter_uuid = None |
|
814 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
815 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
816 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
817 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
818 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
819 | ||
820 | denominator_meter_uuid = None |
|
821 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
822 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
823 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
824 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
825 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
826 | ||
827 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
828 | cursor = cnx.cursor() |
|
829 | cursor.execute(" SELECT name " |
|
830 | " FROM tbl_equipments " |
|
831 | " WHERE id = %s ", (id_,)) |
|
832 | if cursor.fetchone() is None: |
|
833 | cursor.close() |
|
834 | cnx.close() |
|
835 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
|
836 | description='API.EQUIPMENT_NOT_FOUND') |
|
837 | ||
838 | cursor.execute(" SELECT name " |
|
839 | " FROM tbl_equipments_parameters " |
|
840 | " WHERE name = %s AND equipment_id = %s ", (name, id_)) |
|
841 | if cursor.fetchone() is not None: |
|
842 | cursor.close() |
|
843 | cnx.close() |
|
844 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
845 | description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
846 | ||
847 | # validate by parameter type |
|
848 | if parameter_type == 'point': |
|
849 | if point_id is None: |
|
850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
851 | description='API.INVALID_POINT_ID') |
|
852 | query = (" SELECT id, name " |
|
853 | " FROM tbl_points " |
|
854 | " WHERE id = %s ") |
|
855 | cursor.execute(query, (point_id, )) |
|
856 | if cursor.fetchone() is None: |
|
857 | cursor.close() |
|
858 | cnx.close() |
|
859 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
860 | description='API.POINT_NOT_FOUND') |
|
861 | ||
862 | elif parameter_type == 'constant': |
|
863 | if constant is None: |
|
864 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
865 | description='API.INVALID_CONSTANT_VALUE') |
|
866 | ||
867 | elif parameter_type == 'fraction': |
|
868 | query = (" SELECT id, name, uuid " |
|
869 | " FROM tbl_meters ") |
|
870 | cursor.execute(query) |
|
871 | rows_meters = cursor.fetchall() |
|
872 | meter_dict = dict() |
|
873 | if rows_meters is not None and len(rows_meters) > 0: |
|
874 | for row in rows_meters: |
|
875 | meter_dict[row[2]] = {"type": 'meter', |
|
876 | "id": row[0], |
|
877 | "name": row[1], |
|
878 | "uuid": row[2]} |
|
879 | ||
880 | query = (" SELECT id, name, uuid " |
|
881 | " FROM tbl_offline_meters ") |
|
882 | cursor.execute(query) |
|
883 | rows_offline_meters = cursor.fetchall() |
|
884 | ||
885 | offline_meter_dict = dict() |
|
886 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
887 | for row in rows_offline_meters: |
|
888 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
889 | "id": row[0], |
|
890 | "name": row[1], |
|
891 | "uuid": row[2]} |
|
892 | ||
893 | query = (" SELECT id, name, uuid " |
|
894 | " FROM tbl_virtual_meters ") |
|
895 | cursor.execute(query) |
|
896 | rows_virtual_meters = cursor.fetchall() |
|
897 | ||
898 | virtual_meter_dict = dict() |
|
899 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
900 | for row in rows_virtual_meters: |
|
901 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
902 | "id": row[0], |
|
903 | "name": row[1], |
|
904 | "uuid": row[2]} |
|
905 | ||
906 | # validate numerator meter uuid |
|
907 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
908 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
909 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
910 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
911 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
912 | ||
913 | # validate denominator meter uuid |
|
914 | if denominator_meter_uuid == numerator_meter_uuid: |
|
915 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
916 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
917 | ||
918 | if denominator_meter_uuid not in meter_dict and \ |
|
919 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
920 | denominator_meter_uuid not in offline_meter_dict: |
|
921 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
922 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
923 | ||
924 | add_values = (" INSERT INTO tbl_equipments_parameters " |
|
925 | " (equipment_id, name, parameter_type, constant, " |
|
926 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
|
927 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
|
928 | cursor.execute(add_values, (id_, |
|
929 | name, |
|
930 | parameter_type, |
|
931 | constant, |
|
932 | point_id, |
|
933 | numerator_meter_uuid, |
|
934 | denominator_meter_uuid)) |
|
935 | new_id = cursor.lastrowid |
|
936 | cnx.commit() |
|
937 | cursor.close() |
|
938 | cnx.close() |
|
939 | ||
940 | resp.status = falcon.HTTP_201 |
|
941 | resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) |
|
942 | ||
943 | ||
944 | class EquipmentParameterItem: |
@@ 784-1116 (lines=333) @@ | ||
781 | resp.status = falcon.HTTP_204 |
|
782 | ||
783 | ||
784 | class CombinedEquipmentParameterCollection: |
|
785 | def __init__(self): |
|
786 | """Initializes CombinedEquipmentParameterCollection""" |
|
787 | pass |
|
788 | ||
789 | @staticmethod |
|
790 | def on_options(req, resp, id_): |
|
791 | _ = req |
|
792 | resp.status = falcon.HTTP_200 |
|
793 | _ = id_ |
|
794 | ||
795 | @staticmethod |
|
796 | def on_get(req, resp, id_): |
|
797 | if 'API-KEY' not in req.headers or \ |
|
798 | not isinstance(req.headers['API-KEY'], str) or \ |
|
799 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
800 | access_control(req) |
|
801 | else: |
|
802 | api_key_control(req) |
|
803 | if not id_.isdigit() or int(id_) <= 0: |
|
804 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
805 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
806 | ||
807 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
808 | cursor = cnx.cursor() |
|
809 | ||
810 | cursor.execute(" SELECT name " |
|
811 | " FROM tbl_combined_equipments " |
|
812 | " WHERE id = %s ", (id_,)) |
|
813 | if cursor.fetchone() is None: |
|
814 | cursor.close() |
|
815 | cnx.close() |
|
816 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
817 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
818 | ||
819 | query = (" SELECT id, name " |
|
820 | " FROM tbl_points ") |
|
821 | cursor.execute(query) |
|
822 | rows_points = cursor.fetchall() |
|
823 | ||
824 | point_dict = dict() |
|
825 | if rows_points is not None and len(rows_points) > 0: |
|
826 | for row in rows_points: |
|
827 | point_dict[row[0]] = {"id": row[0], |
|
828 | "name": row[1]} |
|
829 | ||
830 | query = (" SELECT id, name, uuid " |
|
831 | " FROM tbl_meters ") |
|
832 | cursor.execute(query) |
|
833 | rows_meters = cursor.fetchall() |
|
834 | ||
835 | meter_dict = dict() |
|
836 | if rows_meters is not None and len(rows_meters) > 0: |
|
837 | for row in rows_meters: |
|
838 | meter_dict[row[2]] = {"type": 'meter', |
|
839 | "id": row[0], |
|
840 | "name": row[1], |
|
841 | "uuid": row[2]} |
|
842 | ||
843 | query = (" SELECT id, name, uuid " |
|
844 | " FROM tbl_offline_meters ") |
|
845 | cursor.execute(query) |
|
846 | rows_offline_meters = cursor.fetchall() |
|
847 | ||
848 | offline_meter_dict = dict() |
|
849 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
850 | for row in rows_offline_meters: |
|
851 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
852 | "id": row[0], |
|
853 | "name": row[1], |
|
854 | "uuid": row[2]} |
|
855 | ||
856 | query = (" SELECT id, name, uuid " |
|
857 | " FROM tbl_virtual_meters ") |
|
858 | cursor.execute(query) |
|
859 | rows_virtual_meters = cursor.fetchall() |
|
860 | ||
861 | virtual_meter_dict = dict() |
|
862 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
863 | for row in rows_virtual_meters: |
|
864 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
865 | "id": row[0], |
|
866 | "name": row[1], |
|
867 | "uuid": row[2]} |
|
868 | ||
869 | query = (" SELECT id, name, parameter_type, " |
|
870 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
871 | " FROM tbl_combined_equipments_parameters " |
|
872 | " WHERE combined_equipment_id = %s " |
|
873 | " ORDER BY id ") |
|
874 | cursor.execute(query, (id_,)) |
|
875 | rows_parameters = cursor.fetchall() |
|
876 | ||
877 | result = list() |
|
878 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
879 | for row in rows_parameters: |
|
880 | constant = None |
|
881 | point = None |
|
882 | numerator_meter = None |
|
883 | denominator_meter = None |
|
884 | if row[2] == 'point': |
|
885 | point = point_dict.get(row[4], None) |
|
886 | constant = None |
|
887 | numerator_meter = None |
|
888 | denominator_meter = None |
|
889 | elif row[2] == 'constant': |
|
890 | constant = row[3] |
|
891 | point = None |
|
892 | numerator_meter = None |
|
893 | denominator_meter = None |
|
894 | elif row[2] == 'fraction': |
|
895 | constant = None |
|
896 | point = None |
|
897 | # find numerator meter by uuid |
|
898 | numerator_meter = meter_dict.get(row[5], None) |
|
899 | if numerator_meter is None: |
|
900 | numerator_meter = virtual_meter_dict.get(row[5], None) |
|
901 | if numerator_meter is None: |
|
902 | numerator_meter = offline_meter_dict.get(row[5], None) |
|
903 | # find denominator meter by uuid |
|
904 | denominator_meter = meter_dict.get(row[6], None) |
|
905 | if denominator_meter is None: |
|
906 | denominator_meter = virtual_meter_dict.get(row[6], None) |
|
907 | if denominator_meter is None: |
|
908 | denominator_meter = offline_meter_dict.get(row[6], None) |
|
909 | ||
910 | meta_result = {"id": row[0], |
|
911 | "name": row[1], |
|
912 | "parameter_type": row[2], |
|
913 | "constant": constant, |
|
914 | "point": point, |
|
915 | "numerator_meter": numerator_meter, |
|
916 | "denominator_meter": denominator_meter} |
|
917 | result.append(meta_result) |
|
918 | ||
919 | cursor.close() |
|
920 | cnx.close() |
|
921 | resp.text = json.dumps(result) |
|
922 | ||
923 | @staticmethod |
|
924 | @user_logger |
|
925 | def on_post(req, resp, id_): |
|
926 | """Handles POST requests""" |
|
927 | admin_control(req) |
|
928 | if not id_.isdigit() or int(id_) <= 0: |
|
929 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
930 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
931 | try: |
|
932 | raw_json = req.stream.read().decode('utf-8') |
|
933 | except Exception as ex: |
|
934 | print(ex) |
|
935 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
936 | title='API.BAD_REQUEST', |
|
937 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
938 | ||
939 | new_values = json.loads(raw_json) |
|
940 | ||
941 | if 'name' not in new_values['data'].keys() or \ |
|
942 | not isinstance(new_values['data']['name'], str) or \ |
|
943 | len(str.strip(new_values['data']['name'])) == 0: |
|
944 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
945 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
|
946 | name = str.strip(new_values['data']['name']) |
|
947 | ||
948 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
949 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
950 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
951 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
952 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
953 | ||
954 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
955 | ||
956 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
957 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
958 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
959 | ||
960 | constant = None |
|
961 | if 'constant' in new_values['data'].keys(): |
|
962 | if new_values['data']['constant'] is not None and \ |
|
963 | isinstance(new_values['data']['constant'], str) and \ |
|
964 | len(str.strip(new_values['data']['constant'])) > 0: |
|
965 | constant = str.strip(new_values['data']['constant']) |
|
966 | ||
967 | point_id = None |
|
968 | if 'point_id' in new_values['data'].keys(): |
|
969 | if new_values['data']['point_id'] is not None and \ |
|
970 | new_values['data']['point_id'] <= 0: |
|
971 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
972 | description='API.INVALID_POINT_ID') |
|
973 | point_id = new_values['data']['point_id'] |
|
974 | ||
975 | numerator_meter_uuid = None |
|
976 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
977 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
978 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
979 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
980 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
981 | ||
982 | denominator_meter_uuid = None |
|
983 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
984 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
985 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
986 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
987 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
988 | ||
989 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
990 | cursor = cnx.cursor() |
|
991 | ||
992 | cursor.execute(" SELECT name " |
|
993 | " FROM tbl_combined_equipments " |
|
994 | " WHERE id = %s ", (id_,)) |
|
995 | if cursor.fetchone() is None: |
|
996 | cursor.close() |
|
997 | cnx.close() |
|
998 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
|
999 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1000 | ||
1001 | cursor.execute(" SELECT name " |
|
1002 | " FROM tbl_combined_equipments_parameters " |
|
1003 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
|
1004 | if cursor.fetchone() is not None: |
|
1005 | cursor.close() |
|
1006 | cnx.close() |
|
1007 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1008 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
1009 | ||
1010 | # validate by parameter type |
|
1011 | if parameter_type == 'point': |
|
1012 | if point_id is None: |
|
1013 | cursor.close() |
|
1014 | cnx.close() |
|
1015 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1016 | description='API.INVALID_POINT_ID') |
|
1017 | ||
1018 | query = (" SELECT id, name " |
|
1019 | " FROM tbl_points " |
|
1020 | " WHERE id = %s ") |
|
1021 | cursor.execute(query, (point_id,)) |
|
1022 | if cursor.fetchone() is None: |
|
1023 | cursor.close() |
|
1024 | cnx.close() |
|
1025 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1026 | description='API.POINT_NOT_FOUND') |
|
1027 | ||
1028 | elif parameter_type == 'constant': |
|
1029 | if constant is None: |
|
1030 | cursor.close() |
|
1031 | cnx.close() |
|
1032 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1033 | description='API.INVALID_CONSTANT_VALUE') |
|
1034 | ||
1035 | elif parameter_type == 'fraction': |
|
1036 | ||
1037 | query = (" SELECT id, name, uuid " |
|
1038 | " FROM tbl_meters ") |
|
1039 | cursor.execute(query) |
|
1040 | rows_meters = cursor.fetchall() |
|
1041 | ||
1042 | meter_dict = dict() |
|
1043 | if rows_meters is not None and len(rows_meters) > 0: |
|
1044 | for row in rows_meters: |
|
1045 | meter_dict[row[2]] = {"type": 'meter', |
|
1046 | "id": row[0], |
|
1047 | "name": row[1], |
|
1048 | "uuid": row[2]} |
|
1049 | ||
1050 | query = (" SELECT id, name, uuid " |
|
1051 | " FROM tbl_offline_meters ") |
|
1052 | cursor.execute(query) |
|
1053 | rows_offline_meters = cursor.fetchall() |
|
1054 | ||
1055 | offline_meter_dict = dict() |
|
1056 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
1057 | for row in rows_offline_meters: |
|
1058 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
1059 | "id": row[0], |
|
1060 | "name": row[1], |
|
1061 | "uuid": row[2]} |
|
1062 | ||
1063 | query = (" SELECT id, name, uuid " |
|
1064 | " FROM tbl_virtual_meters ") |
|
1065 | cursor.execute(query) |
|
1066 | rows_virtual_meters = cursor.fetchall() |
|
1067 | ||
1068 | virtual_meter_dict = dict() |
|
1069 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1070 | for row in rows_virtual_meters: |
|
1071 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1072 | "id": row[0], |
|
1073 | "name": row[1], |
|
1074 | "uuid": row[2]} |
|
1075 | ||
1076 | # validate numerator meter uuid |
|
1077 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
1078 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
1079 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
1080 | cursor.close() |
|
1081 | cnx.close() |
|
1082 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1083 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
1084 | ||
1085 | # validate denominator meter uuid |
|
1086 | if denominator_meter_uuid == numerator_meter_uuid: |
|
1087 | cursor.close() |
|
1088 | cnx.close() |
|
1089 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1090 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1091 | ||
1092 | if denominator_meter_uuid not in meter_dict and \ |
|
1093 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
1094 | denominator_meter_uuid not in offline_meter_dict: |
|
1095 | cursor.close() |
|
1096 | cnx.close() |
|
1097 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1098 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1099 | ||
1100 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
|
1101 | " (combined_equipment_id, name, parameter_type, constant, " |
|
1102 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
|
1103 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
|
1104 | cursor.execute(add_values, (id_, |
|
1105 | name, |
|
1106 | parameter_type, |
|
1107 | constant, |
|
1108 | point_id, |
|
1109 | numerator_meter_uuid, |
|
1110 | denominator_meter_uuid)) |
|
1111 | new_id = cursor.lastrowid |
|
1112 | cnx.commit() |
|
1113 | cursor.close() |
|
1114 | cnx.close() |
|
1115 | ||
1116 | resp.status = falcon.HTTP_201 |
|
1117 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
|
1118 | ||
1119 |