| Total Complexity | 1151 |
| Total Lines | 6270 |
| Duplicated Lines | 69.27 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like core.energystoragecontainer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import uuid |
||
| 2 | import falcon |
||
| 3 | import mysql.connector |
||
| 4 | import simplejson as json |
||
| 5 | from core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
| 6 | import config |
||
| 7 | from datetime import datetime, timedelta |
||
| 8 | from decimal import Decimal |
||
| 9 | |||
| 10 | |||
| 11 | class EnergyStorageContainerCollection: |
||
| 12 | """ |
||
| 13 | Energy Storage Container Collection Resource |
||
| 14 | |||
| 15 | This class handles CRUD operations for energy storage container collection. |
||
| 16 | It provides endpoints for listing all energy storage containers and creating new ones. |
||
| 17 | Energy storage containers represent physical or logical containers that store |
||
| 18 | energy (batteries, capacitors, etc.) in the energy management system. |
||
| 19 | """ |
||
| 20 | def __init__(self): |
||
| 21 | pass |
||
| 22 | |||
| 23 | @staticmethod |
||
| 24 | def on_options(req, resp): |
||
| 25 | """ |
||
| 26 | Handle OPTIONS request for CORS preflight |
||
| 27 | |||
| 28 | Args: |
||
| 29 | req: Falcon request object |
||
| 30 | resp: Falcon response object |
||
| 31 | """ |
||
| 32 | _ = req |
||
| 33 | resp.status = falcon.HTTP_200 |
||
| 34 | |||
| 35 | @staticmethod |
||
| 36 | def on_get(req, resp): |
||
| 37 | """ |
||
| 38 | Handle GET requests to retrieve all energy storage containers |
||
| 39 | |||
| 40 | Returns a list of all energy storage containers with their complete information including: |
||
| 41 | - Container ID, name, and UUID |
||
| 42 | - Associated contact and cost center information |
||
| 43 | - Container specifications and parameters |
||
| 44 | - Related equipment and meter associations |
||
| 45 | |||
| 46 | Args: |
||
| 47 | req: Falcon request object |
||
| 48 | resp: Falcon response object |
||
| 49 | """ |
||
| 50 | access_control(req) |
||
| 51 | search_query = req.get_param('q', default=None) |
||
| 52 | if search_query is not None and len(search_query.strip()) > 0: |
||
| 53 | search_query = search_query.strip() |
||
| 54 | else: |
||
| 55 | search_query = '' |
||
| 56 | |||
| 57 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 58 | cursor = cnx.cursor() |
||
| 59 | |||
| 60 | query = (" SELECT id, name, uuid " |
||
| 61 | " FROM tbl_contacts ") |
||
| 62 | cursor.execute(query) |
||
| 63 | rows_contacts = cursor.fetchall() |
||
| 64 | |||
| 65 | contact_dict = dict() |
||
| 66 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 67 | for row in rows_contacts: |
||
| 68 | contact_dict[row[0]] = {"id": row[0], |
||
| 69 | "name": row[1], |
||
| 70 | "uuid": row[2]} |
||
| 71 | |||
| 72 | query = (" SELECT id, name, uuid " |
||
| 73 | " FROM tbl_cost_centers ") |
||
| 74 | cursor.execute(query) |
||
| 75 | rows_cost_centers = cursor.fetchall() |
||
| 76 | |||
| 77 | cost_center_dict = dict() |
||
| 78 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 79 | for row in rows_cost_centers: |
||
| 80 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 81 | "name": row[1], |
||
| 82 | "uuid": row[2]} |
||
| 83 | |||
| 84 | query = (" SELECT id, name, uuid, " |
||
| 85 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 86 | " FROM tbl_energy_storage_containers ") |
||
| 87 | params = [] |
||
| 88 | if search_query: |
||
| 89 | query += " WHERE name LIKE %s OR description LIKE %s " |
||
| 90 | params = [f'%{search_query}%', f'%{search_query}%'] |
||
| 91 | query += " ORDER BY id " |
||
| 92 | |||
| 93 | cursor.execute(query, params) |
||
| 94 | rows_spaces = cursor.fetchall() |
||
| 95 | |||
| 96 | result = list() |
||
| 97 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 98 | for row in rows_spaces: |
||
| 99 | meta_result = {"id": row[0], |
||
| 100 | "name": row[1], |
||
| 101 | "uuid": row[2], |
||
| 102 | "rated_capacity": row[3], |
||
| 103 | "rated_power": row[4], |
||
| 104 | "contact": contact_dict.get(row[5], None), |
||
| 105 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 106 | "description": row[7], |
||
| 107 | "qrcode": 'energystoragecontainer:' + row[2]} |
||
| 108 | result.append(meta_result) |
||
| 109 | |||
| 110 | cursor.close() |
||
| 111 | cnx.close() |
||
| 112 | resp.text = json.dumps(result) |
||
| 113 | |||
| 114 | @staticmethod |
||
| 115 | @user_logger |
||
| 116 | def on_post(req, resp): |
||
| 117 | """ |
||
| 118 | Handle POST requests to create a new energy storage container |
||
| 119 | |||
| 120 | Creates a new energy storage container with the provided specifications. |
||
| 121 | The container will be empty initially and equipment/meters can be added separately. |
||
| 122 | |||
| 123 | Args: |
||
| 124 | req: Falcon request object containing container data |
||
| 125 | resp: Falcon response object |
||
| 126 | """ |
||
| 127 | admin_control(req) |
||
| 128 | try: |
||
| 129 | raw_json = req.stream.read().decode('utf-8') |
||
| 130 | except UnicodeDecodeError as ex: |
||
| 131 | print("Failed to decode request") |
||
| 132 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 133 | title='API.BAD_REQUEST', |
||
| 134 | description='API.INVALID_ENCODING') |
||
| 135 | except Exception as ex: |
||
| 136 | print("Unexpected error reading request stream") |
||
| 137 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 138 | title='API.BAD_REQUEST', |
||
| 139 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 140 | |||
| 141 | new_values = json.loads(raw_json) |
||
| 142 | |||
| 143 | if 'name' not in new_values['data'].keys() or \ |
||
| 144 | not isinstance(new_values['data']['name'], str) or \ |
||
| 145 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 146 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 147 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME') |
||
| 148 | name = str.strip(new_values['data']['name']) |
||
| 149 | |||
| 150 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 151 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 152 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 153 | new_values['data']['rated_capacity'] < 0.0: |
||
| 154 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 155 | description='API.INVALID_RATED_CAPACITY') |
||
| 156 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 157 | |||
| 158 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 159 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 160 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 161 | new_values['data']['rated_power'] < 0.0: |
||
| 162 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 163 | description='API.INVALID_RATED_POWER') |
||
| 164 | rated_power = new_values['data']['rated_power'] |
||
| 165 | |||
| 166 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 167 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 168 | new_values['data']['contact_id'] <= 0: |
||
| 169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 170 | description='API.INVALID_CONTACT_ID') |
||
| 171 | contact_id = new_values['data']['contact_id'] |
||
| 172 | |||
| 173 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 174 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 175 | new_values['data']['cost_center_id'] <= 0: |
||
| 176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 177 | description='API.INVALID_COST_CENTER_ID') |
||
| 178 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 179 | |||
| 180 | if 'description' in new_values['data'].keys() and \ |
||
| 181 | new_values['data']['description'] is not None and \ |
||
| 182 | len(str(new_values['data']['description'])) > 0: |
||
| 183 | description = str.strip(new_values['data']['description']) |
||
| 184 | else: |
||
| 185 | description = None |
||
| 186 | |||
| 187 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 188 | cursor = cnx.cursor() |
||
| 189 | |||
| 190 | cursor.execute(" SELECT name " |
||
| 191 | " FROM tbl_energy_storage_containers " |
||
| 192 | " WHERE name = %s ", (name,)) |
||
| 193 | if cursor.fetchone() is not None: |
||
| 194 | cursor.close() |
||
| 195 | cnx.close() |
||
| 196 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 197 | description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE') |
||
| 198 | |||
| 199 | cursor.execute(" SELECT name " |
||
| 200 | " FROM tbl_contacts " |
||
| 201 | " WHERE id = %s ", |
||
| 202 | (new_values['data']['contact_id'],)) |
||
| 203 | row = cursor.fetchone() |
||
| 204 | if row is None: |
||
| 205 | cursor.close() |
||
| 206 | cnx.close() |
||
| 207 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 208 | description='API.CONTACT_NOT_FOUND') |
||
| 209 | |||
| 210 | cursor.execute(" SELECT name " |
||
| 211 | " FROM tbl_cost_centers " |
||
| 212 | " WHERE id = %s ", |
||
| 213 | (new_values['data']['cost_center_id'],)) |
||
| 214 | row = cursor.fetchone() |
||
| 215 | if row is None: |
||
| 216 | cursor.close() |
||
| 217 | cnx.close() |
||
| 218 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 219 | description='API.COST_CENTER_NOT_FOUND') |
||
| 220 | |||
| 221 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
||
| 222 | " (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, description) " |
||
| 223 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 224 | cursor.execute(add_values, (name, |
||
| 225 | str(uuid.uuid4()), |
||
| 226 | rated_capacity, |
||
| 227 | rated_power, |
||
| 228 | contact_id, |
||
| 229 | cost_center_id, |
||
| 230 | description)) |
||
| 231 | new_id = cursor.lastrowid |
||
| 232 | cnx.commit() |
||
| 233 | cursor.close() |
||
| 234 | cnx.close() |
||
| 235 | |||
| 236 | resp.status = falcon.HTTP_201 |
||
| 237 | resp.location = '/energystoragecontainers/' + str(new_id) |
||
| 238 | |||
| 239 | |||
| 240 | class EnergyStorageContainerItem: |
||
| 241 | """ |
||
| 242 | Energy Storage Container Item Resource |
||
| 243 | |||
| 244 | This class handles CRUD operations for individual energy storage containers. |
||
| 245 | It provides endpoints for retrieving, updating, and deleting specific |
||
| 246 | energy storage containers by their ID. |
||
| 247 | """ |
||
| 248 | def __init__(self): |
||
| 249 | pass |
||
| 250 | |||
| 251 | @staticmethod |
||
| 252 | def on_options(req, resp, id_): |
||
| 253 | _ = req |
||
| 254 | resp.status = falcon.HTTP_200 |
||
| 255 | _ = id_ |
||
| 256 | |||
| 257 | View Code Duplication | @staticmethod |
|
|
|
|||
| 258 | def on_get(req, resp, id_): |
||
| 259 | access_control(req) |
||
| 260 | if not id_.isdigit() or int(id_) <= 0: |
||
| 261 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 262 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 263 | |||
| 264 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 265 | cursor = cnx.cursor() |
||
| 266 | |||
| 267 | query = (" SELECT id, name, uuid " |
||
| 268 | " FROM tbl_contacts ") |
||
| 269 | cursor.execute(query) |
||
| 270 | rows_contacts = cursor.fetchall() |
||
| 271 | |||
| 272 | contact_dict = dict() |
||
| 273 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 274 | for row in rows_contacts: |
||
| 275 | contact_dict[row[0]] = {"id": row[0], |
||
| 276 | "name": row[1], |
||
| 277 | "uuid": row[2]} |
||
| 278 | |||
| 279 | query = (" SELECT id, name, uuid " |
||
| 280 | " FROM tbl_cost_centers ") |
||
| 281 | cursor.execute(query) |
||
| 282 | rows_cost_centers = cursor.fetchall() |
||
| 283 | |||
| 284 | cost_center_dict = dict() |
||
| 285 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 286 | for row in rows_cost_centers: |
||
| 287 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 288 | "name": row[1], |
||
| 289 | "uuid": row[2]} |
||
| 290 | |||
| 291 | query = (" SELECT id, name, uuid, " |
||
| 292 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 293 | " FROM tbl_energy_storage_containers " |
||
| 294 | " WHERE id = %s ") |
||
| 295 | cursor.execute(query, (id_,)) |
||
| 296 | row = cursor.fetchone() |
||
| 297 | cursor.close() |
||
| 298 | cnx.close() |
||
| 299 | |||
| 300 | if row is None: |
||
| 301 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 302 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 303 | else: |
||
| 304 | meta_result = {"id": row[0], |
||
| 305 | "name": row[1], |
||
| 306 | "uuid": row[2], |
||
| 307 | "rated_capacity": row[3], |
||
| 308 | "rated_power": row[4], |
||
| 309 | "contact": contact_dict.get(row[5], None), |
||
| 310 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 311 | "description": row[7], |
||
| 312 | "qrcode": 'energystoragecontainer:' + row[2]} |
||
| 313 | |||
| 314 | resp.text = json.dumps(meta_result) |
||
| 315 | |||
| 316 | @staticmethod |
||
| 317 | @user_logger |
||
| 318 | def on_delete(req, resp, id_): |
||
| 319 | admin_control(req) |
||
| 320 | if not id_.isdigit() or int(id_) <= 0: |
||
| 321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 322 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 323 | |||
| 324 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 325 | cursor = cnx.cursor() |
||
| 326 | |||
| 327 | cursor.execute(" SELECT name " |
||
| 328 | " FROM tbl_energy_storage_containers " |
||
| 329 | " WHERE id = %s ", (id_,)) |
||
| 330 | if cursor.fetchone() is None: |
||
| 331 | cursor.close() |
||
| 332 | cnx.close() |
||
| 333 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 334 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 335 | |||
| 336 | cursor.execute( |
||
| 337 | " SELECT id " |
||
| 338 | " FROM tbl_energy_storage_power_stations_containers " |
||
| 339 | " WHERE energy_storage_container_id = %s ", |
||
| 340 | (id_,) |
||
| 341 | ) |
||
| 342 | rows_energy_storage_power_stations = cursor.fetchall() |
||
| 343 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
||
| 344 | cursor.close() |
||
| 345 | cnx.close() |
||
| 346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 347 | description='API.THERE_IS_RELATION_WITH_ENERGY_STORAGE_POWER_STATIONS') |
||
| 348 | |||
| 349 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_bmses_points " |
||
| 350 | " WHERE bms_id IN " |
||
| 351 | " (SELECT id FROM tbl_energy_storage_containers_batteries " |
||
| 352 | " WHERE energy_storage_container_id = %s) ", (id_, )) |
||
| 353 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_batteries " |
||
| 354 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 355 | |||
| 356 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_commands " |
||
| 357 | " WHERE energy_storage_container_id = %s ", (id_,)) |
||
| 358 | |||
| 359 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs_points " |
||
| 360 | " WHERE dcdc_id IN " |
||
| 361 | " (SELECT id FROM tbl_energy_storage_containers_dcdcs " |
||
| 362 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 363 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs " |
||
| 364 | " WHERE energy_storage_container_id = %s ", (id_,)) |
||
| 365 | |||
| 366 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols_points " |
||
| 367 | " WHERE firecontrol_id IN " |
||
| 368 | " (SELECT id FROM tbl_energy_storage_containers_firecontrols " |
||
| 369 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 370 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols " |
||
| 371 | " WHERE energy_storage_container_id = %s ", (id_,)) |
||
| 372 | |||
| 373 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids_points " |
||
| 374 | " WHERE grid_id IN " |
||
| 375 | " (SELECT id FROM tbl_energy_storage_containers_grids " |
||
| 376 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 377 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
||
| 378 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 379 | |||
| 380 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs_points " |
||
| 381 | " WHERE hvac_id IN " |
||
| 382 | " (SELECT id FROM tbl_energy_storage_containers_hvacs " |
||
| 383 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 384 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs " |
||
| 385 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 386 | |||
| 387 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads_points " |
||
| 388 | " WHERE load_id IN " |
||
| 389 | " (SELECT id FROM tbl_energy_storage_containers_loads " |
||
| 390 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 391 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads " |
||
| 392 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 393 | |||
| 394 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_pcses_points " |
||
| 395 | " WHERE pcs_id IN " |
||
| 396 | " (SELECT id FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 397 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 398 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 399 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 400 | |||
| 401 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
||
| 402 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 403 | |||
| 404 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses_points " |
||
| 405 | " WHERE sts_id IN " |
||
| 406 | " (SELECT id FROM tbl_energy_storage_containers_stses " |
||
| 407 | " WHERE energy_storage_container_id = %s) ", (id_,)) |
||
| 408 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses " |
||
| 409 | " WHERE energy_storage_container_id = %s ", (id_,)) |
||
| 410 | |||
| 411 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
||
| 412 | " WHERE energy_storage_container_id = %s ", (id_, )) |
||
| 413 | |||
| 414 | cursor.execute(" DELETE FROM tbl_energy_storage_containers " |
||
| 415 | " WHERE id = %s ", (id_,)) |
||
| 416 | cnx.commit() |
||
| 417 | |||
| 418 | cursor.close() |
||
| 419 | cnx.close() |
||
| 420 | |||
| 421 | resp.status = falcon.HTTP_204 |
||
| 422 | |||
| 423 | @staticmethod |
||
| 424 | @user_logger |
||
| 425 | def on_put(req, resp, id_): |
||
| 426 | """Handles PUT requests""" |
||
| 427 | admin_control(req) |
||
| 428 | try: |
||
| 429 | raw_json = req.stream.read().decode('utf-8') |
||
| 430 | except UnicodeDecodeError as ex: |
||
| 431 | print("Failed to decode request") |
||
| 432 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 433 | title='API.BAD_REQUEST', |
||
| 434 | description='API.INVALID_ENCODING') |
||
| 435 | except Exception as ex: |
||
| 436 | print("Unexpected error reading request stream") |
||
| 437 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 438 | title='API.BAD_REQUEST', |
||
| 439 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 440 | |||
| 441 | if not id_.isdigit() or int(id_) <= 0: |
||
| 442 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 443 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 444 | |||
| 445 | new_values = json.loads(raw_json) |
||
| 446 | |||
| 447 | if 'name' not in new_values['data'].keys() or \ |
||
| 448 | not isinstance(new_values['data']['name'], str) or \ |
||
| 449 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 451 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME') |
||
| 452 | name = str.strip(new_values['data']['name']) |
||
| 453 | |||
| 454 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 455 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 456 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 457 | new_values['data']['rated_capacity'] < 0.0: |
||
| 458 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 459 | description='API.INVALID_RATED_CAPACITY') |
||
| 460 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 461 | |||
| 462 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 463 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 464 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 465 | new_values['data']['rated_power'] < 0.0: |
||
| 466 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 467 | description='API.INVALID_RATED_POWER') |
||
| 468 | rated_power = new_values['data']['rated_power'] |
||
| 469 | |||
| 470 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 471 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 472 | new_values['data']['contact_id'] <= 0: |
||
| 473 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 474 | description='API.INVALID_CONTACT_ID') |
||
| 475 | contact_id = new_values['data']['contact_id'] |
||
| 476 | |||
| 477 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 478 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 479 | new_values['data']['cost_center_id'] <= 0: |
||
| 480 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 481 | description='API.INVALID_COST_CENTER_ID') |
||
| 482 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 483 | |||
| 484 | if 'description' in new_values['data'].keys() and \ |
||
| 485 | new_values['data']['description'] is not None and \ |
||
| 486 | len(str(new_values['data']['description'])) > 0: |
||
| 487 | description = str.strip(new_values['data']['description']) |
||
| 488 | else: |
||
| 489 | description = None |
||
| 490 | |||
| 491 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 492 | cursor = cnx.cursor() |
||
| 493 | |||
| 494 | cursor.execute(" SELECT name " |
||
| 495 | " FROM tbl_energy_storage_containers " |
||
| 496 | " WHERE id = %s ", (id_,)) |
||
| 497 | if cursor.fetchone() is None: |
||
| 498 | cursor.close() |
||
| 499 | cnx.close() |
||
| 500 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 501 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 502 | |||
| 503 | cursor.execute(" SELECT name " |
||
| 504 | " FROM tbl_energy_storage_containers " |
||
| 505 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 506 | if cursor.fetchone() is not None: |
||
| 507 | cursor.close() |
||
| 508 | cnx.close() |
||
| 509 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 510 | description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE') |
||
| 511 | |||
| 512 | cursor.execute(" SELECT name " |
||
| 513 | " FROM tbl_contacts " |
||
| 514 | " WHERE id = %s ", |
||
| 515 | (new_values['data']['contact_id'],)) |
||
| 516 | row = cursor.fetchone() |
||
| 517 | if row is None: |
||
| 518 | cursor.close() |
||
| 519 | cnx.close() |
||
| 520 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 521 | description='API.CONTACT_NOT_FOUND') |
||
| 522 | |||
| 523 | cursor.execute(" SELECT name " |
||
| 524 | " FROM tbl_cost_centers " |
||
| 525 | " WHERE id = %s ", |
||
| 526 | (new_values['data']['cost_center_id'],)) |
||
| 527 | row = cursor.fetchone() |
||
| 528 | if row is None: |
||
| 529 | cursor.close() |
||
| 530 | cnx.close() |
||
| 531 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 532 | description='API.COST_CENTER_NOT_FOUND') |
||
| 533 | |||
| 534 | update_row = (" UPDATE tbl_energy_storage_containers " |
||
| 535 | " SET name = %s, rated_capacity = %s, rated_power = %s, contact_id = %s, cost_center_id = %s, " |
||
| 536 | " description = %s " |
||
| 537 | " WHERE id = %s ") |
||
| 538 | cursor.execute(update_row, (name, |
||
| 539 | rated_capacity, |
||
| 540 | rated_power, |
||
| 541 | contact_id, |
||
| 542 | cost_center_id, |
||
| 543 | description, |
||
| 544 | id_)) |
||
| 545 | cnx.commit() |
||
| 546 | |||
| 547 | cursor.close() |
||
| 548 | cnx.close() |
||
| 549 | |||
| 550 | resp.status = falcon.HTTP_200 |
||
| 551 | |||
| 552 | |||
| 553 | class EnergyStorageContainerBatteryCollection: |
||
| 554 | def __init__(self): |
||
| 555 | pass |
||
| 556 | |||
| 557 | @staticmethod |
||
| 558 | def on_options(req, resp, id_): |
||
| 559 | _ = req |
||
| 560 | resp.status = falcon.HTTP_200 |
||
| 561 | _ = id_ |
||
| 562 | |||
| 563 | View Code Duplication | @staticmethod |
|
| 564 | def on_get(req, resp, id_): |
||
| 565 | access_control(req) |
||
| 566 | if not id_.isdigit() or int(id_) <= 0: |
||
| 567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 568 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 569 | |||
| 570 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 571 | cursor = cnx.cursor() |
||
| 572 | |||
| 573 | cursor.execute(" SELECT name " |
||
| 574 | " FROM tbl_energy_storage_containers " |
||
| 575 | " WHERE id = %s ", (id_,)) |
||
| 576 | if cursor.fetchone() is None: |
||
| 577 | cursor.close() |
||
| 578 | cnx.close() |
||
| 579 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 580 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 581 | |||
| 582 | # query meter dict |
||
| 583 | query = (" SELECT id, name, uuid " |
||
| 584 | " FROM tbl_meters ") |
||
| 585 | cursor.execute(query) |
||
| 586 | rows_meters = cursor.fetchall() |
||
| 587 | |||
| 588 | meter_dict = dict() |
||
| 589 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 590 | for row in rows_meters: |
||
| 591 | meter_dict[row[0]] = {"id": row[0], |
||
| 592 | "name": row[1], |
||
| 593 | "uuid": row[2]} |
||
| 594 | # query point dict |
||
| 595 | query = (" SELECT id, name " |
||
| 596 | " FROM tbl_points ") |
||
| 597 | cursor.execute(query) |
||
| 598 | rows_points = cursor.fetchall() |
||
| 599 | |||
| 600 | point_dict = dict() |
||
| 601 | if rows_points is not None and len(rows_points) > 0: |
||
| 602 | for row in rows_points: |
||
| 603 | point_dict[row[0]] = {"id": row[0], |
||
| 604 | "name": row[1]} |
||
| 605 | |||
| 606 | query = (" SELECT id, name, uuid, " |
||
| 607 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 608 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
||
| 609 | " FROM tbl_energy_storage_containers_batteries " |
||
| 610 | " WHERE energy_storage_container_id = %s " |
||
| 611 | " ORDER BY name ") |
||
| 612 | cursor.execute(query, (id_,)) |
||
| 613 | rows = cursor.fetchall() |
||
| 614 | |||
| 615 | result = list() |
||
| 616 | if rows is not None and len(rows) > 0: |
||
| 617 | for row in rows: |
||
| 618 | meta_result = {"id": row[0], |
||
| 619 | "name": row[1], |
||
| 620 | "uuid": row[2], |
||
| 621 | "battery_state_point": point_dict.get(row[3]), |
||
| 622 | "soc_point": point_dict.get(row[4]), |
||
| 623 | "power_point": point_dict.get(row[5]), |
||
| 624 | "charge_meter": meter_dict.get(row[6]), |
||
| 625 | "discharge_meter": meter_dict.get(row[7]), |
||
| 626 | "rated_capacity": row[8], |
||
| 627 | "rated_power": row[9], |
||
| 628 | "nominal_voltage": row[10] |
||
| 629 | } |
||
| 630 | result.append(meta_result) |
||
| 631 | |||
| 632 | resp.text = json.dumps(result) |
||
| 633 | |||
| 634 | @staticmethod |
||
| 635 | @user_logger |
||
| 636 | def on_post(req, resp, id_): |
||
| 637 | """Handles POST requests""" |
||
| 638 | admin_control(req) |
||
| 639 | try: |
||
| 640 | raw_json = req.stream.read().decode('utf-8') |
||
| 641 | except UnicodeDecodeError as ex: |
||
| 642 | print("Failed to decode request") |
||
| 643 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 644 | title='API.BAD_REQUEST', |
||
| 645 | description='API.INVALID_ENCODING') |
||
| 646 | except Exception as ex: |
||
| 647 | print("Unexpected error reading request stream") |
||
| 648 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 649 | title='API.BAD_REQUEST', |
||
| 650 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 651 | |||
| 652 | if not id_.isdigit() or int(id_) <= 0: |
||
| 653 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 654 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 655 | |||
| 656 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 657 | cursor = cnx.cursor() |
||
| 658 | |||
| 659 | cursor.execute(" SELECT name " |
||
| 660 | " FROM tbl_energy_storage_containers " |
||
| 661 | " WHERE id = %s ", (id_,)) |
||
| 662 | if cursor.fetchone() is None: |
||
| 663 | cursor.close() |
||
| 664 | cnx.close() |
||
| 665 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 666 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 667 | |||
| 668 | new_values = json.loads(raw_json) |
||
| 669 | |||
| 670 | if 'name' not in new_values['data'].keys() or \ |
||
| 671 | not isinstance(new_values['data']['name'], str) or \ |
||
| 672 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 673 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 674 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_BATTERY_NAME') |
||
| 675 | name = str.strip(new_values['data']['name']) |
||
| 676 | |||
| 677 | if 'battery_state_point_id' not in new_values['data'].keys() or \ |
||
| 678 | not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
||
| 679 | new_values['data']['battery_state_point_id'] <= 0: |
||
| 680 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 681 | description='API.INVALID_BATTERY_STATE_POINT_ID') |
||
| 682 | battery_state_point_id = new_values['data']['battery_state_point_id'] |
||
| 683 | |||
| 684 | if 'soc_point_id' not in new_values['data'].keys() or \ |
||
| 685 | not isinstance(new_values['data']['soc_point_id'], int) or \ |
||
| 686 | new_values['data']['soc_point_id'] <= 0: |
||
| 687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 688 | description='API.INVALID_SOC_POINT_ID') |
||
| 689 | soc_point_id = new_values['data']['soc_point_id'] |
||
| 690 | |||
| 691 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 692 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 693 | new_values['data']['power_point_id'] <= 0: |
||
| 694 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 695 | description='API.INVALID_POWER_POINT_ID') |
||
| 696 | power_point_id = new_values['data']['power_point_id'] |
||
| 697 | |||
| 698 | if 'charge_meter_id' not in new_values['data'].keys() or \ |
||
| 699 | not isinstance(new_values['data']['charge_meter_id'], int) or \ |
||
| 700 | new_values['data']['charge_meter_id'] <= 0: |
||
| 701 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 702 | description='API.INVALID_CHARGE_METER_ID') |
||
| 703 | charge_meter_id = new_values['data']['charge_meter_id'] |
||
| 704 | |||
| 705 | if 'discharge_meter_id' not in new_values['data'].keys() or \ |
||
| 706 | not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
||
| 707 | new_values['data']['discharge_meter_id'] <= 0: |
||
| 708 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 709 | description='API.INVALID_DISCHARGE_METER_ID') |
||
| 710 | discharge_meter_id = new_values['data']['discharge_meter_id'] |
||
| 711 | |||
| 712 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 713 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 714 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 715 | new_values['data']['rated_capacity'] < 0.0: |
||
| 716 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 717 | description='API.INVALID_RATED_CAPACITY') |
||
| 718 | rated_capacity = Decimal(new_values['data']['rated_capacity']) |
||
| 719 | |||
| 720 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 721 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 722 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 723 | new_values['data']['rated_power'] < 0.0: |
||
| 724 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 725 | description='API.INVALID_RATED_POWER') |
||
| 726 | rated_power = Decimal(new_values['data']['rated_power']) |
||
| 727 | |||
| 728 | if 'nominal_voltage' not in new_values['data'].keys() or \ |
||
| 729 | not (isinstance(new_values['data']['nominal_voltage'], float) or |
||
| 730 | isinstance(new_values['data']['nominal_voltage'], int)): |
||
| 731 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 732 | description='API.INVALID_NOMINAL_VOLTAGE') |
||
| 733 | nominal_voltage = Decimal(new_values['data']['nominal_voltage']) |
||
| 734 | |||
| 735 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 736 | cursor = cnx.cursor() |
||
| 737 | |||
| 738 | cursor.execute(" SELECT name " |
||
| 739 | " FROM tbl_energy_storage_containers_batteries " |
||
| 740 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 741 | (id_, name,)) |
||
| 742 | if cursor.fetchone() is not None: |
||
| 743 | cursor.close() |
||
| 744 | cnx.close() |
||
| 745 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 746 | description='API.ENERGY_STORAGE_CONTAINER_BATTERY_NAME_IS_ALREADY_IN_USE') |
||
| 747 | |||
| 748 | cursor.execute(" SELECT name " |
||
| 749 | " FROM tbl_points " |
||
| 750 | " WHERE id = %s ", |
||
| 751 | (battery_state_point_id,)) |
||
| 752 | if cursor.fetchone() is None: |
||
| 753 | cursor.close() |
||
| 754 | cnx.close() |
||
| 755 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 756 | description='API.BATTERY_STATE_POINT_NOT_FOUND') |
||
| 757 | |||
| 758 | cursor.execute(" SELECT name " |
||
| 759 | " FROM tbl_points " |
||
| 760 | " WHERE id = %s ", |
||
| 761 | (soc_point_id,)) |
||
| 762 | if cursor.fetchone() is None: |
||
| 763 | cursor.close() |
||
| 764 | cnx.close() |
||
| 765 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 766 | description='API.SOC_POINT_NOT_FOUND') |
||
| 767 | |||
| 768 | cursor.execute(" SELECT name " |
||
| 769 | " FROM tbl_points " |
||
| 770 | " WHERE id = %s ", |
||
| 771 | (power_point_id,)) |
||
| 772 | if cursor.fetchone() is None: |
||
| 773 | cursor.close() |
||
| 774 | cnx.close() |
||
| 775 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 776 | description='API.POWER_POINT_NOT_FOUND') |
||
| 777 | |||
| 778 | cursor.execute(" SELECT name " |
||
| 779 | " FROM tbl_meters " |
||
| 780 | " WHERE id = %s ", |
||
| 781 | (charge_meter_id,)) |
||
| 782 | if cursor.fetchone() is None: |
||
| 783 | cursor.close() |
||
| 784 | cnx.close() |
||
| 785 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 786 | description='API.CHARGE_METER_NOT_FOUND') |
||
| 787 | |||
| 788 | cursor.execute(" SELECT name " |
||
| 789 | " FROM tbl_meters " |
||
| 790 | " WHERE id = %s ", |
||
| 791 | (discharge_meter_id,)) |
||
| 792 | if cursor.fetchone() is None: |
||
| 793 | cursor.close() |
||
| 794 | cnx.close() |
||
| 795 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 796 | description='API.DISCHARGE_METER_NOT_FOUND') |
||
| 797 | |||
| 798 | add_values = (" INSERT INTO tbl_energy_storage_containers_batteries " |
||
| 799 | " (name, uuid, energy_storage_container_id, " |
||
| 800 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 801 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage) " |
||
| 802 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 803 | cursor.execute(add_values, (name, |
||
| 804 | str(uuid.uuid4()), |
||
| 805 | id_, |
||
| 806 | battery_state_point_id, |
||
| 807 | soc_point_id, |
||
| 808 | power_point_id, |
||
| 809 | charge_meter_id, |
||
| 810 | discharge_meter_id, |
||
| 811 | rated_capacity, |
||
| 812 | rated_power, |
||
| 813 | nominal_voltage |
||
| 814 | )) |
||
| 815 | new_id = cursor.lastrowid |
||
| 816 | cnx.commit() |
||
| 817 | cursor.close() |
||
| 818 | cnx.close() |
||
| 819 | |||
| 820 | resp.status = falcon.HTTP_201 |
||
| 821 | resp.location = '/energystoragecontainers/' + str(id_) + '/batteries/' + str(new_id) |
||
| 822 | |||
| 823 | |||
| 824 | class EnergyStorageContainerBatteryItem: |
||
| 825 | def __init__(self): |
||
| 826 | pass |
||
| 827 | |||
| 828 | @staticmethod |
||
| 829 | def on_options(req, resp, id_, bid): |
||
| 830 | _ = req |
||
| 831 | resp.status = falcon.HTTP_200 |
||
| 832 | _ = id_ |
||
| 833 | |||
| 834 | View Code Duplication | @staticmethod |
|
| 835 | def on_get(req, resp, id_, bid): |
||
| 836 | access_control(req) |
||
| 837 | if not id_.isdigit() or int(id_) <= 0: |
||
| 838 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 839 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 840 | if not bid.isdigit() or int(bid) <= 0: |
||
| 841 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 842 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_BATTERY_ID') |
||
| 843 | |||
| 844 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 845 | cursor = cnx.cursor() |
||
| 846 | |||
| 847 | cursor.execute(" SELECT name " |
||
| 848 | " FROM tbl_energy_storage_containers " |
||
| 849 | " WHERE id = %s ", (id_,)) |
||
| 850 | if cursor.fetchone() is None: |
||
| 851 | cursor.close() |
||
| 852 | cnx.close() |
||
| 853 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 854 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 855 | |||
| 856 | # query energy storage power station dict |
||
| 857 | query = (" SELECT id, name, uuid " |
||
| 858 | " FROM tbl_energy_storage_containers ") |
||
| 859 | cursor.execute(query) |
||
| 860 | rows_energystoragecontainers = cursor.fetchall() |
||
| 861 | |||
| 862 | energy_storage_container_dict = dict() |
||
| 863 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 864 | for row in rows_energystoragecontainers: |
||
| 865 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 866 | "name": row[1], |
||
| 867 | "uuid": row[2]} |
||
| 868 | # query meter dict |
||
| 869 | query = (" SELECT id, name, uuid " |
||
| 870 | " FROM tbl_meters ") |
||
| 871 | cursor.execute(query) |
||
| 872 | rows_meters = cursor.fetchall() |
||
| 873 | |||
| 874 | meter_dict = dict() |
||
| 875 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 876 | for row in rows_meters: |
||
| 877 | meter_dict[row[0]] = {"id": row[0], |
||
| 878 | "name": row[1], |
||
| 879 | "uuid": row[2]} |
||
| 880 | # query point dict |
||
| 881 | query = (" SELECT id, name " |
||
| 882 | " FROM tbl_points ") |
||
| 883 | cursor.execute(query) |
||
| 884 | rows_points = cursor.fetchall() |
||
| 885 | |||
| 886 | point_dict = dict() |
||
| 887 | if rows_points is not None and len(rows_points) > 0: |
||
| 888 | for row in rows_points: |
||
| 889 | point_dict[row[0]] = {"id": row[0], |
||
| 890 | "name": row[1]} |
||
| 891 | |||
| 892 | query = (" SELECT id, name, uuid, energy_storage_container_id, " |
||
| 893 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 894 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
||
| 895 | " FROM tbl_energy_storage_containers_batteries " |
||
| 896 | " WHERE id = %s ") |
||
| 897 | cursor.execute(query, (bid,)) |
||
| 898 | row = cursor.fetchone() |
||
| 899 | cursor.close() |
||
| 900 | cnx.close() |
||
| 901 | |||
| 902 | if row is None: |
||
| 903 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 904 | description='API.ENERGY_STORAGE_CONTAINER_BATTERY_NOT_FOUND') |
||
| 905 | else: |
||
| 906 | meta_result = {"id": row[0], |
||
| 907 | "name": row[1], |
||
| 908 | "uuid": row[2], |
||
| 909 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 910 | "battery_state_point": point_dict.get(row[4]), |
||
| 911 | "soc_point": point_dict.get(row[5]), |
||
| 912 | "power_point": point_dict.get(row[6]), |
||
| 913 | "charge_meter": meter_dict.get(row[7]), |
||
| 914 | "discharge_meter": meter_dict.get(row[8]), |
||
| 915 | "rated_capacity": row[9], |
||
| 916 | "rated_power": row[10], |
||
| 917 | "nominal_voltage": row[11] |
||
| 918 | } |
||
| 919 | |||
| 920 | resp.text = json.dumps(meta_result) |
||
| 921 | |||
| 922 | @staticmethod |
||
| 923 | @user_logger |
||
| 924 | def on_delete(req, resp, id_, bid): |
||
| 925 | admin_control(req) |
||
| 926 | if not id_.isdigit() or int(id_) <= 0: |
||
| 927 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 928 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 929 | if not bid.isdigit() or int(bid) <= 0: |
||
| 930 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 931 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_BATTERY_ID') |
||
| 932 | |||
| 933 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 934 | cursor = cnx.cursor() |
||
| 935 | |||
| 936 | cursor.execute(" SELECT name " |
||
| 937 | " FROM tbl_energy_storage_containers " |
||
| 938 | " WHERE id = %s ", (id_,)) |
||
| 939 | if cursor.fetchone() is None: |
||
| 940 | cursor.close() |
||
| 941 | cnx.close() |
||
| 942 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 943 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 944 | |||
| 945 | cursor.execute(" SELECT name " |
||
| 946 | " FROM tbl_energy_storage_containers_batteries " |
||
| 947 | " WHERE id = %s ", (bid,)) |
||
| 948 | if cursor.fetchone() is None: |
||
| 949 | cursor.close() |
||
| 950 | cnx.close() |
||
| 951 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 952 | description='API.ENERGY_STORAGE_CONTAINER_BATTERY_NOT_FOUND') |
||
| 953 | |||
| 954 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_batteries " |
||
| 955 | " WHERE id = %s ", (bid,)) |
||
| 956 | cnx.commit() |
||
| 957 | |||
| 958 | cursor.close() |
||
| 959 | cnx.close() |
||
| 960 | |||
| 961 | resp.status = falcon.HTTP_204 |
||
| 962 | |||
| 963 | @staticmethod |
||
| 964 | @user_logger |
||
| 965 | def on_put(req, resp, id_, bid): |
||
| 966 | """Handles PUT requests""" |
||
| 967 | admin_control(req) |
||
| 968 | try: |
||
| 969 | raw_json = req.stream.read().decode('utf-8') |
||
| 970 | except UnicodeDecodeError as ex: |
||
| 971 | print("Failed to decode request") |
||
| 972 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 973 | title='API.BAD_REQUEST', |
||
| 974 | description='API.INVALID_ENCODING') |
||
| 975 | except Exception as ex: |
||
| 976 | print("Unexpected error reading request stream") |
||
| 977 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 978 | title='API.BAD_REQUEST', |
||
| 979 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 980 | if not id_.isdigit() or int(id_) <= 0: |
||
| 981 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 982 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 983 | if not bid.isdigit() or int(bid) <= 0: |
||
| 984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 985 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_BATTERY_ID') |
||
| 986 | |||
| 987 | new_values = json.loads(raw_json) |
||
| 988 | |||
| 989 | if 'name' not in new_values['data'].keys() or \ |
||
| 990 | not isinstance(new_values['data']['name'], str) or \ |
||
| 991 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 992 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 993 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_BATTERY_NAME') |
||
| 994 | name = str.strip(new_values['data']['name']) |
||
| 995 | |||
| 996 | if 'battery_state_point_id' not in new_values['data'].keys() or \ |
||
| 997 | not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
||
| 998 | new_values['data']['battery_state_point_id'] <= 0: |
||
| 999 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1000 | description='API.INVALID_BATTERY_STATE_POINT_ID') |
||
| 1001 | battery_state_point_id = new_values['data']['battery_state_point_id'] |
||
| 1002 | |||
| 1003 | if 'soc_point_id' not in new_values['data'].keys() or \ |
||
| 1004 | not isinstance(new_values['data']['soc_point_id'], int) or \ |
||
| 1005 | new_values['data']['soc_point_id'] <= 0: |
||
| 1006 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1007 | description='API.INVALID_SOC_POINT_ID') |
||
| 1008 | soc_point_id = new_values['data']['soc_point_id'] |
||
| 1009 | |||
| 1010 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 1011 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 1012 | new_values['data']['power_point_id'] <= 0: |
||
| 1013 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1014 | description='API.INVALID_POWER_POINT_ID') |
||
| 1015 | power_point_id = new_values['data']['power_point_id'] |
||
| 1016 | |||
| 1017 | if 'charge_meter_id' not in new_values['data'].keys() or \ |
||
| 1018 | not isinstance(new_values['data']['charge_meter_id'], int) or \ |
||
| 1019 | new_values['data']['charge_meter_id'] <= 0: |
||
| 1020 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1021 | description='API.INVALID_CHARGE_METER_ID') |
||
| 1022 | charge_meter_id = new_values['data']['charge_meter_id'] |
||
| 1023 | |||
| 1024 | if 'discharge_meter_id' not in new_values['data'].keys() or \ |
||
| 1025 | not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
||
| 1026 | new_values['data']['discharge_meter_id'] <= 0: |
||
| 1027 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1028 | description='API.INVALID_DISCHARGE_METER_ID') |
||
| 1029 | discharge_meter_id = new_values['data']['discharge_meter_id'] |
||
| 1030 | |||
| 1031 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 1032 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 1033 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 1034 | new_values['data']['rated_capacity'] < 0.0: |
||
| 1035 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1036 | description='API.INVALID_RATED_CAPACITY') |
||
| 1037 | rated_capacity = Decimal(new_values['data']['rated_capacity']) |
||
| 1038 | |||
| 1039 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 1040 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 1041 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 1042 | new_values['data']['rated_power'] < 0.0: |
||
| 1043 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1044 | description='API.INVALID_RATED_POWER') |
||
| 1045 | rated_power = Decimal(new_values['data']['rated_power']) |
||
| 1046 | |||
| 1047 | if 'nominal_voltage' not in new_values['data'].keys() or \ |
||
| 1048 | not (isinstance(new_values['data']['nominal_voltage'], float) or |
||
| 1049 | isinstance(new_values['data']['nominal_voltage'], int)): |
||
| 1050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1051 | description='API.INVALID_NOMINAL_VOLTAGE') |
||
| 1052 | nominal_voltage = Decimal(new_values['data']['nominal_voltage']) |
||
| 1053 | |||
| 1054 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1055 | cursor = cnx.cursor() |
||
| 1056 | |||
| 1057 | cursor.execute(" SELECT name " |
||
| 1058 | " FROM tbl_energy_storage_containers " |
||
| 1059 | " WHERE id = %s ", |
||
| 1060 | (id_,)) |
||
| 1061 | if cursor.fetchone() is None: |
||
| 1062 | cursor.close() |
||
| 1063 | cnx.close() |
||
| 1064 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1065 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1066 | |||
| 1067 | cursor.execute(" SELECT name " |
||
| 1068 | " FROM tbl_energy_storage_containers_batteries " |
||
| 1069 | " WHERE id = %s ", (bid,)) |
||
| 1070 | if cursor.fetchone() is None: |
||
| 1071 | cursor.close() |
||
| 1072 | cnx.close() |
||
| 1073 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1074 | description='API.ENERGY_STORAGE_CONTAINER_BATTERY_NOT_FOUND') |
||
| 1075 | |||
| 1076 | cursor.execute(" SELECT name " |
||
| 1077 | " FROM tbl_energy_storage_containers_batteries " |
||
| 1078 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 1079 | (id_, name, bid)) |
||
| 1080 | if cursor.fetchone() is not None: |
||
| 1081 | cursor.close() |
||
| 1082 | cnx.close() |
||
| 1083 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1084 | description='API.ENERGY_STORAGE_CONTAINER_BATTERY_NAME_IS_ALREADY_IN_USE') |
||
| 1085 | |||
| 1086 | cursor.execute(" SELECT name " |
||
| 1087 | " FROM tbl_points " |
||
| 1088 | " WHERE id = %s ", |
||
| 1089 | (battery_state_point_id,)) |
||
| 1090 | if cursor.fetchone() is None: |
||
| 1091 | cursor.close() |
||
| 1092 | cnx.close() |
||
| 1093 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1094 | description='API.BATTERY_STATE_POINT_NOT_FOUND') |
||
| 1095 | |||
| 1096 | cursor.execute(" SELECT name " |
||
| 1097 | " FROM tbl_points " |
||
| 1098 | " WHERE id = %s ", |
||
| 1099 | (soc_point_id,)) |
||
| 1100 | if cursor.fetchone() is None: |
||
| 1101 | cursor.close() |
||
| 1102 | cnx.close() |
||
| 1103 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1104 | description='API.SOC_POINT_NOT_FOUND') |
||
| 1105 | |||
| 1106 | cursor.execute(" SELECT name " |
||
| 1107 | " FROM tbl_points " |
||
| 1108 | " WHERE id = %s ", |
||
| 1109 | (power_point_id,)) |
||
| 1110 | if cursor.fetchone() is None: |
||
| 1111 | cursor.close() |
||
| 1112 | cnx.close() |
||
| 1113 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1114 | description='API.POWER_POINT_NOT_FOUND') |
||
| 1115 | |||
| 1116 | cursor.execute(" SELECT name " |
||
| 1117 | " FROM tbl_meters " |
||
| 1118 | " WHERE id = %s ", |
||
| 1119 | (charge_meter_id,)) |
||
| 1120 | if cursor.fetchone() is None: |
||
| 1121 | cursor.close() |
||
| 1122 | cnx.close() |
||
| 1123 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1124 | description='API.CHARGE_METER_NOT_FOUND') |
||
| 1125 | |||
| 1126 | cursor.execute(" SELECT name " |
||
| 1127 | " FROM tbl_meters " |
||
| 1128 | " WHERE id = %s ", |
||
| 1129 | (discharge_meter_id,)) |
||
| 1130 | if cursor.fetchone() is None: |
||
| 1131 | cursor.close() |
||
| 1132 | cnx.close() |
||
| 1133 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1134 | description='API.DISCHARGE_METER_NOT_FOUND') |
||
| 1135 | |||
| 1136 | update_row = (" UPDATE tbl_energy_storage_containers_batteries " |
||
| 1137 | " SET name = %s, energy_storage_container_id = %s, " |
||
| 1138 | " battery_state_point_id = %s, soc_point_id = %s, power_point_id = %s, " |
||
| 1139 | " charge_meter_id = %s, discharge_meter_id = %s, " |
||
| 1140 | " rated_capacity = %s, rated_power = %s, nominal_voltage = %s " |
||
| 1141 | " WHERE id = %s ") |
||
| 1142 | cursor.execute(update_row, (name, |
||
| 1143 | id_, |
||
| 1144 | battery_state_point_id, |
||
| 1145 | soc_point_id, |
||
| 1146 | power_point_id, |
||
| 1147 | charge_meter_id, |
||
| 1148 | discharge_meter_id, |
||
| 1149 | rated_capacity, |
||
| 1150 | rated_power, |
||
| 1151 | nominal_voltage, |
||
| 1152 | bid)) |
||
| 1153 | cnx.commit() |
||
| 1154 | |||
| 1155 | cursor.close() |
||
| 1156 | cnx.close() |
||
| 1157 | |||
| 1158 | resp.status = falcon.HTTP_200 |
||
| 1159 | |||
| 1160 | |||
| 1161 | View Code Duplication | class EnergyStorageContainerBatteryPointCollection: |
|
| 1162 | def __init__(self): |
||
| 1163 | pass |
||
| 1164 | |||
| 1165 | @staticmethod |
||
| 1166 | def on_options(req, resp, id_, bid): |
||
| 1167 | _ = req |
||
| 1168 | resp.status = falcon.HTTP_200 |
||
| 1169 | _ = id_ |
||
| 1170 | |||
| 1171 | @staticmethod |
||
| 1172 | def on_get(req, resp, id_, bid): |
||
| 1173 | if 'API-KEY' not in req.headers or \ |
||
| 1174 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1175 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1176 | access_control(req) |
||
| 1177 | else: |
||
| 1178 | api_key_control(req) |
||
| 1179 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1180 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1181 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1182 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1183 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1184 | description='API.INVALID_BMS_ID') |
||
| 1185 | |||
| 1186 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1187 | cursor = cnx.cursor() |
||
| 1188 | |||
| 1189 | cursor.execute(" SELECT name " |
||
| 1190 | " FROM tbl_energy_storage_containers_batteries " |
||
| 1191 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid, )) |
||
| 1192 | if cursor.fetchone() is None: |
||
| 1193 | cursor.close() |
||
| 1194 | cnx.close() |
||
| 1195 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1196 | description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND') |
||
| 1197 | |||
| 1198 | query = (" SELECT p.id, p.name, " |
||
| 1199 | " ds.id, ds.name, ds.uuid, " |
||
| 1200 | " p.address " |
||
| 1201 | " FROM tbl_points p, tbl_energy_storage_containers_bmses_points mp, tbl_data_sources ds " |
||
| 1202 | " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 1203 | " ORDER BY p.name ") |
||
| 1204 | cursor.execute(query, (bid,)) |
||
| 1205 | rows = cursor.fetchall() |
||
| 1206 | |||
| 1207 | result = list() |
||
| 1208 | if rows is not None and len(rows) > 0: |
||
| 1209 | for row in rows: |
||
| 1210 | meta_result = {"id": row[0], "name": row[1], |
||
| 1211 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 1212 | "address": row[5]} |
||
| 1213 | result.append(meta_result) |
||
| 1214 | |||
| 1215 | resp.text = json.dumps(result) |
||
| 1216 | |||
| 1217 | @staticmethod |
||
| 1218 | @user_logger |
||
| 1219 | def on_post(req, resp, id_, bid): |
||
| 1220 | """Handles POST requests""" |
||
| 1221 | admin_control(req) |
||
| 1222 | try: |
||
| 1223 | raw_json = req.stream.read().decode('utf-8') |
||
| 1224 | except UnicodeDecodeError as ex: |
||
| 1225 | print("Failed to decode request") |
||
| 1226 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1227 | title='API.BAD_REQUEST', |
||
| 1228 | description='API.INVALID_ENCODING') |
||
| 1229 | except Exception as ex: |
||
| 1230 | print("Unexpected error reading request stream") |
||
| 1231 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1232 | title='API.BAD_REQUEST', |
||
| 1233 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1234 | |||
| 1235 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1236 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1237 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1238 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1240 | description='API.INVALID_BMS_ID') |
||
| 1241 | |||
| 1242 | new_values = json.loads(raw_json) |
||
| 1243 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1244 | cursor = cnx.cursor() |
||
| 1245 | |||
| 1246 | cursor.execute(" SELECT name " |
||
| 1247 | " FROM tbl_energy_storage_containers_batteries " |
||
| 1248 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid,)) |
||
| 1249 | if cursor.fetchone() is None: |
||
| 1250 | cursor.close() |
||
| 1251 | cnx.close() |
||
| 1252 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1253 | description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND') |
||
| 1254 | |||
| 1255 | cursor.execute(" SELECT name, object_type " |
||
| 1256 | " FROM tbl_points " |
||
| 1257 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 1258 | row = cursor.fetchone() |
||
| 1259 | if row is None: |
||
| 1260 | cursor.close() |
||
| 1261 | cnx.close() |
||
| 1262 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1263 | description='API.POINT_NOT_FOUND') |
||
| 1264 | |||
| 1265 | query = (" SELECT id " |
||
| 1266 | " FROM tbl_energy_storage_containers_bmses_points " |
||
| 1267 | " WHERE bms_id = %s AND point_id = %s") |
||
| 1268 | cursor.execute(query, (bid, new_values['data']['point_id'],)) |
||
| 1269 | if cursor.fetchone() is not None: |
||
| 1270 | cursor.close() |
||
| 1271 | cnx.close() |
||
| 1272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1273 | description='API.BMS_POINT_RELATION_EXISTS') |
||
| 1274 | |||
| 1275 | add_row = (" INSERT INTO tbl_energy_storage_containers_bmses_points (bms_id, point_id) " |
||
| 1276 | " VALUES (%s, %s) ") |
||
| 1277 | cursor.execute(add_row, (bid, new_values['data']['point_id'],)) |
||
| 1278 | cnx.commit() |
||
| 1279 | cursor.close() |
||
| 1280 | cnx.close() |
||
| 1281 | |||
| 1282 | resp.status = falcon.HTTP_201 |
||
| 1283 | resp.location = '/energystoragecontainers/' + str(id_) + '/batteries/' + str(bid) + '/points/' + \ |
||
| 1284 | str(new_values['data']['point_id']) |
||
| 1285 | |||
| 1286 | |||
| 1287 | View Code Duplication | class EnergyStorageContainerBatteryPointItem: |
|
| 1288 | def __init__(self): |
||
| 1289 | pass |
||
| 1290 | |||
| 1291 | @staticmethod |
||
| 1292 | def on_options(req, resp, id_, bid, pid): |
||
| 1293 | _ = req |
||
| 1294 | resp.status = falcon.HTTP_200 |
||
| 1295 | _ = id_ |
||
| 1296 | |||
| 1297 | @staticmethod |
||
| 1298 | @user_logger |
||
| 1299 | def on_delete(req, resp, id_, bid, pid): |
||
| 1300 | """Handles DELETE requests""" |
||
| 1301 | admin_control(req) |
||
| 1302 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1303 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1304 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1305 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1306 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1307 | description='API.INVALID_BMS_ID') |
||
| 1308 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1309 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1310 | description='API.INVALID_POINT_ID') |
||
| 1311 | |||
| 1312 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1313 | cursor = cnx.cursor() |
||
| 1314 | |||
| 1315 | cursor.execute(" SELECT name " |
||
| 1316 | " FROM tbl_energy_storage_containers_batteries " |
||
| 1317 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid,)) |
||
| 1318 | if cursor.fetchone() is None: |
||
| 1319 | cursor.close() |
||
| 1320 | cnx.close() |
||
| 1321 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1322 | description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND') |
||
| 1323 | |||
| 1324 | cursor.execute(" SELECT name " |
||
| 1325 | " FROM tbl_points " |
||
| 1326 | " WHERE id = %s ", (pid,)) |
||
| 1327 | if cursor.fetchone() is None: |
||
| 1328 | cursor.close() |
||
| 1329 | cnx.close() |
||
| 1330 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1331 | description='API.POINT_NOT_FOUND') |
||
| 1332 | |||
| 1333 | cursor.execute(" SELECT id " |
||
| 1334 | " FROM tbl_energy_storage_containers_bmses_points " |
||
| 1335 | " WHERE bms_id = %s AND point_id = %s ", (bid, pid)) |
||
| 1336 | if cursor.fetchone() is None: |
||
| 1337 | cursor.close() |
||
| 1338 | cnx.close() |
||
| 1339 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1340 | description='API.BMS_POINT_RELATION_NOT_FOUND') |
||
| 1341 | |||
| 1342 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_bmses_points " |
||
| 1343 | " WHERE bms_id = %s AND point_id = %s ", (bid, pid)) |
||
| 1344 | cnx.commit() |
||
| 1345 | |||
| 1346 | cursor.close() |
||
| 1347 | cnx.close() |
||
| 1348 | |||
| 1349 | resp.status = falcon.HTTP_204 |
||
| 1350 | |||
| 1351 | |||
| 1352 | View Code Duplication | class EnergyStorageContainerCommandCollection: |
|
| 1353 | def __init__(self): |
||
| 1354 | pass |
||
| 1355 | |||
| 1356 | @staticmethod |
||
| 1357 | def on_options(req, resp, id_): |
||
| 1358 | _ = req |
||
| 1359 | resp.status = falcon.HTTP_200 |
||
| 1360 | _ = id_ |
||
| 1361 | |||
| 1362 | @staticmethod |
||
| 1363 | def on_get(req, resp, id_): |
||
| 1364 | if 'API-KEY' not in req.headers or \ |
||
| 1365 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1366 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1367 | access_control(req) |
||
| 1368 | else: |
||
| 1369 | api_key_control(req) |
||
| 1370 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1371 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1372 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1373 | |||
| 1374 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1375 | cursor = cnx.cursor() |
||
| 1376 | |||
| 1377 | cursor.execute(" SELECT name " |
||
| 1378 | " FROM tbl_energy_storage_containers " |
||
| 1379 | " WHERE id = %s ", (id_,)) |
||
| 1380 | if cursor.fetchone() is None: |
||
| 1381 | cursor.close() |
||
| 1382 | cnx.close() |
||
| 1383 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1384 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1385 | |||
| 1386 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 1387 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_commands cec, tbl_commands c " |
||
| 1388 | " WHERE cec.energy_storage_container_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 1389 | " ORDER BY c.id ") |
||
| 1390 | cursor.execute(query, (id_,)) |
||
| 1391 | rows = cursor.fetchall() |
||
| 1392 | |||
| 1393 | result = list() |
||
| 1394 | if rows is not None and len(rows) > 0: |
||
| 1395 | for row in rows: |
||
| 1396 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1397 | result.append(meta_result) |
||
| 1398 | |||
| 1399 | resp.text = json.dumps(result) |
||
| 1400 | |||
| 1401 | @staticmethod |
||
| 1402 | @user_logger |
||
| 1403 | def on_post(req, resp, id_): |
||
| 1404 | """Handles POST requests""" |
||
| 1405 | admin_control(req) |
||
| 1406 | try: |
||
| 1407 | raw_json = req.stream.read().decode('utf-8') |
||
| 1408 | except UnicodeDecodeError as ex: |
||
| 1409 | print("Failed to decode request") |
||
| 1410 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1411 | title='API.BAD_REQUEST', |
||
| 1412 | description='API.INVALID_ENCODING') |
||
| 1413 | except Exception as ex: |
||
| 1414 | print("Unexpected error reading request stream") |
||
| 1415 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1416 | title='API.BAD_REQUEST', |
||
| 1417 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1418 | |||
| 1419 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1420 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1421 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1422 | |||
| 1423 | new_values = json.loads(raw_json) |
||
| 1424 | |||
| 1425 | if 'command_id' not in new_values['data'].keys() or \ |
||
| 1426 | not isinstance(new_values['data']['command_id'], int) or \ |
||
| 1427 | new_values['data']['command_id'] <= 0: |
||
| 1428 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1429 | description='API.INVALID_COMMAND_ID') |
||
| 1430 | command_id = new_values['data']['command_id'] |
||
| 1431 | |||
| 1432 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1433 | cursor = cnx.cursor() |
||
| 1434 | |||
| 1435 | cursor.execute(" SELECT name " |
||
| 1436 | " from tbl_energy_storage_containers " |
||
| 1437 | " WHERE id = %s ", (id_,)) |
||
| 1438 | if cursor.fetchone() is None: |
||
| 1439 | cursor.close() |
||
| 1440 | cnx.close() |
||
| 1441 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1442 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1443 | |||
| 1444 | cursor.execute(" SELECT name " |
||
| 1445 | " FROM tbl_commands " |
||
| 1446 | " WHERE id = %s ", (command_id,)) |
||
| 1447 | if cursor.fetchone() is None: |
||
| 1448 | cursor.close() |
||
| 1449 | cnx.close() |
||
| 1450 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1451 | description='API.COMMAND_NOT_FOUND') |
||
| 1452 | |||
| 1453 | query = (" SELECT id " |
||
| 1454 | " FROM tbl_energy_storage_containers_commands " |
||
| 1455 | " WHERE energy_storage_container_id = %s AND command_id = %s") |
||
| 1456 | cursor.execute(query, (id_, command_id,)) |
||
| 1457 | if cursor.fetchone() is not None: |
||
| 1458 | cursor.close() |
||
| 1459 | cnx.close() |
||
| 1460 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1461 | description='API.ENERGY_STORAGE_CONTAINER_COMMAND_RELATION_EXISTS') |
||
| 1462 | |||
| 1463 | add_row = (" INSERT INTO tbl_energy_storage_containers_commands (energy_storage_container_id, command_id) " |
||
| 1464 | " VALUES (%s, %s) ") |
||
| 1465 | cursor.execute(add_row, (id_, command_id,)) |
||
| 1466 | cnx.commit() |
||
| 1467 | cursor.close() |
||
| 1468 | cnx.close() |
||
| 1469 | |||
| 1470 | resp.status = falcon.HTTP_201 |
||
| 1471 | resp.location = '/energystoragecontainers/' + str(id_) + '/commands/' + str(command_id) |
||
| 1472 | |||
| 1473 | |||
| 1474 | class EnergyStorageContainerCommandItem: |
||
| 1475 | def __init__(self): |
||
| 1476 | pass |
||
| 1477 | |||
| 1478 | @staticmethod |
||
| 1479 | def on_options(req, resp, id_, cid): |
||
| 1480 | _ = req |
||
| 1481 | resp.status = falcon.HTTP_200 |
||
| 1482 | _ = id_ |
||
| 1483 | |||
| 1484 | @staticmethod |
||
| 1485 | @user_logger |
||
| 1486 | def on_delete(req, resp, id_, cid): |
||
| 1487 | admin_control(req) |
||
| 1488 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1489 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1490 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1491 | |||
| 1492 | if not cid.isdigit() or int(cid) <= 0: |
||
| 1493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1494 | description='API.INVALID_COMMAND_ID') |
||
| 1495 | |||
| 1496 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1497 | cursor = cnx.cursor() |
||
| 1498 | |||
| 1499 | cursor.execute(" SELECT name " |
||
| 1500 | " FROM tbl_energy_storage_containers " |
||
| 1501 | " WHERE id = %s ", (id_,)) |
||
| 1502 | if cursor.fetchone() is None: |
||
| 1503 | cursor.close() |
||
| 1504 | cnx.close() |
||
| 1505 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1506 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1507 | |||
| 1508 | cursor.execute(" SELECT name " |
||
| 1509 | " FROM tbl_commands " |
||
| 1510 | " WHERE id = %s ", (cid,)) |
||
| 1511 | if cursor.fetchone() is None: |
||
| 1512 | cursor.close() |
||
| 1513 | cnx.close() |
||
| 1514 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1515 | description='API.COMMAND_NOT_FOUND') |
||
| 1516 | |||
| 1517 | cursor.execute(" SELECT id " |
||
| 1518 | " FROM tbl_energy_storage_containers_commands " |
||
| 1519 | " WHERE energy_storage_container_id = %s AND command_id = %s ", (id_, cid)) |
||
| 1520 | if cursor.fetchone() is None: |
||
| 1521 | cursor.close() |
||
| 1522 | cnx.close() |
||
| 1523 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1524 | description='API.ENERGY_STORAGE_CONTAINER_COMMAND_RELATION_NOT_FOUND') |
||
| 1525 | |||
| 1526 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_commands " |
||
| 1527 | " WHERE energy_storage_container_id = %s AND command_id = %s ", (id_, cid)) |
||
| 1528 | cnx.commit() |
||
| 1529 | |||
| 1530 | cursor.close() |
||
| 1531 | cnx.close() |
||
| 1532 | |||
| 1533 | resp.status = falcon.HTTP_204 |
||
| 1534 | |||
| 1535 | |||
| 1536 | View Code Duplication | class EnergyStorageContainerDataSourceCollection: |
|
| 1537 | def __init__(self): |
||
| 1538 | pass |
||
| 1539 | |||
| 1540 | @staticmethod |
||
| 1541 | def on_options(req, resp, id_): |
||
| 1542 | _ = req |
||
| 1543 | resp.status = falcon.HTTP_200 |
||
| 1544 | _ = id_ |
||
| 1545 | |||
| 1546 | @staticmethod |
||
| 1547 | def on_get(req, resp, id_): |
||
| 1548 | if 'API-KEY' not in req.headers or \ |
||
| 1549 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1550 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1551 | access_control(req) |
||
| 1552 | else: |
||
| 1553 | api_key_control(req) |
||
| 1554 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1555 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1556 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1557 | |||
| 1558 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1559 | cursor = cnx.cursor() |
||
| 1560 | |||
| 1561 | cursor.execute(" SELECT name " |
||
| 1562 | " FROM tbl_energy_storage_containers " |
||
| 1563 | " WHERE id = %s ", (id_,)) |
||
| 1564 | if cursor.fetchone() is None: |
||
| 1565 | cursor.close() |
||
| 1566 | cnx.close() |
||
| 1567 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1568 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1569 | |||
| 1570 | query = (" SELECT ds.id, ds.name, ds.uuid " |
||
| 1571 | " FROM tbl_energy_storage_containers ce, tbl_energy_storage_containers_data_sources ceds, " |
||
| 1572 | " tbl_data_sources ds " |
||
| 1573 | " WHERE ceds.energy_storage_container_id = ce.id AND ds.id = ceds.data_source_id AND ce.id = %s " |
||
| 1574 | " ORDER BY ds.id ") |
||
| 1575 | cursor.execute(query, (id_,)) |
||
| 1576 | rows = cursor.fetchall() |
||
| 1577 | |||
| 1578 | result = list() |
||
| 1579 | if rows is not None and len(rows) > 0: |
||
| 1580 | for row in rows: |
||
| 1581 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1582 | result.append(meta_result) |
||
| 1583 | |||
| 1584 | resp.text = json.dumps(result) |
||
| 1585 | |||
| 1586 | @staticmethod |
||
| 1587 | @user_logger |
||
| 1588 | def on_post(req, resp, id_): |
||
| 1589 | """Handles POST requests""" |
||
| 1590 | admin_control(req) |
||
| 1591 | try: |
||
| 1592 | raw_json = req.stream.read().decode('utf-8') |
||
| 1593 | except UnicodeDecodeError as ex: |
||
| 1594 | print("Failed to decode request") |
||
| 1595 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1596 | title='API.BAD_REQUEST', |
||
| 1597 | description='API.INVALID_ENCODING') |
||
| 1598 | except Exception as ex: |
||
| 1599 | print("Unexpected error reading request stream") |
||
| 1600 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1601 | title='API.BAD_REQUEST', |
||
| 1602 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1603 | |||
| 1604 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1605 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1606 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1607 | |||
| 1608 | new_values = json.loads(raw_json) |
||
| 1609 | |||
| 1610 | if 'data_source_id' not in new_values['data'].keys() or \ |
||
| 1611 | not isinstance(new_values['data']['data_source_id'], int) or \ |
||
| 1612 | new_values['data']['data_source_id'] <= 0: |
||
| 1613 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1614 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 1615 | data_source_id = new_values['data']['data_source_id'] |
||
| 1616 | |||
| 1617 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1618 | cursor = cnx.cursor() |
||
| 1619 | |||
| 1620 | cursor.execute(" SELECT name " |
||
| 1621 | " from tbl_energy_storage_containers " |
||
| 1622 | " WHERE id = %s ", (id_,)) |
||
| 1623 | if cursor.fetchone() is None: |
||
| 1624 | cursor.close() |
||
| 1625 | cnx.close() |
||
| 1626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1627 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1628 | |||
| 1629 | cursor.execute(" SELECT name " |
||
| 1630 | " FROM tbl_data_sources " |
||
| 1631 | " WHERE id = %s ", (data_source_id,)) |
||
| 1632 | if cursor.fetchone() is None: |
||
| 1633 | cursor.close() |
||
| 1634 | cnx.close() |
||
| 1635 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1636 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 1637 | |||
| 1638 | query = (" SELECT id " |
||
| 1639 | " FROM tbl_energy_storage_containers_data_sources " |
||
| 1640 | " WHERE energy_storage_container_id = %s AND data_source_id = %s") |
||
| 1641 | cursor.execute(query, (id_, data_source_id,)) |
||
| 1642 | if cursor.fetchone() is not None: |
||
| 1643 | cursor.close() |
||
| 1644 | cnx.close() |
||
| 1645 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1646 | description='API.ENERGY_STORAGE_CONTAINER_DATA_SOURCE_RELATION_EXISTS') |
||
| 1647 | |||
| 1648 | add_row = (" INSERT INTO " |
||
| 1649 | " tbl_energy_storage_containers_data_sources (energy_storage_container_id, data_source_id) " |
||
| 1650 | " VALUES (%s, %s) ") |
||
| 1651 | cursor.execute(add_row, (id_, data_source_id,)) |
||
| 1652 | cnx.commit() |
||
| 1653 | cursor.close() |
||
| 1654 | cnx.close() |
||
| 1655 | |||
| 1656 | resp.status = falcon.HTTP_201 |
||
| 1657 | resp.location = '/energystoragecontainers/' + str(id_) + '/datasources/' + str(data_source_id) |
||
| 1658 | |||
| 1659 | |||
| 1660 | class EnergyStorageContainerDataSourceItem: |
||
| 1661 | def __init__(self): |
||
| 1662 | pass |
||
| 1663 | |||
| 1664 | @staticmethod |
||
| 1665 | def on_options(req, resp, id_, dsid): |
||
| 1666 | _ = req |
||
| 1667 | resp.status = falcon.HTTP_200 |
||
| 1668 | _ = id_ |
||
| 1669 | |||
| 1670 | @staticmethod |
||
| 1671 | @user_logger |
||
| 1672 | def on_delete(req, resp, id_, dsid): |
||
| 1673 | admin_control(req) |
||
| 1674 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1675 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1676 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1677 | |||
| 1678 | if not dsid.isdigit() or int(dsid) <= 0: |
||
| 1679 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1680 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 1681 | |||
| 1682 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1683 | cursor = cnx.cursor() |
||
| 1684 | |||
| 1685 | cursor.execute(" SELECT name " |
||
| 1686 | " FROM tbl_energy_storage_containers " |
||
| 1687 | " WHERE id = %s ", (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.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1693 | |||
| 1694 | cursor.execute(" SELECT name " |
||
| 1695 | " FROM tbl_data_sources " |
||
| 1696 | " WHERE id = %s ", (dsid,)) |
||
| 1697 | if cursor.fetchone() is None: |
||
| 1698 | cursor.close() |
||
| 1699 | cnx.close() |
||
| 1700 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1701 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 1702 | |||
| 1703 | cursor.execute(" SELECT id " |
||
| 1704 | " FROM tbl_energy_storage_containers_data_sources " |
||
| 1705 | " WHERE energy_storage_container_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 1706 | if cursor.fetchone() is None: |
||
| 1707 | cursor.close() |
||
| 1708 | cnx.close() |
||
| 1709 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1710 | description='API.ENERGY_STORAGE_CONTAINER_DATA_SOURCE_RELATION_NOT_FOUND') |
||
| 1711 | |||
| 1712 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_data_sources " |
||
| 1713 | " WHERE energy_storage_container_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 1714 | cnx.commit() |
||
| 1715 | |||
| 1716 | cursor.close() |
||
| 1717 | cnx.close() |
||
| 1718 | |||
| 1719 | resp.status = falcon.HTTP_204 |
||
| 1720 | |||
| 1721 | |||
| 1722 | View Code Duplication | class EnergyStorageContainerDataSourcePointCollection: |
|
| 1723 | def __init__(self): |
||
| 1724 | pass |
||
| 1725 | |||
| 1726 | @staticmethod |
||
| 1727 | def on_options(req, resp, id_,): |
||
| 1728 | _ = req |
||
| 1729 | resp.status = falcon.HTTP_200 |
||
| 1730 | _ = id_ |
||
| 1731 | |||
| 1732 | @staticmethod |
||
| 1733 | def on_get(req, resp, id_,): |
||
| 1734 | if 'API-KEY' not in req.headers or \ |
||
| 1735 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1736 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1737 | access_control(req) |
||
| 1738 | else: |
||
| 1739 | api_key_control(req) |
||
| 1740 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1741 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1742 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1743 | |||
| 1744 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1745 | cursor = cnx.cursor() |
||
| 1746 | |||
| 1747 | query = (" SELECT p.id, p.name " |
||
| 1748 | " FROM tbl_points p, tbl_energy_storage_containers_data_sources ecds, tbl_data_sources ds " |
||
| 1749 | " WHERE ecds.energy_storage_container_id = %s " |
||
| 1750 | " AND ecds.data_source_id = ds.id " |
||
| 1751 | " AND p.data_source_id = ds.id " |
||
| 1752 | " ORDER BY p.id ") |
||
| 1753 | cursor.execute(query, (id_,)) |
||
| 1754 | rows = cursor.fetchall() |
||
| 1755 | |||
| 1756 | result = list() |
||
| 1757 | if rows is not None and len(rows) > 0: |
||
| 1758 | for row in rows: |
||
| 1759 | meta_result = {"id": row[0], "name": row[1]} |
||
| 1760 | result.append(meta_result) |
||
| 1761 | |||
| 1762 | resp.text = json.dumps(result) |
||
| 1763 | |||
| 1764 | |||
| 1765 | View Code Duplication | class EnergyStorageContainerDCDCCollection: |
|
| 1766 | def __init__(self): |
||
| 1767 | pass |
||
| 1768 | |||
| 1769 | @staticmethod |
||
| 1770 | def on_options(req, resp, id_): |
||
| 1771 | _ = req |
||
| 1772 | resp.status = falcon.HTTP_200 |
||
| 1773 | _ = id_ |
||
| 1774 | |||
| 1775 | @staticmethod |
||
| 1776 | def on_get(req, resp, id_): |
||
| 1777 | access_control(req) |
||
| 1778 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1779 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1780 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1781 | |||
| 1782 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1783 | cursor = cnx.cursor() |
||
| 1784 | |||
| 1785 | cursor.execute(" SELECT name " |
||
| 1786 | " FROM tbl_energy_storage_containers " |
||
| 1787 | " WHERE id = %s ", (id_,)) |
||
| 1788 | if cursor.fetchone() is None: |
||
| 1789 | cursor.close() |
||
| 1790 | cnx.close() |
||
| 1791 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1792 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1793 | |||
| 1794 | query = (" SELECT id, name, uuid " |
||
| 1795 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 1796 | " WHERE energy_storage_container_id = %s " |
||
| 1797 | " ORDER BY name ") |
||
| 1798 | cursor.execute(query, (id_,)) |
||
| 1799 | rows = cursor.fetchall() |
||
| 1800 | |||
| 1801 | result = list() |
||
| 1802 | if rows is not None and len(rows) > 0: |
||
| 1803 | for row in rows: |
||
| 1804 | meta_result = {"id": row[0], |
||
| 1805 | "name": row[1], |
||
| 1806 | "uuid": row[2] |
||
| 1807 | } |
||
| 1808 | result.append(meta_result) |
||
| 1809 | |||
| 1810 | resp.text = json.dumps(result) |
||
| 1811 | |||
| 1812 | @staticmethod |
||
| 1813 | @user_logger |
||
| 1814 | def on_post(req, resp, id_): |
||
| 1815 | """Handles POST requests""" |
||
| 1816 | admin_control(req) |
||
| 1817 | try: |
||
| 1818 | raw_json = req.stream.read().decode('utf-8') |
||
| 1819 | except UnicodeDecodeError as ex: |
||
| 1820 | print("Failed to decode request") |
||
| 1821 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1822 | title='API.BAD_REQUEST', |
||
| 1823 | description='API.INVALID_ENCODING') |
||
| 1824 | except Exception as ex: |
||
| 1825 | print("Unexpected error reading request stream") |
||
| 1826 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1827 | title='API.BAD_REQUEST', |
||
| 1828 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1829 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1830 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1831 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1832 | |||
| 1833 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1834 | cursor = cnx.cursor() |
||
| 1835 | |||
| 1836 | cursor.execute(" SELECT name " |
||
| 1837 | " FROM tbl_energy_storage_containers " |
||
| 1838 | " WHERE id = %s ", (id_,)) |
||
| 1839 | if cursor.fetchone() is None: |
||
| 1840 | cursor.close() |
||
| 1841 | cnx.close() |
||
| 1842 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1843 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1844 | |||
| 1845 | new_values = json.loads(raw_json) |
||
| 1846 | |||
| 1847 | if 'name' not in new_values['data'].keys() or \ |
||
| 1848 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1849 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1851 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME') |
||
| 1852 | name = str.strip(new_values['data']['name']) |
||
| 1853 | |||
| 1854 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1855 | cursor = cnx.cursor() |
||
| 1856 | |||
| 1857 | cursor.execute(" SELECT name " |
||
| 1858 | " FROM tbl_energy_storage_containers " |
||
| 1859 | " WHERE id = %s ", |
||
| 1860 | (id_,)) |
||
| 1861 | if cursor.fetchone() is None: |
||
| 1862 | cursor.close() |
||
| 1863 | cnx.close() |
||
| 1864 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1865 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1866 | |||
| 1867 | cursor.execute(" SELECT name " |
||
| 1868 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 1869 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 1870 | (id_, name,)) |
||
| 1871 | if cursor.fetchone() is not None: |
||
| 1872 | cursor.close() |
||
| 1873 | cnx.close() |
||
| 1874 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1875 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE') |
||
| 1876 | |||
| 1877 | add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs " |
||
| 1878 | " (name, uuid, energy_storage_container_id) " |
||
| 1879 | " VALUES (%s, %s, %s) ") |
||
| 1880 | cursor.execute(add_values, (name, |
||
| 1881 | str(uuid.uuid4()), |
||
| 1882 | id_ |
||
| 1883 | )) |
||
| 1884 | new_id = cursor.lastrowid |
||
| 1885 | cnx.commit() |
||
| 1886 | cursor.close() |
||
| 1887 | cnx.close() |
||
| 1888 | |||
| 1889 | resp.status = falcon.HTTP_201 |
||
| 1890 | resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id) |
||
| 1891 | |||
| 1892 | |||
| 1893 | View Code Duplication | class EnergyStorageContainerDCDCItem: |
|
| 1894 | def __init__(self): |
||
| 1895 | pass |
||
| 1896 | |||
| 1897 | @staticmethod |
||
| 1898 | def on_options(req, resp, id_, did): |
||
| 1899 | _ = req |
||
| 1900 | resp.status = falcon.HTTP_200 |
||
| 1901 | _ = id_ |
||
| 1902 | |||
| 1903 | @staticmethod |
||
| 1904 | def on_get(req, resp, id_, did): |
||
| 1905 | access_control(req) |
||
| 1906 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1907 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1908 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1909 | if not did.isdigit() or int(did) <= 0: |
||
| 1910 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1911 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
||
| 1912 | |||
| 1913 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1914 | cursor = cnx.cursor() |
||
| 1915 | |||
| 1916 | cursor.execute(" SELECT name " |
||
| 1917 | " FROM tbl_energy_storage_containers " |
||
| 1918 | " WHERE id = %s ", (id_,)) |
||
| 1919 | if cursor.fetchone() is None: |
||
| 1920 | cursor.close() |
||
| 1921 | cnx.close() |
||
| 1922 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1923 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1924 | |||
| 1925 | query = (" SELECT id, name, uuid " |
||
| 1926 | " FROM tbl_energy_storage_containers ") |
||
| 1927 | cursor.execute(query) |
||
| 1928 | rows_energystoragecontainers = cursor.fetchall() |
||
| 1929 | |||
| 1930 | energy_storage_container_dict = dict() |
||
| 1931 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 1932 | for row in rows_energystoragecontainers: |
||
| 1933 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 1934 | "name": row[1], |
||
| 1935 | "uuid": row[2]} |
||
| 1936 | |||
| 1937 | query = (" SELECT id, name, uuid " |
||
| 1938 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 1939 | " WHERE id = %s ") |
||
| 1940 | cursor.execute(query, (did,)) |
||
| 1941 | row = cursor.fetchone() |
||
| 1942 | cursor.close() |
||
| 1943 | cnx.close() |
||
| 1944 | |||
| 1945 | if row is None: |
||
| 1946 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1947 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 1948 | else: |
||
| 1949 | meta_result = {"id": row[0], |
||
| 1950 | "name": row[1], |
||
| 1951 | "uuid": row[2] |
||
| 1952 | } |
||
| 1953 | |||
| 1954 | resp.text = json.dumps(meta_result) |
||
| 1955 | |||
| 1956 | @staticmethod |
||
| 1957 | @user_logger |
||
| 1958 | def on_delete(req, resp, id_, did): |
||
| 1959 | admin_control(req) |
||
| 1960 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1961 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1962 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1963 | if not did.isdigit() or int(did) <= 0: |
||
| 1964 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1965 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
||
| 1966 | |||
| 1967 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1968 | cursor = cnx.cursor() |
||
| 1969 | |||
| 1970 | cursor.execute(" SELECT name " |
||
| 1971 | " FROM tbl_energy_storage_containers " |
||
| 1972 | " WHERE id = %s ", (id_,)) |
||
| 1973 | if cursor.fetchone() is None: |
||
| 1974 | cursor.close() |
||
| 1975 | cnx.close() |
||
| 1976 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1977 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1978 | |||
| 1979 | cursor.execute(" SELECT name " |
||
| 1980 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 1981 | " WHERE id = %s ", (did,)) |
||
| 1982 | if cursor.fetchone() is None: |
||
| 1983 | cursor.close() |
||
| 1984 | cnx.close() |
||
| 1985 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1986 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 1987 | |||
| 1988 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs " |
||
| 1989 | " WHERE id = %s ", (did,)) |
||
| 1990 | cnx.commit() |
||
| 1991 | |||
| 1992 | cursor.close() |
||
| 1993 | cnx.close() |
||
| 1994 | |||
| 1995 | resp.status = falcon.HTTP_204 |
||
| 1996 | |||
| 1997 | @staticmethod |
||
| 1998 | @user_logger |
||
| 1999 | def on_put(req, resp, id_, did): |
||
| 2000 | """Handles PUT requests""" |
||
| 2001 | admin_control(req) |
||
| 2002 | try: |
||
| 2003 | raw_json = req.stream.read().decode('utf-8') |
||
| 2004 | except UnicodeDecodeError as ex: |
||
| 2005 | print("Failed to decode request") |
||
| 2006 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2007 | title='API.BAD_REQUEST', |
||
| 2008 | description='API.INVALID_ENCODING') |
||
| 2009 | except Exception as ex: |
||
| 2010 | print("Unexpected error reading request stream") |
||
| 2011 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2012 | title='API.BAD_REQUEST', |
||
| 2013 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2014 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2015 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2016 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2017 | |||
| 2018 | if not did.isdigit() or int(did) <= 0: |
||
| 2019 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2020 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
||
| 2021 | |||
| 2022 | new_values = json.loads(raw_json) |
||
| 2023 | |||
| 2024 | if 'name' not in new_values['data'].keys() or \ |
||
| 2025 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2026 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2027 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2028 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME') |
||
| 2029 | name = str.strip(new_values['data']['name']) |
||
| 2030 | |||
| 2031 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2032 | cursor = cnx.cursor() |
||
| 2033 | |||
| 2034 | cursor.execute(" SELECT name " |
||
| 2035 | " FROM tbl_energy_storage_containers " |
||
| 2036 | " WHERE id = %s ", (id_,)) |
||
| 2037 | if cursor.fetchone() is None: |
||
| 2038 | cursor.close() |
||
| 2039 | cnx.close() |
||
| 2040 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2041 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2042 | |||
| 2043 | cursor.execute(" SELECT name " |
||
| 2044 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 2045 | " WHERE id = %s ", (did,)) |
||
| 2046 | if cursor.fetchone() is None: |
||
| 2047 | cursor.close() |
||
| 2048 | cnx.close() |
||
| 2049 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2050 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 2051 | |||
| 2052 | cursor.execute(" SELECT name " |
||
| 2053 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 2054 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 2055 | (id_, name, did)) |
||
| 2056 | if cursor.fetchone() is not None: |
||
| 2057 | cursor.close() |
||
| 2058 | cnx.close() |
||
| 2059 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2060 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE') |
||
| 2061 | |||
| 2062 | update_row = (" UPDATE tbl_energy_storage_containers_dcdcs " |
||
| 2063 | " SET name = %s " |
||
| 2064 | " WHERE id = %s ") |
||
| 2065 | cursor.execute(update_row, (name, |
||
| 2066 | did)) |
||
| 2067 | cnx.commit() |
||
| 2068 | |||
| 2069 | cursor.close() |
||
| 2070 | cnx.close() |
||
| 2071 | |||
| 2072 | resp.status = falcon.HTTP_200 |
||
| 2073 | |||
| 2074 | |||
| 2075 | View Code Duplication | class EnergyStorageContainerDCDCPointCollection: |
|
| 2076 | def __init__(self): |
||
| 2077 | pass |
||
| 2078 | |||
| 2079 | @staticmethod |
||
| 2080 | def on_options(req, resp, id_, did): |
||
| 2081 | _ = req |
||
| 2082 | resp.status = falcon.HTTP_200 |
||
| 2083 | _ = id_ |
||
| 2084 | |||
| 2085 | @staticmethod |
||
| 2086 | def on_get(req, resp, id_, did): |
||
| 2087 | if 'API-KEY' not in req.headers or \ |
||
| 2088 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2089 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2090 | access_control(req) |
||
| 2091 | else: |
||
| 2092 | api_key_control(req) |
||
| 2093 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2094 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2095 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2096 | if not did.isdigit() or int(did) <= 0: |
||
| 2097 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2098 | description='API.INVALID_DCDC_ID') |
||
| 2099 | |||
| 2100 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2101 | cursor = cnx.cursor() |
||
| 2102 | |||
| 2103 | cursor.execute(" SELECT name " |
||
| 2104 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 2105 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did, )) |
||
| 2106 | if cursor.fetchone() is None: |
||
| 2107 | cursor.close() |
||
| 2108 | cnx.close() |
||
| 2109 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2110 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 2111 | |||
| 2112 | query = (" SELECT p.id, p.name, " |
||
| 2113 | " ds.id, ds.name, ds.uuid, " |
||
| 2114 | " p.address " |
||
| 2115 | " FROM tbl_points p, tbl_energy_storage_containers_dcdcs_points mp, tbl_data_sources ds " |
||
| 2116 | " WHERE mp.dcdc_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 2117 | " ORDER BY p.name ") |
||
| 2118 | cursor.execute(query, (did,)) |
||
| 2119 | rows = cursor.fetchall() |
||
| 2120 | |||
| 2121 | result = list() |
||
| 2122 | if rows is not None and len(rows) > 0: |
||
| 2123 | for row in rows: |
||
| 2124 | meta_result = {"id": row[0], "name": row[1], |
||
| 2125 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 2126 | "address": row[5]} |
||
| 2127 | result.append(meta_result) |
||
| 2128 | |||
| 2129 | resp.text = json.dumps(result) |
||
| 2130 | |||
| 2131 | @staticmethod |
||
| 2132 | @user_logger |
||
| 2133 | def on_post(req, resp, id_, did): |
||
| 2134 | """Handles POST requests""" |
||
| 2135 | admin_control(req) |
||
| 2136 | try: |
||
| 2137 | raw_json = req.stream.read().decode('utf-8') |
||
| 2138 | except UnicodeDecodeError as ex: |
||
| 2139 | print("Failed to decode request") |
||
| 2140 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2141 | title='API.BAD_REQUEST', |
||
| 2142 | description='API.INVALID_ENCODING') |
||
| 2143 | except Exception as ex: |
||
| 2144 | print("Unexpected error reading request stream") |
||
| 2145 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2146 | title='API.BAD_REQUEST', |
||
| 2147 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2148 | |||
| 2149 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2151 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2152 | if not did.isdigit() or int(did) <= 0: |
||
| 2153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2154 | description='API.INVALID_DCDC_ID') |
||
| 2155 | |||
| 2156 | new_values = json.loads(raw_json) |
||
| 2157 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2158 | cursor = cnx.cursor() |
||
| 2159 | |||
| 2160 | cursor.execute(" SELECT name " |
||
| 2161 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 2162 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did,)) |
||
| 2163 | if cursor.fetchone() is None: |
||
| 2164 | cursor.close() |
||
| 2165 | cnx.close() |
||
| 2166 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2167 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 2168 | |||
| 2169 | cursor.execute(" SELECT name, object_type " |
||
| 2170 | " FROM tbl_points " |
||
| 2171 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 2172 | row = cursor.fetchone() |
||
| 2173 | if row is None: |
||
| 2174 | cursor.close() |
||
| 2175 | cnx.close() |
||
| 2176 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2177 | description='API.POINT_NOT_FOUND') |
||
| 2178 | |||
| 2179 | query = (" SELECT id " |
||
| 2180 | " FROM tbl_energy_storage_containers_dcdcs_points " |
||
| 2181 | " WHERE dcdc_id = %s AND point_id = %s") |
||
| 2182 | cursor.execute(query, (did, new_values['data']['point_id'],)) |
||
| 2183 | if cursor.fetchone() is not None: |
||
| 2184 | cursor.close() |
||
| 2185 | cnx.close() |
||
| 2186 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2187 | description='API.DCDC_POINT_RELATION_EXISTS') |
||
| 2188 | |||
| 2189 | add_row = (" INSERT INTO tbl_energy_storage_containers_dcdcs_points (dcdc_id, point_id) " |
||
| 2190 | " VALUES (%s, %s) ") |
||
| 2191 | cursor.execute(add_row, (did, new_values['data']['point_id'],)) |
||
| 2192 | cnx.commit() |
||
| 2193 | cursor.close() |
||
| 2194 | cnx.close() |
||
| 2195 | |||
| 2196 | resp.status = falcon.HTTP_201 |
||
| 2197 | resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(did) + '/points/' + \ |
||
| 2198 | str(new_values['data']['point_id']) |
||
| 2199 | |||
| 2200 | |||
| 2201 | View Code Duplication | class EnergyStorageContainerDCDCPointItem: |
|
| 2202 | def __init__(self): |
||
| 2203 | pass |
||
| 2204 | |||
| 2205 | @staticmethod |
||
| 2206 | def on_options(req, resp, id_, did, pid): |
||
| 2207 | _ = req |
||
| 2208 | resp.status = falcon.HTTP_200 |
||
| 2209 | _ = id_ |
||
| 2210 | |||
| 2211 | @staticmethod |
||
| 2212 | @user_logger |
||
| 2213 | def on_delete(req, resp, id_, did, pid): |
||
| 2214 | """Handles DELETE requests""" |
||
| 2215 | admin_control(req) |
||
| 2216 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2217 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2218 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2219 | if not did.isdigit() or int(did) <= 0: |
||
| 2220 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2221 | description='API.INVALID_DCDC_ID') |
||
| 2222 | if not pid.isdigit() or int(pid) <= 0: |
||
| 2223 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2224 | description='API.INVALID_POINT_ID') |
||
| 2225 | |||
| 2226 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2227 | cursor = cnx.cursor() |
||
| 2228 | |||
| 2229 | cursor.execute(" SELECT name " |
||
| 2230 | " FROM tbl_energy_storage_containers_dcdcs " |
||
| 2231 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did,)) |
||
| 2232 | if cursor.fetchone() is None: |
||
| 2233 | cursor.close() |
||
| 2234 | cnx.close() |
||
| 2235 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2236 | description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
||
| 2237 | |||
| 2238 | cursor.execute(" SELECT name " |
||
| 2239 | " FROM tbl_points " |
||
| 2240 | " WHERE id = %s ", (pid,)) |
||
| 2241 | if cursor.fetchone() is None: |
||
| 2242 | cursor.close() |
||
| 2243 | cnx.close() |
||
| 2244 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2245 | description='API.POINT_NOT_FOUND') |
||
| 2246 | |||
| 2247 | cursor.execute(" SELECT id " |
||
| 2248 | " FROM tbl_energy_storage_containers_dcdcs_points " |
||
| 2249 | " WHERE dcdc_id = %s AND point_id = %s ", (did, pid)) |
||
| 2250 | if cursor.fetchone() is None: |
||
| 2251 | cursor.close() |
||
| 2252 | cnx.close() |
||
| 2253 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2254 | description='API.DCDC_POINT_RELATION_NOT_FOUND') |
||
| 2255 | |||
| 2256 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs_points " |
||
| 2257 | " WHERE dcdc_id = %s AND point_id = %s ", (did, pid)) |
||
| 2258 | cnx.commit() |
||
| 2259 | |||
| 2260 | cursor.close() |
||
| 2261 | cnx.close() |
||
| 2262 | |||
| 2263 | resp.status = falcon.HTTP_204 |
||
| 2264 | |||
| 2265 | |||
| 2266 | View Code Duplication | class EnergyStorageContainerFirecontrolCollection: |
|
| 2267 | def __init__(self): |
||
| 2268 | pass |
||
| 2269 | |||
| 2270 | @staticmethod |
||
| 2271 | def on_options(req, resp, id_): |
||
| 2272 | _ = req |
||
| 2273 | resp.status = falcon.HTTP_200 |
||
| 2274 | _ = id_ |
||
| 2275 | |||
| 2276 | @staticmethod |
||
| 2277 | def on_get(req, resp, id_): |
||
| 2278 | access_control(req) |
||
| 2279 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2280 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2281 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2282 | |||
| 2283 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2284 | cursor = cnx.cursor() |
||
| 2285 | |||
| 2286 | cursor.execute(" SELECT name " |
||
| 2287 | " FROM tbl_energy_storage_containers " |
||
| 2288 | " WHERE id = %s ", (id_,)) |
||
| 2289 | if cursor.fetchone() is None: |
||
| 2290 | cursor.close() |
||
| 2291 | cnx.close() |
||
| 2292 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2293 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2294 | |||
| 2295 | query = (" SELECT id, name, uuid " |
||
| 2296 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2297 | " WHERE energy_storage_container_id = %s " |
||
| 2298 | " ORDER BY name ") |
||
| 2299 | cursor.execute(query, (id_,)) |
||
| 2300 | rows = cursor.fetchall() |
||
| 2301 | |||
| 2302 | result = list() |
||
| 2303 | if rows is not None and len(rows) > 0: |
||
| 2304 | for row in rows: |
||
| 2305 | meta_result = {"id": row[0], |
||
| 2306 | "name": row[1], |
||
| 2307 | "uuid": row[2] |
||
| 2308 | } |
||
| 2309 | result.append(meta_result) |
||
| 2310 | |||
| 2311 | resp.text = json.dumps(result) |
||
| 2312 | |||
| 2313 | @staticmethod |
||
| 2314 | @user_logger |
||
| 2315 | def on_post(req, resp, id_): |
||
| 2316 | """Handles POST requests""" |
||
| 2317 | admin_control(req) |
||
| 2318 | try: |
||
| 2319 | raw_json = req.stream.read().decode('utf-8') |
||
| 2320 | except UnicodeDecodeError as ex: |
||
| 2321 | print("Failed to decode request") |
||
| 2322 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2323 | title='API.BAD_REQUEST', |
||
| 2324 | description='API.INVALID_ENCODING') |
||
| 2325 | except Exception as ex: |
||
| 2326 | print("Unexpected error reading request stream") |
||
| 2327 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2328 | title='API.BAD_REQUEST', |
||
| 2329 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2330 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2331 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2332 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2333 | |||
| 2334 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2335 | cursor = cnx.cursor() |
||
| 2336 | |||
| 2337 | cursor.execute(" SELECT name " |
||
| 2338 | " FROM tbl_energy_storage_containers " |
||
| 2339 | " WHERE id = %s ", (id_,)) |
||
| 2340 | if cursor.fetchone() is None: |
||
| 2341 | cursor.close() |
||
| 2342 | cnx.close() |
||
| 2343 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2344 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2345 | |||
| 2346 | new_values = json.loads(raw_json) |
||
| 2347 | |||
| 2348 | if 'name' not in new_values['data'].keys() or \ |
||
| 2349 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2350 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2351 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2352 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME') |
||
| 2353 | name = str.strip(new_values['data']['name']) |
||
| 2354 | |||
| 2355 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2356 | cursor = cnx.cursor() |
||
| 2357 | |||
| 2358 | cursor.execute(" SELECT name " |
||
| 2359 | " FROM tbl_energy_storage_containers " |
||
| 2360 | " WHERE id = %s ", |
||
| 2361 | (id_,)) |
||
| 2362 | if cursor.fetchone() is None: |
||
| 2363 | cursor.close() |
||
| 2364 | cnx.close() |
||
| 2365 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2366 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2367 | |||
| 2368 | cursor.execute(" SELECT name " |
||
| 2369 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2370 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 2371 | (id_, name,)) |
||
| 2372 | if cursor.fetchone() is not None: |
||
| 2373 | cursor.close() |
||
| 2374 | cnx.close() |
||
| 2375 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2376 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE') |
||
| 2377 | |||
| 2378 | add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols " |
||
| 2379 | " (name, uuid, energy_storage_container_id) " |
||
| 2380 | " VALUES (%s, %s, %s) ") |
||
| 2381 | cursor.execute(add_values, (name, |
||
| 2382 | str(uuid.uuid4()), |
||
| 2383 | id_ |
||
| 2384 | )) |
||
| 2385 | new_id = cursor.lastrowid |
||
| 2386 | cnx.commit() |
||
| 2387 | cursor.close() |
||
| 2388 | cnx.close() |
||
| 2389 | |||
| 2390 | resp.status = falcon.HTTP_201 |
||
| 2391 | resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id) |
||
| 2392 | |||
| 2393 | |||
| 2394 | View Code Duplication | class EnergyStorageContainerFirecontrolItem: |
|
| 2395 | def __init__(self): |
||
| 2396 | pass |
||
| 2397 | |||
| 2398 | @staticmethod |
||
| 2399 | def on_options(req, resp, id_, fid): |
||
| 2400 | _ = req |
||
| 2401 | resp.status = falcon.HTTP_200 |
||
| 2402 | _ = id_ |
||
| 2403 | |||
| 2404 | @staticmethod |
||
| 2405 | def on_get(req, resp, id_, fid): |
||
| 2406 | access_control(req) |
||
| 2407 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2408 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2409 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2410 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2411 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2412 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2413 | |||
| 2414 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2415 | cursor = cnx.cursor() |
||
| 2416 | |||
| 2417 | cursor.execute(" SELECT name " |
||
| 2418 | " FROM tbl_energy_storage_containers " |
||
| 2419 | " WHERE id = %s ", (id_,)) |
||
| 2420 | if cursor.fetchone() is None: |
||
| 2421 | cursor.close() |
||
| 2422 | cnx.close() |
||
| 2423 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2424 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2425 | |||
| 2426 | query = (" SELECT id, name, uuid " |
||
| 2427 | " FROM tbl_energy_storage_containers ") |
||
| 2428 | cursor.execute(query) |
||
| 2429 | rows_energystoragecontainers = cursor.fetchall() |
||
| 2430 | |||
| 2431 | energy_storage_container_dict = dict() |
||
| 2432 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 2433 | for row in rows_energystoragecontainers: |
||
| 2434 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 2435 | "name": row[1], |
||
| 2436 | "uuid": row[2]} |
||
| 2437 | |||
| 2438 | query = (" SELECT id, name, uuid " |
||
| 2439 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2440 | " WHERE id = %s ") |
||
| 2441 | cursor.execute(query, (fid,)) |
||
| 2442 | row = cursor.fetchone() |
||
| 2443 | cursor.close() |
||
| 2444 | cnx.close() |
||
| 2445 | |||
| 2446 | if row is None: |
||
| 2447 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2448 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2449 | else: |
||
| 2450 | meta_result = {"id": row[0], |
||
| 2451 | "name": row[1], |
||
| 2452 | "uuid": row[2] |
||
| 2453 | } |
||
| 2454 | |||
| 2455 | resp.text = json.dumps(meta_result) |
||
| 2456 | |||
| 2457 | @staticmethod |
||
| 2458 | @user_logger |
||
| 2459 | def on_delete(req, resp, id_, fid): |
||
| 2460 | admin_control(req) |
||
| 2461 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2462 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2463 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2464 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2465 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2466 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2467 | |||
| 2468 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2469 | cursor = cnx.cursor() |
||
| 2470 | |||
| 2471 | cursor.execute(" SELECT name " |
||
| 2472 | " FROM tbl_energy_storage_containers " |
||
| 2473 | " WHERE id = %s ", (id_,)) |
||
| 2474 | if cursor.fetchone() is None: |
||
| 2475 | cursor.close() |
||
| 2476 | cnx.close() |
||
| 2477 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2478 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2479 | |||
| 2480 | cursor.execute(" SELECT name " |
||
| 2481 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2482 | " WHERE id = %s ", (fid,)) |
||
| 2483 | if cursor.fetchone() is None: |
||
| 2484 | cursor.close() |
||
| 2485 | cnx.close() |
||
| 2486 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2487 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2488 | |||
| 2489 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols " |
||
| 2490 | " WHERE id = %s ", (fid,)) |
||
| 2491 | cnx.commit() |
||
| 2492 | |||
| 2493 | cursor.close() |
||
| 2494 | cnx.close() |
||
| 2495 | |||
| 2496 | resp.status = falcon.HTTP_204 |
||
| 2497 | |||
| 2498 | @staticmethod |
||
| 2499 | @user_logger |
||
| 2500 | def on_put(req, resp, id_, fid): |
||
| 2501 | """Handles PUT requests""" |
||
| 2502 | admin_control(req) |
||
| 2503 | try: |
||
| 2504 | raw_json = req.stream.read().decode('utf-8') |
||
| 2505 | except UnicodeDecodeError as ex: |
||
| 2506 | print("Failed to decode request") |
||
| 2507 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2508 | title='API.BAD_REQUEST', |
||
| 2509 | description='API.INVALID_ENCODING') |
||
| 2510 | except Exception as ex: |
||
| 2511 | print("Unexpected error reading request stream") |
||
| 2512 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2513 | title='API.BAD_REQUEST', |
||
| 2514 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2515 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2516 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2517 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2518 | |||
| 2519 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2521 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2522 | |||
| 2523 | new_values = json.loads(raw_json) |
||
| 2524 | |||
| 2525 | if 'name' not in new_values['data'].keys() or \ |
||
| 2526 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2527 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2528 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2529 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME') |
||
| 2530 | name = str.strip(new_values['data']['name']) |
||
| 2531 | |||
| 2532 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2533 | cursor = cnx.cursor() |
||
| 2534 | |||
| 2535 | cursor.execute(" SELECT name " |
||
| 2536 | " FROM tbl_energy_storage_containers " |
||
| 2537 | " WHERE id = %s ", (id_,)) |
||
| 2538 | if cursor.fetchone() is None: |
||
| 2539 | cursor.close() |
||
| 2540 | cnx.close() |
||
| 2541 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2542 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2543 | |||
| 2544 | cursor.execute(" SELECT name " |
||
| 2545 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2546 | " WHERE id = %s ", (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 | cursor.execute(" SELECT name " |
||
| 2554 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2555 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 2556 | (id_, name, fid)) |
||
| 2557 | if cursor.fetchone() is not None: |
||
| 2558 | cursor.close() |
||
| 2559 | cnx.close() |
||
| 2560 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2561 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE') |
||
| 2562 | |||
| 2563 | update_row = (" UPDATE tbl_energy_storage_containers_firecontrols " |
||
| 2564 | " SET name = %s " |
||
| 2565 | " WHERE id = %s ") |
||
| 2566 | cursor.execute(update_row, (name, |
||
| 2567 | fid)) |
||
| 2568 | cnx.commit() |
||
| 2569 | |||
| 2570 | cursor.close() |
||
| 2571 | cnx.close() |
||
| 2572 | |||
| 2573 | resp.status = falcon.HTTP_200 |
||
| 2574 | |||
| 2575 | |||
| 2576 | View Code Duplication | class EnergyStorageContainerFirecontrolPointCollection: |
|
| 2577 | def __init__(self): |
||
| 2578 | pass |
||
| 2579 | |||
| 2580 | @staticmethod |
||
| 2581 | def on_options(req, resp, id_, fid): |
||
| 2582 | _ = req |
||
| 2583 | resp.status = falcon.HTTP_200 |
||
| 2584 | _ = id_ |
||
| 2585 | |||
| 2586 | @staticmethod |
||
| 2587 | def on_get(req, resp, id_, fid): |
||
| 2588 | if 'API-KEY' not in req.headers or \ |
||
| 2589 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2590 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2591 | access_control(req) |
||
| 2592 | else: |
||
| 2593 | api_key_control(req) |
||
| 2594 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2596 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2597 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2598 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2599 | description='API.INVALID_FIRECONTROL_ID') |
||
| 2600 | |||
| 2601 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2602 | cursor = cnx.cursor() |
||
| 2603 | |||
| 2604 | cursor.execute(" SELECT name " |
||
| 2605 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2606 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, )) |
||
| 2607 | if cursor.fetchone() is None: |
||
| 2608 | cursor.close() |
||
| 2609 | cnx.close() |
||
| 2610 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2611 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2612 | |||
| 2613 | query = (" SELECT p.id, p.name, " |
||
| 2614 | " ds.id, ds.name, ds.uuid, " |
||
| 2615 | " p.address " |
||
| 2616 | " FROM tbl_points p, tbl_energy_storage_containers_firecontrols_points mp, tbl_data_sources ds " |
||
| 2617 | " WHERE mp.firecontrol_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 2618 | " ORDER BY p.name ") |
||
| 2619 | cursor.execute(query, (fid,)) |
||
| 2620 | rows = cursor.fetchall() |
||
| 2621 | |||
| 2622 | result = list() |
||
| 2623 | if rows is not None and len(rows) > 0: |
||
| 2624 | for row in rows: |
||
| 2625 | meta_result = {"id": row[0], "name": row[1], |
||
| 2626 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 2627 | "address": row[5]} |
||
| 2628 | result.append(meta_result) |
||
| 2629 | |||
| 2630 | resp.text = json.dumps(result) |
||
| 2631 | |||
| 2632 | @staticmethod |
||
| 2633 | @user_logger |
||
| 2634 | def on_post(req, resp, id_, fid): |
||
| 2635 | """Handles POST requests""" |
||
| 2636 | admin_control(req) |
||
| 2637 | try: |
||
| 2638 | raw_json = req.stream.read().decode('utf-8') |
||
| 2639 | except UnicodeDecodeError as ex: |
||
| 2640 | print("Failed to decode request") |
||
| 2641 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2642 | title='API.BAD_REQUEST', |
||
| 2643 | description='API.INVALID_ENCODING') |
||
| 2644 | except Exception as ex: |
||
| 2645 | print("Unexpected error reading request stream") |
||
| 2646 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2647 | title='API.BAD_REQUEST', |
||
| 2648 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2649 | |||
| 2650 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2652 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2653 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2654 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2655 | description='API.INVALID_FIRECONTROL_ID') |
||
| 2656 | |||
| 2657 | new_values = json.loads(raw_json) |
||
| 2658 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2659 | cursor = cnx.cursor() |
||
| 2660 | |||
| 2661 | cursor.execute(" SELECT name " |
||
| 2662 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2663 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
||
| 2664 | if cursor.fetchone() is None: |
||
| 2665 | cursor.close() |
||
| 2666 | cnx.close() |
||
| 2667 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2668 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2669 | |||
| 2670 | cursor.execute(" SELECT name, object_type " |
||
| 2671 | " FROM tbl_points " |
||
| 2672 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 2673 | row = cursor.fetchone() |
||
| 2674 | if row is None: |
||
| 2675 | cursor.close() |
||
| 2676 | cnx.close() |
||
| 2677 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2678 | description='API.POINT_NOT_FOUND') |
||
| 2679 | |||
| 2680 | query = (" SELECT id " |
||
| 2681 | " FROM tbl_energy_storage_containers_firecontrols_points " |
||
| 2682 | " WHERE firecontrol_id = %s AND point_id = %s") |
||
| 2683 | cursor.execute(query, (fid, new_values['data']['point_id'],)) |
||
| 2684 | if cursor.fetchone() is not None: |
||
| 2685 | cursor.close() |
||
| 2686 | cnx.close() |
||
| 2687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2688 | description='API.FIRECONTROL_POINT_RELATION_EXISTS') |
||
| 2689 | |||
| 2690 | add_row = (" INSERT INTO tbl_energy_storage_containers_firecontrols_points (firecontrol_id, point_id) " |
||
| 2691 | " VALUES (%s, %s) ") |
||
| 2692 | cursor.execute(add_row, (fid, new_values['data']['point_id'],)) |
||
| 2693 | cnx.commit() |
||
| 2694 | cursor.close() |
||
| 2695 | cnx.close() |
||
| 2696 | |||
| 2697 | resp.status = falcon.HTTP_201 |
||
| 2698 | resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(fid) + '/points/' + \ |
||
| 2699 | str(new_values['data']['point_id']) |
||
| 2700 | |||
| 2701 | |||
| 2702 | View Code Duplication | class EnergyStorageContainerFirecontrolPointItem: |
|
| 2703 | def __init__(self): |
||
| 2704 | pass |
||
| 2705 | |||
| 2706 | @staticmethod |
||
| 2707 | def on_options(req, resp, id_, fid, pid): |
||
| 2708 | _ = req |
||
| 2709 | resp.status = falcon.HTTP_200 |
||
| 2710 | _ = id_ |
||
| 2711 | |||
| 2712 | @staticmethod |
||
| 2713 | @user_logger |
||
| 2714 | def on_delete(req, resp, id_, fid, pid): |
||
| 2715 | """Handles DELETE requests""" |
||
| 2716 | admin_control(req) |
||
| 2717 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2718 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2719 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2720 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2721 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2722 | description='API.INVALID_FIRECONTROL_ID') |
||
| 2723 | if not pid.isdigit() or int(pid) <= 0: |
||
| 2724 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2725 | description='API.INVALID_POINT_ID') |
||
| 2726 | |||
| 2727 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2728 | cursor = cnx.cursor() |
||
| 2729 | |||
| 2730 | cursor.execute(" SELECT name " |
||
| 2731 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2732 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
||
| 2733 | if cursor.fetchone() is None: |
||
| 2734 | cursor.close() |
||
| 2735 | cnx.close() |
||
| 2736 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2737 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2738 | |||
| 2739 | cursor.execute(" SELECT name " |
||
| 2740 | " FROM tbl_points " |
||
| 2741 | " WHERE id = %s ", (pid,)) |
||
| 2742 | if cursor.fetchone() is None: |
||
| 2743 | cursor.close() |
||
| 2744 | cnx.close() |
||
| 2745 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2746 | description='API.POINT_NOT_FOUND') |
||
| 2747 | |||
| 2748 | cursor.execute(" SELECT id " |
||
| 2749 | " FROM tbl_energy_storage_containers_firecontrols_points " |
||
| 2750 | " WHERE firecontrol_id = %s AND point_id = %s ", (fid, pid)) |
||
| 2751 | if cursor.fetchone() is None: |
||
| 2752 | cursor.close() |
||
| 2753 | cnx.close() |
||
| 2754 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2755 | description='API.FIRECONTROL_POINT_RELATION_NOT_FOUND') |
||
| 2756 | |||
| 2757 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols_points " |
||
| 2758 | " WHERE firecontrol_id = %s AND point_id = %s ", (fid, pid)) |
||
| 2759 | cnx.commit() |
||
| 2760 | |||
| 2761 | cursor.close() |
||
| 2762 | cnx.close() |
||
| 2763 | |||
| 2764 | resp.status = falcon.HTTP_204 |
||
| 2765 | |||
| 2766 | |||
| 2767 | View Code Duplication | class EnergyStorageContainerGridCollection: |
|
| 2768 | def __init__(self): |
||
| 2769 | pass |
||
| 2770 | |||
| 2771 | @staticmethod |
||
| 2772 | def on_options(req, resp, id_): |
||
| 2773 | _ = req |
||
| 2774 | resp.status = falcon.HTTP_200 |
||
| 2775 | _ = id_ |
||
| 2776 | |||
| 2777 | @staticmethod |
||
| 2778 | def on_get(req, resp, id_): |
||
| 2779 | access_control(req) |
||
| 2780 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2781 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2782 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2783 | |||
| 2784 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2785 | cursor = cnx.cursor() |
||
| 2786 | |||
| 2787 | cursor.execute(" SELECT name " |
||
| 2788 | " FROM tbl_energy_storage_containers " |
||
| 2789 | " WHERE id = %s ", (id_,)) |
||
| 2790 | if cursor.fetchone() is None: |
||
| 2791 | cursor.close() |
||
| 2792 | cnx.close() |
||
| 2793 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2794 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2795 | |||
| 2796 | # query meter dict |
||
| 2797 | query = (" SELECT id, name, uuid " |
||
| 2798 | " FROM tbl_meters ") |
||
| 2799 | cursor.execute(query) |
||
| 2800 | rows_meters = cursor.fetchall() |
||
| 2801 | |||
| 2802 | meter_dict = dict() |
||
| 2803 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2804 | for row in rows_meters: |
||
| 2805 | meter_dict[row[0]] = {"id": row[0], |
||
| 2806 | "name": row[1], |
||
| 2807 | "uuid": row[2]} |
||
| 2808 | # query point dict |
||
| 2809 | query = (" SELECT id, name " |
||
| 2810 | " FROM tbl_points ") |
||
| 2811 | cursor.execute(query) |
||
| 2812 | rows_points = cursor.fetchall() |
||
| 2813 | |||
| 2814 | point_dict = dict() |
||
| 2815 | if rows_points is not None and len(rows_points) > 0: |
||
| 2816 | for row in rows_points: |
||
| 2817 | point_dict[row[0]] = {"id": row[0], |
||
| 2818 | "name": row[1]} |
||
| 2819 | |||
| 2820 | query = (" SELECT id, name, uuid, " |
||
| 2821 | " power_point_id, buy_meter_id, sell_meter_id, capacity " |
||
| 2822 | " FROM tbl_energy_storage_containers_grids " |
||
| 2823 | " WHERE energy_storage_container_id = %s " |
||
| 2824 | " ORDER BY name ") |
||
| 2825 | cursor.execute(query, (id_,)) |
||
| 2826 | rows = cursor.fetchall() |
||
| 2827 | |||
| 2828 | result = list() |
||
| 2829 | if rows is not None and len(rows) > 0: |
||
| 2830 | for row in rows: |
||
| 2831 | meta_result = {"id": row[0], |
||
| 2832 | "name": row[1], |
||
| 2833 | "uuid": row[2], |
||
| 2834 | "power_point": point_dict.get(row[3]), |
||
| 2835 | "buy_meter": meter_dict.get(row[4]), |
||
| 2836 | "sell_meter": meter_dict.get(row[5]), |
||
| 2837 | "capacity": row[6] |
||
| 2838 | } |
||
| 2839 | result.append(meta_result) |
||
| 2840 | |||
| 2841 | resp.text = json.dumps(result) |
||
| 2842 | |||
| 2843 | @staticmethod |
||
| 2844 | @user_logger |
||
| 2845 | def on_post(req, resp, id_): |
||
| 2846 | """Handles POST requests""" |
||
| 2847 | admin_control(req) |
||
| 2848 | try: |
||
| 2849 | raw_json = req.stream.read().decode('utf-8') |
||
| 2850 | except UnicodeDecodeError as ex: |
||
| 2851 | print("Failed to decode request") |
||
| 2852 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2853 | title='API.BAD_REQUEST', |
||
| 2854 | description='API.INVALID_ENCODING') |
||
| 2855 | except Exception as ex: |
||
| 2856 | print("Unexpected error reading request stream") |
||
| 2857 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2858 | title='API.BAD_REQUEST', |
||
| 2859 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2860 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2861 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2862 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2863 | |||
| 2864 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2865 | cursor = cnx.cursor() |
||
| 2866 | |||
| 2867 | cursor.execute(" SELECT name " |
||
| 2868 | " FROM tbl_energy_storage_containers " |
||
| 2869 | " WHERE id = %s ", (id_,)) |
||
| 2870 | if cursor.fetchone() is None: |
||
| 2871 | cursor.close() |
||
| 2872 | cnx.close() |
||
| 2873 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2874 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2875 | |||
| 2876 | new_values = json.loads(raw_json) |
||
| 2877 | |||
| 2878 | if 'name' not in new_values['data'].keys() or \ |
||
| 2879 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2880 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2881 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2882 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
||
| 2883 | name = str.strip(new_values['data']['name']) |
||
| 2884 | |||
| 2885 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2886 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2887 | new_values['data']['power_point_id'] <= 0: |
||
| 2888 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2889 | description='API.INVALID_POWER_POINT_ID') |
||
| 2890 | power_point_id = new_values['data']['power_point_id'] |
||
| 2891 | |||
| 2892 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 2893 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 2894 | new_values['data']['buy_meter_id'] <= 0: |
||
| 2895 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2896 | description='API.INVALID_BUY_METER_ID') |
||
| 2897 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 2898 | |||
| 2899 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 2900 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 2901 | new_values['data']['sell_meter_id'] <= 0: |
||
| 2902 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2903 | description='API.INVALID_SELL_METER_ID') |
||
| 2904 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 2905 | |||
| 2906 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 2907 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 2908 | isinstance(new_values['data']['capacity'], int)): |
||
| 2909 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2910 | description='API.INVALID_CAPACITY') |
||
| 2911 | capacity = Decimal(new_values['data']['capacity']) |
||
| 2912 | |||
| 2913 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2914 | cursor = cnx.cursor() |
||
| 2915 | |||
| 2916 | cursor.execute(" SELECT name " |
||
| 2917 | " FROM tbl_energy_storage_containers " |
||
| 2918 | " WHERE id = %s ", |
||
| 2919 | (id_,)) |
||
| 2920 | if cursor.fetchone() is None: |
||
| 2921 | cursor.close() |
||
| 2922 | cnx.close() |
||
| 2923 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2924 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2925 | |||
| 2926 | cursor.execute(" SELECT name " |
||
| 2927 | " FROM tbl_energy_storage_containers_grids " |
||
| 2928 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 2929 | (id_, name,)) |
||
| 2930 | if cursor.fetchone() is not None: |
||
| 2931 | cursor.close() |
||
| 2932 | cnx.close() |
||
| 2933 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2934 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 2935 | |||
| 2936 | cursor.execute(" SELECT name " |
||
| 2937 | " FROM tbl_points " |
||
| 2938 | " WHERE id = %s ", |
||
| 2939 | (power_point_id,)) |
||
| 2940 | if cursor.fetchone() is None: |
||
| 2941 | cursor.close() |
||
| 2942 | cnx.close() |
||
| 2943 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2944 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2945 | |||
| 2946 | cursor.execute(" SELECT name " |
||
| 2947 | " FROM tbl_meters " |
||
| 2948 | " WHERE id = %s ", |
||
| 2949 | (buy_meter_id,)) |
||
| 2950 | if cursor.fetchone() is None: |
||
| 2951 | cursor.close() |
||
| 2952 | cnx.close() |
||
| 2953 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2954 | description='API.BUY_METER_NOT_FOUND') |
||
| 2955 | |||
| 2956 | cursor.execute(" SELECT name " |
||
| 2957 | " FROM tbl_meters " |
||
| 2958 | " WHERE id = %s ", |
||
| 2959 | (sell_meter_id,)) |
||
| 2960 | if cursor.fetchone() is None: |
||
| 2961 | cursor.close() |
||
| 2962 | cnx.close() |
||
| 2963 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2964 | description='API.SELL_METER_NOT_FOUND') |
||
| 2965 | |||
| 2966 | add_values = (" INSERT INTO tbl_energy_storage_containers_grids " |
||
| 2967 | " (name, uuid, energy_storage_container_id, power_point_id, " |
||
| 2968 | " buy_meter_id, sell_meter_id, capacity) " |
||
| 2969 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 2970 | cursor.execute(add_values, (name, |
||
| 2971 | str(uuid.uuid4()), |
||
| 2972 | id_, |
||
| 2973 | power_point_id, |
||
| 2974 | buy_meter_id, |
||
| 2975 | sell_meter_id, |
||
| 2976 | capacity |
||
| 2977 | )) |
||
| 2978 | new_id = cursor.lastrowid |
||
| 2979 | cnx.commit() |
||
| 2980 | cursor.close() |
||
| 2981 | cnx.close() |
||
| 2982 | |||
| 2983 | resp.status = falcon.HTTP_201 |
||
| 2984 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
||
| 2985 | |||
| 2986 | |||
| 2987 | View Code Duplication | class EnergyStorageContainerGridItem: |
|
| 2988 | def __init__(self): |
||
| 2989 | pass |
||
| 2990 | |||
| 2991 | @staticmethod |
||
| 2992 | def on_options(req, resp, id_, gid): |
||
| 2993 | _ = req |
||
| 2994 | resp.status = falcon.HTTP_200 |
||
| 2995 | _ = id_ |
||
| 2996 | |||
| 2997 | @staticmethod |
||
| 2998 | def on_get(req, resp, id_, gid): |
||
| 2999 | access_control(req) |
||
| 3000 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3001 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3002 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3003 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3004 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3005 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 3006 | |||
| 3007 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3008 | cursor = cnx.cursor() |
||
| 3009 | |||
| 3010 | cursor.execute(" SELECT name " |
||
| 3011 | " FROM tbl_energy_storage_containers " |
||
| 3012 | " WHERE id = %s ", (id_,)) |
||
| 3013 | if cursor.fetchone() is None: |
||
| 3014 | cursor.close() |
||
| 3015 | cnx.close() |
||
| 3016 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3017 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3018 | |||
| 3019 | query = (" SELECT id, name, uuid " |
||
| 3020 | " FROM tbl_energy_storage_containers ") |
||
| 3021 | cursor.execute(query) |
||
| 3022 | rows_energystoragecontainers = cursor.fetchall() |
||
| 3023 | |||
| 3024 | energy_storage_container_dict = dict() |
||
| 3025 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 3026 | for row in rows_energystoragecontainers: |
||
| 3027 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 3028 | "name": row[1], |
||
| 3029 | "uuid": row[2]} |
||
| 3030 | # query meter dict |
||
| 3031 | query = (" SELECT id, name, uuid " |
||
| 3032 | " FROM tbl_meters ") |
||
| 3033 | cursor.execute(query) |
||
| 3034 | rows_meters = cursor.fetchall() |
||
| 3035 | |||
| 3036 | meter_dict = dict() |
||
| 3037 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3038 | for row in rows_meters: |
||
| 3039 | meter_dict[row[0]] = {"id": row[0], |
||
| 3040 | "name": row[1], |
||
| 3041 | "uuid": row[2]} |
||
| 3042 | # query point dict |
||
| 3043 | query = (" SELECT id, name " |
||
| 3044 | " FROM tbl_points ") |
||
| 3045 | cursor.execute(query) |
||
| 3046 | rows_points = cursor.fetchall() |
||
| 3047 | |||
| 3048 | point_dict = dict() |
||
| 3049 | if rows_points is not None and len(rows_points) > 0: |
||
| 3050 | for row in rows_points: |
||
| 3051 | point_dict[row[0]] = {"id": row[0], |
||
| 3052 | "name": row[1]} |
||
| 3053 | |||
| 3054 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
||
| 3055 | " buy_meter_id, sell_meter_id, capacity " |
||
| 3056 | " FROM tbl_energy_storage_containers_grids " |
||
| 3057 | " WHERE id = %s ") |
||
| 3058 | cursor.execute(query, (gid,)) |
||
| 3059 | row = cursor.fetchone() |
||
| 3060 | cursor.close() |
||
| 3061 | cnx.close() |
||
| 3062 | |||
| 3063 | if row is None: |
||
| 3064 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3065 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3066 | else: |
||
| 3067 | meta_result = {"id": row[0], |
||
| 3068 | "name": row[1], |
||
| 3069 | "uuid": row[2], |
||
| 3070 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 3071 | "power_point": point_dict.get(row[4]), |
||
| 3072 | "buy_meter": meter_dict.get(row[5]), |
||
| 3073 | "sell_meter": meter_dict.get(row[6]), |
||
| 3074 | "capacity": row[7] |
||
| 3075 | } |
||
| 3076 | |||
| 3077 | resp.text = json.dumps(meta_result) |
||
| 3078 | |||
| 3079 | @staticmethod |
||
| 3080 | @user_logger |
||
| 3081 | def on_delete(req, resp, id_, gid): |
||
| 3082 | admin_control(req) |
||
| 3083 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3084 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3085 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3086 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3087 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3088 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 3089 | |||
| 3090 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3091 | cursor = cnx.cursor() |
||
| 3092 | |||
| 3093 | cursor.execute(" SELECT name " |
||
| 3094 | " FROM tbl_energy_storage_containers " |
||
| 3095 | " WHERE id = %s ", (id_,)) |
||
| 3096 | if cursor.fetchone() is None: |
||
| 3097 | cursor.close() |
||
| 3098 | cnx.close() |
||
| 3099 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3100 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3101 | |||
| 3102 | cursor.execute(" SELECT name " |
||
| 3103 | " FROM tbl_energy_storage_containers_grids " |
||
| 3104 | " WHERE id = %s ", (gid,)) |
||
| 3105 | if cursor.fetchone() is None: |
||
| 3106 | cursor.close() |
||
| 3107 | cnx.close() |
||
| 3108 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3109 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3110 | |||
| 3111 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
||
| 3112 | " WHERE id = %s ", (gid,)) |
||
| 3113 | cnx.commit() |
||
| 3114 | |||
| 3115 | cursor.close() |
||
| 3116 | cnx.close() |
||
| 3117 | |||
| 3118 | resp.status = falcon.HTTP_204 |
||
| 3119 | |||
| 3120 | @staticmethod |
||
| 3121 | @user_logger |
||
| 3122 | def on_put(req, resp, id_, gid): |
||
| 3123 | """Handles PUT requests""" |
||
| 3124 | admin_control(req) |
||
| 3125 | try: |
||
| 3126 | raw_json = req.stream.read().decode('utf-8') |
||
| 3127 | except UnicodeDecodeError as ex: |
||
| 3128 | print("Failed to decode request") |
||
| 3129 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3130 | title='API.BAD_REQUEST', |
||
| 3131 | description='API.INVALID_ENCODING') |
||
| 3132 | except Exception as ex: |
||
| 3133 | print("Unexpected error reading request stream") |
||
| 3134 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3135 | title='API.BAD_REQUEST', |
||
| 3136 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3137 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3138 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3139 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3140 | |||
| 3141 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3142 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3143 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 3144 | |||
| 3145 | new_values = json.loads(raw_json) |
||
| 3146 | |||
| 3147 | if 'name' not in new_values['data'].keys() or \ |
||
| 3148 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3149 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3151 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
||
| 3152 | name = str.strip(new_values['data']['name']) |
||
| 3153 | |||
| 3154 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3155 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3156 | new_values['data']['power_point_id'] <= 0: |
||
| 3157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3158 | description='API.INVALID_POWER_POINT_ID') |
||
| 3159 | power_point_id = new_values['data']['power_point_id'] |
||
| 3160 | |||
| 3161 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 3162 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 3163 | new_values['data']['buy_meter_id'] <= 0: |
||
| 3164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3165 | description='API.INVALID_BUY_METER_ID') |
||
| 3166 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 3167 | |||
| 3168 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 3169 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 3170 | new_values['data']['sell_meter_id'] <= 0: |
||
| 3171 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3172 | description='API.INVALID_METER_ID') |
||
| 3173 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 3174 | |||
| 3175 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 3176 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 3177 | isinstance(new_values['data']['capacity'], int)): |
||
| 3178 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3179 | description='API.INVALID_CAPACITY') |
||
| 3180 | capacity = Decimal(new_values['data']['capacity']) |
||
| 3181 | |||
| 3182 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3183 | cursor = cnx.cursor() |
||
| 3184 | |||
| 3185 | cursor.execute(" SELECT name " |
||
| 3186 | " FROM tbl_energy_storage_containers " |
||
| 3187 | " WHERE id = %s ", (id_,)) |
||
| 3188 | if cursor.fetchone() is None: |
||
| 3189 | cursor.close() |
||
| 3190 | cnx.close() |
||
| 3191 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3192 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3193 | |||
| 3194 | cursor.execute(" SELECT name " |
||
| 3195 | " FROM tbl_energy_storage_containers_grids " |
||
| 3196 | " WHERE id = %s ", (gid,)) |
||
| 3197 | if cursor.fetchone() is None: |
||
| 3198 | cursor.close() |
||
| 3199 | cnx.close() |
||
| 3200 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3201 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3202 | |||
| 3203 | cursor.execute(" SELECT name " |
||
| 3204 | " FROM tbl_energy_storage_containers_grids " |
||
| 3205 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 3206 | (id_, name, gid)) |
||
| 3207 | if cursor.fetchone() is not None: |
||
| 3208 | cursor.close() |
||
| 3209 | cnx.close() |
||
| 3210 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3211 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 3212 | |||
| 3213 | cursor.execute(" SELECT name " |
||
| 3214 | " FROM tbl_points " |
||
| 3215 | " WHERE id = %s ", |
||
| 3216 | (power_point_id,)) |
||
| 3217 | if cursor.fetchone() is None: |
||
| 3218 | cursor.close() |
||
| 3219 | cnx.close() |
||
| 3220 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3221 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3222 | |||
| 3223 | cursor.execute(" SELECT name " |
||
| 3224 | " FROM tbl_meters " |
||
| 3225 | " WHERE id = %s ", |
||
| 3226 | (buy_meter_id,)) |
||
| 3227 | if cursor.fetchone() is None: |
||
| 3228 | cursor.close() |
||
| 3229 | cnx.close() |
||
| 3230 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3231 | description='API.BUY_METER_NOT_FOUND') |
||
| 3232 | |||
| 3233 | cursor.execute(" SELECT name " |
||
| 3234 | " FROM tbl_meters " |
||
| 3235 | " WHERE id = %s ", |
||
| 3236 | (sell_meter_id,)) |
||
| 3237 | if cursor.fetchone() is None: |
||
| 3238 | cursor.close() |
||
| 3239 | cnx.close() |
||
| 3240 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3241 | description='API.SELL_METER_NOT_FOUND') |
||
| 3242 | |||
| 3243 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
||
| 3244 | " SET name = %s, energy_storage_container_id = %s, " |
||
| 3245 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
||
| 3246 | " WHERE id = %s ") |
||
| 3247 | cursor.execute(update_row, (name, |
||
| 3248 | id_, |
||
| 3249 | power_point_id, |
||
| 3250 | buy_meter_id, |
||
| 3251 | sell_meter_id, |
||
| 3252 | capacity, |
||
| 3253 | gid)) |
||
| 3254 | cnx.commit() |
||
| 3255 | |||
| 3256 | cursor.close() |
||
| 3257 | cnx.close() |
||
| 3258 | |||
| 3259 | resp.status = falcon.HTTP_200 |
||
| 3260 | |||
| 3261 | |||
| 3262 | View Code Duplication | class EnergyStorageContainerGridPointCollection: |
|
| 3263 | def __init__(self): |
||
| 3264 | pass |
||
| 3265 | |||
| 3266 | @staticmethod |
||
| 3267 | def on_options(req, resp, id_, gid): |
||
| 3268 | _ = req |
||
| 3269 | resp.status = falcon.HTTP_200 |
||
| 3270 | _ = id_ |
||
| 3271 | |||
| 3272 | @staticmethod |
||
| 3273 | def on_get(req, resp, id_, gid): |
||
| 3274 | if 'API-KEY' not in req.headers or \ |
||
| 3275 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3276 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3277 | access_control(req) |
||
| 3278 | else: |
||
| 3279 | api_key_control(req) |
||
| 3280 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3281 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3282 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3283 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3284 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3285 | description='API.INVALID_GRID_ID') |
||
| 3286 | |||
| 3287 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3288 | cursor = cnx.cursor() |
||
| 3289 | |||
| 3290 | cursor.execute(" SELECT name " |
||
| 3291 | " FROM tbl_energy_storage_containers_grids " |
||
| 3292 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid, )) |
||
| 3293 | if cursor.fetchone() is None: |
||
| 3294 | cursor.close() |
||
| 3295 | cnx.close() |
||
| 3296 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3297 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3298 | |||
| 3299 | query = (" SELECT p.id, p.name, " |
||
| 3300 | " ds.id, ds.name, ds.uuid, " |
||
| 3301 | " p.address " |
||
| 3302 | " FROM tbl_points p, tbl_energy_storage_containers_grids_points mp, tbl_data_sources ds " |
||
| 3303 | " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 3304 | " ORDER BY p.name ") |
||
| 3305 | cursor.execute(query, (gid,)) |
||
| 3306 | rows = cursor.fetchall() |
||
| 3307 | |||
| 3308 | result = list() |
||
| 3309 | if rows is not None and len(rows) > 0: |
||
| 3310 | for row in rows: |
||
| 3311 | meta_result = {"id": row[0], "name": row[1], |
||
| 3312 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 3313 | "address": row[5]} |
||
| 3314 | result.append(meta_result) |
||
| 3315 | |||
| 3316 | resp.text = json.dumps(result) |
||
| 3317 | |||
| 3318 | @staticmethod |
||
| 3319 | @user_logger |
||
| 3320 | def on_post(req, resp, id_, gid): |
||
| 3321 | """Handles POST requests""" |
||
| 3322 | admin_control(req) |
||
| 3323 | try: |
||
| 3324 | raw_json = req.stream.read().decode('utf-8') |
||
| 3325 | except UnicodeDecodeError as ex: |
||
| 3326 | print("Failed to decode request") |
||
| 3327 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3328 | title='API.BAD_REQUEST', |
||
| 3329 | description='API.INVALID_ENCODING') |
||
| 3330 | except Exception as ex: |
||
| 3331 | print("Unexpected error reading request stream") |
||
| 3332 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3333 | title='API.BAD_REQUEST', |
||
| 3334 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3335 | |||
| 3336 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3337 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3338 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3339 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3340 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3341 | description='API.INVALID_GRID_ID') |
||
| 3342 | |||
| 3343 | new_values = json.loads(raw_json) |
||
| 3344 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3345 | cursor = cnx.cursor() |
||
| 3346 | |||
| 3347 | cursor.execute(" SELECT name " |
||
| 3348 | " FROM tbl_energy_storage_containers_grids " |
||
| 3349 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid,)) |
||
| 3350 | if cursor.fetchone() is None: |
||
| 3351 | cursor.close() |
||
| 3352 | cnx.close() |
||
| 3353 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3354 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3355 | |||
| 3356 | cursor.execute(" SELECT name, object_type " |
||
| 3357 | " FROM tbl_points " |
||
| 3358 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 3359 | row = cursor.fetchone() |
||
| 3360 | if row is None: |
||
| 3361 | cursor.close() |
||
| 3362 | cnx.close() |
||
| 3363 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3364 | description='API.POINT_NOT_FOUND') |
||
| 3365 | |||
| 3366 | query = (" SELECT id " |
||
| 3367 | " FROM tbl_energy_storage_containers_grids_points " |
||
| 3368 | " WHERE grid_id = %s AND point_id = %s") |
||
| 3369 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
||
| 3370 | if cursor.fetchone() is not None: |
||
| 3371 | cursor.close() |
||
| 3372 | cnx.close() |
||
| 3373 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3374 | description='API.GRID_POINT_RELATION_EXISTS') |
||
| 3375 | |||
| 3376 | add_row = (" INSERT INTO tbl_energy_storage_containers_grids_points (grid_id, point_id) " |
||
| 3377 | " VALUES (%s, %s) ") |
||
| 3378 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
||
| 3379 | cnx.commit() |
||
| 3380 | cursor.close() |
||
| 3381 | cnx.close() |
||
| 3382 | |||
| 3383 | resp.status = falcon.HTTP_201 |
||
| 3384 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(gid) + '/points/' + \ |
||
| 3385 | str(new_values['data']['point_id']) |
||
| 3386 | |||
| 3387 | |||
| 3388 | View Code Duplication | class EnergyStorageContainerGridPointItem: |
|
| 3389 | def __init__(self): |
||
| 3390 | pass |
||
| 3391 | |||
| 3392 | @staticmethod |
||
| 3393 | def on_options(req, resp, id_, gid, pid): |
||
| 3394 | _ = req |
||
| 3395 | resp.status = falcon.HTTP_200 |
||
| 3396 | _ = id_ |
||
| 3397 | |||
| 3398 | @staticmethod |
||
| 3399 | @user_logger |
||
| 3400 | def on_delete(req, resp, id_, gid, pid): |
||
| 3401 | """Handles DELETE requests""" |
||
| 3402 | admin_control(req) |
||
| 3403 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3404 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3405 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3406 | if not gid.isdigit() or int(gid) <= 0: |
||
| 3407 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3408 | description='API.INVALID_GRID_ID') |
||
| 3409 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3410 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3411 | description='API.INVALID_POINT_ID') |
||
| 3412 | |||
| 3413 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3414 | cursor = cnx.cursor() |
||
| 3415 | |||
| 3416 | cursor.execute(" SELECT name " |
||
| 3417 | " FROM tbl_energy_storage_containers_grids " |
||
| 3418 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid,)) |
||
| 3419 | if cursor.fetchone() is None: |
||
| 3420 | cursor.close() |
||
| 3421 | cnx.close() |
||
| 3422 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3423 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 3424 | |||
| 3425 | cursor.execute(" SELECT name " |
||
| 3426 | " FROM tbl_points " |
||
| 3427 | " WHERE id = %s ", (pid,)) |
||
| 3428 | if cursor.fetchone() is None: |
||
| 3429 | cursor.close() |
||
| 3430 | cnx.close() |
||
| 3431 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3432 | description='API.POINT_NOT_FOUND') |
||
| 3433 | |||
| 3434 | cursor.execute(" SELECT id " |
||
| 3435 | " FROM tbl_energy_storage_containers_grids_points " |
||
| 3436 | " WHERE grid_id = %s AND point_id = %s ", (gid, pid)) |
||
| 3437 | if cursor.fetchone() is None: |
||
| 3438 | cursor.close() |
||
| 3439 | cnx.close() |
||
| 3440 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3441 | description='API.GRID_POINT_RELATION_NOT_FOUND') |
||
| 3442 | |||
| 3443 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids_points " |
||
| 3444 | " WHERE grid_id = %s AND point_id = %s ", (gid, pid)) |
||
| 3445 | cnx.commit() |
||
| 3446 | |||
| 3447 | cursor.close() |
||
| 3448 | cnx.close() |
||
| 3449 | |||
| 3450 | resp.status = falcon.HTTP_204 |
||
| 3451 | |||
| 3452 | |||
| 3453 | View Code Duplication | class EnergyStorageContainerHVACCollection: |
|
| 3454 | def __init__(self): |
||
| 3455 | pass |
||
| 3456 | |||
| 3457 | @staticmethod |
||
| 3458 | def on_options(req, resp, id_): |
||
| 3459 | _ = req |
||
| 3460 | resp.status = falcon.HTTP_200 |
||
| 3461 | _ = id_ |
||
| 3462 | |||
| 3463 | @staticmethod |
||
| 3464 | def on_get(req, resp, id_): |
||
| 3465 | access_control(req) |
||
| 3466 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3467 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3468 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3469 | |||
| 3470 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3471 | cursor = cnx.cursor() |
||
| 3472 | |||
| 3473 | cursor.execute(" SELECT name " |
||
| 3474 | " FROM tbl_energy_storage_containers " |
||
| 3475 | " WHERE id = %s ", (id_,)) |
||
| 3476 | if cursor.fetchone() is None: |
||
| 3477 | cursor.close() |
||
| 3478 | cnx.close() |
||
| 3479 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3480 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3481 | |||
| 3482 | query = (" SELECT id, name, uuid " |
||
| 3483 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3484 | " WHERE energy_storage_container_id = %s " |
||
| 3485 | " ORDER BY name ") |
||
| 3486 | cursor.execute(query, (id_,)) |
||
| 3487 | rows = cursor.fetchall() |
||
| 3488 | |||
| 3489 | result = list() |
||
| 3490 | if rows is not None and len(rows) > 0: |
||
| 3491 | for row in rows: |
||
| 3492 | meta_result = {"id": row[0], |
||
| 3493 | "name": row[1], |
||
| 3494 | "uuid": row[2] |
||
| 3495 | } |
||
| 3496 | result.append(meta_result) |
||
| 3497 | |||
| 3498 | resp.text = json.dumps(result) |
||
| 3499 | |||
| 3500 | @staticmethod |
||
| 3501 | @user_logger |
||
| 3502 | def on_post(req, resp, id_): |
||
| 3503 | """Handles POST requests""" |
||
| 3504 | admin_control(req) |
||
| 3505 | try: |
||
| 3506 | raw_json = req.stream.read().decode('utf-8') |
||
| 3507 | except UnicodeDecodeError as ex: |
||
| 3508 | print("Failed to decode request") |
||
| 3509 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3510 | title='API.BAD_REQUEST', |
||
| 3511 | description='API.INVALID_ENCODING') |
||
| 3512 | except Exception as ex: |
||
| 3513 | print("Unexpected error reading request stream") |
||
| 3514 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3515 | title='API.BAD_REQUEST', |
||
| 3516 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3517 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3518 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3519 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3520 | |||
| 3521 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3522 | cursor = cnx.cursor() |
||
| 3523 | |||
| 3524 | cursor.execute(" SELECT name " |
||
| 3525 | " FROM tbl_energy_storage_containers " |
||
| 3526 | " WHERE id = %s ", (id_,)) |
||
| 3527 | if cursor.fetchone() is None: |
||
| 3528 | cursor.close() |
||
| 3529 | cnx.close() |
||
| 3530 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3531 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3532 | |||
| 3533 | new_values = json.loads(raw_json) |
||
| 3534 | |||
| 3535 | if 'name' not in new_values['data'].keys() or \ |
||
| 3536 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3537 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3538 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3539 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME') |
||
| 3540 | name = str.strip(new_values['data']['name']) |
||
| 3541 | |||
| 3542 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3543 | cursor = cnx.cursor() |
||
| 3544 | |||
| 3545 | cursor.execute(" SELECT name " |
||
| 3546 | " FROM tbl_energy_storage_containers " |
||
| 3547 | " WHERE id = %s ", |
||
| 3548 | (id_,)) |
||
| 3549 | if cursor.fetchone() is None: |
||
| 3550 | cursor.close() |
||
| 3551 | cnx.close() |
||
| 3552 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3553 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3554 | |||
| 3555 | cursor.execute(" SELECT name " |
||
| 3556 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3557 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 3558 | (id_, name,)) |
||
| 3559 | if cursor.fetchone() is not None: |
||
| 3560 | cursor.close() |
||
| 3561 | cnx.close() |
||
| 3562 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3563 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE') |
||
| 3564 | |||
| 3565 | add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs " |
||
| 3566 | " (name, uuid, energy_storage_container_id) " |
||
| 3567 | " VALUES (%s, %s, %s) ") |
||
| 3568 | cursor.execute(add_values, (name, |
||
| 3569 | str(uuid.uuid4()), |
||
| 3570 | id_ |
||
| 3571 | )) |
||
| 3572 | new_id = cursor.lastrowid |
||
| 3573 | cnx.commit() |
||
| 3574 | cursor.close() |
||
| 3575 | cnx.close() |
||
| 3576 | |||
| 3577 | resp.status = falcon.HTTP_201 |
||
| 3578 | resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id) |
||
| 3579 | |||
| 3580 | |||
| 3581 | View Code Duplication | class EnergyStorageContainerHVACItem: |
|
| 3582 | def __init__(self): |
||
| 3583 | pass |
||
| 3584 | |||
| 3585 | @staticmethod |
||
| 3586 | def on_options(req, resp, id_, hid): |
||
| 3587 | _ = req |
||
| 3588 | resp.status = falcon.HTTP_200 |
||
| 3589 | _ = id_ |
||
| 3590 | |||
| 3591 | @staticmethod |
||
| 3592 | def on_get(req, resp, id_, hid): |
||
| 3593 | access_control(req) |
||
| 3594 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3596 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3597 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3598 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3599 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 3600 | |||
| 3601 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3602 | cursor = cnx.cursor() |
||
| 3603 | |||
| 3604 | cursor.execute(" SELECT name " |
||
| 3605 | " FROM tbl_energy_storage_containers " |
||
| 3606 | " WHERE id = %s ", (id_,)) |
||
| 3607 | if cursor.fetchone() is None: |
||
| 3608 | cursor.close() |
||
| 3609 | cnx.close() |
||
| 3610 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3611 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3612 | |||
| 3613 | query = (" SELECT id, name, uuid " |
||
| 3614 | " FROM tbl_energy_storage_containers ") |
||
| 3615 | cursor.execute(query) |
||
| 3616 | rows_energystoragecontainers = cursor.fetchall() |
||
| 3617 | |||
| 3618 | energy_storage_container_dict = dict() |
||
| 3619 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 3620 | for row in rows_energystoragecontainers: |
||
| 3621 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 3622 | "name": row[1], |
||
| 3623 | "uuid": row[2]} |
||
| 3624 | |||
| 3625 | query = (" SELECT id, name, uuid " |
||
| 3626 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3627 | " WHERE id = %s ") |
||
| 3628 | cursor.execute(query, (hid,)) |
||
| 3629 | row = cursor.fetchone() |
||
| 3630 | cursor.close() |
||
| 3631 | cnx.close() |
||
| 3632 | |||
| 3633 | if row is None: |
||
| 3634 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3635 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3636 | else: |
||
| 3637 | meta_result = {"id": row[0], |
||
| 3638 | "name": row[1], |
||
| 3639 | "uuid": row[2] |
||
| 3640 | } |
||
| 3641 | |||
| 3642 | resp.text = json.dumps(meta_result) |
||
| 3643 | |||
| 3644 | @staticmethod |
||
| 3645 | @user_logger |
||
| 3646 | def on_delete(req, resp, id_, hid): |
||
| 3647 | admin_control(req) |
||
| 3648 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3650 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3651 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3652 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3653 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 3654 | |||
| 3655 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3656 | cursor = cnx.cursor() |
||
| 3657 | |||
| 3658 | cursor.execute(" SELECT name " |
||
| 3659 | " FROM tbl_energy_storage_containers " |
||
| 3660 | " WHERE id = %s ", (id_,)) |
||
| 3661 | if cursor.fetchone() is None: |
||
| 3662 | cursor.close() |
||
| 3663 | cnx.close() |
||
| 3664 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3665 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3666 | |||
| 3667 | cursor.execute(" SELECT name " |
||
| 3668 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3669 | " WHERE id = %s ", (hid,)) |
||
| 3670 | if cursor.fetchone() is None: |
||
| 3671 | cursor.close() |
||
| 3672 | cnx.close() |
||
| 3673 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3674 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3675 | |||
| 3676 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs " |
||
| 3677 | " WHERE id = %s ", (hid,)) |
||
| 3678 | cnx.commit() |
||
| 3679 | |||
| 3680 | cursor.close() |
||
| 3681 | cnx.close() |
||
| 3682 | |||
| 3683 | resp.status = falcon.HTTP_204 |
||
| 3684 | |||
| 3685 | @staticmethod |
||
| 3686 | @user_logger |
||
| 3687 | def on_put(req, resp, id_, hid): |
||
| 3688 | """Handles PUT requests""" |
||
| 3689 | admin_control(req) |
||
| 3690 | try: |
||
| 3691 | raw_json = req.stream.read().decode('utf-8') |
||
| 3692 | except UnicodeDecodeError as ex: |
||
| 3693 | print("Failed to decode request") |
||
| 3694 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3695 | title='API.BAD_REQUEST', |
||
| 3696 | description='API.INVALID_ENCODING') |
||
| 3697 | except Exception as ex: |
||
| 3698 | print("Unexpected error reading request stream") |
||
| 3699 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3700 | title='API.BAD_REQUEST', |
||
| 3701 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3702 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3703 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3704 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3705 | |||
| 3706 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3707 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3708 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 3709 | |||
| 3710 | new_values = json.loads(raw_json) |
||
| 3711 | |||
| 3712 | if 'name' not in new_values['data'].keys() or \ |
||
| 3713 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3714 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3715 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3716 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME') |
||
| 3717 | name = str.strip(new_values['data']['name']) |
||
| 3718 | |||
| 3719 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3720 | cursor = cnx.cursor() |
||
| 3721 | |||
| 3722 | cursor.execute(" SELECT name " |
||
| 3723 | " FROM tbl_energy_storage_containers " |
||
| 3724 | " WHERE id = %s ", (id_,)) |
||
| 3725 | if cursor.fetchone() is None: |
||
| 3726 | cursor.close() |
||
| 3727 | cnx.close() |
||
| 3728 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3729 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3730 | |||
| 3731 | cursor.execute(" SELECT name " |
||
| 3732 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3733 | " WHERE id = %s ", (hid,)) |
||
| 3734 | if cursor.fetchone() is None: |
||
| 3735 | cursor.close() |
||
| 3736 | cnx.close() |
||
| 3737 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3738 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3739 | |||
| 3740 | cursor.execute(" SELECT name " |
||
| 3741 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3742 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 3743 | (id_, name, hid)) |
||
| 3744 | if cursor.fetchone() is not None: |
||
| 3745 | cursor.close() |
||
| 3746 | cnx.close() |
||
| 3747 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3748 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE') |
||
| 3749 | |||
| 3750 | update_row = (" UPDATE tbl_energy_storage_containers_hvacs " |
||
| 3751 | " SET name = %s " |
||
| 3752 | " WHERE id = %s ") |
||
| 3753 | cursor.execute(update_row, (name, |
||
| 3754 | hid)) |
||
| 3755 | cnx.commit() |
||
| 3756 | |||
| 3757 | cursor.close() |
||
| 3758 | cnx.close() |
||
| 3759 | |||
| 3760 | resp.status = falcon.HTTP_200 |
||
| 3761 | |||
| 3762 | |||
| 3763 | View Code Duplication | class EnergyStorageContainerHVACPointCollection: |
|
| 3764 | def __init__(self): |
||
| 3765 | pass |
||
| 3766 | |||
| 3767 | @staticmethod |
||
| 3768 | def on_options(req, resp, id_, hid): |
||
| 3769 | _ = req |
||
| 3770 | resp.status = falcon.HTTP_200 |
||
| 3771 | _ = id_ |
||
| 3772 | |||
| 3773 | @staticmethod |
||
| 3774 | def on_get(req, resp, id_, hid): |
||
| 3775 | if 'API-KEY' not in req.headers or \ |
||
| 3776 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3777 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3778 | access_control(req) |
||
| 3779 | else: |
||
| 3780 | api_key_control(req) |
||
| 3781 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3783 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3784 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3785 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3786 | description='API.INVALID_HVAC_ID') |
||
| 3787 | |||
| 3788 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3789 | cursor = cnx.cursor() |
||
| 3790 | |||
| 3791 | cursor.execute(" SELECT name " |
||
| 3792 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3793 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid, )) |
||
| 3794 | if cursor.fetchone() is None: |
||
| 3795 | cursor.close() |
||
| 3796 | cnx.close() |
||
| 3797 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3798 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3799 | |||
| 3800 | query = (" SELECT p.id, p.name, " |
||
| 3801 | " ds.id, ds.name, ds.uuid, " |
||
| 3802 | " p.address " |
||
| 3803 | " FROM tbl_points p, tbl_energy_storage_containers_hvacs_points mp, tbl_data_sources ds " |
||
| 3804 | " WHERE mp.hvac_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 3805 | " ORDER BY p.name ") |
||
| 3806 | cursor.execute(query, (hid,)) |
||
| 3807 | rows = cursor.fetchall() |
||
| 3808 | |||
| 3809 | result = list() |
||
| 3810 | if rows is not None and len(rows) > 0: |
||
| 3811 | for row in rows: |
||
| 3812 | meta_result = {"id": row[0], "name": row[1], |
||
| 3813 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 3814 | "address": row[5]} |
||
| 3815 | result.append(meta_result) |
||
| 3816 | |||
| 3817 | resp.text = json.dumps(result) |
||
| 3818 | |||
| 3819 | @staticmethod |
||
| 3820 | @user_logger |
||
| 3821 | def on_post(req, resp, id_, hid): |
||
| 3822 | """Handles POST requests""" |
||
| 3823 | admin_control(req) |
||
| 3824 | try: |
||
| 3825 | raw_json = req.stream.read().decode('utf-8') |
||
| 3826 | except UnicodeDecodeError as ex: |
||
| 3827 | print("Failed to decode request") |
||
| 3828 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3829 | title='API.BAD_REQUEST', |
||
| 3830 | description='API.INVALID_ENCODING') |
||
| 3831 | except Exception as ex: |
||
| 3832 | print("Unexpected error reading request stream") |
||
| 3833 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3834 | title='API.BAD_REQUEST', |
||
| 3835 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3836 | |||
| 3837 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3838 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3839 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3840 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3841 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3842 | description='API.INVALID_HVAC_ID') |
||
| 3843 | |||
| 3844 | new_values = json.loads(raw_json) |
||
| 3845 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3846 | cursor = cnx.cursor() |
||
| 3847 | |||
| 3848 | cursor.execute(" SELECT name " |
||
| 3849 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3850 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid,)) |
||
| 3851 | if cursor.fetchone() is None: |
||
| 3852 | cursor.close() |
||
| 3853 | cnx.close() |
||
| 3854 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3855 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3856 | |||
| 3857 | cursor.execute(" SELECT name, object_type " |
||
| 3858 | " FROM tbl_points " |
||
| 3859 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 3860 | row = cursor.fetchone() |
||
| 3861 | if row is None: |
||
| 3862 | cursor.close() |
||
| 3863 | cnx.close() |
||
| 3864 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3865 | description='API.POINT_NOT_FOUND') |
||
| 3866 | |||
| 3867 | query = (" SELECT id " |
||
| 3868 | " FROM tbl_energy_storage_containers_hvacs_points " |
||
| 3869 | " WHERE hvac_id = %s AND point_id = %s") |
||
| 3870 | cursor.execute(query, (hid, new_values['data']['point_id'],)) |
||
| 3871 | if cursor.fetchone() is not None: |
||
| 3872 | cursor.close() |
||
| 3873 | cnx.close() |
||
| 3874 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3875 | description='API.HVAC_POINT_RELATION_EXISTS') |
||
| 3876 | |||
| 3877 | add_row = (" INSERT INTO tbl_energy_storage_containers_hvacs_points (hvac_id, point_id) " |
||
| 3878 | " VALUES (%s, %s) ") |
||
| 3879 | cursor.execute(add_row, (hid, new_values['data']['point_id'],)) |
||
| 3880 | cnx.commit() |
||
| 3881 | cursor.close() |
||
| 3882 | cnx.close() |
||
| 3883 | |||
| 3884 | resp.status = falcon.HTTP_201 |
||
| 3885 | resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(hid) + '/points/' + \ |
||
| 3886 | str(new_values['data']['point_id']) |
||
| 3887 | |||
| 3888 | |||
| 3889 | View Code Duplication | class EnergyStorageContainerHVACPointItem: |
|
| 3890 | def __init__(self): |
||
| 3891 | pass |
||
| 3892 | |||
| 3893 | @staticmethod |
||
| 3894 | def on_options(req, resp, id_, hid, pid): |
||
| 3895 | _ = req |
||
| 3896 | resp.status = falcon.HTTP_200 |
||
| 3897 | _ = id_ |
||
| 3898 | |||
| 3899 | @staticmethod |
||
| 3900 | @user_logger |
||
| 3901 | def on_delete(req, resp, id_, hid, pid): |
||
| 3902 | """Handles DELETE requests""" |
||
| 3903 | admin_control(req) |
||
| 3904 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3905 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3906 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3907 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3908 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3909 | description='API.INVALID_HVAC_ID') |
||
| 3910 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3911 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3912 | description='API.INVALID_POINT_ID') |
||
| 3913 | |||
| 3914 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3915 | cursor = cnx.cursor() |
||
| 3916 | |||
| 3917 | cursor.execute(" SELECT name " |
||
| 3918 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3919 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid,)) |
||
| 3920 | if cursor.fetchone() is None: |
||
| 3921 | cursor.close() |
||
| 3922 | cnx.close() |
||
| 3923 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3924 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3925 | |||
| 3926 | cursor.execute(" SELECT name " |
||
| 3927 | " FROM tbl_points " |
||
| 3928 | " WHERE id = %s ", (pid,)) |
||
| 3929 | if cursor.fetchone() is None: |
||
| 3930 | cursor.close() |
||
| 3931 | cnx.close() |
||
| 3932 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3933 | description='API.POINT_NOT_FOUND') |
||
| 3934 | |||
| 3935 | cursor.execute(" SELECT id " |
||
| 3936 | " FROM tbl_energy_storage_containers_hvacs_points " |
||
| 3937 | " WHERE hvac_id = %s AND point_id = %s ", (hid, pid)) |
||
| 3938 | if cursor.fetchone() is None: |
||
| 3939 | cursor.close() |
||
| 3940 | cnx.close() |
||
| 3941 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3942 | description='API.HVAC_POINT_RELATION_NOT_FOUND') |
||
| 3943 | |||
| 3944 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs_points " |
||
| 3945 | " WHERE hvac_id = %s AND point_id = %s ", (hid, pid)) |
||
| 3946 | cnx.commit() |
||
| 3947 | |||
| 3948 | cursor.close() |
||
| 3949 | cnx.close() |
||
| 3950 | |||
| 3951 | resp.status = falcon.HTTP_204 |
||
| 3952 | |||
| 3953 | |||
| 3954 | View Code Duplication | class EnergyStorageContainerLoadCollection: |
|
| 3955 | def __init__(self): |
||
| 3956 | pass |
||
| 3957 | |||
| 3958 | @staticmethod |
||
| 3959 | def on_options(req, resp, id_): |
||
| 3960 | _ = req |
||
| 3961 | resp.status = falcon.HTTP_200 |
||
| 3962 | _ = id_ |
||
| 3963 | |||
| 3964 | @staticmethod |
||
| 3965 | def on_get(req, resp, id_): |
||
| 3966 | access_control(req) |
||
| 3967 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3968 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3969 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3970 | |||
| 3971 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3972 | cursor = cnx.cursor() |
||
| 3973 | |||
| 3974 | cursor.execute(" SELECT name " |
||
| 3975 | " FROM tbl_energy_storage_containers " |
||
| 3976 | " WHERE id = %s ", (id_,)) |
||
| 3977 | if cursor.fetchone() is None: |
||
| 3978 | cursor.close() |
||
| 3979 | cnx.close() |
||
| 3980 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3981 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3982 | |||
| 3983 | # query meter dict |
||
| 3984 | query = (" SELECT id, name, uuid " |
||
| 3985 | " FROM tbl_meters ") |
||
| 3986 | cursor.execute(query) |
||
| 3987 | rows_meters = cursor.fetchall() |
||
| 3988 | |||
| 3989 | meter_dict = dict() |
||
| 3990 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3991 | for row in rows_meters: |
||
| 3992 | meter_dict[row[0]] = {"id": row[0], |
||
| 3993 | "name": row[1], |
||
| 3994 | "uuid": row[2]} |
||
| 3995 | # query point dict |
||
| 3996 | query = (" SELECT id, name " |
||
| 3997 | " FROM tbl_points ") |
||
| 3998 | cursor.execute(query) |
||
| 3999 | rows_points = cursor.fetchall() |
||
| 4000 | |||
| 4001 | point_dict = dict() |
||
| 4002 | if rows_points is not None and len(rows_points) > 0: |
||
| 4003 | for row in rows_points: |
||
| 4004 | point_dict[row[0]] = {"id": row[0], |
||
| 4005 | "name": row[1]} |
||
| 4006 | |||
| 4007 | query = (" SELECT id, name, uuid, " |
||
| 4008 | " power_point_id, meter_id, rated_input_power " |
||
| 4009 | " FROM tbl_energy_storage_containers_loads " |
||
| 4010 | " WHERE energy_storage_container_id = %s " |
||
| 4011 | " ORDER BY name ") |
||
| 4012 | cursor.execute(query, (id_,)) |
||
| 4013 | rows = cursor.fetchall() |
||
| 4014 | |||
| 4015 | result = list() |
||
| 4016 | if rows is not None and len(rows) > 0: |
||
| 4017 | for row in rows: |
||
| 4018 | meta_result = {"id": row[0], |
||
| 4019 | "name": row[1], |
||
| 4020 | "uuid": row[2], |
||
| 4021 | "power_point": point_dict.get(row[3]), |
||
| 4022 | "meter": meter_dict.get(row[4]), |
||
| 4023 | "rated_input_power": row[5] |
||
| 4024 | } |
||
| 4025 | result.append(meta_result) |
||
| 4026 | |||
| 4027 | resp.text = json.dumps(result) |
||
| 4028 | |||
| 4029 | @staticmethod |
||
| 4030 | @user_logger |
||
| 4031 | def on_post(req, resp, id_): |
||
| 4032 | """Handles POST requests""" |
||
| 4033 | admin_control(req) |
||
| 4034 | try: |
||
| 4035 | raw_json = req.stream.read().decode('utf-8') |
||
| 4036 | except UnicodeDecodeError as ex: |
||
| 4037 | print("Failed to decode request") |
||
| 4038 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4039 | title='API.BAD_REQUEST', |
||
| 4040 | description='API.INVALID_ENCODING') |
||
| 4041 | except Exception as ex: |
||
| 4042 | print("Unexpected error reading request stream") |
||
| 4043 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4044 | title='API.BAD_REQUEST', |
||
| 4045 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4046 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4047 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4048 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4049 | |||
| 4050 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4051 | cursor = cnx.cursor() |
||
| 4052 | |||
| 4053 | cursor.execute(" SELECT name " |
||
| 4054 | " FROM tbl_energy_storage_containers " |
||
| 4055 | " WHERE id = %s ", (id_,)) |
||
| 4056 | if cursor.fetchone() is None: |
||
| 4057 | cursor.close() |
||
| 4058 | cnx.close() |
||
| 4059 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4060 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4061 | |||
| 4062 | new_values = json.loads(raw_json) |
||
| 4063 | |||
| 4064 | if 'name' not in new_values['data'].keys() or \ |
||
| 4065 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4066 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4067 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4068 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_NAME') |
||
| 4069 | name = str.strip(new_values['data']['name']) |
||
| 4070 | |||
| 4071 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 4072 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 4073 | new_values['data']['power_point_id'] <= 0: |
||
| 4074 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4075 | description='API.INVALID_POWER_POINT_ID') |
||
| 4076 | power_point_id = new_values['data']['power_point_id'] |
||
| 4077 | |||
| 4078 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 4079 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 4080 | new_values['data']['meter_id'] <= 0: |
||
| 4081 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4082 | description='API.INVALID_METER_ID') |
||
| 4083 | meter_id = new_values['data']['meter_id'] |
||
| 4084 | |||
| 4085 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 4086 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 4087 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 4088 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4089 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 4090 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
||
| 4091 | |||
| 4092 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4093 | cursor = cnx.cursor() |
||
| 4094 | |||
| 4095 | cursor.execute(" SELECT name " |
||
| 4096 | " FROM tbl_energy_storage_containers " |
||
| 4097 | " WHERE id = %s ", |
||
| 4098 | (id_,)) |
||
| 4099 | if cursor.fetchone() is None: |
||
| 4100 | cursor.close() |
||
| 4101 | cnx.close() |
||
| 4102 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4103 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4104 | |||
| 4105 | cursor.execute(" SELECT name " |
||
| 4106 | " FROM tbl_energy_storage_containers_loads " |
||
| 4107 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 4108 | (id_, name,)) |
||
| 4109 | if cursor.fetchone() is not None: |
||
| 4110 | cursor.close() |
||
| 4111 | cnx.close() |
||
| 4112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4113 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 4114 | |||
| 4115 | cursor.execute(" SELECT name " |
||
| 4116 | " FROM tbl_points " |
||
| 4117 | " WHERE id = %s ", |
||
| 4118 | (power_point_id,)) |
||
| 4119 | if cursor.fetchone() is None: |
||
| 4120 | cursor.close() |
||
| 4121 | cnx.close() |
||
| 4122 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4123 | description='API.POWER_POINT_NOT_FOUND') |
||
| 4124 | |||
| 4125 | cursor.execute(" SELECT name " |
||
| 4126 | " FROM tbl_meters " |
||
| 4127 | " WHERE id = %s ", |
||
| 4128 | (meter_id,)) |
||
| 4129 | if cursor.fetchone() is None: |
||
| 4130 | cursor.close() |
||
| 4131 | cnx.close() |
||
| 4132 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4133 | description='API.METER_NOT_FOUND') |
||
| 4134 | |||
| 4135 | add_values = (" INSERT INTO tbl_energy_storage_containers_loads " |
||
| 4136 | " (name, uuid, energy_storage_container_id, power_point_id, meter_id, rated_input_power) " |
||
| 4137 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 4138 | cursor.execute(add_values, (name, |
||
| 4139 | str(uuid.uuid4()), |
||
| 4140 | id_, |
||
| 4141 | power_point_id, |
||
| 4142 | meter_id, |
||
| 4143 | rated_input_power |
||
| 4144 | )) |
||
| 4145 | new_id = cursor.lastrowid |
||
| 4146 | cnx.commit() |
||
| 4147 | cursor.close() |
||
| 4148 | cnx.close() |
||
| 4149 | |||
| 4150 | resp.status = falcon.HTTP_201 |
||
| 4151 | resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(new_id) |
||
| 4152 | |||
| 4153 | |||
| 4154 | class EnergyStorageContainerLoadItem: |
||
| 4155 | def __init__(self): |
||
| 4156 | pass |
||
| 4157 | |||
| 4158 | @staticmethod |
||
| 4159 | def on_options(req, resp, id_, lid): |
||
| 4160 | _ = req |
||
| 4161 | resp.status = falcon.HTTP_200 |
||
| 4162 | _ = id_ |
||
| 4163 | |||
| 4164 | @staticmethod |
||
| 4165 | def on_get(req, resp, id_, lid): |
||
| 4166 | access_control(req) |
||
| 4167 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4168 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4169 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4170 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4171 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4172 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 4173 | |||
| 4174 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4175 | cursor = cnx.cursor() |
||
| 4176 | |||
| 4177 | cursor.execute(" SELECT name " |
||
| 4178 | " FROM tbl_energy_storage_containers " |
||
| 4179 | " WHERE id = %s ", (id_,)) |
||
| 4180 | if cursor.fetchone() is None: |
||
| 4181 | cursor.close() |
||
| 4182 | cnx.close() |
||
| 4183 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4184 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4185 | |||
| 4186 | query = (" SELECT id, name, uuid " |
||
| 4187 | " FROM tbl_energy_storage_containers ") |
||
| 4188 | cursor.execute(query) |
||
| 4189 | rows_energystoragecontainers = cursor.fetchall() |
||
| 4190 | |||
| 4191 | energy_storage_container_dict = dict() |
||
| 4192 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 4193 | for row in rows_energystoragecontainers: |
||
| 4194 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 4195 | "name": row[1], |
||
| 4196 | "uuid": row[2]} |
||
| 4197 | # query meter dict |
||
| 4198 | query = (" SELECT id, name, uuid " |
||
| 4199 | " FROM tbl_meters ") |
||
| 4200 | cursor.execute(query) |
||
| 4201 | rows_meters = cursor.fetchall() |
||
| 4202 | |||
| 4203 | meter_dict = dict() |
||
| 4204 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 4205 | for row in rows_meters: |
||
| 4206 | meter_dict[row[0]] = {"id": row[0], |
||
| 4207 | "name": row[1], |
||
| 4208 | "uuid": row[2]} |
||
| 4209 | # query point dict |
||
| 4210 | query = (" SELECT id, name " |
||
| 4211 | " FROM tbl_points ") |
||
| 4212 | cursor.execute(query) |
||
| 4213 | rows_points = cursor.fetchall() |
||
| 4214 | |||
| 4215 | point_dict = dict() |
||
| 4216 | if rows_points is not None and len(rows_points) > 0: |
||
| 4217 | for row in rows_points: |
||
| 4218 | point_dict[row[0]] = {"id": row[0], |
||
| 4219 | "name": row[1]} |
||
| 4220 | |||
| 4221 | query = (" SELECT id, name, uuid, " |
||
| 4222 | " energy_storage_container_id, power_point_id, meter_id, rated_input_power " |
||
| 4223 | " FROM tbl_energy_storage_containers_loads " |
||
| 4224 | " WHERE id = %s ") |
||
| 4225 | cursor.execute(query, (lid,)) |
||
| 4226 | row = cursor.fetchone() |
||
| 4227 | cursor.close() |
||
| 4228 | cnx.close() |
||
| 4229 | |||
| 4230 | if row is None: |
||
| 4231 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4232 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4233 | else: |
||
| 4234 | meta_result = {"id": row[0], |
||
| 4235 | "name": row[1], |
||
| 4236 | "uuid": row[2], |
||
| 4237 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 4238 | "power_point": point_dict.get(row[4]), |
||
| 4239 | "meter": meter_dict.get(row[5]), |
||
| 4240 | "rated_input_power": row[6] |
||
| 4241 | } |
||
| 4242 | |||
| 4243 | resp.text = json.dumps(meta_result) |
||
| 4244 | |||
| 4245 | @staticmethod |
||
| 4246 | @user_logger |
||
| 4247 | def on_delete(req, resp, id_, lid): |
||
| 4248 | admin_control(req) |
||
| 4249 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4250 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4251 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4252 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4253 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4254 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 4255 | |||
| 4256 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4257 | cursor = cnx.cursor() |
||
| 4258 | |||
| 4259 | cursor.execute(" SELECT name " |
||
| 4260 | " FROM tbl_energy_storage_containers " |
||
| 4261 | " WHERE id = %s ", (id_,)) |
||
| 4262 | if cursor.fetchone() is None: |
||
| 4263 | cursor.close() |
||
| 4264 | cnx.close() |
||
| 4265 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4266 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4267 | |||
| 4268 | cursor.execute(" SELECT name " |
||
| 4269 | " FROM tbl_energy_storage_containers_loads " |
||
| 4270 | " WHERE id = %s ", (lid,)) |
||
| 4271 | if cursor.fetchone() is None: |
||
| 4272 | cursor.close() |
||
| 4273 | cnx.close() |
||
| 4274 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4275 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4276 | |||
| 4277 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads " |
||
| 4278 | " WHERE id = %s ", (lid,)) |
||
| 4279 | cnx.commit() |
||
| 4280 | |||
| 4281 | cursor.close() |
||
| 4282 | cnx.close() |
||
| 4283 | |||
| 4284 | resp.status = falcon.HTTP_204 |
||
| 4285 | |||
| 4286 | @staticmethod |
||
| 4287 | @user_logger |
||
| 4288 | def on_put(req, resp, id_, lid): |
||
| 4289 | """Handles PUT requests""" |
||
| 4290 | admin_control(req) |
||
| 4291 | try: |
||
| 4292 | raw_json = req.stream.read().decode('utf-8') |
||
| 4293 | except UnicodeDecodeError as ex: |
||
| 4294 | print("Failed to decode request") |
||
| 4295 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4296 | title='API.BAD_REQUEST', |
||
| 4297 | description='API.INVALID_ENCODING') |
||
| 4298 | except Exception as ex: |
||
| 4299 | print("Unexpected error reading request stream") |
||
| 4300 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4301 | title='API.BAD_REQUEST', |
||
| 4302 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4303 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4304 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4305 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4306 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4307 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4308 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 4309 | |||
| 4310 | new_values = json.loads(raw_json) |
||
| 4311 | |||
| 4312 | if 'name' not in new_values['data'].keys() or \ |
||
| 4313 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4314 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4315 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4316 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_NAME') |
||
| 4317 | name = str.strip(new_values['data']['name']) |
||
| 4318 | |||
| 4319 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 4320 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 4321 | new_values['data']['power_point_id'] <= 0: |
||
| 4322 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4323 | description='API.INVALID_POWER_POINT_ID') |
||
| 4324 | power_point_id = new_values['data']['power_point_id'] |
||
| 4325 | |||
| 4326 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 4327 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 4328 | new_values['data']['meter_id'] <= 0: |
||
| 4329 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4330 | description='API.INVALID_METER_ID') |
||
| 4331 | meter_id = new_values['data']['meter_id'] |
||
| 4332 | |||
| 4333 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 4334 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 4335 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 4336 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4337 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 4338 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
||
| 4339 | |||
| 4340 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4341 | cursor = cnx.cursor() |
||
| 4342 | |||
| 4343 | cursor.execute(" SELECT name " |
||
| 4344 | " FROM tbl_energy_storage_containers " |
||
| 4345 | " WHERE id = %s ", (id_,)) |
||
| 4346 | if cursor.fetchone() is None: |
||
| 4347 | cursor.close() |
||
| 4348 | cnx.close() |
||
| 4349 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4350 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4351 | |||
| 4352 | cursor.execute(" SELECT name " |
||
| 4353 | " FROM tbl_energy_storage_containers_loads " |
||
| 4354 | " WHERE id = %s ", (lid,)) |
||
| 4355 | if cursor.fetchone() is None: |
||
| 4356 | cursor.close() |
||
| 4357 | cnx.close() |
||
| 4358 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4359 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4360 | |||
| 4361 | cursor.execute(" SELECT name " |
||
| 4362 | " FROM tbl_energy_storage_containers_loads " |
||
| 4363 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 4364 | (id_, name, lid)) |
||
| 4365 | if cursor.fetchone() is not None: |
||
| 4366 | cursor.close() |
||
| 4367 | cnx.close() |
||
| 4368 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4369 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 4370 | |||
| 4371 | cursor.execute(" SELECT name " |
||
| 4372 | " FROM tbl_points " |
||
| 4373 | " WHERE id = %s ", |
||
| 4374 | (power_point_id,)) |
||
| 4375 | if cursor.fetchone() is None: |
||
| 4376 | cursor.close() |
||
| 4377 | cnx.close() |
||
| 4378 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4379 | description='API.POWER_POINT_NOT_FOUND') |
||
| 4380 | |||
| 4381 | cursor.execute(" SELECT name " |
||
| 4382 | " FROM tbl_meters " |
||
| 4383 | " WHERE id = %s ", |
||
| 4384 | (meter_id,)) |
||
| 4385 | if cursor.fetchone() is None: |
||
| 4386 | cursor.close() |
||
| 4387 | cnx.close() |
||
| 4388 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4389 | description='API.METER_NOT_FOUND') |
||
| 4390 | |||
| 4391 | update_row = (" UPDATE tbl_energy_storage_containers_loads " |
||
| 4392 | " SET name = %s, energy_storage_container_id = %s, power_point_id = %s, " |
||
| 4393 | " meter_id = %s, rated_input_power = %s " |
||
| 4394 | " WHERE id = %s ") |
||
| 4395 | cursor.execute(update_row, (name, |
||
| 4396 | id_, |
||
| 4397 | power_point_id, |
||
| 4398 | meter_id, |
||
| 4399 | rated_input_power, |
||
| 4400 | lid)) |
||
| 4401 | cnx.commit() |
||
| 4402 | |||
| 4403 | cursor.close() |
||
| 4404 | cnx.close() |
||
| 4405 | |||
| 4406 | resp.status = falcon.HTTP_200 |
||
| 4407 | |||
| 4408 | |||
| 4409 | View Code Duplication | class EnergyStorageContainerLoadPointCollection: |
|
| 4410 | def __init__(self): |
||
| 4411 | pass |
||
| 4412 | |||
| 4413 | @staticmethod |
||
| 4414 | def on_options(req, resp, id_, lid): |
||
| 4415 | _ = req |
||
| 4416 | resp.status = falcon.HTTP_200 |
||
| 4417 | _ = id_ |
||
| 4418 | |||
| 4419 | @staticmethod |
||
| 4420 | def on_get(req, resp, id_, lid): |
||
| 4421 | if 'API-KEY' not in req.headers or \ |
||
| 4422 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 4423 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 4424 | access_control(req) |
||
| 4425 | else: |
||
| 4426 | api_key_control(req) |
||
| 4427 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4428 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4429 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4430 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4431 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4432 | description='API.INVALID_LOAD_ID') |
||
| 4433 | |||
| 4434 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4435 | cursor = cnx.cursor() |
||
| 4436 | |||
| 4437 | cursor.execute(" SELECT name " |
||
| 4438 | " FROM tbl_energy_storage_containers_loads " |
||
| 4439 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid, )) |
||
| 4440 | if cursor.fetchone() is None: |
||
| 4441 | cursor.close() |
||
| 4442 | cnx.close() |
||
| 4443 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4444 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4445 | |||
| 4446 | query = (" SELECT p.id, p.name, " |
||
| 4447 | " ds.id, ds.name, ds.uuid, " |
||
| 4448 | " p.address " |
||
| 4449 | " FROM tbl_points p, tbl_energy_storage_containers_loads_points mp, tbl_data_sources ds " |
||
| 4450 | " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 4451 | " ORDER BY p.name ") |
||
| 4452 | cursor.execute(query, (lid,)) |
||
| 4453 | rows = cursor.fetchall() |
||
| 4454 | |||
| 4455 | result = list() |
||
| 4456 | if rows is not None and len(rows) > 0: |
||
| 4457 | for row in rows: |
||
| 4458 | meta_result = {"id": row[0], "name": row[1], |
||
| 4459 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 4460 | "address": row[5]} |
||
| 4461 | result.append(meta_result) |
||
| 4462 | |||
| 4463 | resp.text = json.dumps(result) |
||
| 4464 | |||
| 4465 | @staticmethod |
||
| 4466 | @user_logger |
||
| 4467 | def on_post(req, resp, id_, lid): |
||
| 4468 | """Handles POST requests""" |
||
| 4469 | admin_control(req) |
||
| 4470 | try: |
||
| 4471 | raw_json = req.stream.read().decode('utf-8') |
||
| 4472 | except UnicodeDecodeError as ex: |
||
| 4473 | print("Failed to decode request") |
||
| 4474 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4475 | title='API.BAD_REQUEST', |
||
| 4476 | description='API.INVALID_ENCODING') |
||
| 4477 | except Exception as ex: |
||
| 4478 | print("Unexpected error reading request stream") |
||
| 4479 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4480 | title='API.BAD_REQUEST', |
||
| 4481 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4482 | |||
| 4483 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4484 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4485 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4486 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4487 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4488 | description='API.INVALID_LOAD_ID') |
||
| 4489 | |||
| 4490 | new_values = json.loads(raw_json) |
||
| 4491 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4492 | cursor = cnx.cursor() |
||
| 4493 | |||
| 4494 | cursor.execute(" SELECT name " |
||
| 4495 | " FROM tbl_energy_storage_containers_loads " |
||
| 4496 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid,)) |
||
| 4497 | if cursor.fetchone() is None: |
||
| 4498 | cursor.close() |
||
| 4499 | cnx.close() |
||
| 4500 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4501 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4502 | |||
| 4503 | cursor.execute(" SELECT name, object_type " |
||
| 4504 | " FROM tbl_points " |
||
| 4505 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 4506 | row = cursor.fetchone() |
||
| 4507 | if row is None: |
||
| 4508 | cursor.close() |
||
| 4509 | cnx.close() |
||
| 4510 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4511 | description='API.POINT_NOT_FOUND') |
||
| 4512 | |||
| 4513 | query = (" SELECT id " |
||
| 4514 | " FROM tbl_energy_storage_containers_loads_points " |
||
| 4515 | " WHERE load_id = %s AND point_id = %s") |
||
| 4516 | cursor.execute(query, (lid, new_values['data']['point_id'],)) |
||
| 4517 | if cursor.fetchone() is not None: |
||
| 4518 | cursor.close() |
||
| 4519 | cnx.close() |
||
| 4520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4521 | description='API.LOAD_POINT_RELATION_EXISTS') |
||
| 4522 | |||
| 4523 | add_row = (" INSERT INTO tbl_energy_storage_containers_loads_points (load_id, point_id) " |
||
| 4524 | " VALUES (%s, %s) ") |
||
| 4525 | cursor.execute(add_row, (lid, new_values['data']['point_id'],)) |
||
| 4526 | cnx.commit() |
||
| 4527 | cursor.close() |
||
| 4528 | cnx.close() |
||
| 4529 | |||
| 4530 | resp.status = falcon.HTTP_201 |
||
| 4531 | resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(lid) + '/points/' + \ |
||
| 4532 | str(new_values['data']['point_id']) |
||
| 4533 | |||
| 4534 | |||
| 4535 | View Code Duplication | class EnergyStorageContainerLoadPointItem: |
|
| 4536 | def __init__(self): |
||
| 4537 | pass |
||
| 4538 | |||
| 4539 | @staticmethod |
||
| 4540 | def on_options(req, resp, id_, lid, pid): |
||
| 4541 | _ = req |
||
| 4542 | resp.status = falcon.HTTP_200 |
||
| 4543 | _ = id_ |
||
| 4544 | |||
| 4545 | @staticmethod |
||
| 4546 | @user_logger |
||
| 4547 | def on_delete(req, resp, id_, lid, pid): |
||
| 4548 | """Handles DELETE requests""" |
||
| 4549 | admin_control(req) |
||
| 4550 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4551 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4552 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4553 | if not lid.isdigit() or int(lid) <= 0: |
||
| 4554 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4555 | description='API.INVALID_LOAD_ID') |
||
| 4556 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4557 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4558 | description='API.INVALID_POINT_ID') |
||
| 4559 | |||
| 4560 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4561 | cursor = cnx.cursor() |
||
| 4562 | |||
| 4563 | cursor.execute(" SELECT name " |
||
| 4564 | " FROM tbl_energy_storage_containers_loads " |
||
| 4565 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid,)) |
||
| 4566 | if cursor.fetchone() is None: |
||
| 4567 | cursor.close() |
||
| 4568 | cnx.close() |
||
| 4569 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4570 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 4571 | |||
| 4572 | cursor.execute(" SELECT name " |
||
| 4573 | " FROM tbl_points " |
||
| 4574 | " WHERE id = %s ", (pid,)) |
||
| 4575 | if cursor.fetchone() is None: |
||
| 4576 | cursor.close() |
||
| 4577 | cnx.close() |
||
| 4578 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4579 | description='API.POINT_NOT_FOUND') |
||
| 4580 | |||
| 4581 | cursor.execute(" SELECT id " |
||
| 4582 | " FROM tbl_energy_storage_containers_loads_points " |
||
| 4583 | " WHERE load_id = %s AND point_id = %s ", (lid, pid)) |
||
| 4584 | if cursor.fetchone() is None: |
||
| 4585 | cursor.close() |
||
| 4586 | cnx.close() |
||
| 4587 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4588 | description='API.LOAD_POINT_RELATION_NOT_FOUND') |
||
| 4589 | |||
| 4590 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads_points " |
||
| 4591 | " WHERE load_id = %s AND point_id = %s ", (lid, pid)) |
||
| 4592 | cnx.commit() |
||
| 4593 | |||
| 4594 | cursor.close() |
||
| 4595 | cnx.close() |
||
| 4596 | |||
| 4597 | resp.status = falcon.HTTP_204 |
||
| 4598 | |||
| 4599 | |||
| 4600 | class EnergyStorageContainerPCSCollection: |
||
| 4601 | def __init__(self): |
||
| 4602 | pass |
||
| 4603 | |||
| 4604 | @staticmethod |
||
| 4605 | def on_options(req, resp, id_): |
||
| 4606 | _ = req |
||
| 4607 | resp.status = falcon.HTTP_200 |
||
| 4608 | _ = id_ |
||
| 4609 | |||
| 4610 | @staticmethod |
||
| 4611 | def on_get(req, resp, id_): |
||
| 4612 | access_control(req) |
||
| 4613 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4614 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4615 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4616 | |||
| 4617 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4618 | cursor = cnx.cursor() |
||
| 4619 | |||
| 4620 | cursor.execute(" SELECT name " |
||
| 4621 | " FROM tbl_energy_storage_containers " |
||
| 4622 | " WHERE id = %s ", (id_,)) |
||
| 4623 | if cursor.fetchone() is None: |
||
| 4624 | cursor.close() |
||
| 4625 | cnx.close() |
||
| 4626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4627 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4628 | |||
| 4629 | # query point dict |
||
| 4630 | query = (" SELECT id, name " |
||
| 4631 | " FROM tbl_points ") |
||
| 4632 | cursor.execute(query) |
||
| 4633 | rows_points = cursor.fetchall() |
||
| 4634 | |||
| 4635 | point_dict = dict() |
||
| 4636 | if rows_points is not None and len(rows_points) > 0: |
||
| 4637 | for row in rows_points: |
||
| 4638 | point_dict[row[0]] = {"id": row[0], |
||
| 4639 | "name": row[1]} |
||
| 4640 | # query command dict |
||
| 4641 | query = (" SELECT id, name " |
||
| 4642 | " FROM tbl_commands ") |
||
| 4643 | cursor.execute(query) |
||
| 4644 | rows_commands = cursor.fetchall() |
||
| 4645 | |||
| 4646 | command_dict = dict() |
||
| 4647 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 4648 | for row in rows_commands: |
||
| 4649 | command_dict[row[0]] = {"id": row[0], |
||
| 4650 | "name": row[1]} |
||
| 4651 | |||
| 4652 | query = (" SELECT id, name, uuid, run_state_point_id, rated_output_power " |
||
| 4653 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4654 | " WHERE energy_storage_container_id = %s " |
||
| 4655 | " ORDER BY name ") |
||
| 4656 | cursor.execute(query, (id_,)) |
||
| 4657 | rows = cursor.fetchall() |
||
| 4658 | |||
| 4659 | result = list() |
||
| 4660 | if rows is not None and len(rows) > 0: |
||
| 4661 | for row in rows: |
||
| 4662 | meta_result = {"id": row[0], |
||
| 4663 | "name": row[1], |
||
| 4664 | "uuid": row[2], |
||
| 4665 | "run_state_point": point_dict.get(row[3]), |
||
| 4666 | "rated_output_power": row[4] |
||
| 4667 | } |
||
| 4668 | result.append(meta_result) |
||
| 4669 | |||
| 4670 | resp.text = json.dumps(result) |
||
| 4671 | |||
| 4672 | @staticmethod |
||
| 4673 | @user_logger |
||
| 4674 | def on_post(req, resp, id_): |
||
| 4675 | """Handles POST requests""" |
||
| 4676 | admin_control(req) |
||
| 4677 | try: |
||
| 4678 | raw_json = req.stream.read().decode('utf-8') |
||
| 4679 | except UnicodeDecodeError as ex: |
||
| 4680 | print("Failed to decode request") |
||
| 4681 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4682 | title='API.BAD_REQUEST', |
||
| 4683 | description='API.INVALID_ENCODING') |
||
| 4684 | except Exception as ex: |
||
| 4685 | print("Unexpected error reading request stream") |
||
| 4686 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4687 | title='API.BAD_REQUEST', |
||
| 4688 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4689 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4690 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4691 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4692 | |||
| 4693 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4694 | cursor = cnx.cursor() |
||
| 4695 | |||
| 4696 | cursor.execute(" SELECT name " |
||
| 4697 | " FROM tbl_energy_storage_containers " |
||
| 4698 | " WHERE id = %s ", (id_,)) |
||
| 4699 | if cursor.fetchone() is None: |
||
| 4700 | cursor.close() |
||
| 4701 | cnx.close() |
||
| 4702 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4703 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4704 | |||
| 4705 | new_values = json.loads(raw_json) |
||
| 4706 | |||
| 4707 | if 'name' not in new_values['data'].keys() or \ |
||
| 4708 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4709 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4710 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4711 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NAME') |
||
| 4712 | name = str.strip(new_values['data']['name']) |
||
| 4713 | |||
| 4714 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 4715 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 4716 | new_values['data']['run_state_point_id'] <= 0: |
||
| 4717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4718 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 4719 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 4720 | |||
| 4721 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 4722 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 4723 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 4724 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4725 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 4726 | rated_output_power = Decimal(new_values['data']['rated_output_power']) |
||
| 4727 | |||
| 4728 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4729 | cursor = cnx.cursor() |
||
| 4730 | |||
| 4731 | cursor.execute(" SELECT name " |
||
| 4732 | " FROM tbl_energy_storage_containers " |
||
| 4733 | " WHERE id = %s ", |
||
| 4734 | (id_,)) |
||
| 4735 | if cursor.fetchone() is None: |
||
| 4736 | cursor.close() |
||
| 4737 | cnx.close() |
||
| 4738 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4739 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4740 | |||
| 4741 | cursor.execute(" SELECT name " |
||
| 4742 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4743 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 4744 | (id_, name,)) |
||
| 4745 | if cursor.fetchone() is not None: |
||
| 4746 | cursor.close() |
||
| 4747 | cnx.close() |
||
| 4748 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4749 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NAME_IS_ALREADY_IN_USE') |
||
| 4750 | |||
| 4751 | add_values = (" INSERT INTO tbl_energy_storage_containers_power_conversion_systems " |
||
| 4752 | " (name, uuid, energy_storage_container_id, run_state_point_id, rated_output_power) " |
||
| 4753 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 4754 | cursor.execute(add_values, (name, |
||
| 4755 | str(uuid.uuid4()), |
||
| 4756 | id_, |
||
| 4757 | run_state_point_id, |
||
| 4758 | rated_output_power |
||
| 4759 | )) |
||
| 4760 | new_id = cursor.lastrowid |
||
| 4761 | cnx.commit() |
||
| 4762 | cursor.close() |
||
| 4763 | cnx.close() |
||
| 4764 | |||
| 4765 | resp.status = falcon.HTTP_201 |
||
| 4766 | resp.location = '/energystoragecontainerpowerconversionsystems/' + str(new_id) |
||
| 4767 | |||
| 4768 | |||
| 4769 | class EnergyStorageContainerPCSItem: |
||
| 4770 | def __init__(self): |
||
| 4771 | pass |
||
| 4772 | |||
| 4773 | @staticmethod |
||
| 4774 | def on_options(req, resp, id_, pcsid): |
||
| 4775 | _ = req |
||
| 4776 | resp.status = falcon.HTTP_200 |
||
| 4777 | _ = id_ |
||
| 4778 | |||
| 4779 | @staticmethod |
||
| 4780 | def on_get(req, resp, id_, pcsid): |
||
| 4781 | access_control(req) |
||
| 4782 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4783 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4784 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4785 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 4786 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4787 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 4788 | |||
| 4789 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4790 | cursor = cnx.cursor() |
||
| 4791 | |||
| 4792 | cursor.execute(" SELECT name " |
||
| 4793 | " FROM tbl_energy_storage_containers " |
||
| 4794 | " WHERE id = %s ", (id_,)) |
||
| 4795 | if cursor.fetchone() is None: |
||
| 4796 | cursor.close() |
||
| 4797 | cnx.close() |
||
| 4798 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4799 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4800 | |||
| 4801 | query = (" SELECT id, name, uuid " |
||
| 4802 | " FROM tbl_energy_storage_containers ") |
||
| 4803 | cursor.execute(query) |
||
| 4804 | rows_energystoragecontainers = cursor.fetchall() |
||
| 4805 | |||
| 4806 | energy_storage_container_dict = dict() |
||
| 4807 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 4808 | for row in rows_energystoragecontainers: |
||
| 4809 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 4810 | "name": row[1], |
||
| 4811 | "uuid": row[2]} |
||
| 4812 | query = (" SELECT id, name, uuid " |
||
| 4813 | " FROM tbl_meters ") |
||
| 4814 | cursor.execute(query) |
||
| 4815 | rows_meters = cursor.fetchall() |
||
| 4816 | |||
| 4817 | meter_dict = dict() |
||
| 4818 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 4819 | for row in rows_meters: |
||
| 4820 | meter_dict[row[0]] = {"id": row[0], |
||
| 4821 | "name": row[1], |
||
| 4822 | "uuid": row[2]} |
||
| 4823 | # query point dict |
||
| 4824 | query = (" SELECT id, name " |
||
| 4825 | " FROM tbl_points ") |
||
| 4826 | cursor.execute(query) |
||
| 4827 | rows_points = cursor.fetchall() |
||
| 4828 | |||
| 4829 | point_dict = dict() |
||
| 4830 | if rows_points is not None and len(rows_points) > 0: |
||
| 4831 | for row in rows_points: |
||
| 4832 | point_dict[row[0]] = {"id": row[0], |
||
| 4833 | "name": row[1]} |
||
| 4834 | |||
| 4835 | # query command dict |
||
| 4836 | query = (" SELECT id, name " |
||
| 4837 | " FROM tbl_commands ") |
||
| 4838 | cursor.execute(query) |
||
| 4839 | rows_commands = cursor.fetchall() |
||
| 4840 | |||
| 4841 | command_dict = dict() |
||
| 4842 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 4843 | for row in rows_commands: |
||
| 4844 | command_dict[row[0]] = {"id": row[0], |
||
| 4845 | "name": row[1]} |
||
| 4846 | |||
| 4847 | query = (" SELECT id, name, uuid, energy_storage_container_id, run_state_point_id, rated_output_power " |
||
| 4848 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4849 | " WHERE id = %s ") |
||
| 4850 | cursor.execute(query, (pcsid,)) |
||
| 4851 | row = cursor.fetchone() |
||
| 4852 | cursor.close() |
||
| 4853 | cnx.close() |
||
| 4854 | |||
| 4855 | if row is None: |
||
| 4856 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4857 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4858 | else: |
||
| 4859 | meta_result = {"id": row[0], |
||
| 4860 | "name": row[1], |
||
| 4861 | "uuid": row[2], |
||
| 4862 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 4863 | "run_state_point": point_dict.get(row[4]), |
||
| 4864 | "rated_output_power": row[5] |
||
| 4865 | } |
||
| 4866 | |||
| 4867 | resp.text = json.dumps(meta_result) |
||
| 4868 | |||
| 4869 | @staticmethod |
||
| 4870 | @user_logger |
||
| 4871 | def on_delete(req, resp, id_, pcsid): |
||
| 4872 | admin_control(req) |
||
| 4873 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4874 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4875 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4876 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 4877 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4878 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 4879 | |||
| 4880 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4881 | cursor = cnx.cursor() |
||
| 4882 | |||
| 4883 | cursor.execute(" SELECT name " |
||
| 4884 | " FROM tbl_energy_storage_containers " |
||
| 4885 | " WHERE id = %s ", (id_,)) |
||
| 4886 | if cursor.fetchone() is None: |
||
| 4887 | cursor.close() |
||
| 4888 | cnx.close() |
||
| 4889 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4890 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4891 | |||
| 4892 | cursor.execute(" SELECT name " |
||
| 4893 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4894 | " WHERE id = %s ", (pcsid,)) |
||
| 4895 | if cursor.fetchone() is None: |
||
| 4896 | cursor.close() |
||
| 4897 | cnx.close() |
||
| 4898 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4899 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4900 | |||
| 4901 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4902 | " WHERE id = %s ", (pcsid,)) |
||
| 4903 | cnx.commit() |
||
| 4904 | |||
| 4905 | cursor.close() |
||
| 4906 | cnx.close() |
||
| 4907 | |||
| 4908 | resp.status = falcon.HTTP_204 |
||
| 4909 | |||
| 4910 | @staticmethod |
||
| 4911 | @user_logger |
||
| 4912 | def on_put(req, resp, id_, pcsid): |
||
| 4913 | """Handles PUT requests""" |
||
| 4914 | admin_control(req) |
||
| 4915 | try: |
||
| 4916 | raw_json = req.stream.read().decode('utf-8') |
||
| 4917 | except UnicodeDecodeError as ex: |
||
| 4918 | print("Failed to decode request") |
||
| 4919 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4920 | title='API.BAD_REQUEST', |
||
| 4921 | description='API.INVALID_ENCODING') |
||
| 4922 | except Exception as ex: |
||
| 4923 | print("Unexpected error reading request stream") |
||
| 4924 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4925 | title='API.BAD_REQUEST', |
||
| 4926 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4927 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4928 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4929 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4930 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 4931 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4932 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 4933 | |||
| 4934 | new_values = json.loads(raw_json) |
||
| 4935 | |||
| 4936 | if 'name' not in new_values['data'].keys() or \ |
||
| 4937 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4938 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4939 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4940 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NAME') |
||
| 4941 | name = str.strip(new_values['data']['name']) |
||
| 4942 | |||
| 4943 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 4944 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 4945 | new_values['data']['run_state_point_id'] <= 0: |
||
| 4946 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4947 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 4948 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 4949 | |||
| 4950 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 4951 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 4952 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 4953 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4954 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 4955 | rated_output_power = Decimal(new_values['data']['rated_output_power']) |
||
| 4956 | |||
| 4957 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4958 | cursor = cnx.cursor() |
||
| 4959 | |||
| 4960 | cursor.execute(" SELECT name " |
||
| 4961 | " FROM tbl_energy_storage_containers " |
||
| 4962 | " WHERE id = %s ", (id_,)) |
||
| 4963 | if cursor.fetchone() is None: |
||
| 4964 | cursor.close() |
||
| 4965 | cnx.close() |
||
| 4966 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4967 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4968 | |||
| 4969 | cursor.execute(" SELECT name " |
||
| 4970 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4971 | " WHERE id = %s ", (pcsid,)) |
||
| 4972 | if cursor.fetchone() is None: |
||
| 4973 | cursor.close() |
||
| 4974 | cnx.close() |
||
| 4975 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4976 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4977 | |||
| 4978 | cursor.execute(" SELECT name " |
||
| 4979 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 4980 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 4981 | (id_, name, pcsid)) |
||
| 4982 | if cursor.fetchone() is not None: |
||
| 4983 | cursor.close() |
||
| 4984 | cnx.close() |
||
| 4985 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4986 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NAME_IS_ALREADY_IN_USE') |
||
| 4987 | |||
| 4988 | update_row = (" UPDATE tbl_energy_storage_containers_power_conversion_systems " |
||
| 4989 | " SET name = %s, energy_storage_container_id = %s, run_state_point_id = %s, " |
||
| 4990 | " rated_output_power = %s " |
||
| 4991 | " WHERE id = %s ") |
||
| 4992 | cursor.execute(update_row, (name, |
||
| 4993 | id_, |
||
| 4994 | run_state_point_id, |
||
| 4995 | rated_output_power, |
||
| 4996 | pcsid)) |
||
| 4997 | cnx.commit() |
||
| 4998 | |||
| 4999 | cursor.close() |
||
| 5000 | cnx.close() |
||
| 5001 | |||
| 5002 | resp.status = falcon.HTTP_200 |
||
| 5003 | |||
| 5004 | |||
| 5005 | View Code Duplication | class EnergyStorageContainerPCSPointCollection: |
|
| 5006 | def __init__(self): |
||
| 5007 | pass |
||
| 5008 | |||
| 5009 | @staticmethod |
||
| 5010 | def on_options(req, resp, id_, pcsid): |
||
| 5011 | _ = req |
||
| 5012 | resp.status = falcon.HTTP_200 |
||
| 5013 | _ = id_ |
||
| 5014 | |||
| 5015 | @staticmethod |
||
| 5016 | def on_get(req, resp, id_, pcsid): |
||
| 5017 | if 'API-KEY' not in req.headers or \ |
||
| 5018 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5019 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5020 | access_control(req) |
||
| 5021 | else: |
||
| 5022 | api_key_control(req) |
||
| 5023 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5024 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5025 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5026 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 5027 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5028 | description='API.INVALID_PCS_ID') |
||
| 5029 | |||
| 5030 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5031 | cursor = cnx.cursor() |
||
| 5032 | |||
| 5033 | cursor.execute(" SELECT name " |
||
| 5034 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 5035 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid, )) |
||
| 5036 | if cursor.fetchone() is None: |
||
| 5037 | cursor.close() |
||
| 5038 | cnx.close() |
||
| 5039 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5040 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND') |
||
| 5041 | |||
| 5042 | query = (" SELECT p.id, p.name, " |
||
| 5043 | " ds.id, ds.name, ds.uuid, " |
||
| 5044 | " p.address " |
||
| 5045 | " FROM tbl_points p, tbl_energy_storage_containers_pcses_points mp, tbl_data_sources ds " |
||
| 5046 | " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 5047 | " ORDER BY p.name ") |
||
| 5048 | cursor.execute(query, (pcsid,)) |
||
| 5049 | rows = cursor.fetchall() |
||
| 5050 | |||
| 5051 | result = list() |
||
| 5052 | if rows is not None and len(rows) > 0: |
||
| 5053 | for row in rows: |
||
| 5054 | meta_result = {"id": row[0], "name": row[1], |
||
| 5055 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 5056 | "address": row[5]} |
||
| 5057 | result.append(meta_result) |
||
| 5058 | |||
| 5059 | resp.text = json.dumps(result) |
||
| 5060 | |||
| 5061 | @staticmethod |
||
| 5062 | @user_logger |
||
| 5063 | def on_post(req, resp, id_, pcsid): |
||
| 5064 | """Handles POST requests""" |
||
| 5065 | admin_control(req) |
||
| 5066 | try: |
||
| 5067 | raw_json = req.stream.read().decode('utf-8') |
||
| 5068 | except UnicodeDecodeError as ex: |
||
| 5069 | print("Failed to decode request") |
||
| 5070 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5071 | title='API.BAD_REQUEST', |
||
| 5072 | description='API.INVALID_ENCODING') |
||
| 5073 | except Exception as ex: |
||
| 5074 | print("Unexpected error reading request stream") |
||
| 5075 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5076 | title='API.BAD_REQUEST', |
||
| 5077 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5078 | |||
| 5079 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5080 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5081 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5082 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 5083 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5084 | description='API.INVALID_PCS_ID') |
||
| 5085 | |||
| 5086 | new_values = json.loads(raw_json) |
||
| 5087 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5088 | cursor = cnx.cursor() |
||
| 5089 | |||
| 5090 | cursor.execute(" SELECT name " |
||
| 5091 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 5092 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid,)) |
||
| 5093 | if cursor.fetchone() is None: |
||
| 5094 | cursor.close() |
||
| 5095 | cnx.close() |
||
| 5096 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5097 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND') |
||
| 5098 | |||
| 5099 | cursor.execute(" SELECT name, object_type " |
||
| 5100 | " FROM tbl_points " |
||
| 5101 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 5102 | row = cursor.fetchone() |
||
| 5103 | if row is None: |
||
| 5104 | cursor.close() |
||
| 5105 | cnx.close() |
||
| 5106 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5107 | description='API.POINT_NOT_FOUND') |
||
| 5108 | |||
| 5109 | query = (" SELECT id " |
||
| 5110 | " FROM tbl_energy_storage_containers_pcses_points " |
||
| 5111 | " WHERE pcs_id = %s AND point_id = %s") |
||
| 5112 | cursor.execute(query, (pcsid, new_values['data']['point_id'],)) |
||
| 5113 | if cursor.fetchone() is not None: |
||
| 5114 | cursor.close() |
||
| 5115 | cnx.close() |
||
| 5116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5117 | description='API.PCS_POINT_RELATION_EXISTS') |
||
| 5118 | |||
| 5119 | add_row = (" INSERT INTO tbl_energy_storage_containers_pcses_points (pcs_id, point_id) " |
||
| 5120 | " VALUES (%s, %s) ") |
||
| 5121 | cursor.execute(add_row, (pcsid, new_values['data']['point_id'],)) |
||
| 5122 | cnx.commit() |
||
| 5123 | cursor.close() |
||
| 5124 | cnx.close() |
||
| 5125 | |||
| 5126 | resp.status = falcon.HTTP_201 |
||
| 5127 | resp.location = '/energystoragecontainers/' + str(id_) + '/pcses/' + str(pcsid) + '/points/' + \ |
||
| 5128 | str(new_values['data']['point_id']) |
||
| 5129 | |||
| 5130 | |||
| 5131 | View Code Duplication | class EnergyStorageContainerPCSPointItem: |
|
| 5132 | def __init__(self): |
||
| 5133 | pass |
||
| 5134 | |||
| 5135 | @staticmethod |
||
| 5136 | def on_options(req, resp, id_, pcsid, pid): |
||
| 5137 | _ = req |
||
| 5138 | resp.status = falcon.HTTP_200 |
||
| 5139 | _ = id_ |
||
| 5140 | |||
| 5141 | @staticmethod |
||
| 5142 | @user_logger |
||
| 5143 | def on_delete(req, resp, id_, pcsid, pid): |
||
| 5144 | """Handles DELETE requests""" |
||
| 5145 | admin_control(req) |
||
| 5146 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5147 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5148 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5149 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 5150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5151 | description='API.INVALID_PCS_ID') |
||
| 5152 | if not pid.isdigit() or int(pid) <= 0: |
||
| 5153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5154 | description='API.INVALID_POINT_ID') |
||
| 5155 | |||
| 5156 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5157 | cursor = cnx.cursor() |
||
| 5158 | |||
| 5159 | cursor.execute(" SELECT name " |
||
| 5160 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 5161 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid,)) |
||
| 5162 | if cursor.fetchone() is None: |
||
| 5163 | cursor.close() |
||
| 5164 | cnx.close() |
||
| 5165 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5166 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND') |
||
| 5167 | |||
| 5168 | cursor.execute(" SELECT name " |
||
| 5169 | " FROM tbl_points " |
||
| 5170 | " WHERE id = %s ", (pid,)) |
||
| 5171 | if cursor.fetchone() is None: |
||
| 5172 | cursor.close() |
||
| 5173 | cnx.close() |
||
| 5174 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5175 | description='API.POINT_NOT_FOUND') |
||
| 5176 | |||
| 5177 | cursor.execute(" SELECT id " |
||
| 5178 | " FROM tbl_energy_storage_containers_pcses_points " |
||
| 5179 | " WHERE pcs_id = %s AND point_id = %s ", (pcsid, pid)) |
||
| 5180 | if cursor.fetchone() is None: |
||
| 5181 | cursor.close() |
||
| 5182 | cnx.close() |
||
| 5183 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5184 | description='API.PCS_POINT_RELATION_NOT_FOUND') |
||
| 5185 | |||
| 5186 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_pcses_points " |
||
| 5187 | " WHERE pcs_id = %s AND point_id = %s ", (pcsid, pid)) |
||
| 5188 | cnx.commit() |
||
| 5189 | |||
| 5190 | cursor.close() |
||
| 5191 | cnx.close() |
||
| 5192 | |||
| 5193 | resp.status = falcon.HTTP_204 |
||
| 5194 | |||
| 5195 | |||
| 5196 | View Code Duplication | class EnergyStorageContainerScheduleCollection: |
|
| 5197 | def __init__(self): |
||
| 5198 | pass |
||
| 5199 | |||
| 5200 | @staticmethod |
||
| 5201 | def on_options(req, resp, id_): |
||
| 5202 | _ = req |
||
| 5203 | resp.status = falcon.HTTP_200 |
||
| 5204 | _ = id_ |
||
| 5205 | |||
| 5206 | @staticmethod |
||
| 5207 | def on_get(req, resp, id_): |
||
| 5208 | access_control(req) |
||
| 5209 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5210 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5211 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5212 | |||
| 5213 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5214 | cursor = cnx.cursor() |
||
| 5215 | |||
| 5216 | cursor.execute(" SELECT name " |
||
| 5217 | " FROM tbl_energy_storage_containers " |
||
| 5218 | " WHERE id = %s ", (id_,)) |
||
| 5219 | if cursor.fetchone() is None: |
||
| 5220 | cursor.close() |
||
| 5221 | cnx.close() |
||
| 5222 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5223 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5224 | |||
| 5225 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 5226 | " FROM tbl_energy_storage_containers_schedules " |
||
| 5227 | " WHERE energy_storage_container_id = %s " |
||
| 5228 | " ORDER BY start_time_of_day ") |
||
| 5229 | cursor.execute(query, (id_,)) |
||
| 5230 | rows = cursor.fetchall() |
||
| 5231 | |||
| 5232 | result = list() |
||
| 5233 | if rows is not None and len(rows) > 0: |
||
| 5234 | for row in rows: |
||
| 5235 | meta_result = {"id": row[0], |
||
| 5236 | "start_time_of_day": str(row[1]), |
||
| 5237 | "end_time_of_day": str(row[2]), |
||
| 5238 | "peak_type": row[3], |
||
| 5239 | "power": row[4], |
||
| 5240 | } |
||
| 5241 | result.append(meta_result) |
||
| 5242 | |||
| 5243 | resp.text = json.dumps(result) |
||
| 5244 | |||
| 5245 | @staticmethod |
||
| 5246 | @user_logger |
||
| 5247 | def on_post(req, resp, id_): |
||
| 5248 | """Handles POST requests""" |
||
| 5249 | admin_control(req) |
||
| 5250 | try: |
||
| 5251 | raw_json = req.stream.read().decode('utf-8') |
||
| 5252 | except UnicodeDecodeError as ex: |
||
| 5253 | print("Failed to decode request") |
||
| 5254 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5255 | title='API.BAD_REQUEST', |
||
| 5256 | description='API.INVALID_ENCODING') |
||
| 5257 | except Exception as ex: |
||
| 5258 | print("Unexpected error reading request stream") |
||
| 5259 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5260 | title='API.BAD_REQUEST', |
||
| 5261 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5262 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5263 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5264 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5265 | |||
| 5266 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5267 | cursor = cnx.cursor() |
||
| 5268 | |||
| 5269 | cursor.execute(" SELECT name " |
||
| 5270 | " FROM tbl_energy_storage_containers " |
||
| 5271 | " WHERE id = %s ", (id_,)) |
||
| 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.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5277 | |||
| 5278 | new_values = json.loads(raw_json) |
||
| 5279 | |||
| 5280 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5281 | cursor = cnx.cursor() |
||
| 5282 | |||
| 5283 | cursor.execute(" SELECT name " |
||
| 5284 | " FROM tbl_energy_storage_containers " |
||
| 5285 | " WHERE id = %s ", |
||
| 5286 | (id_,)) |
||
| 5287 | if cursor.fetchone() is None: |
||
| 5288 | cursor.close() |
||
| 5289 | cnx.close() |
||
| 5290 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5291 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5292 | |||
| 5293 | add_schedule = (" INSERT INTO tbl_energy_storage_containers_schedules " |
||
| 5294 | " (energy_storage_container_id, start_time_of_day, end_time_of_day, peak_type, power) " |
||
| 5295 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 5296 | cursor.execute(add_schedule, (id_, |
||
| 5297 | new_values['data']['start_time_of_day'], |
||
| 5298 | new_values['data']['end_time_of_day'], |
||
| 5299 | new_values['data']['peak_type'], |
||
| 5300 | new_values['data']['power'])) |
||
| 5301 | new_id = cursor.lastrowid |
||
| 5302 | cnx.commit() |
||
| 5303 | cursor.close() |
||
| 5304 | cnx.close() |
||
| 5305 | resp.status = falcon.HTTP_201 |
||
| 5306 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
||
| 5307 | |||
| 5308 | |||
| 5309 | View Code Duplication | class EnergyStorageContainerScheduleItem: |
|
| 5310 | def __init__(self): |
||
| 5311 | pass |
||
| 5312 | |||
| 5313 | @staticmethod |
||
| 5314 | def on_options(req, resp, id_, sid): |
||
| 5315 | _ = req |
||
| 5316 | resp.status = falcon.HTTP_200 |
||
| 5317 | _ = id_ |
||
| 5318 | |||
| 5319 | @staticmethod |
||
| 5320 | def on_get(req, resp, id_, sid): |
||
| 5321 | access_control(req) |
||
| 5322 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5323 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5324 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5325 | if not sid.isdigit() or int(sid) <= 0: |
||
| 5326 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5327 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 5328 | |||
| 5329 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5330 | cursor = cnx.cursor() |
||
| 5331 | |||
| 5332 | cursor.execute(" SELECT name " |
||
| 5333 | " FROM tbl_energy_storage_containers " |
||
| 5334 | " WHERE id = %s ", (id_,)) |
||
| 5335 | if cursor.fetchone() is None: |
||
| 5336 | cursor.close() |
||
| 5337 | cnx.close() |
||
| 5338 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5339 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5340 | |||
| 5341 | query = (" SELECT id, name, uuid " |
||
| 5342 | " FROM tbl_energy_storage_containers ") |
||
| 5343 | cursor.execute(query) |
||
| 5344 | rows_energystoragecontainers = cursor.fetchall() |
||
| 5345 | |||
| 5346 | energy_storage_container_dict = dict() |
||
| 5347 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 5348 | for row in rows_energystoragecontainers: |
||
| 5349 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 5350 | "name": row[1], |
||
| 5351 | "uuid": row[2]} |
||
| 5352 | |||
| 5353 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 5354 | " FROM tbl_energy_storage_containers_schedules " |
||
| 5355 | " WHERE id = %s ") |
||
| 5356 | cursor.execute(query, (sid,)) |
||
| 5357 | row = cursor.fetchone() |
||
| 5358 | cursor.close() |
||
| 5359 | cnx.close() |
||
| 5360 | |||
| 5361 | if row is None: |
||
| 5362 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5363 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 5364 | else: |
||
| 5365 | meta_result = {"id": row[0], |
||
| 5366 | "start_time_of_day": str(row[1]), |
||
| 5367 | "end_time_of_day": str(row[2]), |
||
| 5368 | "peak_type": row[3], |
||
| 5369 | "power": row[4]} |
||
| 5370 | |||
| 5371 | resp.text = json.dumps(meta_result) |
||
| 5372 | |||
| 5373 | @staticmethod |
||
| 5374 | @user_logger |
||
| 5375 | def on_delete(req, resp, id_, sid): |
||
| 5376 | admin_control(req) |
||
| 5377 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5378 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5379 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5380 | if not sid.isdigit() or int(sid) <= 0: |
||
| 5381 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5382 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 5383 | |||
| 5384 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5385 | cursor = cnx.cursor() |
||
| 5386 | |||
| 5387 | cursor.execute(" SELECT name " |
||
| 5388 | " FROM tbl_energy_storage_containers " |
||
| 5389 | " WHERE id = %s ", (id_,)) |
||
| 5390 | if cursor.fetchone() is None: |
||
| 5391 | cursor.close() |
||
| 5392 | cnx.close() |
||
| 5393 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5394 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5395 | |||
| 5396 | cursor.execute(" SELECT id " |
||
| 5397 | " FROM tbl_energy_storage_containers_schedules " |
||
| 5398 | " WHERE id = %s ", (sid,)) |
||
| 5399 | if cursor.fetchone() is None: |
||
| 5400 | cursor.close() |
||
| 5401 | cnx.close() |
||
| 5402 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5403 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 5404 | |||
| 5405 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
||
| 5406 | " WHERE id = %s ", (sid,)) |
||
| 5407 | cnx.commit() |
||
| 5408 | |||
| 5409 | cursor.close() |
||
| 5410 | cnx.close() |
||
| 5411 | |||
| 5412 | resp.status = falcon.HTTP_204 |
||
| 5413 | |||
| 5414 | @staticmethod |
||
| 5415 | @user_logger |
||
| 5416 | def on_put(req, resp, id_, sid): |
||
| 5417 | """Handles PUT requests""" |
||
| 5418 | admin_control(req) |
||
| 5419 | try: |
||
| 5420 | raw_json = req.stream.read().decode('utf-8') |
||
| 5421 | except UnicodeDecodeError as ex: |
||
| 5422 | print("Failed to decode request") |
||
| 5423 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5424 | title='API.BAD_REQUEST', |
||
| 5425 | description='API.INVALID_ENCODING') |
||
| 5426 | except Exception as ex: |
||
| 5427 | print("Unexpected error reading request stream") |
||
| 5428 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5429 | title='API.BAD_REQUEST', |
||
| 5430 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5431 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5432 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5433 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5434 | if not sid.isdigit() or int(sid) <= 0: |
||
| 5435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5436 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 5437 | |||
| 5438 | new_values = json.loads(raw_json) |
||
| 5439 | |||
| 5440 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
||
| 5441 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
||
| 5442 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
||
| 5443 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5444 | description='API.INVALID_START_TIME_OF_DAY') |
||
| 5445 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
||
| 5446 | |||
| 5447 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
||
| 5448 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
||
| 5449 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
||
| 5450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5451 | description='API.INVALID_END_TIME_OF_DAY') |
||
| 5452 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
||
| 5453 | |||
| 5454 | if 'peak_type' not in new_values['data'].keys() or \ |
||
| 5455 | not isinstance(new_values['data']['peak_type'], str) or \ |
||
| 5456 | len(str.strip(new_values['data']['peak_type'])) == 0: |
||
| 5457 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5458 | description='API.INVALID_PEAK_TYPE') |
||
| 5459 | peak_type = str.strip(new_values['data']['peak_type']) |
||
| 5460 | |||
| 5461 | if 'power' not in new_values['data'].keys() or \ |
||
| 5462 | not (isinstance(new_values['data']['power'], float) or |
||
| 5463 | isinstance(new_values['data']['power'], int)): |
||
| 5464 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5465 | description='API.INVALID_POWER') |
||
| 5466 | power = Decimal(new_values['data']['power']) |
||
| 5467 | |||
| 5468 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5469 | cursor = cnx.cursor() |
||
| 5470 | |||
| 5471 | cursor.execute(" SELECT name " |
||
| 5472 | " FROM tbl_energy_storage_containers " |
||
| 5473 | " WHERE id = %s ", (id_,)) |
||
| 5474 | if cursor.fetchone() is None: |
||
| 5475 | cursor.close() |
||
| 5476 | cnx.close() |
||
| 5477 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5478 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5479 | |||
| 5480 | cursor.execute(" SELECT id " |
||
| 5481 | " FROM tbl_energy_storage_containers_schedules " |
||
| 5482 | " WHERE id = %s ", (sid,)) |
||
| 5483 | if cursor.fetchone() is None: |
||
| 5484 | cursor.close() |
||
| 5485 | cnx.close() |
||
| 5486 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5487 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 5488 | |||
| 5489 | update_row = (" UPDATE tbl_energy_storage_containers_schedules " |
||
| 5490 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
||
| 5491 | " WHERE id = %s ") |
||
| 5492 | cursor.execute(update_row, (start_time_of_day, |
||
| 5493 | end_time_of_day, |
||
| 5494 | peak_type, |
||
| 5495 | power, |
||
| 5496 | sid)) |
||
| 5497 | cnx.commit() |
||
| 5498 | |||
| 5499 | cursor.close() |
||
| 5500 | cnx.close() |
||
| 5501 | |||
| 5502 | resp.status = falcon.HTTP_200 |
||
| 5503 | |||
| 5504 | |||
| 5505 | View Code Duplication | class EnergyStorageContainerSTSCollection: |
|
| 5506 | def __init__(self): |
||
| 5507 | pass |
||
| 5508 | |||
| 5509 | @staticmethod |
||
| 5510 | def on_options(req, resp, id_): |
||
| 5511 | _ = req |
||
| 5512 | resp.status = falcon.HTTP_200 |
||
| 5513 | _ = id_ |
||
| 5514 | |||
| 5515 | @staticmethod |
||
| 5516 | def on_get(req, resp, id_): |
||
| 5517 | access_control(req) |
||
| 5518 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5519 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5520 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5521 | |||
| 5522 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5523 | cursor = cnx.cursor() |
||
| 5524 | |||
| 5525 | cursor.execute(" SELECT name " |
||
| 5526 | " FROM tbl_energy_storage_containers " |
||
| 5527 | " WHERE id = %s ", (id_,)) |
||
| 5528 | if cursor.fetchone() is None: |
||
| 5529 | cursor.close() |
||
| 5530 | cnx.close() |
||
| 5531 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5532 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5533 | |||
| 5534 | query = (" SELECT id, name, uuid " |
||
| 5535 | " FROM tbl_energy_storage_containers_stses " |
||
| 5536 | " WHERE energy_storage_container_id = %s " |
||
| 5537 | " ORDER BY name ") |
||
| 5538 | cursor.execute(query, (id_,)) |
||
| 5539 | rows = cursor.fetchall() |
||
| 5540 | |||
| 5541 | result = list() |
||
| 5542 | if rows is not None and len(rows) > 0: |
||
| 5543 | for row in rows: |
||
| 5544 | meta_result = {"id": row[0], |
||
| 5545 | "name": row[1], |
||
| 5546 | "uuid": row[2] |
||
| 5547 | } |
||
| 5548 | result.append(meta_result) |
||
| 5549 | |||
| 5550 | resp.text = json.dumps(result) |
||
| 5551 | |||
| 5552 | @staticmethod |
||
| 5553 | @user_logger |
||
| 5554 | def on_post(req, resp, id_): |
||
| 5555 | """Handles POST requests""" |
||
| 5556 | admin_control(req) |
||
| 5557 | try: |
||
| 5558 | raw_json = req.stream.read().decode('utf-8') |
||
| 5559 | except UnicodeDecodeError as ex: |
||
| 5560 | print("Failed to decode request") |
||
| 5561 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5562 | title='API.BAD_REQUEST', |
||
| 5563 | description='API.INVALID_ENCODING') |
||
| 5564 | except Exception as ex: |
||
| 5565 | print("Unexpected error reading request stream") |
||
| 5566 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5567 | title='API.BAD_REQUEST', |
||
| 5568 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5569 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5570 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5571 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5572 | |||
| 5573 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5574 | cursor = cnx.cursor() |
||
| 5575 | |||
| 5576 | cursor.execute(" SELECT name " |
||
| 5577 | " FROM tbl_energy_storage_containers " |
||
| 5578 | " WHERE id = %s ", (id_,)) |
||
| 5579 | if cursor.fetchone() is None: |
||
| 5580 | cursor.close() |
||
| 5581 | cnx.close() |
||
| 5582 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5583 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5584 | |||
| 5585 | new_values = json.loads(raw_json) |
||
| 5586 | |||
| 5587 | if 'name' not in new_values['data'].keys() or \ |
||
| 5588 | not isinstance(new_values['data']['name'], str) or \ |
||
| 5589 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 5590 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5591 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME') |
||
| 5592 | name = str.strip(new_values['data']['name']) |
||
| 5593 | |||
| 5594 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5595 | cursor = cnx.cursor() |
||
| 5596 | |||
| 5597 | cursor.execute(" SELECT name " |
||
| 5598 | " FROM tbl_energy_storage_containers " |
||
| 5599 | " WHERE id = %s ", |
||
| 5600 | (id_,)) |
||
| 5601 | if cursor.fetchone() is None: |
||
| 5602 | cursor.close() |
||
| 5603 | cnx.close() |
||
| 5604 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5605 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5606 | |||
| 5607 | cursor.execute(" SELECT name " |
||
| 5608 | " FROM tbl_energy_storage_containers_stses " |
||
| 5609 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 5610 | (id_, name,)) |
||
| 5611 | if cursor.fetchone() is not None: |
||
| 5612 | cursor.close() |
||
| 5613 | cnx.close() |
||
| 5614 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5615 | description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE') |
||
| 5616 | |||
| 5617 | add_values = (" INSERT INTO tbl_energy_storage_containers_stses " |
||
| 5618 | " (name, uuid, energy_storage_container_id) " |
||
| 5619 | " VALUES (%s, %s, %s) ") |
||
| 5620 | cursor.execute(add_values, (name, |
||
| 5621 | str(uuid.uuid4()), |
||
| 5622 | id_ |
||
| 5623 | )) |
||
| 5624 | new_id = cursor.lastrowid |
||
| 5625 | cnx.commit() |
||
| 5626 | cursor.close() |
||
| 5627 | cnx.close() |
||
| 5628 | |||
| 5629 | resp.status = falcon.HTTP_201 |
||
| 5630 | resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id) |
||
| 5631 | |||
| 5632 | |||
| 5633 | View Code Duplication | class EnergyStorageContainerSTSItem: |
|
| 5634 | def __init__(self): |
||
| 5635 | pass |
||
| 5636 | |||
| 5637 | @staticmethod |
||
| 5638 | def on_options(req, resp, id_, fid): |
||
| 5639 | _ = req |
||
| 5640 | resp.status = falcon.HTTP_200 |
||
| 5641 | _ = id_ |
||
| 5642 | |||
| 5643 | @staticmethod |
||
| 5644 | def on_get(req, resp, id_, fid): |
||
| 5645 | access_control(req) |
||
| 5646 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5647 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5648 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5649 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5650 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5651 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID') |
||
| 5652 | |||
| 5653 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5654 | cursor = cnx.cursor() |
||
| 5655 | |||
| 5656 | cursor.execute(" SELECT name " |
||
| 5657 | " FROM tbl_energy_storage_containers " |
||
| 5658 | " WHERE id = %s ", (id_,)) |
||
| 5659 | if cursor.fetchone() is None: |
||
| 5660 | cursor.close() |
||
| 5661 | cnx.close() |
||
| 5662 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5663 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5664 | |||
| 5665 | query = (" SELECT id, name, uuid " |
||
| 5666 | " FROM tbl_energy_storage_containers ") |
||
| 5667 | cursor.execute(query) |
||
| 5668 | rows_energystoragecontainers = cursor.fetchall() |
||
| 5669 | |||
| 5670 | energy_storage_container_dict = dict() |
||
| 5671 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 5672 | for row in rows_energystoragecontainers: |
||
| 5673 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 5674 | "name": row[1], |
||
| 5675 | "uuid": row[2]} |
||
| 5676 | |||
| 5677 | query = (" SELECT id, name, uuid " |
||
| 5678 | " FROM tbl_energy_storage_containers_stses " |
||
| 5679 | " WHERE id = %s ") |
||
| 5680 | cursor.execute(query, (fid,)) |
||
| 5681 | row = cursor.fetchone() |
||
| 5682 | cursor.close() |
||
| 5683 | cnx.close() |
||
| 5684 | |||
| 5685 | if row is None: |
||
| 5686 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5687 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5688 | else: |
||
| 5689 | meta_result = {"id": row[0], |
||
| 5690 | "name": row[1], |
||
| 5691 | "uuid": row[2] |
||
| 5692 | } |
||
| 5693 | |||
| 5694 | resp.text = json.dumps(meta_result) |
||
| 5695 | |||
| 5696 | @staticmethod |
||
| 5697 | @user_logger |
||
| 5698 | def on_delete(req, resp, id_, fid): |
||
| 5699 | admin_control(req) |
||
| 5700 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5701 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5702 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5703 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5704 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5705 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID') |
||
| 5706 | |||
| 5707 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5708 | cursor = cnx.cursor() |
||
| 5709 | |||
| 5710 | cursor.execute(" SELECT name " |
||
| 5711 | " FROM tbl_energy_storage_containers " |
||
| 5712 | " WHERE id = %s ", (id_,)) |
||
| 5713 | if cursor.fetchone() is None: |
||
| 5714 | cursor.close() |
||
| 5715 | cnx.close() |
||
| 5716 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5717 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5718 | |||
| 5719 | cursor.execute(" SELECT name " |
||
| 5720 | " FROM tbl_energy_storage_containers_stses " |
||
| 5721 | " WHERE id = %s ", (fid,)) |
||
| 5722 | if cursor.fetchone() is None: |
||
| 5723 | cursor.close() |
||
| 5724 | cnx.close() |
||
| 5725 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5726 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5727 | |||
| 5728 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses " |
||
| 5729 | " WHERE id = %s ", (fid,)) |
||
| 5730 | cnx.commit() |
||
| 5731 | |||
| 5732 | cursor.close() |
||
| 5733 | cnx.close() |
||
| 5734 | |||
| 5735 | resp.status = falcon.HTTP_204 |
||
| 5736 | |||
| 5737 | @staticmethod |
||
| 5738 | @user_logger |
||
| 5739 | def on_put(req, resp, id_, fid): |
||
| 5740 | """Handles PUT requests""" |
||
| 5741 | admin_control(req) |
||
| 5742 | try: |
||
| 5743 | raw_json = req.stream.read().decode('utf-8') |
||
| 5744 | except UnicodeDecodeError as ex: |
||
| 5745 | print("Failed to decode request") |
||
| 5746 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5747 | title='API.BAD_REQUEST', |
||
| 5748 | description='API.INVALID_ENCODING') |
||
| 5749 | except Exception as ex: |
||
| 5750 | print("Unexpected error reading request stream") |
||
| 5751 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5752 | title='API.BAD_REQUEST', |
||
| 5753 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5754 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5755 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5756 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5757 | |||
| 5758 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5759 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5760 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID') |
||
| 5761 | |||
| 5762 | new_values = json.loads(raw_json) |
||
| 5763 | |||
| 5764 | if 'name' not in new_values['data'].keys() or \ |
||
| 5765 | not isinstance(new_values['data']['name'], str) or \ |
||
| 5766 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 5767 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5768 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME') |
||
| 5769 | name = str.strip(new_values['data']['name']) |
||
| 5770 | |||
| 5771 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5772 | cursor = cnx.cursor() |
||
| 5773 | |||
| 5774 | cursor.execute(" SELECT name " |
||
| 5775 | " FROM tbl_energy_storage_containers " |
||
| 5776 | " WHERE id = %s ", (id_,)) |
||
| 5777 | if cursor.fetchone() is None: |
||
| 5778 | cursor.close() |
||
| 5779 | cnx.close() |
||
| 5780 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5781 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 5782 | |||
| 5783 | cursor.execute(" SELECT name " |
||
| 5784 | " FROM tbl_energy_storage_containers_stses " |
||
| 5785 | " WHERE id = %s ", (fid,)) |
||
| 5786 | if cursor.fetchone() is None: |
||
| 5787 | cursor.close() |
||
| 5788 | cnx.close() |
||
| 5789 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5790 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5791 | |||
| 5792 | cursor.execute(" SELECT name " |
||
| 5793 | " FROM tbl_energy_storage_containers_stses " |
||
| 5794 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 5795 | (id_, name, fid)) |
||
| 5796 | if cursor.fetchone() is not None: |
||
| 5797 | cursor.close() |
||
| 5798 | cnx.close() |
||
| 5799 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5800 | description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE') |
||
| 5801 | |||
| 5802 | update_row = (" UPDATE tbl_energy_storage_containers_stses " |
||
| 5803 | " SET name = %s " |
||
| 5804 | " WHERE id = %s ") |
||
| 5805 | cursor.execute(update_row, (name, |
||
| 5806 | fid)) |
||
| 5807 | cnx.commit() |
||
| 5808 | |||
| 5809 | cursor.close() |
||
| 5810 | cnx.close() |
||
| 5811 | |||
| 5812 | resp.status = falcon.HTTP_200 |
||
| 5813 | |||
| 5814 | |||
| 5815 | View Code Duplication | class EnergyStorageContainerSTSPointCollection: |
|
| 5816 | def __init__(self): |
||
| 5817 | pass |
||
| 5818 | |||
| 5819 | @staticmethod |
||
| 5820 | def on_options(req, resp, id_, fid): |
||
| 5821 | _ = req |
||
| 5822 | resp.status = falcon.HTTP_200 |
||
| 5823 | _ = id_ |
||
| 5824 | |||
| 5825 | @staticmethod |
||
| 5826 | def on_get(req, resp, id_, fid): |
||
| 5827 | if 'API-KEY' not in req.headers or \ |
||
| 5828 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5829 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5830 | access_control(req) |
||
| 5831 | else: |
||
| 5832 | api_key_control(req) |
||
| 5833 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5834 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5835 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5836 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5837 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5838 | description='API.INVALID_STS_ID') |
||
| 5839 | |||
| 5840 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5841 | cursor = cnx.cursor() |
||
| 5842 | |||
| 5843 | cursor.execute(" SELECT name " |
||
| 5844 | " FROM tbl_energy_storage_containers_stses " |
||
| 5845 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, )) |
||
| 5846 | if cursor.fetchone() is None: |
||
| 5847 | cursor.close() |
||
| 5848 | cnx.close() |
||
| 5849 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5850 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5851 | |||
| 5852 | query = (" SELECT p.id, p.name, " |
||
| 5853 | " ds.id, ds.name, ds.uuid, " |
||
| 5854 | " p.address " |
||
| 5855 | " FROM tbl_points p, tbl_energy_storage_containers_stses_points mp, tbl_data_sources ds " |
||
| 5856 | " WHERE mp.sts_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 5857 | " ORDER BY p.name ") |
||
| 5858 | cursor.execute(query, (fid,)) |
||
| 5859 | rows = cursor.fetchall() |
||
| 5860 | |||
| 5861 | result = list() |
||
| 5862 | if rows is not None and len(rows) > 0: |
||
| 5863 | for row in rows: |
||
| 5864 | meta_result = {"id": row[0], "name": row[1], |
||
| 5865 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 5866 | "address": row[5]} |
||
| 5867 | result.append(meta_result) |
||
| 5868 | |||
| 5869 | resp.text = json.dumps(result) |
||
| 5870 | |||
| 5871 | @staticmethod |
||
| 5872 | @user_logger |
||
| 5873 | def on_post(req, resp, id_, fid): |
||
| 5874 | """Handles POST requests""" |
||
| 5875 | admin_control(req) |
||
| 5876 | try: |
||
| 5877 | raw_json = req.stream.read().decode('utf-8') |
||
| 5878 | except UnicodeDecodeError as ex: |
||
| 5879 | print("Failed to decode request") |
||
| 5880 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5881 | title='API.BAD_REQUEST', |
||
| 5882 | description='API.INVALID_ENCODING') |
||
| 5883 | except Exception as ex: |
||
| 5884 | print("Unexpected error reading request stream") |
||
| 5885 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5886 | title='API.BAD_REQUEST', |
||
| 5887 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5888 | |||
| 5889 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5890 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5891 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5892 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5893 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5894 | description='API.INVALID_STS_ID') |
||
| 5895 | |||
| 5896 | new_values = json.loads(raw_json) |
||
| 5897 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5898 | cursor = cnx.cursor() |
||
| 5899 | |||
| 5900 | cursor.execute(" SELECT name " |
||
| 5901 | " FROM tbl_energy_storage_containers_stses " |
||
| 5902 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
||
| 5903 | if cursor.fetchone() is None: |
||
| 5904 | cursor.close() |
||
| 5905 | cnx.close() |
||
| 5906 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5907 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5908 | |||
| 5909 | cursor.execute(" SELECT name, object_type " |
||
| 5910 | " FROM tbl_points " |
||
| 5911 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 5912 | row = cursor.fetchone() |
||
| 5913 | if row is None: |
||
| 5914 | cursor.close() |
||
| 5915 | cnx.close() |
||
| 5916 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5917 | description='API.POINT_NOT_FOUND') |
||
| 5918 | |||
| 5919 | query = (" SELECT id " |
||
| 5920 | " FROM tbl_energy_storage_containers_stses_points " |
||
| 5921 | " WHERE sts_id = %s AND point_id = %s") |
||
| 5922 | cursor.execute(query, (fid, new_values['data']['point_id'],)) |
||
| 5923 | if cursor.fetchone() is not None: |
||
| 5924 | cursor.close() |
||
| 5925 | cnx.close() |
||
| 5926 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5927 | description='API.STS_POINT_RELATION_EXISTS') |
||
| 5928 | |||
| 5929 | add_row = (" INSERT INTO tbl_energy_storage_containers_stses_points (sts_id, point_id) " |
||
| 5930 | " VALUES (%s, %s) ") |
||
| 5931 | cursor.execute(add_row, (fid, new_values['data']['point_id'],)) |
||
| 5932 | cnx.commit() |
||
| 5933 | cursor.close() |
||
| 5934 | cnx.close() |
||
| 5935 | |||
| 5936 | resp.status = falcon.HTTP_201 |
||
| 5937 | resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(fid) + '/points/' + \ |
||
| 5938 | str(new_values['data']['point_id']) |
||
| 5939 | |||
| 5940 | |||
| 5941 | View Code Duplication | class EnergyStorageContainerSTSPointItem: |
|
| 5942 | def __init__(self): |
||
| 5943 | pass |
||
| 5944 | |||
| 5945 | @staticmethod |
||
| 5946 | def on_options(req, resp, id_, fid, pid): |
||
| 5947 | _ = req |
||
| 5948 | resp.status = falcon.HTTP_200 |
||
| 5949 | _ = id_ |
||
| 5950 | |||
| 5951 | @staticmethod |
||
| 5952 | @user_logger |
||
| 5953 | def on_delete(req, resp, id_, fid, pid): |
||
| 5954 | """Handles DELETE requests""" |
||
| 5955 | admin_control(req) |
||
| 5956 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5957 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5958 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 5959 | if not fid.isdigit() or int(fid) <= 0: |
||
| 5960 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5961 | description='API.INVALID_STS_ID') |
||
| 5962 | if not pid.isdigit() or int(pid) <= 0: |
||
| 5963 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5964 | description='API.INVALID_POINT_ID') |
||
| 5965 | |||
| 5966 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5967 | cursor = cnx.cursor() |
||
| 5968 | |||
| 5969 | cursor.execute(" SELECT name " |
||
| 5970 | " FROM tbl_energy_storage_containers_stses " |
||
| 5971 | " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,)) |
||
| 5972 | if cursor.fetchone() is None: |
||
| 5973 | cursor.close() |
||
| 5974 | cnx.close() |
||
| 5975 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5976 | description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND') |
||
| 5977 | |||
| 5978 | cursor.execute(" SELECT name " |
||
| 5979 | " FROM tbl_points " |
||
| 5980 | " WHERE id = %s ", (pid,)) |
||
| 5981 | if cursor.fetchone() is None: |
||
| 5982 | cursor.close() |
||
| 5983 | cnx.close() |
||
| 5984 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5985 | description='API.POINT_NOT_FOUND') |
||
| 5986 | |||
| 5987 | cursor.execute(" SELECT id " |
||
| 5988 | " FROM tbl_energy_storage_containers_stses_points " |
||
| 5989 | " WHERE sts_id = %s AND point_id = %s ", (fid, pid)) |
||
| 5990 | if cursor.fetchone() is None: |
||
| 5991 | cursor.close() |
||
| 5992 | cnx.close() |
||
| 5993 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5994 | description='API.STS_POINT_RELATION_NOT_FOUND') |
||
| 5995 | |||
| 5996 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses_points " |
||
| 5997 | " WHERE sts_id = %s AND point_id = %s ", (fid, pid)) |
||
| 5998 | cnx.commit() |
||
| 5999 | |||
| 6000 | cursor.close() |
||
| 6001 | cnx.close() |
||
| 6002 | |||
| 6003 | resp.status = falcon.HTTP_204 |
||
| 6004 | |||
| 6005 | |||
| 6006 | View Code Duplication | class EnergyStorageContainerClone: |
|
| 6007 | def __init__(self): |
||
| 6008 | pass |
||
| 6009 | |||
| 6010 | @staticmethod |
||
| 6011 | def on_options(req, resp, id_): |
||
| 6012 | _ = req |
||
| 6013 | resp.status = falcon.HTTP_200 |
||
| 6014 | _ = id_ |
||
| 6015 | |||
| 6016 | @staticmethod |
||
| 6017 | @user_logger |
||
| 6018 | def on_post(req, resp, id_): |
||
| 6019 | """Handles POST requests""" |
||
| 6020 | admin_control(req) |
||
| 6021 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6022 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6023 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 6024 | |||
| 6025 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6026 | cursor = cnx.cursor() |
||
| 6027 | |||
| 6028 | query = (" SELECT id, name, uuid, " |
||
| 6029 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 6030 | " FROM tbl_energy_storage_containers " |
||
| 6031 | " WHERE id = %s ") |
||
| 6032 | cursor.execute(query, (id_,)) |
||
| 6033 | row = cursor.fetchone() |
||
| 6034 | |||
| 6035 | if row is None: |
||
| 6036 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6037 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 6038 | else: |
||
| 6039 | meta_result = {"id": row[0], |
||
| 6040 | "name": row[1], |
||
| 6041 | "uuid": row[2], |
||
| 6042 | "rated_capacity": row[3], |
||
| 6043 | "rated_power": row[4], |
||
| 6044 | "contact_id": row[5], |
||
| 6045 | "cost_center_id": row[6], |
||
| 6046 | "description": row[7]} |
||
| 6047 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 6048 | if config.utc_offset[0] == '-': |
||
| 6049 | timezone_offset = -timezone_offset |
||
| 6050 | new_name = str.strip(meta_result['name']) + \ |
||
| 6051 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
| 6052 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
||
| 6053 | " (name, uuid, rated_capacity, rated_power, contact_id, " |
||
| 6054 | " cost_center_id, description) " |
||
| 6055 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 6056 | cursor.execute(add_values, (new_name, |
||
| 6057 | str(uuid.uuid4()), |
||
| 6058 | meta_result['rated_capacity'], |
||
| 6059 | meta_result['rated_power'], |
||
| 6060 | meta_result['contact_id'], |
||
| 6061 | meta_result['cost_center_id'], |
||
| 6062 | meta_result['description'])) |
||
| 6063 | new_id = cursor.lastrowid |
||
| 6064 | cnx.commit() |
||
| 6065 | cursor.close() |
||
| 6066 | cnx.close() |
||
| 6067 | |||
| 6068 | resp.status = falcon.HTTP_201 |
||
| 6069 | resp.location = '/energystoragecontainers/' + str(new_id) |
||
| 6070 | |||
| 6071 | |||
| 6072 | class EnergyStorageContainerExport: |
||
| 6073 | def __init__(self): |
||
| 6074 | pass |
||
| 6075 | |||
| 6076 | @staticmethod |
||
| 6077 | def on_options(req, resp, id_): |
||
| 6078 | _ = req |
||
| 6079 | resp.status = falcon.HTTP_200 |
||
| 6080 | _ = id_ |
||
| 6081 | |||
| 6082 | View Code Duplication | @staticmethod |
|
| 6083 | def on_get(req, resp, id_): |
||
| 6084 | access_control(req) |
||
| 6085 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6086 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6087 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 6088 | |||
| 6089 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6090 | cursor = cnx.cursor() |
||
| 6091 | |||
| 6092 | query = (" SELECT id, name, uuid " |
||
| 6093 | " FROM tbl_contacts ") |
||
| 6094 | cursor.execute(query) |
||
| 6095 | rows_contacts = cursor.fetchall() |
||
| 6096 | |||
| 6097 | contact_dict = dict() |
||
| 6098 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 6099 | for row in rows_contacts: |
||
| 6100 | contact_dict[row[0]] = {"id": row[0], |
||
| 6101 | "name": row[1], |
||
| 6102 | "uuid": row[2]} |
||
| 6103 | |||
| 6104 | query = (" SELECT id, name, uuid " |
||
| 6105 | " FROM tbl_cost_centers ") |
||
| 6106 | cursor.execute(query) |
||
| 6107 | rows_cost_centers = cursor.fetchall() |
||
| 6108 | |||
| 6109 | cost_center_dict = dict() |
||
| 6110 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 6111 | for row in rows_cost_centers: |
||
| 6112 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 6113 | "name": row[1], |
||
| 6114 | "uuid": row[2]} |
||
| 6115 | |||
| 6116 | query = (" SELECT id, name, uuid, " |
||
| 6117 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 6118 | " FROM tbl_energy_storage_containers " |
||
| 6119 | " WHERE id = %s ") |
||
| 6120 | cursor.execute(query, (id_,)) |
||
| 6121 | row = cursor.fetchone() |
||
| 6122 | cursor.close() |
||
| 6123 | cnx.close() |
||
| 6124 | |||
| 6125 | if row is None: |
||
| 6126 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6127 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 6128 | else: |
||
| 6129 | meta_result = {"id": row[0], |
||
| 6130 | "name": row[1], |
||
| 6131 | "uuid": row[2], |
||
| 6132 | "rated_capacity": row[3], |
||
| 6133 | "rated_power": row[4], |
||
| 6134 | "contact": contact_dict.get(row[5], None), |
||
| 6135 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 6136 | "description": row[7]} |
||
| 6137 | |||
| 6138 | resp.text = json.dumps(meta_result) |
||
| 6139 | |||
| 6140 | |||
| 6141 | class EnergyStorageContainerImport: |
||
| 6142 | def __init__(self): |
||
| 6143 | pass |
||
| 6144 | |||
| 6145 | @staticmethod |
||
| 6146 | def on_options(req, resp): |
||
| 6147 | _ = req |
||
| 6148 | resp.status = falcon.HTTP_200 |
||
| 6149 | |||
| 6150 | @staticmethod |
||
| 6151 | @user_logger |
||
| 6152 | def on_post(req, resp): |
||
| 6153 | """Handles POST requests""" |
||
| 6154 | admin_control(req) |
||
| 6155 | try: |
||
| 6156 | raw_json = req.stream.read().decode('utf-8') |
||
| 6157 | except UnicodeDecodeError as ex: |
||
| 6158 | print("Failed to decode request") |
||
| 6159 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6160 | title='API.BAD_REQUEST', |
||
| 6161 | description='API.INVALID_ENCODING') |
||
| 6162 | except Exception as ex: |
||
| 6163 | print("Unexpected error reading request stream") |
||
| 6164 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6165 | title='API.BAD_REQUEST', |
||
| 6166 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6167 | |||
| 6168 | new_values = json.loads(raw_json) |
||
| 6169 | |||
| 6170 | if 'name' not in new_values.keys() or \ |
||
| 6171 | not isinstance(new_values['name'], str) or \ |
||
| 6172 | len(str.strip(new_values['name'])) == 0: |
||
| 6173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6174 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME') |
||
| 6175 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 6176 | if config.utc_offset[0] == '-': |
||
| 6177 | timezone_offset = -timezone_offset |
||
| 6178 | name = str.strip(new_values['name']) |
||
| 6179 | |||
| 6180 | if 'rated_capacity' not in new_values.keys() or \ |
||
| 6181 | not (isinstance(new_values['rated_capacity'], float) or |
||
| 6182 | isinstance(new_values['rated_capacity'], int)) or \ |
||
| 6183 | new_values['rated_capacity'] < 0.0: |
||
| 6184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6185 | description='API.INVALID_RATED_CAPACITY') |
||
| 6186 | rated_capacity = new_values['rated_capacity'] |
||
| 6187 | |||
| 6188 | if 'rated_power' not in new_values.keys() or \ |
||
| 6189 | not (isinstance(new_values['rated_power'], float) or |
||
| 6190 | isinstance(new_values['rated_power'], int)) or \ |
||
| 6191 | new_values['rated_power'] < 0.0: |
||
| 6192 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6193 | description='API.INVALID_RATED_POWER') |
||
| 6194 | rated_power = new_values['rated_power'] |
||
| 6195 | |||
| 6196 | if 'contact' not in new_values.keys() or \ |
||
| 6197 | 'id' not in new_values['contact'].keys() or \ |
||
| 6198 | not isinstance(new_values['contact']['id'], int) or \ |
||
| 6199 | new_values['contact']['id'] <= 0: |
||
| 6200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6201 | description='API.INVALID_CONTACT_ID') |
||
| 6202 | contact_id = new_values['contact']['id'] |
||
| 6203 | |||
| 6204 | if 'cost_center' not in new_values.keys() or \ |
||
| 6205 | 'id' not in new_values['cost_center'].keys() or \ |
||
| 6206 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 6207 | new_values['cost_center']['id'] <= 0: |
||
| 6208 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6209 | description='API.INVALID_COST_CENTER_ID') |
||
| 6210 | cost_center_id = new_values['cost_center']['id'] |
||
| 6211 | |||
| 6212 | if 'description' in new_values.keys() and \ |
||
| 6213 | new_values['description'] is not None and \ |
||
| 6214 | len(str(new_values['description'])) > 0: |
||
| 6215 | description = str.strip(new_values['description']) |
||
| 6216 | else: |
||
| 6217 | description = None |
||
| 6218 | |||
| 6219 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6220 | cursor = cnx.cursor() |
||
| 6221 | |||
| 6222 | cursor.execute(" SELECT name " |
||
| 6223 | " FROM tbl_energy_storage_containers " |
||
| 6224 | " WHERE name = %s ", (name,)) |
||
| 6225 | if cursor.fetchone() is not None: |
||
| 6226 | cursor.close() |
||
| 6227 | cnx.close() |
||
| 6228 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6229 | description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE') |
||
| 6230 | |||
| 6231 | cursor.execute(" SELECT name " |
||
| 6232 | " FROM tbl_contacts " |
||
| 6233 | " WHERE id = %s ", |
||
| 6234 | (new_values['contact']['id'],)) |
||
| 6235 | row = cursor.fetchone() |
||
| 6236 | if row is None: |
||
| 6237 | cursor.close() |
||
| 6238 | cnx.close() |
||
| 6239 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6240 | description='API.CONTACT_NOT_FOUND') |
||
| 6241 | |||
| 6242 | cursor.execute(" SELECT name " |
||
| 6243 | " FROM tbl_cost_centers " |
||
| 6244 | " WHERE id = %s ", |
||
| 6245 | (new_values['cost_center']['id'],)) |
||
| 6246 | row = cursor.fetchone() |
||
| 6247 | if row is None: |
||
| 6248 | cursor.close() |
||
| 6249 | cnx.close() |
||
| 6250 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6251 | description='API.COST_CENTER_NOT_FOUND') |
||
| 6252 | |||
| 6253 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
||
| 6254 | " (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, description) " |
||
| 6255 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 6256 | cursor.execute(add_values, (name, |
||
| 6257 | str(uuid.uuid4()), |
||
| 6258 | rated_capacity, |
||
| 6259 | rated_power, |
||
| 6260 | contact_id, |
||
| 6261 | cost_center_id, |
||
| 6262 | description)) |
||
| 6263 | new_id = cursor.lastrowid |
||
| 6264 | cnx.commit() |
||
| 6265 | cursor.close() |
||
| 6266 | cnx.close() |
||
| 6267 | |||
| 6268 | resp.status = falcon.HTTP_201 |
||
| 6269 | resp.location = '/energystoragecontainers/' + str(new_id) |
||
| 6270 |