Code Duplication    Length = 42-42 lines in 2 locations

myems-api/core/meter.py 1 location

@@ 1091-1132 (lines=42) @@
1088
        _ = id_
1089
        resp.status = falcon.HTTP_200
1090
1091
    @staticmethod
1092
    def on_get(req, resp, id_):
1093
        if 'API-KEY' not in req.headers or \
1094
                not isinstance(req.headers['API-KEY'], str) or \
1095
                len(str.strip(req.headers['API-KEY'])) == 0:
1096
            access_control(req)
1097
        else:
1098
            api_key_control(req)
1099
        if not id_.isdigit() or int(id_) <= 0:
1100
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1101
                                   description='API.INVALID_METER_ID')
1102
1103
        cnx = mysql.connector.connect(**config.myems_system_db)
1104
        cursor = cnx.cursor()
1105
1106
        cursor.execute(" SELECT name "
1107
                       " FROM tbl_meters "
1108
                       " WHERE id = %s ", (id_,))
1109
        if cursor.fetchone() is None:
1110
            cursor.close()
1111
            cnx.close()
1112
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1113
                                   description='API.METER_NOT_FOUND')
1114
1115
        query = (" SELECT p.id, p.name, "
1116
                 "        ds.id, ds.name, ds.uuid, "
1117
                 "        p.address "
1118
                 " FROM tbl_points p, tbl_meters_points mp, tbl_data_sources ds "
1119
                 " WHERE mp.meter_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
1120
                 " ORDER BY p.name ")
1121
        cursor.execute(query, (id_,))
1122
        rows = cursor.fetchall()
1123
1124
        result = list()
1125
        if rows is not None and len(rows) > 0:
1126
            for row in rows:
1127
                meta_result = {"id": row[0], "name": row[1],
1128
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
1129
                               "address": row[5]}
1130
                result.append(meta_result)
1131
1132
        resp.text = json.dumps(result)
1133
1134
    @staticmethod
1135
    @user_logger

myems-api/core/sensor.py 1 location

@@ 351-392 (lines=42) @@
348
        resp.status = falcon.HTTP_200
349
        _ = id_
350
351
    @staticmethod
352
    def on_get(req, resp, id_):
353
        if 'API-KEY' not in req.headers or \
354
                not isinstance(req.headers['API-KEY'], str) or \
355
                len(str.strip(req.headers['API-KEY'])) == 0:
356
            access_control(req)
357
        else:
358
            api_key_control(req)
359
        if not id_.isdigit() or int(id_) <= 0:
360
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
361
                                   description='API.INVALID_SENSOR_ID')
362
363
        cnx = mysql.connector.connect(**config.myems_system_db)
364
        cursor = cnx.cursor()
365
366
        cursor.execute(" SELECT name "
367
                       " FROM tbl_sensors "
368
                       " WHERE id = %s ", (id_,))
369
        if cursor.fetchone() is None:
370
            cursor.close()
371
            cnx.close()
372
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
373
                                   description='API.SENSOR_NOT_FOUND')
374
375
        query = (" SELECT p.id, p.name, "
376
                 "        ds.id, ds.name, ds.uuid, "
377
                 "        p.address "
378
                 " FROM tbl_points p, tbl_sensors_points sp, tbl_data_sources ds "
379
                 " WHERE sp.sensor_id = %s AND p.id = sp.point_id AND p.data_source_id = ds.id "
380
                 " ORDER BY p.name ")
381
        cursor.execute(query, (id_,))
382
        rows = cursor.fetchall()
383
384
        result = list()
385
        if rows is not None and len(rows) > 0:
386
            for row in rows:
387
                meta_result = {"id": row[0], "name": row[1],
388
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
389
                               "address": row[5]}
390
                result.append(meta_result)
391
392
        resp.text = json.dumps(result)
393
394
    @staticmethod
395
    @user_logger