| @@ 5670-5789 (lines=120) @@ | ||
| 5667 | resp.status = falcon.HTTP_200 |
|
| 5668 | ||
| 5669 | ||
| 5670 | class EnergyStorageContainerSTSPointCollection: |
|
| 5671 | def __init__(self): |
|
| 5672 | pass |
|
| 5673 | ||
| 5674 | @staticmethod |
|
| 5675 | def on_options(req, resp, id_, fid): |
|
| 5676 | _ = req |
|
| 5677 | resp.status = falcon.HTTP_200 |
|
| 5678 | _ = id_ |
|
| 5679 | ||
| 5680 | @staticmethod |
|
| 5681 | def on_get(req, resp, id_, fid): |
|
| 5682 | if 'API-KEY' not in req.headers or \ |
|
| 5683 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5684 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5685 | access_control(req) |
|
| 5686 | else: |
|
| 5687 | api_key_control(req) |
|
| 5688 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5689 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5690 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 5691 | if not fid.isdigit() or int(fid) <= 0: |
|
| 5692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5693 | description='API.INVALID_STS_ID') |
|
| 5694 | ||
| 5695 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5696 | cursor = cnx.cursor() |
|
| 5697 | ||
| 5698 | cursor.execute(" SELECT name " |
|
| 5699 | " FROM tbl_energy_storage_containers_stses " |
|
| 5700 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, )) |
|
| 5701 | if cursor.fetchone() is None: |
|
| 5702 | cursor.close() |
|
| 5703 | cnx.close() |
|
| 5704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5705 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
|
| 5706 | ||
| 5707 | query = (" SELECT p.id, p.name, " |
|
| 5708 | " ds.id, ds.name, ds.uuid, " |
|
| 5709 | " p.address " |
|
| 5710 | " FROM tbl_points p, tbl_energy_storage_containers_stses_points mp, tbl_data_sources ds " |
|
| 5711 | " WHERE mp.sts_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 5712 | " ORDER BY p.name ") |
|
| 5713 | cursor.execute(query, (fid,)) |
|
| 5714 | rows = cursor.fetchall() |
|
| 5715 | ||
| 5716 | result = list() |
|
| 5717 | if rows is not None and len(rows) > 0: |
|
| 5718 | for row in rows: |
|
| 5719 | meta_result = {"id": row[0], "name": row[1], |
|
| 5720 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 5721 | "address": row[5]} |
|
| 5722 | result.append(meta_result) |
|
| 5723 | ||
| 5724 | resp.text = json.dumps(result) |
|
| 5725 | ||
| 5726 | @staticmethod |
|
| 5727 | @user_logger |
|
| 5728 | def on_post(req, resp, id_, fid): |
|
| 5729 | """Handles POST requests""" |
|
| 5730 | admin_control(req) |
|
| 5731 | try: |
|
| 5732 | raw_json = req.stream.read().decode('utf-8') |
|
| 5733 | except Exception as ex: |
|
| 5734 | print(str(ex)) |
|
| 5735 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5736 | title='API.BAD_REQUEST', |
|
| 5737 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5738 | ||
| 5739 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5740 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5741 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 5742 | if not fid.isdigit() or int(fid) <= 0: |
|
| 5743 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5744 | description='API.INVALID_STS_ID') |
|
| 5745 | ||
| 5746 | new_values = json.loads(raw_json) |
|
| 5747 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5748 | cursor = cnx.cursor() |
|
| 5749 | ||
| 5750 | cursor.execute(" SELECT name " |
|
| 5751 | " FROM tbl_energy_storage_containers_stses " |
|
| 5752 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
|
| 5753 | if cursor.fetchone() is None: |
|
| 5754 | cursor.close() |
|
| 5755 | cnx.close() |
|
| 5756 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5757 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
|
| 5758 | ||
| 5759 | cursor.execute(" SELECT name, object_type " |
|
| 5760 | " FROM tbl_points " |
|
| 5761 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 5762 | row = cursor.fetchone() |
|
| 5763 | if row is None: |
|
| 5764 | cursor.close() |
|
| 5765 | cnx.close() |
|
| 5766 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5767 | description='API.POINT_NOT_FOUND') |
|
| 5768 | ||
| 5769 | query = (" SELECT id " |
|
| 5770 | " FROM tbl_energy_storage_containers_stses_points " |
|
| 5771 | " WHERE sts_id = %s AND point_id = %s") |
|
| 5772 | cursor.execute(query, (fid, new_values['data']['point_id'],)) |
|
| 5773 | if cursor.fetchone() is not None: |
|
| 5774 | cursor.close() |
|
| 5775 | cnx.close() |
|
| 5776 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5777 | description='API.STS_POINT_RELATION_EXISTS') |
|
| 5778 | ||
| 5779 | add_row = (" INSERT INTO tbl_energy_storage_containers_stses_points (sts_id, point_id) " |
|
| 5780 | " VALUES (%s, %s) ") |
|
| 5781 | cursor.execute(add_row, (fid, new_values['data']['point_id'],)) |
|
| 5782 | cnx.commit() |
|
| 5783 | cursor.close() |
|
| 5784 | cnx.close() |
|
| 5785 | ||
| 5786 | resp.status = falcon.HTTP_201 |
|
| 5787 | resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(fid) + '/points/' + \ |
|
| 5788 | str(new_values['data']['point_id']) |
|
| 5789 | ||
| 5790 | ||
| 5791 | class EnergyStorageContainerSTSPointItem: |
|
| 5792 | def __init__(self): |
|
| @@ 4885-5004 (lines=120) @@ | ||
| 4882 | resp.status = falcon.HTTP_200 |
|
| 4883 | ||
| 4884 | ||
| 4885 | class EnergyStorageContainerPCSPointCollection: |
|
| 4886 | def __init__(self): |
|
| 4887 | pass |
|
| 4888 | ||
| 4889 | @staticmethod |
|
| 4890 | def on_options(req, resp, id_, pcsid): |
|
| 4891 | _ = req |
|
| 4892 | resp.status = falcon.HTTP_200 |
|
| 4893 | _ = id_ |
|
| 4894 | ||
| 4895 | @staticmethod |
|
| 4896 | def on_get(req, resp, id_, pcsid): |
|
| 4897 | if 'API-KEY' not in req.headers or \ |
|
| 4898 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 4899 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 4900 | access_control(req) |
|
| 4901 | else: |
|
| 4902 | api_key_control(req) |
|
| 4903 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4904 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4905 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4906 | if not pcsid.isdigit() or int(pcsid) <= 0: |
|
| 4907 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4908 | description='API.INVALID_PCS_ID') |
|
| 4909 | ||
| 4910 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4911 | cursor = cnx.cursor() |
|
| 4912 | ||
| 4913 | cursor.execute(" SELECT name " |
|
| 4914 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
|
| 4915 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid, )) |
|
| 4916 | if cursor.fetchone() is None: |
|
| 4917 | cursor.close() |
|
| 4918 | cnx.close() |
|
| 4919 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4920 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND') |
|
| 4921 | ||
| 4922 | query = (" SELECT p.id, p.name, " |
|
| 4923 | " ds.id, ds.name, ds.uuid, " |
|
| 4924 | " p.address " |
|
| 4925 | " FROM tbl_points p, tbl_energy_storage_containers_pcses_points mp, tbl_data_sources ds " |
|
| 4926 | " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 4927 | " ORDER BY p.name ") |
|
| 4928 | cursor.execute(query, (pcsid,)) |
|
| 4929 | rows = cursor.fetchall() |
|
| 4930 | ||
| 4931 | result = list() |
|
| 4932 | if rows is not None and len(rows) > 0: |
|
| 4933 | for row in rows: |
|
| 4934 | meta_result = {"id": row[0], "name": row[1], |
|
| 4935 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 4936 | "address": row[5]} |
|
| 4937 | result.append(meta_result) |
|
| 4938 | ||
| 4939 | resp.text = json.dumps(result) |
|
| 4940 | ||
| 4941 | @staticmethod |
|
| 4942 | @user_logger |
|
| 4943 | def on_post(req, resp, id_, pcsid): |
|
| 4944 | """Handles POST requests""" |
|
| 4945 | admin_control(req) |
|
| 4946 | try: |
|
| 4947 | raw_json = req.stream.read().decode('utf-8') |
|
| 4948 | except Exception as ex: |
|
| 4949 | print(str(ex)) |
|
| 4950 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4951 | title='API.BAD_REQUEST', |
|
| 4952 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4953 | ||
| 4954 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4955 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4956 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4957 | if not pcsid.isdigit() or int(pcsid) <= 0: |
|
| 4958 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4959 | description='API.INVALID_PCS_ID') |
|
| 4960 | ||
| 4961 | new_values = json.loads(raw_json) |
|
| 4962 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4963 | cursor = cnx.cursor() |
|
| 4964 | ||
| 4965 | cursor.execute(" SELECT name " |
|
| 4966 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
|
| 4967 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid,)) |
|
| 4968 | if cursor.fetchone() is None: |
|
| 4969 | cursor.close() |
|
| 4970 | cnx.close() |
|
| 4971 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4972 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND') |
|
| 4973 | ||
| 4974 | cursor.execute(" SELECT name, object_type " |
|
| 4975 | " FROM tbl_points " |
|
| 4976 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 4977 | row = cursor.fetchone() |
|
| 4978 | if row is None: |
|
| 4979 | cursor.close() |
|
| 4980 | cnx.close() |
|
| 4981 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4982 | description='API.POINT_NOT_FOUND') |
|
| 4983 | ||
| 4984 | query = (" SELECT id " |
|
| 4985 | " FROM tbl_energy_storage_containers_pcses_points " |
|
| 4986 | " WHERE pcs_id = %s AND point_id = %s") |
|
| 4987 | cursor.execute(query, (pcsid, new_values['data']['point_id'],)) |
|
| 4988 | if cursor.fetchone() is not None: |
|
| 4989 | cursor.close() |
|
| 4990 | cnx.close() |
|
| 4991 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 4992 | description='API.PCS_POINT_RELATION_EXISTS') |
|
| 4993 | ||
| 4994 | add_row = (" INSERT INTO tbl_energy_storage_containers_pcses_points (pcs_id, point_id) " |
|
| 4995 | " VALUES (%s, %s) ") |
|
| 4996 | cursor.execute(add_row, (pcsid, new_values['data']['point_id'],)) |
|
| 4997 | cnx.commit() |
|
| 4998 | cursor.close() |
|
| 4999 | cnx.close() |
|
| 5000 | ||
| 5001 | resp.status = falcon.HTTP_201 |
|
| 5002 | resp.location = '/energystoragecontainers/' + str(id_) + '/pcses/' + str(pcsid) + '/points/' + \ |
|
| 5003 | str(new_values['data']['point_id']) |
|
| 5004 | ||
| 5005 | ||
| 5006 | class EnergyStorageContainerPCSPointItem: |
|
| 5007 | def __init__(self): |
|
| @@ 4304-4423 (lines=120) @@ | ||
| 4301 | resp.status = falcon.HTTP_200 |
|
| 4302 | ||
| 4303 | ||
| 4304 | class EnergyStorageContainerLoadPointCollection: |
|
| 4305 | def __init__(self): |
|
| 4306 | pass |
|
| 4307 | ||
| 4308 | @staticmethod |
|
| 4309 | def on_options(req, resp, id_, lid): |
|
| 4310 | _ = req |
|
| 4311 | resp.status = falcon.HTTP_200 |
|
| 4312 | _ = id_ |
|
| 4313 | ||
| 4314 | @staticmethod |
|
| 4315 | def on_get(req, resp, id_, lid): |
|
| 4316 | if 'API-KEY' not in req.headers or \ |
|
| 4317 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 4318 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 4319 | access_control(req) |
|
| 4320 | else: |
|
| 4321 | api_key_control(req) |
|
| 4322 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4323 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4324 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4325 | if not lid.isdigit() or int(lid) <= 0: |
|
| 4326 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4327 | description='API.INVALID_LOAD_ID') |
|
| 4328 | ||
| 4329 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4330 | cursor = cnx.cursor() |
|
| 4331 | ||
| 4332 | cursor.execute(" SELECT name " |
|
| 4333 | " FROM tbl_energy_storage_containers_loads " |
|
| 4334 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid, )) |
|
| 4335 | if cursor.fetchone() is None: |
|
| 4336 | cursor.close() |
|
| 4337 | cnx.close() |
|
| 4338 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4339 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
|
| 4340 | ||
| 4341 | query = (" SELECT p.id, p.name, " |
|
| 4342 | " ds.id, ds.name, ds.uuid, " |
|
| 4343 | " p.address " |
|
| 4344 | " FROM tbl_points p, tbl_energy_storage_containers_loads_points mp, tbl_data_sources ds " |
|
| 4345 | " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 4346 | " ORDER BY p.name ") |
|
| 4347 | cursor.execute(query, (lid,)) |
|
| 4348 | rows = cursor.fetchall() |
|
| 4349 | ||
| 4350 | result = list() |
|
| 4351 | if rows is not None and len(rows) > 0: |
|
| 4352 | for row in rows: |
|
| 4353 | meta_result = {"id": row[0], "name": row[1], |
|
| 4354 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 4355 | "address": row[5]} |
|
| 4356 | result.append(meta_result) |
|
| 4357 | ||
| 4358 | resp.text = json.dumps(result) |
|
| 4359 | ||
| 4360 | @staticmethod |
|
| 4361 | @user_logger |
|
| 4362 | def on_post(req, resp, id_, lid): |
|
| 4363 | """Handles POST requests""" |
|
| 4364 | admin_control(req) |
|
| 4365 | try: |
|
| 4366 | raw_json = req.stream.read().decode('utf-8') |
|
| 4367 | except Exception as ex: |
|
| 4368 | print(str(ex)) |
|
| 4369 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4370 | title='API.BAD_REQUEST', |
|
| 4371 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4372 | ||
| 4373 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4374 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4375 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4376 | if not lid.isdigit() or int(lid) <= 0: |
|
| 4377 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4378 | description='API.INVALID_LOAD_ID') |
|
| 4379 | ||
| 4380 | new_values = json.loads(raw_json) |
|
| 4381 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4382 | cursor = cnx.cursor() |
|
| 4383 | ||
| 4384 | cursor.execute(" SELECT name " |
|
| 4385 | " FROM tbl_energy_storage_containers_loads " |
|
| 4386 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid,)) |
|
| 4387 | if cursor.fetchone() is None: |
|
| 4388 | cursor.close() |
|
| 4389 | cnx.close() |
|
| 4390 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4391 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
|
| 4392 | ||
| 4393 | cursor.execute(" SELECT name, object_type " |
|
| 4394 | " FROM tbl_points " |
|
| 4395 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 4396 | row = cursor.fetchone() |
|
| 4397 | if row is None: |
|
| 4398 | cursor.close() |
|
| 4399 | cnx.close() |
|
| 4400 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4401 | description='API.POINT_NOT_FOUND') |
|
| 4402 | ||
| 4403 | query = (" SELECT id " |
|
| 4404 | " FROM tbl_energy_storage_containers_loads_points " |
|
| 4405 | " WHERE load_id = %s AND point_id = %s") |
|
| 4406 | cursor.execute(query, (lid, new_values['data']['point_id'],)) |
|
| 4407 | if cursor.fetchone() is not None: |
|
| 4408 | cursor.close() |
|
| 4409 | cnx.close() |
|
| 4410 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 4411 | description='API.LOAD_POINT_RELATION_EXISTS') |
|
| 4412 | ||
| 4413 | add_row = (" INSERT INTO tbl_energy_storage_containers_loads_points (load_id, point_id) " |
|
| 4414 | " VALUES (%s, %s) ") |
|
| 4415 | cursor.execute(add_row, (lid, new_values['data']['point_id'],)) |
|
| 4416 | cnx.commit() |
|
| 4417 | cursor.close() |
|
| 4418 | cnx.close() |
|
| 4419 | ||
| 4420 | resp.status = falcon.HTTP_201 |
|
| 4421 | resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(lid) + '/points/' + \ |
|
| 4422 | str(new_values['data']['point_id']) |
|
| 4423 | ||
| 4424 | ||
| 4425 | class EnergyStorageContainerLoadPointItem: |
|
| 4426 | def __init__(self): |
|
| @@ 3673-3792 (lines=120) @@ | ||
| 3670 | resp.status = falcon.HTTP_200 |
|
| 3671 | ||
| 3672 | ||
| 3673 | class EnergyStorageContainerHVACPointCollection: |
|
| 3674 | def __init__(self): |
|
| 3675 | pass |
|
| 3676 | ||
| 3677 | @staticmethod |
|
| 3678 | def on_options(req, resp, id_, hid): |
|
| 3679 | _ = req |
|
| 3680 | resp.status = falcon.HTTP_200 |
|
| 3681 | _ = id_ |
|
| 3682 | ||
| 3683 | @staticmethod |
|
| 3684 | def on_get(req, resp, id_, hid): |
|
| 3685 | if 'API-KEY' not in req.headers or \ |
|
| 3686 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3687 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3688 | access_control(req) |
|
| 3689 | else: |
|
| 3690 | api_key_control(req) |
|
| 3691 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3693 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3694 | if not hid.isdigit() or int(hid) <= 0: |
|
| 3695 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3696 | description='API.INVALID_HVAC_ID') |
|
| 3697 | ||
| 3698 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3699 | cursor = cnx.cursor() |
|
| 3700 | ||
| 3701 | cursor.execute(" SELECT name " |
|
| 3702 | " FROM tbl_energy_storage_containers_hvacs " |
|
| 3703 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid, )) |
|
| 3704 | if cursor.fetchone() is None: |
|
| 3705 | cursor.close() |
|
| 3706 | cnx.close() |
|
| 3707 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3708 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
|
| 3709 | ||
| 3710 | query = (" SELECT p.id, p.name, " |
|
| 3711 | " ds.id, ds.name, ds.uuid, " |
|
| 3712 | " p.address " |
|
| 3713 | " FROM tbl_points p, tbl_energy_storage_containers_hvacs_points mp, tbl_data_sources ds " |
|
| 3714 | " WHERE mp.hvac_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 3715 | " ORDER BY p.name ") |
|
| 3716 | cursor.execute(query, (hid,)) |
|
| 3717 | rows = cursor.fetchall() |
|
| 3718 | ||
| 3719 | result = list() |
|
| 3720 | if rows is not None and len(rows) > 0: |
|
| 3721 | for row in rows: |
|
| 3722 | meta_result = {"id": row[0], "name": row[1], |
|
| 3723 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 3724 | "address": row[5]} |
|
| 3725 | result.append(meta_result) |
|
| 3726 | ||
| 3727 | resp.text = json.dumps(result) |
|
| 3728 | ||
| 3729 | @staticmethod |
|
| 3730 | @user_logger |
|
| 3731 | def on_post(req, resp, id_, hid): |
|
| 3732 | """Handles POST requests""" |
|
| 3733 | admin_control(req) |
|
| 3734 | try: |
|
| 3735 | raw_json = req.stream.read().decode('utf-8') |
|
| 3736 | except Exception as ex: |
|
| 3737 | print(str(ex)) |
|
| 3738 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3739 | title='API.BAD_REQUEST', |
|
| 3740 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3741 | ||
| 3742 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3743 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3744 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3745 | if not hid.isdigit() or int(hid) <= 0: |
|
| 3746 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3747 | description='API.INVALID_HVAC_ID') |
|
| 3748 | ||
| 3749 | new_values = json.loads(raw_json) |
|
| 3750 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3751 | cursor = cnx.cursor() |
|
| 3752 | ||
| 3753 | cursor.execute(" SELECT name " |
|
| 3754 | " FROM tbl_energy_storage_containers_hvacs " |
|
| 3755 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid,)) |
|
| 3756 | if cursor.fetchone() is None: |
|
| 3757 | cursor.close() |
|
| 3758 | cnx.close() |
|
| 3759 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3760 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
|
| 3761 | ||
| 3762 | cursor.execute(" SELECT name, object_type " |
|
| 3763 | " FROM tbl_points " |
|
| 3764 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 3765 | row = cursor.fetchone() |
|
| 3766 | if row is None: |
|
| 3767 | cursor.close() |
|
| 3768 | cnx.close() |
|
| 3769 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3770 | description='API.POINT_NOT_FOUND') |
|
| 3771 | ||
| 3772 | query = (" SELECT id " |
|
| 3773 | " FROM tbl_energy_storage_containers_hvacs_points " |
|
| 3774 | " WHERE hvac_id = %s AND point_id = %s") |
|
| 3775 | cursor.execute(query, (hid, new_values['data']['point_id'],)) |
|
| 3776 | if cursor.fetchone() is not None: |
|
| 3777 | cursor.close() |
|
| 3778 | cnx.close() |
|
| 3779 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3780 | description='API.HVAC_POINT_RELATION_EXISTS') |
|
| 3781 | ||
| 3782 | add_row = (" INSERT INTO tbl_energy_storage_containers_hvacs_points (hvac_id, point_id) " |
|
| 3783 | " VALUES (%s, %s) ") |
|
| 3784 | cursor.execute(add_row, (hid, new_values['data']['point_id'],)) |
|
| 3785 | cnx.commit() |
|
| 3786 | cursor.close() |
|
| 3787 | cnx.close() |
|
| 3788 | ||
| 3789 | resp.status = falcon.HTTP_201 |
|
| 3790 | resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(hid) + '/points/' + \ |
|
| 3791 | str(new_values['data']['point_id']) |
|
| 3792 | ||
| 3793 | ||
| 3794 | class EnergyStorageContainerHVACPointItem: |
|
| 3795 | def __init__(self): |
|
| @@ 3187-3306 (lines=120) @@ | ||
| 3184 | resp.status = falcon.HTTP_200 |
|
| 3185 | ||
| 3186 | ||
| 3187 | class EnergyStorageContainerGridPointCollection: |
|
| 3188 | def __init__(self): |
|
| 3189 | pass |
|
| 3190 | ||
| 3191 | @staticmethod |
|
| 3192 | def on_options(req, resp, id_, gid): |
|
| 3193 | _ = req |
|
| 3194 | resp.status = falcon.HTTP_200 |
|
| 3195 | _ = id_ |
|
| 3196 | ||
| 3197 | @staticmethod |
|
| 3198 | def on_get(req, resp, id_, gid): |
|
| 3199 | if 'API-KEY' not in req.headers or \ |
|
| 3200 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 3201 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 3202 | access_control(req) |
|
| 3203 | else: |
|
| 3204 | api_key_control(req) |
|
| 3205 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3206 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3207 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3208 | if not gid.isdigit() or int(gid) <= 0: |
|
| 3209 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3210 | description='API.INVALID_GRID_ID') |
|
| 3211 | ||
| 3212 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3213 | cursor = cnx.cursor() |
|
| 3214 | ||
| 3215 | cursor.execute(" SELECT name " |
|
| 3216 | " FROM tbl_energy_storage_containers_grids " |
|
| 3217 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid, )) |
|
| 3218 | if cursor.fetchone() is None: |
|
| 3219 | cursor.close() |
|
| 3220 | cnx.close() |
|
| 3221 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3222 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 3223 | ||
| 3224 | query = (" SELECT p.id, p.name, " |
|
| 3225 | " ds.id, ds.name, ds.uuid, " |
|
| 3226 | " p.address " |
|
| 3227 | " FROM tbl_points p, tbl_energy_storage_containers_grids_points mp, tbl_data_sources ds " |
|
| 3228 | " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 3229 | " ORDER BY p.name ") |
|
| 3230 | cursor.execute(query, (gid,)) |
|
| 3231 | rows = cursor.fetchall() |
|
| 3232 | ||
| 3233 | result = list() |
|
| 3234 | if rows is not None and len(rows) > 0: |
|
| 3235 | for row in rows: |
|
| 3236 | meta_result = {"id": row[0], "name": row[1], |
|
| 3237 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 3238 | "address": row[5]} |
|
| 3239 | result.append(meta_result) |
|
| 3240 | ||
| 3241 | resp.text = json.dumps(result) |
|
| 3242 | ||
| 3243 | @staticmethod |
|
| 3244 | @user_logger |
|
| 3245 | def on_post(req, resp, id_, gid): |
|
| 3246 | """Handles POST requests""" |
|
| 3247 | admin_control(req) |
|
| 3248 | try: |
|
| 3249 | raw_json = req.stream.read().decode('utf-8') |
|
| 3250 | except Exception as ex: |
|
| 3251 | print(str(ex)) |
|
| 3252 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3253 | title='API.BAD_REQUEST', |
|
| 3254 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3255 | ||
| 3256 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3257 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3258 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3259 | if not gid.isdigit() or int(gid) <= 0: |
|
| 3260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3261 | description='API.INVALID_GRID_ID') |
|
| 3262 | ||
| 3263 | new_values = json.loads(raw_json) |
|
| 3264 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3265 | cursor = cnx.cursor() |
|
| 3266 | ||
| 3267 | cursor.execute(" SELECT name " |
|
| 3268 | " FROM tbl_energy_storage_containers_grids " |
|
| 3269 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid,)) |
|
| 3270 | if cursor.fetchone() is None: |
|
| 3271 | cursor.close() |
|
| 3272 | cnx.close() |
|
| 3273 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3274 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 3275 | ||
| 3276 | cursor.execute(" SELECT name, object_type " |
|
| 3277 | " FROM tbl_points " |
|
| 3278 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 3279 | row = cursor.fetchone() |
|
| 3280 | if row is None: |
|
| 3281 | cursor.close() |
|
| 3282 | cnx.close() |
|
| 3283 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3284 | description='API.POINT_NOT_FOUND') |
|
| 3285 | ||
| 3286 | query = (" SELECT id " |
|
| 3287 | " FROM tbl_energy_storage_containers_grids_points " |
|
| 3288 | " WHERE grid_id = %s AND point_id = %s") |
|
| 3289 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
|
| 3290 | if cursor.fetchone() is not None: |
|
| 3291 | cursor.close() |
|
| 3292 | cnx.close() |
|
| 3293 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 3294 | description='API.GRID_POINT_RELATION_EXISTS') |
|
| 3295 | ||
| 3296 | add_row = (" INSERT INTO tbl_energy_storage_containers_grids_points (grid_id, point_id) " |
|
| 3297 | " VALUES (%s, %s) ") |
|
| 3298 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
|
| 3299 | cnx.commit() |
|
| 3300 | cursor.close() |
|
| 3301 | cnx.close() |
|
| 3302 | ||
| 3303 | resp.status = falcon.HTTP_201 |
|
| 3304 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(gid) + '/points/' + \ |
|
| 3305 | str(new_values['data']['point_id']) |
|
| 3306 | ||
| 3307 | ||
| 3308 | class EnergyStorageContainerGridPointItem: |
|
| 3309 | def __init__(self): |
|
| @@ 2516-2635 (lines=120) @@ | ||
| 2513 | resp.status = falcon.HTTP_200 |
|
| 2514 | ||
| 2515 | ||
| 2516 | class EnergyStorageContainerFirecontrolPointCollection: |
|
| 2517 | def __init__(self): |
|
| 2518 | pass |
|
| 2519 | ||
| 2520 | @staticmethod |
|
| 2521 | def on_options(req, resp, id_, fid): |
|
| 2522 | _ = req |
|
| 2523 | resp.status = falcon.HTTP_200 |
|
| 2524 | _ = id_ |
|
| 2525 | ||
| 2526 | @staticmethod |
|
| 2527 | def on_get(req, resp, id_, fid): |
|
| 2528 | if 'API-KEY' not in req.headers or \ |
|
| 2529 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2530 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2531 | access_control(req) |
|
| 2532 | else: |
|
| 2533 | api_key_control(req) |
|
| 2534 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2536 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2537 | if not fid.isdigit() or int(fid) <= 0: |
|
| 2538 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2539 | description='API.INVALID_FIRECONTROL_ID') |
|
| 2540 | ||
| 2541 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2542 | cursor = cnx.cursor() |
|
| 2543 | ||
| 2544 | cursor.execute(" SELECT name " |
|
| 2545 | " FROM tbl_energy_storage_containers_firecontrols " |
|
| 2546 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, )) |
|
| 2547 | if cursor.fetchone() is None: |
|
| 2548 | cursor.close() |
|
| 2549 | cnx.close() |
|
| 2550 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2551 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
|
| 2552 | ||
| 2553 | query = (" SELECT p.id, p.name, " |
|
| 2554 | " ds.id, ds.name, ds.uuid, " |
|
| 2555 | " p.address " |
|
| 2556 | " FROM tbl_points p, tbl_energy_storage_containers_firecontrols_points mp, tbl_data_sources ds " |
|
| 2557 | " WHERE mp.firecontrol_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 2558 | " ORDER BY p.name ") |
|
| 2559 | cursor.execute(query, (fid,)) |
|
| 2560 | rows = cursor.fetchall() |
|
| 2561 | ||
| 2562 | result = list() |
|
| 2563 | if rows is not None and len(rows) > 0: |
|
| 2564 | for row in rows: |
|
| 2565 | meta_result = {"id": row[0], "name": row[1], |
|
| 2566 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 2567 | "address": row[5]} |
|
| 2568 | result.append(meta_result) |
|
| 2569 | ||
| 2570 | resp.text = json.dumps(result) |
|
| 2571 | ||
| 2572 | @staticmethod |
|
| 2573 | @user_logger |
|
| 2574 | def on_post(req, resp, id_, fid): |
|
| 2575 | """Handles POST requests""" |
|
| 2576 | admin_control(req) |
|
| 2577 | try: |
|
| 2578 | raw_json = req.stream.read().decode('utf-8') |
|
| 2579 | except Exception as ex: |
|
| 2580 | print(str(ex)) |
|
| 2581 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2582 | title='API.BAD_REQUEST', |
|
| 2583 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2584 | ||
| 2585 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2586 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2587 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2588 | if not fid.isdigit() or int(fid) <= 0: |
|
| 2589 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2590 | description='API.INVALID_FIRECONTROL_ID') |
|
| 2591 | ||
| 2592 | new_values = json.loads(raw_json) |
|
| 2593 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2594 | cursor = cnx.cursor() |
|
| 2595 | ||
| 2596 | cursor.execute(" SELECT name " |
|
| 2597 | " FROM tbl_energy_storage_containers_firecontrols " |
|
| 2598 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
|
| 2599 | if cursor.fetchone() is None: |
|
| 2600 | cursor.close() |
|
| 2601 | cnx.close() |
|
| 2602 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2603 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
|
| 2604 | ||
| 2605 | cursor.execute(" SELECT name, object_type " |
|
| 2606 | " FROM tbl_points " |
|
| 2607 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 2608 | row = cursor.fetchone() |
|
| 2609 | if row is None: |
|
| 2610 | cursor.close() |
|
| 2611 | cnx.close() |
|
| 2612 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2613 | description='API.POINT_NOT_FOUND') |
|
| 2614 | ||
| 2615 | query = (" SELECT id " |
|
| 2616 | " FROM tbl_energy_storage_containers_firecontrols_points " |
|
| 2617 | " WHERE firecontrol_id = %s AND point_id = %s") |
|
| 2618 | cursor.execute(query, (fid, new_values['data']['point_id'],)) |
|
| 2619 | if cursor.fetchone() is not None: |
|
| 2620 | cursor.close() |
|
| 2621 | cnx.close() |
|
| 2622 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2623 | description='API.FIRECONTROL_POINT_RELATION_EXISTS') |
|
| 2624 | ||
| 2625 | add_row = (" INSERT INTO tbl_energy_storage_containers_firecontrols_points (firecontrol_id, point_id) " |
|
| 2626 | " VALUES (%s, %s) ") |
|
| 2627 | cursor.execute(add_row, (fid, new_values['data']['point_id'],)) |
|
| 2628 | cnx.commit() |
|
| 2629 | cursor.close() |
|
| 2630 | cnx.close() |
|
| 2631 | ||
| 2632 | resp.status = falcon.HTTP_201 |
|
| 2633 | resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(fid) + '/points/' + \ |
|
| 2634 | str(new_values['data']['point_id']) |
|
| 2635 | ||
| 2636 | ||
| 2637 | class EnergyStorageContainerFirecontrolPointItem: |
|
| 2638 | def __init__(self): |
|
| @@ 2030-2149 (lines=120) @@ | ||
| 2027 | resp.status = falcon.HTTP_200 |
|
| 2028 | ||
| 2029 | ||
| 2030 | class EnergyStorageContainerDCDCPointCollection: |
|
| 2031 | def __init__(self): |
|
| 2032 | pass |
|
| 2033 | ||
| 2034 | @staticmethod |
|
| 2035 | def on_options(req, resp, id_, did): |
|
| 2036 | _ = req |
|
| 2037 | resp.status = falcon.HTTP_200 |
|
| 2038 | _ = id_ |
|
| 2039 | ||
| 2040 | @staticmethod |
|
| 2041 | def on_get(req, resp, id_, did): |
|
| 2042 | if 'API-KEY' not in req.headers or \ |
|
| 2043 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 2044 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 2045 | access_control(req) |
|
| 2046 | else: |
|
| 2047 | api_key_control(req) |
|
| 2048 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2049 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2050 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2051 | if not did.isdigit() or int(did) <= 0: |
|
| 2052 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2053 | description='API.INVALID_DCDC_ID') |
|
| 2054 | ||
| 2055 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2056 | cursor = cnx.cursor() |
|
| 2057 | ||
| 2058 | cursor.execute(" SELECT name " |
|
| 2059 | " FROM tbl_energy_storage_containers_dcdcs " |
|
| 2060 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did, )) |
|
| 2061 | if cursor.fetchone() is None: |
|
| 2062 | cursor.close() |
|
| 2063 | cnx.close() |
|
| 2064 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2065 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
|
| 2066 | ||
| 2067 | query = (" SELECT p.id, p.name, " |
|
| 2068 | " ds.id, ds.name, ds.uuid, " |
|
| 2069 | " p.address " |
|
| 2070 | " FROM tbl_points p, tbl_energy_storage_containers_dcdcs_points mp, tbl_data_sources ds " |
|
| 2071 | " WHERE mp.dcdc_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 2072 | " ORDER BY p.name ") |
|
| 2073 | cursor.execute(query, (did,)) |
|
| 2074 | rows = cursor.fetchall() |
|
| 2075 | ||
| 2076 | result = list() |
|
| 2077 | if rows is not None and len(rows) > 0: |
|
| 2078 | for row in rows: |
|
| 2079 | meta_result = {"id": row[0], "name": row[1], |
|
| 2080 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 2081 | "address": row[5]} |
|
| 2082 | result.append(meta_result) |
|
| 2083 | ||
| 2084 | resp.text = json.dumps(result) |
|
| 2085 | ||
| 2086 | @staticmethod |
|
| 2087 | @user_logger |
|
| 2088 | def on_post(req, resp, id_, did): |
|
| 2089 | """Handles POST requests""" |
|
| 2090 | admin_control(req) |
|
| 2091 | try: |
|
| 2092 | raw_json = req.stream.read().decode('utf-8') |
|
| 2093 | except Exception as ex: |
|
| 2094 | print(str(ex)) |
|
| 2095 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2096 | title='API.BAD_REQUEST', |
|
| 2097 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2098 | ||
| 2099 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2100 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2101 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2102 | if not did.isdigit() or int(did) <= 0: |
|
| 2103 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2104 | description='API.INVALID_DCDC_ID') |
|
| 2105 | ||
| 2106 | new_values = json.loads(raw_json) |
|
| 2107 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2108 | cursor = cnx.cursor() |
|
| 2109 | ||
| 2110 | cursor.execute(" SELECT name " |
|
| 2111 | " FROM tbl_energy_storage_containers_dcdcs " |
|
| 2112 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did,)) |
|
| 2113 | if cursor.fetchone() is None: |
|
| 2114 | cursor.close() |
|
| 2115 | cnx.close() |
|
| 2116 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2117 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
|
| 2118 | ||
| 2119 | cursor.execute(" SELECT name, object_type " |
|
| 2120 | " FROM tbl_points " |
|
| 2121 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 2122 | row = cursor.fetchone() |
|
| 2123 | if row is None: |
|
| 2124 | cursor.close() |
|
| 2125 | cnx.close() |
|
| 2126 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2127 | description='API.POINT_NOT_FOUND') |
|
| 2128 | ||
| 2129 | query = (" SELECT id " |
|
| 2130 | " FROM tbl_energy_storage_containers_dcdcs_points " |
|
| 2131 | " WHERE dcdc_id = %s AND point_id = %s") |
|
| 2132 | cursor.execute(query, (did, new_values['data']['point_id'],)) |
|
| 2133 | if cursor.fetchone() is not None: |
|
| 2134 | cursor.close() |
|
| 2135 | cnx.close() |
|
| 2136 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 2137 | description='API.DCDC_POINT_RELATION_EXISTS') |
|
| 2138 | ||
| 2139 | add_row = (" INSERT INTO tbl_energy_storage_containers_dcdcs_points (dcdc_id, point_id) " |
|
| 2140 | " VALUES (%s, %s) ") |
|
| 2141 | cursor.execute(add_row, (did, new_values['data']['point_id'],)) |
|
| 2142 | cnx.commit() |
|
| 2143 | cursor.close() |
|
| 2144 | cnx.close() |
|
| 2145 | ||
| 2146 | resp.status = falcon.HTTP_201 |
|
| 2147 | resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(did) + '/points/' + \ |
|
| 2148 | str(new_values['data']['point_id']) |
|
| 2149 | ||
| 2150 | ||
| 2151 | class EnergyStorageContainerDCDCPointItem: |
|
| 2152 | def __init__(self): |
|
| @@ 1141-1260 (lines=120) @@ | ||
| 1138 | resp.status = falcon.HTTP_200 |
|
| 1139 | ||
| 1140 | ||
| 1141 | class EnergyStorageContainerBatteryPointCollection: |
|
| 1142 | def __init__(self): |
|
| 1143 | pass |
|
| 1144 | ||
| 1145 | @staticmethod |
|
| 1146 | def on_options(req, resp, id_, bid): |
|
| 1147 | _ = req |
|
| 1148 | resp.status = falcon.HTTP_200 |
|
| 1149 | _ = id_ |
|
| 1150 | ||
| 1151 | @staticmethod |
|
| 1152 | def on_get(req, resp, id_, bid): |
|
| 1153 | if 'API-KEY' not in req.headers or \ |
|
| 1154 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 1155 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 1156 | access_control(req) |
|
| 1157 | else: |
|
| 1158 | api_key_control(req) |
|
| 1159 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1161 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1162 | if not bid.isdigit() or int(bid) <= 0: |
|
| 1163 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1164 | description='API.INVALID_BMS_ID') |
|
| 1165 | ||
| 1166 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1167 | cursor = cnx.cursor() |
|
| 1168 | ||
| 1169 | cursor.execute(" SELECT name " |
|
| 1170 | " FROM tbl_energy_storage_containers_batteries " |
|
| 1171 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid, )) |
|
| 1172 | if cursor.fetchone() is None: |
|
| 1173 | cursor.close() |
|
| 1174 | cnx.close() |
|
| 1175 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1176 | description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND') |
|
| 1177 | ||
| 1178 | query = (" SELECT p.id, p.name, " |
|
| 1179 | " ds.id, ds.name, ds.uuid, " |
|
| 1180 | " p.address " |
|
| 1181 | " FROM tbl_points p, tbl_energy_storage_containers_bmses_points mp, tbl_data_sources ds " |
|
| 1182 | " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 1183 | " ORDER BY p.name ") |
|
| 1184 | cursor.execute(query, (bid,)) |
|
| 1185 | rows = cursor.fetchall() |
|
| 1186 | ||
| 1187 | result = list() |
|
| 1188 | if rows is not None and len(rows) > 0: |
|
| 1189 | for row in rows: |
|
| 1190 | meta_result = {"id": row[0], "name": row[1], |
|
| 1191 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 1192 | "address": row[5]} |
|
| 1193 | result.append(meta_result) |
|
| 1194 | ||
| 1195 | resp.text = json.dumps(result) |
|
| 1196 | ||
| 1197 | @staticmethod |
|
| 1198 | @user_logger |
|
| 1199 | def on_post(req, resp, id_, bid): |
|
| 1200 | """Handles POST requests""" |
|
| 1201 | admin_control(req) |
|
| 1202 | try: |
|
| 1203 | raw_json = req.stream.read().decode('utf-8') |
|
| 1204 | except Exception as ex: |
|
| 1205 | print(str(ex)) |
|
| 1206 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1207 | title='API.BAD_REQUEST', |
|
| 1208 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1209 | ||
| 1210 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1211 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1212 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 1213 | if not bid.isdigit() or int(bid) <= 0: |
|
| 1214 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1215 | description='API.INVALID_BMS_ID') |
|
| 1216 | ||
| 1217 | new_values = json.loads(raw_json) |
|
| 1218 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1219 | cursor = cnx.cursor() |
|
| 1220 | ||
| 1221 | cursor.execute(" SELECT name " |
|
| 1222 | " FROM tbl_energy_storage_containers_batteries " |
|
| 1223 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid,)) |
|
| 1224 | if cursor.fetchone() is None: |
|
| 1225 | cursor.close() |
|
| 1226 | cnx.close() |
|
| 1227 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1228 | description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND') |
|
| 1229 | ||
| 1230 | cursor.execute(" SELECT name, object_type " |
|
| 1231 | " FROM tbl_points " |
|
| 1232 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 1233 | row = cursor.fetchone() |
|
| 1234 | if row is None: |
|
| 1235 | cursor.close() |
|
| 1236 | cnx.close() |
|
| 1237 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1238 | description='API.POINT_NOT_FOUND') |
|
| 1239 | ||
| 1240 | query = (" SELECT id " |
|
| 1241 | " FROM tbl_energy_storage_containers_bmses_points " |
|
| 1242 | " WHERE bms_id = %s AND point_id = %s") |
|
| 1243 | cursor.execute(query, (bid, new_values['data']['point_id'],)) |
|
| 1244 | if cursor.fetchone() is not None: |
|
| 1245 | cursor.close() |
|
| 1246 | cnx.close() |
|
| 1247 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1248 | description='API.BMS_POINT_RELATION_EXISTS') |
|
| 1249 | ||
| 1250 | add_row = (" INSERT INTO tbl_energy_storage_containers_bmses_points (bms_id, point_id) " |
|
| 1251 | " VALUES (%s, %s) ") |
|
| 1252 | cursor.execute(add_row, (bid, new_values['data']['point_id'],)) |
|
| 1253 | cnx.commit() |
|
| 1254 | cursor.close() |
|
| 1255 | cnx.close() |
|
| 1256 | ||
| 1257 | resp.status = falcon.HTTP_201 |
|
| 1258 | resp.location = '/energystoragecontainers/' + str(id_) + '/batteries/' + str(bid) + '/points/' + \ |
|
| 1259 | str(new_values['data']['point_id']) |
|
| 1260 | ||
| 1261 | ||
| 1262 | class EnergyStorageContainerBatteryPointItem: |
|
| 1263 | def __init__(self): |
|
| @@ 7134-7252 (lines=119) @@ | ||
| 7131 | ||
| 7132 | resp.status = falcon.HTTP_204 |
|
| 7133 | ||
| 7134 | class MicrogridPowerConversionSystemPointCollection: |
|
| 7135 | def __init__(self): |
|
| 7136 | pass |
|
| 7137 | ||
| 7138 | @staticmethod |
|
| 7139 | def on_options(req, resp, id_, pcsid): |
|
| 7140 | _ = req |
|
| 7141 | resp.status = falcon.HTTP_200 |
|
| 7142 | _ = id_ |
|
| 7143 | _ = pcsid |
|
| 7144 | ||
| 7145 | @staticmethod |
|
| 7146 | def on_get(req, resp, id_, pcsid): |
|
| 7147 | if 'API-KEY' not in req.headers or \ |
|
| 7148 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 7149 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 7150 | access_control(req) |
|
| 7151 | else: |
|
| 7152 | api_key_control(req) |
|
| 7153 | if not id_.isdigit() or int(id_) <= 0: |
|
| 7154 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7155 | description='API.INVALID_MICROGRID_ID') |
|
| 7156 | if not pcsid.isdigit() or int(pcsid) <= 0: |
|
| 7157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7158 | description='API.INVALID_POWER_CONVERSION_SYSTEM_ID') |
|
| 7159 | ||
| 7160 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 7161 | cursor = cnx.cursor() |
|
| 7162 | ||
| 7163 | cursor.execute(" SELECT name " |
|
| 7164 | " FROM tbl_microgrids_power_conversion_systems " |
|
| 7165 | " WHERE microgrid_id = %s AND id = %s ", (id_, pcsid,)) |
|
| 7166 | if cursor.fetchone() is None: |
|
| 7167 | cursor.close() |
|
| 7168 | cnx.close() |
|
| 7169 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 7170 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
|
| 7171 | ||
| 7172 | query = (" SELECT p.id, p.name, " |
|
| 7173 | " ds.id, ds.name, ds.uuid, " |
|
| 7174 | " p.address " |
|
| 7175 | " FROM tbl_points p, tbl_microgrids_pcses_points mp, tbl_data_sources ds " |
|
| 7176 | " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 7177 | " ORDER BY p.name ") |
|
| 7178 | cursor.execute(query, (pcsid,)) |
|
| 7179 | rows = cursor.fetchall() |
|
| 7180 | ||
| 7181 | result = list() |
|
| 7182 | if rows is not None and len(rows) > 0: |
|
| 7183 | for row in rows: |
|
| 7184 | meta_result = {"id": row[0], "name": row[1], |
|
| 7185 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 7186 | "address": row[5]} |
|
| 7187 | result.append(meta_result) |
|
| 7188 | ||
| 7189 | resp.text = json.dumps(result) |
|
| 7190 | ||
| 7191 | @staticmethod |
|
| 7192 | @user_logger |
|
| 7193 | def on_post(req, resp, id_, pcsid): |
|
| 7194 | admin_control(req) |
|
| 7195 | try: |
|
| 7196 | raw_json = req.stream.read().decode('utf-8') |
|
| 7197 | except Exception as ex: |
|
| 7198 | print(str(ex)) |
|
| 7199 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 7200 | title='API.BAD_REQUEST', |
|
| 7201 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 7202 | ||
| 7203 | if not id_.isdigit() or int(id_) <= 0: |
|
| 7204 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7205 | description='API.INVALID_MICROGRID_ID') |
|
| 7206 | if not pcsid.isdigit() or int(pcsid) <= 0: |
|
| 7207 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7208 | description='API.INVALID_POWER_CONVERSION_SYSTEM_ID') |
|
| 7209 | ||
| 7210 | new_values = json.loads(raw_json) |
|
| 7211 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 7212 | cursor = cnx.cursor() |
|
| 7213 | ||
| 7214 | cursor.execute(" SELECT name " |
|
| 7215 | " FROM tbl_microgrids_power_conversion_systems " |
|
| 7216 | " WHERE microgrid_id = %s AND id = %s ", (id_, pcsid,)) |
|
| 7217 | if cursor.fetchone() is None: |
|
| 7218 | cursor.close() |
|
| 7219 | cnx.close() |
|
| 7220 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 7221 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
|
| 7222 | ||
| 7223 | cursor.execute(" SELECT name, object_type " |
|
| 7224 | " FROM tbl_points " |
|
| 7225 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 7226 | row = cursor.fetchone() |
|
| 7227 | if row is None: |
|
| 7228 | cursor.close() |
|
| 7229 | cnx.close() |
|
| 7230 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 7231 | description='API.POINT_NOT_FOUND') |
|
| 7232 | ||
| 7233 | query = (" SELECT id " |
|
| 7234 | " FROM tbl_microgrids_pcses_points " |
|
| 7235 | " WHERE pcs_id = %s AND point_id = %s") |
|
| 7236 | cursor.execute(query, (pcsid, new_values['data']['point_id'],)) |
|
| 7237 | if cursor.fetchone() is not None: |
|
| 7238 | cursor.close() |
|
| 7239 | cnx.close() |
|
| 7240 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 7241 | description='API.POWER_CONVERSION_SYSTEM_POINT_RELATION_EXISTS') |
|
| 7242 | ||
| 7243 | add_row = (" INSERT INTO tbl_microgrids_pcses_points (pcs_id, point_id) " |
|
| 7244 | " VALUES (%s, %s) ") |
|
| 7245 | cursor.execute(add_row, (pcsid, new_values['data']['point_id'],)) |
|
| 7246 | cnx.commit() |
|
| 7247 | cursor.close() |
|
| 7248 | cnx.close() |
|
| 7249 | ||
| 7250 | resp.status = falcon.HTTP_201 |
|
| 7251 | resp.location = '/microgrids/' + str(id_) + '/powerconversionsystems/' + str(pcsid) + '/points/' + \ |
|
| 7252 | str(new_values['data']['point_id']) |
|
| 7253 | ||
| 7254 | ||
| 7255 | class MicrogridPowerConversionSystemPointItem: |
|
| @@ 6948-7066 (lines=119) @@ | ||
| 6945 | ||
| 6946 | resp.status = falcon.HTTP_204 |
|
| 6947 | ||
| 6948 | class MicrogridPhotovoltaicPointCollection: |
|
| 6949 | def __init__(self): |
|
| 6950 | pass |
|
| 6951 | ||
| 6952 | @staticmethod |
|
| 6953 | def on_options(req, resp, id_, pvid): |
|
| 6954 | _ = req |
|
| 6955 | resp.status = falcon.HTTP_200 |
|
| 6956 | _ = id_ |
|
| 6957 | _ = pvid |
|
| 6958 | ||
| 6959 | @staticmethod |
|
| 6960 | def on_get(req, resp, id_, pvid): |
|
| 6961 | if 'API-KEY' not in req.headers or \ |
|
| 6962 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6963 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6964 | access_control(req) |
|
| 6965 | else: |
|
| 6966 | api_key_control(req) |
|
| 6967 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6968 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6969 | description='API.INVALID_MICROGRID_ID') |
|
| 6970 | if not pvid.isdigit() or int(pvid) <= 0: |
|
| 6971 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6972 | description='API.INVALID_PHOTOVOLTAIC_ID') |
|
| 6973 | ||
| 6974 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6975 | cursor = cnx.cursor() |
|
| 6976 | ||
| 6977 | cursor.execute(" SELECT name " |
|
| 6978 | " FROM tbl_microgrids_photovoltaics " |
|
| 6979 | " WHERE microgrid_id = %s AND id = %s ", (id_, pvid,)) |
|
| 6980 | if cursor.fetchone() is None: |
|
| 6981 | cursor.close() |
|
| 6982 | cnx.close() |
|
| 6983 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6984 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
|
| 6985 | ||
| 6986 | query = (" SELECT p.id, p.name, " |
|
| 6987 | " ds.id, ds.name, ds.uuid, " |
|
| 6988 | " p.address " |
|
| 6989 | " FROM tbl_points p, tbl_microgrids_pvs_points mp, tbl_data_sources ds " |
|
| 6990 | " WHERE mp.pv_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6991 | " ORDER BY p.name ") |
|
| 6992 | cursor.execute(query, (pvid,)) |
|
| 6993 | rows = cursor.fetchall() |
|
| 6994 | ||
| 6995 | result = list() |
|
| 6996 | if rows is not None and len(rows) > 0: |
|
| 6997 | for row in rows: |
|
| 6998 | meta_result = {"id": row[0], "name": row[1], |
|
| 6999 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 7000 | "address": row[5]} |
|
| 7001 | result.append(meta_result) |
|
| 7002 | ||
| 7003 | resp.text = json.dumps(result) |
|
| 7004 | ||
| 7005 | @staticmethod |
|
| 7006 | @user_logger |
|
| 7007 | def on_post(req, resp, id_, pvid): |
|
| 7008 | admin_control(req) |
|
| 7009 | try: |
|
| 7010 | raw_json = req.stream.read().decode('utf-8') |
|
| 7011 | except Exception as ex: |
|
| 7012 | print(str(ex)) |
|
| 7013 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 7014 | title='API.BAD_REQUEST', |
|
| 7015 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 7016 | ||
| 7017 | if not id_.isdigit() or int(id_) <= 0: |
|
| 7018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7019 | description='API.INVALID_MICROGRID_ID') |
|
| 7020 | if not pvid.isdigit() or int(pvid) <= 0: |
|
| 7021 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 7022 | description='API.INVALID_PHOTOVOLTAIC_ID') |
|
| 7023 | ||
| 7024 | new_values = json.loads(raw_json) |
|
| 7025 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 7026 | cursor = cnx.cursor() |
|
| 7027 | ||
| 7028 | cursor.execute(" SELECT name " |
|
| 7029 | " FROM tbl_microgrids_photovoltaics " |
|
| 7030 | " WHERE microgrid_id = %s AND id = %s ", (id_, pvid,)) |
|
| 7031 | if cursor.fetchone() is None: |
|
| 7032 | cursor.close() |
|
| 7033 | cnx.close() |
|
| 7034 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 7035 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
|
| 7036 | ||
| 7037 | cursor.execute(" SELECT name, object_type " |
|
| 7038 | " FROM tbl_points " |
|
| 7039 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 7040 | row = cursor.fetchone() |
|
| 7041 | if row is None: |
|
| 7042 | cursor.close() |
|
| 7043 | cnx.close() |
|
| 7044 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 7045 | description='API.POINT_NOT_FOUND') |
|
| 7046 | ||
| 7047 | query = (" SELECT id " |
|
| 7048 | " FROM tbl_microgrids_pvs_points " |
|
| 7049 | " WHERE pv_id = %s AND point_id = %s") |
|
| 7050 | cursor.execute(query, (pvid, new_values['data']['point_id'],)) |
|
| 7051 | if cursor.fetchone() is not None: |
|
| 7052 | cursor.close() |
|
| 7053 | cnx.close() |
|
| 7054 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 7055 | description='API.PHOTOVOLTAIC_POINT_RELATION_EXISTS') |
|
| 7056 | ||
| 7057 | add_row = (" INSERT INTO tbl_microgrids_pvs_points (pv_id, point_id) " |
|
| 7058 | " VALUES (%s, %s) ") |
|
| 7059 | cursor.execute(add_row, (pvid, new_values['data']['point_id'],)) |
|
| 7060 | cnx.commit() |
|
| 7061 | cursor.close() |
|
| 7062 | cnx.close() |
|
| 7063 | ||
| 7064 | resp.status = falcon.HTTP_201 |
|
| 7065 | resp.location = '/microgrids/' + str(id_) + '/photovoltaics/' + str(pvid) + '/points/' + \ |
|
| 7066 | str(new_values['data']['point_id']) |
|
| 7067 | ||
| 7068 | ||
| 7069 | class MicrogridPhotovoltaicPointItem: |
|
| @@ 6762-6880 (lines=119) @@ | ||
| 6759 | ||
| 6760 | resp.status = falcon.HTTP_204 |
|
| 6761 | ||
| 6762 | class MicrogridLoadPointCollection: |
|
| 6763 | def __init__(self): |
|
| 6764 | pass |
|
| 6765 | ||
| 6766 | @staticmethod |
|
| 6767 | def on_options(req, resp, id_, lid): |
|
| 6768 | _ = req |
|
| 6769 | resp.status = falcon.HTTP_200 |
|
| 6770 | _ = id_ |
|
| 6771 | _ = lid |
|
| 6772 | ||
| 6773 | @staticmethod |
|
| 6774 | def on_get(req, resp, id_, lid): |
|
| 6775 | if 'API-KEY' not in req.headers or \ |
|
| 6776 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6777 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6778 | access_control(req) |
|
| 6779 | else: |
|
| 6780 | api_key_control(req) |
|
| 6781 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6783 | description='API.INVALID_MICROGRID_ID') |
|
| 6784 | if not lid.isdigit() or int(lid) <= 0: |
|
| 6785 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6786 | description='API.INVALID_LOAD_ID') |
|
| 6787 | ||
| 6788 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6789 | cursor = cnx.cursor() |
|
| 6790 | ||
| 6791 | cursor.execute(" SELECT name " |
|
| 6792 | " FROM tbl_microgrids_loads " |
|
| 6793 | " WHERE microgrid_id = %s AND id = %s ", (id_, lid,)) |
|
| 6794 | if cursor.fetchone() is None: |
|
| 6795 | cursor.close() |
|
| 6796 | cnx.close() |
|
| 6797 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6798 | description='API.MICROGRID_LOAD_NOT_FOUND') |
|
| 6799 | ||
| 6800 | query = (" SELECT p.id, p.name, " |
|
| 6801 | " ds.id, ds.name, ds.uuid, " |
|
| 6802 | " p.address " |
|
| 6803 | " FROM tbl_points p, tbl_microgrids_loads_points mp, tbl_data_sources ds " |
|
| 6804 | " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6805 | " ORDER BY p.name ") |
|
| 6806 | cursor.execute(query, (lid,)) |
|
| 6807 | rows = cursor.fetchall() |
|
| 6808 | ||
| 6809 | result = list() |
|
| 6810 | if rows is not None and len(rows) > 0: |
|
| 6811 | for row in rows: |
|
| 6812 | meta_result = {"id": row[0], "name": row[1], |
|
| 6813 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 6814 | "address": row[5]} |
|
| 6815 | result.append(meta_result) |
|
| 6816 | ||
| 6817 | resp.text = json.dumps(result) |
|
| 6818 | ||
| 6819 | @staticmethod |
|
| 6820 | @user_logger |
|
| 6821 | def on_post(req, resp, id_, lid): |
|
| 6822 | admin_control(req) |
|
| 6823 | try: |
|
| 6824 | raw_json = req.stream.read().decode('utf-8') |
|
| 6825 | except Exception as ex: |
|
| 6826 | print(str(ex)) |
|
| 6827 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 6828 | title='API.BAD_REQUEST', |
|
| 6829 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 6830 | ||
| 6831 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6832 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6833 | description='API.INVALID_MICROGRID_ID') |
|
| 6834 | if not lid.isdigit() or int(lid) <= 0: |
|
| 6835 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6836 | description='API.INVALID_LOAD_ID') |
|
| 6837 | ||
| 6838 | new_values = json.loads(raw_json) |
|
| 6839 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6840 | cursor = cnx.cursor() |
|
| 6841 | ||
| 6842 | cursor.execute(" SELECT name " |
|
| 6843 | " FROM tbl_microgrids_loads " |
|
| 6844 | " WHERE microgrid_id = %s AND id = %s ", (id_, lid,)) |
|
| 6845 | if cursor.fetchone() is None: |
|
| 6846 | cursor.close() |
|
| 6847 | cnx.close() |
|
| 6848 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6849 | description='API.MICROGRID_LOAD_NOT_FOUND') |
|
| 6850 | ||
| 6851 | cursor.execute(" SELECT name, object_type " |
|
| 6852 | " FROM tbl_points " |
|
| 6853 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 6854 | row = cursor.fetchone() |
|
| 6855 | if row is None: |
|
| 6856 | cursor.close() |
|
| 6857 | cnx.close() |
|
| 6858 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6859 | description='API.POINT_NOT_FOUND') |
|
| 6860 | ||
| 6861 | query = (" SELECT id " |
|
| 6862 | " FROM tbl_microgrids_loads_points " |
|
| 6863 | " WHERE load_id = %s AND point_id = %s") |
|
| 6864 | cursor.execute(query, (lid, new_values['data']['point_id'],)) |
|
| 6865 | if cursor.fetchone() is not None: |
|
| 6866 | cursor.close() |
|
| 6867 | cnx.close() |
|
| 6868 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 6869 | description='API.LOAD_POINT_RELATION_EXISTS') |
|
| 6870 | ||
| 6871 | add_row = (" INSERT INTO tbl_microgrids_loads_points (load_id, point_id) " |
|
| 6872 | " VALUES (%s, %s) ") |
|
| 6873 | cursor.execute(add_row, (lid, new_values['data']['point_id'],)) |
|
| 6874 | cnx.commit() |
|
| 6875 | cursor.close() |
|
| 6876 | cnx.close() |
|
| 6877 | ||
| 6878 | resp.status = falcon.HTTP_201 |
|
| 6879 | resp.location = '/microgrids/' + str(id_) + '/loads/' + str(lid) + '/points/' + \ |
|
| 6880 | str(new_values['data']['point_id']) |
|
| 6881 | ||
| 6882 | ||
| 6883 | class MicrogridLoadPointItem: |
|
| @@ 6576-6694 (lines=119) @@ | ||
| 6573 | ||
| 6574 | resp.status = falcon.HTTP_204 |
|
| 6575 | ||
| 6576 | class MicrogridHeatPumpPointCollection: |
|
| 6577 | def __init__(self): |
|
| 6578 | pass |
|
| 6579 | ||
| 6580 | @staticmethod |
|
| 6581 | def on_options(req, resp, id_, hid): |
|
| 6582 | _ = req |
|
| 6583 | resp.status = falcon.HTTP_200 |
|
| 6584 | _ = id_ |
|
| 6585 | _ = hid |
|
| 6586 | ||
| 6587 | @staticmethod |
|
| 6588 | def on_get(req, resp, id_, hid): |
|
| 6589 | if 'API-KEY' not in req.headers or \ |
|
| 6590 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6591 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6592 | access_control(req) |
|
| 6593 | else: |
|
| 6594 | api_key_control(req) |
|
| 6595 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6596 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6597 | description='API.INVALID_MICROGRID_ID') |
|
| 6598 | if not hid.isdigit() or int(hid) <= 0: |
|
| 6599 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6600 | description='API.INVALID_HEATPUMP_ID') |
|
| 6601 | ||
| 6602 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6603 | cursor = cnx.cursor() |
|
| 6604 | ||
| 6605 | cursor.execute(" SELECT name " |
|
| 6606 | " FROM tbl_microgrids_heatpumps " |
|
| 6607 | " WHERE microgrid_id = %s AND id = %s ", (id_, hid,)) |
|
| 6608 | if cursor.fetchone() is None: |
|
| 6609 | cursor.close() |
|
| 6610 | cnx.close() |
|
| 6611 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6612 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
|
| 6613 | ||
| 6614 | query = (" SELECT p.id, p.name, " |
|
| 6615 | " ds.id, ds.name, ds.uuid, " |
|
| 6616 | " p.address " |
|
| 6617 | " FROM tbl_points p, tbl_microgrids_heatpumps_points mp, tbl_data_sources ds " |
|
| 6618 | " WHERE mp.heatpump_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6619 | " ORDER BY p.name ") |
|
| 6620 | cursor.execute(query, (hid,)) |
|
| 6621 | rows = cursor.fetchall() |
|
| 6622 | ||
| 6623 | result = list() |
|
| 6624 | if rows is not None and len(rows) > 0: |
|
| 6625 | for row in rows: |
|
| 6626 | meta_result = {"id": row[0], "name": row[1], |
|
| 6627 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 6628 | "address": row[5]} |
|
| 6629 | result.append(meta_result) |
|
| 6630 | ||
| 6631 | resp.text = json.dumps(result) |
|
| 6632 | ||
| 6633 | @staticmethod |
|
| 6634 | @user_logger |
|
| 6635 | def on_post(req, resp, id_, hid): |
|
| 6636 | admin_control(req) |
|
| 6637 | try: |
|
| 6638 | raw_json = req.stream.read().decode('utf-8') |
|
| 6639 | except Exception as ex: |
|
| 6640 | print(str(ex)) |
|
| 6641 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 6642 | title='API.BAD_REQUEST', |
|
| 6643 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 6644 | ||
| 6645 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6646 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6647 | description='API.INVALID_MICROGRID_ID') |
|
| 6648 | if not hid.isdigit() or int(hid) <= 0: |
|
| 6649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6650 | description='API.INVALID_HEATPUMP_ID') |
|
| 6651 | ||
| 6652 | new_values = json.loads(raw_json) |
|
| 6653 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6654 | cursor = cnx.cursor() |
|
| 6655 | ||
| 6656 | cursor.execute(" SELECT name " |
|
| 6657 | " FROM tbl_microgrids_heatpumps " |
|
| 6658 | " WHERE microgrid_id = %s AND id = %s ", (id_, hid,)) |
|
| 6659 | if cursor.fetchone() is None: |
|
| 6660 | cursor.close() |
|
| 6661 | cnx.close() |
|
| 6662 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6663 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
|
| 6664 | ||
| 6665 | cursor.execute(" SELECT name, object_type " |
|
| 6666 | " FROM tbl_points " |
|
| 6667 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 6668 | row = cursor.fetchone() |
|
| 6669 | if row is None: |
|
| 6670 | cursor.close() |
|
| 6671 | cnx.close() |
|
| 6672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6673 | description='API.POINT_NOT_FOUND') |
|
| 6674 | ||
| 6675 | query = (" SELECT id " |
|
| 6676 | " FROM tbl_microgrids_heatpumps_points " |
|
| 6677 | " WHERE heatpump_id = %s AND point_id = %s") |
|
| 6678 | cursor.execute(query, (hid, new_values['data']['point_id'],)) |
|
| 6679 | if cursor.fetchone() is not None: |
|
| 6680 | cursor.close() |
|
| 6681 | cnx.close() |
|
| 6682 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 6683 | description='API.HEATPUMP_POINT_RELATION_EXISTS') |
|
| 6684 | ||
| 6685 | add_row = (" INSERT INTO tbl_microgrids_heatpumps_points (heatpump_id, point_id) " |
|
| 6686 | " VALUES (%s, %s) ") |
|
| 6687 | cursor.execute(add_row, (hid, new_values['data']['point_id'],)) |
|
| 6688 | cnx.commit() |
|
| 6689 | cursor.close() |
|
| 6690 | cnx.close() |
|
| 6691 | ||
| 6692 | resp.status = falcon.HTTP_201 |
|
| 6693 | resp.location = '/microgrids/' + str(id_) + '/heatpumps/' + str(hid) + '/points/' + \ |
|
| 6694 | str(new_values['data']['point_id']) |
|
| 6695 | ||
| 6696 | ||
| 6697 | class MicrogridHeatPumpPointItem: |
|
| @@ 6390-6508 (lines=119) @@ | ||
| 6387 | ||
| 6388 | resp.status = falcon.HTTP_204 |
|
| 6389 | ||
| 6390 | class MicrogridGridPointCollection: |
|
| 6391 | def __init__(self): |
|
| 6392 | pass |
|
| 6393 | ||
| 6394 | @staticmethod |
|
| 6395 | def on_options(req, resp, id_, gid): |
|
| 6396 | _ = req |
|
| 6397 | resp.status = falcon.HTTP_200 |
|
| 6398 | _ = id_ |
|
| 6399 | _ = gid |
|
| 6400 | ||
| 6401 | @staticmethod |
|
| 6402 | def on_get(req, resp, id_, gid): |
|
| 6403 | if 'API-KEY' not in req.headers or \ |
|
| 6404 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6405 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6406 | access_control(req) |
|
| 6407 | else: |
|
| 6408 | api_key_control(req) |
|
| 6409 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6410 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6411 | description='API.INVALID_MICROGRID_ID') |
|
| 6412 | if not gid.isdigit() or int(gid) <= 0: |
|
| 6413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6414 | description='API.INVALID_GRID_ID') |
|
| 6415 | ||
| 6416 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6417 | cursor = cnx.cursor() |
|
| 6418 | ||
| 6419 | cursor.execute(" SELECT name " |
|
| 6420 | " FROM tbl_microgrids_grids " |
|
| 6421 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
|
| 6422 | if cursor.fetchone() is None: |
|
| 6423 | cursor.close() |
|
| 6424 | cnx.close() |
|
| 6425 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6426 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
| 6427 | ||
| 6428 | query = (" SELECT p.id, p.name, " |
|
| 6429 | " ds.id, ds.name, ds.uuid, " |
|
| 6430 | " p.address " |
|
| 6431 | " FROM tbl_points p, tbl_microgrids_grids_points mp, tbl_data_sources ds " |
|
| 6432 | " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6433 | " ORDER BY p.name ") |
|
| 6434 | cursor.execute(query, (gid,)) |
|
| 6435 | rows = cursor.fetchall() |
|
| 6436 | ||
| 6437 | result = list() |
|
| 6438 | if rows is not None and len(rows) > 0: |
|
| 6439 | for row in rows: |
|
| 6440 | meta_result = {"id": row[0], "name": row[1], |
|
| 6441 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 6442 | "address": row[5]} |
|
| 6443 | result.append(meta_result) |
|
| 6444 | ||
| 6445 | resp.text = json.dumps(result) |
|
| 6446 | ||
| 6447 | @staticmethod |
|
| 6448 | @user_logger |
|
| 6449 | def on_post(req, resp, id_, gid): |
|
| 6450 | admin_control(req) |
|
| 6451 | try: |
|
| 6452 | raw_json = req.stream.read().decode('utf-8') |
|
| 6453 | except Exception as ex: |
|
| 6454 | print(str(ex)) |
|
| 6455 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 6456 | title='API.BAD_REQUEST', |
|
| 6457 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 6458 | ||
| 6459 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6460 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6461 | description='API.INVALID_MICROGRID_ID') |
|
| 6462 | if not gid.isdigit() or int(gid) <= 0: |
|
| 6463 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6464 | description='API.INVALID_GRID_ID') |
|
| 6465 | ||
| 6466 | new_values = json.loads(raw_json) |
|
| 6467 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6468 | cursor = cnx.cursor() |
|
| 6469 | ||
| 6470 | cursor.execute(" SELECT name " |
|
| 6471 | " FROM tbl_microgrids_grids " |
|
| 6472 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
|
| 6473 | if cursor.fetchone() is None: |
|
| 6474 | cursor.close() |
|
| 6475 | cnx.close() |
|
| 6476 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6477 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
| 6478 | ||
| 6479 | cursor.execute(" SELECT name, object_type " |
|
| 6480 | " FROM tbl_points " |
|
| 6481 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 6482 | row = cursor.fetchone() |
|
| 6483 | if row is None: |
|
| 6484 | cursor.close() |
|
| 6485 | cnx.close() |
|
| 6486 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6487 | description='API.POINT_NOT_FOUND') |
|
| 6488 | ||
| 6489 | query = (" SELECT id " |
|
| 6490 | " FROM tbl_microgrids_grids_points " |
|
| 6491 | " WHERE grid_id = %s AND point_id = %s") |
|
| 6492 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
|
| 6493 | if cursor.fetchone() is not None: |
|
| 6494 | cursor.close() |
|
| 6495 | cnx.close() |
|
| 6496 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 6497 | description='API.GRID_POINT_RELATION_EXISTS') |
|
| 6498 | ||
| 6499 | add_row = (" INSERT INTO tbl_microgrids_grids_points (grid_id, point_id) " |
|
| 6500 | " VALUES (%s, %s) ") |
|
| 6501 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
|
| 6502 | cnx.commit() |
|
| 6503 | cursor.close() |
|
| 6504 | cnx.close() |
|
| 6505 | ||
| 6506 | resp.status = falcon.HTTP_201 |
|
| 6507 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(gid) + '/points/' + \ |
|
| 6508 | str(new_values['data']['point_id']) |
|
| 6509 | ||
| 6510 | ||
| 6511 | class MicrogridGridPointItem: |
|
| @@ 6204-6322 (lines=119) @@ | ||
| 6201 | ||
| 6202 | resp.status = falcon.HTTP_204 |
|
| 6203 | ||
| 6204 | class MicrogridGeneratorPointCollection: |
|
| 6205 | def __init__(self): |
|
| 6206 | pass |
|
| 6207 | ||
| 6208 | @staticmethod |
|
| 6209 | def on_options(req, resp, id_, gid): |
|
| 6210 | _ = req |
|
| 6211 | resp.status = falcon.HTTP_200 |
|
| 6212 | _ = id_ |
|
| 6213 | _ = gid |
|
| 6214 | ||
| 6215 | @staticmethod |
|
| 6216 | def on_get(req, resp, id_, gid): |
|
| 6217 | if 'API-KEY' not in req.headers or \ |
|
| 6218 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6219 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6220 | access_control(req) |
|
| 6221 | else: |
|
| 6222 | api_key_control(req) |
|
| 6223 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6224 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6225 | description='API.INVALID_MICROGRID_ID') |
|
| 6226 | if not gid.isdigit() or int(gid) <= 0: |
|
| 6227 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6228 | description='API.INVALID_GENERATOR_ID') |
|
| 6229 | ||
| 6230 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6231 | cursor = cnx.cursor() |
|
| 6232 | ||
| 6233 | cursor.execute(" SELECT name " |
|
| 6234 | " FROM tbl_microgrids_generators " |
|
| 6235 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
|
| 6236 | if cursor.fetchone() is None: |
|
| 6237 | cursor.close() |
|
| 6238 | cnx.close() |
|
| 6239 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6240 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
|
| 6241 | ||
| 6242 | query = (" SELECT p.id, p.name, " |
|
| 6243 | " ds.id, ds.name, ds.uuid, " |
|
| 6244 | " p.address " |
|
| 6245 | " FROM tbl_points p, tbl_microgrids_generators_points mp, tbl_data_sources ds " |
|
| 6246 | " WHERE mp.generator_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6247 | " ORDER BY p.name ") |
|
| 6248 | cursor.execute(query, (gid,)) |
|
| 6249 | rows = cursor.fetchall() |
|
| 6250 | ||
| 6251 | result = list() |
|
| 6252 | if rows is not None and len(rows) > 0: |
|
| 6253 | for row in rows: |
|
| 6254 | meta_result = {"id": row[0], "name": row[1], |
|
| 6255 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 6256 | "address": row[5]} |
|
| 6257 | result.append(meta_result) |
|
| 6258 | ||
| 6259 | resp.text = json.dumps(result) |
|
| 6260 | ||
| 6261 | @staticmethod |
|
| 6262 | @user_logger |
|
| 6263 | def on_post(req, resp, id_, gid): |
|
| 6264 | admin_control(req) |
|
| 6265 | try: |
|
| 6266 | raw_json = req.stream.read().decode('utf-8') |
|
| 6267 | except Exception as ex: |
|
| 6268 | print(str(ex)) |
|
| 6269 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 6270 | title='API.BAD_REQUEST', |
|
| 6271 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 6272 | ||
| 6273 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6274 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6275 | description='API.INVALID_MICROGRID_ID') |
|
| 6276 | if not gid.isdigit() or int(gid) <= 0: |
|
| 6277 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6278 | description='API.INVALID_GENERATOR_ID') |
|
| 6279 | ||
| 6280 | new_values = json.loads(raw_json) |
|
| 6281 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6282 | cursor = cnx.cursor() |
|
| 6283 | ||
| 6284 | cursor.execute(" SELECT name " |
|
| 6285 | " FROM tbl_microgrids_generators " |
|
| 6286 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
|
| 6287 | if cursor.fetchone() is None: |
|
| 6288 | cursor.close() |
|
| 6289 | cnx.close() |
|
| 6290 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6291 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
|
| 6292 | ||
| 6293 | cursor.execute(" SELECT name, object_type " |
|
| 6294 | " FROM tbl_points " |
|
| 6295 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 6296 | row = cursor.fetchone() |
|
| 6297 | if row is None: |
|
| 6298 | cursor.close() |
|
| 6299 | cnx.close() |
|
| 6300 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6301 | description='API.POINT_NOT_FOUND') |
|
| 6302 | ||
| 6303 | query = (" SELECT id " |
|
| 6304 | " FROM tbl_microgrids_generators_points " |
|
| 6305 | " WHERE generator_id = %s AND point_id = %s") |
|
| 6306 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
|
| 6307 | if cursor.fetchone() is not None: |
|
| 6308 | cursor.close() |
|
| 6309 | cnx.close() |
|
| 6310 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 6311 | description='API.GENERATOR_POINT_RELATION_EXISTS') |
|
| 6312 | ||
| 6313 | add_row = (" INSERT INTO tbl_microgrids_generators_points (generator_id, point_id) " |
|
| 6314 | " VALUES (%s, %s) ") |
|
| 6315 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
|
| 6316 | cnx.commit() |
|
| 6317 | cursor.close() |
|
| 6318 | cnx.close() |
|
| 6319 | ||
| 6320 | resp.status = falcon.HTTP_201 |
|
| 6321 | resp.location = '/microgrids/' + str(id_) + '/generators/' + str(gid) + '/points/' + \ |
|
| 6322 | str(new_values['data']['point_id']) |
|
| 6323 | ||
| 6324 | ||
| 6325 | class MicrogridGeneratorPointItem: |
|
| @@ 6018-6136 (lines=119) @@ | ||
| 6015 | ||
| 6016 | resp.status = falcon.HTTP_204 |
|
| 6017 | ||
| 6018 | class MicrogridEVChargerPointCollection: |
|
| 6019 | def __init__(self): |
|
| 6020 | pass |
|
| 6021 | ||
| 6022 | @staticmethod |
|
| 6023 | def on_options(req, resp, id_, eid): |
|
| 6024 | _ = req |
|
| 6025 | resp.status = falcon.HTTP_200 |
|
| 6026 | _ = id_ |
|
| 6027 | _ = eid |
|
| 6028 | ||
| 6029 | @staticmethod |
|
| 6030 | def on_get(req, resp, id_, eid): |
|
| 6031 | if 'API-KEY' not in req.headers or \ |
|
| 6032 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 6033 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 6034 | access_control(req) |
|
| 6035 | else: |
|
| 6036 | api_key_control(req) |
|
| 6037 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6038 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6039 | description='API.INVALID_MICROGRID_ID') |
|
| 6040 | if not eid.isdigit() or int(eid) <= 0: |
|
| 6041 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6042 | description='API.INVALID_EVCHARGER_ID') |
|
| 6043 | ||
| 6044 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6045 | cursor = cnx.cursor() |
|
| 6046 | ||
| 6047 | cursor.execute(" SELECT name " |
|
| 6048 | " FROM tbl_microgrids_evchargers " |
|
| 6049 | " WHERE microgrid_id = %s AND id = %s ", (id_, eid,)) |
|
| 6050 | if cursor.fetchone() is None: |
|
| 6051 | cursor.close() |
|
| 6052 | cnx.close() |
|
| 6053 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6054 | description='API.MICROGRID_EV_CHARGER_NOT_FOUND') |
|
| 6055 | ||
| 6056 | query = (" SELECT p.id, p.name, " |
|
| 6057 | " ds.id, ds.name, ds.uuid, " |
|
| 6058 | " p.address " |
|
| 6059 | " FROM tbl_points p, tbl_microgrids_evchargers_points mp, tbl_data_sources ds " |
|
| 6060 | " WHERE mp.evcharger_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 6061 | " ORDER BY p.name ") |
|
| 6062 | cursor.execute(query, (eid,)) |
|
| 6063 | rows = cursor.fetchall() |
|
| 6064 | ||
| 6065 | result = list() |
|
| 6066 | if rows is not None and len(rows) > 0: |
|
| 6067 | for row in rows: |
|
| 6068 | meta_result = {"id": row[0], "name": row[1], |
|
| 6069 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 6070 | "address": row[5]} |
|
| 6071 | result.append(meta_result) |
|
| 6072 | ||
| 6073 | resp.text = json.dumps(result) |
|
| 6074 | ||
| 6075 | @staticmethod |
|
| 6076 | @user_logger |
|
| 6077 | def on_post(req, resp, id_, eid): |
|
| 6078 | admin_control(req) |
|
| 6079 | try: |
|
| 6080 | raw_json = req.stream.read().decode('utf-8') |
|
| 6081 | except Exception as ex: |
|
| 6082 | print(str(ex)) |
|
| 6083 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 6084 | title='API.BAD_REQUEST', |
|
| 6085 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 6086 | ||
| 6087 | if not id_.isdigit() or int(id_) <= 0: |
|
| 6088 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6089 | description='API.INVALID_MICROGRID_ID') |
|
| 6090 | if not eid.isdigit() or int(eid) <= 0: |
|
| 6091 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 6092 | description='API.INVALID_EVCHARGER_ID') |
|
| 6093 | ||
| 6094 | new_values = json.loads(raw_json) |
|
| 6095 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 6096 | cursor = cnx.cursor() |
|
| 6097 | ||
| 6098 | cursor.execute(" SELECT name " |
|
| 6099 | " FROM tbl_microgrids_evchargers " |
|
| 6100 | " WHERE microgrid_id = %s AND id = %s ", (id_, eid,)) |
|
| 6101 | if cursor.fetchone() is None: |
|
| 6102 | cursor.close() |
|
| 6103 | cnx.close() |
|
| 6104 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6105 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
|
| 6106 | ||
| 6107 | cursor.execute(" SELECT name, object_type " |
|
| 6108 | " FROM tbl_points " |
|
| 6109 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 6110 | row = cursor.fetchone() |
|
| 6111 | if row is None: |
|
| 6112 | cursor.close() |
|
| 6113 | cnx.close() |
|
| 6114 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 6115 | description='API.POINT_NOT_FOUND') |
|
| 6116 | ||
| 6117 | query = (" SELECT id " |
|
| 6118 | " FROM tbl_microgrids_evchargers_points " |
|
| 6119 | " WHERE evcharger_id = %s AND point_id = %s") |
|
| 6120 | cursor.execute(query, (eid, new_values['data']['point_id'],)) |
|
| 6121 | if cursor.fetchone() is not None: |
|
| 6122 | cursor.close() |
|
| 6123 | cnx.close() |
|
| 6124 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 6125 | description='API.EVCHARGER_POINT_RELATION_EXISTS') |
|
| 6126 | ||
| 6127 | add_row = (" INSERT INTO tbl_microgrids_evchargers_points (evcharger_id, point_id) " |
|
| 6128 | " VALUES (%s, %s) ") |
|
| 6129 | cursor.execute(add_row, (eid, new_values['data']['point_id'],)) |
|
| 6130 | cnx.commit() |
|
| 6131 | cursor.close() |
|
| 6132 | cnx.close() |
|
| 6133 | ||
| 6134 | resp.status = falcon.HTTP_201 |
|
| 6135 | resp.location = '/microgrids/' + str(id_) + '/evchargers/' + str(eid) + '/points/' + \ |
|
| 6136 | str(new_values['data']['point_id']) |
|
| 6137 | ||
| 6138 | ||
| 6139 | class MicrogridEVChargerPointItem: |
|
| @@ 5832-5950 (lines=119) @@ | ||
| 5829 | ||
| 5830 | resp.text = json.dumps(result) |
|
| 5831 | ||
| 5832 | class MicrogridBatteryPointCollection: |
|
| 5833 | def __init__(self): |
|
| 5834 | pass |
|
| 5835 | ||
| 5836 | @staticmethod |
|
| 5837 | def on_options(req, resp, id_, bid): |
|
| 5838 | _ = req |
|
| 5839 | resp.status = falcon.HTTP_200 |
|
| 5840 | _ = id_ |
|
| 5841 | _ = bid |
|
| 5842 | ||
| 5843 | @staticmethod |
|
| 5844 | def on_get(req, resp, id_, bid): |
|
| 5845 | if 'API-KEY' not in req.headers or \ |
|
| 5846 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5847 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5848 | access_control(req) |
|
| 5849 | else: |
|
| 5850 | api_key_control(req) |
|
| 5851 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5852 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5853 | description='API.INVALID_MICROGRID_ID') |
|
| 5854 | if not bid.isdigit() or int(bid) <= 0: |
|
| 5855 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5856 | description='API.INVALID_BATTERY_ID') |
|
| 5857 | ||
| 5858 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5859 | cursor = cnx.cursor() |
|
| 5860 | ||
| 5861 | cursor.execute(" SELECT name " |
|
| 5862 | " FROM tbl_microgrids_batteries " |
|
| 5863 | " WHERE microgrid_id = %s AND id = %s ", (id_, bid,)) |
|
| 5864 | if cursor.fetchone() is None: |
|
| 5865 | cursor.close() |
|
| 5866 | cnx.close() |
|
| 5867 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5868 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
|
| 5869 | ||
| 5870 | query = (" SELECT p.id, p.name, " |
|
| 5871 | " ds.id, ds.name, ds.uuid, " |
|
| 5872 | " p.address " |
|
| 5873 | " FROM tbl_points p, tbl_microgrids_bmses_points mp, tbl_data_sources ds " |
|
| 5874 | " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 5875 | " ORDER BY p.name ") |
|
| 5876 | cursor.execute(query, (bid,)) |
|
| 5877 | rows = cursor.fetchall() |
|
| 5878 | ||
| 5879 | result = list() |
|
| 5880 | if rows is not None and len(rows) > 0: |
|
| 5881 | for row in rows: |
|
| 5882 | meta_result = {"id": row[0], "name": row[1], |
|
| 5883 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 5884 | "address": row[5]} |
|
| 5885 | result.append(meta_result) |
|
| 5886 | ||
| 5887 | resp.text = json.dumps(result) |
|
| 5888 | ||
| 5889 | @staticmethod |
|
| 5890 | @user_logger |
|
| 5891 | def on_post(req, resp, id_, bid): |
|
| 5892 | admin_control(req) |
|
| 5893 | try: |
|
| 5894 | raw_json = req.stream.read().decode('utf-8') |
|
| 5895 | except Exception as ex: |
|
| 5896 | print(str(ex)) |
|
| 5897 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5898 | title='API.BAD_REQUEST', |
|
| 5899 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5900 | ||
| 5901 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5902 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5903 | description='API.INVALID_MICROGRID_ID') |
|
| 5904 | if not bid.isdigit() or int(bid) <= 0: |
|
| 5905 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5906 | description='API.INVALID_BATTERY_ID') |
|
| 5907 | ||
| 5908 | new_values = json.loads(raw_json) |
|
| 5909 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5910 | cursor = cnx.cursor() |
|
| 5911 | ||
| 5912 | cursor.execute(" SELECT name " |
|
| 5913 | " FROM tbl_microgrids_batteries " |
|
| 5914 | " WHERE microgrid_id = %s AND id = %s ", (id_, bid,)) |
|
| 5915 | if cursor.fetchone() is None: |
|
| 5916 | cursor.close() |
|
| 5917 | cnx.close() |
|
| 5918 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5919 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
|
| 5920 | ||
| 5921 | cursor.execute(" SELECT name, object_type " |
|
| 5922 | " FROM tbl_points " |
|
| 5923 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 5924 | row = cursor.fetchone() |
|
| 5925 | if row is None: |
|
| 5926 | cursor.close() |
|
| 5927 | cnx.close() |
|
| 5928 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5929 | description='API.POINT_NOT_FOUND') |
|
| 5930 | ||
| 5931 | query = (" SELECT id " |
|
| 5932 | " FROM tbl_microgrids_bmses_points " |
|
| 5933 | " WHERE bms_id = %s AND point_id = %s") |
|
| 5934 | cursor.execute(query, (bid, new_values['data']['point_id'],)) |
|
| 5935 | if cursor.fetchone() is not None: |
|
| 5936 | cursor.close() |
|
| 5937 | cnx.close() |
|
| 5938 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5939 | description='API.BATTERY_POINT_RELATION_EXISTS') |
|
| 5940 | ||
| 5941 | add_row = (" INSERT INTO tbl_microgrids_bmses_points (bms_id, point_id) " |
|
| 5942 | " VALUES (%s, %s) ") |
|
| 5943 | cursor.execute(add_row, (bid, new_values['data']['point_id'],)) |
|
| 5944 | cnx.commit() |
|
| 5945 | cursor.close() |
|
| 5946 | cnx.close() |
|
| 5947 | ||
| 5948 | resp.status = falcon.HTTP_201 |
|
| 5949 | resp.location = '/microgrids/' + str(id_) + '/batteries/' + str(bid) + '/points/' + \ |
|
| 5950 | str(new_values['data']['point_id']) |
|
| 5951 | ||
| 5952 | ||
| 5953 | class MicrogridBatteryPointItem: |
|
| @@ 5614-5732 (lines=119) @@ | ||
| 5611 | resp.status = falcon.HTTP_204 |
|
| 5612 | ||
| 5613 | ||
| 5614 | class PhotovoltaicPowerStationLoadPointCollection: |
|
| 5615 | def __init__(self): |
|
| 5616 | pass |
|
| 5617 | ||
| 5618 | @staticmethod |
|
| 5619 | def on_options(req, resp, id_, lid): |
|
| 5620 | _ = req |
|
| 5621 | resp.status = falcon.HTTP_200 |
|
| 5622 | _ = id_ |
|
| 5623 | _ = lid |
|
| 5624 | ||
| 5625 | @staticmethod |
|
| 5626 | def on_get(req, resp, id_, lid): |
|
| 5627 | if 'API-KEY' not in req.headers or \ |
|
| 5628 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5629 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5630 | access_control(req) |
|
| 5631 | else: |
|
| 5632 | api_key_control(req) |
|
| 5633 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5634 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5635 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5636 | if not lid.isdigit() or int(lid) <= 0: |
|
| 5637 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5638 | description='API.INVALID_LOAD_ID') |
|
| 5639 | ||
| 5640 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5641 | cursor = cnx.cursor() |
|
| 5642 | ||
| 5643 | cursor.execute(" SELECT name " |
|
| 5644 | " FROM tbl_photovoltaic_power_stations_loads " |
|
| 5645 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, lid,)) |
|
| 5646 | if cursor.fetchone() is None: |
|
| 5647 | cursor.close() |
|
| 5648 | cnx.close() |
|
| 5649 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5650 | description='API.PHOTOVOLTAIC_LOAD_NOT_FOUND') |
|
| 5651 | ||
| 5652 | query = (" SELECT p.id, p.name, " |
|
| 5653 | " ds.id, ds.name, ds.uuid, " |
|
| 5654 | " p.address " |
|
| 5655 | " FROM tbl_points p, tbl_photovoltaic_power_stations_loads_points mp, tbl_data_sources ds " |
|
| 5656 | " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 5657 | " ORDER BY p.name ") |
|
| 5658 | cursor.execute(query, (lid,)) |
|
| 5659 | rows = cursor.fetchall() |
|
| 5660 | ||
| 5661 | result = list() |
|
| 5662 | if rows is not None and len(rows) > 0: |
|
| 5663 | for row in rows: |
|
| 5664 | meta_result = {"id": row[0], "name": row[1], |
|
| 5665 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 5666 | "address": row[5]} |
|
| 5667 | result.append(meta_result) |
|
| 5668 | ||
| 5669 | resp.text = json.dumps(result) |
|
| 5670 | ||
| 5671 | @staticmethod |
|
| 5672 | @user_logger |
|
| 5673 | def on_post(req, resp, id_, lid): |
|
| 5674 | admin_control(req) |
|
| 5675 | try: |
|
| 5676 | raw_json = req.stream.read().decode('utf-8') |
|
| 5677 | except Exception as ex: |
|
| 5678 | print(str(ex)) |
|
| 5679 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5680 | title='API.BAD_REQUEST', |
|
| 5681 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5682 | ||
| 5683 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5684 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5685 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5686 | if not lid.isdigit() or int(lid) <= 0: |
|
| 5687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5688 | description='API.INVALID_LOAD_ID') |
|
| 5689 | ||
| 5690 | new_values = json.loads(raw_json) |
|
| 5691 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5692 | cursor = cnx.cursor() |
|
| 5693 | ||
| 5694 | cursor.execute(" SELECT name " |
|
| 5695 | " FROM tbl_photovoltaic_power_stations_loads " |
|
| 5696 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, lid,)) |
|
| 5697 | if cursor.fetchone() is None: |
|
| 5698 | cursor.close() |
|
| 5699 | cnx.close() |
|
| 5700 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5701 | description='API.PHOTOVOLTAIC_LOAD_NOT_FOUND') |
|
| 5702 | ||
| 5703 | cursor.execute(" SELECT name, object_type " |
|
| 5704 | " FROM tbl_points " |
|
| 5705 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 5706 | row = cursor.fetchone() |
|
| 5707 | if row is None: |
|
| 5708 | cursor.close() |
|
| 5709 | cnx.close() |
|
| 5710 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5711 | description='API.POINT_NOT_FOUND') |
|
| 5712 | ||
| 5713 | query = (" SELECT id " |
|
| 5714 | " FROM tbl_photovoltaic_power_stations_loads_points " |
|
| 5715 | " WHERE load_id = %s AND point_id = %s") |
|
| 5716 | cursor.execute(query, (lid, new_values['data']['point_id'],)) |
|
| 5717 | if cursor.fetchone() is not None: |
|
| 5718 | cursor.close() |
|
| 5719 | cnx.close() |
|
| 5720 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5721 | description='API.LOAD_POINT_RELATION_EXISTS') |
|
| 5722 | ||
| 5723 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_loads_points (load_id, point_id) " |
|
| 5724 | " VALUES (%s, %s) ") |
|
| 5725 | cursor.execute(add_row, (lid, new_values['data']['point_id'],)) |
|
| 5726 | cnx.commit() |
|
| 5727 | cursor.close() |
|
| 5728 | cnx.close() |
|
| 5729 | ||
| 5730 | resp.status = falcon.HTTP_201 |
|
| 5731 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/loads/' + str(lid) + '/points/' + \ |
|
| 5732 | str(new_values['data']['point_id']) |
|
| 5733 | ||
| 5734 | ||
| 5735 | class PhotovoltaicPowerStationLoadPointItem: |
|
| @@ 5427-5545 (lines=119) @@ | ||
| 5424 | resp.status = falcon.HTTP_204 |
|
| 5425 | ||
| 5426 | ||
| 5427 | class PhotovoltaicPowerStationGridPointCollection: |
|
| 5428 | def __init__(self): |
|
| 5429 | pass |
|
| 5430 | ||
| 5431 | @staticmethod |
|
| 5432 | def on_options(req, resp, id_, gid): |
|
| 5433 | _ = req |
|
| 5434 | resp.status = falcon.HTTP_200 |
|
| 5435 | _ = id_ |
|
| 5436 | _ = gid |
|
| 5437 | ||
| 5438 | @staticmethod |
|
| 5439 | def on_get(req, resp, id_, gid): |
|
| 5440 | if 'API-KEY' not in req.headers or \ |
|
| 5441 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5442 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5443 | access_control(req) |
|
| 5444 | else: |
|
| 5445 | api_key_control(req) |
|
| 5446 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5447 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5448 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5449 | if not gid.isdigit() or int(gid) <= 0: |
|
| 5450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5451 | description='API.INVALID_GRID_ID') |
|
| 5452 | ||
| 5453 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5454 | cursor = cnx.cursor() |
|
| 5455 | ||
| 5456 | cursor.execute(" SELECT name " |
|
| 5457 | " FROM tbl_photovoltaic_power_stations_grids " |
|
| 5458 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, gid,)) |
|
| 5459 | if cursor.fetchone() is None: |
|
| 5460 | cursor.close() |
|
| 5461 | cnx.close() |
|
| 5462 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5463 | description='API.PHOTOVOLTAIC_GRID_NOT_FOUND') |
|
| 5464 | ||
| 5465 | query = (" SELECT p.id, p.name, " |
|
| 5466 | " ds.id, ds.name, ds.uuid, " |
|
| 5467 | " p.address " |
|
| 5468 | " FROM tbl_points p, tbl_photovoltaic_power_stations_grids_points mp, tbl_data_sources ds " |
|
| 5469 | " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 5470 | " ORDER BY p.name ") |
|
| 5471 | cursor.execute(query, (gid,)) |
|
| 5472 | rows = cursor.fetchall() |
|
| 5473 | ||
| 5474 | result = list() |
|
| 5475 | if rows is not None and len(rows) > 0: |
|
| 5476 | for row in rows: |
|
| 5477 | meta_result = {"id": row[0], "name": row[1], |
|
| 5478 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 5479 | "address": row[5]} |
|
| 5480 | result.append(meta_result) |
|
| 5481 | ||
| 5482 | resp.text = json.dumps(result) |
|
| 5483 | ||
| 5484 | @staticmethod |
|
| 5485 | @user_logger |
|
| 5486 | def on_post(req, resp, id_, gid): |
|
| 5487 | admin_control(req) |
|
| 5488 | try: |
|
| 5489 | raw_json = req.stream.read().decode('utf-8') |
|
| 5490 | except Exception as ex: |
|
| 5491 | print(str(ex)) |
|
| 5492 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5493 | title='API.BAD_REQUEST', |
|
| 5494 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5495 | ||
| 5496 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5497 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5498 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5499 | if not gid.isdigit() or int(gid) <= 0: |
|
| 5500 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5501 | description='API.INVALID_GRID_ID') |
|
| 5502 | ||
| 5503 | new_values = json.loads(raw_json) |
|
| 5504 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5505 | cursor = cnx.cursor() |
|
| 5506 | ||
| 5507 | cursor.execute(" SELECT name " |
|
| 5508 | " FROM tbl_photovoltaic_power_stations_grids " |
|
| 5509 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, gid,)) |
|
| 5510 | if cursor.fetchone() is None: |
|
| 5511 | cursor.close() |
|
| 5512 | cnx.close() |
|
| 5513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5514 | description='API.PHOTOVOLTAIC_GRID_NOT_FOUND') |
|
| 5515 | ||
| 5516 | cursor.execute(" SELECT name, object_type " |
|
| 5517 | " FROM tbl_points " |
|
| 5518 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 5519 | row = cursor.fetchone() |
|
| 5520 | if row is None: |
|
| 5521 | cursor.close() |
|
| 5522 | cnx.close() |
|
| 5523 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5524 | description='API.POINT_NOT_FOUND') |
|
| 5525 | ||
| 5526 | query = (" SELECT id " |
|
| 5527 | " FROM tbl_photovoltaic_power_stations_grids_points " |
|
| 5528 | " WHERE grid_id = %s AND point_id = %s") |
|
| 5529 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
|
| 5530 | if cursor.fetchone() is not None: |
|
| 5531 | cursor.close() |
|
| 5532 | cnx.close() |
|
| 5533 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5534 | description='API.GRID_POINT_RELATION_EXISTS') |
|
| 5535 | ||
| 5536 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_grids_points (grid_id, point_id) " |
|
| 5537 | " VALUES (%s, %s) ") |
|
| 5538 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
|
| 5539 | cnx.commit() |
|
| 5540 | cursor.close() |
|
| 5541 | cnx.close() |
|
| 5542 | ||
| 5543 | resp.status = falcon.HTTP_201 |
|
| 5544 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/grids/' + str(gid) + '/points/' + \ |
|
| 5545 | str(new_values['data']['point_id']) |
|
| 5546 | ||
| 5547 | ||
| 5548 | class PhotovoltaicPowerStationGridPointItem: |
|
| @@ 5240-5358 (lines=119) @@ | ||
| 5237 | ||
| 5238 | resp.status = falcon.HTTP_204 |
|
| 5239 | ||
| 5240 | class PhotovoltaicPowerStationInvertorPointCollection: |
|
| 5241 | def __init__(self): |
|
| 5242 | pass |
|
| 5243 | ||
| 5244 | @staticmethod |
|
| 5245 | def on_options(req, resp, id_, iid): |
|
| 5246 | _ = req |
|
| 5247 | resp.status = falcon.HTTP_200 |
|
| 5248 | _ = id_ |
|
| 5249 | _ = iid |
|
| 5250 | ||
| 5251 | @staticmethod |
|
| 5252 | def on_get(req, resp, id_, iid): |
|
| 5253 | if 'API-KEY' not in req.headers or \ |
|
| 5254 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 5255 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 5256 | access_control(req) |
|
| 5257 | else: |
|
| 5258 | api_key_control(req) |
|
| 5259 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5261 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5262 | if not iid.isdigit() or int(iid) <= 0: |
|
| 5263 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5264 | description='API.INVALID_INVERTOR_ID') |
|
| 5265 | ||
| 5266 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5267 | cursor = cnx.cursor() |
|
| 5268 | ||
| 5269 | cursor.execute(" SELECT name " |
|
| 5270 | " FROM tbl_photovoltaic_power_stations_invertors " |
|
| 5271 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, iid,)) |
|
| 5272 | if cursor.fetchone() is None: |
|
| 5273 | cursor.close() |
|
| 5274 | cnx.close() |
|
| 5275 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5276 | description='API.PHOTOVOLTAIC_INVERTOR_NOT_FOUND') |
|
| 5277 | ||
| 5278 | query = (" SELECT p.id, p.name, " |
|
| 5279 | " ds.id, ds.name, ds.uuid, " |
|
| 5280 | " p.address " |
|
| 5281 | " FROM tbl_points p, tbl_photovoltaic_power_stations_invertors_points mp, tbl_data_sources ds " |
|
| 5282 | " WHERE mp.invertor_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
|
| 5283 | " ORDER BY p.name ") |
|
| 5284 | cursor.execute(query, (iid,)) |
|
| 5285 | rows = cursor.fetchall() |
|
| 5286 | ||
| 5287 | result = list() |
|
| 5288 | if rows is not None and len(rows) > 0: |
|
| 5289 | for row in rows: |
|
| 5290 | meta_result = {"id": row[0], "name": row[1], |
|
| 5291 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
|
| 5292 | "address": row[5]} |
|
| 5293 | result.append(meta_result) |
|
| 5294 | ||
| 5295 | resp.text = json.dumps(result) |
|
| 5296 | ||
| 5297 | @staticmethod |
|
| 5298 | @user_logger |
|
| 5299 | def on_post(req, resp, id_, iid): |
|
| 5300 | admin_control(req) |
|
| 5301 | try: |
|
| 5302 | raw_json = req.stream.read().decode('utf-8') |
|
| 5303 | except Exception as ex: |
|
| 5304 | print(str(ex)) |
|
| 5305 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5306 | title='API.BAD_REQUEST', |
|
| 5307 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5308 | ||
| 5309 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5310 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5311 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 5312 | if not iid.isdigit() or int(iid) <= 0: |
|
| 5313 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5314 | description='API.INVALID_INVERTOR_ID') |
|
| 5315 | ||
| 5316 | new_values = json.loads(raw_json) |
|
| 5317 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5318 | cursor = cnx.cursor() |
|
| 5319 | ||
| 5320 | cursor.execute(" SELECT name " |
|
| 5321 | " FROM tbl_photovoltaic_power_stations_invertors " |
|
| 5322 | " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, iid,)) |
|
| 5323 | if cursor.fetchone() is None: |
|
| 5324 | cursor.close() |
|
| 5325 | cnx.close() |
|
| 5326 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5327 | description='API.PHOTOVOLTAIC_INVERTOR_NOT_FOUND') |
|
| 5328 | ||
| 5329 | cursor.execute(" SELECT name, object_type " |
|
| 5330 | " FROM tbl_points " |
|
| 5331 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 5332 | row = cursor.fetchone() |
|
| 5333 | if row is None: |
|
| 5334 | cursor.close() |
|
| 5335 | cnx.close() |
|
| 5336 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5337 | description='API.POINT_NOT_FOUND') |
|
| 5338 | ||
| 5339 | query = (" SELECT id " |
|
| 5340 | " FROM tbl_photovoltaic_power_stations_invertors_points " |
|
| 5341 | " WHERE invertor_id = %s AND point_id = %s") |
|
| 5342 | cursor.execute(query, (iid, new_values['data']['point_id'],)) |
|
| 5343 | if cursor.fetchone() is not None: |
|
| 5344 | cursor.close() |
|
| 5345 | cnx.close() |
|
| 5346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5347 | description='API.INVERTOR_POINT_RELATION_EXISTS') |
|
| 5348 | ||
| 5349 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_invertors_points (invertor_id, point_id) " |
|
| 5350 | " VALUES (%s, %s) ") |
|
| 5351 | cursor.execute(add_row, (iid, new_values['data']['point_id'],)) |
|
| 5352 | cnx.commit() |
|
| 5353 | cursor.close() |
|
| 5354 | cnx.close() |
|
| 5355 | ||
| 5356 | resp.status = falcon.HTTP_201 |
|
| 5357 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/invertors/' + str(iid) + '/points/' + \ |
|
| 5358 | str(new_values['data']['point_id']) |
|
| 5359 | ||
| 5360 | ||
| 5361 | class PhotovoltaicPowerStationInvertorPointItem: |
|