Code Duplication    Length = 69-70 lines in 2 locations

myems-api/core/energystoragecontainer.py 1 location

@@ 443-512 (lines=70) @@
440
    def on_options(req, resp, id_):
441
        resp.status = falcon.HTTP_200
442
443
    @staticmethod
444
    def on_get(req, resp, id_):
445
        access_control(req)
446
        if not id_.isdigit() or int(id_) <= 0:
447
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
448
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
449
450
        cnx = mysql.connector.connect(**config.myems_system_db)
451
        cursor = cnx.cursor()
452
453
        cursor.execute(" SELECT name "
454
                       " FROM tbl_energy_storage_containers "
455
                       " WHERE id = %s ", (id_,))
456
        if cursor.fetchone() is None:
457
            cursor.close()
458
            cnx.close()
459
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
460
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
461
462
        # query meter dict
463
        query = (" SELECT id, name, uuid "
464
                 " FROM tbl_meters ")
465
        cursor.execute(query)
466
        rows_meters = cursor.fetchall()
467
468
        meter_dict = dict()
469
        if rows_meters is not None and len(rows_meters) > 0:
470
            for row in rows_meters:
471
                meter_dict[row[0]] = {"id": row[0],
472
                                      "name": row[1],
473
                                      "uuid": row[2]}
474
        # query point dict
475
        query = (" SELECT id, name "
476
                 " FROM tbl_points ")
477
        cursor.execute(query)
478
        rows_points = cursor.fetchall()
479
480
        point_dict = dict()
481
        if rows_points is not None and len(rows_points) > 0:
482
            for row in rows_points:
483
                point_dict[row[0]] = {"id": row[0],
484
                                      "name": row[1]}
485
486
        query = (" SELECT id, name, uuid, "
487
                 "        battery_state_point_id, soc_point_id, power_point_id, "
488
                 "        charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage "
489
                 " FROM tbl_energy_storage_containers_batteries "
490
                 " WHERE energy_storage_container_id = %s "
491
                 " ORDER BY name ")
492
        cursor.execute(query, (id_,))
493
        rows = cursor.fetchall()
494
495
        result = list()
496
        if rows is not None and len(rows) > 0:
497
            for row in rows:
498
                meta_result = {"id": row[0],
499
                               "name": row[1],
500
                               "uuid": row[2],
501
                               "battery_state_point": point_dict.get(row[3]),
502
                               "soc_point": point_dict.get(row[4]),
503
                               "power_point": point_dict.get(row[5]),
504
                               "charge_meter": meter_dict.get(row[6]),
505
                               "discharge_meter": meter_dict.get(row[7]),
506
                               "rated_capacity": row[8],
507
                               "rated_power": row[9],
508
                               "nominal_voltage": row[10]
509
                               }
510
                result.append(meta_result)
511
512
        resp.text = json.dumps(result)
513
514
    @staticmethod
515
    @user_logger

myems-api/core/microgrid.py 1 location

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