| @@ 4593-4779 (lines=187) @@ | ||
| 4590 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
|
| 4591 | ||
| 4592 | ||
| 4593 | class MicrogridScheduleItem: |
|
| 4594 | def __init__(self): |
|
| 4595 | """Initializes Class""" |
|
| 4596 | pass |
|
| 4597 | ||
| 4598 | @staticmethod |
|
| 4599 | def on_options(req, resp, id_, sid): |
|
| 4600 | resp.status = falcon.HTTP_200 |
|
| 4601 | ||
| 4602 | @staticmethod |
|
| 4603 | def on_get(req, resp, id_, sid): |
|
| 4604 | access_control(req) |
|
| 4605 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4606 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4607 | description='API.INVALID_MICROGRID_ID') |
|
| 4608 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4609 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4610 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4611 | ||
| 4612 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4613 | cursor = cnx.cursor() |
|
| 4614 | ||
| 4615 | cursor.execute(" SELECT name " |
|
| 4616 | " FROM tbl_microgrids " |
|
| 4617 | " WHERE id = %s ", (id_,)) |
|
| 4618 | if cursor.fetchone() is None: |
|
| 4619 | cursor.close() |
|
| 4620 | cnx.close() |
|
| 4621 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4622 | description='API.MICROGRID_NOT_FOUND') |
|
| 4623 | ||
| 4624 | query = (" SELECT id, name, uuid " |
|
| 4625 | " FROM tbl_microgrids ") |
|
| 4626 | cursor.execute(query) |
|
| 4627 | rows_energystoragecontainers = cursor.fetchall() |
|
| 4628 | ||
| 4629 | microgrid_dict = dict() |
|
| 4630 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 4631 | for row in rows_energystoragecontainers: |
|
| 4632 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 4633 | "name": row[1], |
|
| 4634 | "uuid": row[2]} |
|
| 4635 | ||
| 4636 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
|
| 4637 | " FROM tbl_microgrids_schedules " |
|
| 4638 | " WHERE id = %s ") |
|
| 4639 | cursor.execute(query, (sid,)) |
|
| 4640 | row = cursor.fetchone() |
|
| 4641 | cursor.close() |
|
| 4642 | cnx.close() |
|
| 4643 | ||
| 4644 | if row is None: |
|
| 4645 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4646 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4647 | else: |
|
| 4648 | meta_result = {"id": row[0], |
|
| 4649 | "start_time_of_day": str(row[1]), |
|
| 4650 | "end_time_of_day": str(row[2]), |
|
| 4651 | "peak_type": row[3], |
|
| 4652 | "power": row[4]} |
|
| 4653 | ||
| 4654 | resp.text = json.dumps(meta_result) |
|
| 4655 | ||
| 4656 | @staticmethod |
|
| 4657 | @user_logger |
|
| 4658 | def on_delete(req, resp, id_, sid): |
|
| 4659 | admin_control(req) |
|
| 4660 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4661 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4662 | description='API.INVALID_MICROGRID_ID') |
|
| 4663 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4664 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4665 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4666 | ||
| 4667 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4668 | cursor = cnx.cursor() |
|
| 4669 | ||
| 4670 | cursor.execute(" SELECT name " |
|
| 4671 | " FROM tbl_microgrids " |
|
| 4672 | " WHERE id = %s ", (id_,)) |
|
| 4673 | if cursor.fetchone() is None: |
|
| 4674 | cursor.close() |
|
| 4675 | cnx.close() |
|
| 4676 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4677 | description='API.MICROGRID_NOT_FOUND') |
|
| 4678 | ||
| 4679 | cursor.execute(" SELECT id " |
|
| 4680 | " FROM tbl_microgrids_schedules " |
|
| 4681 | " WHERE id = %s ", (sid,)) |
|
| 4682 | if cursor.fetchone() is None: |
|
| 4683 | cursor.close() |
|
| 4684 | cnx.close() |
|
| 4685 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4686 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4687 | ||
| 4688 | cursor.execute(" DELETE FROM tbl_microgrids_schedules " |
|
| 4689 | " WHERE id = %s ", (sid,)) |
|
| 4690 | cnx.commit() |
|
| 4691 | ||
| 4692 | cursor.close() |
|
| 4693 | cnx.close() |
|
| 4694 | ||
| 4695 | resp.status = falcon.HTTP_204 |
|
| 4696 | ||
| 4697 | @staticmethod |
|
| 4698 | @user_logger |
|
| 4699 | def on_put(req, resp, id_, sid): |
|
| 4700 | """Handles PUT requests""" |
|
| 4701 | admin_control(req) |
|
| 4702 | try: |
|
| 4703 | raw_json = req.stream.read().decode('utf-8') |
|
| 4704 | except Exception as ex: |
|
| 4705 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4706 | title='API.BAD_REQUEST', |
|
| 4707 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4708 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4709 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4710 | description='API.INVALID_MICROGRID_ID') |
|
| 4711 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4712 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4713 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4714 | ||
| 4715 | new_values = json.loads(raw_json) |
|
| 4716 | ||
| 4717 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
|
| 4718 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
|
| 4719 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
|
| 4720 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4721 | description='API.INVALID_START_TIME_OF_DAY') |
|
| 4722 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
|
| 4723 | ||
| 4724 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
|
| 4725 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
|
| 4726 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
|
| 4727 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4728 | description='API.INVALID_END_TIME_OF_DAY') |
|
| 4729 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
|
| 4730 | ||
| 4731 | if 'peak_type' not in new_values['data'].keys() or \ |
|
| 4732 | not isinstance(new_values['data']['peak_type'], str) or \ |
|
| 4733 | len(str.strip(new_values['data']['peak_type'])) == 0: |
|
| 4734 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4735 | description='API.INVALID_PEAK_TYPE') |
|
| 4736 | peak_type = str.strip(new_values['data']['peak_type']) |
|
| 4737 | ||
| 4738 | if 'power' not in new_values['data'].keys() or \ |
|
| 4739 | not (isinstance(new_values['data']['power'], float) or |
|
| 4740 | isinstance(new_values['data']['power'], int)): |
|
| 4741 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4742 | description='API.INVALID_POWER') |
|
| 4743 | power = float(new_values['data']['power']) |
|
| 4744 | ||
| 4745 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4746 | cursor = cnx.cursor() |
|
| 4747 | ||
| 4748 | cursor.execute(" SELECT name " |
|
| 4749 | " FROM tbl_microgrids " |
|
| 4750 | " WHERE id = %s ", (id_,)) |
|
| 4751 | if cursor.fetchone() is None: |
|
| 4752 | cursor.close() |
|
| 4753 | cnx.close() |
|
| 4754 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4755 | description='API.MICROGRID_NOT_FOUND') |
|
| 4756 | ||
| 4757 | cursor.execute(" SELECT id " |
|
| 4758 | " FROM tbl_microgrids_schedules " |
|
| 4759 | " WHERE id = %s ", (sid,)) |
|
| 4760 | if cursor.fetchone() is None: |
|
| 4761 | cursor.close() |
|
| 4762 | cnx.close() |
|
| 4763 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4764 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4765 | ||
| 4766 | update_row = (" UPDATE tbl_microgrids_schedules " |
|
| 4767 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
|
| 4768 | " WHERE id = %s ") |
|
| 4769 | cursor.execute(update_row, (start_time_of_day, |
|
| 4770 | end_time_of_day, |
|
| 4771 | peak_type, |
|
| 4772 | power, |
|
| 4773 | sid)) |
|
| 4774 | cnx.commit() |
|
| 4775 | ||
| 4776 | cursor.close() |
|
| 4777 | cnx.close() |
|
| 4778 | ||
| 4779 | resp.status = falcon.HTTP_200 |
|
| 4780 | ||
| 4781 | ||
| 4782 | class MicrogridSensorCollection: |
|
| @@ 3971-4157 (lines=187) @@ | ||
| 3968 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
|
| 3969 | ||
| 3970 | ||
| 3971 | class EnergyStorageContainerScheduleItem: |
|
| 3972 | def __init__(self): |
|
| 3973 | """Initializes Class""" |
|
| 3974 | pass |
|
| 3975 | ||
| 3976 | @staticmethod |
|
| 3977 | def on_options(req, resp, id_, sid): |
|
| 3978 | resp.status = falcon.HTTP_200 |
|
| 3979 | ||
| 3980 | @staticmethod |
|
| 3981 | def on_get(req, resp, id_, sid): |
|
| 3982 | access_control(req) |
|
| 3983 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3985 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3986 | if not sid.isdigit() or int(sid) <= 0: |
|
| 3987 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3988 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 3989 | ||
| 3990 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3991 | cursor = cnx.cursor() |
|
| 3992 | ||
| 3993 | cursor.execute(" SELECT name " |
|
| 3994 | " FROM tbl_energy_storage_containers " |
|
| 3995 | " WHERE id = %s ", (id_,)) |
|
| 3996 | if cursor.fetchone() is None: |
|
| 3997 | cursor.close() |
|
| 3998 | cnx.close() |
|
| 3999 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4000 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4001 | ||
| 4002 | query = (" SELECT id, name, uuid " |
|
| 4003 | " FROM tbl_energy_storage_containers ") |
|
| 4004 | cursor.execute(query) |
|
| 4005 | rows_energystoragecontainers = cursor.fetchall() |
|
| 4006 | ||
| 4007 | energy_storage_container_dict = dict() |
|
| 4008 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 4009 | for row in rows_energystoragecontainers: |
|
| 4010 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 4011 | "name": row[1], |
|
| 4012 | "uuid": row[2]} |
|
| 4013 | ||
| 4014 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
|
| 4015 | " FROM tbl_energy_storage_containers_schedules " |
|
| 4016 | " WHERE id = %s ") |
|
| 4017 | cursor.execute(query, (sid,)) |
|
| 4018 | row = cursor.fetchone() |
|
| 4019 | cursor.close() |
|
| 4020 | cnx.close() |
|
| 4021 | ||
| 4022 | if row is None: |
|
| 4023 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4024 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 4025 | else: |
|
| 4026 | meta_result = {"id": row[0], |
|
| 4027 | "start_time_of_day": str(row[1]), |
|
| 4028 | "end_time_of_day": str(row[2]), |
|
| 4029 | "peak_type": row[3], |
|
| 4030 | "power": row[4]} |
|
| 4031 | ||
| 4032 | resp.text = json.dumps(meta_result) |
|
| 4033 | ||
| 4034 | @staticmethod |
|
| 4035 | @user_logger |
|
| 4036 | def on_delete(req, resp, id_, sid): |
|
| 4037 | admin_control(req) |
|
| 4038 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4039 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4040 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4041 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4042 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4043 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 4044 | ||
| 4045 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4046 | cursor = cnx.cursor() |
|
| 4047 | ||
| 4048 | cursor.execute(" SELECT name " |
|
| 4049 | " FROM tbl_energy_storage_containers " |
|
| 4050 | " WHERE id = %s ", (id_,)) |
|
| 4051 | if cursor.fetchone() is None: |
|
| 4052 | cursor.close() |
|
| 4053 | cnx.close() |
|
| 4054 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4055 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4056 | ||
| 4057 | cursor.execute(" SELECT id " |
|
| 4058 | " FROM tbl_energy_storage_containers_schedules " |
|
| 4059 | " WHERE id = %s ", (sid,)) |
|
| 4060 | if cursor.fetchone() is None: |
|
| 4061 | cursor.close() |
|
| 4062 | cnx.close() |
|
| 4063 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4064 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 4065 | ||
| 4066 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
|
| 4067 | " WHERE id = %s ", (sid,)) |
|
| 4068 | cnx.commit() |
|
| 4069 | ||
| 4070 | cursor.close() |
|
| 4071 | cnx.close() |
|
| 4072 | ||
| 4073 | resp.status = falcon.HTTP_204 |
|
| 4074 | ||
| 4075 | @staticmethod |
|
| 4076 | @user_logger |
|
| 4077 | def on_put(req, resp, id_, sid): |
|
| 4078 | """Handles PUT requests""" |
|
| 4079 | admin_control(req) |
|
| 4080 | try: |
|
| 4081 | raw_json = req.stream.read().decode('utf-8') |
|
| 4082 | except Exception as ex: |
|
| 4083 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4084 | title='API.BAD_REQUEST', |
|
| 4085 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4086 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4087 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4088 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4089 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4090 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4091 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 4092 | ||
| 4093 | new_values = json.loads(raw_json) |
|
| 4094 | ||
| 4095 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
|
| 4096 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
|
| 4097 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
|
| 4098 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4099 | description='API.INVALID_START_TIME_OF_DAY') |
|
| 4100 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
|
| 4101 | ||
| 4102 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
|
| 4103 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
|
| 4104 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
|
| 4105 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4106 | description='API.INVALID_END_TIME_OF_DAY') |
|
| 4107 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
|
| 4108 | ||
| 4109 | if 'peak_type' not in new_values['data'].keys() or \ |
|
| 4110 | not isinstance(new_values['data']['peak_type'], str) or \ |
|
| 4111 | len(str.strip(new_values['data']['peak_type'])) == 0: |
|
| 4112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4113 | description='API.INVALID_PEAK_TYPE') |
|
| 4114 | peak_type = str.strip(new_values['data']['peak_type']) |
|
| 4115 | ||
| 4116 | if 'power' not in new_values['data'].keys() or \ |
|
| 4117 | not (isinstance(new_values['data']['power'], float) or |
|
| 4118 | isinstance(new_values['data']['power'], int)): |
|
| 4119 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4120 | description='API.INVALID_POWER') |
|
| 4121 | power = Decimal(new_values['data']['power']) |
|
| 4122 | ||
| 4123 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4124 | cursor = cnx.cursor() |
|
| 4125 | ||
| 4126 | cursor.execute(" SELECT name " |
|
| 4127 | " FROM tbl_energy_storage_containers " |
|
| 4128 | " WHERE id = %s ", (id_,)) |
|
| 4129 | if cursor.fetchone() is None: |
|
| 4130 | cursor.close() |
|
| 4131 | cnx.close() |
|
| 4132 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4133 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4134 | ||
| 4135 | cursor.execute(" SELECT id " |
|
| 4136 | " FROM tbl_energy_storage_containers_schedules " |
|
| 4137 | " WHERE id = %s ", (sid,)) |
|
| 4138 | if cursor.fetchone() is None: |
|
| 4139 | cursor.close() |
|
| 4140 | cnx.close() |
|
| 4141 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4142 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 4143 | ||
| 4144 | update_row = (" UPDATE tbl_energy_storage_containers_schedules " |
|
| 4145 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
|
| 4146 | " WHERE id = %s ") |
|
| 4147 | cursor.execute(update_row, (start_time_of_day, |
|
| 4148 | end_time_of_day, |
|
| 4149 | peak_type, |
|
| 4150 | power, |
|
| 4151 | sid)) |
|
| 4152 | cnx.commit() |
|
| 4153 | ||
| 4154 | cursor.close() |
|
| 4155 | cnx.close() |
|
| 4156 | ||
| 4157 | resp.status = falcon.HTTP_200 |
|
| 4158 | ||
| 4159 | ||
| 4160 | class EnergyStorageContainerClone: |
|