@@ 5034-5151 (lines=118) @@ | ||
5031 | resp.status = falcon.HTTP_204 |
|
5032 | ||
5033 | ||
5034 | class PhotovoltaicPowerStationDataSourceCollection: |
|
5035 | def __init__(self): |
|
5036 | """Initializes Class""" |
|
5037 | pass |
|
5038 | ||
5039 | @staticmethod |
|
5040 | def on_options(req, resp, id_): |
|
5041 | _ = req |
|
5042 | resp.status = falcon.HTTP_200 |
|
5043 | _ = id_ |
|
5044 | ||
5045 | @staticmethod |
|
5046 | def on_get(req, resp, id_): |
|
5047 | if 'API-KEY' not in req.headers or \ |
|
5048 | not isinstance(req.headers['API-KEY'], str) or \ |
|
5049 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
5050 | access_control(req) |
|
5051 | else: |
|
5052 | api_key_control(req) |
|
5053 | if not id_.isdigit() or int(id_) <= 0: |
|
5054 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5055 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
5056 | ||
5057 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5058 | cursor = cnx.cursor() |
|
5059 | ||
5060 | cursor.execute(" SELECT name " |
|
5061 | " FROM tbl_photovoltaic_power_stations " |
|
5062 | " WHERE id = %s ", (id_,)) |
|
5063 | if cursor.fetchone() is None: |
|
5064 | cursor.close() |
|
5065 | cnx.close() |
|
5066 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5067 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
5068 | ||
5069 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
5070 | " FROM tbl_photovoltaic_power_stations psp, tbl_photovoltaic_power_stations_data_sources psds, " |
|
5071 | " tbl_data_sources ds " |
|
5072 | " WHERE psds.photovoltaic_power_station_id = psp.id AND ds.id = psds.data_source_id AND psp.id = %s " |
|
5073 | " ORDER BY ds.id ") |
|
5074 | cursor.execute(query, (id_,)) |
|
5075 | rows = cursor.fetchall() |
|
5076 | ||
5077 | result = list() |
|
5078 | if rows is not None and len(rows) > 0: |
|
5079 | for row in rows: |
|
5080 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
5081 | result.append(meta_result) |
|
5082 | ||
5083 | resp.text = json.dumps(result) |
|
5084 | ||
5085 | @staticmethod |
|
5086 | @user_logger |
|
5087 | def on_post(req, resp, id_): |
|
5088 | """Handles POST requests""" |
|
5089 | admin_control(req) |
|
5090 | try: |
|
5091 | raw_json = req.stream.read().decode('utf-8') |
|
5092 | except Exception as ex: |
|
5093 | print(str(ex)) |
|
5094 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5095 | title='API.BAD_REQUEST', |
|
5096 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5097 | ||
5098 | if not id_.isdigit() or int(id_) <= 0: |
|
5099 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5100 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
5101 | ||
5102 | new_values = json.loads(raw_json) |
|
5103 | ||
5104 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
5105 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
5106 | new_values['data']['data_source_id'] <= 0: |
|
5107 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5108 | description='API.INVALID_DATA_SOURCE_ID') |
|
5109 | data_source_id = new_values['data']['data_source_id'] |
|
5110 | ||
5111 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5112 | cursor = cnx.cursor() |
|
5113 | ||
5114 | cursor.execute(" SELECT name " |
|
5115 | " from tbl_photovoltaic_power_stations " |
|
5116 | " WHERE id = %s ", (id_,)) |
|
5117 | if cursor.fetchone() is None: |
|
5118 | cursor.close() |
|
5119 | cnx.close() |
|
5120 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5121 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
5122 | ||
5123 | cursor.execute(" SELECT name " |
|
5124 | " FROM tbl_data_sources " |
|
5125 | " WHERE id = %s ", (data_source_id,)) |
|
5126 | if cursor.fetchone() is None: |
|
5127 | cursor.close() |
|
5128 | cnx.close() |
|
5129 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5130 | description='API.DATA_SOURCE_NOT_FOUND') |
|
5131 | ||
5132 | query = (" SELECT id " |
|
5133 | " FROM tbl_photovoltaic_power_stations_data_sources " |
|
5134 | " WHERE photovoltaic_power_station_id = %s AND data_source_id = %s") |
|
5135 | cursor.execute(query, (id_, data_source_id,)) |
|
5136 | if cursor.fetchone() is not None: |
|
5137 | cursor.close() |
|
5138 | cnx.close() |
|
5139 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
5140 | description='API.PHOTOVOLTAIC_POWER_STATION_DATA_SOURCE_RELATION_EXISTS') |
|
5141 | ||
5142 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_data_sources " |
|
5143 | " (photovoltaic_power_station_id, data_source_id) " |
|
5144 | " VALUES (%s, %s) ") |
|
5145 | cursor.execute(add_row, (id_, data_source_id,)) |
|
5146 | cnx.commit() |
|
5147 | cursor.close() |
|
5148 | cnx.close() |
|
5149 | ||
5150 | resp.status = falcon.HTTP_201 |
|
5151 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/datasources/' + str(data_source_id) |
|
5152 | ||
5153 | ||
5154 | class PhotovoltaicPowerStationDataSourceItem: |
@@ 1944-2061 (lines=118) @@ | ||
1941 | resp.status = falcon.HTTP_204 |
|
1942 | ||
1943 | ||
1944 | class EquipmentCommandCollection: |
|
1945 | def __init__(self): |
|
1946 | """Initializes Class""" |
|
1947 | pass |
|
1948 | ||
1949 | @staticmethod |
|
1950 | def on_options(req, resp, id_): |
|
1951 | _ = req |
|
1952 | resp.status = falcon.HTTP_200 |
|
1953 | _ = id_ |
|
1954 | ||
1955 | @staticmethod |
|
1956 | def on_get(req, resp, id_): |
|
1957 | if 'API-KEY' not in req.headers or \ |
|
1958 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1959 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1960 | access_control(req) |
|
1961 | else: |
|
1962 | api_key_control(req) |
|
1963 | if not id_.isdigit() or int(id_) <= 0: |
|
1964 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1965 | description='API.INVALID_EQUIPMENT_ID') |
|
1966 | ||
1967 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1968 | cursor = cnx.cursor() |
|
1969 | ||
1970 | cursor.execute(" SELECT name " |
|
1971 | " FROM tbl_equipments " |
|
1972 | " WHERE id = %s ", (id_,)) |
|
1973 | if cursor.fetchone() is None: |
|
1974 | cursor.close() |
|
1975 | cnx.close() |
|
1976 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1977 | description='API.EQUIPMENT_NOT_FOUND') |
|
1978 | ||
1979 | query = (" SELECT c.id, c.name, c.uuid " |
|
1980 | " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
|
1981 | " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
|
1982 | " ORDER BY c.id ") |
|
1983 | cursor.execute(query, (id_,)) |
|
1984 | rows = cursor.fetchall() |
|
1985 | ||
1986 | result = list() |
|
1987 | if rows is not None and len(rows) > 0: |
|
1988 | for row in rows: |
|
1989 | meta_result = {"id": row[0], |
|
1990 | "name": row[1], |
|
1991 | "uuid": row[2]} |
|
1992 | result.append(meta_result) |
|
1993 | ||
1994 | resp.text = json.dumps(result) |
|
1995 | ||
1996 | @staticmethod |
|
1997 | @user_logger |
|
1998 | def on_post(req, resp, id_): |
|
1999 | """Handles POST requests""" |
|
2000 | admin_control(req) |
|
2001 | try: |
|
2002 | raw_json = req.stream.read().decode('utf-8') |
|
2003 | except Exception as ex: |
|
2004 | print(str(ex)) |
|
2005 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2006 | title='API.BAD_REQUEST', |
|
2007 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2008 | ||
2009 | if not id_.isdigit() or int(id_) <= 0: |
|
2010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2011 | description='API.INVALID_EQUIPMENT_ID') |
|
2012 | ||
2013 | new_values = json.loads(raw_json) |
|
2014 | ||
2015 | if 'command_id' not in new_values['data'].keys() or \ |
|
2016 | not isinstance(new_values['data']['command_id'], int) or \ |
|
2017 | new_values['data']['command_id'] <= 0: |
|
2018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2019 | description='API.INVALID_COMMAND_ID') |
|
2020 | command_id = new_values['data']['command_id'] |
|
2021 | ||
2022 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2023 | cursor = cnx.cursor() |
|
2024 | ||
2025 | cursor.execute(" SELECT name " |
|
2026 | " from tbl_equipments " |
|
2027 | " WHERE id = %s ", (id_,)) |
|
2028 | if cursor.fetchone() is None: |
|
2029 | cursor.close() |
|
2030 | cnx.close() |
|
2031 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2032 | description='API.EQUIPMENT_NOT_FOUND') |
|
2033 | ||
2034 | cursor.execute(" SELECT name " |
|
2035 | " FROM tbl_commands " |
|
2036 | " WHERE id = %s ", (command_id,)) |
|
2037 | if cursor.fetchone() is None: |
|
2038 | cursor.close() |
|
2039 | cnx.close() |
|
2040 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2041 | description='API.COMMAND_NOT_FOUND') |
|
2042 | ||
2043 | query = (" SELECT id " |
|
2044 | " FROM tbl_equipments_commands " |
|
2045 | " WHERE equipment_id = %s AND command_id = %s") |
|
2046 | cursor.execute(query, (id_, command_id,)) |
|
2047 | if cursor.fetchone() is not None: |
|
2048 | cursor.close() |
|
2049 | cnx.close() |
|
2050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2051 | description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
2052 | ||
2053 | add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
|
2054 | " VALUES (%s, %s) ") |
|
2055 | cursor.execute(add_row, (id_, command_id,)) |
|
2056 | cnx.commit() |
|
2057 | cursor.close() |
|
2058 | cnx.close() |
|
2059 | ||
2060 | resp.status = falcon.HTTP_201 |
|
2061 | resp.location = '/equipments/' + str(id_) + '/commands/' + str(command_id) |
|
2062 | ||
2063 | ||
2064 | class EquipmentCommandItem: |
@@ 1827-1944 (lines=118) @@ | ||
1824 | resp.status = falcon.HTTP_204 |
|
1825 | ||
1826 | ||
1827 | class TenantCommandCollection: |
|
1828 | def __init__(self): |
|
1829 | """Initializes Class""" |
|
1830 | pass |
|
1831 | ||
1832 | @staticmethod |
|
1833 | def on_options(req, resp, id_): |
|
1834 | _ = req |
|
1835 | resp.status = falcon.HTTP_200 |
|
1836 | _ = id_ |
|
1837 | ||
1838 | @staticmethod |
|
1839 | def on_get(req, resp, id_): |
|
1840 | if 'API-KEY' not in req.headers or \ |
|
1841 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1842 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1843 | access_control(req) |
|
1844 | else: |
|
1845 | api_key_control(req) |
|
1846 | if not id_.isdigit() or int(id_) <= 0: |
|
1847 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1848 | description='API.INVALID_TENANT_ID') |
|
1849 | ||
1850 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1851 | cursor = cnx.cursor() |
|
1852 | ||
1853 | cursor.execute(" SELECT name " |
|
1854 | " FROM tbl_tenants " |
|
1855 | " WHERE id = %s ", (id_,)) |
|
1856 | if cursor.fetchone() is None: |
|
1857 | cursor.close() |
|
1858 | cnx.close() |
|
1859 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1860 | description='API.TENANT_NOT_FOUND') |
|
1861 | ||
1862 | query = (" SELECT c.id, c.name, c.uuid " |
|
1863 | " FROM tbl_tenants t, tbl_tenants_commands tc, tbl_commands c " |
|
1864 | " WHERE tc.tenant_id = t.id AND c.id = tc.command_id AND t.id = %s " |
|
1865 | " ORDER BY c.id ") |
|
1866 | cursor.execute(query, (id_,)) |
|
1867 | rows = cursor.fetchall() |
|
1868 | ||
1869 | result = list() |
|
1870 | if rows is not None and len(rows) > 0: |
|
1871 | for row in rows: |
|
1872 | meta_result = {"id": row[0], |
|
1873 | "name": row[1], |
|
1874 | "uuid": row[2]} |
|
1875 | result.append(meta_result) |
|
1876 | ||
1877 | resp.text = json.dumps(result) |
|
1878 | ||
1879 | @staticmethod |
|
1880 | @user_logger |
|
1881 | def on_post(req, resp, id_): |
|
1882 | """Handles POST requests""" |
|
1883 | admin_control(req) |
|
1884 | try: |
|
1885 | raw_json = req.stream.read().decode('utf-8') |
|
1886 | except Exception as ex: |
|
1887 | print(str(ex)) |
|
1888 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1889 | title='API.BAD_REQUEST', |
|
1890 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1891 | ||
1892 | if not id_.isdigit() or int(id_) <= 0: |
|
1893 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1894 | description='API.INVALID_TENANT_ID') |
|
1895 | ||
1896 | new_values = json.loads(raw_json) |
|
1897 | ||
1898 | if 'command_id' not in new_values['data'].keys() or \ |
|
1899 | not isinstance(new_values['data']['command_id'], int) or \ |
|
1900 | new_values['data']['command_id'] <= 0: |
|
1901 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1902 | description='API.INVALID_COMMAND_ID') |
|
1903 | command_id = new_values['data']['command_id'] |
|
1904 | ||
1905 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1906 | cursor = cnx.cursor() |
|
1907 | ||
1908 | cursor.execute(" SELECT name " |
|
1909 | " from tbl_tenants " |
|
1910 | " WHERE id = %s ", (id_,)) |
|
1911 | if cursor.fetchone() is None: |
|
1912 | cursor.close() |
|
1913 | cnx.close() |
|
1914 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1915 | description='API.TENANT_NOT_FOUND') |
|
1916 | ||
1917 | cursor.execute(" SELECT name " |
|
1918 | " FROM tbl_commands " |
|
1919 | " WHERE id = %s ", (command_id,)) |
|
1920 | if cursor.fetchone() is None: |
|
1921 | cursor.close() |
|
1922 | cnx.close() |
|
1923 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1924 | description='API.COMMAND_NOT_FOUND') |
|
1925 | ||
1926 | query = (" SELECT id " |
|
1927 | " FROM tbl_tenants_commands " |
|
1928 | " WHERE tenant_id = %s AND command_id = %s") |
|
1929 | cursor.execute(query, (id_, command_id,)) |
|
1930 | if cursor.fetchone() is not None: |
|
1931 | cursor.close() |
|
1932 | cnx.close() |
|
1933 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1934 | description='API.TENANT_COMMAND_RELATION_EXISTS') |
|
1935 | ||
1936 | add_row = (" INSERT INTO tbl_tenants_commands (tenant_id, command_id) " |
|
1937 | " VALUES (%s, %s) ") |
|
1938 | cursor.execute(add_row, (id_, command_id,)) |
|
1939 | cnx.commit() |
|
1940 | cursor.close() |
|
1941 | cnx.close() |
|
1942 | ||
1943 | resp.status = falcon.HTTP_201 |
|
1944 | resp.location = '/tenants/' + str(id_) + '/commands/' + str(command_id) |
|
1945 | ||
1946 | ||
1947 | class TenantCommandItem: |
|
@@ 1644-1761 (lines=118) @@ | ||
1641 | resp.status = falcon.HTTP_204 |
|
1642 | ||
1643 | ||
1644 | class TenantWorkingCalendarCollection: |
|
1645 | def __init__(self): |
|
1646 | """Initializes TenantWorkingCalendarCollection Class""" |
|
1647 | pass |
|
1648 | ||
1649 | @staticmethod |
|
1650 | def on_options(req, resp, id_): |
|
1651 | _ = req |
|
1652 | resp.status = falcon.HTTP_200 |
|
1653 | _ = id_ |
|
1654 | ||
1655 | @staticmethod |
|
1656 | def on_get(req, resp, id_): |
|
1657 | if 'API-KEY' not in req.headers or \ |
|
1658 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1659 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1660 | access_control(req) |
|
1661 | else: |
|
1662 | api_key_control(req) |
|
1663 | if not id_.isdigit() or int(id_) <= 0: |
|
1664 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1665 | description='API.INVALID_TENANT_ID') |
|
1666 | ||
1667 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1668 | cursor = cnx.cursor() |
|
1669 | ||
1670 | cursor.execute(" SELECT name " |
|
1671 | " FROM tbl_tenants " |
|
1672 | " WHERE id = %s ", (id_,)) |
|
1673 | if cursor.fetchone() is None: |
|
1674 | cursor.close() |
|
1675 | cnx.close() |
|
1676 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1677 | description='API.TENANT_NOT_FOUND') |
|
1678 | ||
1679 | query = (" SELECT wc.id, wc.name, wc.description " |
|
1680 | " FROM tbl_tenants t, tbl_tenants_working_calendars twc, tbl_working_calendars wc " |
|
1681 | " WHERE twc.tenant_id = t.id AND wc.id = twc.working_calendar_id AND t.id = %s " |
|
1682 | " ORDER BY wc.id ") |
|
1683 | cursor.execute(query, (id_,)) |
|
1684 | rows = cursor.fetchall() |
|
1685 | ||
1686 | result = list() |
|
1687 | if rows is not None and len(rows) > 0: |
|
1688 | for row in rows: |
|
1689 | meta_result = {"id": row[0], |
|
1690 | "name": row[1], |
|
1691 | "description": row[2]} |
|
1692 | result.append(meta_result) |
|
1693 | ||
1694 | resp.text = json.dumps(result) |
|
1695 | ||
1696 | @staticmethod |
|
1697 | @user_logger |
|
1698 | def on_post(req, resp, id_): |
|
1699 | """Handles POST requests""" |
|
1700 | admin_control(req) |
|
1701 | try: |
|
1702 | raw_json = req.stream.read().decode('utf-8') |
|
1703 | except Exception as ex: |
|
1704 | print(str(ex)) |
|
1705 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1706 | title='API.BAD_REQUEST', |
|
1707 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1708 | ||
1709 | if not id_.isdigit() or int(id_) <= 0: |
|
1710 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1711 | description='API.INVALID_TENANT_ID') |
|
1712 | ||
1713 | new_values = json.loads(raw_json) |
|
1714 | ||
1715 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
1716 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
1717 | new_values['data']['working_calendar_id'] <= 0: |
|
1718 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1719 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
1720 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
1721 | ||
1722 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1723 | cursor = cnx.cursor() |
|
1724 | ||
1725 | cursor.execute(" SELECT name " |
|
1726 | " from tbl_tenants " |
|
1727 | " WHERE id = %s ", (id_,)) |
|
1728 | if cursor.fetchone() is None: |
|
1729 | cursor.close() |
|
1730 | cnx.close() |
|
1731 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1732 | description='API.TENANT_NOT_FOUND') |
|
1733 | ||
1734 | cursor.execute(" SELECT name " |
|
1735 | " FROM tbl_working_calendars " |
|
1736 | " WHERE id = %s ", (working_calendar_id,)) |
|
1737 | if cursor.fetchone() is None: |
|
1738 | cursor.close() |
|
1739 | cnx.close() |
|
1740 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1741 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
1742 | ||
1743 | query = (" SELECT id " |
|
1744 | " FROM tbl_tenants_working_calendars " |
|
1745 | " WHERE tenant_id = %s AND working_calendar_id = %s") |
|
1746 | cursor.execute(query, (id_, working_calendar_id,)) |
|
1747 | if cursor.fetchone() is not None: |
|
1748 | cursor.close() |
|
1749 | cnx.close() |
|
1750 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1751 | description='API.TENANT_WORKING_CALENDAR_RELATION_EXISTS') |
|
1752 | ||
1753 | add_row = (" INSERT INTO tbl_tenants_working_calendars (tenant_id, working_calendar_id) " |
|
1754 | " VALUES (%s, %s) ") |
|
1755 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
1756 | cnx.commit() |
|
1757 | cursor.close() |
|
1758 | cnx.close() |
|
1759 | ||
1760 | resp.status = falcon.HTTP_201 |
|
1761 | resp.location = '/tenants/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
1762 | ||
1763 | ||
1764 | class TenantWorkingCalendarItem: |
|
@@ 1266-1383 (lines=118) @@ | ||
1263 | resp.status = falcon.HTTP_204 |
|
1264 | ||
1265 | ||
1266 | class TenantSensorCollection: |
|
1267 | def __init__(self): |
|
1268 | """Initializes Class""" |
|
1269 | pass |
|
1270 | ||
1271 | @staticmethod |
|
1272 | def on_options(req, resp, id_): |
|
1273 | _ = req |
|
1274 | resp.status = falcon.HTTP_200 |
|
1275 | _ = id_ |
|
1276 | ||
1277 | @staticmethod |
|
1278 | def on_get(req, resp, id_): |
|
1279 | if 'API-KEY' not in req.headers or \ |
|
1280 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1281 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1282 | access_control(req) |
|
1283 | else: |
|
1284 | api_key_control(req) |
|
1285 | if not id_.isdigit() or int(id_) <= 0: |
|
1286 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1287 | description='API.INVALID_TENANT_ID') |
|
1288 | ||
1289 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1290 | cursor = cnx.cursor() |
|
1291 | ||
1292 | cursor.execute(" SELECT name " |
|
1293 | " FROM tbl_tenants " |
|
1294 | " WHERE id = %s ", (id_,)) |
|
1295 | if cursor.fetchone() is None: |
|
1296 | cursor.close() |
|
1297 | cnx.close() |
|
1298 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1299 | description='API.TENANT_NOT_FOUND') |
|
1300 | ||
1301 | query = (" SELECT s.id, s.name, s.uuid " |
|
1302 | " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s " |
|
1303 | " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
1304 | " ORDER BY s.id ") |
|
1305 | cursor.execute(query, (id_,)) |
|
1306 | rows = cursor.fetchall() |
|
1307 | ||
1308 | result = list() |
|
1309 | if rows is not None and len(rows) > 0: |
|
1310 | for row in rows: |
|
1311 | meta_result = {"id": row[0], |
|
1312 | "name": row[1], |
|
1313 | "uuid": row[2]} |
|
1314 | result.append(meta_result) |
|
1315 | ||
1316 | resp.text = json.dumps(result) |
|
1317 | ||
1318 | @staticmethod |
|
1319 | @user_logger |
|
1320 | def on_post(req, resp, id_): |
|
1321 | """Handles POST requests""" |
|
1322 | admin_control(req) |
|
1323 | try: |
|
1324 | raw_json = req.stream.read().decode('utf-8') |
|
1325 | except Exception as ex: |
|
1326 | print(str(ex)) |
|
1327 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1328 | title='API.BAD_REQUEST', |
|
1329 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1330 | ||
1331 | if not id_.isdigit() or int(id_) <= 0: |
|
1332 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1333 | description='API.INVALID_TENANT_ID') |
|
1334 | ||
1335 | new_values = json.loads(raw_json) |
|
1336 | ||
1337 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
1338 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
1339 | new_values['data']['sensor_id'] <= 0: |
|
1340 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1341 | description='API.INVALID_SENSOR_ID') |
|
1342 | sensor_id = new_values['data']['sensor_id'] |
|
1343 | ||
1344 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1345 | cursor = cnx.cursor() |
|
1346 | ||
1347 | cursor.execute(" SELECT name " |
|
1348 | " from tbl_tenants " |
|
1349 | " WHERE id = %s ", (id_,)) |
|
1350 | if cursor.fetchone() is None: |
|
1351 | cursor.close() |
|
1352 | cnx.close() |
|
1353 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1354 | description='API.TENANT_NOT_FOUND') |
|
1355 | ||
1356 | cursor.execute(" SELECT name " |
|
1357 | " FROM tbl_sensors " |
|
1358 | " WHERE id = %s ", (sensor_id,)) |
|
1359 | if cursor.fetchone() is None: |
|
1360 | cursor.close() |
|
1361 | cnx.close() |
|
1362 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1363 | description='API.SENSOR_NOT_FOUND') |
|
1364 | ||
1365 | query = (" SELECT id " |
|
1366 | " FROM tbl_tenants_sensors " |
|
1367 | " WHERE tenant_id = %s AND sensor_id = %s") |
|
1368 | cursor.execute(query, (id_, sensor_id,)) |
|
1369 | if cursor.fetchone() is not None: |
|
1370 | cursor.close() |
|
1371 | cnx.close() |
|
1372 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1373 | description='API.TENANT_SENSOR_RELATION_EXISTS') |
|
1374 | ||
1375 | add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) " |
|
1376 | " VALUES (%s, %s) ") |
|
1377 | cursor.execute(add_row, (id_, sensor_id,)) |
|
1378 | cnx.commit() |
|
1379 | cursor.close() |
|
1380 | cnx.close() |
|
1381 | ||
1382 | resp.status = falcon.HTTP_201 |
|
1383 | resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id) |
|
1384 | ||
1385 | ||
1386 | class TenantSensorItem: |
@@ 1458-1575 (lines=118) @@ | ||
1455 | resp.status = falcon.HTTP_204 |
|
1456 | ||
1457 | ||
1458 | class EnergyStorageContainerDataSourceCollection: |
|
1459 | def __init__(self): |
|
1460 | """Initializes Class""" |
|
1461 | pass |
|
1462 | ||
1463 | @staticmethod |
|
1464 | def on_options(req, resp, id_): |
|
1465 | _ = req |
|
1466 | resp.status = falcon.HTTP_200 |
|
1467 | _ = id_ |
|
1468 | ||
1469 | @staticmethod |
|
1470 | def on_get(req, resp, id_): |
|
1471 | if 'API-KEY' not in req.headers or \ |
|
1472 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1473 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1474 | access_control(req) |
|
1475 | else: |
|
1476 | api_key_control(req) |
|
1477 | if not id_.isdigit() or int(id_) <= 0: |
|
1478 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1479 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
1480 | ||
1481 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1482 | cursor = cnx.cursor() |
|
1483 | ||
1484 | cursor.execute(" SELECT name " |
|
1485 | " FROM tbl_energy_storage_containers " |
|
1486 | " WHERE id = %s ", (id_,)) |
|
1487 | if cursor.fetchone() is None: |
|
1488 | cursor.close() |
|
1489 | cnx.close() |
|
1490 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1491 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
1492 | ||
1493 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
1494 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_data_sources ceds, " |
|
1495 | " tbl_data_sources ds " |
|
1496 | " WHERE ceds.energy_storage_container_id = ce.id AND ds.id = ceds.data_source_id AND ce.id = %s " |
|
1497 | " ORDER BY ds.id ") |
|
1498 | cursor.execute(query, (id_,)) |
|
1499 | rows = cursor.fetchall() |
|
1500 | ||
1501 | result = list() |
|
1502 | if rows is not None and len(rows) > 0: |
|
1503 | for row in rows: |
|
1504 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1505 | result.append(meta_result) |
|
1506 | ||
1507 | resp.text = json.dumps(result) |
|
1508 | ||
1509 | @staticmethod |
|
1510 | @user_logger |
|
1511 | def on_post(req, resp, id_): |
|
1512 | """Handles POST requests""" |
|
1513 | admin_control(req) |
|
1514 | try: |
|
1515 | raw_json = req.stream.read().decode('utf-8') |
|
1516 | except Exception as ex: |
|
1517 | print(str(ex)) |
|
1518 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1519 | title='API.BAD_REQUEST', |
|
1520 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1521 | ||
1522 | if not id_.isdigit() or int(id_) <= 0: |
|
1523 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1524 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
1525 | ||
1526 | new_values = json.loads(raw_json) |
|
1527 | ||
1528 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
1529 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
1530 | new_values['data']['data_source_id'] <= 0: |
|
1531 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1532 | description='API.INVALID_DATA_SOURCE_ID') |
|
1533 | data_source_id = new_values['data']['data_source_id'] |
|
1534 | ||
1535 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1536 | cursor = cnx.cursor() |
|
1537 | ||
1538 | cursor.execute(" SELECT name " |
|
1539 | " from tbl_energy_storage_containers " |
|
1540 | " WHERE id = %s ", (id_,)) |
|
1541 | if cursor.fetchone() is None: |
|
1542 | cursor.close() |
|
1543 | cnx.close() |
|
1544 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1545 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
1546 | ||
1547 | cursor.execute(" SELECT name " |
|
1548 | " FROM tbl_data_sources " |
|
1549 | " WHERE id = %s ", (data_source_id,)) |
|
1550 | if cursor.fetchone() is None: |
|
1551 | cursor.close() |
|
1552 | cnx.close() |
|
1553 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1554 | description='API.DATA_SOURCE_NOT_FOUND') |
|
1555 | ||
1556 | query = (" SELECT id " |
|
1557 | " FROM tbl_energy_storage_containers_data_sources " |
|
1558 | " WHERE energy_storage_container_id = %s AND data_source_id = %s") |
|
1559 | cursor.execute(query, (id_, data_source_id,)) |
|
1560 | if cursor.fetchone() is not None: |
|
1561 | cursor.close() |
|
1562 | cnx.close() |
|
1563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1564 | description='API.ENERGY_STORAGE_CONTAINER_DATA_SOURCE_RELATION_EXISTS') |
|
1565 | ||
1566 | add_row = (" INSERT INTO " |
|
1567 | " tbl_energy_storage_containers_data_sources (energy_storage_container_id, data_source_id) " |
|
1568 | " VALUES (%s, %s) ") |
|
1569 | cursor.execute(add_row, (id_, data_source_id,)) |
|
1570 | cnx.commit() |
|
1571 | cursor.close() |
|
1572 | cnx.close() |
|
1573 | ||
1574 | resp.status = falcon.HTTP_201 |
|
1575 | resp.location = '/energystoragecontainers/' + str(id_) + '/datasources/' + str(data_source_id) |
|
1576 | ||
1577 | ||
1578 | class EnergyStorageContainerDataSourceItem: |
|
@@ 1277-1392 (lines=116) @@ | ||
1274 | resp.status = falcon.HTTP_204 |
|
1275 | ||
1276 | ||
1277 | class EnergyStorageContainerCommandCollection: |
|
1278 | def __init__(self): |
|
1279 | """Initializes Class""" |
|
1280 | pass |
|
1281 | ||
1282 | @staticmethod |
|
1283 | def on_options(req, resp, id_): |
|
1284 | _ = req |
|
1285 | resp.status = falcon.HTTP_200 |
|
1286 | _ = id_ |
|
1287 | ||
1288 | @staticmethod |
|
1289 | def on_get(req, resp, id_): |
|
1290 | if 'API-KEY' not in req.headers or \ |
|
1291 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1292 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1293 | access_control(req) |
|
1294 | else: |
|
1295 | api_key_control(req) |
|
1296 | if not id_.isdigit() or int(id_) <= 0: |
|
1297 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1298 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
1299 | ||
1300 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1301 | cursor = cnx.cursor() |
|
1302 | ||
1303 | cursor.execute(" SELECT name " |
|
1304 | " FROM tbl_energy_storage_containers " |
|
1305 | " WHERE id = %s ", (id_,)) |
|
1306 | if cursor.fetchone() is None: |
|
1307 | cursor.close() |
|
1308 | cnx.close() |
|
1309 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1310 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
1311 | ||
1312 | query = (" SELECT c.id, c.name, c.uuid " |
|
1313 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_commands cec, tbl_commands c " |
|
1314 | " WHERE cec.energy_storage_container_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
1315 | " ORDER BY c.id ") |
|
1316 | cursor.execute(query, (id_,)) |
|
1317 | rows = cursor.fetchall() |
|
1318 | ||
1319 | result = list() |
|
1320 | if rows is not None and len(rows) > 0: |
|
1321 | for row in rows: |
|
1322 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1323 | result.append(meta_result) |
|
1324 | ||
1325 | resp.text = json.dumps(result) |
|
1326 | ||
1327 | @staticmethod |
|
1328 | @user_logger |
|
1329 | def on_post(req, resp, id_): |
|
1330 | """Handles POST requests""" |
|
1331 | admin_control(req) |
|
1332 | try: |
|
1333 | raw_json = req.stream.read().decode('utf-8') |
|
1334 | except Exception as ex: |
|
1335 | print(str(ex)) |
|
1336 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1337 | title='API.BAD_REQUEST', |
|
1338 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1339 | ||
1340 | if not id_.isdigit() or int(id_) <= 0: |
|
1341 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1342 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
1343 | ||
1344 | new_values = json.loads(raw_json) |
|
1345 | ||
1346 | if 'command_id' not in new_values['data'].keys() or \ |
|
1347 | not isinstance(new_values['data']['command_id'], int) or \ |
|
1348 | new_values['data']['command_id'] <= 0: |
|
1349 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1350 | description='API.INVALID_COMMAND_ID') |
|
1351 | command_id = new_values['data']['command_id'] |
|
1352 | ||
1353 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1354 | cursor = cnx.cursor() |
|
1355 | ||
1356 | cursor.execute(" SELECT name " |
|
1357 | " from tbl_energy_storage_containers " |
|
1358 | " WHERE id = %s ", (id_,)) |
|
1359 | if cursor.fetchone() is None: |
|
1360 | cursor.close() |
|
1361 | cnx.close() |
|
1362 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1363 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
1364 | ||
1365 | cursor.execute(" SELECT name " |
|
1366 | " FROM tbl_commands " |
|
1367 | " WHERE id = %s ", (command_id,)) |
|
1368 | if cursor.fetchone() is None: |
|
1369 | cursor.close() |
|
1370 | cnx.close() |
|
1371 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1372 | description='API.COMMAND_NOT_FOUND') |
|
1373 | ||
1374 | query = (" SELECT id " |
|
1375 | " FROM tbl_energy_storage_containers_commands " |
|
1376 | " WHERE energy_storage_container_id = %s AND command_id = %s") |
|
1377 | cursor.execute(query, (id_, command_id,)) |
|
1378 | if cursor.fetchone() is not None: |
|
1379 | cursor.close() |
|
1380 | cnx.close() |
|
1381 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1382 | description='API.ENERGY_STORAGE_CONTAINER_COMMAND_RELATION_EXISTS') |
|
1383 | ||
1384 | add_row = (" INSERT INTO tbl_energy_storage_containers_commands (energy_storage_container_id, command_id) " |
|
1385 | " VALUES (%s, %s) ") |
|
1386 | cursor.execute(add_row, (id_, command_id,)) |
|
1387 | cnx.commit() |
|
1388 | cursor.close() |
|
1389 | cnx.close() |
|
1390 | ||
1391 | resp.status = falcon.HTTP_201 |
|
1392 | resp.location = '/energystoragecontainers/' + str(id_) + '/commands/' + str(command_id) |
|
1393 | ||
1394 | ||
1395 | class EnergyStorageContainerCommandItem: |
@@ 601-718 (lines=118) @@ | ||
598 | resp.location = '/combinedequipments/' + str(new_id) |
|
599 | ||
600 | ||
601 | class CombinedEquipmentEquipmentCollection: |
|
602 | def __init__(self): |
|
603 | """Initializes CombinedEquipmentEquipmentCollection""" |
|
604 | pass |
|
605 | ||
606 | @staticmethod |
|
607 | def on_options(req, resp, id_): |
|
608 | _ = req |
|
609 | resp.status = falcon.HTTP_200 |
|
610 | _ = id_ |
|
611 | ||
612 | @staticmethod |
|
613 | def on_get(req, resp, id_): |
|
614 | if 'API-KEY' not in req.headers or \ |
|
615 | not isinstance(req.headers['API-KEY'], str) or \ |
|
616 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
617 | access_control(req) |
|
618 | else: |
|
619 | api_key_control(req) |
|
620 | if not id_.isdigit() or int(id_) <= 0: |
|
621 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
622 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
623 | ||
624 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
625 | cursor = cnx.cursor() |
|
626 | ||
627 | cursor.execute(" SELECT name " |
|
628 | " FROM tbl_combined_equipments " |
|
629 | " WHERE id = %s ", (id_,)) |
|
630 | if cursor.fetchone() is None: |
|
631 | cursor.close() |
|
632 | cnx.close() |
|
633 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
634 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
635 | ||
636 | query = (" SELECT e.id, e.name, e.uuid " |
|
637 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
|
638 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
|
639 | " ORDER BY e.id ") |
|
640 | cursor.execute(query, (id_,)) |
|
641 | rows = cursor.fetchall() |
|
642 | cursor.close() |
|
643 | cnx.close() |
|
644 | ||
645 | result = list() |
|
646 | if rows is not None and len(rows) > 0: |
|
647 | for row in rows: |
|
648 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
649 | result.append(meta_result) |
|
650 | ||
651 | resp.text = json.dumps(result) |
|
652 | ||
653 | @staticmethod |
|
654 | @user_logger |
|
655 | def on_post(req, resp, id_): |
|
656 | """Handles POST requests""" |
|
657 | admin_control(req) |
|
658 | try: |
|
659 | raw_json = req.stream.read().decode('utf-8') |
|
660 | except Exception as ex: |
|
661 | print(ex) |
|
662 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
663 | title='API.BAD_REQUEST', |
|
664 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
665 | ||
666 | if not id_.isdigit() or int(id_) <= 0: |
|
667 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
668 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
669 | ||
670 | new_values = json.loads(raw_json) |
|
671 | ||
672 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
673 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
674 | new_values['data']['equipment_id'] <= 0: |
|
675 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
676 | description='API.INVALID_EQUIPMENT_ID') |
|
677 | equipment_id = new_values['data']['equipment_id'] |
|
678 | ||
679 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
680 | cursor = cnx.cursor() |
|
681 | ||
682 | cursor.execute(" SELECT name " |
|
683 | " from tbl_combined_equipments " |
|
684 | " WHERE id = %s ", (id_,)) |
|
685 | if cursor.fetchone() is None: |
|
686 | cursor.close() |
|
687 | cnx.close() |
|
688 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
689 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
690 | ||
691 | cursor.execute(" SELECT name " |
|
692 | " FROM tbl_equipments " |
|
693 | " WHERE id = %s ", (equipment_id,)) |
|
694 | if cursor.fetchone() is None: |
|
695 | cursor.close() |
|
696 | cnx.close() |
|
697 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
698 | description='API.EQUIPMENT_NOT_FOUND') |
|
699 | ||
700 | query = (" SELECT id " |
|
701 | " FROM tbl_combined_equipments_equipments " |
|
702 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
|
703 | cursor.execute(query, (id_, equipment_id,)) |
|
704 | if cursor.fetchone() is not None: |
|
705 | cursor.close() |
|
706 | cnx.close() |
|
707 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
708 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
|
709 | ||
710 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
|
711 | " VALUES (%s, %s) ") |
|
712 | cursor.execute(add_row, (id_, equipment_id,)) |
|
713 | cnx.commit() |
|
714 | cursor.close() |
|
715 | cnx.close() |
|
716 | ||
717 | resp.status = falcon.HTTP_201 |
|
718 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
|
719 | ||
720 | ||
721 | class CombinedEquipmentEquipmentItem: |
|
@@ 2129-2244 (lines=116) @@ | ||
2126 | resp.status = falcon.HTTP_204 |
|
2127 | ||
2128 | ||
2129 | class CombinedEquipmentCommandCollection: |
|
2130 | def __init__(self): |
|
2131 | """Initializes Class""" |
|
2132 | pass |
|
2133 | ||
2134 | @staticmethod |
|
2135 | def on_options(req, resp, id_): |
|
2136 | _ = req |
|
2137 | resp.status = falcon.HTTP_200 |
|
2138 | _ = id_ |
|
2139 | ||
2140 | @staticmethod |
|
2141 | def on_get(req, resp, id_): |
|
2142 | if 'API-KEY' not in req.headers or \ |
|
2143 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2144 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2145 | access_control(req) |
|
2146 | else: |
|
2147 | api_key_control(req) |
|
2148 | if not id_.isdigit() or int(id_) <= 0: |
|
2149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2150 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
2151 | ||
2152 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2153 | cursor = cnx.cursor() |
|
2154 | ||
2155 | cursor.execute(" SELECT name " |
|
2156 | " FROM tbl_combined_equipments " |
|
2157 | " WHERE id = %s ", (id_,)) |
|
2158 | if cursor.fetchone() is None: |
|
2159 | cursor.close() |
|
2160 | cnx.close() |
|
2161 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2162 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
2163 | ||
2164 | query = (" SELECT c.id, c.name, c.uuid " |
|
2165 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
|
2166 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
2167 | " ORDER BY c.id ") |
|
2168 | cursor.execute(query, (id_,)) |
|
2169 | rows = cursor.fetchall() |
|
2170 | ||
2171 | result = list() |
|
2172 | if rows is not None and len(rows) > 0: |
|
2173 | for row in rows: |
|
2174 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2175 | result.append(meta_result) |
|
2176 | ||
2177 | resp.text = json.dumps(result) |
|
2178 | ||
2179 | @staticmethod |
|
2180 | @user_logger |
|
2181 | def on_post(req, resp, id_): |
|
2182 | """Handles POST requests""" |
|
2183 | admin_control(req) |
|
2184 | try: |
|
2185 | raw_json = req.stream.read().decode('utf-8') |
|
2186 | except Exception as ex: |
|
2187 | print(ex) |
|
2188 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2189 | title='API.BAD_REQUEST', |
|
2190 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2191 | ||
2192 | if not id_.isdigit() or int(id_) <= 0: |
|
2193 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2194 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
2195 | ||
2196 | new_values = json.loads(raw_json) |
|
2197 | ||
2198 | if 'command_id' not in new_values['data'].keys() or \ |
|
2199 | not isinstance(new_values['data']['command_id'], int) or \ |
|
2200 | new_values['data']['command_id'] <= 0: |
|
2201 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2202 | description='API.INVALID_COMMAND_ID') |
|
2203 | command_id = new_values['data']['command_id'] |
|
2204 | ||
2205 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2206 | cursor = cnx.cursor() |
|
2207 | ||
2208 | cursor.execute(" SELECT name " |
|
2209 | " from tbl_combined_equipments " |
|
2210 | " WHERE id = %s ", (id_,)) |
|
2211 | if cursor.fetchone() is None: |
|
2212 | cursor.close() |
|
2213 | cnx.close() |
|
2214 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2215 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
2216 | ||
2217 | cursor.execute(" SELECT name " |
|
2218 | " FROM tbl_commands " |
|
2219 | " WHERE id = %s ", (command_id,)) |
|
2220 | if cursor.fetchone() is None: |
|
2221 | cursor.close() |
|
2222 | cnx.close() |
|
2223 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2224 | description='API.COMMAND_NOT_FOUND') |
|
2225 | ||
2226 | query = (" SELECT id " |
|
2227 | " FROM tbl_combined_equipments_commands " |
|
2228 | " WHERE combined_equipment_id = %s AND command_id = %s") |
|
2229 | cursor.execute(query, (id_, command_id,)) |
|
2230 | if cursor.fetchone() is not None: |
|
2231 | cursor.close() |
|
2232 | cnx.close() |
|
2233 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2234 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
2235 | ||
2236 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
|
2237 | " VALUES (%s, %s) ") |
|
2238 | cursor.execute(add_row, (id_, command_id,)) |
|
2239 | cnx.commit() |
|
2240 | cursor.close() |
|
2241 | cnx.close() |
|
2242 | ||
2243 | resp.status = falcon.HTTP_201 |
|
2244 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
|
2245 | ||
2246 | ||
2247 | class CombinedEquipmentCommandItem: |
@@ 5574-5689 (lines=116) @@ | ||
5571 | resp.status = falcon.HTTP_204 |
|
5572 | ||
5573 | ||
5574 | class DistributionSystemCollection: |
|
5575 | def __init__(self): |
|
5576 | """Initializes Class""" |
|
5577 | pass |
|
5578 | ||
5579 | @staticmethod |
|
5580 | def on_options(req, resp, id_): |
|
5581 | _ = req |
|
5582 | resp.status = falcon.HTTP_200 |
|
5583 | _ = id_ |
|
5584 | ||
5585 | @staticmethod |
|
5586 | def on_get(req, resp, id_): |
|
5587 | if 'API-KEY' not in req.headers or \ |
|
5588 | not isinstance(req.headers['API-KEY'], str) or \ |
|
5589 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
5590 | access_control(req) |
|
5591 | else: |
|
5592 | api_key_control(req) |
|
5593 | if not id_.isdigit() or int(id_) <= 0: |
|
5594 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5595 | description='API.INVALID_SPACE_ID') |
|
5596 | ||
5597 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5598 | cursor = cnx.cursor() |
|
5599 | ||
5600 | cursor.execute(" SELECT name " |
|
5601 | " FROM tbl_spaces " |
|
5602 | " WHERE id = %s ", (id_,)) |
|
5603 | if cursor.fetchone() is None: |
|
5604 | cursor.close() |
|
5605 | cnx.close() |
|
5606 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5607 | description='API.SPACE_NOT_FOUND') |
|
5608 | ||
5609 | query = (" SELECT d.id, d.name, d.uuid " |
|
5610 | " FROM tbl_spaces s, tbl_spaces_distribution_systems sd, tbl_distribution_systems d " |
|
5611 | " WHERE sd.space_id = s.id AND d.id = sd.distribution_system_id AND s.id = %s " |
|
5612 | " ORDER BY d.id ") |
|
5613 | cursor.execute(query, (id_,)) |
|
5614 | rows = cursor.fetchall() |
|
5615 | ||
5616 | result = list() |
|
5617 | if rows is not None and len(rows) > 0: |
|
5618 | for row in rows: |
|
5619 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
5620 | result.append(meta_result) |
|
5621 | ||
5622 | resp.text = json.dumps(result) |
|
5623 | ||
5624 | @staticmethod |
|
5625 | @user_logger |
|
5626 | def on_post(req, resp, id_): |
|
5627 | """Handles POST requests""" |
|
5628 | admin_control(req) |
|
5629 | try: |
|
5630 | raw_json = req.stream.read().decode('utf-8') |
|
5631 | except Exception as ex: |
|
5632 | print(str(ex)) |
|
5633 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5634 | title='API.BAD_REQUEST', |
|
5635 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5636 | ||
5637 | if not id_.isdigit() or int(id_) <= 0: |
|
5638 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5639 | description='API.INVALID_SPACE_ID') |
|
5640 | ||
5641 | new_values = json.loads(raw_json) |
|
5642 | ||
5643 | if 'distribution_system_id' not in new_values['data'].keys() or \ |
|
5644 | not isinstance(new_values['data']['distribution_system_id'], int) or \ |
|
5645 | new_values['data']['distribution_system_id'] <= 0: |
|
5646 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5647 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
|
5648 | distribution_system_id = new_values['data']['distribution_system_id'] |
|
5649 | ||
5650 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5651 | cursor = cnx.cursor() |
|
5652 | ||
5653 | cursor.execute(" SELECT name " |
|
5654 | " from tbl_spaces " |
|
5655 | " WHERE id = %s ", (id_,)) |
|
5656 | if cursor.fetchone() is None: |
|
5657 | cursor.close() |
|
5658 | cnx.close() |
|
5659 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5660 | description='API.SPACE_NOT_FOUND') |
|
5661 | ||
5662 | cursor.execute(" SELECT name " |
|
5663 | " FROM tbl_distribution_systems " |
|
5664 | " WHERE id = %s ", (distribution_system_id,)) |
|
5665 | if cursor.fetchone() is None: |
|
5666 | cursor.close() |
|
5667 | cnx.close() |
|
5668 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5669 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
|
5670 | ||
5671 | query = (" SELECT id " |
|
5672 | " FROM tbl_spaces_distribution_systems " |
|
5673 | " WHERE space_id = %s AND distribution_system_id = %s") |
|
5674 | cursor.execute(query, (id_, distribution_system_id,)) |
|
5675 | if cursor.fetchone() is not None: |
|
5676 | cursor.close() |
|
5677 | cnx.close() |
|
5678 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
5679 | description='API.SPACE_DISTRIBUTION_SYSTEM_RELATION_EXISTS') |
|
5680 | ||
5681 | add_row = (" INSERT INTO tbl_spaces_distribution_systems (space_id, distribution_system_id) " |
|
5682 | " VALUES (%s, %s) ") |
|
5683 | cursor.execute(add_row, (id_, distribution_system_id,)) |
|
5684 | cnx.commit() |
|
5685 | cursor.close() |
|
5686 | cnx.close() |
|
5687 | ||
5688 | resp.status = falcon.HTTP_201 |
|
5689 | resp.location = '/spaces/' + str(id_) + '/distributionsystems/' + str(distribution_system_id) |
|
5690 | ||
5691 | ||
5692 | class DistributionSystemItem: |
|
@@ 5393-5508 (lines=116) @@ | ||
5390 | resp.location = '/spaces/' + str(new_id) |
|
5391 | ||
5392 | ||
5393 | class SpaceEnergyFlowDiagramCollection: |
|
5394 | def __init__(self): |
|
5395 | """Initializes Class""" |
|
5396 | pass |
|
5397 | ||
5398 | @staticmethod |
|
5399 | def on_options(req, resp, id_): |
|
5400 | _ = req |
|
5401 | resp.status = falcon.HTTP_200 |
|
5402 | _ = id_ |
|
5403 | ||
5404 | @staticmethod |
|
5405 | def on_get(req, resp, id_): |
|
5406 | if 'API-KEY' not in req.headers or \ |
|
5407 | not isinstance(req.headers['API-KEY'], str) or \ |
|
5408 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
5409 | access_control(req) |
|
5410 | else: |
|
5411 | api_key_control(req) |
|
5412 | if not id_.isdigit() or int(id_) <= 0: |
|
5413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5414 | description='API.INVALID_SPACE_ID') |
|
5415 | ||
5416 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5417 | cursor = cnx.cursor() |
|
5418 | ||
5419 | cursor.execute(" SELECT name " |
|
5420 | " FROM tbl_spaces " |
|
5421 | " WHERE id = %s ", (id_,)) |
|
5422 | if cursor.fetchone() is None: |
|
5423 | cursor.close() |
|
5424 | cnx.close() |
|
5425 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5426 | description='API.SPACE_NOT_FOUND') |
|
5427 | ||
5428 | query = (" SELECT e.id, e.name, e.uuid " |
|
5429 | " FROM tbl_spaces s, tbl_spaces_energy_flow_diagrams se, tbl_energy_flow_diagrams e " |
|
5430 | " WHERE se.space_id = s.id AND e.id = se.energy_flow_diagram_id AND s.id = %s " |
|
5431 | " ORDER BY e.id ") |
|
5432 | cursor.execute(query, (id_,)) |
|
5433 | rows = cursor.fetchall() |
|
5434 | ||
5435 | result = list() |
|
5436 | if rows is not None and len(rows) > 0: |
|
5437 | for row in rows: |
|
5438 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
5439 | result.append(meta_result) |
|
5440 | ||
5441 | resp.text = json.dumps(result) |
|
5442 | ||
5443 | @staticmethod |
|
5444 | @user_logger |
|
5445 | def on_post(req, resp, id_): |
|
5446 | """Handles POST requests""" |
|
5447 | admin_control(req) |
|
5448 | try: |
|
5449 | raw_json = req.stream.read().decode('utf-8') |
|
5450 | except Exception as ex: |
|
5451 | print(str(ex)) |
|
5452 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5453 | title='API.BAD_REQUEST', |
|
5454 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5455 | ||
5456 | if not id_.isdigit() or int(id_) <= 0: |
|
5457 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5458 | description='API.INVALID_SPACE_ID') |
|
5459 | ||
5460 | new_values = json.loads(raw_json) |
|
5461 | ||
5462 | if 'energy_flow_diagram_id' not in new_values['data'].keys() or \ |
|
5463 | not isinstance(new_values['data']['energy_flow_diagram_id'], int) or \ |
|
5464 | new_values['data']['energy_flow_diagram_id'] <= 0: |
|
5465 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5466 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
5467 | energy_flow_diagram_id = new_values['data']['energy_flow_diagram_id'] |
|
5468 | ||
5469 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5470 | cursor = cnx.cursor() |
|
5471 | ||
5472 | cursor.execute(" SELECT name " |
|
5473 | " from tbl_spaces " |
|
5474 | " WHERE id = %s ", (id_,)) |
|
5475 | if cursor.fetchone() is None: |
|
5476 | cursor.close() |
|
5477 | cnx.close() |
|
5478 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5479 | description='API.SPACE_NOT_FOUND') |
|
5480 | ||
5481 | cursor.execute(" SELECT name " |
|
5482 | " FROM tbl_energy_flow_diagrams " |
|
5483 | " WHERE id = %s ", (energy_flow_diagram_id,)) |
|
5484 | if cursor.fetchone() is None: |
|
5485 | cursor.close() |
|
5486 | cnx.close() |
|
5487 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5488 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
5489 | ||
5490 | query = (" SELECT id " |
|
5491 | " FROM tbl_spaces_energy_flow_diagrams " |
|
5492 | " WHERE space_id = %s AND energy_flow_diagram_id = %s") |
|
5493 | cursor.execute(query, (id_, energy_flow_diagram_id,)) |
|
5494 | if cursor.fetchone() is not None: |
|
5495 | cursor.close() |
|
5496 | cnx.close() |
|
5497 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
5498 | description='API.SPACE_ENERGY_FLOW_DIAGRAM_RELATION_EXISTS') |
|
5499 | ||
5500 | add_row = (" INSERT INTO tbl_spaces_energy_flow_diagrams (space_id, energy_flow_diagram_id) " |
|
5501 | " VALUES (%s, %s) ") |
|
5502 | cursor.execute(add_row, (id_, energy_flow_diagram_id,)) |
|
5503 | cnx.commit() |
|
5504 | cursor.close() |
|
5505 | cnx.close() |
|
5506 | ||
5507 | resp.status = falcon.HTTP_201 |
|
5508 | resp.location = '/spaces/' + str(id_) + '/energyflowdiagrams/' + str(energy_flow_diagram_id) |
|
5509 | ||
5510 | ||
5511 | class SpaceEnergyFlowDiagramItem: |
|
@@ 3654-3769 (lines=116) @@ | ||
3651 | resp.status = falcon.HTTP_204 |
|
3652 | ||
3653 | ||
3654 | class SpaceCommandCollection: |
|
3655 | def __init__(self): |
|
3656 | """Initializes Class""" |
|
3657 | pass |
|
3658 | ||
3659 | @staticmethod |
|
3660 | def on_options(req, resp, id_): |
|
3661 | _ = req |
|
3662 | resp.status = falcon.HTTP_200 |
|
3663 | _ = id_ |
|
3664 | ||
3665 | @staticmethod |
|
3666 | def on_get(req, resp, id_): |
|
3667 | if 'API-KEY' not in req.headers or \ |
|
3668 | not isinstance(req.headers['API-KEY'], str) or \ |
|
3669 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
3670 | access_control(req) |
|
3671 | else: |
|
3672 | api_key_control(req) |
|
3673 | if not id_.isdigit() or int(id_) <= 0: |
|
3674 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3675 | description='API.INVALID_SPACE_ID') |
|
3676 | ||
3677 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3678 | cursor = cnx.cursor() |
|
3679 | ||
3680 | cursor.execute(" SELECT name " |
|
3681 | " FROM tbl_spaces " |
|
3682 | " WHERE id = %s ", (id_,)) |
|
3683 | if cursor.fetchone() is None: |
|
3684 | cursor.close() |
|
3685 | cnx.close() |
|
3686 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3687 | description='API.SPACE_NOT_FOUND') |
|
3688 | ||
3689 | query = (" SELECT c.id, c.name, c.uuid " |
|
3690 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
|
3691 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
3692 | " ORDER BY c.id ") |
|
3693 | cursor.execute(query, (id_,)) |
|
3694 | rows = cursor.fetchall() |
|
3695 | ||
3696 | result = list() |
|
3697 | if rows is not None and len(rows) > 0: |
|
3698 | for row in rows: |
|
3699 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
3700 | result.append(meta_result) |
|
3701 | ||
3702 | resp.text = json.dumps(result) |
|
3703 | ||
3704 | @staticmethod |
|
3705 | @user_logger |
|
3706 | def on_post(req, resp, id_): |
|
3707 | """Handles POST requests""" |
|
3708 | admin_control(req) |
|
3709 | try: |
|
3710 | raw_json = req.stream.read().decode('utf-8') |
|
3711 | except Exception as ex: |
|
3712 | print(str(ex)) |
|
3713 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
3714 | title='API.BAD_REQUEST', |
|
3715 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
3716 | ||
3717 | if not id_.isdigit() or int(id_) <= 0: |
|
3718 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3719 | description='API.INVALID_SPACE_ID') |
|
3720 | ||
3721 | new_values = json.loads(raw_json) |
|
3722 | ||
3723 | if 'command_id' not in new_values['data'].keys() or \ |
|
3724 | not isinstance(new_values['data']['command_id'], int) or \ |
|
3725 | new_values['data']['command_id'] <= 0: |
|
3726 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3727 | description='API.INVALID_COMMAND_ID') |
|
3728 | command_id = new_values['data']['command_id'] |
|
3729 | ||
3730 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3731 | cursor = cnx.cursor() |
|
3732 | ||
3733 | cursor.execute(" SELECT name " |
|
3734 | " from tbl_spaces " |
|
3735 | " WHERE id = %s ", (id_,)) |
|
3736 | if cursor.fetchone() is None: |
|
3737 | cursor.close() |
|
3738 | cnx.close() |
|
3739 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3740 | description='API.SPACE_NOT_FOUND') |
|
3741 | ||
3742 | cursor.execute(" SELECT name " |
|
3743 | " FROM tbl_commands " |
|
3744 | " WHERE id = %s ", (command_id,)) |
|
3745 | if cursor.fetchone() is None: |
|
3746 | cursor.close() |
|
3747 | cnx.close() |
|
3748 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3749 | description='API.COMMAND_NOT_FOUND') |
|
3750 | ||
3751 | query = (" SELECT id " |
|
3752 | " FROM tbl_spaces_commands " |
|
3753 | " WHERE space_id = %s AND command_id = %s") |
|
3754 | cursor.execute(query, (id_, command_id,)) |
|
3755 | if cursor.fetchone() is not None: |
|
3756 | cursor.close() |
|
3757 | cnx.close() |
|
3758 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
3759 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
|
3760 | ||
3761 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
|
3762 | " VALUES (%s, %s) ") |
|
3763 | cursor.execute(add_row, (id_, command_id,)) |
|
3764 | cnx.commit() |
|
3765 | cursor.close() |
|
3766 | cnx.close() |
|
3767 | ||
3768 | resp.status = falcon.HTTP_201 |
|
3769 | resp.location = '/spaces/' + str(id_) + '/commands/' + str(command_id) |
|
3770 | ||
3771 | ||
3772 | class SpaceCommandItem: |
|
@@ 3473-3588 (lines=116) @@ | ||
3470 | resp.text = json.dumps(result) |
|
3471 | ||
3472 | ||
3473 | class SpaceWorkingCalendarCollection: |
|
3474 | def __init__(self): |
|
3475 | """Initializes SpaceWorkingCalendarCollection Class""" |
|
3476 | pass |
|
3477 | ||
3478 | @staticmethod |
|
3479 | def on_options(req, resp, id_): |
|
3480 | _ = req |
|
3481 | resp.status = falcon.HTTP_200 |
|
3482 | _ = id_ |
|
3483 | ||
3484 | @staticmethod |
|
3485 | def on_get(req, resp, id_): |
|
3486 | if 'API-KEY' not in req.headers or \ |
|
3487 | not isinstance(req.headers['API-KEY'], str) or \ |
|
3488 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
3489 | access_control(req) |
|
3490 | else: |
|
3491 | api_key_control(req) |
|
3492 | if not id_.isdigit() or int(id_) <= 0: |
|
3493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3494 | description='API.INVALID_SPACE_ID') |
|
3495 | ||
3496 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3497 | cursor = cnx.cursor() |
|
3498 | ||
3499 | cursor.execute(" SELECT name " |
|
3500 | " FROM tbl_spaces " |
|
3501 | " WHERE id = %s ", (id_,)) |
|
3502 | if cursor.fetchone() is None: |
|
3503 | cursor.close() |
|
3504 | cnx.close() |
|
3505 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3506 | description='API.SPACE_NOT_FOUND') |
|
3507 | ||
3508 | query = (" SELECT wc.id, wc.name, wc.description " |
|
3509 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
|
3510 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
3511 | " ORDER BY wc.id ") |
|
3512 | cursor.execute(query, (id_,)) |
|
3513 | rows = cursor.fetchall() |
|
3514 | ||
3515 | result = list() |
|
3516 | if rows is not None and len(rows) > 0: |
|
3517 | for row in rows: |
|
3518 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
3519 | result.append(meta_result) |
|
3520 | ||
3521 | resp.text = json.dumps(result) |
|
3522 | ||
3523 | @staticmethod |
|
3524 | @user_logger |
|
3525 | def on_post(req, resp, id_): |
|
3526 | """Handles POST requests""" |
|
3527 | admin_control(req) |
|
3528 | try: |
|
3529 | raw_json = req.stream.read().decode('utf-8') |
|
3530 | except Exception as ex: |
|
3531 | print(str(ex)) |
|
3532 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
3533 | title='API.BAD_REQUEST', |
|
3534 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
3535 | ||
3536 | if not id_.isdigit() or int(id_) <= 0: |
|
3537 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3538 | description='API.INVALID_SPACE_ID') |
|
3539 | ||
3540 | new_values = json.loads(raw_json) |
|
3541 | ||
3542 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
3543 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
3544 | new_values['data']['working_calendar_id'] <= 0: |
|
3545 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3546 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
3547 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
3548 | ||
3549 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3550 | cursor = cnx.cursor() |
|
3551 | ||
3552 | cursor.execute(" SELECT name " |
|
3553 | " from tbl_spaces " |
|
3554 | " WHERE id = %s ", (id_,)) |
|
3555 | if cursor.fetchone() is None: |
|
3556 | cursor.close() |
|
3557 | cnx.close() |
|
3558 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3559 | description='API.SPACE_NOT_FOUND') |
|
3560 | ||
3561 | cursor.execute(" SELECT name " |
|
3562 | " FROM tbl_working_calendars " |
|
3563 | " WHERE id = %s ", (working_calendar_id,)) |
|
3564 | if cursor.fetchone() is None: |
|
3565 | cursor.close() |
|
3566 | cnx.close() |
|
3567 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3568 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
3569 | ||
3570 | query = (" SELECT id " |
|
3571 | " FROM tbl_spaces_working_calendars " |
|
3572 | " WHERE space_id = %s AND working_calendar_id = %s") |
|
3573 | cursor.execute(query, (id_, working_calendar_id,)) |
|
3574 | if cursor.fetchone() is not None: |
|
3575 | cursor.close() |
|
3576 | cnx.close() |
|
3577 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
3578 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
|
3579 | ||
3580 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
|
3581 | " VALUES (%s, %s) ") |
|
3582 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
3583 | cnx.commit() |
|
3584 | cursor.close() |
|
3585 | cnx.close() |
|
3586 | ||
3587 | resp.status = falcon.HTTP_201 |
|
3588 | resp.location = '/spaces/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
3589 | ||
3590 | ||
3591 | class SpaceWorkingCalendarItem: |
|
@@ 2911-3026 (lines=116) @@ | ||
2908 | resp.status = falcon.HTTP_204 |
|
2909 | ||
2910 | ||
2911 | class SpaceTenantCollection: |
|
2912 | def __init__(self): |
|
2913 | """Initializes Class""" |
|
2914 | pass |
|
2915 | ||
2916 | @staticmethod |
|
2917 | def on_options(req, resp, id_): |
|
2918 | _ = req |
|
2919 | resp.status = falcon.HTTP_200 |
|
2920 | _ = id_ |
|
2921 | ||
2922 | @staticmethod |
|
2923 | def on_get(req, resp, id_): |
|
2924 | if 'API-KEY' not in req.headers or \ |
|
2925 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2926 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2927 | access_control(req) |
|
2928 | else: |
|
2929 | api_key_control(req) |
|
2930 | if not id_.isdigit() or int(id_) <= 0: |
|
2931 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2932 | description='API.INVALID_SPACE_ID') |
|
2933 | ||
2934 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2935 | cursor = cnx.cursor() |
|
2936 | ||
2937 | cursor.execute(" SELECT name " |
|
2938 | " FROM tbl_spaces " |
|
2939 | " WHERE id = %s ", (id_,)) |
|
2940 | if cursor.fetchone() is None: |
|
2941 | cursor.close() |
|
2942 | cnx.close() |
|
2943 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2944 | description='API.SPACE_NOT_FOUND') |
|
2945 | ||
2946 | query = (" SELECT t.id, t.name, t.uuid " |
|
2947 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
|
2948 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
|
2949 | " ORDER BY t.id ") |
|
2950 | cursor.execute(query, (id_,)) |
|
2951 | rows = cursor.fetchall() |
|
2952 | ||
2953 | result = list() |
|
2954 | if rows is not None and len(rows) > 0: |
|
2955 | for row in rows: |
|
2956 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2957 | result.append(meta_result) |
|
2958 | ||
2959 | resp.text = json.dumps(result) |
|
2960 | ||
2961 | @staticmethod |
|
2962 | @user_logger |
|
2963 | def on_post(req, resp, id_): |
|
2964 | """Handles POST requests""" |
|
2965 | admin_control(req) |
|
2966 | try: |
|
2967 | raw_json = req.stream.read().decode('utf-8') |
|
2968 | except Exception as ex: |
|
2969 | print(str(ex)) |
|
2970 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2971 | title='API.BAD_REQUEST', |
|
2972 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2973 | ||
2974 | if not id_.isdigit() or int(id_) <= 0: |
|
2975 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2976 | description='API.INVALID_SPACE_ID') |
|
2977 | ||
2978 | new_values = json.loads(raw_json) |
|
2979 | ||
2980 | if 'tenant_id' not in new_values['data'].keys() or \ |
|
2981 | not isinstance(new_values['data']['tenant_id'], int) or \ |
|
2982 | new_values['data']['tenant_id'] <= 0: |
|
2983 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2984 | description='API.INVALID_TENANT_ID') |
|
2985 | tenant_id = new_values['data']['tenant_id'] |
|
2986 | ||
2987 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2988 | cursor = cnx.cursor() |
|
2989 | ||
2990 | cursor.execute(" SELECT name " |
|
2991 | " from tbl_spaces " |
|
2992 | " WHERE id = %s ", (id_,)) |
|
2993 | if cursor.fetchone() is None: |
|
2994 | cursor.close() |
|
2995 | cnx.close() |
|
2996 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2997 | description='API.SPACE_NOT_FOUND') |
|
2998 | ||
2999 | cursor.execute(" SELECT name " |
|
3000 | " FROM tbl_tenants " |
|
3001 | " WHERE id = %s ", (tenant_id,)) |
|
3002 | if cursor.fetchone() is None: |
|
3003 | cursor.close() |
|
3004 | cnx.close() |
|
3005 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3006 | description='API.TENANT_NOT_FOUND') |
|
3007 | ||
3008 | query = (" SELECT id " |
|
3009 | " FROM tbl_spaces_tenants " |
|
3010 | " WHERE space_id = %s AND tenant_id = %s") |
|
3011 | cursor.execute(query, (id_, tenant_id,)) |
|
3012 | if cursor.fetchone() is not None: |
|
3013 | cursor.close() |
|
3014 | cnx.close() |
|
3015 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
3016 | description='API.SPACE_TENANT_RELATION_EXISTS') |
|
3017 | ||
3018 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
|
3019 | " VALUES (%s, %s) ") |
|
3020 | cursor.execute(add_row, (id_, tenant_id,)) |
|
3021 | cnx.commit() |
|
3022 | cursor.close() |
|
3023 | cnx.close() |
|
3024 | ||
3025 | resp.status = falcon.HTTP_201 |
|
3026 | resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id) |
|
3027 | ||
3028 | ||
3029 | class SpaceTenantItem: |
|
@@ 2731-2846 (lines=116) @@ | ||
2728 | resp.status = falcon.HTTP_204 |
|
2729 | ||
2730 | ||
2731 | class SpaceStoreCollection: |
|
2732 | def __init__(self): |
|
2733 | """Initializes Class""" |
|
2734 | pass |
|
2735 | ||
2736 | @staticmethod |
|
2737 | def on_options(req, resp, id_): |
|
2738 | _ = req |
|
2739 | resp.status = falcon.HTTP_200 |
|
2740 | _ = id_ |
|
2741 | ||
2742 | @staticmethod |
|
2743 | def on_get(req, resp, id_): |
|
2744 | if 'API-KEY' not in req.headers or \ |
|
2745 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2746 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2747 | access_control(req) |
|
2748 | else: |
|
2749 | api_key_control(req) |
|
2750 | if not id_.isdigit() or int(id_) <= 0: |
|
2751 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2752 | description='API.INVALID_SPACE_ID') |
|
2753 | ||
2754 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2755 | cursor = cnx.cursor() |
|
2756 | ||
2757 | cursor.execute(" SELECT name " |
|
2758 | " FROM tbl_spaces " |
|
2759 | " WHERE id = %s ", (id_,)) |
|
2760 | if cursor.fetchone() is None: |
|
2761 | cursor.close() |
|
2762 | cnx.close() |
|
2763 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2764 | description='API.SPACE_NOT_FOUND') |
|
2765 | ||
2766 | query = (" SELECT t.id, t.name, t.uuid " |
|
2767 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
|
2768 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
|
2769 | " ORDER BY t.id ") |
|
2770 | cursor.execute(query, (id_,)) |
|
2771 | rows = cursor.fetchall() |
|
2772 | ||
2773 | result = list() |
|
2774 | if rows is not None and len(rows) > 0: |
|
2775 | for row in rows: |
|
2776 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2777 | result.append(meta_result) |
|
2778 | ||
2779 | resp.text = json.dumps(result) |
|
2780 | ||
2781 | @staticmethod |
|
2782 | @user_logger |
|
2783 | def on_post(req, resp, id_): |
|
2784 | """Handles POST requests""" |
|
2785 | admin_control(req) |
|
2786 | try: |
|
2787 | raw_json = req.stream.read().decode('utf-8') |
|
2788 | except Exception as ex: |
|
2789 | print(str(ex)) |
|
2790 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2791 | title='API.BAD_REQUEST', |
|
2792 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2793 | ||
2794 | if not id_.isdigit() or int(id_) <= 0: |
|
2795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2796 | description='API.INVALID_SPACE_ID') |
|
2797 | ||
2798 | new_values = json.loads(raw_json) |
|
2799 | ||
2800 | if 'store_id' not in new_values['data'].keys() or \ |
|
2801 | not isinstance(new_values['data']['store_id'], int) or \ |
|
2802 | new_values['data']['store_id'] <= 0: |
|
2803 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2804 | description='API.INVALID_STORE_ID') |
|
2805 | store_id = new_values['data']['store_id'] |
|
2806 | ||
2807 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2808 | cursor = cnx.cursor() |
|
2809 | ||
2810 | cursor.execute(" SELECT name " |
|
2811 | " from tbl_spaces " |
|
2812 | " WHERE id = %s ", (id_,)) |
|
2813 | if cursor.fetchone() is None: |
|
2814 | cursor.close() |
|
2815 | cnx.close() |
|
2816 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2817 | description='API.SPACE_NOT_FOUND') |
|
2818 | ||
2819 | cursor.execute(" SELECT name " |
|
2820 | " FROM tbl_stores " |
|
2821 | " WHERE id = %s ", (store_id,)) |
|
2822 | if cursor.fetchone() is None: |
|
2823 | cursor.close() |
|
2824 | cnx.close() |
|
2825 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2826 | description='API.STORE_NOT_FOUND') |
|
2827 | ||
2828 | query = (" SELECT id " |
|
2829 | " FROM tbl_spaces_stores " |
|
2830 | " WHERE space_id = %s AND store_id = %s") |
|
2831 | cursor.execute(query, (id_, store_id,)) |
|
2832 | if cursor.fetchone() is not None: |
|
2833 | cursor.close() |
|
2834 | cnx.close() |
|
2835 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2836 | description='API.SPACE_STORE_RELATION_EXISTS') |
|
2837 | ||
2838 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
|
2839 | " VALUES (%s, %s) ") |
|
2840 | cursor.execute(add_row, (id_, store_id,)) |
|
2841 | cnx.commit() |
|
2842 | cursor.close() |
|
2843 | cnx.close() |
|
2844 | ||
2845 | resp.status = falcon.HTTP_201 |
|
2846 | resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id) |
|
2847 | ||
2848 | ||
2849 | class SpaceStoreItem: |
|
@@ 2551-2666 (lines=116) @@ | ||
2548 | resp.status = falcon.HTTP_204 |
|
2549 | ||
2550 | ||
2551 | class SpaceShopfloorCollection: |
|
2552 | def __init__(self): |
|
2553 | """Initializes Class""" |
|
2554 | pass |
|
2555 | ||
2556 | @staticmethod |
|
2557 | def on_options(req, resp, id_): |
|
2558 | _ = req |
|
2559 | resp.status = falcon.HTTP_200 |
|
2560 | _ = id_ |
|
2561 | ||
2562 | @staticmethod |
|
2563 | def on_get(req, resp, id_): |
|
2564 | if 'API-KEY' not in req.headers or \ |
|
2565 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2566 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2567 | access_control(req) |
|
2568 | else: |
|
2569 | api_key_control(req) |
|
2570 | if not id_.isdigit() or int(id_) <= 0: |
|
2571 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2572 | description='API.INVALID_SPACE_ID') |
|
2573 | ||
2574 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2575 | cursor = cnx.cursor() |
|
2576 | ||
2577 | cursor.execute(" SELECT name " |
|
2578 | " FROM tbl_spaces " |
|
2579 | " WHERE id = %s ", (id_,)) |
|
2580 | if cursor.fetchone() is None: |
|
2581 | cursor.close() |
|
2582 | cnx.close() |
|
2583 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2584 | description='API.SPACE_NOT_FOUND') |
|
2585 | ||
2586 | query = (" SELECT sf.id, sf.name, sf.uuid " |
|
2587 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
|
2588 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
|
2589 | " ORDER BY sf.id ") |
|
2590 | cursor.execute(query, (id_,)) |
|
2591 | rows = cursor.fetchall() |
|
2592 | ||
2593 | result = list() |
|
2594 | if rows is not None and len(rows) > 0: |
|
2595 | for row in rows: |
|
2596 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2597 | result.append(meta_result) |
|
2598 | ||
2599 | resp.text = json.dumps(result) |
|
2600 | ||
2601 | @staticmethod |
|
2602 | @user_logger |
|
2603 | def on_post(req, resp, id_): |
|
2604 | """Handles POST requests""" |
|
2605 | admin_control(req) |
|
2606 | try: |
|
2607 | raw_json = req.stream.read().decode('utf-8') |
|
2608 | except Exception as ex: |
|
2609 | print(str(ex)) |
|
2610 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2611 | title='API.BAD_REQUEST', |
|
2612 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2613 | ||
2614 | if not id_.isdigit() or int(id_) <= 0: |
|
2615 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2616 | description='API.INVALID_SPACE_ID') |
|
2617 | ||
2618 | new_values = json.loads(raw_json) |
|
2619 | ||
2620 | if 'shopfloor_id' not in new_values['data'].keys() or \ |
|
2621 | not isinstance(new_values['data']['shopfloor_id'], int) or \ |
|
2622 | new_values['data']['shopfloor_id'] <= 0: |
|
2623 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2624 | description='API.INVALID_SHOPFLOOR_ID') |
|
2625 | shopfloor_id = new_values['data']['shopfloor_id'] |
|
2626 | ||
2627 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2628 | cursor = cnx.cursor() |
|
2629 | ||
2630 | cursor.execute(" SELECT name " |
|
2631 | " from tbl_spaces " |
|
2632 | " WHERE id = %s ", (id_,)) |
|
2633 | if cursor.fetchone() is None: |
|
2634 | cursor.close() |
|
2635 | cnx.close() |
|
2636 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2637 | description='API.SPACE_NOT_FOUND') |
|
2638 | ||
2639 | cursor.execute(" SELECT name " |
|
2640 | " FROM tbl_shopfloors " |
|
2641 | " WHERE id = %s ", (shopfloor_id,)) |
|
2642 | if cursor.fetchone() is None: |
|
2643 | cursor.close() |
|
2644 | cnx.close() |
|
2645 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2646 | description='API.SHOPFLOOR_NOT_FOUND') |
|
2647 | ||
2648 | query = (" SELECT id " |
|
2649 | " FROM tbl_spaces_shopfloors " |
|
2650 | " WHERE space_id = %s AND shopfloor_id = %s") |
|
2651 | cursor.execute(query, (id_, shopfloor_id,)) |
|
2652 | if cursor.fetchone() is not None: |
|
2653 | cursor.close() |
|
2654 | cnx.close() |
|
2655 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2656 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
|
2657 | ||
2658 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
|
2659 | " VALUES (%s, %s) ") |
|
2660 | cursor.execute(add_row, (id_, shopfloor_id,)) |
|
2661 | cnx.commit() |
|
2662 | cursor.close() |
|
2663 | cnx.close() |
|
2664 | ||
2665 | resp.status = falcon.HTTP_201 |
|
2666 | resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id) |
|
2667 | ||
2668 | ||
2669 | class SpaceShopfloorItem: |
|
@@ 2371-2486 (lines=116) @@ | ||
2368 | resp.status = falcon.HTTP_204 |
|
2369 | ||
2370 | ||
2371 | class SpaceSensorCollection: |
|
2372 | def __init__(self): |
|
2373 | """Initializes Class""" |
|
2374 | pass |
|
2375 | ||
2376 | @staticmethod |
|
2377 | def on_options(req, resp, id_): |
|
2378 | _ = req |
|
2379 | resp.status = falcon.HTTP_200 |
|
2380 | _ = id_ |
|
2381 | ||
2382 | @staticmethod |
|
2383 | def on_get(req, resp, id_): |
|
2384 | if 'API-KEY' not in req.headers or \ |
|
2385 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2386 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2387 | access_control(req) |
|
2388 | else: |
|
2389 | api_key_control(req) |
|
2390 | if not id_.isdigit() or int(id_) <= 0: |
|
2391 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2392 | description='API.INVALID_SPACE_ID') |
|
2393 | ||
2394 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2395 | cursor = cnx.cursor() |
|
2396 | ||
2397 | cursor.execute(" SELECT name " |
|
2398 | " FROM tbl_spaces " |
|
2399 | " WHERE id = %s ", (id_,)) |
|
2400 | if cursor.fetchone() is None: |
|
2401 | cursor.close() |
|
2402 | cnx.close() |
|
2403 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2404 | description='API.SPACE_NOT_FOUND') |
|
2405 | ||
2406 | query = (" SELECT se.id, se.name, se.uuid " |
|
2407 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
|
2408 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
2409 | " ORDER BY se.id ") |
|
2410 | cursor.execute(query, (id_,)) |
|
2411 | rows = cursor.fetchall() |
|
2412 | ||
2413 | result = list() |
|
2414 | if rows is not None and len(rows) > 0: |
|
2415 | for row in rows: |
|
2416 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2417 | result.append(meta_result) |
|
2418 | ||
2419 | resp.text = json.dumps(result) |
|
2420 | ||
2421 | @staticmethod |
|
2422 | @user_logger |
|
2423 | def on_post(req, resp, id_): |
|
2424 | """Handles POST requests""" |
|
2425 | admin_control(req) |
|
2426 | try: |
|
2427 | raw_json = req.stream.read().decode('utf-8') |
|
2428 | except Exception as ex: |
|
2429 | print(str(ex)) |
|
2430 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2431 | title='API.BAD_REQUEST', |
|
2432 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2433 | ||
2434 | if not id_.isdigit() or int(id_) <= 0: |
|
2435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2436 | description='API.INVALID_SPACE_ID') |
|
2437 | ||
2438 | new_values = json.loads(raw_json) |
|
2439 | ||
2440 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
2441 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
2442 | new_values['data']['sensor_id'] <= 0: |
|
2443 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2444 | description='API.INVALID_SENSOR_ID') |
|
2445 | sensor_id = new_values['data']['sensor_id'] |
|
2446 | ||
2447 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2448 | cursor = cnx.cursor() |
|
2449 | ||
2450 | cursor.execute(" SELECT name " |
|
2451 | " from tbl_spaces " |
|
2452 | " WHERE id = %s ", (id_,)) |
|
2453 | if cursor.fetchone() is None: |
|
2454 | cursor.close() |
|
2455 | cnx.close() |
|
2456 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2457 | description='API.SPACE_NOT_FOUND') |
|
2458 | ||
2459 | cursor.execute(" SELECT name " |
|
2460 | " FROM tbl_sensors " |
|
2461 | " WHERE id = %s ", (sensor_id,)) |
|
2462 | if cursor.fetchone() is None: |
|
2463 | cursor.close() |
|
2464 | cnx.close() |
|
2465 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2466 | description='API.SENSOR_NOT_FOUND') |
|
2467 | ||
2468 | query = (" SELECT id " |
|
2469 | " FROM tbl_spaces_sensors " |
|
2470 | " WHERE space_id = %s AND sensor_id = %s") |
|
2471 | cursor.execute(query, (id_, sensor_id,)) |
|
2472 | if cursor.fetchone() is not None: |
|
2473 | cursor.close() |
|
2474 | cnx.close() |
|
2475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2476 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
|
2477 | ||
2478 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
|
2479 | " VALUES (%s, %s) ") |
|
2480 | cursor.execute(add_row, (id_, sensor_id,)) |
|
2481 | cnx.commit() |
|
2482 | cursor.close() |
|
2483 | cnx.close() |
|
2484 | ||
2485 | resp.status = falcon.HTTP_201 |
|
2486 | resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id) |
|
2487 | ||
2488 | ||
2489 | class SpaceSensorItem: |
|
@@ 1997-2112 (lines=116) @@ | ||
1994 | resp.status = falcon.HTTP_204 |
|
1995 | ||
1996 | ||
1997 | class SpacePhotovoltaicPowerStationCollection: |
|
1998 | def __init__(self): |
|
1999 | """Initializes Class""" |
|
2000 | pass |
|
2001 | ||
2002 | @staticmethod |
|
2003 | def on_options(req, resp, id_): |
|
2004 | _ = req |
|
2005 | resp.status = falcon.HTTP_200 |
|
2006 | _ = id_ |
|
2007 | ||
2008 | @staticmethod |
|
2009 | def on_get(req, resp, id_): |
|
2010 | if 'API-KEY' not in req.headers or \ |
|
2011 | not isinstance(req.headers['API-KEY'], str) or \ |
|
2012 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
2013 | access_control(req) |
|
2014 | else: |
|
2015 | api_key_control(req) |
|
2016 | if not id_.isdigit() or int(id_) <= 0: |
|
2017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2018 | description='API.INVALID_SPACE_ID') |
|
2019 | ||
2020 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2021 | cursor = cnx.cursor() |
|
2022 | ||
2023 | cursor.execute(" SELECT name " |
|
2024 | " FROM tbl_spaces " |
|
2025 | " WHERE id = %s ", (id_,)) |
|
2026 | if cursor.fetchone() is None: |
|
2027 | cursor.close() |
|
2028 | cnx.close() |
|
2029 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2030 | description='API.SPACE_NOT_FOUND') |
|
2031 | ||
2032 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
2033 | " FROM tbl_spaces s, tbl_spaces_photovoltaic_power_stations se, tbl_photovoltaic_power_stations e " |
|
2034 | " WHERE se.space_id = s.id AND e.id = se.photovoltaic_power_station_id AND s.id = %s " |
|
2035 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
2036 | cursor.execute(query, (id_,)) |
|
2037 | rows = cursor.fetchall() |
|
2038 | ||
2039 | result = list() |
|
2040 | if rows is not None and len(rows) > 0: |
|
2041 | for row in rows: |
|
2042 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
2043 | result.append(meta_result) |
|
2044 | ||
2045 | resp.text = json.dumps(result) |
|
2046 | ||
2047 | @staticmethod |
|
2048 | @user_logger |
|
2049 | def on_post(req, resp, id_): |
|
2050 | """Handles POST requests""" |
|
2051 | admin_control(req) |
|
2052 | try: |
|
2053 | raw_json = req.stream.read().decode('utf-8') |
|
2054 | except Exception as ex: |
|
2055 | print(str(ex)) |
|
2056 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2057 | title='API.BAD_REQUEST', |
|
2058 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2059 | ||
2060 | if not id_.isdigit() or int(id_) <= 0: |
|
2061 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2062 | description='API.INVALID_SPACE_ID') |
|
2063 | ||
2064 | new_values = json.loads(raw_json) |
|
2065 | ||
2066 | if 'photovoltaic_power_station_id' not in new_values['data'].keys() or \ |
|
2067 | not isinstance(new_values['data']['photovoltaic_power_station_id'], int) or \ |
|
2068 | new_values['data']['photovoltaic_power_station_id'] <= 0: |
|
2069 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2070 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
2071 | photovoltaic_power_station_id = new_values['data']['photovoltaic_power_station_id'] |
|
2072 | ||
2073 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2074 | cursor = cnx.cursor() |
|
2075 | ||
2076 | cursor.execute(" SELECT name " |
|
2077 | " from tbl_spaces " |
|
2078 | " WHERE id = %s ", (id_,)) |
|
2079 | if cursor.fetchone() is None: |
|
2080 | cursor.close() |
|
2081 | cnx.close() |
|
2082 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2083 | description='API.SPACE_NOT_FOUND') |
|
2084 | ||
2085 | cursor.execute(" SELECT name " |
|
2086 | " FROM tbl_photovoltaic_power_stations " |
|
2087 | " WHERE id = %s ", (photovoltaic_power_station_id,)) |
|
2088 | if cursor.fetchone() is None: |
|
2089 | cursor.close() |
|
2090 | cnx.close() |
|
2091 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2092 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
2093 | ||
2094 | query = (" SELECT id " |
|
2095 | " FROM tbl_spaces_photovoltaic_power_stations " |
|
2096 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s") |
|
2097 | cursor.execute(query, (id_, photovoltaic_power_station_id,)) |
|
2098 | if cursor.fetchone() is not None: |
|
2099 | cursor.close() |
|
2100 | cnx.close() |
|
2101 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2102 | description='API.SPACE_PHOTOVOLTAIC_POWER_STATION_RELATION_EXISTS') |
|
2103 | ||
2104 | add_row = (" INSERT INTO tbl_spaces_photovoltaic_power_stations (space_id, photovoltaic_power_station_id) " |
|
2105 | " VALUES (%s, %s) ") |
|
2106 | cursor.execute(add_row, (id_, photovoltaic_power_station_id,)) |
|
2107 | cnx.commit() |
|
2108 | cursor.close() |
|
2109 | cnx.close() |
|
2110 | ||
2111 | resp.status = falcon.HTTP_201 |
|
2112 | resp.location = '/spaces/' + str(id_) + '/photovoltaicpowerstations/' + str(photovoltaic_power_station_id) |
|
2113 | ||
2114 | ||
2115 | class SpacePhotovoltaicPowerStationItem: |
|
@@ 1622-1737 (lines=116) @@ | ||
1619 | resp.status = falcon.HTTP_204 |
|
1620 | ||
1621 | ||
1622 | class SpaceMicrogridCollection: |
|
1623 | def __init__(self): |
|
1624 | """Initializes Class""" |
|
1625 | pass |
|
1626 | ||
1627 | @staticmethod |
|
1628 | def on_options(req, resp, id_): |
|
1629 | _ = req |
|
1630 | resp.status = falcon.HTTP_200 |
|
1631 | _ = id_ |
|
1632 | ||
1633 | @staticmethod |
|
1634 | def on_get(req, resp, id_): |
|
1635 | if 'API-KEY' not in req.headers or \ |
|
1636 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1637 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1638 | access_control(req) |
|
1639 | else: |
|
1640 | api_key_control(req) |
|
1641 | if not id_.isdigit() or int(id_) <= 0: |
|
1642 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1643 | description='API.INVALID_SPACE_ID') |
|
1644 | ||
1645 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1646 | cursor = cnx.cursor() |
|
1647 | ||
1648 | cursor.execute(" SELECT name " |
|
1649 | " FROM tbl_spaces " |
|
1650 | " WHERE id = %s ", (id_,)) |
|
1651 | if cursor.fetchone() is None: |
|
1652 | cursor.close() |
|
1653 | cnx.close() |
|
1654 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1655 | description='API.SPACE_NOT_FOUND') |
|
1656 | ||
1657 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
1658 | " FROM tbl_spaces s, tbl_spaces_microgrids se, tbl_microgrids e " |
|
1659 | " WHERE se.space_id = s.id AND e.id = se.microgrid_id AND s.id = %s " |
|
1660 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
1661 | cursor.execute(query, (id_,)) |
|
1662 | rows = cursor.fetchall() |
|
1663 | ||
1664 | result = list() |
|
1665 | if rows is not None and len(rows) > 0: |
|
1666 | for row in rows: |
|
1667 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1668 | result.append(meta_result) |
|
1669 | ||
1670 | resp.text = json.dumps(result) |
|
1671 | ||
1672 | @staticmethod |
|
1673 | @user_logger |
|
1674 | def on_post(req, resp, id_): |
|
1675 | """Handles POST requests""" |
|
1676 | admin_control(req) |
|
1677 | try: |
|
1678 | raw_json = req.stream.read().decode('utf-8') |
|
1679 | except Exception as ex: |
|
1680 | print(str(ex)) |
|
1681 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1682 | title='API.BAD_REQUEST', |
|
1683 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1684 | ||
1685 | if not id_.isdigit() or int(id_) <= 0: |
|
1686 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1687 | description='API.INVALID_SPACE_ID') |
|
1688 | ||
1689 | new_values = json.loads(raw_json) |
|
1690 | ||
1691 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
1692 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
1693 | new_values['data']['microgrid_id'] <= 0: |
|
1694 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1695 | description='API.INVALID_MICROGRID_ID') |
|
1696 | microgrid_id = new_values['data']['microgrid_id'] |
|
1697 | ||
1698 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1699 | cursor = cnx.cursor() |
|
1700 | ||
1701 | cursor.execute(" SELECT name " |
|
1702 | " from tbl_spaces " |
|
1703 | " WHERE id = %s ", (id_,)) |
|
1704 | if cursor.fetchone() is None: |
|
1705 | cursor.close() |
|
1706 | cnx.close() |
|
1707 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1708 | description='API.SPACE_NOT_FOUND') |
|
1709 | ||
1710 | cursor.execute(" SELECT name " |
|
1711 | " FROM tbl_microgrids " |
|
1712 | " WHERE id = %s ", (microgrid_id,)) |
|
1713 | if cursor.fetchone() is None: |
|
1714 | cursor.close() |
|
1715 | cnx.close() |
|
1716 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1717 | description='API.MICROGRID_NOT_FOUND') |
|
1718 | ||
1719 | query = (" SELECT id " |
|
1720 | " FROM tbl_spaces_microgrids " |
|
1721 | " WHERE space_id = %s AND microgrid_id = %s") |
|
1722 | cursor.execute(query, (id_, microgrid_id,)) |
|
1723 | if cursor.fetchone() is not None: |
|
1724 | cursor.close() |
|
1725 | cnx.close() |
|
1726 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1727 | description='API.SPACE_MICROGRID_RELATION_EXISTS') |
|
1728 | ||
1729 | add_row = (" INSERT INTO tbl_spaces_microgrids (space_id, microgrid_id) " |
|
1730 | " VALUES (%s, %s) ") |
|
1731 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
1732 | cnx.commit() |
|
1733 | cursor.close() |
|
1734 | cnx.close() |
|
1735 | ||
1736 | resp.status = falcon.HTTP_201 |
|
1737 | resp.location = '/spaces/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
1738 | ||
1739 | ||
1740 | class SpaceMicrogridItem: |
|
@@ 1247-1362 (lines=116) @@ | ||
1244 | resp.status = falcon.HTTP_204 |
|
1245 | ||
1246 | ||
1247 | class SpaceEquipmentCollection: |
|
1248 | def __init__(self): |
|
1249 | """Initializes Class""" |
|
1250 | pass |
|
1251 | ||
1252 | @staticmethod |
|
1253 | def on_options(req, resp, id_): |
|
1254 | _ = req |
|
1255 | resp.status = falcon.HTTP_200 |
|
1256 | _ = id_ |
|
1257 | ||
1258 | @staticmethod |
|
1259 | def on_get(req, resp, id_): |
|
1260 | if 'API-KEY' not in req.headers or \ |
|
1261 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1262 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1263 | access_control(req) |
|
1264 | else: |
|
1265 | api_key_control(req) |
|
1266 | if not id_.isdigit() or int(id_) <= 0: |
|
1267 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1268 | description='API.INVALID_SPACE_ID') |
|
1269 | ||
1270 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1271 | cursor = cnx.cursor() |
|
1272 | ||
1273 | cursor.execute(" SELECT name " |
|
1274 | " FROM tbl_spaces " |
|
1275 | " WHERE id = %s ", (id_,)) |
|
1276 | if cursor.fetchone() is None: |
|
1277 | cursor.close() |
|
1278 | cnx.close() |
|
1279 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1280 | description='API.SPACE_NOT_FOUND') |
|
1281 | ||
1282 | query = (" SELECT e.id, e.name, e.uuid " |
|
1283 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
|
1284 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
1285 | " ORDER BY e.id ") |
|
1286 | cursor.execute(query, (id_,)) |
|
1287 | rows = cursor.fetchall() |
|
1288 | ||
1289 | result = list() |
|
1290 | if rows is not None and len(rows) > 0: |
|
1291 | for row in rows: |
|
1292 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1293 | result.append(meta_result) |
|
1294 | ||
1295 | resp.text = json.dumps(result) |
|
1296 | ||
1297 | @staticmethod |
|
1298 | @user_logger |
|
1299 | def on_post(req, resp, id_): |
|
1300 | """Handles POST requests""" |
|
1301 | admin_control(req) |
|
1302 | try: |
|
1303 | raw_json = req.stream.read().decode('utf-8') |
|
1304 | except Exception as ex: |
|
1305 | print(str(ex)) |
|
1306 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1307 | title='API.BAD_REQUEST', |
|
1308 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1309 | ||
1310 | if not id_.isdigit() or int(id_) <= 0: |
|
1311 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1312 | description='API.INVALID_SPACE_ID') |
|
1313 | ||
1314 | new_values = json.loads(raw_json) |
|
1315 | ||
1316 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
1317 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
1318 | new_values['data']['equipment_id'] <= 0: |
|
1319 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1320 | description='API.INVALID_EQUIPMENT_ID') |
|
1321 | equipment_id = new_values['data']['equipment_id'] |
|
1322 | ||
1323 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1324 | cursor = cnx.cursor() |
|
1325 | ||
1326 | cursor.execute(" SELECT name " |
|
1327 | " from tbl_spaces " |
|
1328 | " WHERE id = %s ", (id_,)) |
|
1329 | if cursor.fetchone() is None: |
|
1330 | cursor.close() |
|
1331 | cnx.close() |
|
1332 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1333 | description='API.SPACE_NOT_FOUND') |
|
1334 | ||
1335 | cursor.execute(" SELECT name " |
|
1336 | " FROM tbl_equipments " |
|
1337 | " WHERE id = %s ", (equipment_id,)) |
|
1338 | if cursor.fetchone() is None: |
|
1339 | cursor.close() |
|
1340 | cnx.close() |
|
1341 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1342 | description='API.EQUIPMENT_NOT_FOUND') |
|
1343 | ||
1344 | query = (" SELECT id " |
|
1345 | " FROM tbl_spaces_equipments " |
|
1346 | " WHERE space_id = %s AND equipment_id = %s") |
|
1347 | cursor.execute(query, (id_, equipment_id,)) |
|
1348 | if cursor.fetchone() is not None: |
|
1349 | cursor.close() |
|
1350 | cnx.close() |
|
1351 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1352 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
|
1353 | ||
1354 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
|
1355 | " VALUES (%s, %s) ") |
|
1356 | cursor.execute(add_row, (id_, equipment_id,)) |
|
1357 | cnx.commit() |
|
1358 | cursor.close() |
|
1359 | cnx.close() |
|
1360 | ||
1361 | resp.status = falcon.HTTP_201 |
|
1362 | resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id) |
|
1363 | ||
1364 | ||
1365 | class SpaceEquipmentItem: |
|
@@ 846-961 (lines=116) @@ | ||
843 | resp.text = json.dumps(result) |
|
844 | ||
845 | ||
846 | class SpaceCombinedEquipmentCollection: |
|
847 | def __init__(self): |
|
848 | """Initializes Class""" |
|
849 | pass |
|
850 | ||
851 | @staticmethod |
|
852 | def on_options(req, resp, id_): |
|
853 | _ = req |
|
854 | resp.status = falcon.HTTP_200 |
|
855 | _ = id_ |
|
856 | ||
857 | @staticmethod |
|
858 | def on_get(req, resp, id_): |
|
859 | if 'API-KEY' not in req.headers or \ |
|
860 | not isinstance(req.headers['API-KEY'], str) or \ |
|
861 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
862 | access_control(req) |
|
863 | else: |
|
864 | api_key_control(req) |
|
865 | if not id_.isdigit() or int(id_) <= 0: |
|
866 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
867 | description='API.INVALID_SPACE_ID') |
|
868 | ||
869 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
870 | cursor = cnx.cursor() |
|
871 | ||
872 | cursor.execute(" SELECT name " |
|
873 | " FROM tbl_spaces " |
|
874 | " WHERE id = %s ", (id_,)) |
|
875 | if cursor.fetchone() is None: |
|
876 | cursor.close() |
|
877 | cnx.close() |
|
878 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
879 | description='API.SPACE_NOT_FOUND') |
|
880 | ||
881 | query = (" SELECT e.id, e.name, e.uuid " |
|
882 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
|
883 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
|
884 | " ORDER BY e.id ") |
|
885 | cursor.execute(query, (id_,)) |
|
886 | rows = cursor.fetchall() |
|
887 | ||
888 | result = list() |
|
889 | if rows is not None and len(rows) > 0: |
|
890 | for row in rows: |
|
891 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
892 | result.append(meta_result) |
|
893 | ||
894 | resp.text = json.dumps(result) |
|
895 | ||
896 | @staticmethod |
|
897 | @user_logger |
|
898 | def on_post(req, resp, id_): |
|
899 | """Handles POST requests""" |
|
900 | admin_control(req) |
|
901 | try: |
|
902 | raw_json = req.stream.read().decode('utf-8') |
|
903 | except Exception as ex: |
|
904 | print(str(ex)) |
|
905 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
906 | title='API.BAD_REQUEST', |
|
907 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
908 | ||
909 | if not id_.isdigit() or int(id_) <= 0: |
|
910 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
911 | description='API.INVALID_SPACE_ID') |
|
912 | ||
913 | new_values = json.loads(raw_json) |
|
914 | ||
915 | if 'combined_equipment_id' not in new_values['data'].keys() or \ |
|
916 | not isinstance(new_values['data']['combined_equipment_id'], int) or \ |
|
917 | new_values['data']['combined_equipment_id'] <= 0: |
|
918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
919 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
920 | combined_equipment_id = new_values['data']['combined_equipment_id'] |
|
921 | ||
922 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
923 | cursor = cnx.cursor() |
|
924 | ||
925 | cursor.execute(" SELECT name " |
|
926 | " from tbl_spaces " |
|
927 | " WHERE id = %s ", (id_,)) |
|
928 | if cursor.fetchone() is None: |
|
929 | cursor.close() |
|
930 | cnx.close() |
|
931 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
932 | description='API.SPACE_NOT_FOUND') |
|
933 | ||
934 | cursor.execute(" SELECT name " |
|
935 | " FROM tbl_combined_equipments " |
|
936 | " WHERE id = %s ", (combined_equipment_id,)) |
|
937 | if cursor.fetchone() is None: |
|
938 | cursor.close() |
|
939 | cnx.close() |
|
940 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
941 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
942 | ||
943 | query = (" SELECT id " |
|
944 | " FROM tbl_spaces_combined_equipments " |
|
945 | " WHERE space_id = %s AND combined_equipment_id = %s") |
|
946 | cursor.execute(query, (id_, combined_equipment_id,)) |
|
947 | if cursor.fetchone() is not None: |
|
948 | cursor.close() |
|
949 | cnx.close() |
|
950 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
951 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
|
952 | ||
953 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
|
954 | " VALUES (%s, %s) ") |
|
955 | cursor.execute(add_row, (id_, combined_equipment_id,)) |
|
956 | cnx.commit() |
|
957 | cursor.close() |
|
958 | cnx.close() |
|
959 | ||
960 | resp.status = falcon.HTTP_201 |
|
961 | resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id) |
|
962 | ||
963 | ||
964 | class SpaceCombinedEquipmentItem: |
@@ 1786-1901 (lines=116) @@ | ||
1783 | resp.status = falcon.HTTP_204 |
|
1784 | ||
1785 | ||
1786 | class ShopfloorCommandCollection: |
|
1787 | def __init__(self): |
|
1788 | """Initializes Class""" |
|
1789 | pass |
|
1790 | ||
1791 | @staticmethod |
|
1792 | def on_options(req, resp, id_): |
|
1793 | resp.status = falcon.HTTP_200 |
|
1794 | _ = req |
|
1795 | _ = id_ |
|
1796 | ||
1797 | @staticmethod |
|
1798 | def on_get(req, resp, id_): |
|
1799 | if 'API-KEY' not in req.headers or \ |
|
1800 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1801 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1802 | access_control(req) |
|
1803 | else: |
|
1804 | api_key_control(req) |
|
1805 | if not id_.isdigit() or int(id_) <= 0: |
|
1806 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1807 | description='API.INVALID_STORE_ID') |
|
1808 | ||
1809 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1810 | cursor = cnx.cursor() |
|
1811 | ||
1812 | cursor.execute(" SELECT name " |
|
1813 | " FROM tbl_shopfloors " |
|
1814 | " WHERE id = %s ", (id_,)) |
|
1815 | if cursor.fetchone() is None: |
|
1816 | cursor.close() |
|
1817 | cnx.close() |
|
1818 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1819 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1820 | ||
1821 | query = (" SELECT c.id, c.name, c.uuid " |
|
1822 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
|
1823 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
1824 | " ORDER BY c.id ") |
|
1825 | cursor.execute(query, (id_,)) |
|
1826 | rows = cursor.fetchall() |
|
1827 | ||
1828 | result = list() |
|
1829 | if rows is not None and len(rows) > 0: |
|
1830 | for row in rows: |
|
1831 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1832 | result.append(meta_result) |
|
1833 | ||
1834 | resp.text = json.dumps(result) |
|
1835 | ||
1836 | @staticmethod |
|
1837 | @user_logger |
|
1838 | def on_post(req, resp, id_): |
|
1839 | """Handles POST requests""" |
|
1840 | admin_control(req) |
|
1841 | try: |
|
1842 | raw_json = req.stream.read().decode('utf-8') |
|
1843 | except Exception as ex: |
|
1844 | print(ex) |
|
1845 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1846 | title='API.BAD_REQUEST', |
|
1847 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1848 | ||
1849 | if not id_.isdigit() or int(id_) <= 0: |
|
1850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1851 | description='API.INVALID_SHOPFLOOR_ID') |
|
1852 | ||
1853 | new_values = json.loads(raw_json) |
|
1854 | ||
1855 | if 'command_id' not in new_values['data'].keys() or \ |
|
1856 | not isinstance(new_values['data']['command_id'], int) or \ |
|
1857 | new_values['data']['command_id'] <= 0: |
|
1858 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1859 | description='API.INVALID_COMMAND_ID') |
|
1860 | command_id = new_values['data']['command_id'] |
|
1861 | ||
1862 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1863 | cursor = cnx.cursor() |
|
1864 | ||
1865 | cursor.execute(" SELECT name " |
|
1866 | " from tbl_shopfloors " |
|
1867 | " WHERE id = %s ", (id_,)) |
|
1868 | if cursor.fetchone() is None: |
|
1869 | cursor.close() |
|
1870 | cnx.close() |
|
1871 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1872 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1873 | ||
1874 | cursor.execute(" SELECT name " |
|
1875 | " FROM tbl_commands " |
|
1876 | " WHERE id = %s ", (command_id,)) |
|
1877 | if cursor.fetchone() is None: |
|
1878 | cursor.close() |
|
1879 | cnx.close() |
|
1880 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1881 | description='API.COMMAND_NOT_FOUND') |
|
1882 | ||
1883 | query = (" SELECT id " |
|
1884 | " FROM tbl_shopfloors_commands " |
|
1885 | " WHERE shopfloor_id = %s AND command_id = %s") |
|
1886 | cursor.execute(query, (id_, command_id,)) |
|
1887 | if cursor.fetchone() is not None: |
|
1888 | cursor.close() |
|
1889 | cnx.close() |
|
1890 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1891 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
|
1892 | ||
1893 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
|
1894 | " VALUES (%s, %s) ") |
|
1895 | cursor.execute(add_row, (id_, command_id,)) |
|
1896 | cnx.commit() |
|
1897 | cursor.close() |
|
1898 | cnx.close() |
|
1899 | ||
1900 | resp.status = falcon.HTTP_201 |
|
1901 | resp.location = '/shopfloors/' + str(id_) + '/commands/' + str(command_id) |
|
1902 | ||
1903 | ||
1904 | class ShopfloorCommandItem: |
|
@@ 1604-1719 (lines=116) @@ | ||
1601 | resp.status = falcon.HTTP_204 |
|
1602 | ||
1603 | ||
1604 | class ShopfloorWorkingCalendarCollection: |
|
1605 | def __init__(self): |
|
1606 | """Initializes ShopfloorWorkingCalendarCollection Class""" |
|
1607 | pass |
|
1608 | ||
1609 | @staticmethod |
|
1610 | def on_options(req, resp, id_): |
|
1611 | resp.status = falcon.HTTP_200 |
|
1612 | _ = req |
|
1613 | _ = id_ |
|
1614 | ||
1615 | @staticmethod |
|
1616 | def on_get(req, resp, id_): |
|
1617 | if 'API-KEY' not in req.headers or \ |
|
1618 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1619 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1620 | access_control(req) |
|
1621 | else: |
|
1622 | api_key_control(req) |
|
1623 | if not id_.isdigit() or int(id_) <= 0: |
|
1624 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1625 | description='API.INVALID_SHOPFLOOR_ID') |
|
1626 | ||
1627 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1628 | cursor = cnx.cursor() |
|
1629 | ||
1630 | cursor.execute(" SELECT name " |
|
1631 | " FROM tbl_shopfloors " |
|
1632 | " WHERE id = %s ", (id_,)) |
|
1633 | if cursor.fetchone() is None: |
|
1634 | cursor.close() |
|
1635 | cnx.close() |
|
1636 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1637 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1638 | ||
1639 | query = (" SELECT wc.id, wc.name, wc.description " |
|
1640 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
|
1641 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
1642 | " ORDER BY wc.id ") |
|
1643 | cursor.execute(query, (id_,)) |
|
1644 | rows = cursor.fetchall() |
|
1645 | ||
1646 | result = list() |
|
1647 | if rows is not None and len(rows) > 0: |
|
1648 | for row in rows: |
|
1649 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
1650 | result.append(meta_result) |
|
1651 | ||
1652 | resp.text = json.dumps(result) |
|
1653 | ||
1654 | @staticmethod |
|
1655 | @user_logger |
|
1656 | def on_post(req, resp, id_): |
|
1657 | """Handles POST requests""" |
|
1658 | admin_control(req) |
|
1659 | try: |
|
1660 | raw_json = req.stream.read().decode('utf-8') |
|
1661 | except Exception as ex: |
|
1662 | print(ex) |
|
1663 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1664 | title='API.BAD_REQUEST', |
|
1665 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1666 | ||
1667 | if not id_.isdigit() or int(id_) <= 0: |
|
1668 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1669 | description='API.INVALID_SHOPFLOOR_ID') |
|
1670 | ||
1671 | new_values = json.loads(raw_json) |
|
1672 | ||
1673 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
1674 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
1675 | new_values['data']['working_calendar_id'] <= 0: |
|
1676 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1677 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
1678 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
1679 | ||
1680 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1681 | cursor = cnx.cursor() |
|
1682 | ||
1683 | cursor.execute(" SELECT name " |
|
1684 | " from tbl_shopfloors " |
|
1685 | " WHERE id = %s ", (id_,)) |
|
1686 | if cursor.fetchone() is None: |
|
1687 | cursor.close() |
|
1688 | cnx.close() |
|
1689 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1690 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1691 | ||
1692 | cursor.execute(" SELECT name " |
|
1693 | " FROM tbl_working_calendars " |
|
1694 | " WHERE id = %s ", (working_calendar_id,)) |
|
1695 | if cursor.fetchone() is None: |
|
1696 | cursor.close() |
|
1697 | cnx.close() |
|
1698 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1699 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
1700 | ||
1701 | query = (" SELECT id " |
|
1702 | " FROM tbl_shopfloors_working_calendars " |
|
1703 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
|
1704 | cursor.execute(query, (id_, working_calendar_id,)) |
|
1705 | if cursor.fetchone() is not None: |
|
1706 | cursor.close() |
|
1707 | cnx.close() |
|
1708 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1709 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
|
1710 | ||
1711 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
|
1712 | " VALUES (%s, %s) ") |
|
1713 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
1714 | cnx.commit() |
|
1715 | cursor.close() |
|
1716 | cnx.close() |
|
1717 | ||
1718 | resp.status = falcon.HTTP_201 |
|
1719 | resp.location = '/shopfloors/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
1720 | ||
1721 | ||
1722 | class ShopfloorWorkingCalendarItem: |
|
@@ 1228-1343 (lines=116) @@ | ||
1225 | resp.status = falcon.HTTP_204 |
|
1226 | ||
1227 | ||
1228 | class ShopfloorSensorCollection: |
|
1229 | def __init__(self): |
|
1230 | """Initializes ShopfloorSensorCollection""" |
|
1231 | pass |
|
1232 | ||
1233 | @staticmethod |
|
1234 | def on_options(req, resp, id_): |
|
1235 | resp.status = falcon.HTTP_200 |
|
1236 | _ = req |
|
1237 | _ = id_ |
|
1238 | ||
1239 | @staticmethod |
|
1240 | def on_get(req, resp, id_): |
|
1241 | if 'API-KEY' not in req.headers or \ |
|
1242 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1243 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1244 | access_control(req) |
|
1245 | else: |
|
1246 | api_key_control(req) |
|
1247 | if not id_.isdigit() or int(id_) <= 0: |
|
1248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1249 | description='API.INVALID_SHOPFLOOR_ID') |
|
1250 | ||
1251 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1252 | cursor = cnx.cursor() |
|
1253 | ||
1254 | cursor.execute(" SELECT name " |
|
1255 | " FROM tbl_shopfloors " |
|
1256 | " WHERE id = %s ", (id_,)) |
|
1257 | if cursor.fetchone() is None: |
|
1258 | cursor.close() |
|
1259 | cnx.close() |
|
1260 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1261 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1262 | ||
1263 | query = (" SELECT se.id, se.name, se.uuid " |
|
1264 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
|
1265 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
1266 | " ORDER BY se.id ") |
|
1267 | cursor.execute(query, (id_,)) |
|
1268 | rows = cursor.fetchall() |
|
1269 | ||
1270 | result = list() |
|
1271 | if rows is not None and len(rows) > 0: |
|
1272 | for row in rows: |
|
1273 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1274 | result.append(meta_result) |
|
1275 | ||
1276 | resp.text = json.dumps(result) |
|
1277 | ||
1278 | @staticmethod |
|
1279 | @user_logger |
|
1280 | def on_post(req, resp, id_): |
|
1281 | """Handles POST requests""" |
|
1282 | admin_control(req) |
|
1283 | try: |
|
1284 | raw_json = req.stream.read().decode('utf-8') |
|
1285 | except Exception as ex: |
|
1286 | print(ex) |
|
1287 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1288 | title='API.BAD_REQUEST', |
|
1289 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1290 | ||
1291 | if not id_.isdigit() or int(id_) <= 0: |
|
1292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1293 | description='API.INVALID_SHOPFLOOR_ID') |
|
1294 | ||
1295 | new_values = json.loads(raw_json) |
|
1296 | ||
1297 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
1298 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
1299 | new_values['data']['sensor_id'] <= 0: |
|
1300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1301 | description='API.INVALID_SENSOR_ID') |
|
1302 | sensor_id = new_values['data']['sensor_id'] |
|
1303 | ||
1304 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1305 | cursor = cnx.cursor() |
|
1306 | ||
1307 | cursor.execute(" SELECT name " |
|
1308 | " from tbl_shopfloors " |
|
1309 | " WHERE id = %s ", (id_,)) |
|
1310 | if cursor.fetchone() is None: |
|
1311 | cursor.close() |
|
1312 | cnx.close() |
|
1313 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1314 | description='API.SHOPFLOOR_NOT_FOUND') |
|
1315 | ||
1316 | cursor.execute(" SELECT name " |
|
1317 | " FROM tbl_sensors " |
|
1318 | " WHERE id = %s ", (sensor_id,)) |
|
1319 | if cursor.fetchone() is None: |
|
1320 | cursor.close() |
|
1321 | cnx.close() |
|
1322 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1323 | description='API.SENSOR_NOT_FOUND') |
|
1324 | ||
1325 | query = (" SELECT id " |
|
1326 | " FROM tbl_shopfloors_sensors " |
|
1327 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
|
1328 | cursor.execute(query, (id_, sensor_id,)) |
|
1329 | if cursor.fetchone() is not None: |
|
1330 | cursor.close() |
|
1331 | cnx.close() |
|
1332 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1333 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
|
1334 | ||
1335 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
|
1336 | " VALUES (%s, %s) ") |
|
1337 | cursor.execute(add_row, (id_, sensor_id,)) |
|
1338 | cnx.commit() |
|
1339 | cursor.close() |
|
1340 | cnx.close() |
|
1341 | ||
1342 | resp.status = falcon.HTTP_201 |
|
1343 | resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id) |
|
1344 | ||
1345 | ||
1346 | class ShopfloorSensorItem: |
|
@@ 463-578 (lines=116) @@ | ||
460 | resp.status = falcon.HTTP_200 |
|
461 | ||
462 | ||
463 | class ShopfloorEquipmentCollection: |
|
464 | def __init__(self): |
|
465 | """Initializes ShopfloorEquipmentCollection""" |
|
466 | pass |
|
467 | ||
468 | @staticmethod |
|
469 | def on_options(req, resp, id_): |
|
470 | resp.status = falcon.HTTP_200 |
|
471 | _ = req |
|
472 | _ = id_ |
|
473 | ||
474 | @staticmethod |
|
475 | def on_get(req, resp, id_): |
|
476 | if 'API-KEY' not in req.headers or \ |
|
477 | not isinstance(req.headers['API-KEY'], str) or \ |
|
478 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
479 | access_control(req) |
|
480 | else: |
|
481 | api_key_control(req) |
|
482 | if not id_.isdigit() or int(id_) <= 0: |
|
483 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
484 | description='API.INVALID_SHOPFLOOR_ID') |
|
485 | ||
486 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
487 | cursor = cnx.cursor() |
|
488 | ||
489 | cursor.execute(" SELECT name " |
|
490 | " FROM tbl_shopfloors " |
|
491 | " WHERE id = %s ", (id_,)) |
|
492 | if cursor.fetchone() is None: |
|
493 | cursor.close() |
|
494 | cnx.close() |
|
495 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
496 | description='API.SHOPFLOOR_NOT_FOUND') |
|
497 | ||
498 | query = (" SELECT e.id, e.name, e.uuid " |
|
499 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
|
500 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
501 | " ORDER BY e.id ") |
|
502 | cursor.execute(query, (id_,)) |
|
503 | rows = cursor.fetchall() |
|
504 | ||
505 | result = list() |
|
506 | if rows is not None and len(rows) > 0: |
|
507 | for row in rows: |
|
508 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
509 | result.append(meta_result) |
|
510 | ||
511 | resp.text = json.dumps(result) |
|
512 | ||
513 | @staticmethod |
|
514 | @user_logger |
|
515 | def on_post(req, resp, id_): |
|
516 | """Handles POST requests""" |
|
517 | admin_control(req) |
|
518 | try: |
|
519 | raw_json = req.stream.read().decode('utf-8') |
|
520 | except Exception as ex: |
|
521 | print(ex) |
|
522 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
523 | title='API.BAD_REQUEST', |
|
524 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
525 | ||
526 | if not id_.isdigit() or int(id_) <= 0: |
|
527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
528 | description='API.INVALID_SHOPFLOOR_ID') |
|
529 | ||
530 | new_values = json.loads(raw_json) |
|
531 | ||
532 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
533 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
534 | new_values['data']['equipment_id'] <= 0: |
|
535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
536 | description='API.INVALID_EQUIPMENT_ID') |
|
537 | equipment_id = new_values['data']['equipment_id'] |
|
538 | ||
539 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
540 | cursor = cnx.cursor() |
|
541 | ||
542 | cursor.execute(" SELECT name " |
|
543 | " from tbl_shopfloors " |
|
544 | " WHERE id = %s ", (id_,)) |
|
545 | if cursor.fetchone() is None: |
|
546 | cursor.close() |
|
547 | cnx.close() |
|
548 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
549 | description='API.SHOPFLOOR_NOT_FOUND') |
|
550 | ||
551 | cursor.execute(" SELECT name " |
|
552 | " FROM tbl_equipments " |
|
553 | " WHERE id = %s ", (equipment_id,)) |
|
554 | if cursor.fetchone() is None: |
|
555 | cursor.close() |
|
556 | cnx.close() |
|
557 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
558 | description='API.EQUIPMENT_NOT_FOUND') |
|
559 | ||
560 | query = (" SELECT id " |
|
561 | " FROM tbl_shopfloors_equipments " |
|
562 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
|
563 | cursor.execute(query, (id_, equipment_id,)) |
|
564 | if cursor.fetchone() is not None: |
|
565 | cursor.close() |
|
566 | cnx.close() |
|
567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
568 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
|
569 | ||
570 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
|
571 | " VALUES (%s, %s) ") |
|
572 | cursor.execute(add_row, (id_, equipment_id,)) |
|
573 | cnx.commit() |
|
574 | cursor.close() |
|
575 | cnx.close() |
|
576 | ||
577 | resp.status = falcon.HTTP_201 |
|
578 | resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id) |
|
579 | ||
580 | ||
581 | class ShopfloorEquipmentItem: |
@@ 1717-1832 (lines=116) @@ | ||
1714 | resp.status = falcon.HTTP_204 |
|
1715 | ||
1716 | ||
1717 | class StoreCommandCollection: |
|
1718 | def __init__(self): |
|
1719 | """Initializes Class""" |
|
1720 | pass |
|
1721 | ||
1722 | @staticmethod |
|
1723 | def on_options(req, resp, id_): |
|
1724 | _ = req |
|
1725 | resp.status = falcon.HTTP_200 |
|
1726 | _ = id_ |
|
1727 | ||
1728 | @staticmethod |
|
1729 | def on_get(req, resp, id_): |
|
1730 | if 'API-KEY' not in req.headers or \ |
|
1731 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1732 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1733 | access_control(req) |
|
1734 | else: |
|
1735 | api_key_control(req) |
|
1736 | if not id_.isdigit() or int(id_) <= 0: |
|
1737 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1738 | description='API.INVALID_STORE_ID') |
|
1739 | ||
1740 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1741 | cursor = cnx.cursor() |
|
1742 | ||
1743 | cursor.execute(" SELECT name " |
|
1744 | " FROM tbl_stores " |
|
1745 | " WHERE id = %s ", (id_,)) |
|
1746 | if cursor.fetchone() is None: |
|
1747 | cursor.close() |
|
1748 | cnx.close() |
|
1749 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1750 | description='API.STORE_NOT_FOUND') |
|
1751 | ||
1752 | query = (" SELECT c.id, c.name, c.uuid " |
|
1753 | " FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
|
1754 | " WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
1755 | " ORDER BY c.id ") |
|
1756 | cursor.execute(query, (id_,)) |
|
1757 | rows = cursor.fetchall() |
|
1758 | ||
1759 | result = list() |
|
1760 | if rows is not None and len(rows) > 0: |
|
1761 | for row in rows: |
|
1762 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1763 | result.append(meta_result) |
|
1764 | ||
1765 | resp.text = json.dumps(result) |
|
1766 | ||
1767 | @staticmethod |
|
1768 | @user_logger |
|
1769 | def on_post(req, resp, id_): |
|
1770 | """Handles POST requests""" |
|
1771 | admin_control(req) |
|
1772 | try: |
|
1773 | raw_json = req.stream.read().decode('utf-8') |
|
1774 | except Exception as ex: |
|
1775 | print(str(ex)) |
|
1776 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1777 | title='API.BAD_REQUEST', |
|
1778 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1779 | ||
1780 | if not id_.isdigit() or int(id_) <= 0: |
|
1781 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1782 | description='API.INVALID_STORE_ID') |
|
1783 | ||
1784 | new_values = json.loads(raw_json) |
|
1785 | ||
1786 | if 'command_id' not in new_values['data'].keys() or \ |
|
1787 | not isinstance(new_values['data']['command_id'], int) or \ |
|
1788 | new_values['data']['command_id'] <= 0: |
|
1789 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1790 | description='API.INVALID_COMMAND_ID') |
|
1791 | command_id = new_values['data']['command_id'] |
|
1792 | ||
1793 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1794 | cursor = cnx.cursor() |
|
1795 | ||
1796 | cursor.execute(" SELECT name " |
|
1797 | " from tbl_stores " |
|
1798 | " WHERE id = %s ", (id_,)) |
|
1799 | if cursor.fetchone() is None: |
|
1800 | cursor.close() |
|
1801 | cnx.close() |
|
1802 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1803 | description='API.STORE_NOT_FOUND') |
|
1804 | ||
1805 | cursor.execute(" SELECT name " |
|
1806 | " FROM tbl_commands " |
|
1807 | " WHERE id = %s ", (command_id,)) |
|
1808 | if cursor.fetchone() is None: |
|
1809 | cursor.close() |
|
1810 | cnx.close() |
|
1811 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1812 | description='API.COMMAND_NOT_FOUND') |
|
1813 | ||
1814 | query = (" SELECT id " |
|
1815 | " FROM tbl_stores_commands " |
|
1816 | " WHERE store_id = %s AND command_id = %s") |
|
1817 | cursor.execute(query, (id_, command_id,)) |
|
1818 | if cursor.fetchone() is not None: |
|
1819 | cursor.close() |
|
1820 | cnx.close() |
|
1821 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1822 | description='API.STORE_COMMAND_RELATION_EXISTS') |
|
1823 | ||
1824 | add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
|
1825 | " VALUES (%s, %s) ") |
|
1826 | cursor.execute(add_row, (id_, command_id,)) |
|
1827 | cnx.commit() |
|
1828 | cursor.close() |
|
1829 | cnx.close() |
|
1830 | ||
1831 | resp.status = falcon.HTTP_201 |
|
1832 | resp.location = '/stores/' + str(id_) + '/commands/' + str(command_id) |
|
1833 | ||
1834 | ||
1835 | class StoreCommandItem: |
|
@@ 1536-1651 (lines=116) @@ | ||
1533 | resp.status = falcon.HTTP_204 |
|
1534 | ||
1535 | ||
1536 | class StoreWorkingCalendarCollection: |
|
1537 | def __init__(self): |
|
1538 | """Initializes StoreWorkingCalendarCollection Class""" |
|
1539 | pass |
|
1540 | ||
1541 | @staticmethod |
|
1542 | def on_options(req, resp, id_): |
|
1543 | _ = req |
|
1544 | resp.status = falcon.HTTP_200 |
|
1545 | _ = id_ |
|
1546 | ||
1547 | @staticmethod |
|
1548 | def on_get(req, resp, id_): |
|
1549 | if 'API-KEY' not in req.headers or \ |
|
1550 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1551 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1552 | access_control(req) |
|
1553 | else: |
|
1554 | api_key_control(req) |
|
1555 | if not id_.isdigit() or int(id_) <= 0: |
|
1556 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1557 | description='API.INVALID_STORE_ID') |
|
1558 | ||
1559 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1560 | cursor = cnx.cursor() |
|
1561 | ||
1562 | cursor.execute(" SELECT name " |
|
1563 | " FROM tbl_stores " |
|
1564 | " WHERE id = %s ", (id_,)) |
|
1565 | if cursor.fetchone() is None: |
|
1566 | cursor.close() |
|
1567 | cnx.close() |
|
1568 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1569 | description='API.STORE_NOT_FOUND') |
|
1570 | ||
1571 | query = (" SELECT wc.id, wc.name, wc.description " |
|
1572 | " FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
|
1573 | " WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
1574 | " ORDER BY wc.id ") |
|
1575 | cursor.execute(query, (id_,)) |
|
1576 | rows = cursor.fetchall() |
|
1577 | ||
1578 | result = list() |
|
1579 | if rows is not None and len(rows) > 0: |
|
1580 | for row in rows: |
|
1581 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
1582 | result.append(meta_result) |
|
1583 | ||
1584 | resp.text = json.dumps(result) |
|
1585 | ||
1586 | @staticmethod |
|
1587 | @user_logger |
|
1588 | def on_post(req, resp, id_): |
|
1589 | """Handles POST requests""" |
|
1590 | admin_control(req) |
|
1591 | try: |
|
1592 | raw_json = req.stream.read().decode('utf-8') |
|
1593 | except Exception as ex: |
|
1594 | print(str(ex)) |
|
1595 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1596 | title='API.BAD_REQUEST', |
|
1597 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1598 | ||
1599 | if not id_.isdigit() or int(id_) <= 0: |
|
1600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1601 | description='API.INVALID_STORE_ID') |
|
1602 | ||
1603 | new_values = json.loads(raw_json) |
|
1604 | ||
1605 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
1606 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
1607 | new_values['data']['working_calendar_id'] <= 0: |
|
1608 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1609 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
1610 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
1611 | ||
1612 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1613 | cursor = cnx.cursor() |
|
1614 | ||
1615 | cursor.execute(" SELECT name " |
|
1616 | " from tbl_stores " |
|
1617 | " WHERE id = %s ", (id_,)) |
|
1618 | if cursor.fetchone() is None: |
|
1619 | cursor.close() |
|
1620 | cnx.close() |
|
1621 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1622 | description='API.STORE_NOT_FOUND') |
|
1623 | ||
1624 | cursor.execute(" SELECT name " |
|
1625 | " FROM tbl_working_calendars " |
|
1626 | " WHERE id = %s ", (working_calendar_id,)) |
|
1627 | if cursor.fetchone() is None: |
|
1628 | cursor.close() |
|
1629 | cnx.close() |
|
1630 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1631 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
1632 | ||
1633 | query = (" SELECT id " |
|
1634 | " FROM tbl_stores_working_calendars " |
|
1635 | " WHERE store_id = %s AND working_calendar_id = %s") |
|
1636 | cursor.execute(query, (id_, working_calendar_id,)) |
|
1637 | if cursor.fetchone() is not None: |
|
1638 | cursor.close() |
|
1639 | cnx.close() |
|
1640 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1641 | description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
|
1642 | ||
1643 | add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
|
1644 | " VALUES (%s, %s) ") |
|
1645 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
1646 | cnx.commit() |
|
1647 | cursor.close() |
|
1648 | cnx.close() |
|
1649 | ||
1650 | resp.status = falcon.HTTP_201 |
|
1651 | resp.location = '/stores/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
1652 | ||
1653 | ||
1654 | class StoreWorkingCalendarItem: |
|
@@ 1162-1277 (lines=116) @@ | ||
1159 | resp.status = falcon.HTTP_204 |
|
1160 | ||
1161 | ||
1162 | class StoreSensorCollection: |
|
1163 | def __init__(self): |
|
1164 | """Initializes Class""" |
|
1165 | pass |
|
1166 | ||
1167 | @staticmethod |
|
1168 | def on_options(req, resp, id_): |
|
1169 | _ = req |
|
1170 | resp.status = falcon.HTTP_200 |
|
1171 | _ = id_ |
|
1172 | ||
1173 | @staticmethod |
|
1174 | def on_get(req, resp, id_): |
|
1175 | if 'API-KEY' not in req.headers or \ |
|
1176 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1177 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1178 | access_control(req) |
|
1179 | else: |
|
1180 | api_key_control(req) |
|
1181 | if not id_.isdigit() or int(id_) <= 0: |
|
1182 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1183 | description='API.INVALID_STORE_ID') |
|
1184 | ||
1185 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1186 | cursor = cnx.cursor() |
|
1187 | ||
1188 | cursor.execute(" SELECT name " |
|
1189 | " FROM tbl_stores " |
|
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.STORE_NOT_FOUND') |
|
1196 | ||
1197 | query = (" SELECT s.id, s.name, s.uuid " |
|
1198 | " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
|
1199 | " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
1200 | " ORDER BY s.id ") |
|
1201 | cursor.execute(query, (id_,)) |
|
1202 | rows = cursor.fetchall() |
|
1203 | ||
1204 | result = list() |
|
1205 | if rows is not None and len(rows) > 0: |
|
1206 | for row in rows: |
|
1207 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1208 | result.append(meta_result) |
|
1209 | ||
1210 | resp.text = json.dumps(result) |
|
1211 | ||
1212 | @staticmethod |
|
1213 | @user_logger |
|
1214 | def on_post(req, resp, id_): |
|
1215 | """Handles POST requests""" |
|
1216 | admin_control(req) |
|
1217 | try: |
|
1218 | raw_json = req.stream.read().decode('utf-8') |
|
1219 | except Exception as ex: |
|
1220 | print(str(ex)) |
|
1221 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1222 | title='API.BAD_REQUEST', |
|
1223 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1224 | ||
1225 | if not id_.isdigit() or int(id_) <= 0: |
|
1226 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1227 | description='API.INVALID_STORE_ID') |
|
1228 | ||
1229 | new_values = json.loads(raw_json) |
|
1230 | ||
1231 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
1232 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
1233 | new_values['data']['sensor_id'] <= 0: |
|
1234 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1235 | description='API.INVALID_SENSOR_ID') |
|
1236 | sensor_id = new_values['data']['sensor_id'] |
|
1237 | ||
1238 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1239 | cursor = cnx.cursor() |
|
1240 | ||
1241 | cursor.execute(" SELECT name " |
|
1242 | " from tbl_stores " |
|
1243 | " WHERE id = %s ", (id_,)) |
|
1244 | if cursor.fetchone() is None: |
|
1245 | cursor.close() |
|
1246 | cnx.close() |
|
1247 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1248 | description='API.STORE_NOT_FOUND') |
|
1249 | ||
1250 | cursor.execute(" SELECT name " |
|
1251 | " FROM tbl_sensors " |
|
1252 | " WHERE id = %s ", (sensor_id,)) |
|
1253 | if cursor.fetchone() is None: |
|
1254 | cursor.close() |
|
1255 | cnx.close() |
|
1256 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1257 | description='API.SENSOR_NOT_FOUND') |
|
1258 | ||
1259 | query = (" SELECT id " |
|
1260 | " FROM tbl_stores_sensors " |
|
1261 | " WHERE store_id = %s AND sensor_id = %s") |
|
1262 | cursor.execute(query, (id_, sensor_id,)) |
|
1263 | if cursor.fetchone() is not None: |
|
1264 | cursor.close() |
|
1265 | cnx.close() |
|
1266 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1267 | description='API.STORE_SENSOR_RELATION_EXISTS') |
|
1268 | ||
1269 | add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
|
1270 | " VALUES (%s, %s) ") |
|
1271 | cursor.execute(add_row, (id_, sensor_id,)) |
|
1272 | cnx.commit() |
|
1273 | cursor.close() |
|
1274 | cnx.close() |
|
1275 | ||
1276 | resp.status = falcon.HTTP_201 |
|
1277 | resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id) |
|
1278 | ||
1279 | ||
1280 | class StoreSensorItem: |
@@ 1256-1371 (lines=116) @@ | ||
1253 | resp.status = falcon.HTTP_204 |
|
1254 | ||
1255 | ||
1256 | class MeterCommandCollection: |
|
1257 | def __init__(self): |
|
1258 | """Initializes Class""" |
|
1259 | pass |
|
1260 | ||
1261 | @staticmethod |
|
1262 | def on_options(req, resp, id_): |
|
1263 | _ = req |
|
1264 | _ = id_ |
|
1265 | resp.status = falcon.HTTP_200 |
|
1266 | ||
1267 | @staticmethod |
|
1268 | def on_get(req, resp, id_): |
|
1269 | if 'API-KEY' not in req.headers or \ |
|
1270 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1271 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1272 | access_control(req) |
|
1273 | else: |
|
1274 | api_key_control(req) |
|
1275 | if not id_.isdigit() or int(id_) <= 0: |
|
1276 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1277 | description='API.INVALID_METER_ID') |
|
1278 | ||
1279 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1280 | cursor = cnx.cursor() |
|
1281 | ||
1282 | cursor.execute(" SELECT name " |
|
1283 | " FROM tbl_meters " |
|
1284 | " WHERE id = %s ", (id_,)) |
|
1285 | if cursor.fetchone() is None: |
|
1286 | cursor.close() |
|
1287 | cnx.close() |
|
1288 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1289 | description='API.METER_NOT_FOUND') |
|
1290 | ||
1291 | query = (" SELECT c.id, c.name, c.uuid " |
|
1292 | " FROM tbl_meters m, tbl_meters_commands mc, tbl_commands c " |
|
1293 | " WHERE mc.meter_id = m.id AND c.id = mc.command_id AND m.id = %s " |
|
1294 | " ORDER BY c.id ") |
|
1295 | cursor.execute(query, (id_,)) |
|
1296 | rows = cursor.fetchall() |
|
1297 | ||
1298 | result = list() |
|
1299 | if rows is not None and len(rows) > 0: |
|
1300 | for row in rows: |
|
1301 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
1302 | result.append(meta_result) |
|
1303 | ||
1304 | resp.text = json.dumps(result) |
|
1305 | ||
1306 | @staticmethod |
|
1307 | @user_logger |
|
1308 | def on_post(req, resp, id_): |
|
1309 | """Handles POST requests""" |
|
1310 | admin_control(req) |
|
1311 | try: |
|
1312 | raw_json = req.stream.read().decode('utf-8') |
|
1313 | except Exception as ex: |
|
1314 | print(ex) |
|
1315 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1316 | title='API.BAD_REQUEST', |
|
1317 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1318 | ||
1319 | if not id_.isdigit() or int(id_) <= 0: |
|
1320 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1321 | description='API.INVALID_METER_ID') |
|
1322 | ||
1323 | new_values = json.loads(raw_json) |
|
1324 | ||
1325 | if 'command_id' not in new_values['data'].keys() or \ |
|
1326 | not isinstance(new_values['data']['command_id'], int) or \ |
|
1327 | new_values['data']['command_id'] <= 0: |
|
1328 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1329 | description='API.INVALID_COMMAND_ID') |
|
1330 | command_id = new_values['data']['command_id'] |
|
1331 | ||
1332 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1333 | cursor = cnx.cursor() |
|
1334 | ||
1335 | cursor.execute(" SELECT name " |
|
1336 | " from tbl_meters " |
|
1337 | " WHERE id = %s ", (id_,)) |
|
1338 | if cursor.fetchone() is None: |
|
1339 | cursor.close() |
|
1340 | cnx.close() |
|
1341 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1342 | description='API.METER_NOT_FOUND') |
|
1343 | ||
1344 | cursor.execute(" SELECT name " |
|
1345 | " FROM tbl_commands " |
|
1346 | " WHERE id = %s ", (command_id,)) |
|
1347 | if cursor.fetchone() is None: |
|
1348 | cursor.close() |
|
1349 | cnx.close() |
|
1350 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1351 | description='API.COMMAND_NOT_FOUND') |
|
1352 | ||
1353 | query = (" SELECT id " |
|
1354 | " FROM tbl_meters_commands " |
|
1355 | " WHERE meter_id = %s AND command_id = %s") |
|
1356 | cursor.execute(query, (id_, command_id,)) |
|
1357 | if cursor.fetchone() is not None: |
|
1358 | cursor.close() |
|
1359 | cnx.close() |
|
1360 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1361 | description='API.METER_COMMAND_RELATION_EXISTS') |
|
1362 | ||
1363 | add_row = (" INSERT INTO tbl_meters_commands (meter_id, command_id) " |
|
1364 | " VALUES (%s, %s) ") |
|
1365 | cursor.execute(add_row, (id_, command_id,)) |
|
1366 | cnx.commit() |
|
1367 | cursor.close() |
|
1368 | cnx.close() |
|
1369 | ||
1370 | resp.status = falcon.HTTP_201 |
|
1371 | resp.location = '/meters/' + str(id_) + '/commands/' + str(command_id) |
|
1372 | ||
1373 | ||
1374 | class MeterCommandItem: |
@@ 440-555 (lines=116) @@ | ||
437 | resp.status = falcon.HTTP_200 |
|
438 | ||
439 | ||
440 | class VirtualPowerPlantMicrogridCollection: |
|
441 | def __init__(self): |
|
442 | """Initializes Class""" |
|
443 | pass |
|
444 | ||
445 | @staticmethod |
|
446 | def on_options(req, resp, id_): |
|
447 | _ = req |
|
448 | resp.status = falcon.HTTP_200 |
|
449 | _ = id_ |
|
450 | ||
451 | @staticmethod |
|
452 | def on_get(req, resp, id_): |
|
453 | if 'API-KEY' not in req.headers or \ |
|
454 | not isinstance(req.headers['API-KEY'], str) or \ |
|
455 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
456 | access_control(req) |
|
457 | else: |
|
458 | api_key_control(req) |
|
459 | if not id_.isdigit() or int(id_) <= 0: |
|
460 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
461 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
462 | ||
463 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
464 | cursor = cnx.cursor() |
|
465 | ||
466 | cursor.execute(" SELECT name " |
|
467 | " FROM tbl_virtual_power_plants " |
|
468 | " WHERE id = %s ", (id_,)) |
|
469 | if cursor.fetchone() is None: |
|
470 | cursor.close() |
|
471 | cnx.close() |
|
472 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
473 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
474 | ||
475 | query = (" SELECT m.id, m.name, m.uuid " |
|
476 | " FROM tbl_virtual_power_plants_microgrids vppm, tbl_microgrids m " |
|
477 | " WHERE m.id = vppm.microgrid_id AND vppm.virtual_power_plant_id = %s " |
|
478 | " ORDER BY m.id ") |
|
479 | cursor.execute(query, (id_,)) |
|
480 | rows = cursor.fetchall() |
|
481 | ||
482 | result = list() |
|
483 | if rows is not None and len(rows) > 0: |
|
484 | for row in rows: |
|
485 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
486 | result.append(meta_result) |
|
487 | ||
488 | resp.text = json.dumps(result) |
|
489 | ||
490 | @staticmethod |
|
491 | @user_logger |
|
492 | def on_post(req, resp, id_): |
|
493 | """Handles POST requests""" |
|
494 | admin_control(req) |
|
495 | try: |
|
496 | raw_json = req.stream.read().decode('utf-8') |
|
497 | except Exception as ex: |
|
498 | print(str(ex)) |
|
499 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
500 | title='API.BAD_REQUEST', |
|
501 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
502 | ||
503 | if not id_.isdigit() or int(id_) <= 0: |
|
504 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
505 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
506 | ||
507 | new_values = json.loads(raw_json) |
|
508 | ||
509 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
510 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
511 | new_values['data']['microgrid_id'] <= 0: |
|
512 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
513 | description='API.INVALID_MICROGRID_ID') |
|
514 | microgrid_id = new_values['data']['microgrid_id'] |
|
515 | ||
516 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
517 | cursor = cnx.cursor() |
|
518 | ||
519 | cursor.execute(" SELECT name " |
|
520 | " from tbl_virtual_power_plants " |
|
521 | " WHERE id = %s ", (id_,)) |
|
522 | if cursor.fetchone() is None: |
|
523 | cursor.close() |
|
524 | cnx.close() |
|
525 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
526 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
527 | ||
528 | cursor.execute(" SELECT name " |
|
529 | " FROM tbl_microgrids " |
|
530 | " WHERE id = %s ", (microgrid_id,)) |
|
531 | if cursor.fetchone() is None: |
|
532 | cursor.close() |
|
533 | cnx.close() |
|
534 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
535 | description='API.MICROGRID_NOT_FOUND') |
|
536 | ||
537 | query = (" SELECT id " |
|
538 | " FROM tbl_virtual_power_plants_microgrids " |
|
539 | " WHERE virtual_power_plant_id = %s AND microgrid_id = %s") |
|
540 | cursor.execute(query, (id_, microgrid_id,)) |
|
541 | if cursor.fetchone() is not None: |
|
542 | cursor.close() |
|
543 | cnx.close() |
|
544 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
545 | description='API.VIRTUAL_POWER_PLANT_MICROGRID_RELATION_EXISTS') |
|
546 | ||
547 | add_row = (" INSERT INTO tbl_virtual_power_plants_microgrids (virtual_power_plant_id, microgrid_id) " |
|
548 | " VALUES (%s, %s) ") |
|
549 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
550 | cnx.commit() |
|
551 | cursor.close() |
|
552 | cnx.close() |
|
553 | ||
554 | resp.status = falcon.HTTP_201 |
|
555 | resp.location = '/virtualpowerplants/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
556 | ||
557 | ||
558 | class VirtualPowerPlantMicrogridItem: |
@@ 5598-5707 (lines=110) @@ | ||
5595 | resp.status = falcon.HTTP_201 |
|
5596 | resp.location = '/microgrids/' + str(new_id) |
|
5597 | ||
5598 | class MicrogridDataSourceCollection: |
|
5599 | def __init__(self): |
|
5600 | pass |
|
5601 | ||
5602 | @staticmethod |
|
5603 | def on_options(req, resp, id_): |
|
5604 | _ = req |
|
5605 | _ = id_ |
|
5606 | resp.status = falcon.HTTP_200 |
|
5607 | ||
5608 | @staticmethod |
|
5609 | def on_get(req, resp, id_): |
|
5610 | if 'API-KEY' not in req.headers or \ |
|
5611 | not isinstance(req.headers['API-KEY'], str) or \ |
|
5612 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
5613 | access_control(req) |
|
5614 | else: |
|
5615 | api_key_control(req) |
|
5616 | ||
5617 | if not id_.isdigit() or int(id_) <= 0: |
|
5618 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5619 | description='API.INVALID_MICROGRID_ID') |
|
5620 | ||
5621 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5622 | cursor = cnx.cursor() |
|
5623 | ||
5624 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
|
5625 | if cursor.fetchone() is None: |
|
5626 | cursor.close() |
|
5627 | cnx.close() |
|
5628 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5629 | description='API.MICROGRID_NOT_FOUND') |
|
5630 | ||
5631 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
5632 | " FROM tbl_microgrids mg, tbl_microgrids_data_sources mgds, tbl_data_sources ds " |
|
5633 | " WHERE mgds.microgrid_id = mg.id AND ds.id = mgds.data_source_id AND mg.id = %s " |
|
5634 | " ORDER BY ds.id ") |
|
5635 | cursor.execute(query, (id_,)) |
|
5636 | rows = cursor.fetchall() |
|
5637 | ||
5638 | result = list() |
|
5639 | if rows is not None and len(rows) > 0: |
|
5640 | for row in rows: |
|
5641 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
5642 | result.append(meta_result) |
|
5643 | ||
5644 | cursor.close() |
|
5645 | cnx.close() |
|
5646 | ||
5647 | resp.text = json.dumps(result) |
|
5648 | ||
5649 | @staticmethod |
|
5650 | @user_logger |
|
5651 | def on_post(req, resp, id_): |
|
5652 | admin_control(req) |
|
5653 | try: |
|
5654 | raw_json = req.stream.read().decode('utf-8') |
|
5655 | except Exception as ex: |
|
5656 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5657 | title='API.BAD_REQUEST', |
|
5658 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5659 | ||
5660 | if not id_.isdigit() or int(id_) <= 0: |
|
5661 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5662 | description='API.INVALID_MICROGRID_ID') |
|
5663 | ||
5664 | new_values = json.loads(raw_json) |
|
5665 | ||
5666 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
5667 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
5668 | new_values['data']['data_source_id'] <= 0: |
|
5669 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5670 | description='API.INVALID_DATA_SOURCE_ID') |
|
5671 | ||
5672 | data_source_id = new_values['data']['data_source_id'] |
|
5673 | ||
5674 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5675 | cursor = cnx.cursor() |
|
5676 | ||
5677 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
|
5678 | if cursor.fetchone() is None: |
|
5679 | cursor.close() |
|
5680 | cnx.close() |
|
5681 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5682 | description='API.MICROGRID_NOT_FOUND') |
|
5683 | ||
5684 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
|
5685 | if cursor.fetchone() is None: |
|
5686 | cursor.close() |
|
5687 | cnx.close() |
|
5688 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5689 | description='API.DATA_SOURCE_NOT_FOUND') |
|
5690 | ||
5691 | cursor.execute(" SELECT id " |
|
5692 | " FROM tbl_microgrids_data_sources " |
|
5693 | " WHERE microgrid_id = %s AND data_source_id = %s", (id_, data_source_id)) |
|
5694 | if cursor.fetchone() is not None: |
|
5695 | cursor.close() |
|
5696 | cnx.close() |
|
5697 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
5698 | description='API.MICROGRID_DATA_SOURCE_RELATION_EXISTS') |
|
5699 | ||
5700 | cursor.execute(" INSERT INTO tbl_microgrids_data_sources (microgrid_id, data_source_id) " |
|
5701 | " VALUES (%s, %s) ", (id_, data_source_id)) |
|
5702 | cnx.commit() |
|
5703 | cursor.close() |
|
5704 | cnx.close() |
|
5705 | ||
5706 | resp.status = falcon.HTTP_201 |
|
5707 | resp.location = '/microgrids/' + str(id_) + '/datasources/' + str(data_source_id) |
|
5708 | ||
5709 | class MicrogridDataSourceItem: |
|
5710 | def __init__(self): |