@@ 4782-4889 (lines=108) @@ | ||
4779 | resp.status = falcon.HTTP_200 |
|
4780 | ||
4781 | ||
4782 | class MicrogridSensorCollection: |
|
4783 | def __init__(self): |
|
4784 | """Initializes Class""" |
|
4785 | pass |
|
4786 | ||
4787 | @staticmethod |
|
4788 | def on_options(req, resp, id_): |
|
4789 | resp.status = falcon.HTTP_200 |
|
4790 | ||
4791 | @staticmethod |
|
4792 | def on_get(req, resp, id_): |
|
4793 | access_control(req) |
|
4794 | if not id_.isdigit() or int(id_) <= 0: |
|
4795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4796 | description='API.INVALID_MICROGRID_ID') |
|
4797 | ||
4798 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4799 | cursor = cnx.cursor() |
|
4800 | ||
4801 | cursor.execute(" SELECT name " |
|
4802 | " FROM tbl_microgrids " |
|
4803 | " WHERE id = %s ", (id_,)) |
|
4804 | if cursor.fetchone() is None: |
|
4805 | cursor.close() |
|
4806 | cnx.close() |
|
4807 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4808 | description='API.MICROGRID_NOT_FOUND') |
|
4809 | ||
4810 | query = (" SELECT s.id, s.name, s.uuid " |
|
4811 | " FROM tbl_microgrids m, tbl_microgrids_sensors ms, tbl_sensors s " |
|
4812 | " WHERE ms.microgrid_id = m.id AND s.id = ms.sensor_id AND m.id = %s " |
|
4813 | " ORDER BY s.id ") |
|
4814 | cursor.execute(query, (id_,)) |
|
4815 | rows = cursor.fetchall() |
|
4816 | ||
4817 | result = list() |
|
4818 | if rows is not None and len(rows) > 0: |
|
4819 | for row in rows: |
|
4820 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
4821 | result.append(meta_result) |
|
4822 | ||
4823 | resp.text = json.dumps(result) |
|
4824 | ||
4825 | @staticmethod |
|
4826 | @user_logger |
|
4827 | def on_post(req, resp, id_): |
|
4828 | """Handles POST requests""" |
|
4829 | admin_control(req) |
|
4830 | try: |
|
4831 | raw_json = req.stream.read().decode('utf-8') |
|
4832 | except Exception as ex: |
|
4833 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
4834 | title='API.BAD_REQUEST', |
|
4835 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
4836 | ||
4837 | if not id_.isdigit() or int(id_) <= 0: |
|
4838 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4839 | description='API.INVALID_MICROGRID_ID') |
|
4840 | ||
4841 | new_values = json.loads(raw_json) |
|
4842 | ||
4843 | if 'sensor_id' not in new_values['data'].keys() or \ |
|
4844 | not isinstance(new_values['data']['sensor_id'], int) or \ |
|
4845 | new_values['data']['sensor_id'] <= 0: |
|
4846 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4847 | description='API.INVALID_SENSOR_ID') |
|
4848 | sensor_id = new_values['data']['sensor_id'] |
|
4849 | ||
4850 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4851 | cursor = cnx.cursor() |
|
4852 | ||
4853 | cursor.execute(" SELECT name " |
|
4854 | " from tbl_microgrids " |
|
4855 | " WHERE id = %s ", (id_,)) |
|
4856 | if cursor.fetchone() is None: |
|
4857 | cursor.close() |
|
4858 | cnx.close() |
|
4859 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4860 | description='API.MICROGRID_NOT_FOUND') |
|
4861 | ||
4862 | cursor.execute(" SELECT name " |
|
4863 | " FROM tbl_sensors " |
|
4864 | " WHERE id = %s ", (sensor_id,)) |
|
4865 | if cursor.fetchone() is None: |
|
4866 | cursor.close() |
|
4867 | cnx.close() |
|
4868 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4869 | description='API.SENSOR_NOT_FOUND') |
|
4870 | ||
4871 | query = (" SELECT id " |
|
4872 | " FROM tbl_microgrids_sensors " |
|
4873 | " WHERE microgrid_id = %s AND sensor_id = %s") |
|
4874 | cursor.execute(query, (id_, sensor_id,)) |
|
4875 | if cursor.fetchone() is not None: |
|
4876 | cursor.close() |
|
4877 | cnx.close() |
|
4878 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
4879 | description='API.MICROGRID_SENSOR_RELATION_EXISTS') |
|
4880 | ||
4881 | add_row = (" INSERT INTO tbl_microgrids_sensors (microgrid_id, sensor_id) " |
|
4882 | " VALUES (%s, %s) ") |
|
4883 | cursor.execute(add_row, (id_, sensor_id,)) |
|
4884 | cnx.commit() |
|
4885 | cursor.close() |
|
4886 | cnx.close() |
|
4887 | ||
4888 | resp.status = falcon.HTTP_201 |
|
4889 | resp.location = '/microgrids/' + str(id_) + '/sensors/' + str(sensor_id) |
|
4890 | ||
4891 | ||
4892 | class MicrogridSensorItem: |
@@ 704-815 (lines=112) @@ | ||
701 | resp.status = falcon.HTTP_200 |
|
702 | ||
703 | ||
704 | class EnergyStoragePowerStationContainerCollection: |
|
705 | def __init__(self): |
|
706 | """Initializes Class""" |
|
707 | pass |
|
708 | ||
709 | @staticmethod |
|
710 | def on_options(req, resp, id_): |
|
711 | resp.status = falcon.HTTP_200 |
|
712 | ||
713 | @staticmethod |
|
714 | def on_get(req, resp, id_): |
|
715 | access_control(req) |
|
716 | if not id_.isdigit() or int(id_) <= 0: |
|
717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
718 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
719 | ||
720 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
721 | cursor = cnx.cursor() |
|
722 | ||
723 | cursor.execute(" SELECT name " |
|
724 | " FROM tbl_energy_storage_power_stations " |
|
725 | " WHERE id = %s ", (id_,)) |
|
726 | if cursor.fetchone() is None: |
|
727 | cursor.close() |
|
728 | cnx.close() |
|
729 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
730 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
731 | ||
732 | query = (" SELECT s.id, s.name, s.uuid " |
|
733 | " FROM tbl_energy_storage_power_stations e, " |
|
734 | " tbl_energy_storage_power_stations_containers es, tbl_energy_storage_containers s " |
|
735 | " WHERE es.energy_storage_power_station_id = e.id " |
|
736 | " AND s.id = es.energy_storage_container_id " |
|
737 | " AND e.id = %s " |
|
738 | " ORDER BY s.id ") |
|
739 | cursor.execute(query, (id_,)) |
|
740 | rows = cursor.fetchall() |
|
741 | ||
742 | result = list() |
|
743 | if rows is not None and len(rows) > 0: |
|
744 | for row in rows: |
|
745 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
746 | result.append(meta_result) |
|
747 | ||
748 | resp.text = json.dumps(result) |
|
749 | ||
750 | @staticmethod |
|
751 | @user_logger |
|
752 | def on_post(req, resp, id_): |
|
753 | """Handles POST requests""" |
|
754 | admin_control(req) |
|
755 | try: |
|
756 | raw_json = req.stream.read().decode('utf-8') |
|
757 | except Exception as ex: |
|
758 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
759 | title='API.BAD_REQUEST', |
|
760 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
761 | ||
762 | if not id_.isdigit() or int(id_) <= 0: |
|
763 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
764 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
765 | ||
766 | new_values = json.loads(raw_json) |
|
767 | ||
768 | if 'energy_storage_container_id' not in new_values['data'].keys() or \ |
|
769 | not isinstance(new_values['data']['energy_storage_container_id'], int) or \ |
|
770 | new_values['data']['energy_storage_container_id'] <= 0: |
|
771 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
772 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
773 | energy_storage_container_id = new_values['data']['energy_storage_container_id'] |
|
774 | ||
775 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
776 | cursor = cnx.cursor() |
|
777 | ||
778 | cursor.execute(" SELECT name " |
|
779 | " FROM tbl_energy_storage_power_stations " |
|
780 | " WHERE id = %s ", (id_,)) |
|
781 | if cursor.fetchone() is None: |
|
782 | cursor.close() |
|
783 | cnx.close() |
|
784 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
785 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
786 | ||
787 | cursor.execute(" SELECT name " |
|
788 | " FROM tbl_energy_storage_containers " |
|
789 | " WHERE id = %s ", (energy_storage_container_id,)) |
|
790 | if cursor.fetchone() is None: |
|
791 | cursor.close() |
|
792 | cnx.close() |
|
793 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
794 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
795 | ||
796 | query = (" SELECT id " |
|
797 | " FROM tbl_energy_storage_power_stations_containers " |
|
798 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s") |
|
799 | cursor.execute(query, (id_, energy_storage_container_id,)) |
|
800 | if cursor.fetchone() is not None: |
|
801 | cursor.close() |
|
802 | cnx.close() |
|
803 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
804 | description='API.ENERGY_STORAGE_CONTAINER_SENSOR_RELATION_EXISTS') |
|
805 | ||
806 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_containers " |
|
807 | " (energy_storage_power_station_id, energy_storage_container_id) " |
|
808 | " VALUES (%s, %s) ") |
|
809 | cursor.execute(add_row, (id_, energy_storage_container_id,)) |
|
810 | cnx.commit() |
|
811 | cursor.close() |
|
812 | cnx.close() |
|
813 | ||
814 | resp.status = falcon.HTTP_201 |
|
815 | resp.location = '/energystoragepowerstationss/' + str(id_) + '/containers/' + str(energy_storage_container_id) |
|
816 | ||
817 | ||
818 | class EnergyStoragePowerStationContainerItem: |