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