Code Duplication    Length = 101-101 lines in 2 locations

meter.py 1 location

@@ 842-942 (lines=101) @@
839
        resp.body = json.dumps(result)
840
841
842
class MeterPointCollection:
843
    @staticmethod
844
    def __init__():
845
        pass
846
847
    @staticmethod
848
    def on_options(req, resp, id_):
849
        resp.status = falcon.HTTP_200
850
851
    @staticmethod
852
    def on_get(req, resp, id_):
853
        if not id_.isdigit() or int(id_) <= 0:
854
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
855
                                   description='API.INVALID_METER_ID')
856
857
        cnx = mysql.connector.connect(**config.myems_system_db)
858
        cursor = cnx.cursor()
859
860
        cursor.execute(" SELECT name "
861
                       " FROM tbl_meters "
862
                       " WHERE id = %s ", (id_,))
863
        if cursor.fetchone() is None:
864
            cursor.close()
865
            cnx.disconnect()
866
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
867
                                   description='API.METER_NOT_FOUND')
868
869
        query = (" SELECT p.id, p.name, "
870
                 "        ds.id, ds.name, ds.uuid, "
871
                 "        p.address "
872
                 " FROM tbl_points p, tbl_meters_points mp, tbl_data_sources ds "
873
                 " WHERE mp.meter_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
874
                 " ORDER BY p.name ")
875
        cursor.execute(query, (id_,))
876
        rows = cursor.fetchall()
877
878
        result = list()
879
        if rows is not None and len(rows) > 0:
880
            for row in rows:
881
                meta_result = {"id": row[0], "name": row[1],
882
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
883
                               "address": row[5]}
884
                result.append(meta_result)
885
886
        resp.body = json.dumps(result)
887
888
    @staticmethod
889
    def on_post(req, resp, id_):
890
        """Handles POST requests"""
891
        try:
892
            raw_json = req.stream.read().decode('utf-8')
893
        except Exception as ex:
894
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
895
896
        if not id_.isdigit() or int(id_) <= 0:
897
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
898
                                   description='API.INVALID_METER_ID')
899
900
        new_values = json.loads(raw_json, encoding='utf-8')
901
902
        cnx = mysql.connector.connect(**config.myems_system_db)
903
        cursor = cnx.cursor()
904
905
        cursor.execute(" SELECT name "
906
                       " from tbl_meters "
907
                       " WHERE id = %s ", (id_,))
908
        if cursor.fetchone() is None:
909
            cursor.close()
910
            cnx.disconnect()
911
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
912
                                   description='API.METER_NOT_FOUND')
913
914
        cursor.execute(" SELECT name "
915
                       " FROM tbl_points "
916
                       " WHERE id = %s ", (new_values['data']['point_id'],))
917
        if cursor.fetchone() is None:
918
            cursor.close()
919
            cnx.disconnect()
920
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
921
                                   description='API.POINT_NOT_FOUND')
922
923
        query = (" SELECT id " 
924
                 " FROM tbl_meters_points "
925
                 " WHERE meter_id = %s AND point_id = %s")
926
        cursor.execute(query, (id_, new_values['data']['point_id'],))
927
        if cursor.fetchone() is not None:
928
            cursor.close()
929
            cnx.disconnect()
930
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
931
                                   description='API.METER_POINT_RELATION_EXISTED')
932
933
        add_row = (" INSERT INTO tbl_meters_points (meter_id, point_id) "
934
                   " VALUES (%s, %s) ")
935
        cursor.execute(add_row, (id_, new_values['data']['point_id'],))
936
        new_id = cursor.lastrowid
937
        cnx.commit()
938
        cursor.close()
939
        cnx.disconnect()
940
941
        resp.status = falcon.HTTP_201
942
        resp.location = '/meters/' + str(id_) + '/points/' + str(new_values['data']['point_id'])
943
944
945
class MeterPointItem:

sensor.py 1 location

@@ 266-366 (lines=101) @@
263
        resp.status = falcon.HTTP_200
264
265
266
class SensorPointCollection:
267
    @staticmethod
268
    def __init__():
269
        pass
270
271
    @staticmethod
272
    def on_options(req, resp, id_):
273
        resp.status = falcon.HTTP_200
274
275
    @staticmethod
276
    def on_get(req, resp, id_):
277
        if not id_.isdigit() or int(id_) <= 0:
278
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
279
                                   description='API.INVALID_SENSOR_ID')
280
281
        cnx = mysql.connector.connect(**config.myems_system_db)
282
        cursor = cnx.cursor()
283
284
        cursor.execute(" SELECT name "
285
                       " FROM tbl_sensors "
286
                       " WHERE id = %s ", (id_,))
287
        if cursor.fetchone() is None:
288
            cursor.close()
289
            cnx.disconnect()
290
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
291
                                   description='API.SENSOR_NOT_FOUND')
292
293
        query = (" SELECT p.id, p.name, "
294
                 "        ds.id, ds.name, ds.uuid, "
295
                 "        p.address "
296
                 " FROM tbl_points p, tbl_sensors_points sp, tbl_data_sources ds "
297
                 " WHERE sp.sensor_id = %s AND p.id = sp.point_id AND p.data_source_id = ds.id "
298
                 " ORDER BY p.name ")
299
        cursor.execute(query, (id_,))
300
        rows = cursor.fetchall()
301
302
        result = list()
303
        if rows is not None and len(rows) > 0:
304
            for row in rows:
305
                meta_result = {"id": row[0], "name": row[1],
306
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
307
                               "address": row[5]}
308
                result.append(meta_result)
309
310
        resp.body = json.dumps(result)
311
312
    @staticmethod
313
    def on_post(req, resp, id_):
314
        """Handles POST requests"""
315
        try:
316
            raw_json = req.stream.read().decode('utf-8')
317
        except Exception as ex:
318
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
319
320
        if not id_.isdigit() or int(id_) <= 0:
321
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
322
                                   description='API.INVALID_SENSOR_ID')
323
324
        new_values = json.loads(raw_json, encoding='utf-8')
325
326
        cnx = mysql.connector.connect(**config.myems_system_db)
327
        cursor = cnx.cursor()
328
329
        cursor.execute(" SELECT name "
330
                       " from tbl_sensors "
331
                       " WHERE id = %s ", (id_,))
332
        if cursor.fetchone() is None:
333
            cursor.close()
334
            cnx.disconnect()
335
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
336
                                   description='API.SENSOR_NOT_FOUND')
337
338
        cursor.execute(" SELECT name "
339
                       " FROM tbl_points "
340
                       " WHERE id = %s ", (new_values['data']['point_id'],))
341
        if cursor.fetchone() is None:
342
            cursor.close()
343
            cnx.disconnect()
344
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
345
                                   description='API.POINT_NOT_FOUND')
346
347
        query = (" SELECT id " 
348
                 " FROM tbl_sensors_points "
349
                 " WHERE sensor_id = %s AND point_id = %s")
350
        cursor.execute(query, (id_, new_values['data']['point_id'],))
351
        if cursor.fetchone() is not None:
352
            cursor.close()
353
            cnx.disconnect()
354
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
355
                                   description='API.SENSOR_POINT_RELATION_EXISTED')
356
357
        add_row = (" INSERT INTO tbl_sensors_points (sensor_id, point_id) "
358
                   " VALUES (%s, %s) ")
359
        cursor.execute(add_row, (id_, new_values['data']['point_id'],))
360
        new_id = cursor.lastrowid
361
        cnx.commit()
362
        cursor.close()
363
        cnx.disconnect()
364
365
        resp.status = falcon.HTTP_201
366
        resp.location = '/sensors/' + str(id_) + '/points/' + str(new_values['data']['point_id'])
367
368
369
class SensorPointItem: