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