| @@ 1081-1122 (lines=42) @@ | ||
| 1078 | _ = id_ |
|
| 1079 | resp.status = falcon.HTTP_200 |
|
| 1080 | ||
| 1081 | @staticmethod |
|
| 1082 | def on_get(req, resp, id_): |
|
| 1083 | if 'API-KEY' not in req.headers or \ |
|
| 1084 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1085 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1086 | access_control(req) |
|
| 1087 | else: |
|
| 1088 | api_key_control(req) |
|
| 1089 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1090 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1091 | description='API.INVALID_METER_ID') |
|
| 1092 | ||
| 1093 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1094 | cursor = cnx.cursor() |
|
| 1095 | ||
| 1096 | cursor.execute(" SELECT name " |
|
| 1097 | " FROM tbl_meters " |
|
| 1098 | " WHERE id = %s ", (id_,)) |
|
| 1099 | if cursor.fetchone() is None: |
|
| 1100 | cursor.close() |
|
| 1101 | cnx.close() |
|
| 1102 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1103 | description='API.METER_NOT_FOUND') |
|
| 1104 | ||
| 1105 | query = (" SELECT p.id, p.name, " |
|
| 1106 | " ds.id, ds.name, ds.uuid, " |
|
| 1107 | " p.address " |
|
| 1108 | " FROM tbl_points p, tbl_meters_points mp, tbl_data_sources ds " |
|
| 1109 | " WHERE mp.meter_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 1110 | " ORDER BY p.name ") |
|
| 1111 | cursor.execute(query, (id_,)) |
|
| 1112 | rows = cursor.fetchall() |
|
| 1113 | ||
| 1114 | result = list() |
|
| 1115 | if rows is not None and len(rows) > 0: |
|
| 1116 | for row in rows: |
|
| 1117 | meta_result = {"id": row[0], "name": row[1], |
|
| 1118 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 1119 | "address": row[5]} |
|
| 1120 | result.append(meta_result) |
|
| 1121 | ||
| 1122 | resp.text = json.dumps(result) |
|
| 1123 | ||
| 1124 | @staticmethod |
|
| 1125 | @user_logger |
|
| @@ 341-382 (lines=42) @@ | ||
| 338 | resp.status = falcon.HTTP_200 |
|
| 339 | _ = id_ |
|
| 340 | ||
| 341 | @staticmethod |
|
| 342 | def on_get(req, resp, id_): |
|
| 343 | if 'API-KEY' not in req.headers or \ |
|
| 344 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 345 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 346 | access_control(req) |
|
| 347 | else: |
|
| 348 | api_key_control(req) |
|
| 349 | if not id_.isdigit() or int(id_) <= 0: |
|
| 350 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 351 | description='API.INVALID_SENSOR_ID') |
|
| 352 | ||
| 353 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 354 | cursor = cnx.cursor() |
|
| 355 | ||
| 356 | cursor.execute(" SELECT name " |
|
| 357 | " FROM tbl_sensors " |
|
| 358 | " WHERE id = %s ", (id_,)) |
|
| 359 | if cursor.fetchone() is None: |
|
| 360 | cursor.close() |
|
| 361 | cnx.close() |
|
| 362 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 363 | description='API.SENSOR_NOT_FOUND') |
|
| 364 | ||
| 365 | query = (" SELECT p.id, p.name, " |
|
| 366 | " ds.id, ds.name, ds.uuid, " |
|
| 367 | " p.address " |
|
| 368 | " FROM tbl_points p, tbl_sensors_points sp, tbl_data_sources ds " |
|
| 369 | " WHERE sp.sensor_id = %s AND p.id = sp.point_id AND p.data_source_id = ds.id " |
|
| 370 | " ORDER BY p.name ") |
|
| 371 | cursor.execute(query, (id_,)) |
|
| 372 | rows = cursor.fetchall() |
|
| 373 | ||
| 374 | result = list() |
|
| 375 | if rows is not None and len(rows) > 0: |
|
| 376 | for row in rows: |
|
| 377 | meta_result = {"id": row[0], "name": row[1], |
|
| 378 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 379 | "address": row[5]} |
|
| 380 | result.append(meta_result) |
|
| 381 | ||
| 382 | resp.text = json.dumps(result) |
|
| 383 | ||
| 384 | @staticmethod |
|
| 385 | @user_logger |
|