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