Code Duplication    Length = 104-104 lines in 2 locations

myems-api/core/microgrid.py 1 location

@@ 4487-4590 (lines=104) @@
4484
        resp.status = falcon.HTTP_200
4485
4486
4487
class MicrogridScheduleCollection:
4488
    def __init__(self):
4489
        """Initializes Class"""
4490
        pass
4491
4492
    @staticmethod
4493
    def on_options(req, resp, id_):
4494
        resp.status = falcon.HTTP_200
4495
4496
    @staticmethod
4497
    def on_get(req, resp, id_):
4498
        access_control(req)
4499
        if not id_.isdigit() or int(id_) <= 0:
4500
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4501
                                   description='API.INVALID_MICROGRID_ID')
4502
4503
        cnx = mysql.connector.connect(**config.myems_system_db)
4504
        cursor = cnx.cursor()
4505
4506
        cursor.execute(" SELECT name "
4507
                       " FROM tbl_microgrids "
4508
                       " WHERE id = %s ", (id_,))
4509
        if cursor.fetchone() is None:
4510
            cursor.close()
4511
            cnx.close()
4512
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4513
                                   description='API.MICROGRID_NOT_FOUND')
4514
4515
        query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power "
4516
                 " FROM tbl_microgrids_schedules "
4517
                 " WHERE microgrid_id = %s "
4518
                 " ORDER BY start_time_of_day ")
4519
        cursor.execute(query, (id_,))
4520
        rows = cursor.fetchall()
4521
4522
        result = list()
4523
        if rows is not None and len(rows) > 0:
4524
            for row in rows:
4525
                meta_result = {"id": row[0],
4526
                               "start_time_of_day": str(row[1]),
4527
                               "end_time_of_day": str(row[2]),
4528
                               "peak_type": row[3],
4529
                               "power": row[4],
4530
                               }
4531
                result.append(meta_result)
4532
4533
        resp.text = json.dumps(result)
4534
4535
    @staticmethod
4536
    @user_logger
4537
    def on_post(req, resp, id_):
4538
        """Handles POST requests"""
4539
        admin_control(req)
4540
        try:
4541
            raw_json = req.stream.read().decode('utf-8')
4542
        except Exception as ex:
4543
            raise falcon.HTTPError(status=falcon.HTTP_400,
4544
                                   title='API.BAD_REQUEST',
4545
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4546
        if not id_.isdigit() or int(id_) <= 0:
4547
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4548
                                   description='API.INVALID_MICROGRID_ID')
4549
4550
        cnx = mysql.connector.connect(**config.myems_system_db)
4551
        cursor = cnx.cursor()
4552
4553
        cursor.execute(" SELECT name "
4554
                       " FROM tbl_microgrids "
4555
                       " WHERE id = %s ", (id_,))
4556
        if cursor.fetchone() is None:
4557
            cursor.close()
4558
            cnx.close()
4559
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4560
                                   description='API.MICROGRID_NOT_FOUND')
4561
4562
        new_values = json.loads(raw_json)
4563
4564
        cnx = mysql.connector.connect(**config.myems_system_db)
4565
        cursor = cnx.cursor()
4566
4567
        cursor.execute(" SELECT name "
4568
                       " FROM tbl_microgrids "
4569
                       " WHERE id = %s ",
4570
                       (id_,))
4571
        if cursor.fetchone() is None:
4572
            cursor.close()
4573
            cnx.close()
4574
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4575
                                   description='API.MICROGRID_NOT_FOUND')
4576
4577
        add_schedule = (" INSERT INTO tbl_microgrids_schedules "
4578
                        "     (microgrid_id, start_time_of_day, end_time_of_day, peak_type, power) "
4579
                        " VALUES (%s, %s, %s, %s, %s) ")
4580
        cursor.execute(add_schedule, (id_,
4581
                                      new_values['data']['start_time_of_day'],
4582
                                      new_values['data']['end_time_of_day'],
4583
                                      new_values['data']['peak_type'],
4584
                                      new_values['data']['power']))
4585
        new_id = cursor.lastrowid
4586
        cnx.commit()
4587
        cursor.close()
4588
        cnx.close()
4589
        resp.status = falcon.HTTP_201
4590
        resp.location = '/energystoragecontainerschedules/' + str(new_id)
4591
4592
4593
class MicrogridScheduleItem:

myems-api/core/energystoragecontainer.py 1 location

@@ 4963-5066 (lines=104) @@
4960
        resp.status = falcon.HTTP_204
4961
4962
4963
class EnergyStorageContainerScheduleCollection:
4964
    def __init__(self):
4965
        """Initializes Class"""
4966
        pass
4967
4968
    @staticmethod
4969
    def on_options(req, resp, id_):
4970
        resp.status = falcon.HTTP_200
4971
4972
    @staticmethod
4973
    def on_get(req, resp, id_):
4974
        access_control(req)
4975
        if not id_.isdigit() or int(id_) <= 0:
4976
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4977
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4978
4979
        cnx = mysql.connector.connect(**config.myems_system_db)
4980
        cursor = cnx.cursor()
4981
4982
        cursor.execute(" SELECT name "
4983
                       " FROM tbl_energy_storage_containers "
4984
                       " WHERE id = %s ", (id_,))
4985
        if cursor.fetchone() is None:
4986
            cursor.close()
4987
            cnx.close()
4988
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4989
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
4990
4991
        query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power "
4992
                 " FROM tbl_energy_storage_containers_schedules "
4993
                 " WHERE energy_storage_container_id = %s "
4994
                 " ORDER BY start_time_of_day ")
4995
        cursor.execute(query, (id_,))
4996
        rows = cursor.fetchall()
4997
4998
        result = list()
4999
        if rows is not None and len(rows) > 0:
5000
            for row in rows:
5001
                meta_result = {"id": row[0],
5002
                               "start_time_of_day": str(row[1]),
5003
                               "end_time_of_day": str(row[2]),
5004
                               "peak_type": row[3],
5005
                               "power": row[4],
5006
                               }
5007
                result.append(meta_result)
5008
5009
        resp.text = json.dumps(result)
5010
5011
    @staticmethod
5012
    @user_logger
5013
    def on_post(req, resp, id_):
5014
        """Handles POST requests"""
5015
        admin_control(req)
5016
        try:
5017
            raw_json = req.stream.read().decode('utf-8')
5018
        except Exception as ex:
5019
            raise falcon.HTTPError(status=falcon.HTTP_400,
5020
                                   title='API.BAD_REQUEST',
5021
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5022
        if not id_.isdigit() or int(id_) <= 0:
5023
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5024
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5025
5026
        cnx = mysql.connector.connect(**config.myems_system_db)
5027
        cursor = cnx.cursor()
5028
5029
        cursor.execute(" SELECT name "
5030
                       " FROM tbl_energy_storage_containers "
5031
                       " WHERE id = %s ", (id_,))
5032
        if cursor.fetchone() is None:
5033
            cursor.close()
5034
            cnx.close()
5035
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5036
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5037
5038
        new_values = json.loads(raw_json)
5039
5040
        cnx = mysql.connector.connect(**config.myems_system_db)
5041
        cursor = cnx.cursor()
5042
5043
        cursor.execute(" SELECT name "
5044
                       " FROM tbl_energy_storage_containers "
5045
                       " WHERE id = %s ",
5046
                       (id_,))
5047
        if cursor.fetchone() is None:
5048
            cursor.close()
5049
            cnx.close()
5050
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5051
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5052
5053
        add_schedule = (" INSERT INTO tbl_energy_storage_containers_schedules "
5054
                        "     (energy_storage_container_id, start_time_of_day, end_time_of_day, peak_type, power) "
5055
                        " VALUES (%s, %s, %s, %s, %s) ")
5056
        cursor.execute(add_schedule, (id_,
5057
                                      new_values['data']['start_time_of_day'],
5058
                                      new_values['data']['end_time_of_day'],
5059
                                      new_values['data']['peak_type'],
5060
                                      new_values['data']['power']))
5061
        new_id = cursor.lastrowid
5062
        cnx.commit()
5063
        cursor.close()
5064
        cnx.close()
5065
        resp.status = falcon.HTTP_201
5066
        resp.location = '/energystoragecontainerschedules/' + str(new_id)
5067
5068
5069
class EnergyStorageContainerScheduleItem: