added 'meter_uuid' parameter

pull/115/head
Caozhenhui 2022-01-17 20:15:43 +08:00
parent 4de486ac74
commit 8c9b55b298
1 changed files with 21 additions and 7 deletions

View File

@ -33,6 +33,7 @@ class Reporting:
def on_get(req, resp): def on_get(req, resp):
print(req.params) print(req.params)
meter_id = req.params.get('meterid') meter_id = req.params.get('meterid')
meter_uuid = req.params.get('meteruuid')
period_type = req.params.get('periodtype') period_type = req.params.get('periodtype')
base_period_start_datetime = req.params.get('baseperiodstartdatetime') base_period_start_datetime = req.params.get('baseperiodstartdatetime')
base_period_end_datetime = req.params.get('baseperiodenddatetime') base_period_end_datetime = req.params.get('baseperiodenddatetime')
@ -42,13 +43,17 @@ class Reporting:
################################################################################################################ ################################################################################################################
# Step 1: valid parameters # Step 1: valid parameters
################################################################################################################ ################################################################################################################
if meter_id is None: if meter_id is None and meter_uuid is None:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID')
else:
if meter_id is not None:
meter_id = str.strip(meter_id) meter_id = str.strip(meter_id)
if not meter_id.isdigit() or int(meter_id) <= 0: if not meter_id.isdigit() or int(meter_id) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID')
if meter_uuid is not None:
meter_uuid = str.strip(meter_uuid)
if period_type is None: if period_type is None:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE')
else: else:
@ -131,11 +136,18 @@ class Reporting:
cnx_historical = mysql.connector.connect(**config.myems_historical_db) cnx_historical = mysql.connector.connect(**config.myems_historical_db)
cursor_historical = cnx_historical.cursor() cursor_historical = cnx_historical.cursor()
if meter_id is not None:
cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, "
" ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e "
" FROM tbl_meters m, tbl_energy_categories ec " " FROM tbl_meters m, tbl_energy_categories ec "
" WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id,)) " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id,))
row_meter = cursor_system.fetchone() row_meter = cursor_system.fetchone()
if meter_uuid is not None:
cursor_system.execute(" SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, "
" ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e "
" FROM tbl_meters m, tbl_energy_categories ec "
" WHERE m.uuid = %s AND m.energy_category_id = ec.id ", (meter_uuid,))
row_meter = cursor_system.fetchone()
if row_meter is None: if row_meter is None:
if cursor_system: if cursor_system:
cursor_system.close() cursor_system.close()
@ -152,6 +164,8 @@ class Reporting:
if cnx_historical: if cnx_historical:
cnx_historical.disconnect() cnx_historical.disconnect()
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND')
if meter_id is not None and int(meter_id) != int(row_meter[0]):
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND')
meter = dict() meter = dict()
meter['id'] = row_meter[0] meter['id'] = row_meter[0]