2644 lines
104 KiB
Python
2644 lines
104 KiB
Python
import falcon
|
|
import simplejson as json
|
|
import mysql.connector
|
|
import config
|
|
import uuid
|
|
from datetime import datetime
|
|
from anytree import AnyNode
|
|
from anytree.exporter import JsonExporter
|
|
|
|
|
|
class SpaceCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
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_spaces ")
|
|
cursor.execute(query)
|
|
rows_spaces = cursor.fetchall()
|
|
|
|
space_dict = dict()
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
for row in rows_spaces:
|
|
space_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"uuid": row['uuid']}
|
|
|
|
query = (" SELECT id, name, utc_offset "
|
|
" FROM tbl_timezones ")
|
|
cursor.execute(query)
|
|
rows_timezones = cursor.fetchall()
|
|
|
|
timezone_dict = dict()
|
|
if rows_timezones is not None and len(rows_timezones) > 0:
|
|
for row in rows_timezones:
|
|
timezone_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"utc_offset": row['utc_offset']}
|
|
|
|
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, "
|
|
" parent_space_id, area, timezone_id, is_input_counted, is_output_counted, "
|
|
" contact_id, cost_center_id, description "
|
|
" FROM tbl_spaces "
|
|
" 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:
|
|
timezone = timezone_dict.get(row['timezone_id'], None)
|
|
contact = contact_dict.get(row['contact_id'], None)
|
|
cost_center = cost_center_dict.get(row['cost_center_id'], None)
|
|
parent_space = space_dict.get(row['parent_space_id'], None)
|
|
meta_result = {"id": row['id'],
|
|
"name": row['name'],
|
|
"uuid": row['uuid'],
|
|
"parent_space": parent_space,
|
|
"area": row['area'],
|
|
"timezone": timezone,
|
|
"is_input_counted": bool(row['is_input_counted']),
|
|
"is_output_counted": bool(row['is_output_counted']),
|
|
"contact": contact,
|
|
"cost_center": cost_center,
|
|
"description": row['description']}
|
|
result.append(meta_result)
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
resp.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
def on_post(req, resp):
|
|
"""Handles POST requests"""
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
new_values = json.loads(raw_json)
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
|
|
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_SPACE_NAME')
|
|
name = str.strip(new_values['data']['name'])
|
|
|
|
if 'parent_space_id' in new_values['data'].keys():
|
|
if new_values['data']['parent_space_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_PARENT_SPACE_ID')
|
|
parent_space_id = new_values['data']['parent_space_id']
|
|
else:
|
|
parent_space_id = None
|
|
|
|
if 'area' not in new_values['data'].keys() or \
|
|
not (isinstance(new_values['data']['area'], float) or
|
|
isinstance(new_values['data']['area'], int)):
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_AREA_VALUE')
|
|
area = new_values['data']['area']
|
|
|
|
if 'timezone_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['timezone_id'], int) or \
|
|
new_values['data']['timezone_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TIMEZONE_ID')
|
|
timezone_id = new_values['data']['timezone_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 'is_output_counted' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['is_output_counted'], bool):
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE')
|
|
is_output_counted = new_values['data']['is_output_counted']
|
|
|
|
if 'contact_id' in new_values['data'].keys():
|
|
if 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']
|
|
else:
|
|
contact_id = None
|
|
|
|
if 'cost_center_id' in new_values['data'].keys():
|
|
if 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']
|
|
else:
|
|
cost_center_id = None
|
|
|
|
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_spaces "
|
|
" 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.SPACE_NAME_IS_ALREADY_IN_USE')
|
|
|
|
if parent_space_id is not None:
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" WHERE id = %s ",
|
|
(new_values['data']['parent_space_id'],))
|
|
row = cursor.fetchone()
|
|
if row is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.PARENT_SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_timezones "
|
|
" WHERE id = %s ",
|
|
(new_values['data']['timezone_id'],))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.TIMEZONE_NOT_FOUND')
|
|
if contact_id is not None:
|
|
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')
|
|
|
|
if cost_center_id is not None:
|
|
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_spaces "
|
|
" (name, uuid, parent_space_id, area, timezone_id, is_input_counted, is_output_counted, "
|
|
" contact_id, cost_center_id, description) "
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
|
|
cursor.execute(add_values, (name,
|
|
str(uuid.uuid4()),
|
|
parent_space_id,
|
|
area,
|
|
timezone_id,
|
|
is_input_counted,
|
|
is_output_counted,
|
|
contact_id,
|
|
cost_center_id,
|
|
description))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(new_id)
|
|
|
|
|
|
class SpaceItem:
|
|
@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_METER_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
query = (" SELECT id, name, uuid "
|
|
" FROM tbl_spaces ")
|
|
cursor.execute(query)
|
|
rows_spaces = cursor.fetchall()
|
|
|
|
space_dict = dict()
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
for row in rows_spaces:
|
|
space_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"uuid": row['uuid']}
|
|
|
|
query = (" SELECT id, name, utc_offset "
|
|
" FROM tbl_timezones ")
|
|
cursor.execute(query)
|
|
rows_timezones = cursor.fetchall()
|
|
|
|
timezone_dict = dict()
|
|
if rows_timezones is not None and len(rows_timezones) > 0:
|
|
for row in rows_timezones:
|
|
timezone_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"utc_offset": row['utc_offset']}
|
|
|
|
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, "
|
|
" parent_space_id, area, timezone_id, is_input_counted, is_output_counted, "
|
|
" contact_id, cost_center_id, description "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
else:
|
|
parent_space = space_dict.get(row['parent_space_id'], None)
|
|
timezone = timezone_dict.get(row['timezone_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'],
|
|
"parent_space_id": parent_space,
|
|
"area": row['area'],
|
|
"timezone": timezone,
|
|
"is_input_counted": bool(row['is_input_counted']),
|
|
"is_output_counted": bool(row['is_output_counted']),
|
|
"contact": contact,
|
|
"cost_center": cost_center,
|
|
"description": row['description']}
|
|
|
|
resp.body = json.dumps(meta_result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
if int(id_) == 1:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.THIS_SPACE_CANNOT_BE_DELETED')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
# checkout relation with children spaces
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces "
|
|
" WHERE parent_space_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_CHILDREN_SPACES')
|
|
|
|
# check relation with equipment
|
|
cursor.execute(" SELECT equipment_id "
|
|
" FROM tbl_spaces_equipments "
|
|
" WHERE space_id = %s ",
|
|
(id_,))
|
|
rows_equipments = cursor.fetchall()
|
|
if rows_equipments is not None and len(rows_equipments) > 0:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_EQUIPMENTS')
|
|
|
|
# check relation with combined equipment
|
|
cursor.execute(" SELECT combined_equipment_id "
|
|
" FROM tbl_spaces_combined_equipments "
|
|
" WHERE space_id = %s ",
|
|
(id_,))
|
|
rows_combined_equipments = cursor.fetchall()
|
|
if rows_combined_equipments is not None and len(rows_combined_equipments) > 0:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS')
|
|
|
|
# check relation with meter
|
|
cursor.execute(" SELECT meter_id "
|
|
" FROM tbl_spaces_meters "
|
|
" WHERE space_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_spaces_offline_meters "
|
|
" WHERE space_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_spaces_points "
|
|
" WHERE space_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_spaces_sensors "
|
|
" WHERE space_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 store
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_stores "
|
|
" WHERE space_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 tenant
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_tenants "
|
|
" WHERE space_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 virtual meter
|
|
cursor.execute(" SELECT virtual_meter_id "
|
|
" FROM tbl_spaces_virtual_meters "
|
|
" WHERE space_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_spaces WHERE id = %s ", (id_,))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
@staticmethod
|
|
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_SPACE_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_SPACE_NAME')
|
|
name = str.strip(new_values['data']['name'])
|
|
|
|
if int(id_) == 1:
|
|
parent_space_id = None
|
|
else:
|
|
if 'parent_space_id' not in new_values['data'].keys() or \
|
|
new_values['data']['parent_space_id'] is None or \
|
|
not isinstance(new_values['data']['parent_space_id'], int) or \
|
|
int(new_values['data']['parent_space_id']) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TIMEZONE_ID')
|
|
parent_space_id = int(new_values['data']['parent_space_id'])
|
|
|
|
if 'area' not in new_values['data'].keys() or \
|
|
not (isinstance(new_values['data']['area'], float) or
|
|
isinstance(new_values['data']['area'], int)):
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_AREA_VALUE')
|
|
area = new_values['data']['area']
|
|
|
|
if 'timezone_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['timezone_id'], int) or \
|
|
new_values['data']['timezone_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TIMEZONE_ID')
|
|
timezone_id = new_values['data']['timezone_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 'is_output_counted' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['is_output_counted'], bool):
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_IS_OUTPUT_COUNTED_VALUE')
|
|
is_output_counted = new_values['data']['is_output_counted']
|
|
|
|
if 'contact_id' in new_values['data'].keys() and new_values['data']['contact_id'] is not None:
|
|
if 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']
|
|
else:
|
|
contact_id = None
|
|
|
|
if 'cost_center_id' in new_values['data'].keys():
|
|
if 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']
|
|
else:
|
|
cost_center_id = None
|
|
|
|
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_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" WHERE name = %s AND id != %s ", (name, id_))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.SPACE_NAME_IS_ALREADY_IN_USE')
|
|
|
|
if parent_space_id is not None:
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" WHERE id = %s ",
|
|
(new_values['data']['parent_space_id'],))
|
|
row = cursor.fetchone()
|
|
if row is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.PARENT_SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_timezones "
|
|
" WHERE id = %s ",
|
|
(new_values['data']['timezone_id'],))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.TIMEZONE_NOT_FOUND')
|
|
if contact_id is not None:
|
|
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')
|
|
|
|
if cost_center_id is not None:
|
|
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_spaces "
|
|
" SET name = %s, parent_space_id = %s, area = %s, timezone_id = %s, "
|
|
" is_input_counted = %s, is_output_counted = %s, contact_id = %s, cost_center_id = %s, "
|
|
" description = %s "
|
|
" WHERE id = %s ")
|
|
cursor.execute(update_row, (name,
|
|
parent_space_id,
|
|
area,
|
|
timezone_id,
|
|
is_input_counted,
|
|
is_output_counted,
|
|
contact_id,
|
|
cost_center_id,
|
|
description,
|
|
id_))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
class SpaceChildrenCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
query = (" SELECT id, name, uuid, "
|
|
" parent_space_id, area, timezone_id, is_input_counted, is_output_counted, "
|
|
" contact_id, cost_center_id, description "
|
|
" FROM tbl_spaces "
|
|
" WHERE id = %s ")
|
|
cursor.execute(query, (id_,))
|
|
row_current_space = cursor.fetchone()
|
|
if row_current_space is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_NOT_FOUND')
|
|
# note: row_current_space will be used at the end
|
|
|
|
query = (" SELECT id, name, uuid "
|
|
" FROM tbl_spaces ")
|
|
cursor.execute(query)
|
|
rows_spaces = cursor.fetchall()
|
|
|
|
space_dict = dict()
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
for row in rows_spaces:
|
|
space_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"uuid": row['uuid']}
|
|
|
|
query = (" SELECT id, name, utc_offset "
|
|
" FROM tbl_timezones ")
|
|
cursor.execute(query)
|
|
rows_timezones = cursor.fetchall()
|
|
|
|
timezone_dict = dict()
|
|
if rows_timezones is not None and len(rows_timezones) > 0:
|
|
for row in rows_timezones:
|
|
timezone_dict[row['id']] = {"id": row['id'],
|
|
"name": row['name'],
|
|
"utc_offset": row['utc_offset']}
|
|
|
|
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']}
|
|
result = dict()
|
|
result['current'] = dict()
|
|
result['current']['id'] = row_current_space['id']
|
|
result['current']['name'] = row_current_space['name']
|
|
result['current']['uuid'] = row_current_space['uuid']
|
|
result['current']['parent_space'] = space_dict.get(row_current_space['parent_space_id'], None)
|
|
result['current']['area'] = row_current_space['area']
|
|
result['current']['timezone'] = timezone_dict.get(row_current_space['timezone_id'], None)
|
|
result['current']['is_input_counted'] = bool(row_current_space['is_input_counted'])
|
|
result['current']['is_output_counted'] = bool(row_current_space['is_output_counted'])
|
|
result['current']['contact'] = contact_dict.get(row_current_space['contact_id'], None)
|
|
result['current']['cost_center'] = cost_center_dict.get(row_current_space['cost_center_id'], None)
|
|
result['current']['description'] = row_current_space['description']
|
|
|
|
result['children'] = list()
|
|
|
|
query = (" SELECT id, name, uuid, "
|
|
" parent_space_id, area, timezone_id, is_input_counted, is_output_counted, "
|
|
" contact_id, cost_center_id, description "
|
|
" FROM tbl_spaces "
|
|
" WHERE parent_space_id = %s "
|
|
" ORDER BY id ")
|
|
cursor.execute(query, (id_, ))
|
|
rows_spaces = cursor.fetchall()
|
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
for row in rows_spaces:
|
|
timezone = timezone_dict.get(row['timezone_id'], None)
|
|
contact = contact_dict.get(row['contact_id'], None)
|
|
cost_center = cost_center_dict.get(row['cost_center_id'], None)
|
|
parent_space = space_dict.get(row['parent_space_id'], None)
|
|
meta_result = {"id": row['id'],
|
|
"name": row['name'],
|
|
"uuid": row['uuid'],
|
|
"parent_space": parent_space,
|
|
"area": row['area'],
|
|
"timezone": timezone,
|
|
"is_input_counted": bool(row['is_input_counted']),
|
|
"is_output_counted": bool(row['is_output_counted']),
|
|
"contact": contact,
|
|
"cost_center": cost_center,
|
|
"description": row['description']}
|
|
result['children'].append(meta_result)
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
resp.body = json.dumps(result)
|
|
|
|
|
|
class SpaceCombinedEquipmentCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT e.id, e.name, e.uuid "
|
|
" FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e "
|
|
" WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s "
|
|
" ORDER BY e.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'combined_equipment_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['combined_equipment_id'], int) or \
|
|
new_values['data']['combined_equipment_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_COMBINED_EQUIPMENT_ID')
|
|
combined_equipment_id = new_values['data']['combined_equipment_id']
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" from tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_combined_equipments "
|
|
" WHERE id = %s ", (combined_equipment_id,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.COMBINED_EQUIPMENT_NOT_FOUND')
|
|
|
|
query = (" SELECT id "
|
|
" FROM tbl_spaces_combined_equipments "
|
|
" WHERE space_id = %s AND combined_equipment_id = %s")
|
|
cursor.execute(query, (id_, combined_equipment_id,))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
|
description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) "
|
|
" VALUES (%s, %s) ")
|
|
cursor.execute(add_row, (id_, combined_equipment_id,))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id)
|
|
|
|
|
|
class SpaceCombinedEquipmentItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, eid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_delete(req, resp, id_, eid):
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SPACE_ID')
|
|
|
|
if not eid.isdigit() or int(eid) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_COMBINED_EQUIPMENT_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_combined_equipments "
|
|
" WHERE id = %s ", (eid,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.COMBINED_EQUIPMENT_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_combined_equipments "
|
|
" WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_COMBINED_EQUIPMENT_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_combined_equipments "
|
|
" WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceEquipmentCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT e.id, e.name, e.uuid "
|
|
" FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e "
|
|
" WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s "
|
|
" ORDER BY e.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'equipment_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['equipment_id'], int) or \
|
|
new_values['data']['equipment_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_EQUIPMENT_ID')
|
|
equipment_id = new_values['data']['equipment_id']
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" from tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_equipments "
|
|
" WHERE id = %s ", (equipment_id,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.EQUIPMENT_NOT_FOUND')
|
|
|
|
query = (" SELECT id "
|
|
" FROM tbl_spaces_equipments "
|
|
" WHERE space_id = %s AND equipment_id = %s")
|
|
cursor.execute(query, (id_, equipment_id,))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
|
description='API.SPACE_EQUIPMENT_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) "
|
|
" VALUES (%s, %s) ")
|
|
cursor.execute(add_row, (id_, equipment_id,))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id)
|
|
|
|
|
|
class SpaceEquipmentItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, eid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_delete(req, resp, id_, eid):
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SPACE_ID')
|
|
|
|
if not eid.isdigit() or int(eid) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_EQUIPMENT_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_equipments "
|
|
" WHERE id = %s ", (eid,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.EQUIPMENT_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_equipments "
|
|
" WHERE space_id = %s AND equipment_id = %s ", (id_, eid))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_EQUIPMENT_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_equipments WHERE space_id = %s AND equipment_id = %s ", (id_, eid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceMeterCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_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_spaces s, tbl_spaces_meters sm, tbl_meters m "
|
|
" WHERE sm.space_id = s.id AND m.id = sm.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_meters "
|
|
" WHERE space_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.SPACE_METER_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_meters (space_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 = '/spaces/' + str(id_) + '/meters/' + str(meter_id)
|
|
|
|
|
|
class SpaceMeterItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, mid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_meters "
|
|
" WHERE space_id = %s AND meter_id = %s ", (id_, mid))
|
|
# use fetchall to avoid 'Unread result found' error in case there are duplicate rows
|
|
rows = cursor.fetchall()
|
|
if rows is None or len(rows) == 0:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_METER_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_meters WHERE space_id = %s AND meter_id = %s ", (id_, mid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceOfflineMeterCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_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_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m "
|
|
" WHERE sm.space_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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_offline_meters "
|
|
" WHERE space_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.SPACE_OFFLINE_METER_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_offline_meters (space_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 = '/spaces/' + str(id_) + '/offlinemeters/' + str(offline_meter_id)
|
|
|
|
|
|
class SpaceOfflineMeterItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, mid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_offline_meters "
|
|
" WHERE space_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.SPACE_OFFLINE_METER_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_offline_meters "
|
|
" WHERE space_id = %s AND offline_meter_id = %s ", (id_, mid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpacePointCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_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_spaces s, tbl_spaces_points sp, tbl_points p "
|
|
" WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_points "
|
|
" WHERE space_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.SPACE_POINT_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_points (space_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 = '/spaces/' + str(id_) + '/points/' + str(point_id)
|
|
|
|
|
|
class SpacePointItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, pid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_points "
|
|
" WHERE space_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.SPACE_POINT_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_points "
|
|
" WHERE space_id = %s AND point_id = %s ", (id_, pid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceSensorCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT se.id, se.name, se.uuid "
|
|
" FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se "
|
|
" WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
|
|
" ORDER BY se.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_sensors "
|
|
" WHERE space_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.SPACE_SENSOR_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_sensors (space_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 = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id)
|
|
|
|
|
|
class SpaceSensorItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, sid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_sensors "
|
|
" WHERE space_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.SPACE_SENSOR_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_sensors WHERE space_id = %s AND sensor_id = %s ", (id_, sid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceShopfloorCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT sf.id, sf.name, sf.uuid "
|
|
" FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf "
|
|
" WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s "
|
|
" ORDER BY sf.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'shopfloor_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['shopfloor_id'], int) or \
|
|
new_values['data']['shopfloor_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SHOPFLOOR_ID')
|
|
shopfloor_id = new_values['data']['shopfloor_id']
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" from tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_shopfloors "
|
|
" WHERE id = %s ", (shopfloor_id,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SHOPFLOOR_NOT_FOUND')
|
|
|
|
query = (" SELECT id "
|
|
" FROM tbl_spaces_shopfloors "
|
|
" WHERE space_id = %s AND shopfloor_id = %s")
|
|
cursor.execute(query, (id_, shopfloor_id,))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
|
description='API.SPACE_SHOPFLOOR_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) "
|
|
" VALUES (%s, %s) ")
|
|
cursor.execute(add_row, (id_, shopfloor_id,))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id)
|
|
|
|
|
|
class SpaceShopfloorItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, sid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
if not sid.isdigit() or int(sid) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SHOPFLOOR_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_shopfloors "
|
|
" 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.SHOPFLOOR_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_shopfloors "
|
|
" WHERE space_id = %s AND shopfloor_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.SPACE_SHOPFLOOR_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_shopfloors WHERE space_id = %s AND shopfloor_id = %s ", (id_, sid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceStoreCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT t.id, t.name, t.uuid "
|
|
" FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t "
|
|
" WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s "
|
|
" ORDER BY t.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'store_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['store_id'], int) or \
|
|
new_values['data']['store_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_STORE_ID')
|
|
store_id = new_values['data']['store_id']
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" from tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_stores "
|
|
" WHERE id = %s ", (store_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 "
|
|
" FROM tbl_spaces_stores "
|
|
" WHERE space_id = %s AND store_id = %s")
|
|
cursor.execute(query, (id_, store_id,))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
|
description='API.SPACE_STORE_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) "
|
|
" VALUES (%s, %s) ")
|
|
cursor.execute(add_row, (id_, store_id,))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id)
|
|
|
|
|
|
class SpaceStoreItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, tid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_delete(req, resp, id_, tid):
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SPACE_ID')
|
|
|
|
if not tid.isdigit() or int(tid) <= 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_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_stores "
|
|
" WHERE id = %s ", (tid,))
|
|
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 id "
|
|
" FROM tbl_spaces_stores "
|
|
" WHERE space_id = %s AND store_id = %s ", (id_, tid))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_STORE_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_stores WHERE space_id = %s AND store_id = %s ", (id_, tid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceTenantCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
query = (" SELECT t.id, t.name, t.uuid "
|
|
" FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t "
|
|
" WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s "
|
|
" ORDER BY t.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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'tenant_id' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['tenant_id'], int) or \
|
|
new_values['data']['tenant_id'] <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TENANT_ID')
|
|
tenant_id = new_values['data']['tenant_id']
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" from tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_tenants "
|
|
" WHERE id = %s ", (tenant_id,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.TENANT_NOT_FOUND')
|
|
|
|
query = (" SELECT id "
|
|
" FROM tbl_spaces_tenants "
|
|
" WHERE space_id = %s AND tenant_id = %s")
|
|
cursor.execute(query, (id_, tenant_id,))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
|
description='API.SPACE_TENANT_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) "
|
|
" VALUES (%s, %s) ")
|
|
cursor.execute(add_row, (id_, tenant_id,))
|
|
new_id = cursor.lastrowid
|
|
cnx.commit()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id)
|
|
|
|
|
|
class SpaceTenantItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, tid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_delete(req, resp, id_, tid):
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_SPACE_ID')
|
|
|
|
if not tid.isdigit() or int(tid) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TENANT_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_tenants "
|
|
" WHERE id = %s ", (tid,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.TENANT_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces_tenants "
|
|
" WHERE space_id = %s AND tenant_id = %s ", (id_, tid))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_TENANT_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_tenants WHERE space_id = %s AND tenant_id = %s ", (id_, tid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceVirtualMeterCollection:
|
|
@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_SPACE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_spaces "
|
|
" 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.SPACE_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_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m "
|
|
" WHERE sm.space_id = s.id AND m.id = sm.virtual_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.body = json.dumps(result)
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_virtual_meters "
|
|
" WHERE space_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.SPACE_VIRTUAL_METER_RELATION_EXISTS')
|
|
|
|
add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_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 = '/spaces/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id)
|
|
|
|
|
|
class SpaceVirtualMeterItem:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_, mid):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
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_SPACE_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_spaces "
|
|
" 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.SPACE_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_spaces_virtual_meters "
|
|
" WHERE space_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.SPACE_VIRTUAL_METER_RELATION_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_spaces_virtual_meters "
|
|
" WHERE space_id = %s AND virtual_meter_id = %s ", (id_, mid))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class SpaceTreeCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
"""Initializes Class"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(req, resp):
|
|
if 'USER-UUID' not in req.headers or \
|
|
not isinstance(req.headers['USER-UUID'], str) or \
|
|
len(str.strip(req.headers['USER-UUID'])) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_USER_UUID')
|
|
user_uuid = str.strip(req.headers['USER-UUID'])
|
|
|
|
if 'TOKEN' not in req.headers or \
|
|
not isinstance(req.headers['TOKEN'], str) or \
|
|
len(str.strip(req.headers['TOKEN'])) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_TOKEN')
|
|
token = str.strip(req.headers['TOKEN'])
|
|
|
|
# Verify User Session
|
|
cnx = mysql.connector.connect(**config.myems_user_db)
|
|
cursor = cnx.cursor()
|
|
query = (" SELECT utc_expires "
|
|
" FROM tbl_sessions "
|
|
" WHERE user_uuid = %s AND token = %s")
|
|
cursor.execute(query, (user_uuid, token,))
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.USER_SESSION_NOT_FOUND')
|
|
else:
|
|
utc_expires = row[0]
|
|
if datetime.utcnow() > utc_expires:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.USER_SESSION_TIMEOUT')
|
|
# get privilege
|
|
query = (" SELECT is_admin, privilege_id "
|
|
" FROM tbl_users "
|
|
" WHERE uuid = %s ")
|
|
cursor.execute(query, (user_uuid,))
|
|
row = cursor.fetchone()
|
|
if row is None:
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, 'API.NOT_FOUND', 'API.USER_NOT_FOUND')
|
|
else:
|
|
is_admin = bool(row[0])
|
|
privilege_id = row[1]
|
|
|
|
# get space_id in privilege
|
|
if is_admin:
|
|
space_id = 1
|
|
elif privilege_id is None:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.PRIVILEGE_NOT_FOUND')
|
|
else:
|
|
query = (" SELECT data "
|
|
" FROM tbl_privileges "
|
|
" WHERE id =%s ")
|
|
cursor.execute(query, (privilege_id,))
|
|
row = cursor.fetchone()
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
|
|
if row is None:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.PRIVILEGE_NOT_FOUND')
|
|
try:
|
|
data = json.loads(row[0])
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
|
|
if 'spaces' not in data or len(data['spaces']) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.SPACE_NOT_FOUND_IN_PRIVILEGE')
|
|
|
|
space_id = data['spaces'][0]
|
|
if space_id is None:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.PRIVILEGE_NOT_FOUND')
|
|
# get all spaces
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor(dictionary=True)
|
|
|
|
query = (" SELECT id, name, parent_space_id "
|
|
" FROM tbl_spaces "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
rows_spaces = cursor.fetchall()
|
|
node_dict = dict()
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
for row in rows_spaces:
|
|
parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None
|
|
node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name'])
|
|
|
|
cursor.close()
|
|
cnx.disconnect()
|
|
resp.body = JsonExporter(sort_keys=True).export(node_dict[space_id], )
|