@@ 1448-1578 (lines=131) @@ | ||
1445 | resp.status = falcon.HTTP_204 |
|
1446 | ||
1447 | ||
1448 | class TenantVirtualMeterCollection: |
|
1449 | def __init__(self): |
|
1450 | """Initializes Class""" |
|
1451 | pass |
|
1452 | ||
1453 | @staticmethod |
|
1454 | def on_options(req, resp, id_): |
|
1455 | _ = req |
|
1456 | resp.status = falcon.HTTP_200 |
|
1457 | _ = id_ |
|
1458 | ||
1459 | @staticmethod |
|
1460 | def on_get(req, resp, id_): |
|
1461 | if 'API-KEY' not in req.headers or \ |
|
1462 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1463 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1464 | access_control(req) |
|
1465 | else: |
|
1466 | api_key_control(req) |
|
1467 | if not id_.isdigit() or int(id_) <= 0: |
|
1468 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1469 | description='API.INVALID_TENANT_ID') |
|
1470 | ||
1471 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1472 | cursor = cnx.cursor() |
|
1473 | ||
1474 | cursor.execute(" SELECT name " |
|
1475 | " FROM tbl_tenants " |
|
1476 | " WHERE id = %s ", (id_,)) |
|
1477 | if cursor.fetchone() is None: |
|
1478 | cursor.close() |
|
1479 | cnx.close() |
|
1480 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1481 | description='API.TENANT_NOT_FOUND') |
|
1482 | ||
1483 | query = (" SELECT id, name, uuid " |
|
1484 | " FROM tbl_energy_categories ") |
|
1485 | cursor.execute(query) |
|
1486 | rows_energy_categories = cursor.fetchall() |
|
1487 | ||
1488 | energy_category_dict = dict() |
|
1489 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1490 | for row in rows_energy_categories: |
|
1491 | energy_category_dict[row[0]] = {"id": row[0], |
|
1492 | "name": row[1], |
|
1493 | "uuid": row[2]} |
|
1494 | ||
1495 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
1496 | " FROM tbl_tenants t, tbl_tenants_virtual_meters tm, tbl_virtual_meters m " |
|
1497 | " WHERE tm.tenant_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
|
1498 | " ORDER BY m.id ") |
|
1499 | cursor.execute(query, (id_,)) |
|
1500 | rows = cursor.fetchall() |
|
1501 | ||
1502 | result = list() |
|
1503 | if rows is not None and len(rows) > 0: |
|
1504 | for row in rows: |
|
1505 | meta_result = {"id": row[0], |
|
1506 | "name": row[1], |
|
1507 | "uuid": row[2], |
|
1508 | "energy_category": energy_category_dict.get(row[3], None)} |
|
1509 | result.append(meta_result) |
|
1510 | ||
1511 | resp.text = json.dumps(result) |
|
1512 | ||
1513 | @staticmethod |
|
1514 | @user_logger |
|
1515 | def on_post(req, resp, id_): |
|
1516 | """Handles POST requests""" |
|
1517 | admin_control(req) |
|
1518 | try: |
|
1519 | raw_json = req.stream.read().decode('utf-8') |
|
1520 | except Exception as ex: |
|
1521 | print(str(ex)) |
|
1522 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1523 | title='API.BAD_REQUEST', |
|
1524 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1525 | ||
1526 | if not id_.isdigit() or int(id_) <= 0: |
|
1527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1528 | description='API.INVALID_TENANT_ID') |
|
1529 | ||
1530 | new_values = json.loads(raw_json) |
|
1531 | ||
1532 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1533 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1534 | new_values['data']['virtual_meter_id'] <= 0: |
|
1535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1536 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1537 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1538 | ||
1539 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1540 | cursor = cnx.cursor() |
|
1541 | ||
1542 | cursor.execute(" SELECT name " |
|
1543 | " from tbl_tenants " |
|
1544 | " WHERE id = %s ", (id_,)) |
|
1545 | if cursor.fetchone() is None: |
|
1546 | cursor.close() |
|
1547 | cnx.close() |
|
1548 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1549 | description='API.TENANT_NOT_FOUND') |
|
1550 | ||
1551 | cursor.execute(" SELECT name " |
|
1552 | " FROM tbl_virtual_meters " |
|
1553 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1554 | if cursor.fetchone() is None: |
|
1555 | cursor.close() |
|
1556 | cnx.close() |
|
1557 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1558 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1559 | ||
1560 | query = (" SELECT id " |
|
1561 | " FROM tbl_tenants_virtual_meters " |
|
1562 | " WHERE tenant_id = %s AND virtual_meter_id = %s") |
|
1563 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1564 | if cursor.fetchone() is not None: |
|
1565 | cursor.close() |
|
1566 | cnx.close() |
|
1567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1568 | description='API.TENANT_VIRTUAL_METER_RELATION_EXISTS') |
|
1569 | ||
1570 | add_row = (" INSERT INTO tbl_tenants_virtual_meters (tenant_id, virtual_meter_id) " |
|
1571 | " VALUES (%s, %s) ") |
|
1572 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
1573 | cnx.commit() |
|
1574 | cursor.close() |
|
1575 | cnx.close() |
|
1576 | ||
1577 | resp.status = falcon.HTTP_201 |
|
1578 | resp.location = '/tenants/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1579 | ||
1580 | ||
1581 | class TenantVirtualMeterItem: |
|
@@ 875-1005 (lines=131) @@ | ||
872 | resp.status = falcon.HTTP_204 |
|
873 | ||
874 | ||
875 | class TenantOfflineMeterCollection: |
|
876 | def __init__(self): |
|
877 | """Initializes Class""" |
|
878 | pass |
|
879 | ||
880 | @staticmethod |
|
881 | def on_options(req, resp, id_): |
|
882 | _ = req |
|
883 | resp.status = falcon.HTTP_200 |
|
884 | _ = id_ |
|
885 | ||
886 | @staticmethod |
|
887 | def on_get(req, resp, id_): |
|
888 | if 'API-KEY' not in req.headers or \ |
|
889 | not isinstance(req.headers['API-KEY'], str) or \ |
|
890 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
891 | access_control(req) |
|
892 | else: |
|
893 | api_key_control(req) |
|
894 | if not id_.isdigit() or int(id_) <= 0: |
|
895 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
896 | description='API.INVALID_TENANT_ID') |
|
897 | ||
898 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
899 | cursor = cnx.cursor() |
|
900 | ||
901 | cursor.execute(" SELECT name " |
|
902 | " FROM tbl_tenants " |
|
903 | " WHERE id = %s ", (id_,)) |
|
904 | if cursor.fetchone() is None: |
|
905 | cursor.close() |
|
906 | cnx.close() |
|
907 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
908 | description='API.TENANT_NOT_FOUND') |
|
909 | ||
910 | query = (" SELECT id, name, uuid " |
|
911 | " FROM tbl_energy_categories ") |
|
912 | cursor.execute(query) |
|
913 | rows_energy_categories = cursor.fetchall() |
|
914 | ||
915 | energy_category_dict = dict() |
|
916 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
917 | for row in rows_energy_categories: |
|
918 | energy_category_dict[row[0]] = {"id": row[0], |
|
919 | "name": row[1], |
|
920 | "uuid": row[2]} |
|
921 | ||
922 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
923 | " FROM tbl_tenants s, tbl_tenants_offline_meters sm, tbl_offline_meters m " |
|
924 | " WHERE sm.tenant_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
925 | " ORDER BY m.id ") |
|
926 | cursor.execute(query, (id_,)) |
|
927 | rows = cursor.fetchall() |
|
928 | ||
929 | result = list() |
|
930 | if rows is not None and len(rows) > 0: |
|
931 | for row in rows: |
|
932 | meta_result = {"id": row[0], |
|
933 | "name": row[1], |
|
934 | "uuid": row[2], |
|
935 | "energy_category": energy_category_dict.get(row[3], None)} |
|
936 | result.append(meta_result) |
|
937 | ||
938 | resp.text = json.dumps(result) |
|
939 | ||
940 | @staticmethod |
|
941 | @user_logger |
|
942 | def on_post(req, resp, id_): |
|
943 | """Handles POST requests""" |
|
944 | admin_control(req) |
|
945 | try: |
|
946 | raw_json = req.stream.read().decode('utf-8') |
|
947 | except Exception as ex: |
|
948 | print(str(ex)) |
|
949 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
950 | title='API.BAD_REQUEST', |
|
951 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
952 | ||
953 | if not id_.isdigit() or int(id_) <= 0: |
|
954 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
955 | description='API.INVALID_TENANT_ID') |
|
956 | ||
957 | new_values = json.loads(raw_json) |
|
958 | ||
959 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
960 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
961 | new_values['data']['offline_meter_id'] <= 0: |
|
962 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
963 | description='API.INVALID_OFFLINE_METER_ID') |
|
964 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
965 | ||
966 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
967 | cursor = cnx.cursor() |
|
968 | ||
969 | cursor.execute(" SELECT name " |
|
970 | " from tbl_tenants " |
|
971 | " WHERE id = %s ", (id_,)) |
|
972 | if cursor.fetchone() is None: |
|
973 | cursor.close() |
|
974 | cnx.close() |
|
975 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
976 | description='API.TENANT_NOT_FOUND') |
|
977 | ||
978 | cursor.execute(" SELECT name " |
|
979 | " FROM tbl_offline_meters " |
|
980 | " WHERE id = %s ", (offline_meter_id,)) |
|
981 | if cursor.fetchone() is None: |
|
982 | cursor.close() |
|
983 | cnx.close() |
|
984 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
985 | description='API.OFFLINE_METER_NOT_FOUND') |
|
986 | ||
987 | query = (" SELECT id " |
|
988 | " FROM tbl_tenants_offline_meters " |
|
989 | " WHERE tenant_id = %s AND offline_meter_id = %s") |
|
990 | cursor.execute(query, (id_, offline_meter_id,)) |
|
991 | if cursor.fetchone() is not None: |
|
992 | cursor.close() |
|
993 | cnx.close() |
|
994 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
995 | description='API.TENANT_OFFLINE_METER_RELATION_EXISTS') |
|
996 | ||
997 | add_row = (" INSERT INTO tbl_tenants_offline_meters (tenant_id, offline_meter_id) " |
|
998 | " VALUES (%s, %s) ") |
|
999 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
1000 | cnx.commit() |
|
1001 | cursor.close() |
|
1002 | cnx.close() |
|
1003 | ||
1004 | resp.status = falcon.HTTP_201 |
|
1005 | resp.location = '/tenants/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1006 | ||
1007 | ||
1008 | class TenantOfflineMeterItem: |
|
@@ 680-810 (lines=131) @@ | ||
677 | resp.status = falcon.HTTP_200 |
|
678 | ||
679 | ||
680 | class TenantMeterCollection: |
|
681 | def __init__(self): |
|
682 | """Initializes Class""" |
|
683 | pass |
|
684 | ||
685 | @staticmethod |
|
686 | def on_options(req, resp, id_): |
|
687 | _ = req |
|
688 | resp.status = falcon.HTTP_200 |
|
689 | _ = id_ |
|
690 | ||
691 | @staticmethod |
|
692 | def on_get(req, resp, id_): |
|
693 | if 'API-KEY' not in req.headers or \ |
|
694 | not isinstance(req.headers['API-KEY'], str) or \ |
|
695 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
696 | access_control(req) |
|
697 | else: |
|
698 | api_key_control(req) |
|
699 | if not id_.isdigit() or int(id_) <= 0: |
|
700 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
701 | description='API.INVALID_TENANT_ID') |
|
702 | ||
703 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
704 | cursor = cnx.cursor() |
|
705 | ||
706 | cursor.execute(" SELECT name " |
|
707 | " FROM tbl_tenants " |
|
708 | " WHERE id = %s ", (id_,)) |
|
709 | if cursor.fetchone() is None: |
|
710 | cursor.close() |
|
711 | cnx.close() |
|
712 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
713 | description='API.TENANT_NOT_FOUND') |
|
714 | ||
715 | query = (" SELECT id, name, uuid " |
|
716 | " FROM tbl_energy_categories ") |
|
717 | cursor.execute(query) |
|
718 | rows_energy_categories = cursor.fetchall() |
|
719 | ||
720 | energy_category_dict = dict() |
|
721 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
722 | for row in rows_energy_categories: |
|
723 | energy_category_dict[row[0]] = {"id": row[0], |
|
724 | "name": row[1], |
|
725 | "uuid": row[2]} |
|
726 | ||
727 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
728 | " FROM tbl_tenants t, tbl_tenants_meters tm, tbl_meters m " |
|
729 | " WHERE tm.tenant_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
|
730 | " ORDER BY m.id ") |
|
731 | cursor.execute(query, (id_,)) |
|
732 | rows = cursor.fetchall() |
|
733 | ||
734 | result = list() |
|
735 | if rows is not None and len(rows) > 0: |
|
736 | for row in rows: |
|
737 | meta_result = {"id": row[0], |
|
738 | "name": row[1], |
|
739 | "uuid": row[2], |
|
740 | "energy_category": energy_category_dict.get(row[3], None)} |
|
741 | result.append(meta_result) |
|
742 | ||
743 | resp.text = json.dumps(result) |
|
744 | ||
745 | @staticmethod |
|
746 | @user_logger |
|
747 | def on_post(req, resp, id_): |
|
748 | """Handles POST requests""" |
|
749 | admin_control(req) |
|
750 | try: |
|
751 | raw_json = req.stream.read().decode('utf-8') |
|
752 | except Exception as ex: |
|
753 | print(str(ex)) |
|
754 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
755 | title='API.BAD_REQUEST', |
|
756 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
757 | ||
758 | if not id_.isdigit() or int(id_) <= 0: |
|
759 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
760 | description='API.INVALID_TENANT_ID') |
|
761 | ||
762 | new_values = json.loads(raw_json) |
|
763 | ||
764 | if 'meter_id' not in new_values['data'].keys() or \ |
|
765 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
766 | new_values['data']['meter_id'] <= 0: |
|
767 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
768 | description='API.INVALID_METER_ID') |
|
769 | meter_id = new_values['data']['meter_id'] |
|
770 | ||
771 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
772 | cursor = cnx.cursor() |
|
773 | ||
774 | cursor.execute(" SELECT name " |
|
775 | " from tbl_tenants " |
|
776 | " WHERE id = %s ", (id_,)) |
|
777 | if cursor.fetchone() is None: |
|
778 | cursor.close() |
|
779 | cnx.close() |
|
780 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
781 | description='API.TENANT_NOT_FOUND') |
|
782 | ||
783 | cursor.execute(" SELECT name " |
|
784 | " FROM tbl_meters " |
|
785 | " WHERE id = %s ", (meter_id,)) |
|
786 | if cursor.fetchone() is None: |
|
787 | cursor.close() |
|
788 | cnx.close() |
|
789 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
790 | description='API.METER_NOT_FOUND') |
|
791 | ||
792 | query = (" SELECT id " |
|
793 | " FROM tbl_tenants_meters " |
|
794 | " WHERE tenant_id = %s AND meter_id = %s") |
|
795 | cursor.execute(query, (id_, meter_id,)) |
|
796 | if cursor.fetchone() is not None: |
|
797 | cursor.close() |
|
798 | cnx.close() |
|
799 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
800 | description='API.TENANT_METER_RELATION_EXISTS') |
|
801 | ||
802 | add_row = (" INSERT INTO tbl_tenants_meters (tenant_id, meter_id) " |
|
803 | " VALUES (%s, %s) ") |
|
804 | cursor.execute(add_row, (id_, meter_id,)) |
|
805 | cnx.commit() |
|
806 | cursor.close() |
|
807 | cnx.close() |
|
808 | ||
809 | resp.status = falcon.HTTP_201 |
|
810 | resp.location = '/tenants/' + str(id_) + '/meters/' + str(meter_id) |
|
811 | ||
812 | ||
813 | class TenantMeterItem: |
|
@@ 1071-1200 (lines=130) @@ | ||
1068 | resp.status = falcon.HTTP_204 |
|
1069 | ||
1070 | ||
1071 | class TenantPointCollection: |
|
1072 | def __init__(self): |
|
1073 | """Initializes Class""" |
|
1074 | pass |
|
1075 | ||
1076 | @staticmethod |
|
1077 | def on_options(req, resp, id_): |
|
1078 | _ = req |
|
1079 | resp.status = falcon.HTTP_200 |
|
1080 | _ = id_ |
|
1081 | ||
1082 | @staticmethod |
|
1083 | def on_get(req, resp, id_): |
|
1084 | if 'API-KEY' not in req.headers or \ |
|
1085 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1086 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1087 | access_control(req) |
|
1088 | else: |
|
1089 | api_key_control(req) |
|
1090 | if not id_.isdigit() or int(id_) <= 0: |
|
1091 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1092 | description='API.INVALID_TENANT_ID') |
|
1093 | ||
1094 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1095 | cursor = cnx.cursor() |
|
1096 | ||
1097 | cursor.execute(" SELECT name " |
|
1098 | " FROM tbl_tenants " |
|
1099 | " WHERE id = %s ", (id_,)) |
|
1100 | if cursor.fetchone() is None: |
|
1101 | cursor.close() |
|
1102 | cnx.close() |
|
1103 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1104 | description='API.TENANT_NOT_FOUND') |
|
1105 | ||
1106 | query = (" SELECT id, name, uuid " |
|
1107 | " FROM tbl_data_sources ") |
|
1108 | cursor.execute(query) |
|
1109 | rows_data_sources = cursor.fetchall() |
|
1110 | ||
1111 | data_source_dict = dict() |
|
1112 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
1113 | for row in rows_data_sources: |
|
1114 | data_source_dict[row[0]] = {"id": row[0], |
|
1115 | "name": row[1], |
|
1116 | "uuid": row[2]} |
|
1117 | ||
1118 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
1119 | " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
|
1120 | " WHERE tp.tenant_id = t.id AND p.id = tp.point_id AND t.id = %s " |
|
1121 | " ORDER BY p.id ") |
|
1122 | cursor.execute(query, (id_,)) |
|
1123 | rows = cursor.fetchall() |
|
1124 | ||
1125 | result = list() |
|
1126 | if rows is not None and len(rows) > 0: |
|
1127 | for row in rows: |
|
1128 | meta_result = {"id": row[0], |
|
1129 | "name": row[1], |
|
1130 | "data_source": data_source_dict.get(row[2], None)} |
|
1131 | result.append(meta_result) |
|
1132 | ||
1133 | resp.text = json.dumps(result) |
|
1134 | ||
1135 | @staticmethod |
|
1136 | @user_logger |
|
1137 | def on_post(req, resp, id_): |
|
1138 | """Handles POST requests""" |
|
1139 | admin_control(req) |
|
1140 | try: |
|
1141 | raw_json = req.stream.read().decode('utf-8') |
|
1142 | except Exception as ex: |
|
1143 | print(str(ex)) |
|
1144 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1145 | title='API.BAD_REQUEST', |
|
1146 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1147 | ||
1148 | if not id_.isdigit() or int(id_) <= 0: |
|
1149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1150 | description='API.INVALID_TENANT_ID') |
|
1151 | ||
1152 | new_values = json.loads(raw_json) |
|
1153 | ||
1154 | if 'point_id' not in new_values['data'].keys() or \ |
|
1155 | not isinstance(new_values['data']['point_id'], int) or \ |
|
1156 | new_values['data']['point_id'] <= 0: |
|
1157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1158 | description='API.INVALID_POINT_ID') |
|
1159 | point_id = new_values['data']['point_id'] |
|
1160 | ||
1161 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1162 | cursor = cnx.cursor() |
|
1163 | ||
1164 | cursor.execute(" SELECT name " |
|
1165 | " from tbl_tenants " |
|
1166 | " WHERE id = %s ", (id_,)) |
|
1167 | if cursor.fetchone() is None: |
|
1168 | cursor.close() |
|
1169 | cnx.close() |
|
1170 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1171 | description='API.TENANT_NOT_FOUND') |
|
1172 | ||
1173 | cursor.execute(" SELECT name " |
|
1174 | " FROM tbl_points " |
|
1175 | " WHERE id = %s ", (point_id,)) |
|
1176 | if cursor.fetchone() is None: |
|
1177 | cursor.close() |
|
1178 | cnx.close() |
|
1179 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1180 | description='API.POINT_NOT_FOUND') |
|
1181 | ||
1182 | query = (" SELECT id " |
|
1183 | " FROM tbl_tenants_points " |
|
1184 | " WHERE tenant_id = %s AND point_id = %s") |
|
1185 | cursor.execute(query, (id_, point_id,)) |
|
1186 | if cursor.fetchone() is not None: |
|
1187 | cursor.close() |
|
1188 | cnx.close() |
|
1189 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1190 | description='API.TENANT_POINT_RELATION_EXISTS') |
|
1191 | ||
1192 | add_row = (" INSERT INTO tbl_tenants_points (tenant_id, point_id) " |
|
1193 | " VALUES (%s, %s) ") |
|
1194 | cursor.execute(add_row, (id_, point_id,)) |
|
1195 | cnx.commit() |
|
1196 | cursor.close() |
|
1197 | cnx.close() |
|
1198 | ||
1199 | resp.status = falcon.HTTP_201 |
|
1200 | resp.location = '/tenants/' + str(id_) + '/points/' + str(point_id) |
|
1201 | ||
1202 | ||
1203 | class TenantPointItem: |
@@ 3091-3219 (lines=129) @@ | ||
3088 | resp.status = falcon.HTTP_204 |
|
3089 | ||
3090 | ||
3091 | class SpaceVirtualMeterCollection: |
|
3092 | def __init__(self): |
|
3093 | """Initializes Class""" |
|
3094 | pass |
|
3095 | ||
3096 | @staticmethod |
|
3097 | def on_options(req, resp, id_): |
|
3098 | _ = req |
|
3099 | resp.status = falcon.HTTP_200 |
|
3100 | _ = id_ |
|
3101 | ||
3102 | @staticmethod |
|
3103 | def on_get(req, resp, id_): |
|
3104 | if 'API-KEY' not in req.headers or \ |
|
3105 | not isinstance(req.headers['API-KEY'], str) or \ |
|
3106 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
3107 | access_control(req) |
|
3108 | else: |
|
3109 | api_key_control(req) |
|
3110 | if not id_.isdigit() or int(id_) <= 0: |
|
3111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3112 | description='API.INVALID_SPACE_ID') |
|
3113 | ||
3114 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3115 | cursor = cnx.cursor() |
|
3116 | ||
3117 | cursor.execute(" SELECT name " |
|
3118 | " FROM tbl_spaces " |
|
3119 | " WHERE id = %s ", (id_,)) |
|
3120 | if cursor.fetchone() is None: |
|
3121 | cursor.close() |
|
3122 | cnx.close() |
|
3123 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3124 | description='API.SPACE_NOT_FOUND') |
|
3125 | ||
3126 | query = (" SELECT id, name, uuid " |
|
3127 | " FROM tbl_energy_categories ") |
|
3128 | cursor.execute(query) |
|
3129 | rows_energy_categories = cursor.fetchall() |
|
3130 | ||
3131 | energy_category_dict = dict() |
|
3132 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
3133 | for row in rows_energy_categories: |
|
3134 | energy_category_dict[row[0]] = {"id": row[0], |
|
3135 | "name": row[1], |
|
3136 | "uuid": row[2]} |
|
3137 | ||
3138 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
3139 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
|
3140 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
|
3141 | " ORDER BY m.id ") |
|
3142 | cursor.execute(query, (id_,)) |
|
3143 | rows = cursor.fetchall() |
|
3144 | ||
3145 | result = list() |
|
3146 | if rows is not None and len(rows) > 0: |
|
3147 | for row in rows: |
|
3148 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
3149 | "energy_category": energy_category_dict.get(row[3], None)} |
|
3150 | result.append(meta_result) |
|
3151 | ||
3152 | resp.text = json.dumps(result) |
|
3153 | ||
3154 | @staticmethod |
|
3155 | @user_logger |
|
3156 | def on_post(req, resp, id_): |
|
3157 | """Handles POST requests""" |
|
3158 | admin_control(req) |
|
3159 | try: |
|
3160 | raw_json = req.stream.read().decode('utf-8') |
|
3161 | except Exception as ex: |
|
3162 | print(str(ex)) |
|
3163 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
3164 | title='API.BAD_REQUEST', |
|
3165 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
3166 | ||
3167 | if not id_.isdigit() or int(id_) <= 0: |
|
3168 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3169 | description='API.INVALID_SPACE_ID') |
|
3170 | ||
3171 | new_values = json.loads(raw_json) |
|
3172 | ||
3173 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
3174 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
3175 | new_values['data']['virtual_meter_id'] <= 0: |
|
3176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3177 | description='API.INVALID_VIRTUAL_METER_ID') |
|
3178 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
3179 | ||
3180 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3181 | cursor = cnx.cursor() |
|
3182 | ||
3183 | cursor.execute(" SELECT name " |
|
3184 | " from tbl_spaces " |
|
3185 | " WHERE id = %s ", (id_,)) |
|
3186 | if cursor.fetchone() is None: |
|
3187 | cursor.close() |
|
3188 | cnx.close() |
|
3189 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3190 | description='API.SPACE_NOT_FOUND') |
|
3191 | ||
3192 | cursor.execute(" SELECT name " |
|
3193 | " FROM tbl_virtual_meters " |
|
3194 | " WHERE id = %s ", (virtual_meter_id,)) |
|
3195 | if cursor.fetchone() is None: |
|
3196 | cursor.close() |
|
3197 | cnx.close() |
|
3198 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3199 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
3200 | ||
3201 | query = (" SELECT id " |
|
3202 | " FROM tbl_spaces_virtual_meters " |
|
3203 | " WHERE space_id = %s AND virtual_meter_id = %s") |
|
3204 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
3205 | if cursor.fetchone() is not None: |
|
3206 | cursor.close() |
|
3207 | cnx.close() |
|
3208 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
3209 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTS') |
|
3210 | ||
3211 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
|
3212 | " VALUES (%s, %s) ") |
|
3213 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
3214 | cnx.commit() |
|
3215 | cursor.close() |
|
3216 | cnx.close() |
|
3217 | ||
3218 | resp.status = falcon.HTTP_201 |
|
3219 | resp.location = '/spaces/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
3220 | ||
3221 | ||
3222 | class SpaceVirtualMeterItem: |
|
@@ 1803-1931 (lines=129) @@ | ||
1800 | resp.status = falcon.HTTP_204 |
|
1801 | ||
1802 | ||
1803 | class SpaceOfflineMeterCollection: |
|
1804 | def __init__(self): |
|
1805 | """Initializes Class""" |
|
1806 | pass |
|
1807 | ||
1808 | @staticmethod |
|
1809 | def on_options(req, resp, id_): |
|
1810 | _ = req |
|
1811 | resp.status = falcon.HTTP_200 |
|
1812 | _ = id_ |
|
1813 | ||
1814 | @staticmethod |
|
1815 | def on_get(req, resp, id_): |
|
1816 | if 'API-KEY' not in req.headers or \ |
|
1817 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1818 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1819 | access_control(req) |
|
1820 | else: |
|
1821 | api_key_control(req) |
|
1822 | if not id_.isdigit() or int(id_) <= 0: |
|
1823 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1824 | description='API.INVALID_SPACE_ID') |
|
1825 | ||
1826 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1827 | cursor = cnx.cursor() |
|
1828 | ||
1829 | cursor.execute(" SELECT name " |
|
1830 | " FROM tbl_spaces " |
|
1831 | " WHERE id = %s ", (id_,)) |
|
1832 | if cursor.fetchone() is None: |
|
1833 | cursor.close() |
|
1834 | cnx.close() |
|
1835 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1836 | description='API.SPACE_NOT_FOUND') |
|
1837 | ||
1838 | query = (" SELECT id, name, uuid " |
|
1839 | " FROM tbl_energy_categories ") |
|
1840 | cursor.execute(query) |
|
1841 | rows_energy_categories = cursor.fetchall() |
|
1842 | ||
1843 | energy_category_dict = dict() |
|
1844 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1845 | for row in rows_energy_categories: |
|
1846 | energy_category_dict[row[0]] = {"id": row[0], |
|
1847 | "name": row[1], |
|
1848 | "uuid": row[2]} |
|
1849 | ||
1850 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
1851 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
|
1852 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
1853 | " ORDER BY m.id ") |
|
1854 | cursor.execute(query, (id_,)) |
|
1855 | rows = cursor.fetchall() |
|
1856 | ||
1857 | result = list() |
|
1858 | if rows is not None and len(rows) > 0: |
|
1859 | for row in rows: |
|
1860 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1861 | "energy_category": energy_category_dict.get(row[3], None)} |
|
1862 | result.append(meta_result) |
|
1863 | ||
1864 | resp.text = json.dumps(result) |
|
1865 | ||
1866 | @staticmethod |
|
1867 | @user_logger |
|
1868 | def on_post(req, resp, id_): |
|
1869 | """Handles POST requests""" |
|
1870 | admin_control(req) |
|
1871 | try: |
|
1872 | raw_json = req.stream.read().decode('utf-8') |
|
1873 | except Exception as ex: |
|
1874 | print(str(ex)) |
|
1875 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1876 | title='API.BAD_REQUEST', |
|
1877 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1878 | ||
1879 | if not id_.isdigit() or int(id_) <= 0: |
|
1880 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1881 | description='API.INVALID_SPACE_ID') |
|
1882 | ||
1883 | new_values = json.loads(raw_json) |
|
1884 | ||
1885 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
1886 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
1887 | new_values['data']['offline_meter_id'] <= 0: |
|
1888 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1889 | description='API.INVALID_OFFLINE_METER_ID') |
|
1890 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
1891 | ||
1892 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1893 | cursor = cnx.cursor() |
|
1894 | ||
1895 | cursor.execute(" SELECT name " |
|
1896 | " from tbl_spaces " |
|
1897 | " WHERE id = %s ", (id_,)) |
|
1898 | if cursor.fetchone() is None: |
|
1899 | cursor.close() |
|
1900 | cnx.close() |
|
1901 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1902 | description='API.SPACE_NOT_FOUND') |
|
1903 | ||
1904 | cursor.execute(" SELECT name " |
|
1905 | " FROM tbl_offline_meters " |
|
1906 | " WHERE id = %s ", (offline_meter_id,)) |
|
1907 | if cursor.fetchone() is None: |
|
1908 | cursor.close() |
|
1909 | cnx.close() |
|
1910 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1911 | description='API.OFFLINE_METER_NOT_FOUND') |
|
1912 | ||
1913 | query = (" SELECT id " |
|
1914 | " FROM tbl_spaces_offline_meters " |
|
1915 | " WHERE space_id = %s AND offline_meter_id = %s") |
|
1916 | cursor.execute(query, (id_, offline_meter_id,)) |
|
1917 | if cursor.fetchone() is not None: |
|
1918 | cursor.close() |
|
1919 | cnx.close() |
|
1920 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1921 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTS') |
|
1922 | ||
1923 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
|
1924 | " VALUES (%s, %s) ") |
|
1925 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
1926 | cnx.commit() |
|
1927 | cursor.close() |
|
1928 | cnx.close() |
|
1929 | ||
1930 | resp.status = falcon.HTTP_201 |
|
1931 | resp.location = '/spaces/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1932 | ||
1933 | ||
1934 | class SpaceOfflineMeterItem: |
|
@@ 1427-1555 (lines=129) @@ | ||
1424 | resp.status = falcon.HTTP_204 |
|
1425 | ||
1426 | ||
1427 | class SpaceMeterCollection: |
|
1428 | def __init__(self): |
|
1429 | """Initializes Class""" |
|
1430 | pass |
|
1431 | ||
1432 | @staticmethod |
|
1433 | def on_options(req, resp, id_): |
|
1434 | _ = req |
|
1435 | resp.status = falcon.HTTP_200 |
|
1436 | _ = id_ |
|
1437 | ||
1438 | @staticmethod |
|
1439 | def on_get(req, resp, id_): |
|
1440 | if 'API-KEY' not in req.headers or \ |
|
1441 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1442 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1443 | access_control(req) |
|
1444 | else: |
|
1445 | api_key_control(req) |
|
1446 | if not id_.isdigit() or int(id_) <= 0: |
|
1447 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1448 | description='API.INVALID_SPACE_ID') |
|
1449 | ||
1450 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1451 | cursor = cnx.cursor() |
|
1452 | ||
1453 | cursor.execute(" SELECT name " |
|
1454 | " FROM tbl_spaces " |
|
1455 | " WHERE id = %s ", (id_,)) |
|
1456 | if cursor.fetchone() is None: |
|
1457 | cursor.close() |
|
1458 | cnx.close() |
|
1459 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1460 | description='API.SPACE_NOT_FOUND') |
|
1461 | ||
1462 | query = (" SELECT id, name, uuid " |
|
1463 | " FROM tbl_energy_categories ") |
|
1464 | cursor.execute(query) |
|
1465 | rows_energy_categories = cursor.fetchall() |
|
1466 | ||
1467 | energy_category_dict = dict() |
|
1468 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1469 | for row in rows_energy_categories: |
|
1470 | energy_category_dict[row[0]] = {"id": row[0], |
|
1471 | "name": row[1], |
|
1472 | "uuid": row[2]} |
|
1473 | ||
1474 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
1475 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
|
1476 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
|
1477 | " ORDER BY m.id ") |
|
1478 | cursor.execute(query, (id_,)) |
|
1479 | rows = cursor.fetchall() |
|
1480 | ||
1481 | result = list() |
|
1482 | if rows is not None and len(rows) > 0: |
|
1483 | for row in rows: |
|
1484 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1485 | "energy_category": energy_category_dict.get(row[3], None)} |
|
1486 | result.append(meta_result) |
|
1487 | ||
1488 | resp.text = json.dumps(result) |
|
1489 | ||
1490 | @staticmethod |
|
1491 | @user_logger |
|
1492 | def on_post(req, resp, id_): |
|
1493 | """Handles POST requests""" |
|
1494 | admin_control(req) |
|
1495 | try: |
|
1496 | raw_json = req.stream.read().decode('utf-8') |
|
1497 | except Exception as ex: |
|
1498 | print(str(ex)) |
|
1499 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1500 | title='API.BAD_REQUEST', |
|
1501 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1502 | ||
1503 | if not id_.isdigit() or int(id_) <= 0: |
|
1504 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1505 | description='API.INVALID_SPACE_ID') |
|
1506 | ||
1507 | new_values = json.loads(raw_json) |
|
1508 | ||
1509 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1510 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1511 | new_values['data']['meter_id'] <= 0: |
|
1512 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1513 | description='API.INVALID_METER_ID') |
|
1514 | meter_id = new_values['data']['meter_id'] |
|
1515 | ||
1516 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1517 | cursor = cnx.cursor() |
|
1518 | ||
1519 | cursor.execute(" SELECT name " |
|
1520 | " from tbl_spaces " |
|
1521 | " WHERE id = %s ", (id_,)) |
|
1522 | if cursor.fetchone() is None: |
|
1523 | cursor.close() |
|
1524 | cnx.close() |
|
1525 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1526 | description='API.SPACE_NOT_FOUND') |
|
1527 | ||
1528 | cursor.execute(" SELECT name " |
|
1529 | " FROM tbl_meters " |
|
1530 | " WHERE id = %s ", (meter_id,)) |
|
1531 | if cursor.fetchone() is None: |
|
1532 | cursor.close() |
|
1533 | cnx.close() |
|
1534 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1535 | description='API.METER_NOT_FOUND') |
|
1536 | ||
1537 | query = (" SELECT id " |
|
1538 | " FROM tbl_spaces_meters " |
|
1539 | " WHERE space_id = %s AND meter_id = %s") |
|
1540 | cursor.execute(query, (id_, meter_id,)) |
|
1541 | if cursor.fetchone() is not None: |
|
1542 | cursor.close() |
|
1543 | cnx.close() |
|
1544 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1545 | description='API.SPACE_METER_RELATION_EXISTS') |
|
1546 | ||
1547 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
|
1548 | " VALUES (%s, %s) ") |
|
1549 | cursor.execute(add_row, (id_, meter_id,)) |
|
1550 | cnx.commit() |
|
1551 | cursor.close() |
|
1552 | cnx.close() |
|
1553 | ||
1554 | resp.status = falcon.HTTP_201 |
|
1555 | resp.location = '/spaces/' + str(id_) + '/meters/' + str(meter_id) |
|
1556 | ||
1557 | ||
1558 | class SpaceMeterItem: |
|
@@ 2178-2305 (lines=128) @@ | ||
2175 | resp.status = falcon.HTTP_204 |
|
2176 | ||
2177 | ||
2178 | class SpacePointCollection: |
|
2179 | def __init__(self): |
|
2180 | """Initializes Class""" |
|
2181 | pass |
|
2182 | ||
2183 | @staticmethod |
|
2184 | def on_options(req, resp, id_): |
|
2185 | _ = req |
|
2186 | resp.status = falcon.HTTP_200 |
|
2187 | _ = id_ |
|
2188 | ||
2189 | @staticmethod |
|
2190 | def on_get(req, resp, id_): |
|
2191 | if 'API-KEY' not in req.headers or \ |
|
2192 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2193 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2194 | access_control(req) |
|
2195 | else: |
|
2196 | api_key_control(req) |
|
2197 | if not id_.isdigit() or int(id_) <= 0: |
|
2198 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2199 | description='API.INVALID_SPACE_ID') |
|
2200 | ||
2201 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2202 | cursor = cnx.cursor() |
|
2203 | ||
2204 | cursor.execute(" SELECT name " |
|
2205 | " FROM tbl_spaces " |
|
2206 | " WHERE id = %s ", (id_,)) |
|
2207 | if cursor.fetchone() is None: |
|
2208 | cursor.close() |
|
2209 | cnx.close() |
|
2210 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2211 | description='API.SPACE_NOT_FOUND') |
|
2212 | ||
2213 | query = (" SELECT id, name, uuid " |
|
2214 | " FROM tbl_data_sources ") |
|
2215 | cursor.execute(query) |
|
2216 | rows_data_sources = cursor.fetchall() |
|
2217 | ||
2218 | data_source_dict = dict() |
|
2219 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
2220 | for row in rows_data_sources: |
|
2221 | data_source_dict[row[0]] = {"id": row[0], |
|
2222 | "name": row[1], |
|
2223 | "uuid": row[2]} |
|
2224 | ||
2225 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
2226 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
|
2227 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
|
2228 | " ORDER BY p.id ") |
|
2229 | cursor.execute(query, (id_,)) |
|
2230 | rows = cursor.fetchall() |
|
2231 | ||
2232 | result = list() |
|
2233 | if rows is not None and len(rows) > 0: |
|
2234 | for row in rows: |
|
2235 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
2236 | result.append(meta_result) |
|
2237 | ||
2238 | resp.text = json.dumps(result) |
|
2239 | ||
2240 | @staticmethod |
|
2241 | @user_logger |
|
2242 | def on_post(req, resp, id_): |
|
2243 | """Handles POST requests""" |
|
2244 | admin_control(req) |
|
2245 | try: |
|
2246 | raw_json = req.stream.read().decode('utf-8') |
|
2247 | except Exception as ex: |
|
2248 | print(str(ex)) |
|
2249 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2250 | title='API.BAD_REQUEST', |
|
2251 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2252 | ||
2253 | if not id_.isdigit() or int(id_) <= 0: |
|
2254 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2255 | description='API.INVALID_SPACE_ID') |
|
2256 | ||
2257 | new_values = json.loads(raw_json) |
|
2258 | ||
2259 | if 'point_id' not in new_values['data'].keys() or \ |
|
2260 | not isinstance(new_values['data']['point_id'], int) or \ |
|
2261 | new_values['data']['point_id'] <= 0: |
|
2262 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2263 | description='API.INVALID_POINT_ID') |
|
2264 | point_id = new_values['data']['point_id'] |
|
2265 | ||
2266 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2267 | cursor = cnx.cursor() |
|
2268 | ||
2269 | cursor.execute(" SELECT name " |
|
2270 | " from tbl_spaces " |
|
2271 | " WHERE id = %s ", (id_,)) |
|
2272 | if cursor.fetchone() is None: |
|
2273 | cursor.close() |
|
2274 | cnx.close() |
|
2275 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2276 | description='API.SPACE_NOT_FOUND') |
|
2277 | ||
2278 | cursor.execute(" SELECT name " |
|
2279 | " FROM tbl_points " |
|
2280 | " WHERE id = %s ", (point_id,)) |
|
2281 | if cursor.fetchone() is None: |
|
2282 | cursor.close() |
|
2283 | cnx.close() |
|
2284 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2285 | description='API.POINT_NOT_FOUND') |
|
2286 | ||
2287 | query = (" SELECT id " |
|
2288 | " FROM tbl_spaces_points " |
|
2289 | " WHERE space_id = %s AND point_id = %s") |
|
2290 | cursor.execute(query, (id_, point_id,)) |
|
2291 | if cursor.fetchone() is not None: |
|
2292 | cursor.close() |
|
2293 | cnx.close() |
|
2294 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2295 | description='API.SPACE_POINT_RELATION_EXISTS') |
|
2296 | ||
2297 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
|
2298 | " VALUES (%s, %s) ") |
|
2299 | cursor.execute(add_row, (id_, point_id,)) |
|
2300 | cnx.commit() |
|
2301 | cursor.close() |
|
2302 | cnx.close() |
|
2303 | ||
2304 | resp.status = falcon.HTTP_201 |
|
2305 | resp.location = '/spaces/' + str(id_) + '/points/' + str(point_id) |
|
2306 | ||
2307 | ||
2308 | class SpacePointItem: |
@@ 1409-1537 (lines=129) @@ | ||
1406 | resp.status = falcon.HTTP_204 |
|
1407 | ||
1408 | ||
1409 | class ShopfloorVirtualMeterCollection: |
|
1410 | def __init__(self): |
|
1411 | """Initializes ShopfloorVirtualMeterCollection""" |
|
1412 | pass |
|
1413 | ||
1414 | @staticmethod |
|
1415 | def on_options(req, resp, id_): |
|
1416 | resp.status = falcon.HTTP_200 |
|
1417 | _ = req |
|
1418 | _ = id_ |
|
1419 | ||
1420 | @staticmethod |
|
1421 | def on_get(req, resp, id_): |
|
1422 | if 'API-KEY' not in req.headers or \ |
|
1423 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1424 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1425 | access_control(req) |
|
1426 | else: |
|
1427 | api_key_control(req) |
|
1428 | if not id_.isdigit() or int(id_) <= 0: |
|
1429 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1430 | description='API.INVALID_SHOPFLOOR_ID') |
|
1431 | ||
1432 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1433 | cursor = cnx.cursor() |
|
1434 | ||
1435 | cursor.execute(" SELECT name " |
|
1436 | " FROM tbl_shopfloors " |
|
1437 | " WHERE id = %s ", (id_,)) |
|
1438 | if cursor.fetchone() is None: |
|
1439 | cursor.close() |
|
1440 | cnx.close() |
|
1441 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1442 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1443 | ||
1444 | query = (" SELECT id, name, uuid " |
|
1445 | " FROM tbl_energy_categories ") |
|
1446 | cursor.execute(query) |
|
1447 | rows_energy_categories = cursor.fetchall() |
|
1448 | ||
1449 | energy_category_dict = dict() |
|
1450 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1451 | for row in rows_energy_categories: |
|
1452 | energy_category_dict[row[0]] = {"id": row[0], |
|
1453 | "name": row[1], |
|
1454 | "uuid": row[2]} |
|
1455 | ||
1456 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
1457 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
|
1458 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
|
1459 | " ORDER BY m.id ") |
|
1460 | cursor.execute(query, (id_,)) |
|
1461 | rows = cursor.fetchall() |
|
1462 | ||
1463 | result = list() |
|
1464 | if rows is not None and len(rows) > 0: |
|
1465 | for row in rows: |
|
1466 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1467 | "energy_category": energy_category_dict.get(row[3], None)} |
|
1468 | result.append(meta_result) |
|
1469 | ||
1470 | resp.text = json.dumps(result) |
|
1471 | ||
1472 | @staticmethod |
|
1473 | @user_logger |
|
1474 | def on_post(req, resp, id_): |
|
1475 | """Handles POST requests""" |
|
1476 | admin_control(req) |
|
1477 | try: |
|
1478 | raw_json = req.stream.read().decode('utf-8') |
|
1479 | except Exception as ex: |
|
1480 | print(ex) |
|
1481 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1482 | title='API.BAD_REQUEST', |
|
1483 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1484 | ||
1485 | if not id_.isdigit() or int(id_) <= 0: |
|
1486 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1487 | description='API.INVALID_SHOPFLOOR_ID') |
|
1488 | ||
1489 | new_values = json.loads(raw_json) |
|
1490 | ||
1491 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1492 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1493 | new_values['data']['virtual_meter_id'] <= 0: |
|
1494 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1495 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1496 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1497 | ||
1498 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1499 | cursor = cnx.cursor() |
|
1500 | ||
1501 | cursor.execute(" SELECT name " |
|
1502 | " from tbl_shopfloors " |
|
1503 | " WHERE id = %s ", (id_,)) |
|
1504 | if cursor.fetchone() is None: |
|
1505 | cursor.close() |
|
1506 | cnx.close() |
|
1507 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1508 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1509 | ||
1510 | cursor.execute(" SELECT name " |
|
1511 | " FROM tbl_virtual_meters " |
|
1512 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1513 | if cursor.fetchone() is None: |
|
1514 | cursor.close() |
|
1515 | cnx.close() |
|
1516 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1517 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1518 | ||
1519 | query = (" SELECT id " |
|
1520 | " FROM tbl_shopfloors_virtual_meters " |
|
1521 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
|
1522 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1523 | if cursor.fetchone() is not None: |
|
1524 | cursor.close() |
|
1525 | cnx.close() |
|
1526 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1527 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
|
1528 | ||
1529 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
|
1530 | " VALUES (%s, %s) ") |
|
1531 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
1532 | cnx.commit() |
|
1533 | cursor.close() |
|
1534 | cnx.close() |
|
1535 | ||
1536 | resp.status = falcon.HTTP_201 |
|
1537 | resp.location = '/shopfloors/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1538 | ||
1539 | ||
1540 | class ShopfloorVirtualMeterItem: |
|
@@ 839-967 (lines=129) @@ | ||
836 | resp.status = falcon.HTTP_204 |
|
837 | ||
838 | ||
839 | class ShopfloorOfflineMeterCollection: |
|
840 | def __init__(self): |
|
841 | """Initializes ShopfloorOfflineMeterCollection""" |
|
842 | pass |
|
843 | ||
844 | @staticmethod |
|
845 | def on_options(req, resp, id_): |
|
846 | resp.status = falcon.HTTP_200 |
|
847 | _ = req |
|
848 | _ = id_ |
|
849 | ||
850 | @staticmethod |
|
851 | def on_get(req, resp, id_): |
|
852 | if 'API-KEY' not in req.headers or \ |
|
853 | not isinstance(req.headers['API-KEY'], str) or \ |
|
854 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
855 | access_control(req) |
|
856 | else: |
|
857 | api_key_control(req) |
|
858 | if not id_.isdigit() or int(id_) <= 0: |
|
859 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
860 | description='API.INVALID_SHOPFLOOR_ID') |
|
861 | ||
862 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
863 | cursor = cnx.cursor() |
|
864 | ||
865 | cursor.execute(" SELECT name " |
|
866 | " FROM tbl_shopfloors " |
|
867 | " WHERE id = %s ", (id_,)) |
|
868 | if cursor.fetchone() is None: |
|
869 | cursor.close() |
|
870 | cnx.close() |
|
871 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
872 | description='API.SHOPFLOOR_NOT_FOUND') |
|
873 | ||
874 | query = (" SELECT id, name, uuid " |
|
875 | " FROM tbl_energy_categories ") |
|
876 | cursor.execute(query) |
|
877 | rows_energy_categories = cursor.fetchall() |
|
878 | ||
879 | energy_category_dict = dict() |
|
880 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
881 | for row in rows_energy_categories: |
|
882 | energy_category_dict[row[0]] = {"id": row[0], |
|
883 | "name": row[1], |
|
884 | "uuid": row[2]} |
|
885 | ||
886 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
887 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
|
888 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
889 | " ORDER BY m.id ") |
|
890 | cursor.execute(query, (id_,)) |
|
891 | rows = cursor.fetchall() |
|
892 | ||
893 | result = list() |
|
894 | if rows is not None and len(rows) > 0: |
|
895 | for row in rows: |
|
896 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
897 | "energy_category": energy_category_dict.get(row[3], None)} |
|
898 | result.append(meta_result) |
|
899 | ||
900 | resp.text = json.dumps(result) |
|
901 | ||
902 | @staticmethod |
|
903 | @user_logger |
|
904 | def on_post(req, resp, id_): |
|
905 | """Handles POST requests""" |
|
906 | admin_control(req) |
|
907 | try: |
|
908 | raw_json = req.stream.read().decode('utf-8') |
|
909 | except Exception as ex: |
|
910 | print(ex) |
|
911 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
912 | title='API.BAD_REQUEST', |
|
913 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
914 | ||
915 | if not id_.isdigit() or int(id_) <= 0: |
|
916 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
917 | description='API.INVALID_SHOPFLOOR_ID') |
|
918 | ||
919 | new_values = json.loads(raw_json) |
|
920 | ||
921 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
922 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
923 | new_values['data']['offline_meter_id'] <= 0: |
|
924 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
925 | description='API.INVALID_OFFLINE_METER_ID') |
|
926 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
927 | ||
928 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
929 | cursor = cnx.cursor() |
|
930 | ||
931 | cursor.execute(" SELECT name " |
|
932 | " from tbl_shopfloors " |
|
933 | " WHERE id = %s ", (id_,)) |
|
934 | if cursor.fetchone() is None: |
|
935 | cursor.close() |
|
936 | cnx.close() |
|
937 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
938 | description='API.SHOPFLOOR_NOT_FOUND') |
|
939 | ||
940 | cursor.execute(" SELECT name " |
|
941 | " FROM tbl_offline_meters " |
|
942 | " WHERE id = %s ", (offline_meter_id,)) |
|
943 | if cursor.fetchone() is None: |
|
944 | cursor.close() |
|
945 | cnx.close() |
|
946 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
947 | description='API.OFFLINE_METER_NOT_FOUND') |
|
948 | ||
949 | query = (" SELECT id " |
|
950 | " FROM tbl_shopfloors_offline_meters " |
|
951 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
|
952 | cursor.execute(query, (id_, offline_meter_id,)) |
|
953 | if cursor.fetchone() is not None: |
|
954 | cursor.close() |
|
955 | cnx.close() |
|
956 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
957 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
|
958 | ||
959 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
|
960 | " VALUES (%s, %s) ") |
|
961 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
962 | cnx.commit() |
|
963 | cursor.close() |
|
964 | cnx.close() |
|
965 | ||
966 | resp.status = falcon.HTTP_201 |
|
967 | resp.location = '/shopfloors/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
968 | ||
969 | ||
970 | class ShopfloorOfflineMeterItem: |
|
@@ 645-773 (lines=129) @@ | ||
642 | resp.status = falcon.HTTP_204 |
|
643 | ||
644 | ||
645 | class ShopfloorMeterCollection: |
|
646 | def __init__(self): |
|
647 | """Initializes ShopfloorMeterCollection""" |
|
648 | pass |
|
649 | ||
650 | @staticmethod |
|
651 | def on_options(req, resp, id_): |
|
652 | resp.status = falcon.HTTP_200 |
|
653 | _ = req |
|
654 | _ = id_ |
|
655 | ||
656 | @staticmethod |
|
657 | def on_get(req, resp, id_): |
|
658 | if 'API-KEY' not in req.headers or \ |
|
659 | not isinstance(req.headers['API-KEY'], str) or \ |
|
660 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
661 | access_control(req) |
|
662 | else: |
|
663 | api_key_control(req) |
|
664 | if not id_.isdigit() or int(id_) <= 0: |
|
665 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
666 | description='API.INVALID_SHOPFLOOR_ID') |
|
667 | ||
668 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
669 | cursor = cnx.cursor() |
|
670 | ||
671 | cursor.execute(" SELECT name " |
|
672 | " FROM tbl_shopfloors " |
|
673 | " WHERE id = %s ", (id_,)) |
|
674 | if cursor.fetchone() is None: |
|
675 | cursor.close() |
|
676 | cnx.close() |
|
677 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
678 | description='API.SHOPFLOOR_NOT_FOUND') |
|
679 | ||
680 | query = (" SELECT id, name, uuid " |
|
681 | " FROM tbl_energy_categories ") |
|
682 | cursor.execute(query) |
|
683 | rows_energy_categories = cursor.fetchall() |
|
684 | ||
685 | energy_category_dict = dict() |
|
686 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
687 | for row in rows_energy_categories: |
|
688 | energy_category_dict[row[0]] = {"id": row[0], |
|
689 | "name": row[1], |
|
690 | "uuid": row[2]} |
|
691 | ||
692 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
693 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
|
694 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
|
695 | " ORDER BY m.id ") |
|
696 | cursor.execute(query, (id_,)) |
|
697 | rows = cursor.fetchall() |
|
698 | ||
699 | result = list() |
|
700 | if rows is not None and len(rows) > 0: |
|
701 | for row in rows: |
|
702 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
703 | "energy_category": energy_category_dict.get(row[3], None)} |
|
704 | result.append(meta_result) |
|
705 | ||
706 | resp.text = json.dumps(result) |
|
707 | ||
708 | @staticmethod |
|
709 | @user_logger |
|
710 | def on_post(req, resp, id_): |
|
711 | """Handles POST requests""" |
|
712 | admin_control(req) |
|
713 | try: |
|
714 | raw_json = req.stream.read().decode('utf-8') |
|
715 | except Exception as ex: |
|
716 | print(ex) |
|
717 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
718 | title='API.BAD_REQUEST', |
|
719 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
720 | ||
721 | if not id_.isdigit() or int(id_) <= 0: |
|
722 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
723 | description='API.INVALID_SHOPFLOOR_ID') |
|
724 | ||
725 | new_values = json.loads(raw_json) |
|
726 | ||
727 | if 'meter_id' not in new_values['data'].keys() or \ |
|
728 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
729 | new_values['data']['meter_id'] <= 0: |
|
730 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
731 | description='API.INVALID_METER_ID') |
|
732 | meter_id = new_values['data']['meter_id'] |
|
733 | ||
734 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
735 | cursor = cnx.cursor() |
|
736 | ||
737 | cursor.execute(" SELECT name " |
|
738 | " from tbl_shopfloors " |
|
739 | " WHERE id = %s ", (id_,)) |
|
740 | if cursor.fetchone() is None: |
|
741 | cursor.close() |
|
742 | cnx.close() |
|
743 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
744 | description='API.SHOPFLOOR_NOT_FOUND') |
|
745 | ||
746 | cursor.execute(" SELECT name " |
|
747 | " FROM tbl_meters " |
|
748 | " WHERE id = %s ", (meter_id,)) |
|
749 | if cursor.fetchone() is None: |
|
750 | cursor.close() |
|
751 | cnx.close() |
|
752 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
753 | description='API.METER_NOT_FOUND') |
|
754 | ||
755 | query = (" SELECT id " |
|
756 | " FROM tbl_shopfloors_meters " |
|
757 | " WHERE shopfloor_id = %s AND meter_id = %s") |
|
758 | cursor.execute(query, (id_, meter_id,)) |
|
759 | if cursor.fetchone() is not None: |
|
760 | cursor.close() |
|
761 | cnx.close() |
|
762 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
763 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
|
764 | ||
765 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
|
766 | " VALUES (%s, %s) ") |
|
767 | cursor.execute(add_row, (id_, meter_id,)) |
|
768 | cnx.commit() |
|
769 | cursor.close() |
|
770 | cnx.close() |
|
771 | ||
772 | resp.status = falcon.HTTP_201 |
|
773 | resp.location = '/shopfloors/' + str(id_) + '/meters/' + str(meter_id) |
|
774 | ||
775 | ||
776 | class ShopfloorMeterItem: |
|
@@ 1034-1161 (lines=128) @@ | ||
1031 | resp.status = falcon.HTTP_204 |
|
1032 | ||
1033 | ||
1034 | class ShopfloorPointCollection: |
|
1035 | def __init__(self): |
|
1036 | """Initializes ShopfloorPointCollection""" |
|
1037 | pass |
|
1038 | ||
1039 | @staticmethod |
|
1040 | def on_options(req, resp, id_): |
|
1041 | resp.status = falcon.HTTP_200 |
|
1042 | _ = req |
|
1043 | _ = id_ |
|
1044 | ||
1045 | @staticmethod |
|
1046 | def on_get(req, resp, id_): |
|
1047 | if 'API-KEY' not in req.headers or \ |
|
1048 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1049 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1050 | access_control(req) |
|
1051 | else: |
|
1052 | api_key_control(req) |
|
1053 | if not id_.isdigit() or int(id_) <= 0: |
|
1054 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1055 | description='API.INVALID_SHOPFLOOR_ID') |
|
1056 | ||
1057 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1058 | cursor = cnx.cursor() |
|
1059 | ||
1060 | cursor.execute(" SELECT name " |
|
1061 | " FROM tbl_shopfloors " |
|
1062 | " WHERE id = %s ", (id_,)) |
|
1063 | if cursor.fetchone() is None: |
|
1064 | cursor.close() |
|
1065 | cnx.close() |
|
1066 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1067 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1068 | ||
1069 | query = (" SELECT id, name, uuid " |
|
1070 | " FROM tbl_data_sources ") |
|
1071 | cursor.execute(query) |
|
1072 | rows_data_sources = cursor.fetchall() |
|
1073 | ||
1074 | data_source_dict = dict() |
|
1075 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
1076 | for row in rows_data_sources: |
|
1077 | data_source_dict[row[0]] = {"id": row[0], |
|
1078 | "name": row[1], |
|
1079 | "uuid": row[2]} |
|
1080 | ||
1081 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
1082 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
|
1083 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
|
1084 | " ORDER BY p.id ") |
|
1085 | cursor.execute(query, (id_,)) |
|
1086 | rows = cursor.fetchall() |
|
1087 | ||
1088 | result = list() |
|
1089 | if rows is not None and len(rows) > 0: |
|
1090 | for row in rows: |
|
1091 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
1092 | result.append(meta_result) |
|
1093 | ||
1094 | resp.text = json.dumps(result) |
|
1095 | ||
1096 | @staticmethod |
|
1097 | @user_logger |
|
1098 | def on_post(req, resp, id_): |
|
1099 | """Handles POST requests""" |
|
1100 | admin_control(req) |
|
1101 | try: |
|
1102 | raw_json = req.stream.read().decode('utf-8') |
|
1103 | except Exception as ex: |
|
1104 | print(ex) |
|
1105 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1106 | title='API.BAD_REQUEST', |
|
1107 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1108 | ||
1109 | if not id_.isdigit() or int(id_) <= 0: |
|
1110 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1111 | description='API.INVALID_SHOPFLOOR_ID') |
|
1112 | ||
1113 | new_values = json.loads(raw_json) |
|
1114 | ||
1115 | if 'point_id' not in new_values['data'].keys() or \ |
|
1116 | not isinstance(new_values['data']['point_id'], int) or \ |
|
1117 | new_values['data']['point_id'] <= 0: |
|
1118 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1119 | description='API.INVALID_POINT_ID') |
|
1120 | point_id = new_values['data']['point_id'] |
|
1121 | ||
1122 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1123 | cursor = cnx.cursor() |
|
1124 | ||
1125 | cursor.execute(" SELECT name " |
|
1126 | " from tbl_shopfloors " |
|
1127 | " WHERE id = %s ", (id_,)) |
|
1128 | if cursor.fetchone() is None: |
|
1129 | cursor.close() |
|
1130 | cnx.close() |
|
1131 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1132 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1133 | ||
1134 | cursor.execute(" SELECT name " |
|
1135 | " FROM tbl_points " |
|
1136 | " WHERE id = %s ", (point_id,)) |
|
1137 | if cursor.fetchone() is None: |
|
1138 | cursor.close() |
|
1139 | cnx.close() |
|
1140 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1141 | description='API.POINT_NOT_FOUND') |
|
1142 | ||
1143 | query = (" SELECT id " |
|
1144 | " FROM tbl_shopfloors_points " |
|
1145 | " WHERE shopfloor_id = %s AND point_id = %s") |
|
1146 | cursor.execute(query, (id_, point_id,)) |
|
1147 | if cursor.fetchone() is not None: |
|
1148 | cursor.close() |
|
1149 | cnx.close() |
|
1150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1151 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
|
1152 | ||
1153 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
|
1154 | " VALUES (%s, %s) ") |
|
1155 | cursor.execute(add_row, (id_, point_id,)) |
|
1156 | cnx.commit() |
|
1157 | cursor.close() |
|
1158 | cnx.close() |
|
1159 | ||
1160 | resp.status = falcon.HTTP_201 |
|
1161 | resp.location = '/shopfloors/' + str(id_) + '/points/' + str(point_id) |
|
1162 | ||
1163 | ||
1164 | class ShopfloorPointItem: |
@@ 1342-1470 (lines=129) @@ | ||
1339 | resp.status = falcon.HTTP_204 |
|
1340 | ||
1341 | ||
1342 | class StoreVirtualMeterCollection: |
|
1343 | def __init__(self): |
|
1344 | """Initializes Class""" |
|
1345 | pass |
|
1346 | ||
1347 | @staticmethod |
|
1348 | def on_options(req, resp, id_): |
|
1349 | _ = req |
|
1350 | resp.status = falcon.HTTP_200 |
|
1351 | _ = id_ |
|
1352 | ||
1353 | @staticmethod |
|
1354 | def on_get(req, resp, id_): |
|
1355 | if 'API-KEY' not in req.headers or \ |
|
1356 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1357 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1358 | access_control(req) |
|
1359 | else: |
|
1360 | api_key_control(req) |
|
1361 | if not id_.isdigit() or int(id_) <= 0: |
|
1362 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1363 | description='API.INVALID_STORE_ID') |
|
1364 | ||
1365 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1366 | cursor = cnx.cursor() |
|
1367 | ||
1368 | cursor.execute(" SELECT name " |
|
1369 | " FROM tbl_stores " |
|
1370 | " WHERE id = %s ", (id_,)) |
|
1371 | if cursor.fetchone() is None: |
|
1372 | cursor.close() |
|
1373 | cnx.close() |
|
1374 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1375 | description='API.STORE_NOT_FOUND') |
|
1376 | ||
1377 | query = (" SELECT id, name, uuid " |
|
1378 | " FROM tbl_energy_categories ") |
|
1379 | cursor.execute(query) |
|
1380 | rows_energy_categories = cursor.fetchall() |
|
1381 | ||
1382 | energy_category_dict = dict() |
|
1383 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1384 | for row in rows_energy_categories: |
|
1385 | energy_category_dict[row[0]] = {"id": row[0], |
|
1386 | "name": row[1], |
|
1387 | "uuid": row[2]} |
|
1388 | ||
1389 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
1390 | " FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " |
|
1391 | " WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
|
1392 | " ORDER BY m.id ") |
|
1393 | cursor.execute(query, (id_,)) |
|
1394 | rows = cursor.fetchall() |
|
1395 | ||
1396 | result = list() |
|
1397 | if rows is not None and len(rows) > 0: |
|
1398 | for row in rows: |
|
1399 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1400 | "energy_category": energy_category_dict.get(row[3], None)} |
|
1401 | result.append(meta_result) |
|
1402 | ||
1403 | resp.text = json.dumps(result) |
|
1404 | ||
1405 | @staticmethod |
|
1406 | @user_logger |
|
1407 | def on_post(req, resp, id_): |
|
1408 | """Handles POST requests""" |
|
1409 | admin_control(req) |
|
1410 | try: |
|
1411 | raw_json = req.stream.read().decode('utf-8') |
|
1412 | except Exception as ex: |
|
1413 | print(str(ex)) |
|
1414 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1415 | title='API.BAD_REQUEST', |
|
1416 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1417 | ||
1418 | if not id_.isdigit() or int(id_) <= 0: |
|
1419 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1420 | description='API.INVALID_STORE_ID') |
|
1421 | ||
1422 | new_values = json.loads(raw_json) |
|
1423 | ||
1424 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1425 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1426 | new_values['data']['virtual_meter_id'] <= 0: |
|
1427 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1428 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1429 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1430 | ||
1431 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1432 | cursor = cnx.cursor() |
|
1433 | ||
1434 | cursor.execute(" SELECT name " |
|
1435 | " from tbl_stores " |
|
1436 | " WHERE id = %s ", (id_,)) |
|
1437 | if cursor.fetchone() is None: |
|
1438 | cursor.close() |
|
1439 | cnx.close() |
|
1440 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1441 | description='API.STORE_NOT_FOUND') |
|
1442 | ||
1443 | cursor.execute(" SELECT name " |
|
1444 | " FROM tbl_virtual_meters " |
|
1445 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1446 | if cursor.fetchone() is None: |
|
1447 | cursor.close() |
|
1448 | cnx.close() |
|
1449 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1450 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1451 | ||
1452 | query = (" SELECT id " |
|
1453 | " FROM tbl_stores_virtual_meters " |
|
1454 | " WHERE store_id = %s AND virtual_meter_id = %s") |
|
1455 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1456 | if cursor.fetchone() is not None: |
|
1457 | cursor.close() |
|
1458 | cnx.close() |
|
1459 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1460 | description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') |
|
1461 | ||
1462 | add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " |
|
1463 | " VALUES (%s, %s) ") |
|
1464 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
1465 | cnx.commit() |
|
1466 | cursor.close() |
|
1467 | cnx.close() |
|
1468 | ||
1469 | resp.status = falcon.HTTP_201 |
|
1470 | resp.location = '/stores/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1471 | ||
1472 | ||
1473 | class StoreVirtualMeterItem: |
|
@@ 775-903 (lines=129) @@ | ||
772 | resp.status = falcon.HTTP_204 |
|
773 | ||
774 | ||
775 | class StoreOfflineMeterCollection: |
|
776 | def __init__(self): |
|
777 | """Initializes Class""" |
|
778 | pass |
|
779 | ||
780 | @staticmethod |
|
781 | def on_options(req, resp, id_): |
|
782 | _ = req |
|
783 | resp.status = falcon.HTTP_200 |
|
784 | _ = id_ |
|
785 | ||
786 | @staticmethod |
|
787 | def on_get(req, resp, id_): |
|
788 | if 'API-KEY' not in req.headers or \ |
|
789 | not isinstance(req.headers['API-KEY'], str) or \ |
|
790 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
791 | access_control(req) |
|
792 | else: |
|
793 | api_key_control(req) |
|
794 | if not id_.isdigit() or int(id_) <= 0: |
|
795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
796 | description='API.INVALID_STORE_ID') |
|
797 | ||
798 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
799 | cursor = cnx.cursor() |
|
800 | ||
801 | cursor.execute(" SELECT name " |
|
802 | " FROM tbl_stores " |
|
803 | " WHERE id = %s ", (id_,)) |
|
804 | if cursor.fetchone() is None: |
|
805 | cursor.close() |
|
806 | cnx.close() |
|
807 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
808 | description='API.STORE_NOT_FOUND') |
|
809 | ||
810 | query = (" SELECT id, name, uuid " |
|
811 | " FROM tbl_energy_categories ") |
|
812 | cursor.execute(query) |
|
813 | rows_energy_categories = cursor.fetchall() |
|
814 | ||
815 | energy_category_dict = dict() |
|
816 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
817 | for row in rows_energy_categories: |
|
818 | energy_category_dict[row[0]] = {"id": row[0], |
|
819 | "name": row[1], |
|
820 | "uuid": row[2]} |
|
821 | ||
822 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
823 | " FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " |
|
824 | " WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
825 | " ORDER BY m.id ") |
|
826 | cursor.execute(query, (id_,)) |
|
827 | rows = cursor.fetchall() |
|
828 | ||
829 | result = list() |
|
830 | if rows is not None and len(rows) > 0: |
|
831 | for row in rows: |
|
832 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
833 | "energy_category": energy_category_dict.get(row[3], None)} |
|
834 | result.append(meta_result) |
|
835 | ||
836 | resp.text = json.dumps(result) |
|
837 | ||
838 | @staticmethod |
|
839 | @user_logger |
|
840 | def on_post(req, resp, id_): |
|
841 | """Handles POST requests""" |
|
842 | admin_control(req) |
|
843 | try: |
|
844 | raw_json = req.stream.read().decode('utf-8') |
|
845 | except Exception as ex: |
|
846 | print(str(ex)) |
|
847 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
848 | title='API.BAD_REQUEST', |
|
849 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
850 | ||
851 | if not id_.isdigit() or int(id_) <= 0: |
|
852 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
853 | description='API.INVALID_STORE_ID') |
|
854 | ||
855 | new_values = json.loads(raw_json) |
|
856 | ||
857 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
858 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
859 | new_values['data']['offline_meter_id'] <= 0: |
|
860 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
861 | description='API.INVALID_OFFLINE_METER_ID') |
|
862 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
863 | ||
864 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
865 | cursor = cnx.cursor() |
|
866 | ||
867 | cursor.execute(" SELECT name " |
|
868 | " from tbl_stores " |
|
869 | " WHERE id = %s ", (id_,)) |
|
870 | if cursor.fetchone() is None: |
|
871 | cursor.close() |
|
872 | cnx.close() |
|
873 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
874 | description='API.STORE_NOT_FOUND') |
|
875 | ||
876 | cursor.execute(" SELECT name " |
|
877 | " FROM tbl_offline_meters " |
|
878 | " WHERE id = %s ", (offline_meter_id,)) |
|
879 | if cursor.fetchone() is None: |
|
880 | cursor.close() |
|
881 | cnx.close() |
|
882 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
883 | description='API.OFFLINE_METER_NOT_FOUND') |
|
884 | ||
885 | query = (" SELECT id " |
|
886 | " FROM tbl_stores_offline_meters " |
|
887 | " WHERE store_id = %s AND offline_meter_id = %s") |
|
888 | cursor.execute(query, (id_, offline_meter_id,)) |
|
889 | if cursor.fetchone() is not None: |
|
890 | cursor.close() |
|
891 | cnx.close() |
|
892 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
893 | description='API.STORE_OFFLINE_METER_RELATION_EXISTS') |
|
894 | ||
895 | add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " |
|
896 | " VALUES (%s, %s) ") |
|
897 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
898 | cnx.commit() |
|
899 | cursor.close() |
|
900 | cnx.close() |
|
901 | ||
902 | resp.status = falcon.HTTP_201 |
|
903 | resp.location = '/stores/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
904 | ||
905 | ||
906 | class StoreOfflineMeterItem: |
|
@@ 582-710 (lines=129) @@ | ||
579 | resp.status = falcon.HTTP_200 |
|
580 | ||
581 | ||
582 | class StoreMeterCollection: |
|
583 | def __init__(self): |
|
584 | """"Initializes StoreMeterCollection""" |
|
585 | pass |
|
586 | ||
587 | @staticmethod |
|
588 | def on_options(req, resp, id_): |
|
589 | _ = req |
|
590 | resp.status = falcon.HTTP_200 |
|
591 | _ = id_ |
|
592 | ||
593 | @staticmethod |
|
594 | def on_get(req, resp, id_): |
|
595 | if 'API-KEY' not in req.headers or \ |
|
596 | not isinstance(req.headers['API-KEY'], str) or \ |
|
597 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
598 | access_control(req) |
|
599 | else: |
|
600 | api_key_control(req) |
|
601 | if not id_.isdigit() or int(id_) <= 0: |
|
602 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
603 | description='API.INVALID_STORE_ID') |
|
604 | ||
605 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
606 | cursor = cnx.cursor() |
|
607 | ||
608 | cursor.execute(" SELECT name " |
|
609 | " FROM tbl_stores " |
|
610 | " WHERE id = %s ", (id_,)) |
|
611 | if cursor.fetchone() is None: |
|
612 | cursor.close() |
|
613 | cnx.close() |
|
614 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
615 | description='API.STORE_NOT_FOUND') |
|
616 | ||
617 | query = (" SELECT id, name, uuid " |
|
618 | " FROM tbl_energy_categories ") |
|
619 | cursor.execute(query) |
|
620 | rows_energy_categories = cursor.fetchall() |
|
621 | ||
622 | energy_category_dict = dict() |
|
623 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
624 | for row in rows_energy_categories: |
|
625 | energy_category_dict[row[0]] = {"id": row[0], |
|
626 | "name": row[1], |
|
627 | "uuid": row[2]} |
|
628 | ||
629 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
630 | " FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " |
|
631 | " WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
|
632 | " ORDER BY m.id ") |
|
633 | cursor.execute(query, (id_,)) |
|
634 | rows = cursor.fetchall() |
|
635 | ||
636 | result = list() |
|
637 | if rows is not None and len(rows) > 0: |
|
638 | for row in rows: |
|
639 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
640 | "energy_category": energy_category_dict.get(row[3], None)} |
|
641 | result.append(meta_result) |
|
642 | ||
643 | resp.text = json.dumps(result) |
|
644 | ||
645 | @staticmethod |
|
646 | @user_logger |
|
647 | def on_post(req, resp, id_): |
|
648 | """Handles POST requests""" |
|
649 | admin_control(req) |
|
650 | try: |
|
651 | raw_json = req.stream.read().decode('utf-8') |
|
652 | except Exception as ex: |
|
653 | print(str(ex)) |
|
654 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
655 | title='API.BAD_REQUEST', |
|
656 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
657 | ||
658 | if not id_.isdigit() or int(id_) <= 0: |
|
659 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
660 | description='API.INVALID_STORE_ID') |
|
661 | ||
662 | new_values = json.loads(raw_json) |
|
663 | ||
664 | if 'meter_id' not in new_values['data'].keys() or \ |
|
665 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
666 | new_values['data']['meter_id'] <= 0: |
|
667 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
668 | description='API.INVALID_METER_ID') |
|
669 | meter_id = new_values['data']['meter_id'] |
|
670 | ||
671 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
672 | cursor = cnx.cursor() |
|
673 | ||
674 | cursor.execute(" SELECT name " |
|
675 | " from tbl_stores " |
|
676 | " WHERE id = %s ", (id_,)) |
|
677 | if cursor.fetchone() is None: |
|
678 | cursor.close() |
|
679 | cnx.close() |
|
680 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
681 | description='API.STORE_NOT_FOUND') |
|
682 | ||
683 | cursor.execute(" SELECT name " |
|
684 | " FROM tbl_meters " |
|
685 | " WHERE id = %s ", (meter_id,)) |
|
686 | if cursor.fetchone() is None: |
|
687 | cursor.close() |
|
688 | cnx.close() |
|
689 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
690 | description='API.METER_NOT_FOUND') |
|
691 | ||
692 | query = (" SELECT id " |
|
693 | " FROM tbl_stores_meters " |
|
694 | " WHERE store_id = %s AND meter_id = %s") |
|
695 | cursor.execute(query, (id_, meter_id,)) |
|
696 | if cursor.fetchone() is not None: |
|
697 | cursor.close() |
|
698 | cnx.close() |
|
699 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
700 | description='API.STORE_METER_RELATION_EXISTS') |
|
701 | ||
702 | add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " |
|
703 | " VALUES (%s, %s) ") |
|
704 | cursor.execute(add_row, (id_, meter_id,)) |
|
705 | cnx.commit() |
|
706 | cursor.close() |
|
707 | cnx.close() |
|
708 | ||
709 | resp.status = falcon.HTTP_201 |
|
710 | resp.location = '/stores/' + str(id_) + '/meters/' + str(meter_id) |
|
711 | ||
712 | ||
713 | class StoreMeterItem: |
|
@@ 969-1096 (lines=128) @@ | ||
966 | resp.status = falcon.HTTP_204 |
|
967 | ||
968 | ||
969 | class StorePointCollection: |
|
970 | def __init__(self): |
|
971 | """Initializes Class""" |
|
972 | pass |
|
973 | ||
974 | @staticmethod |
|
975 | def on_options(req, resp, id_): |
|
976 | _ = req |
|
977 | resp.status = falcon.HTTP_200 |
|
978 | _ = id_ |
|
979 | ||
980 | @staticmethod |
|
981 | def on_get(req, resp, id_): |
|
982 | if 'API-KEY' not in req.headers or \ |
|
983 | not isinstance(req.headers['API-KEY'], str) or \ |
|
984 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
985 | access_control(req) |
|
986 | else: |
|
987 | api_key_control(req) |
|
988 | if not id_.isdigit() or int(id_) <= 0: |
|
989 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
990 | description='API.INVALID_STORE_ID') |
|
991 | ||
992 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
993 | cursor = cnx.cursor() |
|
994 | ||
995 | cursor.execute(" SELECT name " |
|
996 | " FROM tbl_stores " |
|
997 | " WHERE id = %s ", (id_,)) |
|
998 | if cursor.fetchone() is None: |
|
999 | cursor.close() |
|
1000 | cnx.close() |
|
1001 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1002 | description='API.STORE_NOT_FOUND') |
|
1003 | ||
1004 | query = (" SELECT id, name, uuid " |
|
1005 | " FROM tbl_data_sources ") |
|
1006 | cursor.execute(query) |
|
1007 | rows_data_sources = cursor.fetchall() |
|
1008 | ||
1009 | data_source_dict = dict() |
|
1010 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
1011 | for row in rows_data_sources: |
|
1012 | data_source_dict[row[0]] = {"id": row[0], |
|
1013 | "name": row[1], |
|
1014 | "uuid": row[2]} |
|
1015 | ||
1016 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
1017 | " FROM tbl_stores t, tbl_stores_points tp, tbl_points p " |
|
1018 | " WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " |
|
1019 | " ORDER BY p.id ") |
|
1020 | cursor.execute(query, (id_,)) |
|
1021 | rows = cursor.fetchall() |
|
1022 | ||
1023 | result = list() |
|
1024 | if rows is not None and len(rows) > 0: |
|
1025 | for row in rows: |
|
1026 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
1027 | result.append(meta_result) |
|
1028 | ||
1029 | resp.text = json.dumps(result) |
|
1030 | ||
1031 | @staticmethod |
|
1032 | @user_logger |
|
1033 | def on_post(req, resp, id_): |
|
1034 | """Handles POST requests""" |
|
1035 | admin_control(req) |
|
1036 | try: |
|
1037 | raw_json = req.stream.read().decode('utf-8') |
|
1038 | except Exception as ex: |
|
1039 | print(str(ex)) |
|
1040 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1041 | title='API.BAD_REQUEST', |
|
1042 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1043 | ||
1044 | if not id_.isdigit() or int(id_) <= 0: |
|
1045 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1046 | description='API.INVALID_STORE_ID') |
|
1047 | ||
1048 | new_values = json.loads(raw_json) |
|
1049 | ||
1050 | if 'point_id' not in new_values['data'].keys() or \ |
|
1051 | not isinstance(new_values['data']['point_id'], int) or \ |
|
1052 | new_values['data']['point_id'] <= 0: |
|
1053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1054 | description='API.INVALID_POINT_ID') |
|
1055 | point_id = new_values['data']['point_id'] |
|
1056 | ||
1057 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1058 | cursor = cnx.cursor() |
|
1059 | ||
1060 | cursor.execute(" SELECT name " |
|
1061 | " from tbl_stores " |
|
1062 | " WHERE id = %s ", (id_,)) |
|
1063 | if cursor.fetchone() is None: |
|
1064 | cursor.close() |
|
1065 | cnx.close() |
|
1066 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1067 | description='API.STORE_NOT_FOUND') |
|
1068 | ||
1069 | cursor.execute(" SELECT name " |
|
1070 | " FROM tbl_points " |
|
1071 | " WHERE id = %s ", (point_id,)) |
|
1072 | if cursor.fetchone() is None: |
|
1073 | cursor.close() |
|
1074 | cnx.close() |
|
1075 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1076 | description='API.POINT_NOT_FOUND') |
|
1077 | ||
1078 | query = (" SELECT id " |
|
1079 | " FROM tbl_stores_points " |
|
1080 | " WHERE store_id = %s AND point_id = %s") |
|
1081 | cursor.execute(query, (id_, point_id,)) |
|
1082 | if cursor.fetchone() is not None: |
|
1083 | cursor.close() |
|
1084 | cnx.close() |
|
1085 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1086 | description='API.STORE_POINT_RELATION_EXISTS') |
|
1087 | ||
1088 | add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " |
|
1089 | " VALUES (%s, %s) ") |
|
1090 | cursor.execute(add_row, (id_, point_id,)) |
|
1091 | cnx.commit() |
|
1092 | cursor.close() |
|
1093 | cnx.close() |
|
1094 | ||
1095 | resp.status = falcon.HTTP_201 |
|
1096 | resp.location = '/stores/' + str(id_) + '/points/' + str(point_id) |
|
1097 | ||
1098 | ||
1099 | class StorePointItem: |