Code Duplication    Length = 101-101 lines in 2 locations

core/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)
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:

core/meter.py 1 location

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