Code Duplication    Length = 42-45 lines in 4 locations

myems-api/core/microgrid.py 1 location

@@ 5907-5951 (lines=45) @@
5904
5905
        resp.status = falcon.HTTP_204
5906
5907
class MicrogridDataSourcePointCollection:
5908
    def __init__(self):
5909
        pass
5910
5911
    @staticmethod
5912
    def on_options(req, resp, id_):
5913
        _ = req
5914
        resp.status = falcon.HTTP_200
5915
        _ = id_
5916
5917
    @staticmethod
5918
    def on_get(req, resp, id_):
5919
        if 'API-KEY' not in req.headers or \
5920
                not isinstance(req.headers['API-KEY'], str) or \
5921
                len(str.strip(req.headers['API-KEY'])) == 0:
5922
            access_control(req)
5923
        else:
5924
            api_key_control(req)
5925
        if not id_.isdigit() or int(id_) <= 0:
5926
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5927
                                   description='API.INVALID_MICROGRID_ID')
5928
5929
        cnx = mysql.connector.connect(**config.myems_system_db)
5930
        cursor = cnx.cursor()
5931
5932
        query = (" SELECT p.id, p.name "
5933
                 " FROM tbl_points p, tbl_microgrids_data_sources mds, tbl_data_sources ds "
5934
                 " WHERE mds.microgrid_id = %s "
5935
                 "       AND mds.data_source_id = ds.id "
5936
                 "       AND p.data_source_id = ds.id "
5937
                 " ORDER BY p.id ")
5938
        cursor.execute(query, (id_,))
5939
        rows = cursor.fetchall()
5940
5941
        result = list()
5942
        if rows is not None and len(rows) > 0:
5943
            for row in rows:
5944
                meta_result = {"id": row[0], "name": row[1]}
5945
                result.append(meta_result)
5946
5947
        cursor.close()
5948
        cnx.close()
5949
5950
        resp.text = json.dumps(result)
5951
5952
class MicrogridBatteryPointCollection:
5953
    def __init__(self):
5954
        pass

myems-api/core/photovoltaicpowerstation.py 1 location

@@ 655-699 (lines=45) @@
652
        resp.status = falcon.HTTP_200
653
654
655
class PhotovoltaicPowerStationDataSourcePointCollection:
656
    def __init__(self):
657
        pass
658
659
    @staticmethod
660
    def on_options(req, resp, id_):
661
        _ = req
662
        resp.status = falcon.HTTP_200
663
        _ = id_
664
665
    @staticmethod
666
    def on_get(req, resp, id_):
667
        if 'API-KEY' not in req.headers or \
668
                not isinstance(req.headers['API-KEY'], str) or \
669
                len(str.strip(req.headers['API-KEY'])) == 0:
670
            access_control(req)
671
        else:
672
            api_key_control(req)
673
        if not id_.isdigit() or int(id_) <= 0:
674
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
675
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
676
677
        cnx = mysql.connector.connect(** config.myems_system_db)
678
        cursor = cnx.cursor()
679
680
        query = (" SELECT p.id, p.name "
681
                 " FROM tbl_points p, tbl_photovoltaic_power_stations_data_sources ppds, tbl_data_sources ds "
682
                 " WHERE ppds.photovoltaic_power_station_id = %s "
683
                 "       AND ppds.data_source_id = ds.id "
684
                 "       AND p.data_source_id = ds.id "
685
                 " ORDER BY p.id ")
686
        cursor.execute(query, (id_,))
687
        rows = cursor.fetchall()
688
689
        result = list()
690
        if rows is not None and len(rows) > 0:
691
            for row in rows:
692
                meta_result = {"id": row[0], "name": row[1]}
693
                result.append(meta_result)
694
695
        cursor.close()
696
        cnx.close()
697
698
        resp.text = json.dumps(result)
699
700
class PhotovoltaicPowerStationExport:
701
    def __init__(self):
702
        pass

myems-api/core/energystoragepowerstation.py 1 location

@@ 991-1034 (lines=44) @@
988
        resp.status = falcon.HTTP_204
989
990
991
class EnergyStoragePowerStationDataSourcePointCollection:
992
    def __init__(self):
993
        pass
994
995
    @staticmethod
996
    def on_options(req, resp, id_,):
997
        _ = req
998
        resp.status = falcon.HTTP_200
999
        _ = id_
1000
1001
    @staticmethod
1002
    def on_get(req, resp, id_,):
1003
        if 'API-KEY' not in req.headers or \
1004
                not isinstance(req.headers['API-KEY'], str) or \
1005
                len(str.strip(req.headers['API-KEY'])) == 0:
1006
            access_control(req)
1007
        else:
1008
            api_key_control(req)
1009
        if not id_.isdigit() or int(id_) <= 0:
1010
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1011
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1012
1013
        cnx = mysql.connector.connect(**config.myems_system_db)
1014
        cursor = cnx.cursor()
1015
1016
        query = (" SELECT p.id, p.name "
1017
                 " FROM tbl_points p, tbl_energy_storage_power_stations_containers espsc, "
1018
                 "      tbl_energy_storage_containers_data_sources ecds, tbl_data_sources ds "
1019
                 " WHERE espsc.energy_storage_power_station_id = %s "
1020
                 "       AND espsc.energy_storage_container_id = ecds.energy_storage_container_id "
1021
                 "       AND ecds.data_source_id = ds.id  "
1022
                 "       AND p.data_source_id = ds.id "
1023
                 " ORDER BY p.id ")
1024
        cursor.execute(query, (id_,))
1025
        rows = cursor.fetchall()
1026
1027
        result = list()
1028
        if rows is not None and len(rows) > 0:
1029
            for row in rows:
1030
                meta_result = {"id": row[0], "name": row[1]}
1031
                result.append(meta_result)
1032
1033
        resp.text = json.dumps(result)
1034
1035
1036
class EnergyStoragePowerStationUserCollection:
1037
    def __init__(self):

myems-api/core/energystoragecontainer.py 1 location

@@ 1722-1763 (lines=42) @@
1719
        resp.status = falcon.HTTP_204
1720
1721
1722
class EnergyStorageContainerDataSourcePointCollection:
1723
    def __init__(self):
1724
        pass
1725
1726
    @staticmethod
1727
    def on_options(req, resp, id_,):
1728
        _ = req
1729
        resp.status = falcon.HTTP_200
1730
        _ = id_
1731
1732
    @staticmethod
1733
    def on_get(req, resp, id_,):
1734
        if 'API-KEY' not in req.headers or \
1735
                not isinstance(req.headers['API-KEY'], str) or \
1736
                len(str.strip(req.headers['API-KEY'])) == 0:
1737
            access_control(req)
1738
        else:
1739
            api_key_control(req)
1740
        if not id_.isdigit() or int(id_) <= 0:
1741
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1742
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1743
1744
        cnx = mysql.connector.connect(**config.myems_system_db)
1745
        cursor = cnx.cursor()
1746
1747
        query = (" SELECT p.id, p.name "
1748
                 " FROM tbl_points p, tbl_energy_storage_containers_data_sources ecds, tbl_data_sources ds "
1749
                 " WHERE ecds.energy_storage_container_id = %s "
1750
                 "       AND ecds.data_source_id = ds.id "
1751
                 "       AND p.data_source_id = ds.id "
1752
                 " ORDER BY p.id ")
1753
        cursor.execute(query, (id_,))
1754
        rows = cursor.fetchall()
1755
1756
        result = list()
1757
        if rows is not None and len(rows) > 0:
1758
            for row in rows:
1759
                meta_result = {"id": row[0], "name": row[1]}
1760
                result.append(meta_result)
1761
1762
        resp.text = json.dumps(result)
1763
1764
1765
class EnergyStorageContainerDCDCCollection:
1766
    def __init__(self):