Code Duplication    Length = 42-42 lines in 2 locations

myems-api/core/meter.py 1 location

@@ 977-1018 (lines=42) @@
974
        _ = id_
975
        resp.status = falcon.HTTP_200
976
977
    @staticmethod
978
    def on_get(req, resp, id_):
979
        if 'API-KEY' not in req.headers or \
980
                not isinstance(req.headers['API-KEY'], str) or \
981
                len(str.strip(req.headers['API-KEY'])) == 0:
982
            access_control(req)
983
        else:
984
            api_key_control(req)
985
        if not id_.isdigit() or int(id_) <= 0:
986
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
987
                                   description='API.INVALID_METER_ID')
988
989
        cnx = mysql.connector.connect(**config.myems_system_db)
990
        cursor = cnx.cursor()
991
992
        cursor.execute(" SELECT name "
993
                       " FROM tbl_meters "
994
                       " WHERE id = %s ", (id_,))
995
        if cursor.fetchone() is None:
996
            cursor.close()
997
            cnx.close()
998
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
999
                                   description='API.METER_NOT_FOUND')
1000
1001
        query = (" SELECT p.id, p.name, "
1002
                 "        ds.id, ds.name, ds.uuid, "
1003
                 "        p.address "
1004
                 " FROM tbl_points p, tbl_meters_points mp, tbl_data_sources ds "
1005
                 " WHERE mp.meter_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
1006
                 " ORDER BY p.name ")
1007
        cursor.execute(query, (id_,))
1008
        rows = cursor.fetchall()
1009
1010
        result = list()
1011
        if rows is not None and len(rows) > 0:
1012
            for row in rows:
1013
                meta_result = {"id": row[0], "name": row[1],
1014
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
1015
                               "address": row[5]}
1016
                result.append(meta_result)
1017
1018
        resp.text = json.dumps(result)
1019
1020
    @staticmethod
1021
    @user_logger

myems-api/core/sensor.py 1 location

@@ 321-362 (lines=42) @@
318
        resp.status = falcon.HTTP_200
319
        _ = id_
320
321
    @staticmethod
322
    def on_get(req, resp, id_):
323
        if 'API-KEY' not in req.headers or \
324
                not isinstance(req.headers['API-KEY'], str) or \
325
                len(str.strip(req.headers['API-KEY'])) == 0:
326
            access_control(req)
327
        else:
328
            api_key_control(req)
329
        if not id_.isdigit() or int(id_) <= 0:
330
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
331
                                   description='API.INVALID_SENSOR_ID')
332
333
        cnx = mysql.connector.connect(**config.myems_system_db)
334
        cursor = cnx.cursor()
335
336
        cursor.execute(" SELECT name "
337
                       " FROM tbl_sensors "
338
                       " WHERE id = %s ", (id_,))
339
        if cursor.fetchone() is None:
340
            cursor.close()
341
            cnx.close()
342
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
343
                                   description='API.SENSOR_NOT_FOUND')
344
345
        query = (" SELECT p.id, p.name, "
346
                 "        ds.id, ds.name, ds.uuid, "
347
                 "        p.address "
348
                 " FROM tbl_points p, tbl_sensors_points sp, tbl_data_sources ds "
349
                 " WHERE sp.sensor_id = %s AND p.id = sp.point_id AND p.data_source_id = ds.id "
350
                 " ORDER BY p.name ")
351
        cursor.execute(query, (id_,))
352
        rows = cursor.fetchall()
353
354
        result = list()
355
        if rows is not None and len(rows) > 0:
356
            for row in rows:
357
                meta_result = {"id": row[0], "name": row[1],
358
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
359
                               "address": row[5]}
360
                result.append(meta_result)
361
362
        resp.text = json.dumps(result)
363
364
    @staticmethod
365
    @user_logger