Code Duplication    Length = 69-70 lines in 2 locations

myems-api/core/microgrid.py 1 location

@@ 671-739 (lines=69) @@
668
        resp.status = falcon.HTTP_200
669
        _ = id_
670
671
    @staticmethod
672
    def on_get(req, resp, id_):
673
        access_control(req)
674
        if not id_.isdigit() or int(id_) <= 0:
675
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
676
                                   description='API.INVALID_MICROGRID_ID')
677
678
        cnx = mysql.connector.connect(**config.myems_system_db)
679
        cursor = cnx.cursor()
680
681
        cursor.execute(" SELECT name "
682
                       " FROM tbl_microgrids "
683
                       " WHERE id = %s ", (id_,))
684
        if cursor.fetchone() is None:
685
            cursor.close()
686
            cnx.close()
687
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
688
                                   description='API.MICROGRID_NOT_FOUND')
689
690
        # query meter dict
691
        query = (" SELECT id, name, uuid "
692
                 " FROM tbl_meters ")
693
        cursor.execute(query)
694
        rows_meters = cursor.fetchall()
695
696
        meter_dict = dict()
697
        if rows_meters is not None and len(rows_meters) > 0:
698
            for row in rows_meters:
699
                meter_dict[row[0]] = {"id": row[0],
700
                                      "name": row[1],
701
                                      "uuid": row[2]}
702
        # query point dict
703
        query = (" SELECT id, name "
704
                 " FROM tbl_points ")
705
        cursor.execute(query)
706
        rows_points = cursor.fetchall()
707
708
        point_dict = dict()
709
        if rows_points is not None and len(rows_points) > 0:
710
            for row in rows_points:
711
                point_dict[row[0]] = {"id": row[0],
712
                                      "name": row[1]}
713
714
        query = (" SELECT id, name, uuid, "
715
                 "        battery_state_point_id, soc_point_id, power_point_id, "
716
                 "        charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage "
717
                 " FROM tbl_microgrids_batteries "
718
                 " WHERE microgrid_id = %s "
719
                 " ORDER BY name ")
720
        cursor.execute(query, (id_,))
721
        rows = cursor.fetchall()
722
723
        result = list()
724
        if rows is not None and len(rows) > 0:
725
            for row in rows:
726
                meta_result = {"id": row[0],
727
                               "name": row[1],
728
                               "uuid": row[2],
729
                               "battery_state_point": point_dict.get(row[3]),
730
                               "soc_point": point_dict.get(row[4]),
731
                               "power_point": point_dict.get(row[5]),
732
                               "charge_meter": meter_dict.get(row[6]),
733
                               "discharge_meter": meter_dict.get(row[7]),
734
                               "rated_capacity": row[8],
735
                               "rated_power": row[9],
736
                               "nominal_voltage": row[10]}
737
                result.append(meta_result)
738
739
        resp.text = json.dumps(result)
740
741
    @staticmethod
742
    @user_logger

myems-api/core/energystoragecontainer.py 1 location

@@ 500-569 (lines=70) @@
497
        resp.status = falcon.HTTP_200
498
        _ = id_
499
500
    @staticmethod
501
    def on_get(req, resp, id_):
502
        access_control(req)
503
        if not id_.isdigit() or int(id_) <= 0:
504
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
505
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
506
507
        cnx = mysql.connector.connect(**config.myems_system_db)
508
        cursor = cnx.cursor()
509
510
        cursor.execute(" SELECT name "
511
                       " FROM tbl_energy_storage_containers "
512
                       " WHERE id = %s ", (id_,))
513
        if cursor.fetchone() is None:
514
            cursor.close()
515
            cnx.close()
516
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
517
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
518
519
        # query meter dict
520
        query = (" SELECT id, name, uuid "
521
                 " FROM tbl_meters ")
522
        cursor.execute(query)
523
        rows_meters = cursor.fetchall()
524
525
        meter_dict = dict()
526
        if rows_meters is not None and len(rows_meters) > 0:
527
            for row in rows_meters:
528
                meter_dict[row[0]] = {"id": row[0],
529
                                      "name": row[1],
530
                                      "uuid": row[2]}
531
        # query point dict
532
        query = (" SELECT id, name "
533
                 " FROM tbl_points ")
534
        cursor.execute(query)
535
        rows_points = cursor.fetchall()
536
537
        point_dict = dict()
538
        if rows_points is not None and len(rows_points) > 0:
539
            for row in rows_points:
540
                point_dict[row[0]] = {"id": row[0],
541
                                      "name": row[1]}
542
543
        query = (" SELECT id, name, uuid, "
544
                 "        battery_state_point_id, soc_point_id, power_point_id, "
545
                 "        charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage "
546
                 " FROM tbl_energy_storage_containers_batteries "
547
                 " WHERE energy_storage_container_id = %s "
548
                 " ORDER BY name ")
549
        cursor.execute(query, (id_,))
550
        rows = cursor.fetchall()
551
552
        result = list()
553
        if rows is not None and len(rows) > 0:
554
            for row in rows:
555
                meta_result = {"id": row[0],
556
                               "name": row[1],
557
                               "uuid": row[2],
558
                               "battery_state_point": point_dict.get(row[3]),
559
                               "soc_point": point_dict.get(row[4]),
560
                               "power_point": point_dict.get(row[5]),
561
                               "charge_meter": meter_dict.get(row[6]),
562
                               "discharge_meter": meter_dict.get(row[7]),
563
                               "rated_capacity": row[8],
564
                               "rated_power": row[9],
565
                               "nominal_voltage": row[10]
566
                               }
567
                result.append(meta_result)
568
569
        resp.text = json.dumps(result)
570
571
    @staticmethod
572
    @user_logger