Code Duplication    Length = 95-97 lines in 2 locations

myems-api/core/photovoltaicpowerstation.py 2 locations

@@ 1038-1134 (lines=97) @@
1035
        resp.status = falcon.HTTP_200
1036
        _ = id_
1037
1038
    @staticmethod
1039
    def on_get(req, resp, id_):
1040
        access_control(req)
1041
        if not id_.isdigit() or int(id_) <= 0:
1042
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1043
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1044
1045
        cnx = mysql.connector.connect(**config.myems_system_db)
1046
        cursor = cnx.cursor()
1047
1048
        cursor.execute(" SELECT name "
1049
                       " FROM tbl_photovoltaic_power_stations "
1050
                       " WHERE id = %s ", (id_,))
1051
        if cursor.fetchone() is None:
1052
            cursor.close()
1053
            cnx.close()
1054
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1055
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1056
1057
        # query meter dict
1058
        query = (" SELECT id, name, uuid "
1059
                 " FROM tbl_meters ")
1060
        cursor.execute(query)
1061
        rows_meters = cursor.fetchall()
1062
1063
        meter_dict = dict()
1064
        if rows_meters is not None and len(rows_meters) > 0:
1065
            for row in rows_meters:
1066
                meter_dict[row[0]] = {"id": row[0],
1067
                                      "name": row[1],
1068
                                      "uuid": row[2]}
1069
        # query point dict
1070
        query = (" SELECT id, name "
1071
                 " FROM tbl_points ")
1072
        cursor.execute(query)
1073
        rows_points = cursor.fetchall()
1074
1075
        point_dict = dict()
1076
        if rows_points is not None and len(rows_points) > 0:
1077
            for row in rows_points:
1078
                point_dict[row[0]] = {"id": row[0],
1079
                                      "name": row[1]}
1080
1081
        query = (" SELECT id, name, uuid, "
1082
                 "        power_point_id, buy_meter_id, sell_meter_id, capacity, "
1083
                 "        total_active_power_point_id, "
1084
                 "        active_power_a_point_id, "
1085
                 "        active_power_b_point_id, "
1086
                 "        active_power_c_point_id, "
1087
                 "        total_reactive_power_point_id, "
1088
                 "        reactive_power_a_point_id, "
1089
                 "        reactive_power_b_point_id, "
1090
                 "        reactive_power_c_point_id, "
1091
                 "        total_apparent_power_point_id, "
1092
                 "        apparent_power_a_point_id, "
1093
                 "        apparent_power_b_point_id, "
1094
                 "        apparent_power_c_point_id, "
1095
                 "        total_power_factor_point_id, "
1096
                 "        active_energy_import_point_id, "
1097
                 "        active_energy_export_point_id, "
1098
                 "        active_energy_net_point_id "
1099
                 " FROM tbl_photovoltaic_power_stations_grids "
1100
                 " WHERE photovoltaic_power_station_id = %s "
1101
                 " ORDER BY name ")
1102
        cursor.execute(query, (id_,))
1103
        rows = cursor.fetchall()
1104
1105
        result = list()
1106
        if rows is not None and len(rows) > 0:
1107
            for row in rows:
1108
                meta_result = {"id": row[0],
1109
                               "name": row[1],
1110
                               "uuid": row[2],
1111
                               "power_point": point_dict.get(row[3]),
1112
                               "buy_meter": meter_dict.get(row[4]),
1113
                               "sell_meter": meter_dict.get(row[5]),
1114
                               "capacity": row[6],
1115
                               "total_active_power_point": point_dict.get(row[7]),
1116
                               "active_power_a_point": point_dict.get(row[8]),
1117
                               "active_power_b_point": point_dict.get(row[9]),
1118
                               "active_power_c_point": point_dict.get(row[10]),
1119
                               "total_reactive_power_point": point_dict.get(row[11]),
1120
                               "reactive_power_a_point": point_dict.get(row[12]),
1121
                               "reactive_power_b_point": point_dict.get(row[13]),
1122
                               "reactive_power_c_point": point_dict.get(row[14]),
1123
                               "total_apparent_power_point": point_dict.get(row[15]),
1124
                               "apparent_power_a_point": point_dict.get(row[16]),
1125
                               "apparent_power_b_point": point_dict.get(row[17]),
1126
                               "apparent_power_c_point": point_dict.get(row[19]),
1127
                               "total_power_factor_point": point_dict.get(row[19]),
1128
                               "active_energy_import_point": point_dict.get(row[20]),
1129
                               "active_energy_export_point": point_dict.get(row[21]),
1130
                               "active_energy_net_point_id": point_dict.get(row[22]),
1131
                               }
1132
                result.append(meta_result)
1133
1134
        resp.text = json.dumps(result)
1135
1136
    @staticmethod
1137
    @user_logger
@@ 4096-4190 (lines=95) @@
4093
        resp.status = falcon.HTTP_200
4094
        _ = id_
4095
4096
    @staticmethod
4097
    def on_get(req, resp, id_):
4098
        access_control(req)
4099
        if not id_.isdigit() or int(id_) <= 0:
4100
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4101
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4102
4103
        cnx = mysql.connector.connect(**config.myems_system_db)
4104
        cursor = cnx.cursor()
4105
4106
        cursor.execute(" SELECT name "
4107
                       " FROM tbl_photovoltaic_power_stations "
4108
                       " WHERE id = %s ", (id_,))
4109
        if cursor.fetchone() is None:
4110
            cursor.close()
4111
            cnx.close()
4112
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4113
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4114
4115
        # query meter dict
4116
        query = (" SELECT id, name, uuid "
4117
                 " FROM tbl_meters ")
4118
        cursor.execute(query)
4119
        rows_meters = cursor.fetchall()
4120
4121
        meter_dict = dict()
4122
        if rows_meters is not None and len(rows_meters) > 0:
4123
            for row in rows_meters:
4124
                meter_dict[row[0]] = {"id": row[0],
4125
                                      "name": row[1],
4126
                                      "uuid": row[2]}
4127
        # query point dict
4128
        query = (" SELECT id, name "
4129
                 " FROM tbl_points ")
4130
        cursor.execute(query)
4131
        rows_points = cursor.fetchall()
4132
4133
        point_dict = dict()
4134
        if rows_points is not None and len(rows_points) > 0:
4135
            for row in rows_points:
4136
                point_dict[row[0]] = {"id": row[0],
4137
                                      "name": row[1]}
4138
4139
        query = (" SELECT id, name, uuid, "
4140
                 "        power_point_id, meter_id, rated_input_power, "
4141
                 "        total_active_power_point_id, "
4142
                 "        active_power_a_point_id, "
4143
                 "        active_power_b_point_id, "
4144
                 "        active_power_c_point_id, "
4145
                 "        total_reactive_power_point_id, "
4146
                 "        reactive_power_a_point_id, "
4147
                 "        reactive_power_b_point_id, "
4148
                 "        reactive_power_c_point_id, "
4149
                 "        total_apparent_power_point_id, "
4150
                 "        apparent_power_a_point_id, "
4151
                 "        apparent_power_b_point_id, "
4152
                 "        apparent_power_c_point_id, "
4153
                 "        total_power_factor_point_id, "
4154
                 "        active_energy_import_point_id, "
4155
                 "        active_energy_export_point_id, "
4156
                 "        active_energy_net_point_id "
4157
                 " FROM tbl_photovoltaic_power_stations_loads "
4158
                 " WHERE photovoltaic_power_station_id = %s "
4159
                 " ORDER BY name ")
4160
        cursor.execute(query, (id_,))
4161
        rows = cursor.fetchall()
4162
4163
        result = list()
4164
        if rows is not None and len(rows) > 0:
4165
            for row in rows:
4166
                meta_result = {"id": row[0],
4167
                               "name": row[1],
4168
                               "uuid": row[2],
4169
                               "power_point": point_dict.get(row[3]),
4170
                               "meter": meter_dict.get(row[4]),
4171
                               "rated_input_power": row[5],
4172
                               "total_active_power_point": point_dict.get(row[6]),
4173
                               "active_power_a_point": point_dict.get(row[7]),
4174
                               "active_power_b_point": point_dict.get(row[8]),
4175
                               "active_power_c_point": point_dict.get(row[9]),
4176
                               "total_reactive_power_point": point_dict.get(row[10]),
4177
                               "reactive_power_a_point": point_dict.get(row[11]),
4178
                               "reactive_power_b_point": point_dict.get(row[12]),
4179
                               "reactive_power_c_point": point_dict.get(row[13]),
4180
                               "total_apparent_power_point": point_dict.get(row[14]),
4181
                               "apparent_power_a_point": point_dict.get(row[15]),
4182
                               "apparent_power_b_point": point_dict.get(row[16]),
4183
                               "apparent_power_c_point": point_dict.get(row[17]),
4184
                               "total_power_factor_point": point_dict.get(row[18]),
4185
                               "active_energy_import_point": point_dict.get(row[19]),
4186
                               "active_energy_export_point": point_dict.get(row[20]),
4187
                               "active_energy_net_point": point_dict.get(row[21])}
4188
                result.append(meta_result)
4189
4190
        resp.text = json.dumps(result)
4191
4192
    @staticmethod
4193
    @user_logger