| @@ 1509-1643 (lines=135) @@ | ||
| 1506 | resp.status = falcon.HTTP_204 |
|
| 1507 | ||
| 1508 | ||
| 1509 | class TenantVirtualMeterCollection: |
|
| 1510 | def __init__(self): |
|
| 1511 | pass |
|
| 1512 | ||
| 1513 | @staticmethod |
|
| 1514 | def on_options(req, resp, id_): |
|
| 1515 | _ = req |
|
| 1516 | resp.status = falcon.HTTP_200 |
|
| 1517 | _ = id_ |
|
| 1518 | ||
| 1519 | @staticmethod |
|
| 1520 | def on_get(req, resp, id_): |
|
| 1521 | if 'API-KEY' not in req.headers or \ |
|
| 1522 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1523 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1524 | access_control(req) |
|
| 1525 | else: |
|
| 1526 | api_key_control(req) |
|
| 1527 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1528 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1529 | description='API.INVALID_TENANT_ID') |
|
| 1530 | ||
| 1531 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1532 | cursor = cnx.cursor() |
|
| 1533 | ||
| 1534 | cursor.execute(" SELECT name " |
|
| 1535 | " FROM tbl_tenants " |
|
| 1536 | " WHERE id = %s ", (id_,)) |
|
| 1537 | if cursor.fetchone() is None: |
|
| 1538 | cursor.close() |
|
| 1539 | cnx.close() |
|
| 1540 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1541 | description='API.TENANT_NOT_FOUND') |
|
| 1542 | ||
| 1543 | query = (" SELECT id, name, uuid " |
|
| 1544 | " FROM tbl_energy_categories ") |
|
| 1545 | cursor.execute(query) |
|
| 1546 | rows_energy_categories = cursor.fetchall() |
|
| 1547 | ||
| 1548 | energy_category_dict = dict() |
|
| 1549 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 1550 | for row in rows_energy_categories: |
|
| 1551 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 1552 | "name": row[1], |
|
| 1553 | "uuid": row[2]} |
|
| 1554 | ||
| 1555 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 1556 | " FROM tbl_tenants t, tbl_tenants_virtual_meters tm, tbl_virtual_meters m " |
|
| 1557 | " WHERE tm.tenant_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
|
| 1558 | " ORDER BY m.id ") |
|
| 1559 | cursor.execute(query, (id_,)) |
|
| 1560 | rows = cursor.fetchall() |
|
| 1561 | ||
| 1562 | result = list() |
|
| 1563 | if rows is not None and len(rows) > 0: |
|
| 1564 | for row in rows: |
|
| 1565 | meta_result = {"id": row[0], |
|
| 1566 | "name": row[1], |
|
| 1567 | "uuid": row[2], |
|
| 1568 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 1569 | result.append(meta_result) |
|
| 1570 | ||
| 1571 | resp.text = json.dumps(result) |
|
| 1572 | ||
| 1573 | @staticmethod |
|
| 1574 | @user_logger |
|
| 1575 | def on_post(req, resp, id_): |
|
| 1576 | """Handles POST requests""" |
|
| 1577 | admin_control(req) |
|
| 1578 | try: |
|
| 1579 | raw_json = req.stream.read().decode('utf-8') |
|
| 1580 | except UnicodeDecodeError as ex: |
|
| 1581 | print("Failed to decode request") |
|
| 1582 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1583 | title='API.BAD_REQUEST', |
|
| 1584 | description='API.INVALID_ENCODING') |
|
| 1585 | except Exception as ex: |
|
| 1586 | print("Unexpected error reading request stream") |
|
| 1587 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1588 | title='API.BAD_REQUEST', |
|
| 1589 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1590 | ||
| 1591 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1592 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1593 | description='API.INVALID_TENANT_ID') |
|
| 1594 | ||
| 1595 | new_values = json.loads(raw_json) |
|
| 1596 | ||
| 1597 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
| 1598 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
| 1599 | new_values['data']['virtual_meter_id'] <= 0: |
|
| 1600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1601 | description='API.INVALID_VIRTUAL_METER_ID') |
|
| 1602 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
| 1603 | ||
| 1604 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1605 | cursor = cnx.cursor() |
|
| 1606 | ||
| 1607 | cursor.execute(" SELECT name " |
|
| 1608 | " from tbl_tenants " |
|
| 1609 | " WHERE id = %s ", (id_,)) |
|
| 1610 | if cursor.fetchone() is None: |
|
| 1611 | cursor.close() |
|
| 1612 | cnx.close() |
|
| 1613 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1614 | description='API.TENANT_NOT_FOUND') |
|
| 1615 | ||
| 1616 | cursor.execute(" SELECT name " |
|
| 1617 | " FROM tbl_virtual_meters " |
|
| 1618 | " WHERE id = %s ", (virtual_meter_id,)) |
|
| 1619 | if cursor.fetchone() is None: |
|
| 1620 | cursor.close() |
|
| 1621 | cnx.close() |
|
| 1622 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1623 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
| 1624 | ||
| 1625 | query = (" SELECT id " |
|
| 1626 | " FROM tbl_tenants_virtual_meters " |
|
| 1627 | " WHERE tenant_id = %s AND virtual_meter_id = %s") |
|
| 1628 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
| 1629 | if cursor.fetchone() is not None: |
|
| 1630 | cursor.close() |
|
| 1631 | cnx.close() |
|
| 1632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1633 | description='API.TENANT_VIRTUAL_METER_RELATION_EXISTS') |
|
| 1634 | ||
| 1635 | add_row = (" INSERT INTO tbl_tenants_virtual_meters (tenant_id, virtual_meter_id) " |
|
| 1636 | " VALUES (%s, %s) ") |
|
| 1637 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
| 1638 | cnx.commit() |
|
| 1639 | cursor.close() |
|
| 1640 | cnx.close() |
|
| 1641 | ||
| 1642 | resp.status = falcon.HTTP_201 |
|
| 1643 | resp.location = '/tenants/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
| 1644 | ||
| 1645 | ||
| 1646 | class TenantVirtualMeterItem: |
|
| @@ 927-1061 (lines=135) @@ | ||
| 924 | resp.status = falcon.HTTP_204 |
|
| 925 | ||
| 926 | ||
| 927 | class TenantOfflineMeterCollection: |
|
| 928 | def __init__(self): |
|
| 929 | pass |
|
| 930 | ||
| 931 | @staticmethod |
|
| 932 | def on_options(req, resp, id_): |
|
| 933 | _ = req |
|
| 934 | resp.status = falcon.HTTP_200 |
|
| 935 | _ = id_ |
|
| 936 | ||
| 937 | @staticmethod |
|
| 938 | def on_get(req, resp, id_): |
|
| 939 | if 'API-KEY' not in req.headers or \ |
|
| 940 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 941 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 942 | access_control(req) |
|
| 943 | else: |
|
| 944 | api_key_control(req) |
|
| 945 | if not id_.isdigit() or int(id_) <= 0: |
|
| 946 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 947 | description='API.INVALID_TENANT_ID') |
|
| 948 | ||
| 949 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 950 | cursor = cnx.cursor() |
|
| 951 | ||
| 952 | cursor.execute(" SELECT name " |
|
| 953 | " FROM tbl_tenants " |
|
| 954 | " WHERE id = %s ", (id_,)) |
|
| 955 | if cursor.fetchone() is None: |
|
| 956 | cursor.close() |
|
| 957 | cnx.close() |
|
| 958 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 959 | description='API.TENANT_NOT_FOUND') |
|
| 960 | ||
| 961 | query = (" SELECT id, name, uuid " |
|
| 962 | " FROM tbl_energy_categories ") |
|
| 963 | cursor.execute(query) |
|
| 964 | rows_energy_categories = cursor.fetchall() |
|
| 965 | ||
| 966 | energy_category_dict = dict() |
|
| 967 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 968 | for row in rows_energy_categories: |
|
| 969 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 970 | "name": row[1], |
|
| 971 | "uuid": row[2]} |
|
| 972 | ||
| 973 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 974 | " FROM tbl_tenants s, tbl_tenants_offline_meters sm, tbl_offline_meters m " |
|
| 975 | " WHERE sm.tenant_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
| 976 | " ORDER BY m.id ") |
|
| 977 | cursor.execute(query, (id_,)) |
|
| 978 | rows = cursor.fetchall() |
|
| 979 | ||
| 980 | result = list() |
|
| 981 | if rows is not None and len(rows) > 0: |
|
| 982 | for row in rows: |
|
| 983 | meta_result = {"id": row[0], |
|
| 984 | "name": row[1], |
|
| 985 | "uuid": row[2], |
|
| 986 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 987 | result.append(meta_result) |
|
| 988 | ||
| 989 | resp.text = json.dumps(result) |
|
| 990 | ||
| 991 | @staticmethod |
|
| 992 | @user_logger |
|
| 993 | def on_post(req, resp, id_): |
|
| 994 | """Handles POST requests""" |
|
| 995 | admin_control(req) |
|
| 996 | try: |
|
| 997 | raw_json = req.stream.read().decode('utf-8') |
|
| 998 | except UnicodeDecodeError as ex: |
|
| 999 | print("Failed to decode request") |
|
| 1000 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1001 | title='API.BAD_REQUEST', |
|
| 1002 | description='API.INVALID_ENCODING') |
|
| 1003 | except Exception as ex: |
|
| 1004 | print("Unexpected error reading request stream") |
|
| 1005 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1006 | title='API.BAD_REQUEST', |
|
| 1007 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1008 | ||
| 1009 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1011 | description='API.INVALID_TENANT_ID') |
|
| 1012 | ||
| 1013 | new_values = json.loads(raw_json) |
|
| 1014 | ||
| 1015 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
| 1016 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
| 1017 | new_values['data']['offline_meter_id'] <= 0: |
|
| 1018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1019 | description='API.INVALID_OFFLINE_METER_ID') |
|
| 1020 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
| 1021 | ||
| 1022 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1023 | cursor = cnx.cursor() |
|
| 1024 | ||
| 1025 | cursor.execute(" SELECT name " |
|
| 1026 | " from tbl_tenants " |
|
| 1027 | " WHERE id = %s ", (id_,)) |
|
| 1028 | if cursor.fetchone() is None: |
|
| 1029 | cursor.close() |
|
| 1030 | cnx.close() |
|
| 1031 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1032 | description='API.TENANT_NOT_FOUND') |
|
| 1033 | ||
| 1034 | cursor.execute(" SELECT name " |
|
| 1035 | " FROM tbl_offline_meters " |
|
| 1036 | " WHERE id = %s ", (offline_meter_id,)) |
|
| 1037 | if cursor.fetchone() is None: |
|
| 1038 | cursor.close() |
|
| 1039 | cnx.close() |
|
| 1040 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1041 | description='API.OFFLINE_METER_NOT_FOUND') |
|
| 1042 | ||
| 1043 | query = (" SELECT id " |
|
| 1044 | " FROM tbl_tenants_offline_meters " |
|
| 1045 | " WHERE tenant_id = %s AND offline_meter_id = %s") |
|
| 1046 | cursor.execute(query, (id_, offline_meter_id,)) |
|
| 1047 | if cursor.fetchone() is not None: |
|
| 1048 | cursor.close() |
|
| 1049 | cnx.close() |
|
| 1050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1051 | description='API.TENANT_OFFLINE_METER_RELATION_EXISTS') |
|
| 1052 | ||
| 1053 | add_row = (" INSERT INTO tbl_tenants_offline_meters (tenant_id, offline_meter_id) " |
|
| 1054 | " VALUES (%s, %s) ") |
|
| 1055 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
| 1056 | cnx.commit() |
|
| 1057 | cursor.close() |
|
| 1058 | cnx.close() |
|
| 1059 | ||
| 1060 | resp.status = falcon.HTTP_201 |
|
| 1061 | resp.location = '/tenants/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
| 1062 | ||
| 1063 | ||
| 1064 | class TenantOfflineMeterItem: |
|
| @@ 729-863 (lines=135) @@ | ||
| 726 | resp.status = falcon.HTTP_200 |
|
| 727 | ||
| 728 | ||
| 729 | class TenantMeterCollection: |
|
| 730 | def __init__(self): |
|
| 731 | pass |
|
| 732 | ||
| 733 | @staticmethod |
|
| 734 | def on_options(req, resp, id_): |
|
| 735 | _ = req |
|
| 736 | resp.status = falcon.HTTP_200 |
|
| 737 | _ = id_ |
|
| 738 | ||
| 739 | @staticmethod |
|
| 740 | def on_get(req, resp, id_): |
|
| 741 | if 'API-KEY' not in req.headers or \ |
|
| 742 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 743 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 744 | access_control(req) |
|
| 745 | else: |
|
| 746 | api_key_control(req) |
|
| 747 | if not id_.isdigit() or int(id_) <= 0: |
|
| 748 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 749 | description='API.INVALID_TENANT_ID') |
|
| 750 | ||
| 751 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 752 | cursor = cnx.cursor() |
|
| 753 | ||
| 754 | cursor.execute(" SELECT name " |
|
| 755 | " FROM tbl_tenants " |
|
| 756 | " WHERE id = %s ", (id_,)) |
|
| 757 | if cursor.fetchone() is None: |
|
| 758 | cursor.close() |
|
| 759 | cnx.close() |
|
| 760 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 761 | description='API.TENANT_NOT_FOUND') |
|
| 762 | ||
| 763 | query = (" SELECT id, name, uuid " |
|
| 764 | " FROM tbl_energy_categories ") |
|
| 765 | cursor.execute(query) |
|
| 766 | rows_energy_categories = cursor.fetchall() |
|
| 767 | ||
| 768 | energy_category_dict = dict() |
|
| 769 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 770 | for row in rows_energy_categories: |
|
| 771 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 772 | "name": row[1], |
|
| 773 | "uuid": row[2]} |
|
| 774 | ||
| 775 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 776 | " FROM tbl_tenants t, tbl_tenants_meters tm, tbl_meters m " |
|
| 777 | " WHERE tm.tenant_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
|
| 778 | " ORDER BY m.id ") |
|
| 779 | cursor.execute(query, (id_,)) |
|
| 780 | rows = cursor.fetchall() |
|
| 781 | ||
| 782 | result = list() |
|
| 783 | if rows is not None and len(rows) > 0: |
|
| 784 | for row in rows: |
|
| 785 | meta_result = {"id": row[0], |
|
| 786 | "name": row[1], |
|
| 787 | "uuid": row[2], |
|
| 788 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 789 | result.append(meta_result) |
|
| 790 | ||
| 791 | resp.text = json.dumps(result) |
|
| 792 | ||
| 793 | @staticmethod |
|
| 794 | @user_logger |
|
| 795 | def on_post(req, resp, id_): |
|
| 796 | """Handles POST requests""" |
|
| 797 | admin_control(req) |
|
| 798 | try: |
|
| 799 | raw_json = req.stream.read().decode('utf-8') |
|
| 800 | except UnicodeDecodeError as ex: |
|
| 801 | print("Failed to decode request") |
|
| 802 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 803 | title='API.BAD_REQUEST', |
|
| 804 | description='API.INVALID_ENCODING') |
|
| 805 | except Exception as ex: |
|
| 806 | print("Unexpected error reading request stream") |
|
| 807 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 808 | title='API.BAD_REQUEST', |
|
| 809 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 810 | ||
| 811 | if not id_.isdigit() or int(id_) <= 0: |
|
| 812 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 813 | description='API.INVALID_TENANT_ID') |
|
| 814 | ||
| 815 | new_values = json.loads(raw_json) |
|
| 816 | ||
| 817 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 818 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 819 | new_values['data']['meter_id'] <= 0: |
|
| 820 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 821 | description='API.INVALID_METER_ID') |
|
| 822 | meter_id = new_values['data']['meter_id'] |
|
| 823 | ||
| 824 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 825 | cursor = cnx.cursor() |
|
| 826 | ||
| 827 | cursor.execute(" SELECT name " |
|
| 828 | " from tbl_tenants " |
|
| 829 | " WHERE id = %s ", (id_,)) |
|
| 830 | if cursor.fetchone() is None: |
|
| 831 | cursor.close() |
|
| 832 | cnx.close() |
|
| 833 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 834 | description='API.TENANT_NOT_FOUND') |
|
| 835 | ||
| 836 | cursor.execute(" SELECT name " |
|
| 837 | " FROM tbl_meters " |
|
| 838 | " WHERE id = %s ", (meter_id,)) |
|
| 839 | if cursor.fetchone() is None: |
|
| 840 | cursor.close() |
|
| 841 | cnx.close() |
|
| 842 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 843 | description='API.METER_NOT_FOUND') |
|
| 844 | ||
| 845 | query = (" SELECT id " |
|
| 846 | " FROM tbl_tenants_meters " |
|
| 847 | " WHERE tenant_id = %s AND meter_id = %s") |
|
| 848 | cursor.execute(query, (id_, meter_id,)) |
|
| 849 | if cursor.fetchone() is not None: |
|
| 850 | cursor.close() |
|
| 851 | cnx.close() |
|
| 852 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 853 | description='API.TENANT_METER_RELATION_EXISTS') |
|
| 854 | ||
| 855 | add_row = (" INSERT INTO tbl_tenants_meters (tenant_id, meter_id) " |
|
| 856 | " VALUES (%s, %s) ") |
|
| 857 | cursor.execute(add_row, (id_, meter_id,)) |
|
| 858 | cnx.commit() |
|
| 859 | cursor.close() |
|
| 860 | cnx.close() |
|
| 861 | ||
| 862 | resp.status = falcon.HTTP_201 |
|
| 863 | resp.location = '/tenants/' + str(id_) + '/meters/' + str(meter_id) |
|
| 864 | ||
| 865 | ||
| 866 | class TenantMeterItem: |
|
| @@ 1126-1259 (lines=134) @@ | ||
| 1123 | resp.status = falcon.HTTP_204 |
|
| 1124 | ||
| 1125 | ||
| 1126 | class TenantPointCollection: |
|
| 1127 | def __init__(self): |
|
| 1128 | pass |
|
| 1129 | ||
| 1130 | @staticmethod |
|
| 1131 | def on_options(req, resp, id_): |
|
| 1132 | _ = req |
|
| 1133 | resp.status = falcon.HTTP_200 |
|
| 1134 | _ = id_ |
|
| 1135 | ||
| 1136 | @staticmethod |
|
| 1137 | def on_get(req, resp, id_): |
|
| 1138 | if 'API-KEY' not in req.headers or \ |
|
| 1139 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1140 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1141 | access_control(req) |
|
| 1142 | else: |
|
| 1143 | api_key_control(req) |
|
| 1144 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1146 | description='API.INVALID_TENANT_ID') |
|
| 1147 | ||
| 1148 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1149 | cursor = cnx.cursor() |
|
| 1150 | ||
| 1151 | cursor.execute(" SELECT name " |
|
| 1152 | " FROM tbl_tenants " |
|
| 1153 | " WHERE id = %s ", (id_,)) |
|
| 1154 | if cursor.fetchone() is None: |
|
| 1155 | cursor.close() |
|
| 1156 | cnx.close() |
|
| 1157 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1158 | description='API.TENANT_NOT_FOUND') |
|
| 1159 | ||
| 1160 | query = (" SELECT id, name, uuid " |
|
| 1161 | " FROM tbl_data_sources ") |
|
| 1162 | cursor.execute(query) |
|
| 1163 | rows_data_sources = cursor.fetchall() |
|
| 1164 | ||
| 1165 | data_source_dict = dict() |
|
| 1166 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
| 1167 | for row in rows_data_sources: |
|
| 1168 | data_source_dict[row[0]] = {"id": row[0], |
|
| 1169 | "name": row[1], |
|
| 1170 | "uuid": row[2]} |
|
| 1171 | ||
| 1172 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
| 1173 | " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
|
| 1174 | " WHERE tp.tenant_id = t.id AND p.id = tp.point_id AND t.id = %s " |
|
| 1175 | " ORDER BY p.id ") |
|
| 1176 | cursor.execute(query, (id_,)) |
|
| 1177 | rows = cursor.fetchall() |
|
| 1178 | ||
| 1179 | result = list() |
|
| 1180 | if rows is not None and len(rows) > 0: |
|
| 1181 | for row in rows: |
|
| 1182 | meta_result = {"id": row[0], |
|
| 1183 | "name": row[1], |
|
| 1184 | "data_source": data_source_dict.get(row[2], None)} |
|
| 1185 | result.append(meta_result) |
|
| 1186 | ||
| 1187 | resp.text = json.dumps(result) |
|
| 1188 | ||
| 1189 | @staticmethod |
|
| 1190 | @user_logger |
|
| 1191 | def on_post(req, resp, id_): |
|
| 1192 | """Handles POST requests""" |
|
| 1193 | admin_control(req) |
|
| 1194 | try: |
|
| 1195 | raw_json = req.stream.read().decode('utf-8') |
|
| 1196 | except UnicodeDecodeError as ex: |
|
| 1197 | print("Failed to decode request") |
|
| 1198 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1199 | title='API.BAD_REQUEST', |
|
| 1200 | description='API.INVALID_ENCODING') |
|
| 1201 | except Exception as ex: |
|
| 1202 | print("Unexpected error reading request stream") |
|
| 1203 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1204 | title='API.BAD_REQUEST', |
|
| 1205 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1206 | ||
| 1207 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1208 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1209 | description='API.INVALID_TENANT_ID') |
|
| 1210 | ||
| 1211 | new_values = json.loads(raw_json) |
|
| 1212 | ||
| 1213 | if 'point_id' not in new_values['data'].keys() or \ |
|
| 1214 | not isinstance(new_values['data']['point_id'], int) or \ |
|
| 1215 | new_values['data']['point_id'] <= 0: |
|
| 1216 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1217 | description='API.INVALID_POINT_ID') |
|
| 1218 | point_id = new_values['data']['point_id'] |
|
| 1219 | ||
| 1220 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1221 | cursor = cnx.cursor() |
|
| 1222 | ||
| 1223 | cursor.execute(" SELECT name " |
|
| 1224 | " from tbl_tenants " |
|
| 1225 | " WHERE id = %s ", (id_,)) |
|
| 1226 | if cursor.fetchone() is None: |
|
| 1227 | cursor.close() |
|
| 1228 | cnx.close() |
|
| 1229 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1230 | description='API.TENANT_NOT_FOUND') |
|
| 1231 | ||
| 1232 | cursor.execute(" SELECT name " |
|
| 1233 | " FROM tbl_points " |
|
| 1234 | " WHERE id = %s ", (point_id,)) |
|
| 1235 | if cursor.fetchone() is None: |
|
| 1236 | cursor.close() |
|
| 1237 | cnx.close() |
|
| 1238 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1239 | description='API.POINT_NOT_FOUND') |
|
| 1240 | ||
| 1241 | query = (" SELECT id " |
|
| 1242 | " FROM tbl_tenants_points " |
|
| 1243 | " WHERE tenant_id = %s AND point_id = %s") |
|
| 1244 | cursor.execute(query, (id_, point_id,)) |
|
| 1245 | if cursor.fetchone() is not None: |
|
| 1246 | cursor.close() |
|
| 1247 | cnx.close() |
|
| 1248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1249 | description='API.TENANT_POINT_RELATION_EXISTS') |
|
| 1250 | ||
| 1251 | add_row = (" INSERT INTO tbl_tenants_points (tenant_id, point_id) " |
|
| 1252 | " VALUES (%s, %s) ") |
|
| 1253 | cursor.execute(add_row, (id_, point_id,)) |
|
| 1254 | cnx.commit() |
|
| 1255 | cursor.close() |
|
| 1256 | cnx.close() |
|
| 1257 | ||
| 1258 | resp.status = falcon.HTTP_201 |
|
| 1259 | resp.location = '/tenants/' + str(id_) + '/points/' + str(point_id) |
|
| 1260 | ||
| 1261 | ||
| 1262 | class TenantPointItem: |
|
| @@ 3146-3278 (lines=133) @@ | ||
| 3143 | resp.status = falcon.HTTP_204 |
|
| 3144 | ||
| 3145 | ||
| 3146 | class SpaceVirtualMeterCollection: |
|
| 3147 | def __init__(self): |
|
| 3148 | pass |
|
| 3149 | ||
| 3150 | @staticmethod |
|
| 3151 | def on_options(req, resp, id_): |
|
| 3152 | _ = req |
|
| 3153 | resp.status = falcon.HTTP_200 |
|
| 3154 | _ = id_ |
|
| 3155 | ||
| 3156 | @staticmethod |
|
| 3157 | def on_get(req, resp, id_): |
|
| 3158 | if 'API-KEY' not in req.headers or \ |
|
| 3159 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3160 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3161 | access_control(req) |
|
| 3162 | else: |
|
| 3163 | api_key_control(req) |
|
| 3164 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3166 | description='API.INVALID_SPACE_ID') |
|
| 3167 | ||
| 3168 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3169 | cursor = cnx.cursor() |
|
| 3170 | ||
| 3171 | cursor.execute(" SELECT name " |
|
| 3172 | " FROM tbl_spaces " |
|
| 3173 | " WHERE id = %s ", (id_,)) |
|
| 3174 | if cursor.fetchone() is None: |
|
| 3175 | cursor.close() |
|
| 3176 | cnx.close() |
|
| 3177 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3178 | description='API.SPACE_NOT_FOUND') |
|
| 3179 | ||
| 3180 | query = (" SELECT id, name, uuid " |
|
| 3181 | " FROM tbl_energy_categories ") |
|
| 3182 | cursor.execute(query) |
|
| 3183 | rows_energy_categories = cursor.fetchall() |
|
| 3184 | ||
| 3185 | energy_category_dict = dict() |
|
| 3186 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 3187 | for row in rows_energy_categories: |
|
| 3188 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 3189 | "name": row[1], |
|
| 3190 | "uuid": row[2]} |
|
| 3191 | ||
| 3192 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 3193 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
|
| 3194 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
|
| 3195 | " ORDER BY m.id ") |
|
| 3196 | cursor.execute(query, (id_,)) |
|
| 3197 | rows = cursor.fetchall() |
|
| 3198 | ||
| 3199 | result = list() |
|
| 3200 | if rows is not None and len(rows) > 0: |
|
| 3201 | for row in rows: |
|
| 3202 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 3203 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 3204 | result.append(meta_result) |
|
| 3205 | ||
| 3206 | resp.text = json.dumps(result) |
|
| 3207 | ||
| 3208 | @staticmethod |
|
| 3209 | @user_logger |
|
| 3210 | def on_post(req, resp, id_): |
|
| 3211 | """Handles POST requests""" |
|
| 3212 | admin_control(req) |
|
| 3213 | try: |
|
| 3214 | raw_json = req.stream.read().decode('utf-8') |
|
| 3215 | except UnicodeDecodeError as ex: |
|
| 3216 | print("Failed to decode request") |
|
| 3217 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3218 | title='API.BAD_REQUEST', |
|
| 3219 | description='API.INVALID_ENCODING') |
|
| 3220 | except Exception as ex: |
|
| 3221 | print("Unexpected error reading request stream") |
|
| 3222 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3223 | title='API.BAD_REQUEST', |
|
| 3224 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3225 | ||
| 3226 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3227 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3228 | description='API.INVALID_SPACE_ID') |
|
| 3229 | ||
| 3230 | new_values = json.loads(raw_json) |
|
| 3231 | ||
| 3232 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
| 3233 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
| 3234 | new_values['data']['virtual_meter_id'] <= 0: |
|
| 3235 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3236 | description='API.INVALID_VIRTUAL_METER_ID') |
|
| 3237 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
| 3238 | ||
| 3239 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3240 | cursor = cnx.cursor() |
|
| 3241 | ||
| 3242 | cursor.execute(" SELECT name " |
|
| 3243 | " from tbl_spaces " |
|
| 3244 | " WHERE id = %s ", (id_,)) |
|
| 3245 | if cursor.fetchone() is None: |
|
| 3246 | cursor.close() |
|
| 3247 | cnx.close() |
|
| 3248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3249 | description='API.SPACE_NOT_FOUND') |
|
| 3250 | ||
| 3251 | cursor.execute(" SELECT name " |
|
| 3252 | " FROM tbl_virtual_meters " |
|
| 3253 | " WHERE id = %s ", (virtual_meter_id,)) |
|
| 3254 | if cursor.fetchone() is None: |
|
| 3255 | cursor.close() |
|
| 3256 | cnx.close() |
|
| 3257 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3258 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
| 3259 | ||
| 3260 | query = (" SELECT id " |
|
| 3261 | " FROM tbl_spaces_virtual_meters " |
|
| 3262 | " WHERE space_id = %s AND virtual_meter_id = %s") |
|
| 3263 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
| 3264 | if cursor.fetchone() is not None: |
|
| 3265 | cursor.close() |
|
| 3266 | cnx.close() |
|
| 3267 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3268 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTS') |
|
| 3269 | ||
| 3270 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
|
| 3271 | " VALUES (%s, %s) ") |
|
| 3272 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
| 3273 | cnx.commit() |
|
| 3274 | cursor.close() |
|
| 3275 | cnx.close() |
|
| 3276 | ||
| 3277 | resp.status = falcon.HTTP_201 |
|
| 3278 | resp.location = '/spaces/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
| 3279 | ||
| 3280 | ||
| 3281 | class SpaceVirtualMeterItem: |
|
| @@ 1837-1969 (lines=133) @@ | ||
| 1834 | resp.status = falcon.HTTP_204 |
|
| 1835 | ||
| 1836 | ||
| 1837 | class SpaceOfflineMeterCollection: |
|
| 1838 | def __init__(self): |
|
| 1839 | pass |
|
| 1840 | ||
| 1841 | @staticmethod |
|
| 1842 | def on_options(req, resp, id_): |
|
| 1843 | _ = req |
|
| 1844 | resp.status = falcon.HTTP_200 |
|
| 1845 | _ = id_ |
|
| 1846 | ||
| 1847 | @staticmethod |
|
| 1848 | def on_get(req, resp, id_): |
|
| 1849 | if 'API-KEY' not in req.headers or \ |
|
| 1850 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1851 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1852 | access_control(req) |
|
| 1853 | else: |
|
| 1854 | api_key_control(req) |
|
| 1855 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1856 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1857 | description='API.INVALID_SPACE_ID') |
|
| 1858 | ||
| 1859 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1860 | cursor = cnx.cursor() |
|
| 1861 | ||
| 1862 | cursor.execute(" SELECT name " |
|
| 1863 | " FROM tbl_spaces " |
|
| 1864 | " WHERE id = %s ", (id_,)) |
|
| 1865 | if cursor.fetchone() is None: |
|
| 1866 | cursor.close() |
|
| 1867 | cnx.close() |
|
| 1868 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1869 | description='API.SPACE_NOT_FOUND') |
|
| 1870 | ||
| 1871 | query = (" SELECT id, name, uuid " |
|
| 1872 | " FROM tbl_energy_categories ") |
|
| 1873 | cursor.execute(query) |
|
| 1874 | rows_energy_categories = cursor.fetchall() |
|
| 1875 | ||
| 1876 | energy_category_dict = dict() |
|
| 1877 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 1878 | for row in rows_energy_categories: |
|
| 1879 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 1880 | "name": row[1], |
|
| 1881 | "uuid": row[2]} |
|
| 1882 | ||
| 1883 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 1884 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
|
| 1885 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
| 1886 | " ORDER BY m.id ") |
|
| 1887 | cursor.execute(query, (id_,)) |
|
| 1888 | rows = cursor.fetchall() |
|
| 1889 | ||
| 1890 | result = list() |
|
| 1891 | if rows is not None and len(rows) > 0: |
|
| 1892 | for row in rows: |
|
| 1893 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 1894 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 1895 | result.append(meta_result) |
|
| 1896 | ||
| 1897 | resp.text = json.dumps(result) |
|
| 1898 | ||
| 1899 | @staticmethod |
|
| 1900 | @user_logger |
|
| 1901 | def on_post(req, resp, id_): |
|
| 1902 | """Handles POST requests""" |
|
| 1903 | admin_control(req) |
|
| 1904 | try: |
|
| 1905 | raw_json = req.stream.read().decode('utf-8') |
|
| 1906 | except UnicodeDecodeError as ex: |
|
| 1907 | print("Failed to decode request") |
|
| 1908 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1909 | title='API.BAD_REQUEST', |
|
| 1910 | description='API.INVALID_ENCODING') |
|
| 1911 | except Exception as ex: |
|
| 1912 | print("Unexpected error reading request stream") |
|
| 1913 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1914 | title='API.BAD_REQUEST', |
|
| 1915 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1916 | ||
| 1917 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1919 | description='API.INVALID_SPACE_ID') |
|
| 1920 | ||
| 1921 | new_values = json.loads(raw_json) |
|
| 1922 | ||
| 1923 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
| 1924 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
| 1925 | new_values['data']['offline_meter_id'] <= 0: |
|
| 1926 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1927 | description='API.INVALID_OFFLINE_METER_ID') |
|
| 1928 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
| 1929 | ||
| 1930 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1931 | cursor = cnx.cursor() |
|
| 1932 | ||
| 1933 | cursor.execute(" SELECT name " |
|
| 1934 | " from tbl_spaces " |
|
| 1935 | " WHERE id = %s ", (id_,)) |
|
| 1936 | if cursor.fetchone() is None: |
|
| 1937 | cursor.close() |
|
| 1938 | cnx.close() |
|
| 1939 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1940 | description='API.SPACE_NOT_FOUND') |
|
| 1941 | ||
| 1942 | cursor.execute(" SELECT name " |
|
| 1943 | " FROM tbl_offline_meters " |
|
| 1944 | " WHERE id = %s ", (offline_meter_id,)) |
|
| 1945 | if cursor.fetchone() is None: |
|
| 1946 | cursor.close() |
|
| 1947 | cnx.close() |
|
| 1948 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1949 | description='API.OFFLINE_METER_NOT_FOUND') |
|
| 1950 | ||
| 1951 | query = (" SELECT id " |
|
| 1952 | " FROM tbl_spaces_offline_meters " |
|
| 1953 | " WHERE space_id = %s AND offline_meter_id = %s") |
|
| 1954 | cursor.execute(query, (id_, offline_meter_id,)) |
|
| 1955 | if cursor.fetchone() is not None: |
|
| 1956 | cursor.close() |
|
| 1957 | cnx.close() |
|
| 1958 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1959 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTS') |
|
| 1960 | ||
| 1961 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
|
| 1962 | " VALUES (%s, %s) ") |
|
| 1963 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
| 1964 | cnx.commit() |
|
| 1965 | cursor.close() |
|
| 1966 | cnx.close() |
|
| 1967 | ||
| 1968 | resp.status = falcon.HTTP_201 |
|
| 1969 | resp.location = '/spaces/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
| 1970 | ||
| 1971 | ||
| 1972 | class SpaceOfflineMeterItem: |
|
| @@ 1455-1587 (lines=133) @@ | ||
| 1452 | resp.status = falcon.HTTP_204 |
|
| 1453 | ||
| 1454 | ||
| 1455 | class SpaceMeterCollection: |
|
| 1456 | def __init__(self): |
|
| 1457 | pass |
|
| 1458 | ||
| 1459 | @staticmethod |
|
| 1460 | def on_options(req, resp, id_): |
|
| 1461 | _ = req |
|
| 1462 | resp.status = falcon.HTTP_200 |
|
| 1463 | _ = id_ |
|
| 1464 | ||
| 1465 | @staticmethod |
|
| 1466 | def on_get(req, resp, id_): |
|
| 1467 | if 'API-KEY' not in req.headers or \ |
|
| 1468 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1469 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1470 | access_control(req) |
|
| 1471 | else: |
|
| 1472 | api_key_control(req) |
|
| 1473 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1474 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1475 | description='API.INVALID_SPACE_ID') |
|
| 1476 | ||
| 1477 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1478 | cursor = cnx.cursor() |
|
| 1479 | ||
| 1480 | cursor.execute(" SELECT name " |
|
| 1481 | " FROM tbl_spaces " |
|
| 1482 | " WHERE id = %s ", (id_,)) |
|
| 1483 | if cursor.fetchone() is None: |
|
| 1484 | cursor.close() |
|
| 1485 | cnx.close() |
|
| 1486 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1487 | description='API.SPACE_NOT_FOUND') |
|
| 1488 | ||
| 1489 | query = (" SELECT id, name, uuid " |
|
| 1490 | " FROM tbl_energy_categories ") |
|
| 1491 | cursor.execute(query) |
|
| 1492 | rows_energy_categories = cursor.fetchall() |
|
| 1493 | ||
| 1494 | energy_category_dict = dict() |
|
| 1495 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 1496 | for row in rows_energy_categories: |
|
| 1497 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 1498 | "name": row[1], |
|
| 1499 | "uuid": row[2]} |
|
| 1500 | ||
| 1501 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 1502 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
|
| 1503 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
|
| 1504 | " ORDER BY m.id ") |
|
| 1505 | cursor.execute(query, (id_,)) |
|
| 1506 | rows = cursor.fetchall() |
|
| 1507 | ||
| 1508 | result = list() |
|
| 1509 | if rows is not None and len(rows) > 0: |
|
| 1510 | for row in rows: |
|
| 1511 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 1512 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 1513 | result.append(meta_result) |
|
| 1514 | ||
| 1515 | resp.text = json.dumps(result) |
|
| 1516 | ||
| 1517 | @staticmethod |
|
| 1518 | @user_logger |
|
| 1519 | def on_post(req, resp, id_): |
|
| 1520 | """Handles POST requests""" |
|
| 1521 | admin_control(req) |
|
| 1522 | try: |
|
| 1523 | raw_json = req.stream.read().decode('utf-8') |
|
| 1524 | except UnicodeDecodeError as ex: |
|
| 1525 | print("Failed to decode request") |
|
| 1526 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1527 | title='API.BAD_REQUEST', |
|
| 1528 | description='API.INVALID_ENCODING') |
|
| 1529 | except Exception as ex: |
|
| 1530 | print("Unexpected error reading request stream") |
|
| 1531 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1532 | title='API.BAD_REQUEST', |
|
| 1533 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1534 | ||
| 1535 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1536 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1537 | description='API.INVALID_SPACE_ID') |
|
| 1538 | ||
| 1539 | new_values = json.loads(raw_json) |
|
| 1540 | ||
| 1541 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 1542 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 1543 | new_values['data']['meter_id'] <= 0: |
|
| 1544 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1545 | description='API.INVALID_METER_ID') |
|
| 1546 | meter_id = new_values['data']['meter_id'] |
|
| 1547 | ||
| 1548 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1549 | cursor = cnx.cursor() |
|
| 1550 | ||
| 1551 | cursor.execute(" SELECT name " |
|
| 1552 | " from tbl_spaces " |
|
| 1553 | " WHERE id = %s ", (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.SPACE_NOT_FOUND') |
|
| 1559 | ||
| 1560 | cursor.execute(" SELECT name " |
|
| 1561 | " FROM tbl_meters " |
|
| 1562 | " WHERE id = %s ", (meter_id,)) |
|
| 1563 | if cursor.fetchone() is None: |
|
| 1564 | cursor.close() |
|
| 1565 | cnx.close() |
|
| 1566 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1567 | description='API.METER_NOT_FOUND') |
|
| 1568 | ||
| 1569 | query = (" SELECT id " |
|
| 1570 | " FROM tbl_spaces_meters " |
|
| 1571 | " WHERE space_id = %s AND meter_id = %s") |
|
| 1572 | cursor.execute(query, (id_, meter_id,)) |
|
| 1573 | if cursor.fetchone() is not None: |
|
| 1574 | cursor.close() |
|
| 1575 | cnx.close() |
|
| 1576 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1577 | description='API.SPACE_METER_RELATION_EXISTS') |
|
| 1578 | ||
| 1579 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
|
| 1580 | " VALUES (%s, %s) ") |
|
| 1581 | cursor.execute(add_row, (id_, meter_id,)) |
|
| 1582 | cnx.commit() |
|
| 1583 | cursor.close() |
|
| 1584 | cnx.close() |
|
| 1585 | ||
| 1586 | resp.status = falcon.HTTP_201 |
|
| 1587 | resp.location = '/spaces/' + str(id_) + '/meters/' + str(meter_id) |
|
| 1588 | ||
| 1589 | ||
| 1590 | class SpaceMeterItem: |
|
| @@ 2218-2349 (lines=132) @@ | ||
| 2215 | resp.status = falcon.HTTP_204 |
|
| 2216 | ||
| 2217 | ||
| 2218 | class SpacePointCollection: |
|
| 2219 | def __init__(self): |
|
| 2220 | pass |
|
| 2221 | ||
| 2222 | @staticmethod |
|
| 2223 | def on_options(req, resp, id_): |
|
| 2224 | _ = req |
|
| 2225 | resp.status = falcon.HTTP_200 |
|
| 2226 | _ = id_ |
|
| 2227 | ||
| 2228 | @staticmethod |
|
| 2229 | def on_get(req, resp, id_): |
|
| 2230 | if 'API-KEY' not in req.headers or \ |
|
| 2231 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2232 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2233 | access_control(req) |
|
| 2234 | else: |
|
| 2235 | api_key_control(req) |
|
| 2236 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2237 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2238 | description='API.INVALID_SPACE_ID') |
|
| 2239 | ||
| 2240 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2241 | cursor = cnx.cursor() |
|
| 2242 | ||
| 2243 | cursor.execute(" SELECT name " |
|
| 2244 | " FROM tbl_spaces " |
|
| 2245 | " WHERE id = %s ", (id_,)) |
|
| 2246 | if cursor.fetchone() is None: |
|
| 2247 | cursor.close() |
|
| 2248 | cnx.close() |
|
| 2249 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2250 | description='API.SPACE_NOT_FOUND') |
|
| 2251 | ||
| 2252 | query = (" SELECT id, name, uuid " |
|
| 2253 | " FROM tbl_data_sources ") |
|
| 2254 | cursor.execute(query) |
|
| 2255 | rows_data_sources = cursor.fetchall() |
|
| 2256 | ||
| 2257 | data_source_dict = dict() |
|
| 2258 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
| 2259 | for row in rows_data_sources: |
|
| 2260 | data_source_dict[row[0]] = {"id": row[0], |
|
| 2261 | "name": row[1], |
|
| 2262 | "uuid": row[2]} |
|
| 2263 | ||
| 2264 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
| 2265 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
|
| 2266 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
|
| 2267 | " ORDER BY p.id ") |
|
| 2268 | cursor.execute(query, (id_,)) |
|
| 2269 | rows = cursor.fetchall() |
|
| 2270 | ||
| 2271 | result = list() |
|
| 2272 | if rows is not None and len(rows) > 0: |
|
| 2273 | for row in rows: |
|
| 2274 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
| 2275 | result.append(meta_result) |
|
| 2276 | ||
| 2277 | resp.text = json.dumps(result) |
|
| 2278 | ||
| 2279 | @staticmethod |
|
| 2280 | @user_logger |
|
| 2281 | def on_post(req, resp, id_): |
|
| 2282 | """Handles POST requests""" |
|
| 2283 | admin_control(req) |
|
| 2284 | try: |
|
| 2285 | raw_json = req.stream.read().decode('utf-8') |
|
| 2286 | except UnicodeDecodeError as ex: |
|
| 2287 | print("Failed to decode request") |
|
| 2288 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2289 | title='API.BAD_REQUEST', |
|
| 2290 | description='API.INVALID_ENCODING') |
|
| 2291 | except Exception as ex: |
|
| 2292 | print("Unexpected error reading request stream") |
|
| 2293 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2294 | title='API.BAD_REQUEST', |
|
| 2295 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2296 | ||
| 2297 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2298 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2299 | description='API.INVALID_SPACE_ID') |
|
| 2300 | ||
| 2301 | new_values = json.loads(raw_json) |
|
| 2302 | ||
| 2303 | if 'point_id' not in new_values['data'].keys() or \ |
|
| 2304 | not isinstance(new_values['data']['point_id'], int) or \ |
|
| 2305 | new_values['data']['point_id'] <= 0: |
|
| 2306 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2307 | description='API.INVALID_POINT_ID') |
|
| 2308 | point_id = new_values['data']['point_id'] |
|
| 2309 | ||
| 2310 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2311 | cursor = cnx.cursor() |
|
| 2312 | ||
| 2313 | cursor.execute(" SELECT name " |
|
| 2314 | " from tbl_spaces " |
|
| 2315 | " WHERE id = %s ", (id_,)) |
|
| 2316 | if cursor.fetchone() is None: |
|
| 2317 | cursor.close() |
|
| 2318 | cnx.close() |
|
| 2319 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2320 | description='API.SPACE_NOT_FOUND') |
|
| 2321 | ||
| 2322 | cursor.execute(" SELECT name " |
|
| 2323 | " FROM tbl_points " |
|
| 2324 | " WHERE id = %s ", (point_id,)) |
|
| 2325 | if cursor.fetchone() is None: |
|
| 2326 | cursor.close() |
|
| 2327 | cnx.close() |
|
| 2328 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2329 | description='API.POINT_NOT_FOUND') |
|
| 2330 | ||
| 2331 | query = (" SELECT id " |
|
| 2332 | " FROM tbl_spaces_points " |
|
| 2333 | " WHERE space_id = %s AND point_id = %s") |
|
| 2334 | cursor.execute(query, (id_, point_id,)) |
|
| 2335 | if cursor.fetchone() is not None: |
|
| 2336 | cursor.close() |
|
| 2337 | cnx.close() |
|
| 2338 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2339 | description='API.SPACE_POINT_RELATION_EXISTS') |
|
| 2340 | ||
| 2341 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
|
| 2342 | " VALUES (%s, %s) ") |
|
| 2343 | cursor.execute(add_row, (id_, point_id,)) |
|
| 2344 | cnx.commit() |
|
| 2345 | cursor.close() |
|
| 2346 | cnx.close() |
|
| 2347 | ||
| 2348 | resp.status = falcon.HTTP_201 |
|
| 2349 | resp.location = '/spaces/' + str(id_) + '/points/' + str(point_id) |
|
| 2350 | ||
| 2351 | ||
| 2352 | class SpacePointItem: |
|
| @@ 1474-1606 (lines=133) @@ | ||
| 1471 | resp.status = falcon.HTTP_204 |
|
| 1472 | ||
| 1473 | ||
| 1474 | class ShopfloorVirtualMeterCollection: |
|
| 1475 | def __init__(self): |
|
| 1476 | pass |
|
| 1477 | ||
| 1478 | @staticmethod |
|
| 1479 | def on_options(req, resp, id_): |
|
| 1480 | resp.status = falcon.HTTP_200 |
|
| 1481 | _ = req |
|
| 1482 | _ = id_ |
|
| 1483 | ||
| 1484 | @staticmethod |
|
| 1485 | def on_get(req, resp, id_): |
|
| 1486 | if 'API-KEY' not in req.headers or \ |
|
| 1487 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1488 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1489 | access_control(req) |
|
| 1490 | else: |
|
| 1491 | api_key_control(req) |
|
| 1492 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1494 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1495 | ||
| 1496 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1497 | cursor = cnx.cursor() |
|
| 1498 | ||
| 1499 | cursor.execute(" SELECT name " |
|
| 1500 | " FROM tbl_shopfloors " |
|
| 1501 | " WHERE id = %s ", (id_,)) |
|
| 1502 | if cursor.fetchone() is None: |
|
| 1503 | cursor.close() |
|
| 1504 | cnx.close() |
|
| 1505 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1506 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1507 | ||
| 1508 | query = (" SELECT id, name, uuid " |
|
| 1509 | " FROM tbl_energy_categories ") |
|
| 1510 | cursor.execute(query) |
|
| 1511 | rows_energy_categories = cursor.fetchall() |
|
| 1512 | ||
| 1513 | energy_category_dict = dict() |
|
| 1514 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 1515 | for row in rows_energy_categories: |
|
| 1516 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 1517 | "name": row[1], |
|
| 1518 | "uuid": row[2]} |
|
| 1519 | ||
| 1520 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 1521 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
|
| 1522 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
|
| 1523 | " ORDER BY m.id ") |
|
| 1524 | cursor.execute(query, (id_,)) |
|
| 1525 | rows = cursor.fetchall() |
|
| 1526 | ||
| 1527 | result = list() |
|
| 1528 | if rows is not None and len(rows) > 0: |
|
| 1529 | for row in rows: |
|
| 1530 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 1531 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 1532 | result.append(meta_result) |
|
| 1533 | ||
| 1534 | resp.text = json.dumps(result) |
|
| 1535 | ||
| 1536 | @staticmethod |
|
| 1537 | @user_logger |
|
| 1538 | def on_post(req, resp, id_): |
|
| 1539 | """Handles POST requests""" |
|
| 1540 | admin_control(req) |
|
| 1541 | try: |
|
| 1542 | raw_json = req.stream.read().decode('utf-8') |
|
| 1543 | except UnicodeDecodeError as ex: |
|
| 1544 | print("Failed to decode request") |
|
| 1545 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1546 | title='API.BAD_REQUEST', |
|
| 1547 | description='API.INVALID_ENCODING') |
|
| 1548 | except Exception as ex: |
|
| 1549 | print("Unexpected error reading request stream") |
|
| 1550 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1551 | title='API.BAD_REQUEST', |
|
| 1552 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1553 | ||
| 1554 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1555 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1556 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1557 | ||
| 1558 | new_values = json.loads(raw_json) |
|
| 1559 | ||
| 1560 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
| 1561 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
| 1562 | new_values['data']['virtual_meter_id'] <= 0: |
|
| 1563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1564 | description='API.INVALID_VIRTUAL_METER_ID') |
|
| 1565 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
| 1566 | ||
| 1567 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1568 | cursor = cnx.cursor() |
|
| 1569 | ||
| 1570 | cursor.execute(" SELECT name " |
|
| 1571 | " from tbl_shopfloors " |
|
| 1572 | " WHERE id = %s ", (id_,)) |
|
| 1573 | if cursor.fetchone() is None: |
|
| 1574 | cursor.close() |
|
| 1575 | cnx.close() |
|
| 1576 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1577 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1578 | ||
| 1579 | cursor.execute(" SELECT name " |
|
| 1580 | " FROM tbl_virtual_meters " |
|
| 1581 | " WHERE id = %s ", (virtual_meter_id,)) |
|
| 1582 | if cursor.fetchone() is None: |
|
| 1583 | cursor.close() |
|
| 1584 | cnx.close() |
|
| 1585 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1586 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
| 1587 | ||
| 1588 | query = (" SELECT id " |
|
| 1589 | " FROM tbl_shopfloors_virtual_meters " |
|
| 1590 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
|
| 1591 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
| 1592 | if cursor.fetchone() is not None: |
|
| 1593 | cursor.close() |
|
| 1594 | cnx.close() |
|
| 1595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1596 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
|
| 1597 | ||
| 1598 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
|
| 1599 | " VALUES (%s, %s) ") |
|
| 1600 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
| 1601 | cnx.commit() |
|
| 1602 | cursor.close() |
|
| 1603 | cnx.close() |
|
| 1604 | ||
| 1605 | resp.status = falcon.HTTP_201 |
|
| 1606 | resp.location = '/shopfloors/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
| 1607 | ||
| 1608 | ||
| 1609 | class ShopfloorVirtualMeterItem: |
|
| @@ 895-1027 (lines=133) @@ | ||
| 892 | resp.status = falcon.HTTP_204 |
|
| 893 | ||
| 894 | ||
| 895 | class ShopfloorOfflineMeterCollection: |
|
| 896 | def __init__(self): |
|
| 897 | pass |
|
| 898 | ||
| 899 | @staticmethod |
|
| 900 | def on_options(req, resp, id_): |
|
| 901 | resp.status = falcon.HTTP_200 |
|
| 902 | _ = req |
|
| 903 | _ = id_ |
|
| 904 | ||
| 905 | @staticmethod |
|
| 906 | def on_get(req, resp, id_): |
|
| 907 | if 'API-KEY' not in req.headers or \ |
|
| 908 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 909 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 910 | access_control(req) |
|
| 911 | else: |
|
| 912 | api_key_control(req) |
|
| 913 | if not id_.isdigit() or int(id_) <= 0: |
|
| 914 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 915 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 916 | ||
| 917 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 918 | cursor = cnx.cursor() |
|
| 919 | ||
| 920 | cursor.execute(" SELECT name " |
|
| 921 | " FROM tbl_shopfloors " |
|
| 922 | " WHERE id = %s ", (id_,)) |
|
| 923 | if cursor.fetchone() is None: |
|
| 924 | cursor.close() |
|
| 925 | cnx.close() |
|
| 926 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 927 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 928 | ||
| 929 | query = (" SELECT id, name, uuid " |
|
| 930 | " FROM tbl_energy_categories ") |
|
| 931 | cursor.execute(query) |
|
| 932 | rows_energy_categories = cursor.fetchall() |
|
| 933 | ||
| 934 | energy_category_dict = dict() |
|
| 935 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 936 | for row in rows_energy_categories: |
|
| 937 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 938 | "name": row[1], |
|
| 939 | "uuid": row[2]} |
|
| 940 | ||
| 941 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 942 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
|
| 943 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
| 944 | " ORDER BY m.id ") |
|
| 945 | cursor.execute(query, (id_,)) |
|
| 946 | rows = cursor.fetchall() |
|
| 947 | ||
| 948 | result = list() |
|
| 949 | if rows is not None and len(rows) > 0: |
|
| 950 | for row in rows: |
|
| 951 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 952 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 953 | result.append(meta_result) |
|
| 954 | ||
| 955 | resp.text = json.dumps(result) |
|
| 956 | ||
| 957 | @staticmethod |
|
| 958 | @user_logger |
|
| 959 | def on_post(req, resp, id_): |
|
| 960 | """Handles POST requests""" |
|
| 961 | admin_control(req) |
|
| 962 | try: |
|
| 963 | raw_json = req.stream.read().decode('utf-8') |
|
| 964 | except UnicodeDecodeError as ex: |
|
| 965 | print("Failed to decode request") |
|
| 966 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 967 | title='API.BAD_REQUEST', |
|
| 968 | description='API.INVALID_ENCODING') |
|
| 969 | except Exception as ex: |
|
| 970 | print("Unexpected error reading request stream") |
|
| 971 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 972 | title='API.BAD_REQUEST', |
|
| 973 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 974 | ||
| 975 | if not id_.isdigit() or int(id_) <= 0: |
|
| 976 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 977 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 978 | ||
| 979 | new_values = json.loads(raw_json) |
|
| 980 | ||
| 981 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
| 982 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
| 983 | new_values['data']['offline_meter_id'] <= 0: |
|
| 984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 985 | description='API.INVALID_OFFLINE_METER_ID') |
|
| 986 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
| 987 | ||
| 988 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 989 | cursor = cnx.cursor() |
|
| 990 | ||
| 991 | cursor.execute(" SELECT name " |
|
| 992 | " from tbl_shopfloors " |
|
| 993 | " WHERE id = %s ", (id_,)) |
|
| 994 | if cursor.fetchone() is None: |
|
| 995 | cursor.close() |
|
| 996 | cnx.close() |
|
| 997 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 998 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 999 | ||
| 1000 | cursor.execute(" SELECT name " |
|
| 1001 | " FROM tbl_offline_meters " |
|
| 1002 | " WHERE id = %s ", (offline_meter_id,)) |
|
| 1003 | if cursor.fetchone() is None: |
|
| 1004 | cursor.close() |
|
| 1005 | cnx.close() |
|
| 1006 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1007 | description='API.OFFLINE_METER_NOT_FOUND') |
|
| 1008 | ||
| 1009 | query = (" SELECT id " |
|
| 1010 | " FROM tbl_shopfloors_offline_meters " |
|
| 1011 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
|
| 1012 | cursor.execute(query, (id_, offline_meter_id,)) |
|
| 1013 | if cursor.fetchone() is not None: |
|
| 1014 | cursor.close() |
|
| 1015 | cnx.close() |
|
| 1016 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1017 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
|
| 1018 | ||
| 1019 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
|
| 1020 | " VALUES (%s, %s) ") |
|
| 1021 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
| 1022 | cnx.commit() |
|
| 1023 | cursor.close() |
|
| 1024 | cnx.close() |
|
| 1025 | ||
| 1026 | resp.status = falcon.HTTP_201 |
|
| 1027 | resp.location = '/shopfloors/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
| 1028 | ||
| 1029 | ||
| 1030 | class ShopfloorOfflineMeterItem: |
|
| @@ 698-830 (lines=133) @@ | ||
| 695 | resp.status = falcon.HTTP_204 |
|
| 696 | ||
| 697 | ||
| 698 | class ShopfloorMeterCollection: |
|
| 699 | def __init__(self): |
|
| 700 | pass |
|
| 701 | ||
| 702 | @staticmethod |
|
| 703 | def on_options(req, resp, id_): |
|
| 704 | resp.status = falcon.HTTP_200 |
|
| 705 | _ = req |
|
| 706 | _ = id_ |
|
| 707 | ||
| 708 | @staticmethod |
|
| 709 | def on_get(req, resp, id_): |
|
| 710 | if 'API-KEY' not in req.headers or \ |
|
| 711 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 712 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 713 | access_control(req) |
|
| 714 | else: |
|
| 715 | api_key_control(req) |
|
| 716 | if not id_.isdigit() or int(id_) <= 0: |
|
| 717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 718 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 719 | ||
| 720 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 721 | cursor = cnx.cursor() |
|
| 722 | ||
| 723 | cursor.execute(" SELECT name " |
|
| 724 | " FROM tbl_shopfloors " |
|
| 725 | " WHERE id = %s ", (id_,)) |
|
| 726 | if cursor.fetchone() is None: |
|
| 727 | cursor.close() |
|
| 728 | cnx.close() |
|
| 729 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 730 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 731 | ||
| 732 | query = (" SELECT id, name, uuid " |
|
| 733 | " FROM tbl_energy_categories ") |
|
| 734 | cursor.execute(query) |
|
| 735 | rows_energy_categories = cursor.fetchall() |
|
| 736 | ||
| 737 | energy_category_dict = dict() |
|
| 738 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 739 | for row in rows_energy_categories: |
|
| 740 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 741 | "name": row[1], |
|
| 742 | "uuid": row[2]} |
|
| 743 | ||
| 744 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 745 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
|
| 746 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
|
| 747 | " ORDER BY m.id ") |
|
| 748 | cursor.execute(query, (id_,)) |
|
| 749 | rows = cursor.fetchall() |
|
| 750 | ||
| 751 | result = list() |
|
| 752 | if rows is not None and len(rows) > 0: |
|
| 753 | for row in rows: |
|
| 754 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 755 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 756 | result.append(meta_result) |
|
| 757 | ||
| 758 | resp.text = json.dumps(result) |
|
| 759 | ||
| 760 | @staticmethod |
|
| 761 | @user_logger |
|
| 762 | def on_post(req, resp, id_): |
|
| 763 | """Handles POST requests""" |
|
| 764 | admin_control(req) |
|
| 765 | try: |
|
| 766 | raw_json = req.stream.read().decode('utf-8') |
|
| 767 | except UnicodeDecodeError as ex: |
|
| 768 | print("Failed to decode request") |
|
| 769 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 770 | title='API.BAD_REQUEST', |
|
| 771 | description='API.INVALID_ENCODING') |
|
| 772 | except Exception as ex: |
|
| 773 | print("Unexpected error reading request stream") |
|
| 774 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 775 | title='API.BAD_REQUEST', |
|
| 776 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 777 | ||
| 778 | if not id_.isdigit() or int(id_) <= 0: |
|
| 779 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 780 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 781 | ||
| 782 | new_values = json.loads(raw_json) |
|
| 783 | ||
| 784 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 785 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 786 | new_values['data']['meter_id'] <= 0: |
|
| 787 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 788 | description='API.INVALID_METER_ID') |
|
| 789 | meter_id = new_values['data']['meter_id'] |
|
| 790 | ||
| 791 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 792 | cursor = cnx.cursor() |
|
| 793 | ||
| 794 | cursor.execute(" SELECT name " |
|
| 795 | " from tbl_shopfloors " |
|
| 796 | " WHERE id = %s ", (id_,)) |
|
| 797 | if cursor.fetchone() is None: |
|
| 798 | cursor.close() |
|
| 799 | cnx.close() |
|
| 800 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 801 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 802 | ||
| 803 | cursor.execute(" SELECT name " |
|
| 804 | " FROM tbl_meters " |
|
| 805 | " WHERE id = %s ", (meter_id,)) |
|
| 806 | if cursor.fetchone() is None: |
|
| 807 | cursor.close() |
|
| 808 | cnx.close() |
|
| 809 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 810 | description='API.METER_NOT_FOUND') |
|
| 811 | ||
| 812 | query = (" SELECT id " |
|
| 813 | " FROM tbl_shopfloors_meters " |
|
| 814 | " WHERE shopfloor_id = %s AND meter_id = %s") |
|
| 815 | cursor.execute(query, (id_, meter_id,)) |
|
| 816 | if cursor.fetchone() is not None: |
|
| 817 | cursor.close() |
|
| 818 | cnx.close() |
|
| 819 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 820 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
|
| 821 | ||
| 822 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
|
| 823 | " VALUES (%s, %s) ") |
|
| 824 | cursor.execute(add_row, (id_, meter_id,)) |
|
| 825 | cnx.commit() |
|
| 826 | cursor.close() |
|
| 827 | cnx.close() |
|
| 828 | ||
| 829 | resp.status = falcon.HTTP_201 |
|
| 830 | resp.location = '/shopfloors/' + str(id_) + '/meters/' + str(meter_id) |
|
| 831 | ||
| 832 | ||
| 833 | class ShopfloorMeterItem: |
|
| @@ 1093-1224 (lines=132) @@ | ||
| 1090 | resp.status = falcon.HTTP_204 |
|
| 1091 | ||
| 1092 | ||
| 1093 | class ShopfloorPointCollection: |
|
| 1094 | def __init__(self): |
|
| 1095 | pass |
|
| 1096 | ||
| 1097 | @staticmethod |
|
| 1098 | def on_options(req, resp, id_): |
|
| 1099 | resp.status = falcon.HTTP_200 |
|
| 1100 | _ = req |
|
| 1101 | _ = id_ |
|
| 1102 | ||
| 1103 | @staticmethod |
|
| 1104 | def on_get(req, resp, id_): |
|
| 1105 | if 'API-KEY' not in req.headers or \ |
|
| 1106 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1107 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1108 | access_control(req) |
|
| 1109 | else: |
|
| 1110 | api_key_control(req) |
|
| 1111 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1113 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1114 | ||
| 1115 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1116 | cursor = cnx.cursor() |
|
| 1117 | ||
| 1118 | cursor.execute(" SELECT name " |
|
| 1119 | " FROM tbl_shopfloors " |
|
| 1120 | " WHERE id = %s ", (id_,)) |
|
| 1121 | if cursor.fetchone() is None: |
|
| 1122 | cursor.close() |
|
| 1123 | cnx.close() |
|
| 1124 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1125 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1126 | ||
| 1127 | query = (" SELECT id, name, uuid " |
|
| 1128 | " FROM tbl_data_sources ") |
|
| 1129 | cursor.execute(query) |
|
| 1130 | rows_data_sources = cursor.fetchall() |
|
| 1131 | ||
| 1132 | data_source_dict = dict() |
|
| 1133 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
| 1134 | for row in rows_data_sources: |
|
| 1135 | data_source_dict[row[0]] = {"id": row[0], |
|
| 1136 | "name": row[1], |
|
| 1137 | "uuid": row[2]} |
|
| 1138 | ||
| 1139 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
| 1140 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
|
| 1141 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
|
| 1142 | " ORDER BY p.id ") |
|
| 1143 | cursor.execute(query, (id_,)) |
|
| 1144 | rows = cursor.fetchall() |
|
| 1145 | ||
| 1146 | result = list() |
|
| 1147 | if rows is not None and len(rows) > 0: |
|
| 1148 | for row in rows: |
|
| 1149 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
| 1150 | result.append(meta_result) |
|
| 1151 | ||
| 1152 | resp.text = json.dumps(result) |
|
| 1153 | ||
| 1154 | @staticmethod |
|
| 1155 | @user_logger |
|
| 1156 | def on_post(req, resp, id_): |
|
| 1157 | """Handles POST requests""" |
|
| 1158 | admin_control(req) |
|
| 1159 | try: |
|
| 1160 | raw_json = req.stream.read().decode('utf-8') |
|
| 1161 | except UnicodeDecodeError as ex: |
|
| 1162 | print("Failed to decode request") |
|
| 1163 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1164 | title='API.BAD_REQUEST', |
|
| 1165 | description='API.INVALID_ENCODING') |
|
| 1166 | except Exception as ex: |
|
| 1167 | print("Unexpected error reading request stream") |
|
| 1168 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1169 | title='API.BAD_REQUEST', |
|
| 1170 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1171 | ||
| 1172 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1174 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1175 | ||
| 1176 | new_values = json.loads(raw_json) |
|
| 1177 | ||
| 1178 | if 'point_id' not in new_values['data'].keys() or \ |
|
| 1179 | not isinstance(new_values['data']['point_id'], int) or \ |
|
| 1180 | new_values['data']['point_id'] <= 0: |
|
| 1181 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1182 | description='API.INVALID_POINT_ID') |
|
| 1183 | point_id = new_values['data']['point_id'] |
|
| 1184 | ||
| 1185 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1186 | cursor = cnx.cursor() |
|
| 1187 | ||
| 1188 | cursor.execute(" SELECT name " |
|
| 1189 | " from tbl_shopfloors " |
|
| 1190 | " WHERE id = %s ", (id_,)) |
|
| 1191 | if cursor.fetchone() is None: |
|
| 1192 | cursor.close() |
|
| 1193 | cnx.close() |
|
| 1194 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1195 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1196 | ||
| 1197 | cursor.execute(" SELECT name " |
|
| 1198 | " FROM tbl_points " |
|
| 1199 | " WHERE id = %s ", (point_id,)) |
|
| 1200 | if cursor.fetchone() is None: |
|
| 1201 | cursor.close() |
|
| 1202 | cnx.close() |
|
| 1203 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1204 | description='API.POINT_NOT_FOUND') |
|
| 1205 | ||
| 1206 | query = (" SELECT id " |
|
| 1207 | " FROM tbl_shopfloors_points " |
|
| 1208 | " WHERE shopfloor_id = %s AND point_id = %s") |
|
| 1209 | cursor.execute(query, (id_, point_id,)) |
|
| 1210 | if cursor.fetchone() is not None: |
|
| 1211 | cursor.close() |
|
| 1212 | cnx.close() |
|
| 1213 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1214 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
|
| 1215 | ||
| 1216 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
|
| 1217 | " VALUES (%s, %s) ") |
|
| 1218 | cursor.execute(add_row, (id_, point_id,)) |
|
| 1219 | cnx.commit() |
|
| 1220 | cursor.close() |
|
| 1221 | cnx.close() |
|
| 1222 | ||
| 1223 | resp.status = falcon.HTTP_201 |
|
| 1224 | resp.location = '/shopfloors/' + str(id_) + '/points/' + str(point_id) |
|
| 1225 | ||
| 1226 | ||
| 1227 | class ShopfloorPointItem: |
|
| @@ 1404-1536 (lines=133) @@ | ||
| 1401 | resp.status = falcon.HTTP_204 |
|
| 1402 | ||
| 1403 | ||
| 1404 | class StoreVirtualMeterCollection: |
|
| 1405 | def __init__(self): |
|
| 1406 | pass |
|
| 1407 | ||
| 1408 | @staticmethod |
|
| 1409 | def on_options(req, resp, id_): |
|
| 1410 | _ = req |
|
| 1411 | resp.status = falcon.HTTP_200 |
|
| 1412 | _ = id_ |
|
| 1413 | ||
| 1414 | @staticmethod |
|
| 1415 | def on_get(req, resp, id_): |
|
| 1416 | if 'API-KEY' not in req.headers or \ |
|
| 1417 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1418 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1419 | access_control(req) |
|
| 1420 | else: |
|
| 1421 | api_key_control(req) |
|
| 1422 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1423 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1424 | description='API.INVALID_STORE_ID') |
|
| 1425 | ||
| 1426 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1427 | cursor = cnx.cursor() |
|
| 1428 | ||
| 1429 | cursor.execute(" SELECT name " |
|
| 1430 | " FROM tbl_stores " |
|
| 1431 | " WHERE id = %s ", (id_,)) |
|
| 1432 | if cursor.fetchone() is None: |
|
| 1433 | cursor.close() |
|
| 1434 | cnx.close() |
|
| 1435 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1436 | description='API.STORE_NOT_FOUND') |
|
| 1437 | ||
| 1438 | query = (" SELECT id, name, uuid " |
|
| 1439 | " FROM tbl_energy_categories ") |
|
| 1440 | cursor.execute(query) |
|
| 1441 | rows_energy_categories = cursor.fetchall() |
|
| 1442 | ||
| 1443 | energy_category_dict = dict() |
|
| 1444 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 1445 | for row in rows_energy_categories: |
|
| 1446 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 1447 | "name": row[1], |
|
| 1448 | "uuid": row[2]} |
|
| 1449 | ||
| 1450 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 1451 | " FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " |
|
| 1452 | " WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
|
| 1453 | " ORDER BY m.id ") |
|
| 1454 | cursor.execute(query, (id_,)) |
|
| 1455 | rows = cursor.fetchall() |
|
| 1456 | ||
| 1457 | result = list() |
|
| 1458 | if rows is not None and len(rows) > 0: |
|
| 1459 | for row in rows: |
|
| 1460 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 1461 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 1462 | result.append(meta_result) |
|
| 1463 | ||
| 1464 | resp.text = json.dumps(result) |
|
| 1465 | ||
| 1466 | @staticmethod |
|
| 1467 | @user_logger |
|
| 1468 | def on_post(req, resp, id_): |
|
| 1469 | """Handles POST requests""" |
|
| 1470 | admin_control(req) |
|
| 1471 | try: |
|
| 1472 | raw_json = req.stream.read().decode('utf-8') |
|
| 1473 | except UnicodeDecodeError as ex: |
|
| 1474 | print("Failed to decode request") |
|
| 1475 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1476 | title='API.BAD_REQUEST', |
|
| 1477 | description='API.INVALID_ENCODING') |
|
| 1478 | except Exception as ex: |
|
| 1479 | print("Unexpected error reading request stream") |
|
| 1480 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1481 | title='API.BAD_REQUEST', |
|
| 1482 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1483 | ||
| 1484 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1485 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1486 | description='API.INVALID_STORE_ID') |
|
| 1487 | ||
| 1488 | new_values = json.loads(raw_json) |
|
| 1489 | ||
| 1490 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
| 1491 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
| 1492 | new_values['data']['virtual_meter_id'] <= 0: |
|
| 1493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1494 | description='API.INVALID_VIRTUAL_METER_ID') |
|
| 1495 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
| 1496 | ||
| 1497 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1498 | cursor = cnx.cursor() |
|
| 1499 | ||
| 1500 | cursor.execute(" SELECT name " |
|
| 1501 | " from tbl_stores " |
|
| 1502 | " WHERE id = %s ", (id_,)) |
|
| 1503 | if cursor.fetchone() is None: |
|
| 1504 | cursor.close() |
|
| 1505 | cnx.close() |
|
| 1506 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1507 | description='API.STORE_NOT_FOUND') |
|
| 1508 | ||
| 1509 | cursor.execute(" SELECT name " |
|
| 1510 | " FROM tbl_virtual_meters " |
|
| 1511 | " WHERE id = %s ", (virtual_meter_id,)) |
|
| 1512 | if cursor.fetchone() is None: |
|
| 1513 | cursor.close() |
|
| 1514 | cnx.close() |
|
| 1515 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1516 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
| 1517 | ||
| 1518 | query = (" SELECT id " |
|
| 1519 | " FROM tbl_stores_virtual_meters " |
|
| 1520 | " WHERE store_id = %s AND virtual_meter_id = %s") |
|
| 1521 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
| 1522 | if cursor.fetchone() is not None: |
|
| 1523 | cursor.close() |
|
| 1524 | cnx.close() |
|
| 1525 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1526 | description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') |
|
| 1527 | ||
| 1528 | add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " |
|
| 1529 | " VALUES (%s, %s) ") |
|
| 1530 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
|
| 1531 | cnx.commit() |
|
| 1532 | cursor.close() |
|
| 1533 | cnx.close() |
|
| 1534 | ||
| 1535 | resp.status = falcon.HTTP_201 |
|
| 1536 | resp.location = '/stores/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
| 1537 | ||
| 1538 | ||
| 1539 | class StoreVirtualMeterItem: |
|
| @@ 828-960 (lines=133) @@ | ||
| 825 | resp.status = falcon.HTTP_204 |
|
| 826 | ||
| 827 | ||
| 828 | class StoreOfflineMeterCollection: |
|
| 829 | def __init__(self): |
|
| 830 | pass |
|
| 831 | ||
| 832 | @staticmethod |
|
| 833 | def on_options(req, resp, id_): |
|
| 834 | _ = req |
|
| 835 | resp.status = falcon.HTTP_200 |
|
| 836 | _ = id_ |
|
| 837 | ||
| 838 | @staticmethod |
|
| 839 | def on_get(req, resp, id_): |
|
| 840 | if 'API-KEY' not in req.headers or \ |
|
| 841 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 842 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 843 | access_control(req) |
|
| 844 | else: |
|
| 845 | api_key_control(req) |
|
| 846 | if not id_.isdigit() or int(id_) <= 0: |
|
| 847 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 848 | description='API.INVALID_STORE_ID') |
|
| 849 | ||
| 850 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 851 | cursor = cnx.cursor() |
|
| 852 | ||
| 853 | cursor.execute(" SELECT name " |
|
| 854 | " FROM tbl_stores " |
|
| 855 | " WHERE id = %s ", (id_,)) |
|
| 856 | if cursor.fetchone() is None: |
|
| 857 | cursor.close() |
|
| 858 | cnx.close() |
|
| 859 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 860 | description='API.STORE_NOT_FOUND') |
|
| 861 | ||
| 862 | query = (" SELECT id, name, uuid " |
|
| 863 | " FROM tbl_energy_categories ") |
|
| 864 | cursor.execute(query) |
|
| 865 | rows_energy_categories = cursor.fetchall() |
|
| 866 | ||
| 867 | energy_category_dict = dict() |
|
| 868 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 869 | for row in rows_energy_categories: |
|
| 870 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 871 | "name": row[1], |
|
| 872 | "uuid": row[2]} |
|
| 873 | ||
| 874 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 875 | " FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " |
|
| 876 | " WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
|
| 877 | " ORDER BY m.id ") |
|
| 878 | cursor.execute(query, (id_,)) |
|
| 879 | rows = cursor.fetchall() |
|
| 880 | ||
| 881 | result = list() |
|
| 882 | if rows is not None and len(rows) > 0: |
|
| 883 | for row in rows: |
|
| 884 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 885 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 886 | result.append(meta_result) |
|
| 887 | ||
| 888 | resp.text = json.dumps(result) |
|
| 889 | ||
| 890 | @staticmethod |
|
| 891 | @user_logger |
|
| 892 | def on_post(req, resp, id_): |
|
| 893 | """Handles POST requests""" |
|
| 894 | admin_control(req) |
|
| 895 | try: |
|
| 896 | raw_json = req.stream.read().decode('utf-8') |
|
| 897 | except UnicodeDecodeError as ex: |
|
| 898 | print("Failed to decode request") |
|
| 899 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 900 | title='API.BAD_REQUEST', |
|
| 901 | description='API.INVALID_ENCODING') |
|
| 902 | except Exception as ex: |
|
| 903 | print("Unexpected error reading request stream") |
|
| 904 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 905 | title='API.BAD_REQUEST', |
|
| 906 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 907 | ||
| 908 | if not id_.isdigit() or int(id_) <= 0: |
|
| 909 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 910 | description='API.INVALID_STORE_ID') |
|
| 911 | ||
| 912 | new_values = json.loads(raw_json) |
|
| 913 | ||
| 914 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
| 915 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
| 916 | new_values['data']['offline_meter_id'] <= 0: |
|
| 917 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 918 | description='API.INVALID_OFFLINE_METER_ID') |
|
| 919 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
| 920 | ||
| 921 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 922 | cursor = cnx.cursor() |
|
| 923 | ||
| 924 | cursor.execute(" SELECT name " |
|
| 925 | " from tbl_stores " |
|
| 926 | " WHERE id = %s ", (id_,)) |
|
| 927 | if cursor.fetchone() is None: |
|
| 928 | cursor.close() |
|
| 929 | cnx.close() |
|
| 930 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 931 | description='API.STORE_NOT_FOUND') |
|
| 932 | ||
| 933 | cursor.execute(" SELECT name " |
|
| 934 | " FROM tbl_offline_meters " |
|
| 935 | " WHERE id = %s ", (offline_meter_id,)) |
|
| 936 | if cursor.fetchone() is None: |
|
| 937 | cursor.close() |
|
| 938 | cnx.close() |
|
| 939 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 940 | description='API.OFFLINE_METER_NOT_FOUND') |
|
| 941 | ||
| 942 | query = (" SELECT id " |
|
| 943 | " FROM tbl_stores_offline_meters " |
|
| 944 | " WHERE store_id = %s AND offline_meter_id = %s") |
|
| 945 | cursor.execute(query, (id_, offline_meter_id,)) |
|
| 946 | if cursor.fetchone() is not None: |
|
| 947 | cursor.close() |
|
| 948 | cnx.close() |
|
| 949 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 950 | description='API.STORE_OFFLINE_METER_RELATION_EXISTS') |
|
| 951 | ||
| 952 | add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " |
|
| 953 | " VALUES (%s, %s) ") |
|
| 954 | cursor.execute(add_row, (id_, offline_meter_id,)) |
|
| 955 | cnx.commit() |
|
| 956 | cursor.close() |
|
| 957 | cnx.close() |
|
| 958 | ||
| 959 | resp.status = falcon.HTTP_201 |
|
| 960 | resp.location = '/stores/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
| 961 | ||
| 962 | ||
| 963 | class StoreOfflineMeterItem: |
|
| @@ 632-764 (lines=133) @@ | ||
| 629 | resp.status = falcon.HTTP_200 |
|
| 630 | ||
| 631 | ||
| 632 | class StoreMeterCollection: |
|
| 633 | def __init__(self): |
|
| 634 | pass |
|
| 635 | ||
| 636 | @staticmethod |
|
| 637 | def on_options(req, resp, id_): |
|
| 638 | _ = req |
|
| 639 | resp.status = falcon.HTTP_200 |
|
| 640 | _ = id_ |
|
| 641 | ||
| 642 | @staticmethod |
|
| 643 | def on_get(req, resp, id_): |
|
| 644 | if 'API-KEY' not in req.headers or \ |
|
| 645 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 646 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 647 | access_control(req) |
|
| 648 | else: |
|
| 649 | api_key_control(req) |
|
| 650 | if not id_.isdigit() or int(id_) <= 0: |
|
| 651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 652 | description='API.INVALID_STORE_ID') |
|
| 653 | ||
| 654 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 655 | cursor = cnx.cursor() |
|
| 656 | ||
| 657 | cursor.execute(" SELECT name " |
|
| 658 | " FROM tbl_stores " |
|
| 659 | " WHERE id = %s ", (id_,)) |
|
| 660 | if cursor.fetchone() is None: |
|
| 661 | cursor.close() |
|
| 662 | cnx.close() |
|
| 663 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 664 | description='API.STORE_NOT_FOUND') |
|
| 665 | ||
| 666 | query = (" SELECT id, name, uuid " |
|
| 667 | " FROM tbl_energy_categories ") |
|
| 668 | cursor.execute(query) |
|
| 669 | rows_energy_categories = cursor.fetchall() |
|
| 670 | ||
| 671 | energy_category_dict = dict() |
|
| 672 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 673 | for row in rows_energy_categories: |
|
| 674 | energy_category_dict[row[0]] = {"id": row[0], |
|
| 675 | "name": row[1], |
|
| 676 | "uuid": row[2]} |
|
| 677 | ||
| 678 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
|
| 679 | " FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " |
|
| 680 | " WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
|
| 681 | " ORDER BY m.id ") |
|
| 682 | cursor.execute(query, (id_,)) |
|
| 683 | rows = cursor.fetchall() |
|
| 684 | ||
| 685 | result = list() |
|
| 686 | if rows is not None and len(rows) > 0: |
|
| 687 | for row in rows: |
|
| 688 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
| 689 | "energy_category": energy_category_dict.get(row[3], None)} |
|
| 690 | result.append(meta_result) |
|
| 691 | ||
| 692 | resp.text = json.dumps(result) |
|
| 693 | ||
| 694 | @staticmethod |
|
| 695 | @user_logger |
|
| 696 | def on_post(req, resp, id_): |
|
| 697 | """Handles POST requests""" |
|
| 698 | admin_control(req) |
|
| 699 | try: |
|
| 700 | raw_json = req.stream.read().decode('utf-8') |
|
| 701 | except UnicodeDecodeError as ex: |
|
| 702 | print("Failed to decode request") |
|
| 703 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 704 | title='API.BAD_REQUEST', |
|
| 705 | description='API.INVALID_ENCODING') |
|
| 706 | except Exception as ex: |
|
| 707 | print("Unexpected error reading request stream") |
|
| 708 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 709 | title='API.BAD_REQUEST', |
|
| 710 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 711 | ||
| 712 | if not id_.isdigit() or int(id_) <= 0: |
|
| 713 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 714 | description='API.INVALID_STORE_ID') |
|
| 715 | ||
| 716 | new_values = json.loads(raw_json) |
|
| 717 | ||
| 718 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 719 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 720 | new_values['data']['meter_id'] <= 0: |
|
| 721 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 722 | description='API.INVALID_METER_ID') |
|
| 723 | meter_id = new_values['data']['meter_id'] |
|
| 724 | ||
| 725 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 726 | cursor = cnx.cursor() |
|
| 727 | ||
| 728 | cursor.execute(" SELECT name " |
|
| 729 | " from tbl_stores " |
|
| 730 | " WHERE id = %s ", (id_,)) |
|
| 731 | if cursor.fetchone() is None: |
|
| 732 | cursor.close() |
|
| 733 | cnx.close() |
|
| 734 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 735 | description='API.STORE_NOT_FOUND') |
|
| 736 | ||
| 737 | cursor.execute(" SELECT name " |
|
| 738 | " FROM tbl_meters " |
|
| 739 | " WHERE id = %s ", (meter_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.METER_NOT_FOUND') |
|
| 745 | ||
| 746 | query = (" SELECT id " |
|
| 747 | " FROM tbl_stores_meters " |
|
| 748 | " WHERE store_id = %s AND meter_id = %s") |
|
| 749 | cursor.execute(query, (id_, meter_id,)) |
|
| 750 | if cursor.fetchone() is not None: |
|
| 751 | cursor.close() |
|
| 752 | cnx.close() |
|
| 753 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 754 | description='API.STORE_METER_RELATION_EXISTS') |
|
| 755 | ||
| 756 | add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " |
|
| 757 | " VALUES (%s, %s) ") |
|
| 758 | cursor.execute(add_row, (id_, meter_id,)) |
|
| 759 | cnx.commit() |
|
| 760 | cursor.close() |
|
| 761 | cnx.close() |
|
| 762 | ||
| 763 | resp.status = falcon.HTTP_201 |
|
| 764 | resp.location = '/stores/' + str(id_) + '/meters/' + str(meter_id) |
|
| 765 | ||
| 766 | ||
| 767 | class StoreMeterItem: |
|
| @@ 1025-1156 (lines=132) @@ | ||
| 1022 | resp.status = falcon.HTTP_204 |
|
| 1023 | ||
| 1024 | ||
| 1025 | class StorePointCollection: |
|
| 1026 | def __init__(self): |
|
| 1027 | pass |
|
| 1028 | ||
| 1029 | @staticmethod |
|
| 1030 | def on_options(req, resp, id_): |
|
| 1031 | _ = req |
|
| 1032 | resp.status = falcon.HTTP_200 |
|
| 1033 | _ = id_ |
|
| 1034 | ||
| 1035 | @staticmethod |
|
| 1036 | def on_get(req, resp, id_): |
|
| 1037 | if 'API-KEY' not in req.headers or \ |
|
| 1038 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1039 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1040 | access_control(req) |
|
| 1041 | else: |
|
| 1042 | api_key_control(req) |
|
| 1043 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1044 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1045 | description='API.INVALID_STORE_ID') |
|
| 1046 | ||
| 1047 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1048 | cursor = cnx.cursor() |
|
| 1049 | ||
| 1050 | cursor.execute(" SELECT name " |
|
| 1051 | " FROM tbl_stores " |
|
| 1052 | " WHERE id = %s ", (id_,)) |
|
| 1053 | if cursor.fetchone() is None: |
|
| 1054 | cursor.close() |
|
| 1055 | cnx.close() |
|
| 1056 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1057 | description='API.STORE_NOT_FOUND') |
|
| 1058 | ||
| 1059 | query = (" SELECT id, name, uuid " |
|
| 1060 | " FROM tbl_data_sources ") |
|
| 1061 | cursor.execute(query) |
|
| 1062 | rows_data_sources = cursor.fetchall() |
|
| 1063 | ||
| 1064 | data_source_dict = dict() |
|
| 1065 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
|
| 1066 | for row in rows_data_sources: |
|
| 1067 | data_source_dict[row[0]] = {"id": row[0], |
|
| 1068 | "name": row[1], |
|
| 1069 | "uuid": row[2]} |
|
| 1070 | ||
| 1071 | query = (" SELECT p.id, p.name, p.data_source_id " |
|
| 1072 | " FROM tbl_stores t, tbl_stores_points tp, tbl_points p " |
|
| 1073 | " WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " |
|
| 1074 | " ORDER BY p.id ") |
|
| 1075 | cursor.execute(query, (id_,)) |
|
| 1076 | rows = cursor.fetchall() |
|
| 1077 | ||
| 1078 | result = list() |
|
| 1079 | if rows is not None and len(rows) > 0: |
|
| 1080 | for row in rows: |
|
| 1081 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
|
| 1082 | result.append(meta_result) |
|
| 1083 | ||
| 1084 | resp.text = json.dumps(result) |
|
| 1085 | ||
| 1086 | @staticmethod |
|
| 1087 | @user_logger |
|
| 1088 | def on_post(req, resp, id_): |
|
| 1089 | """Handles POST requests""" |
|
| 1090 | admin_control(req) |
|
| 1091 | try: |
|
| 1092 | raw_json = req.stream.read().decode('utf-8') |
|
| 1093 | except UnicodeDecodeError as ex: |
|
| 1094 | print("Failed to decode request") |
|
| 1095 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1096 | title='API.BAD_REQUEST', |
|
| 1097 | description='API.INVALID_ENCODING') |
|
| 1098 | except Exception as ex: |
|
| 1099 | print("Unexpected error reading request stream") |
|
| 1100 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1101 | title='API.BAD_REQUEST', |
|
| 1102 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1103 | ||
| 1104 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1105 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1106 | description='API.INVALID_STORE_ID') |
|
| 1107 | ||
| 1108 | new_values = json.loads(raw_json) |
|
| 1109 | ||
| 1110 | if 'point_id' not in new_values['data'].keys() or \ |
|
| 1111 | not isinstance(new_values['data']['point_id'], int) or \ |
|
| 1112 | new_values['data']['point_id'] <= 0: |
|
| 1113 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1114 | description='API.INVALID_POINT_ID') |
|
| 1115 | point_id = new_values['data']['point_id'] |
|
| 1116 | ||
| 1117 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1118 | cursor = cnx.cursor() |
|
| 1119 | ||
| 1120 | cursor.execute(" SELECT name " |
|
| 1121 | " from tbl_stores " |
|
| 1122 | " WHERE id = %s ", (id_,)) |
|
| 1123 | if cursor.fetchone() is None: |
|
| 1124 | cursor.close() |
|
| 1125 | cnx.close() |
|
| 1126 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1127 | description='API.STORE_NOT_FOUND') |
|
| 1128 | ||
| 1129 | cursor.execute(" SELECT name " |
|
| 1130 | " FROM tbl_points " |
|
| 1131 | " WHERE id = %s ", (point_id,)) |
|
| 1132 | if cursor.fetchone() is None: |
|
| 1133 | cursor.close() |
|
| 1134 | cnx.close() |
|
| 1135 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1136 | description='API.POINT_NOT_FOUND') |
|
| 1137 | ||
| 1138 | query = (" SELECT id " |
|
| 1139 | " FROM tbl_stores_points " |
|
| 1140 | " WHERE store_id = %s AND point_id = %s") |
|
| 1141 | cursor.execute(query, (id_, point_id,)) |
|
| 1142 | if cursor.fetchone() is not None: |
|
| 1143 | cursor.close() |
|
| 1144 | cnx.close() |
|
| 1145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1146 | description='API.STORE_POINT_RELATION_EXISTS') |
|
| 1147 | ||
| 1148 | add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " |
|
| 1149 | " VALUES (%s, %s) ") |
|
| 1150 | cursor.execute(add_row, (id_, point_id,)) |
|
| 1151 | cnx.commit() |
|
| 1152 | cursor.close() |
|
| 1153 | cnx.close() |
|
| 1154 | ||
| 1155 | resp.status = falcon.HTTP_201 |
|
| 1156 | resp.location = '/stores/' + str(id_) + '/points/' + str(point_id) |
|
| 1157 | ||
| 1158 | ||
| 1159 | class StorePointItem: |
|