Code Duplication    Length = 95-97 lines in 2 locations

myems-api/core/photovoltaicpowerstation.py 2 locations

@@ 967-1063 (lines=97) @@
964
    def on_options(req, resp, id_):
965
        resp.status = falcon.HTTP_200
966
967
    @staticmethod
968
    def on_get(req, resp, id_):
969
        access_control(req)
970
        if not id_.isdigit() or int(id_) <= 0:
971
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
972
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
973
974
        cnx = mysql.connector.connect(**config.myems_system_db)
975
        cursor = cnx.cursor()
976
977
        cursor.execute(" SELECT name "
978
                       " FROM tbl_photovoltaic_power_stations "
979
                       " WHERE id = %s ", (id_,))
980
        if cursor.fetchone() is None:
981
            cursor.close()
982
            cnx.close()
983
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
984
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
985
986
        # query meter dict
987
        query = (" SELECT id, name, uuid "
988
                 " FROM tbl_meters ")
989
        cursor.execute(query)
990
        rows_meters = cursor.fetchall()
991
992
        meter_dict = dict()
993
        if rows_meters is not None and len(rows_meters) > 0:
994
            for row in rows_meters:
995
                meter_dict[row[0]] = {"id": row[0],
996
                                      "name": row[1],
997
                                      "uuid": row[2]}
998
        # query point dict
999
        query = (" SELECT id, name "
1000
                 " FROM tbl_points ")
1001
        cursor.execute(query)
1002
        rows_points = cursor.fetchall()
1003
1004
        point_dict = dict()
1005
        if rows_points is not None and len(rows_points) > 0:
1006
            for row in rows_points:
1007
                point_dict[row[0]] = {"id": row[0],
1008
                                      "name": row[1]}
1009
1010
        query = (" SELECT id, name, uuid, "
1011
                 "        power_point_id, buy_meter_id, sell_meter_id, capacity, "
1012
                 "        total_active_power_point_id, "
1013
                 "        active_power_a_point_id, "
1014
                 "        active_power_b_point_id, "
1015
                 "        active_power_c_point_id, "
1016
                 "        total_reactive_power_point_id, "
1017
                 "        reactive_power_a_point_id, "
1018
                 "        reactive_power_b_point_id, "
1019
                 "        reactive_power_c_point_id, "
1020
                 "        total_apparent_power_point_id, "
1021
                 "        apparent_power_a_point_id, "
1022
                 "        apparent_power_b_point_id, "
1023
                 "        apparent_power_c_point_id, "
1024
                 "        total_power_factor_point_id, "
1025
                 "        active_energy_import_point_id, "
1026
                 "        active_energy_export_point_id, "
1027
                 "        active_energy_net_point_id "
1028
                 " FROM tbl_photovoltaic_power_stations_grids "
1029
                 " WHERE photovoltaic_power_station_id = %s "
1030
                 " ORDER BY name ")
1031
        cursor.execute(query, (id_,))
1032
        rows = cursor.fetchall()
1033
1034
        result = list()
1035
        if rows is not None and len(rows) > 0:
1036
            for row in rows:
1037
                meta_result = {"id": row[0],
1038
                               "name": row[1],
1039
                               "uuid": row[2],
1040
                               "power_point": point_dict.get(row[3]),
1041
                               "buy_meter": meter_dict.get(row[4]),
1042
                               "sell_meter": meter_dict.get(row[5]),
1043
                               "capacity": row[6],
1044
                               "total_active_power_point": point_dict.get(row[7]),
1045
                               "active_power_a_point": point_dict.get(row[8]),
1046
                               "active_power_b_point": point_dict.get(row[9]),
1047
                               "active_power_c_point": point_dict.get(row[10]),
1048
                               "total_reactive_power_point": point_dict.get(row[11]),
1049
                               "reactive_power_a_point": point_dict.get(row[12]),
1050
                               "reactive_power_b_point": point_dict.get(row[13]),
1051
                               "reactive_power_c_point": point_dict.get(row[14]),
1052
                               "total_apparent_power_point": point_dict.get(row[15]),
1053
                               "apparent_power_a_point": point_dict.get(row[16]),
1054
                               "apparent_power_b_point": point_dict.get(row[17]),
1055
                               "apparent_power_c_point": point_dict.get(row[19]),
1056
                               "total_power_factor_point": point_dict.get(row[19]),
1057
                               "active_energy_import_point": point_dict.get(row[20]),
1058
                               "active_energy_export_point": point_dict.get(row[21]),
1059
                               "active_energy_net_point_id": point_dict.get(row[22]),
1060
                               }
1061
                result.append(meta_result)
1062
1063
        resp.text = json.dumps(result)
1064
1065
    @staticmethod
1066
    @user_logger
@@ 4013-4107 (lines=95) @@
4010
    def on_options(req, resp, id_):
4011
        resp.status = falcon.HTTP_200
4012
4013
    @staticmethod
4014
    def on_get(req, resp, id_):
4015
        access_control(req)
4016
        if not id_.isdigit() or int(id_) <= 0:
4017
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4018
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4019
4020
        cnx = mysql.connector.connect(**config.myems_system_db)
4021
        cursor = cnx.cursor()
4022
4023
        cursor.execute(" SELECT name "
4024
                       " FROM tbl_photovoltaic_power_stations "
4025
                       " WHERE id = %s ", (id_,))
4026
        if cursor.fetchone() is None:
4027
            cursor.close()
4028
            cnx.close()
4029
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4030
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4031
4032
        # query meter dict
4033
        query = (" SELECT id, name, uuid "
4034
                 " FROM tbl_meters ")
4035
        cursor.execute(query)
4036
        rows_meters = cursor.fetchall()
4037
4038
        meter_dict = dict()
4039
        if rows_meters is not None and len(rows_meters) > 0:
4040
            for row in rows_meters:
4041
                meter_dict[row[0]] = {"id": row[0],
4042
                                      "name": row[1],
4043
                                      "uuid": row[2]}
4044
        # query point dict
4045
        query = (" SELECT id, name "
4046
                 " FROM tbl_points ")
4047
        cursor.execute(query)
4048
        rows_points = cursor.fetchall()
4049
4050
        point_dict = dict()
4051
        if rows_points is not None and len(rows_points) > 0:
4052
            for row in rows_points:
4053
                point_dict[row[0]] = {"id": row[0],
4054
                                      "name": row[1]}
4055
4056
        query = (" SELECT id, name, uuid, "
4057
                 "        power_point_id, meter_id, rated_input_power, "
4058
                 "        total_active_power_point_id, "
4059
                 "        active_power_a_point_id, "
4060
                 "        active_power_b_point_id, "
4061
                 "        active_power_c_point_id, "
4062
                 "        total_reactive_power_point_id, "
4063
                 "        reactive_power_a_point_id, "
4064
                 "        reactive_power_b_point_id, "
4065
                 "        reactive_power_c_point_id, "
4066
                 "        total_apparent_power_point_id, "
4067
                 "        apparent_power_a_point_id, "
4068
                 "        apparent_power_b_point_id, "
4069
                 "        apparent_power_c_point_id, "
4070
                 "        total_power_factor_point_id, "
4071
                 "        active_energy_import_point_id, "
4072
                 "        active_energy_export_point_id, "
4073
                 "        active_energy_net_point_id "
4074
                 " FROM tbl_photovoltaic_power_stations_loads "
4075
                 " WHERE photovoltaic_power_station_id = %s "
4076
                 " ORDER BY name ")
4077
        cursor.execute(query, (id_,))
4078
        rows = cursor.fetchall()
4079
4080
        result = list()
4081
        if rows is not None and len(rows) > 0:
4082
            for row in rows:
4083
                meta_result = {"id": row[0],
4084
                               "name": row[1],
4085
                               "uuid": row[2],
4086
                               "power_point": point_dict.get(row[3]),
4087
                               "meter": meter_dict.get(row[4]),
4088
                               "rated_input_power": row[5],
4089
                               "total_active_power_point": point_dict.get(row[6]),
4090
                               "active_power_a_point": point_dict.get(row[7]),
4091
                               "active_power_b_point": point_dict.get(row[8]),
4092
                               "active_power_c_point": point_dict.get(row[9]),
4093
                               "total_reactive_power_point": point_dict.get(row[10]),
4094
                               "reactive_power_a_point": point_dict.get(row[11]),
4095
                               "reactive_power_b_point": point_dict.get(row[12]),
4096
                               "reactive_power_c_point": point_dict.get(row[13]),
4097
                               "total_apparent_power_point": point_dict.get(row[14]),
4098
                               "apparent_power_a_point": point_dict.get(row[15]),
4099
                               "apparent_power_b_point": point_dict.get(row[16]),
4100
                               "apparent_power_c_point": point_dict.get(row[17]),
4101
                               "total_power_factor_point": point_dict.get(row[18]),
4102
                               "active_energy_import_point": point_dict.get(row[19]),
4103
                               "active_energy_export_point": point_dict.get(row[20]),
4104
                               "active_energy_net_point": point_dict.get(row[21])}
4105
                result.append(meta_result)
4106
4107
        resp.text = json.dumps(result)
4108
4109
    @staticmethod
4110
    @user_logger