| Total Complexity | 915 |
| Total Lines | 4417 |
| Duplicated Lines | 57.17 % |
| 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 | 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 | 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 EnergyStorageContainerFirecontrolCollection: |
|
| 1961 | def __init__(self): |
||
| 1962 | """Initializes Class""" |
||
| 1963 | pass |
||
| 1964 | |||
| 1965 | @staticmethod |
||
| 1966 | def on_options(req, resp, id_): |
||
| 1967 | resp.status = falcon.HTTP_200 |
||
| 1968 | |||
| 1969 | @staticmethod |
||
| 1970 | def on_get(req, resp, id_): |
||
| 1971 | access_control(req) |
||
| 1972 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1973 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1974 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1975 | |||
| 1976 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1977 | cursor = cnx.cursor() |
||
| 1978 | |||
| 1979 | cursor.execute(" SELECT name " |
||
| 1980 | " FROM tbl_energy_storage_containers " |
||
| 1981 | " WHERE id = %s ", (id_,)) |
||
| 1982 | if cursor.fetchone() is None: |
||
| 1983 | cursor.close() |
||
| 1984 | cnx.close() |
||
| 1985 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1986 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 1987 | |||
| 1988 | query = (" SELECT id, name, uuid " |
||
| 1989 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 1990 | " WHERE energy_storage_container_id = %s " |
||
| 1991 | " ORDER BY name ") |
||
| 1992 | cursor.execute(query, (id_,)) |
||
| 1993 | rows = cursor.fetchall() |
||
| 1994 | |||
| 1995 | result = list() |
||
| 1996 | if rows is not None and len(rows) > 0: |
||
| 1997 | for row in rows: |
||
| 1998 | meta_result = {"id": row[0], |
||
| 1999 | "name": row[1], |
||
| 2000 | "uuid": row[2] |
||
| 2001 | } |
||
| 2002 | result.append(meta_result) |
||
| 2003 | |||
| 2004 | resp.text = json.dumps(result) |
||
| 2005 | |||
| 2006 | @staticmethod |
||
| 2007 | @user_logger |
||
| 2008 | def on_post(req, resp, id_): |
||
| 2009 | """Handles POST requests""" |
||
| 2010 | admin_control(req) |
||
| 2011 | try: |
||
| 2012 | raw_json = req.stream.read().decode('utf-8') |
||
| 2013 | except Exception as ex: |
||
| 2014 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2015 | title='API.BAD_REQUEST', |
||
| 2016 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2017 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2019 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2020 | |||
| 2021 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2022 | cursor = cnx.cursor() |
||
| 2023 | |||
| 2024 | cursor.execute(" SELECT name " |
||
| 2025 | " FROM tbl_energy_storage_containers " |
||
| 2026 | " WHERE id = %s ", (id_,)) |
||
| 2027 | if cursor.fetchone() is None: |
||
| 2028 | cursor.close() |
||
| 2029 | cnx.close() |
||
| 2030 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2031 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2032 | |||
| 2033 | new_values = json.loads(raw_json) |
||
| 2034 | |||
| 2035 | if 'name' not in new_values['data'].keys() or \ |
||
| 2036 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2037 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2038 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2039 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME') |
||
| 2040 | name = str.strip(new_values['data']['name']) |
||
| 2041 | |||
| 2042 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2043 | cursor = cnx.cursor() |
||
| 2044 | |||
| 2045 | cursor.execute(" SELECT name " |
||
| 2046 | " FROM tbl_energy_storage_containers " |
||
| 2047 | " WHERE id = %s ", |
||
| 2048 | (id_,)) |
||
| 2049 | if cursor.fetchone() is None: |
||
| 2050 | cursor.close() |
||
| 2051 | cnx.close() |
||
| 2052 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2053 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2054 | |||
| 2055 | cursor.execute(" SELECT name " |
||
| 2056 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2057 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 2058 | (id_, name,)) |
||
| 2059 | if cursor.fetchone() is not None: |
||
| 2060 | cursor.close() |
||
| 2061 | cnx.close() |
||
| 2062 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2063 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE') |
||
| 2064 | |||
| 2065 | add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols " |
||
| 2066 | " (name, uuid, energy_storage_container_id) " |
||
| 2067 | " VALUES (%s, %s, %s) ") |
||
| 2068 | cursor.execute(add_values, (name, |
||
| 2069 | str(uuid.uuid4()), |
||
| 2070 | id_ |
||
| 2071 | )) |
||
| 2072 | new_id = cursor.lastrowid |
||
| 2073 | cnx.commit() |
||
| 2074 | cursor.close() |
||
| 2075 | cnx.close() |
||
| 2076 | |||
| 2077 | resp.status = falcon.HTTP_201 |
||
| 2078 | resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id) |
||
| 2079 | |||
| 2080 | |||
| 2081 | View Code Duplication | class EnergyStorageContainerFirecontrolItem: |
|
| 2082 | def __init__(self): |
||
| 2083 | """Initializes Class""" |
||
| 2084 | pass |
||
| 2085 | |||
| 2086 | @staticmethod |
||
| 2087 | def on_options(req, resp, id_, fid): |
||
| 2088 | resp.status = falcon.HTTP_200 |
||
| 2089 | |||
| 2090 | @staticmethod |
||
| 2091 | def on_get(req, resp, id_, fid): |
||
| 2092 | access_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 fid.isdigit() or int(fid) <= 0: |
||
| 2097 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2098 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2099 | |||
| 2100 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2101 | cursor = cnx.cursor() |
||
| 2102 | |||
| 2103 | cursor.execute(" SELECT name " |
||
| 2104 | " FROM tbl_energy_storage_containers " |
||
| 2105 | " WHERE id = %s ", (id_,)) |
||
| 2106 | if cursor.fetchone() is None: |
||
| 2107 | cursor.close() |
||
| 2108 | cnx.close() |
||
| 2109 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2110 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2111 | |||
| 2112 | query = (" SELECT id, name, uuid " |
||
| 2113 | " FROM tbl_energy_storage_containers ") |
||
| 2114 | cursor.execute(query) |
||
| 2115 | rows_energystoragecontainers = cursor.fetchall() |
||
| 2116 | |||
| 2117 | energy_storage_container_dict = dict() |
||
| 2118 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 2119 | for row in rows_energystoragecontainers: |
||
| 2120 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 2121 | "name": row[1], |
||
| 2122 | "uuid": row[2]} |
||
| 2123 | |||
| 2124 | query = (" SELECT id, name, uuid " |
||
| 2125 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2126 | " WHERE id = %s ") |
||
| 2127 | cursor.execute(query, (fid,)) |
||
| 2128 | row = cursor.fetchone() |
||
| 2129 | cursor.close() |
||
| 2130 | cnx.close() |
||
| 2131 | |||
| 2132 | if row is None: |
||
| 2133 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2134 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2135 | else: |
||
| 2136 | meta_result = {"id": row[0], |
||
| 2137 | "name": row[1], |
||
| 2138 | "uuid": row[2] |
||
| 2139 | } |
||
| 2140 | |||
| 2141 | resp.text = json.dumps(meta_result) |
||
| 2142 | |||
| 2143 | @staticmethod |
||
| 2144 | @user_logger |
||
| 2145 | def on_delete(req, resp, id_, fid): |
||
| 2146 | admin_control(req) |
||
| 2147 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2148 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2149 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2150 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2151 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2152 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2153 | |||
| 2154 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2155 | cursor = cnx.cursor() |
||
| 2156 | |||
| 2157 | cursor.execute(" SELECT name " |
||
| 2158 | " FROM tbl_energy_storage_containers " |
||
| 2159 | " WHERE id = %s ", (id_,)) |
||
| 2160 | if cursor.fetchone() is None: |
||
| 2161 | cursor.close() |
||
| 2162 | cnx.close() |
||
| 2163 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2164 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2165 | |||
| 2166 | cursor.execute(" SELECT name " |
||
| 2167 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2168 | " WHERE id = %s ", (fid,)) |
||
| 2169 | if cursor.fetchone() is None: |
||
| 2170 | cursor.close() |
||
| 2171 | cnx.close() |
||
| 2172 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2173 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2174 | |||
| 2175 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols " |
||
| 2176 | " WHERE id = %s ", (fid,)) |
||
| 2177 | cnx.commit() |
||
| 2178 | |||
| 2179 | cursor.close() |
||
| 2180 | cnx.close() |
||
| 2181 | |||
| 2182 | resp.status = falcon.HTTP_204 |
||
| 2183 | |||
| 2184 | @staticmethod |
||
| 2185 | @user_logger |
||
| 2186 | def on_put(req, resp, id_, fid): |
||
| 2187 | """Handles PUT requests""" |
||
| 2188 | admin_control(req) |
||
| 2189 | try: |
||
| 2190 | raw_json = req.stream.read().decode('utf-8') |
||
| 2191 | except Exception as ex: |
||
| 2192 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2193 | title='API.BAD_REQUEST', |
||
| 2194 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2195 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2196 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2197 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2198 | |||
| 2199 | if not fid.isdigit() or int(fid) <= 0: |
||
| 2200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2201 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
||
| 2202 | |||
| 2203 | new_values = json.loads(raw_json) |
||
| 2204 | |||
| 2205 | if 'name' not in new_values['data'].keys() or \ |
||
| 2206 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2207 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2208 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2209 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME') |
||
| 2210 | name = str.strip(new_values['data']['name']) |
||
| 2211 | |||
| 2212 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2213 | cursor = cnx.cursor() |
||
| 2214 | |||
| 2215 | cursor.execute(" SELECT name " |
||
| 2216 | " FROM tbl_energy_storage_containers " |
||
| 2217 | " WHERE id = %s ", (id_,)) |
||
| 2218 | if cursor.fetchone() is None: |
||
| 2219 | cursor.close() |
||
| 2220 | cnx.close() |
||
| 2221 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2222 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2223 | |||
| 2224 | cursor.execute(" SELECT name " |
||
| 2225 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2226 | " WHERE id = %s ", (fid,)) |
||
| 2227 | if cursor.fetchone() is None: |
||
| 2228 | cursor.close() |
||
| 2229 | cnx.close() |
||
| 2230 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2231 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
||
| 2232 | |||
| 2233 | cursor.execute(" SELECT name " |
||
| 2234 | " FROM tbl_energy_storage_containers_firecontrols " |
||
| 2235 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 2236 | (id_, name, fid)) |
||
| 2237 | if cursor.fetchone() is not None: |
||
| 2238 | cursor.close() |
||
| 2239 | cnx.close() |
||
| 2240 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2241 | description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE') |
||
| 2242 | |||
| 2243 | update_row = (" UPDATE tbl_energy_storage_containers_firecontrols " |
||
| 2244 | " SET name = %s " |
||
| 2245 | " WHERE id = %s ") |
||
| 2246 | cursor.execute(update_row, (name, |
||
| 2247 | fid)) |
||
| 2248 | cnx.commit() |
||
| 2249 | |||
| 2250 | cursor.close() |
||
| 2251 | cnx.close() |
||
| 2252 | |||
| 2253 | resp.status = falcon.HTTP_200 |
||
| 2254 | |||
| 2255 | |||
| 2256 | View Code Duplication | class EnergyStorageContainerGridCollection: |
|
| 2257 | def __init__(self): |
||
| 2258 | """Initializes Class""" |
||
| 2259 | pass |
||
| 2260 | |||
| 2261 | @staticmethod |
||
| 2262 | def on_options(req, resp, id_): |
||
| 2263 | resp.status = falcon.HTTP_200 |
||
| 2264 | |||
| 2265 | @staticmethod |
||
| 2266 | def on_get(req, resp, id_): |
||
| 2267 | access_control(req) |
||
| 2268 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2269 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2270 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2271 | |||
| 2272 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2273 | cursor = cnx.cursor() |
||
| 2274 | |||
| 2275 | cursor.execute(" SELECT name " |
||
| 2276 | " FROM tbl_energy_storage_containers " |
||
| 2277 | " WHERE id = %s ", (id_,)) |
||
| 2278 | if cursor.fetchone() is None: |
||
| 2279 | cursor.close() |
||
| 2280 | cnx.close() |
||
| 2281 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2282 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2283 | |||
| 2284 | # query meter dict |
||
| 2285 | query = (" SELECT id, name, uuid " |
||
| 2286 | " FROM tbl_meters ") |
||
| 2287 | cursor.execute(query) |
||
| 2288 | rows_meters = cursor.fetchall() |
||
| 2289 | |||
| 2290 | meter_dict = dict() |
||
| 2291 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2292 | for row in rows_meters: |
||
| 2293 | meter_dict[row[0]] = {"id": row[0], |
||
| 2294 | "name": row[1], |
||
| 2295 | "uuid": row[2]} |
||
| 2296 | # query point dict |
||
| 2297 | query = (" SELECT id, name " |
||
| 2298 | " FROM tbl_points ") |
||
| 2299 | cursor.execute(query) |
||
| 2300 | rows_points = cursor.fetchall() |
||
| 2301 | |||
| 2302 | point_dict = dict() |
||
| 2303 | if rows_points is not None and len(rows_points) > 0: |
||
| 2304 | for row in rows_points: |
||
| 2305 | point_dict[row[0]] = {"id": row[0], |
||
| 2306 | "name": row[1]} |
||
| 2307 | |||
| 2308 | query = (" SELECT id, name, uuid, " |
||
| 2309 | " power_point_id, buy_meter_id, sell_meter_id, capacity " |
||
| 2310 | " FROM tbl_energy_storage_containers_grids " |
||
| 2311 | " WHERE energy_storage_container_id = %s " |
||
| 2312 | " ORDER BY name ") |
||
| 2313 | cursor.execute(query, (id_,)) |
||
| 2314 | rows = cursor.fetchall() |
||
| 2315 | |||
| 2316 | result = list() |
||
| 2317 | if rows is not None and len(rows) > 0: |
||
| 2318 | for row in rows: |
||
| 2319 | meta_result = {"id": row[0], |
||
| 2320 | "name": row[1], |
||
| 2321 | "uuid": row[2], |
||
| 2322 | "power_point": point_dict.get(row[3]), |
||
| 2323 | "buy_meter": meter_dict.get(row[4]), |
||
| 2324 | "sell_meter": meter_dict.get(row[5]), |
||
| 2325 | "capacity": row[6] |
||
| 2326 | } |
||
| 2327 | result.append(meta_result) |
||
| 2328 | |||
| 2329 | resp.text = json.dumps(result) |
||
| 2330 | |||
| 2331 | @staticmethod |
||
| 2332 | @user_logger |
||
| 2333 | def on_post(req, resp, id_): |
||
| 2334 | """Handles POST requests""" |
||
| 2335 | admin_control(req) |
||
| 2336 | try: |
||
| 2337 | raw_json = req.stream.read().decode('utf-8') |
||
| 2338 | except Exception as ex: |
||
| 2339 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2340 | title='API.BAD_REQUEST', |
||
| 2341 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2342 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2343 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2344 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2345 | |||
| 2346 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2347 | cursor = cnx.cursor() |
||
| 2348 | |||
| 2349 | cursor.execute(" SELECT name " |
||
| 2350 | " FROM tbl_energy_storage_containers " |
||
| 2351 | " WHERE id = %s ", (id_,)) |
||
| 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_NOT_FOUND') |
||
| 2357 | |||
| 2358 | new_values = json.loads(raw_json) |
||
| 2359 | |||
| 2360 | if 'name' not in new_values['data'].keys() or \ |
||
| 2361 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2362 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2363 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2364 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
||
| 2365 | name = str.strip(new_values['data']['name']) |
||
| 2366 | |||
| 2367 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2368 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2369 | new_values['data']['power_point_id'] <= 0: |
||
| 2370 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2371 | description='API.INVALID_POWER_POINT_ID') |
||
| 2372 | power_point_id = new_values['data']['power_point_id'] |
||
| 2373 | |||
| 2374 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 2375 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 2376 | new_values['data']['buy_meter_id'] <= 0: |
||
| 2377 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2378 | description='API.INVALID_BUY_METER_ID') |
||
| 2379 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 2380 | |||
| 2381 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 2382 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 2383 | new_values['data']['sell_meter_id'] <= 0: |
||
| 2384 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2385 | description='API.INVALID_SELL_METER_ID') |
||
| 2386 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 2387 | |||
| 2388 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 2389 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 2390 | isinstance(new_values['data']['capacity'], int)): |
||
| 2391 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2392 | description='API.INVALID_CAPACITY') |
||
| 2393 | capacity = Decimal(new_values['data']['capacity']) |
||
| 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 ", |
||
| 2401 | (id_,)) |
||
| 2402 | if cursor.fetchone() is None: |
||
| 2403 | cursor.close() |
||
| 2404 | cnx.close() |
||
| 2405 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2406 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2407 | |||
| 2408 | cursor.execute(" SELECT name " |
||
| 2409 | " FROM tbl_energy_storage_containers_grids " |
||
| 2410 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 2411 | (id_, name,)) |
||
| 2412 | if cursor.fetchone() is not None: |
||
| 2413 | cursor.close() |
||
| 2414 | cnx.close() |
||
| 2415 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2416 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 2417 | |||
| 2418 | cursor.execute(" SELECT name " |
||
| 2419 | " FROM tbl_points " |
||
| 2420 | " WHERE id = %s ", |
||
| 2421 | (power_point_id,)) |
||
| 2422 | if cursor.fetchone() is None: |
||
| 2423 | cursor.close() |
||
| 2424 | cnx.close() |
||
| 2425 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2426 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2427 | |||
| 2428 | cursor.execute(" SELECT name " |
||
| 2429 | " FROM tbl_meters " |
||
| 2430 | " WHERE id = %s ", |
||
| 2431 | (buy_meter_id,)) |
||
| 2432 | if cursor.fetchone() is None: |
||
| 2433 | cursor.close() |
||
| 2434 | cnx.close() |
||
| 2435 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2436 | description='API.BUY_METER_NOT_FOUND') |
||
| 2437 | |||
| 2438 | cursor.execute(" SELECT name " |
||
| 2439 | " FROM tbl_meters " |
||
| 2440 | " WHERE id = %s ", |
||
| 2441 | (sell_meter_id,)) |
||
| 2442 | if cursor.fetchone() is None: |
||
| 2443 | cursor.close() |
||
| 2444 | cnx.close() |
||
| 2445 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2446 | description='API.SELL_METER_NOT_FOUND') |
||
| 2447 | |||
| 2448 | add_values = (" INSERT INTO tbl_energy_storage_containers_grids " |
||
| 2449 | " (name, uuid, energy_storage_container_id, power_point_id, " |
||
| 2450 | " buy_meter_id, sell_meter_id, capacity) " |
||
| 2451 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 2452 | cursor.execute(add_values, (name, |
||
| 2453 | str(uuid.uuid4()), |
||
| 2454 | id_, |
||
| 2455 | power_point_id, |
||
| 2456 | buy_meter_id, |
||
| 2457 | sell_meter_id, |
||
| 2458 | capacity |
||
| 2459 | )) |
||
| 2460 | new_id = cursor.lastrowid |
||
| 2461 | cnx.commit() |
||
| 2462 | cursor.close() |
||
| 2463 | cnx.close() |
||
| 2464 | |||
| 2465 | resp.status = falcon.HTTP_201 |
||
| 2466 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
||
| 2467 | |||
| 2468 | |||
| 2469 | View Code Duplication | class EnergyStorageContainerGridItem: |
|
| 2470 | def __init__(self): |
||
| 2471 | """Initializes Class""" |
||
| 2472 | pass |
||
| 2473 | |||
| 2474 | @staticmethod |
||
| 2475 | def on_options(req, resp, id_, gid): |
||
| 2476 | resp.status = falcon.HTTP_200 |
||
| 2477 | |||
| 2478 | @staticmethod |
||
| 2479 | def on_get(req, resp, id_, gid): |
||
| 2480 | access_control(req) |
||
| 2481 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2482 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2483 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2484 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2485 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2486 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 2487 | |||
| 2488 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2489 | cursor = cnx.cursor() |
||
| 2490 | |||
| 2491 | cursor.execute(" SELECT name " |
||
| 2492 | " FROM tbl_energy_storage_containers " |
||
| 2493 | " WHERE id = %s ", (id_,)) |
||
| 2494 | if cursor.fetchone() is None: |
||
| 2495 | cursor.close() |
||
| 2496 | cnx.close() |
||
| 2497 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2498 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2499 | |||
| 2500 | query = (" SELECT id, name, uuid " |
||
| 2501 | " FROM tbl_energy_storage_containers ") |
||
| 2502 | cursor.execute(query) |
||
| 2503 | rows_energystoragecontainers = cursor.fetchall() |
||
| 2504 | |||
| 2505 | energy_storage_container_dict = dict() |
||
| 2506 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 2507 | for row in rows_energystoragecontainers: |
||
| 2508 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 2509 | "name": row[1], |
||
| 2510 | "uuid": row[2]} |
||
| 2511 | # query meter dict |
||
| 2512 | query = (" SELECT id, name, uuid " |
||
| 2513 | " FROM tbl_meters ") |
||
| 2514 | cursor.execute(query) |
||
| 2515 | rows_meters = cursor.fetchall() |
||
| 2516 | |||
| 2517 | meter_dict = dict() |
||
| 2518 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2519 | for row in rows_meters: |
||
| 2520 | meter_dict[row[0]] = {"id": row[0], |
||
| 2521 | "name": row[1], |
||
| 2522 | "uuid": row[2]} |
||
| 2523 | # query point dict |
||
| 2524 | query = (" SELECT id, name " |
||
| 2525 | " FROM tbl_points ") |
||
| 2526 | cursor.execute(query) |
||
| 2527 | rows_points = cursor.fetchall() |
||
| 2528 | |||
| 2529 | point_dict = dict() |
||
| 2530 | if rows_points is not None and len(rows_points) > 0: |
||
| 2531 | for row in rows_points: |
||
| 2532 | point_dict[row[0]] = {"id": row[0], |
||
| 2533 | "name": row[1]} |
||
| 2534 | |||
| 2535 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
||
| 2536 | " buy_meter_id, sell_meter_id, capacity " |
||
| 2537 | " FROM tbl_energy_storage_containers_grids " |
||
| 2538 | " WHERE id = %s ") |
||
| 2539 | cursor.execute(query, (gid,)) |
||
| 2540 | row = cursor.fetchone() |
||
| 2541 | cursor.close() |
||
| 2542 | cnx.close() |
||
| 2543 | |||
| 2544 | if row is None: |
||
| 2545 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2546 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 2547 | else: |
||
| 2548 | meta_result = {"id": row[0], |
||
| 2549 | "name": row[1], |
||
| 2550 | "uuid": row[2], |
||
| 2551 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 2552 | "power_point": point_dict.get(row[4]), |
||
| 2553 | "buy_meter": meter_dict.get(row[5]), |
||
| 2554 | "sell_meter": meter_dict.get(row[6]), |
||
| 2555 | "capacity": row[7] |
||
| 2556 | } |
||
| 2557 | |||
| 2558 | resp.text = json.dumps(meta_result) |
||
| 2559 | |||
| 2560 | @staticmethod |
||
| 2561 | @user_logger |
||
| 2562 | def on_delete(req, resp, id_, gid): |
||
| 2563 | admin_control(req) |
||
| 2564 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2565 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2566 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2567 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2568 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2569 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 2570 | |||
| 2571 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2572 | cursor = cnx.cursor() |
||
| 2573 | |||
| 2574 | cursor.execute(" SELECT name " |
||
| 2575 | " FROM tbl_energy_storage_containers " |
||
| 2576 | " WHERE id = %s ", (id_,)) |
||
| 2577 | if cursor.fetchone() is None: |
||
| 2578 | cursor.close() |
||
| 2579 | cnx.close() |
||
| 2580 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2581 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2582 | |||
| 2583 | cursor.execute(" SELECT name " |
||
| 2584 | " FROM tbl_energy_storage_containers_grids " |
||
| 2585 | " WHERE id = %s ", (gid,)) |
||
| 2586 | if cursor.fetchone() is None: |
||
| 2587 | cursor.close() |
||
| 2588 | cnx.close() |
||
| 2589 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2590 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 2591 | |||
| 2592 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
||
| 2593 | " WHERE id = %s ", (gid,)) |
||
| 2594 | cnx.commit() |
||
| 2595 | |||
| 2596 | cursor.close() |
||
| 2597 | cnx.close() |
||
| 2598 | |||
| 2599 | resp.status = falcon.HTTP_204 |
||
| 2600 | |||
| 2601 | @staticmethod |
||
| 2602 | @user_logger |
||
| 2603 | def on_put(req, resp, id_, gid): |
||
| 2604 | """Handles PUT requests""" |
||
| 2605 | admin_control(req) |
||
| 2606 | try: |
||
| 2607 | raw_json = req.stream.read().decode('utf-8') |
||
| 2608 | except Exception as ex: |
||
| 2609 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2610 | title='API.BAD_REQUEST', |
||
| 2611 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2612 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2613 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2614 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2615 | |||
| 2616 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2617 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2618 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
||
| 2619 | |||
| 2620 | new_values = json.loads(raw_json) |
||
| 2621 | |||
| 2622 | if 'name' not in new_values['data'].keys() or \ |
||
| 2623 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2624 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2625 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2626 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
||
| 2627 | name = str.strip(new_values['data']['name']) |
||
| 2628 | |||
| 2629 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2630 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2631 | new_values['data']['power_point_id'] <= 0: |
||
| 2632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2633 | description='API.INVALID_POWER_POINT_ID') |
||
| 2634 | power_point_id = new_values['data']['power_point_id'] |
||
| 2635 | |||
| 2636 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 2637 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 2638 | new_values['data']['buy_meter_id'] <= 0: |
||
| 2639 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2640 | description='API.INVALID_BUY_METER_ID') |
||
| 2641 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 2642 | |||
| 2643 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 2644 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 2645 | new_values['data']['sell_meter_id'] <= 0: |
||
| 2646 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2647 | description='API.INVALID_METER_ID') |
||
| 2648 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 2649 | |||
| 2650 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 2651 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 2652 | isinstance(new_values['data']['capacity'], int)): |
||
| 2653 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2654 | description='API.INVALID_CAPACITY') |
||
| 2655 | capacity = Decimal(new_values['data']['capacity']) |
||
| 2656 | |||
| 2657 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2658 | cursor = cnx.cursor() |
||
| 2659 | |||
| 2660 | cursor.execute(" SELECT name " |
||
| 2661 | " FROM tbl_energy_storage_containers " |
||
| 2662 | " WHERE id = %s ", (id_,)) |
||
| 2663 | if cursor.fetchone() is None: |
||
| 2664 | cursor.close() |
||
| 2665 | cnx.close() |
||
| 2666 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2667 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2668 | |||
| 2669 | cursor.execute(" SELECT name " |
||
| 2670 | " FROM tbl_energy_storage_containers_grids " |
||
| 2671 | " WHERE id = %s ", (gid,)) |
||
| 2672 | if cursor.fetchone() is None: |
||
| 2673 | cursor.close() |
||
| 2674 | cnx.close() |
||
| 2675 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2676 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
||
| 2677 | |||
| 2678 | cursor.execute(" SELECT name " |
||
| 2679 | " FROM tbl_energy_storage_containers_grids " |
||
| 2680 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 2681 | (id_, name, gid)) |
||
| 2682 | if cursor.fetchone() is not None: |
||
| 2683 | cursor.close() |
||
| 2684 | cnx.close() |
||
| 2685 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2686 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 2687 | |||
| 2688 | cursor.execute(" SELECT name " |
||
| 2689 | " FROM tbl_points " |
||
| 2690 | " WHERE id = %s ", |
||
| 2691 | (power_point_id,)) |
||
| 2692 | if cursor.fetchone() is None: |
||
| 2693 | cursor.close() |
||
| 2694 | cnx.close() |
||
| 2695 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2696 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2697 | |||
| 2698 | cursor.execute(" SELECT name " |
||
| 2699 | " FROM tbl_meters " |
||
| 2700 | " WHERE id = %s ", |
||
| 2701 | (buy_meter_id,)) |
||
| 2702 | if cursor.fetchone() is None: |
||
| 2703 | cursor.close() |
||
| 2704 | cnx.close() |
||
| 2705 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2706 | description='API.BUY_METER_NOT_FOUND') |
||
| 2707 | |||
| 2708 | cursor.execute(" SELECT name " |
||
| 2709 | " FROM tbl_meters " |
||
| 2710 | " WHERE id = %s ", |
||
| 2711 | (sell_meter_id,)) |
||
| 2712 | if cursor.fetchone() is None: |
||
| 2713 | cursor.close() |
||
| 2714 | cnx.close() |
||
| 2715 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2716 | description='API.SELL_METER_NOT_FOUND') |
||
| 2717 | |||
| 2718 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
||
| 2719 | " SET name = %s, energy_storage_container_id = %s, " |
||
| 2720 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
||
| 2721 | " WHERE id = %s ") |
||
| 2722 | cursor.execute(update_row, (name, |
||
| 2723 | id_, |
||
| 2724 | power_point_id, |
||
| 2725 | buy_meter_id, |
||
| 2726 | sell_meter_id, |
||
| 2727 | capacity, |
||
| 2728 | gid)) |
||
| 2729 | cnx.commit() |
||
| 2730 | |||
| 2731 | cursor.close() |
||
| 2732 | cnx.close() |
||
| 2733 | |||
| 2734 | resp.status = falcon.HTTP_200 |
||
| 2735 | |||
| 2736 | |||
| 2737 | View Code Duplication | class EnergyStorageContainerHVACCollection: |
|
| 2738 | def __init__(self): |
||
| 2739 | """Initializes Class""" |
||
| 2740 | pass |
||
| 2741 | |||
| 2742 | @staticmethod |
||
| 2743 | def on_options(req, resp, id_): |
||
| 2744 | resp.status = falcon.HTTP_200 |
||
| 2745 | |||
| 2746 | @staticmethod |
||
| 2747 | def on_get(req, resp, id_): |
||
| 2748 | access_control(req) |
||
| 2749 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2750 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2751 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2752 | |||
| 2753 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2754 | cursor = cnx.cursor() |
||
| 2755 | |||
| 2756 | cursor.execute(" SELECT name " |
||
| 2757 | " FROM tbl_energy_storage_containers " |
||
| 2758 | " WHERE id = %s ", (id_,)) |
||
| 2759 | if cursor.fetchone() is None: |
||
| 2760 | cursor.close() |
||
| 2761 | cnx.close() |
||
| 2762 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2763 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2764 | |||
| 2765 | query = (" SELECT id, name, uuid " |
||
| 2766 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 2767 | " WHERE energy_storage_container_id = %s " |
||
| 2768 | " ORDER BY name ") |
||
| 2769 | cursor.execute(query, (id_,)) |
||
| 2770 | rows = cursor.fetchall() |
||
| 2771 | |||
| 2772 | result = list() |
||
| 2773 | if rows is not None and len(rows) > 0: |
||
| 2774 | for row in rows: |
||
| 2775 | meta_result = {"id": row[0], |
||
| 2776 | "name": row[1], |
||
| 2777 | "uuid": row[2] |
||
| 2778 | } |
||
| 2779 | result.append(meta_result) |
||
| 2780 | |||
| 2781 | resp.text = json.dumps(result) |
||
| 2782 | |||
| 2783 | @staticmethod |
||
| 2784 | @user_logger |
||
| 2785 | def on_post(req, resp, id_): |
||
| 2786 | """Handles POST requests""" |
||
| 2787 | admin_control(req) |
||
| 2788 | try: |
||
| 2789 | raw_json = req.stream.read().decode('utf-8') |
||
| 2790 | except Exception as ex: |
||
| 2791 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2792 | title='API.BAD_REQUEST', |
||
| 2793 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2794 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2795 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2796 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2797 | |||
| 2798 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2799 | cursor = cnx.cursor() |
||
| 2800 | |||
| 2801 | cursor.execute(" SELECT name " |
||
| 2802 | " FROM tbl_energy_storage_containers " |
||
| 2803 | " WHERE id = %s ", (id_,)) |
||
| 2804 | if cursor.fetchone() is None: |
||
| 2805 | cursor.close() |
||
| 2806 | cnx.close() |
||
| 2807 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2808 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2809 | |||
| 2810 | new_values = json.loads(raw_json) |
||
| 2811 | |||
| 2812 | if 'name' not in new_values['data'].keys() or \ |
||
| 2813 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2814 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2815 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2816 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME') |
||
| 2817 | name = str.strip(new_values['data']['name']) |
||
| 2818 | |||
| 2819 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2820 | cursor = cnx.cursor() |
||
| 2821 | |||
| 2822 | cursor.execute(" SELECT name " |
||
| 2823 | " FROM tbl_energy_storage_containers " |
||
| 2824 | " WHERE id = %s ", |
||
| 2825 | (id_,)) |
||
| 2826 | if cursor.fetchone() is None: |
||
| 2827 | cursor.close() |
||
| 2828 | cnx.close() |
||
| 2829 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2830 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2831 | |||
| 2832 | cursor.execute(" SELECT name " |
||
| 2833 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 2834 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 2835 | (id_, name,)) |
||
| 2836 | if cursor.fetchone() is not None: |
||
| 2837 | cursor.close() |
||
| 2838 | cnx.close() |
||
| 2839 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2840 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE') |
||
| 2841 | |||
| 2842 | add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs " |
||
| 2843 | " (name, uuid, energy_storage_container_id) " |
||
| 2844 | " VALUES (%s, %s, %s) ") |
||
| 2845 | cursor.execute(add_values, (name, |
||
| 2846 | str(uuid.uuid4()), |
||
| 2847 | id_ |
||
| 2848 | )) |
||
| 2849 | new_id = cursor.lastrowid |
||
| 2850 | cnx.commit() |
||
| 2851 | cursor.close() |
||
| 2852 | cnx.close() |
||
| 2853 | |||
| 2854 | resp.status = falcon.HTTP_201 |
||
| 2855 | resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id) |
||
| 2856 | |||
| 2857 | |||
| 2858 | View Code Duplication | class EnergyStorageContainerHVACItem: |
|
| 2859 | def __init__(self): |
||
| 2860 | """Initializes Class""" |
||
| 2861 | pass |
||
| 2862 | |||
| 2863 | @staticmethod |
||
| 2864 | def on_options(req, resp, id_, hid): |
||
| 2865 | resp.status = falcon.HTTP_200 |
||
| 2866 | |||
| 2867 | @staticmethod |
||
| 2868 | def on_get(req, resp, id_, hid): |
||
| 2869 | access_control(req) |
||
| 2870 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2871 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2872 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2873 | if not hid.isdigit() or int(hid) <= 0: |
||
| 2874 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2875 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 2876 | |||
| 2877 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2878 | cursor = cnx.cursor() |
||
| 2879 | |||
| 2880 | cursor.execute(" SELECT name " |
||
| 2881 | " FROM tbl_energy_storage_containers " |
||
| 2882 | " WHERE id = %s ", (id_,)) |
||
| 2883 | if cursor.fetchone() is None: |
||
| 2884 | cursor.close() |
||
| 2885 | cnx.close() |
||
| 2886 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2887 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2888 | |||
| 2889 | query = (" SELECT id, name, uuid " |
||
| 2890 | " FROM tbl_energy_storage_containers ") |
||
| 2891 | cursor.execute(query) |
||
| 2892 | rows_energystoragecontainers = cursor.fetchall() |
||
| 2893 | |||
| 2894 | energy_storage_container_dict = dict() |
||
| 2895 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 2896 | for row in rows_energystoragecontainers: |
||
| 2897 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 2898 | "name": row[1], |
||
| 2899 | "uuid": row[2]} |
||
| 2900 | |||
| 2901 | query = (" SELECT id, name, uuid " |
||
| 2902 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 2903 | " WHERE id = %s ") |
||
| 2904 | cursor.execute(query, (hid,)) |
||
| 2905 | row = cursor.fetchone() |
||
| 2906 | cursor.close() |
||
| 2907 | cnx.close() |
||
| 2908 | |||
| 2909 | if row is None: |
||
| 2910 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2911 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 2912 | else: |
||
| 2913 | meta_result = {"id": row[0], |
||
| 2914 | "name": row[1], |
||
| 2915 | "uuid": row[2] |
||
| 2916 | } |
||
| 2917 | |||
| 2918 | resp.text = json.dumps(meta_result) |
||
| 2919 | |||
| 2920 | @staticmethod |
||
| 2921 | @user_logger |
||
| 2922 | def on_delete(req, resp, id_, hid): |
||
| 2923 | admin_control(req) |
||
| 2924 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2925 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2926 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2927 | if not hid.isdigit() or int(hid) <= 0: |
||
| 2928 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2929 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 2930 | |||
| 2931 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2932 | cursor = cnx.cursor() |
||
| 2933 | |||
| 2934 | cursor.execute(" SELECT name " |
||
| 2935 | " FROM tbl_energy_storage_containers " |
||
| 2936 | " WHERE id = %s ", (id_,)) |
||
| 2937 | if cursor.fetchone() is None: |
||
| 2938 | cursor.close() |
||
| 2939 | cnx.close() |
||
| 2940 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2941 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 2942 | |||
| 2943 | cursor.execute(" SELECT name " |
||
| 2944 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 2945 | " WHERE id = %s ", (hid,)) |
||
| 2946 | if cursor.fetchone() is None: |
||
| 2947 | cursor.close() |
||
| 2948 | cnx.close() |
||
| 2949 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2950 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 2951 | |||
| 2952 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs " |
||
| 2953 | " WHERE id = %s ", (hid,)) |
||
| 2954 | cnx.commit() |
||
| 2955 | |||
| 2956 | cursor.close() |
||
| 2957 | cnx.close() |
||
| 2958 | |||
| 2959 | resp.status = falcon.HTTP_204 |
||
| 2960 | |||
| 2961 | @staticmethod |
||
| 2962 | @user_logger |
||
| 2963 | def on_put(req, resp, id_, hid): |
||
| 2964 | """Handles PUT requests""" |
||
| 2965 | admin_control(req) |
||
| 2966 | try: |
||
| 2967 | raw_json = req.stream.read().decode('utf-8') |
||
| 2968 | except Exception as ex: |
||
| 2969 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2970 | title='API.BAD_REQUEST', |
||
| 2971 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2972 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2973 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2974 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 2975 | |||
| 2976 | if not hid.isdigit() or int(hid) <= 0: |
||
| 2977 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2978 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
||
| 2979 | |||
| 2980 | new_values = json.loads(raw_json) |
||
| 2981 | |||
| 2982 | if 'name' not in new_values['data'].keys() or \ |
||
| 2983 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2984 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2985 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2986 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME') |
||
| 2987 | name = str.strip(new_values['data']['name']) |
||
| 2988 | |||
| 2989 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2990 | cursor = cnx.cursor() |
||
| 2991 | |||
| 2992 | cursor.execute(" SELECT name " |
||
| 2993 | " FROM tbl_energy_storage_containers " |
||
| 2994 | " WHERE id = %s ", (id_,)) |
||
| 2995 | if cursor.fetchone() is None: |
||
| 2996 | cursor.close() |
||
| 2997 | cnx.close() |
||
| 2998 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2999 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3000 | |||
| 3001 | cursor.execute(" SELECT name " |
||
| 3002 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3003 | " WHERE id = %s ", (hid,)) |
||
| 3004 | if cursor.fetchone() is None: |
||
| 3005 | cursor.close() |
||
| 3006 | cnx.close() |
||
| 3007 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3008 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
||
| 3009 | |||
| 3010 | cursor.execute(" SELECT name " |
||
| 3011 | " FROM tbl_energy_storage_containers_hvacs " |
||
| 3012 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 3013 | (id_, name, hid)) |
||
| 3014 | if cursor.fetchone() is not None: |
||
| 3015 | cursor.close() |
||
| 3016 | cnx.close() |
||
| 3017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3018 | description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE') |
||
| 3019 | |||
| 3020 | update_row = (" UPDATE tbl_energy_storage_containers_hvacs " |
||
| 3021 | " SET name = %s " |
||
| 3022 | " WHERE id = %s ") |
||
| 3023 | cursor.execute(update_row, (name, |
||
| 3024 | hid)) |
||
| 3025 | cnx.commit() |
||
| 3026 | |||
| 3027 | cursor.close() |
||
| 3028 | cnx.close() |
||
| 3029 | |||
| 3030 | resp.status = falcon.HTTP_200 |
||
| 3031 | |||
| 3032 | |||
| 3033 | View Code Duplication | class EnergyStorageContainerLoadCollection: |
|
| 3034 | def __init__(self): |
||
| 3035 | """Initializes Class""" |
||
| 3036 | pass |
||
| 3037 | |||
| 3038 | @staticmethod |
||
| 3039 | def on_options(req, resp, id_): |
||
| 3040 | resp.status = falcon.HTTP_200 |
||
| 3041 | |||
| 3042 | @staticmethod |
||
| 3043 | def on_get(req, resp, id_): |
||
| 3044 | access_control(req) |
||
| 3045 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3046 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3047 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3048 | |||
| 3049 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3050 | cursor = cnx.cursor() |
||
| 3051 | |||
| 3052 | cursor.execute(" SELECT name " |
||
| 3053 | " FROM tbl_energy_storage_containers " |
||
| 3054 | " WHERE id = %s ", (id_,)) |
||
| 3055 | if cursor.fetchone() is None: |
||
| 3056 | cursor.close() |
||
| 3057 | cnx.close() |
||
| 3058 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3059 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3060 | |||
| 3061 | # query meter dict |
||
| 3062 | query = (" SELECT id, name, uuid " |
||
| 3063 | " FROM tbl_meters ") |
||
| 3064 | cursor.execute(query) |
||
| 3065 | rows_meters = cursor.fetchall() |
||
| 3066 | |||
| 3067 | meter_dict = dict() |
||
| 3068 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3069 | for row in rows_meters: |
||
| 3070 | meter_dict[row[0]] = {"id": row[0], |
||
| 3071 | "name": row[1], |
||
| 3072 | "uuid": row[2]} |
||
| 3073 | # query point dict |
||
| 3074 | query = (" SELECT id, name " |
||
| 3075 | " FROM tbl_points ") |
||
| 3076 | cursor.execute(query) |
||
| 3077 | rows_points = cursor.fetchall() |
||
| 3078 | |||
| 3079 | point_dict = dict() |
||
| 3080 | if rows_points is not None and len(rows_points) > 0: |
||
| 3081 | for row in rows_points: |
||
| 3082 | point_dict[row[0]] = {"id": row[0], |
||
| 3083 | "name": row[1]} |
||
| 3084 | |||
| 3085 | query = (" SELECT id, name, uuid, " |
||
| 3086 | " power_point_id, meter_id, rated_input_power " |
||
| 3087 | " FROM tbl_energy_storage_containers_loads " |
||
| 3088 | " WHERE energy_storage_container_id = %s " |
||
| 3089 | " ORDER BY name ") |
||
| 3090 | cursor.execute(query, (id_,)) |
||
| 3091 | rows = cursor.fetchall() |
||
| 3092 | |||
| 3093 | result = list() |
||
| 3094 | if rows is not None and len(rows) > 0: |
||
| 3095 | for row in rows: |
||
| 3096 | meta_result = {"id": row[0], |
||
| 3097 | "name": row[1], |
||
| 3098 | "uuid": row[2], |
||
| 3099 | "power_point": point_dict.get(row[3]), |
||
| 3100 | "meter": meter_dict.get(row[4]), |
||
| 3101 | "rated_input_power": row[5] |
||
| 3102 | } |
||
| 3103 | result.append(meta_result) |
||
| 3104 | |||
| 3105 | resp.text = json.dumps(result) |
||
| 3106 | |||
| 3107 | @staticmethod |
||
| 3108 | @user_logger |
||
| 3109 | def on_post(req, resp, id_): |
||
| 3110 | """Handles POST requests""" |
||
| 3111 | admin_control(req) |
||
| 3112 | try: |
||
| 3113 | raw_json = req.stream.read().decode('utf-8') |
||
| 3114 | except Exception as ex: |
||
| 3115 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3116 | title='API.BAD_REQUEST', |
||
| 3117 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3118 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3119 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3120 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3121 | |||
| 3122 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3123 | cursor = cnx.cursor() |
||
| 3124 | |||
| 3125 | cursor.execute(" SELECT name " |
||
| 3126 | " FROM tbl_energy_storage_containers " |
||
| 3127 | " WHERE id = %s ", (id_,)) |
||
| 3128 | if cursor.fetchone() is None: |
||
| 3129 | cursor.close() |
||
| 3130 | cnx.close() |
||
| 3131 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3132 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3133 | |||
| 3134 | new_values = json.loads(raw_json) |
||
| 3135 | |||
| 3136 | if 'name' not in new_values['data'].keys() or \ |
||
| 3137 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3138 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3139 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3140 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_NAME') |
||
| 3141 | name = str.strip(new_values['data']['name']) |
||
| 3142 | |||
| 3143 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3144 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3145 | new_values['data']['power_point_id'] <= 0: |
||
| 3146 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3147 | description='API.INVALID_POWER_POINT_ID') |
||
| 3148 | power_point_id = new_values['data']['power_point_id'] |
||
| 3149 | |||
| 3150 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 3151 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 3152 | new_values['data']['meter_id'] <= 0: |
||
| 3153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3154 | description='API.INVALID_METER_ID') |
||
| 3155 | meter_id = new_values['data']['meter_id'] |
||
| 3156 | |||
| 3157 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 3158 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 3159 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 3160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3161 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 3162 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
||
| 3163 | |||
| 3164 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3165 | cursor = cnx.cursor() |
||
| 3166 | |||
| 3167 | cursor.execute(" SELECT name " |
||
| 3168 | " FROM tbl_energy_storage_containers " |
||
| 3169 | " WHERE id = %s ", |
||
| 3170 | (id_,)) |
||
| 3171 | if cursor.fetchone() is None: |
||
| 3172 | cursor.close() |
||
| 3173 | cnx.close() |
||
| 3174 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3175 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3176 | |||
| 3177 | cursor.execute(" SELECT name " |
||
| 3178 | " FROM tbl_energy_storage_containers_loads " |
||
| 3179 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 3180 | (id_, name,)) |
||
| 3181 | if cursor.fetchone() is not None: |
||
| 3182 | cursor.close() |
||
| 3183 | cnx.close() |
||
| 3184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3185 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 3186 | |||
| 3187 | cursor.execute(" SELECT name " |
||
| 3188 | " FROM tbl_points " |
||
| 3189 | " WHERE id = %s ", |
||
| 3190 | (power_point_id,)) |
||
| 3191 | if cursor.fetchone() is None: |
||
| 3192 | cursor.close() |
||
| 3193 | cnx.close() |
||
| 3194 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3195 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3196 | |||
| 3197 | cursor.execute(" SELECT name " |
||
| 3198 | " FROM tbl_meters " |
||
| 3199 | " WHERE id = %s ", |
||
| 3200 | (meter_id,)) |
||
| 3201 | if cursor.fetchone() is None: |
||
| 3202 | cursor.close() |
||
| 3203 | cnx.close() |
||
| 3204 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3205 | description='API.METER_NOT_FOUND') |
||
| 3206 | |||
| 3207 | add_values = (" INSERT INTO tbl_energy_storage_containers_loads " |
||
| 3208 | " (name, uuid, energy_storage_container_id, power_point_id, meter_id, rated_input_power) " |
||
| 3209 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 3210 | cursor.execute(add_values, (name, |
||
| 3211 | str(uuid.uuid4()), |
||
| 3212 | id_, |
||
| 3213 | power_point_id, |
||
| 3214 | meter_id, |
||
| 3215 | rated_input_power |
||
| 3216 | )) |
||
| 3217 | new_id = cursor.lastrowid |
||
| 3218 | cnx.commit() |
||
| 3219 | cursor.close() |
||
| 3220 | cnx.close() |
||
| 3221 | |||
| 3222 | resp.status = falcon.HTTP_201 |
||
| 3223 | resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(new_id) |
||
| 3224 | |||
| 3225 | |||
| 3226 | View Code Duplication | class EnergyStorageContainerLoadItem: |
|
| 3227 | def __init__(self): |
||
| 3228 | """Initializes Class""" |
||
| 3229 | pass |
||
| 3230 | |||
| 3231 | @staticmethod |
||
| 3232 | def on_options(req, resp, id_, lid): |
||
| 3233 | resp.status = falcon.HTTP_200 |
||
| 3234 | |||
| 3235 | @staticmethod |
||
| 3236 | def on_get(req, resp, id_, lid): |
||
| 3237 | access_control(req) |
||
| 3238 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3240 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3241 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3242 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3243 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 3244 | |||
| 3245 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3246 | cursor = cnx.cursor() |
||
| 3247 | |||
| 3248 | cursor.execute(" SELECT name " |
||
| 3249 | " FROM tbl_energy_storage_containers " |
||
| 3250 | " WHERE id = %s ", (id_,)) |
||
| 3251 | if cursor.fetchone() is None: |
||
| 3252 | cursor.close() |
||
| 3253 | cnx.close() |
||
| 3254 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3255 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3256 | |||
| 3257 | query = (" SELECT id, name, uuid " |
||
| 3258 | " FROM tbl_energy_storage_containers ") |
||
| 3259 | cursor.execute(query) |
||
| 3260 | rows_energystoragecontainers = cursor.fetchall() |
||
| 3261 | |||
| 3262 | energy_storage_container_dict = dict() |
||
| 3263 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 3264 | for row in rows_energystoragecontainers: |
||
| 3265 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 3266 | "name": row[1], |
||
| 3267 | "uuid": row[2]} |
||
| 3268 | # query meter dict |
||
| 3269 | query = (" SELECT id, name, uuid " |
||
| 3270 | " FROM tbl_meters ") |
||
| 3271 | cursor.execute(query) |
||
| 3272 | rows_meters = cursor.fetchall() |
||
| 3273 | |||
| 3274 | meter_dict = dict() |
||
| 3275 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3276 | for row in rows_meters: |
||
| 3277 | meter_dict[row[0]] = {"id": row[0], |
||
| 3278 | "name": row[1], |
||
| 3279 | "uuid": row[2]} |
||
| 3280 | # query point dict |
||
| 3281 | query = (" SELECT id, name " |
||
| 3282 | " FROM tbl_points ") |
||
| 3283 | cursor.execute(query) |
||
| 3284 | rows_points = cursor.fetchall() |
||
| 3285 | |||
| 3286 | point_dict = dict() |
||
| 3287 | if rows_points is not None and len(rows_points) > 0: |
||
| 3288 | for row in rows_points: |
||
| 3289 | point_dict[row[0]] = {"id": row[0], |
||
| 3290 | "name": row[1]} |
||
| 3291 | |||
| 3292 | query = (" SELECT id, name, uuid, " |
||
| 3293 | " energy_storage_container_id, power_point_id, meter_id, rated_input_power " |
||
| 3294 | " FROM tbl_energy_storage_containers_loads " |
||
| 3295 | " WHERE id = %s ") |
||
| 3296 | cursor.execute(query, (lid,)) |
||
| 3297 | row = cursor.fetchone() |
||
| 3298 | cursor.close() |
||
| 3299 | cnx.close() |
||
| 3300 | |||
| 3301 | if row is None: |
||
| 3302 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3303 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 3304 | else: |
||
| 3305 | meta_result = {"id": row[0], |
||
| 3306 | "name": row[1], |
||
| 3307 | "uuid": row[2], |
||
| 3308 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 3309 | "power_point": point_dict.get(row[4]), |
||
| 3310 | "meter": meter_dict.get(row[5]), |
||
| 3311 | "rated_input_power": row[6] |
||
| 3312 | } |
||
| 3313 | |||
| 3314 | resp.text = json.dumps(meta_result) |
||
| 3315 | |||
| 3316 | @staticmethod |
||
| 3317 | @user_logger |
||
| 3318 | def on_delete(req, resp, id_, lid): |
||
| 3319 | admin_control(req) |
||
| 3320 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3322 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3323 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3324 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3325 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 3326 | |||
| 3327 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3328 | cursor = cnx.cursor() |
||
| 3329 | |||
| 3330 | cursor.execute(" SELECT name " |
||
| 3331 | " FROM tbl_energy_storage_containers " |
||
| 3332 | " WHERE id = %s ", (id_,)) |
||
| 3333 | if cursor.fetchone() is None: |
||
| 3334 | cursor.close() |
||
| 3335 | cnx.close() |
||
| 3336 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3337 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3338 | |||
| 3339 | cursor.execute(" SELECT name " |
||
| 3340 | " FROM tbl_energy_storage_containers_loads " |
||
| 3341 | " WHERE id = %s ", (lid,)) |
||
| 3342 | if cursor.fetchone() is None: |
||
| 3343 | cursor.close() |
||
| 3344 | cnx.close() |
||
| 3345 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3346 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 3347 | |||
| 3348 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_loads " |
||
| 3349 | " WHERE id = %s ", (lid,)) |
||
| 3350 | cnx.commit() |
||
| 3351 | |||
| 3352 | cursor.close() |
||
| 3353 | cnx.close() |
||
| 3354 | |||
| 3355 | resp.status = falcon.HTTP_204 |
||
| 3356 | |||
| 3357 | @staticmethod |
||
| 3358 | @user_logger |
||
| 3359 | def on_put(req, resp, id_, lid): |
||
| 3360 | """Handles PUT requests""" |
||
| 3361 | admin_control(req) |
||
| 3362 | try: |
||
| 3363 | raw_json = req.stream.read().decode('utf-8') |
||
| 3364 | except Exception as ex: |
||
| 3365 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3366 | title='API.BAD_REQUEST', |
||
| 3367 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3368 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3369 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3370 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3371 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3372 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3373 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_ID') |
||
| 3374 | |||
| 3375 | new_values = json.loads(raw_json) |
||
| 3376 | |||
| 3377 | if 'name' not in new_values['data'].keys() or \ |
||
| 3378 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3379 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3380 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3381 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_LOAD_NAME') |
||
| 3382 | name = str.strip(new_values['data']['name']) |
||
| 3383 | |||
| 3384 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3385 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3386 | new_values['data']['power_point_id'] <= 0: |
||
| 3387 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3388 | description='API.INVALID_POWER_POINT_ID') |
||
| 3389 | power_point_id = new_values['data']['power_point_id'] |
||
| 3390 | |||
| 3391 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 3392 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 3393 | new_values['data']['meter_id'] <= 0: |
||
| 3394 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3395 | description='API.INVALID_METER_ID') |
||
| 3396 | meter_id = new_values['data']['meter_id'] |
||
| 3397 | |||
| 3398 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 3399 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 3400 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 3401 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3402 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 3403 | rated_input_power = Decimal(new_values['data']['rated_input_power']) |
||
| 3404 | |||
| 3405 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3406 | cursor = cnx.cursor() |
||
| 3407 | |||
| 3408 | cursor.execute(" SELECT name " |
||
| 3409 | " FROM tbl_energy_storage_containers " |
||
| 3410 | " WHERE id = %s ", (id_,)) |
||
| 3411 | if cursor.fetchone() is None: |
||
| 3412 | cursor.close() |
||
| 3413 | cnx.close() |
||
| 3414 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3415 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3416 | |||
| 3417 | cursor.execute(" SELECT name " |
||
| 3418 | " FROM tbl_energy_storage_containers_loads " |
||
| 3419 | " WHERE id = %s ", (lid,)) |
||
| 3420 | if cursor.fetchone() is None: |
||
| 3421 | cursor.close() |
||
| 3422 | cnx.close() |
||
| 3423 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3424 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND') |
||
| 3425 | |||
| 3426 | cursor.execute(" SELECT name " |
||
| 3427 | " FROM tbl_energy_storage_containers_loads " |
||
| 3428 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 3429 | (id_, name, lid)) |
||
| 3430 | if cursor.fetchone() is not None: |
||
| 3431 | cursor.close() |
||
| 3432 | cnx.close() |
||
| 3433 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3434 | description='API.ENERGY_STORAGE_CONTAINER_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 3435 | |||
| 3436 | cursor.execute(" SELECT name " |
||
| 3437 | " FROM tbl_points " |
||
| 3438 | " WHERE id = %s ", |
||
| 3439 | (power_point_id,)) |
||
| 3440 | if cursor.fetchone() is None: |
||
| 3441 | cursor.close() |
||
| 3442 | cnx.close() |
||
| 3443 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3444 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3445 | |||
| 3446 | cursor.execute(" SELECT name " |
||
| 3447 | " FROM tbl_meters " |
||
| 3448 | " WHERE id = %s ", |
||
| 3449 | (meter_id,)) |
||
| 3450 | if cursor.fetchone() is None: |
||
| 3451 | cursor.close() |
||
| 3452 | cnx.close() |
||
| 3453 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3454 | description='API.METER_NOT_FOUND') |
||
| 3455 | |||
| 3456 | update_row = (" UPDATE tbl_energy_storage_containers_loads " |
||
| 3457 | " SET name = %s, energy_storage_container_id = %s, power_point_id = %s, " |
||
| 3458 | " meter_id = %s, rated_input_power = %s " |
||
| 3459 | " WHERE id = %s ") |
||
| 3460 | cursor.execute(update_row, (name, |
||
| 3461 | id_, |
||
| 3462 | power_point_id, |
||
| 3463 | meter_id, |
||
| 3464 | rated_input_power, |
||
| 3465 | lid)) |
||
| 3466 | cnx.commit() |
||
| 3467 | |||
| 3468 | cursor.close() |
||
| 3469 | cnx.close() |
||
| 3470 | |||
| 3471 | resp.status = falcon.HTTP_200 |
||
| 3472 | |||
| 3473 | |||
| 3474 | class EnergyStorageContainerPowerconversionsystemCollection: |
||
| 3475 | def __init__(self): |
||
| 3476 | """Initializes Class""" |
||
| 3477 | pass |
||
| 3478 | |||
| 3479 | @staticmethod |
||
| 3480 | def on_options(req, resp, id_): |
||
| 3481 | resp.status = falcon.HTTP_200 |
||
| 3482 | |||
| 3483 | @staticmethod |
||
| 3484 | def on_get(req, resp, id_): |
||
| 3485 | access_control(req) |
||
| 3486 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3487 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3488 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3489 | |||
| 3490 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3491 | cursor = cnx.cursor() |
||
| 3492 | |||
| 3493 | cursor.execute(" SELECT name " |
||
| 3494 | " FROM tbl_energy_storage_containers " |
||
| 3495 | " WHERE id = %s ", (id_,)) |
||
| 3496 | if cursor.fetchone() is None: |
||
| 3497 | cursor.close() |
||
| 3498 | cnx.close() |
||
| 3499 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3500 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3501 | |||
| 3502 | # query point dict |
||
| 3503 | query = (" SELECT id, name " |
||
| 3504 | " FROM tbl_points ") |
||
| 3505 | cursor.execute(query) |
||
| 3506 | rows_points = cursor.fetchall() |
||
| 3507 | |||
| 3508 | point_dict = dict() |
||
| 3509 | if rows_points is not None and len(rows_points) > 0: |
||
| 3510 | for row in rows_points: |
||
| 3511 | point_dict[row[0]] = {"id": row[0], |
||
| 3512 | "name": row[1]} |
||
| 3513 | # query command dict |
||
| 3514 | query = (" SELECT id, name " |
||
| 3515 | " FROM tbl_commands ") |
||
| 3516 | cursor.execute(query) |
||
| 3517 | rows_commands = cursor.fetchall() |
||
| 3518 | |||
| 3519 | command_dict = dict() |
||
| 3520 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 3521 | for row in rows_commands: |
||
| 3522 | command_dict[row[0]] = {"id": row[0], |
||
| 3523 | "name": row[1]} |
||
| 3524 | |||
| 3525 | query = (" SELECT id, name, uuid, run_state_point_id, rated_output_power " |
||
| 3526 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3527 | " WHERE energy_storage_container_id = %s " |
||
| 3528 | " ORDER BY name ") |
||
| 3529 | cursor.execute(query, (id_,)) |
||
| 3530 | rows = cursor.fetchall() |
||
| 3531 | |||
| 3532 | result = list() |
||
| 3533 | if rows is not None and len(rows) > 0: |
||
| 3534 | for row in rows: |
||
| 3535 | meta_result = {"id": row[0], |
||
| 3536 | "name": row[1], |
||
| 3537 | "uuid": row[2], |
||
| 3538 | "run_state_point": point_dict.get(row[3]), |
||
| 3539 | "rated_output_power": row[4] |
||
| 3540 | } |
||
| 3541 | result.append(meta_result) |
||
| 3542 | |||
| 3543 | resp.text = json.dumps(result) |
||
| 3544 | |||
| 3545 | @staticmethod |
||
| 3546 | @user_logger |
||
| 3547 | def on_post(req, resp, id_): |
||
| 3548 | """Handles POST requests""" |
||
| 3549 | admin_control(req) |
||
| 3550 | try: |
||
| 3551 | raw_json = req.stream.read().decode('utf-8') |
||
| 3552 | except Exception as ex: |
||
| 3553 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3554 | title='API.BAD_REQUEST', |
||
| 3555 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3556 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3557 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3558 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3559 | |||
| 3560 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3561 | cursor = cnx.cursor() |
||
| 3562 | |||
| 3563 | cursor.execute(" SELECT name " |
||
| 3564 | " FROM tbl_energy_storage_containers " |
||
| 3565 | " WHERE id = %s ", (id_,)) |
||
| 3566 | if cursor.fetchone() is None: |
||
| 3567 | cursor.close() |
||
| 3568 | cnx.close() |
||
| 3569 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3570 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3571 | |||
| 3572 | new_values = json.loads(raw_json) |
||
| 3573 | |||
| 3574 | if 'name' not in new_values['data'].keys() or \ |
||
| 3575 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3576 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3577 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3578 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NAME') |
||
| 3579 | name = str.strip(new_values['data']['name']) |
||
| 3580 | |||
| 3581 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 3582 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 3583 | new_values['data']['run_state_point_id'] <= 0: |
||
| 3584 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3585 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 3586 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 3587 | |||
| 3588 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 3589 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 3590 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 3591 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3592 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 3593 | rated_output_power = Decimal(new_values['data']['rated_output_power']) |
||
| 3594 | |||
| 3595 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3596 | cursor = cnx.cursor() |
||
| 3597 | |||
| 3598 | cursor.execute(" SELECT name " |
||
| 3599 | " FROM tbl_energy_storage_containers " |
||
| 3600 | " WHERE id = %s ", |
||
| 3601 | (id_,)) |
||
| 3602 | if cursor.fetchone() is None: |
||
| 3603 | cursor.close() |
||
| 3604 | cnx.close() |
||
| 3605 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3606 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3607 | |||
| 3608 | cursor.execute(" SELECT name " |
||
| 3609 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3610 | " WHERE energy_storage_container_id = %s AND name = %s ", |
||
| 3611 | (id_, name,)) |
||
| 3612 | if cursor.fetchone() is not None: |
||
| 3613 | cursor.close() |
||
| 3614 | cnx.close() |
||
| 3615 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3616 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NAME_IS_ALREADY_IN_USE') |
||
| 3617 | |||
| 3618 | add_values = (" INSERT INTO tbl_energy_storage_containers_power_conversion_systems " |
||
| 3619 | " (name, uuid, energy_storage_container_id, run_state_point_id, rated_output_power) " |
||
| 3620 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 3621 | cursor.execute(add_values, (name, |
||
| 3622 | str(uuid.uuid4()), |
||
| 3623 | id_, |
||
| 3624 | run_state_point_id, |
||
| 3625 | rated_output_power |
||
| 3626 | )) |
||
| 3627 | new_id = cursor.lastrowid |
||
| 3628 | cnx.commit() |
||
| 3629 | cursor.close() |
||
| 3630 | cnx.close() |
||
| 3631 | |||
| 3632 | resp.status = falcon.HTTP_201 |
||
| 3633 | resp.location = '/energystoragecontainerpowerconversionsystems/' + str(new_id) |
||
| 3634 | |||
| 3635 | |||
| 3636 | class EnergyStorageContainerPowerconversionsystemItem: |
||
| 3637 | def __init__(self): |
||
| 3638 | """Initializes Class""" |
||
| 3639 | pass |
||
| 3640 | |||
| 3641 | @staticmethod |
||
| 3642 | def on_options(req, resp, id_, pid): |
||
| 3643 | resp.status = falcon.HTTP_200 |
||
| 3644 | |||
| 3645 | @staticmethod |
||
| 3646 | def on_get(req, resp, id_, pid): |
||
| 3647 | access_control(req) |
||
| 3648 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3650 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3651 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3652 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3653 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 3654 | |||
| 3655 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3656 | cursor = cnx.cursor() |
||
| 3657 | |||
| 3658 | cursor.execute(" SELECT name " |
||
| 3659 | " FROM tbl_energy_storage_containers " |
||
| 3660 | " WHERE id = %s ", (id_,)) |
||
| 3661 | if cursor.fetchone() is None: |
||
| 3662 | cursor.close() |
||
| 3663 | cnx.close() |
||
| 3664 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3665 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3666 | |||
| 3667 | query = (" SELECT id, name, uuid " |
||
| 3668 | " FROM tbl_energy_storage_containers ") |
||
| 3669 | cursor.execute(query) |
||
| 3670 | rows_energystoragecontainers = cursor.fetchall() |
||
| 3671 | |||
| 3672 | energy_storage_container_dict = dict() |
||
| 3673 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 3674 | for row in rows_energystoragecontainers: |
||
| 3675 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 3676 | "name": row[1], |
||
| 3677 | "uuid": row[2]} |
||
| 3678 | query = (" SELECT id, name, uuid " |
||
| 3679 | " FROM tbl_meters ") |
||
| 3680 | cursor.execute(query) |
||
| 3681 | rows_meters = cursor.fetchall() |
||
| 3682 | |||
| 3683 | meter_dict = dict() |
||
| 3684 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3685 | for row in rows_meters: |
||
| 3686 | meter_dict[row[0]] = {"id": row[0], |
||
| 3687 | "name": row[1], |
||
| 3688 | "uuid": row[2]} |
||
| 3689 | # query point dict |
||
| 3690 | query = (" SELECT id, name " |
||
| 3691 | " FROM tbl_points ") |
||
| 3692 | cursor.execute(query) |
||
| 3693 | rows_points = cursor.fetchall() |
||
| 3694 | |||
| 3695 | point_dict = dict() |
||
| 3696 | if rows_points is not None and len(rows_points) > 0: |
||
| 3697 | for row in rows_points: |
||
| 3698 | point_dict[row[0]] = {"id": row[0], |
||
| 3699 | "name": row[1]} |
||
| 3700 | |||
| 3701 | # query command dict |
||
| 3702 | query = (" SELECT id, name " |
||
| 3703 | " FROM tbl_commands ") |
||
| 3704 | cursor.execute(query) |
||
| 3705 | rows_commands = cursor.fetchall() |
||
| 3706 | |||
| 3707 | command_dict = dict() |
||
| 3708 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 3709 | for row in rows_commands: |
||
| 3710 | command_dict[row[0]] = {"id": row[0], |
||
| 3711 | "name": row[1]} |
||
| 3712 | |||
| 3713 | query = (" SELECT id, name, uuid, energy_storage_container_id, run_state_point_id, rated_output_power " |
||
| 3714 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3715 | " WHERE id = %s ") |
||
| 3716 | cursor.execute(query, (pid,)) |
||
| 3717 | row = cursor.fetchone() |
||
| 3718 | cursor.close() |
||
| 3719 | cnx.close() |
||
| 3720 | |||
| 3721 | if row is None: |
||
| 3722 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3723 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 3724 | else: |
||
| 3725 | meta_result = {"id": row[0], |
||
| 3726 | "name": row[1], |
||
| 3727 | "uuid": row[2], |
||
| 3728 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
||
| 3729 | "run_state_point": point_dict.get(row[4]), |
||
| 3730 | "rated_output_power": row[5] |
||
| 3731 | } |
||
| 3732 | |||
| 3733 | resp.text = json.dumps(meta_result) |
||
| 3734 | |||
| 3735 | @staticmethod |
||
| 3736 | @user_logger |
||
| 3737 | def on_delete(req, resp, id_, pid): |
||
| 3738 | admin_control(req) |
||
| 3739 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3740 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3741 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3742 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3743 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3744 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 3745 | |||
| 3746 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3747 | cursor = cnx.cursor() |
||
| 3748 | |||
| 3749 | cursor.execute(" SELECT name " |
||
| 3750 | " FROM tbl_energy_storage_containers " |
||
| 3751 | " WHERE id = %s ", (id_,)) |
||
| 3752 | if cursor.fetchone() is None: |
||
| 3753 | cursor.close() |
||
| 3754 | cnx.close() |
||
| 3755 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3756 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3757 | |||
| 3758 | cursor.execute(" SELECT name " |
||
| 3759 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3760 | " WHERE id = %s ", (pid,)) |
||
| 3761 | if cursor.fetchone() is None: |
||
| 3762 | cursor.close() |
||
| 3763 | cnx.close() |
||
| 3764 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3765 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 3766 | |||
| 3767 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3768 | " WHERE id = %s ", (pid,)) |
||
| 3769 | cnx.commit() |
||
| 3770 | |||
| 3771 | cursor.close() |
||
| 3772 | cnx.close() |
||
| 3773 | |||
| 3774 | resp.status = falcon.HTTP_204 |
||
| 3775 | |||
| 3776 | @staticmethod |
||
| 3777 | @user_logger |
||
| 3778 | def on_put(req, resp, id_, pid): |
||
| 3779 | """Handles PUT requests""" |
||
| 3780 | admin_control(req) |
||
| 3781 | try: |
||
| 3782 | raw_json = req.stream.read().decode('utf-8') |
||
| 3783 | except Exception as ex: |
||
| 3784 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3785 | title='API.BAD_REQUEST', |
||
| 3786 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3787 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3788 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3789 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3790 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3791 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3792 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_ID') |
||
| 3793 | |||
| 3794 | new_values = json.loads(raw_json) |
||
| 3795 | |||
| 3796 | if 'name' not in new_values['data'].keys() or \ |
||
| 3797 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3798 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3799 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3800 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NAME') |
||
| 3801 | name = str.strip(new_values['data']['name']) |
||
| 3802 | |||
| 3803 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 3804 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 3805 | new_values['data']['run_state_point_id'] <= 0: |
||
| 3806 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3807 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 3808 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 3809 | |||
| 3810 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 3811 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 3812 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 3813 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3814 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 3815 | rated_output_power = Decimal(new_values['data']['rated_output_power']) |
||
| 3816 | |||
| 3817 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3818 | cursor = cnx.cursor() |
||
| 3819 | |||
| 3820 | cursor.execute(" SELECT name " |
||
| 3821 | " FROM tbl_energy_storage_containers " |
||
| 3822 | " WHERE id = %s ", (id_,)) |
||
| 3823 | if cursor.fetchone() is None: |
||
| 3824 | cursor.close() |
||
| 3825 | cnx.close() |
||
| 3826 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3827 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3828 | |||
| 3829 | cursor.execute(" SELECT name " |
||
| 3830 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3831 | " WHERE id = %s ", (pid,)) |
||
| 3832 | if cursor.fetchone() is None: |
||
| 3833 | cursor.close() |
||
| 3834 | cnx.close() |
||
| 3835 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3836 | description='API.ENERGY_STORAGE_CONTAINER_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 3837 | |||
| 3838 | cursor.execute(" SELECT name " |
||
| 3839 | " FROM tbl_energy_storage_containers_power_conversion_systems " |
||
| 3840 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
||
| 3841 | (id_, name, pid)) |
||
| 3842 | if cursor.fetchone() is not None: |
||
| 3843 | cursor.close() |
||
| 3844 | cnx.close() |
||
| 3845 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3846 | description='API.ENERGY_STORAGE_CONTAINER_PCS_NAME_IS_ALREADY_IN_USE') |
||
| 3847 | |||
| 3848 | update_row = (" UPDATE tbl_energy_storage_containers_power_conversion_systems " |
||
| 3849 | " SET name = %s, energy_storage_container_id = %s, run_state_point_id = %s, " |
||
| 3850 | " rated_output_power = %s " |
||
| 3851 | " WHERE id = %s ") |
||
| 3852 | cursor.execute(update_row, (name, |
||
| 3853 | id_, |
||
| 3854 | run_state_point_id, |
||
| 3855 | rated_output_power, |
||
| 3856 | pid)) |
||
| 3857 | cnx.commit() |
||
| 3858 | |||
| 3859 | cursor.close() |
||
| 3860 | cnx.close() |
||
| 3861 | |||
| 3862 | resp.status = falcon.HTTP_200 |
||
| 3863 | |||
| 3864 | |||
| 3865 | View Code Duplication | class EnergyStorageContainerScheduleCollection: |
|
| 3866 | def __init__(self): |
||
| 3867 | """Initializes Class""" |
||
| 3868 | pass |
||
| 3869 | |||
| 3870 | @staticmethod |
||
| 3871 | def on_options(req, resp, id_): |
||
| 3872 | resp.status = falcon.HTTP_200 |
||
| 3873 | |||
| 3874 | @staticmethod |
||
| 3875 | def on_get(req, resp, id_): |
||
| 3876 | access_control(req) |
||
| 3877 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3878 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3879 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3880 | |||
| 3881 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3882 | cursor = cnx.cursor() |
||
| 3883 | |||
| 3884 | cursor.execute(" SELECT name " |
||
| 3885 | " FROM tbl_energy_storage_containers " |
||
| 3886 | " WHERE id = %s ", (id_,)) |
||
| 3887 | if cursor.fetchone() is None: |
||
| 3888 | cursor.close() |
||
| 3889 | cnx.close() |
||
| 3890 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3891 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3892 | |||
| 3893 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 3894 | " FROM tbl_energy_storage_containers_schedules " |
||
| 3895 | " WHERE energy_storage_container_id = %s " |
||
| 3896 | " ORDER BY start_time_of_day ") |
||
| 3897 | cursor.execute(query, (id_,)) |
||
| 3898 | rows = cursor.fetchall() |
||
| 3899 | |||
| 3900 | result = list() |
||
| 3901 | if rows is not None and len(rows) > 0: |
||
| 3902 | for row in rows: |
||
| 3903 | meta_result = {"id": row[0], |
||
| 3904 | "start_time_of_day": str(row[1]), |
||
| 3905 | "end_time_of_day": str(row[2]), |
||
| 3906 | "peak_type": row[3], |
||
| 3907 | "power": row[4], |
||
| 3908 | } |
||
| 3909 | result.append(meta_result) |
||
| 3910 | |||
| 3911 | resp.text = json.dumps(result) |
||
| 3912 | |||
| 3913 | @staticmethod |
||
| 3914 | @user_logger |
||
| 3915 | def on_post(req, resp, id_): |
||
| 3916 | """Handles POST requests""" |
||
| 3917 | admin_control(req) |
||
| 3918 | try: |
||
| 3919 | raw_json = req.stream.read().decode('utf-8') |
||
| 3920 | except Exception as ex: |
||
| 3921 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3922 | title='API.BAD_REQUEST', |
||
| 3923 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3924 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3925 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3926 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3927 | |||
| 3928 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3929 | cursor = cnx.cursor() |
||
| 3930 | |||
| 3931 | cursor.execute(" SELECT name " |
||
| 3932 | " FROM tbl_energy_storage_containers " |
||
| 3933 | " WHERE id = %s ", (id_,)) |
||
| 3934 | if cursor.fetchone() is None: |
||
| 3935 | cursor.close() |
||
| 3936 | cnx.close() |
||
| 3937 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3938 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3939 | |||
| 3940 | new_values = json.loads(raw_json) |
||
| 3941 | |||
| 3942 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3943 | cursor = cnx.cursor() |
||
| 3944 | |||
| 3945 | cursor.execute(" SELECT name " |
||
| 3946 | " FROM tbl_energy_storage_containers " |
||
| 3947 | " WHERE id = %s ", |
||
| 3948 | (id_,)) |
||
| 3949 | if cursor.fetchone() is None: |
||
| 3950 | cursor.close() |
||
| 3951 | cnx.close() |
||
| 3952 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3953 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 3954 | |||
| 3955 | add_schedule = (" INSERT INTO tbl_energy_storage_containers_schedules " |
||
| 3956 | " (energy_storage_container_id, start_time_of_day, end_time_of_day, peak_type, power) " |
||
| 3957 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 3958 | cursor.execute(add_schedule, (id_, |
||
| 3959 | new_values['data']['start_time_of_day'], |
||
| 3960 | new_values['data']['end_time_of_day'], |
||
| 3961 | new_values['data']['peak_type'], |
||
| 3962 | new_values['data']['power'])) |
||
| 3963 | new_id = cursor.lastrowid |
||
| 3964 | cnx.commit() |
||
| 3965 | cursor.close() |
||
| 3966 | cnx.close() |
||
| 3967 | resp.status = falcon.HTTP_201 |
||
| 3968 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
||
| 3969 | |||
| 3970 | |||
| 3971 | View Code Duplication | class EnergyStorageContainerScheduleItem: |
|
| 3972 | def __init__(self): |
||
| 3973 | """Initializes Class""" |
||
| 3974 | pass |
||
| 3975 | |||
| 3976 | @staticmethod |
||
| 3977 | def on_options(req, resp, id_, sid): |
||
| 3978 | resp.status = falcon.HTTP_200 |
||
| 3979 | |||
| 3980 | @staticmethod |
||
| 3981 | def on_get(req, resp, id_, sid): |
||
| 3982 | access_control(req) |
||
| 3983 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3985 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 3986 | if not sid.isdigit() or int(sid) <= 0: |
||
| 3987 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3988 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 3989 | |||
| 3990 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3991 | cursor = cnx.cursor() |
||
| 3992 | |||
| 3993 | cursor.execute(" SELECT name " |
||
| 3994 | " FROM tbl_energy_storage_containers " |
||
| 3995 | " WHERE id = %s ", (id_,)) |
||
| 3996 | if cursor.fetchone() is None: |
||
| 3997 | cursor.close() |
||
| 3998 | cnx.close() |
||
| 3999 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4000 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4001 | |||
| 4002 | query = (" SELECT id, name, uuid " |
||
| 4003 | " FROM tbl_energy_storage_containers ") |
||
| 4004 | cursor.execute(query) |
||
| 4005 | rows_energystoragecontainers = cursor.fetchall() |
||
| 4006 | |||
| 4007 | energy_storage_container_dict = dict() |
||
| 4008 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 4009 | for row in rows_energystoragecontainers: |
||
| 4010 | energy_storage_container_dict[row[0]] = {"id": row[0], |
||
| 4011 | "name": row[1], |
||
| 4012 | "uuid": row[2]} |
||
| 4013 | |||
| 4014 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 4015 | " FROM tbl_energy_storage_containers_schedules " |
||
| 4016 | " WHERE id = %s ") |
||
| 4017 | cursor.execute(query, (sid,)) |
||
| 4018 | row = cursor.fetchone() |
||
| 4019 | cursor.close() |
||
| 4020 | cnx.close() |
||
| 4021 | |||
| 4022 | if row is None: |
||
| 4023 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4024 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 4025 | else: |
||
| 4026 | meta_result = {"id": row[0], |
||
| 4027 | "start_time_of_day": str(row[1]), |
||
| 4028 | "end_time_of_day": str(row[2]), |
||
| 4029 | "peak_type": row[3], |
||
| 4030 | "power": row[4]} |
||
| 4031 | |||
| 4032 | resp.text = json.dumps(meta_result) |
||
| 4033 | |||
| 4034 | @staticmethod |
||
| 4035 | @user_logger |
||
| 4036 | def on_delete(req, resp, id_, sid): |
||
| 4037 | admin_control(req) |
||
| 4038 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4039 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4040 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4041 | if not sid.isdigit() or int(sid) <= 0: |
||
| 4042 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4043 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 4044 | |||
| 4045 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4046 | cursor = cnx.cursor() |
||
| 4047 | |||
| 4048 | cursor.execute(" SELECT name " |
||
| 4049 | " FROM tbl_energy_storage_containers " |
||
| 4050 | " WHERE id = %s ", (id_,)) |
||
| 4051 | if cursor.fetchone() is None: |
||
| 4052 | cursor.close() |
||
| 4053 | cnx.close() |
||
| 4054 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4055 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4056 | |||
| 4057 | cursor.execute(" SELECT id " |
||
| 4058 | " FROM tbl_energy_storage_containers_schedules " |
||
| 4059 | " WHERE id = %s ", (sid,)) |
||
| 4060 | if cursor.fetchone() is None: |
||
| 4061 | cursor.close() |
||
| 4062 | cnx.close() |
||
| 4063 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4064 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 4065 | |||
| 4066 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_schedules " |
||
| 4067 | " WHERE id = %s ", (sid,)) |
||
| 4068 | cnx.commit() |
||
| 4069 | |||
| 4070 | cursor.close() |
||
| 4071 | cnx.close() |
||
| 4072 | |||
| 4073 | resp.status = falcon.HTTP_204 |
||
| 4074 | |||
| 4075 | @staticmethod |
||
| 4076 | @user_logger |
||
| 4077 | def on_put(req, resp, id_, sid): |
||
| 4078 | """Handles PUT requests""" |
||
| 4079 | admin_control(req) |
||
| 4080 | try: |
||
| 4081 | raw_json = req.stream.read().decode('utf-8') |
||
| 4082 | except Exception as ex: |
||
| 4083 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4084 | title='API.BAD_REQUEST', |
||
| 4085 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4086 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4087 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4088 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4089 | if not sid.isdigit() or int(sid) <= 0: |
||
| 4090 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4091 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_SCHEDULE_ID') |
||
| 4092 | |||
| 4093 | new_values = json.loads(raw_json) |
||
| 4094 | |||
| 4095 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
||
| 4096 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
||
| 4097 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
||
| 4098 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4099 | description='API.INVALID_START_TIME_OF_DAY') |
||
| 4100 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
||
| 4101 | |||
| 4102 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
||
| 4103 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
||
| 4104 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
||
| 4105 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4106 | description='API.INVALID_END_TIME_OF_DAY') |
||
| 4107 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
||
| 4108 | |||
| 4109 | if 'peak_type' not in new_values['data'].keys() or \ |
||
| 4110 | not isinstance(new_values['data']['peak_type'], str) or \ |
||
| 4111 | len(str.strip(new_values['data']['peak_type'])) == 0: |
||
| 4112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4113 | description='API.INVALID_PEAK_TYPE') |
||
| 4114 | peak_type = str.strip(new_values['data']['peak_type']) |
||
| 4115 | |||
| 4116 | if 'power' not in new_values['data'].keys() or \ |
||
| 4117 | not (isinstance(new_values['data']['power'], float) or |
||
| 4118 | isinstance(new_values['data']['power'], int)): |
||
| 4119 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4120 | description='API.INVALID_POWER') |
||
| 4121 | power = Decimal(new_values['data']['power']) |
||
| 4122 | |||
| 4123 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4124 | cursor = cnx.cursor() |
||
| 4125 | |||
| 4126 | cursor.execute(" SELECT name " |
||
| 4127 | " FROM tbl_energy_storage_containers " |
||
| 4128 | " WHERE id = %s ", (id_,)) |
||
| 4129 | if cursor.fetchone() is None: |
||
| 4130 | cursor.close() |
||
| 4131 | cnx.close() |
||
| 4132 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4133 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4134 | |||
| 4135 | cursor.execute(" SELECT id " |
||
| 4136 | " FROM tbl_energy_storage_containers_schedules " |
||
| 4137 | " WHERE id = %s ", (sid,)) |
||
| 4138 | if cursor.fetchone() is None: |
||
| 4139 | cursor.close() |
||
| 4140 | cnx.close() |
||
| 4141 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4142 | description='API.ENERGY_STORAGE_CONTAINER_SCHEDULE_NOT_FOUND') |
||
| 4143 | |||
| 4144 | update_row = (" UPDATE tbl_energy_storage_containers_schedules " |
||
| 4145 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
||
| 4146 | " WHERE id = %s ") |
||
| 4147 | cursor.execute(update_row, (start_time_of_day, |
||
| 4148 | end_time_of_day, |
||
| 4149 | peak_type, |
||
| 4150 | power, |
||
| 4151 | sid)) |
||
| 4152 | cnx.commit() |
||
| 4153 | |||
| 4154 | cursor.close() |
||
| 4155 | cnx.close() |
||
| 4156 | |||
| 4157 | resp.status = falcon.HTTP_200 |
||
| 4158 | |||
| 4159 | |||
| 4160 | class EnergyStorageContainerClone: |
||
| 4161 | def __init__(self): |
||
| 4162 | """Initializes Class""" |
||
| 4163 | pass |
||
| 4164 | |||
| 4165 | @staticmethod |
||
| 4166 | def on_options(req, resp, id_): |
||
| 4167 | resp.status = falcon.HTTP_200 |
||
| 4168 | |||
| 4169 | @staticmethod |
||
| 4170 | @user_logger |
||
| 4171 | def on_post(req, resp, id_): |
||
| 4172 | """Handles POST requests""" |
||
| 4173 | access_control(req) |
||
| 4174 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4176 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4177 | |||
| 4178 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4179 | cursor = cnx.cursor() |
||
| 4180 | |||
| 4181 | query = (" SELECT id, name, uuid, " |
||
| 4182 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 4183 | " FROM tbl_energy_storage_containers " |
||
| 4184 | " WHERE id = %s ") |
||
| 4185 | cursor.execute(query, (id_,)) |
||
| 4186 | row = cursor.fetchone() |
||
| 4187 | |||
| 4188 | if row is None: |
||
| 4189 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4190 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4191 | else: |
||
| 4192 | meta_result = {"id": row[0], |
||
| 4193 | "name": row[1], |
||
| 4194 | "uuid": row[2], |
||
| 4195 | "rated_capacity": row[3], |
||
| 4196 | "rated_power": row[4], |
||
| 4197 | "contact_id": row[5], |
||
| 4198 | "cost_center_id": row[6], |
||
| 4199 | "description": row[7]} |
||
| 4200 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 4201 | if config.utc_offset[0] == '-': |
||
| 4202 | timezone_offset = -timezone_offset |
||
| 4203 | new_name = str.strip(meta_result['name']) + \ |
||
| 4204 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
| 4205 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
||
| 4206 | " (name, uuid, rated_capacity, rated_power, contact_id, " |
||
| 4207 | " cost_center_id, description) " |
||
| 4208 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 4209 | cursor.execute(add_values, (new_name, |
||
| 4210 | str(uuid.uuid4()), |
||
| 4211 | meta_result['rated_capacity'], |
||
| 4212 | meta_result['rated_power'], |
||
| 4213 | meta_result['contact_id'], |
||
| 4214 | meta_result['cost_center_id'], |
||
| 4215 | meta_result['description'])) |
||
| 4216 | new_id = cursor.lastrowid |
||
| 4217 | cnx.commit() |
||
| 4218 | cursor.close() |
||
| 4219 | cnx.close() |
||
| 4220 | |||
| 4221 | resp.status = falcon.HTTP_201 |
||
| 4222 | resp.location = '/energystoragecontainers/' + str(new_id) |
||
| 4223 | |||
| 4224 | |||
| 4225 | class EnergyStorageContainerExport: |
||
| 4226 | def __init__(self): |
||
| 4227 | """"Initializes Class""" |
||
| 4228 | pass |
||
| 4229 | |||
| 4230 | @staticmethod |
||
| 4231 | def on_options(req, resp, id_): |
||
| 4232 | resp.status = falcon.HTTP_200 |
||
| 4233 | |||
| 4234 | View Code Duplication | @staticmethod |
|
| 4235 | def on_get(req, resp, id_): |
||
| 4236 | access_control(req) |
||
| 4237 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4238 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4239 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 4240 | |||
| 4241 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4242 | cursor = cnx.cursor() |
||
| 4243 | |||
| 4244 | query = (" SELECT id, name, uuid " |
||
| 4245 | " FROM tbl_contacts ") |
||
| 4246 | cursor.execute(query) |
||
| 4247 | rows_contacts = cursor.fetchall() |
||
| 4248 | |||
| 4249 | contact_dict = dict() |
||
| 4250 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 4251 | for row in rows_contacts: |
||
| 4252 | contact_dict[row[0]] = {"id": row[0], |
||
| 4253 | "name": row[1], |
||
| 4254 | "uuid": row[2]} |
||
| 4255 | |||
| 4256 | query = (" SELECT id, name, uuid " |
||
| 4257 | " FROM tbl_cost_centers ") |
||
| 4258 | cursor.execute(query) |
||
| 4259 | rows_cost_centers = cursor.fetchall() |
||
| 4260 | |||
| 4261 | cost_center_dict = dict() |
||
| 4262 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 4263 | for row in rows_cost_centers: |
||
| 4264 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 4265 | "name": row[1], |
||
| 4266 | "uuid": row[2]} |
||
| 4267 | |||
| 4268 | query = (" SELECT id, name, uuid, " |
||
| 4269 | " rated_capacity, rated_power, contact_id, cost_center_id, description " |
||
| 4270 | " FROM tbl_energy_storage_containers " |
||
| 4271 | " WHERE id = %s ") |
||
| 4272 | cursor.execute(query, (id_,)) |
||
| 4273 | row = cursor.fetchone() |
||
| 4274 | cursor.close() |
||
| 4275 | cnx.close() |
||
| 4276 | |||
| 4277 | if row is None: |
||
| 4278 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4279 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 4280 | else: |
||
| 4281 | meta_result = {"id": row[0], |
||
| 4282 | "name": row[1], |
||
| 4283 | "uuid": row[2], |
||
| 4284 | "rated_capacity": row[3], |
||
| 4285 | "rated_power": row[4], |
||
| 4286 | "contact": contact_dict.get(row[5], None), |
||
| 4287 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 4288 | "description": row[7]} |
||
| 4289 | |||
| 4290 | resp.text = json.dumps(meta_result) |
||
| 4291 | |||
| 4292 | |||
| 4293 | class EnergyStorageContainerImport: |
||
| 4294 | def __init__(self): |
||
| 4295 | """"Initializes Class""" |
||
| 4296 | pass |
||
| 4297 | |||
| 4298 | @staticmethod |
||
| 4299 | def on_options(req, resp): |
||
| 4300 | resp.status = falcon.HTTP_200 |
||
| 4301 | |||
| 4302 | @staticmethod |
||
| 4303 | @user_logger |
||
| 4304 | def on_post(req, resp): |
||
| 4305 | """Handles POST requests""" |
||
| 4306 | admin_control(req) |
||
| 4307 | try: |
||
| 4308 | raw_json = req.stream.read().decode('utf-8') |
||
| 4309 | except Exception as ex: |
||
| 4310 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4311 | title='API.BAD_REQUEST', |
||
| 4312 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4313 | |||
| 4314 | new_values = json.loads(raw_json) |
||
| 4315 | |||
| 4316 | if 'name' not in new_values.keys() or \ |
||
| 4317 | not isinstance(new_values['name'], str) or \ |
||
| 4318 | len(str.strip(new_values['name'])) == 0: |
||
| 4319 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4320 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME') |
||
| 4321 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 4322 | if config.utc_offset[0] == '-': |
||
| 4323 | timezone_offset = -timezone_offset |
||
| 4324 | name = str.strip(new_values['name']) + \ |
||
| 4325 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
| 4326 | |||
| 4327 | if 'rated_capacity' not in new_values.keys() or \ |
||
| 4328 | not (isinstance(new_values['rated_capacity'], float) or |
||
| 4329 | isinstance(new_values['rated_capacity'], int)) or \ |
||
| 4330 | new_values['rated_capacity'] < 0.0: |
||
| 4331 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4332 | description='API.INVALID_RATED_CAPACITY') |
||
| 4333 | rated_capacity = new_values['rated_capacity'] |
||
| 4334 | |||
| 4335 | if 'rated_power' not in new_values.keys() or \ |
||
| 4336 | not (isinstance(new_values['rated_power'], float) or |
||
| 4337 | isinstance(new_values['rated_power'], int)) or \ |
||
| 4338 | new_values['rated_power'] < 0.0: |
||
| 4339 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4340 | description='API.INVALID_RATED_POWER') |
||
| 4341 | rated_power = new_values['rated_power'] |
||
| 4342 | |||
| 4343 | if 'contact' not in new_values.keys() or \ |
||
| 4344 | 'id' not in new_values['contact'].keys() or \ |
||
| 4345 | not isinstance(new_values['contact']['id'], int) or \ |
||
| 4346 | new_values['contact']['id'] <= 0: |
||
| 4347 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4348 | description='API.INVALID_CONTACT_ID') |
||
| 4349 | contact_id = new_values['contact']['id'] |
||
| 4350 | |||
| 4351 | if 'cost_center' not in new_values.keys() or \ |
||
| 4352 | 'id' not in new_values['cost_center'].keys() or \ |
||
| 4353 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 4354 | new_values['cost_center']['id'] <= 0: |
||
| 4355 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4356 | description='API.INVALID_COST_CENTER_ID') |
||
| 4357 | cost_center_id = new_values['cost_center']['id'] |
||
| 4358 | |||
| 4359 | if 'description' in new_values.keys() and \ |
||
| 4360 | new_values['description'] is not None and \ |
||
| 4361 | len(str(new_values['description'])) > 0: |
||
| 4362 | description = str.strip(new_values['description']) |
||
| 4363 | else: |
||
| 4364 | description = None |
||
| 4365 | |||
| 4366 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4367 | cursor = cnx.cursor() |
||
| 4368 | |||
| 4369 | cursor.execute(" SELECT name " |
||
| 4370 | " FROM tbl_energy_storage_containers " |
||
| 4371 | " WHERE name = %s ", (name,)) |
||
| 4372 | if cursor.fetchone() is not None: |
||
| 4373 | cursor.close() |
||
| 4374 | cnx.close() |
||
| 4375 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4376 | description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE') |
||
| 4377 | |||
| 4378 | cursor.execute(" SELECT name " |
||
| 4379 | " FROM tbl_contacts " |
||
| 4380 | " WHERE id = %s ", |
||
| 4381 | (new_values['contact']['id'],)) |
||
| 4382 | row = cursor.fetchone() |
||
| 4383 | if row is None: |
||
| 4384 | cursor.close() |
||
| 4385 | cnx.close() |
||
| 4386 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4387 | description='API.CONTACT_NOT_FOUND') |
||
| 4388 | |||
| 4389 | cursor.execute(" SELECT name " |
||
| 4390 | " FROM tbl_cost_centers " |
||
| 4391 | " WHERE id = %s ", |
||
| 4392 | (new_values['cost_center']['id'],)) |
||
| 4393 | row = cursor.fetchone() |
||
| 4394 | if row is None: |
||
| 4395 | cursor.close() |
||
| 4396 | cnx.close() |
||
| 4397 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4398 | description='API.COST_CENTER_NOT_FOUND') |
||
| 4399 | |||
| 4400 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
||
| 4401 | " (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, description) " |
||
| 4402 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 4403 | cursor.execute(add_values, (name, |
||
| 4404 | str(uuid.uuid4()), |
||
| 4405 | rated_capacity, |
||
| 4406 | rated_power, |
||
| 4407 | contact_id, |
||
| 4408 | cost_center_id, |
||
| 4409 | description)) |
||
| 4410 | new_id = cursor.lastrowid |
||
| 4411 | cnx.commit() |
||
| 4412 | cursor.close() |
||
| 4413 | cnx.close() |
||
| 4414 | |||
| 4415 | resp.status = falcon.HTTP_201 |
||
| 4416 | resp.location = '/energystoragecontainers/' + str(new_id) |
||
| 4417 |