Code Duplication    Length = 113-117 lines in 2 locations

myems-api/core/photovoltaicpowerstation.py 2 locations

@@ 1468-1584 (lines=117) @@
1465
        resp.status = falcon.HTTP_200
1466
        _ = id_
1467
1468
    @staticmethod
1469
    def on_get(req, resp, id_, gid):
1470
        access_control(req)
1471
        if not id_.isdigit() or int(id_) <= 0:
1472
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1473
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1474
        if not gid.isdigit() or int(gid) <= 0:
1475
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1476
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_ID')
1477
1478
        cnx = mysql.connector.connect(**config.myems_system_db)
1479
        cursor = cnx.cursor()
1480
1481
        cursor.execute(" SELECT name "
1482
                       " FROM tbl_photovoltaic_power_stations "
1483
                       " WHERE id = %s ", (id_,))
1484
        if cursor.fetchone() is None:
1485
            cursor.close()
1486
            cnx.close()
1487
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1488
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1489
1490
        query = (" SELECT id, name, uuid "
1491
                 " FROM tbl_photovoltaic_power_stations ")
1492
        cursor.execute(query)
1493
        rows_photovoltaic_power_stations = cursor.fetchall()
1494
1495
        photovoltaic_power_station_dict = dict()
1496
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
1497
            for row in rows_photovoltaic_power_stations:
1498
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
1499
                                                           "name": row[1],
1500
                                                           "uuid": row[2]}
1501
        # query meter dict
1502
        query = (" SELECT id, name, uuid "
1503
                 " FROM tbl_meters ")
1504
        cursor.execute(query)
1505
        rows_meters = cursor.fetchall()
1506
1507
        meter_dict = dict()
1508
        if rows_meters is not None and len(rows_meters) > 0:
1509
            for row in rows_meters:
1510
                meter_dict[row[0]] = {"id": row[0],
1511
                                      "name": row[1],
1512
                                      "uuid": row[2]}
1513
        # query point dict
1514
        query = (" SELECT id, name "
1515
                 " FROM tbl_points ")
1516
        cursor.execute(query)
1517
        rows_points = cursor.fetchall()
1518
1519
        point_dict = dict()
1520
        if rows_points is not None and len(rows_points) > 0:
1521
            for row in rows_points:
1522
                point_dict[row[0]] = {"id": row[0],
1523
                                      "name": row[1]}
1524
1525
        query = (" SELECT id, name, uuid, "
1526
                 "        photovoltaic_power_station_id, "
1527
                 "        power_point_id, "
1528
                 "        buy_meter_id, "
1529
                 "        sell_meter_id, "
1530
                 "        capacity, "
1531
                 "        total_active_power_point_id, "
1532
                 "        active_power_a_point_id, "
1533
                 "        active_power_b_point_id, "
1534
                 "        active_power_c_point_id, "
1535
                 "        total_reactive_power_point_id, "
1536
                 "        reactive_power_a_point_id, "
1537
                 "        reactive_power_b_point_id, "
1538
                 "        reactive_power_c_point_id, "
1539
                 "        total_apparent_power_point_id, "
1540
                 "        apparent_power_a_point_id, "
1541
                 "        apparent_power_b_point_id, "
1542
                 "        apparent_power_c_point_id, "
1543
                 "        total_power_factor_point_id, "
1544
                 "        active_energy_import_point_id, "
1545
                 "        active_energy_export_point_id, "
1546
                 "        active_energy_net_point_id "
1547
                 " FROM tbl_photovoltaic_power_stations_grids "
1548
                 " WHERE id = %s ")
1549
        cursor.execute(query, (gid,))
1550
        row = cursor.fetchone()
1551
        cursor.close()
1552
        cnx.close()
1553
1554
        if row is None:
1555
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1556
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NOT_FOUND')
1557
        else:
1558
            meta_result = {"id": row[0],
1559
                           "name": row[1],
1560
                           "uuid": row[2],
1561
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
1562
                           "power_point": point_dict.get(row[4]),
1563
                           "buy_meter": meter_dict.get(row[5]),
1564
                           "sell_meter": meter_dict.get(row[6]),
1565
                           "capacity": row[7],
1566
                           "total_active_power_point": point_dict.get(row[8]),
1567
                           "active_power_a_point": point_dict.get(row[9]),
1568
                           "active_power_b_point": point_dict.get(row[10]),
1569
                           "active_power_c_point": point_dict.get(row[11]),
1570
                           "total_reactive_power_point": point_dict.get(row[12]),
1571
                           "reactive_power_a_point": point_dict.get(row[13]),
1572
                           "reactive_power_b_point": point_dict.get(row[14]),
1573
                           "reactive_power_c_point": point_dict.get(row[15]),
1574
                           "total_apparent_power_point": point_dict.get(row[16]),
1575
                           "apparent_power_a_point": point_dict.get(row[17]),
1576
                           "apparent_power_b_point": point_dict.get(row[18]),
1577
                           "apparent_power_c_point": point_dict.get(row[19]),
1578
                           "total_power_factor_point": point_dict.get(row[20]),
1579
                           "active_energy_import_point": point_dict.get(row[21]),
1580
                           "active_energy_export_point": point_dict.get(row[22]),
1581
                           "active_energy_net_point_id": point_dict.get(row[23]),
1582
                           }
1583
1584
        resp.text = json.dumps(meta_result)
1585
1586
    @staticmethod
1587
    @user_logger
@@ 4521-4633 (lines=113) @@
4518
        resp.status = falcon.HTTP_200
4519
        _ = id_
4520
4521
    @staticmethod
4522
    def on_get(req, resp, id_, lid):
4523
        access_control(req)
4524
        if not id_.isdigit() or int(id_) <= 0:
4525
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4526
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4527
        if not lid.isdigit() or int(lid) <= 0:
4528
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4529
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_ID')
4530
4531
        cnx = mysql.connector.connect(**config.myems_system_db)
4532
        cursor = cnx.cursor()
4533
4534
        cursor.execute(" SELECT name "
4535
                       " FROM tbl_photovoltaic_power_stations "
4536
                       " WHERE id = %s ", (id_,))
4537
        if cursor.fetchone() is None:
4538
            cursor.close()
4539
            cnx.close()
4540
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4541
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4542
4543
        query = (" SELECT id, name, uuid "
4544
                 " FROM tbl_photovoltaic_power_stations ")
4545
        cursor.execute(query)
4546
        rows_photovoltaic_power_stations = cursor.fetchall()
4547
4548
        photovoltaic_power_station_dict = dict()
4549
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
4550
            for row in rows_photovoltaic_power_stations:
4551
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
4552
                                                           "name": row[1],
4553
                                                           "uuid": row[2]}
4554
        # query meter dict
4555
        query = (" SELECT id, name, uuid "
4556
                 " FROM tbl_meters ")
4557
        cursor.execute(query)
4558
        rows_meters = cursor.fetchall()
4559
4560
        meter_dict = dict()
4561
        if rows_meters is not None and len(rows_meters) > 0:
4562
            for row in rows_meters:
4563
                meter_dict[row[0]] = {"id": row[0],
4564
                                      "name": row[1],
4565
                                      "uuid": row[2]}
4566
        # query point dict
4567
        query = (" SELECT id, name "
4568
                 " FROM tbl_points ")
4569
        cursor.execute(query)
4570
        rows_points = cursor.fetchall()
4571
4572
        point_dict = dict()
4573
        if rows_points is not None and len(rows_points) > 0:
4574
            for row in rows_points:
4575
                point_dict[row[0]] = {"id": row[0],
4576
                                      "name": row[1]}
4577
4578
        query = (" SELECT id, name, uuid, "
4579
                 "        photovoltaic_power_station_id, "
4580
                 "        power_point_id, meter_id, "
4581
                 "        rated_input_power, "
4582
                 "        total_active_power_point_id, "
4583
                 "        active_power_a_point_id, "
4584
                 "        active_power_b_point_id, "
4585
                 "        active_power_c_point_id, "
4586
                 "        total_reactive_power_point_id, "
4587
                 "        reactive_power_a_point_id, "
4588
                 "        reactive_power_b_point_id, "
4589
                 "        reactive_power_c_point_id, "
4590
                 "        total_apparent_power_point_id, "
4591
                 "        apparent_power_a_point_id, "
4592
                 "        apparent_power_b_point_id, "
4593
                 "        apparent_power_c_point_id, "
4594
                 "        total_power_factor_point_id, "
4595
                 "        active_energy_import_point_id, "
4596
                 "        active_energy_export_point_id, "
4597
                 "        active_energy_net_point_id "
4598
                 " FROM tbl_photovoltaic_power_stations_loads "
4599
                 " WHERE id = %s ")
4600
        cursor.execute(query, (lid,))
4601
        row = cursor.fetchone()
4602
        cursor.close()
4603
        cnx.close()
4604
4605
        if row is None:
4606
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4607
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NOT_FOUND')
4608
        else:
4609
            meta_result = {"id": row[0],
4610
                           "name": row[1],
4611
                           "uuid": row[2],
4612
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
4613
                           "power_point": point_dict.get(row[4]),
4614
                           "meter": meter_dict.get(row[5]),
4615
                           "rated_input_power": row[6],
4616
                           "total_active_power_point": point_dict.get(row[7]),
4617
                           "active_power_a_point": point_dict.get(row[8]),
4618
                           "active_power_b_point": point_dict.get(row[9]),
4619
                           "active_power_c_point": point_dict.get(row[10]),
4620
                           "total_reactive_power_point": point_dict.get(row[11]),
4621
                           "reactive_power_a_point": point_dict.get(row[12]),
4622
                           "reactive_power_b_point": point_dict.get(row[13]),
4623
                           "reactive_power_c_point": point_dict.get(row[14]),
4624
                           "total_apparent_power_point": point_dict.get(row[15]),
4625
                           "apparent_power_a_point": point_dict.get(row[16]),
4626
                           "apparent_power_b_point": point_dict.get(row[17]),
4627
                           "apparent_power_c_point": point_dict.get(row[18]),
4628
                           "total_power_factor_point": point_dict.get(row[19]),
4629
                           "active_energy_import_point": point_dict.get(row[20]),
4630
                           "active_energy_export_point": point_dict.get(row[21]),
4631
                           "active_energy_net_point": point_dict.get(row[22])}
4632
4633
        resp.text = json.dumps(meta_result)
4634
4635
    @staticmethod
4636
    @user_logger