@@ 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: |
@@ 5159-5348 (lines=190) @@ | ||
5156 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
|
5157 | ||
5158 | ||
5159 | class EnergyStorageContainerScheduleItem: |
|
5160 | def __init__(self): |
|
5161 | """Initializes Class""" |
|
5162 | pass |
|
5163 | ||
5164 | @staticmethod |
|
5165 | def on_options(req, resp, id_, sid): |
|
5166 | _ = req |
|
5167 | resp.status = falcon.HTTP_200 |
|
5168 | _ = id_ |
|
5169 | ||
5170 | @staticmethod |
|
5171 | def on_get(req, resp, id_, sid): |
|
5172 | access_control(req) |
|
5173 | if not id_.isdigit() or int(id_) <= 0: |
|
5174 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5175 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
5176 | if not sid.isdigit() or int(sid) <= 0: |
|
5177 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5178 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
5179 | ||
5180 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5181 | cursor = cnx.cursor() |
|
5182 | ||
5183 | cursor.execute(" SELECT name " |
|
5184 | " FROM tbl_energy_storage_containers " |
|
5185 | " WHERE id = %s ", (id_,)) |
|
5186 | if cursor.fetchone() is None: |
|
5187 | cursor.close() |
|
5188 | cnx.close() |
|
5189 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5190 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
5191 | ||
5192 | query = (" SELECT id, name, uuid " |
|
5193 | " FROM tbl_energy_storage_containers ") |
|
5194 | cursor.execute(query) |
|
5195 | rows_energystoragecontainers = cursor.fetchall() |
|
5196 | ||
5197 | energy_storage_container_dict = dict() |
|
5198 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
5199 | for row in rows_energystoragecontainers: |
|
5200 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
5201 | "name": row[1], |
|
5202 | "uuid": row[2]} |
|
5203 | ||
5204 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
|
5205 | " FROM tbl_energy_storage_containers_schedules " |
|
5206 | " WHERE id = %s ") |
|
5207 | cursor.execute(query, (sid,)) |
|
5208 | row = cursor.fetchone() |
|
5209 | cursor.close() |
|
5210 | cnx.close() |
|
5211 | ||
5212 | if row is None: |
|
5213 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5214 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
5215 | else: |
|
5216 | meta_result = {"id": row[0], |
|
5217 | "start_time_of_day": str(row[1]), |
|
5218 | "end_time_of_day": str(row[2]), |
|
5219 | "peak_type": row[3], |
|
5220 | "power": row[4]} |
|
5221 | ||
5222 | resp.text = json.dumps(meta_result) |
|
5223 | ||
5224 | @staticmethod |
|
5225 | @user_logger |
|
5226 | def on_delete(req, resp, id_, sid): |
|
5227 | admin_control(req) |
|
5228 | if not id_.isdigit() or int(id_) <= 0: |
|
5229 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5230 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
5231 | if not sid.isdigit() or int(sid) <= 0: |
|
5232 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5233 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
5234 | ||
5235 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5236 | cursor = cnx.cursor() |
|
5237 | ||
5238 | cursor.execute(" SELECT name " |
|
5239 | " FROM tbl_energy_storage_containers " |
|
5240 | " WHERE id = %s ", (id_,)) |
|
5241 | if cursor.fetchone() is None: |
|
5242 | cursor.close() |
|
5243 | cnx.close() |
|
5244 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5245 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
5246 | ||
5247 | cursor.execute(" SELECT id " |
|
5248 | " FROM tbl_energy_storage_containers_schedules " |
|
5249 | " WHERE id = %s ", (sid,)) |
|
5250 | if cursor.fetchone() is None: |
|
5251 | cursor.close() |
|
5252 | cnx.close() |
|
5253 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5254 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
5255 | ||
5256 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
|
5257 | " WHERE id = %s ", (sid,)) |
|
5258 | cnx.commit() |
|
5259 | ||
5260 | cursor.close() |
|
5261 | cnx.close() |
|
5262 | ||
5263 | resp.status = falcon.HTTP_204 |
|
5264 | ||
5265 | @staticmethod |
|
5266 | @user_logger |
|
5267 | def on_put(req, resp, id_, sid): |
|
5268 | """Handles PUT requests""" |
|
5269 | admin_control(req) |
|
5270 | try: |
|
5271 | raw_json = req.stream.read().decode('utf-8') |
|
5272 | except Exception as ex: |
|
5273 | print(str(ex)) |
|
5274 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5275 | title='API.BAD_REQUEST', |
|
5276 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5277 | if not id_.isdigit() or int(id_) <= 0: |
|
5278 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5279 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
5280 | if not sid.isdigit() or int(sid) <= 0: |
|
5281 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5282 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
|
5283 | ||
5284 | new_values = json.loads(raw_json) |
|
5285 | ||
5286 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
|
5287 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
|
5288 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
|
5289 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5290 | description='API.INVALID_START_TIME_OF_DAY') |
|
5291 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
|
5292 | ||
5293 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
|
5294 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
|
5295 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
|
5296 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5297 | description='API.INVALID_END_TIME_OF_DAY') |
|
5298 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
|
5299 | ||
5300 | if 'peak_type' not in new_values['data'].keys() or \ |
|
5301 | not isinstance(new_values['data']['peak_type'], str) or \ |
|
5302 | len(str.strip(new_values['data']['peak_type'])) == 0: |
|
5303 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5304 | description='API.INVALID_PEAK_TYPE') |
|
5305 | peak_type = str.strip(new_values['data']['peak_type']) |
|
5306 | ||
5307 | if 'power' not in new_values['data'].keys() or \ |
|
5308 | not (isinstance(new_values['data']['power'], float) or |
|
5309 | isinstance(new_values['data']['power'], int)): |
|
5310 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5311 | description='API.INVALID_POWER') |
|
5312 | power = Decimal(new_values['data']['power']) |
|
5313 | ||
5314 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5315 | cursor = cnx.cursor() |
|
5316 | ||
5317 | cursor.execute(" SELECT name " |
|
5318 | " FROM tbl_energy_storage_containers " |
|
5319 | " WHERE id = %s ", (id_,)) |
|
5320 | if cursor.fetchone() is None: |
|
5321 | cursor.close() |
|
5322 | cnx.close() |
|
5323 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5324 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
5325 | ||
5326 | cursor.execute(" SELECT id " |
|
5327 | " FROM tbl_energy_storage_containers_schedules " |
|
5328 | " WHERE id = %s ", (sid,)) |
|
5329 | if cursor.fetchone() is None: |
|
5330 | cursor.close() |
|
5331 | cnx.close() |
|
5332 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5333 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
|
5334 | ||
5335 | update_row = (" UPDATE tbl_energy_storage_containers_schedules " |
|
5336 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
|
5337 | " WHERE id = %s ") |
|
5338 | cursor.execute(update_row, (start_time_of_day, |
|
5339 | end_time_of_day, |
|
5340 | peak_type, |
|
5341 | power, |
|
5342 | sid)) |
|
5343 | cnx.commit() |
|
5344 | ||
5345 | cursor.close() |
|
5346 | cnx.close() |
|
5347 | ||
5348 | resp.status = falcon.HTTP_200 |
|
5349 | ||
5350 | ||
5351 | class EnergyStorageContainerSTSCollection: |