Code Duplication    Length = 69-70 lines in 2 locations

myems-api/core/energystoragecontainer.py 1 location

@@ 553-622 (lines=70) @@
550
        resp.status = falcon.HTTP_200
551
        _ = id_
552
553
    @staticmethod
554
    def on_get(req, resp, id_):
555
        access_control(req)
556
        if not id_.isdigit() or int(id_) <= 0:
557
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
558
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
559
560
        cnx = mysql.connector.connect(**config.myems_system_db)
561
        cursor = cnx.cursor()
562
563
        cursor.execute(" SELECT name "
564
                       " FROM tbl_energy_storage_containers "
565
                       " WHERE id = %s ", (id_,))
566
        if cursor.fetchone() is None:
567
            cursor.close()
568
            cnx.close()
569
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
570
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
571
572
        # query meter dict
573
        query = (" SELECT id, name, uuid "
574
                 " FROM tbl_meters ")
575
        cursor.execute(query)
576
        rows_meters = cursor.fetchall()
577
578
        meter_dict = dict()
579
        if rows_meters is not None and len(rows_meters) > 0:
580
            for row in rows_meters:
581
                meter_dict[row[0]] = {"id": row[0],
582
                                      "name": row[1],
583
                                      "uuid": row[2]}
584
        # query point dict
585
        query = (" SELECT id, name "
586
                 " FROM tbl_points ")
587
        cursor.execute(query)
588
        rows_points = cursor.fetchall()
589
590
        point_dict = dict()
591
        if rows_points is not None and len(rows_points) > 0:
592
            for row in rows_points:
593
                point_dict[row[0]] = {"id": row[0],
594
                                      "name": row[1]}
595
596
        query = (" SELECT id, name, uuid, "
597
                 "        battery_state_point_id, soc_point_id, power_point_id, "
598
                 "        charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage "
599
                 " FROM tbl_energy_storage_containers_batteries "
600
                 " WHERE energy_storage_container_id = %s "
601
                 " ORDER BY name ")
602
        cursor.execute(query, (id_,))
603
        rows = cursor.fetchall()
604
605
        result = list()
606
        if rows is not None and len(rows) > 0:
607
            for row in rows:
608
                meta_result = {"id": row[0],
609
                               "name": row[1],
610
                               "uuid": row[2],
611
                               "battery_state_point": point_dict.get(row[3]),
612
                               "soc_point": point_dict.get(row[4]),
613
                               "power_point": point_dict.get(row[5]),
614
                               "charge_meter": meter_dict.get(row[6]),
615
                               "discharge_meter": meter_dict.get(row[7]),
616
                               "rated_capacity": row[8],
617
                               "rated_power": row[9],
618
                               "nominal_voltage": row[10]
619
                               }
620
                result.append(meta_result)
621
622
        resp.text = json.dumps(result)
623
624
    @staticmethod
625
    @user_logger

myems-api/core/microgrid.py 1 location

@@ 716-784 (lines=69) @@
713
        resp.status = falcon.HTTP_200
714
        _ = id_
715
716
    @staticmethod
717
    def on_get(req, resp, id_):
718
        access_control(req)
719
        if not id_.isdigit() or int(id_) <= 0:
720
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
721
                                   description='API.INVALID_MICROGRID_ID')
722
723
        cnx = mysql.connector.connect(**config.myems_system_db)
724
        cursor = cnx.cursor()
725
726
        cursor.execute(" SELECT name "
727
                       " FROM tbl_microgrids "
728
                       " WHERE id = %s ", (id_,))
729
        if cursor.fetchone() is None:
730
            cursor.close()
731
            cnx.close()
732
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
733
                                   description='API.MICROGRID_NOT_FOUND')
734
735
        # query meter dict
736
        query = (" SELECT id, name, uuid "
737
                 " FROM tbl_meters ")
738
        cursor.execute(query)
739
        rows_meters = cursor.fetchall()
740
741
        meter_dict = dict()
742
        if rows_meters is not None and len(rows_meters) > 0:
743
            for row in rows_meters:
744
                meter_dict[row[0]] = {"id": row[0],
745
                                      "name": row[1],
746
                                      "uuid": row[2]}
747
        # query point dict
748
        query = (" SELECT id, name "
749
                 " FROM tbl_points ")
750
        cursor.execute(query)
751
        rows_points = cursor.fetchall()
752
753
        point_dict = dict()
754
        if rows_points is not None and len(rows_points) > 0:
755
            for row in rows_points:
756
                point_dict[row[0]] = {"id": row[0],
757
                                      "name": row[1]}
758
759
        query = (" SELECT id, name, uuid, "
760
                 "        battery_state_point_id, soc_point_id, power_point_id, "
761
                 "        charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage "
762
                 " FROM tbl_microgrids_batteries "
763
                 " WHERE microgrid_id = %s "
764
                 " ORDER BY name ")
765
        cursor.execute(query, (id_,))
766
        rows = cursor.fetchall()
767
768
        result = list()
769
        if rows is not None and len(rows) > 0:
770
            for row in rows:
771
                meta_result = {"id": row[0],
772
                               "name": row[1],
773
                               "uuid": row[2],
774
                               "battery_state_point": point_dict.get(row[3]),
775
                               "soc_point": point_dict.get(row[4]),
776
                               "power_point": point_dict.get(row[5]),
777
                               "charge_meter": meter_dict.get(row[6]),
778
                               "discharge_meter": meter_dict.get(row[7]),
779
                               "rated_capacity": row[8],
780
                               "rated_power": row[9],
781
                               "nominal_voltage": row[10]}
782
                result.append(meta_result)
783
784
        resp.text = json.dumps(result)
785
786
    @staticmethod
787
    @user_logger