Code Duplication    Length = 113-117 lines in 2 locations

myems-api/core/photovoltaicpowerstation.py 2 locations

@@ 1417-1533 (lines=117) @@
1414
        resp.status = falcon.HTTP_200
1415
        _ = id_
1416
1417
    @staticmethod
1418
    def on_get(req, resp, id_, gid):
1419
        access_control(req)
1420
        if not id_.isdigit() or int(id_) <= 0:
1421
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1422
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1423
        if not gid.isdigit() or int(gid) <= 0:
1424
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1425
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_ID')
1426
1427
        cnx = mysql.connector.connect(**config.myems_system_db)
1428
        cursor = cnx.cursor()
1429
1430
        cursor.execute(" SELECT name "
1431
                       " FROM tbl_photovoltaic_power_stations "
1432
                       " WHERE id = %s ", (id_,))
1433
        if cursor.fetchone() is None:
1434
            cursor.close()
1435
            cnx.close()
1436
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1437
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1438
1439
        query = (" SELECT id, name, uuid "
1440
                 " FROM tbl_photovoltaic_power_stations ")
1441
        cursor.execute(query)
1442
        rows_photovoltaic_power_stations = cursor.fetchall()
1443
1444
        photovoltaic_power_station_dict = dict()
1445
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
1446
            for row in rows_photovoltaic_power_stations:
1447
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
1448
                                                           "name": row[1],
1449
                                                           "uuid": row[2]}
1450
        # query meter dict
1451
        query = (" SELECT id, name, uuid "
1452
                 " FROM tbl_meters ")
1453
        cursor.execute(query)
1454
        rows_meters = cursor.fetchall()
1455
1456
        meter_dict = dict()
1457
        if rows_meters is not None and len(rows_meters) > 0:
1458
            for row in rows_meters:
1459
                meter_dict[row[0]] = {"id": row[0],
1460
                                      "name": row[1],
1461
                                      "uuid": row[2]}
1462
        # query point dict
1463
        query = (" SELECT id, name "
1464
                 " FROM tbl_points ")
1465
        cursor.execute(query)
1466
        rows_points = cursor.fetchall()
1467
1468
        point_dict = dict()
1469
        if rows_points is not None and len(rows_points) > 0:
1470
            for row in rows_points:
1471
                point_dict[row[0]] = {"id": row[0],
1472
                                      "name": row[1]}
1473
1474
        query = (" SELECT id, name, uuid, "
1475
                 "        photovoltaic_power_station_id, "
1476
                 "        power_point_id, "
1477
                 "        buy_meter_id, "
1478
                 "        sell_meter_id, "
1479
                 "        capacity, "
1480
                 "        total_active_power_point_id, "
1481
                 "        active_power_a_point_id, "
1482
                 "        active_power_b_point_id, "
1483
                 "        active_power_c_point_id, "
1484
                 "        total_reactive_power_point_id, "
1485
                 "        reactive_power_a_point_id, "
1486
                 "        reactive_power_b_point_id, "
1487
                 "        reactive_power_c_point_id, "
1488
                 "        total_apparent_power_point_id, "
1489
                 "        apparent_power_a_point_id, "
1490
                 "        apparent_power_b_point_id, "
1491
                 "        apparent_power_c_point_id, "
1492
                 "        total_power_factor_point_id, "
1493
                 "        active_energy_import_point_id, "
1494
                 "        active_energy_export_point_id, "
1495
                 "        active_energy_net_point_id "
1496
                 " FROM tbl_photovoltaic_power_stations_grids "
1497
                 " WHERE id = %s ")
1498
        cursor.execute(query, (gid,))
1499
        row = cursor.fetchone()
1500
        cursor.close()
1501
        cnx.close()
1502
1503
        if row is None:
1504
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1505
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NOT_FOUND')
1506
        else:
1507
            meta_result = {"id": row[0],
1508
                           "name": row[1],
1509
                           "uuid": row[2],
1510
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
1511
                           "power_point": point_dict.get(row[4]),
1512
                           "buy_meter": meter_dict.get(row[5]),
1513
                           "sell_meter": meter_dict.get(row[6]),
1514
                           "capacity": row[7],
1515
                           "total_active_power_point": point_dict.get(row[8]),
1516
                           "active_power_a_point": point_dict.get(row[9]),
1517
                           "active_power_b_point": point_dict.get(row[10]),
1518
                           "active_power_c_point": point_dict.get(row[11]),
1519
                           "total_reactive_power_point": point_dict.get(row[12]),
1520
                           "reactive_power_a_point": point_dict.get(row[13]),
1521
                           "reactive_power_b_point": point_dict.get(row[14]),
1522
                           "reactive_power_c_point": point_dict.get(row[15]),
1523
                           "total_apparent_power_point": point_dict.get(row[16]),
1524
                           "apparent_power_a_point": point_dict.get(row[17]),
1525
                           "apparent_power_b_point": point_dict.get(row[18]),
1526
                           "apparent_power_c_point": point_dict.get(row[19]),
1527
                           "total_power_factor_point": point_dict.get(row[20]),
1528
                           "active_energy_import_point": point_dict.get(row[21]),
1529
                           "active_energy_export_point": point_dict.get(row[22]),
1530
                           "active_energy_net_point_id": point_dict.get(row[23]),
1531
                           }
1532
1533
        resp.text = json.dumps(meta_result)
1534
1535
    @staticmethod
1536
    @user_logger
@@ 4454-4566 (lines=113) @@
4451
        resp.status = falcon.HTTP_200
4452
        _ = id_
4453
4454
    @staticmethod
4455
    def on_get(req, resp, id_, lid):
4456
        access_control(req)
4457
        if not id_.isdigit() or int(id_) <= 0:
4458
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4459
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4460
        if not lid.isdigit() or int(lid) <= 0:
4461
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4462
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_ID')
4463
4464
        cnx = mysql.connector.connect(**config.myems_system_db)
4465
        cursor = cnx.cursor()
4466
4467
        cursor.execute(" SELECT name "
4468
                       " FROM tbl_photovoltaic_power_stations "
4469
                       " WHERE id = %s ", (id_,))
4470
        if cursor.fetchone() is None:
4471
            cursor.close()
4472
            cnx.close()
4473
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4474
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4475
4476
        query = (" SELECT id, name, uuid "
4477
                 " FROM tbl_photovoltaic_power_stations ")
4478
        cursor.execute(query)
4479
        rows_photovoltaic_power_stations = cursor.fetchall()
4480
4481
        photovoltaic_power_station_dict = dict()
4482
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
4483
            for row in rows_photovoltaic_power_stations:
4484
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
4485
                                                           "name": row[1],
4486
                                                           "uuid": row[2]}
4487
        # query meter dict
4488
        query = (" SELECT id, name, uuid "
4489
                 " FROM tbl_meters ")
4490
        cursor.execute(query)
4491
        rows_meters = cursor.fetchall()
4492
4493
        meter_dict = dict()
4494
        if rows_meters is not None and len(rows_meters) > 0:
4495
            for row in rows_meters:
4496
                meter_dict[row[0]] = {"id": row[0],
4497
                                      "name": row[1],
4498
                                      "uuid": row[2]}
4499
        # query point dict
4500
        query = (" SELECT id, name "
4501
                 " FROM tbl_points ")
4502
        cursor.execute(query)
4503
        rows_points = cursor.fetchall()
4504
4505
        point_dict = dict()
4506
        if rows_points is not None and len(rows_points) > 0:
4507
            for row in rows_points:
4508
                point_dict[row[0]] = {"id": row[0],
4509
                                      "name": row[1]}
4510
4511
        query = (" SELECT id, name, uuid, "
4512
                 "        photovoltaic_power_station_id, "
4513
                 "        power_point_id, meter_id, "
4514
                 "        rated_input_power, "
4515
                 "        total_active_power_point_id, "
4516
                 "        active_power_a_point_id, "
4517
                 "        active_power_b_point_id, "
4518
                 "        active_power_c_point_id, "
4519
                 "        total_reactive_power_point_id, "
4520
                 "        reactive_power_a_point_id, "
4521
                 "        reactive_power_b_point_id, "
4522
                 "        reactive_power_c_point_id, "
4523
                 "        total_apparent_power_point_id, "
4524
                 "        apparent_power_a_point_id, "
4525
                 "        apparent_power_b_point_id, "
4526
                 "        apparent_power_c_point_id, "
4527
                 "        total_power_factor_point_id, "
4528
                 "        active_energy_import_point_id, "
4529
                 "        active_energy_export_point_id, "
4530
                 "        active_energy_net_point_id "
4531
                 " FROM tbl_photovoltaic_power_stations_loads "
4532
                 " WHERE id = %s ")
4533
        cursor.execute(query, (lid,))
4534
        row = cursor.fetchone()
4535
        cursor.close()
4536
        cnx.close()
4537
4538
        if row is None:
4539
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4540
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NOT_FOUND')
4541
        else:
4542
            meta_result = {"id": row[0],
4543
                           "name": row[1],
4544
                           "uuid": row[2],
4545
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
4546
                           "power_point": point_dict.get(row[4]),
4547
                           "meter": meter_dict.get(row[5]),
4548
                           "rated_input_power": row[6],
4549
                           "total_active_power_point": point_dict.get(row[7]),
4550
                           "active_power_a_point": point_dict.get(row[8]),
4551
                           "active_power_b_point": point_dict.get(row[9]),
4552
                           "active_power_c_point": point_dict.get(row[10]),
4553
                           "total_reactive_power_point": point_dict.get(row[11]),
4554
                           "reactive_power_a_point": point_dict.get(row[12]),
4555
                           "reactive_power_b_point": point_dict.get(row[13]),
4556
                           "reactive_power_c_point": point_dict.get(row[14]),
4557
                           "total_apparent_power_point": point_dict.get(row[15]),
4558
                           "apparent_power_a_point": point_dict.get(row[16]),
4559
                           "apparent_power_b_point": point_dict.get(row[17]),
4560
                           "apparent_power_c_point": point_dict.get(row[18]),
4561
                           "total_power_factor_point": point_dict.get(row[19]),
4562
                           "active_energy_import_point": point_dict.get(row[20]),
4563
                           "active_energy_export_point": point_dict.get(row[21]),
4564
                           "active_energy_net_point": point_dict.get(row[22])}
4565
4566
        resp.text = json.dumps(meta_result)
4567
4568
    @staticmethod
4569
    @user_logger