Code Duplication    Length = 95-97 lines in 2 locations

myems-api/core/photovoltaicpowerstation.py 2 locations

@@ 992-1088 (lines=97) @@
989
        resp.status = falcon.HTTP_200
990
        _ = id_
991
992
    @staticmethod
993
    def on_get(req, resp, id_):
994
        access_control(req)
995
        if not id_.isdigit() or int(id_) <= 0:
996
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
997
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
998
999
        cnx = mysql.connector.connect(**config.myems_system_db)
1000
        cursor = cnx.cursor()
1001
1002
        cursor.execute(" SELECT name "
1003
                       " FROM tbl_photovoltaic_power_stations "
1004
                       " WHERE id = %s ", (id_,))
1005
        if cursor.fetchone() is None:
1006
            cursor.close()
1007
            cnx.close()
1008
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1009
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1010
1011
        # query meter dict
1012
        query = (" SELECT id, name, uuid "
1013
                 " FROM tbl_meters ")
1014
        cursor.execute(query)
1015
        rows_meters = cursor.fetchall()
1016
1017
        meter_dict = dict()
1018
        if rows_meters is not None and len(rows_meters) > 0:
1019
            for row in rows_meters:
1020
                meter_dict[row[0]] = {"id": row[0],
1021
                                      "name": row[1],
1022
                                      "uuid": row[2]}
1023
        # query point dict
1024
        query = (" SELECT id, name "
1025
                 " FROM tbl_points ")
1026
        cursor.execute(query)
1027
        rows_points = cursor.fetchall()
1028
1029
        point_dict = dict()
1030
        if rows_points is not None and len(rows_points) > 0:
1031
            for row in rows_points:
1032
                point_dict[row[0]] = {"id": row[0],
1033
                                      "name": row[1]}
1034
1035
        query = (" SELECT id, name, uuid, "
1036
                 "        power_point_id, buy_meter_id, sell_meter_id, capacity, "
1037
                 "        total_active_power_point_id, "
1038
                 "        active_power_a_point_id, "
1039
                 "        active_power_b_point_id, "
1040
                 "        active_power_c_point_id, "
1041
                 "        total_reactive_power_point_id, "
1042
                 "        reactive_power_a_point_id, "
1043
                 "        reactive_power_b_point_id, "
1044
                 "        reactive_power_c_point_id, "
1045
                 "        total_apparent_power_point_id, "
1046
                 "        apparent_power_a_point_id, "
1047
                 "        apparent_power_b_point_id, "
1048
                 "        apparent_power_c_point_id, "
1049
                 "        total_power_factor_point_id, "
1050
                 "        active_energy_import_point_id, "
1051
                 "        active_energy_export_point_id, "
1052
                 "        active_energy_net_point_id "
1053
                 " FROM tbl_photovoltaic_power_stations_grids "
1054
                 " WHERE photovoltaic_power_station_id = %s "
1055
                 " ORDER BY name ")
1056
        cursor.execute(query, (id_,))
1057
        rows = cursor.fetchall()
1058
1059
        result = list()
1060
        if rows is not None and len(rows) > 0:
1061
            for row in rows:
1062
                meta_result = {"id": row[0],
1063
                               "name": row[1],
1064
                               "uuid": row[2],
1065
                               "power_point": point_dict.get(row[3]),
1066
                               "buy_meter": meter_dict.get(row[4]),
1067
                               "sell_meter": meter_dict.get(row[5]),
1068
                               "capacity": row[6],
1069
                               "total_active_power_point": point_dict.get(row[7]),
1070
                               "active_power_a_point": point_dict.get(row[8]),
1071
                               "active_power_b_point": point_dict.get(row[9]),
1072
                               "active_power_c_point": point_dict.get(row[10]),
1073
                               "total_reactive_power_point": point_dict.get(row[11]),
1074
                               "reactive_power_a_point": point_dict.get(row[12]),
1075
                               "reactive_power_b_point": point_dict.get(row[13]),
1076
                               "reactive_power_c_point": point_dict.get(row[14]),
1077
                               "total_apparent_power_point": point_dict.get(row[15]),
1078
                               "apparent_power_a_point": point_dict.get(row[16]),
1079
                               "apparent_power_b_point": point_dict.get(row[17]),
1080
                               "apparent_power_c_point": point_dict.get(row[19]),
1081
                               "total_power_factor_point": point_dict.get(row[19]),
1082
                               "active_energy_import_point": point_dict.get(row[20]),
1083
                               "active_energy_export_point": point_dict.get(row[21]),
1084
                               "active_energy_net_point_id": point_dict.get(row[22]),
1085
                               }
1086
                result.append(meta_result)
1087
1088
        resp.text = json.dumps(result)
1089
1090
    @staticmethod
1091
    @user_logger
@@ 4050-4144 (lines=95) @@
4047
        resp.status = falcon.HTTP_200
4048
        _ = id_
4049
4050
    @staticmethod
4051
    def on_get(req, resp, id_):
4052
        access_control(req)
4053
        if not id_.isdigit() or int(id_) <= 0:
4054
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4055
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4056
4057
        cnx = mysql.connector.connect(**config.myems_system_db)
4058
        cursor = cnx.cursor()
4059
4060
        cursor.execute(" SELECT name "
4061
                       " FROM tbl_photovoltaic_power_stations "
4062
                       " WHERE id = %s ", (id_,))
4063
        if cursor.fetchone() is None:
4064
            cursor.close()
4065
            cnx.close()
4066
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4067
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4068
4069
        # query meter dict
4070
        query = (" SELECT id, name, uuid "
4071
                 " FROM tbl_meters ")
4072
        cursor.execute(query)
4073
        rows_meters = cursor.fetchall()
4074
4075
        meter_dict = dict()
4076
        if rows_meters is not None and len(rows_meters) > 0:
4077
            for row in rows_meters:
4078
                meter_dict[row[0]] = {"id": row[0],
4079
                                      "name": row[1],
4080
                                      "uuid": row[2]}
4081
        # query point dict
4082
        query = (" SELECT id, name "
4083
                 " FROM tbl_points ")
4084
        cursor.execute(query)
4085
        rows_points = cursor.fetchall()
4086
4087
        point_dict = dict()
4088
        if rows_points is not None and len(rows_points) > 0:
4089
            for row in rows_points:
4090
                point_dict[row[0]] = {"id": row[0],
4091
                                      "name": row[1]}
4092
4093
        query = (" SELECT id, name, uuid, "
4094
                 "        power_point_id, meter_id, rated_input_power, "
4095
                 "        total_active_power_point_id, "
4096
                 "        active_power_a_point_id, "
4097
                 "        active_power_b_point_id, "
4098
                 "        active_power_c_point_id, "
4099
                 "        total_reactive_power_point_id, "
4100
                 "        reactive_power_a_point_id, "
4101
                 "        reactive_power_b_point_id, "
4102
                 "        reactive_power_c_point_id, "
4103
                 "        total_apparent_power_point_id, "
4104
                 "        apparent_power_a_point_id, "
4105
                 "        apparent_power_b_point_id, "
4106
                 "        apparent_power_c_point_id, "
4107
                 "        total_power_factor_point_id, "
4108
                 "        active_energy_import_point_id, "
4109
                 "        active_energy_export_point_id, "
4110
                 "        active_energy_net_point_id "
4111
                 " FROM tbl_photovoltaic_power_stations_loads "
4112
                 " WHERE photovoltaic_power_station_id = %s "
4113
                 " ORDER BY name ")
4114
        cursor.execute(query, (id_,))
4115
        rows = cursor.fetchall()
4116
4117
        result = list()
4118
        if rows is not None and len(rows) > 0:
4119
            for row in rows:
4120
                meta_result = {"id": row[0],
4121
                               "name": row[1],
4122
                               "uuid": row[2],
4123
                               "power_point": point_dict.get(row[3]),
4124
                               "meter": meter_dict.get(row[4]),
4125
                               "rated_input_power": row[5],
4126
                               "total_active_power_point": point_dict.get(row[6]),
4127
                               "active_power_a_point": point_dict.get(row[7]),
4128
                               "active_power_b_point": point_dict.get(row[8]),
4129
                               "active_power_c_point": point_dict.get(row[9]),
4130
                               "total_reactive_power_point": point_dict.get(row[10]),
4131
                               "reactive_power_a_point": point_dict.get(row[11]),
4132
                               "reactive_power_b_point": point_dict.get(row[12]),
4133
                               "reactive_power_c_point": point_dict.get(row[13]),
4134
                               "total_apparent_power_point": point_dict.get(row[14]),
4135
                               "apparent_power_a_point": point_dict.get(row[15]),
4136
                               "apparent_power_b_point": point_dict.get(row[16]),
4137
                               "apparent_power_c_point": point_dict.get(row[17]),
4138
                               "total_power_factor_point": point_dict.get(row[18]),
4139
                               "active_energy_import_point": point_dict.get(row[19]),
4140
                               "active_energy_export_point": point_dict.get(row[20]),
4141
                               "active_energy_net_point": point_dict.get(row[21])}
4142
                result.append(meta_result)
4143
4144
        resp.text = json.dumps(result)
4145
4146
    @staticmethod
4147
    @user_logger