| @@ 1914-2028 (lines=115) @@ | ||
| 1911 | resp.status = falcon.HTTP_204 |
|
| 1912 | ||
| 1913 | ||
| 1914 | class EquipmentCommandCollection: |
|
| 1915 | def __init__(self): |
|
| 1916 | """Initializes Class""" |
|
| 1917 | pass |
|
| 1918 | ||
| 1919 | @staticmethod |
|
| 1920 | def on_options(req, resp, id_): |
|
| 1921 | resp.status = falcon.HTTP_200 |
|
| 1922 | ||
| 1923 | @staticmethod |
|
| 1924 | def on_get(req, resp, id_): |
|
| 1925 | if 'API-KEY' not in req.headers or \ |
|
| 1926 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1927 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1928 | access_control(req) |
|
| 1929 | else: |
|
| 1930 | api_key_control(req) |
|
| 1931 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1932 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1933 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1934 | ||
| 1935 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1936 | cursor = cnx.cursor() |
|
| 1937 | ||
| 1938 | cursor.execute(" SELECT name " |
|
| 1939 | " FROM tbl_equipments " |
|
| 1940 | " WHERE id = %s ", (id_,)) |
|
| 1941 | if cursor.fetchone() is None: |
|
| 1942 | cursor.close() |
|
| 1943 | cnx.close() |
|
| 1944 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1945 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 1946 | ||
| 1947 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1948 | " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
|
| 1949 | " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
|
| 1950 | " ORDER BY c.id ") |
|
| 1951 | cursor.execute(query, (id_,)) |
|
| 1952 | rows = cursor.fetchall() |
|
| 1953 | ||
| 1954 | result = list() |
|
| 1955 | if rows is not None and len(rows) > 0: |
|
| 1956 | for row in rows: |
|
| 1957 | meta_result = {"id": row[0], |
|
| 1958 | "name": row[1], |
|
| 1959 | "uuid": row[2]} |
|
| 1960 | result.append(meta_result) |
|
| 1961 | ||
| 1962 | resp.text = json.dumps(result) |
|
| 1963 | ||
| 1964 | @staticmethod |
|
| 1965 | @user_logger |
|
| 1966 | def on_post(req, resp, id_): |
|
| 1967 | """Handles POST requests""" |
|
| 1968 | admin_control(req) |
|
| 1969 | try: |
|
| 1970 | raw_json = req.stream.read().decode('utf-8') |
|
| 1971 | except Exception as ex: |
|
| 1972 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1973 | title='API.BAD_REQUEST', |
|
| 1974 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1975 | ||
| 1976 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1977 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1978 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1979 | ||
| 1980 | new_values = json.loads(raw_json) |
|
| 1981 | ||
| 1982 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1983 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1984 | new_values['data']['command_id'] <= 0: |
|
| 1985 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1986 | description='API.INVALID_COMMAND_ID') |
|
| 1987 | command_id = new_values['data']['command_id'] |
|
| 1988 | ||
| 1989 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1990 | cursor = cnx.cursor() |
|
| 1991 | ||
| 1992 | cursor.execute(" SELECT name " |
|
| 1993 | " from tbl_equipments " |
|
| 1994 | " WHERE id = %s ", (id_,)) |
|
| 1995 | if cursor.fetchone() is None: |
|
| 1996 | cursor.close() |
|
| 1997 | cnx.close() |
|
| 1998 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1999 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 2000 | ||
| 2001 | cursor.execute(" SELECT name " |
|
| 2002 | " FROM tbl_commands " |
|
| 2003 | " WHERE id = %s ", (command_id,)) |
|
| 2004 | if cursor.fetchone() is None: |
|
| 2005 | cursor.close() |
|
| 2006 | cnx.close() |
|
| 2007 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2008 | description='API.COMMAND_NOT_FOUND') |
|
| 2009 | ||
| 2010 | query = (" SELECT id " |
|
| 2011 | " FROM tbl_equipments_commands " |
|
| 2012 | " WHERE equipment_id = %s AND command_id = %s") |
|
| 2013 | cursor.execute(query, (id_, command_id,)) |
|
| 2014 | if cursor.fetchone() is not None: |
|
| 2015 | cursor.close() |
|
| 2016 | cnx.close() |
|
| 2017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2018 | description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
| 2019 | ||
| 2020 | add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
|
| 2021 | " VALUES (%s, %s) ") |
|
| 2022 | cursor.execute(add_row, (id_, command_id,)) |
|
| 2023 | cnx.commit() |
|
| 2024 | cursor.close() |
|
| 2025 | cnx.close() |
|
| 2026 | ||
| 2027 | resp.status = falcon.HTTP_201 |
|
| 2028 | resp.location = '/equipments/' + str(id_) + '/commands/' + str(command_id) |
|
| 2029 | ||
| 2030 | ||
| 2031 | class EquipmentCommandItem: |
|
| @@ 1792-1906 (lines=115) @@ | ||
| 1789 | resp.status = falcon.HTTP_204 |
|
| 1790 | ||
| 1791 | ||
| 1792 | class TenantCommandCollection: |
|
| 1793 | def __init__(self): |
|
| 1794 | """Initializes Class""" |
|
| 1795 | pass |
|
| 1796 | ||
| 1797 | @staticmethod |
|
| 1798 | def on_options(req, resp, id_): |
|
| 1799 | resp.status = falcon.HTTP_200 |
|
| 1800 | ||
| 1801 | @staticmethod |
|
| 1802 | def on_get(req, resp, id_): |
|
| 1803 | if 'API-KEY' not in req.headers or \ |
|
| 1804 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1805 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1806 | access_control(req) |
|
| 1807 | else: |
|
| 1808 | api_key_control(req) |
|
| 1809 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1810 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1811 | description='API.INVALID_TENANT_ID') |
|
| 1812 | ||
| 1813 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1814 | cursor = cnx.cursor() |
|
| 1815 | ||
| 1816 | cursor.execute(" SELECT name " |
|
| 1817 | " FROM tbl_tenants " |
|
| 1818 | " WHERE id = %s ", (id_,)) |
|
| 1819 | if cursor.fetchone() is None: |
|
| 1820 | cursor.close() |
|
| 1821 | cnx.close() |
|
| 1822 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1823 | description='API.TENANT_NOT_FOUND') |
|
| 1824 | ||
| 1825 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1826 | " FROM tbl_tenants t, tbl_tenants_commands tc, tbl_commands c " |
|
| 1827 | " WHERE tc.tenant_id = t.id AND c.id = tc.command_id AND t.id = %s " |
|
| 1828 | " ORDER BY c.id ") |
|
| 1829 | cursor.execute(query, (id_,)) |
|
| 1830 | rows = cursor.fetchall() |
|
| 1831 | ||
| 1832 | result = list() |
|
| 1833 | if rows is not None and len(rows) > 0: |
|
| 1834 | for row in rows: |
|
| 1835 | meta_result = {"id": row[0], |
|
| 1836 | "name": row[1], |
|
| 1837 | "uuid": row[2]} |
|
| 1838 | result.append(meta_result) |
|
| 1839 | ||
| 1840 | resp.text = json.dumps(result) |
|
| 1841 | ||
| 1842 | @staticmethod |
|
| 1843 | @user_logger |
|
| 1844 | def on_post(req, resp, id_): |
|
| 1845 | """Handles POST requests""" |
|
| 1846 | admin_control(req) |
|
| 1847 | try: |
|
| 1848 | raw_json = req.stream.read().decode('utf-8') |
|
| 1849 | except Exception as ex: |
|
| 1850 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1851 | title='API.BAD_REQUEST', |
|
| 1852 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1853 | ||
| 1854 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1855 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1856 | description='API.INVALID_TENANT_ID') |
|
| 1857 | ||
| 1858 | new_values = json.loads(raw_json) |
|
| 1859 | ||
| 1860 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1861 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1862 | new_values['data']['command_id'] <= 0: |
|
| 1863 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1864 | description='API.INVALID_COMMAND_ID') |
|
| 1865 | command_id = new_values['data']['command_id'] |
|
| 1866 | ||
| 1867 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1868 | cursor = cnx.cursor() |
|
| 1869 | ||
| 1870 | cursor.execute(" SELECT name " |
|
| 1871 | " from tbl_tenants " |
|
| 1872 | " WHERE id = %s ", (id_,)) |
|
| 1873 | if cursor.fetchone() is None: |
|
| 1874 | cursor.close() |
|
| 1875 | cnx.close() |
|
| 1876 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1877 | description='API.TENANT_NOT_FOUND') |
|
| 1878 | ||
| 1879 | cursor.execute(" SELECT name " |
|
| 1880 | " FROM tbl_commands " |
|
| 1881 | " WHERE id = %s ", (command_id,)) |
|
| 1882 | if cursor.fetchone() is None: |
|
| 1883 | cursor.close() |
|
| 1884 | cnx.close() |
|
| 1885 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1886 | description='API.COMMAND_NOT_FOUND') |
|
| 1887 | ||
| 1888 | query = (" SELECT id " |
|
| 1889 | " FROM tbl_tenants_commands " |
|
| 1890 | " WHERE tenant_id = %s AND command_id = %s") |
|
| 1891 | cursor.execute(query, (id_, command_id,)) |
|
| 1892 | if cursor.fetchone() is not None: |
|
| 1893 | cursor.close() |
|
| 1894 | cnx.close() |
|
| 1895 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1896 | description='API.TENANT_COMMAND_RELATION_EXISTS') |
|
| 1897 | ||
| 1898 | add_row = (" INSERT INTO tbl_tenants_commands (tenant_id, command_id) " |
|
| 1899 | " VALUES (%s, %s) ") |
|
| 1900 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1901 | cnx.commit() |
|
| 1902 | cursor.close() |
|
| 1903 | cnx.close() |
|
| 1904 | ||
| 1905 | resp.status = falcon.HTTP_201 |
|
| 1906 | resp.location = '/tenants/' + str(id_) + '/commands/' + str(command_id) |
|
| 1907 | ||
| 1908 | ||
| 1909 | class TenantCommandItem: |
|
| @@ 1614-1728 (lines=115) @@ | ||
| 1611 | resp.status = falcon.HTTP_204 |
|
| 1612 | ||
| 1613 | ||
| 1614 | class TenantWorkingCalendarCollection: |
|
| 1615 | def __init__(self): |
|
| 1616 | """Initializes TenantWorkingCalendarCollection Class""" |
|
| 1617 | pass |
|
| 1618 | ||
| 1619 | @staticmethod |
|
| 1620 | def on_options(req, resp, id_): |
|
| 1621 | resp.status = falcon.HTTP_200 |
|
| 1622 | ||
| 1623 | @staticmethod |
|
| 1624 | def on_get(req, resp, id_): |
|
| 1625 | if 'API-KEY' not in req.headers or \ |
|
| 1626 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1627 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1628 | access_control(req) |
|
| 1629 | else: |
|
| 1630 | api_key_control(req) |
|
| 1631 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1633 | description='API.INVALID_TENANT_ID') |
|
| 1634 | ||
| 1635 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1636 | cursor = cnx.cursor() |
|
| 1637 | ||
| 1638 | cursor.execute(" SELECT name " |
|
| 1639 | " FROM tbl_tenants " |
|
| 1640 | " WHERE id = %s ", (id_,)) |
|
| 1641 | if cursor.fetchone() is None: |
|
| 1642 | cursor.close() |
|
| 1643 | cnx.close() |
|
| 1644 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1645 | description='API.TENANT_NOT_FOUND') |
|
| 1646 | ||
| 1647 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1648 | " FROM tbl_tenants t, tbl_tenants_working_calendars twc, tbl_working_calendars wc " |
|
| 1649 | " WHERE twc.tenant_id = t.id AND wc.id = twc.working_calendar_id AND t.id = %s " |
|
| 1650 | " ORDER BY wc.id ") |
|
| 1651 | cursor.execute(query, (id_,)) |
|
| 1652 | rows = cursor.fetchall() |
|
| 1653 | ||
| 1654 | result = list() |
|
| 1655 | if rows is not None and len(rows) > 0: |
|
| 1656 | for row in rows: |
|
| 1657 | meta_result = {"id": row[0], |
|
| 1658 | "name": row[1], |
|
| 1659 | "description": row[2]} |
|
| 1660 | result.append(meta_result) |
|
| 1661 | ||
| 1662 | resp.text = json.dumps(result) |
|
| 1663 | ||
| 1664 | @staticmethod |
|
| 1665 | @user_logger |
|
| 1666 | def on_post(req, resp, id_): |
|
| 1667 | """Handles POST requests""" |
|
| 1668 | admin_control(req) |
|
| 1669 | try: |
|
| 1670 | raw_json = req.stream.read().decode('utf-8') |
|
| 1671 | except Exception as ex: |
|
| 1672 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1673 | title='API.BAD_REQUEST', |
|
| 1674 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1675 | ||
| 1676 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1677 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1678 | description='API.INVALID_TENANT_ID') |
|
| 1679 | ||
| 1680 | new_values = json.loads(raw_json) |
|
| 1681 | ||
| 1682 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1683 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1684 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1685 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1686 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1687 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1688 | ||
| 1689 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1690 | cursor = cnx.cursor() |
|
| 1691 | ||
| 1692 | cursor.execute(" SELECT name " |
|
| 1693 | " from tbl_tenants " |
|
| 1694 | " WHERE id = %s ", (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.TENANT_NOT_FOUND') |
|
| 1700 | ||
| 1701 | cursor.execute(" SELECT name " |
|
| 1702 | " FROM tbl_working_calendars " |
|
| 1703 | " WHERE id = %s ", (working_calendar_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.WORKING_CALENDAR_NOT_FOUND') |
|
| 1709 | ||
| 1710 | query = (" SELECT id " |
|
| 1711 | " FROM tbl_tenants_working_calendars " |
|
| 1712 | " WHERE tenant_id = %s AND working_calendar_id = %s") |
|
| 1713 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1714 | if cursor.fetchone() is not None: |
|
| 1715 | cursor.close() |
|
| 1716 | cnx.close() |
|
| 1717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1718 | description='API.TENANT_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1719 | ||
| 1720 | add_row = (" INSERT INTO tbl_tenants_working_calendars (tenant_id, working_calendar_id) " |
|
| 1721 | " VALUES (%s, %s) ") |
|
| 1722 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1723 | cnx.commit() |
|
| 1724 | cursor.close() |
|
| 1725 | cnx.close() |
|
| 1726 | ||
| 1727 | resp.status = falcon.HTTP_201 |
|
| 1728 | resp.location = '/tenants/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1729 | ||
| 1730 | ||
| 1731 | class TenantWorkingCalendarItem: |
|
| @@ 1246-1360 (lines=115) @@ | ||
| 1243 | resp.status = falcon.HTTP_204 |
|
| 1244 | ||
| 1245 | ||
| 1246 | class TenantSensorCollection: |
|
| 1247 | def __init__(self): |
|
| 1248 | """Initializes Class""" |
|
| 1249 | pass |
|
| 1250 | ||
| 1251 | @staticmethod |
|
| 1252 | def on_options(req, resp, id_): |
|
| 1253 | resp.status = falcon.HTTP_200 |
|
| 1254 | ||
| 1255 | @staticmethod |
|
| 1256 | def on_get(req, resp, id_): |
|
| 1257 | if 'API-KEY' not in req.headers or \ |
|
| 1258 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1259 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1260 | access_control(req) |
|
| 1261 | else: |
|
| 1262 | api_key_control(req) |
|
| 1263 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1264 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1265 | description='API.INVALID_TENANT_ID') |
|
| 1266 | ||
| 1267 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1268 | cursor = cnx.cursor() |
|
| 1269 | ||
| 1270 | cursor.execute(" SELECT name " |
|
| 1271 | " FROM tbl_tenants " |
|
| 1272 | " WHERE id = %s ", (id_,)) |
|
| 1273 | if cursor.fetchone() is None: |
|
| 1274 | cursor.close() |
|
| 1275 | cnx.close() |
|
| 1276 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1277 | description='API.TENANT_NOT_FOUND') |
|
| 1278 | ||
| 1279 | query = (" SELECT s.id, s.name, s.uuid " |
|
| 1280 | " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s " |
|
| 1281 | " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
| 1282 | " ORDER BY s.id ") |
|
| 1283 | cursor.execute(query, (id_,)) |
|
| 1284 | rows = cursor.fetchall() |
|
| 1285 | ||
| 1286 | result = list() |
|
| 1287 | if rows is not None and len(rows) > 0: |
|
| 1288 | for row in rows: |
|
| 1289 | meta_result = {"id": row[0], |
|
| 1290 | "name": row[1], |
|
| 1291 | "uuid": row[2]} |
|
| 1292 | result.append(meta_result) |
|
| 1293 | ||
| 1294 | resp.text = json.dumps(result) |
|
| 1295 | ||
| 1296 | @staticmethod |
|
| 1297 | @user_logger |
|
| 1298 | def on_post(req, resp, id_): |
|
| 1299 | """Handles POST requests""" |
|
| 1300 | admin_control(req) |
|
| 1301 | try: |
|
| 1302 | raw_json = req.stream.read().decode('utf-8') |
|
| 1303 | except Exception as ex: |
|
| 1304 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1305 | title='API.BAD_REQUEST', |
|
| 1306 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1307 | ||
| 1308 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1309 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1310 | description='API.INVALID_TENANT_ID') |
|
| 1311 | ||
| 1312 | new_values = json.loads(raw_json) |
|
| 1313 | ||
| 1314 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1315 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1316 | new_values['data']['sensor_id'] <= 0: |
|
| 1317 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1318 | description='API.INVALID_SENSOR_ID') |
|
| 1319 | sensor_id = new_values['data']['sensor_id'] |
|
| 1320 | ||
| 1321 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1322 | cursor = cnx.cursor() |
|
| 1323 | ||
| 1324 | cursor.execute(" SELECT name " |
|
| 1325 | " from tbl_tenants " |
|
| 1326 | " WHERE id = %s ", (id_,)) |
|
| 1327 | if cursor.fetchone() is None: |
|
| 1328 | cursor.close() |
|
| 1329 | cnx.close() |
|
| 1330 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1331 | description='API.TENANT_NOT_FOUND') |
|
| 1332 | ||
| 1333 | cursor.execute(" SELECT name " |
|
| 1334 | " FROM tbl_sensors " |
|
| 1335 | " WHERE id = %s ", (sensor_id,)) |
|
| 1336 | if cursor.fetchone() is None: |
|
| 1337 | cursor.close() |
|
| 1338 | cnx.close() |
|
| 1339 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1340 | description='API.SENSOR_NOT_FOUND') |
|
| 1341 | ||
| 1342 | query = (" SELECT id " |
|
| 1343 | " FROM tbl_tenants_sensors " |
|
| 1344 | " WHERE tenant_id = %s AND sensor_id = %s") |
|
| 1345 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1346 | if cursor.fetchone() is not None: |
|
| 1347 | cursor.close() |
|
| 1348 | cnx.close() |
|
| 1349 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1350 | description='API.TENANT_SENSOR_RELATION_EXISTS') |
|
| 1351 | ||
| 1352 | add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) " |
|
| 1353 | " VALUES (%s, %s) ") |
|
| 1354 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1355 | cnx.commit() |
|
| 1356 | cursor.close() |
|
| 1357 | cnx.close() |
|
| 1358 | ||
| 1359 | resp.status = falcon.HTTP_201 |
|
| 1360 | resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1361 | ||
| 1362 | ||
| 1363 | class TenantSensorItem: |
|
| @@ 596-710 (lines=115) @@ | ||
| 593 | resp.location = '/combinedequipments/' + str(new_id) |
|
| 594 | ||
| 595 | ||
| 596 | class CombinedEquipmentEquipmentCollection: |
|
| 597 | def __init__(self): |
|
| 598 | """Initializes CombinedEquipmentEquipmentCollection""" |
|
| 599 | pass |
|
| 600 | ||
| 601 | @staticmethod |
|
| 602 | def on_options(req, resp, id_): |
|
| 603 | resp.status = falcon.HTTP_200 |
|
| 604 | ||
| 605 | @staticmethod |
|
| 606 | def on_get(req, resp, id_): |
|
| 607 | if 'API-KEY' not in req.headers or \ |
|
| 608 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 609 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 610 | access_control(req) |
|
| 611 | else: |
|
| 612 | api_key_control(req) |
|
| 613 | if not id_.isdigit() or int(id_) <= 0: |
|
| 614 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 615 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 616 | ||
| 617 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 618 | cursor = cnx.cursor() |
|
| 619 | ||
| 620 | cursor.execute(" SELECT name " |
|
| 621 | " FROM tbl_combined_equipments " |
|
| 622 | " WHERE id = %s ", (id_,)) |
|
| 623 | if cursor.fetchone() is None: |
|
| 624 | cursor.close() |
|
| 625 | cnx.close() |
|
| 626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 627 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 628 | ||
| 629 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 630 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
|
| 631 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
|
| 632 | " ORDER BY e.id ") |
|
| 633 | cursor.execute(query, (id_,)) |
|
| 634 | rows = cursor.fetchall() |
|
| 635 | cursor.close() |
|
| 636 | cnx.close() |
|
| 637 | ||
| 638 | result = list() |
|
| 639 | if rows is not None and len(rows) > 0: |
|
| 640 | for row in rows: |
|
| 641 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 642 | result.append(meta_result) |
|
| 643 | ||
| 644 | resp.text = json.dumps(result) |
|
| 645 | ||
| 646 | @staticmethod |
|
| 647 | @user_logger |
|
| 648 | def on_post(req, resp, id_): |
|
| 649 | """Handles POST requests""" |
|
| 650 | admin_control(req) |
|
| 651 | try: |
|
| 652 | raw_json = req.stream.read().decode('utf-8') |
|
| 653 | except Exception as ex: |
|
| 654 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 655 | title='API.BAD_REQUEST', |
|
| 656 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 657 | ||
| 658 | if not id_.isdigit() or int(id_) <= 0: |
|
| 659 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 660 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 661 | ||
| 662 | new_values = json.loads(raw_json) |
|
| 663 | ||
| 664 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 665 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 666 | new_values['data']['equipment_id'] <= 0: |
|
| 667 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 668 | description='API.INVALID_EQUIPMENT_ID') |
|
| 669 | equipment_id = new_values['data']['equipment_id'] |
|
| 670 | ||
| 671 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 672 | cursor = cnx.cursor() |
|
| 673 | ||
| 674 | cursor.execute(" SELECT name " |
|
| 675 | " from tbl_combined_equipments " |
|
| 676 | " WHERE id = %s ", (id_,)) |
|
| 677 | if cursor.fetchone() is None: |
|
| 678 | cursor.close() |
|
| 679 | cnx.close() |
|
| 680 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 681 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 682 | ||
| 683 | cursor.execute(" SELECT name " |
|
| 684 | " FROM tbl_equipments " |
|
| 685 | " WHERE id = %s ", (equipment_id,)) |
|
| 686 | if cursor.fetchone() is None: |
|
| 687 | cursor.close() |
|
| 688 | cnx.close() |
|
| 689 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 690 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 691 | ||
| 692 | query = (" SELECT id " |
|
| 693 | " FROM tbl_combined_equipments_equipments " |
|
| 694 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
|
| 695 | cursor.execute(query, (id_, equipment_id,)) |
|
| 696 | if cursor.fetchone() is not None: |
|
| 697 | cursor.close() |
|
| 698 | cnx.close() |
|
| 699 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 700 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
|
| 701 | ||
| 702 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
|
| 703 | " VALUES (%s, %s) ") |
|
| 704 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 705 | cnx.commit() |
|
| 706 | cursor.close() |
|
| 707 | cnx.close() |
|
| 708 | ||
| 709 | resp.status = falcon.HTTP_201 |
|
| 710 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 711 | ||
| 712 | ||
| 713 | class CombinedEquipmentEquipmentItem: |
|
| @@ 2098-2210 (lines=113) @@ | ||
| 2095 | resp.status = falcon.HTTP_204 |
|
| 2096 | ||
| 2097 | ||
| 2098 | class CombinedEquipmentCommandCollection: |
|
| 2099 | def __init__(self): |
|
| 2100 | """Initializes Class""" |
|
| 2101 | pass |
|
| 2102 | ||
| 2103 | @staticmethod |
|
| 2104 | def on_options(req, resp, id_): |
|
| 2105 | resp.status = falcon.HTTP_200 |
|
| 2106 | ||
| 2107 | @staticmethod |
|
| 2108 | def on_get(req, resp, id_): |
|
| 2109 | if 'API-KEY' not in req.headers or \ |
|
| 2110 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2111 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2112 | access_control(req) |
|
| 2113 | else: |
|
| 2114 | api_key_control(req) |
|
| 2115 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2117 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 2118 | ||
| 2119 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2120 | cursor = cnx.cursor() |
|
| 2121 | ||
| 2122 | cursor.execute(" SELECT name " |
|
| 2123 | " FROM tbl_combined_equipments " |
|
| 2124 | " WHERE id = %s ", (id_,)) |
|
| 2125 | if cursor.fetchone() is None: |
|
| 2126 | cursor.close() |
|
| 2127 | cnx.close() |
|
| 2128 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2129 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 2130 | ||
| 2131 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 2132 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
|
| 2133 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
| 2134 | " ORDER BY c.id ") |
|
| 2135 | cursor.execute(query, (id_,)) |
|
| 2136 | rows = cursor.fetchall() |
|
| 2137 | ||
| 2138 | result = list() |
|
| 2139 | if rows is not None and len(rows) > 0: |
|
| 2140 | for row in rows: |
|
| 2141 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2142 | result.append(meta_result) |
|
| 2143 | ||
| 2144 | resp.text = json.dumps(result) |
|
| 2145 | ||
| 2146 | @staticmethod |
|
| 2147 | @user_logger |
|
| 2148 | def on_post(req, resp, id_): |
|
| 2149 | """Handles POST requests""" |
|
| 2150 | admin_control(req) |
|
| 2151 | try: |
|
| 2152 | raw_json = req.stream.read().decode('utf-8') |
|
| 2153 | except Exception as ex: |
|
| 2154 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2155 | title='API.BAD_REQUEST', |
|
| 2156 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2157 | ||
| 2158 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2159 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2160 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 2161 | ||
| 2162 | new_values = json.loads(raw_json) |
|
| 2163 | ||
| 2164 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 2165 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 2166 | new_values['data']['command_id'] <= 0: |
|
| 2167 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2168 | description='API.INVALID_COMMAND_ID') |
|
| 2169 | command_id = new_values['data']['command_id'] |
|
| 2170 | ||
| 2171 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2172 | cursor = cnx.cursor() |
|
| 2173 | ||
| 2174 | cursor.execute(" SELECT name " |
|
| 2175 | " from tbl_combined_equipments " |
|
| 2176 | " WHERE id = %s ", (id_,)) |
|
| 2177 | if cursor.fetchone() is None: |
|
| 2178 | cursor.close() |
|
| 2179 | cnx.close() |
|
| 2180 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2181 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 2182 | ||
| 2183 | cursor.execute(" SELECT name " |
|
| 2184 | " FROM tbl_commands " |
|
| 2185 | " WHERE id = %s ", (command_id,)) |
|
| 2186 | if cursor.fetchone() is None: |
|
| 2187 | cursor.close() |
|
| 2188 | cnx.close() |
|
| 2189 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2190 | description='API.COMMAND_NOT_FOUND') |
|
| 2191 | ||
| 2192 | query = (" SELECT id " |
|
| 2193 | " FROM tbl_combined_equipments_commands " |
|
| 2194 | " WHERE combined_equipment_id = %s AND command_id = %s") |
|
| 2195 | cursor.execute(query, (id_, command_id,)) |
|
| 2196 | if cursor.fetchone() is not None: |
|
| 2197 | cursor.close() |
|
| 2198 | cnx.close() |
|
| 2199 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2200 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
| 2201 | ||
| 2202 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
|
| 2203 | " VALUES (%s, %s) ") |
|
| 2204 | cursor.execute(add_row, (id_, command_id,)) |
|
| 2205 | cnx.commit() |
|
| 2206 | cursor.close() |
|
| 2207 | cnx.close() |
|
| 2208 | ||
| 2209 | resp.status = falcon.HTTP_201 |
|
| 2210 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
|
| 2211 | ||
| 2212 | ||
| 2213 | class CombinedEquipmentCommandItem: |
|
| @@ 5615-5727 (lines=113) @@ | ||
| 5612 | ||
| 5613 | resp.status = falcon.HTTP_204 |
|
| 5614 | ||
| 5615 | class DistributionSystemCollection: |
|
| 5616 | def __init__(self): |
|
| 5617 | """Initializes Class""" |
|
| 5618 | pass |
|
| 5619 | ||
| 5620 | @staticmethod |
|
| 5621 | def on_options(req, resp, id_): |
|
| 5622 | resp.status = falcon.HTTP_200 |
|
| 5623 | ||
| 5624 | @staticmethod |
|
| 5625 | def on_get(req, resp, id_): |
|
| 5626 | if 'API-KEY' not in req.headers or \ |
|
| 5627 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5628 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5629 | access_control(req) |
|
| 5630 | else: |
|
| 5631 | api_key_control(req) |
|
| 5632 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5633 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5634 | description='API.INVALID_SPACE_ID') |
|
| 5635 | ||
| 5636 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5637 | cursor = cnx.cursor() |
|
| 5638 | ||
| 5639 | cursor.execute(" SELECT name " |
|
| 5640 | " FROM tbl_spaces " |
|
| 5641 | " WHERE id = %s ", (id_,)) |
|
| 5642 | if cursor.fetchone() is None: |
|
| 5643 | cursor.close() |
|
| 5644 | cnx.close() |
|
| 5645 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5646 | description='API.SPACE_NOT_FOUND') |
|
| 5647 | ||
| 5648 | query = (" SELECT d.id, d.name, d.uuid " |
|
| 5649 | " FROM tbl_spaces s, tbl_spaces_distribution_systems sd, tbl_distribution_systems d " |
|
| 5650 | " WHERE sd.space_id = s.id AND d.id = sd.distribution_system_id AND s.id = %s " |
|
| 5651 | " ORDER BY d.id ") |
|
| 5652 | cursor.execute(query, (id_,)) |
|
| 5653 | rows = cursor.fetchall() |
|
| 5654 | ||
| 5655 | result = list() |
|
| 5656 | if rows is not None and len(rows) > 0: |
|
| 5657 | for row in rows: |
|
| 5658 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5659 | result.append(meta_result) |
|
| 5660 | ||
| 5661 | resp.text = json.dumps(result) |
|
| 5662 | ||
| 5663 | @staticmethod |
|
| 5664 | @user_logger |
|
| 5665 | def on_post(req, resp, id_): |
|
| 5666 | """Handles POST requests""" |
|
| 5667 | admin_control(req) |
|
| 5668 | try: |
|
| 5669 | raw_json = req.stream.read().decode('utf-8') |
|
| 5670 | except Exception as ex: |
|
| 5671 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5672 | title='API.BAD_REQUEST', |
|
| 5673 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5674 | ||
| 5675 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5676 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5677 | description='API.INVALID_SPACE_ID') |
|
| 5678 | ||
| 5679 | new_values = json.loads(raw_json) |
|
| 5680 | ||
| 5681 | if 'distribution_system_id' not in new_values['data'].keys() or \ |
|
| 5682 | not isinstance(new_values['data']['distribution_system_id'], int) or \ |
|
| 5683 | new_values['data']['distribution_system_id'] <= 0: |
|
| 5684 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5685 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
|
| 5686 | distribution_system_id = new_values['data']['distribution_system_id'] |
|
| 5687 | ||
| 5688 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5689 | cursor = cnx.cursor() |
|
| 5690 | ||
| 5691 | cursor.execute(" SELECT name " |
|
| 5692 | " from tbl_spaces " |
|
| 5693 | " WHERE id = %s ", (id_,)) |
|
| 5694 | if cursor.fetchone() is None: |
|
| 5695 | cursor.close() |
|
| 5696 | cnx.close() |
|
| 5697 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5698 | description='API.SPACE_NOT_FOUND') |
|
| 5699 | ||
| 5700 | cursor.execute(" SELECT name " |
|
| 5701 | " FROM tbl_distribution_systems " |
|
| 5702 | " WHERE id = %s ", (distribution_system_id,)) |
|
| 5703 | if cursor.fetchone() is None: |
|
| 5704 | cursor.close() |
|
| 5705 | cnx.close() |
|
| 5706 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5707 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
|
| 5708 | ||
| 5709 | query = (" SELECT id " |
|
| 5710 | " FROM tbl_spaces_distribution_systems " |
|
| 5711 | " WHERE space_id = %s AND distribution_system_id = %s") |
|
| 5712 | cursor.execute(query, (id_, distribution_system_id,)) |
|
| 5713 | if cursor.fetchone() is not None: |
|
| 5714 | cursor.close() |
|
| 5715 | cnx.close() |
|
| 5716 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5717 | description='API.SPACE_DISTRIBUTION_SYSTEM_RELATION_EXISTS') |
|
| 5718 | ||
| 5719 | add_row = (" INSERT INTO tbl_spaces_distribution_systems (space_id, distribution_system_id) " |
|
| 5720 | " VALUES (%s, %s) ") |
|
| 5721 | cursor.execute(add_row, (id_, distribution_system_id,)) |
|
| 5722 | cnx.commit() |
|
| 5723 | cursor.close() |
|
| 5724 | cnx.close() |
|
| 5725 | ||
| 5726 | resp.status = falcon.HTTP_201 |
|
| 5727 | resp.location = '/spaces/' + str(id_) + '/distributionsystems/' + str(distribution_system_id) |
|
| 5728 | ||
| 5729 | ||
| 5730 | class DistributionSystemItem: |
|
| @@ 5440-5552 (lines=113) @@ | ||
| 5437 | resp.location = '/spaces/' + str(new_id) |
|
| 5438 | ||
| 5439 | ||
| 5440 | class SpaceEnergyFlowDiagramCollection: |
|
| 5441 | def __init__(self): |
|
| 5442 | """Initializes Class""" |
|
| 5443 | pass |
|
| 5444 | ||
| 5445 | @staticmethod |
|
| 5446 | def on_options(req, resp, id_): |
|
| 5447 | resp.status = falcon.HTTP_200 |
|
| 5448 | ||
| 5449 | @staticmethod |
|
| 5450 | def on_get(req, resp, id_): |
|
| 5451 | if 'API-KEY' not in req.headers or \ |
|
| 5452 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5453 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5454 | access_control(req) |
|
| 5455 | else: |
|
| 5456 | api_key_control(req) |
|
| 5457 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5458 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5459 | description='API.INVALID_SPACE_ID') |
|
| 5460 | ||
| 5461 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5462 | cursor = cnx.cursor() |
|
| 5463 | ||
| 5464 | cursor.execute(" SELECT name " |
|
| 5465 | " FROM tbl_spaces " |
|
| 5466 | " WHERE id = %s ", (id_,)) |
|
| 5467 | if cursor.fetchone() is None: |
|
| 5468 | cursor.close() |
|
| 5469 | cnx.close() |
|
| 5470 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5471 | description='API.SPACE_NOT_FOUND') |
|
| 5472 | ||
| 5473 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 5474 | " FROM tbl_spaces s, tbl_spaces_energy_flow_diagrams se, tbl_energy_flow_diagrams e " |
|
| 5475 | " WHERE se.space_id = s.id AND e.id = se.energy_flow_diagram_id AND s.id = %s " |
|
| 5476 | " ORDER BY e.id ") |
|
| 5477 | cursor.execute(query, (id_,)) |
|
| 5478 | rows = cursor.fetchall() |
|
| 5479 | ||
| 5480 | result = list() |
|
| 5481 | if rows is not None and len(rows) > 0: |
|
| 5482 | for row in rows: |
|
| 5483 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5484 | result.append(meta_result) |
|
| 5485 | ||
| 5486 | resp.text = json.dumps(result) |
|
| 5487 | ||
| 5488 | @staticmethod |
|
| 5489 | @user_logger |
|
| 5490 | def on_post(req, resp, id_): |
|
| 5491 | """Handles POST requests""" |
|
| 5492 | admin_control(req) |
|
| 5493 | try: |
|
| 5494 | raw_json = req.stream.read().decode('utf-8') |
|
| 5495 | except Exception as ex: |
|
| 5496 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5497 | title='API.BAD_REQUEST', |
|
| 5498 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5499 | ||
| 5500 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5501 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5502 | description='API.INVALID_SPACE_ID') |
|
| 5503 | ||
| 5504 | new_values = json.loads(raw_json) |
|
| 5505 | ||
| 5506 | if 'energy_flow_diagram_id' not in new_values['data'].keys() or \ |
|
| 5507 | not isinstance(new_values['data']['energy_flow_diagram_id'], int) or \ |
|
| 5508 | new_values['data']['energy_flow_diagram_id'] <= 0: |
|
| 5509 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5510 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
| 5511 | energy_flow_diagram_id = new_values['data']['energy_flow_diagram_id'] |
|
| 5512 | ||
| 5513 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5514 | cursor = cnx.cursor() |
|
| 5515 | ||
| 5516 | cursor.execute(" SELECT name " |
|
| 5517 | " from tbl_spaces " |
|
| 5518 | " WHERE id = %s ", (id_,)) |
|
| 5519 | if cursor.fetchone() is None: |
|
| 5520 | cursor.close() |
|
| 5521 | cnx.close() |
|
| 5522 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5523 | description='API.SPACE_NOT_FOUND') |
|
| 5524 | ||
| 5525 | cursor.execute(" SELECT name " |
|
| 5526 | " FROM tbl_energy_flow_diagrams " |
|
| 5527 | " WHERE id = %s ", (energy_flow_diagram_id,)) |
|
| 5528 | if cursor.fetchone() is None: |
|
| 5529 | cursor.close() |
|
| 5530 | cnx.close() |
|
| 5531 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5532 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
| 5533 | ||
| 5534 | query = (" SELECT id " |
|
| 5535 | " FROM tbl_spaces_energy_flow_diagrams " |
|
| 5536 | " WHERE space_id = %s AND energy_flow_diagram_id = %s") |
|
| 5537 | cursor.execute(query, (id_, energy_flow_diagram_id,)) |
|
| 5538 | if cursor.fetchone() is not None: |
|
| 5539 | cursor.close() |
|
| 5540 | cnx.close() |
|
| 5541 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5542 | description='API.SPACE_ENERGY_FLOW_DIAGRAM_RELATION_EXISTS') |
|
| 5543 | ||
| 5544 | add_row = (" INSERT INTO tbl_spaces_energy_flow_diagrams (space_id, energy_flow_diagram_id) " |
|
| 5545 | " VALUES (%s, %s) ") |
|
| 5546 | cursor.execute(add_row, (id_, energy_flow_diagram_id,)) |
|
| 5547 | cnx.commit() |
|
| 5548 | cursor.close() |
|
| 5549 | cnx.close() |
|
| 5550 | ||
| 5551 | resp.status = falcon.HTTP_201 |
|
| 5552 | resp.location = '/spaces/' + str(id_) + '/energyflowdiagrams/' + str(energy_flow_diagram_id) |
|
| 5553 | ||
| 5554 | ||
| 5555 | class SpaceEnergyFlowDiagramItem: |
|
| @@ 3711-3823 (lines=113) @@ | ||
| 3708 | resp.status = falcon.HTTP_204 |
|
| 3709 | ||
| 3710 | ||
| 3711 | class SpaceCommandCollection: |
|
| 3712 | def __init__(self): |
|
| 3713 | """Initializes Class""" |
|
| 3714 | pass |
|
| 3715 | ||
| 3716 | @staticmethod |
|
| 3717 | def on_options(req, resp, id_): |
|
| 3718 | resp.status = falcon.HTTP_200 |
|
| 3719 | ||
| 3720 | @staticmethod |
|
| 3721 | def on_get(req, resp, id_): |
|
| 3722 | if 'API-KEY' not in req.headers or \ |
|
| 3723 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3724 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3725 | access_control(req) |
|
| 3726 | else: |
|
| 3727 | api_key_control(req) |
|
| 3728 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3729 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3730 | description='API.INVALID_SPACE_ID') |
|
| 3731 | ||
| 3732 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3733 | cursor = cnx.cursor() |
|
| 3734 | ||
| 3735 | cursor.execute(" SELECT name " |
|
| 3736 | " FROM tbl_spaces " |
|
| 3737 | " WHERE id = %s ", (id_,)) |
|
| 3738 | if cursor.fetchone() is None: |
|
| 3739 | cursor.close() |
|
| 3740 | cnx.close() |
|
| 3741 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3742 | description='API.SPACE_NOT_FOUND') |
|
| 3743 | ||
| 3744 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 3745 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
|
| 3746 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 3747 | " ORDER BY c.id ") |
|
| 3748 | cursor.execute(query, (id_,)) |
|
| 3749 | rows = cursor.fetchall() |
|
| 3750 | ||
| 3751 | result = list() |
|
| 3752 | if rows is not None and len(rows) > 0: |
|
| 3753 | for row in rows: |
|
| 3754 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 3755 | result.append(meta_result) |
|
| 3756 | ||
| 3757 | resp.text = json.dumps(result) |
|
| 3758 | ||
| 3759 | @staticmethod |
|
| 3760 | @user_logger |
|
| 3761 | def on_post(req, resp, id_): |
|
| 3762 | """Handles POST requests""" |
|
| 3763 | admin_control(req) |
|
| 3764 | try: |
|
| 3765 | raw_json = req.stream.read().decode('utf-8') |
|
| 3766 | except Exception as ex: |
|
| 3767 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3768 | title='API.BAD_REQUEST', |
|
| 3769 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3770 | ||
| 3771 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3772 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3773 | description='API.INVALID_SPACE_ID') |
|
| 3774 | ||
| 3775 | new_values = json.loads(raw_json) |
|
| 3776 | ||
| 3777 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 3778 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 3779 | new_values['data']['command_id'] <= 0: |
|
| 3780 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3781 | description='API.INVALID_COMMAND_ID') |
|
| 3782 | command_id = new_values['data']['command_id'] |
|
| 3783 | ||
| 3784 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3785 | cursor = cnx.cursor() |
|
| 3786 | ||
| 3787 | cursor.execute(" SELECT name " |
|
| 3788 | " from tbl_spaces " |
|
| 3789 | " WHERE id = %s ", (id_,)) |
|
| 3790 | if cursor.fetchone() is None: |
|
| 3791 | cursor.close() |
|
| 3792 | cnx.close() |
|
| 3793 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3794 | description='API.SPACE_NOT_FOUND') |
|
| 3795 | ||
| 3796 | cursor.execute(" SELECT name " |
|
| 3797 | " FROM tbl_commands " |
|
| 3798 | " WHERE id = %s ", (command_id,)) |
|
| 3799 | if cursor.fetchone() is None: |
|
| 3800 | cursor.close() |
|
| 3801 | cnx.close() |
|
| 3802 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3803 | description='API.COMMAND_NOT_FOUND') |
|
| 3804 | ||
| 3805 | query = (" SELECT id " |
|
| 3806 | " FROM tbl_spaces_commands " |
|
| 3807 | " WHERE space_id = %s AND command_id = %s") |
|
| 3808 | cursor.execute(query, (id_, command_id,)) |
|
| 3809 | if cursor.fetchone() is not None: |
|
| 3810 | cursor.close() |
|
| 3811 | cnx.close() |
|
| 3812 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3813 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
|
| 3814 | ||
| 3815 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
|
| 3816 | " VALUES (%s, %s) ") |
|
| 3817 | cursor.execute(add_row, (id_, command_id,)) |
|
| 3818 | cnx.commit() |
|
| 3819 | cursor.close() |
|
| 3820 | cnx.close() |
|
| 3821 | ||
| 3822 | resp.status = falcon.HTTP_201 |
|
| 3823 | resp.location = '/spaces/' + str(id_) + '/commands/' + str(command_id) |
|
| 3824 | ||
| 3825 | ||
| 3826 | class SpaceCommandItem: |
|
| @@ 3535-3647 (lines=113) @@ | ||
| 3532 | resp.text = json.dumps(result) |
|
| 3533 | ||
| 3534 | ||
| 3535 | class SpaceWorkingCalendarCollection: |
|
| 3536 | def __init__(self): |
|
| 3537 | """Initializes SpaceWorkingCalendarCollection Class""" |
|
| 3538 | pass |
|
| 3539 | ||
| 3540 | @staticmethod |
|
| 3541 | def on_options(req, resp, id_): |
|
| 3542 | resp.status = falcon.HTTP_200 |
|
| 3543 | ||
| 3544 | @staticmethod |
|
| 3545 | def on_get(req, resp, id_): |
|
| 3546 | if 'API-KEY' not in req.headers or \ |
|
| 3547 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3548 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3549 | access_control(req) |
|
| 3550 | else: |
|
| 3551 | api_key_control(req) |
|
| 3552 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3553 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3554 | description='API.INVALID_SPACE_ID') |
|
| 3555 | ||
| 3556 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3557 | cursor = cnx.cursor() |
|
| 3558 | ||
| 3559 | cursor.execute(" SELECT name " |
|
| 3560 | " FROM tbl_spaces " |
|
| 3561 | " WHERE id = %s ", (id_,)) |
|
| 3562 | if cursor.fetchone() is None: |
|
| 3563 | cursor.close() |
|
| 3564 | cnx.close() |
|
| 3565 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3566 | description='API.SPACE_NOT_FOUND') |
|
| 3567 | ||
| 3568 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 3569 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
|
| 3570 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 3571 | " ORDER BY wc.id ") |
|
| 3572 | cursor.execute(query, (id_,)) |
|
| 3573 | rows = cursor.fetchall() |
|
| 3574 | ||
| 3575 | result = list() |
|
| 3576 | if rows is not None and len(rows) > 0: |
|
| 3577 | for row in rows: |
|
| 3578 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 3579 | result.append(meta_result) |
|
| 3580 | ||
| 3581 | resp.text = json.dumps(result) |
|
| 3582 | ||
| 3583 | @staticmethod |
|
| 3584 | @user_logger |
|
| 3585 | def on_post(req, resp, id_): |
|
| 3586 | """Handles POST requests""" |
|
| 3587 | admin_control(req) |
|
| 3588 | try: |
|
| 3589 | raw_json = req.stream.read().decode('utf-8') |
|
| 3590 | except Exception as ex: |
|
| 3591 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3592 | title='API.BAD_REQUEST', |
|
| 3593 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3594 | ||
| 3595 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3596 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3597 | description='API.INVALID_SPACE_ID') |
|
| 3598 | ||
| 3599 | new_values = json.loads(raw_json) |
|
| 3600 | ||
| 3601 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 3602 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 3603 | new_values['data']['working_calendar_id'] <= 0: |
|
| 3604 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3605 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 3606 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 3607 | ||
| 3608 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3609 | cursor = cnx.cursor() |
|
| 3610 | ||
| 3611 | cursor.execute(" SELECT name " |
|
| 3612 | " from tbl_spaces " |
|
| 3613 | " WHERE id = %s ", (id_,)) |
|
| 3614 | if cursor.fetchone() is None: |
|
| 3615 | cursor.close() |
|
| 3616 | cnx.close() |
|
| 3617 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3618 | description='API.SPACE_NOT_FOUND') |
|
| 3619 | ||
| 3620 | cursor.execute(" SELECT name " |
|
| 3621 | " FROM tbl_working_calendars " |
|
| 3622 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 3623 | if cursor.fetchone() is None: |
|
| 3624 | cursor.close() |
|
| 3625 | cnx.close() |
|
| 3626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3627 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 3628 | ||
| 3629 | query = (" SELECT id " |
|
| 3630 | " FROM tbl_spaces_working_calendars " |
|
| 3631 | " WHERE space_id = %s AND working_calendar_id = %s") |
|
| 3632 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 3633 | if cursor.fetchone() is not None: |
|
| 3634 | cursor.close() |
|
| 3635 | cnx.close() |
|
| 3636 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3637 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 3638 | ||
| 3639 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
|
| 3640 | " VALUES (%s, %s) ") |
|
| 3641 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 3642 | cnx.commit() |
|
| 3643 | cursor.close() |
|
| 3644 | cnx.close() |
|
| 3645 | ||
| 3646 | resp.status = falcon.HTTP_201 |
|
| 3647 | resp.location = '/spaces/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 3648 | ||
| 3649 | ||
| 3650 | class SpaceWorkingCalendarItem: |
|
| @@ 2986-3098 (lines=113) @@ | ||
| 2983 | resp.status = falcon.HTTP_204 |
|
| 2984 | ||
| 2985 | ||
| 2986 | class SpaceTenantCollection: |
|
| 2987 | def __init__(self): |
|
| 2988 | """Initializes Class""" |
|
| 2989 | pass |
|
| 2990 | ||
| 2991 | @staticmethod |
|
| 2992 | def on_options(req, resp, id_): |
|
| 2993 | resp.status = falcon.HTTP_200 |
|
| 2994 | ||
| 2995 | @staticmethod |
|
| 2996 | def on_get(req, resp, id_): |
|
| 2997 | if 'API-KEY' not in req.headers or \ |
|
| 2998 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2999 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3000 | access_control(req) |
|
| 3001 | else: |
|
| 3002 | api_key_control(req) |
|
| 3003 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3004 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3005 | description='API.INVALID_SPACE_ID') |
|
| 3006 | ||
| 3007 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3008 | cursor = cnx.cursor() |
|
| 3009 | ||
| 3010 | cursor.execute(" SELECT name " |
|
| 3011 | " FROM tbl_spaces " |
|
| 3012 | " WHERE id = %s ", (id_,)) |
|
| 3013 | if cursor.fetchone() is None: |
|
| 3014 | cursor.close() |
|
| 3015 | cnx.close() |
|
| 3016 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3017 | description='API.SPACE_NOT_FOUND') |
|
| 3018 | ||
| 3019 | query = (" SELECT t.id, t.name, t.uuid " |
|
| 3020 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
|
| 3021 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
|
| 3022 | " ORDER BY t.id ") |
|
| 3023 | cursor.execute(query, (id_,)) |
|
| 3024 | rows = cursor.fetchall() |
|
| 3025 | ||
| 3026 | result = list() |
|
| 3027 | if rows is not None and len(rows) > 0: |
|
| 3028 | for row in rows: |
|
| 3029 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 3030 | result.append(meta_result) |
|
| 3031 | ||
| 3032 | resp.text = json.dumps(result) |
|
| 3033 | ||
| 3034 | @staticmethod |
|
| 3035 | @user_logger |
|
| 3036 | def on_post(req, resp, id_): |
|
| 3037 | """Handles POST requests""" |
|
| 3038 | admin_control(req) |
|
| 3039 | try: |
|
| 3040 | raw_json = req.stream.read().decode('utf-8') |
|
| 3041 | except Exception as ex: |
|
| 3042 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3043 | title='API.BAD_REQUEST', |
|
| 3044 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3045 | ||
| 3046 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3047 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3048 | description='API.INVALID_SPACE_ID') |
|
| 3049 | ||
| 3050 | new_values = json.loads(raw_json) |
|
| 3051 | ||
| 3052 | if 'tenant_id' not in new_values['data'].keys() or \ |
|
| 3053 | not isinstance(new_values['data']['tenant_id'], int) or \ |
|
| 3054 | new_values['data']['tenant_id'] <= 0: |
|
| 3055 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3056 | description='API.INVALID_TENANT_ID') |
|
| 3057 | tenant_id = new_values['data']['tenant_id'] |
|
| 3058 | ||
| 3059 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3060 | cursor = cnx.cursor() |
|
| 3061 | ||
| 3062 | cursor.execute(" SELECT name " |
|
| 3063 | " from tbl_spaces " |
|
| 3064 | " WHERE id = %s ", (id_,)) |
|
| 3065 | if cursor.fetchone() is None: |
|
| 3066 | cursor.close() |
|
| 3067 | cnx.close() |
|
| 3068 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3069 | description='API.SPACE_NOT_FOUND') |
|
| 3070 | ||
| 3071 | cursor.execute(" SELECT name " |
|
| 3072 | " FROM tbl_tenants " |
|
| 3073 | " WHERE id = %s ", (tenant_id,)) |
|
| 3074 | if cursor.fetchone() is None: |
|
| 3075 | cursor.close() |
|
| 3076 | cnx.close() |
|
| 3077 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3078 | description='API.TENANT_NOT_FOUND') |
|
| 3079 | ||
| 3080 | query = (" SELECT id " |
|
| 3081 | " FROM tbl_spaces_tenants " |
|
| 3082 | " WHERE space_id = %s AND tenant_id = %s") |
|
| 3083 | cursor.execute(query, (id_, tenant_id,)) |
|
| 3084 | if cursor.fetchone() is not None: |
|
| 3085 | cursor.close() |
|
| 3086 | cnx.close() |
|
| 3087 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3088 | description='API.SPACE_TENANT_RELATION_EXISTS') |
|
| 3089 | ||
| 3090 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
|
| 3091 | " VALUES (%s, %s) ") |
|
| 3092 | cursor.execute(add_row, (id_, tenant_id,)) |
|
| 3093 | cnx.commit() |
|
| 3094 | cursor.close() |
|
| 3095 | cnx.close() |
|
| 3096 | ||
| 3097 | resp.status = falcon.HTTP_201 |
|
| 3098 | resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id) |
|
| 3099 | ||
| 3100 | ||
| 3101 | class SpaceTenantItem: |
|
| @@ 2811-2923 (lines=113) @@ | ||
| 2808 | resp.status = falcon.HTTP_204 |
|
| 2809 | ||
| 2810 | ||
| 2811 | class SpaceStoreCollection: |
|
| 2812 | def __init__(self): |
|
| 2813 | """Initializes Class""" |
|
| 2814 | pass |
|
| 2815 | ||
| 2816 | @staticmethod |
|
| 2817 | def on_options(req, resp, id_): |
|
| 2818 | resp.status = falcon.HTTP_200 |
|
| 2819 | ||
| 2820 | @staticmethod |
|
| 2821 | def on_get(req, resp, id_): |
|
| 2822 | if 'API-KEY' not in req.headers or \ |
|
| 2823 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2824 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2825 | access_control(req) |
|
| 2826 | else: |
|
| 2827 | api_key_control(req) |
|
| 2828 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2829 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2830 | description='API.INVALID_SPACE_ID') |
|
| 2831 | ||
| 2832 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2833 | cursor = cnx.cursor() |
|
| 2834 | ||
| 2835 | cursor.execute(" SELECT name " |
|
| 2836 | " FROM tbl_spaces " |
|
| 2837 | " WHERE id = %s ", (id_,)) |
|
| 2838 | if cursor.fetchone() is None: |
|
| 2839 | cursor.close() |
|
| 2840 | cnx.close() |
|
| 2841 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2842 | description='API.SPACE_NOT_FOUND') |
|
| 2843 | ||
| 2844 | query = (" SELECT t.id, t.name, t.uuid " |
|
| 2845 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
|
| 2846 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
|
| 2847 | " ORDER BY t.id ") |
|
| 2848 | cursor.execute(query, (id_,)) |
|
| 2849 | rows = cursor.fetchall() |
|
| 2850 | ||
| 2851 | result = list() |
|
| 2852 | if rows is not None and len(rows) > 0: |
|
| 2853 | for row in rows: |
|
| 2854 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2855 | result.append(meta_result) |
|
| 2856 | ||
| 2857 | resp.text = json.dumps(result) |
|
| 2858 | ||
| 2859 | @staticmethod |
|
| 2860 | @user_logger |
|
| 2861 | def on_post(req, resp, id_): |
|
| 2862 | """Handles POST requests""" |
|
| 2863 | admin_control(req) |
|
| 2864 | try: |
|
| 2865 | raw_json = req.stream.read().decode('utf-8') |
|
| 2866 | except Exception as ex: |
|
| 2867 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2868 | title='API.BAD_REQUEST', |
|
| 2869 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2870 | ||
| 2871 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2872 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2873 | description='API.INVALID_SPACE_ID') |
|
| 2874 | ||
| 2875 | new_values = json.loads(raw_json) |
|
| 2876 | ||
| 2877 | if 'store_id' not in new_values['data'].keys() or \ |
|
| 2878 | not isinstance(new_values['data']['store_id'], int) or \ |
|
| 2879 | new_values['data']['store_id'] <= 0: |
|
| 2880 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2881 | description='API.INVALID_STORE_ID') |
|
| 2882 | store_id = new_values['data']['store_id'] |
|
| 2883 | ||
| 2884 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2885 | cursor = cnx.cursor() |
|
| 2886 | ||
| 2887 | cursor.execute(" SELECT name " |
|
| 2888 | " from tbl_spaces " |
|
| 2889 | " WHERE id = %s ", (id_,)) |
|
| 2890 | if cursor.fetchone() is None: |
|
| 2891 | cursor.close() |
|
| 2892 | cnx.close() |
|
| 2893 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2894 | description='API.SPACE_NOT_FOUND') |
|
| 2895 | ||
| 2896 | cursor.execute(" SELECT name " |
|
| 2897 | " FROM tbl_stores " |
|
| 2898 | " WHERE id = %s ", (store_id,)) |
|
| 2899 | if cursor.fetchone() is None: |
|
| 2900 | cursor.close() |
|
| 2901 | cnx.close() |
|
| 2902 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2903 | description='API.STORE_NOT_FOUND') |
|
| 2904 | ||
| 2905 | query = (" SELECT id " |
|
| 2906 | " FROM tbl_spaces_stores " |
|
| 2907 | " WHERE space_id = %s AND store_id = %s") |
|
| 2908 | cursor.execute(query, (id_, store_id,)) |
|
| 2909 | if cursor.fetchone() is not None: |
|
| 2910 | cursor.close() |
|
| 2911 | cnx.close() |
|
| 2912 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2913 | description='API.SPACE_STORE_RELATION_EXISTS') |
|
| 2914 | ||
| 2915 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
|
| 2916 | " VALUES (%s, %s) ") |
|
| 2917 | cursor.execute(add_row, (id_, store_id,)) |
|
| 2918 | cnx.commit() |
|
| 2919 | cursor.close() |
|
| 2920 | cnx.close() |
|
| 2921 | ||
| 2922 | resp.status = falcon.HTTP_201 |
|
| 2923 | resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id) |
|
| 2924 | ||
| 2925 | ||
| 2926 | class SpaceStoreItem: |
|
| @@ 2636-2748 (lines=113) @@ | ||
| 2633 | resp.status = falcon.HTTP_204 |
|
| 2634 | ||
| 2635 | ||
| 2636 | class SpaceShopfloorCollection: |
|
| 2637 | def __init__(self): |
|
| 2638 | """Initializes Class""" |
|
| 2639 | pass |
|
| 2640 | ||
| 2641 | @staticmethod |
|
| 2642 | def on_options(req, resp, id_): |
|
| 2643 | resp.status = falcon.HTTP_200 |
|
| 2644 | ||
| 2645 | @staticmethod |
|
| 2646 | def on_get(req, resp, id_): |
|
| 2647 | if 'API-KEY' not in req.headers or \ |
|
| 2648 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2649 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2650 | access_control(req) |
|
| 2651 | else: |
|
| 2652 | api_key_control(req) |
|
| 2653 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2654 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2655 | description='API.INVALID_SPACE_ID') |
|
| 2656 | ||
| 2657 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2658 | cursor = cnx.cursor() |
|
| 2659 | ||
| 2660 | cursor.execute(" SELECT name " |
|
| 2661 | " FROM tbl_spaces " |
|
| 2662 | " WHERE id = %s ", (id_,)) |
|
| 2663 | if cursor.fetchone() is None: |
|
| 2664 | cursor.close() |
|
| 2665 | cnx.close() |
|
| 2666 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2667 | description='API.SPACE_NOT_FOUND') |
|
| 2668 | ||
| 2669 | query = (" SELECT sf.id, sf.name, sf.uuid " |
|
| 2670 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
|
| 2671 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
|
| 2672 | " ORDER BY sf.id ") |
|
| 2673 | cursor.execute(query, (id_,)) |
|
| 2674 | rows = cursor.fetchall() |
|
| 2675 | ||
| 2676 | result = list() |
|
| 2677 | if rows is not None and len(rows) > 0: |
|
| 2678 | for row in rows: |
|
| 2679 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2680 | result.append(meta_result) |
|
| 2681 | ||
| 2682 | resp.text = json.dumps(result) |
|
| 2683 | ||
| 2684 | @staticmethod |
|
| 2685 | @user_logger |
|
| 2686 | def on_post(req, resp, id_): |
|
| 2687 | """Handles POST requests""" |
|
| 2688 | admin_control(req) |
|
| 2689 | try: |
|
| 2690 | raw_json = req.stream.read().decode('utf-8') |
|
| 2691 | except Exception as ex: |
|
| 2692 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2693 | title='API.BAD_REQUEST', |
|
| 2694 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2695 | ||
| 2696 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2697 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2698 | description='API.INVALID_SPACE_ID') |
|
| 2699 | ||
| 2700 | new_values = json.loads(raw_json) |
|
| 2701 | ||
| 2702 | if 'shopfloor_id' not in new_values['data'].keys() or \ |
|
| 2703 | not isinstance(new_values['data']['shopfloor_id'], int) or \ |
|
| 2704 | new_values['data']['shopfloor_id'] <= 0: |
|
| 2705 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2706 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 2707 | shopfloor_id = new_values['data']['shopfloor_id'] |
|
| 2708 | ||
| 2709 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2710 | cursor = cnx.cursor() |
|
| 2711 | ||
| 2712 | cursor.execute(" SELECT name " |
|
| 2713 | " from tbl_spaces " |
|
| 2714 | " WHERE id = %s ", (id_,)) |
|
| 2715 | if cursor.fetchone() is None: |
|
| 2716 | cursor.close() |
|
| 2717 | cnx.close() |
|
| 2718 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2719 | description='API.SPACE_NOT_FOUND') |
|
| 2720 | ||
| 2721 | cursor.execute(" SELECT name " |
|
| 2722 | " FROM tbl_shopfloors " |
|
| 2723 | " WHERE id = %s ", (shopfloor_id,)) |
|
| 2724 | if cursor.fetchone() is None: |
|
| 2725 | cursor.close() |
|
| 2726 | cnx.close() |
|
| 2727 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2728 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 2729 | ||
| 2730 | query = (" SELECT id " |
|
| 2731 | " FROM tbl_spaces_shopfloors " |
|
| 2732 | " WHERE space_id = %s AND shopfloor_id = %s") |
|
| 2733 | cursor.execute(query, (id_, shopfloor_id,)) |
|
| 2734 | if cursor.fetchone() is not None: |
|
| 2735 | cursor.close() |
|
| 2736 | cnx.close() |
|
| 2737 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2738 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
|
| 2739 | ||
| 2740 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
|
| 2741 | " VALUES (%s, %s) ") |
|
| 2742 | cursor.execute(add_row, (id_, shopfloor_id,)) |
|
| 2743 | cnx.commit() |
|
| 2744 | cursor.close() |
|
| 2745 | cnx.close() |
|
| 2746 | ||
| 2747 | resp.status = falcon.HTTP_201 |
|
| 2748 | resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id) |
|
| 2749 | ||
| 2750 | ||
| 2751 | class SpaceShopfloorItem: |
|
| @@ 2461-2573 (lines=113) @@ | ||
| 2458 | resp.status = falcon.HTTP_204 |
|
| 2459 | ||
| 2460 | ||
| 2461 | class SpaceSensorCollection: |
|
| 2462 | def __init__(self): |
|
| 2463 | """Initializes Class""" |
|
| 2464 | pass |
|
| 2465 | ||
| 2466 | @staticmethod |
|
| 2467 | def on_options(req, resp, id_): |
|
| 2468 | resp.status = falcon.HTTP_200 |
|
| 2469 | ||
| 2470 | @staticmethod |
|
| 2471 | def on_get(req, resp, id_): |
|
| 2472 | if 'API-KEY' not in req.headers or \ |
|
| 2473 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2474 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2475 | access_control(req) |
|
| 2476 | else: |
|
| 2477 | api_key_control(req) |
|
| 2478 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2479 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2480 | description='API.INVALID_SPACE_ID') |
|
| 2481 | ||
| 2482 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2483 | cursor = cnx.cursor() |
|
| 2484 | ||
| 2485 | cursor.execute(" SELECT name " |
|
| 2486 | " FROM tbl_spaces " |
|
| 2487 | " WHERE id = %s ", (id_,)) |
|
| 2488 | if cursor.fetchone() is None: |
|
| 2489 | cursor.close() |
|
| 2490 | cnx.close() |
|
| 2491 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2492 | description='API.SPACE_NOT_FOUND') |
|
| 2493 | ||
| 2494 | query = (" SELECT se.id, se.name, se.uuid " |
|
| 2495 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
|
| 2496 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
| 2497 | " ORDER BY se.id ") |
|
| 2498 | cursor.execute(query, (id_,)) |
|
| 2499 | rows = cursor.fetchall() |
|
| 2500 | ||
| 2501 | result = list() |
|
| 2502 | if rows is not None and len(rows) > 0: |
|
| 2503 | for row in rows: |
|
| 2504 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2505 | result.append(meta_result) |
|
| 2506 | ||
| 2507 | resp.text = json.dumps(result) |
|
| 2508 | ||
| 2509 | @staticmethod |
|
| 2510 | @user_logger |
|
| 2511 | def on_post(req, resp, id_): |
|
| 2512 | """Handles POST requests""" |
|
| 2513 | admin_control(req) |
|
| 2514 | try: |
|
| 2515 | raw_json = req.stream.read().decode('utf-8') |
|
| 2516 | except Exception as ex: |
|
| 2517 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2518 | title='API.BAD_REQUEST', |
|
| 2519 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2520 | ||
| 2521 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2522 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2523 | description='API.INVALID_SPACE_ID') |
|
| 2524 | ||
| 2525 | new_values = json.loads(raw_json) |
|
| 2526 | ||
| 2527 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 2528 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 2529 | new_values['data']['sensor_id'] <= 0: |
|
| 2530 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2531 | description='API.INVALID_SENSOR_ID') |
|
| 2532 | sensor_id = new_values['data']['sensor_id'] |
|
| 2533 | ||
| 2534 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2535 | cursor = cnx.cursor() |
|
| 2536 | ||
| 2537 | cursor.execute(" SELECT name " |
|
| 2538 | " from tbl_spaces " |
|
| 2539 | " WHERE id = %s ", (id_,)) |
|
| 2540 | if cursor.fetchone() is None: |
|
| 2541 | cursor.close() |
|
| 2542 | cnx.close() |
|
| 2543 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2544 | description='API.SPACE_NOT_FOUND') |
|
| 2545 | ||
| 2546 | cursor.execute(" SELECT name " |
|
| 2547 | " FROM tbl_sensors " |
|
| 2548 | " WHERE id = %s ", (sensor_id,)) |
|
| 2549 | if cursor.fetchone() is None: |
|
| 2550 | cursor.close() |
|
| 2551 | cnx.close() |
|
| 2552 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2553 | description='API.SENSOR_NOT_FOUND') |
|
| 2554 | ||
| 2555 | query = (" SELECT id " |
|
| 2556 | " FROM tbl_spaces_sensors " |
|
| 2557 | " WHERE space_id = %s AND sensor_id = %s") |
|
| 2558 | cursor.execute(query, (id_, sensor_id,)) |
|
| 2559 | if cursor.fetchone() is not None: |
|
| 2560 | cursor.close() |
|
| 2561 | cnx.close() |
|
| 2562 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2563 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
|
| 2564 | ||
| 2565 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
|
| 2566 | " VALUES (%s, %s) ") |
|
| 2567 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 2568 | cnx.commit() |
|
| 2569 | cursor.close() |
|
| 2570 | cnx.close() |
|
| 2571 | ||
| 2572 | resp.status = falcon.HTTP_201 |
|
| 2573 | resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 2574 | ||
| 2575 | ||
| 2576 | class SpaceSensorItem: |
|
| @@ 2097-2209 (lines=113) @@ | ||
| 2094 | resp.status = falcon.HTTP_204 |
|
| 2095 | ||
| 2096 | ||
| 2097 | class SpacePhotovoltaicPowerStationCollection: |
|
| 2098 | def __init__(self): |
|
| 2099 | """Initializes Class""" |
|
| 2100 | pass |
|
| 2101 | ||
| 2102 | @staticmethod |
|
| 2103 | def on_options(req, resp, id_): |
|
| 2104 | resp.status = falcon.HTTP_200 |
|
| 2105 | ||
| 2106 | @staticmethod |
|
| 2107 | def on_get(req, resp, id_): |
|
| 2108 | if 'API-KEY' not in req.headers or \ |
|
| 2109 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2110 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2111 | access_control(req) |
|
| 2112 | else: |
|
| 2113 | api_key_control(req) |
|
| 2114 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2115 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2116 | description='API.INVALID_SPACE_ID') |
|
| 2117 | ||
| 2118 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2119 | cursor = cnx.cursor() |
|
| 2120 | ||
| 2121 | cursor.execute(" SELECT name " |
|
| 2122 | " FROM tbl_spaces " |
|
| 2123 | " WHERE id = %s ", (id_,)) |
|
| 2124 | if cursor.fetchone() is None: |
|
| 2125 | cursor.close() |
|
| 2126 | cnx.close() |
|
| 2127 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2128 | description='API.SPACE_NOT_FOUND') |
|
| 2129 | ||
| 2130 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 2131 | " FROM tbl_spaces s, tbl_spaces_photovoltaic_power_stations se, tbl_photovoltaic_power_stations e " |
|
| 2132 | " WHERE se.space_id = s.id AND e.id = se.photovoltaic_power_station_id AND s.id = %s " |
|
| 2133 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 2134 | cursor.execute(query, (id_,)) |
|
| 2135 | rows = cursor.fetchall() |
|
| 2136 | ||
| 2137 | result = list() |
|
| 2138 | if rows is not None and len(rows) > 0: |
|
| 2139 | for row in rows: |
|
| 2140 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2141 | result.append(meta_result) |
|
| 2142 | ||
| 2143 | resp.text = json.dumps(result) |
|
| 2144 | ||
| 2145 | @staticmethod |
|
| 2146 | @user_logger |
|
| 2147 | def on_post(req, resp, id_): |
|
| 2148 | """Handles POST requests""" |
|
| 2149 | admin_control(req) |
|
| 2150 | try: |
|
| 2151 | raw_json = req.stream.read().decode('utf-8') |
|
| 2152 | except Exception as ex: |
|
| 2153 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2154 | title='API.BAD_REQUEST', |
|
| 2155 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2156 | ||
| 2157 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2158 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2159 | description='API.INVALID_SPACE_ID') |
|
| 2160 | ||
| 2161 | new_values = json.loads(raw_json) |
|
| 2162 | ||
| 2163 | if 'photovoltaic_power_station_id' not in new_values['data'].keys() or \ |
|
| 2164 | not isinstance(new_values['data']['photovoltaic_power_station_id'], int) or \ |
|
| 2165 | new_values['data']['photovoltaic_power_station_id'] <= 0: |
|
| 2166 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2167 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 2168 | photovoltaic_power_station_id = new_values['data']['photovoltaic_power_station_id'] |
|
| 2169 | ||
| 2170 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2171 | cursor = cnx.cursor() |
|
| 2172 | ||
| 2173 | cursor.execute(" SELECT name " |
|
| 2174 | " from tbl_spaces " |
|
| 2175 | " WHERE id = %s ", (id_,)) |
|
| 2176 | if cursor.fetchone() is None: |
|
| 2177 | cursor.close() |
|
| 2178 | cnx.close() |
|
| 2179 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2180 | description='API.SPACE_NOT_FOUND') |
|
| 2181 | ||
| 2182 | cursor.execute(" SELECT name " |
|
| 2183 | " FROM tbl_photovoltaic_power_stations " |
|
| 2184 | " WHERE id = %s ", (photovoltaic_power_station_id,)) |
|
| 2185 | if cursor.fetchone() is None: |
|
| 2186 | cursor.close() |
|
| 2187 | cnx.close() |
|
| 2188 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2189 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 2190 | ||
| 2191 | query = (" SELECT id " |
|
| 2192 | " FROM tbl_spaces_photovoltaic_power_stations " |
|
| 2193 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s") |
|
| 2194 | cursor.execute(query, (id_, photovoltaic_power_station_id,)) |
|
| 2195 | if cursor.fetchone() is not None: |
|
| 2196 | cursor.close() |
|
| 2197 | cnx.close() |
|
| 2198 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2199 | description='API.SPACE_PHOTOVOLTAIC_POWER_STATION_RELATION_EXISTS') |
|
| 2200 | ||
| 2201 | add_row = (" INSERT INTO tbl_spaces_photovoltaic_power_stations (space_id, photovoltaic_power_station_id) " |
|
| 2202 | " VALUES (%s, %s) ") |
|
| 2203 | cursor.execute(add_row, (id_, photovoltaic_power_station_id,)) |
|
| 2204 | cnx.commit() |
|
| 2205 | cursor.close() |
|
| 2206 | cnx.close() |
|
| 2207 | ||
| 2208 | resp.status = falcon.HTTP_201 |
|
| 2209 | resp.location = '/spaces/' + str(id_) + '/photovoltaicpowerstations/' + str(photovoltaic_power_station_id) |
|
| 2210 | ||
| 2211 | ||
| 2212 | class SpacePhotovoltaicPowerStationItem: |
|
| @@ 1732-1844 (lines=113) @@ | ||
| 1729 | resp.status = falcon.HTTP_204 |
|
| 1730 | ||
| 1731 | ||
| 1732 | class SpaceMicrogridCollection: |
|
| 1733 | def __init__(self): |
|
| 1734 | """Initializes Class""" |
|
| 1735 | pass |
|
| 1736 | ||
| 1737 | @staticmethod |
|
| 1738 | def on_options(req, resp, id_): |
|
| 1739 | resp.status = falcon.HTTP_200 |
|
| 1740 | ||
| 1741 | @staticmethod |
|
| 1742 | def on_get(req, resp, id_): |
|
| 1743 | if 'API-KEY' not in req.headers or \ |
|
| 1744 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1745 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1746 | access_control(req) |
|
| 1747 | else: |
|
| 1748 | api_key_control(req) |
|
| 1749 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1750 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1751 | description='API.INVALID_SPACE_ID') |
|
| 1752 | ||
| 1753 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1754 | cursor = cnx.cursor() |
|
| 1755 | ||
| 1756 | cursor.execute(" SELECT name " |
|
| 1757 | " FROM tbl_spaces " |
|
| 1758 | " WHERE id = %s ", (id_,)) |
|
| 1759 | if cursor.fetchone() is None: |
|
| 1760 | cursor.close() |
|
| 1761 | cnx.close() |
|
| 1762 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1763 | description='API.SPACE_NOT_FOUND') |
|
| 1764 | ||
| 1765 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 1766 | " FROM tbl_spaces s, tbl_spaces_microgrids se, tbl_microgrids e " |
|
| 1767 | " WHERE se.space_id = s.id AND e.id = se.microgrid_id AND s.id = %s " |
|
| 1768 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 1769 | cursor.execute(query, (id_,)) |
|
| 1770 | rows = cursor.fetchall() |
|
| 1771 | ||
| 1772 | result = list() |
|
| 1773 | if rows is not None and len(rows) > 0: |
|
| 1774 | for row in rows: |
|
| 1775 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1776 | result.append(meta_result) |
|
| 1777 | ||
| 1778 | resp.text = json.dumps(result) |
|
| 1779 | ||
| 1780 | @staticmethod |
|
| 1781 | @user_logger |
|
| 1782 | def on_post(req, resp, id_): |
|
| 1783 | """Handles POST requests""" |
|
| 1784 | admin_control(req) |
|
| 1785 | try: |
|
| 1786 | raw_json = req.stream.read().decode('utf-8') |
|
| 1787 | except Exception as ex: |
|
| 1788 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1789 | title='API.BAD_REQUEST', |
|
| 1790 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1791 | ||
| 1792 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1793 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1794 | description='API.INVALID_SPACE_ID') |
|
| 1795 | ||
| 1796 | new_values = json.loads(raw_json) |
|
| 1797 | ||
| 1798 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
| 1799 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
| 1800 | new_values['data']['microgrid_id'] <= 0: |
|
| 1801 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1802 | description='API.INVALID_MICROGRID_ID') |
|
| 1803 | microgrid_id = new_values['data']['microgrid_id'] |
|
| 1804 | ||
| 1805 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1806 | cursor = cnx.cursor() |
|
| 1807 | ||
| 1808 | cursor.execute(" SELECT name " |
|
| 1809 | " from tbl_spaces " |
|
| 1810 | " WHERE id = %s ", (id_,)) |
|
| 1811 | if cursor.fetchone() is None: |
|
| 1812 | cursor.close() |
|
| 1813 | cnx.close() |
|
| 1814 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1815 | description='API.SPACE_NOT_FOUND') |
|
| 1816 | ||
| 1817 | cursor.execute(" SELECT name " |
|
| 1818 | " FROM tbl_microgrids " |
|
| 1819 | " WHERE id = %s ", (microgrid_id,)) |
|
| 1820 | if cursor.fetchone() is None: |
|
| 1821 | cursor.close() |
|
| 1822 | cnx.close() |
|
| 1823 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1824 | description='API.MICROGRID_NOT_FOUND') |
|
| 1825 | ||
| 1826 | query = (" SELECT id " |
|
| 1827 | " FROM tbl_spaces_microgrids " |
|
| 1828 | " WHERE space_id = %s AND microgrid_id = %s") |
|
| 1829 | cursor.execute(query, (id_, microgrid_id,)) |
|
| 1830 | if cursor.fetchone() is not None: |
|
| 1831 | cursor.close() |
|
| 1832 | cnx.close() |
|
| 1833 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1834 | description='API.SPACE_MICROGRID_RELATION_EXISTS') |
|
| 1835 | ||
| 1836 | add_row = (" INSERT INTO tbl_spaces_microgrids (space_id, microgrid_id) " |
|
| 1837 | " VALUES (%s, %s) ") |
|
| 1838 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
| 1839 | cnx.commit() |
|
| 1840 | cursor.close() |
|
| 1841 | cnx.close() |
|
| 1842 | ||
| 1843 | resp.status = falcon.HTTP_201 |
|
| 1844 | resp.location = '/spaces/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
| 1845 | ||
| 1846 | ||
| 1847 | class SpaceMicrogridItem: |
|
| @@ 1366-1478 (lines=113) @@ | ||
| 1363 | resp.status = falcon.HTTP_204 |
|
| 1364 | ||
| 1365 | ||
| 1366 | class SpaceHybridPowerStationCollection: |
|
| 1367 | def __init__(self): |
|
| 1368 | """Initializes Class""" |
|
| 1369 | pass |
|
| 1370 | ||
| 1371 | @staticmethod |
|
| 1372 | def on_options(req, resp, id_): |
|
| 1373 | resp.status = falcon.HTTP_200 |
|
| 1374 | ||
| 1375 | @staticmethod |
|
| 1376 | def on_get(req, resp, id_): |
|
| 1377 | if 'API-KEY' not in req.headers or \ |
|
| 1378 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1379 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1380 | access_control(req) |
|
| 1381 | else: |
|
| 1382 | api_key_control(req) |
|
| 1383 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1384 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1385 | description='API.INVALID_SPACE_ID') |
|
| 1386 | ||
| 1387 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1388 | cursor = cnx.cursor() |
|
| 1389 | ||
| 1390 | cursor.execute(" SELECT name " |
|
| 1391 | " FROM tbl_spaces " |
|
| 1392 | " WHERE id = %s ", (id_,)) |
|
| 1393 | if cursor.fetchone() is None: |
|
| 1394 | cursor.close() |
|
| 1395 | cnx.close() |
|
| 1396 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1397 | description='API.SPACE_NOT_FOUND') |
|
| 1398 | ||
| 1399 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 1400 | " FROM tbl_spaces s, tbl_spaces_hybrid_power_stations se, tbl_hybrid_power_stations e " |
|
| 1401 | " WHERE se.space_id = s.id AND e.id = se.hybrid_power_station_id AND s.id = %s " |
|
| 1402 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 1403 | cursor.execute(query, (id_,)) |
|
| 1404 | rows = cursor.fetchall() |
|
| 1405 | ||
| 1406 | result = list() |
|
| 1407 | if rows is not None and len(rows) > 0: |
|
| 1408 | for row in rows: |
|
| 1409 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1410 | result.append(meta_result) |
|
| 1411 | ||
| 1412 | resp.text = json.dumps(result) |
|
| 1413 | ||
| 1414 | @staticmethod |
|
| 1415 | @user_logger |
|
| 1416 | def on_post(req, resp, id_): |
|
| 1417 | """Handles POST requests""" |
|
| 1418 | admin_control(req) |
|
| 1419 | try: |
|
| 1420 | raw_json = req.stream.read().decode('utf-8') |
|
| 1421 | except Exception as ex: |
|
| 1422 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1423 | title='API.BAD_REQUEST', |
|
| 1424 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1425 | ||
| 1426 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1427 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1428 | description='API.INVALID_SPACE_ID') |
|
| 1429 | ||
| 1430 | new_values = json.loads(raw_json) |
|
| 1431 | ||
| 1432 | if 'hybrid_power_station_id' not in new_values['data'].keys() or \ |
|
| 1433 | not isinstance(new_values['data']['hybrid_power_station_id'], int) or \ |
|
| 1434 | new_values['data']['hybrid_power_station_id'] <= 0: |
|
| 1435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1436 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 1437 | hybrid_power_station_id = new_values['data']['hybrid_power_station_id'] |
|
| 1438 | ||
| 1439 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1440 | cursor = cnx.cursor() |
|
| 1441 | ||
| 1442 | cursor.execute(" SELECT name " |
|
| 1443 | " from tbl_spaces " |
|
| 1444 | " WHERE id = %s ", (id_,)) |
|
| 1445 | if cursor.fetchone() is None: |
|
| 1446 | cursor.close() |
|
| 1447 | cnx.close() |
|
| 1448 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1449 | description='API.SPACE_NOT_FOUND') |
|
| 1450 | ||
| 1451 | cursor.execute(" SELECT name " |
|
| 1452 | " FROM tbl_hybrid_power_stations " |
|
| 1453 | " WHERE id = %s ", (hybrid_power_station_id,)) |
|
| 1454 | if cursor.fetchone() is None: |
|
| 1455 | cursor.close() |
|
| 1456 | cnx.close() |
|
| 1457 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1458 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 1459 | ||
| 1460 | query = (" SELECT id " |
|
| 1461 | " FROM tbl_spaces_hybrid_power_stations " |
|
| 1462 | " WHERE space_id = %s AND hybrid_power_station_id = %s") |
|
| 1463 | cursor.execute(query, (id_, hybrid_power_station_id,)) |
|
| 1464 | if cursor.fetchone() is not None: |
|
| 1465 | cursor.close() |
|
| 1466 | cnx.close() |
|
| 1467 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1468 | description='API.SPACE_HYBRID_POWER_STATION_RELATION_EXISTS') |
|
| 1469 | ||
| 1470 | add_row = (" INSERT INTO tbl_spaces_hybrid_power_stations (space_id, hybrid_power_station_id) " |
|
| 1471 | " VALUES (%s, %s) ") |
|
| 1472 | cursor.execute(add_row, (id_, hybrid_power_station_id,)) |
|
| 1473 | cnx.commit() |
|
| 1474 | cursor.close() |
|
| 1475 | cnx.close() |
|
| 1476 | ||
| 1477 | resp.status = falcon.HTTP_201 |
|
| 1478 | resp.location = '/spaces/' + str(id_) + '/energystoragepowerstations/' + str(hybrid_power_station_id) |
|
| 1479 | ||
| 1480 | ||
| 1481 | class SpaceHybridPowerStationItem: |
|
| @@ 1191-1303 (lines=113) @@ | ||
| 1188 | resp.status = falcon.HTTP_204 |
|
| 1189 | ||
| 1190 | ||
| 1191 | class SpaceEquipmentCollection: |
|
| 1192 | def __init__(self): |
|
| 1193 | """Initializes Class""" |
|
| 1194 | pass |
|
| 1195 | ||
| 1196 | @staticmethod |
|
| 1197 | def on_options(req, resp, id_): |
|
| 1198 | resp.status = falcon.HTTP_200 |
|
| 1199 | ||
| 1200 | @staticmethod |
|
| 1201 | def on_get(req, resp, id_): |
|
| 1202 | if 'API-KEY' not in req.headers or \ |
|
| 1203 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1204 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1205 | access_control(req) |
|
| 1206 | else: |
|
| 1207 | api_key_control(req) |
|
| 1208 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1209 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1210 | description='API.INVALID_SPACE_ID') |
|
| 1211 | ||
| 1212 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1213 | cursor = cnx.cursor() |
|
| 1214 | ||
| 1215 | cursor.execute(" SELECT name " |
|
| 1216 | " FROM tbl_spaces " |
|
| 1217 | " WHERE id = %s ", (id_,)) |
|
| 1218 | if cursor.fetchone() is None: |
|
| 1219 | cursor.close() |
|
| 1220 | cnx.close() |
|
| 1221 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1222 | description='API.SPACE_NOT_FOUND') |
|
| 1223 | ||
| 1224 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 1225 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
|
| 1226 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
| 1227 | " ORDER BY e.id ") |
|
| 1228 | cursor.execute(query, (id_,)) |
|
| 1229 | rows = cursor.fetchall() |
|
| 1230 | ||
| 1231 | result = list() |
|
| 1232 | if rows is not None and len(rows) > 0: |
|
| 1233 | for row in rows: |
|
| 1234 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1235 | result.append(meta_result) |
|
| 1236 | ||
| 1237 | resp.text = json.dumps(result) |
|
| 1238 | ||
| 1239 | @staticmethod |
|
| 1240 | @user_logger |
|
| 1241 | def on_post(req, resp, id_): |
|
| 1242 | """Handles POST requests""" |
|
| 1243 | admin_control(req) |
|
| 1244 | try: |
|
| 1245 | raw_json = req.stream.read().decode('utf-8') |
|
| 1246 | except Exception as ex: |
|
| 1247 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1248 | title='API.BAD_REQUEST', |
|
| 1249 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1250 | ||
| 1251 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1252 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1253 | description='API.INVALID_SPACE_ID') |
|
| 1254 | ||
| 1255 | new_values = json.loads(raw_json) |
|
| 1256 | ||
| 1257 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 1258 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 1259 | new_values['data']['equipment_id'] <= 0: |
|
| 1260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1261 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1262 | equipment_id = new_values['data']['equipment_id'] |
|
| 1263 | ||
| 1264 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1265 | cursor = cnx.cursor() |
|
| 1266 | ||
| 1267 | cursor.execute(" SELECT name " |
|
| 1268 | " from tbl_spaces " |
|
| 1269 | " WHERE id = %s ", (id_,)) |
|
| 1270 | if cursor.fetchone() is None: |
|
| 1271 | cursor.close() |
|
| 1272 | cnx.close() |
|
| 1273 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1274 | description='API.SPACE_NOT_FOUND') |
|
| 1275 | ||
| 1276 | cursor.execute(" SELECT name " |
|
| 1277 | " FROM tbl_equipments " |
|
| 1278 | " WHERE id = %s ", (equipment_id,)) |
|
| 1279 | if cursor.fetchone() is None: |
|
| 1280 | cursor.close() |
|
| 1281 | cnx.close() |
|
| 1282 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1283 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 1284 | ||
| 1285 | query = (" SELECT id " |
|
| 1286 | " FROM tbl_spaces_equipments " |
|
| 1287 | " WHERE space_id = %s AND equipment_id = %s") |
|
| 1288 | cursor.execute(query, (id_, equipment_id,)) |
|
| 1289 | if cursor.fetchone() is not None: |
|
| 1290 | cursor.close() |
|
| 1291 | cnx.close() |
|
| 1292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1293 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
|
| 1294 | ||
| 1295 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
|
| 1296 | " VALUES (%s, %s) ") |
|
| 1297 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 1298 | cnx.commit() |
|
| 1299 | cursor.close() |
|
| 1300 | cnx.close() |
|
| 1301 | ||
| 1302 | resp.status = falcon.HTTP_201 |
|
| 1303 | resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 1304 | ||
| 1305 | ||
| 1306 | class SpaceEquipmentItem: |
|
| @@ 1015-1127 (lines=113) @@ | ||
| 1012 | resp.status = falcon.HTTP_204 |
|
| 1013 | ||
| 1014 | ||
| 1015 | class SpaceEnergyStoragePowerStationCollection: |
|
| 1016 | def __init__(self): |
|
| 1017 | """Initializes Class""" |
|
| 1018 | pass |
|
| 1019 | ||
| 1020 | @staticmethod |
|
| 1021 | def on_options(req, resp, id_): |
|
| 1022 | resp.status = falcon.HTTP_200 |
|
| 1023 | ||
| 1024 | @staticmethod |
|
| 1025 | def on_get(req, resp, id_): |
|
| 1026 | if 'API-KEY' not in req.headers or \ |
|
| 1027 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1028 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1029 | access_control(req) |
|
| 1030 | else: |
|
| 1031 | api_key_control(req) |
|
| 1032 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1033 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1034 | description='API.INVALID_SPACE_ID') |
|
| 1035 | ||
| 1036 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1037 | cursor = cnx.cursor() |
|
| 1038 | ||
| 1039 | cursor.execute(" SELECT name " |
|
| 1040 | " FROM tbl_spaces " |
|
| 1041 | " WHERE id = %s ", (id_,)) |
|
| 1042 | if cursor.fetchone() is None: |
|
| 1043 | cursor.close() |
|
| 1044 | cnx.close() |
|
| 1045 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1046 | description='API.SPACE_NOT_FOUND') |
|
| 1047 | ||
| 1048 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 1049 | " FROM tbl_spaces s, tbl_spaces_energy_storage_power_stations se, tbl_energy_storage_power_stations e " |
|
| 1050 | " WHERE se.space_id = s.id AND e.id = se.energy_storage_power_station_id AND s.id = %s " |
|
| 1051 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 1052 | cursor.execute(query, (id_,)) |
|
| 1053 | rows = cursor.fetchall() |
|
| 1054 | ||
| 1055 | result = list() |
|
| 1056 | if rows is not None and len(rows) > 0: |
|
| 1057 | for row in rows: |
|
| 1058 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1059 | result.append(meta_result) |
|
| 1060 | ||
| 1061 | resp.text = json.dumps(result) |
|
| 1062 | ||
| 1063 | @staticmethod |
|
| 1064 | @user_logger |
|
| 1065 | def on_post(req, resp, id_): |
|
| 1066 | """Handles POST requests""" |
|
| 1067 | admin_control(req) |
|
| 1068 | try: |
|
| 1069 | raw_json = req.stream.read().decode('utf-8') |
|
| 1070 | except Exception as ex: |
|
| 1071 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1072 | title='API.BAD_REQUEST', |
|
| 1073 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1074 | ||
| 1075 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1076 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1077 | description='API.INVALID_SPACE_ID') |
|
| 1078 | ||
| 1079 | new_values = json.loads(raw_json) |
|
| 1080 | ||
| 1081 | if 'energy_storage_power_station_id' not in new_values['data'].keys() or \ |
|
| 1082 | not isinstance(new_values['data']['energy_storage_power_station_id'], int) or \ |
|
| 1083 | new_values['data']['energy_storage_power_station_id'] <= 0: |
|
| 1084 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1085 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 1086 | energy_storage_power_station_id = new_values['data']['energy_storage_power_station_id'] |
|
| 1087 | ||
| 1088 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1089 | cursor = cnx.cursor() |
|
| 1090 | ||
| 1091 | cursor.execute(" SELECT name " |
|
| 1092 | " from tbl_spaces " |
|
| 1093 | " WHERE id = %s ", (id_,)) |
|
| 1094 | if cursor.fetchone() is None: |
|
| 1095 | cursor.close() |
|
| 1096 | cnx.close() |
|
| 1097 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1098 | description='API.SPACE_NOT_FOUND') |
|
| 1099 | ||
| 1100 | cursor.execute(" SELECT name " |
|
| 1101 | " FROM tbl_energy_storage_power_stations " |
|
| 1102 | " WHERE id = %s ", (energy_storage_power_station_id,)) |
|
| 1103 | if cursor.fetchone() is None: |
|
| 1104 | cursor.close() |
|
| 1105 | cnx.close() |
|
| 1106 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1107 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 1108 | ||
| 1109 | query = (" SELECT id " |
|
| 1110 | " FROM tbl_spaces_energy_storage_power_stations " |
|
| 1111 | " WHERE space_id = %s AND energy_storage_power_station_id = %s") |
|
| 1112 | cursor.execute(query, (id_, energy_storage_power_station_id,)) |
|
| 1113 | if cursor.fetchone() is not None: |
|
| 1114 | cursor.close() |
|
| 1115 | cnx.close() |
|
| 1116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1117 | description='API.SPACE_ENERGY_STORAGE_POWER_STATION_RELATION_EXISTS') |
|
| 1118 | ||
| 1119 | add_row = (" INSERT INTO tbl_spaces_energy_storage_power_stations (space_id, energy_storage_power_station_id) " |
|
| 1120 | " VALUES (%s, %s) ") |
|
| 1121 | cursor.execute(add_row, (id_, energy_storage_power_station_id,)) |
|
| 1122 | cnx.commit() |
|
| 1123 | cursor.close() |
|
| 1124 | cnx.close() |
|
| 1125 | ||
| 1126 | resp.status = falcon.HTTP_201 |
|
| 1127 | resp.location = '/spaces/' + str(id_) + '/energystoragepowerstations/' + str(energy_storage_power_station_id) |
|
| 1128 | ||
| 1129 | ||
| 1130 | class SpaceEnergyStoragePowerStationItem: |
|
| @@ 839-951 (lines=113) @@ | ||
| 836 | resp.text = json.dumps(result) |
|
| 837 | ||
| 838 | ||
| 839 | class SpaceCombinedEquipmentCollection: |
|
| 840 | def __init__(self): |
|
| 841 | """Initializes Class""" |
|
| 842 | pass |
|
| 843 | ||
| 844 | @staticmethod |
|
| 845 | def on_options(req, resp, id_): |
|
| 846 | resp.status = falcon.HTTP_200 |
|
| 847 | ||
| 848 | @staticmethod |
|
| 849 | def on_get(req, resp, id_): |
|
| 850 | if 'API-KEY' not in req.headers or \ |
|
| 851 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 852 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 853 | access_control(req) |
|
| 854 | else: |
|
| 855 | api_key_control(req) |
|
| 856 | if not id_.isdigit() or int(id_) <= 0: |
|
| 857 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 858 | description='API.INVALID_SPACE_ID') |
|
| 859 | ||
| 860 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 861 | cursor = cnx.cursor() |
|
| 862 | ||
| 863 | cursor.execute(" SELECT name " |
|
| 864 | " FROM tbl_spaces " |
|
| 865 | " WHERE id = %s ", (id_,)) |
|
| 866 | if cursor.fetchone() is None: |
|
| 867 | cursor.close() |
|
| 868 | cnx.close() |
|
| 869 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 870 | description='API.SPACE_NOT_FOUND') |
|
| 871 | ||
| 872 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 873 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
|
| 874 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
|
| 875 | " ORDER BY e.id ") |
|
| 876 | cursor.execute(query, (id_,)) |
|
| 877 | rows = cursor.fetchall() |
|
| 878 | ||
| 879 | result = list() |
|
| 880 | if rows is not None and len(rows) > 0: |
|
| 881 | for row in rows: |
|
| 882 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 883 | result.append(meta_result) |
|
| 884 | ||
| 885 | resp.text = json.dumps(result) |
|
| 886 | ||
| 887 | @staticmethod |
|
| 888 | @user_logger |
|
| 889 | def on_post(req, resp, id_): |
|
| 890 | """Handles POST requests""" |
|
| 891 | admin_control(req) |
|
| 892 | try: |
|
| 893 | raw_json = req.stream.read().decode('utf-8') |
|
| 894 | except Exception as ex: |
|
| 895 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 896 | title='API.BAD_REQUEST', |
|
| 897 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 898 | ||
| 899 | if not id_.isdigit() or int(id_) <= 0: |
|
| 900 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 901 | description='API.INVALID_SPACE_ID') |
|
| 902 | ||
| 903 | new_values = json.loads(raw_json) |
|
| 904 | ||
| 905 | if 'combined_equipment_id' not in new_values['data'].keys() or \ |
|
| 906 | not isinstance(new_values['data']['combined_equipment_id'], int) or \ |
|
| 907 | new_values['data']['combined_equipment_id'] <= 0: |
|
| 908 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 909 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 910 | combined_equipment_id = new_values['data']['combined_equipment_id'] |
|
| 911 | ||
| 912 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 913 | cursor = cnx.cursor() |
|
| 914 | ||
| 915 | cursor.execute(" SELECT name " |
|
| 916 | " from tbl_spaces " |
|
| 917 | " WHERE id = %s ", (id_,)) |
|
| 918 | if cursor.fetchone() is None: |
|
| 919 | cursor.close() |
|
| 920 | cnx.close() |
|
| 921 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 922 | description='API.SPACE_NOT_FOUND') |
|
| 923 | ||
| 924 | cursor.execute(" SELECT name " |
|
| 925 | " FROM tbl_combined_equipments " |
|
| 926 | " WHERE id = %s ", (combined_equipment_id,)) |
|
| 927 | if cursor.fetchone() is None: |
|
| 928 | cursor.close() |
|
| 929 | cnx.close() |
|
| 930 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 931 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 932 | ||
| 933 | query = (" SELECT id " |
|
| 934 | " FROM tbl_spaces_combined_equipments " |
|
| 935 | " WHERE space_id = %s AND combined_equipment_id = %s") |
|
| 936 | cursor.execute(query, (id_, combined_equipment_id,)) |
|
| 937 | if cursor.fetchone() is not None: |
|
| 938 | cursor.close() |
|
| 939 | cnx.close() |
|
| 940 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 941 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
|
| 942 | ||
| 943 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
|
| 944 | " VALUES (%s, %s) ") |
|
| 945 | cursor.execute(add_row, (id_, combined_equipment_id,)) |
|
| 946 | cnx.commit() |
|
| 947 | cursor.close() |
|
| 948 | cnx.close() |
|
| 949 | ||
| 950 | resp.status = falcon.HTTP_201 |
|
| 951 | resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id) |
|
| 952 | ||
| 953 | ||
| 954 | class SpaceCombinedEquipmentItem: |
|
| @@ 1739-1851 (lines=113) @@ | ||
| 1736 | resp.status = falcon.HTTP_204 |
|
| 1737 | ||
| 1738 | ||
| 1739 | class ShopfloorCommandCollection: |
|
| 1740 | def __init__(self): |
|
| 1741 | """Initializes Class""" |
|
| 1742 | pass |
|
| 1743 | ||
| 1744 | @staticmethod |
|
| 1745 | def on_options(req, resp, id_): |
|
| 1746 | resp.status = falcon.HTTP_200 |
|
| 1747 | ||
| 1748 | @staticmethod |
|
| 1749 | def on_get(req, resp, id_): |
|
| 1750 | if 'API-KEY' not in req.headers or \ |
|
| 1751 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1752 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1753 | access_control(req) |
|
| 1754 | else: |
|
| 1755 | api_key_control(req) |
|
| 1756 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1757 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1758 | description='API.INVALID_STORE_ID') |
|
| 1759 | ||
| 1760 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1761 | cursor = cnx.cursor() |
|
| 1762 | ||
| 1763 | cursor.execute(" SELECT name " |
|
| 1764 | " FROM tbl_shopfloors " |
|
| 1765 | " WHERE id = %s ", (id_,)) |
|
| 1766 | if cursor.fetchone() is None: |
|
| 1767 | cursor.close() |
|
| 1768 | cnx.close() |
|
| 1769 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1770 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1771 | ||
| 1772 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1773 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
|
| 1774 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 1775 | " ORDER BY c.id ") |
|
| 1776 | cursor.execute(query, (id_,)) |
|
| 1777 | rows = cursor.fetchall() |
|
| 1778 | ||
| 1779 | result = list() |
|
| 1780 | if rows is not None and len(rows) > 0: |
|
| 1781 | for row in rows: |
|
| 1782 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1783 | result.append(meta_result) |
|
| 1784 | ||
| 1785 | resp.text = json.dumps(result) |
|
| 1786 | ||
| 1787 | @staticmethod |
|
| 1788 | @user_logger |
|
| 1789 | def on_post(req, resp, id_): |
|
| 1790 | """Handles POST requests""" |
|
| 1791 | admin_control(req) |
|
| 1792 | try: |
|
| 1793 | raw_json = req.stream.read().decode('utf-8') |
|
| 1794 | except Exception as ex: |
|
| 1795 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1796 | title='API.BAD_REQUEST', |
|
| 1797 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1798 | ||
| 1799 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1800 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1801 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1802 | ||
| 1803 | new_values = json.loads(raw_json) |
|
| 1804 | ||
| 1805 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1806 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1807 | new_values['data']['command_id'] <= 0: |
|
| 1808 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1809 | description='API.INVALID_COMMAND_ID') |
|
| 1810 | command_id = new_values['data']['command_id'] |
|
| 1811 | ||
| 1812 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1813 | cursor = cnx.cursor() |
|
| 1814 | ||
| 1815 | cursor.execute(" SELECT name " |
|
| 1816 | " from tbl_shopfloors " |
|
| 1817 | " WHERE id = %s ", (id_,)) |
|
| 1818 | if cursor.fetchone() is None: |
|
| 1819 | cursor.close() |
|
| 1820 | cnx.close() |
|
| 1821 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1822 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1823 | ||
| 1824 | cursor.execute(" SELECT name " |
|
| 1825 | " FROM tbl_commands " |
|
| 1826 | " WHERE id = %s ", (command_id,)) |
|
| 1827 | if cursor.fetchone() is None: |
|
| 1828 | cursor.close() |
|
| 1829 | cnx.close() |
|
| 1830 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1831 | description='API.COMMAND_NOT_FOUND') |
|
| 1832 | ||
| 1833 | query = (" SELECT id " |
|
| 1834 | " FROM tbl_shopfloors_commands " |
|
| 1835 | " WHERE shopfloor_id = %s AND command_id = %s") |
|
| 1836 | cursor.execute(query, (id_, command_id,)) |
|
| 1837 | if cursor.fetchone() is not None: |
|
| 1838 | cursor.close() |
|
| 1839 | cnx.close() |
|
| 1840 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1841 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
|
| 1842 | ||
| 1843 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
|
| 1844 | " VALUES (%s, %s) ") |
|
| 1845 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1846 | cnx.commit() |
|
| 1847 | cursor.close() |
|
| 1848 | cnx.close() |
|
| 1849 | ||
| 1850 | resp.status = falcon.HTTP_201 |
|
| 1851 | resp.location = '/shopfloors/' + str(id_) + '/commands/' + str(command_id) |
|
| 1852 | ||
| 1853 | ||
| 1854 | class ShopfloorCommandItem: |
|
| @@ 1563-1675 (lines=113) @@ | ||
| 1560 | resp.status = falcon.HTTP_204 |
|
| 1561 | ||
| 1562 | ||
| 1563 | class ShopfloorWorkingCalendarCollection: |
|
| 1564 | def __init__(self): |
|
| 1565 | """Initializes ShopfloorWorkingCalendarCollection Class""" |
|
| 1566 | pass |
|
| 1567 | ||
| 1568 | @staticmethod |
|
| 1569 | def on_options(req, resp, id_): |
|
| 1570 | resp.status = falcon.HTTP_200 |
|
| 1571 | ||
| 1572 | @staticmethod |
|
| 1573 | def on_get(req, resp, id_): |
|
| 1574 | if 'API-KEY' not in req.headers or \ |
|
| 1575 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1576 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1577 | access_control(req) |
|
| 1578 | else: |
|
| 1579 | api_key_control(req) |
|
| 1580 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1581 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1582 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1583 | ||
| 1584 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1585 | cursor = cnx.cursor() |
|
| 1586 | ||
| 1587 | cursor.execute(" SELECT name " |
|
| 1588 | " FROM tbl_shopfloors " |
|
| 1589 | " WHERE id = %s ", (id_,)) |
|
| 1590 | if cursor.fetchone() is None: |
|
| 1591 | cursor.close() |
|
| 1592 | cnx.close() |
|
| 1593 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1594 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1595 | ||
| 1596 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1597 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
|
| 1598 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 1599 | " ORDER BY wc.id ") |
|
| 1600 | cursor.execute(query, (id_,)) |
|
| 1601 | rows = cursor.fetchall() |
|
| 1602 | ||
| 1603 | result = list() |
|
| 1604 | if rows is not None and len(rows) > 0: |
|
| 1605 | for row in rows: |
|
| 1606 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 1607 | result.append(meta_result) |
|
| 1608 | ||
| 1609 | resp.text = json.dumps(result) |
|
| 1610 | ||
| 1611 | @staticmethod |
|
| 1612 | @user_logger |
|
| 1613 | def on_post(req, resp, id_): |
|
| 1614 | """Handles POST requests""" |
|
| 1615 | admin_control(req) |
|
| 1616 | try: |
|
| 1617 | raw_json = req.stream.read().decode('utf-8') |
|
| 1618 | except Exception as ex: |
|
| 1619 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1620 | title='API.BAD_REQUEST', |
|
| 1621 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1622 | ||
| 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 | new_values = json.loads(raw_json) |
|
| 1628 | ||
| 1629 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1630 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1631 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1633 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1634 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1635 | ||
| 1636 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1637 | cursor = cnx.cursor() |
|
| 1638 | ||
| 1639 | cursor.execute(" SELECT name " |
|
| 1640 | " from tbl_shopfloors " |
|
| 1641 | " WHERE id = %s ", (id_,)) |
|
| 1642 | if cursor.fetchone() is None: |
|
| 1643 | cursor.close() |
|
| 1644 | cnx.close() |
|
| 1645 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1646 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1647 | ||
| 1648 | cursor.execute(" SELECT name " |
|
| 1649 | " FROM tbl_working_calendars " |
|
| 1650 | " WHERE id = %s ", (working_calendar_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.WORKING_CALENDAR_NOT_FOUND') |
|
| 1656 | ||
| 1657 | query = (" SELECT id " |
|
| 1658 | " FROM tbl_shopfloors_working_calendars " |
|
| 1659 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
|
| 1660 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1661 | if cursor.fetchone() is not None: |
|
| 1662 | cursor.close() |
|
| 1663 | cnx.close() |
|
| 1664 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1665 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1666 | ||
| 1667 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
|
| 1668 | " VALUES (%s, %s) ") |
|
| 1669 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1670 | cnx.commit() |
|
| 1671 | cursor.close() |
|
| 1672 | cnx.close() |
|
| 1673 | ||
| 1674 | resp.status = falcon.HTTP_201 |
|
| 1675 | resp.location = '/shopfloors/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1676 | ||
| 1677 | ||
| 1678 | class ShopfloorWorkingCalendarItem: |
|
| @@ 1199-1311 (lines=113) @@ | ||
| 1196 | resp.status = falcon.HTTP_204 |
|
| 1197 | ||
| 1198 | ||
| 1199 | class ShopfloorSensorCollection: |
|
| 1200 | def __init__(self): |
|
| 1201 | """Initializes ShopfloorSensorCollection""" |
|
| 1202 | pass |
|
| 1203 | ||
| 1204 | @staticmethod |
|
| 1205 | def on_options(req, resp, id_): |
|
| 1206 | resp.status = falcon.HTTP_200 |
|
| 1207 | ||
| 1208 | @staticmethod |
|
| 1209 | def on_get(req, resp, id_): |
|
| 1210 | if 'API-KEY' not in req.headers or \ |
|
| 1211 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1212 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1213 | access_control(req) |
|
| 1214 | else: |
|
| 1215 | api_key_control(req) |
|
| 1216 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1217 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1218 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1219 | ||
| 1220 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1221 | cursor = cnx.cursor() |
|
| 1222 | ||
| 1223 | cursor.execute(" SELECT name " |
|
| 1224 | " FROM tbl_shopfloors " |
|
| 1225 | " WHERE id = %s ", (id_,)) |
|
| 1226 | if cursor.fetchone() is None: |
|
| 1227 | cursor.close() |
|
| 1228 | cnx.close() |
|
| 1229 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1230 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1231 | ||
| 1232 | query = (" SELECT se.id, se.name, se.uuid " |
|
| 1233 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
|
| 1234 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
| 1235 | " ORDER BY se.id ") |
|
| 1236 | cursor.execute(query, (id_,)) |
|
| 1237 | rows = cursor.fetchall() |
|
| 1238 | ||
| 1239 | result = list() |
|
| 1240 | if rows is not None and len(rows) > 0: |
|
| 1241 | for row in rows: |
|
| 1242 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1243 | result.append(meta_result) |
|
| 1244 | ||
| 1245 | resp.text = json.dumps(result) |
|
| 1246 | ||
| 1247 | @staticmethod |
|
| 1248 | @user_logger |
|
| 1249 | def on_post(req, resp, id_): |
|
| 1250 | """Handles POST requests""" |
|
| 1251 | admin_control(req) |
|
| 1252 | try: |
|
| 1253 | raw_json = req.stream.read().decode('utf-8') |
|
| 1254 | except Exception as ex: |
|
| 1255 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1256 | title='API.BAD_REQUEST', |
|
| 1257 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1258 | ||
| 1259 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1261 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1262 | ||
| 1263 | new_values = json.loads(raw_json) |
|
| 1264 | ||
| 1265 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1266 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1267 | new_values['data']['sensor_id'] <= 0: |
|
| 1268 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1269 | description='API.INVALID_SENSOR_ID') |
|
| 1270 | sensor_id = new_values['data']['sensor_id'] |
|
| 1271 | ||
| 1272 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1273 | cursor = cnx.cursor() |
|
| 1274 | ||
| 1275 | cursor.execute(" SELECT name " |
|
| 1276 | " from tbl_shopfloors " |
|
| 1277 | " WHERE id = %s ", (id_,)) |
|
| 1278 | if cursor.fetchone() is None: |
|
| 1279 | cursor.close() |
|
| 1280 | cnx.close() |
|
| 1281 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1282 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1283 | ||
| 1284 | cursor.execute(" SELECT name " |
|
| 1285 | " FROM tbl_sensors " |
|
| 1286 | " WHERE id = %s ", (sensor_id,)) |
|
| 1287 | if cursor.fetchone() is None: |
|
| 1288 | cursor.close() |
|
| 1289 | cnx.close() |
|
| 1290 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1291 | description='API.SENSOR_NOT_FOUND') |
|
| 1292 | ||
| 1293 | query = (" SELECT id " |
|
| 1294 | " FROM tbl_shopfloors_sensors " |
|
| 1295 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
|
| 1296 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1297 | if cursor.fetchone() is not None: |
|
| 1298 | cursor.close() |
|
| 1299 | cnx.close() |
|
| 1300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1301 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
|
| 1302 | ||
| 1303 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
|
| 1304 | " VALUES (%s, %s) ") |
|
| 1305 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1306 | cnx.commit() |
|
| 1307 | cursor.close() |
|
| 1308 | cnx.close() |
|
| 1309 | ||
| 1310 | resp.status = falcon.HTTP_201 |
|
| 1311 | resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1312 | ||
| 1313 | ||
| 1314 | class ShopfloorSensorItem: |
|
| @@ 458-570 (lines=113) @@ | ||
| 455 | resp.status = falcon.HTTP_200 |
|
| 456 | ||
| 457 | ||
| 458 | class ShopfloorEquipmentCollection: |
|
| 459 | def __init__(self): |
|
| 460 | """Initializes ShopfloorEquipmentCollection""" |
|
| 461 | pass |
|
| 462 | ||
| 463 | @staticmethod |
|
| 464 | def on_options(req, resp, id_): |
|
| 465 | resp.status = falcon.HTTP_200 |
|
| 466 | ||
| 467 | @staticmethod |
|
| 468 | def on_get(req, resp, id_): |
|
| 469 | if 'API-KEY' not in req.headers or \ |
|
| 470 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 471 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 472 | access_control(req) |
|
| 473 | else: |
|
| 474 | api_key_control(req) |
|
| 475 | if not id_.isdigit() or int(id_) <= 0: |
|
| 476 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 477 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 478 | ||
| 479 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 480 | cursor = cnx.cursor() |
|
| 481 | ||
| 482 | cursor.execute(" SELECT name " |
|
| 483 | " FROM tbl_shopfloors " |
|
| 484 | " WHERE id = %s ", (id_,)) |
|
| 485 | if cursor.fetchone() is None: |
|
| 486 | cursor.close() |
|
| 487 | cnx.close() |
|
| 488 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 489 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 490 | ||
| 491 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 492 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
|
| 493 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
| 494 | " ORDER BY e.id ") |
|
| 495 | cursor.execute(query, (id_,)) |
|
| 496 | rows = cursor.fetchall() |
|
| 497 | ||
| 498 | result = list() |
|
| 499 | if rows is not None and len(rows) > 0: |
|
| 500 | for row in rows: |
|
| 501 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 502 | result.append(meta_result) |
|
| 503 | ||
| 504 | resp.text = json.dumps(result) |
|
| 505 | ||
| 506 | @staticmethod |
|
| 507 | @user_logger |
|
| 508 | def on_post(req, resp, id_): |
|
| 509 | """Handles POST requests""" |
|
| 510 | admin_control(req) |
|
| 511 | try: |
|
| 512 | raw_json = req.stream.read().decode('utf-8') |
|
| 513 | except Exception as ex: |
|
| 514 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 515 | title='API.BAD_REQUEST', |
|
| 516 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 517 | ||
| 518 | if not id_.isdigit() or int(id_) <= 0: |
|
| 519 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 520 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 521 | ||
| 522 | new_values = json.loads(raw_json) |
|
| 523 | ||
| 524 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 525 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 526 | new_values['data']['equipment_id'] <= 0: |
|
| 527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 528 | description='API.INVALID_EQUIPMENT_ID') |
|
| 529 | equipment_id = new_values['data']['equipment_id'] |
|
| 530 | ||
| 531 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 532 | cursor = cnx.cursor() |
|
| 533 | ||
| 534 | cursor.execute(" SELECT name " |
|
| 535 | " from tbl_shopfloors " |
|
| 536 | " WHERE id = %s ", (id_,)) |
|
| 537 | if cursor.fetchone() is None: |
|
| 538 | cursor.close() |
|
| 539 | cnx.close() |
|
| 540 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 541 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 542 | ||
| 543 | cursor.execute(" SELECT name " |
|
| 544 | " FROM tbl_equipments " |
|
| 545 | " WHERE id = %s ", (equipment_id,)) |
|
| 546 | if cursor.fetchone() is None: |
|
| 547 | cursor.close() |
|
| 548 | cnx.close() |
|
| 549 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 550 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 551 | ||
| 552 | query = (" SELECT id " |
|
| 553 | " FROM tbl_shopfloors_equipments " |
|
| 554 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
|
| 555 | cursor.execute(query, (id_, equipment_id,)) |
|
| 556 | if cursor.fetchone() is not None: |
|
| 557 | cursor.close() |
|
| 558 | cnx.close() |
|
| 559 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 560 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
|
| 561 | ||
| 562 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
|
| 563 | " VALUES (%s, %s) ") |
|
| 564 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 565 | cnx.commit() |
|
| 566 | cursor.close() |
|
| 567 | cnx.close() |
|
| 568 | ||
| 569 | resp.status = falcon.HTTP_201 |
|
| 570 | resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 571 | ||
| 572 | ||
| 573 | class ShopfloorEquipmentItem: |
|
| @@ 1682-1794 (lines=113) @@ | ||
| 1679 | resp.status = falcon.HTTP_204 |
|
| 1680 | ||
| 1681 | ||
| 1682 | class StoreCommandCollection: |
|
| 1683 | def __init__(self): |
|
| 1684 | """Initializes Class""" |
|
| 1685 | pass |
|
| 1686 | ||
| 1687 | @staticmethod |
|
| 1688 | def on_options(req, resp, id_): |
|
| 1689 | resp.status = falcon.HTTP_200 |
|
| 1690 | ||
| 1691 | @staticmethod |
|
| 1692 | def on_get(req, resp, id_): |
|
| 1693 | if 'API-KEY' not in req.headers or \ |
|
| 1694 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1695 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1696 | access_control(req) |
|
| 1697 | else: |
|
| 1698 | api_key_control(req) |
|
| 1699 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1700 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1701 | description='API.INVALID_STORE_ID') |
|
| 1702 | ||
| 1703 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1704 | cursor = cnx.cursor() |
|
| 1705 | ||
| 1706 | cursor.execute(" SELECT name " |
|
| 1707 | " FROM tbl_stores " |
|
| 1708 | " WHERE id = %s ", (id_,)) |
|
| 1709 | if cursor.fetchone() is None: |
|
| 1710 | cursor.close() |
|
| 1711 | cnx.close() |
|
| 1712 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1713 | description='API.STORE_NOT_FOUND') |
|
| 1714 | ||
| 1715 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1716 | " FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
|
| 1717 | " WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 1718 | " ORDER BY c.id ") |
|
| 1719 | cursor.execute(query, (id_,)) |
|
| 1720 | rows = cursor.fetchall() |
|
| 1721 | ||
| 1722 | result = list() |
|
| 1723 | if rows is not None and len(rows) > 0: |
|
| 1724 | for row in rows: |
|
| 1725 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1726 | result.append(meta_result) |
|
| 1727 | ||
| 1728 | resp.text = json.dumps(result) |
|
| 1729 | ||
| 1730 | @staticmethod |
|
| 1731 | @user_logger |
|
| 1732 | def on_post(req, resp, id_): |
|
| 1733 | """Handles POST requests""" |
|
| 1734 | admin_control(req) |
|
| 1735 | try: |
|
| 1736 | raw_json = req.stream.read().decode('utf-8') |
|
| 1737 | except Exception as ex: |
|
| 1738 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1739 | title='API.BAD_REQUEST', |
|
| 1740 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1741 | ||
| 1742 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1743 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1744 | description='API.INVALID_STORE_ID') |
|
| 1745 | ||
| 1746 | new_values = json.loads(raw_json) |
|
| 1747 | ||
| 1748 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1749 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1750 | new_values['data']['command_id'] <= 0: |
|
| 1751 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1752 | description='API.INVALID_COMMAND_ID') |
|
| 1753 | command_id = new_values['data']['command_id'] |
|
| 1754 | ||
| 1755 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1756 | cursor = cnx.cursor() |
|
| 1757 | ||
| 1758 | cursor.execute(" SELECT name " |
|
| 1759 | " from tbl_stores " |
|
| 1760 | " WHERE id = %s ", (id_,)) |
|
| 1761 | if cursor.fetchone() is None: |
|
| 1762 | cursor.close() |
|
| 1763 | cnx.close() |
|
| 1764 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1765 | description='API.STORE_NOT_FOUND') |
|
| 1766 | ||
| 1767 | cursor.execute(" SELECT name " |
|
| 1768 | " FROM tbl_commands " |
|
| 1769 | " WHERE id = %s ", (command_id,)) |
|
| 1770 | if cursor.fetchone() is None: |
|
| 1771 | cursor.close() |
|
| 1772 | cnx.close() |
|
| 1773 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1774 | description='API.COMMAND_NOT_FOUND') |
|
| 1775 | ||
| 1776 | query = (" SELECT id " |
|
| 1777 | " FROM tbl_stores_commands " |
|
| 1778 | " WHERE store_id = %s AND command_id = %s") |
|
| 1779 | cursor.execute(query, (id_, command_id,)) |
|
| 1780 | if cursor.fetchone() is not None: |
|
| 1781 | cursor.close() |
|
| 1782 | cnx.close() |
|
| 1783 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1784 | description='API.STORE_COMMAND_RELATION_EXISTS') |
|
| 1785 | ||
| 1786 | add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
|
| 1787 | " VALUES (%s, %s) ") |
|
| 1788 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1789 | cnx.commit() |
|
| 1790 | cursor.close() |
|
| 1791 | cnx.close() |
|
| 1792 | ||
| 1793 | resp.status = falcon.HTTP_201 |
|
| 1794 | resp.location = '/stores/' + str(id_) + '/commands/' + str(command_id) |
|
| 1795 | ||
| 1796 | ||
| 1797 | class StoreCommandItem: |
|
| @@ 1506-1618 (lines=113) @@ | ||
| 1503 | resp.status = falcon.HTTP_204 |
|
| 1504 | ||
| 1505 | ||
| 1506 | class StoreWorkingCalendarCollection: |
|
| 1507 | def __init__(self): |
|
| 1508 | """Initializes StoreWorkingCalendarCollection Class""" |
|
| 1509 | pass |
|
| 1510 | ||
| 1511 | @staticmethod |
|
| 1512 | def on_options(req, resp, id_): |
|
| 1513 | resp.status = falcon.HTTP_200 |
|
| 1514 | ||
| 1515 | @staticmethod |
|
| 1516 | def on_get(req, resp, id_): |
|
| 1517 | if 'API-KEY' not in req.headers or \ |
|
| 1518 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1519 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1520 | access_control(req) |
|
| 1521 | else: |
|
| 1522 | api_key_control(req) |
|
| 1523 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1524 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1525 | description='API.INVALID_STORE_ID') |
|
| 1526 | ||
| 1527 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1528 | cursor = cnx.cursor() |
|
| 1529 | ||
| 1530 | cursor.execute(" SELECT name " |
|
| 1531 | " FROM tbl_stores " |
|
| 1532 | " WHERE id = %s ", (id_,)) |
|
| 1533 | if cursor.fetchone() is None: |
|
| 1534 | cursor.close() |
|
| 1535 | cnx.close() |
|
| 1536 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1537 | description='API.STORE_NOT_FOUND') |
|
| 1538 | ||
| 1539 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1540 | " FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
|
| 1541 | " WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 1542 | " ORDER BY wc.id ") |
|
| 1543 | cursor.execute(query, (id_,)) |
|
| 1544 | rows = cursor.fetchall() |
|
| 1545 | ||
| 1546 | result = list() |
|
| 1547 | if rows is not None and len(rows) > 0: |
|
| 1548 | for row in rows: |
|
| 1549 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 1550 | result.append(meta_result) |
|
| 1551 | ||
| 1552 | resp.text = json.dumps(result) |
|
| 1553 | ||
| 1554 | @staticmethod |
|
| 1555 | @user_logger |
|
| 1556 | def on_post(req, resp, id_): |
|
| 1557 | """Handles POST requests""" |
|
| 1558 | admin_control(req) |
|
| 1559 | try: |
|
| 1560 | raw_json = req.stream.read().decode('utf-8') |
|
| 1561 | except Exception as ex: |
|
| 1562 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1563 | title='API.BAD_REQUEST', |
|
| 1564 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1565 | ||
| 1566 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1568 | description='API.INVALID_STORE_ID') |
|
| 1569 | ||
| 1570 | new_values = json.loads(raw_json) |
|
| 1571 | ||
| 1572 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1573 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1574 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1575 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1576 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1577 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1578 | ||
| 1579 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1580 | cursor = cnx.cursor() |
|
| 1581 | ||
| 1582 | cursor.execute(" SELECT name " |
|
| 1583 | " from tbl_stores " |
|
| 1584 | " WHERE id = %s ", (id_,)) |
|
| 1585 | if cursor.fetchone() is None: |
|
| 1586 | cursor.close() |
|
| 1587 | cnx.close() |
|
| 1588 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1589 | description='API.STORE_NOT_FOUND') |
|
| 1590 | ||
| 1591 | cursor.execute(" SELECT name " |
|
| 1592 | " FROM tbl_working_calendars " |
|
| 1593 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 1594 | if cursor.fetchone() is None: |
|
| 1595 | cursor.close() |
|
| 1596 | cnx.close() |
|
| 1597 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1598 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 1599 | ||
| 1600 | query = (" SELECT id " |
|
| 1601 | " FROM tbl_stores_working_calendars " |
|
| 1602 | " WHERE store_id = %s AND working_calendar_id = %s") |
|
| 1603 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1604 | if cursor.fetchone() is not None: |
|
| 1605 | cursor.close() |
|
| 1606 | cnx.close() |
|
| 1607 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1608 | description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1609 | ||
| 1610 | add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
|
| 1611 | " VALUES (%s, %s) ") |
|
| 1612 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1613 | cnx.commit() |
|
| 1614 | cursor.close() |
|
| 1615 | cnx.close() |
|
| 1616 | ||
| 1617 | resp.status = falcon.HTTP_201 |
|
| 1618 | resp.location = '/stores/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1619 | ||
| 1620 | ||
| 1621 | class StoreWorkingCalendarItem: |
|
| @@ 1142-1254 (lines=113) @@ | ||
| 1139 | resp.status = falcon.HTTP_204 |
|
| 1140 | ||
| 1141 | ||
| 1142 | class StoreSensorCollection: |
|
| 1143 | def __init__(self): |
|
| 1144 | """Initializes Class""" |
|
| 1145 | pass |
|
| 1146 | ||
| 1147 | @staticmethod |
|
| 1148 | def on_options(req, resp, id_): |
|
| 1149 | resp.status = falcon.HTTP_200 |
|
| 1150 | ||
| 1151 | @staticmethod |
|
| 1152 | def on_get(req, resp, id_): |
|
| 1153 | if 'API-KEY' not in req.headers or \ |
|
| 1154 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1155 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1156 | access_control(req) |
|
| 1157 | else: |
|
| 1158 | api_key_control(req) |
|
| 1159 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1161 | description='API.INVALID_STORE_ID') |
|
| 1162 | ||
| 1163 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1164 | cursor = cnx.cursor() |
|
| 1165 | ||
| 1166 | cursor.execute(" SELECT name " |
|
| 1167 | " FROM tbl_stores " |
|
| 1168 | " WHERE id = %s ", (id_,)) |
|
| 1169 | if cursor.fetchone() is None: |
|
| 1170 | cursor.close() |
|
| 1171 | cnx.close() |
|
| 1172 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1173 | description='API.STORE_NOT_FOUND') |
|
| 1174 | ||
| 1175 | query = (" SELECT s.id, s.name, s.uuid " |
|
| 1176 | " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
|
| 1177 | " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
| 1178 | " ORDER BY s.id ") |
|
| 1179 | cursor.execute(query, (id_,)) |
|
| 1180 | rows = cursor.fetchall() |
|
| 1181 | ||
| 1182 | result = list() |
|
| 1183 | if rows is not None and len(rows) > 0: |
|
| 1184 | for row in rows: |
|
| 1185 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1186 | result.append(meta_result) |
|
| 1187 | ||
| 1188 | resp.text = json.dumps(result) |
|
| 1189 | ||
| 1190 | @staticmethod |
|
| 1191 | @user_logger |
|
| 1192 | def on_post(req, resp, id_): |
|
| 1193 | """Handles POST requests""" |
|
| 1194 | admin_control(req) |
|
| 1195 | try: |
|
| 1196 | raw_json = req.stream.read().decode('utf-8') |
|
| 1197 | except Exception as ex: |
|
| 1198 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1199 | title='API.BAD_REQUEST', |
|
| 1200 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1201 | ||
| 1202 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1203 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1204 | description='API.INVALID_STORE_ID') |
|
| 1205 | ||
| 1206 | new_values = json.loads(raw_json) |
|
| 1207 | ||
| 1208 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1209 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1210 | new_values['data']['sensor_id'] <= 0: |
|
| 1211 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1212 | description='API.INVALID_SENSOR_ID') |
|
| 1213 | sensor_id = new_values['data']['sensor_id'] |
|
| 1214 | ||
| 1215 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1216 | cursor = cnx.cursor() |
|
| 1217 | ||
| 1218 | cursor.execute(" SELECT name " |
|
| 1219 | " from tbl_stores " |
|
| 1220 | " WHERE id = %s ", (id_,)) |
|
| 1221 | if cursor.fetchone() is None: |
|
| 1222 | cursor.close() |
|
| 1223 | cnx.close() |
|
| 1224 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1225 | description='API.STORE_NOT_FOUND') |
|
| 1226 | ||
| 1227 | cursor.execute(" SELECT name " |
|
| 1228 | " FROM tbl_sensors " |
|
| 1229 | " WHERE id = %s ", (sensor_id,)) |
|
| 1230 | if cursor.fetchone() is None: |
|
| 1231 | cursor.close() |
|
| 1232 | cnx.close() |
|
| 1233 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1234 | description='API.SENSOR_NOT_FOUND') |
|
| 1235 | ||
| 1236 | query = (" SELECT id " |
|
| 1237 | " FROM tbl_stores_sensors " |
|
| 1238 | " WHERE store_id = %s AND sensor_id = %s") |
|
| 1239 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1240 | if cursor.fetchone() is not None: |
|
| 1241 | cursor.close() |
|
| 1242 | cnx.close() |
|
| 1243 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1244 | description='API.STORE_SENSOR_RELATION_EXISTS') |
|
| 1245 | ||
| 1246 | add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
|
| 1247 | " VALUES (%s, %s) ") |
|
| 1248 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1249 | cnx.commit() |
|
| 1250 | cursor.close() |
|
| 1251 | cnx.close() |
|
| 1252 | ||
| 1253 | resp.status = falcon.HTTP_201 |
|
| 1254 | resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1255 | ||
| 1256 | ||
| 1257 | class StoreSensorItem: |
|
| @@ 1142-1254 (lines=113) @@ | ||
| 1139 | resp.status = falcon.HTTP_204 |
|
| 1140 | ||
| 1141 | ||
| 1142 | class MeterCommandCollection: |
|
| 1143 | def __init__(self): |
|
| 1144 | """Initializes Class""" |
|
| 1145 | pass |
|
| 1146 | ||
| 1147 | @staticmethod |
|
| 1148 | def on_options(req, resp, id_): |
|
| 1149 | resp.status = falcon.HTTP_200 |
|
| 1150 | ||
| 1151 | @staticmethod |
|
| 1152 | def on_get(req, resp, id_): |
|
| 1153 | if 'API-KEY' not in req.headers or \ |
|
| 1154 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1155 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1156 | access_control(req) |
|
| 1157 | else: |
|
| 1158 | api_key_control(req) |
|
| 1159 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1161 | description='API.INVALID_METER_ID') |
|
| 1162 | ||
| 1163 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1164 | cursor = cnx.cursor() |
|
| 1165 | ||
| 1166 | cursor.execute(" SELECT name " |
|
| 1167 | " FROM tbl_meters " |
|
| 1168 | " WHERE id = %s ", (id_,)) |
|
| 1169 | if cursor.fetchone() is None: |
|
| 1170 | cursor.close() |
|
| 1171 | cnx.close() |
|
| 1172 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1173 | description='API.METER_NOT_FOUND') |
|
| 1174 | ||
| 1175 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1176 | " FROM tbl_meters m, tbl_meters_commands mc, tbl_commands c " |
|
| 1177 | " WHERE mc.meter_id = m.id AND c.id = mc.command_id AND m.id = %s " |
|
| 1178 | " ORDER BY c.id ") |
|
| 1179 | cursor.execute(query, (id_,)) |
|
| 1180 | rows = cursor.fetchall() |
|
| 1181 | ||
| 1182 | result = list() |
|
| 1183 | if rows is not None and len(rows) > 0: |
|
| 1184 | for row in rows: |
|
| 1185 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1186 | result.append(meta_result) |
|
| 1187 | ||
| 1188 | resp.text = json.dumps(result) |
|
| 1189 | ||
| 1190 | @staticmethod |
|
| 1191 | @user_logger |
|
| 1192 | def on_post(req, resp, id_): |
|
| 1193 | """Handles POST requests""" |
|
| 1194 | admin_control(req) |
|
| 1195 | try: |
|
| 1196 | raw_json = req.stream.read().decode('utf-8') |
|
| 1197 | except Exception as ex: |
|
| 1198 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1199 | title='API.BAD_REQUEST', |
|
| 1200 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1201 | ||
| 1202 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1203 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1204 | description='API.INVALID_METER_ID') |
|
| 1205 | ||
| 1206 | new_values = json.loads(raw_json) |
|
| 1207 | ||
| 1208 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1209 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1210 | new_values['data']['command_id'] <= 0: |
|
| 1211 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1212 | description='API.INVALID_COMMAND_ID') |
|
| 1213 | command_id = new_values['data']['command_id'] |
|
| 1214 | ||
| 1215 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1216 | cursor = cnx.cursor() |
|
| 1217 | ||
| 1218 | cursor.execute(" SELECT name " |
|
| 1219 | " from tbl_meters " |
|
| 1220 | " WHERE id = %s ", (id_,)) |
|
| 1221 | if cursor.fetchone() is None: |
|
| 1222 | cursor.close() |
|
| 1223 | cnx.close() |
|
| 1224 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1225 | description='API.METER_NOT_FOUND') |
|
| 1226 | ||
| 1227 | cursor.execute(" SELECT name " |
|
| 1228 | " FROM tbl_commands " |
|
| 1229 | " WHERE id = %s ", (command_id,)) |
|
| 1230 | if cursor.fetchone() is None: |
|
| 1231 | cursor.close() |
|
| 1232 | cnx.close() |
|
| 1233 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1234 | description='API.COMMAND_NOT_FOUND') |
|
| 1235 | ||
| 1236 | query = (" SELECT id " |
|
| 1237 | " FROM tbl_meters_commands " |
|
| 1238 | " WHERE meter_id = %s AND command_id = %s") |
|
| 1239 | cursor.execute(query, (id_, command_id,)) |
|
| 1240 | if cursor.fetchone() is not None: |
|
| 1241 | cursor.close() |
|
| 1242 | cnx.close() |
|
| 1243 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1244 | description='API.METER_COMMAND_RELATION_EXISTS') |
|
| 1245 | ||
| 1246 | add_row = (" INSERT INTO tbl_meters_commands (meter_id, command_id) " |
|
| 1247 | " VALUES (%s, %s) ") |
|
| 1248 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1249 | cnx.commit() |
|
| 1250 | cursor.close() |
|
| 1251 | cnx.close() |
|
| 1252 | ||
| 1253 | resp.status = falcon.HTTP_201 |
|
| 1254 | resp.location = '/meters/' + str(id_) + '/commands/' + str(command_id) |
|
| 1255 | ||
| 1256 | ||
| 1257 | class MeterCommandItem: |
|
| @@ 435-547 (lines=113) @@ | ||
| 432 | resp.status = falcon.HTTP_200 |
|
| 433 | ||
| 434 | ||
| 435 | class VirtualPowerPlantMicrogridCollection: |
|
| 436 | def __init__(self): |
|
| 437 | """Initializes Class""" |
|
| 438 | pass |
|
| 439 | ||
| 440 | @staticmethod |
|
| 441 | def on_options(req, resp, id_): |
|
| 442 | resp.status = falcon.HTTP_200 |
|
| 443 | ||
| 444 | @staticmethod |
|
| 445 | def on_get(req, resp, id_): |
|
| 446 | if 'API-KEY' not in req.headers or \ |
|
| 447 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 448 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 449 | access_control(req) |
|
| 450 | else: |
|
| 451 | api_key_control(req) |
|
| 452 | if not id_.isdigit() or int(id_) <= 0: |
|
| 453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 454 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
| 455 | ||
| 456 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 457 | cursor = cnx.cursor() |
|
| 458 | ||
| 459 | cursor.execute(" SELECT name " |
|
| 460 | " FROM tbl_virtual_power_plants " |
|
| 461 | " WHERE id = %s ", (id_,)) |
|
| 462 | if cursor.fetchone() is None: |
|
| 463 | cursor.close() |
|
| 464 | cnx.close() |
|
| 465 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 466 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
| 467 | ||
| 468 | query = (" SELECT m.id, m.name, m.uuid " |
|
| 469 | " FROM tbl_virtual_power_plants_microgrids vppm, tbl_microgrids m " |
|
| 470 | " WHERE m.id = vppm.microgrid_id AND vppm.virtual_power_plant_id = %s " |
|
| 471 | " ORDER BY m.id ") |
|
| 472 | cursor.execute(query, (id_,)) |
|
| 473 | rows = cursor.fetchall() |
|
| 474 | ||
| 475 | result = list() |
|
| 476 | if rows is not None and len(rows) > 0: |
|
| 477 | for row in rows: |
|
| 478 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 479 | result.append(meta_result) |
|
| 480 | ||
| 481 | resp.text = json.dumps(result) |
|
| 482 | ||
| 483 | @staticmethod |
|
| 484 | @user_logger |
|
| 485 | def on_post(req, resp, id_): |
|
| 486 | """Handles POST requests""" |
|
| 487 | admin_control(req) |
|
| 488 | try: |
|
| 489 | raw_json = req.stream.read().decode('utf-8') |
|
| 490 | except Exception as ex: |
|
| 491 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 492 | title='API.BAD_REQUEST', |
|
| 493 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 494 | ||
| 495 | if not id_.isdigit() or int(id_) <= 0: |
|
| 496 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 497 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
| 498 | ||
| 499 | new_values = json.loads(raw_json) |
|
| 500 | ||
| 501 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
| 502 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
| 503 | new_values['data']['microgrid_id'] <= 0: |
|
| 504 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 505 | description='API.INVALID_MICROGRID_ID') |
|
| 506 | microgrid_id = new_values['data']['microgrid_id'] |
|
| 507 | ||
| 508 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 509 | cursor = cnx.cursor() |
|
| 510 | ||
| 511 | cursor.execute(" SELECT name " |
|
| 512 | " from tbl_virtual_power_plants " |
|
| 513 | " WHERE id = %s ", (id_,)) |
|
| 514 | if cursor.fetchone() is None: |
|
| 515 | cursor.close() |
|
| 516 | cnx.close() |
|
| 517 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 518 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
| 519 | ||
| 520 | cursor.execute(" SELECT name " |
|
| 521 | " FROM tbl_microgrids " |
|
| 522 | " WHERE id = %s ", (microgrid_id,)) |
|
| 523 | if cursor.fetchone() is None: |
|
| 524 | cursor.close() |
|
| 525 | cnx.close() |
|
| 526 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 527 | description='API.MICROGRID_NOT_FOUND') |
|
| 528 | ||
| 529 | query = (" SELECT id " |
|
| 530 | " FROM tbl_virtual_power_plants_microgrids " |
|
| 531 | " WHERE virtual_power_plant_id = %s AND microgrid_id = %s") |
|
| 532 | cursor.execute(query, (id_, microgrid_id,)) |
|
| 533 | if cursor.fetchone() is not None: |
|
| 534 | cursor.close() |
|
| 535 | cnx.close() |
|
| 536 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 537 | description='API.VIRTUAL_POWER_PLANT_MICROGRID_RELATION_EXISTS') |
|
| 538 | ||
| 539 | add_row = (" INSERT INTO tbl_virtual_power_plants_microgrids (virtual_power_plant_id, microgrid_id) " |
|
| 540 | " VALUES (%s, %s) ") |
|
| 541 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
| 542 | cnx.commit() |
|
| 543 | cursor.close() |
|
| 544 | cnx.close() |
|
| 545 | ||
| 546 | resp.status = falcon.HTTP_201 |
|
| 547 | resp.location = '/virtualpowerplants/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
| 548 | ||
| 549 | ||
| 550 | class VirtualPowerPlantMicrogridItem: |
|
| @@ 1488-1600 (lines=113) @@ | ||
| 1485 | resp.status = falcon.HTTP_204 |
|
| 1486 | ||
| 1487 | ||
| 1488 | class EnergyStorageContainerCommandCollection: |
|
| 1489 | def __init__(self): |
|
| 1490 | """Initializes Class""" |
|
| 1491 | pass |
|
| 1492 | ||
| 1493 | @staticmethod |
|
| 1494 | def on_options(req, resp, id_): |
|
| 1495 | resp.status = falcon.HTTP_200 |
|
| 1496 | ||
| 1497 | @staticmethod |
|
| 1498 | def on_get(req, resp, id_): |
|
| 1499 | if 'API-KEY' not in req.headers or \ |
|
| 1500 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1501 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1502 | access_control(req) |
|
| 1503 | else: |
|
| 1504 | api_key_control(req) |
|
| 1505 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1506 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1507 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1508 | ||
| 1509 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1510 | cursor = cnx.cursor() |
|
| 1511 | ||
| 1512 | cursor.execute(" SELECT name " |
|
| 1513 | " FROM tbl_energy_storage_containers " |
|
| 1514 | " WHERE id = %s ", (id_,)) |
|
| 1515 | if cursor.fetchone() is None: |
|
| 1516 | cursor.close() |
|
| 1517 | cnx.close() |
|
| 1518 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1519 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1520 | ||
| 1521 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1522 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_commands cec, tbl_commands c " |
|
| 1523 | " WHERE cec.energy_storage_container_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
| 1524 | " ORDER BY c.id ") |
|
| 1525 | cursor.execute(query, (id_,)) |
|
| 1526 | rows = cursor.fetchall() |
|
| 1527 | ||
| 1528 | result = list() |
|
| 1529 | if rows is not None and len(rows) > 0: |
|
| 1530 | for row in rows: |
|
| 1531 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1532 | result.append(meta_result) |
|
| 1533 | ||
| 1534 | resp.text = json.dumps(result) |
|
| 1535 | ||
| 1536 | @staticmethod |
|
| 1537 | @user_logger |
|
| 1538 | def on_post(req, resp, id_): |
|
| 1539 | """Handles POST requests""" |
|
| 1540 | admin_control(req) |
|
| 1541 | try: |
|
| 1542 | raw_json = req.stream.read().decode('utf-8') |
|
| 1543 | except Exception as ex: |
|
| 1544 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1545 | title='API.BAD_REQUEST', |
|
| 1546 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1547 | ||
| 1548 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1549 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1550 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1551 | ||
| 1552 | new_values = json.loads(raw_json) |
|
| 1553 | ||
| 1554 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1555 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1556 | new_values['data']['command_id'] <= 0: |
|
| 1557 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1558 | description='API.INVALID_COMMAND_ID') |
|
| 1559 | command_id = new_values['data']['command_id'] |
|
| 1560 | ||
| 1561 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1562 | cursor = cnx.cursor() |
|
| 1563 | ||
| 1564 | cursor.execute(" SELECT name " |
|
| 1565 | " from tbl_energy_storage_containers " |
|
| 1566 | " WHERE id = %s ", (id_,)) |
|
| 1567 | if cursor.fetchone() is None: |
|
| 1568 | cursor.close() |
|
| 1569 | cnx.close() |
|
| 1570 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1571 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1572 | ||
| 1573 | cursor.execute(" SELECT name " |
|
| 1574 | " FROM tbl_commands " |
|
| 1575 | " WHERE id = %s ", (command_id,)) |
|
| 1576 | if cursor.fetchone() is None: |
|
| 1577 | cursor.close() |
|
| 1578 | cnx.close() |
|
| 1579 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1580 | description='API.COMMAND_NOT_FOUND') |
|
| 1581 | ||
| 1582 | query = (" SELECT id " |
|
| 1583 | " FROM tbl_energy_storage_containers_commands " |
|
| 1584 | " WHERE energy_storage_container_id = %s AND command_id = %s") |
|
| 1585 | cursor.execute(query, (id_, command_id,)) |
|
| 1586 | if cursor.fetchone() is not None: |
|
| 1587 | cursor.close() |
|
| 1588 | cnx.close() |
|
| 1589 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1590 | description='API.ENERGY_STORAGE_CONTAINER_COMMAND_RELATION_EXISTS') |
|
| 1591 | ||
| 1592 | add_row = (" INSERT INTO tbl_energy_storage_containers_commands (energy_storage_container_id, command_id) " |
|
| 1593 | " VALUES (%s, %s) ") |
|
| 1594 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1595 | cnx.commit() |
|
| 1596 | cursor.close() |
|
| 1597 | cnx.close() |
|
| 1598 | ||
| 1599 | resp.status = falcon.HTTP_201 |
|
| 1600 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
|
| 1601 | ||
| 1602 | ||
| 1603 | class EnergyStorageContainerCommandItem: |
|