| @@ 3958-4203 (lines=246) @@ | ||
| 3955 | resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(new_id) |
|
| 3956 | ||
| 3957 | ||
| 3958 | class EnergyStorageContainerLoadItem: |
|
| 3959 | def __init__(self): |
|
| 3960 | """Initializes Class""" |
|
| 3961 | pass |
|
| 3962 | ||
| 3963 | @staticmethod |
|
| 3964 | def on_options(req, resp, id_, lid): |
|
| 3965 | resp.status = falcon.HTTP_200 |
|
| 3966 | ||
| 3967 | @staticmethod |
|
| 3968 | def on_get(req, resp, id_, lid): |
|
| 3969 | access_control(req) |
|
| 3970 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3971 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3972 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3973 | if not lid.isdigit() or int(lid) <= 0: |
|
| 3974 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3975 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
|
| 3976 | ||
| 3977 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3978 | cursor = cnx.cursor() |
|
| 3979 | ||
| 3980 | cursor.execute(" SELECT name " |
|
| 3981 | " FROM tbl_energy_storage_containers " |
|
| 3982 | " WHERE id = %s ", (id_,)) |
|
| 3983 | if cursor.fetchone() is None: |
|
| 3984 | cursor.close() |
|
| 3985 | cnx.close() |
|
| 3986 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3987 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 3988 | ||
| 3989 | query = (" SELECT id, name, uuid " |
|
| 3990 | " FROM tbl_energy_storage_containers ") |
|
| 3991 | cursor.execute(query) |
|
| 3992 | rows_energystoragecontainers = cursor.fetchall() |
|
| 3993 | ||
| 3994 | energy_storage_container_dict = dict() |
|
| 3995 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 3996 | for row in rows_energystoragecontainers: |
|
| 3997 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 3998 | "name": row[1], |
|
| 3999 | "uuid": row[2]} |
|
| 4000 | # query meter dict |
|
| 4001 | query = (" SELECT id, name, uuid " |
|
| 4002 | " FROM tbl_meters ") |
|
| 4003 | cursor.execute(query) |
|
| 4004 | rows_meters = cursor.fetchall() |
|
| 4005 | ||
| 4006 | meter_dict = dict() |
|
| 4007 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 4008 | for row in rows_meters: |
|
| 4009 | meter_dict[row[0]] = {"id": row[0], |
|
| 4010 | "name": row[1], |
|
| 4011 | "uuid": row[2]} |
|
| 4012 | # query point dict |
|
| 4013 | query = (" SELECT id, name " |
|
| 4014 | " FROM tbl_points ") |
|
| 4015 | cursor.execute(query) |
|
| 4016 | rows_points = cursor.fetchall() |
|
| 4017 | ||
| 4018 | point_dict = dict() |
|
| 4019 | if rows_points is not None and len(rows_points) > 0: |
|
| 4020 | for row in rows_points: |
|
| 4021 | point_dict[row[0]] = {"id": row[0], |
|
| 4022 | "name": row[1]} |
|
| 4023 | ||
| 4024 | query = (" SELECT id, name, uuid, " |
|
| 4025 | " energy_storage_container_id, power_point_id, meter_id, rated_input_power " |
|
| 4026 | " FROM tbl_energy_storage_containers_loads " |
|
| 4027 | " WHERE id = %s ") |
|
| 4028 | cursor.execute(query, (lid,)) |
|
| 4029 | row = cursor.fetchone() |
|
| 4030 | cursor.close() |
|
| 4031 | cnx.close() |
|
| 4032 | ||
| 4033 | if row is None: |
|
| 4034 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4035 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
|
| 4036 | else: |
|
| 4037 | meta_result = {"id": row[0], |
|
| 4038 | "name": row[1], |
|
| 4039 | "uuid": row[2], |
|
| 4040 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
| 4041 | "power_point": point_dict.get(row[4]), |
|
| 4042 | "meter": meter_dict.get(row[5]), |
|
| 4043 | "rated_input_power": row[6] |
|
| 4044 | } |
|
| 4045 | ||
| 4046 | resp.text = json.dumps(meta_result) |
|
| 4047 | ||
| 4048 | @staticmethod |
|
| 4049 | @user_logger |
|
| 4050 | def on_delete(req, resp, id_, lid): |
|
| 4051 | admin_control(req) |
|
| 4052 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4054 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4055 | if not lid.isdigit() or int(lid) <= 0: |
|
| 4056 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4057 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
|
| 4058 | ||
| 4059 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4060 | cursor = cnx.cursor() |
|
| 4061 | ||
| 4062 | cursor.execute(" SELECT name " |
|
| 4063 | " FROM tbl_energy_storage_containers " |
|
| 4064 | " WHERE id = %s ", (id_,)) |
|
| 4065 | if cursor.fetchone() is None: |
|
| 4066 | cursor.close() |
|
| 4067 | cnx.close() |
|
| 4068 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4069 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4070 | ||
| 4071 | cursor.execute(" SELECT name " |
|
| 4072 | " FROM tbl_energy_storage_containers_loads " |
|
| 4073 | " WHERE id = %s ", (lid,)) |
|
| 4074 | if cursor.fetchone() is None: |
|
| 4075 | cursor.close() |
|
| 4076 | cnx.close() |
|
| 4077 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4078 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
|
| 4079 | ||
| 4080 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads " |
|
| 4081 | " WHERE id = %s ", (lid,)) |
|
| 4082 | cnx.commit() |
|
| 4083 | ||
| 4084 | cursor.close() |
|
| 4085 | cnx.close() |
|
| 4086 | ||
| 4087 | resp.status = falcon.HTTP_204 |
|
| 4088 | ||
| 4089 | @staticmethod |
|
| 4090 | @user_logger |
|
| 4091 | def on_put(req, resp, id_, lid): |
|
| 4092 | """Handles PUT requests""" |
|
| 4093 | admin_control(req) |
|
| 4094 | try: |
|
| 4095 | raw_json = req.stream.read().decode('utf-8') |
|
| 4096 | except Exception as ex: |
|
| 4097 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4098 | title='API.BAD_REQUEST', |
|
| 4099 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4100 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4101 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4102 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 4103 | if not lid.isdigit() or int(lid) <= 0: |
|
| 4104 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4105 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
|
| 4106 | ||
| 4107 | new_values = json.loads(raw_json) |
|
| 4108 | ||
| 4109 | if 'name' not in new_values['data'].keys() or \ |
|
| 4110 | not isinstance(new_values['data']['name'], str) or \ |
|
| 4111 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 4112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4113 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_NAME') |
|
| 4114 | name = str.strip(new_values['data']['name']) |
|
| 4115 | ||
| 4116 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 4117 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 4118 | new_values['data']['power_point_id'] <= 0: |
|
| 4119 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4120 | description='API.INVALID_POWER_POINT_ID') |
|
| 4121 | power_point_id = new_values['data']['power_point_id'] |
|
| 4122 | ||
| 4123 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 4124 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 4125 | new_values['data']['meter_id'] <= 0: |
|
| 4126 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4127 | description='API.INVALID_METER_ID') |
|
| 4128 | meter_id = new_values['data']['meter_id'] |
|
| 4129 | ||
| 4130 | if 'rated_input_power' not in new_values['data'].keys() or \ |
|
| 4131 | not (isinstance(new_values['data']['rated_input_power'], float) or |
|
| 4132 | isinstance(new_values['data']['rated_input_power'], int)): |
|
| 4133 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4134 | description='API.INVALID_RATED_INPUT_POWER') |
|
| 4135 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
|
| 4136 | ||
| 4137 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4138 | cursor = cnx.cursor() |
|
| 4139 | ||
| 4140 | cursor.execute(" SELECT name " |
|
| 4141 | " FROM tbl_energy_storage_containers " |
|
| 4142 | " WHERE id = %s ", (id_,)) |
|
| 4143 | if cursor.fetchone() is None: |
|
| 4144 | cursor.close() |
|
| 4145 | cnx.close() |
|
| 4146 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4147 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 4148 | ||
| 4149 | cursor.execute(" SELECT name " |
|
| 4150 | " FROM tbl_energy_storage_containers_loads " |
|
| 4151 | " WHERE id = %s ", (lid,)) |
|
| 4152 | if cursor.fetchone() is None: |
|
| 4153 | cursor.close() |
|
| 4154 | cnx.close() |
|
| 4155 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4156 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
|
| 4157 | ||
| 4158 | cursor.execute(" SELECT name " |
|
| 4159 | " FROM tbl_energy_storage_containers_loads " |
|
| 4160 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
| 4161 | (id_, name, lid)) |
|
| 4162 | if cursor.fetchone() is not None: |
|
| 4163 | cursor.close() |
|
| 4164 | cnx.close() |
|
| 4165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4166 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NAME_IS_ALREADY_IN_USE') |
|
| 4167 | ||
| 4168 | cursor.execute(" SELECT name " |
|
| 4169 | " FROM tbl_points " |
|
| 4170 | " WHERE id = %s ", |
|
| 4171 | (power_point_id,)) |
|
| 4172 | if cursor.fetchone() is None: |
|
| 4173 | cursor.close() |
|
| 4174 | cnx.close() |
|
| 4175 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4176 | description='API.POWER_POINT_NOT_FOUND') |
|
| 4177 | ||
| 4178 | cursor.execute(" SELECT name " |
|
| 4179 | " FROM tbl_meters " |
|
| 4180 | " WHERE id = %s ", |
|
| 4181 | (meter_id,)) |
|
| 4182 | if cursor.fetchone() is None: |
|
| 4183 | cursor.close() |
|
| 4184 | cnx.close() |
|
| 4185 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4186 | description='API.METER_NOT_FOUND') |
|
| 4187 | ||
| 4188 | update_row = (" UPDATE tbl_energy_storage_containers_loads " |
|
| 4189 | " SET name = %s, energy_storage_container_id = %s, power_point_id = %s, " |
|
| 4190 | " meter_id = %s, rated_input_power = %s " |
|
| 4191 | " WHERE id = %s ") |
|
| 4192 | cursor.execute(update_row, (name, |
|
| 4193 | id_, |
|
| 4194 | power_point_id, |
|
| 4195 | meter_id, |
|
| 4196 | rated_input_power, |
|
| 4197 | lid)) |
|
| 4198 | cnx.commit() |
|
| 4199 | ||
| 4200 | cursor.close() |
|
| 4201 | cnx.close() |
|
| 4202 | ||
| 4203 | resp.status = falcon.HTTP_200 |
|
| 4204 | ||
| 4205 | ||
| 4206 | class EnergyStorageContainerLoadPointCollection: |
|
| @@ 2811-3056 (lines=246) @@ | ||
| 2808 | resp.location = '/hybridpowerstations/' + str(id_) + '/loads/' + str(new_id) |
|
| 2809 | ||
| 2810 | ||
| 2811 | class HybridPowerStationLoadItem: |
|
| 2812 | def __init__(self): |
|
| 2813 | """Initializes Class""" |
|
| 2814 | pass |
|
| 2815 | ||
| 2816 | @staticmethod |
|
| 2817 | def on_options(req, resp, id_, lid): |
|
| 2818 | resp.status = falcon.HTTP_200 |
|
| 2819 | ||
| 2820 | @staticmethod |
|
| 2821 | def on_get(req, resp, id_, lid): |
|
| 2822 | access_control(req) |
|
| 2823 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2824 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2825 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 2826 | if not lid.isdigit() or int(lid) <= 0: |
|
| 2827 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2828 | description='API.INVALID_HYBRID_POWER_STATION_LOAD_ID') |
|
| 2829 | ||
| 2830 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2831 | cursor = cnx.cursor() |
|
| 2832 | ||
| 2833 | cursor.execute(" SELECT name " |
|
| 2834 | " FROM tbl_hybrid_power_stations " |
|
| 2835 | " WHERE id = %s ", (id_,)) |
|
| 2836 | if cursor.fetchone() is None: |
|
| 2837 | cursor.close() |
|
| 2838 | cnx.close() |
|
| 2839 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2840 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 2841 | ||
| 2842 | query = (" SELECT id, name, uuid " |
|
| 2843 | " FROM tbl_hybrid_power_stations ") |
|
| 2844 | cursor.execute(query) |
|
| 2845 | rows_hybridpowerstations = cursor.fetchall() |
|
| 2846 | ||
| 2847 | hybrid_power_station_dict = dict() |
|
| 2848 | if rows_hybridpowerstations is not None and len(rows_hybridpowerstations) > 0: |
|
| 2849 | for row in rows_hybridpowerstations: |
|
| 2850 | hybrid_power_station_dict[row[0]] = {"id": row[0], |
|
| 2851 | "name": row[1], |
|
| 2852 | "uuid": row[2]} |
|
| 2853 | # query meter dict |
|
| 2854 | query = (" SELECT id, name, uuid " |
|
| 2855 | " FROM tbl_meters ") |
|
| 2856 | cursor.execute(query) |
|
| 2857 | rows_meters = cursor.fetchall() |
|
| 2858 | ||
| 2859 | meter_dict = dict() |
|
| 2860 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 2861 | for row in rows_meters: |
|
| 2862 | meter_dict[row[0]] = {"id": row[0], |
|
| 2863 | "name": row[1], |
|
| 2864 | "uuid": row[2]} |
|
| 2865 | # query point dict |
|
| 2866 | query = (" SELECT id, name " |
|
| 2867 | " FROM tbl_points ") |
|
| 2868 | cursor.execute(query) |
|
| 2869 | rows_points = cursor.fetchall() |
|
| 2870 | ||
| 2871 | point_dict = dict() |
|
| 2872 | if rows_points is not None and len(rows_points) > 0: |
|
| 2873 | for row in rows_points: |
|
| 2874 | point_dict[row[0]] = {"id": row[0], |
|
| 2875 | "name": row[1]} |
|
| 2876 | ||
| 2877 | query = (" SELECT id, name, uuid, " |
|
| 2878 | " hybrid_power_station_id, meter_id " |
|
| 2879 | " FROM tbl_hybrid_power_stations_loads " |
|
| 2880 | " WHERE id = %s ") |
|
| 2881 | cursor.execute(query, (lid,)) |
|
| 2882 | row = cursor.fetchone() |
|
| 2883 | cursor.close() |
|
| 2884 | cnx.close() |
|
| 2885 | ||
| 2886 | if row is None: |
|
| 2887 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2888 | description='API.HYBRID_POWER_STATION_LOAD_NOT_FOUND') |
|
| 2889 | else: |
|
| 2890 | meta_result = {"id": row[0], |
|
| 2891 | "name": row[1], |
|
| 2892 | "uuid": row[2], |
|
| 2893 | "hybrid_power_station": hybrid_power_station_dict.get(row[3]), |
|
| 2894 | "power_point": point_dict.get(row[4]), |
|
| 2895 | "meter": meter_dict.get(row[5]), |
|
| 2896 | "rated_input_power": row[6] |
|
| 2897 | } |
|
| 2898 | ||
| 2899 | resp.text = json.dumps(meta_result) |
|
| 2900 | ||
| 2901 | @staticmethod |
|
| 2902 | @user_logger |
|
| 2903 | def on_delete(req, resp, id_, lid): |
|
| 2904 | admin_control(req) |
|
| 2905 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2906 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2907 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 2908 | if not lid.isdigit() or int(lid) <= 0: |
|
| 2909 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2910 | description='API.INVALID_HYBRID_POWER_STATION_LOAD_ID') |
|
| 2911 | ||
| 2912 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2913 | cursor = cnx.cursor() |
|
| 2914 | ||
| 2915 | cursor.execute(" SELECT name " |
|
| 2916 | " FROM tbl_hybrid_power_stations " |
|
| 2917 | " WHERE id = %s ", (id_,)) |
|
| 2918 | if cursor.fetchone() is None: |
|
| 2919 | cursor.close() |
|
| 2920 | cnx.close() |
|
| 2921 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2922 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 2923 | ||
| 2924 | cursor.execute(" SELECT name " |
|
| 2925 | " FROM tbl_hybrid_power_stations_loads " |
|
| 2926 | " WHERE id = %s ", (lid,)) |
|
| 2927 | if cursor.fetchone() is None: |
|
| 2928 | cursor.close() |
|
| 2929 | cnx.close() |
|
| 2930 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2931 | description='API.HYBRID_POWER_STATION_LOAD_NOT_FOUND') |
|
| 2932 | ||
| 2933 | cursor.execute(" DELETE FROM tbl_hybrid_power_stations_loads " |
|
| 2934 | " WHERE id = %s ", (lid,)) |
|
| 2935 | cnx.commit() |
|
| 2936 | ||
| 2937 | cursor.close() |
|
| 2938 | cnx.close() |
|
| 2939 | ||
| 2940 | resp.status = falcon.HTTP_204 |
|
| 2941 | ||
| 2942 | @staticmethod |
|
| 2943 | @user_logger |
|
| 2944 | def on_put(req, resp, id_, lid): |
|
| 2945 | """Handles PUT requests""" |
|
| 2946 | admin_control(req) |
|
| 2947 | try: |
|
| 2948 | raw_json = req.stream.read().decode('utf-8') |
|
| 2949 | except Exception as ex: |
|
| 2950 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2951 | title='API.BAD_REQUEST', |
|
| 2952 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2953 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2954 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2955 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
| 2956 | if not lid.isdigit() or int(lid) <= 0: |
|
| 2957 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2958 | description='API.INVALID_HYBRID_POWER_STATION_LOAD_ID') |
|
| 2959 | ||
| 2960 | new_values = json.loads(raw_json) |
|
| 2961 | ||
| 2962 | if 'name' not in new_values['data'].keys() or \ |
|
| 2963 | not isinstance(new_values['data']['name'], str) or \ |
|
| 2964 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 2965 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2966 | description='API.INVALID_HYBRID_POWER_STATION_LOAD_NAME') |
|
| 2967 | name = str.strip(new_values['data']['name']) |
|
| 2968 | ||
| 2969 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 2970 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 2971 | new_values['data']['power_point_id'] <= 0: |
|
| 2972 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2973 | description='API.INVALID_POWER_POINT_ID') |
|
| 2974 | power_point_id = new_values['data']['power_point_id'] |
|
| 2975 | ||
| 2976 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 2977 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 2978 | new_values['data']['meter_id'] <= 0: |
|
| 2979 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2980 | description='API.INVALID_METER_ID') |
|
| 2981 | meter_id = new_values['data']['meter_id'] |
|
| 2982 | ||
| 2983 | if 'rated_input_power' not in new_values['data'].keys() or \ |
|
| 2984 | not (isinstance(new_values['data']['rated_input_power'], float) or |
|
| 2985 | isinstance(new_values['data']['rated_input_power'], int)): |
|
| 2986 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2987 | description='API.INVALID_RATED_INPUT_POWER') |
|
| 2988 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
|
| 2989 | ||
| 2990 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2991 | cursor = cnx.cursor() |
|
| 2992 | ||
| 2993 | cursor.execute(" SELECT name " |
|
| 2994 | " FROM tbl_hybrid_power_stations " |
|
| 2995 | " WHERE id = %s ", (id_,)) |
|
| 2996 | if cursor.fetchone() is None: |
|
| 2997 | cursor.close() |
|
| 2998 | cnx.close() |
|
| 2999 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3000 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
| 3001 | ||
| 3002 | cursor.execute(" SELECT name " |
|
| 3003 | " FROM tbl_hybrid_power_stations_loads " |
|
| 3004 | " WHERE id = %s ", (lid,)) |
|
| 3005 | if cursor.fetchone() is None: |
|
| 3006 | cursor.close() |
|
| 3007 | cnx.close() |
|
| 3008 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3009 | description='API.HYBRID_POWER_STATION_LOAD_NOT_FOUND') |
|
| 3010 | ||
| 3011 | cursor.execute(" SELECT name " |
|
| 3012 | " FROM tbl_hybrid_power_stations_loads " |
|
| 3013 | " WHERE hybrid_power_station_id = %s AND name = %s AND id != %s ", |
|
| 3014 | (id_, name, lid)) |
|
| 3015 | if cursor.fetchone() is not None: |
|
| 3016 | cursor.close() |
|
| 3017 | cnx.close() |
|
| 3018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3019 | description='API.HYBRID_POWER_STATION_LOAD_NAME_IS_ALREADY_IN_USE') |
|
| 3020 | ||
| 3021 | cursor.execute(" SELECT name " |
|
| 3022 | " FROM tbl_points " |
|
| 3023 | " WHERE id = %s ", |
|
| 3024 | (power_point_id,)) |
|
| 3025 | if cursor.fetchone() is None: |
|
| 3026 | cursor.close() |
|
| 3027 | cnx.close() |
|
| 3028 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3029 | description='API.POWER_POINT_NOT_FOUND') |
|
| 3030 | ||
| 3031 | cursor.execute(" SELECT name " |
|
| 3032 | " FROM tbl_meters " |
|
| 3033 | " WHERE id = %s ", |
|
| 3034 | (meter_id,)) |
|
| 3035 | if cursor.fetchone() is None: |
|
| 3036 | cursor.close() |
|
| 3037 | cnx.close() |
|
| 3038 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3039 | description='API.METER_NOT_FOUND') |
|
| 3040 | ||
| 3041 | update_row = (" UPDATE tbl_hybrid_power_stations_loads " |
|
| 3042 | " SET name = %s, hybrid_power_station_id = %s, power_point_id = %s, " |
|
| 3043 | " meter_id = %s, rated_input_power = %s " |
|
| 3044 | " WHERE id = %s ") |
|
| 3045 | cursor.execute(update_row, (name, |
|
| 3046 | id_, |
|
| 3047 | power_point_id, |
|
| 3048 | meter_id, |
|
| 3049 | rated_input_power, |
|
| 3050 | lid)) |
|
| 3051 | cnx.commit() |
|
| 3052 | ||
| 3053 | cursor.close() |
|
| 3054 | cnx.close() |
|
| 3055 | ||
| 3056 | resp.status = falcon.HTTP_200 |
|
| 3057 | ||
| 3058 | ||
| 3059 | class HybridPowerStationLoadPointCollection: |
|
| @@ 3771-4014 (lines=244) @@ | ||
| 3768 | resp.location = '/microgrids' + str(id_) + '/photovoltaics/' + str(new_id) |
|
| 3769 | ||
| 3770 | ||
| 3771 | class MicrogridPhotovoltaicItem: |
|
| 3772 | def __init__(self): |
|
| 3773 | """Initializes MicrogridPhotovoltaicItem""" |
|
| 3774 | pass |
|
| 3775 | ||
| 3776 | @staticmethod |
|
| 3777 | def on_options(req, resp, id_, pid): |
|
| 3778 | resp.status = falcon.HTTP_200 |
|
| 3779 | ||
| 3780 | @staticmethod |
|
| 3781 | def on_get(req, resp, id_, pid): |
|
| 3782 | access_control(req) |
|
| 3783 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3784 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3785 | description='API.INVALID_MICROGRID_ID') |
|
| 3786 | if not pid.isdigit() or int(pid) <= 0: |
|
| 3787 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3788 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
|
| 3789 | ||
| 3790 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3791 | cursor = cnx.cursor() |
|
| 3792 | ||
| 3793 | cursor.execute(" SELECT name " |
|
| 3794 | " FROM tbl_microgrids " |
|
| 3795 | " WHERE id = %s ", (id_,)) |
|
| 3796 | if cursor.fetchone() is None: |
|
| 3797 | cursor.close() |
|
| 3798 | cnx.close() |
|
| 3799 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3800 | description='API.MICROGRID_NOT_FOUND') |
|
| 3801 | ||
| 3802 | # query microgrid dict |
|
| 3803 | query = (" SELECT id, name, uuid " |
|
| 3804 | " FROM tbl_microgrids ") |
|
| 3805 | cursor.execute(query) |
|
| 3806 | rows_microgrids = cursor.fetchall() |
|
| 3807 | ||
| 3808 | microgrid_dict = dict() |
|
| 3809 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
| 3810 | for row in rows_microgrids: |
|
| 3811 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 3812 | "name": row[1], |
|
| 3813 | "uuid": row[2]} |
|
| 3814 | # query meter dict |
|
| 3815 | query = (" SELECT id, name, uuid " |
|
| 3816 | " FROM tbl_meters ") |
|
| 3817 | cursor.execute(query) |
|
| 3818 | rows_meters = cursor.fetchall() |
|
| 3819 | ||
| 3820 | meter_dict = dict() |
|
| 3821 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 3822 | for row in rows_meters: |
|
| 3823 | meter_dict[row[0]] = {"id": row[0], |
|
| 3824 | "name": row[1], |
|
| 3825 | "uuid": row[2]} |
|
| 3826 | # query point dict |
|
| 3827 | query = (" SELECT id, name " |
|
| 3828 | " FROM tbl_points ") |
|
| 3829 | cursor.execute(query) |
|
| 3830 | rows_points = cursor.fetchall() |
|
| 3831 | ||
| 3832 | point_dict = dict() |
|
| 3833 | if rows_points is not None and len(rows_points) > 0: |
|
| 3834 | for row in rows_points: |
|
| 3835 | point_dict[row[0]] = {"id": row[0], |
|
| 3836 | "name": row[1]} |
|
| 3837 | ||
| 3838 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_power " |
|
| 3839 | " FROM tbl_microgrids_photovoltaics " |
|
| 3840 | " WHERE id = %s ") |
|
| 3841 | cursor.execute(query, (pid,)) |
|
| 3842 | row = cursor.fetchone() |
|
| 3843 | cursor.close() |
|
| 3844 | cnx.close() |
|
| 3845 | ||
| 3846 | if row is None: |
|
| 3847 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3848 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
|
| 3849 | else: |
|
| 3850 | meta_result = {"id": row[0], |
|
| 3851 | "name": row[1], |
|
| 3852 | "uuid": row[2], |
|
| 3853 | "microgrid": microgrid_dict.get(row[3], None), |
|
| 3854 | "power_point": point_dict.get(row[4], None), |
|
| 3855 | "meter": meter_dict.get(row[5], None), |
|
| 3856 | "rated_power": row[6]} |
|
| 3857 | ||
| 3858 | resp.text = json.dumps(meta_result) |
|
| 3859 | ||
| 3860 | @staticmethod |
|
| 3861 | @user_logger |
|
| 3862 | def on_delete(req, resp, id_, pid): |
|
| 3863 | admin_control(req) |
|
| 3864 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3865 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3866 | description='API.INVALID_MICROGRID_ID') |
|
| 3867 | if not pid.isdigit() or int(pid) <= 0: |
|
| 3868 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3869 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
|
| 3870 | ||
| 3871 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3872 | cursor = cnx.cursor() |
|
| 3873 | ||
| 3874 | cursor.execute(" SELECT name " |
|
| 3875 | " FROM tbl_microgrids " |
|
| 3876 | " WHERE id = %s ", (id_,)) |
|
| 3877 | if cursor.fetchone() is None: |
|
| 3878 | cursor.close() |
|
| 3879 | cnx.close() |
|
| 3880 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3881 | description='API.MICROGRID_NOT_FOUND') |
|
| 3882 | ||
| 3883 | cursor.execute(" SELECT name " |
|
| 3884 | " FROM tbl_microgrids_photovoltaics " |
|
| 3885 | " WHERE id = %s ", (pid,)) |
|
| 3886 | if cursor.fetchone() is None: |
|
| 3887 | cursor.close() |
|
| 3888 | cnx.close() |
|
| 3889 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3890 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
|
| 3891 | ||
| 3892 | cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics " |
|
| 3893 | " WHERE id = %s ", (pid,)) |
|
| 3894 | cnx.commit() |
|
| 3895 | ||
| 3896 | cursor.close() |
|
| 3897 | cnx.close() |
|
| 3898 | ||
| 3899 | resp.status = falcon.HTTP_204 |
|
| 3900 | ||
| 3901 | @staticmethod |
|
| 3902 | @user_logger |
|
| 3903 | def on_put(req, resp, id_, pid): |
|
| 3904 | """Handles PUT requests""" |
|
| 3905 | admin_control(req) |
|
| 3906 | try: |
|
| 3907 | raw_json = req.stream.read().decode('utf-8') |
|
| 3908 | except Exception as ex: |
|
| 3909 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3910 | title='API.BAD_REQUEST', |
|
| 3911 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3912 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3913 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3914 | description='API.INVALID_MICROGRID_ID') |
|
| 3915 | if not pid.isdigit() or int(pid) <= 0: |
|
| 3916 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3917 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
|
| 3918 | ||
| 3919 | new_values = json.loads(raw_json) |
|
| 3920 | ||
| 3921 | if 'name' not in new_values['data'].keys() or \ |
|
| 3922 | not isinstance(new_values['data']['name'], str) or \ |
|
| 3923 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 3924 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3925 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_NAME') |
|
| 3926 | name = str.strip(new_values['data']['name']) |
|
| 3927 | ||
| 3928 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 3929 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 3930 | new_values['data']['power_point_id'] <= 0: |
|
| 3931 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3932 | description='API.INVALID_POWER_POINT_ID') |
|
| 3933 | power_point_id = new_values['data']['power_point_id'] |
|
| 3934 | ||
| 3935 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 3936 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 3937 | new_values['data']['meter_id'] <= 0: |
|
| 3938 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3939 | description='API.INVALID_METER_ID') |
|
| 3940 | meter_id = new_values['data']['meter_id'] |
|
| 3941 | ||
| 3942 | if 'rated_power' not in new_values['data'].keys() or \ |
|
| 3943 | not (isinstance(new_values['data']['rated_power'], float) or |
|
| 3944 | isinstance(new_values['data']['rated_power'], int)): |
|
| 3945 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3946 | description='API.INVALID_RATED_POWER') |
|
| 3947 | rated_power = float(new_values['data']['rated_power']) |
|
| 3948 | ||
| 3949 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3950 | cursor = cnx.cursor() |
|
| 3951 | ||
| 3952 | cursor.execute(" SELECT name " |
|
| 3953 | " FROM tbl_microgrids " |
|
| 3954 | " WHERE id = %s ", (id_,)) |
|
| 3955 | if cursor.fetchone() is None: |
|
| 3956 | cursor.close() |
|
| 3957 | cnx.close() |
|
| 3958 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3959 | description='API.MICROGRID_NOT_FOUND') |
|
| 3960 | ||
| 3961 | cursor.execute(" SELECT name " |
|
| 3962 | " FROM tbl_microgrids_photovoltaics " |
|
| 3963 | " WHERE id = %s ", (pid,)) |
|
| 3964 | if cursor.fetchone() is None: |
|
| 3965 | cursor.close() |
|
| 3966 | cnx.close() |
|
| 3967 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3968 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
|
| 3969 | ||
| 3970 | cursor.execute(" SELECT name " |
|
| 3971 | " FROM tbl_microgrids_photovoltaics " |
|
| 3972 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
| 3973 | (id_, name, pid)) |
|
| 3974 | if cursor.fetchone() is not None: |
|
| 3975 | cursor.close() |
|
| 3976 | cnx.close() |
|
| 3977 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3978 | description='API.MICROGRID_PHOTOVOLTAIC_NAME_IS_ALREADY_IN_USE') |
|
| 3979 | ||
| 3980 | cursor.execute(" SELECT name " |
|
| 3981 | " FROM tbl_points " |
|
| 3982 | " WHERE id = %s ", |
|
| 3983 | (power_point_id,)) |
|
| 3984 | if cursor.fetchone() is None: |
|
| 3985 | cursor.close() |
|
| 3986 | cnx.close() |
|
| 3987 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3988 | description='API.POWER_POINT_NOT_FOUND') |
|
| 3989 | ||
| 3990 | cursor.execute(" SELECT name " |
|
| 3991 | " FROM tbl_meters " |
|
| 3992 | " WHERE id = %s ", |
|
| 3993 | (meter_id,)) |
|
| 3994 | if cursor.fetchone() is None: |
|
| 3995 | cursor.close() |
|
| 3996 | cnx.close() |
|
| 3997 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3998 | description='API.METER_NOT_FOUND') |
|
| 3999 | ||
| 4000 | update_row = (" UPDATE tbl_microgrids_photovoltaics " |
|
| 4001 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_power = %s " |
|
| 4002 | " WHERE id = %s ") |
|
| 4003 | cursor.execute(update_row, (name, |
|
| 4004 | id_, |
|
| 4005 | power_point_id, |
|
| 4006 | meter_id, |
|
| 4007 | rated_power, |
|
| 4008 | pid)) |
|
| 4009 | cnx.commit() |
|
| 4010 | ||
| 4011 | cursor.close() |
|
| 4012 | cnx.close() |
|
| 4013 | ||
| 4014 | resp.status = falcon.HTTP_200 |
|
| 4015 | ||
| 4016 | ||
| 4017 | class MicrogridPowerconversionsystemCollection: |
|
| @@ 3333-3576 (lines=244) @@ | ||
| 3330 | resp.location = '/microgrids/' + str(id_) + '/loads/' + str(new_id) |
|
| 3331 | ||
| 3332 | ||
| 3333 | class MicrogridLoadItem: |
|
| 3334 | def __init__(self): |
|
| 3335 | """Initializes MicrogridLoadItem""" |
|
| 3336 | pass |
|
| 3337 | ||
| 3338 | @staticmethod |
|
| 3339 | def on_options(req, resp, id_, lid): |
|
| 3340 | resp.status = falcon.HTTP_200 |
|
| 3341 | ||
| 3342 | @staticmethod |
|
| 3343 | def on_get(req, resp, id_, lid): |
|
| 3344 | access_control(req) |
|
| 3345 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3347 | description='API.INVALID_MICROGRID_ID') |
|
| 3348 | if not lid.isdigit() or int(lid) <= 0: |
|
| 3349 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3350 | description='API.INVALID_MICROGRID_LOAD_ID') |
|
| 3351 | ||
| 3352 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3353 | cursor = cnx.cursor() |
|
| 3354 | ||
| 3355 | cursor.execute(" SELECT name " |
|
| 3356 | " FROM tbl_microgrids " |
|
| 3357 | " WHERE id = %s ", (id_,)) |
|
| 3358 | if cursor.fetchone() is None: |
|
| 3359 | cursor.close() |
|
| 3360 | cnx.close() |
|
| 3361 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3362 | description='API.MICROGRID_NOT_FOUND') |
|
| 3363 | ||
| 3364 | # query microgrid dict |
|
| 3365 | query = (" SELECT id, name, uuid " |
|
| 3366 | " FROM tbl_microgrids ") |
|
| 3367 | cursor.execute(query) |
|
| 3368 | rows_microgrids = cursor.fetchall() |
|
| 3369 | ||
| 3370 | microgrid_dict = dict() |
|
| 3371 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
| 3372 | for row in rows_microgrids: |
|
| 3373 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 3374 | "name": row[1], |
|
| 3375 | "uuid": row[2]} |
|
| 3376 | # query meter dict |
|
| 3377 | query = (" SELECT id, name, uuid " |
|
| 3378 | " FROM tbl_meters ") |
|
| 3379 | cursor.execute(query) |
|
| 3380 | rows_meters = cursor.fetchall() |
|
| 3381 | ||
| 3382 | meter_dict = dict() |
|
| 3383 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 3384 | for row in rows_meters: |
|
| 3385 | meter_dict[row[0]] = {"id": row[0], |
|
| 3386 | "name": row[1], |
|
| 3387 | "uuid": row[2]} |
|
| 3388 | # query point dict |
|
| 3389 | query = (" SELECT id, name " |
|
| 3390 | " FROM tbl_points ") |
|
| 3391 | cursor.execute(query) |
|
| 3392 | rows_points = cursor.fetchall() |
|
| 3393 | ||
| 3394 | point_dict = dict() |
|
| 3395 | if rows_points is not None and len(rows_points) > 0: |
|
| 3396 | for row in rows_points: |
|
| 3397 | point_dict[row[0]] = {"id": row[0], |
|
| 3398 | "name": row[1]} |
|
| 3399 | ||
| 3400 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_input_power " |
|
| 3401 | " FROM tbl_microgrids_loads " |
|
| 3402 | " WHERE id = %s ") |
|
| 3403 | cursor.execute(query, (lid,)) |
|
| 3404 | row = cursor.fetchone() |
|
| 3405 | cursor.close() |
|
| 3406 | cnx.close() |
|
| 3407 | ||
| 3408 | if row is None: |
|
| 3409 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3410 | description='API.MICROGRID_LOAD_NOT_FOUND') |
|
| 3411 | else: |
|
| 3412 | meta_result = {"id": row[0], |
|
| 3413 | "name": row[1], |
|
| 3414 | "uuid": row[2], |
|
| 3415 | "microgrid": microgrid_dict.get(row[3], None), |
|
| 3416 | "power_point": point_dict.get(row[4], None), |
|
| 3417 | "meter": meter_dict.get(row[5], None), |
|
| 3418 | "rated_input_power": row[6]} |
|
| 3419 | ||
| 3420 | resp.text = json.dumps(meta_result) |
|
| 3421 | ||
| 3422 | @staticmethod |
|
| 3423 | @user_logger |
|
| 3424 | def on_delete(req, resp, id_, lid): |
|
| 3425 | admin_control(req) |
|
| 3426 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3427 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3428 | description='API.INVALID_MICROGRID_ID') |
|
| 3429 | if not lid.isdigit() or int(lid) <= 0: |
|
| 3430 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3431 | description='API.INVALID_MICROGRID_LOAD_ID') |
|
| 3432 | ||
| 3433 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3434 | cursor = cnx.cursor() |
|
| 3435 | ||
| 3436 | cursor.execute(" SELECT name " |
|
| 3437 | " FROM tbl_microgrids " |
|
| 3438 | " WHERE id = %s ", (id_,)) |
|
| 3439 | if cursor.fetchone() is None: |
|
| 3440 | cursor.close() |
|
| 3441 | cnx.close() |
|
| 3442 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3443 | description='API.MICROGRID_NOT_FOUND') |
|
| 3444 | ||
| 3445 | cursor.execute(" SELECT name " |
|
| 3446 | " FROM tbl_microgrids_loads " |
|
| 3447 | " WHERE id = %s ", (lid,)) |
|
| 3448 | if cursor.fetchone() is None: |
|
| 3449 | cursor.close() |
|
| 3450 | cnx.close() |
|
| 3451 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3452 | description='API.MICROGRID_LOAD_NOT_FOUND') |
|
| 3453 | ||
| 3454 | cursor.execute(" DELETE FROM tbl_microgrids_loads " |
|
| 3455 | " WHERE id = %s ", (lid,)) |
|
| 3456 | cnx.commit() |
|
| 3457 | ||
| 3458 | cursor.close() |
|
| 3459 | cnx.close() |
|
| 3460 | ||
| 3461 | resp.status = falcon.HTTP_204 |
|
| 3462 | ||
| 3463 | @staticmethod |
|
| 3464 | @user_logger |
|
| 3465 | def on_put(req, resp, id_, lid): |
|
| 3466 | """Handles PUT requests""" |
|
| 3467 | admin_control(req) |
|
| 3468 | try: |
|
| 3469 | raw_json = req.stream.read().decode('utf-8') |
|
| 3470 | except Exception as ex: |
|
| 3471 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3472 | title='API.BAD_REQUEST', |
|
| 3473 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3474 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3476 | description='API.INVALID_MICROGRID_ID') |
|
| 3477 | if not lid.isdigit() or int(lid) <= 0: |
|
| 3478 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3479 | description='API.INVALID_MICROGRID_LOAD_ID') |
|
| 3480 | ||
| 3481 | new_values = json.loads(raw_json) |
|
| 3482 | ||
| 3483 | if 'name' not in new_values['data'].keys() or \ |
|
| 3484 | not isinstance(new_values['data']['name'], str) or \ |
|
| 3485 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 3486 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3487 | description='API.INVALID_MICROGRID_LOAD_NAME') |
|
| 3488 | name = str.strip(new_values['data']['name']) |
|
| 3489 | ||
| 3490 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 3491 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 3492 | new_values['data']['power_point_id'] <= 0: |
|
| 3493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3494 | description='API.INVALID_POWER_POINT_ID') |
|
| 3495 | power_point_id = new_values['data']['power_point_id'] |
|
| 3496 | ||
| 3497 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 3498 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 3499 | new_values['data']['meter_id'] <= 0: |
|
| 3500 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3501 | description='API.INVALID_METER_ID') |
|
| 3502 | meter_id = new_values['data']['meter_id'] |
|
| 3503 | ||
| 3504 | if 'rated_input_power' not in new_values['data'].keys() or \ |
|
| 3505 | not (isinstance(new_values['data']['rated_input_power'], float) or |
|
| 3506 | isinstance(new_values['data']['rated_input_power'], int)): |
|
| 3507 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3508 | description='API.INVALID_RATED_INPUT_POWER') |
|
| 3509 | rated_input_power = float(new_values['data']['rated_input_power']) |
|
| 3510 | ||
| 3511 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3512 | cursor = cnx.cursor() |
|
| 3513 | ||
| 3514 | cursor.execute(" SELECT name " |
|
| 3515 | " FROM tbl_microgrids " |
|
| 3516 | " WHERE id = %s ", (id_,)) |
|
| 3517 | if cursor.fetchone() is None: |
|
| 3518 | cursor.close() |
|
| 3519 | cnx.close() |
|
| 3520 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3521 | description='API.MICROGRID_NOT_FOUND') |
|
| 3522 | ||
| 3523 | cursor.execute(" SELECT name " |
|
| 3524 | " FROM tbl_microgrids_loads " |
|
| 3525 | " WHERE id = %s ", (lid,)) |
|
| 3526 | if cursor.fetchone() is None: |
|
| 3527 | cursor.close() |
|
| 3528 | cnx.close() |
|
| 3529 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3530 | description='API.MICROGRID_LOAD_NOT_FOUND') |
|
| 3531 | ||
| 3532 | cursor.execute(" SELECT name " |
|
| 3533 | " FROM tbl_microgrids_loads " |
|
| 3534 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
| 3535 | (id_, name, lid)) |
|
| 3536 | if cursor.fetchone() is not None: |
|
| 3537 | cursor.close() |
|
| 3538 | cnx.close() |
|
| 3539 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3540 | description='API.MICROGRID_LOAD_NAME_IS_ALREADY_IN_USE') |
|
| 3541 | ||
| 3542 | cursor.execute(" SELECT name " |
|
| 3543 | " FROM tbl_points " |
|
| 3544 | " WHERE id = %s ", |
|
| 3545 | (power_point_id,)) |
|
| 3546 | if cursor.fetchone() is None: |
|
| 3547 | cursor.close() |
|
| 3548 | cnx.close() |
|
| 3549 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3550 | description='API.POWER_POINT_NOT_FOUND') |
|
| 3551 | ||
| 3552 | cursor.execute(" SELECT name " |
|
| 3553 | " FROM tbl_meters " |
|
| 3554 | " WHERE id = %s ", |
|
| 3555 | (meter_id,)) |
|
| 3556 | if cursor.fetchone() is None: |
|
| 3557 | cursor.close() |
|
| 3558 | cnx.close() |
|
| 3559 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3560 | description='API.METER_NOT_FOUND') |
|
| 3561 | ||
| 3562 | update_row = (" UPDATE tbl_microgrids_loads " |
|
| 3563 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_input_power = %s " |
|
| 3564 | " WHERE id = %s ") |
|
| 3565 | cursor.execute(update_row, (name, |
|
| 3566 | id_, |
|
| 3567 | power_point_id, |
|
| 3568 | meter_id, |
|
| 3569 | rated_input_power, |
|
| 3570 | lid)) |
|
| 3571 | cnx.commit() |
|
| 3572 | ||
| 3573 | cursor.close() |
|
| 3574 | cnx.close() |
|
| 3575 | ||
| 3576 | resp.status = falcon.HTTP_200 |
|
| 3577 | ||
| 3578 | ||
| 3579 | class MicrogridPhotovoltaicCollection: |
|
| @@ 1902-2145 (lines=244) @@ | ||
| 1899 | resp.location = '/microgrids/' + str(id_) + '/generators/' + str(new_id) |
|
| 1900 | ||
| 1901 | ||
| 1902 | class MicrogridGeneratorItem: |
|
| 1903 | def __init__(self): |
|
| 1904 | """Initializes MicrogridGeneratorItem""" |
|
| 1905 | pass |
|
| 1906 | ||
| 1907 | @staticmethod |
|
| 1908 | def on_options(req, resp, id_, gid): |
|
| 1909 | resp.status = falcon.HTTP_200 |
|
| 1910 | ||
| 1911 | @staticmethod |
|
| 1912 | def on_get(req, resp, id_, gid): |
|
| 1913 | access_control(req) |
|
| 1914 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1915 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1916 | description='API.INVALID_MICROGRID_ID') |
|
| 1917 | if not gid.isdigit() or int(gid) <= 0: |
|
| 1918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1919 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
|
| 1920 | ||
| 1921 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1922 | cursor = cnx.cursor() |
|
| 1923 | ||
| 1924 | cursor.execute(" SELECT name " |
|
| 1925 | " FROM tbl_microgrids " |
|
| 1926 | " WHERE id = %s ", (id_,)) |
|
| 1927 | if cursor.fetchone() is None: |
|
| 1928 | cursor.close() |
|
| 1929 | cnx.close() |
|
| 1930 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1931 | description='API.MICROGRID_NOT_FOUND') |
|
| 1932 | ||
| 1933 | # query microgrid dict |
|
| 1934 | query = (" SELECT id, name, uuid " |
|
| 1935 | " FROM tbl_microgrids ") |
|
| 1936 | cursor.execute(query) |
|
| 1937 | rows_microgrids = cursor.fetchall() |
|
| 1938 | ||
| 1939 | microgrid_dict = dict() |
|
| 1940 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
| 1941 | for row in rows_microgrids: |
|
| 1942 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 1943 | "name": row[1], |
|
| 1944 | "uuid": row[2]} |
|
| 1945 | # query meter dict |
|
| 1946 | query = (" SELECT id, name, uuid " |
|
| 1947 | " FROM tbl_meters ") |
|
| 1948 | cursor.execute(query) |
|
| 1949 | rows_meters = cursor.fetchall() |
|
| 1950 | ||
| 1951 | meter_dict = dict() |
|
| 1952 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 1953 | for row in rows_meters: |
|
| 1954 | meter_dict[row[0]] = {"id": row[0], |
|
| 1955 | "name": row[1], |
|
| 1956 | "uuid": row[2]} |
|
| 1957 | # query point dict |
|
| 1958 | query = (" SELECT id, name " |
|
| 1959 | " FROM tbl_points ") |
|
| 1960 | cursor.execute(query) |
|
| 1961 | rows_points = cursor.fetchall() |
|
| 1962 | ||
| 1963 | point_dict = dict() |
|
| 1964 | if rows_points is not None and len(rows_points) > 0: |
|
| 1965 | for row in rows_points: |
|
| 1966 | point_dict[row[0]] = {"id": row[0], |
|
| 1967 | "name": row[1]} |
|
| 1968 | ||
| 1969 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
|
| 1970 | " FROM tbl_microgrids_generators " |
|
| 1971 | " WHERE id = %s ") |
|
| 1972 | cursor.execute(query, (gid,)) |
|
| 1973 | row = cursor.fetchone() |
|
| 1974 | cursor.close() |
|
| 1975 | cnx.close() |
|
| 1976 | ||
| 1977 | if row is None: |
|
| 1978 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1979 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
|
| 1980 | else: |
|
| 1981 | meta_result = {"id": row[0], |
|
| 1982 | "name": row[1], |
|
| 1983 | "uuid": row[2], |
|
| 1984 | "microgrid": microgrid_dict.get(row[3]), |
|
| 1985 | "power_point": point_dict.get(row[4]), |
|
| 1986 | "meter": meter_dict.get(row[5]), |
|
| 1987 | "rated_output_power": row[6]} |
|
| 1988 | ||
| 1989 | resp.text = json.dumps(meta_result) |
|
| 1990 | ||
| 1991 | @staticmethod |
|
| 1992 | @user_logger |
|
| 1993 | def on_delete(req, resp, id_, gid): |
|
| 1994 | admin_control(req) |
|
| 1995 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1996 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1997 | description='API.INVALID_MICROGRID_ID') |
|
| 1998 | if not gid.isdigit() or int(gid) <= 0: |
|
| 1999 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2000 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
|
| 2001 | ||
| 2002 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2003 | cursor = cnx.cursor() |
|
| 2004 | ||
| 2005 | cursor.execute(" SELECT name " |
|
| 2006 | " FROM tbl_microgrids " |
|
| 2007 | " WHERE id = %s ", (id_,)) |
|
| 2008 | if cursor.fetchone() is None: |
|
| 2009 | cursor.close() |
|
| 2010 | cnx.close() |
|
| 2011 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2012 | description='API.MICROGRID_NOT_FOUND') |
|
| 2013 | ||
| 2014 | cursor.execute(" SELECT name " |
|
| 2015 | " FROM tbl_microgrids_generators " |
|
| 2016 | " WHERE id = %s ", (gid,)) |
|
| 2017 | if cursor.fetchone() is None: |
|
| 2018 | cursor.close() |
|
| 2019 | cnx.close() |
|
| 2020 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2021 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
|
| 2022 | ||
| 2023 | cursor.execute(" DELETE FROM tbl_microgrids_generators " |
|
| 2024 | " WHERE id = %s ", (gid,)) |
|
| 2025 | cnx.commit() |
|
| 2026 | ||
| 2027 | cursor.close() |
|
| 2028 | cnx.close() |
|
| 2029 | ||
| 2030 | resp.status = falcon.HTTP_204 |
|
| 2031 | ||
| 2032 | @staticmethod |
|
| 2033 | @user_logger |
|
| 2034 | def on_put(req, resp, id_, gid): |
|
| 2035 | """Handles PUT requests""" |
|
| 2036 | admin_control(req) |
|
| 2037 | try: |
|
| 2038 | raw_json = req.stream.read().decode('utf-8') |
|
| 2039 | except Exception as ex: |
|
| 2040 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2041 | title='API.BAD_REQUEST', |
|
| 2042 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2043 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2044 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2045 | description='API.INVALID_MICROGRID_ID') |
|
| 2046 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2047 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2048 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
|
| 2049 | ||
| 2050 | new_values = json.loads(raw_json) |
|
| 2051 | ||
| 2052 | if 'name' not in new_values['data'].keys() or \ |
|
| 2053 | not isinstance(new_values['data']['name'], str) or \ |
|
| 2054 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 2055 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2056 | description='API.INVALID_MICROGRID_GENERATOR_NAME') |
|
| 2057 | name = str.strip(new_values['data']['name']) |
|
| 2058 | ||
| 2059 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 2060 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 2061 | new_values['data']['power_point_id'] <= 0: |
|
| 2062 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2063 | description='API.INVALID_POWER_POINT_ID') |
|
| 2064 | power_point_id = new_values['data']['power_point_id'] |
|
| 2065 | ||
| 2066 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 2067 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 2068 | new_values['data']['meter_id'] <= 0: |
|
| 2069 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2070 | description='API.INVALID_METER_ID') |
|
| 2071 | meter_id = new_values['data']['meter_id'] |
|
| 2072 | ||
| 2073 | if 'rated_output_power' not in new_values['data'].keys() or \ |
|
| 2074 | not (isinstance(new_values['data']['rated_output_power'], float) or |
|
| 2075 | isinstance(new_values['data']['rated_output_power'], int)): |
|
| 2076 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2077 | description='API.INVALID_RATED_OUTPUT_POWER') |
|
| 2078 | rated_output_power = float(new_values['data']['rated_output_power']) |
|
| 2079 | ||
| 2080 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2081 | cursor = cnx.cursor() |
|
| 2082 | ||
| 2083 | cursor.execute(" SELECT name " |
|
| 2084 | " FROM tbl_microgrids " |
|
| 2085 | " WHERE id = %s ", (id_,)) |
|
| 2086 | if cursor.fetchone() is None: |
|
| 2087 | cursor.close() |
|
| 2088 | cnx.close() |
|
| 2089 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2090 | description='API.MICROGRID_NOT_FOUND') |
|
| 2091 | ||
| 2092 | cursor.execute(" SELECT name " |
|
| 2093 | " FROM tbl_microgrids_generators " |
|
| 2094 | " WHERE id = %s ", (gid,)) |
|
| 2095 | if cursor.fetchone() is None: |
|
| 2096 | cursor.close() |
|
| 2097 | cnx.close() |
|
| 2098 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2099 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
|
| 2100 | ||
| 2101 | cursor.execute(" SELECT name " |
|
| 2102 | " FROM tbl_microgrids_generators " |
|
| 2103 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
| 2104 | (id_, name, gid)) |
|
| 2105 | if cursor.fetchone() is not None: |
|
| 2106 | cursor.close() |
|
| 2107 | cnx.close() |
|
| 2108 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2109 | description='API.MICROGRID_GENERATOR_NAME_IS_ALREADY_IN_USE') |
|
| 2110 | ||
| 2111 | cursor.execute(" SELECT name " |
|
| 2112 | " FROM tbl_points " |
|
| 2113 | " WHERE id = %s ", |
|
| 2114 | (power_point_id,)) |
|
| 2115 | if cursor.fetchone() is None: |
|
| 2116 | cursor.close() |
|
| 2117 | cnx.close() |
|
| 2118 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2119 | description='API.POWER_POINT_NOT_FOUND') |
|
| 2120 | ||
| 2121 | cursor.execute(" SELECT name " |
|
| 2122 | " FROM tbl_meters " |
|
| 2123 | " WHERE id = %s ", |
|
| 2124 | (meter_id,)) |
|
| 2125 | if cursor.fetchone() is None: |
|
| 2126 | cursor.close() |
|
| 2127 | cnx.close() |
|
| 2128 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2129 | description='API.METER_NOT_FOUND') |
|
| 2130 | ||
| 2131 | update_row = (" UPDATE tbl_microgrids_generators " |
|
| 2132 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
|
| 2133 | " WHERE id = %s ") |
|
| 2134 | cursor.execute(update_row, (name, |
|
| 2135 | id_, |
|
| 2136 | power_point_id, |
|
| 2137 | meter_id, |
|
| 2138 | rated_output_power, |
|
| 2139 | gid)) |
|
| 2140 | cnx.commit() |
|
| 2141 | ||
| 2142 | cursor.close() |
|
| 2143 | cnx.close() |
|
| 2144 | ||
| 2145 | resp.status = falcon.HTTP_200 |
|
| 2146 | ||
| 2147 | ||
| 2148 | class MicrogridGridCollection: |
|
| @@ 1466-1708 (lines=243) @@ | ||
| 1463 | resp.location = '/microgrids/' + id_ + '/evchargers/' + str(new_id) |
|
| 1464 | ||
| 1465 | ||
| 1466 | class MicrogridEVChargerItem: |
|
| 1467 | def __init__(self): |
|
| 1468 | """Initializes MicrogridEVChargerItem""" |
|
| 1469 | pass |
|
| 1470 | ||
| 1471 | @staticmethod |
|
| 1472 | def on_options(req, resp, id_, eid): |
|
| 1473 | resp.status = falcon.HTTP_200 |
|
| 1474 | ||
| 1475 | @staticmethod |
|
| 1476 | def on_get(req, resp, id_, eid): |
|
| 1477 | access_control(req) |
|
| 1478 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1479 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1480 | description='API.INVALID_MICROGRID_ID') |
|
| 1481 | if not eid.isdigit() or int(eid) <= 0: |
|
| 1482 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1483 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
|
| 1484 | ||
| 1485 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1486 | cursor = cnx.cursor() |
|
| 1487 | ||
| 1488 | cursor.execute(" SELECT name " |
|
| 1489 | " FROM tbl_microgrids " |
|
| 1490 | " WHERE id = %s ", (id_,)) |
|
| 1491 | if cursor.fetchone() is None: |
|
| 1492 | cursor.close() |
|
| 1493 | cnx.close() |
|
| 1494 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1495 | description='API.MICROGRID_NOT_FOUND') |
|
| 1496 | ||
| 1497 | # query microgrid dict |
|
| 1498 | query = (" SELECT id, name, uuid " |
|
| 1499 | " FROM tbl_microgrids ") |
|
| 1500 | cursor.execute(query) |
|
| 1501 | rows_microgrids = cursor.fetchall() |
|
| 1502 | ||
| 1503 | microgrid_dict = dict() |
|
| 1504 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
| 1505 | for row in rows_microgrids: |
|
| 1506 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 1507 | "name": row[1], |
|
| 1508 | "uuid": row[2]} |
|
| 1509 | # query meter dict |
|
| 1510 | query = (" SELECT id, name, uuid " |
|
| 1511 | " FROM tbl_meters ") |
|
| 1512 | cursor.execute(query) |
|
| 1513 | rows_meters = cursor.fetchall() |
|
| 1514 | ||
| 1515 | meter_dict = dict() |
|
| 1516 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 1517 | for row in rows_meters: |
|
| 1518 | meter_dict[row[0]] = {"id": row[0], |
|
| 1519 | "name": row[1], |
|
| 1520 | "uuid": row[2]} |
|
| 1521 | # query point dict |
|
| 1522 | query = (" SELECT id, name " |
|
| 1523 | " FROM tbl_points ") |
|
| 1524 | cursor.execute(query) |
|
| 1525 | rows_points = cursor.fetchall() |
|
| 1526 | ||
| 1527 | point_dict = dict() |
|
| 1528 | if rows_points is not None and len(rows_points) > 0: |
|
| 1529 | for row in rows_points: |
|
| 1530 | point_dict[row[0]] = {"id": row[0], |
|
| 1531 | "name": row[1]} |
|
| 1532 | ||
| 1533 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
|
| 1534 | " FROM tbl_microgrids_evchargers " |
|
| 1535 | " WHERE id = %s ") |
|
| 1536 | cursor.execute(query, (eid,)) |
|
| 1537 | row = cursor.fetchone() |
|
| 1538 | cursor.close() |
|
| 1539 | cnx.close() |
|
| 1540 | ||
| 1541 | if row is None: |
|
| 1542 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1543 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
|
| 1544 | else: |
|
| 1545 | meta_result = {"id": row[0], |
|
| 1546 | "name": row[1], |
|
| 1547 | "uuid": row[2], |
|
| 1548 | "microgrid": microgrid_dict.get(row[3]), |
|
| 1549 | "power_point": point_dict.get(row[4]), |
|
| 1550 | "meter": meter_dict.get(row[5]), |
|
| 1551 | "rated_output_power": row[6]} |
|
| 1552 | ||
| 1553 | resp.text = json.dumps(meta_result) |
|
| 1554 | ||
| 1555 | @staticmethod |
|
| 1556 | @user_logger |
|
| 1557 | def on_delete(req, resp, id_, eid): |
|
| 1558 | admin_control(req) |
|
| 1559 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1560 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1561 | description='API.INVALID_MICROGRID_ID') |
|
| 1562 | if not eid.isdigit() or int(eid) <= 0: |
|
| 1563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1564 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
|
| 1565 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1566 | cursor = cnx.cursor() |
|
| 1567 | ||
| 1568 | cursor.execute(" SELECT name " |
|
| 1569 | " FROM tbl_microgrids " |
|
| 1570 | " WHERE id = %s ", (id_,)) |
|
| 1571 | if cursor.fetchone() is None: |
|
| 1572 | cursor.close() |
|
| 1573 | cnx.close() |
|
| 1574 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1575 | description='API.MICROGRID_NOT_FOUND') |
|
| 1576 | ||
| 1577 | cursor.execute(" SELECT name " |
|
| 1578 | " FROM tbl_microgrids_evchargers " |
|
| 1579 | " WHERE id = %s ", (eid,)) |
|
| 1580 | if cursor.fetchone() is None: |
|
| 1581 | cursor.close() |
|
| 1582 | cnx.close() |
|
| 1583 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1584 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
|
| 1585 | ||
| 1586 | cursor.execute(" DELETE FROM tbl_microgrids_evchargers " |
|
| 1587 | " WHERE id = %s ", (eid,)) |
|
| 1588 | cnx.commit() |
|
| 1589 | ||
| 1590 | cursor.close() |
|
| 1591 | cnx.close() |
|
| 1592 | ||
| 1593 | resp.status = falcon.HTTP_204 |
|
| 1594 | ||
| 1595 | @staticmethod |
|
| 1596 | @user_logger |
|
| 1597 | def on_put(req, resp, id_, eid): |
|
| 1598 | """Handles PUT requests""" |
|
| 1599 | admin_control(req) |
|
| 1600 | try: |
|
| 1601 | raw_json = req.stream.read().decode('utf-8') |
|
| 1602 | except Exception as ex: |
|
| 1603 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1604 | title='API.BAD_REQUEST', |
|
| 1605 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1606 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1607 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1608 | description='API.INVALID_MICROGRID_ID') |
|
| 1609 | if not eid.isdigit() or int(eid) <= 0: |
|
| 1610 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1611 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
|
| 1612 | ||
| 1613 | new_values = json.loads(raw_json) |
|
| 1614 | ||
| 1615 | if 'name' not in new_values['data'].keys() or \ |
|
| 1616 | not isinstance(new_values['data']['name'], str) or \ |
|
| 1617 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 1618 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1619 | description='API.INVALID_MICROGRID_EVCHARGER_NAME') |
|
| 1620 | name = str.strip(new_values['data']['name']) |
|
| 1621 | ||
| 1622 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 1623 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 1624 | new_values['data']['power_point_id'] <= 0: |
|
| 1625 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1626 | description='API.INVALID_POWER_POINT_ID') |
|
| 1627 | power_point_id = new_values['data']['power_point_id'] |
|
| 1628 | ||
| 1629 | if 'meter_id' not in new_values['data'].keys() or \ |
|
| 1630 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
| 1631 | new_values['data']['meter_id'] <= 0: |
|
| 1632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1633 | description='API.INVALID_METER_ID') |
|
| 1634 | meter_id = new_values['data']['meter_id'] |
|
| 1635 | ||
| 1636 | if 'rated_output_power' not in new_values['data'].keys() or \ |
|
| 1637 | not (isinstance(new_values['data']['rated_output_power'], float) or |
|
| 1638 | isinstance(new_values['data']['rated_output_power'], int)): |
|
| 1639 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1640 | description='API.INVALID_RATED_OUTPUT_POWER') |
|
| 1641 | rated_output_power = float(new_values['data']['rated_output_power']) |
|
| 1642 | ||
| 1643 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1644 | cursor = cnx.cursor() |
|
| 1645 | ||
| 1646 | cursor.execute(" SELECT name " |
|
| 1647 | " FROM tbl_microgrids " |
|
| 1648 | " WHERE id = %s ", (id_,)) |
|
| 1649 | if cursor.fetchone() is None: |
|
| 1650 | cursor.close() |
|
| 1651 | cnx.close() |
|
| 1652 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1653 | description='API.MICROGRID_NOT_FOUND') |
|
| 1654 | ||
| 1655 | cursor.execute(" SELECT name " |
|
| 1656 | " FROM tbl_microgrids_evchargers " |
|
| 1657 | " WHERE id = %s ", (eid,)) |
|
| 1658 | if cursor.fetchone() is None: |
|
| 1659 | cursor.close() |
|
| 1660 | cnx.close() |
|
| 1661 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1662 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
|
| 1663 | ||
| 1664 | cursor.execute(" SELECT name " |
|
| 1665 | " FROM tbl_microgrids_evchargers " |
|
| 1666 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
| 1667 | (id_, name, eid)) |
|
| 1668 | if cursor.fetchone() is not None: |
|
| 1669 | cursor.close() |
|
| 1670 | cnx.close() |
|
| 1671 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1672 | description='API.MICROGRID_EVCHARGER_NAME_IS_ALREADY_IN_USE') |
|
| 1673 | ||
| 1674 | cursor.execute(" SELECT name " |
|
| 1675 | " FROM tbl_points " |
|
| 1676 | " WHERE id = %s ", |
|
| 1677 | (power_point_id,)) |
|
| 1678 | if cursor.fetchone() is None: |
|
| 1679 | cursor.close() |
|
| 1680 | cnx.close() |
|
| 1681 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1682 | description='API.POWER_POINT_NOT_FOUND') |
|
| 1683 | ||
| 1684 | cursor.execute(" SELECT name " |
|
| 1685 | " FROM tbl_meters " |
|
| 1686 | " WHERE id = %s ", |
|
| 1687 | (meter_id,)) |
|
| 1688 | if cursor.fetchone() is None: |
|
| 1689 | cursor.close() |
|
| 1690 | cnx.close() |
|
| 1691 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1692 | description='API.METER_NOT_FOUND') |
|
| 1693 | ||
| 1694 | update_row = (" UPDATE tbl_microgrids_evchargers " |
|
| 1695 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
|
| 1696 | " WHERE id = %s ") |
|
| 1697 | cursor.execute(update_row, (name, |
|
| 1698 | id_, |
|
| 1699 | power_point_id, |
|
| 1700 | meter_id, |
|
| 1701 | rated_output_power, |
|
| 1702 | eid)) |
|
| 1703 | cnx.commit() |
|
| 1704 | ||
| 1705 | cursor.close() |
|
| 1706 | cnx.close() |
|
| 1707 | ||
| 1708 | resp.status = falcon.HTTP_200 |
|
| 1709 | ||
| 1710 | ||
| 1711 | class MicrogridGeneratorCollection: |
|