| @@ 4919-5108 (lines=190) @@ | ||
| 4916 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
|
| 4917 | ||
| 4918 | ||
| 4919 | class EnergyStorageContainerScheduleItem: |
|
| 4920 | def __init__(self): |
|
| 4921 | """Initializes Class""" |
|
| 4922 | pass |
|
| 4923 | ||
| 4924 | @staticmethod |
|
| 4925 | def on_options(req, resp, id_, sid): |
|
| 4926 | _ = req |
|
| 4927 | resp.status = falcon.HTTP_200 |
|
| 4928 | _ = id_ |
|
| 4929 | ||
| 4930 | @staticmethod |
|
| 4931 | def on_get(req, resp, id_, sid): |
|
| 4932 | access_control(req) |
|
| 4933 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4934 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4935 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4936 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4937 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4938 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 4939 | ||
| 4940 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4941 | cursor = cnx.cursor() |
|
| 4942 | ||
| 4943 | cursor.execute(" SELECT name " |
|
| 4944 | " FROM tbl_energy_storage_containers " |
|
| 4945 | " WHERE id = %s ", (id_,)) |
|
| 4946 | if cursor.fetchone() is None: |
|
| 4947 | cursor.close() |
|
| 4948 | cnx.close() |
|
| 4949 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4950 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4951 | ||
| 4952 | query = (" SELECT id, name, uuid " |
|
| 4953 | " FROM tbl_energy_storage_containers ") |
|
| 4954 | cursor.execute(query) |
|
| 4955 | rows_energystoragecontainers = cursor.fetchall() |
|
| 4956 | ||
| 4957 | energy_storage_container_dict = dict() |
|
| 4958 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 4959 | for row in rows_energystoragecontainers: |
|
| 4960 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 4961 | "name": row[1], |
|
| 4962 | "uuid": row[2]} |
|
| 4963 | ||
| 4964 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
|
| 4965 | " FROM tbl_energy_storage_containers_schedules " |
|
| 4966 | " WHERE id = %s ") |
|
| 4967 | cursor.execute(query, (sid,)) |
|
| 4968 | row = cursor.fetchone() |
|
| 4969 | cursor.close() |
|
| 4970 | cnx.close() |
|
| 4971 | ||
| 4972 | if row is None: |
|
| 4973 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4974 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 4975 | else: |
|
| 4976 | meta_result = {"id": row[0], |
|
| 4977 | "start_time_of_day": str(row[1]), |
|
| 4978 | "end_time_of_day": str(row[2]), |
|
| 4979 | "peak_type": row[3], |
|
| 4980 | "power": row[4]} |
|
| 4981 | ||
| 4982 | resp.text = json.dumps(meta_result) |
|
| 4983 | ||
| 4984 | @staticmethod |
|
| 4985 | @user_logger |
|
| 4986 | def on_delete(req, resp, id_, sid): |
|
| 4987 | admin_control(req) |
|
| 4988 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4989 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4990 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4991 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4992 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4993 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 4994 | ||
| 4995 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4996 | cursor = cnx.cursor() |
|
| 4997 | ||
| 4998 | cursor.execute(" SELECT name " |
|
| 4999 | " FROM tbl_energy_storage_containers " |
|
| 5000 | " WHERE id = %s ", (id_,)) |
|
| 5001 | if cursor.fetchone() is None: |
|
| 5002 | cursor.close() |
|
| 5003 | cnx.close() |
|
| 5004 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5005 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 5006 | ||
| 5007 | cursor.execute(" SELECT id " |
|
| 5008 | " FROM tbl_energy_storage_containers_schedules " |
|
| 5009 | " WHERE id = %s ", (sid,)) |
|
| 5010 | if cursor.fetchone() is None: |
|
| 5011 | cursor.close() |
|
| 5012 | cnx.close() |
|
| 5013 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5014 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 5015 | ||
| 5016 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
|
| 5017 | " WHERE id = %s ", (sid,)) |
|
| 5018 | cnx.commit() |
|
| 5019 | ||
| 5020 | cursor.close() |
|
| 5021 | cnx.close() |
|
| 5022 | ||
| 5023 | resp.status = falcon.HTTP_204 |
|
| 5024 | ||
| 5025 | @staticmethod |
|
| 5026 | @user_logger |
|
| 5027 | def on_put(req, resp, id_, sid): |
|
| 5028 | """Handles PUT requests""" |
|
| 5029 | admin_control(req) |
|
| 5030 | try: |
|
| 5031 | raw_json = req.stream.read().decode('utf-8') |
|
| 5032 | except Exception as ex: |
|
| 5033 | print(str(ex)) |
|
| 5034 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5035 | title='API.BAD_REQUEST', |
|
| 5036 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5037 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5038 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5039 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 5040 | if not sid.isdigit() or int(sid) <= 0: |
|
| 5041 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5042 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
| 5043 | ||
| 5044 | new_values = json.loads(raw_json) |
|
| 5045 | ||
| 5046 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
|
| 5047 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
|
| 5048 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
|
| 5049 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5050 | description='API.INVALID_START_TIME_OF_DAY') |
|
| 5051 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
|
| 5052 | ||
| 5053 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
|
| 5054 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
|
| 5055 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
|
| 5056 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5057 | description='API.INVALID_END_TIME_OF_DAY') |
|
| 5058 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
|
| 5059 | ||
| 5060 | if 'peak_type' not in new_values['data'].keys() or \ |
|
| 5061 | not isinstance(new_values['data']['peak_type'], str) or \ |
|
| 5062 | len(str.strip(new_values['data']['peak_type'])) == 0: |
|
| 5063 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5064 | description='API.INVALID_PEAK_TYPE') |
|
| 5065 | peak_type = str.strip(new_values['data']['peak_type']) |
|
| 5066 | ||
| 5067 | if 'power' not in new_values['data'].keys() or \ |
|
| 5068 | not (isinstance(new_values['data']['power'], float) or |
|
| 5069 | isinstance(new_values['data']['power'], int)): |
|
| 5070 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5071 | description='API.INVALID_POWER') |
|
| 5072 | power = Decimal(new_values['data']['power']) |
|
| 5073 | ||
| 5074 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5075 | cursor = cnx.cursor() |
|
| 5076 | ||
| 5077 | cursor.execute(" SELECT name " |
|
| 5078 | " FROM tbl_energy_storage_containers " |
|
| 5079 | " WHERE id = %s ", (id_,)) |
|
| 5080 | if cursor.fetchone() is None: |
|
| 5081 | cursor.close() |
|
| 5082 | cnx.close() |
|
| 5083 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5084 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 5085 | ||
| 5086 | cursor.execute(" SELECT id " |
|
| 5087 | " FROM tbl_energy_storage_containers_schedules " |
|
| 5088 | " WHERE id = %s ", (sid,)) |
|
| 5089 | if cursor.fetchone() is None: |
|
| 5090 | cursor.close() |
|
| 5091 | cnx.close() |
|
| 5092 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5093 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
| 5094 | ||
| 5095 | update_row = (" UPDATE tbl_energy_storage_containers_schedules " |
|
| 5096 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
|
| 5097 | " WHERE id = %s ") |
|
| 5098 | cursor.execute(update_row, (start_time_of_day, |
|
| 5099 | end_time_of_day, |
|
| 5100 | peak_type, |
|
| 5101 | power, |
|
| 5102 | sid)) |
|
| 5103 | cnx.commit() |
|
| 5104 | ||
| 5105 | cursor.close() |
|
| 5106 | cnx.close() |
|
| 5107 | ||
| 5108 | resp.status = falcon.HTTP_200 |
|
| 5109 | ||
| 5110 | ||
| 5111 | class EnergyStorageContainerSTSCollection: |
|
| @@ 4663-4852 (lines=190) @@ | ||
| 4660 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
|
| 4661 | ||
| 4662 | ||
| 4663 | class MicrogridScheduleItem: |
|
| 4664 | def __init__(self): |
|
| 4665 | """Initializes Class""" |
|
| 4666 | pass |
|
| 4667 | ||
| 4668 | @staticmethod |
|
| 4669 | def on_options(req, resp, id_, sid): |
|
| 4670 | _ = req |
|
| 4671 | resp.status = falcon.HTTP_200 |
|
| 4672 | _ = id_ |
|
| 4673 | ||
| 4674 | @staticmethod |
|
| 4675 | def on_get(req, resp, id_, sid): |
|
| 4676 | access_control(req) |
|
| 4677 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4678 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4679 | description='API.INVALID_MICROGRID_ID') |
|
| 4680 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4681 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4682 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4683 | ||
| 4684 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4685 | cursor = cnx.cursor() |
|
| 4686 | ||
| 4687 | cursor.execute(" SELECT name " |
|
| 4688 | " FROM tbl_microgrids " |
|
| 4689 | " WHERE id = %s ", (id_,)) |
|
| 4690 | if cursor.fetchone() is None: |
|
| 4691 | cursor.close() |
|
| 4692 | cnx.close() |
|
| 4693 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4694 | description='API.MICROGRID_NOT_FOUND') |
|
| 4695 | ||
| 4696 | query = (" SELECT id, name, uuid " |
|
| 4697 | " FROM tbl_microgrids ") |
|
| 4698 | cursor.execute(query) |
|
| 4699 | rows_energystoragecontainers = cursor.fetchall() |
|
| 4700 | ||
| 4701 | microgrid_dict = dict() |
|
| 4702 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 4703 | for row in rows_energystoragecontainers: |
|
| 4704 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 4705 | "name": row[1], |
|
| 4706 | "uuid": row[2]} |
|
| 4707 | ||
| 4708 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
|
| 4709 | " FROM tbl_microgrids_schedules " |
|
| 4710 | " WHERE id = %s ") |
|
| 4711 | cursor.execute(query, (sid,)) |
|
| 4712 | row = cursor.fetchone() |
|
| 4713 | cursor.close() |
|
| 4714 | cnx.close() |
|
| 4715 | ||
| 4716 | if row is None: |
|
| 4717 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4718 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4719 | else: |
|
| 4720 | meta_result = {"id": row[0], |
|
| 4721 | "start_time_of_day": str(row[1]), |
|
| 4722 | "end_time_of_day": str(row[2]), |
|
| 4723 | "peak_type": row[3], |
|
| 4724 | "power": row[4]} |
|
| 4725 | ||
| 4726 | resp.text = json.dumps(meta_result) |
|
| 4727 | ||
| 4728 | @staticmethod |
|
| 4729 | @user_logger |
|
| 4730 | def on_delete(req, resp, id_, sid): |
|
| 4731 | admin_control(req) |
|
| 4732 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4733 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4734 | description='API.INVALID_MICROGRID_ID') |
|
| 4735 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4736 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4737 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4738 | ||
| 4739 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4740 | cursor = cnx.cursor() |
|
| 4741 | ||
| 4742 | cursor.execute(" SELECT name " |
|
| 4743 | " FROM tbl_microgrids " |
|
| 4744 | " WHERE id = %s ", (id_,)) |
|
| 4745 | if cursor.fetchone() is None: |
|
| 4746 | cursor.close() |
|
| 4747 | cnx.close() |
|
| 4748 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4749 | description='API.MICROGRID_NOT_FOUND') |
|
| 4750 | ||
| 4751 | cursor.execute(" SELECT id " |
|
| 4752 | " FROM tbl_microgrids_schedules " |
|
| 4753 | " WHERE id = %s ", (sid,)) |
|
| 4754 | if cursor.fetchone() is None: |
|
| 4755 | cursor.close() |
|
| 4756 | cnx.close() |
|
| 4757 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4758 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4759 | ||
| 4760 | cursor.execute(" DELETE FROM tbl_microgrids_schedules " |
|
| 4761 | " WHERE id = %s ", (sid,)) |
|
| 4762 | cnx.commit() |
|
| 4763 | ||
| 4764 | cursor.close() |
|
| 4765 | cnx.close() |
|
| 4766 | ||
| 4767 | resp.status = falcon.HTTP_204 |
|
| 4768 | ||
| 4769 | @staticmethod |
|
| 4770 | @user_logger |
|
| 4771 | def on_put(req, resp, id_, sid): |
|
| 4772 | """Handles PUT requests""" |
|
| 4773 | admin_control(req) |
|
| 4774 | try: |
|
| 4775 | raw_json = req.stream.read().decode('utf-8') |
|
| 4776 | except Exception as ex: |
|
| 4777 | print(str(ex)) |
|
| 4778 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4779 | title='API.BAD_REQUEST', |
|
| 4780 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4781 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4783 | description='API.INVALID_MICROGRID_ID') |
|
| 4784 | if not sid.isdigit() or int(sid) <= 0: |
|
| 4785 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4786 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
|
| 4787 | ||
| 4788 | new_values = json.loads(raw_json) |
|
| 4789 | ||
| 4790 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
|
| 4791 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
|
| 4792 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
|
| 4793 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4794 | description='API.INVALID_START_TIME_OF_DAY') |
|
| 4795 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
|
| 4796 | ||
| 4797 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
|
| 4798 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
|
| 4799 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
|
| 4800 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4801 | description='API.INVALID_END_TIME_OF_DAY') |
|
| 4802 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
|
| 4803 | ||
| 4804 | if 'peak_type' not in new_values['data'].keys() or \ |
|
| 4805 | not isinstance(new_values['data']['peak_type'], str) or \ |
|
| 4806 | len(str.strip(new_values['data']['peak_type'])) == 0: |
|
| 4807 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4808 | description='API.INVALID_PEAK_TYPE') |
|
| 4809 | peak_type = str.strip(new_values['data']['peak_type']) |
|
| 4810 | ||
| 4811 | if 'power' not in new_values['data'].keys() or \ |
|
| 4812 | not (isinstance(new_values['data']['power'], float) or |
|
| 4813 | isinstance(new_values['data']['power'], int)): |
|
| 4814 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4815 | description='API.INVALID_POWER') |
|
| 4816 | power = float(new_values['data']['power']) |
|
| 4817 | ||
| 4818 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4819 | cursor = cnx.cursor() |
|
| 4820 | ||
| 4821 | cursor.execute(" SELECT name " |
|
| 4822 | " FROM tbl_microgrids " |
|
| 4823 | " WHERE id = %s ", (id_,)) |
|
| 4824 | if cursor.fetchone() is None: |
|
| 4825 | cursor.close() |
|
| 4826 | cnx.close() |
|
| 4827 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4828 | description='API.MICROGRID_NOT_FOUND') |
|
| 4829 | ||
| 4830 | cursor.execute(" SELECT id " |
|
| 4831 | " FROM tbl_microgrids_schedules " |
|
| 4832 | " WHERE id = %s ", (sid,)) |
|
| 4833 | if cursor.fetchone() is None: |
|
| 4834 | cursor.close() |
|
| 4835 | cnx.close() |
|
| 4836 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4837 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
|
| 4838 | ||
| 4839 | update_row = (" UPDATE tbl_microgrids_schedules " |
|
| 4840 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
|
| 4841 | " WHERE id = %s ") |
|
| 4842 | cursor.execute(update_row, (start_time_of_day, |
|
| 4843 | end_time_of_day, |
|
| 4844 | peak_type, |
|
| 4845 | power, |
|
| 4846 | sid)) |
|
| 4847 | cnx.commit() |
|
| 4848 | ||
| 4849 | cursor.close() |
|
| 4850 | cnx.close() |
|
| 4851 | ||
| 4852 | resp.status = falcon.HTTP_200 |
|
| 4853 | ||
| 4854 | ||
| 4855 | class MicrogridSensorCollection: |
|