import falcon import simplejson as json import mysql.connector import config import uuid from core.userlogger import user_logger class StoreCollection: @staticmethod def __init__(): """"Initializes StoreCollection""" 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 " " FROM tbl_store_types ") cursor.execute(query) rows_store_types = cursor.fetchall() store_type_dict = dict() if rows_store_types is not None and len(rows_store_types) > 0: for row in rows_store_types: store_type_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_contacts ") cursor.execute(query) rows_contacts = cursor.fetchall() contact_dict = dict() if rows_contacts is not None and len(rows_contacts) > 0: for row in rows_contacts: contact_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_cost_centers ") cursor.execute(query) rows_cost_centers = cursor.fetchall() cost_center_dict = dict() if rows_cost_centers is not None and len(rows_cost_centers) > 0: for row in rows_cost_centers: cost_center_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid, " " address, latitude, longitude, area, store_type_id, " " is_input_counted, contact_id, cost_center_id, description " " FROM tbl_stores " " ORDER BY id ") cursor.execute(query) rows_spaces = cursor.fetchall() result = list() if rows_spaces is not None and len(rows_spaces) > 0: for row in rows_spaces: store_type = store_type_dict.get(row['store_type_id'], None) contact = contact_dict.get(row['contact_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "address": row['address'], "latitude": row['latitude'], "longitude": row['longitude'], "area": row['area'], "store_type": store_type, "is_input_counted": bool(row['is_input_counted']), "contact": contact, "cost_center": cost_center, "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_STORE_NAME') name = str.strip(new_values['data']['name']) if 'address' not in new_values['data'].keys() or \ not isinstance(new_values['data']['address'], str) or \ len(str.strip(new_values['data']['address'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_ADDRESS_VALUE') address = str.strip(new_values['data']['address']) if 'latitude' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['latitude'], float) or isinstance(new_values['data']['latitude'], int)) or \ new_values['data']['latitude'] < -90.0 or \ new_values['data']['latitude'] > 90.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LATITUDE_VALUE') latitude = new_values['data']['latitude'] if 'longitude' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['longitude'], float) or isinstance(new_values['data']['longitude'], int)) or \ new_values['data']['longitude'] < -180.0 or \ new_values['data']['longitude'] > 180.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LONGITUDE_VALUE') longitude = new_values['data']['longitude'] if 'area' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['area'], float) or isinstance(new_values['data']['area'], int)) or \ new_values['data']['area'] <= 0.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_AREA_VALUE') area = new_values['data']['area'] if 'store_type_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['store_type_id'], int) or \ new_values['data']['store_type_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_TYPE_ID') store_type_id = new_values['data']['store_type_id'] if 'is_input_counted' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_input_counted'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_INPUT_COUNTED_VALUE') is_input_counted = new_values['data']['is_input_counted'] if 'contact_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['contact_id'], int) or \ new_values['data']['contact_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_CONTACT_ID') contact_id = new_values['data']['contact_id'] if 'cost_center_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['cost_center_id'], int) or \ new_values['data']['cost_center_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COST_CENTER_ID') cost_center_id = new_values['data']['cost_center_id'] 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_stores " " 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.STORE_NAME_IS_ALREADY_IN_USE') cursor.execute(" SELECT name " " FROM tbl_store_types " " WHERE id = %s ", (store_type_id,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_TYPE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_contacts " " WHERE id = %s ", (new_values['data']['contact_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.CONTACT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_cost_centers " " WHERE id = %s ", (new_values['data']['cost_center_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.COST_CENTER_NOT_FOUND') add_values = (" INSERT INTO tbl_stores " " (name, uuid, address, latitude, longitude, area, store_type_id, " " is_input_counted, " " contact_id, cost_center_id, description) " " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") cursor.execute(add_values, (name, str(uuid.uuid4()), address, latitude, longitude, area, store_type_id, is_input_counted, contact_id, cost_center_id, description)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(new_id) class StoreItem: @staticmethod def __init__(): """"Initializes StoreItem""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) query = (" SELECT id, name, uuid " " FROM tbl_store_types ") cursor.execute(query) rows_store_types = cursor.fetchall() store_type_dict = dict() if rows_store_types is not None and len(rows_store_types) > 0: for row in rows_store_types: store_type_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_contacts ") cursor.execute(query) rows_contacts = cursor.fetchall() contact_dict = dict() if rows_contacts is not None and len(rows_contacts) > 0: for row in rows_contacts: contact_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_cost_centers ") cursor.execute(query) rows_cost_centers = cursor.fetchall() cost_center_dict = dict() if rows_cost_centers is not None and len(rows_cost_centers) > 0: for row in rows_cost_centers: cost_center_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid, " " address, latitude, longitude, area, store_type_id," " is_input_counted, " " contact_id, cost_center_id, description " " FROM tbl_stores " " 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.STORE_NOT_FOUND') else: store_type = store_type_dict.get(row['store_type_id'], None) contact = contact_dict.get(row['contact_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "address": row['address'], "latitude": row['latitude'], "longitude": row['longitude'], "area": row['area'], "store_type": store_type, "is_input_counted": bool(row['is_input_counted']), "contact": contact, "cost_center": cost_center, "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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') # check relation with space cursor.execute(" SELECT space_id " " FROM tbl_spaces_stores " " WHERE store_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 meter cursor.execute(" SELECT meter_id " " FROM tbl_stores_meters " " WHERE store_id = %s ", (id_,)) rows_meters = cursor.fetchall() if rows_meters is not None and len(rows_meters) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_METERS') # check relation with offline meter cursor.execute(" SELECT offline_meter_id " " FROM tbl_stores_offline_meters " " WHERE store_id = %s ", (id_,)) rows_offline_meters = cursor.fetchall() if rows_offline_meters is not None and len(rows_offline_meters) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') # check relation with points cursor.execute(" SELECT point_id " " FROM tbl_stores_points " " WHERE store_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') # check relation with sensor cursor.execute(" SELECT sensor_id " " FROM tbl_stores_sensors " " WHERE store_id = %s ", (id_,)) rows_sensors = cursor.fetchall() if rows_sensors is not None and len(rows_sensors) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_SENSORS') # check relation with virtual meter cursor.execute(" SELECT virtual_meter_id " " FROM tbl_stores_virtual_meters " " WHERE store_id = %s ", (id_,)) rows_virtual_meters = cursor.fetchall() if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_VIRTUAL_METERS') cursor.execute(" DELETE FROM tbl_stores 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_STORE_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_STORE_NAME') name = str.strip(new_values['data']['name']) if 'address' not in new_values['data'].keys() or \ not isinstance(new_values['data']['address'], str) or \ len(str.strip(new_values['data']['address'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_ADDRESS_VALUE') address = str.strip(new_values['data']['address']) if 'latitude' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['latitude'], float) or isinstance(new_values['data']['latitude'], int)) or \ new_values['data']['latitude'] < -90.0 or \ new_values['data']['latitude'] > 90.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LATITUDE_VALUE') latitude = new_values['data']['latitude'] if 'longitude' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['longitude'], float) or isinstance(new_values['data']['longitude'], int)) or \ new_values['data']['longitude'] < -180.0 or \ new_values['data']['longitude'] > 180.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LONGITUDE_VALUE') longitude = new_values['data']['longitude'] if 'area' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['area'], float) or isinstance(new_values['data']['area'], int)) or \ new_values['data']['area'] <= 0.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_AREA_VALUE') area = new_values['data']['area'] if 'store_type_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['store_type_id'], int) or \ new_values['data']['store_type_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_TYPE_ID') store_type_id = new_values['data']['store_type_id'] if 'is_input_counted' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_input_counted'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_INPUT_COUNTED_VALUE') is_input_counted = new_values['data']['is_input_counted'] if 'contact_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['contact_id'], int) or \ new_values['data']['contact_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_CONTACT_ID') contact_id = new_values['data']['contact_id'] if 'cost_center_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['cost_center_id'], int) or \ new_values['data']['cost_center_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COST_CENTER_ID') cost_center_id = new_values['data']['cost_center_id'] 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_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NAME_IS_ALREADY_IN_USE') cursor.execute(" SELECT name " " FROM tbl_store_types " " WHERE id = %s ", (store_type_id,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_TYPE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_contacts " " WHERE id = %s ", (new_values['data']['contact_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.CONTACT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_cost_centers " " WHERE id = %s ", (new_values['data']['cost_center_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.COST_CENTER_NOT_FOUND') update_row = (" UPDATE tbl_stores " " SET name = %s, address = %s, latitude = %s, longitude = %s, area = %s, " " store_type_id = %s, is_input_counted = %s, " " contact_id = %s, cost_center_id = %s, " " description = %s " " WHERE id = %s ") cursor.execute(update_row, (name, address, latitude, longitude, area, store_type_id, is_input_counted, contact_id, cost_center_id, description, id_)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_200 class StoreMeterCollection: @staticmethod def __init__(): """"Initializes StoreMeterCollection""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " " WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} 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_STORE_ID') new_values = json.loads(raw_json) if 'meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['meter_id'], int) or \ new_values['data']['meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') meter_id = new_values['data']['meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_meters " " WHERE id = %s ", (meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_stores_meters " " WHERE store_id = %s AND meter_id = %s") cursor.execute(query, (id_, meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.STORE_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(id_) + '/meters/' + str(meter_id) class StoreMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_stores_meters " " WHERE store_id = %s AND meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_stores_meters WHERE store_id = %s AND meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204 class StoreOfflineMeterCollection: @staticmethod def __init__(): """Initializes Class""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " " WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} 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_STORE_ID') new_values = json.loads(raw_json) if 'offline_meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['offline_meter_id'], int) or \ new_values['data']['offline_meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') offline_meter_id = new_values['data']['offline_meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_offline_meters " " WHERE id = %s ", (offline_meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_stores_offline_meters " " WHERE store_id = %s AND offline_meter_id = %s") cursor.execute(query, (id_, offline_meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.STORE_OFFLINE_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, offline_meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) class StoreOfflineMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_offline_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_stores_offline_meters " " WHERE store_id = %s AND offline_meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_OFFLINE_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_stores_offline_meters " " WHERE store_id = %s AND offline_meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204 class StorePointCollection: @staticmethod def __init__(): """Initializes Class""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_data_sources ") cursor.execute(query) rows_data_sources = cursor.fetchall() data_source_dict = dict() if rows_data_sources is not None and len(rows_data_sources) > 0: for row in rows_data_sources: data_source_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT p.id, p.name, p.data_source_id " " FROM tbl_stores t, tbl_stores_points tp, tbl_points p " " WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " " ORDER BY p.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: data_source = data_source_dict.get(row['data_source_id'], None) meta_result = {"id": row['id'], "name": row['name'], "data_source": data_source} 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_STORE_ID') new_values = json.loads(raw_json) if 'point_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['point_id'], int) or \ new_values['data']['point_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_POINT_ID') point_id = new_values['data']['point_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_points " " WHERE id = %s ", (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_stores_points " " WHERE store_id = %s AND point_id = %s") cursor.execute(query, (id_, point_id,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.STORE_POINT_RELATION_EXISTS') add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, point_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(id_) + '/points/' + str(point_id) class StorePointItem: @staticmethod def __init__(): """Initializes Class""" 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_STORE_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_stores " " 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.STORE_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_stores_points " " WHERE store_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.STORE_POINT_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_stores_points " " WHERE store_id = %s AND point_id = %s ", (id_, pid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204 class StoreSensorCollection: @staticmethod def __init__(): """Initializes Class""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') query = (" SELECT s.id, s.name, s.uuid " " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " " ORDER BY s.id ") 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], "uuid": row[2]} 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_STORE_ID') new_values = json.loads(raw_json) if 'sensor_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['sensor_id'], int) or \ new_values['data']['sensor_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') sensor_id = new_values['data']['sensor_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (sensor_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 id " " FROM tbl_stores_sensors " " WHERE store_id = %s AND sensor_id = %s") cursor.execute(query, (id_, sensor_id,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.STORE_SENSOR_RELATION_EXISTS') add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, sensor_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id) class StoreSensorItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, sid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, sid): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') if not sid.isdigit() or int(sid) <= 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_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (sid,)) 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 id " " FROM tbl_stores_sensors " " WHERE store_id = %s AND sensor_id = %s ", (id_, sid)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_SENSOR_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_stores_sensors WHERE store_id = %s AND sensor_id = %s ", (id_, sid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204 class StoreVirtualMeterCollection: @staticmethod def __init__(): """Initializes Class""" 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_STORE_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " " WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} 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_STORE_ID') new_values = json.loads(raw_json) if 'virtual_meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['virtual_meter_id'], int) or \ new_values['data']['virtual_meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_VIRTUAL_METER_ID') virtual_meter_id = new_values['data']['virtual_meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_virtual_meters " " WHERE id = %s ", (virtual_meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.VIRTUAL_METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_stores_virtual_meters " " WHERE store_id = %s AND virtual_meter_id = %s") cursor.execute(query, (id_, virtual_meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, virtual_meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_201 resp.location = '/stores/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) class StoreVirtualMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_VIRTUAL_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_stores " " 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.STORE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_virtual_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.VIRTUAL_METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_stores_virtual_meters " " WHERE store_id = %s AND virtual_meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_VIRTUAL_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_stores_virtual_meters " " WHERE store_id = %s AND virtual_meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.disconnect() resp.status = falcon.HTTP_204