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

@@ 4718-4821 (lines=104) @@
4715
        resp.status = falcon.HTTP_204
4716
4717
4718
class EnergyStorageContainerScheduleCollection:
4719
    def __init__(self):
4720
        """Initializes Class"""
4721
        pass
4722
4723
    @staticmethod
4724
    def on_options(req, resp, id_):
4725
        resp.status = falcon.HTTP_200
4726
4727
    @staticmethod
4728
    def on_get(req, resp, id_):
4729
        access_control(req)
4730
        if not id_.isdigit() or int(id_) <= 0:
4731
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4732
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4733
4734
        cnx = mysql.connector.connect(**config.myems_system_db)
4735
        cursor = cnx.cursor()
4736
4737
        cursor.execute(" SELECT name "
4738
                       " FROM tbl_energy_storage_containers "
4739
                       " WHERE id = %s ", (id_,))
4740
        if cursor.fetchone() is None:
4741
            cursor.close()
4742
            cnx.close()
4743
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4744
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
4745
4746
        query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power "
4747
                 " FROM tbl_energy_storage_containers_schedules "
4748
                 " WHERE energy_storage_container_id = %s "
4749
                 " ORDER BY start_time_of_day ")
4750
        cursor.execute(query, (id_,))
4751
        rows = cursor.fetchall()
4752
4753
        result = list()
4754
        if rows is not None and len(rows) > 0:
4755
            for row in rows:
4756
                meta_result = {"id": row[0],
4757
                               "start_time_of_day": str(row[1]),
4758
                               "end_time_of_day": str(row[2]),
4759
                               "peak_type": row[3],
4760
                               "power": row[4],
4761
                               }
4762
                result.append(meta_result)
4763
4764
        resp.text = json.dumps(result)
4765
4766
    @staticmethod
4767
    @user_logger
4768
    def on_post(req, resp, id_):
4769
        """Handles POST requests"""
4770
        admin_control(req)
4771
        try:
4772
            raw_json = req.stream.read().decode('utf-8')
4773
        except Exception as ex:
4774
            raise falcon.HTTPError(status=falcon.HTTP_400,
4775
                                   title='API.BAD_REQUEST',
4776
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4777
        if not id_.isdigit() or int(id_) <= 0:
4778
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4779
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4780
4781
        cnx = mysql.connector.connect(**config.myems_system_db)
4782
        cursor = cnx.cursor()
4783
4784
        cursor.execute(" SELECT name "
4785
                       " FROM tbl_energy_storage_containers "
4786
                       " WHERE id = %s ", (id_,))
4787
        if cursor.fetchone() is None:
4788
            cursor.close()
4789
            cnx.close()
4790
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4791
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
4792
4793
        new_values = json.loads(raw_json)
4794
4795
        cnx = mysql.connector.connect(**config.myems_system_db)
4796
        cursor = cnx.cursor()
4797
4798
        cursor.execute(" SELECT name "
4799
                       " FROM tbl_energy_storage_containers "
4800
                       " WHERE id = %s ",
4801
                       (id_,))
4802
        if cursor.fetchone() is None:
4803
            cursor.close()
4804
            cnx.close()
4805
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4806
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
4807
4808
        add_schedule = (" INSERT INTO tbl_energy_storage_containers_schedules "
4809
                        "     (energy_storage_container_id, start_time_of_day, end_time_of_day, peak_type, power) "
4810
                        " VALUES (%s, %s, %s, %s, %s) ")
4811
        cursor.execute(add_schedule, (id_,
4812
                                      new_values['data']['start_time_of_day'],
4813
                                      new_values['data']['end_time_of_day'],
4814
                                      new_values['data']['peak_type'],
4815
                                      new_values['data']['power']))
4816
        new_id = cursor.lastrowid
4817
        cnx.commit()
4818
        cursor.close()
4819
        cnx.close()
4820
        resp.status = falcon.HTTP_201
4821
        resp.location = '/energystoragecontainerschedules/' + str(new_id)
4822
4823
4824
class EnergyStorageContainerScheduleItem: