| @@ 5059-5175 (lines=117) @@ | ||
| 5056 | resp.status = falcon.HTTP_204 |
|
| 5057 | ||
| 5058 | ||
| 5059 | class PhotovoltaicPowerStationDataSourceCollection: |
|
| 5060 | def __init__(self): |
|
| 5061 | pass |
|
| 5062 | ||
| 5063 | @staticmethod |
|
| 5064 | def on_options(req, resp, id_): |
|
| 5065 | _ = req |
|
| 5066 | resp.status = falcon.HTTP_200 |
|
| 5067 | _ = id_ |
|
| 5068 | ||
| 5069 | @staticmethod |
|
| 5070 | def on_get(req, resp, id_): |
|
| 5071 | if 'API-KEY' not in req.headers or \ |
|
| 5072 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5073 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5074 | access_control(req) |
|
| 5075 | else: |
|
| 5076 | api_key_control(req) |
|
| 5077 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5078 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5079 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5080 | ||
| 5081 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5082 | cursor = cnx.cursor() |
|
| 5083 | ||
| 5084 | cursor.execute(" SELECT name " |
|
| 5085 | " FROM tbl_photovoltaic_power_stations " |
|
| 5086 | " WHERE id = %s ", (id_,)) |
|
| 5087 | if cursor.fetchone() is None: |
|
| 5088 | cursor.close() |
|
| 5089 | cnx.close() |
|
| 5090 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5091 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 5092 | ||
| 5093 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
| 5094 | " FROM tbl_photovoltaic_power_stations psp, tbl_photovoltaic_power_stations_data_sources psds, " |
|
| 5095 | " tbl_data_sources ds " |
|
| 5096 | " WHERE psds.photovoltaic_power_station_id = psp.id AND ds.id = psds.data_source_id AND psp.id = %s " |
|
| 5097 | " ORDER BY ds.id ") |
|
| 5098 | cursor.execute(query, (id_,)) |
|
| 5099 | rows = cursor.fetchall() |
|
| 5100 | ||
| 5101 | result = list() |
|
| 5102 | if rows is not None and len(rows) > 0: |
|
| 5103 | for row in rows: |
|
| 5104 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5105 | result.append(meta_result) |
|
| 5106 | ||
| 5107 | resp.text = json.dumps(result) |
|
| 5108 | ||
| 5109 | @staticmethod |
|
| 5110 | @user_logger |
|
| 5111 | def on_post(req, resp, id_): |
|
| 5112 | """Handles POST requests""" |
|
| 5113 | admin_control(req) |
|
| 5114 | try: |
|
| 5115 | raw_json = req.stream.read().decode('utf-8') |
|
| 5116 | except Exception as ex: |
|
| 5117 | print(str(ex)) |
|
| 5118 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5119 | title='API.BAD_REQUEST', |
|
| 5120 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5121 | ||
| 5122 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5123 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5124 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5125 | ||
| 5126 | new_values = json.loads(raw_json) |
|
| 5127 | ||
| 5128 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
| 5129 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
| 5130 | new_values['data']['data_source_id'] <= 0: |
|
| 5131 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5132 | description='API.INVALID_DATA_SOURCE_ID') |
|
| 5133 | data_source_id = new_values['data']['data_source_id'] |
|
| 5134 | ||
| 5135 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5136 | cursor = cnx.cursor() |
|
| 5137 | ||
| 5138 | cursor.execute(" SELECT name " |
|
| 5139 | " from tbl_photovoltaic_power_stations " |
|
| 5140 | " WHERE id = %s ", (id_,)) |
|
| 5141 | if cursor.fetchone() is None: |
|
| 5142 | cursor.close() |
|
| 5143 | cnx.close() |
|
| 5144 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5145 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 5146 | ||
| 5147 | cursor.execute(" SELECT name " |
|
| 5148 | " FROM tbl_data_sources " |
|
| 5149 | " WHERE id = %s ", (data_source_id,)) |
|
| 5150 | if cursor.fetchone() is None: |
|
| 5151 | cursor.close() |
|
| 5152 | cnx.close() |
|
| 5153 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5154 | description='API.DATA_SOURCE_NOT_FOUND') |
|
| 5155 | ||
| 5156 | query = (" SELECT id " |
|
| 5157 | " FROM tbl_photovoltaic_power_stations_data_sources " |
|
| 5158 | " WHERE photovoltaic_power_station_id = %s AND data_source_id = %s") |
|
| 5159 | cursor.execute(query, (id_, data_source_id,)) |
|
| 5160 | if cursor.fetchone() is not None: |
|
| 5161 | cursor.close() |
|
| 5162 | cnx.close() |
|
| 5163 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5164 | description='API.PHOTOVOLTAIC_POWER_STATION_DATA_SOURCE_RELATION_EXISTS') |
|
| 5165 | ||
| 5166 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_data_sources " |
|
| 5167 | " (photovoltaic_power_station_id, data_source_id) " |
|
| 5168 | " VALUES (%s, %s) ") |
|
| 5169 | cursor.execute(add_row, (id_, data_source_id,)) |
|
| 5170 | cnx.commit() |
|
| 5171 | cursor.close() |
|
| 5172 | cnx.close() |
|
| 5173 | ||
| 5174 | resp.status = falcon.HTTP_201 |
|
| 5175 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/datasources/' + str(data_source_id) |
|
| 5176 | ||
| 5177 | ||
| 5178 | class PhotovoltaicPowerStationDataSourceItem: |
|
| @@ 1964-2080 (lines=117) @@ | ||
| 1961 | resp.status = falcon.HTTP_204 |
|
| 1962 | ||
| 1963 | ||
| 1964 | class EquipmentCommandCollection: |
|
| 1965 | def __init__(self): |
|
| 1966 | pass |
|
| 1967 | ||
| 1968 | @staticmethod |
|
| 1969 | def on_options(req, resp, id_): |
|
| 1970 | _ = req |
|
| 1971 | resp.status = falcon.HTTP_200 |
|
| 1972 | _ = id_ |
|
| 1973 | ||
| 1974 | @staticmethod |
|
| 1975 | def on_get(req, resp, id_): |
|
| 1976 | if 'API-KEY' not in req.headers or \ |
|
| 1977 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1978 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1979 | access_control(req) |
|
| 1980 | else: |
|
| 1981 | api_key_control(req) |
|
| 1982 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1983 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1984 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1985 | ||
| 1986 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1987 | cursor = cnx.cursor() |
|
| 1988 | ||
| 1989 | cursor.execute(" SELECT name " |
|
| 1990 | " FROM tbl_equipments " |
|
| 1991 | " WHERE id = %s ", (id_,)) |
|
| 1992 | if cursor.fetchone() is None: |
|
| 1993 | cursor.close() |
|
| 1994 | cnx.close() |
|
| 1995 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1996 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 1997 | ||
| 1998 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1999 | " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " |
|
| 2000 | " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " |
|
| 2001 | " ORDER BY c.id ") |
|
| 2002 | cursor.execute(query, (id_,)) |
|
| 2003 | rows = cursor.fetchall() |
|
| 2004 | ||
| 2005 | result = list() |
|
| 2006 | if rows is not None and len(rows) > 0: |
|
| 2007 | for row in rows: |
|
| 2008 | meta_result = {"id": row[0], |
|
| 2009 | "name": row[1], |
|
| 2010 | "uuid": row[2]} |
|
| 2011 | result.append(meta_result) |
|
| 2012 | ||
| 2013 | resp.text = json.dumps(result) |
|
| 2014 | ||
| 2015 | @staticmethod |
|
| 2016 | @user_logger |
|
| 2017 | def on_post(req, resp, id_): |
|
| 2018 | """Handles POST requests""" |
|
| 2019 | admin_control(req) |
|
| 2020 | try: |
|
| 2021 | raw_json = req.stream.read().decode('utf-8') |
|
| 2022 | except Exception as ex: |
|
| 2023 | print(str(ex)) |
|
| 2024 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2025 | title='API.BAD_REQUEST', |
|
| 2026 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2027 | ||
| 2028 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2029 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2030 | description='API.INVALID_EQUIPMENT_ID') |
|
| 2031 | ||
| 2032 | new_values = json.loads(raw_json) |
|
| 2033 | ||
| 2034 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 2035 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 2036 | new_values['data']['command_id'] <= 0: |
|
| 2037 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2038 | description='API.INVALID_COMMAND_ID') |
|
| 2039 | command_id = new_values['data']['command_id'] |
|
| 2040 | ||
| 2041 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2042 | cursor = cnx.cursor() |
|
| 2043 | ||
| 2044 | cursor.execute(" SELECT name " |
|
| 2045 | " from tbl_equipments " |
|
| 2046 | " WHERE id = %s ", (id_,)) |
|
| 2047 | if cursor.fetchone() is None: |
|
| 2048 | cursor.close() |
|
| 2049 | cnx.close() |
|
| 2050 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2051 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 2052 | ||
| 2053 | cursor.execute(" SELECT name " |
|
| 2054 | " FROM tbl_commands " |
|
| 2055 | " WHERE id = %s ", (command_id,)) |
|
| 2056 | if cursor.fetchone() is None: |
|
| 2057 | cursor.close() |
|
| 2058 | cnx.close() |
|
| 2059 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2060 | description='API.COMMAND_NOT_FOUND') |
|
| 2061 | ||
| 2062 | query = (" SELECT id " |
|
| 2063 | " FROM tbl_equipments_commands " |
|
| 2064 | " WHERE equipment_id = %s AND command_id = %s") |
|
| 2065 | cursor.execute(query, (id_, command_id,)) |
|
| 2066 | if cursor.fetchone() is not None: |
|
| 2067 | cursor.close() |
|
| 2068 | cnx.close() |
|
| 2069 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2070 | description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
| 2071 | ||
| 2072 | add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " |
|
| 2073 | " VALUES (%s, %s) ") |
|
| 2074 | cursor.execute(add_row, (id_, command_id,)) |
|
| 2075 | cnx.commit() |
|
| 2076 | cursor.close() |
|
| 2077 | cnx.close() |
|
| 2078 | ||
| 2079 | resp.status = falcon.HTTP_201 |
|
| 2080 | resp.location = '/equipments/' + str(id_) + '/commands/' + str(command_id) |
|
| 2081 | ||
| 2082 | ||
| 2083 | class EquipmentCommandItem: |
|
| @@ 3248-3357 (lines=110) @@ | ||
| 3245 | resp.status = falcon.HTTP_201 |
|
| 3246 | resp.location = '/equipments/' + str(new_id) |
|
| 3247 | ||
| 3248 | class EquipmentDataSourceCollection: |
|
| 3249 | def __init__(self): |
|
| 3250 | pass |
|
| 3251 | ||
| 3252 | @staticmethod |
|
| 3253 | def on_options(req, resp, id_): |
|
| 3254 | _ = req |
|
| 3255 | _ = id_ |
|
| 3256 | resp.status = falcon.HTTP_200 |
|
| 3257 | ||
| 3258 | @staticmethod |
|
| 3259 | def on_get(req, resp, id_): |
|
| 3260 | if 'API-KEY' not in req.headers or \ |
|
| 3261 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3262 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3263 | access_control(req) |
|
| 3264 | else: |
|
| 3265 | api_key_control(req) |
|
| 3266 | ||
| 3267 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3268 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3269 | description='API.INVALID_EQUIPMENT_ID') |
|
| 3270 | ||
| 3271 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3272 | cursor = cnx.cursor() |
|
| 3273 | ||
| 3274 | cursor.execute(" SELECT name FROM tbl_equipments WHERE id = %s ", (id_,)) |
|
| 3275 | if cursor.fetchone() is None: |
|
| 3276 | cursor.close() |
|
| 3277 | cnx.close() |
|
| 3278 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3279 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 3280 | ||
| 3281 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
| 3282 | " FROM tbl_equipments e, tbl_equipments_data_sources eds, tbl_data_sources ds " |
|
| 3283 | " WHERE eds.equipment_id = e.id AND ds.id = eds.data_source_id AND e.id = %s " |
|
| 3284 | " ORDER BY ds.id ") |
|
| 3285 | cursor.execute(query, (id_,)) |
|
| 3286 | rows = cursor.fetchall() |
|
| 3287 | ||
| 3288 | result = list() |
|
| 3289 | if rows is not None and len(rows) > 0: |
|
| 3290 | for row in rows: |
|
| 3291 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 3292 | result.append(meta_result) |
|
| 3293 | ||
| 3294 | cursor.close() |
|
| 3295 | cnx.close() |
|
| 3296 | ||
| 3297 | resp.text = json.dumps(result) |
|
| 3298 | ||
| 3299 | @staticmethod |
|
| 3300 | @user_logger |
|
| 3301 | def on_post(req, resp, id_): |
|
| 3302 | admin_control(req) |
|
| 3303 | try: |
|
| 3304 | raw_json = req.stream.read().decode('utf-8') |
|
| 3305 | except Exception as ex: |
|
| 3306 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3307 | title='API.BAD_REQUEST', |
|
| 3308 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3309 | ||
| 3310 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3311 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3312 | description='API.INVALID_EQUIPMENT_ID') |
|
| 3313 | ||
| 3314 | new_values = json.loads(raw_json) |
|
| 3315 | ||
| 3316 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
| 3317 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
| 3318 | new_values['data']['data_source_id'] <= 0: |
|
| 3319 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3320 | description='API.INVALID_DATA_SOURCE_ID') |
|
| 3321 | ||
| 3322 | data_source_id = new_values['data']['data_source_id'] |
|
| 3323 | ||
| 3324 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3325 | cursor = cnx.cursor() |
|
| 3326 | ||
| 3327 | cursor.execute(" SELECT name FROM tbl_equipments WHERE id = %s ", (id_,)) |
|
| 3328 | if cursor.fetchone() is None: |
|
| 3329 | cursor.close() |
|
| 3330 | cnx.close() |
|
| 3331 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3332 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 3333 | ||
| 3334 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
|
| 3335 | if cursor.fetchone() is None: |
|
| 3336 | cursor.close() |
|
| 3337 | cnx.close() |
|
| 3338 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3339 | description='API.DATA_SOURCE_NOT_FOUND') |
|
| 3340 | ||
| 3341 | cursor.execute(" SELECT id " |
|
| 3342 | " FROM tbl_equipments_data_sources " |
|
| 3343 | " WHERE equipment_id = %s AND data_source_id = %s", (id_, data_source_id)) |
|
| 3344 | if cursor.fetchone() is not None: |
|
| 3345 | cursor.close() |
|
| 3346 | cnx.close() |
|
| 3347 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3348 | description='API.EQUIPMENT_DATA_SOURCE_RELATION_EXISTS') |
|
| 3349 | ||
| 3350 | cursor.execute(" INSERT INTO tbl_equipments_data_sources (equipment_id, data_source_id) " |
|
| 3351 | " VALUES (%s, %s) ", (id_, data_source_id)) |
|
| 3352 | cnx.commit() |
|
| 3353 | cursor.close() |
|
| 3354 | cnx.close() |
|
| 3355 | ||
| 3356 | resp.status = falcon.HTTP_201 |
|
| 3357 | resp.location = '/equipments/' + str(id_) + '/datasources/' + str(data_source_id) |
|
| 3358 | ||
| 3359 | class EquipmentDataSourceItem: |
|
| 3360 | def __init__(self): |
|
| @@ 1854-1970 (lines=117) @@ | ||
| 1851 | resp.status = falcon.HTTP_204 |
|
| 1852 | ||
| 1853 | ||
| 1854 | class TenantCommandCollection: |
|
| 1855 | def __init__(self): |
|
| 1856 | pass |
|
| 1857 | ||
| 1858 | @staticmethod |
|
| 1859 | def on_options(req, resp, id_): |
|
| 1860 | _ = req |
|
| 1861 | resp.status = falcon.HTTP_200 |
|
| 1862 | _ = id_ |
|
| 1863 | ||
| 1864 | @staticmethod |
|
| 1865 | def on_get(req, resp, id_): |
|
| 1866 | if 'API-KEY' not in req.headers or \ |
|
| 1867 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1868 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1869 | access_control(req) |
|
| 1870 | else: |
|
| 1871 | api_key_control(req) |
|
| 1872 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1873 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1874 | description='API.INVALID_TENANT_ID') |
|
| 1875 | ||
| 1876 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1877 | cursor = cnx.cursor() |
|
| 1878 | ||
| 1879 | cursor.execute(" SELECT name " |
|
| 1880 | " FROM tbl_tenants " |
|
| 1881 | " WHERE id = %s ", (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.TENANT_NOT_FOUND') |
|
| 1887 | ||
| 1888 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1889 | " FROM tbl_tenants t, tbl_tenants_commands tc, tbl_commands c " |
|
| 1890 | " WHERE tc.tenant_id = t.id AND c.id = tc.command_id AND t.id = %s " |
|
| 1891 | " ORDER BY c.id ") |
|
| 1892 | cursor.execute(query, (id_,)) |
|
| 1893 | rows = cursor.fetchall() |
|
| 1894 | ||
| 1895 | result = list() |
|
| 1896 | if rows is not None and len(rows) > 0: |
|
| 1897 | for row in rows: |
|
| 1898 | meta_result = {"id": row[0], |
|
| 1899 | "name": row[1], |
|
| 1900 | "uuid": row[2]} |
|
| 1901 | result.append(meta_result) |
|
| 1902 | ||
| 1903 | resp.text = json.dumps(result) |
|
| 1904 | ||
| 1905 | @staticmethod |
|
| 1906 | @user_logger |
|
| 1907 | def on_post(req, resp, id_): |
|
| 1908 | """Handles POST requests""" |
|
| 1909 | admin_control(req) |
|
| 1910 | try: |
|
| 1911 | raw_json = req.stream.read().decode('utf-8') |
|
| 1912 | except Exception as ex: |
|
| 1913 | print(str(ex)) |
|
| 1914 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1915 | title='API.BAD_REQUEST', |
|
| 1916 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1917 | ||
| 1918 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1919 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1920 | description='API.INVALID_TENANT_ID') |
|
| 1921 | ||
| 1922 | new_values = json.loads(raw_json) |
|
| 1923 | ||
| 1924 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1925 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1926 | new_values['data']['command_id'] <= 0: |
|
| 1927 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1928 | description='API.INVALID_COMMAND_ID') |
|
| 1929 | command_id = new_values['data']['command_id'] |
|
| 1930 | ||
| 1931 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1932 | cursor = cnx.cursor() |
|
| 1933 | ||
| 1934 | cursor.execute(" SELECT name " |
|
| 1935 | " from tbl_tenants " |
|
| 1936 | " WHERE id = %s ", (id_,)) |
|
| 1937 | if cursor.fetchone() is None: |
|
| 1938 | cursor.close() |
|
| 1939 | cnx.close() |
|
| 1940 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1941 | description='API.TENANT_NOT_FOUND') |
|
| 1942 | ||
| 1943 | cursor.execute(" SELECT name " |
|
| 1944 | " FROM tbl_commands " |
|
| 1945 | " WHERE id = %s ", (command_id,)) |
|
| 1946 | if cursor.fetchone() is None: |
|
| 1947 | cursor.close() |
|
| 1948 | cnx.close() |
|
| 1949 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1950 | description='API.COMMAND_NOT_FOUND') |
|
| 1951 | ||
| 1952 | query = (" SELECT id " |
|
| 1953 | " FROM tbl_tenants_commands " |
|
| 1954 | " WHERE tenant_id = %s AND command_id = %s") |
|
| 1955 | cursor.execute(query, (id_, command_id,)) |
|
| 1956 | if cursor.fetchone() is not None: |
|
| 1957 | cursor.close() |
|
| 1958 | cnx.close() |
|
| 1959 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1960 | description='API.TENANT_COMMAND_RELATION_EXISTS') |
|
| 1961 | ||
| 1962 | add_row = (" INSERT INTO tbl_tenants_commands (tenant_id, command_id) " |
|
| 1963 | " VALUES (%s, %s) ") |
|
| 1964 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1965 | cnx.commit() |
|
| 1966 | cursor.close() |
|
| 1967 | cnx.close() |
|
| 1968 | ||
| 1969 | resp.status = falcon.HTTP_201 |
|
| 1970 | resp.location = '/tenants/' + str(id_) + '/commands/' + str(command_id) |
|
| 1971 | ||
| 1972 | ||
| 1973 | class TenantCommandItem: |
|
| @@ 1673-1789 (lines=117) @@ | ||
| 1670 | resp.status = falcon.HTTP_204 |
|
| 1671 | ||
| 1672 | ||
| 1673 | class TenantWorkingCalendarCollection: |
|
| 1674 | def __init__(self): |
|
| 1675 | pass |
|
| 1676 | ||
| 1677 | @staticmethod |
|
| 1678 | def on_options(req, resp, id_): |
|
| 1679 | _ = req |
|
| 1680 | resp.status = falcon.HTTP_200 |
|
| 1681 | _ = id_ |
|
| 1682 | ||
| 1683 | @staticmethod |
|
| 1684 | def on_get(req, resp, id_): |
|
| 1685 | if 'API-KEY' not in req.headers or \ |
|
| 1686 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1687 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1688 | access_control(req) |
|
| 1689 | else: |
|
| 1690 | api_key_control(req) |
|
| 1691 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1693 | description='API.INVALID_TENANT_ID') |
|
| 1694 | ||
| 1695 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1696 | cursor = cnx.cursor() |
|
| 1697 | ||
| 1698 | cursor.execute(" SELECT name " |
|
| 1699 | " FROM tbl_tenants " |
|
| 1700 | " WHERE id = %s ", (id_,)) |
|
| 1701 | if cursor.fetchone() is None: |
|
| 1702 | cursor.close() |
|
| 1703 | cnx.close() |
|
| 1704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1705 | description='API.TENANT_NOT_FOUND') |
|
| 1706 | ||
| 1707 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1708 | " FROM tbl_tenants t, tbl_tenants_working_calendars twc, tbl_working_calendars wc " |
|
| 1709 | " WHERE twc.tenant_id = t.id AND wc.id = twc.working_calendar_id AND t.id = %s " |
|
| 1710 | " ORDER BY wc.id ") |
|
| 1711 | cursor.execute(query, (id_,)) |
|
| 1712 | rows = cursor.fetchall() |
|
| 1713 | ||
| 1714 | result = list() |
|
| 1715 | if rows is not None and len(rows) > 0: |
|
| 1716 | for row in rows: |
|
| 1717 | meta_result = {"id": row[0], |
|
| 1718 | "name": row[1], |
|
| 1719 | "description": row[2]} |
|
| 1720 | result.append(meta_result) |
|
| 1721 | ||
| 1722 | resp.text = json.dumps(result) |
|
| 1723 | ||
| 1724 | @staticmethod |
|
| 1725 | @user_logger |
|
| 1726 | def on_post(req, resp, id_): |
|
| 1727 | """Handles POST requests""" |
|
| 1728 | admin_control(req) |
|
| 1729 | try: |
|
| 1730 | raw_json = req.stream.read().decode('utf-8') |
|
| 1731 | except Exception as ex: |
|
| 1732 | print(str(ex)) |
|
| 1733 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1734 | title='API.BAD_REQUEST', |
|
| 1735 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1736 | ||
| 1737 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1738 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1739 | description='API.INVALID_TENANT_ID') |
|
| 1740 | ||
| 1741 | new_values = json.loads(raw_json) |
|
| 1742 | ||
| 1743 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1744 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1745 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1746 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1747 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1748 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1749 | ||
| 1750 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1751 | cursor = cnx.cursor() |
|
| 1752 | ||
| 1753 | cursor.execute(" SELECT name " |
|
| 1754 | " from tbl_tenants " |
|
| 1755 | " WHERE id = %s ", (id_,)) |
|
| 1756 | if cursor.fetchone() is None: |
|
| 1757 | cursor.close() |
|
| 1758 | cnx.close() |
|
| 1759 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1760 | description='API.TENANT_NOT_FOUND') |
|
| 1761 | ||
| 1762 | cursor.execute(" SELECT name " |
|
| 1763 | " FROM tbl_working_calendars " |
|
| 1764 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 1765 | if cursor.fetchone() is None: |
|
| 1766 | cursor.close() |
|
| 1767 | cnx.close() |
|
| 1768 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1769 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 1770 | ||
| 1771 | query = (" SELECT id " |
|
| 1772 | " FROM tbl_tenants_working_calendars " |
|
| 1773 | " WHERE tenant_id = %s AND working_calendar_id = %s") |
|
| 1774 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1775 | if cursor.fetchone() is not None: |
|
| 1776 | cursor.close() |
|
| 1777 | cnx.close() |
|
| 1778 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1779 | description='API.TENANT_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1780 | ||
| 1781 | add_row = (" INSERT INTO tbl_tenants_working_calendars (tenant_id, working_calendar_id) " |
|
| 1782 | " VALUES (%s, %s) ") |
|
| 1783 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1784 | cnx.commit() |
|
| 1785 | cursor.close() |
|
| 1786 | cnx.close() |
|
| 1787 | ||
| 1788 | resp.status = falcon.HTTP_201 |
|
| 1789 | resp.location = '/tenants/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1790 | ||
| 1791 | ||
| 1792 | class TenantWorkingCalendarItem: |
|
| @@ 1299-1415 (lines=117) @@ | ||
| 1296 | resp.status = falcon.HTTP_204 |
|
| 1297 | ||
| 1298 | ||
| 1299 | class TenantSensorCollection: |
|
| 1300 | def __init__(self): |
|
| 1301 | pass |
|
| 1302 | ||
| 1303 | @staticmethod |
|
| 1304 | def on_options(req, resp, id_): |
|
| 1305 | _ = req |
|
| 1306 | resp.status = falcon.HTTP_200 |
|
| 1307 | _ = id_ |
|
| 1308 | ||
| 1309 | @staticmethod |
|
| 1310 | def on_get(req, resp, id_): |
|
| 1311 | if 'API-KEY' not in req.headers or \ |
|
| 1312 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1313 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1314 | access_control(req) |
|
| 1315 | else: |
|
| 1316 | api_key_control(req) |
|
| 1317 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1318 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1319 | description='API.INVALID_TENANT_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 | query = (" SELECT s.id, s.name, s.uuid " |
|
| 1334 | " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s " |
|
| 1335 | " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
| 1336 | " ORDER BY s.id ") |
|
| 1337 | cursor.execute(query, (id_,)) |
|
| 1338 | rows = cursor.fetchall() |
|
| 1339 | ||
| 1340 | result = list() |
|
| 1341 | if rows is not None and len(rows) > 0: |
|
| 1342 | for row in rows: |
|
| 1343 | meta_result = {"id": row[0], |
|
| 1344 | "name": row[1], |
|
| 1345 | "uuid": row[2]} |
|
| 1346 | result.append(meta_result) |
|
| 1347 | ||
| 1348 | resp.text = json.dumps(result) |
|
| 1349 | ||
| 1350 | @staticmethod |
|
| 1351 | @user_logger |
|
| 1352 | def on_post(req, resp, id_): |
|
| 1353 | """Handles POST requests""" |
|
| 1354 | admin_control(req) |
|
| 1355 | try: |
|
| 1356 | raw_json = req.stream.read().decode('utf-8') |
|
| 1357 | except Exception as ex: |
|
| 1358 | print(str(ex)) |
|
| 1359 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1360 | title='API.BAD_REQUEST', |
|
| 1361 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1362 | ||
| 1363 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1364 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1365 | description='API.INVALID_TENANT_ID') |
|
| 1366 | ||
| 1367 | new_values = json.loads(raw_json) |
|
| 1368 | ||
| 1369 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1370 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1371 | new_values['data']['sensor_id'] <= 0: |
|
| 1372 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1373 | description='API.INVALID_SENSOR_ID') |
|
| 1374 | sensor_id = new_values['data']['sensor_id'] |
|
| 1375 | ||
| 1376 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1377 | cursor = cnx.cursor() |
|
| 1378 | ||
| 1379 | cursor.execute(" SELECT name " |
|
| 1380 | " from tbl_tenants " |
|
| 1381 | " WHERE id = %s ", (id_,)) |
|
| 1382 | if cursor.fetchone() is None: |
|
| 1383 | cursor.close() |
|
| 1384 | cnx.close() |
|
| 1385 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1386 | description='API.TENANT_NOT_FOUND') |
|
| 1387 | ||
| 1388 | cursor.execute(" SELECT name " |
|
| 1389 | " FROM tbl_sensors " |
|
| 1390 | " WHERE id = %s ", (sensor_id,)) |
|
| 1391 | if cursor.fetchone() is None: |
|
| 1392 | cursor.close() |
|
| 1393 | cnx.close() |
|
| 1394 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1395 | description='API.SENSOR_NOT_FOUND') |
|
| 1396 | ||
| 1397 | query = (" SELECT id " |
|
| 1398 | " FROM tbl_tenants_sensors " |
|
| 1399 | " WHERE tenant_id = %s AND sensor_id = %s") |
|
| 1400 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1401 | if cursor.fetchone() is not None: |
|
| 1402 | cursor.close() |
|
| 1403 | cnx.close() |
|
| 1404 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1405 | description='API.TENANT_SENSOR_RELATION_EXISTS') |
|
| 1406 | ||
| 1407 | add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) " |
|
| 1408 | " VALUES (%s, %s) ") |
|
| 1409 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1410 | cnx.commit() |
|
| 1411 | cursor.close() |
|
| 1412 | cnx.close() |
|
| 1413 | ||
| 1414 | resp.status = falcon.HTTP_201 |
|
| 1415 | resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1416 | ||
| 1417 | ||
| 1418 | class TenantSensorItem: |
|
| @@ 1506-1622 (lines=117) @@ | ||
| 1503 | resp.status = falcon.HTTP_204 |
|
| 1504 | ||
| 1505 | ||
| 1506 | class EnergyStorageContainerDataSourceCollection: |
|
| 1507 | def __init__(self): |
|
| 1508 | pass |
|
| 1509 | ||
| 1510 | @staticmethod |
|
| 1511 | def on_options(req, resp, id_): |
|
| 1512 | _ = req |
|
| 1513 | resp.status = falcon.HTTP_200 |
|
| 1514 | _ = id_ |
|
| 1515 | ||
| 1516 | @staticmethod |
|
| 1517 | def on_get(req, resp, id_): |
|
| 1518 | if 'API-KEY' not in req.headers or \ |
|
| 1519 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1520 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1521 | access_control(req) |
|
| 1522 | else: |
|
| 1523 | api_key_control(req) |
|
| 1524 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1525 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1526 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1527 | ||
| 1528 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1529 | cursor = cnx.cursor() |
|
| 1530 | ||
| 1531 | cursor.execute(" SELECT name " |
|
| 1532 | " FROM tbl_energy_storage_containers " |
|
| 1533 | " WHERE id = %s ", (id_,)) |
|
| 1534 | if cursor.fetchone() is None: |
|
| 1535 | cursor.close() |
|
| 1536 | cnx.close() |
|
| 1537 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1538 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1539 | ||
| 1540 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
| 1541 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_data_sources ceds, " |
|
| 1542 | " tbl_data_sources ds " |
|
| 1543 | " WHERE ceds.energy_storage_container_id = ce.id AND ds.id = ceds.data_source_id AND ce.id = %s " |
|
| 1544 | " ORDER BY ds.id ") |
|
| 1545 | cursor.execute(query, (id_,)) |
|
| 1546 | rows = cursor.fetchall() |
|
| 1547 | ||
| 1548 | result = list() |
|
| 1549 | if rows is not None and len(rows) > 0: |
|
| 1550 | for row in rows: |
|
| 1551 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1552 | result.append(meta_result) |
|
| 1553 | ||
| 1554 | resp.text = json.dumps(result) |
|
| 1555 | ||
| 1556 | @staticmethod |
|
| 1557 | @user_logger |
|
| 1558 | def on_post(req, resp, id_): |
|
| 1559 | """Handles POST requests""" |
|
| 1560 | admin_control(req) |
|
| 1561 | try: |
|
| 1562 | raw_json = req.stream.read().decode('utf-8') |
|
| 1563 | except Exception as ex: |
|
| 1564 | print(str(ex)) |
|
| 1565 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1566 | title='API.BAD_REQUEST', |
|
| 1567 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1568 | ||
| 1569 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1570 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1571 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1572 | ||
| 1573 | new_values = json.loads(raw_json) |
|
| 1574 | ||
| 1575 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
| 1576 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
| 1577 | new_values['data']['data_source_id'] <= 0: |
|
| 1578 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1579 | description='API.INVALID_DATA_SOURCE_ID') |
|
| 1580 | data_source_id = new_values['data']['data_source_id'] |
|
| 1581 | ||
| 1582 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1583 | cursor = cnx.cursor() |
|
| 1584 | ||
| 1585 | cursor.execute(" SELECT name " |
|
| 1586 | " from tbl_energy_storage_containers " |
|
| 1587 | " WHERE id = %s ", (id_,)) |
|
| 1588 | if cursor.fetchone() is None: |
|
| 1589 | cursor.close() |
|
| 1590 | cnx.close() |
|
| 1591 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1592 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1593 | ||
| 1594 | cursor.execute(" SELECT name " |
|
| 1595 | " FROM tbl_data_sources " |
|
| 1596 | " WHERE id = %s ", (data_source_id,)) |
|
| 1597 | if cursor.fetchone() is None: |
|
| 1598 | cursor.close() |
|
| 1599 | cnx.close() |
|
| 1600 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1601 | description='API.DATA_SOURCE_NOT_FOUND') |
|
| 1602 | ||
| 1603 | query = (" SELECT id " |
|
| 1604 | " FROM tbl_energy_storage_containers_data_sources " |
|
| 1605 | " WHERE energy_storage_container_id = %s AND data_source_id = %s") |
|
| 1606 | cursor.execute(query, (id_, data_source_id,)) |
|
| 1607 | if cursor.fetchone() is not None: |
|
| 1608 | cursor.close() |
|
| 1609 | cnx.close() |
|
| 1610 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1611 | description='API.ENERGY_STORAGE_CONTAINER_DATA_SOURCE_RELATION_EXISTS') |
|
| 1612 | ||
| 1613 | add_row = (" INSERT INTO " |
|
| 1614 | " tbl_energy_storage_containers_data_sources (energy_storage_container_id, data_source_id) " |
|
| 1615 | " VALUES (%s, %s) ") |
|
| 1616 | cursor.execute(add_row, (id_, data_source_id,)) |
|
| 1617 | cnx.commit() |
|
| 1618 | cursor.close() |
|
| 1619 | cnx.close() |
|
| 1620 | ||
| 1621 | resp.status = falcon.HTTP_201 |
|
| 1622 | resp.location = '/energystoragecontainers/' + str(id_) + '/datasources/' + str(data_source_id) |
|
| 1623 | ||
| 1624 | ||
| 1625 | class EnergyStorageContainerDataSourceItem: |
|
| @@ 1327-1441 (lines=115) @@ | ||
| 1324 | resp.status = falcon.HTTP_204 |
|
| 1325 | ||
| 1326 | ||
| 1327 | class EnergyStorageContainerCommandCollection: |
|
| 1328 | def __init__(self): |
|
| 1329 | pass |
|
| 1330 | ||
| 1331 | @staticmethod |
|
| 1332 | def on_options(req, resp, id_): |
|
| 1333 | _ = req |
|
| 1334 | resp.status = falcon.HTTP_200 |
|
| 1335 | _ = id_ |
|
| 1336 | ||
| 1337 | @staticmethod |
|
| 1338 | def on_get(req, resp, id_): |
|
| 1339 | if 'API-KEY' not in req.headers or \ |
|
| 1340 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1341 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1342 | access_control(req) |
|
| 1343 | else: |
|
| 1344 | api_key_control(req) |
|
| 1345 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1347 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1348 | ||
| 1349 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1350 | cursor = cnx.cursor() |
|
| 1351 | ||
| 1352 | cursor.execute(" SELECT name " |
|
| 1353 | " FROM tbl_energy_storage_containers " |
|
| 1354 | " WHERE id = %s ", (id_,)) |
|
| 1355 | if cursor.fetchone() is None: |
|
| 1356 | cursor.close() |
|
| 1357 | cnx.close() |
|
| 1358 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1359 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1360 | ||
| 1361 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1362 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_commands cec, tbl_commands c " |
|
| 1363 | " WHERE cec.energy_storage_container_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
| 1364 | " ORDER BY c.id ") |
|
| 1365 | cursor.execute(query, (id_,)) |
|
| 1366 | rows = cursor.fetchall() |
|
| 1367 | ||
| 1368 | result = list() |
|
| 1369 | if rows is not None and len(rows) > 0: |
|
| 1370 | for row in rows: |
|
| 1371 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1372 | result.append(meta_result) |
|
| 1373 | ||
| 1374 | resp.text = json.dumps(result) |
|
| 1375 | ||
| 1376 | @staticmethod |
|
| 1377 | @user_logger |
|
| 1378 | def on_post(req, resp, id_): |
|
| 1379 | """Handles POST requests""" |
|
| 1380 | admin_control(req) |
|
| 1381 | try: |
|
| 1382 | raw_json = req.stream.read().decode('utf-8') |
|
| 1383 | except Exception as ex: |
|
| 1384 | print(str(ex)) |
|
| 1385 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1386 | title='API.BAD_REQUEST', |
|
| 1387 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1388 | ||
| 1389 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1390 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1391 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1392 | ||
| 1393 | new_values = json.loads(raw_json) |
|
| 1394 | ||
| 1395 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1396 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1397 | new_values['data']['command_id'] <= 0: |
|
| 1398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1399 | description='API.INVALID_COMMAND_ID') |
|
| 1400 | command_id = new_values['data']['command_id'] |
|
| 1401 | ||
| 1402 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1403 | cursor = cnx.cursor() |
|
| 1404 | ||
| 1405 | cursor.execute(" SELECT name " |
|
| 1406 | " from tbl_energy_storage_containers " |
|
| 1407 | " WHERE id = %s ", (id_,)) |
|
| 1408 | if cursor.fetchone() is None: |
|
| 1409 | cursor.close() |
|
| 1410 | cnx.close() |
|
| 1411 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1412 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 1413 | ||
| 1414 | cursor.execute(" SELECT name " |
|
| 1415 | " FROM tbl_commands " |
|
| 1416 | " WHERE id = %s ", (command_id,)) |
|
| 1417 | if cursor.fetchone() is None: |
|
| 1418 | cursor.close() |
|
| 1419 | cnx.close() |
|
| 1420 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1421 | description='API.COMMAND_NOT_FOUND') |
|
| 1422 | ||
| 1423 | query = (" SELECT id " |
|
| 1424 | " FROM tbl_energy_storage_containers_commands " |
|
| 1425 | " WHERE energy_storage_container_id = %s AND command_id = %s") |
|
| 1426 | cursor.execute(query, (id_, command_id,)) |
|
| 1427 | if cursor.fetchone() is not None: |
|
| 1428 | cursor.close() |
|
| 1429 | cnx.close() |
|
| 1430 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1431 | description='API.ENERGY_STORAGE_CONTAINER_COMMAND_RELATION_EXISTS') |
|
| 1432 | ||
| 1433 | add_row = (" INSERT INTO tbl_energy_storage_containers_commands (energy_storage_container_id, command_id) " |
|
| 1434 | " VALUES (%s, %s) ") |
|
| 1435 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1436 | cnx.commit() |
|
| 1437 | cursor.close() |
|
| 1438 | cnx.close() |
|
| 1439 | ||
| 1440 | resp.status = falcon.HTTP_201 |
|
| 1441 | resp.location = '/energystoragecontainers/' + str(id_) + '/commands/' + str(command_id) |
|
| 1442 | ||
| 1443 | ||
| 1444 | class EnergyStorageContainerCommandItem: |
|
| @@ 650-766 (lines=117) @@ | ||
| 647 | resp.location = '/combinedequipments/' + str(new_id) |
|
| 648 | ||
| 649 | ||
| 650 | class CombinedEquipmentEquipmentCollection: |
|
| 651 | def __init__(self): |
|
| 652 | pass |
|
| 653 | ||
| 654 | @staticmethod |
|
| 655 | def on_options(req, resp, id_): |
|
| 656 | _ = req |
|
| 657 | resp.status = falcon.HTTP_200 |
|
| 658 | _ = id_ |
|
| 659 | ||
| 660 | @staticmethod |
|
| 661 | def on_get(req, resp, id_): |
|
| 662 | if 'API-KEY' not in req.headers or \ |
|
| 663 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 664 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 665 | access_control(req) |
|
| 666 | else: |
|
| 667 | api_key_control(req) |
|
| 668 | if not id_.isdigit() or int(id_) <= 0: |
|
| 669 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 670 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 671 | ||
| 672 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 673 | cursor = cnx.cursor() |
|
| 674 | ||
| 675 | cursor.execute(" SELECT name " |
|
| 676 | " FROM tbl_combined_equipments " |
|
| 677 | " WHERE id = %s ", (id_,)) |
|
| 678 | if cursor.fetchone() is None: |
|
| 679 | cursor.close() |
|
| 680 | cnx.close() |
|
| 681 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 682 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 683 | ||
| 684 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 685 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
|
| 686 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
|
| 687 | " ORDER BY e.id ") |
|
| 688 | cursor.execute(query, (id_,)) |
|
| 689 | rows = cursor.fetchall() |
|
| 690 | cursor.close() |
|
| 691 | cnx.close() |
|
| 692 | ||
| 693 | result = list() |
|
| 694 | if rows is not None and len(rows) > 0: |
|
| 695 | for row in rows: |
|
| 696 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 697 | result.append(meta_result) |
|
| 698 | ||
| 699 | resp.text = json.dumps(result) |
|
| 700 | ||
| 701 | @staticmethod |
|
| 702 | @user_logger |
|
| 703 | def on_post(req, resp, id_): |
|
| 704 | """Handles POST requests""" |
|
| 705 | admin_control(req) |
|
| 706 | try: |
|
| 707 | raw_json = req.stream.read().decode('utf-8') |
|
| 708 | except Exception as ex: |
|
| 709 | print(ex) |
|
| 710 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 711 | title='API.BAD_REQUEST', |
|
| 712 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 713 | ||
| 714 | if not id_.isdigit() or int(id_) <= 0: |
|
| 715 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 716 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 717 | ||
| 718 | new_values = json.loads(raw_json) |
|
| 719 | ||
| 720 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 721 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 722 | new_values['data']['equipment_id'] <= 0: |
|
| 723 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 724 | description='API.INVALID_EQUIPMENT_ID') |
|
| 725 | equipment_id = new_values['data']['equipment_id'] |
|
| 726 | ||
| 727 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 728 | cursor = cnx.cursor() |
|
| 729 | ||
| 730 | cursor.execute(" SELECT name " |
|
| 731 | " from tbl_combined_equipments " |
|
| 732 | " WHERE id = %s ", (id_,)) |
|
| 733 | if cursor.fetchone() is None: |
|
| 734 | cursor.close() |
|
| 735 | cnx.close() |
|
| 736 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 737 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 738 | ||
| 739 | cursor.execute(" SELECT name " |
|
| 740 | " FROM tbl_equipments " |
|
| 741 | " WHERE id = %s ", (equipment_id,)) |
|
| 742 | if cursor.fetchone() is None: |
|
| 743 | cursor.close() |
|
| 744 | cnx.close() |
|
| 745 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 746 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 747 | ||
| 748 | query = (" SELECT id " |
|
| 749 | " FROM tbl_combined_equipments_equipments " |
|
| 750 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
|
| 751 | cursor.execute(query, (id_, equipment_id,)) |
|
| 752 | if cursor.fetchone() is not None: |
|
| 753 | cursor.close() |
|
| 754 | cnx.close() |
|
| 755 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 756 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
|
| 757 | ||
| 758 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
|
| 759 | " VALUES (%s, %s) ") |
|
| 760 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 761 | cnx.commit() |
|
| 762 | cursor.close() |
|
| 763 | cnx.close() |
|
| 764 | ||
| 765 | resp.status = falcon.HTTP_201 |
|
| 766 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 767 | ||
| 768 | ||
| 769 | class CombinedEquipmentEquipmentItem: |
|
| @@ 2460-2574 (lines=115) @@ | ||
| 2457 | resp.status = falcon.HTTP_204 |
|
| 2458 | ||
| 2459 | ||
| 2460 | class CombinedEquipmentCommandCollection: |
|
| 2461 | def __init__(self): |
|
| 2462 | pass |
|
| 2463 | ||
| 2464 | @staticmethod |
|
| 2465 | def on_options(req, resp, id_): |
|
| 2466 | _ = req |
|
| 2467 | resp.status = falcon.HTTP_200 |
|
| 2468 | _ = id_ |
|
| 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_COMBINED_EQUIPMENT_ID') |
|
| 2481 | ||
| 2482 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2483 | cursor = cnx.cursor() |
|
| 2484 | ||
| 2485 | cursor.execute(" SELECT name " |
|
| 2486 | " FROM tbl_combined_equipments " |
|
| 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.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 2493 | ||
| 2494 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 2495 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
|
| 2496 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
|
| 2497 | " ORDER BY c.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 | print(ex) |
|
| 2518 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2519 | title='API.BAD_REQUEST', |
|
| 2520 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2521 | ||
| 2522 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2523 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2524 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 2525 | ||
| 2526 | new_values = json.loads(raw_json) |
|
| 2527 | ||
| 2528 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 2529 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 2530 | new_values['data']['command_id'] <= 0: |
|
| 2531 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2532 | description='API.INVALID_COMMAND_ID') |
|
| 2533 | command_id = new_values['data']['command_id'] |
|
| 2534 | ||
| 2535 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2536 | cursor = cnx.cursor() |
|
| 2537 | ||
| 2538 | cursor.execute(" SELECT name " |
|
| 2539 | " from tbl_combined_equipments " |
|
| 2540 | " WHERE id = %s ", (id_,)) |
|
| 2541 | if cursor.fetchone() is None: |
|
| 2542 | cursor.close() |
|
| 2543 | cnx.close() |
|
| 2544 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2545 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 2546 | ||
| 2547 | cursor.execute(" SELECT name " |
|
| 2548 | " FROM tbl_commands " |
|
| 2549 | " WHERE id = %s ", (command_id,)) |
|
| 2550 | if cursor.fetchone() is None: |
|
| 2551 | cursor.close() |
|
| 2552 | cnx.close() |
|
| 2553 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2554 | description='API.COMMAND_NOT_FOUND') |
|
| 2555 | ||
| 2556 | query = (" SELECT id " |
|
| 2557 | " FROM tbl_combined_equipments_commands " |
|
| 2558 | " WHERE combined_equipment_id = %s AND command_id = %s") |
|
| 2559 | cursor.execute(query, (id_, command_id,)) |
|
| 2560 | if cursor.fetchone() is not None: |
|
| 2561 | cursor.close() |
|
| 2562 | cnx.close() |
|
| 2563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2564 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
|
| 2565 | ||
| 2566 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
|
| 2567 | " VALUES (%s, %s) ") |
|
| 2568 | cursor.execute(add_row, (id_, command_id,)) |
|
| 2569 | cnx.commit() |
|
| 2570 | cursor.close() |
|
| 2571 | cnx.close() |
|
| 2572 | ||
| 2573 | resp.status = falcon.HTTP_201 |
|
| 2574 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
|
| 2575 | ||
| 2576 | ||
| 2577 | class CombinedEquipmentCommandItem: |
|
| @@ 1629-1738 (lines=110) @@ | ||
| 1626 | resp.status = falcon.HTTP_200 |
|
| 1627 | ||
| 1628 | ||
| 1629 | class CombinedEquipmentDataSourceCollection: |
|
| 1630 | def __init__(self): |
|
| 1631 | pass |
|
| 1632 | ||
| 1633 | @staticmethod |
|
| 1634 | def on_options(req, resp, id_): |
|
| 1635 | _ = req |
|
| 1636 | resp.status = falcon.HTTP_200 |
|
| 1637 | _ = id_ |
|
| 1638 | ||
| 1639 | @staticmethod |
|
| 1640 | def on_get(req, resp, id_): |
|
| 1641 | if 'API-KEY' not in req.headers or \ |
|
| 1642 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1643 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1644 | access_control(req) |
|
| 1645 | else: |
|
| 1646 | api_key_control(req) |
|
| 1647 | ||
| 1648 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1650 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 1651 | ||
| 1652 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1653 | cursor = cnx.cursor() |
|
| 1654 | ||
| 1655 | cursor.execute(" SELECT name FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
|
| 1656 | if cursor.fetchone() is None: |
|
| 1657 | cursor.close() |
|
| 1658 | cnx.close() |
|
| 1659 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1660 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 1661 | ||
| 1662 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
| 1663 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_data_sources ceds, tbl_data_sources ds " |
|
| 1664 | " WHERE ceds.combined_equipment_id = ce.id AND ds.id = ceds.data_source_id AND ce.id = %s " |
|
| 1665 | " ORDER BY ds.id ") |
|
| 1666 | cursor.execute(query, (id_,)) |
|
| 1667 | rows = cursor.fetchall() |
|
| 1668 | ||
| 1669 | result = list() |
|
| 1670 | if rows is not None and len(rows) > 0: |
|
| 1671 | for row in rows: |
|
| 1672 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1673 | result.append(meta_result) |
|
| 1674 | ||
| 1675 | cursor.close() |
|
| 1676 | cnx.close() |
|
| 1677 | ||
| 1678 | resp.text = json.dumps(result) |
|
| 1679 | ||
| 1680 | @staticmethod |
|
| 1681 | @user_logger |
|
| 1682 | def on_post(req, resp, id_): |
|
| 1683 | admin_control(req) |
|
| 1684 | try: |
|
| 1685 | raw_json = req.stream.read().decode('utf-8') |
|
| 1686 | except Exception as ex: |
|
| 1687 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1688 | title='API.BAD_REQUEST', |
|
| 1689 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1690 | ||
| 1691 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1693 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 1694 | ||
| 1695 | new_values = json.loads(raw_json) |
|
| 1696 | ||
| 1697 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
| 1698 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
| 1699 | new_values['data']['data_source_id'] <= 0: |
|
| 1700 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1701 | description='API.INVALID_DATA_SOURCE_ID') |
|
| 1702 | ||
| 1703 | data_source_id = new_values['data']['data_source_id'] |
|
| 1704 | ||
| 1705 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1706 | cursor = cnx.cursor() |
|
| 1707 | ||
| 1708 | cursor.execute(" SELECT name FROM tbl_combined_equipments 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.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 1714 | ||
| 1715 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
|
| 1716 | if cursor.fetchone() is None: |
|
| 1717 | cursor.close() |
|
| 1718 | cnx.close() |
|
| 1719 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1720 | description='API.DATA_SOURCE_NOT_FOUND') |
|
| 1721 | ||
| 1722 | cursor.execute(" SELECT id " |
|
| 1723 | " FROM tbl_combined_equipments_data_sources " |
|
| 1724 | " WHERE combined_equipment_id = %s AND data_source_id = %s", (id_, data_source_id)) |
|
| 1725 | if cursor.fetchone() is not None: |
|
| 1726 | cursor.close() |
|
| 1727 | cnx.close() |
|
| 1728 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1729 | description='API.COMBINED_EQUIPMENT_DATA_SOURCE_RELATION_EXISTS') |
|
| 1730 | ||
| 1731 | cursor.execute(" INSERT INTO tbl_combined_equipments_data_sources (combined_equipment_id, data_source_id) " |
|
| 1732 | " VALUES (%s, %s) ", (id_, data_source_id)) |
|
| 1733 | cnx.commit() |
|
| 1734 | cursor.close() |
|
| 1735 | cnx.close() |
|
| 1736 | ||
| 1737 | resp.status = falcon.HTTP_201 |
|
| 1738 | resp.location = '/combinedequipments/' + str(id_) + '/datasources/' + str(data_source_id) |
|
| 1739 | ||
| 1740 | ||
| 1741 | class CombinedEquipmentDataSourceItem: |
|
| @@ 2893-3007 (lines=115) @@ | ||
| 2890 | resp.status = falcon.HTTP_204 |
|
| 2891 | ||
| 2892 | ||
| 2893 | class SpaceTenantCollection: |
|
| 2894 | def __init__(self): |
|
| 2895 | pass |
|
| 2896 | ||
| 2897 | @staticmethod |
|
| 2898 | def on_options(req, resp, id_): |
|
| 2899 | _ = req |
|
| 2900 | resp.status = falcon.HTTP_200 |
|
| 2901 | _ = id_ |
|
| 2902 | ||
| 2903 | @staticmethod |
|
| 2904 | def on_get(req, resp, id_): |
|
| 2905 | if 'API-KEY' not in req.headers or \ |
|
| 2906 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2907 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2908 | access_control(req) |
|
| 2909 | else: |
|
| 2910 | api_key_control(req) |
|
| 2911 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2912 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2913 | description='API.INVALID_SPACE_ID') |
|
| 2914 | ||
| 2915 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2916 | cursor = cnx.cursor() |
|
| 2917 | ||
| 2918 | cursor.execute(" SELECT name " |
|
| 2919 | " FROM tbl_spaces " |
|
| 2920 | " WHERE id = %s ", (id_,)) |
|
| 2921 | if cursor.fetchone() is None: |
|
| 2922 | cursor.close() |
|
| 2923 | cnx.close() |
|
| 2924 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2925 | description='API.SPACE_NOT_FOUND') |
|
| 2926 | ||
| 2927 | query = (" SELECT t.id, t.name, t.uuid " |
|
| 2928 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
|
| 2929 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
|
| 2930 | " ORDER BY t.id ") |
|
| 2931 | cursor.execute(query, (id_,)) |
|
| 2932 | rows = cursor.fetchall() |
|
| 2933 | ||
| 2934 | result = list() |
|
| 2935 | if rows is not None and len(rows) > 0: |
|
| 2936 | for row in rows: |
|
| 2937 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2938 | result.append(meta_result) |
|
| 2939 | ||
| 2940 | resp.text = json.dumps(result) |
|
| 2941 | ||
| 2942 | @staticmethod |
|
| 2943 | @user_logger |
|
| 2944 | def on_post(req, resp, id_): |
|
| 2945 | """Handles POST requests""" |
|
| 2946 | admin_control(req) |
|
| 2947 | try: |
|
| 2948 | raw_json = req.stream.read().decode('utf-8') |
|
| 2949 | except Exception as ex: |
|
| 2950 | print(str(ex)) |
|
| 2951 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2952 | title='API.BAD_REQUEST', |
|
| 2953 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2954 | ||
| 2955 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2956 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2957 | description='API.INVALID_SPACE_ID') |
|
| 2958 | ||
| 2959 | new_values = json.loads(raw_json) |
|
| 2960 | ||
| 2961 | if 'tenant_id' not in new_values['data'].keys() or \ |
|
| 2962 | not isinstance(new_values['data']['tenant_id'], int) or \ |
|
| 2963 | new_values['data']['tenant_id'] <= 0: |
|
| 2964 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2965 | description='API.INVALID_TENANT_ID') |
|
| 2966 | tenant_id = new_values['data']['tenant_id'] |
|
| 2967 | ||
| 2968 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2969 | cursor = cnx.cursor() |
|
| 2970 | ||
| 2971 | cursor.execute(" SELECT name " |
|
| 2972 | " from tbl_spaces " |
|
| 2973 | " WHERE id = %s ", (id_,)) |
|
| 2974 | if cursor.fetchone() is None: |
|
| 2975 | cursor.close() |
|
| 2976 | cnx.close() |
|
| 2977 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2978 | description='API.SPACE_NOT_FOUND') |
|
| 2979 | ||
| 2980 | cursor.execute(" SELECT name " |
|
| 2981 | " FROM tbl_tenants " |
|
| 2982 | " WHERE id = %s ", (tenant_id,)) |
|
| 2983 | if cursor.fetchone() is None: |
|
| 2984 | cursor.close() |
|
| 2985 | cnx.close() |
|
| 2986 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2987 | description='API.TENANT_NOT_FOUND') |
|
| 2988 | ||
| 2989 | query = (" SELECT id " |
|
| 2990 | " FROM tbl_spaces_tenants " |
|
| 2991 | " WHERE space_id = %s AND tenant_id = %s") |
|
| 2992 | cursor.execute(query, (id_, tenant_id,)) |
|
| 2993 | if cursor.fetchone() is not None: |
|
| 2994 | cursor.close() |
|
| 2995 | cnx.close() |
|
| 2996 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2997 | description='API.SPACE_TENANT_RELATION_EXISTS') |
|
| 2998 | ||
| 2999 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
|
| 3000 | " VALUES (%s, %s) ") |
|
| 3001 | cursor.execute(add_row, (id_, tenant_id,)) |
|
| 3002 | cnx.commit() |
|
| 3003 | cursor.close() |
|
| 3004 | cnx.close() |
|
| 3005 | ||
| 3006 | resp.status = falcon.HTTP_201 |
|
| 3007 | resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id) |
|
| 3008 | ||
| 3009 | ||
| 3010 | class SpaceTenantItem: |
|
| @@ 2715-2829 (lines=115) @@ | ||
| 2712 | resp.status = falcon.HTTP_204 |
|
| 2713 | ||
| 2714 | ||
| 2715 | class SpaceStoreCollection: |
|
| 2716 | def __init__(self): |
|
| 2717 | pass |
|
| 2718 | ||
| 2719 | @staticmethod |
|
| 2720 | def on_options(req, resp, id_): |
|
| 2721 | _ = req |
|
| 2722 | resp.status = falcon.HTTP_200 |
|
| 2723 | _ = id_ |
|
| 2724 | ||
| 2725 | @staticmethod |
|
| 2726 | def on_get(req, resp, id_): |
|
| 2727 | if 'API-KEY' not in req.headers or \ |
|
| 2728 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2729 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2730 | access_control(req) |
|
| 2731 | else: |
|
| 2732 | api_key_control(req) |
|
| 2733 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2734 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2735 | description='API.INVALID_SPACE_ID') |
|
| 2736 | ||
| 2737 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2738 | cursor = cnx.cursor() |
|
| 2739 | ||
| 2740 | cursor.execute(" SELECT name " |
|
| 2741 | " FROM tbl_spaces " |
|
| 2742 | " WHERE id = %s ", (id_,)) |
|
| 2743 | if cursor.fetchone() is None: |
|
| 2744 | cursor.close() |
|
| 2745 | cnx.close() |
|
| 2746 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2747 | description='API.SPACE_NOT_FOUND') |
|
| 2748 | ||
| 2749 | query = (" SELECT t.id, t.name, t.uuid " |
|
| 2750 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
|
| 2751 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
|
| 2752 | " ORDER BY t.id ") |
|
| 2753 | cursor.execute(query, (id_,)) |
|
| 2754 | rows = cursor.fetchall() |
|
| 2755 | ||
| 2756 | result = list() |
|
| 2757 | if rows is not None and len(rows) > 0: |
|
| 2758 | for row in rows: |
|
| 2759 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2760 | result.append(meta_result) |
|
| 2761 | ||
| 2762 | resp.text = json.dumps(result) |
|
| 2763 | ||
| 2764 | @staticmethod |
|
| 2765 | @user_logger |
|
| 2766 | def on_post(req, resp, id_): |
|
| 2767 | """Handles POST requests""" |
|
| 2768 | admin_control(req) |
|
| 2769 | try: |
|
| 2770 | raw_json = req.stream.read().decode('utf-8') |
|
| 2771 | except Exception as ex: |
|
| 2772 | print(str(ex)) |
|
| 2773 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2774 | title='API.BAD_REQUEST', |
|
| 2775 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2776 | ||
| 2777 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2778 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2779 | description='API.INVALID_SPACE_ID') |
|
| 2780 | ||
| 2781 | new_values = json.loads(raw_json) |
|
| 2782 | ||
| 2783 | if 'store_id' not in new_values['data'].keys() or \ |
|
| 2784 | not isinstance(new_values['data']['store_id'], int) or \ |
|
| 2785 | new_values['data']['store_id'] <= 0: |
|
| 2786 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2787 | description='API.INVALID_STORE_ID') |
|
| 2788 | store_id = new_values['data']['store_id'] |
|
| 2789 | ||
| 2790 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2791 | cursor = cnx.cursor() |
|
| 2792 | ||
| 2793 | cursor.execute(" SELECT name " |
|
| 2794 | " from tbl_spaces " |
|
| 2795 | " WHERE id = %s ", (id_,)) |
|
| 2796 | if cursor.fetchone() is None: |
|
| 2797 | cursor.close() |
|
| 2798 | cnx.close() |
|
| 2799 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2800 | description='API.SPACE_NOT_FOUND') |
|
| 2801 | ||
| 2802 | cursor.execute(" SELECT name " |
|
| 2803 | " FROM tbl_stores " |
|
| 2804 | " WHERE id = %s ", (store_id,)) |
|
| 2805 | if cursor.fetchone() is None: |
|
| 2806 | cursor.close() |
|
| 2807 | cnx.close() |
|
| 2808 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2809 | description='API.STORE_NOT_FOUND') |
|
| 2810 | ||
| 2811 | query = (" SELECT id " |
|
| 2812 | " FROM tbl_spaces_stores " |
|
| 2813 | " WHERE space_id = %s AND store_id = %s") |
|
| 2814 | cursor.execute(query, (id_, store_id,)) |
|
| 2815 | if cursor.fetchone() is not None: |
|
| 2816 | cursor.close() |
|
| 2817 | cnx.close() |
|
| 2818 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2819 | description='API.SPACE_STORE_RELATION_EXISTS') |
|
| 2820 | ||
| 2821 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
|
| 2822 | " VALUES (%s, %s) ") |
|
| 2823 | cursor.execute(add_row, (id_, store_id,)) |
|
| 2824 | cnx.commit() |
|
| 2825 | cursor.close() |
|
| 2826 | cnx.close() |
|
| 2827 | ||
| 2828 | resp.status = falcon.HTTP_201 |
|
| 2829 | resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id) |
|
| 2830 | ||
| 2831 | ||
| 2832 | class SpaceStoreItem: |
|
| @@ 2537-2651 (lines=115) @@ | ||
| 2534 | resp.status = falcon.HTTP_204 |
|
| 2535 | ||
| 2536 | ||
| 2537 | class SpaceShopfloorCollection: |
|
| 2538 | def __init__(self): |
|
| 2539 | pass |
|
| 2540 | ||
| 2541 | @staticmethod |
|
| 2542 | def on_options(req, resp, id_): |
|
| 2543 | _ = req |
|
| 2544 | resp.status = falcon.HTTP_200 |
|
| 2545 | _ = id_ |
|
| 2546 | ||
| 2547 | @staticmethod |
|
| 2548 | def on_get(req, resp, id_): |
|
| 2549 | if 'API-KEY' not in req.headers or \ |
|
| 2550 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2551 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2552 | access_control(req) |
|
| 2553 | else: |
|
| 2554 | api_key_control(req) |
|
| 2555 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2556 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2557 | description='API.INVALID_SPACE_ID') |
|
| 2558 | ||
| 2559 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2560 | cursor = cnx.cursor() |
|
| 2561 | ||
| 2562 | cursor.execute(" SELECT name " |
|
| 2563 | " FROM tbl_spaces " |
|
| 2564 | " WHERE id = %s ", (id_,)) |
|
| 2565 | if cursor.fetchone() is None: |
|
| 2566 | cursor.close() |
|
| 2567 | cnx.close() |
|
| 2568 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2569 | description='API.SPACE_NOT_FOUND') |
|
| 2570 | ||
| 2571 | query = (" SELECT sf.id, sf.name, sf.uuid " |
|
| 2572 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
|
| 2573 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
|
| 2574 | " ORDER BY sf.id ") |
|
| 2575 | cursor.execute(query, (id_,)) |
|
| 2576 | rows = cursor.fetchall() |
|
| 2577 | ||
| 2578 | result = list() |
|
| 2579 | if rows is not None and len(rows) > 0: |
|
| 2580 | for row in rows: |
|
| 2581 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2582 | result.append(meta_result) |
|
| 2583 | ||
| 2584 | resp.text = json.dumps(result) |
|
| 2585 | ||
| 2586 | @staticmethod |
|
| 2587 | @user_logger |
|
| 2588 | def on_post(req, resp, id_): |
|
| 2589 | """Handles POST requests""" |
|
| 2590 | admin_control(req) |
|
| 2591 | try: |
|
| 2592 | raw_json = req.stream.read().decode('utf-8') |
|
| 2593 | except Exception as ex: |
|
| 2594 | print(str(ex)) |
|
| 2595 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2596 | title='API.BAD_REQUEST', |
|
| 2597 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2598 | ||
| 2599 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2601 | description='API.INVALID_SPACE_ID') |
|
| 2602 | ||
| 2603 | new_values = json.loads(raw_json) |
|
| 2604 | ||
| 2605 | if 'shopfloor_id' not in new_values['data'].keys() or \ |
|
| 2606 | not isinstance(new_values['data']['shopfloor_id'], int) or \ |
|
| 2607 | new_values['data']['shopfloor_id'] <= 0: |
|
| 2608 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2609 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 2610 | shopfloor_id = new_values['data']['shopfloor_id'] |
|
| 2611 | ||
| 2612 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2613 | cursor = cnx.cursor() |
|
| 2614 | ||
| 2615 | cursor.execute(" SELECT name " |
|
| 2616 | " from tbl_spaces " |
|
| 2617 | " WHERE id = %s ", (id_,)) |
|
| 2618 | if cursor.fetchone() is None: |
|
| 2619 | cursor.close() |
|
| 2620 | cnx.close() |
|
| 2621 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2622 | description='API.SPACE_NOT_FOUND') |
|
| 2623 | ||
| 2624 | cursor.execute(" SELECT name " |
|
| 2625 | " FROM tbl_shopfloors " |
|
| 2626 | " WHERE id = %s ", (shopfloor_id,)) |
|
| 2627 | if cursor.fetchone() is None: |
|
| 2628 | cursor.close() |
|
| 2629 | cnx.close() |
|
| 2630 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2631 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 2632 | ||
| 2633 | query = (" SELECT id " |
|
| 2634 | " FROM tbl_spaces_shopfloors " |
|
| 2635 | " WHERE space_id = %s AND shopfloor_id = %s") |
|
| 2636 | cursor.execute(query, (id_, shopfloor_id,)) |
|
| 2637 | if cursor.fetchone() is not None: |
|
| 2638 | cursor.close() |
|
| 2639 | cnx.close() |
|
| 2640 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2641 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
|
| 2642 | ||
| 2643 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
|
| 2644 | " VALUES (%s, %s) ") |
|
| 2645 | cursor.execute(add_row, (id_, shopfloor_id,)) |
|
| 2646 | cnx.commit() |
|
| 2647 | cursor.close() |
|
| 2648 | cnx.close() |
|
| 2649 | ||
| 2650 | resp.status = falcon.HTTP_201 |
|
| 2651 | resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id) |
|
| 2652 | ||
| 2653 | ||
| 2654 | class SpaceShopfloorItem: |
|
| @@ 2359-2473 (lines=115) @@ | ||
| 2356 | resp.status = falcon.HTTP_204 |
|
| 2357 | ||
| 2358 | ||
| 2359 | class SpaceSensorCollection: |
|
| 2360 | def __init__(self): |
|
| 2361 | pass |
|
| 2362 | ||
| 2363 | @staticmethod |
|
| 2364 | def on_options(req, resp, id_): |
|
| 2365 | _ = req |
|
| 2366 | resp.status = falcon.HTTP_200 |
|
| 2367 | _ = id_ |
|
| 2368 | ||
| 2369 | @staticmethod |
|
| 2370 | def on_get(req, resp, id_): |
|
| 2371 | if 'API-KEY' not in req.headers or \ |
|
| 2372 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2373 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2374 | access_control(req) |
|
| 2375 | else: |
|
| 2376 | api_key_control(req) |
|
| 2377 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2378 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2379 | description='API.INVALID_SPACE_ID') |
|
| 2380 | ||
| 2381 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2382 | cursor = cnx.cursor() |
|
| 2383 | ||
| 2384 | cursor.execute(" SELECT name " |
|
| 2385 | " FROM tbl_spaces " |
|
| 2386 | " WHERE id = %s ", (id_,)) |
|
| 2387 | if cursor.fetchone() is None: |
|
| 2388 | cursor.close() |
|
| 2389 | cnx.close() |
|
| 2390 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2391 | description='API.SPACE_NOT_FOUND') |
|
| 2392 | ||
| 2393 | query = (" SELECT se.id, se.name, se.uuid " |
|
| 2394 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
|
| 2395 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
| 2396 | " ORDER BY se.id ") |
|
| 2397 | cursor.execute(query, (id_,)) |
|
| 2398 | rows = cursor.fetchall() |
|
| 2399 | ||
| 2400 | result = list() |
|
| 2401 | if rows is not None and len(rows) > 0: |
|
| 2402 | for row in rows: |
|
| 2403 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2404 | result.append(meta_result) |
|
| 2405 | ||
| 2406 | resp.text = json.dumps(result) |
|
| 2407 | ||
| 2408 | @staticmethod |
|
| 2409 | @user_logger |
|
| 2410 | def on_post(req, resp, id_): |
|
| 2411 | """Handles POST requests""" |
|
| 2412 | admin_control(req) |
|
| 2413 | try: |
|
| 2414 | raw_json = req.stream.read().decode('utf-8') |
|
| 2415 | except Exception as ex: |
|
| 2416 | print(str(ex)) |
|
| 2417 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2418 | title='API.BAD_REQUEST', |
|
| 2419 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2420 | ||
| 2421 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2422 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2423 | description='API.INVALID_SPACE_ID') |
|
| 2424 | ||
| 2425 | new_values = json.loads(raw_json) |
|
| 2426 | ||
| 2427 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 2428 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 2429 | new_values['data']['sensor_id'] <= 0: |
|
| 2430 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2431 | description='API.INVALID_SENSOR_ID') |
|
| 2432 | sensor_id = new_values['data']['sensor_id'] |
|
| 2433 | ||
| 2434 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2435 | cursor = cnx.cursor() |
|
| 2436 | ||
| 2437 | cursor.execute(" SELECT name " |
|
| 2438 | " from tbl_spaces " |
|
| 2439 | " WHERE id = %s ", (id_,)) |
|
| 2440 | if cursor.fetchone() is None: |
|
| 2441 | cursor.close() |
|
| 2442 | cnx.close() |
|
| 2443 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2444 | description='API.SPACE_NOT_FOUND') |
|
| 2445 | ||
| 2446 | cursor.execute(" SELECT name " |
|
| 2447 | " FROM tbl_sensors " |
|
| 2448 | " WHERE id = %s ", (sensor_id,)) |
|
| 2449 | if cursor.fetchone() is None: |
|
| 2450 | cursor.close() |
|
| 2451 | cnx.close() |
|
| 2452 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2453 | description='API.SENSOR_NOT_FOUND') |
|
| 2454 | ||
| 2455 | query = (" SELECT id " |
|
| 2456 | " FROM tbl_spaces_sensors " |
|
| 2457 | " WHERE space_id = %s AND sensor_id = %s") |
|
| 2458 | cursor.execute(query, (id_, sensor_id,)) |
|
| 2459 | if cursor.fetchone() is not None: |
|
| 2460 | cursor.close() |
|
| 2461 | cnx.close() |
|
| 2462 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2463 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
|
| 2464 | ||
| 2465 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
|
| 2466 | " VALUES (%s, %s) ") |
|
| 2467 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 2468 | cnx.commit() |
|
| 2469 | cursor.close() |
|
| 2470 | cnx.close() |
|
| 2471 | ||
| 2472 | resp.status = falcon.HTTP_201 |
|
| 2473 | resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 2474 | ||
| 2475 | ||
| 2476 | class SpaceSensorItem: |
|
| @@ 1989-2103 (lines=115) @@ | ||
| 1986 | resp.status = falcon.HTTP_204 |
|
| 1987 | ||
| 1988 | ||
| 1989 | class SpacePhotovoltaicPowerStationCollection: |
|
| 1990 | def __init__(self): |
|
| 1991 | pass |
|
| 1992 | ||
| 1993 | @staticmethod |
|
| 1994 | def on_options(req, resp, id_): |
|
| 1995 | _ = req |
|
| 1996 | resp.status = falcon.HTTP_200 |
|
| 1997 | _ = id_ |
|
| 1998 | ||
| 1999 | @staticmethod |
|
| 2000 | def on_get(req, resp, id_): |
|
| 2001 | if 'API-KEY' not in req.headers or \ |
|
| 2002 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2003 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2004 | access_control(req) |
|
| 2005 | else: |
|
| 2006 | api_key_control(req) |
|
| 2007 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2008 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2009 | description='API.INVALID_SPACE_ID') |
|
| 2010 | ||
| 2011 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2012 | cursor = cnx.cursor() |
|
| 2013 | ||
| 2014 | cursor.execute(" SELECT name " |
|
| 2015 | " FROM tbl_spaces " |
|
| 2016 | " WHERE id = %s ", (id_,)) |
|
| 2017 | if cursor.fetchone() is None: |
|
| 2018 | cursor.close() |
|
| 2019 | cnx.close() |
|
| 2020 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2021 | description='API.SPACE_NOT_FOUND') |
|
| 2022 | ||
| 2023 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 2024 | " FROM tbl_spaces s, tbl_spaces_photovoltaic_power_stations se, tbl_photovoltaic_power_stations e " |
|
| 2025 | " WHERE se.space_id = s.id AND e.id = se.photovoltaic_power_station_id AND s.id = %s " |
|
| 2026 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 2027 | cursor.execute(query, (id_,)) |
|
| 2028 | rows = cursor.fetchall() |
|
| 2029 | ||
| 2030 | result = list() |
|
| 2031 | if rows is not None and len(rows) > 0: |
|
| 2032 | for row in rows: |
|
| 2033 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 2034 | result.append(meta_result) |
|
| 2035 | ||
| 2036 | resp.text = json.dumps(result) |
|
| 2037 | ||
| 2038 | @staticmethod |
|
| 2039 | @user_logger |
|
| 2040 | def on_post(req, resp, id_): |
|
| 2041 | """Handles POST requests""" |
|
| 2042 | admin_control(req) |
|
| 2043 | try: |
|
| 2044 | raw_json = req.stream.read().decode('utf-8') |
|
| 2045 | except Exception as ex: |
|
| 2046 | print(str(ex)) |
|
| 2047 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2048 | title='API.BAD_REQUEST', |
|
| 2049 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2050 | ||
| 2051 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2052 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2053 | description='API.INVALID_SPACE_ID') |
|
| 2054 | ||
| 2055 | new_values = json.loads(raw_json) |
|
| 2056 | ||
| 2057 | if 'photovoltaic_power_station_id' not in new_values['data'].keys() or \ |
|
| 2058 | not isinstance(new_values['data']['photovoltaic_power_station_id'], int) or \ |
|
| 2059 | new_values['data']['photovoltaic_power_station_id'] <= 0: |
|
| 2060 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2061 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 2062 | photovoltaic_power_station_id = new_values['data']['photovoltaic_power_station_id'] |
|
| 2063 | ||
| 2064 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2065 | cursor = cnx.cursor() |
|
| 2066 | ||
| 2067 | cursor.execute(" SELECT name " |
|
| 2068 | " from tbl_spaces " |
|
| 2069 | " WHERE id = %s ", (id_,)) |
|
| 2070 | if cursor.fetchone() is None: |
|
| 2071 | cursor.close() |
|
| 2072 | cnx.close() |
|
| 2073 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2074 | description='API.SPACE_NOT_FOUND') |
|
| 2075 | ||
| 2076 | cursor.execute(" SELECT name " |
|
| 2077 | " FROM tbl_photovoltaic_power_stations " |
|
| 2078 | " WHERE id = %s ", (photovoltaic_power_station_id,)) |
|
| 2079 | if cursor.fetchone() is None: |
|
| 2080 | cursor.close() |
|
| 2081 | cnx.close() |
|
| 2082 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2083 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 2084 | ||
| 2085 | query = (" SELECT id " |
|
| 2086 | " FROM tbl_spaces_photovoltaic_power_stations " |
|
| 2087 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s") |
|
| 2088 | cursor.execute(query, (id_, photovoltaic_power_station_id,)) |
|
| 2089 | if cursor.fetchone() is not None: |
|
| 2090 | cursor.close() |
|
| 2091 | cnx.close() |
|
| 2092 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2093 | description='API.SPACE_PHOTOVOLTAIC_POWER_STATION_RELATION_EXISTS') |
|
| 2094 | ||
| 2095 | add_row = (" INSERT INTO tbl_spaces_photovoltaic_power_stations (space_id, photovoltaic_power_station_id) " |
|
| 2096 | " VALUES (%s, %s) ") |
|
| 2097 | cursor.execute(add_row, (id_, photovoltaic_power_station_id,)) |
|
| 2098 | cnx.commit() |
|
| 2099 | cursor.close() |
|
| 2100 | cnx.close() |
|
| 2101 | ||
| 2102 | resp.status = falcon.HTTP_201 |
|
| 2103 | resp.location = '/spaces/' + str(id_) + '/photovoltaicpowerstations/' + str(photovoltaic_power_station_id) |
|
| 2104 | ||
| 2105 | ||
| 2106 | class SpacePhotovoltaicPowerStationItem: |
|
| @@ 1618-1732 (lines=115) @@ | ||
| 1615 | resp.status = falcon.HTTP_204 |
|
| 1616 | ||
| 1617 | ||
| 1618 | class SpaceMicrogridCollection: |
|
| 1619 | def __init__(self): |
|
| 1620 | pass |
|
| 1621 | ||
| 1622 | @staticmethod |
|
| 1623 | def on_options(req, resp, id_): |
|
| 1624 | _ = req |
|
| 1625 | resp.status = falcon.HTTP_200 |
|
| 1626 | _ = id_ |
|
| 1627 | ||
| 1628 | @staticmethod |
|
| 1629 | def on_get(req, resp, id_): |
|
| 1630 | if 'API-KEY' not in req.headers or \ |
|
| 1631 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1632 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1633 | access_control(req) |
|
| 1634 | else: |
|
| 1635 | api_key_control(req) |
|
| 1636 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1637 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1638 | description='API.INVALID_SPACE_ID') |
|
| 1639 | ||
| 1640 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1641 | cursor = cnx.cursor() |
|
| 1642 | ||
| 1643 | cursor.execute(" SELECT name " |
|
| 1644 | " FROM tbl_spaces " |
|
| 1645 | " WHERE id = %s ", (id_,)) |
|
| 1646 | if cursor.fetchone() is None: |
|
| 1647 | cursor.close() |
|
| 1648 | cnx.close() |
|
| 1649 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1650 | description='API.SPACE_NOT_FOUND') |
|
| 1651 | ||
| 1652 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
|
| 1653 | " FROM tbl_spaces s, tbl_spaces_microgrids se, tbl_microgrids e " |
|
| 1654 | " WHERE se.space_id = s.id AND e.id = se.microgrid_id AND s.id = %s " |
|
| 1655 | " ORDER BY e.phase_of_lifecycle, e.id ") |
|
| 1656 | cursor.execute(query, (id_,)) |
|
| 1657 | rows = cursor.fetchall() |
|
| 1658 | ||
| 1659 | result = list() |
|
| 1660 | if rows is not None and len(rows) > 0: |
|
| 1661 | for row in rows: |
|
| 1662 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1663 | result.append(meta_result) |
|
| 1664 | ||
| 1665 | resp.text = json.dumps(result) |
|
| 1666 | ||
| 1667 | @staticmethod |
|
| 1668 | @user_logger |
|
| 1669 | def on_post(req, resp, id_): |
|
| 1670 | """Handles POST requests""" |
|
| 1671 | admin_control(req) |
|
| 1672 | try: |
|
| 1673 | raw_json = req.stream.read().decode('utf-8') |
|
| 1674 | except Exception as ex: |
|
| 1675 | print(str(ex)) |
|
| 1676 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1677 | title='API.BAD_REQUEST', |
|
| 1678 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1679 | ||
| 1680 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1681 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1682 | description='API.INVALID_SPACE_ID') |
|
| 1683 | ||
| 1684 | new_values = json.loads(raw_json) |
|
| 1685 | ||
| 1686 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
| 1687 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
| 1688 | new_values['data']['microgrid_id'] <= 0: |
|
| 1689 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1690 | description='API.INVALID_MICROGRID_ID') |
|
| 1691 | microgrid_id = new_values['data']['microgrid_id'] |
|
| 1692 | ||
| 1693 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1694 | cursor = cnx.cursor() |
|
| 1695 | ||
| 1696 | cursor.execute(" SELECT name " |
|
| 1697 | " from tbl_spaces " |
|
| 1698 | " WHERE id = %s ", (id_,)) |
|
| 1699 | if cursor.fetchone() is None: |
|
| 1700 | cursor.close() |
|
| 1701 | cnx.close() |
|
| 1702 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1703 | description='API.SPACE_NOT_FOUND') |
|
| 1704 | ||
| 1705 | cursor.execute(" SELECT name " |
|
| 1706 | " FROM tbl_microgrids " |
|
| 1707 | " WHERE id = %s ", (microgrid_id,)) |
|
| 1708 | if cursor.fetchone() is None: |
|
| 1709 | cursor.close() |
|
| 1710 | cnx.close() |
|
| 1711 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1712 | description='API.MICROGRID_NOT_FOUND') |
|
| 1713 | ||
| 1714 | query = (" SELECT id " |
|
| 1715 | " FROM tbl_spaces_microgrids " |
|
| 1716 | " WHERE space_id = %s AND microgrid_id = %s") |
|
| 1717 | cursor.execute(query, (id_, microgrid_id,)) |
|
| 1718 | if cursor.fetchone() is not None: |
|
| 1719 | cursor.close() |
|
| 1720 | cnx.close() |
|
| 1721 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1722 | description='API.SPACE_MICROGRID_RELATION_EXISTS') |
|
| 1723 | ||
| 1724 | add_row = (" INSERT INTO tbl_spaces_microgrids (space_id, microgrid_id) " |
|
| 1725 | " VALUES (%s, %s) ") |
|
| 1726 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
| 1727 | cnx.commit() |
|
| 1728 | cursor.close() |
|
| 1729 | cnx.close() |
|
| 1730 | ||
| 1731 | resp.status = falcon.HTTP_201 |
|
| 1732 | resp.location = '/spaces/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
| 1733 | ||
| 1734 | ||
| 1735 | class SpaceMicrogridItem: |
|
| @@ 1247-1361 (lines=115) @@ | ||
| 1244 | resp.status = falcon.HTTP_204 |
|
| 1245 | ||
| 1246 | ||
| 1247 | class SpaceEquipmentCollection: |
|
| 1248 | def __init__(self): |
|
| 1249 | pass |
|
| 1250 | ||
| 1251 | @staticmethod |
|
| 1252 | def on_options(req, resp, id_): |
|
| 1253 | _ = req |
|
| 1254 | resp.status = falcon.HTTP_200 |
|
| 1255 | _ = id_ |
|
| 1256 | ||
| 1257 | @staticmethod |
|
| 1258 | def on_get(req, resp, id_): |
|
| 1259 | if 'API-KEY' not in req.headers or \ |
|
| 1260 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1261 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1262 | access_control(req) |
|
| 1263 | else: |
|
| 1264 | api_key_control(req) |
|
| 1265 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1266 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1267 | description='API.INVALID_SPACE_ID') |
|
| 1268 | ||
| 1269 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1270 | cursor = cnx.cursor() |
|
| 1271 | ||
| 1272 | cursor.execute(" SELECT name " |
|
| 1273 | " FROM tbl_spaces " |
|
| 1274 | " WHERE id = %s ", (id_,)) |
|
| 1275 | if cursor.fetchone() is None: |
|
| 1276 | cursor.close() |
|
| 1277 | cnx.close() |
|
| 1278 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1279 | description='API.SPACE_NOT_FOUND') |
|
| 1280 | ||
| 1281 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 1282 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
|
| 1283 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
| 1284 | " ORDER BY e.id ") |
|
| 1285 | cursor.execute(query, (id_,)) |
|
| 1286 | rows = cursor.fetchall() |
|
| 1287 | ||
| 1288 | result = list() |
|
| 1289 | if rows is not None and len(rows) > 0: |
|
| 1290 | for row in rows: |
|
| 1291 | meta_result = {"id": row[0], "name": row[1], "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 | print(str(ex)) |
|
| 1305 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1306 | title='API.BAD_REQUEST', |
|
| 1307 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1308 | ||
| 1309 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1310 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1311 | description='API.INVALID_SPACE_ID') |
|
| 1312 | ||
| 1313 | new_values = json.loads(raw_json) |
|
| 1314 | ||
| 1315 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 1316 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 1317 | new_values['data']['equipment_id'] <= 0: |
|
| 1318 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1319 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1320 | equipment_id = new_values['data']['equipment_id'] |
|
| 1321 | ||
| 1322 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1323 | cursor = cnx.cursor() |
|
| 1324 | ||
| 1325 | cursor.execute(" SELECT name " |
|
| 1326 | " from tbl_spaces " |
|
| 1327 | " WHERE id = %s ", (id_,)) |
|
| 1328 | if cursor.fetchone() is None: |
|
| 1329 | cursor.close() |
|
| 1330 | cnx.close() |
|
| 1331 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1332 | description='API.SPACE_NOT_FOUND') |
|
| 1333 | ||
| 1334 | cursor.execute(" SELECT name " |
|
| 1335 | " FROM tbl_equipments " |
|
| 1336 | " WHERE id = %s ", (equipment_id,)) |
|
| 1337 | if cursor.fetchone() is None: |
|
| 1338 | cursor.close() |
|
| 1339 | cnx.close() |
|
| 1340 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1341 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 1342 | ||
| 1343 | query = (" SELECT id " |
|
| 1344 | " FROM tbl_spaces_equipments " |
|
| 1345 | " WHERE space_id = %s AND equipment_id = %s") |
|
| 1346 | cursor.execute(query, (id_, equipment_id,)) |
|
| 1347 | if cursor.fetchone() is not None: |
|
| 1348 | cursor.close() |
|
| 1349 | cnx.close() |
|
| 1350 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1351 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
|
| 1352 | ||
| 1353 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
|
| 1354 | " VALUES (%s, %s) ") |
|
| 1355 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 1356 | cnx.commit() |
|
| 1357 | cursor.close() |
|
| 1358 | cnx.close() |
|
| 1359 | ||
| 1360 | resp.status = falcon.HTTP_201 |
|
| 1361 | resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 1362 | ||
| 1363 | ||
| 1364 | class SpaceEquipmentItem: |
|
| @@ 850-964 (lines=115) @@ | ||
| 847 | resp.text = json.dumps(result) |
|
| 848 | ||
| 849 | ||
| 850 | class SpaceCombinedEquipmentCollection: |
|
| 851 | def __init__(self): |
|
| 852 | pass |
|
| 853 | ||
| 854 | @staticmethod |
|
| 855 | def on_options(req, resp, id_): |
|
| 856 | _ = req |
|
| 857 | resp.status = falcon.HTTP_200 |
|
| 858 | _ = id_ |
|
| 859 | ||
| 860 | @staticmethod |
|
| 861 | def on_get(req, resp, id_): |
|
| 862 | if 'API-KEY' not in req.headers or \ |
|
| 863 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 864 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 865 | access_control(req) |
|
| 866 | else: |
|
| 867 | api_key_control(req) |
|
| 868 | if not id_.isdigit() or int(id_) <= 0: |
|
| 869 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 870 | description='API.INVALID_SPACE_ID') |
|
| 871 | ||
| 872 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 873 | cursor = cnx.cursor() |
|
| 874 | ||
| 875 | cursor.execute(" SELECT name " |
|
| 876 | " FROM tbl_spaces " |
|
| 877 | " WHERE id = %s ", (id_,)) |
|
| 878 | if cursor.fetchone() is None: |
|
| 879 | cursor.close() |
|
| 880 | cnx.close() |
|
| 881 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 882 | description='API.SPACE_NOT_FOUND') |
|
| 883 | ||
| 884 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 885 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
|
| 886 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
|
| 887 | " ORDER BY e.id ") |
|
| 888 | cursor.execute(query, (id_,)) |
|
| 889 | rows = cursor.fetchall() |
|
| 890 | ||
| 891 | result = list() |
|
| 892 | if rows is not None and len(rows) > 0: |
|
| 893 | for row in rows: |
|
| 894 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 895 | result.append(meta_result) |
|
| 896 | ||
| 897 | resp.text = json.dumps(result) |
|
| 898 | ||
| 899 | @staticmethod |
|
| 900 | @user_logger |
|
| 901 | def on_post(req, resp, id_): |
|
| 902 | """Handles POST requests""" |
|
| 903 | admin_control(req) |
|
| 904 | try: |
|
| 905 | raw_json = req.stream.read().decode('utf-8') |
|
| 906 | except Exception as ex: |
|
| 907 | print(str(ex)) |
|
| 908 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 909 | title='API.BAD_REQUEST', |
|
| 910 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 911 | ||
| 912 | if not id_.isdigit() or int(id_) <= 0: |
|
| 913 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 914 | description='API.INVALID_SPACE_ID') |
|
| 915 | ||
| 916 | new_values = json.loads(raw_json) |
|
| 917 | ||
| 918 | if 'combined_equipment_id' not in new_values['data'].keys() or \ |
|
| 919 | not isinstance(new_values['data']['combined_equipment_id'], int) or \ |
|
| 920 | new_values['data']['combined_equipment_id'] <= 0: |
|
| 921 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 922 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 923 | combined_equipment_id = new_values['data']['combined_equipment_id'] |
|
| 924 | ||
| 925 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 926 | cursor = cnx.cursor() |
|
| 927 | ||
| 928 | cursor.execute(" SELECT name " |
|
| 929 | " from tbl_spaces " |
|
| 930 | " WHERE id = %s ", (id_,)) |
|
| 931 | if cursor.fetchone() is None: |
|
| 932 | cursor.close() |
|
| 933 | cnx.close() |
|
| 934 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 935 | description='API.SPACE_NOT_FOUND') |
|
| 936 | ||
| 937 | cursor.execute(" SELECT name " |
|
| 938 | " FROM tbl_combined_equipments " |
|
| 939 | " WHERE id = %s ", (combined_equipment_id,)) |
|
| 940 | if cursor.fetchone() is None: |
|
| 941 | cursor.close() |
|
| 942 | cnx.close() |
|
| 943 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 944 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 945 | ||
| 946 | query = (" SELECT id " |
|
| 947 | " FROM tbl_spaces_combined_equipments " |
|
| 948 | " WHERE space_id = %s AND combined_equipment_id = %s") |
|
| 949 | cursor.execute(query, (id_, combined_equipment_id,)) |
|
| 950 | if cursor.fetchone() is not None: |
|
| 951 | cursor.close() |
|
| 952 | cnx.close() |
|
| 953 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 954 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
|
| 955 | ||
| 956 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
|
| 957 | " VALUES (%s, %s) ") |
|
| 958 | cursor.execute(add_row, (id_, combined_equipment_id,)) |
|
| 959 | cnx.commit() |
|
| 960 | cursor.close() |
|
| 961 | cnx.close() |
|
| 962 | ||
| 963 | resp.status = falcon.HTTP_201 |
|
| 964 | resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id) |
|
| 965 | ||
| 966 | ||
| 967 | class SpaceCombinedEquipmentItem: |
|
| @@ 5350-5464 (lines=115) @@ | ||
| 5347 | resp.location = '/spaces/' + str(new_id) |
|
| 5348 | ||
| 5349 | ||
| 5350 | class SpaceEnergyFlowDiagramCollection: |
|
| 5351 | def __init__(self): |
|
| 5352 | pass |
|
| 5353 | ||
| 5354 | @staticmethod |
|
| 5355 | def on_options(req, resp, id_): |
|
| 5356 | _ = req |
|
| 5357 | resp.status = falcon.HTTP_200 |
|
| 5358 | _ = id_ |
|
| 5359 | ||
| 5360 | @staticmethod |
|
| 5361 | def on_get(req, resp, id_): |
|
| 5362 | if 'API-KEY' not in req.headers or \ |
|
| 5363 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5364 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5365 | access_control(req) |
|
| 5366 | else: |
|
| 5367 | api_key_control(req) |
|
| 5368 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5369 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5370 | description='API.INVALID_SPACE_ID') |
|
| 5371 | ||
| 5372 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5373 | cursor = cnx.cursor() |
|
| 5374 | ||
| 5375 | cursor.execute(" SELECT name " |
|
| 5376 | " FROM tbl_spaces " |
|
| 5377 | " WHERE id = %s ", (id_,)) |
|
| 5378 | if cursor.fetchone() is None: |
|
| 5379 | cursor.close() |
|
| 5380 | cnx.close() |
|
| 5381 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5382 | description='API.SPACE_NOT_FOUND') |
|
| 5383 | ||
| 5384 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 5385 | " FROM tbl_spaces s, tbl_spaces_energy_flow_diagrams se, tbl_energy_flow_diagrams e " |
|
| 5386 | " WHERE se.space_id = s.id AND e.id = se.energy_flow_diagram_id AND s.id = %s " |
|
| 5387 | " ORDER BY e.id ") |
|
| 5388 | cursor.execute(query, (id_,)) |
|
| 5389 | rows = cursor.fetchall() |
|
| 5390 | ||
| 5391 | result = list() |
|
| 5392 | if rows is not None and len(rows) > 0: |
|
| 5393 | for row in rows: |
|
| 5394 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5395 | result.append(meta_result) |
|
| 5396 | ||
| 5397 | resp.text = json.dumps(result) |
|
| 5398 | ||
| 5399 | @staticmethod |
|
| 5400 | @user_logger |
|
| 5401 | def on_post(req, resp, id_): |
|
| 5402 | """Handles POST requests""" |
|
| 5403 | admin_control(req) |
|
| 5404 | try: |
|
| 5405 | raw_json = req.stream.read().decode('utf-8') |
|
| 5406 | except Exception as ex: |
|
| 5407 | print(str(ex)) |
|
| 5408 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5409 | title='API.BAD_REQUEST', |
|
| 5410 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5411 | ||
| 5412 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5414 | description='API.INVALID_SPACE_ID') |
|
| 5415 | ||
| 5416 | new_values = json.loads(raw_json) |
|
| 5417 | ||
| 5418 | if 'energy_flow_diagram_id' not in new_values['data'].keys() or \ |
|
| 5419 | not isinstance(new_values['data']['energy_flow_diagram_id'], int) or \ |
|
| 5420 | new_values['data']['energy_flow_diagram_id'] <= 0: |
|
| 5421 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5422 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
| 5423 | energy_flow_diagram_id = new_values['data']['energy_flow_diagram_id'] |
|
| 5424 | ||
| 5425 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5426 | cursor = cnx.cursor() |
|
| 5427 | ||
| 5428 | cursor.execute(" SELECT name " |
|
| 5429 | " from tbl_spaces " |
|
| 5430 | " WHERE id = %s ", (id_,)) |
|
| 5431 | if cursor.fetchone() is None: |
|
| 5432 | cursor.close() |
|
| 5433 | cnx.close() |
|
| 5434 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5435 | description='API.SPACE_NOT_FOUND') |
|
| 5436 | ||
| 5437 | cursor.execute(" SELECT name " |
|
| 5438 | " FROM tbl_energy_flow_diagrams " |
|
| 5439 | " WHERE id = %s ", (energy_flow_diagram_id,)) |
|
| 5440 | if cursor.fetchone() is None: |
|
| 5441 | cursor.close() |
|
| 5442 | cnx.close() |
|
| 5443 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5444 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
| 5445 | ||
| 5446 | query = (" SELECT id " |
|
| 5447 | " FROM tbl_spaces_energy_flow_diagrams " |
|
| 5448 | " WHERE space_id = %s AND energy_flow_diagram_id = %s") |
|
| 5449 | cursor.execute(query, (id_, energy_flow_diagram_id,)) |
|
| 5450 | if cursor.fetchone() is not None: |
|
| 5451 | cursor.close() |
|
| 5452 | cnx.close() |
|
| 5453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5454 | description='API.SPACE_ENERGY_FLOW_DIAGRAM_RELATION_EXISTS') |
|
| 5455 | ||
| 5456 | add_row = (" INSERT INTO tbl_spaces_energy_flow_diagrams (space_id, energy_flow_diagram_id) " |
|
| 5457 | " VALUES (%s, %s) ") |
|
| 5458 | cursor.execute(add_row, (id_, energy_flow_diagram_id,)) |
|
| 5459 | cnx.commit() |
|
| 5460 | cursor.close() |
|
| 5461 | cnx.close() |
|
| 5462 | ||
| 5463 | resp.status = falcon.HTTP_201 |
|
| 5464 | resp.location = '/spaces/' + str(id_) + '/energyflowdiagrams/' + str(energy_flow_diagram_id) |
|
| 5465 | ||
| 5466 | ||
| 5467 | class SpaceEnergyFlowDiagramItem: |
|
| @@ 3616-3730 (lines=115) @@ | ||
| 3613 | resp.status = falcon.HTTP_204 |
|
| 3614 | ||
| 3615 | ||
| 3616 | class SpaceCommandCollection: |
|
| 3617 | def __init__(self): |
|
| 3618 | pass |
|
| 3619 | ||
| 3620 | @staticmethod |
|
| 3621 | def on_options(req, resp, id_): |
|
| 3622 | _ = req |
|
| 3623 | resp.status = falcon.HTTP_200 |
|
| 3624 | _ = id_ |
|
| 3625 | ||
| 3626 | @staticmethod |
|
| 3627 | def on_get(req, resp, id_): |
|
| 3628 | if 'API-KEY' not in req.headers or \ |
|
| 3629 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3630 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3631 | access_control(req) |
|
| 3632 | else: |
|
| 3633 | api_key_control(req) |
|
| 3634 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3635 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3636 | description='API.INVALID_SPACE_ID') |
|
| 3637 | ||
| 3638 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3639 | cursor = cnx.cursor() |
|
| 3640 | ||
| 3641 | cursor.execute(" SELECT name " |
|
| 3642 | " FROM tbl_spaces " |
|
| 3643 | " WHERE id = %s ", (id_,)) |
|
| 3644 | if cursor.fetchone() is None: |
|
| 3645 | cursor.close() |
|
| 3646 | cnx.close() |
|
| 3647 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3648 | description='API.SPACE_NOT_FOUND') |
|
| 3649 | ||
| 3650 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 3651 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
|
| 3652 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 3653 | " ORDER BY c.id ") |
|
| 3654 | cursor.execute(query, (id_,)) |
|
| 3655 | rows = cursor.fetchall() |
|
| 3656 | ||
| 3657 | result = list() |
|
| 3658 | if rows is not None and len(rows) > 0: |
|
| 3659 | for row in rows: |
|
| 3660 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 3661 | result.append(meta_result) |
|
| 3662 | ||
| 3663 | resp.text = json.dumps(result) |
|
| 3664 | ||
| 3665 | @staticmethod |
|
| 3666 | @user_logger |
|
| 3667 | def on_post(req, resp, id_): |
|
| 3668 | """Handles POST requests""" |
|
| 3669 | admin_control(req) |
|
| 3670 | try: |
|
| 3671 | raw_json = req.stream.read().decode('utf-8') |
|
| 3672 | except Exception as ex: |
|
| 3673 | print(str(ex)) |
|
| 3674 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3675 | title='API.BAD_REQUEST', |
|
| 3676 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3677 | ||
| 3678 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3679 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3680 | description='API.INVALID_SPACE_ID') |
|
| 3681 | ||
| 3682 | new_values = json.loads(raw_json) |
|
| 3683 | ||
| 3684 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 3685 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 3686 | new_values['data']['command_id'] <= 0: |
|
| 3687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3688 | description='API.INVALID_COMMAND_ID') |
|
| 3689 | command_id = new_values['data']['command_id'] |
|
| 3690 | ||
| 3691 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3692 | cursor = cnx.cursor() |
|
| 3693 | ||
| 3694 | cursor.execute(" SELECT name " |
|
| 3695 | " from tbl_spaces " |
|
| 3696 | " WHERE id = %s ", (id_,)) |
|
| 3697 | if cursor.fetchone() is None: |
|
| 3698 | cursor.close() |
|
| 3699 | cnx.close() |
|
| 3700 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3701 | description='API.SPACE_NOT_FOUND') |
|
| 3702 | ||
| 3703 | cursor.execute(" SELECT name " |
|
| 3704 | " FROM tbl_commands " |
|
| 3705 | " WHERE id = %s ", (command_id,)) |
|
| 3706 | if cursor.fetchone() is None: |
|
| 3707 | cursor.close() |
|
| 3708 | cnx.close() |
|
| 3709 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3710 | description='API.COMMAND_NOT_FOUND') |
|
| 3711 | ||
| 3712 | query = (" SELECT id " |
|
| 3713 | " FROM tbl_spaces_commands " |
|
| 3714 | " WHERE space_id = %s AND command_id = %s") |
|
| 3715 | cursor.execute(query, (id_, command_id,)) |
|
| 3716 | if cursor.fetchone() is not None: |
|
| 3717 | cursor.close() |
|
| 3718 | cnx.close() |
|
| 3719 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3720 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
|
| 3721 | ||
| 3722 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
|
| 3723 | " VALUES (%s, %s) ") |
|
| 3724 | cursor.execute(add_row, (id_, command_id,)) |
|
| 3725 | cnx.commit() |
|
| 3726 | cursor.close() |
|
| 3727 | cnx.close() |
|
| 3728 | ||
| 3729 | resp.status = falcon.HTTP_201 |
|
| 3730 | resp.location = '/spaces/' + str(id_) + '/commands/' + str(command_id) |
|
| 3731 | ||
| 3732 | ||
| 3733 | class SpaceCommandItem: |
|
| @@ 3437-3551 (lines=115) @@ | ||
| 3434 | resp.text = json.dumps(result) |
|
| 3435 | ||
| 3436 | ||
| 3437 | class SpaceWorkingCalendarCollection: |
|
| 3438 | def __init__(self): |
|
| 3439 | pass |
|
| 3440 | ||
| 3441 | @staticmethod |
|
| 3442 | def on_options(req, resp, id_): |
|
| 3443 | _ = req |
|
| 3444 | resp.status = falcon.HTTP_200 |
|
| 3445 | _ = id_ |
|
| 3446 | ||
| 3447 | @staticmethod |
|
| 3448 | def on_get(req, resp, id_): |
|
| 3449 | if 'API-KEY' not in req.headers or \ |
|
| 3450 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3451 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3452 | access_control(req) |
|
| 3453 | else: |
|
| 3454 | api_key_control(req) |
|
| 3455 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3456 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3457 | description='API.INVALID_SPACE_ID') |
|
| 3458 | ||
| 3459 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3460 | cursor = cnx.cursor() |
|
| 3461 | ||
| 3462 | cursor.execute(" SELECT name " |
|
| 3463 | " FROM tbl_spaces " |
|
| 3464 | " WHERE id = %s ", (id_,)) |
|
| 3465 | if cursor.fetchone() is None: |
|
| 3466 | cursor.close() |
|
| 3467 | cnx.close() |
|
| 3468 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3469 | description='API.SPACE_NOT_FOUND') |
|
| 3470 | ||
| 3471 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 3472 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
|
| 3473 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 3474 | " ORDER BY wc.id ") |
|
| 3475 | cursor.execute(query, (id_,)) |
|
| 3476 | rows = cursor.fetchall() |
|
| 3477 | ||
| 3478 | result = list() |
|
| 3479 | if rows is not None and len(rows) > 0: |
|
| 3480 | for row in rows: |
|
| 3481 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 3482 | result.append(meta_result) |
|
| 3483 | ||
| 3484 | resp.text = json.dumps(result) |
|
| 3485 | ||
| 3486 | @staticmethod |
|
| 3487 | @user_logger |
|
| 3488 | def on_post(req, resp, id_): |
|
| 3489 | """Handles POST requests""" |
|
| 3490 | admin_control(req) |
|
| 3491 | try: |
|
| 3492 | raw_json = req.stream.read().decode('utf-8') |
|
| 3493 | except Exception as ex: |
|
| 3494 | print(str(ex)) |
|
| 3495 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3496 | title='API.BAD_REQUEST', |
|
| 3497 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3498 | ||
| 3499 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3500 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3501 | description='API.INVALID_SPACE_ID') |
|
| 3502 | ||
| 3503 | new_values = json.loads(raw_json) |
|
| 3504 | ||
| 3505 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 3506 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 3507 | new_values['data']['working_calendar_id'] <= 0: |
|
| 3508 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3509 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 3510 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 3511 | ||
| 3512 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3513 | cursor = cnx.cursor() |
|
| 3514 | ||
| 3515 | cursor.execute(" SELECT name " |
|
| 3516 | " from tbl_spaces " |
|
| 3517 | " WHERE id = %s ", (id_,)) |
|
| 3518 | if cursor.fetchone() is None: |
|
| 3519 | cursor.close() |
|
| 3520 | cnx.close() |
|
| 3521 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3522 | description='API.SPACE_NOT_FOUND') |
|
| 3523 | ||
| 3524 | cursor.execute(" SELECT name " |
|
| 3525 | " FROM tbl_working_calendars " |
|
| 3526 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 3527 | if cursor.fetchone() is None: |
|
| 3528 | cursor.close() |
|
| 3529 | cnx.close() |
|
| 3530 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3531 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 3532 | ||
| 3533 | query = (" SELECT id " |
|
| 3534 | " FROM tbl_spaces_working_calendars " |
|
| 3535 | " WHERE space_id = %s AND working_calendar_id = %s") |
|
| 3536 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 3537 | if cursor.fetchone() is not None: |
|
| 3538 | cursor.close() |
|
| 3539 | cnx.close() |
|
| 3540 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3541 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 3542 | ||
| 3543 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
|
| 3544 | " VALUES (%s, %s) ") |
|
| 3545 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 3546 | cnx.commit() |
|
| 3547 | cursor.close() |
|
| 3548 | cnx.close() |
|
| 3549 | ||
| 3550 | resp.status = falcon.HTTP_201 |
|
| 3551 | resp.location = '/spaces/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 3552 | ||
| 3553 | ||
| 3554 | class SpaceWorkingCalendarItem: |
|
| @@ 1812-1926 (lines=115) @@ | ||
| 1809 | resp.status = falcon.HTTP_204 |
|
| 1810 | ||
| 1811 | ||
| 1812 | class ShopfloorCommandCollection: |
|
| 1813 | def __init__(self): |
|
| 1814 | pass |
|
| 1815 | ||
| 1816 | @staticmethod |
|
| 1817 | def on_options(req, resp, id_): |
|
| 1818 | resp.status = falcon.HTTP_200 |
|
| 1819 | _ = req |
|
| 1820 | _ = id_ |
|
| 1821 | ||
| 1822 | @staticmethod |
|
| 1823 | def on_get(req, resp, id_): |
|
| 1824 | if 'API-KEY' not in req.headers or \ |
|
| 1825 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1826 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1827 | access_control(req) |
|
| 1828 | else: |
|
| 1829 | api_key_control(req) |
|
| 1830 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1831 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1832 | description='API.INVALID_STORE_ID') |
|
| 1833 | ||
| 1834 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1835 | cursor = cnx.cursor() |
|
| 1836 | ||
| 1837 | cursor.execute(" SELECT name " |
|
| 1838 | " FROM tbl_shopfloors " |
|
| 1839 | " WHERE id = %s ", (id_,)) |
|
| 1840 | if cursor.fetchone() is None: |
|
| 1841 | cursor.close() |
|
| 1842 | cnx.close() |
|
| 1843 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1844 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1845 | ||
| 1846 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1847 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
|
| 1848 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 1849 | " ORDER BY c.id ") |
|
| 1850 | cursor.execute(query, (id_,)) |
|
| 1851 | rows = cursor.fetchall() |
|
| 1852 | ||
| 1853 | result = list() |
|
| 1854 | if rows is not None and len(rows) > 0: |
|
| 1855 | for row in rows: |
|
| 1856 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1857 | result.append(meta_result) |
|
| 1858 | ||
| 1859 | resp.text = json.dumps(result) |
|
| 1860 | ||
| 1861 | @staticmethod |
|
| 1862 | @user_logger |
|
| 1863 | def on_post(req, resp, id_): |
|
| 1864 | """Handles POST requests""" |
|
| 1865 | admin_control(req) |
|
| 1866 | try: |
|
| 1867 | raw_json = req.stream.read().decode('utf-8') |
|
| 1868 | except Exception as ex: |
|
| 1869 | print(ex) |
|
| 1870 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1871 | title='API.BAD_REQUEST', |
|
| 1872 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1873 | ||
| 1874 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1875 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1876 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1877 | ||
| 1878 | new_values = json.loads(raw_json) |
|
| 1879 | ||
| 1880 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1881 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1882 | new_values['data']['command_id'] <= 0: |
|
| 1883 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1884 | description='API.INVALID_COMMAND_ID') |
|
| 1885 | command_id = new_values['data']['command_id'] |
|
| 1886 | ||
| 1887 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1888 | cursor = cnx.cursor() |
|
| 1889 | ||
| 1890 | cursor.execute(" SELECT name " |
|
| 1891 | " from tbl_shopfloors " |
|
| 1892 | " WHERE id = %s ", (id_,)) |
|
| 1893 | if cursor.fetchone() is None: |
|
| 1894 | cursor.close() |
|
| 1895 | cnx.close() |
|
| 1896 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1897 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1898 | ||
| 1899 | cursor.execute(" SELECT name " |
|
| 1900 | " FROM tbl_commands " |
|
| 1901 | " WHERE id = %s ", (command_id,)) |
|
| 1902 | if cursor.fetchone() is None: |
|
| 1903 | cursor.close() |
|
| 1904 | cnx.close() |
|
| 1905 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1906 | description='API.COMMAND_NOT_FOUND') |
|
| 1907 | ||
| 1908 | query = (" SELECT id " |
|
| 1909 | " FROM tbl_shopfloors_commands " |
|
| 1910 | " WHERE shopfloor_id = %s AND command_id = %s") |
|
| 1911 | cursor.execute(query, (id_, command_id,)) |
|
| 1912 | if cursor.fetchone() is not None: |
|
| 1913 | cursor.close() |
|
| 1914 | cnx.close() |
|
| 1915 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1916 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
|
| 1917 | ||
| 1918 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
|
| 1919 | " VALUES (%s, %s) ") |
|
| 1920 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1921 | cnx.commit() |
|
| 1922 | cursor.close() |
|
| 1923 | cnx.close() |
|
| 1924 | ||
| 1925 | resp.status = falcon.HTTP_201 |
|
| 1926 | resp.location = '/shopfloors/' + str(id_) + '/commands/' + str(command_id) |
|
| 1927 | ||
| 1928 | ||
| 1929 | class ShopfloorCommandItem: |
|
| @@ 1632-1746 (lines=115) @@ | ||
| 1629 | resp.status = falcon.HTTP_204 |
|
| 1630 | ||
| 1631 | ||
| 1632 | class ShopfloorWorkingCalendarCollection: |
|
| 1633 | def __init__(self): |
|
| 1634 | pass |
|
| 1635 | ||
| 1636 | @staticmethod |
|
| 1637 | def on_options(req, resp, id_): |
|
| 1638 | resp.status = falcon.HTTP_200 |
|
| 1639 | _ = req |
|
| 1640 | _ = id_ |
|
| 1641 | ||
| 1642 | @staticmethod |
|
| 1643 | def on_get(req, resp, id_): |
|
| 1644 | if 'API-KEY' not in req.headers or \ |
|
| 1645 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1646 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1647 | access_control(req) |
|
| 1648 | else: |
|
| 1649 | api_key_control(req) |
|
| 1650 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1652 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1653 | ||
| 1654 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1655 | cursor = cnx.cursor() |
|
| 1656 | ||
| 1657 | cursor.execute(" SELECT name " |
|
| 1658 | " FROM tbl_shopfloors " |
|
| 1659 | " WHERE id = %s ", (id_,)) |
|
| 1660 | if cursor.fetchone() is None: |
|
| 1661 | cursor.close() |
|
| 1662 | cnx.close() |
|
| 1663 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1664 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1665 | ||
| 1666 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1667 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
|
| 1668 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 1669 | " ORDER BY wc.id ") |
|
| 1670 | cursor.execute(query, (id_,)) |
|
| 1671 | rows = cursor.fetchall() |
|
| 1672 | ||
| 1673 | result = list() |
|
| 1674 | if rows is not None and len(rows) > 0: |
|
| 1675 | for row in rows: |
|
| 1676 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 1677 | result.append(meta_result) |
|
| 1678 | ||
| 1679 | resp.text = json.dumps(result) |
|
| 1680 | ||
| 1681 | @staticmethod |
|
| 1682 | @user_logger |
|
| 1683 | def on_post(req, resp, id_): |
|
| 1684 | """Handles POST requests""" |
|
| 1685 | admin_control(req) |
|
| 1686 | try: |
|
| 1687 | raw_json = req.stream.read().decode('utf-8') |
|
| 1688 | except Exception as ex: |
|
| 1689 | print(ex) |
|
| 1690 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1691 | title='API.BAD_REQUEST', |
|
| 1692 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1693 | ||
| 1694 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1695 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1696 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1697 | ||
| 1698 | new_values = json.loads(raw_json) |
|
| 1699 | ||
| 1700 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1701 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1702 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1703 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1704 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1705 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1706 | ||
| 1707 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1708 | cursor = cnx.cursor() |
|
| 1709 | ||
| 1710 | cursor.execute(" SELECT name " |
|
| 1711 | " from tbl_shopfloors " |
|
| 1712 | " WHERE id = %s ", (id_,)) |
|
| 1713 | if cursor.fetchone() is None: |
|
| 1714 | cursor.close() |
|
| 1715 | cnx.close() |
|
| 1716 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1717 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1718 | ||
| 1719 | cursor.execute(" SELECT name " |
|
| 1720 | " FROM tbl_working_calendars " |
|
| 1721 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 1722 | if cursor.fetchone() is None: |
|
| 1723 | cursor.close() |
|
| 1724 | cnx.close() |
|
| 1725 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1726 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 1727 | ||
| 1728 | query = (" SELECT id " |
|
| 1729 | " FROM tbl_shopfloors_working_calendars " |
|
| 1730 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
|
| 1731 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1732 | if cursor.fetchone() is not None: |
|
| 1733 | cursor.close() |
|
| 1734 | cnx.close() |
|
| 1735 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1736 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1737 | ||
| 1738 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
|
| 1739 | " VALUES (%s, %s) ") |
|
| 1740 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1741 | cnx.commit() |
|
| 1742 | cursor.close() |
|
| 1743 | cnx.close() |
|
| 1744 | ||
| 1745 | resp.status = falcon.HTTP_201 |
|
| 1746 | resp.location = '/shopfloors/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1747 | ||
| 1748 | ||
| 1749 | class ShopfloorWorkingCalendarItem: |
|
| @@ 1260-1374 (lines=115) @@ | ||
| 1257 | resp.status = falcon.HTTP_204 |
|
| 1258 | ||
| 1259 | ||
| 1260 | class ShopfloorSensorCollection: |
|
| 1261 | def __init__(self): |
|
| 1262 | pass |
|
| 1263 | ||
| 1264 | @staticmethod |
|
| 1265 | def on_options(req, resp, id_): |
|
| 1266 | resp.status = falcon.HTTP_200 |
|
| 1267 | _ = req |
|
| 1268 | _ = id_ |
|
| 1269 | ||
| 1270 | @staticmethod |
|
| 1271 | def on_get(req, resp, id_): |
|
| 1272 | if 'API-KEY' not in req.headers or \ |
|
| 1273 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1274 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1275 | access_control(req) |
|
| 1276 | else: |
|
| 1277 | api_key_control(req) |
|
| 1278 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1279 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1280 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1281 | ||
| 1282 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1283 | cursor = cnx.cursor() |
|
| 1284 | ||
| 1285 | cursor.execute(" SELECT name " |
|
| 1286 | " FROM tbl_shopfloors " |
|
| 1287 | " WHERE id = %s ", (id_,)) |
|
| 1288 | if cursor.fetchone() is None: |
|
| 1289 | cursor.close() |
|
| 1290 | cnx.close() |
|
| 1291 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1292 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1293 | ||
| 1294 | query = (" SELECT se.id, se.name, se.uuid " |
|
| 1295 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
|
| 1296 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
|
| 1297 | " ORDER BY se.id ") |
|
| 1298 | cursor.execute(query, (id_,)) |
|
| 1299 | rows = cursor.fetchall() |
|
| 1300 | ||
| 1301 | result = list() |
|
| 1302 | if rows is not None and len(rows) > 0: |
|
| 1303 | for row in rows: |
|
| 1304 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1305 | result.append(meta_result) |
|
| 1306 | ||
| 1307 | resp.text = json.dumps(result) |
|
| 1308 | ||
| 1309 | @staticmethod |
|
| 1310 | @user_logger |
|
| 1311 | def on_post(req, resp, id_): |
|
| 1312 | """Handles POST requests""" |
|
| 1313 | admin_control(req) |
|
| 1314 | try: |
|
| 1315 | raw_json = req.stream.read().decode('utf-8') |
|
| 1316 | except Exception as ex: |
|
| 1317 | print(ex) |
|
| 1318 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1319 | title='API.BAD_REQUEST', |
|
| 1320 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1321 | ||
| 1322 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1323 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1324 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 1325 | ||
| 1326 | new_values = json.loads(raw_json) |
|
| 1327 | ||
| 1328 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1329 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1330 | new_values['data']['sensor_id'] <= 0: |
|
| 1331 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1332 | description='API.INVALID_SENSOR_ID') |
|
| 1333 | sensor_id = new_values['data']['sensor_id'] |
|
| 1334 | ||
| 1335 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1336 | cursor = cnx.cursor() |
|
| 1337 | ||
| 1338 | cursor.execute(" SELECT name " |
|
| 1339 | " from tbl_shopfloors " |
|
| 1340 | " WHERE id = %s ", (id_,)) |
|
| 1341 | if cursor.fetchone() is None: |
|
| 1342 | cursor.close() |
|
| 1343 | cnx.close() |
|
| 1344 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1345 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 1346 | ||
| 1347 | cursor.execute(" SELECT name " |
|
| 1348 | " FROM tbl_sensors " |
|
| 1349 | " WHERE id = %s ", (sensor_id,)) |
|
| 1350 | if cursor.fetchone() is None: |
|
| 1351 | cursor.close() |
|
| 1352 | cnx.close() |
|
| 1353 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1354 | description='API.SENSOR_NOT_FOUND') |
|
| 1355 | ||
| 1356 | query = (" SELECT id " |
|
| 1357 | " FROM tbl_shopfloors_sensors " |
|
| 1358 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
|
| 1359 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1360 | if cursor.fetchone() is not None: |
|
| 1361 | cursor.close() |
|
| 1362 | cnx.close() |
|
| 1363 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1364 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
|
| 1365 | ||
| 1366 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
|
| 1367 | " VALUES (%s, %s) ") |
|
| 1368 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1369 | cnx.commit() |
|
| 1370 | cursor.close() |
|
| 1371 | cnx.close() |
|
| 1372 | ||
| 1373 | resp.status = falcon.HTTP_201 |
|
| 1374 | resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1375 | ||
| 1376 | ||
| 1377 | class ShopfloorSensorItem: |
|
| @@ 503-617 (lines=115) @@ | ||
| 500 | resp.status = falcon.HTTP_200 |
|
| 501 | ||
| 502 | ||
| 503 | class ShopfloorEquipmentCollection: |
|
| 504 | def __init__(self): |
|
| 505 | pass |
|
| 506 | ||
| 507 | @staticmethod |
|
| 508 | def on_options(req, resp, id_): |
|
| 509 | resp.status = falcon.HTTP_200 |
|
| 510 | _ = req |
|
| 511 | _ = id_ |
|
| 512 | ||
| 513 | @staticmethod |
|
| 514 | def on_get(req, resp, id_): |
|
| 515 | if 'API-KEY' not in req.headers or \ |
|
| 516 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 517 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 518 | access_control(req) |
|
| 519 | else: |
|
| 520 | api_key_control(req) |
|
| 521 | if not id_.isdigit() or int(id_) <= 0: |
|
| 522 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 523 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 524 | ||
| 525 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 526 | cursor = cnx.cursor() |
|
| 527 | ||
| 528 | cursor.execute(" SELECT name " |
|
| 529 | " FROM tbl_shopfloors " |
|
| 530 | " WHERE id = %s ", (id_,)) |
|
| 531 | if cursor.fetchone() is None: |
|
| 532 | cursor.close() |
|
| 533 | cnx.close() |
|
| 534 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 535 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 536 | ||
| 537 | query = (" SELECT e.id, e.name, e.uuid " |
|
| 538 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
|
| 539 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
|
| 540 | " ORDER BY e.id ") |
|
| 541 | cursor.execute(query, (id_,)) |
|
| 542 | rows = cursor.fetchall() |
|
| 543 | ||
| 544 | result = list() |
|
| 545 | if rows is not None and len(rows) > 0: |
|
| 546 | for row in rows: |
|
| 547 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 548 | result.append(meta_result) |
|
| 549 | ||
| 550 | resp.text = json.dumps(result) |
|
| 551 | ||
| 552 | @staticmethod |
|
| 553 | @user_logger |
|
| 554 | def on_post(req, resp, id_): |
|
| 555 | """Handles POST requests""" |
|
| 556 | admin_control(req) |
|
| 557 | try: |
|
| 558 | raw_json = req.stream.read().decode('utf-8') |
|
| 559 | except Exception as ex: |
|
| 560 | print(ex) |
|
| 561 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 562 | title='API.BAD_REQUEST', |
|
| 563 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 564 | ||
| 565 | if not id_.isdigit() or int(id_) <= 0: |
|
| 566 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 567 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 568 | ||
| 569 | new_values = json.loads(raw_json) |
|
| 570 | ||
| 571 | if 'equipment_id' not in new_values['data'].keys() or \ |
|
| 572 | not isinstance(new_values['data']['equipment_id'], int) or \ |
|
| 573 | new_values['data']['equipment_id'] <= 0: |
|
| 574 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 575 | description='API.INVALID_EQUIPMENT_ID') |
|
| 576 | equipment_id = new_values['data']['equipment_id'] |
|
| 577 | ||
| 578 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 579 | cursor = cnx.cursor() |
|
| 580 | ||
| 581 | cursor.execute(" SELECT name " |
|
| 582 | " from tbl_shopfloors " |
|
| 583 | " WHERE id = %s ", (id_,)) |
|
| 584 | if cursor.fetchone() is None: |
|
| 585 | cursor.close() |
|
| 586 | cnx.close() |
|
| 587 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 588 | description='API.SHOPFLOOR_NOT_FOUND') |
|
| 589 | ||
| 590 | cursor.execute(" SELECT name " |
|
| 591 | " FROM tbl_equipments " |
|
| 592 | " WHERE id = %s ", (equipment_id,)) |
|
| 593 | if cursor.fetchone() is None: |
|
| 594 | cursor.close() |
|
| 595 | cnx.close() |
|
| 596 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 597 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 598 | ||
| 599 | query = (" SELECT id " |
|
| 600 | " FROM tbl_shopfloors_equipments " |
|
| 601 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
|
| 602 | cursor.execute(query, (id_, equipment_id,)) |
|
| 603 | if cursor.fetchone() is not None: |
|
| 604 | cursor.close() |
|
| 605 | cnx.close() |
|
| 606 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 607 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
|
| 608 | ||
| 609 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
|
| 610 | " VALUES (%s, %s) ") |
|
| 611 | cursor.execute(add_row, (id_, equipment_id,)) |
|
| 612 | cnx.commit() |
|
| 613 | cursor.close() |
|
| 614 | cnx.close() |
|
| 615 | ||
| 616 | resp.status = falcon.HTTP_201 |
|
| 617 | resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id) |
|
| 618 | ||
| 619 | ||
| 620 | class ShopfloorEquipmentItem: |
|
| @@ 1745-1859 (lines=115) @@ | ||
| 1742 | resp.status = falcon.HTTP_204 |
|
| 1743 | ||
| 1744 | ||
| 1745 | class StoreCommandCollection: |
|
| 1746 | def __init__(self): |
|
| 1747 | pass |
|
| 1748 | ||
| 1749 | @staticmethod |
|
| 1750 | def on_options(req, resp, id_): |
|
| 1751 | _ = req |
|
| 1752 | resp.status = falcon.HTTP_200 |
|
| 1753 | _ = id_ |
|
| 1754 | ||
| 1755 | @staticmethod |
|
| 1756 | def on_get(req, resp, id_): |
|
| 1757 | if 'API-KEY' not in req.headers or \ |
|
| 1758 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1759 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1760 | access_control(req) |
|
| 1761 | else: |
|
| 1762 | api_key_control(req) |
|
| 1763 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1764 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1765 | description='API.INVALID_STORE_ID') |
|
| 1766 | ||
| 1767 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1768 | cursor = cnx.cursor() |
|
| 1769 | ||
| 1770 | cursor.execute(" SELECT name " |
|
| 1771 | " FROM tbl_stores " |
|
| 1772 | " WHERE id = %s ", (id_,)) |
|
| 1773 | if cursor.fetchone() is None: |
|
| 1774 | cursor.close() |
|
| 1775 | cnx.close() |
|
| 1776 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1777 | description='API.STORE_NOT_FOUND') |
|
| 1778 | ||
| 1779 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1780 | " FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
|
| 1781 | " WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
|
| 1782 | " ORDER BY c.id ") |
|
| 1783 | cursor.execute(query, (id_,)) |
|
| 1784 | rows = cursor.fetchall() |
|
| 1785 | ||
| 1786 | result = list() |
|
| 1787 | if rows is not None and len(rows) > 0: |
|
| 1788 | for row in rows: |
|
| 1789 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1790 | result.append(meta_result) |
|
| 1791 | ||
| 1792 | resp.text = json.dumps(result) |
|
| 1793 | ||
| 1794 | @staticmethod |
|
| 1795 | @user_logger |
|
| 1796 | def on_post(req, resp, id_): |
|
| 1797 | """Handles POST requests""" |
|
| 1798 | admin_control(req) |
|
| 1799 | try: |
|
| 1800 | raw_json = req.stream.read().decode('utf-8') |
|
| 1801 | except Exception as ex: |
|
| 1802 | print(str(ex)) |
|
| 1803 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1804 | title='API.BAD_REQUEST', |
|
| 1805 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1806 | ||
| 1807 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1808 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1809 | description='API.INVALID_STORE_ID') |
|
| 1810 | ||
| 1811 | new_values = json.loads(raw_json) |
|
| 1812 | ||
| 1813 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1814 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1815 | new_values['data']['command_id'] <= 0: |
|
| 1816 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1817 | description='API.INVALID_COMMAND_ID') |
|
| 1818 | command_id = new_values['data']['command_id'] |
|
| 1819 | ||
| 1820 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1821 | cursor = cnx.cursor() |
|
| 1822 | ||
| 1823 | cursor.execute(" SELECT name " |
|
| 1824 | " from tbl_stores " |
|
| 1825 | " WHERE id = %s ", (id_,)) |
|
| 1826 | if cursor.fetchone() is None: |
|
| 1827 | cursor.close() |
|
| 1828 | cnx.close() |
|
| 1829 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1830 | description='API.STORE_NOT_FOUND') |
|
| 1831 | ||
| 1832 | cursor.execute(" SELECT name " |
|
| 1833 | " FROM tbl_commands " |
|
| 1834 | " WHERE id = %s ", (command_id,)) |
|
| 1835 | if cursor.fetchone() is None: |
|
| 1836 | cursor.close() |
|
| 1837 | cnx.close() |
|
| 1838 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1839 | description='API.COMMAND_NOT_FOUND') |
|
| 1840 | ||
| 1841 | query = (" SELECT id " |
|
| 1842 | " FROM tbl_stores_commands " |
|
| 1843 | " WHERE store_id = %s AND command_id = %s") |
|
| 1844 | cursor.execute(query, (id_, command_id,)) |
|
| 1845 | if cursor.fetchone() is not None: |
|
| 1846 | cursor.close() |
|
| 1847 | cnx.close() |
|
| 1848 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1849 | description='API.STORE_COMMAND_RELATION_EXISTS') |
|
| 1850 | ||
| 1851 | add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
|
| 1852 | " VALUES (%s, %s) ") |
|
| 1853 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1854 | cnx.commit() |
|
| 1855 | cursor.close() |
|
| 1856 | cnx.close() |
|
| 1857 | ||
| 1858 | resp.status = falcon.HTTP_201 |
|
| 1859 | resp.location = '/stores/' + str(id_) + '/commands/' + str(command_id) |
|
| 1860 | ||
| 1861 | ||
| 1862 | class StoreCommandItem: |
|
| @@ 1566-1680 (lines=115) @@ | ||
| 1563 | resp.status = falcon.HTTP_204 |
|
| 1564 | ||
| 1565 | ||
| 1566 | class StoreWorkingCalendarCollection: |
|
| 1567 | def __init__(self): |
|
| 1568 | pass |
|
| 1569 | ||
| 1570 | @staticmethod |
|
| 1571 | def on_options(req, resp, id_): |
|
| 1572 | _ = req |
|
| 1573 | resp.status = falcon.HTTP_200 |
|
| 1574 | _ = id_ |
|
| 1575 | ||
| 1576 | @staticmethod |
|
| 1577 | def on_get(req, resp, id_): |
|
| 1578 | if 'API-KEY' not in req.headers or \ |
|
| 1579 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1580 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1581 | access_control(req) |
|
| 1582 | else: |
|
| 1583 | api_key_control(req) |
|
| 1584 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1585 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1586 | description='API.INVALID_STORE_ID') |
|
| 1587 | ||
| 1588 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1589 | cursor = cnx.cursor() |
|
| 1590 | ||
| 1591 | cursor.execute(" SELECT name " |
|
| 1592 | " FROM tbl_stores " |
|
| 1593 | " WHERE id = %s ", (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.STORE_NOT_FOUND') |
|
| 1599 | ||
| 1600 | query = (" SELECT wc.id, wc.name, wc.description " |
|
| 1601 | " FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
|
| 1602 | " WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
|
| 1603 | " ORDER BY wc.id ") |
|
| 1604 | cursor.execute(query, (id_,)) |
|
| 1605 | rows = cursor.fetchall() |
|
| 1606 | ||
| 1607 | result = list() |
|
| 1608 | if rows is not None and len(rows) > 0: |
|
| 1609 | for row in rows: |
|
| 1610 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
|
| 1611 | result.append(meta_result) |
|
| 1612 | ||
| 1613 | resp.text = json.dumps(result) |
|
| 1614 | ||
| 1615 | @staticmethod |
|
| 1616 | @user_logger |
|
| 1617 | def on_post(req, resp, id_): |
|
| 1618 | """Handles POST requests""" |
|
| 1619 | admin_control(req) |
|
| 1620 | try: |
|
| 1621 | raw_json = req.stream.read().decode('utf-8') |
|
| 1622 | except Exception as ex: |
|
| 1623 | print(str(ex)) |
|
| 1624 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1625 | title='API.BAD_REQUEST', |
|
| 1626 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1627 | ||
| 1628 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1629 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1630 | description='API.INVALID_STORE_ID') |
|
| 1631 | ||
| 1632 | new_values = json.loads(raw_json) |
|
| 1633 | ||
| 1634 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
|
| 1635 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
|
| 1636 | new_values['data']['working_calendar_id'] <= 0: |
|
| 1637 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1638 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 1639 | working_calendar_id = new_values['data']['working_calendar_id'] |
|
| 1640 | ||
| 1641 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1642 | cursor = cnx.cursor() |
|
| 1643 | ||
| 1644 | cursor.execute(" SELECT name " |
|
| 1645 | " from tbl_stores " |
|
| 1646 | " WHERE id = %s ", (id_,)) |
|
| 1647 | if cursor.fetchone() is None: |
|
| 1648 | cursor.close() |
|
| 1649 | cnx.close() |
|
| 1650 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1651 | description='API.STORE_NOT_FOUND') |
|
| 1652 | ||
| 1653 | cursor.execute(" SELECT name " |
|
| 1654 | " FROM tbl_working_calendars " |
|
| 1655 | " WHERE id = %s ", (working_calendar_id,)) |
|
| 1656 | if cursor.fetchone() is None: |
|
| 1657 | cursor.close() |
|
| 1658 | cnx.close() |
|
| 1659 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1660 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 1661 | ||
| 1662 | query = (" SELECT id " |
|
| 1663 | " FROM tbl_stores_working_calendars " |
|
| 1664 | " WHERE store_id = %s AND working_calendar_id = %s") |
|
| 1665 | cursor.execute(query, (id_, working_calendar_id,)) |
|
| 1666 | if cursor.fetchone() is not None: |
|
| 1667 | cursor.close() |
|
| 1668 | cnx.close() |
|
| 1669 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1670 | description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
|
| 1671 | ||
| 1672 | add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
|
| 1673 | " VALUES (%s, %s) ") |
|
| 1674 | cursor.execute(add_row, (id_, working_calendar_id,)) |
|
| 1675 | cnx.commit() |
|
| 1676 | cursor.close() |
|
| 1677 | cnx.close() |
|
| 1678 | ||
| 1679 | resp.status = falcon.HTTP_201 |
|
| 1680 | resp.location = '/stores/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
|
| 1681 | ||
| 1682 | ||
| 1683 | class StoreWorkingCalendarItem: |
|
| @@ 1196-1310 (lines=115) @@ | ||
| 1193 | resp.status = falcon.HTTP_204 |
|
| 1194 | ||
| 1195 | ||
| 1196 | class StoreSensorCollection: |
|
| 1197 | def __init__(self): |
|
| 1198 | pass |
|
| 1199 | ||
| 1200 | @staticmethod |
|
| 1201 | def on_options(req, resp, id_): |
|
| 1202 | _ = req |
|
| 1203 | resp.status = falcon.HTTP_200 |
|
| 1204 | _ = id_ |
|
| 1205 | ||
| 1206 | @staticmethod |
|
| 1207 | def on_get(req, resp, id_): |
|
| 1208 | if 'API-KEY' not in req.headers or \ |
|
| 1209 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1210 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1211 | access_control(req) |
|
| 1212 | else: |
|
| 1213 | api_key_control(req) |
|
| 1214 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1215 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1216 | description='API.INVALID_STORE_ID') |
|
| 1217 | ||
| 1218 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1219 | cursor = cnx.cursor() |
|
| 1220 | ||
| 1221 | cursor.execute(" SELECT name " |
|
| 1222 | " FROM tbl_stores " |
|
| 1223 | " WHERE id = %s ", (id_,)) |
|
| 1224 | if cursor.fetchone() is None: |
|
| 1225 | cursor.close() |
|
| 1226 | cnx.close() |
|
| 1227 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1228 | description='API.STORE_NOT_FOUND') |
|
| 1229 | ||
| 1230 | query = (" SELECT s.id, s.name, s.uuid " |
|
| 1231 | " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
|
| 1232 | " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
|
| 1233 | " ORDER BY s.id ") |
|
| 1234 | cursor.execute(query, (id_,)) |
|
| 1235 | rows = cursor.fetchall() |
|
| 1236 | ||
| 1237 | result = list() |
|
| 1238 | if rows is not None and len(rows) > 0: |
|
| 1239 | for row in rows: |
|
| 1240 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1241 | result.append(meta_result) |
|
| 1242 | ||
| 1243 | resp.text = json.dumps(result) |
|
| 1244 | ||
| 1245 | @staticmethod |
|
| 1246 | @user_logger |
|
| 1247 | def on_post(req, resp, id_): |
|
| 1248 | """Handles POST requests""" |
|
| 1249 | admin_control(req) |
|
| 1250 | try: |
|
| 1251 | raw_json = req.stream.read().decode('utf-8') |
|
| 1252 | except Exception as ex: |
|
| 1253 | print(str(ex)) |
|
| 1254 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1255 | title='API.BAD_REQUEST', |
|
| 1256 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1257 | ||
| 1258 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1259 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1260 | description='API.INVALID_STORE_ID') |
|
| 1261 | ||
| 1262 | new_values = json.loads(raw_json) |
|
| 1263 | ||
| 1264 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
| 1265 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
| 1266 | new_values['data']['sensor_id'] <= 0: |
|
| 1267 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1268 | description='API.INVALID_SENSOR_ID') |
|
| 1269 | sensor_id = new_values['data']['sensor_id'] |
|
| 1270 | ||
| 1271 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1272 | cursor = cnx.cursor() |
|
| 1273 | ||
| 1274 | cursor.execute(" SELECT name " |
|
| 1275 | " from tbl_stores " |
|
| 1276 | " WHERE id = %s ", (id_,)) |
|
| 1277 | if cursor.fetchone() is None: |
|
| 1278 | cursor.close() |
|
| 1279 | cnx.close() |
|
| 1280 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1281 | description='API.STORE_NOT_FOUND') |
|
| 1282 | ||
| 1283 | cursor.execute(" SELECT name " |
|
| 1284 | " FROM tbl_sensors " |
|
| 1285 | " WHERE id = %s ", (sensor_id,)) |
|
| 1286 | if cursor.fetchone() is None: |
|
| 1287 | cursor.close() |
|
| 1288 | cnx.close() |
|
| 1289 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1290 | description='API.SENSOR_NOT_FOUND') |
|
| 1291 | ||
| 1292 | query = (" SELECT id " |
|
| 1293 | " FROM tbl_stores_sensors " |
|
| 1294 | " WHERE store_id = %s AND sensor_id = %s") |
|
| 1295 | cursor.execute(query, (id_, sensor_id,)) |
|
| 1296 | if cursor.fetchone() is not None: |
|
| 1297 | cursor.close() |
|
| 1298 | cnx.close() |
|
| 1299 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1300 | description='API.STORE_SENSOR_RELATION_EXISTS') |
|
| 1301 | ||
| 1302 | add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
|
| 1303 | " VALUES (%s, %s) ") |
|
| 1304 | cursor.execute(add_row, (id_, sensor_id,)) |
|
| 1305 | cnx.commit() |
|
| 1306 | cursor.close() |
|
| 1307 | cnx.close() |
|
| 1308 | ||
| 1309 | resp.status = falcon.HTTP_201 |
|
| 1310 | resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id) |
|
| 1311 | ||
| 1312 | ||
| 1313 | class StoreSensorItem: |
|
| @@ 1258-1372 (lines=115) @@ | ||
| 1255 | resp.status = falcon.HTTP_204 |
|
| 1256 | ||
| 1257 | ||
| 1258 | class MeterCommandCollection: |
|
| 1259 | def __init__(self): |
|
| 1260 | pass |
|
| 1261 | ||
| 1262 | @staticmethod |
|
| 1263 | def on_options(req, resp, id_): |
|
| 1264 | _ = req |
|
| 1265 | _ = id_ |
|
| 1266 | resp.status = falcon.HTTP_200 |
|
| 1267 | ||
| 1268 | @staticmethod |
|
| 1269 | def on_get(req, resp, id_): |
|
| 1270 | if 'API-KEY' not in req.headers or \ |
|
| 1271 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1272 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1273 | access_control(req) |
|
| 1274 | else: |
|
| 1275 | api_key_control(req) |
|
| 1276 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1277 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1278 | description='API.INVALID_METER_ID') |
|
| 1279 | ||
| 1280 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1281 | cursor = cnx.cursor() |
|
| 1282 | ||
| 1283 | cursor.execute(" SELECT name " |
|
| 1284 | " FROM tbl_meters " |
|
| 1285 | " WHERE id = %s ", (id_,)) |
|
| 1286 | if cursor.fetchone() is None: |
|
| 1287 | cursor.close() |
|
| 1288 | cnx.close() |
|
| 1289 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1290 | description='API.METER_NOT_FOUND') |
|
| 1291 | ||
| 1292 | query = (" SELECT c.id, c.name, c.uuid " |
|
| 1293 | " FROM tbl_meters m, tbl_meters_commands mc, tbl_commands c " |
|
| 1294 | " WHERE mc.meter_id = m.id AND c.id = mc.command_id AND m.id = %s " |
|
| 1295 | " ORDER BY c.id ") |
|
| 1296 | cursor.execute(query, (id_,)) |
|
| 1297 | rows = cursor.fetchall() |
|
| 1298 | ||
| 1299 | result = list() |
|
| 1300 | if rows is not None and len(rows) > 0: |
|
| 1301 | for row in rows: |
|
| 1302 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1303 | result.append(meta_result) |
|
| 1304 | ||
| 1305 | resp.text = json.dumps(result) |
|
| 1306 | ||
| 1307 | @staticmethod |
|
| 1308 | @user_logger |
|
| 1309 | def on_post(req, resp, id_): |
|
| 1310 | """Handles POST requests""" |
|
| 1311 | admin_control(req) |
|
| 1312 | try: |
|
| 1313 | raw_json = req.stream.read().decode('utf-8') |
|
| 1314 | except Exception as ex: |
|
| 1315 | print(ex) |
|
| 1316 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1317 | title='API.BAD_REQUEST', |
|
| 1318 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1319 | ||
| 1320 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1322 | description='API.INVALID_METER_ID') |
|
| 1323 | ||
| 1324 | new_values = json.loads(raw_json) |
|
| 1325 | ||
| 1326 | if 'command_id' not in new_values['data'].keys() or \ |
|
| 1327 | not isinstance(new_values['data']['command_id'], int) or \ |
|
| 1328 | new_values['data']['command_id'] <= 0: |
|
| 1329 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1330 | description='API.INVALID_COMMAND_ID') |
|
| 1331 | command_id = new_values['data']['command_id'] |
|
| 1332 | ||
| 1333 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1334 | cursor = cnx.cursor() |
|
| 1335 | ||
| 1336 | cursor.execute(" SELECT name " |
|
| 1337 | " from tbl_meters " |
|
| 1338 | " WHERE id = %s ", (id_,)) |
|
| 1339 | if cursor.fetchone() is None: |
|
| 1340 | cursor.close() |
|
| 1341 | cnx.close() |
|
| 1342 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1343 | description='API.METER_NOT_FOUND') |
|
| 1344 | ||
| 1345 | cursor.execute(" SELECT name " |
|
| 1346 | " FROM tbl_commands " |
|
| 1347 | " WHERE id = %s ", (command_id,)) |
|
| 1348 | if cursor.fetchone() is None: |
|
| 1349 | cursor.close() |
|
| 1350 | cnx.close() |
|
| 1351 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1352 | description='API.COMMAND_NOT_FOUND') |
|
| 1353 | ||
| 1354 | query = (" SELECT id " |
|
| 1355 | " FROM tbl_meters_commands " |
|
| 1356 | " WHERE meter_id = %s AND command_id = %s") |
|
| 1357 | cursor.execute(query, (id_, command_id,)) |
|
| 1358 | if cursor.fetchone() is not None: |
|
| 1359 | cursor.close() |
|
| 1360 | cnx.close() |
|
| 1361 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1362 | description='API.METER_COMMAND_RELATION_EXISTS') |
|
| 1363 | ||
| 1364 | add_row = (" INSERT INTO tbl_meters_commands (meter_id, command_id) " |
|
| 1365 | " VALUES (%s, %s) ") |
|
| 1366 | cursor.execute(add_row, (id_, command_id,)) |
|
| 1367 | cnx.commit() |
|
| 1368 | cursor.close() |
|
| 1369 | cnx.close() |
|
| 1370 | ||
| 1371 | resp.status = falcon.HTTP_201 |
|
| 1372 | resp.location = '/meters/' + str(id_) + '/commands/' + str(command_id) |
|
| 1373 | ||
| 1374 | ||
| 1375 | class MeterCommandItem: |
|
| @@ 447-561 (lines=115) @@ | ||
| 444 | resp.status = falcon.HTTP_200 |
|
| 445 | ||
| 446 | ||
| 447 | class VirtualPowerPlantMicrogridCollection: |
|
| 448 | def __init__(self): |
|
| 449 | pass |
|
| 450 | ||
| 451 | @staticmethod |
|
| 452 | def on_options(req, resp, id_): |
|
| 453 | _ = req |
|
| 454 | resp.status = falcon.HTTP_200 |
|
| 455 | _ = id_ |
|
| 456 | ||
| 457 | @staticmethod |
|
| 458 | def on_get(req, resp, id_): |
|
| 459 | if 'API-KEY' not in req.headers or \ |
|
| 460 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 461 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 462 | access_control(req) |
|
| 463 | else: |
|
| 464 | api_key_control(req) |
|
| 465 | if not id_.isdigit() or int(id_) <= 0: |
|
| 466 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 467 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
| 468 | ||
| 469 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 470 | cursor = cnx.cursor() |
|
| 471 | ||
| 472 | cursor.execute(" SELECT name " |
|
| 473 | " FROM tbl_virtual_power_plants " |
|
| 474 | " WHERE id = %s ", (id_,)) |
|
| 475 | if cursor.fetchone() is None: |
|
| 476 | cursor.close() |
|
| 477 | cnx.close() |
|
| 478 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 479 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
| 480 | ||
| 481 | query = (" SELECT m.id, m.name, m.uuid " |
|
| 482 | " FROM tbl_virtual_power_plants_microgrids vppm, tbl_microgrids m " |
|
| 483 | " WHERE m.id = vppm.microgrid_id AND vppm.virtual_power_plant_id = %s " |
|
| 484 | " ORDER BY m.id ") |
|
| 485 | cursor.execute(query, (id_,)) |
|
| 486 | rows = cursor.fetchall() |
|
| 487 | ||
| 488 | result = list() |
|
| 489 | if rows is not None and len(rows) > 0: |
|
| 490 | for row in rows: |
|
| 491 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 492 | result.append(meta_result) |
|
| 493 | ||
| 494 | resp.text = json.dumps(result) |
|
| 495 | ||
| 496 | @staticmethod |
|
| 497 | @user_logger |
|
| 498 | def on_post(req, resp, id_): |
|
| 499 | """Handles POST requests""" |
|
| 500 | admin_control(req) |
|
| 501 | try: |
|
| 502 | raw_json = req.stream.read().decode('utf-8') |
|
| 503 | except Exception as ex: |
|
| 504 | print(str(ex)) |
|
| 505 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 506 | title='API.BAD_REQUEST', |
|
| 507 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 508 | ||
| 509 | if not id_.isdigit() or int(id_) <= 0: |
|
| 510 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 511 | description='API.INVALID_VIRTUAL_POWER_PLANT_ID') |
|
| 512 | ||
| 513 | new_values = json.loads(raw_json) |
|
| 514 | ||
| 515 | if 'microgrid_id' not in new_values['data'].keys() or \ |
|
| 516 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
|
| 517 | new_values['data']['microgrid_id'] <= 0: |
|
| 518 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 519 | description='API.INVALID_MICROGRID_ID') |
|
| 520 | microgrid_id = new_values['data']['microgrid_id'] |
|
| 521 | ||
| 522 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 523 | cursor = cnx.cursor() |
|
| 524 | ||
| 525 | cursor.execute(" SELECT name " |
|
| 526 | " from tbl_virtual_power_plants " |
|
| 527 | " WHERE id = %s ", (id_,)) |
|
| 528 | if cursor.fetchone() is None: |
|
| 529 | cursor.close() |
|
| 530 | cnx.close() |
|
| 531 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 532 | description='API.VIRTUAL_POWER_PLANT_NOT_FOUND') |
|
| 533 | ||
| 534 | cursor.execute(" SELECT name " |
|
| 535 | " FROM tbl_microgrids " |
|
| 536 | " WHERE id = %s ", (microgrid_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.MICROGRID_NOT_FOUND') |
|
| 542 | ||
| 543 | query = (" SELECT id " |
|
| 544 | " FROM tbl_virtual_power_plants_microgrids " |
|
| 545 | " WHERE virtual_power_plant_id = %s AND microgrid_id = %s") |
|
| 546 | cursor.execute(query, (id_, microgrid_id,)) |
|
| 547 | if cursor.fetchone() is not None: |
|
| 548 | cursor.close() |
|
| 549 | cnx.close() |
|
| 550 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 551 | description='API.VIRTUAL_POWER_PLANT_MICROGRID_RELATION_EXISTS') |
|
| 552 | ||
| 553 | add_row = (" INSERT INTO tbl_virtual_power_plants_microgrids (virtual_power_plant_id, microgrid_id) " |
|
| 554 | " VALUES (%s, %s) ") |
|
| 555 | cursor.execute(add_row, (id_, microgrid_id,)) |
|
| 556 | cnx.commit() |
|
| 557 | cursor.close() |
|
| 558 | cnx.close() |
|
| 559 | ||
| 560 | resp.status = falcon.HTTP_201 |
|
| 561 | resp.location = '/virtualpowerplants/' + str(id_) + '/microgrids/' + str(microgrid_id) |
|
| 562 | ||
| 563 | ||
| 564 | class VirtualPowerPlantMicrogridItem: |
|
| @@ 5618-5727 (lines=110) @@ | ||
| 5615 | resp.status = falcon.HTTP_201 |
|
| 5616 | resp.location = '/microgrids/' + str(new_id) |
|
| 5617 | ||
| 5618 | class MicrogridDataSourceCollection: |
|
| 5619 | def __init__(self): |
|
| 5620 | pass |
|
| 5621 | ||
| 5622 | @staticmethod |
|
| 5623 | def on_options(req, resp, id_): |
|
| 5624 | _ = req |
|
| 5625 | _ = id_ |
|
| 5626 | resp.status = falcon.HTTP_200 |
|
| 5627 | ||
| 5628 | @staticmethod |
|
| 5629 | def on_get(req, resp, id_): |
|
| 5630 | if 'API-KEY' not in req.headers or \ |
|
| 5631 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5632 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5633 | access_control(req) |
|
| 5634 | else: |
|
| 5635 | api_key_control(req) |
|
| 5636 | ||
| 5637 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5638 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5639 | description='API.INVALID_MICROGRID_ID') |
|
| 5640 | ||
| 5641 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5642 | cursor = cnx.cursor() |
|
| 5643 | ||
| 5644 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
|
| 5645 | if cursor.fetchone() is None: |
|
| 5646 | cursor.close() |
|
| 5647 | cnx.close() |
|
| 5648 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5649 | description='API.MICROGRID_NOT_FOUND') |
|
| 5650 | ||
| 5651 | query = (" SELECT ds.id, ds.name, ds.uuid " |
|
| 5652 | " FROM tbl_microgrids mg, tbl_microgrids_data_sources mgds, tbl_data_sources ds " |
|
| 5653 | " WHERE mgds.microgrid_id = mg.id AND ds.id = mgds.data_source_id AND mg.id = %s " |
|
| 5654 | " ORDER BY ds.id ") |
|
| 5655 | cursor.execute(query, (id_,)) |
|
| 5656 | rows = cursor.fetchall() |
|
| 5657 | ||
| 5658 | result = list() |
|
| 5659 | if rows is not None and len(rows) > 0: |
|
| 5660 | for row in rows: |
|
| 5661 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5662 | result.append(meta_result) |
|
| 5663 | ||
| 5664 | cursor.close() |
|
| 5665 | cnx.close() |
|
| 5666 | ||
| 5667 | resp.text = json.dumps(result) |
|
| 5668 | ||
| 5669 | @staticmethod |
|
| 5670 | @user_logger |
|
| 5671 | def on_post(req, resp, id_): |
|
| 5672 | admin_control(req) |
|
| 5673 | try: |
|
| 5674 | raw_json = req.stream.read().decode('utf-8') |
|
| 5675 | except Exception as ex: |
|
| 5676 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5677 | title='API.BAD_REQUEST', |
|
| 5678 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5679 | ||
| 5680 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5681 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5682 | description='API.INVALID_MICROGRID_ID') |
|
| 5683 | ||
| 5684 | new_values = json.loads(raw_json) |
|
| 5685 | ||
| 5686 | if 'data_source_id' not in new_values['data'].keys() or \ |
|
| 5687 | not isinstance(new_values['data']['data_source_id'], int) or \ |
|
| 5688 | new_values['data']['data_source_id'] <= 0: |
|
| 5689 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5690 | description='API.INVALID_DATA_SOURCE_ID') |
|
| 5691 | ||
| 5692 | data_source_id = new_values['data']['data_source_id'] |
|
| 5693 | ||
| 5694 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5695 | cursor = cnx.cursor() |
|
| 5696 | ||
| 5697 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
|
| 5698 | if cursor.fetchone() is None: |
|
| 5699 | cursor.close() |
|
| 5700 | cnx.close() |
|
| 5701 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5702 | description='API.MICROGRID_NOT_FOUND') |
|
| 5703 | ||
| 5704 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
|
| 5705 | if cursor.fetchone() is None: |
|
| 5706 | cursor.close() |
|
| 5707 | cnx.close() |
|
| 5708 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5709 | description='API.DATA_SOURCE_NOT_FOUND') |
|
| 5710 | ||
| 5711 | cursor.execute(" SELECT id " |
|
| 5712 | " FROM tbl_microgrids_data_sources " |
|
| 5713 | " WHERE microgrid_id = %s AND data_source_id = %s", (id_, data_source_id)) |
|
| 5714 | if cursor.fetchone() is not None: |
|
| 5715 | cursor.close() |
|
| 5716 | cnx.close() |
|
| 5717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5718 | description='API.MICROGRID_DATA_SOURCE_RELATION_EXISTS') |
|
| 5719 | ||
| 5720 | cursor.execute(" INSERT INTO tbl_microgrids_data_sources (microgrid_id, data_source_id) " |
|
| 5721 | " VALUES (%s, %s) ", (id_, data_source_id)) |
|
| 5722 | cnx.commit() |
|
| 5723 | cursor.close() |
|
| 5724 | cnx.close() |
|
| 5725 | ||
| 5726 | resp.status = falcon.HTTP_201 |
|
| 5727 | resp.location = '/microgrids/' + str(id_) + '/datasources/' + str(data_source_id) |
|
| 5728 | ||
| 5729 | class MicrogridDataSourceItem: |
|
| 5730 | def __init__(self): |
|