| @@ 5161-5275 (lines=115) @@ | ||
| 5158 | resp.status = falcon.HTTP_204 |
|
| 5159 | ||
| 5160 | ||
| 5161 | class MicrogridUserCollection: |
|
| 5162 | def __init__(self): |
|
| 5163 | pass |
|
| 5164 | ||
| 5165 | @staticmethod |
|
| 5166 | def on_options(req, resp, id_): |
|
| 5167 | _ = req |
|
| 5168 | resp.status = falcon.HTTP_200 |
|
| 5169 | _ = id_ |
|
| 5170 | ||
| 5171 | @staticmethod |
|
| 5172 | def on_get(req, resp, id_): |
|
| 5173 | access_control(req) |
|
| 5174 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5176 | description='API.INVALID_MICROGRID_ID') |
|
| 5177 | ||
| 5178 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5179 | cursor = cnx.cursor() |
|
| 5180 | cursor.execute(" SELECT name " |
|
| 5181 | " FROM tbl_microgrids " |
|
| 5182 | " WHERE id = %s ", (id_,)) |
|
| 5183 | if cursor.fetchone() is None: |
|
| 5184 | cursor.close() |
|
| 5185 | cnx.close() |
|
| 5186 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5187 | description='API.MICROGRID_NOT_FOUND') |
|
| 5188 | ||
| 5189 | query = (" SELECT u.id, u.name, u.uuid " |
|
| 5190 | " FROM tbl_microgrids m, tbl_microgrids_users mu, " |
|
| 5191 | + config.myems_user_db['database'] + ".tbl_users u " |
|
| 5192 | " WHERE mu.microgrid_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
| 5193 | " ORDER BY u.id ") |
|
| 5194 | cursor.execute(query, (id_,)) |
|
| 5195 | rows = cursor.fetchall() |
|
| 5196 | result = list() |
|
| 5197 | if rows is not None and len(rows) > 0: |
|
| 5198 | for row in rows: |
|
| 5199 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 5200 | result.append(meta_result) |
|
| 5201 | ||
| 5202 | cursor.close() |
|
| 5203 | cnx.close() |
|
| 5204 | resp.text = json.dumps(result) |
|
| 5205 | ||
| 5206 | @staticmethod |
|
| 5207 | @user_logger |
|
| 5208 | def on_post(req, resp, id_): |
|
| 5209 | """Handles POST requests""" |
|
| 5210 | admin_control(req) |
|
| 5211 | try: |
|
| 5212 | raw_json = req.stream.read().decode('utf-8') |
|
| 5213 | except UnicodeDecodeError as ex: |
|
| 5214 | print("Failed to decode request") |
|
| 5215 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5216 | title='API.BAD_REQUEST', |
|
| 5217 | description='API.INVALID_ENCODING') |
|
| 5218 | except Exception as ex: |
|
| 5219 | print(str(ex)) |
|
| 5220 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5221 | title='API.BAD_REQUEST', |
|
| 5222 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5223 | ||
| 5224 | if not id_.isdigit() or int(id_) <= 0: |
|
| 5225 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5226 | description='API.INVALID_MICROGRID_ID') |
|
| 5227 | ||
| 5228 | new_values = json.loads(raw_json) |
|
| 5229 | if 'user_id' not in new_values['data'].keys() or \ |
|
| 5230 | not isinstance(new_values['data']['user_id'], int) or \ |
|
| 5231 | new_values['data']['user_id'] <= 0: |
|
| 5232 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5233 | description='API.INVALID_USER_ID') |
|
| 5234 | user_id = new_values['data']['user_id'] |
|
| 5235 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5236 | cursor = cnx.cursor() |
|
| 5237 | cursor.execute(" SELECT name " |
|
| 5238 | " from tbl_microgrids " |
|
| 5239 | " WHERE id = %s ", (id_,)) |
|
| 5240 | if cursor.fetchone() is None: |
|
| 5241 | cursor.close() |
|
| 5242 | cnx.close() |
|
| 5243 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5244 | description='API.MICROGRID_NOT_FOUND') |
|
| 5245 | ||
| 5246 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
| 5247 | cursor_user = cnx_user.cursor() |
|
| 5248 | cursor_user.execute(" SELECT name" |
|
| 5249 | " FROM tbl_users " |
|
| 5250 | " WHERE id = %s ", (user_id,)) |
|
| 5251 | if cursor_user.fetchone() is None: |
|
| 5252 | cursor_user.close() |
|
| 5253 | cnx_user.close() |
|
| 5254 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5255 | description='API.USER_NOT_FOUND') |
|
| 5256 | query = (" SELECT id " |
|
| 5257 | " FROM tbl_microgrids_users " |
|
| 5258 | " WHERE microgrid_id = %s AND user_id = %s") |
|
| 5259 | cursor.execute(query, (id_, user_id,)) |
|
| 5260 | if cursor.fetchone() is not None: |
|
| 5261 | cursor.close() |
|
| 5262 | cnx.close() |
|
| 5263 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5264 | description='API.MICROGRID_USER_RELATION_EXISTS') |
|
| 5265 | add_row = (" INSERT INTO tbl_microgrids_users (microgrid_id, user_id) " |
|
| 5266 | " VALUES (%s, %s) ") |
|
| 5267 | cursor.execute(add_row, (id_, user_id,)) |
|
| 5268 | cnx.commit() |
|
| 5269 | cursor.close() |
|
| 5270 | cnx.close() |
|
| 5271 | cursor_user.close() |
|
| 5272 | cnx_user.close() |
|
| 5273 | ||
| 5274 | resp.status = falcon.HTTP_201 |
|
| 5275 | resp.location = '/microgrids/' + str(id_) + '/users/' + str(user_id) |
|
| 5276 | ||
| 5277 | ||
| 5278 | class MicrogridUserItem: |
|
| @@ 4928-5042 (lines=115) @@ | ||
| 4925 | resp.status = falcon.HTTP_200 |
|
| 4926 | ||
| 4927 | ||
| 4928 | class PhotovoltaicPowerStationUserCollection: |
|
| 4929 | def __init__(self): |
|
| 4930 | pass |
|
| 4931 | ||
| 4932 | @staticmethod |
|
| 4933 | def on_options(req, resp, id_): |
|
| 4934 | _ = req |
|
| 4935 | resp.status = falcon.HTTP_200 |
|
| 4936 | _ = id_ |
|
| 4937 | ||
| 4938 | @staticmethod |
|
| 4939 | def on_get(req, resp, id_): |
|
| 4940 | access_control(req) |
|
| 4941 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4942 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4943 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 4944 | ||
| 4945 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 4946 | cursor = cnx.cursor() |
|
| 4947 | cursor.execute(" SELECT name " |
|
| 4948 | " FROM tbl_photovoltaic_power_stations " |
|
| 4949 | " WHERE id = %s ", (id_,)) |
|
| 4950 | if cursor.fetchone() is None: |
|
| 4951 | cursor.close() |
|
| 4952 | cnx.close() |
|
| 4953 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 4954 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 4955 | ||
| 4956 | query = (" SELECT u.id, u.name, u.uuid " |
|
| 4957 | " FROM tbl_photovoltaic_power_stations m, tbl_photovoltaic_power_stations_users mu, " |
|
| 4958 | + config.myems_user_db['database'] + ".tbl_users u " |
|
| 4959 | " WHERE mu.photovoltaic_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
| 4960 | " ORDER BY u.id ") |
|
| 4961 | cursor.execute(query, (id_,)) |
|
| 4962 | rows = cursor.fetchall() |
|
| 4963 | result = list() |
|
| 4964 | if rows is not None and len(rows) > 0: |
|
| 4965 | for row in rows: |
|
| 4966 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 4967 | result.append(meta_result) |
|
| 4968 | ||
| 4969 | cursor.close() |
|
| 4970 | cnx.close() |
|
| 4971 | resp.text = json.dumps(result) |
|
| 4972 | ||
| 4973 | @staticmethod |
|
| 4974 | @user_logger |
|
| 4975 | def on_post(req, resp, id_): |
|
| 4976 | """Handles POST requests""" |
|
| 4977 | admin_control(req) |
|
| 4978 | try: |
|
| 4979 | raw_json = req.stream.read().decode('utf-8') |
|
| 4980 | except UnicodeDecodeError as ex: |
|
| 4981 | print("Failed to decode request") |
|
| 4982 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4983 | title='API.BAD_REQUEST', |
|
| 4984 | description='API.INVALID_ENCODING') |
|
| 4985 | except Exception as ex: |
|
| 4986 | print("Unexpected error reading request stream") |
|
| 4987 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 4988 | title='API.BAD_REQUEST', |
|
| 4989 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 4990 | ||
| 4991 | if not id_.isdigit() or int(id_) <= 0: |
|
| 4992 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 4993 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
| 4994 | ||
| 4995 | new_values = json.loads(raw_json) |
|
| 4996 | if 'user_id' not in new_values['data'].keys() or \ |
|
| 4997 | not isinstance(new_values['data']['user_id'], int) or \ |
|
| 4998 | new_values['data']['user_id'] <= 0: |
|
| 4999 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5000 | description='API.INVALID_USER_ID') |
|
| 5001 | user_id = new_values['data']['user_id'] |
|
| 5002 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5003 | cursor = cnx.cursor() |
|
| 5004 | cursor.execute(" SELECT name " |
|
| 5005 | " FROM tbl_photovoltaic_power_stations " |
|
| 5006 | " WHERE id = %s ", (id_,)) |
|
| 5007 | if cursor.fetchone() is None: |
|
| 5008 | cursor.close() |
|
| 5009 | cnx.close() |
|
| 5010 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5011 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
| 5012 | ||
| 5013 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
| 5014 | cursor_user = cnx_user.cursor() |
|
| 5015 | cursor_user.execute(" SELECT name" |
|
| 5016 | " FROM tbl_users " |
|
| 5017 | " WHERE id = %s ", (user_id,)) |
|
| 5018 | if cursor_user.fetchone() is None: |
|
| 5019 | cursor_user.close() |
|
| 5020 | cnx_user.close() |
|
| 5021 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5022 | description='API.USER_NOT_FOUND') |
|
| 5023 | query = (" SELECT id " |
|
| 5024 | " FROM tbl_photovoltaic_power_stations_users " |
|
| 5025 | " WHERE photovoltaic_power_station_id = %s AND user_id = %s") |
|
| 5026 | cursor.execute(query, (id_, user_id,)) |
|
| 5027 | if cursor.fetchone() is not None: |
|
| 5028 | cursor.close() |
|
| 5029 | cnx.close() |
|
| 5030 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 5031 | description='API.PHOTOVOLTAIC_POWER_STATION_USER_RELATION_EXISTS') |
|
| 5032 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_users (photovoltaic_power_station_id, user_id) " |
|
| 5033 | " VALUES (%s, %s) ") |
|
| 5034 | cursor.execute(add_row, (id_, user_id,)) |
|
| 5035 | cnx.commit() |
|
| 5036 | cursor.close() |
|
| 5037 | cnx.close() |
|
| 5038 | cursor_user.close() |
|
| 5039 | cnx_user.close() |
|
| 5040 | ||
| 5041 | resp.status = falcon.HTTP_201 |
|
| 5042 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/users/' + str(user_id) |
|
| 5043 | ||
| 5044 | ||
| 5045 | class PhotovoltaicPowerStationUserItem: |
|
| @@ 1036-1150 (lines=115) @@ | ||
| 1033 | resp.text = json.dumps(result) |
|
| 1034 | ||
| 1035 | ||
| 1036 | class EnergyStoragePowerStationUserCollection: |
|
| 1037 | def __init__(self): |
|
| 1038 | pass |
|
| 1039 | ||
| 1040 | @staticmethod |
|
| 1041 | def on_options(req, resp, id_): |
|
| 1042 | _ = req |
|
| 1043 | resp.status = falcon.HTTP_200 |
|
| 1044 | _ = id_ |
|
| 1045 | ||
| 1046 | @staticmethod |
|
| 1047 | def on_get(req, resp, id_): |
|
| 1048 | access_control(req) |
|
| 1049 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1051 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 1052 | ||
| 1053 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1054 | cursor = cnx.cursor() |
|
| 1055 | cursor.execute(" SELECT name " |
|
| 1056 | " FROM tbl_energy_storage_power_stations " |
|
| 1057 | " WHERE id = %s ", (id_,)) |
|
| 1058 | if cursor.fetchone() is None: |
|
| 1059 | cursor.close() |
|
| 1060 | cnx.close() |
|
| 1061 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1062 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 1063 | ||
| 1064 | query = (" SELECT u.id, u.name, u.uuid " |
|
| 1065 | " FROM tbl_energy_storage_power_stations m, tbl_energy_storage_power_stations_users mu, " |
|
| 1066 | + config.myems_user_db['database'] + ".tbl_users u " |
|
| 1067 | " WHERE mu.energy_storage_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
| 1068 | " ORDER BY u.id ") |
|
| 1069 | cursor.execute(query, (id_,)) |
|
| 1070 | rows = cursor.fetchall() |
|
| 1071 | result = list() |
|
| 1072 | if rows is not None and len(rows) > 0: |
|
| 1073 | for row in rows: |
|
| 1074 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
| 1075 | result.append(meta_result) |
|
| 1076 | ||
| 1077 | cursor.close() |
|
| 1078 | cnx.close() |
|
| 1079 | resp.text = json.dumps(result) |
|
| 1080 | ||
| 1081 | @staticmethod |
|
| 1082 | @user_logger |
|
| 1083 | def on_post(req, resp, id_): |
|
| 1084 | """Handles POST requests""" |
|
| 1085 | admin_control(req) |
|
| 1086 | try: |
|
| 1087 | raw_json = req.stream.read().decode('utf-8') |
|
| 1088 | except UnicodeDecodeError as ex: |
|
| 1089 | print("Failed to decode request") |
|
| 1090 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1091 | title='API.BAD_REQUEST', |
|
| 1092 | description='API.INVALID_ENCODING') |
|
| 1093 | except Exception as ex: |
|
| 1094 | print("Unexpected error reading request stream") |
|
| 1095 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 1096 | title='API.BAD_REQUEST', |
|
| 1097 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 1098 | ||
| 1099 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1100 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1101 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
| 1102 | ||
| 1103 | new_values = json.loads(raw_json) |
|
| 1104 | if 'user_id' not in new_values['data'].keys() or \ |
|
| 1105 | not isinstance(new_values['data']['user_id'], int) or \ |
|
| 1106 | new_values['data']['user_id'] <= 0: |
|
| 1107 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1108 | description='API.INVALID_USER_ID') |
|
| 1109 | user_id = new_values['data']['user_id'] |
|
| 1110 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1111 | cursor = cnx.cursor() |
|
| 1112 | cursor.execute(" SELECT name " |
|
| 1113 | " FROM tbl_energy_storage_power_stations " |
|
| 1114 | " WHERE id = %s ", (id_,)) |
|
| 1115 | if cursor.fetchone() is None: |
|
| 1116 | cursor.close() |
|
| 1117 | cnx.close() |
|
| 1118 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1119 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
| 1120 | ||
| 1121 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
| 1122 | cursor_user = cnx_user.cursor() |
|
| 1123 | cursor_user.execute(" SELECT name" |
|
| 1124 | " FROM tbl_users " |
|
| 1125 | " WHERE id = %s ", (user_id,)) |
|
| 1126 | if cursor_user.fetchone() is None: |
|
| 1127 | cursor_user.close() |
|
| 1128 | cnx_user.close() |
|
| 1129 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1130 | description='API.USER_NOT_FOUND') |
|
| 1131 | query = (" SELECT id " |
|
| 1132 | " FROM tbl_energy_storage_power_stations_users " |
|
| 1133 | " WHERE energy_storage_power_station_id = %s AND user_id = %s") |
|
| 1134 | cursor.execute(query, (id_, user_id,)) |
|
| 1135 | if cursor.fetchone() is not None: |
|
| 1136 | cursor.close() |
|
| 1137 | cnx.close() |
|
| 1138 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 1139 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_EXISTS') |
|
| 1140 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_users (energy_storage_power_station_id, user_id) " |
|
| 1141 | " VALUES (%s, %s) ") |
|
| 1142 | cursor.execute(add_row, (id_, user_id,)) |
|
| 1143 | cnx.commit() |
|
| 1144 | cursor.close() |
|
| 1145 | cnx.close() |
|
| 1146 | cursor_user.close() |
|
| 1147 | cnx_user.close() |
|
| 1148 | ||
| 1149 | resp.status = falcon.HTTP_201 |
|
| 1150 | resp.location = '/energystoragepowerstations/' + str(id_) + '/users/' + str(user_id) |
|
| 1151 | ||
| 1152 | ||
| 1153 | class EnergyStoragePowerStationUserItem: |
|