import falcon import simplejson as json import mysql.connector import config import uuid from core.userlogger import user_logger class SensorCollection: @staticmethod def __init__(): """"Initializes SensorCollection""" pass @staticmethod def on_options(req, resp): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp): cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) query = (" SELECT id, name, uuid, description " " FROM tbl_sensors " " ORDER BY id ") cursor.execute(query) rows_sensors = cursor.fetchall() result = list() if rows_sensors is not None and len(rows_sensors) > 0: for row in rows_sensors: meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "description": row['description']} result.append(meta_result) cursor.close() cnx.disconnect() resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp): """Handles POST requests""" try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) new_values = json.loads(raw_json) if 'name' not in new_values['data'].keys() or \ not isinstance(new_values['data']['name'], str) or \ len(str.strip(new_values['data']['name'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_NAME') name = str.strip(new_values['data']['name']) if 'description' in new_values['data'].keys() and \ new_values['data']['description'] is not None and \ len(str(new_values['data']['description'])) > 0: description = str.strip(new_values['data']['description']) else: description = None cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE name = %s ", (name,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', description='API.SENSOR_NAME_IS_ALREADY_IN_USE') add_values = (" INSERT INTO tbl_sensors " " (name, uuid, description) " " VALUES (%s, %s, %s) ") cursor.execute(add_values, (name, str(uuid.uuid4()), description)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/sensors/' + str(new_id) class SensorItem: @staticmethod def __init__(): """"Initializes SensorItem""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) query = (" SELECT id, name, uuid, description " " FROM tbl_sensors " " WHERE id = %s ") cursor.execute(query, (id_,)) row = cursor.fetchone() cursor.close() cnx.disconnect() if row is None: raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') else: meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "description": row['description']} resp.text = json.dumps(meta_result) @staticmethod @user_logger def on_delete(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') # check relation with spaces cursor.execute(" SELECT id " " FROM tbl_spaces_sensors " " WHERE sensor_id = %s ", (id_,)) rows_spaces = cursor.fetchall() if rows_spaces is not None and len(rows_spaces) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_SPACES') # check relation with tenants cursor.execute(" SELECT id " " FROM tbl_tenants_sensors " " WHERE sensor_id = %s ", (id_,)) rows_tenants = cursor.fetchall() if rows_tenants is not None and len(rows_tenants) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_TENANTS') # check relation with stores cursor.execute(" SELECT store_id " " FROM tbl_stores_sensors " " WHERE sensor_id = %s ", (id_,)) rows_stores = cursor.fetchall() if rows_stores is not None and len(rows_stores) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_STORES') # check relation with points cursor.execute(" SELECT id " " FROM tbl_sensors_points " " WHERE sensor_id = %s ", (id_,)) rows_points = cursor.fetchall() if rows_points is not None and len(rows_points) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_POINTS') cursor.execute(" DELETE FROM tbl_sensors WHERE id = %s ", (id_,)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204 @staticmethod @user_logger def on_put(req, resp, id_): """Handles PUT requests""" try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') new_values = json.loads(raw_json) if 'name' not in new_values['data'].keys() or \ not isinstance(new_values['data']['name'], str) or \ len(str.strip(new_values['data']['name'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_NAME') name = str.strip(new_values['data']['name']) if 'description' in new_values['data'].keys() and \ new_values['data']['description'] is not None and \ len(str(new_values['data']['description'])) > 0: description = str.strip(new_values['data']['description']) else: description = None cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE name = %s AND id != %s ", (name, id_)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', description='API.SENSOR_NAME_IS_ALREADY_IN_USE') update_row = (" UPDATE tbl_sensors " " SET name = %s, description = %s " " WHERE id = %s ") cursor.execute(update_row, (name, description, id_,)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_200 class SensorPointCollection: @staticmethod def __init__(): """"Initializes SensorPointCollection""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') query = (" SELECT p.id, p.name, " " ds.id, ds.name, ds.uuid, " " p.address " " FROM tbl_points p, tbl_sensors_points sp, tbl_data_sources ds " " WHERE sp.sensor_id = %s AND p.id = sp.point_id AND p.data_source_id = ds.id " " ORDER BY p.name ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: meta_result = {"id": row[0], "name": row[1], "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, "address": row[5]} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') new_values = json.loads(raw_json) cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_sensors " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_points " " WHERE id = %s ", (new_values['data']['point_id'],)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.POINT_NOT_FOUND') query = (" SELECT id " " FROM tbl_sensors_points " " WHERE sensor_id = %s AND point_id = %s") cursor.execute(query, (id_, new_values['data']['point_id'],)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.SENSOR_POINT_RELATION_EXISTS') add_row = (" INSERT INTO tbl_sensors_points (sensor_id, point_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, new_values['data']['point_id'],)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/sensors/' + str(id_) + '/points/' + str(new_values['data']['point_id']) class SensorPointItem: @staticmethod def __init__(): """"Initializes SensorPointItem""" pass @staticmethod def on_options(req, resp, id_, pid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, pid): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') if not pid.isdigit() or int(pid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_POINT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_points " " WHERE id = %s ", (pid,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.POINT_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_sensors_points " " WHERE sensor_id = %s AND point_id = %s ", (id_, pid)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_POINT_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_sensors_points WHERE sensor_id = %s AND point_id = %s ", (id_, pid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204