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