| Total Complexity | 1456 |
| Total Lines | 7479 |
| Duplicated Lines | 65.21 % |
| 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.microgrid 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 | from datetime import datetime, timedelta |
||
| 3 | import falcon |
||
| 4 | import mysql.connector |
||
| 5 | import simplejson as json |
||
| 6 | from core.useractivity import user_logger, admin_control, access_control |
||
| 7 | import config |
||
| 8 | |||
| 9 | |||
| 10 | class MicrogridCollection: |
||
| 11 | """ |
||
| 12 | Microgrid Collection Resource |
||
| 13 | |||
| 14 | This class handles CRUD operations for microgrid collection. |
||
| 15 | It provides endpoints for listing all microgrids and creating new ones. |
||
| 16 | Microgrids represent localized energy systems that can operate independently |
||
| 17 | or in conjunction with the main electrical grid, typically including |
||
| 18 | distributed generation, energy storage, and load management capabilities. |
||
| 19 | """ |
||
| 20 | def __init__(self): |
||
| 21 | pass |
||
| 22 | |||
| 23 | @staticmethod |
||
| 24 | def on_options(req, resp): |
||
| 25 | """ |
||
| 26 | Handle OPTIONS request for CORS preflight |
||
| 27 | |||
| 28 | Args: |
||
| 29 | req: Falcon request object |
||
| 30 | resp: Falcon response object |
||
| 31 | """ |
||
| 32 | _ = req |
||
| 33 | resp.status = falcon.HTTP_200 |
||
| 34 | |||
| 35 | View Code Duplication | @staticmethod |
|
|
|
|||
| 36 | def on_get(req, resp): |
||
| 37 | """ |
||
| 38 | Handle GET requests to retrieve all microgrids |
||
| 39 | |||
| 40 | Returns a list of all microgrids with their complete information including: |
||
| 41 | - Microgrid ID, name, and UUID |
||
| 42 | - Associated contact and cost center information |
||
| 43 | - Microgrid specifications and parameters |
||
| 44 | - Related equipment and meter associations |
||
| 45 | - SVG diagram information for visualization |
||
| 46 | |||
| 47 | Args: |
||
| 48 | req: Falcon request object |
||
| 49 | resp: Falcon response object |
||
| 50 | """ |
||
| 51 | access_control(req) |
||
| 52 | |||
| 53 | search_query = req.get_param('q', default=None) |
||
| 54 | if search_query is not None and len(search_query.strip()) > 0: |
||
| 55 | search_query = search_query.strip() |
||
| 56 | else: |
||
| 57 | search_query = '' |
||
| 58 | |||
| 59 | # Connect to database |
||
| 60 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 61 | cursor = cnx.cursor() |
||
| 62 | |||
| 63 | # Query to retrieve all contacts for reference |
||
| 64 | query = (" SELECT id, name, uuid " |
||
| 65 | " FROM tbl_contacts ") |
||
| 66 | cursor.execute(query) |
||
| 67 | rows_contacts = cursor.fetchall() |
||
| 68 | |||
| 69 | # Build contact dictionary for quick lookup by ID |
||
| 70 | contact_dict = dict() |
||
| 71 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 72 | for row in rows_contacts: |
||
| 73 | contact_dict[row[0]] = {"id": row[0], |
||
| 74 | "name": row[1], |
||
| 75 | "uuid": row[2]} |
||
| 76 | |||
| 77 | # Query to retrieve all cost centers for reference |
||
| 78 | query = (" SELECT id, name, uuid " |
||
| 79 | " FROM tbl_cost_centers ") |
||
| 80 | cursor.execute(query) |
||
| 81 | rows_cost_centers = cursor.fetchall() |
||
| 82 | |||
| 83 | # Build cost center dictionary for quick lookup by ID |
||
| 84 | cost_center_dict = dict() |
||
| 85 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 86 | for row in rows_cost_centers: |
||
| 87 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 88 | "name": row[1], |
||
| 89 | "uuid": row[2]} |
||
| 90 | |||
| 91 | # Initialize SVG dictionary for diagram references |
||
| 92 | svg_dict = dict() |
||
| 93 | |||
| 94 | query = (" SELECT id, name, uuid " |
||
| 95 | " FROM tbl_svgs ") |
||
| 96 | cursor.execute(query) |
||
| 97 | rows_svgs = cursor.fetchall() |
||
| 98 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 99 | for row in rows_svgs: |
||
| 100 | svg_dict[row[0]] = {"id": row[0], |
||
| 101 | "name": row[1], |
||
| 102 | "uuid": row[2]} |
||
| 103 | |||
| 104 | query = (" SELECT id, name, uuid, " |
||
| 105 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 106 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
||
| 107 | " phase_of_lifecycle, description " |
||
| 108 | " FROM tbl_microgrids ") |
||
| 109 | params = [] |
||
| 110 | if search_query: |
||
| 111 | query += " WHERE name LIKE %s OR address LIKE %s OR description LIKE %s " |
||
| 112 | params = [f'%{search_query}%', f'%{search_query}%', f'%{search_query}%'] |
||
| 113 | query += " ORDER BY id " |
||
| 114 | cursor.execute(query, params) |
||
| 115 | rows_microgrids = cursor.fetchall() |
||
| 116 | |||
| 117 | result = list() |
||
| 118 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 119 | for row in rows_microgrids: |
||
| 120 | meta_result = {"id": row[0], |
||
| 121 | "name": row[1], |
||
| 122 | "uuid": row[2], |
||
| 123 | "address": row[3], |
||
| 124 | "postal_code": row[4], |
||
| 125 | "latitude": row[5], |
||
| 126 | "longitude": row[6], |
||
| 127 | "rated_capacity": row[7], |
||
| 128 | "rated_power": row[8], |
||
| 129 | "contact": contact_dict.get(row[9], None), |
||
| 130 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 131 | "serial_number": row[11], |
||
| 132 | "svg": svg_dict.get(row[12], None), |
||
| 133 | "is_cost_data_displayed": bool(row[13]), |
||
| 134 | "phase_of_lifecycle": row[14], |
||
| 135 | "description": row[15], |
||
| 136 | "qrcode": 'microgrid:' + row[2]} |
||
| 137 | result.append(meta_result) |
||
| 138 | |||
| 139 | cursor.close() |
||
| 140 | cnx.close() |
||
| 141 | resp.text = json.dumps(result) |
||
| 142 | |||
| 143 | @staticmethod |
||
| 144 | @user_logger |
||
| 145 | def on_post(req, resp): |
||
| 146 | """Handles POST requests""" |
||
| 147 | admin_control(req) |
||
| 148 | try: |
||
| 149 | raw_json = req.stream.read().decode('utf-8') |
||
| 150 | except UnicodeDecodeError as ex: |
||
| 151 | print("Failed to decode request") |
||
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 153 | title='API.BAD_REQUEST', |
||
| 154 | description='API.INVALID_ENCODING') |
||
| 155 | except Exception as ex: |
||
| 156 | print(str(ex)) |
||
| 157 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 158 | title='API.BAD_REQUEST', |
||
| 159 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 160 | |||
| 161 | new_values = json.loads(raw_json) |
||
| 162 | |||
| 163 | if 'name' not in new_values['data'].keys() or \ |
||
| 164 | not isinstance(new_values['data']['name'], str) or \ |
||
| 165 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 166 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 167 | description='API.INVALID_MICROGRID_NAME') |
||
| 168 | name = str.strip(new_values['data']['name']) |
||
| 169 | |||
| 170 | if 'address' not in new_values['data'].keys() or \ |
||
| 171 | not isinstance(new_values['data']['address'], str) or \ |
||
| 172 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 174 | description='API.INVALID_ADDRESS_VALUE') |
||
| 175 | address = str.strip(new_values['data']['address']) |
||
| 176 | |||
| 177 | if 'postal_code' not in new_values['data'].keys() or \ |
||
| 178 | not isinstance(new_values['data']['postal_code'], str) or \ |
||
| 179 | len(str.strip(new_values['data']['postal_code'])) == 0: |
||
| 180 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 181 | description='API.INVALID_POSTAL_CODE_VALUE') |
||
| 182 | postal_code = str.strip(new_values['data']['postal_code']) |
||
| 183 | |||
| 184 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 185 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 186 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 187 | new_values['data']['latitude'] < -90.0 or \ |
||
| 188 | new_values['data']['latitude'] > 90.0: |
||
| 189 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 190 | description='API.INVALID_LATITUDE_VALUE') |
||
| 191 | latitude = new_values['data']['latitude'] |
||
| 192 | |||
| 193 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 194 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 195 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 196 | new_values['data']['longitude'] < -180.0 or \ |
||
| 197 | new_values['data']['longitude'] > 180.0: |
||
| 198 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 199 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 200 | longitude = new_values['data']['longitude'] |
||
| 201 | |||
| 202 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 203 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 204 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 205 | new_values['data']['rated_capacity'] <= 0.0: |
||
| 206 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 207 | description='API.INVALID_RATED_CAPACITY') |
||
| 208 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 209 | |||
| 210 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 211 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 212 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 213 | new_values['data']['rated_power'] <= 0.0: |
||
| 214 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 215 | description='API.INVALID_RATED_POWER_VALUE') |
||
| 216 | rated_power = new_values['data']['rated_power'] |
||
| 217 | |||
| 218 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 219 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 220 | new_values['data']['contact_id'] <= 0: |
||
| 221 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 222 | description='API.INVALID_CONTACT_ID') |
||
| 223 | contact_id = new_values['data']['contact_id'] |
||
| 224 | |||
| 225 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 226 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 227 | new_values['data']['cost_center_id'] <= 0: |
||
| 228 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 229 | description='API.INVALID_COST_CENTER_ID') |
||
| 230 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 231 | |||
| 232 | if 'serial_number' not in new_values['data'].keys() or \ |
||
| 233 | not isinstance(new_values['data']['serial_number'], str) or \ |
||
| 234 | len(str.strip(new_values['data']['serial_number'])) == 0: |
||
| 235 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 236 | description='API.INVALID_SERIAL_NUMBER') |
||
| 237 | serial_number = str.strip(new_values['data']['serial_number']) |
||
| 238 | |||
| 239 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 240 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 241 | new_values['data']['svg_id'] <= 0: |
||
| 242 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 243 | description='API.INVALID_SVG_ID') |
||
| 244 | svg_id = new_values['data']['svg_id'] |
||
| 245 | |||
| 246 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
| 247 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
| 248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 249 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
| 250 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
| 251 | |||
| 252 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
| 253 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
| 254 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
| 255 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 256 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
| 257 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
| 258 | |||
| 259 | if 'description' in new_values['data'].keys() and \ |
||
| 260 | new_values['data']['description'] is not None and \ |
||
| 261 | len(str(new_values['data']['description'])) > 0: |
||
| 262 | description = str.strip(new_values['data']['description']) |
||
| 263 | else: |
||
| 264 | description = None |
||
| 265 | |||
| 266 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 267 | cursor = cnx.cursor() |
||
| 268 | |||
| 269 | cursor.execute(" SELECT name " |
||
| 270 | " FROM tbl_microgrids " |
||
| 271 | " WHERE name = %s ", (name,)) |
||
| 272 | if cursor.fetchone() is not None: |
||
| 273 | cursor.close() |
||
| 274 | cnx.close() |
||
| 275 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 276 | description='API.MICROGRID_NAME_IS_ALREADY_IN_USE') |
||
| 277 | |||
| 278 | cursor.execute(" SELECT name " |
||
| 279 | " FROM tbl_contacts " |
||
| 280 | " WHERE id = %s ", |
||
| 281 | (new_values['data']['contact_id'],)) |
||
| 282 | row = cursor.fetchone() |
||
| 283 | if row is None: |
||
| 284 | cursor.close() |
||
| 285 | cnx.close() |
||
| 286 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 287 | description='API.CONTACT_NOT_FOUND') |
||
| 288 | |||
| 289 | cursor.execute(" SELECT name " |
||
| 290 | " FROM tbl_cost_centers " |
||
| 291 | " WHERE id = %s ", |
||
| 292 | (new_values['data']['cost_center_id'],)) |
||
| 293 | row = cursor.fetchone() |
||
| 294 | if row is None: |
||
| 295 | cursor.close() |
||
| 296 | cnx.close() |
||
| 297 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 298 | description='API.COST_CENTER_NOT_FOUND') |
||
| 299 | |||
| 300 | cursor.execute(" SELECT name " |
||
| 301 | " FROM tbl_svgs " |
||
| 302 | " WHERE id = %s ", |
||
| 303 | (svg_id,)) |
||
| 304 | row = cursor.fetchone() |
||
| 305 | if row is None: |
||
| 306 | cursor.close() |
||
| 307 | cnx.close() |
||
| 308 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 309 | description='API.SVG_NOT_FOUND') |
||
| 310 | |||
| 311 | add_values = (" INSERT INTO tbl_microgrids " |
||
| 312 | " (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 313 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
||
| 314 | " phase_of_lifecycle, description) " |
||
| 315 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 316 | cursor.execute(add_values, (name, |
||
| 317 | str(uuid.uuid4()), |
||
| 318 | address, |
||
| 319 | postal_code, |
||
| 320 | latitude, |
||
| 321 | longitude, |
||
| 322 | rated_capacity, |
||
| 323 | rated_power, |
||
| 324 | contact_id, |
||
| 325 | cost_center_id, |
||
| 326 | serial_number, |
||
| 327 | svg_id, |
||
| 328 | is_cost_data_displayed, |
||
| 329 | phase_of_lifecycle, |
||
| 330 | description)) |
||
| 331 | new_id = cursor.lastrowid |
||
| 332 | cnx.commit() |
||
| 333 | cursor.close() |
||
| 334 | cnx.close() |
||
| 335 | |||
| 336 | resp.status = falcon.HTTP_201 |
||
| 337 | resp.location = '/microgrids/' + str(new_id) |
||
| 338 | |||
| 339 | |||
| 340 | class MicrogridItem: |
||
| 341 | def __init__(self): |
||
| 342 | pass |
||
| 343 | |||
| 344 | @staticmethod |
||
| 345 | def on_options(req, resp, id_): |
||
| 346 | _ = req |
||
| 347 | resp.status = falcon.HTTP_200 |
||
| 348 | _ = id_ |
||
| 349 | |||
| 350 | View Code Duplication | @staticmethod |
|
| 351 | def on_get(req, resp, id_): |
||
| 352 | access_control(req) |
||
| 353 | if not id_.isdigit() or int(id_) <= 0: |
||
| 354 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 355 | description='API.INVALID_MICROGRID_ID') |
||
| 356 | |||
| 357 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 358 | cursor = cnx.cursor() |
||
| 359 | |||
| 360 | query = (" SELECT id, name, uuid " |
||
| 361 | " FROM tbl_contacts ") |
||
| 362 | cursor.execute(query) |
||
| 363 | rows_contacts = cursor.fetchall() |
||
| 364 | |||
| 365 | contact_dict = dict() |
||
| 366 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 367 | for row in rows_contacts: |
||
| 368 | contact_dict[row[0]] = {"id": row[0], |
||
| 369 | "name": row[1], |
||
| 370 | "uuid": row[2]} |
||
| 371 | |||
| 372 | query = (" SELECT id, name, uuid " |
||
| 373 | " FROM tbl_cost_centers ") |
||
| 374 | cursor.execute(query) |
||
| 375 | rows_cost_centers = cursor.fetchall() |
||
| 376 | |||
| 377 | cost_center_dict = dict() |
||
| 378 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 379 | for row in rows_cost_centers: |
||
| 380 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 381 | "name": row[1], |
||
| 382 | "uuid": row[2]} |
||
| 383 | svg_dict = dict() |
||
| 384 | |||
| 385 | query = (" SELECT id, name, uuid " |
||
| 386 | " FROM tbl_svgs ") |
||
| 387 | cursor.execute(query) |
||
| 388 | rows_svgs = cursor.fetchall() |
||
| 389 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 390 | for row in rows_svgs: |
||
| 391 | svg_dict[row[0]] = {"id": row[0], |
||
| 392 | "name": row[1], |
||
| 393 | "uuid": row[2]} |
||
| 394 | |||
| 395 | query = (" SELECT id, name, uuid, " |
||
| 396 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 397 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, " |
||
| 398 | " phase_of_lifecycle, description " |
||
| 399 | " FROM tbl_microgrids " |
||
| 400 | " WHERE id = %s ") |
||
| 401 | cursor.execute(query, (id_,)) |
||
| 402 | row = cursor.fetchone() |
||
| 403 | cursor.close() |
||
| 404 | cnx.close() |
||
| 405 | |||
| 406 | if row is None: |
||
| 407 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 408 | description='API.MICROGRID_NOT_FOUND') |
||
| 409 | else: |
||
| 410 | meta_result = {"id": row[0], |
||
| 411 | "name": row[1], |
||
| 412 | "uuid": row[2], |
||
| 413 | "address": row[3], |
||
| 414 | "postal_code": row[4], |
||
| 415 | "latitude": row[5], |
||
| 416 | "longitude": row[6], |
||
| 417 | "rated_capacity": row[7], |
||
| 418 | "rated_power": row[8], |
||
| 419 | "contact": contact_dict.get(row[9], None), |
||
| 420 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 421 | "serial_number": row[11], |
||
| 422 | "svg": svg_dict.get(row[12], None), |
||
| 423 | "is_cost_data_displayed": bool(row[13]), |
||
| 424 | "phase_of_lifecycle": row[14], |
||
| 425 | "description": row[15], |
||
| 426 | "qrcode": 'microgrid:' + row[2]} |
||
| 427 | |||
| 428 | resp.text = json.dumps(meta_result) |
||
| 429 | |||
| 430 | @staticmethod |
||
| 431 | @user_logger |
||
| 432 | def on_delete(req, resp, id_): |
||
| 433 | admin_control(req) |
||
| 434 | if not id_.isdigit() or int(id_) <= 0: |
||
| 435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 436 | description='API.INVALID_MICROGRID_ID') |
||
| 437 | |||
| 438 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 439 | cursor = cnx.cursor() |
||
| 440 | |||
| 441 | cursor.execute(" SELECT name " |
||
| 442 | " FROM tbl_microgrids " |
||
| 443 | " WHERE id = %s ", (id_,)) |
||
| 444 | if cursor.fetchone() is None: |
||
| 445 | cursor.close() |
||
| 446 | cnx.close() |
||
| 447 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 448 | description='API.MICROGRID_NOT_FOUND') |
||
| 449 | |||
| 450 | # check relation with spaces |
||
| 451 | cursor.execute(" SELECT id " |
||
| 452 | " FROM tbl_spaces_microgrids " |
||
| 453 | " WHERE microgrid_id = %s ", (id_,)) |
||
| 454 | rows_spaces = cursor.fetchall() |
||
| 455 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 456 | cursor.close() |
||
| 457 | cnx.close() |
||
| 458 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 459 | title='API.BAD_REQUEST', |
||
| 460 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 461 | |||
| 462 | # check relation with power plants |
||
| 463 | cursor.execute(" SELECT id " |
||
| 464 | " FROM tbl_virtual_power_plants_microgrids " |
||
| 465 | " WHERE microgrid_id = %s ", |
||
| 466 | (id_,)) |
||
| 467 | rows_power_plants = cursor.fetchall() |
||
| 468 | if rows_power_plants is not None and len(rows_power_plants) > 0: |
||
| 469 | cursor.close() |
||
| 470 | cnx.close() |
||
| 471 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 472 | title='API.BAD_REQUEST', |
||
| 473 | description='API.THERE_IS_RELATION_WITH_VIRTUAL_POWER_PLANTS') |
||
| 474 | |||
| 475 | cursor.execute(" DELETE FROM tbl_microgrids_batteries WHERE microgrid_id = %s ", (id_,)) |
||
| 476 | cnx.commit() |
||
| 477 | cursor.execute(" DELETE FROM tbl_microgrids_commands WHERE microgrid_id = %s ", (id_,)) |
||
| 478 | cnx.commit() |
||
| 479 | cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems WHERE microgrid_id = %s ", (id_,)) |
||
| 480 | cnx.commit() |
||
| 481 | cursor.execute(" DELETE FROM tbl_microgrids_evchargers WHERE microgrid_id = %s ", (id_,)) |
||
| 482 | cnx.commit() |
||
| 483 | cursor.execute(" DELETE FROM tbl_microgrids_generators WHERE microgrid_id = %s ", (id_,)) |
||
| 484 | cnx.commit() |
||
| 485 | cursor.execute(" DELETE FROM tbl_microgrids_grids WHERE microgrid_id = %s ", (id_,)) |
||
| 486 | cnx.commit() |
||
| 487 | cursor.execute(" DELETE FROM tbl_microgrids_heatpumps WHERE microgrid_id = %s ", (id_,)) |
||
| 488 | cnx.commit() |
||
| 489 | cursor.execute(" DELETE FROM tbl_microgrids_loads WHERE microgrid_id = %s ", (id_,)) |
||
| 490 | cnx.commit() |
||
| 491 | cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics WHERE microgrid_id = %s ", (id_,)) |
||
| 492 | cnx.commit() |
||
| 493 | cursor.execute(" DELETE FROM tbl_microgrids_sensors WHERE microgrid_id = %s ", (id_,)) |
||
| 494 | cnx.commit() |
||
| 495 | cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s ", (id_,)) |
||
| 496 | cnx.commit() |
||
| 497 | cursor.execute(" DELETE FROM tbl_microgrids WHERE id = %s ", (id_,)) |
||
| 498 | cnx.commit() |
||
| 499 | |||
| 500 | cursor.close() |
||
| 501 | cnx.close() |
||
| 502 | |||
| 503 | resp.status = falcon.HTTP_204 |
||
| 504 | |||
| 505 | @staticmethod |
||
| 506 | @user_logger |
||
| 507 | def on_put(req, resp, id_): |
||
| 508 | """Handles PUT requests""" |
||
| 509 | admin_control(req) |
||
| 510 | try: |
||
| 511 | raw_json = req.stream.read().decode('utf-8') |
||
| 512 | except UnicodeDecodeError as ex: |
||
| 513 | print("Failed to decode request") |
||
| 514 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 515 | title='API.BAD_REQUEST', |
||
| 516 | description='API.INVALID_ENCODING') |
||
| 517 | except Exception as ex: |
||
| 518 | print(str(ex)) |
||
| 519 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 520 | title='API.BAD_REQUEST', |
||
| 521 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 522 | |||
| 523 | if not id_.isdigit() or int(id_) <= 0: |
||
| 524 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 525 | description='API.INVALID_MICROGRID_ID') |
||
| 526 | |||
| 527 | new_values = json.loads(raw_json) |
||
| 528 | |||
| 529 | if 'name' not in new_values['data'].keys() or \ |
||
| 530 | not isinstance(new_values['data']['name'], str) or \ |
||
| 531 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 532 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 533 | description='API.INVALID_MICROGRID_NAME') |
||
| 534 | name = str.strip(new_values['data']['name']) |
||
| 535 | |||
| 536 | if 'address' not in new_values['data'].keys() or \ |
||
| 537 | not isinstance(new_values['data']['address'], str) or \ |
||
| 538 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 539 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 540 | description='API.INVALID_ADDRESS_VALUE') |
||
| 541 | address = str.strip(new_values['data']['address']) |
||
| 542 | |||
| 543 | if 'postal_code' not in new_values['data'].keys() or \ |
||
| 544 | not isinstance(new_values['data']['postal_code'], str) or \ |
||
| 545 | len(str.strip(new_values['data']['postal_code'])) == 0: |
||
| 546 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 547 | description='API.INVALID_POSTAL_CODE_VALUE') |
||
| 548 | postal_code = str.strip(new_values['data']['postal_code']) |
||
| 549 | |||
| 550 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 551 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 552 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 553 | new_values['data']['latitude'] < -90.0 or \ |
||
| 554 | new_values['data']['latitude'] > 90.0: |
||
| 555 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 556 | description='API.INVALID_LATITUDE_VALUE') |
||
| 557 | latitude = new_values['data']['latitude'] |
||
| 558 | |||
| 559 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 560 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 561 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 562 | new_values['data']['longitude'] < -180.0 or \ |
||
| 563 | new_values['data']['longitude'] > 180.0: |
||
| 564 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 565 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 566 | longitude = new_values['data']['longitude'] |
||
| 567 | |||
| 568 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 569 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 570 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 571 | new_values['data']['rated_capacity'] <= 0.0: |
||
| 572 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 573 | description='API.INVALID_RATED_CAPACITY') |
||
| 574 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 575 | |||
| 576 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 577 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 578 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 579 | new_values['data']['rated_power'] <= 0.0: |
||
| 580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 581 | description='API.INVALID_RATED_POWER_VALUE') |
||
| 582 | rated_power = new_values['data']['rated_power'] |
||
| 583 | |||
| 584 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 585 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 586 | new_values['data']['contact_id'] <= 0: |
||
| 587 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 588 | description='API.INVALID_CONTACT_ID') |
||
| 589 | contact_id = new_values['data']['contact_id'] |
||
| 590 | |||
| 591 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 592 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 593 | new_values['data']['cost_center_id'] <= 0: |
||
| 594 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 595 | description='API.INVALID_COST_CENTER_ID') |
||
| 596 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 597 | |||
| 598 | if 'serial_number' not in new_values['data'].keys() or \ |
||
| 599 | not isinstance(new_values['data']['serial_number'], str) or \ |
||
| 600 | len(str.strip(new_values['data']['serial_number'])) == 0: |
||
| 601 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 602 | description='API.INVALID_SERIAL_NUMBER') |
||
| 603 | serial_number = str.strip(new_values['data']['serial_number']) |
||
| 604 | |||
| 605 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 606 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 607 | new_values['data']['svg_id'] <= 0: |
||
| 608 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 609 | description='API.INVALID_SVG_ID') |
||
| 610 | svg_id = new_values['data']['svg_id'] |
||
| 611 | |||
| 612 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
| 613 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
| 614 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 615 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
| 616 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
| 617 | |||
| 618 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
| 619 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
| 620 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
| 621 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 622 | description='API.INVALID_MICROGRID_PHASE_OF_LIFECYCLE') |
||
| 623 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
| 624 | |||
| 625 | if 'description' in new_values['data'].keys() and \ |
||
| 626 | new_values['data']['description'] is not None and \ |
||
| 627 | len(str(new_values['data']['description'])) > 0: |
||
| 628 | description = str.strip(new_values['data']['description']) |
||
| 629 | else: |
||
| 630 | description = None |
||
| 631 | |||
| 632 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 633 | cursor = cnx.cursor() |
||
| 634 | |||
| 635 | cursor.execute(" SELECT name " |
||
| 636 | " FROM tbl_microgrids " |
||
| 637 | " WHERE id = %s ", (id_,)) |
||
| 638 | if cursor.fetchone() is None: |
||
| 639 | cursor.close() |
||
| 640 | cnx.close() |
||
| 641 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 642 | description='API.MICROGRID_NOT_FOUND') |
||
| 643 | |||
| 644 | cursor.execute(" SELECT name " |
||
| 645 | " FROM tbl_microgrids " |
||
| 646 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 647 | if cursor.fetchone() is not None: |
||
| 648 | cursor.close() |
||
| 649 | cnx.close() |
||
| 650 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 651 | description='API.MICROGRID_NAME_IS_ALREADY_IN_USE') |
||
| 652 | |||
| 653 | cursor.execute(" SELECT name " |
||
| 654 | " FROM tbl_contacts " |
||
| 655 | " WHERE id = %s ", |
||
| 656 | (new_values['data']['contact_id'],)) |
||
| 657 | row = cursor.fetchone() |
||
| 658 | if row is None: |
||
| 659 | cursor.close() |
||
| 660 | cnx.close() |
||
| 661 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 662 | description='API.CONTACT_NOT_FOUND') |
||
| 663 | |||
| 664 | cursor.execute(" SELECT name " |
||
| 665 | " FROM tbl_cost_centers " |
||
| 666 | " WHERE id = %s ", |
||
| 667 | (new_values['data']['cost_center_id'],)) |
||
| 668 | row = cursor.fetchone() |
||
| 669 | if row is None: |
||
| 670 | cursor.close() |
||
| 671 | cnx.close() |
||
| 672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 673 | description='API.COST_CENTER_NOT_FOUND') |
||
| 674 | |||
| 675 | cursor.execute(" SELECT name " |
||
| 676 | " FROM tbl_svgs " |
||
| 677 | " WHERE id = %s ", |
||
| 678 | (new_values['data']['svg_id'],)) |
||
| 679 | row = cursor.fetchone() |
||
| 680 | if row is None: |
||
| 681 | cursor.close() |
||
| 682 | cnx.close() |
||
| 683 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 684 | description='API.SVG_NOT_FOUND') |
||
| 685 | |||
| 686 | update_row = (" UPDATE tbl_microgrids " |
||
| 687 | " SET name = %s, address = %s, postal_code = %s, latitude = %s, longitude = %s, " |
||
| 688 | " rated_capacity = %s, rated_power = %s, " |
||
| 689 | " contact_id = %s, cost_center_id = %s, " |
||
| 690 | " serial_number = %s, svg_id = %s, is_cost_data_displayed = %s, phase_of_lifecycle = %s, " |
||
| 691 | " description = %s " |
||
| 692 | " WHERE id = %s ") |
||
| 693 | cursor.execute(update_row, (name, |
||
| 694 | address, |
||
| 695 | postal_code, |
||
| 696 | latitude, |
||
| 697 | longitude, |
||
| 698 | rated_capacity, |
||
| 699 | rated_power, |
||
| 700 | contact_id, |
||
| 701 | cost_center_id, |
||
| 702 | serial_number, |
||
| 703 | svg_id, |
||
| 704 | is_cost_data_displayed, |
||
| 705 | phase_of_lifecycle, |
||
| 706 | description, |
||
| 707 | id_)) |
||
| 708 | cnx.commit() |
||
| 709 | |||
| 710 | cursor.close() |
||
| 711 | cnx.close() |
||
| 712 | |||
| 713 | resp.status = falcon.HTTP_200 |
||
| 714 | |||
| 715 | |||
| 716 | class MicrogridBatteryCollection: |
||
| 717 | def __init__(self): |
||
| 718 | pass |
||
| 719 | |||
| 720 | @staticmethod |
||
| 721 | def on_options(req, resp, id_): |
||
| 722 | _ = req |
||
| 723 | resp.status = falcon.HTTP_200 |
||
| 724 | _ = id_ |
||
| 725 | |||
| 726 | View Code Duplication | @staticmethod |
|
| 727 | def on_get(req, resp, id_): |
||
| 728 | access_control(req) |
||
| 729 | if not id_.isdigit() or int(id_) <= 0: |
||
| 730 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 731 | description='API.INVALID_MICROGRID_ID') |
||
| 732 | |||
| 733 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 734 | cursor = cnx.cursor() |
||
| 735 | |||
| 736 | cursor.execute(" SELECT name " |
||
| 737 | " FROM tbl_microgrids " |
||
| 738 | " WHERE id = %s ", (id_,)) |
||
| 739 | if cursor.fetchone() is None: |
||
| 740 | cursor.close() |
||
| 741 | cnx.close() |
||
| 742 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 743 | description='API.MICROGRID_NOT_FOUND') |
||
| 744 | |||
| 745 | # query meter dict |
||
| 746 | query = (" SELECT id, name, uuid " |
||
| 747 | " FROM tbl_meters ") |
||
| 748 | cursor.execute(query) |
||
| 749 | rows_meters = cursor.fetchall() |
||
| 750 | |||
| 751 | meter_dict = dict() |
||
| 752 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 753 | for row in rows_meters: |
||
| 754 | meter_dict[row[0]] = {"id": row[0], |
||
| 755 | "name": row[1], |
||
| 756 | "uuid": row[2]} |
||
| 757 | # query point dict |
||
| 758 | query = (" SELECT id, name " |
||
| 759 | " FROM tbl_points ") |
||
| 760 | cursor.execute(query) |
||
| 761 | rows_points = cursor.fetchall() |
||
| 762 | |||
| 763 | point_dict = dict() |
||
| 764 | if rows_points is not None and len(rows_points) > 0: |
||
| 765 | for row in rows_points: |
||
| 766 | point_dict[row[0]] = {"id": row[0], |
||
| 767 | "name": row[1]} |
||
| 768 | |||
| 769 | query = (" SELECT id, name, uuid, " |
||
| 770 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 771 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
||
| 772 | " FROM tbl_microgrids_batteries " |
||
| 773 | " WHERE microgrid_id = %s " |
||
| 774 | " ORDER BY name ") |
||
| 775 | cursor.execute(query, (id_,)) |
||
| 776 | rows = cursor.fetchall() |
||
| 777 | |||
| 778 | result = list() |
||
| 779 | if rows is not None and len(rows) > 0: |
||
| 780 | for row in rows: |
||
| 781 | meta_result = {"id": row[0], |
||
| 782 | "name": row[1], |
||
| 783 | "uuid": row[2], |
||
| 784 | "battery_state_point": point_dict.get(row[3]), |
||
| 785 | "soc_point": point_dict.get(row[4]), |
||
| 786 | "power_point": point_dict.get(row[5]), |
||
| 787 | "charge_meter": meter_dict.get(row[6]), |
||
| 788 | "discharge_meter": meter_dict.get(row[7]), |
||
| 789 | "rated_capacity": row[8], |
||
| 790 | "rated_power": row[9], |
||
| 791 | "nominal_voltage": row[10]} |
||
| 792 | result.append(meta_result) |
||
| 793 | |||
| 794 | resp.text = json.dumps(result) |
||
| 795 | |||
| 796 | @staticmethod |
||
| 797 | @user_logger |
||
| 798 | def on_post(req, resp, id_): |
||
| 799 | """Handles POST requests""" |
||
| 800 | admin_control(req) |
||
| 801 | try: |
||
| 802 | raw_json = req.stream.read().decode('utf-8') |
||
| 803 | except UnicodeDecodeError as ex: |
||
| 804 | print("Failed to decode request") |
||
| 805 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 806 | title='API.BAD_REQUEST', |
||
| 807 | description='API.INVALID_ENCODING') |
||
| 808 | except Exception as ex: |
||
| 809 | print(str(ex)) |
||
| 810 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 811 | title='API.BAD_REQUEST', |
||
| 812 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 813 | |||
| 814 | if not id_.isdigit() or int(id_) <= 0: |
||
| 815 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 816 | description='API.INVALID_MICROGRID_ID') |
||
| 817 | |||
| 818 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 819 | cursor = cnx.cursor() |
||
| 820 | |||
| 821 | cursor.execute(" SELECT name " |
||
| 822 | " FROM tbl_microgrids " |
||
| 823 | " WHERE id = %s ", (id_,)) |
||
| 824 | if cursor.fetchone() is None: |
||
| 825 | cursor.close() |
||
| 826 | cnx.close() |
||
| 827 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 828 | description='API.MICROGRID_NOT_FOUND') |
||
| 829 | |||
| 830 | new_values = json.loads(raw_json) |
||
| 831 | |||
| 832 | if 'name' not in new_values['data'].keys() or \ |
||
| 833 | not isinstance(new_values['data']['name'], str) or \ |
||
| 834 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 835 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 836 | description='API.INVALID_MICROGRID_BATTERY_NAME') |
||
| 837 | name = str.strip(new_values['data']['name']) |
||
| 838 | |||
| 839 | if 'battery_state_point_id' not in new_values['data'].keys() or \ |
||
| 840 | not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
||
| 841 | new_values['data']['battery_state_point_id'] <= 0: |
||
| 842 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 843 | description='API.INVALID_BATTERY_STATE_POINT_ID') |
||
| 844 | battery_state_point_id = new_values['data']['battery_state_point_id'] |
||
| 845 | |||
| 846 | if 'soc_point_id' not in new_values['data'].keys() or \ |
||
| 847 | not isinstance(new_values['data']['soc_point_id'], int) or \ |
||
| 848 | new_values['data']['soc_point_id'] <= 0: |
||
| 849 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 850 | description='API.INVALID_SOC_POINT_ID') |
||
| 851 | soc_point_id = new_values['data']['soc_point_id'] |
||
| 852 | |||
| 853 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 854 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 855 | new_values['data']['power_point_id'] <= 0: |
||
| 856 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 857 | description='API.INVALID_POWER_POINT_ID') |
||
| 858 | power_point_id = new_values['data']['power_point_id'] |
||
| 859 | |||
| 860 | if 'charge_meter_id' not in new_values['data'].keys() or \ |
||
| 861 | not isinstance(new_values['data']['charge_meter_id'], int) or \ |
||
| 862 | new_values['data']['charge_meter_id'] <= 0: |
||
| 863 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 864 | description='API.INVALID_CHARGE_METER_ID') |
||
| 865 | charge_meter_id = new_values['data']['charge_meter_id'] |
||
| 866 | |||
| 867 | if 'discharge_meter_id' not in new_values['data'].keys() or \ |
||
| 868 | not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
||
| 869 | new_values['data']['discharge_meter_id'] <= 0: |
||
| 870 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 871 | description='API.INVALID_DISCHARGE_METER_ID') |
||
| 872 | discharge_meter_id = new_values['data']['discharge_meter_id'] |
||
| 873 | |||
| 874 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 875 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 876 | isinstance(new_values['data']['rated_capacity'], int)): |
||
| 877 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 878 | description='API.INVALID_RATED_CAPACITY') |
||
| 879 | rated_capacity = float(new_values['data']['rated_capacity']) |
||
| 880 | |||
| 881 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 882 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 883 | isinstance(new_values['data']['rated_power'], int)): |
||
| 884 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 885 | description='API.INVALID_RATED_POWER') |
||
| 886 | rated_power = float(new_values['data']['rated_power']) |
||
| 887 | |||
| 888 | if 'nominal_voltage' not in new_values['data'].keys() or \ |
||
| 889 | not (isinstance(new_values['data']['nominal_voltage'], float) or |
||
| 890 | isinstance(new_values['data']['nominal_voltage'], int)): |
||
| 891 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 892 | description='API.INVALID_NOMINAL_VOLTAGE') |
||
| 893 | nominal_voltage = float(new_values['data']['nominal_voltage']) |
||
| 894 | |||
| 895 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 896 | cursor = cnx.cursor() |
||
| 897 | |||
| 898 | cursor.execute(" SELECT name " |
||
| 899 | " FROM tbl_microgrids_batteries " |
||
| 900 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 901 | (id_, name,)) |
||
| 902 | if cursor.fetchone() is not None: |
||
| 903 | cursor.close() |
||
| 904 | cnx.close() |
||
| 905 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 906 | description='API.MICROGRID_BATTERY_NAME_IS_ALREADY_IN_USE') |
||
| 907 | |||
| 908 | cursor.execute(" SELECT name " |
||
| 909 | " FROM tbl_points " |
||
| 910 | " WHERE id = %s ", |
||
| 911 | (battery_state_point_id,)) |
||
| 912 | if cursor.fetchone() is None: |
||
| 913 | cursor.close() |
||
| 914 | cnx.close() |
||
| 915 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 916 | description='API.BATTERY_STATE_POINT_NOT_FOUND') |
||
| 917 | |||
| 918 | cursor.execute(" SELECT name " |
||
| 919 | " FROM tbl_points " |
||
| 920 | " WHERE id = %s ", |
||
| 921 | (soc_point_id,)) |
||
| 922 | if cursor.fetchone() is None: |
||
| 923 | cursor.close() |
||
| 924 | cnx.close() |
||
| 925 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 926 | description='API.SOC_POINT_NOT_FOUND') |
||
| 927 | |||
| 928 | cursor.execute(" SELECT name " |
||
| 929 | " FROM tbl_points " |
||
| 930 | " WHERE id = %s ", |
||
| 931 | (power_point_id,)) |
||
| 932 | if cursor.fetchone() is None: |
||
| 933 | cursor.close() |
||
| 934 | cnx.close() |
||
| 935 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 936 | description='API.POWER_POINT_NOT_FOUND') |
||
| 937 | |||
| 938 | cursor.execute(" SELECT name " |
||
| 939 | " FROM tbl_meters " |
||
| 940 | " WHERE id = %s ", |
||
| 941 | (charge_meter_id,)) |
||
| 942 | if cursor.fetchone() is None: |
||
| 943 | cursor.close() |
||
| 944 | cnx.close() |
||
| 945 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 946 | description='API.CHARGE_METER_NOT_FOUND') |
||
| 947 | |||
| 948 | cursor.execute(" SELECT name " |
||
| 949 | " FROM tbl_meters " |
||
| 950 | " WHERE id = %s ", |
||
| 951 | (discharge_meter_id,)) |
||
| 952 | if cursor.fetchone() is None: |
||
| 953 | cursor.close() |
||
| 954 | cnx.close() |
||
| 955 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 956 | description='API.DISCHARGE_METER_NOT_FOUND') |
||
| 957 | |||
| 958 | add_values = (" INSERT INTO tbl_microgrids_batteries " |
||
| 959 | " (name, uuid, microgrid_id, " |
||
| 960 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 961 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage) " |
||
| 962 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 963 | cursor.execute(add_values, (name, |
||
| 964 | str(uuid.uuid4()), |
||
| 965 | id_, |
||
| 966 | battery_state_point_id, |
||
| 967 | soc_point_id, |
||
| 968 | power_point_id, |
||
| 969 | charge_meter_id, |
||
| 970 | discharge_meter_id, |
||
| 971 | rated_capacity, |
||
| 972 | rated_power, |
||
| 973 | nominal_voltage)) |
||
| 974 | new_id = cursor.lastrowid |
||
| 975 | cnx.commit() |
||
| 976 | cursor.close() |
||
| 977 | cnx.close() |
||
| 978 | |||
| 979 | resp.status = falcon.HTTP_201 |
||
| 980 | resp.location = '/microgrids/' + str(id_) + '/batteries/' + str(new_id) |
||
| 981 | |||
| 982 | |||
| 983 | class MicrogridBatteryItem: |
||
| 984 | def __init__(self): |
||
| 985 | pass |
||
| 986 | |||
| 987 | @staticmethod |
||
| 988 | def on_options(req, resp, id_, bid): |
||
| 989 | _ = req |
||
| 990 | resp.status = falcon.HTTP_200 |
||
| 991 | _ = id_ |
||
| 992 | |||
| 993 | View Code Duplication | @staticmethod |
|
| 994 | def on_get(req, resp, id_, bid): |
||
| 995 | access_control(req) |
||
| 996 | if not id_.isdigit() or int(id_) <= 0: |
||
| 997 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 998 | description='API.INVALID_MICROGRID_ID') |
||
| 999 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1000 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1001 | description='API.INVALID_MICROGRID_BATTERY_ID') |
||
| 1002 | |||
| 1003 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1004 | cursor = cnx.cursor() |
||
| 1005 | |||
| 1006 | cursor.execute(" SELECT name " |
||
| 1007 | " FROM tbl_microgrids " |
||
| 1008 | " WHERE id = %s ", (id_,)) |
||
| 1009 | if cursor.fetchone() is None: |
||
| 1010 | cursor.close() |
||
| 1011 | cnx.close() |
||
| 1012 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1013 | description='API.MICROGRID_NOT_FOUND') |
||
| 1014 | |||
| 1015 | # query microgrid dict |
||
| 1016 | query = (" SELECT id, name, uuid " |
||
| 1017 | " FROM tbl_microgrids ") |
||
| 1018 | cursor.execute(query) |
||
| 1019 | rows_microgrids = cursor.fetchall() |
||
| 1020 | |||
| 1021 | microgrid_dict = dict() |
||
| 1022 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 1023 | for row in rows_microgrids: |
||
| 1024 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 1025 | "name": row[1], |
||
| 1026 | "uuid": row[2]} |
||
| 1027 | # query meter dict |
||
| 1028 | query = (" SELECT id, name, uuid " |
||
| 1029 | " FROM tbl_meters ") |
||
| 1030 | cursor.execute(query) |
||
| 1031 | rows_meters = cursor.fetchall() |
||
| 1032 | |||
| 1033 | meter_dict = dict() |
||
| 1034 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1035 | for row in rows_meters: |
||
| 1036 | meter_dict[row[0]] = {"id": row[0], |
||
| 1037 | "name": row[1], |
||
| 1038 | "uuid": row[2]} |
||
| 1039 | # query point dict |
||
| 1040 | query = (" SELECT id, name " |
||
| 1041 | " FROM tbl_points ") |
||
| 1042 | cursor.execute(query) |
||
| 1043 | rows_points = cursor.fetchall() |
||
| 1044 | |||
| 1045 | point_dict = dict() |
||
| 1046 | if rows_points is not None and len(rows_points) > 0: |
||
| 1047 | for row in rows_points: |
||
| 1048 | point_dict[row[0]] = {"id": row[0], |
||
| 1049 | "name": row[1]} |
||
| 1050 | |||
| 1051 | query = (" SELECT id, name, uuid, microgrid_id, " |
||
| 1052 | " battery_state_point_id, soc_point_id, power_point_id, " |
||
| 1053 | " charge_meter_id, discharge_meter_id, rated_capacity, rated_power, nominal_voltage " |
||
| 1054 | " FROM tbl_microgrids_batteries " |
||
| 1055 | " WHERE id = %s ") |
||
| 1056 | cursor.execute(query, (bid,)) |
||
| 1057 | row = cursor.fetchone() |
||
| 1058 | cursor.close() |
||
| 1059 | cnx.close() |
||
| 1060 | |||
| 1061 | if row is None: |
||
| 1062 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1063 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 1064 | else: |
||
| 1065 | meta_result = {"id": row[0], |
||
| 1066 | "name": row[1], |
||
| 1067 | "uuid": row[2], |
||
| 1068 | "microgrid": microgrid_dict.get(row[3]), |
||
| 1069 | "battery_state_point": point_dict.get(row[4]), |
||
| 1070 | "soc_point": point_dict.get(row[5]), |
||
| 1071 | "power_point": point_dict.get(row[6]), |
||
| 1072 | "charge_meter": meter_dict.get(row[7]), |
||
| 1073 | "discharge_meter": meter_dict.get(row[8]), |
||
| 1074 | "rated_capacity": row[9], |
||
| 1075 | "rated_power": row[10], |
||
| 1076 | "nominal_voltage": row[11]} |
||
| 1077 | |||
| 1078 | resp.text = json.dumps(meta_result) |
||
| 1079 | |||
| 1080 | @staticmethod |
||
| 1081 | @user_logger |
||
| 1082 | def on_delete(req, resp, id_, bid): |
||
| 1083 | admin_control(req) |
||
| 1084 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1085 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1086 | description='API.INVALID_MICROGRID_ID') |
||
| 1087 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1088 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1089 | description='API.INVALID_MICROGRID_BATTERY_ID') |
||
| 1090 | |||
| 1091 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1092 | cursor = cnx.cursor() |
||
| 1093 | |||
| 1094 | cursor.execute(" SELECT name " |
||
| 1095 | " FROM tbl_microgrids " |
||
| 1096 | " WHERE id = %s ", (id_,)) |
||
| 1097 | if cursor.fetchone() is None: |
||
| 1098 | cursor.close() |
||
| 1099 | cnx.close() |
||
| 1100 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1101 | description='API.MICROGRID_NOT_FOUND') |
||
| 1102 | |||
| 1103 | cursor.execute(" SELECT name " |
||
| 1104 | " FROM tbl_microgrids_batteries " |
||
| 1105 | " WHERE id = %s ", (bid,)) |
||
| 1106 | if cursor.fetchone() is None: |
||
| 1107 | cursor.close() |
||
| 1108 | cnx.close() |
||
| 1109 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1110 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 1111 | |||
| 1112 | cursor.execute(" DELETE FROM tbl_microgrids_batteries " |
||
| 1113 | " WHERE id = %s ", (bid,)) |
||
| 1114 | cnx.commit() |
||
| 1115 | |||
| 1116 | cursor.close() |
||
| 1117 | cnx.close() |
||
| 1118 | |||
| 1119 | resp.status = falcon.HTTP_204 |
||
| 1120 | |||
| 1121 | @staticmethod |
||
| 1122 | @user_logger |
||
| 1123 | def on_put(req, resp, id_, bid): |
||
| 1124 | """Handles PUT requests""" |
||
| 1125 | admin_control(req) |
||
| 1126 | try: |
||
| 1127 | raw_json = req.stream.read().decode('utf-8') |
||
| 1128 | except UnicodeDecodeError as ex: |
||
| 1129 | print("Failed to decode request") |
||
| 1130 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1131 | title='API.BAD_REQUEST', |
||
| 1132 | description='API.INVALID_ENCODING') |
||
| 1133 | except Exception as ex: |
||
| 1134 | print(str(ex)) |
||
| 1135 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1136 | title='API.BAD_REQUEST', |
||
| 1137 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1138 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1139 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1140 | description='API.INVALID_MICROGRID_ID') |
||
| 1141 | if not bid.isdigit() or int(bid) <= 0: |
||
| 1142 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1143 | description='API.INVALID_MICROGRID_BATTERY_ID') |
||
| 1144 | |||
| 1145 | new_values = json.loads(raw_json) |
||
| 1146 | |||
| 1147 | if 'name' not in new_values['data'].keys() or \ |
||
| 1148 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1149 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1151 | description='API.INVALID_MICROGRID_BATTERY_NAME') |
||
| 1152 | name = str.strip(new_values['data']['name']) |
||
| 1153 | |||
| 1154 | if 'battery_state_point_id' not in new_values['data'].keys() or \ |
||
| 1155 | not isinstance(new_values['data']['battery_state_point_id'], int) or \ |
||
| 1156 | new_values['data']['battery_state_point_id'] <= 0: |
||
| 1157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1158 | description='API.INVALID_BATTERY_STATE_POINT_ID') |
||
| 1159 | battery_state_point_id = new_values['data']['battery_state_point_id'] |
||
| 1160 | |||
| 1161 | if 'soc_point_id' not in new_values['data'].keys() or \ |
||
| 1162 | not isinstance(new_values['data']['soc_point_id'], int) or \ |
||
| 1163 | new_values['data']['soc_point_id'] <= 0: |
||
| 1164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1165 | description='API.INVALID_SOC_POINT_ID') |
||
| 1166 | soc_point_id = new_values['data']['soc_point_id'] |
||
| 1167 | |||
| 1168 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 1169 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 1170 | new_values['data']['power_point_id'] <= 0: |
||
| 1171 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1172 | description='API.INVALID_POWER_POINT_ID') |
||
| 1173 | power_point_id = new_values['data']['power_point_id'] |
||
| 1174 | |||
| 1175 | if 'charge_meter_id' not in new_values['data'].keys() or \ |
||
| 1176 | not isinstance(new_values['data']['charge_meter_id'], int) or \ |
||
| 1177 | new_values['data']['charge_meter_id'] <= 0: |
||
| 1178 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1179 | description='API.INVALID_CHARGE_METER_ID') |
||
| 1180 | charge_meter_id = new_values['data']['charge_meter_id'] |
||
| 1181 | |||
| 1182 | if 'discharge_meter_id' not in new_values['data'].keys() or \ |
||
| 1183 | not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
||
| 1184 | new_values['data']['discharge_meter_id'] <= 0: |
||
| 1185 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1186 | description='API.INVALID_DISCHARGE_METER_ID') |
||
| 1187 | discharge_meter_id = new_values['data']['discharge_meter_id'] |
||
| 1188 | |||
| 1189 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 1190 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 1191 | isinstance(new_values['data']['rated_capacity'], int)): |
||
| 1192 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1193 | description='API.INVALID_RATED_CAPACITY') |
||
| 1194 | rated_capacity = float(new_values['data']['rated_capacity']) |
||
| 1195 | |||
| 1196 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 1197 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 1198 | isinstance(new_values['data']['rated_power'], int)): |
||
| 1199 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1200 | description='API.INVALID_RATED_POWER') |
||
| 1201 | rated_power = float(new_values['data']['rated_power']) |
||
| 1202 | |||
| 1203 | if 'nominal_voltage' not in new_values['data'].keys() or \ |
||
| 1204 | not (isinstance(new_values['data']['nominal_voltage'], float) or |
||
| 1205 | isinstance(new_values['data']['nominal_voltage'], int)): |
||
| 1206 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1207 | description='API.INVALID_NOMINAL_VOLTAGE') |
||
| 1208 | nominal_voltage = float(new_values['data']['nominal_voltage']) |
||
| 1209 | |||
| 1210 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1211 | cursor = cnx.cursor() |
||
| 1212 | |||
| 1213 | cursor.execute(" SELECT name " |
||
| 1214 | " FROM tbl_microgrids " |
||
| 1215 | " WHERE id = %s ", |
||
| 1216 | (id_,)) |
||
| 1217 | if cursor.fetchone() is None: |
||
| 1218 | cursor.close() |
||
| 1219 | cnx.close() |
||
| 1220 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1221 | description='API.MICROGRID_NOT_FOUND') |
||
| 1222 | |||
| 1223 | cursor.execute(" SELECT name " |
||
| 1224 | " FROM tbl_microgrids_batteries " |
||
| 1225 | " WHERE id = %s ", (bid,)) |
||
| 1226 | if cursor.fetchone() is None: |
||
| 1227 | cursor.close() |
||
| 1228 | cnx.close() |
||
| 1229 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1230 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 1231 | |||
| 1232 | cursor.execute(" SELECT name " |
||
| 1233 | " FROM tbl_microgrids_batteries " |
||
| 1234 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 1235 | (id_, name, bid)) |
||
| 1236 | if cursor.fetchone() is not None: |
||
| 1237 | cursor.close() |
||
| 1238 | cnx.close() |
||
| 1239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1240 | description='API.MICROGRID_BATTERY_NAME_IS_ALREADY_IN_USE') |
||
| 1241 | |||
| 1242 | cursor.execute(" SELECT name " |
||
| 1243 | " FROM tbl_points " |
||
| 1244 | " WHERE id = %s ", |
||
| 1245 | (battery_state_point_id,)) |
||
| 1246 | if cursor.fetchone() is None: |
||
| 1247 | cursor.close() |
||
| 1248 | cnx.close() |
||
| 1249 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1250 | description='API.BATTERY_STATE_POINT_NOT_FOUND') |
||
| 1251 | |||
| 1252 | cursor.execute(" SELECT name " |
||
| 1253 | " FROM tbl_points " |
||
| 1254 | " WHERE id = %s ", |
||
| 1255 | (soc_point_id,)) |
||
| 1256 | if cursor.fetchone() is None: |
||
| 1257 | cursor.close() |
||
| 1258 | cnx.close() |
||
| 1259 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1260 | description='API.SOC_POINT_NOT_FOUND') |
||
| 1261 | |||
| 1262 | cursor.execute(" SELECT name " |
||
| 1263 | " FROM tbl_points " |
||
| 1264 | " WHERE id = %s ", |
||
| 1265 | (power_point_id,)) |
||
| 1266 | if cursor.fetchone() is None: |
||
| 1267 | cursor.close() |
||
| 1268 | cnx.close() |
||
| 1269 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1270 | description='API.POWER_POINT_NOT_FOUND') |
||
| 1271 | |||
| 1272 | cursor.execute(" SELECT name " |
||
| 1273 | " FROM tbl_meters " |
||
| 1274 | " WHERE id = %s ", |
||
| 1275 | (charge_meter_id,)) |
||
| 1276 | if cursor.fetchone() is None: |
||
| 1277 | cursor.close() |
||
| 1278 | cnx.close() |
||
| 1279 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1280 | description='API.CHARGE_METER_NOT_FOUND') |
||
| 1281 | |||
| 1282 | cursor.execute(" SELECT name " |
||
| 1283 | " FROM tbl_meters " |
||
| 1284 | " WHERE id = %s ", |
||
| 1285 | (discharge_meter_id,)) |
||
| 1286 | if cursor.fetchone() is None: |
||
| 1287 | cursor.close() |
||
| 1288 | cnx.close() |
||
| 1289 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1290 | description='API.DISCHARGE_METER_NOT_FOUND') |
||
| 1291 | |||
| 1292 | update_row = (" UPDATE tbl_microgrids_batteries " |
||
| 1293 | " SET name = %s, microgrid_id = %s, " |
||
| 1294 | " battery_state_point_id = %s, soc_point_id = %s, power_point_id = %s, " |
||
| 1295 | " charge_meter_id = %s, discharge_meter_id = %s, rated_capacity = %s, rated_power = %s," |
||
| 1296 | " nominal_voltage = %s " |
||
| 1297 | " WHERE id = %s ") |
||
| 1298 | cursor.execute(update_row, (name, |
||
| 1299 | id_, |
||
| 1300 | battery_state_point_id, |
||
| 1301 | soc_point_id, |
||
| 1302 | power_point_id, |
||
| 1303 | charge_meter_id, |
||
| 1304 | discharge_meter_id, |
||
| 1305 | rated_capacity, |
||
| 1306 | rated_power, |
||
| 1307 | nominal_voltage, |
||
| 1308 | bid)) |
||
| 1309 | cnx.commit() |
||
| 1310 | |||
| 1311 | cursor.close() |
||
| 1312 | cnx.close() |
||
| 1313 | |||
| 1314 | resp.status = falcon.HTTP_200 |
||
| 1315 | |||
| 1316 | |||
| 1317 | class MicrogridCommandCollection: |
||
| 1318 | def __init__(self): |
||
| 1319 | pass |
||
| 1320 | |||
| 1321 | @staticmethod |
||
| 1322 | def on_options(req, resp, id_): |
||
| 1323 | _ = req |
||
| 1324 | resp.status = falcon.HTTP_200 |
||
| 1325 | _ = id_ |
||
| 1326 | |||
| 1327 | @staticmethod |
||
| 1328 | def on_get(req, resp, id_): |
||
| 1329 | access_control(req) |
||
| 1330 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1331 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1332 | description='API.INVALID_MICROGRID_ID') |
||
| 1333 | |||
| 1334 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1335 | cursor = cnx.cursor() |
||
| 1336 | |||
| 1337 | cursor.execute(" SELECT name " |
||
| 1338 | " FROM tbl_microgrids " |
||
| 1339 | " WHERE id = %s ", (id_,)) |
||
| 1340 | if cursor.fetchone() is None: |
||
| 1341 | cursor.close() |
||
| 1342 | cnx.close() |
||
| 1343 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1344 | description='API.MICROGRID_NOT_FOUND') |
||
| 1345 | |||
| 1346 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 1347 | " FROM tbl_microgrids m, tbl_microgrids_commands mc, tbl_commands c " |
||
| 1348 | " WHERE mc.microgrid_id = m.id AND c.id = mc.command_id AND m.id = %s " |
||
| 1349 | " ORDER BY c.id ") |
||
| 1350 | cursor.execute(query, (id_,)) |
||
| 1351 | rows = cursor.fetchall() |
||
| 1352 | |||
| 1353 | result = list() |
||
| 1354 | if rows is not None and len(rows) > 0: |
||
| 1355 | for row in rows: |
||
| 1356 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1357 | result.append(meta_result) |
||
| 1358 | |||
| 1359 | resp.text = json.dumps(result) |
||
| 1360 | |||
| 1361 | |||
| 1362 | View Code Duplication | class MicrogridEVChargerCollection: |
|
| 1363 | def __init__(self): |
||
| 1364 | pass |
||
| 1365 | |||
| 1366 | @staticmethod |
||
| 1367 | def on_options(req, resp, id_): |
||
| 1368 | _ = req |
||
| 1369 | resp.status = falcon.HTTP_200 |
||
| 1370 | _ = id_ |
||
| 1371 | |||
| 1372 | @staticmethod |
||
| 1373 | def on_get(req, resp, id_): |
||
| 1374 | access_control(req) |
||
| 1375 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1376 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1377 | description='API.INVALID_MICROGRID_ID') |
||
| 1378 | |||
| 1379 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1380 | cursor = cnx.cursor() |
||
| 1381 | |||
| 1382 | cursor.execute(" SELECT name " |
||
| 1383 | " FROM tbl_microgrids " |
||
| 1384 | " WHERE id = %s ", (id_,)) |
||
| 1385 | if cursor.fetchone() is None: |
||
| 1386 | cursor.close() |
||
| 1387 | cnx.close() |
||
| 1388 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1389 | description='API.MICROGRID_NOT_FOUND') |
||
| 1390 | |||
| 1391 | # query meter dict |
||
| 1392 | query = (" SELECT id, name, uuid " |
||
| 1393 | " FROM tbl_meters ") |
||
| 1394 | cursor.execute(query) |
||
| 1395 | rows_meters = cursor.fetchall() |
||
| 1396 | |||
| 1397 | meter_dict = dict() |
||
| 1398 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1399 | for row in rows_meters: |
||
| 1400 | meter_dict[row[0]] = {"id": row[0], |
||
| 1401 | "name": row[1], |
||
| 1402 | "uuid": row[2]} |
||
| 1403 | # query point dict |
||
| 1404 | query = (" SELECT id, name " |
||
| 1405 | " FROM tbl_points ") |
||
| 1406 | cursor.execute(query) |
||
| 1407 | rows_points = cursor.fetchall() |
||
| 1408 | |||
| 1409 | point_dict = dict() |
||
| 1410 | if rows_points is not None and len(rows_points) > 0: |
||
| 1411 | for row in rows_points: |
||
| 1412 | point_dict[row[0]] = {"id": row[0], |
||
| 1413 | "name": row[1]} |
||
| 1414 | |||
| 1415 | query = (" SELECT id, name, uuid, " |
||
| 1416 | " power_point_id, meter_id, rated_output_power " |
||
| 1417 | " FROM tbl_microgrids_evchargers " |
||
| 1418 | " WHERE microgrid_id = %s " |
||
| 1419 | " ORDER BY name ") |
||
| 1420 | cursor.execute(query, (id_,)) |
||
| 1421 | rows = cursor.fetchall() |
||
| 1422 | |||
| 1423 | result = list() |
||
| 1424 | if rows is not None and len(rows) > 0: |
||
| 1425 | for row in rows: |
||
| 1426 | meta_result = {"id": row[0], |
||
| 1427 | "name": row[1], |
||
| 1428 | "uuid": row[2], |
||
| 1429 | "power_point": point_dict.get(row[3]), |
||
| 1430 | "meter": meter_dict.get(row[4]), |
||
| 1431 | "rated_output_power": row[5]} |
||
| 1432 | result.append(meta_result) |
||
| 1433 | |||
| 1434 | resp.text = json.dumps(result) |
||
| 1435 | |||
| 1436 | @staticmethod |
||
| 1437 | @user_logger |
||
| 1438 | def on_post(req, resp, id_): |
||
| 1439 | """Handles POST requests""" |
||
| 1440 | admin_control(req) |
||
| 1441 | try: |
||
| 1442 | raw_json = req.stream.read().decode('utf-8') |
||
| 1443 | except UnicodeDecodeError as ex: |
||
| 1444 | print("Failed to decode request") |
||
| 1445 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1446 | title='API.BAD_REQUEST', |
||
| 1447 | description='API.INVALID_ENCODING') |
||
| 1448 | except Exception as ex: |
||
| 1449 | print(str(ex)) |
||
| 1450 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1451 | title='API.BAD_REQUEST', |
||
| 1452 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1453 | |||
| 1454 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1455 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1456 | description='API.INVALID_MICROGRID_ID') |
||
| 1457 | |||
| 1458 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1459 | cursor = cnx.cursor() |
||
| 1460 | |||
| 1461 | cursor.execute(" SELECT name " |
||
| 1462 | " FROM tbl_microgrids " |
||
| 1463 | " WHERE id = %s ", (id_,)) |
||
| 1464 | if cursor.fetchone() is None: |
||
| 1465 | cursor.close() |
||
| 1466 | cnx.close() |
||
| 1467 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1468 | description='API.MICROGRID_NOT_FOUND') |
||
| 1469 | |||
| 1470 | new_values = json.loads(raw_json) |
||
| 1471 | |||
| 1472 | if 'name' not in new_values['data'].keys() or \ |
||
| 1473 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1474 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1476 | description='API.INVALID_MICROGRID_EVCHARGER_NAME') |
||
| 1477 | name = str.strip(new_values['data']['name']) |
||
| 1478 | |||
| 1479 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 1480 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 1481 | new_values['data']['power_point_id'] <= 0: |
||
| 1482 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1483 | description='API.INVALID_POWER_POINT_ID') |
||
| 1484 | power_point_id = new_values['data']['power_point_id'] |
||
| 1485 | |||
| 1486 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1487 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1488 | new_values['data']['meter_id'] <= 0: |
||
| 1489 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1490 | description='API.INVALID_METER_ID') |
||
| 1491 | meter_id = new_values['data']['meter_id'] |
||
| 1492 | |||
| 1493 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 1494 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 1495 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 1496 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1497 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 1498 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 1499 | |||
| 1500 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1501 | cursor = cnx.cursor() |
||
| 1502 | |||
| 1503 | cursor.execute(" SELECT name " |
||
| 1504 | " FROM tbl_microgrids " |
||
| 1505 | " WHERE id = %s ", |
||
| 1506 | (id_,)) |
||
| 1507 | if cursor.fetchone() is None: |
||
| 1508 | cursor.close() |
||
| 1509 | cnx.close() |
||
| 1510 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1511 | description='API.MICROGRID_NOT_FOUND') |
||
| 1512 | |||
| 1513 | cursor.execute(" SELECT name " |
||
| 1514 | " FROM tbl_microgrids_evchargers " |
||
| 1515 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 1516 | (id_, name,)) |
||
| 1517 | if cursor.fetchone() is not None: |
||
| 1518 | cursor.close() |
||
| 1519 | cnx.close() |
||
| 1520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1521 | description='API.MICROGRID_EVCHARGER_NAME_IS_ALREADY_IN_USE') |
||
| 1522 | |||
| 1523 | cursor.execute(" SELECT name " |
||
| 1524 | " FROM tbl_points " |
||
| 1525 | " WHERE id = %s ", |
||
| 1526 | (power_point_id,)) |
||
| 1527 | if cursor.fetchone() is None: |
||
| 1528 | cursor.close() |
||
| 1529 | cnx.close() |
||
| 1530 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1531 | description='API.POWER_POINT_NOT_FOUND') |
||
| 1532 | |||
| 1533 | cursor.execute(" SELECT name " |
||
| 1534 | " FROM tbl_meters " |
||
| 1535 | " WHERE id = %s ", |
||
| 1536 | (meter_id,)) |
||
| 1537 | if cursor.fetchone() is None: |
||
| 1538 | cursor.close() |
||
| 1539 | cnx.close() |
||
| 1540 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1541 | description='API.METER_NOT_FOUND') |
||
| 1542 | |||
| 1543 | add_values = (" INSERT INTO tbl_microgrids_evchargers " |
||
| 1544 | " (name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power) " |
||
| 1545 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 1546 | cursor.execute(add_values, (name, |
||
| 1547 | str(uuid.uuid4()), |
||
| 1548 | id_, |
||
| 1549 | power_point_id, |
||
| 1550 | meter_id, |
||
| 1551 | rated_output_power)) |
||
| 1552 | new_id = cursor.lastrowid |
||
| 1553 | cnx.commit() |
||
| 1554 | cursor.close() |
||
| 1555 | cnx.close() |
||
| 1556 | |||
| 1557 | resp.status = falcon.HTTP_201 |
||
| 1558 | resp.location = '/microgrids/' + id_ + '/evchargers/' + str(new_id) |
||
| 1559 | |||
| 1560 | |||
| 1561 | View Code Duplication | class MicrogridEVChargerItem: |
|
| 1562 | def __init__(self): |
||
| 1563 | pass |
||
| 1564 | |||
| 1565 | @staticmethod |
||
| 1566 | def on_options(req, resp, id_, eid): |
||
| 1567 | _ = req |
||
| 1568 | resp.status = falcon.HTTP_200 |
||
| 1569 | _ = id_ |
||
| 1570 | |||
| 1571 | @staticmethod |
||
| 1572 | def on_get(req, resp, id_, eid): |
||
| 1573 | access_control(req) |
||
| 1574 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1575 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1576 | description='API.INVALID_MICROGRID_ID') |
||
| 1577 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1578 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1579 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
||
| 1580 | |||
| 1581 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1582 | cursor = cnx.cursor() |
||
| 1583 | |||
| 1584 | cursor.execute(" SELECT name " |
||
| 1585 | " FROM tbl_microgrids " |
||
| 1586 | " WHERE id = %s ", (id_,)) |
||
| 1587 | if cursor.fetchone() is None: |
||
| 1588 | cursor.close() |
||
| 1589 | cnx.close() |
||
| 1590 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1591 | description='API.MICROGRID_NOT_FOUND') |
||
| 1592 | |||
| 1593 | # query microgrid dict |
||
| 1594 | query = (" SELECT id, name, uuid " |
||
| 1595 | " FROM tbl_microgrids ") |
||
| 1596 | cursor.execute(query) |
||
| 1597 | rows_microgrids = cursor.fetchall() |
||
| 1598 | |||
| 1599 | microgrid_dict = dict() |
||
| 1600 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 1601 | for row in rows_microgrids: |
||
| 1602 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 1603 | "name": row[1], |
||
| 1604 | "uuid": row[2]} |
||
| 1605 | # query meter dict |
||
| 1606 | query = (" SELECT id, name, uuid " |
||
| 1607 | " FROM tbl_meters ") |
||
| 1608 | cursor.execute(query) |
||
| 1609 | rows_meters = cursor.fetchall() |
||
| 1610 | |||
| 1611 | meter_dict = dict() |
||
| 1612 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1613 | for row in rows_meters: |
||
| 1614 | meter_dict[row[0]] = {"id": row[0], |
||
| 1615 | "name": row[1], |
||
| 1616 | "uuid": row[2]} |
||
| 1617 | # query point dict |
||
| 1618 | query = (" SELECT id, name " |
||
| 1619 | " FROM tbl_points ") |
||
| 1620 | cursor.execute(query) |
||
| 1621 | rows_points = cursor.fetchall() |
||
| 1622 | |||
| 1623 | point_dict = dict() |
||
| 1624 | if rows_points is not None and len(rows_points) > 0: |
||
| 1625 | for row in rows_points: |
||
| 1626 | point_dict[row[0]] = {"id": row[0], |
||
| 1627 | "name": row[1]} |
||
| 1628 | |||
| 1629 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
||
| 1630 | " FROM tbl_microgrids_evchargers " |
||
| 1631 | " WHERE id = %s ") |
||
| 1632 | cursor.execute(query, (eid,)) |
||
| 1633 | row = cursor.fetchone() |
||
| 1634 | cursor.close() |
||
| 1635 | cnx.close() |
||
| 1636 | |||
| 1637 | if row is None: |
||
| 1638 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1639 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
||
| 1640 | else: |
||
| 1641 | meta_result = {"id": row[0], |
||
| 1642 | "name": row[1], |
||
| 1643 | "uuid": row[2], |
||
| 1644 | "microgrid": microgrid_dict.get(row[3]), |
||
| 1645 | "power_point": point_dict.get(row[4]), |
||
| 1646 | "meter": meter_dict.get(row[5]), |
||
| 1647 | "rated_output_power": row[6]} |
||
| 1648 | |||
| 1649 | resp.text = json.dumps(meta_result) |
||
| 1650 | |||
| 1651 | @staticmethod |
||
| 1652 | @user_logger |
||
| 1653 | def on_delete(req, resp, id_, eid): |
||
| 1654 | admin_control(req) |
||
| 1655 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1656 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1657 | description='API.INVALID_MICROGRID_ID') |
||
| 1658 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1659 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1660 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
||
| 1661 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1662 | cursor = cnx.cursor() |
||
| 1663 | |||
| 1664 | cursor.execute(" SELECT name " |
||
| 1665 | " FROM tbl_microgrids " |
||
| 1666 | " WHERE id = %s ", (id_,)) |
||
| 1667 | if cursor.fetchone() is None: |
||
| 1668 | cursor.close() |
||
| 1669 | cnx.close() |
||
| 1670 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1671 | description='API.MICROGRID_NOT_FOUND') |
||
| 1672 | |||
| 1673 | cursor.execute(" SELECT name " |
||
| 1674 | " FROM tbl_microgrids_evchargers " |
||
| 1675 | " WHERE id = %s ", (eid,)) |
||
| 1676 | if cursor.fetchone() is None: |
||
| 1677 | cursor.close() |
||
| 1678 | cnx.close() |
||
| 1679 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1680 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
||
| 1681 | |||
| 1682 | cursor.execute(" DELETE FROM tbl_microgrids_evchargers " |
||
| 1683 | " WHERE id = %s ", (eid,)) |
||
| 1684 | cnx.commit() |
||
| 1685 | |||
| 1686 | cursor.close() |
||
| 1687 | cnx.close() |
||
| 1688 | |||
| 1689 | resp.status = falcon.HTTP_204 |
||
| 1690 | |||
| 1691 | @staticmethod |
||
| 1692 | @user_logger |
||
| 1693 | def on_put(req, resp, id_, eid): |
||
| 1694 | """Handles PUT requests""" |
||
| 1695 | admin_control(req) |
||
| 1696 | try: |
||
| 1697 | raw_json = req.stream.read().decode('utf-8') |
||
| 1698 | except UnicodeDecodeError as ex: |
||
| 1699 | print("Failed to decode request") |
||
| 1700 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1701 | title='API.BAD_REQUEST', |
||
| 1702 | description='API.INVALID_ENCODING') |
||
| 1703 | except Exception as ex: |
||
| 1704 | print(str(ex)) |
||
| 1705 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1706 | title='API.BAD_REQUEST', |
||
| 1707 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1708 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1709 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1710 | description='API.INVALID_MICROGRID_ID') |
||
| 1711 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1712 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1713 | description='API.INVALID_MICROGRID_EVCHARGER_ID') |
||
| 1714 | |||
| 1715 | new_values = json.loads(raw_json) |
||
| 1716 | |||
| 1717 | if 'name' not in new_values['data'].keys() or \ |
||
| 1718 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1719 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1720 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1721 | description='API.INVALID_MICROGRID_EVCHARGER_NAME') |
||
| 1722 | name = str.strip(new_values['data']['name']) |
||
| 1723 | |||
| 1724 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 1725 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 1726 | new_values['data']['power_point_id'] <= 0: |
||
| 1727 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1728 | description='API.INVALID_POWER_POINT_ID') |
||
| 1729 | power_point_id = new_values['data']['power_point_id'] |
||
| 1730 | |||
| 1731 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1732 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1733 | new_values['data']['meter_id'] <= 0: |
||
| 1734 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1735 | description='API.INVALID_METER_ID') |
||
| 1736 | meter_id = new_values['data']['meter_id'] |
||
| 1737 | |||
| 1738 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 1739 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 1740 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 1741 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1742 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 1743 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 1744 | |||
| 1745 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1746 | cursor = cnx.cursor() |
||
| 1747 | |||
| 1748 | cursor.execute(" SELECT name " |
||
| 1749 | " FROM tbl_microgrids " |
||
| 1750 | " WHERE id = %s ", (id_,)) |
||
| 1751 | if cursor.fetchone() is None: |
||
| 1752 | cursor.close() |
||
| 1753 | cnx.close() |
||
| 1754 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1755 | description='API.MICROGRID_NOT_FOUND') |
||
| 1756 | |||
| 1757 | cursor.execute(" SELECT name " |
||
| 1758 | " FROM tbl_microgrids_evchargers " |
||
| 1759 | " WHERE id = %s ", (eid,)) |
||
| 1760 | if cursor.fetchone() is None: |
||
| 1761 | cursor.close() |
||
| 1762 | cnx.close() |
||
| 1763 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1764 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
||
| 1765 | |||
| 1766 | cursor.execute(" SELECT name " |
||
| 1767 | " FROM tbl_microgrids_evchargers " |
||
| 1768 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 1769 | (id_, name, eid)) |
||
| 1770 | if cursor.fetchone() is not None: |
||
| 1771 | cursor.close() |
||
| 1772 | cnx.close() |
||
| 1773 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1774 | description='API.MICROGRID_EVCHARGER_NAME_IS_ALREADY_IN_USE') |
||
| 1775 | |||
| 1776 | cursor.execute(" SELECT name " |
||
| 1777 | " FROM tbl_points " |
||
| 1778 | " WHERE id = %s ", |
||
| 1779 | (power_point_id,)) |
||
| 1780 | if cursor.fetchone() is None: |
||
| 1781 | cursor.close() |
||
| 1782 | cnx.close() |
||
| 1783 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1784 | description='API.POWER_POINT_NOT_FOUND') |
||
| 1785 | |||
| 1786 | cursor.execute(" SELECT name " |
||
| 1787 | " FROM tbl_meters " |
||
| 1788 | " WHERE id = %s ", |
||
| 1789 | (meter_id,)) |
||
| 1790 | if cursor.fetchone() is None: |
||
| 1791 | cursor.close() |
||
| 1792 | cnx.close() |
||
| 1793 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1794 | description='API.METER_NOT_FOUND') |
||
| 1795 | |||
| 1796 | update_row = (" UPDATE tbl_microgrids_evchargers " |
||
| 1797 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
||
| 1798 | " WHERE id = %s ") |
||
| 1799 | cursor.execute(update_row, (name, |
||
| 1800 | id_, |
||
| 1801 | power_point_id, |
||
| 1802 | meter_id, |
||
| 1803 | rated_output_power, |
||
| 1804 | eid)) |
||
| 1805 | cnx.commit() |
||
| 1806 | |||
| 1807 | cursor.close() |
||
| 1808 | cnx.close() |
||
| 1809 | |||
| 1810 | resp.status = falcon.HTTP_200 |
||
| 1811 | |||
| 1812 | |||
| 1813 | View Code Duplication | class MicrogridGeneratorCollection: |
|
| 1814 | def __init__(self): |
||
| 1815 | pass |
||
| 1816 | |||
| 1817 | @staticmethod |
||
| 1818 | def on_options(req, resp, id_): |
||
| 1819 | _ = req |
||
| 1820 | resp.status = falcon.HTTP_200 |
||
| 1821 | _ = id_ |
||
| 1822 | |||
| 1823 | @staticmethod |
||
| 1824 | def on_get(req, resp, id_): |
||
| 1825 | access_control(req) |
||
| 1826 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1827 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1828 | description='API.INVALID_MICROGRID_ID') |
||
| 1829 | |||
| 1830 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1831 | cursor = cnx.cursor() |
||
| 1832 | |||
| 1833 | cursor.execute(" SELECT name " |
||
| 1834 | " FROM tbl_microgrids " |
||
| 1835 | " WHERE id = %s ", (id_,)) |
||
| 1836 | if cursor.fetchone() is None: |
||
| 1837 | cursor.close() |
||
| 1838 | cnx.close() |
||
| 1839 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1840 | description='API.MICROGRID_NOT_FOUND') |
||
| 1841 | |||
| 1842 | # query meter dict |
||
| 1843 | query = (" SELECT id, name, uuid " |
||
| 1844 | " FROM tbl_meters ") |
||
| 1845 | cursor.execute(query) |
||
| 1846 | rows_meters = cursor.fetchall() |
||
| 1847 | |||
| 1848 | meter_dict = dict() |
||
| 1849 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1850 | for row in rows_meters: |
||
| 1851 | meter_dict[row[0]] = {"id": row[0], |
||
| 1852 | "name": row[1], |
||
| 1853 | "uuid": row[2]} |
||
| 1854 | # query point dict |
||
| 1855 | query = (" SELECT id, name " |
||
| 1856 | " FROM tbl_points ") |
||
| 1857 | cursor.execute(query) |
||
| 1858 | rows_points = cursor.fetchall() |
||
| 1859 | |||
| 1860 | point_dict = dict() |
||
| 1861 | if rows_points is not None and len(rows_points) > 0: |
||
| 1862 | for row in rows_points: |
||
| 1863 | point_dict[row[0]] = {"id": row[0], |
||
| 1864 | "name": row[1]} |
||
| 1865 | |||
| 1866 | query = (" SELECT id, name, uuid, " |
||
| 1867 | " power_point_id, meter_id, rated_output_power " |
||
| 1868 | " FROM tbl_microgrids_generators " |
||
| 1869 | " WHERE microgrid_id = %s " |
||
| 1870 | " ORDER BY name ") |
||
| 1871 | cursor.execute(query, (id_,)) |
||
| 1872 | rows = cursor.fetchall() |
||
| 1873 | |||
| 1874 | result = list() |
||
| 1875 | if rows is not None and len(rows) > 0: |
||
| 1876 | for row in rows: |
||
| 1877 | meta_result = {"id": row[0], |
||
| 1878 | "name": row[1], |
||
| 1879 | "uuid": row[2], |
||
| 1880 | "power_point": point_dict.get(row[3]), |
||
| 1881 | "meter": meter_dict.get(row[4]), |
||
| 1882 | "rated_output_power": row[5]} |
||
| 1883 | result.append(meta_result) |
||
| 1884 | |||
| 1885 | resp.text = json.dumps(result) |
||
| 1886 | |||
| 1887 | @staticmethod |
||
| 1888 | @user_logger |
||
| 1889 | def on_post(req, resp, id_): |
||
| 1890 | """Handles POST requests""" |
||
| 1891 | admin_control(req) |
||
| 1892 | try: |
||
| 1893 | raw_json = req.stream.read().decode('utf-8') |
||
| 1894 | except UnicodeDecodeError as ex: |
||
| 1895 | print("Failed to decode request") |
||
| 1896 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1897 | title='API.BAD_REQUEST', |
||
| 1898 | description='API.INVALID_ENCODING') |
||
| 1899 | except Exception as ex: |
||
| 1900 | print(str(ex)) |
||
| 1901 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1902 | title='API.BAD_REQUEST', |
||
| 1903 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1904 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1905 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1906 | description='API.INVALID_MICROGRID_ID') |
||
| 1907 | |||
| 1908 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1909 | cursor = cnx.cursor() |
||
| 1910 | |||
| 1911 | cursor.execute(" SELECT name " |
||
| 1912 | " FROM tbl_microgrids " |
||
| 1913 | " WHERE id = %s ", (id_,)) |
||
| 1914 | if cursor.fetchone() is None: |
||
| 1915 | cursor.close() |
||
| 1916 | cnx.close() |
||
| 1917 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1918 | description='API.MICROGRID_NOT_FOUND') |
||
| 1919 | |||
| 1920 | new_values = json.loads(raw_json) |
||
| 1921 | |||
| 1922 | if 'name' not in new_values['data'].keys() or \ |
||
| 1923 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1924 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1925 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1926 | description='API.INVALID_MICROGRID_GENERATOR_NAME') |
||
| 1927 | name = str.strip(new_values['data']['name']) |
||
| 1928 | |||
| 1929 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 1930 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 1931 | new_values['data']['power_point_id'] <= 0: |
||
| 1932 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1933 | description='API.INVALID_POWER_POINT_ID') |
||
| 1934 | power_point_id = new_values['data']['power_point_id'] |
||
| 1935 | |||
| 1936 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1937 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1938 | new_values['data']['meter_id'] <= 0: |
||
| 1939 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1940 | description='API.INVALID_METER_ID') |
||
| 1941 | meter_id = new_values['data']['meter_id'] |
||
| 1942 | |||
| 1943 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 1944 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 1945 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 1946 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1947 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 1948 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 1949 | |||
| 1950 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1951 | cursor = cnx.cursor() |
||
| 1952 | |||
| 1953 | cursor.execute(" SELECT name " |
||
| 1954 | " FROM tbl_microgrids " |
||
| 1955 | " WHERE id = %s ", |
||
| 1956 | (id_,)) |
||
| 1957 | if cursor.fetchone() is None: |
||
| 1958 | cursor.close() |
||
| 1959 | cnx.close() |
||
| 1960 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1961 | description='API.MICROGRID_NOT_FOUND') |
||
| 1962 | |||
| 1963 | cursor.execute(" SELECT name " |
||
| 1964 | " FROM tbl_microgrids_generators " |
||
| 1965 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 1966 | (id_, name,)) |
||
| 1967 | if cursor.fetchone() is not None: |
||
| 1968 | cursor.close() |
||
| 1969 | cnx.close() |
||
| 1970 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1971 | description='API.MICROGRID_GENERATOR_NAME_IS_ALREADY_IN_USE') |
||
| 1972 | |||
| 1973 | cursor.execute(" SELECT name " |
||
| 1974 | " FROM tbl_points " |
||
| 1975 | " WHERE id = %s ", |
||
| 1976 | (power_point_id,)) |
||
| 1977 | if cursor.fetchone() is None: |
||
| 1978 | cursor.close() |
||
| 1979 | cnx.close() |
||
| 1980 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1981 | description='API.POWER_POINT_NOT_FOUND') |
||
| 1982 | |||
| 1983 | cursor.execute(" SELECT name " |
||
| 1984 | " FROM tbl_meters " |
||
| 1985 | " WHERE id = %s ", |
||
| 1986 | (meter_id,)) |
||
| 1987 | if cursor.fetchone() is None: |
||
| 1988 | cursor.close() |
||
| 1989 | cnx.close() |
||
| 1990 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1991 | description='API.METER_NOT_FOUND') |
||
| 1992 | |||
| 1993 | add_values = (" INSERT INTO tbl_microgrids_generators " |
||
| 1994 | " (name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power) " |
||
| 1995 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 1996 | cursor.execute(add_values, (name, |
||
| 1997 | str(uuid.uuid4()), |
||
| 1998 | id_, |
||
| 1999 | power_point_id, |
||
| 2000 | meter_id, |
||
| 2001 | rated_output_power)) |
||
| 2002 | new_id = cursor.lastrowid |
||
| 2003 | cnx.commit() |
||
| 2004 | cursor.close() |
||
| 2005 | cnx.close() |
||
| 2006 | |||
| 2007 | resp.status = falcon.HTTP_201 |
||
| 2008 | resp.location = '/microgrids/' + str(id_) + '/generators/' + str(new_id) |
||
| 2009 | |||
| 2010 | |||
| 2011 | View Code Duplication | class MicrogridGeneratorItem: |
|
| 2012 | def __init__(self): |
||
| 2013 | pass |
||
| 2014 | |||
| 2015 | @staticmethod |
||
| 2016 | def on_options(req, resp, id_, gid): |
||
| 2017 | _ = req |
||
| 2018 | resp.status = falcon.HTTP_200 |
||
| 2019 | _ = id_ |
||
| 2020 | |||
| 2021 | @staticmethod |
||
| 2022 | def on_get(req, resp, id_, gid): |
||
| 2023 | access_control(req) |
||
| 2024 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2025 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2026 | description='API.INVALID_MICROGRID_ID') |
||
| 2027 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2028 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2029 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
||
| 2030 | |||
| 2031 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2032 | cursor = cnx.cursor() |
||
| 2033 | |||
| 2034 | cursor.execute(" SELECT name " |
||
| 2035 | " FROM tbl_microgrids " |
||
| 2036 | " WHERE id = %s ", (id_,)) |
||
| 2037 | if cursor.fetchone() is None: |
||
| 2038 | cursor.close() |
||
| 2039 | cnx.close() |
||
| 2040 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2041 | description='API.MICROGRID_NOT_FOUND') |
||
| 2042 | |||
| 2043 | # query microgrid dict |
||
| 2044 | query = (" SELECT id, name, uuid " |
||
| 2045 | " FROM tbl_microgrids ") |
||
| 2046 | cursor.execute(query) |
||
| 2047 | rows_microgrids = cursor.fetchall() |
||
| 2048 | |||
| 2049 | microgrid_dict = dict() |
||
| 2050 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 2051 | for row in rows_microgrids: |
||
| 2052 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 2053 | "name": row[1], |
||
| 2054 | "uuid": row[2]} |
||
| 2055 | # query meter dict |
||
| 2056 | query = (" SELECT id, name, uuid " |
||
| 2057 | " FROM tbl_meters ") |
||
| 2058 | cursor.execute(query) |
||
| 2059 | rows_meters = cursor.fetchall() |
||
| 2060 | |||
| 2061 | meter_dict = dict() |
||
| 2062 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2063 | for row in rows_meters: |
||
| 2064 | meter_dict[row[0]] = {"id": row[0], |
||
| 2065 | "name": row[1], |
||
| 2066 | "uuid": row[2]} |
||
| 2067 | # query point dict |
||
| 2068 | query = (" SELECT id, name " |
||
| 2069 | " FROM tbl_points ") |
||
| 2070 | cursor.execute(query) |
||
| 2071 | rows_points = cursor.fetchall() |
||
| 2072 | |||
| 2073 | point_dict = dict() |
||
| 2074 | if rows_points is not None and len(rows_points) > 0: |
||
| 2075 | for row in rows_points: |
||
| 2076 | point_dict[row[0]] = {"id": row[0], |
||
| 2077 | "name": row[1]} |
||
| 2078 | |||
| 2079 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_output_power " |
||
| 2080 | " FROM tbl_microgrids_generators " |
||
| 2081 | " WHERE id = %s ") |
||
| 2082 | cursor.execute(query, (gid,)) |
||
| 2083 | row = cursor.fetchone() |
||
| 2084 | cursor.close() |
||
| 2085 | cnx.close() |
||
| 2086 | |||
| 2087 | if row is None: |
||
| 2088 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2089 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 2090 | else: |
||
| 2091 | meta_result = {"id": row[0], |
||
| 2092 | "name": row[1], |
||
| 2093 | "uuid": row[2], |
||
| 2094 | "microgrid": microgrid_dict.get(row[3]), |
||
| 2095 | "power_point": point_dict.get(row[4]), |
||
| 2096 | "meter": meter_dict.get(row[5]), |
||
| 2097 | "rated_output_power": row[6]} |
||
| 2098 | |||
| 2099 | resp.text = json.dumps(meta_result) |
||
| 2100 | |||
| 2101 | @staticmethod |
||
| 2102 | @user_logger |
||
| 2103 | def on_delete(req, resp, id_, gid): |
||
| 2104 | admin_control(req) |
||
| 2105 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2106 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2107 | description='API.INVALID_MICROGRID_ID') |
||
| 2108 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2109 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2110 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
||
| 2111 | |||
| 2112 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2113 | cursor = cnx.cursor() |
||
| 2114 | |||
| 2115 | cursor.execute(" SELECT name " |
||
| 2116 | " FROM tbl_microgrids " |
||
| 2117 | " WHERE id = %s ", (id_,)) |
||
| 2118 | if cursor.fetchone() is None: |
||
| 2119 | cursor.close() |
||
| 2120 | cnx.close() |
||
| 2121 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2122 | description='API.MICROGRID_NOT_FOUND') |
||
| 2123 | |||
| 2124 | cursor.execute(" SELECT name " |
||
| 2125 | " FROM tbl_microgrids_generators " |
||
| 2126 | " WHERE id = %s ", (gid,)) |
||
| 2127 | if cursor.fetchone() is None: |
||
| 2128 | cursor.close() |
||
| 2129 | cnx.close() |
||
| 2130 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2131 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 2132 | |||
| 2133 | cursor.execute(" DELETE FROM tbl_microgrids_generators " |
||
| 2134 | " WHERE id = %s ", (gid,)) |
||
| 2135 | cnx.commit() |
||
| 2136 | |||
| 2137 | cursor.close() |
||
| 2138 | cnx.close() |
||
| 2139 | |||
| 2140 | resp.status = falcon.HTTP_204 |
||
| 2141 | |||
| 2142 | @staticmethod |
||
| 2143 | @user_logger |
||
| 2144 | def on_put(req, resp, id_, gid): |
||
| 2145 | """Handles PUT requests""" |
||
| 2146 | admin_control(req) |
||
| 2147 | try: |
||
| 2148 | raw_json = req.stream.read().decode('utf-8') |
||
| 2149 | except UnicodeDecodeError as ex: |
||
| 2150 | print("Failed to decode request") |
||
| 2151 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2152 | title='API.BAD_REQUEST', |
||
| 2153 | description='API.INVALID_ENCODING') |
||
| 2154 | except Exception as ex: |
||
| 2155 | print(str(ex)) |
||
| 2156 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2157 | title='API.BAD_REQUEST', |
||
| 2158 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2159 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2161 | description='API.INVALID_MICROGRID_ID') |
||
| 2162 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2163 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2164 | description='API.INVALID_MICROGRID_GENERATOR_ID') |
||
| 2165 | |||
| 2166 | new_values = json.loads(raw_json) |
||
| 2167 | |||
| 2168 | if 'name' not in new_values['data'].keys() or \ |
||
| 2169 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2170 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2171 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2172 | description='API.INVALID_MICROGRID_GENERATOR_NAME') |
||
| 2173 | name = str.strip(new_values['data']['name']) |
||
| 2174 | |||
| 2175 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2176 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2177 | new_values['data']['power_point_id'] <= 0: |
||
| 2178 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2179 | description='API.INVALID_POWER_POINT_ID') |
||
| 2180 | power_point_id = new_values['data']['power_point_id'] |
||
| 2181 | |||
| 2182 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 2183 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 2184 | new_values['data']['meter_id'] <= 0: |
||
| 2185 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2186 | description='API.INVALID_METER_ID') |
||
| 2187 | meter_id = new_values['data']['meter_id'] |
||
| 2188 | |||
| 2189 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 2190 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 2191 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 2192 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2193 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 2194 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 2195 | |||
| 2196 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2197 | cursor = cnx.cursor() |
||
| 2198 | |||
| 2199 | cursor.execute(" SELECT name " |
||
| 2200 | " FROM tbl_microgrids " |
||
| 2201 | " WHERE id = %s ", (id_,)) |
||
| 2202 | if cursor.fetchone() is None: |
||
| 2203 | cursor.close() |
||
| 2204 | cnx.close() |
||
| 2205 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2206 | description='API.MICROGRID_NOT_FOUND') |
||
| 2207 | |||
| 2208 | cursor.execute(" SELECT name " |
||
| 2209 | " FROM tbl_microgrids_generators " |
||
| 2210 | " WHERE id = %s ", (gid,)) |
||
| 2211 | if cursor.fetchone() is None: |
||
| 2212 | cursor.close() |
||
| 2213 | cnx.close() |
||
| 2214 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2215 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 2216 | |||
| 2217 | cursor.execute(" SELECT name " |
||
| 2218 | " FROM tbl_microgrids_generators " |
||
| 2219 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 2220 | (id_, name, gid)) |
||
| 2221 | if cursor.fetchone() is not None: |
||
| 2222 | cursor.close() |
||
| 2223 | cnx.close() |
||
| 2224 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2225 | description='API.MICROGRID_GENERATOR_NAME_IS_ALREADY_IN_USE') |
||
| 2226 | |||
| 2227 | cursor.execute(" SELECT name " |
||
| 2228 | " FROM tbl_points " |
||
| 2229 | " WHERE id = %s ", |
||
| 2230 | (power_point_id,)) |
||
| 2231 | if cursor.fetchone() is None: |
||
| 2232 | cursor.close() |
||
| 2233 | cnx.close() |
||
| 2234 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2235 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2236 | |||
| 2237 | cursor.execute(" SELECT name " |
||
| 2238 | " FROM tbl_meters " |
||
| 2239 | " WHERE id = %s ", |
||
| 2240 | (meter_id,)) |
||
| 2241 | if cursor.fetchone() is None: |
||
| 2242 | cursor.close() |
||
| 2243 | cnx.close() |
||
| 2244 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2245 | description='API.METER_NOT_FOUND') |
||
| 2246 | |||
| 2247 | update_row = (" UPDATE tbl_microgrids_generators " |
||
| 2248 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_output_power = %s " |
||
| 2249 | " WHERE id = %s ") |
||
| 2250 | cursor.execute(update_row, (name, |
||
| 2251 | id_, |
||
| 2252 | power_point_id, |
||
| 2253 | meter_id, |
||
| 2254 | rated_output_power, |
||
| 2255 | gid)) |
||
| 2256 | cnx.commit() |
||
| 2257 | |||
| 2258 | cursor.close() |
||
| 2259 | cnx.close() |
||
| 2260 | |||
| 2261 | resp.status = falcon.HTTP_200 |
||
| 2262 | |||
| 2263 | |||
| 2264 | View Code Duplication | class MicrogridGridCollection: |
|
| 2265 | def __init__(self): |
||
| 2266 | pass |
||
| 2267 | |||
| 2268 | @staticmethod |
||
| 2269 | def on_options(req, resp, id_): |
||
| 2270 | _ = req |
||
| 2271 | resp.status = falcon.HTTP_200 |
||
| 2272 | _ = id_ |
||
| 2273 | |||
| 2274 | @staticmethod |
||
| 2275 | def on_get(req, resp, id_): |
||
| 2276 | access_control(req) |
||
| 2277 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2278 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2279 | description='API.INVALID_MICROGRID_ID') |
||
| 2280 | |||
| 2281 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2282 | cursor = cnx.cursor() |
||
| 2283 | |||
| 2284 | cursor.execute(" SELECT name " |
||
| 2285 | " FROM tbl_microgrids " |
||
| 2286 | " WHERE id = %s ", (id_,)) |
||
| 2287 | if cursor.fetchone() is None: |
||
| 2288 | cursor.close() |
||
| 2289 | cnx.close() |
||
| 2290 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2291 | description='API.MICROGRID_NOT_FOUND') |
||
| 2292 | |||
| 2293 | # query meter dict |
||
| 2294 | query = (" SELECT id, name, uuid " |
||
| 2295 | " FROM tbl_meters ") |
||
| 2296 | cursor.execute(query) |
||
| 2297 | rows_meters = cursor.fetchall() |
||
| 2298 | |||
| 2299 | meter_dict = dict() |
||
| 2300 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2301 | for row in rows_meters: |
||
| 2302 | meter_dict[row[0]] = {"id": row[0], |
||
| 2303 | "name": row[1], |
||
| 2304 | "uuid": row[2]} |
||
| 2305 | # query point dict |
||
| 2306 | query = (" SELECT id, name " |
||
| 2307 | " FROM tbl_points ") |
||
| 2308 | cursor.execute(query) |
||
| 2309 | rows_points = cursor.fetchall() |
||
| 2310 | |||
| 2311 | point_dict = dict() |
||
| 2312 | if rows_points is not None and len(rows_points) > 0: |
||
| 2313 | for row in rows_points: |
||
| 2314 | point_dict[row[0]] = {"id": row[0], |
||
| 2315 | "name": row[1]} |
||
| 2316 | |||
| 2317 | query = (" SELECT id, name, uuid, " |
||
| 2318 | " power_point_id, buy_meter_id, sell_meter_id, capacity " |
||
| 2319 | " FROM tbl_microgrids_grids " |
||
| 2320 | " WHERE microgrid_id = %s " |
||
| 2321 | " ORDER BY name ") |
||
| 2322 | cursor.execute(query, (id_,)) |
||
| 2323 | rows = cursor.fetchall() |
||
| 2324 | |||
| 2325 | result = list() |
||
| 2326 | if rows is not None and len(rows) > 0: |
||
| 2327 | for row in rows: |
||
| 2328 | meta_result = {"id": row[0], |
||
| 2329 | "name": row[1], |
||
| 2330 | "uuid": row[2], |
||
| 2331 | "power_point": point_dict.get(row[3]), |
||
| 2332 | "buy_meter": meter_dict.get(row[4]), |
||
| 2333 | "sell_meter": meter_dict.get(row[5]), |
||
| 2334 | "capacity": row[6]} |
||
| 2335 | result.append(meta_result) |
||
| 2336 | |||
| 2337 | resp.text = json.dumps(result) |
||
| 2338 | |||
| 2339 | @staticmethod |
||
| 2340 | @user_logger |
||
| 2341 | def on_post(req, resp, id_): |
||
| 2342 | """Handles POST requests""" |
||
| 2343 | admin_control(req) |
||
| 2344 | try: |
||
| 2345 | raw_json = req.stream.read().decode('utf-8') |
||
| 2346 | except UnicodeDecodeError as ex: |
||
| 2347 | print("Failed to decode request") |
||
| 2348 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2349 | title='API.BAD_REQUEST', |
||
| 2350 | description='API.INVALID_ENCODING') |
||
| 2351 | except Exception as ex: |
||
| 2352 | print(str(ex)) |
||
| 2353 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2354 | title='API.BAD_REQUEST', |
||
| 2355 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2356 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2357 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2358 | description='API.INVALID_MICROGRID_ID') |
||
| 2359 | |||
| 2360 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2361 | cursor = cnx.cursor() |
||
| 2362 | |||
| 2363 | cursor.execute(" SELECT name " |
||
| 2364 | " FROM tbl_microgrids " |
||
| 2365 | " WHERE id = %s ", (id_,)) |
||
| 2366 | if cursor.fetchone() is None: |
||
| 2367 | cursor.close() |
||
| 2368 | cnx.close() |
||
| 2369 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2370 | description='API.MICROGRID_NOT_FOUND') |
||
| 2371 | |||
| 2372 | new_values = json.loads(raw_json) |
||
| 2373 | |||
| 2374 | if 'name' not in new_values['data'].keys() or \ |
||
| 2375 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2376 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2377 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2378 | description='API.INVALID_MICROGRID_GRID_NAME') |
||
| 2379 | name = str.strip(new_values['data']['name']) |
||
| 2380 | |||
| 2381 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2382 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2383 | new_values['data']['power_point_id'] <= 0: |
||
| 2384 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2385 | description='API.INVALID_POWER_POINT_ID') |
||
| 2386 | power_point_id = new_values['data']['power_point_id'] |
||
| 2387 | |||
| 2388 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 2389 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 2390 | new_values['data']['buy_meter_id'] <= 0: |
||
| 2391 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2392 | description='API.INVALID_BUY_METER_ID') |
||
| 2393 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 2394 | |||
| 2395 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 2396 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 2397 | new_values['data']['sell_meter_id'] <= 0: |
||
| 2398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2399 | description='API.INVALID_SELL_METER_ID') |
||
| 2400 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 2401 | |||
| 2402 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 2403 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 2404 | isinstance(new_values['data']['capacity'], int)): |
||
| 2405 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2406 | description='API.INVALID_CAPACITY') |
||
| 2407 | capacity = float(new_values['data']['capacity']) |
||
| 2408 | |||
| 2409 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2410 | cursor = cnx.cursor() |
||
| 2411 | |||
| 2412 | cursor.execute(" SELECT name " |
||
| 2413 | " FROM tbl_microgrids " |
||
| 2414 | " WHERE id = %s ", |
||
| 2415 | (id_,)) |
||
| 2416 | if cursor.fetchone() is None: |
||
| 2417 | cursor.close() |
||
| 2418 | cnx.close() |
||
| 2419 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2420 | description='API.MICROGRID_NOT_FOUND') |
||
| 2421 | |||
| 2422 | cursor.execute(" SELECT name " |
||
| 2423 | " FROM tbl_microgrids_grids " |
||
| 2424 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 2425 | (id_, name,)) |
||
| 2426 | if cursor.fetchone() is not None: |
||
| 2427 | cursor.close() |
||
| 2428 | cnx.close() |
||
| 2429 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2430 | description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 2431 | |||
| 2432 | cursor.execute(" SELECT name " |
||
| 2433 | " FROM tbl_points " |
||
| 2434 | " WHERE id = %s ", |
||
| 2435 | (power_point_id,)) |
||
| 2436 | if cursor.fetchone() is None: |
||
| 2437 | cursor.close() |
||
| 2438 | cnx.close() |
||
| 2439 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2440 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2441 | |||
| 2442 | cursor.execute(" SELECT name " |
||
| 2443 | " FROM tbl_meters " |
||
| 2444 | " WHERE id = %s ", |
||
| 2445 | (buy_meter_id,)) |
||
| 2446 | if cursor.fetchone() is None: |
||
| 2447 | cursor.close() |
||
| 2448 | cnx.close() |
||
| 2449 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2450 | description='API.BUY_METER_NOT_FOUND') |
||
| 2451 | |||
| 2452 | cursor.execute(" SELECT name " |
||
| 2453 | " FROM tbl_meters " |
||
| 2454 | " WHERE id = %s ", |
||
| 2455 | (sell_meter_id,)) |
||
| 2456 | if cursor.fetchone() is None: |
||
| 2457 | cursor.close() |
||
| 2458 | cnx.close() |
||
| 2459 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2460 | description='API.SELL_METER_NOT_FOUND') |
||
| 2461 | |||
| 2462 | add_values = (" INSERT INTO tbl_microgrids_grids " |
||
| 2463 | " (name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity) " |
||
| 2464 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 2465 | cursor.execute(add_values, (name, |
||
| 2466 | str(uuid.uuid4()), |
||
| 2467 | id_, |
||
| 2468 | power_point_id, |
||
| 2469 | buy_meter_id, |
||
| 2470 | sell_meter_id, |
||
| 2471 | capacity)) |
||
| 2472 | new_id = cursor.lastrowid |
||
| 2473 | cnx.commit() |
||
| 2474 | cursor.close() |
||
| 2475 | cnx.close() |
||
| 2476 | |||
| 2477 | resp.status = falcon.HTTP_201 |
||
| 2478 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(new_id) |
||
| 2479 | |||
| 2480 | |||
| 2481 | View Code Duplication | class MicrogridGridItem: |
|
| 2482 | def __init__(self): |
||
| 2483 | pass |
||
| 2484 | |||
| 2485 | @staticmethod |
||
| 2486 | def on_options(req, resp, id_, gid): |
||
| 2487 | _ = req |
||
| 2488 | resp.status = falcon.HTTP_200 |
||
| 2489 | _ = id_ |
||
| 2490 | |||
| 2491 | @staticmethod |
||
| 2492 | def on_get(req, resp, id_, gid): |
||
| 2493 | access_control(req) |
||
| 2494 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2495 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2496 | description='API.INVALID_MICROGRID_ID') |
||
| 2497 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2498 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2499 | description='API.INVALID_MICROGRID_GRID_ID') |
||
| 2500 | |||
| 2501 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2502 | cursor = cnx.cursor() |
||
| 2503 | |||
| 2504 | cursor.execute(" SELECT name " |
||
| 2505 | " FROM tbl_microgrids " |
||
| 2506 | " WHERE id = %s ", (id_,)) |
||
| 2507 | if cursor.fetchone() is None: |
||
| 2508 | cursor.close() |
||
| 2509 | cnx.close() |
||
| 2510 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2511 | description='API.MICROGRID_NOT_FOUND') |
||
| 2512 | |||
| 2513 | # query microgrid dict |
||
| 2514 | query = (" SELECT id, name, uuid " |
||
| 2515 | " FROM tbl_microgrids ") |
||
| 2516 | cursor.execute(query) |
||
| 2517 | rows_microgrids = cursor.fetchall() |
||
| 2518 | |||
| 2519 | microgrid_dict = dict() |
||
| 2520 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 2521 | for row in rows_microgrids: |
||
| 2522 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 2523 | "name": row[1], |
||
| 2524 | "uuid": row[2]} |
||
| 2525 | # query meter dict |
||
| 2526 | query = (" SELECT id, name, uuid " |
||
| 2527 | " FROM tbl_meters ") |
||
| 2528 | cursor.execute(query) |
||
| 2529 | rows_meters = cursor.fetchall() |
||
| 2530 | |||
| 2531 | meter_dict = dict() |
||
| 2532 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2533 | for row in rows_meters: |
||
| 2534 | meter_dict[row[0]] = {"id": row[0], |
||
| 2535 | "name": row[1], |
||
| 2536 | "uuid": row[2]} |
||
| 2537 | # query point dict |
||
| 2538 | query = (" SELECT id, name " |
||
| 2539 | " FROM tbl_points ") |
||
| 2540 | cursor.execute(query) |
||
| 2541 | rows_points = cursor.fetchall() |
||
| 2542 | |||
| 2543 | point_dict = dict() |
||
| 2544 | if rows_points is not None and len(rows_points) > 0: |
||
| 2545 | for row in rows_points: |
||
| 2546 | point_dict[row[0]] = {"id": row[0], |
||
| 2547 | "name": row[1]} |
||
| 2548 | |||
| 2549 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity " |
||
| 2550 | " FROM tbl_microgrids_grids " |
||
| 2551 | " WHERE id = %s ") |
||
| 2552 | cursor.execute(query, (gid,)) |
||
| 2553 | row = cursor.fetchone() |
||
| 2554 | cursor.close() |
||
| 2555 | cnx.close() |
||
| 2556 | |||
| 2557 | if row is None: |
||
| 2558 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2559 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 2560 | else: |
||
| 2561 | meta_result = {"id": row[0], |
||
| 2562 | "name": row[1], |
||
| 2563 | "uuid": row[2], |
||
| 2564 | "microgrid": microgrid_dict.get(row[3]), |
||
| 2565 | "power_point": point_dict.get(row[4]), |
||
| 2566 | "buy_meter": meter_dict.get(row[5]), |
||
| 2567 | "sell_meter": meter_dict.get(row[6]), |
||
| 2568 | "capacity": row[7]} |
||
| 2569 | |||
| 2570 | resp.text = json.dumps(meta_result) |
||
| 2571 | |||
| 2572 | @staticmethod |
||
| 2573 | @user_logger |
||
| 2574 | def on_delete(req, resp, id_, gid): |
||
| 2575 | admin_control(req) |
||
| 2576 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2577 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2578 | description='API.INVALID_MICROGRID_ID') |
||
| 2579 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2581 | description='API.INVALID_MICROGRID_GRID_ID') |
||
| 2582 | |||
| 2583 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2584 | cursor = cnx.cursor() |
||
| 2585 | |||
| 2586 | cursor.execute(" SELECT name " |
||
| 2587 | " FROM tbl_microgrids " |
||
| 2588 | " WHERE id = %s ", (id_,)) |
||
| 2589 | if cursor.fetchone() is None: |
||
| 2590 | cursor.close() |
||
| 2591 | cnx.close() |
||
| 2592 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2593 | description='API.MICROGRID_NOT_FOUND') |
||
| 2594 | |||
| 2595 | cursor.execute(" SELECT name " |
||
| 2596 | " FROM tbl_microgrids_grids " |
||
| 2597 | " WHERE id = %s ", (gid,)) |
||
| 2598 | if cursor.fetchone() is None: |
||
| 2599 | cursor.close() |
||
| 2600 | cnx.close() |
||
| 2601 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2602 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 2603 | |||
| 2604 | cursor.execute(" DELETE FROM tbl_microgrids_grids " |
||
| 2605 | " WHERE id = %s ", (gid,)) |
||
| 2606 | cnx.commit() |
||
| 2607 | |||
| 2608 | cursor.close() |
||
| 2609 | cnx.close() |
||
| 2610 | |||
| 2611 | resp.status = falcon.HTTP_204 |
||
| 2612 | |||
| 2613 | @staticmethod |
||
| 2614 | @user_logger |
||
| 2615 | def on_put(req, resp, id_, gid): |
||
| 2616 | """Handles PUT requests""" |
||
| 2617 | admin_control(req) |
||
| 2618 | try: |
||
| 2619 | raw_json = req.stream.read().decode('utf-8') |
||
| 2620 | except UnicodeDecodeError as ex: |
||
| 2621 | print("Failed to decode request") |
||
| 2622 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2623 | title='API.BAD_REQUEST', |
||
| 2624 | description='API.INVALID_ENCODING') |
||
| 2625 | except Exception as ex: |
||
| 2626 | print(str(ex)) |
||
| 2627 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2628 | title='API.BAD_REQUEST', |
||
| 2629 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2630 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2631 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2632 | description='API.INVALID_MICROGRID_ID') |
||
| 2633 | |||
| 2634 | if not gid.isdigit() or int(gid) <= 0: |
||
| 2635 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2636 | description='API.INVALID_MICROGRID_GRID_ID') |
||
| 2637 | |||
| 2638 | new_values = json.loads(raw_json) |
||
| 2639 | |||
| 2640 | if 'name' not in new_values['data'].keys() or \ |
||
| 2641 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2642 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2643 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2644 | description='API.INVALID_MICROGRID_GRID_NAME') |
||
| 2645 | name = str.strip(new_values['data']['name']) |
||
| 2646 | |||
| 2647 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2648 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2649 | new_values['data']['power_point_id'] <= 0: |
||
| 2650 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2651 | description='API.INVALID_POWER_POINT_ID') |
||
| 2652 | power_point_id = new_values['data']['power_point_id'] |
||
| 2653 | |||
| 2654 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
||
| 2655 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
||
| 2656 | new_values['data']['buy_meter_id'] <= 0: |
||
| 2657 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2658 | description='API.INVALID_BUY_METER_ID') |
||
| 2659 | buy_meter_id = new_values['data']['buy_meter_id'] |
||
| 2660 | |||
| 2661 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
||
| 2662 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
||
| 2663 | new_values['data']['sell_meter_id'] <= 0: |
||
| 2664 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2665 | description='API.INVALID_METER_ID') |
||
| 2666 | sell_meter_id = new_values['data']['sell_meter_id'] |
||
| 2667 | |||
| 2668 | if 'capacity' not in new_values['data'].keys() or \ |
||
| 2669 | not (isinstance(new_values['data']['capacity'], float) or |
||
| 2670 | isinstance(new_values['data']['capacity'], int)): |
||
| 2671 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2672 | description='API.INVALID_CAPACITY') |
||
| 2673 | capacity = float(new_values['data']['capacity']) |
||
| 2674 | |||
| 2675 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2676 | cursor = cnx.cursor() |
||
| 2677 | |||
| 2678 | cursor.execute(" SELECT name " |
||
| 2679 | " FROM tbl_microgrids " |
||
| 2680 | " WHERE id = %s ", (id_,)) |
||
| 2681 | if cursor.fetchone() is None: |
||
| 2682 | cursor.close() |
||
| 2683 | cnx.close() |
||
| 2684 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2685 | description='API.MICROGRID_NOT_FOUND') |
||
| 2686 | |||
| 2687 | cursor.execute(" SELECT name " |
||
| 2688 | " FROM tbl_microgrids_grids " |
||
| 2689 | " WHERE id = %s ", (gid,)) |
||
| 2690 | if cursor.fetchone() is None: |
||
| 2691 | cursor.close() |
||
| 2692 | cnx.close() |
||
| 2693 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2694 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 2695 | |||
| 2696 | cursor.execute(" SELECT name " |
||
| 2697 | " FROM tbl_microgrids_grids " |
||
| 2698 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 2699 | (id_, name, gid)) |
||
| 2700 | if cursor.fetchone() is not None: |
||
| 2701 | cursor.close() |
||
| 2702 | cnx.close() |
||
| 2703 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2704 | description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
||
| 2705 | |||
| 2706 | cursor.execute(" SELECT name " |
||
| 2707 | " FROM tbl_points " |
||
| 2708 | " WHERE id = %s ", |
||
| 2709 | (power_point_id,)) |
||
| 2710 | if cursor.fetchone() is None: |
||
| 2711 | cursor.close() |
||
| 2712 | cnx.close() |
||
| 2713 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2714 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2715 | |||
| 2716 | cursor.execute(" SELECT name " |
||
| 2717 | " FROM tbl_meters " |
||
| 2718 | " WHERE id = %s ", |
||
| 2719 | (buy_meter_id,)) |
||
| 2720 | if cursor.fetchone() is None: |
||
| 2721 | cursor.close() |
||
| 2722 | cnx.close() |
||
| 2723 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2724 | description='API.BUY_METER_NOT_FOUND') |
||
| 2725 | |||
| 2726 | cursor.execute(" SELECT name " |
||
| 2727 | " FROM tbl_meters " |
||
| 2728 | " WHERE id = %s ", |
||
| 2729 | (sell_meter_id,)) |
||
| 2730 | if cursor.fetchone() is None: |
||
| 2731 | cursor.close() |
||
| 2732 | cnx.close() |
||
| 2733 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2734 | description='API.SELL_METER_NOT_FOUND') |
||
| 2735 | |||
| 2736 | update_row = (" UPDATE tbl_microgrids_grids " |
||
| 2737 | " SET name = %s, microgrid_id = %s, " |
||
| 2738 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
||
| 2739 | " WHERE id = %s ") |
||
| 2740 | cursor.execute(update_row, (name, |
||
| 2741 | id_, |
||
| 2742 | power_point_id, |
||
| 2743 | buy_meter_id, |
||
| 2744 | sell_meter_id, |
||
| 2745 | capacity, |
||
| 2746 | gid)) |
||
| 2747 | cnx.commit() |
||
| 2748 | |||
| 2749 | cursor.close() |
||
| 2750 | cnx.close() |
||
| 2751 | |||
| 2752 | resp.status = falcon.HTTP_200 |
||
| 2753 | |||
| 2754 | |||
| 2755 | class MicrogridHeatpumpCollection: |
||
| 2756 | def __init__(self): |
||
| 2757 | pass |
||
| 2758 | |||
| 2759 | @staticmethod |
||
| 2760 | def on_options(req, resp, id_): |
||
| 2761 | _ = req |
||
| 2762 | resp.status = falcon.HTTP_200 |
||
| 2763 | _ = id_ |
||
| 2764 | |||
| 2765 | @staticmethod |
||
| 2766 | def on_get(req, resp, id_): |
||
| 2767 | access_control(req) |
||
| 2768 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2769 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2770 | description='API.INVALID_MICROGRID_ID') |
||
| 2771 | |||
| 2772 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2773 | cursor = cnx.cursor() |
||
| 2774 | |||
| 2775 | cursor.execute(" SELECT name " |
||
| 2776 | " FROM tbl_microgrids " |
||
| 2777 | " WHERE id = %s ", (id_,)) |
||
| 2778 | if cursor.fetchone() is None: |
||
| 2779 | cursor.close() |
||
| 2780 | cnx.close() |
||
| 2781 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2782 | description='API.MICROGRID_NOT_FOUND') |
||
| 2783 | |||
| 2784 | # query meter dict |
||
| 2785 | query = (" SELECT id, name, uuid " |
||
| 2786 | " FROM tbl_meters ") |
||
| 2787 | cursor.execute(query) |
||
| 2788 | rows_meters = cursor.fetchall() |
||
| 2789 | |||
| 2790 | meter_dict = dict() |
||
| 2791 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2792 | for row in rows_meters: |
||
| 2793 | meter_dict[row[0]] = {"id": row[0], |
||
| 2794 | "name": row[1], |
||
| 2795 | "uuid": row[2]} |
||
| 2796 | # query point dict |
||
| 2797 | query = (" SELECT id, name " |
||
| 2798 | " FROM tbl_points ") |
||
| 2799 | cursor.execute(query) |
||
| 2800 | rows_points = cursor.fetchall() |
||
| 2801 | |||
| 2802 | point_dict = dict() |
||
| 2803 | if rows_points is not None and len(rows_points) > 0: |
||
| 2804 | for row in rows_points: |
||
| 2805 | point_dict[row[0]] = {"id": row[0], |
||
| 2806 | "name": row[1]} |
||
| 2807 | |||
| 2808 | query = (" SELECT id, name, uuid, " |
||
| 2809 | " power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power " |
||
| 2810 | " FROM tbl_microgrids_heatpumps " |
||
| 2811 | " WHERE microgrid_id = %s " |
||
| 2812 | " ORDER BY name ") |
||
| 2813 | cursor.execute(query, (id_,)) |
||
| 2814 | rows = cursor.fetchall() |
||
| 2815 | |||
| 2816 | result = list() |
||
| 2817 | if rows is not None and len(rows) > 0: |
||
| 2818 | for row in rows: |
||
| 2819 | meta_result = {"id": row[0], |
||
| 2820 | "name": row[1], |
||
| 2821 | "uuid": row[2], |
||
| 2822 | "power_point": point_dict.get(row[3], None), |
||
| 2823 | "electricity_meter": meter_dict.get(row[4], None), |
||
| 2824 | "heat_meter": meter_dict.get(row[5], None), |
||
| 2825 | "cooling_meter": meter_dict.get(row[6], None), |
||
| 2826 | "rated_input_power": row[7]} |
||
| 2827 | result.append(meta_result) |
||
| 2828 | |||
| 2829 | resp.text = json.dumps(result) |
||
| 2830 | |||
| 2831 | @staticmethod |
||
| 2832 | @user_logger |
||
| 2833 | def on_post(req, resp, id_): |
||
| 2834 | """Handles POST requests""" |
||
| 2835 | admin_control(req) |
||
| 2836 | try: |
||
| 2837 | raw_json = req.stream.read().decode('utf-8') |
||
| 2838 | except UnicodeDecodeError as ex: |
||
| 2839 | print("Failed to decode request") |
||
| 2840 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2841 | title='API.BAD_REQUEST', |
||
| 2842 | description='API.INVALID_ENCODING') |
||
| 2843 | except Exception as ex: |
||
| 2844 | print(str(ex)) |
||
| 2845 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2846 | title='API.BAD_REQUEST', |
||
| 2847 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2848 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2849 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2850 | description='API.INVALID_MICROGRID_ID') |
||
| 2851 | |||
| 2852 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2853 | cursor = cnx.cursor() |
||
| 2854 | |||
| 2855 | cursor.execute(" SELECT name " |
||
| 2856 | " FROM tbl_microgrids " |
||
| 2857 | " WHERE id = %s ", (id_,)) |
||
| 2858 | if cursor.fetchone() is None: |
||
| 2859 | cursor.close() |
||
| 2860 | cnx.close() |
||
| 2861 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2862 | description='API.MICROGRID_NOT_FOUND') |
||
| 2863 | |||
| 2864 | new_values = json.loads(raw_json) |
||
| 2865 | |||
| 2866 | if 'name' not in new_values['data'].keys() or \ |
||
| 2867 | not isinstance(new_values['data']['name'], str) or \ |
||
| 2868 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 2869 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2870 | description='API.INVALID_MICROGRID_HEATPUMP_NAME') |
||
| 2871 | name = str.strip(new_values['data']['name']) |
||
| 2872 | |||
| 2873 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 2874 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 2875 | new_values['data']['power_point_id'] <= 0: |
||
| 2876 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2877 | description='API.INVALID_POWER_POINT_ID') |
||
| 2878 | power_point_id = new_values['data']['power_point_id'] |
||
| 2879 | |||
| 2880 | if 'electricity_meter_id' not in new_values['data'].keys() or \ |
||
| 2881 | not isinstance(new_values['data']['electricity_meter_id'], int) or \ |
||
| 2882 | new_values['data']['electricity_meter_id'] <= 0: |
||
| 2883 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2884 | description='API.INVALID_ELECTRICITY_METER_ID') |
||
| 2885 | electricity_meter_id = new_values['data']['electricity_meter_id'] |
||
| 2886 | |||
| 2887 | if 'heat_meter_id' not in new_values['data'].keys() or \ |
||
| 2888 | not isinstance(new_values['data']['heat_meter_id'], int) or \ |
||
| 2889 | new_values['data']['heat_meter_id'] <= 0: |
||
| 2890 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2891 | description='API.INVALID_SELL_METER_ID') |
||
| 2892 | heat_meter_id = new_values['data']['heat_meter_id'] |
||
| 2893 | |||
| 2894 | if 'cooling_meter_id' not in new_values['data'].keys() or \ |
||
| 2895 | not isinstance(new_values['data']['cooling_meter_id'], int) or \ |
||
| 2896 | new_values['data']['cooling_meter_id'] <= 0: |
||
| 2897 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2898 | description='API.INVALID_COOLING_METER_ID') |
||
| 2899 | cooling_meter_id = new_values['data']['cooling_meter_id'] |
||
| 2900 | |||
| 2901 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 2902 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 2903 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 2904 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2905 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 2906 | rated_input_power = float(new_values['data']['rated_input_power']) |
||
| 2907 | |||
| 2908 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2909 | cursor = cnx.cursor() |
||
| 2910 | |||
| 2911 | cursor.execute(" SELECT name " |
||
| 2912 | " FROM tbl_microgrids " |
||
| 2913 | " WHERE id = %s ", |
||
| 2914 | (id_,)) |
||
| 2915 | if cursor.fetchone() is None: |
||
| 2916 | cursor.close() |
||
| 2917 | cnx.close() |
||
| 2918 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2919 | description='API.MICROGRID_NOT_FOUND') |
||
| 2920 | |||
| 2921 | cursor.execute(" SELECT name " |
||
| 2922 | " FROM tbl_microgrids_heatpumps " |
||
| 2923 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 2924 | (id_, name,)) |
||
| 2925 | if cursor.fetchone() is not None: |
||
| 2926 | cursor.close() |
||
| 2927 | cnx.close() |
||
| 2928 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2929 | description='API.MICROGRID_HEATPUMP_NAME_IS_ALREADY_IN_USE') |
||
| 2930 | |||
| 2931 | cursor.execute(" SELECT name " |
||
| 2932 | " FROM tbl_points " |
||
| 2933 | " WHERE id = %s ", |
||
| 2934 | (power_point_id,)) |
||
| 2935 | if cursor.fetchone() is None: |
||
| 2936 | cursor.close() |
||
| 2937 | cnx.close() |
||
| 2938 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2939 | description='API.POWER_POINT_NOT_FOUND') |
||
| 2940 | |||
| 2941 | cursor.execute(" SELECT name " |
||
| 2942 | " FROM tbl_meters " |
||
| 2943 | " WHERE id = %s ", |
||
| 2944 | (electricity_meter_id,)) |
||
| 2945 | if cursor.fetchone() is None: |
||
| 2946 | cursor.close() |
||
| 2947 | cnx.close() |
||
| 2948 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2949 | description='API.ELECTRICITY_METER_NOT_FOUND') |
||
| 2950 | |||
| 2951 | cursor.execute(" SELECT name " |
||
| 2952 | " FROM tbl_meters " |
||
| 2953 | " WHERE id = %s ", |
||
| 2954 | (heat_meter_id,)) |
||
| 2955 | if cursor.fetchone() is None: |
||
| 2956 | cursor.close() |
||
| 2957 | cnx.close() |
||
| 2958 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2959 | description='API.HEAT_METER_NOT_FOUND') |
||
| 2960 | |||
| 2961 | cursor.execute(" SELECT name " |
||
| 2962 | " FROM tbl_meters " |
||
| 2963 | " WHERE id = %s ", |
||
| 2964 | (cooling_meter_id,)) |
||
| 2965 | if cursor.fetchone() is None: |
||
| 2966 | cursor.close() |
||
| 2967 | cnx.close() |
||
| 2968 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2969 | description='API.COOLING_METER_NOT_FOUND') |
||
| 2970 | |||
| 2971 | add_values = (" INSERT INTO tbl_microgrids_heatpumps " |
||
| 2972 | " (name, uuid, microgrid_id, " |
||
| 2973 | " power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power) " |
||
| 2974 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 2975 | cursor.execute(add_values, (name, |
||
| 2976 | str(uuid.uuid4()), |
||
| 2977 | id_, |
||
| 2978 | power_point_id, |
||
| 2979 | electricity_meter_id, |
||
| 2980 | heat_meter_id, |
||
| 2981 | cooling_meter_id, |
||
| 2982 | rated_input_power)) |
||
| 2983 | new_id = cursor.lastrowid |
||
| 2984 | cnx.commit() |
||
| 2985 | cursor.close() |
||
| 2986 | cnx.close() |
||
| 2987 | |||
| 2988 | resp.status = falcon.HTTP_201 |
||
| 2989 | resp.location = '/microgrids/' + str(id_) + '/heatpumps/' + str(new_id) |
||
| 2990 | |||
| 2991 | |||
| 2992 | class MicrogridHeatpumpItem: |
||
| 2993 | def __init__(self): |
||
| 2994 | pass |
||
| 2995 | |||
| 2996 | @staticmethod |
||
| 2997 | def on_options(req, resp, id_, hid): |
||
| 2998 | _ = req |
||
| 2999 | resp.status = falcon.HTTP_200 |
||
| 3000 | _ = id_ |
||
| 3001 | |||
| 3002 | @staticmethod |
||
| 3003 | def on_get(req, resp, id_, hid): |
||
| 3004 | access_control(req) |
||
| 3005 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3006 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3007 | description='API.INVALID_MICROGRID_ID') |
||
| 3008 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3009 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3010 | description='API.INVALID_MICROGRID_HEATPUMP_ID') |
||
| 3011 | |||
| 3012 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3013 | cursor = cnx.cursor() |
||
| 3014 | |||
| 3015 | cursor.execute(" SELECT name " |
||
| 3016 | " FROM tbl_microgrids " |
||
| 3017 | " WHERE id = %s ", (id_,)) |
||
| 3018 | if cursor.fetchone() is None: |
||
| 3019 | cursor.close() |
||
| 3020 | cnx.close() |
||
| 3021 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3022 | description='API.MICROGRID_NOT_FOUND') |
||
| 3023 | |||
| 3024 | # query microgrid dict |
||
| 3025 | query = (" SELECT id, name, uuid " |
||
| 3026 | " FROM tbl_microgrids ") |
||
| 3027 | cursor.execute(query) |
||
| 3028 | rows_microgrids = cursor.fetchall() |
||
| 3029 | |||
| 3030 | microgrid_dict = dict() |
||
| 3031 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 3032 | for row in rows_microgrids: |
||
| 3033 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 3034 | "name": row[1], |
||
| 3035 | "uuid": row[2]} |
||
| 3036 | # query meter dict |
||
| 3037 | query = (" SELECT id, name, uuid " |
||
| 3038 | " FROM tbl_meters ") |
||
| 3039 | cursor.execute(query) |
||
| 3040 | rows_meters = cursor.fetchall() |
||
| 3041 | |||
| 3042 | meter_dict = dict() |
||
| 3043 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3044 | for row in rows_meters: |
||
| 3045 | meter_dict[row[0]] = {"id": row[0], |
||
| 3046 | "name": row[1], |
||
| 3047 | "uuid": row[2]} |
||
| 3048 | # query point dict |
||
| 3049 | query = (" SELECT id, name " |
||
| 3050 | " FROM tbl_points ") |
||
| 3051 | cursor.execute(query) |
||
| 3052 | rows_points = cursor.fetchall() |
||
| 3053 | |||
| 3054 | point_dict = dict() |
||
| 3055 | if rows_points is not None and len(rows_points) > 0: |
||
| 3056 | for row in rows_points: |
||
| 3057 | point_dict[row[0]] = {"id": row[0], |
||
| 3058 | "name": row[1]} |
||
| 3059 | |||
| 3060 | query = (" SELECT id, name, uuid, microgrid_id, " |
||
| 3061 | " power_point_id, electricity_meter_id, heat_meter_id, cooling_meter_id, rated_input_power " |
||
| 3062 | " FROM tbl_microgrids_heatpumps " |
||
| 3063 | " WHERE id = %s ") |
||
| 3064 | cursor.execute(query, (hid,)) |
||
| 3065 | row = cursor.fetchone() |
||
| 3066 | cursor.close() |
||
| 3067 | cnx.close() |
||
| 3068 | |||
| 3069 | if row is None: |
||
| 3070 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3071 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 3072 | else: |
||
| 3073 | meta_result = {"id": row[0], |
||
| 3074 | "name": row[1], |
||
| 3075 | "uuid": row[2], |
||
| 3076 | "microgrid": microgrid_dict.get(row[3], None), |
||
| 3077 | "power_point": point_dict.get(row[4], None), |
||
| 3078 | "electricity_meter": meter_dict.get(row[5], None), |
||
| 3079 | "heat_meter": meter_dict.get(row[6], None), |
||
| 3080 | "cooling_meter": meter_dict.get(row[7], None), |
||
| 3081 | "rated_input_power": row[8]} |
||
| 3082 | |||
| 3083 | resp.text = json.dumps(meta_result) |
||
| 3084 | |||
| 3085 | @staticmethod |
||
| 3086 | @user_logger |
||
| 3087 | def on_delete(req, resp, id_, hid): |
||
| 3088 | admin_control(req) |
||
| 3089 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3090 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3091 | description='API.INVALID_MICROGRID_ID') |
||
| 3092 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3093 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3094 | description='API.INVALID_MICROGRID_HEATPUMP_ID') |
||
| 3095 | |||
| 3096 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3097 | cursor = cnx.cursor() |
||
| 3098 | |||
| 3099 | cursor.execute(" SELECT name " |
||
| 3100 | " FROM tbl_microgrids " |
||
| 3101 | " WHERE id = %s ", (id_,)) |
||
| 3102 | if cursor.fetchone() is None: |
||
| 3103 | cursor.close() |
||
| 3104 | cnx.close() |
||
| 3105 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3106 | description='API.MICROGRID_NOT_FOUND') |
||
| 3107 | |||
| 3108 | cursor.execute(" SELECT name " |
||
| 3109 | " FROM tbl_microgrids_heatpumps " |
||
| 3110 | " WHERE id = %s ", (hid,)) |
||
| 3111 | if cursor.fetchone() is None: |
||
| 3112 | cursor.close() |
||
| 3113 | cnx.close() |
||
| 3114 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3115 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 3116 | |||
| 3117 | cursor.execute(" DELETE FROM tbl_microgrids_heatpumps " |
||
| 3118 | " WHERE id = %s ", (hid,)) |
||
| 3119 | cnx.commit() |
||
| 3120 | |||
| 3121 | cursor.close() |
||
| 3122 | cnx.close() |
||
| 3123 | |||
| 3124 | resp.status = falcon.HTTP_204 |
||
| 3125 | |||
| 3126 | @staticmethod |
||
| 3127 | @user_logger |
||
| 3128 | def on_put(req, resp, id_, hid): |
||
| 3129 | """Handles PUT requests""" |
||
| 3130 | admin_control(req) |
||
| 3131 | try: |
||
| 3132 | raw_json = req.stream.read().decode('utf-8') |
||
| 3133 | except UnicodeDecodeError as ex: |
||
| 3134 | print("Failed to decode request") |
||
| 3135 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3136 | title='API.BAD_REQUEST', |
||
| 3137 | description='API.INVALID_ENCODING') |
||
| 3138 | except Exception as ex: |
||
| 3139 | print(str(ex)) |
||
| 3140 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3141 | title='API.BAD_REQUEST', |
||
| 3142 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3143 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3145 | description='API.INVALID_MICROGRID_ID') |
||
| 3146 | if not hid.isdigit() or int(hid) <= 0: |
||
| 3147 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3148 | description='API.INVALID_MICROGRID_HEATPUMP_ID') |
||
| 3149 | |||
| 3150 | new_values = json.loads(raw_json) |
||
| 3151 | |||
| 3152 | if 'name' not in new_values['data'].keys() or \ |
||
| 3153 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3154 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3156 | description='API.INVALID_MICROGRID_HEATPUMP_NAME') |
||
| 3157 | name = str.strip(new_values['data']['name']) |
||
| 3158 | |||
| 3159 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3160 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3161 | new_values['data']['power_point_id'] <= 0: |
||
| 3162 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3163 | description='API.INVALID_POWER_POINT_ID') |
||
| 3164 | power_point_id = new_values['data']['power_point_id'] |
||
| 3165 | |||
| 3166 | if 'electricity_meter_id' not in new_values['data'].keys() or \ |
||
| 3167 | not isinstance(new_values['data']['electricity_meter_id'], int) or \ |
||
| 3168 | new_values['data']['electricity_meter_id'] <= 0: |
||
| 3169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3170 | description='API.INVALID_ELECTRICITY_METER_ID') |
||
| 3171 | electricity_meter_id = new_values['data']['electricity_meter_id'] |
||
| 3172 | |||
| 3173 | if 'heat_meter_id' not in new_values['data'].keys() or \ |
||
| 3174 | not isinstance(new_values['data']['heat_meter_id'], int) or \ |
||
| 3175 | new_values['data']['heat_meter_id'] <= 0: |
||
| 3176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3177 | description='API.INVALID_HEAT_METER_ID') |
||
| 3178 | heat_meter_id = new_values['data']['heat_meter_id'] |
||
| 3179 | |||
| 3180 | if 'cooling_meter_id' not in new_values['data'].keys() or \ |
||
| 3181 | not isinstance(new_values['data']['cooling_meter_id'], int) or \ |
||
| 3182 | new_values['data']['cooling_meter_id'] <= 0: |
||
| 3183 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3184 | description='API.INVALID_COOLING_METER_ID') |
||
| 3185 | cooling_meter_id = new_values['data']['cooling_meter_id'] |
||
| 3186 | |||
| 3187 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 3188 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 3189 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 3190 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3191 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 3192 | rated_input_power = float(new_values['data']['rated_input_power']) |
||
| 3193 | |||
| 3194 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3195 | cursor = cnx.cursor() |
||
| 3196 | |||
| 3197 | cursor.execute(" SELECT name " |
||
| 3198 | " FROM tbl_microgrids " |
||
| 3199 | " WHERE id = %s ", (id_,)) |
||
| 3200 | if cursor.fetchone() is None: |
||
| 3201 | cursor.close() |
||
| 3202 | cnx.close() |
||
| 3203 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3204 | description='API.MICROGRID_NOT_FOUND') |
||
| 3205 | |||
| 3206 | cursor.execute(" SELECT name " |
||
| 3207 | " FROM tbl_microgrids_heatpumps " |
||
| 3208 | " WHERE id = %s ", (hid,)) |
||
| 3209 | if cursor.fetchone() is None: |
||
| 3210 | cursor.close() |
||
| 3211 | cnx.close() |
||
| 3212 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3213 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 3214 | |||
| 3215 | cursor.execute(" SELECT name " |
||
| 3216 | " FROM tbl_microgrids_heatpumps " |
||
| 3217 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 3218 | (id_, name, hid)) |
||
| 3219 | if cursor.fetchone() is not None: |
||
| 3220 | cursor.close() |
||
| 3221 | cnx.close() |
||
| 3222 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3223 | description='API.MICROGRID_HEATPUMP_NAME_IS_ALREADY_IN_USE') |
||
| 3224 | |||
| 3225 | cursor.execute(" SELECT name " |
||
| 3226 | " FROM tbl_points " |
||
| 3227 | " WHERE id = %s ", |
||
| 3228 | (power_point_id,)) |
||
| 3229 | if cursor.fetchone() is None: |
||
| 3230 | cursor.close() |
||
| 3231 | cnx.close() |
||
| 3232 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3233 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3234 | |||
| 3235 | cursor.execute(" SELECT name " |
||
| 3236 | " FROM tbl_meters " |
||
| 3237 | " WHERE id = %s ", |
||
| 3238 | (electricity_meter_id,)) |
||
| 3239 | if cursor.fetchone() is None: |
||
| 3240 | cursor.close() |
||
| 3241 | cnx.close() |
||
| 3242 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3243 | description='API.ELECTRICITY_METER_NOT_FOUND') |
||
| 3244 | |||
| 3245 | cursor.execute(" SELECT name " |
||
| 3246 | " FROM tbl_meters " |
||
| 3247 | " WHERE id = %s ", |
||
| 3248 | (heat_meter_id,)) |
||
| 3249 | if cursor.fetchone() is None: |
||
| 3250 | cursor.close() |
||
| 3251 | cnx.close() |
||
| 3252 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3253 | description='API.HEAT_METER_NOT_FOUND') |
||
| 3254 | |||
| 3255 | cursor.execute(" SELECT name " |
||
| 3256 | " FROM tbl_meters " |
||
| 3257 | " WHERE id = %s ", |
||
| 3258 | (cooling_meter_id,)) |
||
| 3259 | if cursor.fetchone() is None: |
||
| 3260 | cursor.close() |
||
| 3261 | cnx.close() |
||
| 3262 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3263 | description='API.COOLING_METER_NOT_FOUND') |
||
| 3264 | |||
| 3265 | update_row = (" UPDATE tbl_microgrids_heatpumps " |
||
| 3266 | " SET name = %s, microgrid_id = %s, " |
||
| 3267 | " power_point_id = %s, electricity_meter_id = %s, heat_meter_id = %s, cooling_meter_id = %s, " |
||
| 3268 | " rated_input_power = %s " |
||
| 3269 | " WHERE id = %s ") |
||
| 3270 | cursor.execute(update_row, (name, |
||
| 3271 | id_, |
||
| 3272 | power_point_id, |
||
| 3273 | electricity_meter_id, |
||
| 3274 | heat_meter_id, |
||
| 3275 | heat_meter_id, |
||
| 3276 | rated_input_power, |
||
| 3277 | hid)) |
||
| 3278 | cnx.commit() |
||
| 3279 | |||
| 3280 | cursor.close() |
||
| 3281 | cnx.close() |
||
| 3282 | |||
| 3283 | resp.status = falcon.HTTP_200 |
||
| 3284 | |||
| 3285 | |||
| 3286 | View Code Duplication | class MicrogridLoadCollection: |
|
| 3287 | def __init__(self): |
||
| 3288 | pass |
||
| 3289 | |||
| 3290 | @staticmethod |
||
| 3291 | def on_options(req, resp, id_): |
||
| 3292 | _ = req |
||
| 3293 | resp.status = falcon.HTTP_200 |
||
| 3294 | _ = id_ |
||
| 3295 | |||
| 3296 | @staticmethod |
||
| 3297 | def on_get(req, resp, id_): |
||
| 3298 | access_control(req) |
||
| 3299 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3301 | description='API.INVALID_MICROGRID_ID') |
||
| 3302 | |||
| 3303 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3304 | cursor = cnx.cursor() |
||
| 3305 | |||
| 3306 | cursor.execute(" SELECT name " |
||
| 3307 | " FROM tbl_microgrids " |
||
| 3308 | " WHERE id = %s ", (id_,)) |
||
| 3309 | if cursor.fetchone() is None: |
||
| 3310 | cursor.close() |
||
| 3311 | cnx.close() |
||
| 3312 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3313 | description='API.MICROGRID_NOT_FOUND') |
||
| 3314 | |||
| 3315 | # query meter dict |
||
| 3316 | query = (" SELECT id, name, uuid " |
||
| 3317 | " FROM tbl_meters ") |
||
| 3318 | cursor.execute(query) |
||
| 3319 | rows_meters = cursor.fetchall() |
||
| 3320 | |||
| 3321 | meter_dict = dict() |
||
| 3322 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3323 | for row in rows_meters: |
||
| 3324 | meter_dict[row[0]] = {"id": row[0], |
||
| 3325 | "name": row[1], |
||
| 3326 | "uuid": row[2]} |
||
| 3327 | # query point dict |
||
| 3328 | query = (" SELECT id, name " |
||
| 3329 | " FROM tbl_points ") |
||
| 3330 | cursor.execute(query) |
||
| 3331 | rows_points = cursor.fetchall() |
||
| 3332 | |||
| 3333 | point_dict = dict() |
||
| 3334 | if rows_points is not None and len(rows_points) > 0: |
||
| 3335 | for row in rows_points: |
||
| 3336 | point_dict[row[0]] = {"id": row[0], |
||
| 3337 | "name": row[1]} |
||
| 3338 | |||
| 3339 | query = (" SELECT id, name, uuid, " |
||
| 3340 | " power_point_id, meter_id, rated_input_power " |
||
| 3341 | " FROM tbl_microgrids_loads " |
||
| 3342 | " WHERE microgrid_id = %s " |
||
| 3343 | " ORDER BY name ") |
||
| 3344 | cursor.execute(query, (id_,)) |
||
| 3345 | rows = cursor.fetchall() |
||
| 3346 | |||
| 3347 | result = list() |
||
| 3348 | if rows is not None and len(rows) > 0: |
||
| 3349 | for row in rows: |
||
| 3350 | meta_result = {"id": row[0], |
||
| 3351 | "name": row[1], |
||
| 3352 | "uuid": row[2], |
||
| 3353 | "power_point": point_dict.get(row[3], None), |
||
| 3354 | "meter": meter_dict.get(row[4], None), |
||
| 3355 | "rated_input_power": row[5]} |
||
| 3356 | result.append(meta_result) |
||
| 3357 | |||
| 3358 | resp.text = json.dumps(result) |
||
| 3359 | |||
| 3360 | @staticmethod |
||
| 3361 | @user_logger |
||
| 3362 | def on_post(req, resp, id_): |
||
| 3363 | """Handles POST requests""" |
||
| 3364 | admin_control(req) |
||
| 3365 | try: |
||
| 3366 | raw_json = req.stream.read().decode('utf-8') |
||
| 3367 | except UnicodeDecodeError as ex: |
||
| 3368 | print("Failed to decode request") |
||
| 3369 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3370 | title='API.BAD_REQUEST', |
||
| 3371 | description='API.INVALID_ENCODING') |
||
| 3372 | except Exception as ex: |
||
| 3373 | print(str(ex)) |
||
| 3374 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3375 | title='API.BAD_REQUEST', |
||
| 3376 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3377 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3378 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3379 | description='API.INVALID_MICROGRID_ID') |
||
| 3380 | |||
| 3381 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3382 | cursor = cnx.cursor() |
||
| 3383 | |||
| 3384 | cursor.execute(" SELECT name " |
||
| 3385 | " FROM tbl_microgrids " |
||
| 3386 | " WHERE id = %s ", (id_,)) |
||
| 3387 | if cursor.fetchone() is None: |
||
| 3388 | cursor.close() |
||
| 3389 | cnx.close() |
||
| 3390 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3391 | description='API.MICROGRID_NOT_FOUND') |
||
| 3392 | |||
| 3393 | new_values = json.loads(raw_json) |
||
| 3394 | |||
| 3395 | if 'name' not in new_values['data'].keys() or \ |
||
| 3396 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3397 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3399 | description='API.INVALID_MICROGRID_LOAD_NAME') |
||
| 3400 | name = str.strip(new_values['data']['name']) |
||
| 3401 | |||
| 3402 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3403 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3404 | new_values['data']['power_point_id'] <= 0: |
||
| 3405 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3406 | description='API.INVALID_POWER_POINT_ID') |
||
| 3407 | power_point_id = new_values['data']['power_point_id'] |
||
| 3408 | |||
| 3409 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 3410 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 3411 | new_values['data']['meter_id'] <= 0: |
||
| 3412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3413 | description='API.INVALID_METER_ID') |
||
| 3414 | meter_id = new_values['data']['meter_id'] |
||
| 3415 | |||
| 3416 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 3417 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 3418 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 3419 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3420 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 3421 | rated_input_power = float(new_values['data']['rated_input_power']) |
||
| 3422 | |||
| 3423 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3424 | cursor = cnx.cursor() |
||
| 3425 | |||
| 3426 | cursor.execute(" SELECT name " |
||
| 3427 | " FROM tbl_microgrids " |
||
| 3428 | " WHERE id = %s ", |
||
| 3429 | (id_,)) |
||
| 3430 | if cursor.fetchone() is None: |
||
| 3431 | cursor.close() |
||
| 3432 | cnx.close() |
||
| 3433 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3434 | description='API.MICROGRID_NOT_FOUND') |
||
| 3435 | |||
| 3436 | cursor.execute(" SELECT name " |
||
| 3437 | " FROM tbl_microgrids_loads " |
||
| 3438 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 3439 | (id_, name,)) |
||
| 3440 | if cursor.fetchone() is not None: |
||
| 3441 | cursor.close() |
||
| 3442 | cnx.close() |
||
| 3443 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3444 | description='API.MICROGRID_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 3445 | |||
| 3446 | cursor.execute(" SELECT name " |
||
| 3447 | " FROM tbl_points " |
||
| 3448 | " WHERE id = %s ", |
||
| 3449 | (power_point_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.POWER_POINT_NOT_FOUND') |
||
| 3455 | |||
| 3456 | cursor.execute(" SELECT name " |
||
| 3457 | " FROM tbl_meters " |
||
| 3458 | " WHERE id = %s ", |
||
| 3459 | (meter_id,)) |
||
| 3460 | if cursor.fetchone() is None: |
||
| 3461 | cursor.close() |
||
| 3462 | cnx.close() |
||
| 3463 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3464 | description='API.METER_NOT_FOUND') |
||
| 3465 | |||
| 3466 | add_values = (" INSERT INTO tbl_microgrids_loads " |
||
| 3467 | " (name, uuid, microgrid_id, power_point_id, meter_id, rated_input_power) " |
||
| 3468 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 3469 | cursor.execute(add_values, (name, |
||
| 3470 | str(uuid.uuid4()), |
||
| 3471 | id_, |
||
| 3472 | power_point_id, |
||
| 3473 | meter_id, |
||
| 3474 | rated_input_power)) |
||
| 3475 | new_id = cursor.lastrowid |
||
| 3476 | cnx.commit() |
||
| 3477 | cursor.close() |
||
| 3478 | cnx.close() |
||
| 3479 | |||
| 3480 | resp.status = falcon.HTTP_201 |
||
| 3481 | resp.location = '/microgrids/' + str(id_) + '/loads/' + str(new_id) |
||
| 3482 | |||
| 3483 | |||
| 3484 | View Code Duplication | class MicrogridLoadItem: |
|
| 3485 | def __init__(self): |
||
| 3486 | pass |
||
| 3487 | |||
| 3488 | @staticmethod |
||
| 3489 | def on_options(req, resp, id_, lid): |
||
| 3490 | _ = req |
||
| 3491 | resp.status = falcon.HTTP_200 |
||
| 3492 | _ = id_ |
||
| 3493 | |||
| 3494 | @staticmethod |
||
| 3495 | def on_get(req, resp, id_, lid): |
||
| 3496 | access_control(req) |
||
| 3497 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3498 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3499 | description='API.INVALID_MICROGRID_ID') |
||
| 3500 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3501 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3502 | description='API.INVALID_MICROGRID_LOAD_ID') |
||
| 3503 | |||
| 3504 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3505 | cursor = cnx.cursor() |
||
| 3506 | |||
| 3507 | cursor.execute(" SELECT name " |
||
| 3508 | " FROM tbl_microgrids " |
||
| 3509 | " WHERE id = %s ", (id_,)) |
||
| 3510 | if cursor.fetchone() is None: |
||
| 3511 | cursor.close() |
||
| 3512 | cnx.close() |
||
| 3513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3514 | description='API.MICROGRID_NOT_FOUND') |
||
| 3515 | |||
| 3516 | # query microgrid dict |
||
| 3517 | query = (" SELECT id, name, uuid " |
||
| 3518 | " FROM tbl_microgrids ") |
||
| 3519 | cursor.execute(query) |
||
| 3520 | rows_microgrids = cursor.fetchall() |
||
| 3521 | |||
| 3522 | microgrid_dict = dict() |
||
| 3523 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 3524 | for row in rows_microgrids: |
||
| 3525 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 3526 | "name": row[1], |
||
| 3527 | "uuid": row[2]} |
||
| 3528 | # query meter dict |
||
| 3529 | query = (" SELECT id, name, uuid " |
||
| 3530 | " FROM tbl_meters ") |
||
| 3531 | cursor.execute(query) |
||
| 3532 | rows_meters = cursor.fetchall() |
||
| 3533 | |||
| 3534 | meter_dict = dict() |
||
| 3535 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3536 | for row in rows_meters: |
||
| 3537 | meter_dict[row[0]] = {"id": row[0], |
||
| 3538 | "name": row[1], |
||
| 3539 | "uuid": row[2]} |
||
| 3540 | # query point dict |
||
| 3541 | query = (" SELECT id, name " |
||
| 3542 | " FROM tbl_points ") |
||
| 3543 | cursor.execute(query) |
||
| 3544 | rows_points = cursor.fetchall() |
||
| 3545 | |||
| 3546 | point_dict = dict() |
||
| 3547 | if rows_points is not None and len(rows_points) > 0: |
||
| 3548 | for row in rows_points: |
||
| 3549 | point_dict[row[0]] = {"id": row[0], |
||
| 3550 | "name": row[1]} |
||
| 3551 | |||
| 3552 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_input_power " |
||
| 3553 | " FROM tbl_microgrids_loads " |
||
| 3554 | " WHERE id = %s ") |
||
| 3555 | cursor.execute(query, (lid,)) |
||
| 3556 | row = cursor.fetchone() |
||
| 3557 | cursor.close() |
||
| 3558 | cnx.close() |
||
| 3559 | |||
| 3560 | if row is None: |
||
| 3561 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3562 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 3563 | else: |
||
| 3564 | meta_result = {"id": row[0], |
||
| 3565 | "name": row[1], |
||
| 3566 | "uuid": row[2], |
||
| 3567 | "microgrid": microgrid_dict.get(row[3], None), |
||
| 3568 | "power_point": point_dict.get(row[4], None), |
||
| 3569 | "meter": meter_dict.get(row[5], None), |
||
| 3570 | "rated_input_power": row[6]} |
||
| 3571 | |||
| 3572 | resp.text = json.dumps(meta_result) |
||
| 3573 | |||
| 3574 | @staticmethod |
||
| 3575 | @user_logger |
||
| 3576 | def on_delete(req, resp, id_, lid): |
||
| 3577 | admin_control(req) |
||
| 3578 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3579 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3580 | description='API.INVALID_MICROGRID_ID') |
||
| 3581 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3582 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3583 | description='API.INVALID_MICROGRID_LOAD_ID') |
||
| 3584 | |||
| 3585 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3586 | cursor = cnx.cursor() |
||
| 3587 | |||
| 3588 | cursor.execute(" SELECT name " |
||
| 3589 | " FROM tbl_microgrids " |
||
| 3590 | " WHERE id = %s ", (id_,)) |
||
| 3591 | if cursor.fetchone() is None: |
||
| 3592 | cursor.close() |
||
| 3593 | cnx.close() |
||
| 3594 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3595 | description='API.MICROGRID_NOT_FOUND') |
||
| 3596 | |||
| 3597 | cursor.execute(" SELECT name " |
||
| 3598 | " FROM tbl_microgrids_loads " |
||
| 3599 | " WHERE id = %s ", (lid,)) |
||
| 3600 | if cursor.fetchone() is None: |
||
| 3601 | cursor.close() |
||
| 3602 | cnx.close() |
||
| 3603 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3604 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 3605 | |||
| 3606 | cursor.execute(" DELETE FROM tbl_microgrids_loads " |
||
| 3607 | " WHERE id = %s ", (lid,)) |
||
| 3608 | cnx.commit() |
||
| 3609 | |||
| 3610 | cursor.close() |
||
| 3611 | cnx.close() |
||
| 3612 | |||
| 3613 | resp.status = falcon.HTTP_204 |
||
| 3614 | |||
| 3615 | @staticmethod |
||
| 3616 | @user_logger |
||
| 3617 | def on_put(req, resp, id_, lid): |
||
| 3618 | """Handles PUT requests""" |
||
| 3619 | admin_control(req) |
||
| 3620 | try: |
||
| 3621 | raw_json = req.stream.read().decode('utf-8') |
||
| 3622 | except UnicodeDecodeError as ex: |
||
| 3623 | print("Failed to decode request") |
||
| 3624 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3625 | title='API.BAD_REQUEST', |
||
| 3626 | description='API.INVALID_ENCODING') |
||
| 3627 | except Exception as ex: |
||
| 3628 | print(str(ex)) |
||
| 3629 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3630 | title='API.BAD_REQUEST', |
||
| 3631 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3632 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3633 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3634 | description='API.INVALID_MICROGRID_ID') |
||
| 3635 | if not lid.isdigit() or int(lid) <= 0: |
||
| 3636 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3637 | description='API.INVALID_MICROGRID_LOAD_ID') |
||
| 3638 | |||
| 3639 | new_values = json.loads(raw_json) |
||
| 3640 | |||
| 3641 | if 'name' not in new_values['data'].keys() or \ |
||
| 3642 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3643 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3644 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3645 | description='API.INVALID_MICROGRID_LOAD_NAME') |
||
| 3646 | name = str.strip(new_values['data']['name']) |
||
| 3647 | |||
| 3648 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3649 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3650 | new_values['data']['power_point_id'] <= 0: |
||
| 3651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3652 | description='API.INVALID_POWER_POINT_ID') |
||
| 3653 | power_point_id = new_values['data']['power_point_id'] |
||
| 3654 | |||
| 3655 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 3656 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 3657 | new_values['data']['meter_id'] <= 0: |
||
| 3658 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3659 | description='API.INVALID_METER_ID') |
||
| 3660 | meter_id = new_values['data']['meter_id'] |
||
| 3661 | |||
| 3662 | if 'rated_input_power' not in new_values['data'].keys() or \ |
||
| 3663 | not (isinstance(new_values['data']['rated_input_power'], float) or |
||
| 3664 | isinstance(new_values['data']['rated_input_power'], int)): |
||
| 3665 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3666 | description='API.INVALID_RATED_INPUT_POWER') |
||
| 3667 | rated_input_power = float(new_values['data']['rated_input_power']) |
||
| 3668 | |||
| 3669 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3670 | cursor = cnx.cursor() |
||
| 3671 | |||
| 3672 | cursor.execute(" SELECT name " |
||
| 3673 | " FROM tbl_microgrids " |
||
| 3674 | " WHERE id = %s ", (id_,)) |
||
| 3675 | if cursor.fetchone() is None: |
||
| 3676 | cursor.close() |
||
| 3677 | cnx.close() |
||
| 3678 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3679 | description='API.MICROGRID_NOT_FOUND') |
||
| 3680 | |||
| 3681 | cursor.execute(" SELECT name " |
||
| 3682 | " FROM tbl_microgrids_loads " |
||
| 3683 | " WHERE id = %s ", (lid,)) |
||
| 3684 | if cursor.fetchone() is None: |
||
| 3685 | cursor.close() |
||
| 3686 | cnx.close() |
||
| 3687 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3688 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 3689 | |||
| 3690 | cursor.execute(" SELECT name " |
||
| 3691 | " FROM tbl_microgrids_loads " |
||
| 3692 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 3693 | (id_, name, lid)) |
||
| 3694 | if cursor.fetchone() is not None: |
||
| 3695 | cursor.close() |
||
| 3696 | cnx.close() |
||
| 3697 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3698 | description='API.MICROGRID_LOAD_NAME_IS_ALREADY_IN_USE') |
||
| 3699 | |||
| 3700 | cursor.execute(" SELECT name " |
||
| 3701 | " FROM tbl_points " |
||
| 3702 | " WHERE id = %s ", |
||
| 3703 | (power_point_id,)) |
||
| 3704 | if cursor.fetchone() is None: |
||
| 3705 | cursor.close() |
||
| 3706 | cnx.close() |
||
| 3707 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3708 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3709 | |||
| 3710 | cursor.execute(" SELECT name " |
||
| 3711 | " FROM tbl_meters " |
||
| 3712 | " WHERE id = %s ", |
||
| 3713 | (meter_id,)) |
||
| 3714 | if cursor.fetchone() is None: |
||
| 3715 | cursor.close() |
||
| 3716 | cnx.close() |
||
| 3717 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3718 | description='API.METER_NOT_FOUND') |
||
| 3719 | |||
| 3720 | update_row = (" UPDATE tbl_microgrids_loads " |
||
| 3721 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_input_power = %s " |
||
| 3722 | " WHERE id = %s ") |
||
| 3723 | cursor.execute(update_row, (name, |
||
| 3724 | id_, |
||
| 3725 | power_point_id, |
||
| 3726 | meter_id, |
||
| 3727 | rated_input_power, |
||
| 3728 | lid)) |
||
| 3729 | cnx.commit() |
||
| 3730 | |||
| 3731 | cursor.close() |
||
| 3732 | cnx.close() |
||
| 3733 | |||
| 3734 | resp.status = falcon.HTTP_200 |
||
| 3735 | |||
| 3736 | |||
| 3737 | View Code Duplication | class MicrogridPhotovoltaicCollection: |
|
| 3738 | def __init__(self): |
||
| 3739 | pass |
||
| 3740 | |||
| 3741 | @staticmethod |
||
| 3742 | def on_options(req, resp, id_): |
||
| 3743 | _ = req |
||
| 3744 | resp.status = falcon.HTTP_200 |
||
| 3745 | _ = id_ |
||
| 3746 | |||
| 3747 | @staticmethod |
||
| 3748 | def on_get(req, resp, id_): |
||
| 3749 | access_control(req) |
||
| 3750 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3751 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3752 | description='API.INVALID_MICROGRID_ID') |
||
| 3753 | |||
| 3754 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3755 | cursor = cnx.cursor() |
||
| 3756 | |||
| 3757 | cursor.execute(" SELECT name " |
||
| 3758 | " FROM tbl_microgrids " |
||
| 3759 | " WHERE id = %s ", (id_,)) |
||
| 3760 | if cursor.fetchone() is None: |
||
| 3761 | cursor.close() |
||
| 3762 | cnx.close() |
||
| 3763 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3764 | description='API.MICROGRID_NOT_FOUND') |
||
| 3765 | |||
| 3766 | # query meter dict |
||
| 3767 | query = (" SELECT id, name, uuid " |
||
| 3768 | " FROM tbl_meters ") |
||
| 3769 | cursor.execute(query) |
||
| 3770 | rows_meters = cursor.fetchall() |
||
| 3771 | |||
| 3772 | meter_dict = dict() |
||
| 3773 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3774 | for row in rows_meters: |
||
| 3775 | meter_dict[row[0]] = {"id": row[0], |
||
| 3776 | "name": row[1], |
||
| 3777 | "uuid": row[2]} |
||
| 3778 | # query point dict |
||
| 3779 | query = (" SELECT id, name " |
||
| 3780 | " FROM tbl_points ") |
||
| 3781 | cursor.execute(query) |
||
| 3782 | rows_points = cursor.fetchall() |
||
| 3783 | |||
| 3784 | point_dict = dict() |
||
| 3785 | if rows_points is not None and len(rows_points) > 0: |
||
| 3786 | for row in rows_points: |
||
| 3787 | point_dict[row[0]] = {"id": row[0], |
||
| 3788 | "name": row[1]} |
||
| 3789 | |||
| 3790 | query = (" SELECT id, name, uuid, " |
||
| 3791 | " power_point_id, meter_id, rated_power " |
||
| 3792 | " FROM tbl_microgrids_photovoltaics " |
||
| 3793 | " WHERE microgrid_id = %s " |
||
| 3794 | " ORDER BY name ") |
||
| 3795 | cursor.execute(query, (id_,)) |
||
| 3796 | rows = cursor.fetchall() |
||
| 3797 | |||
| 3798 | result = list() |
||
| 3799 | if rows is not None and len(rows) > 0: |
||
| 3800 | for row in rows: |
||
| 3801 | meta_result = {"id": row[0], |
||
| 3802 | "name": row[1], |
||
| 3803 | "uuid": row[2], |
||
| 3804 | "power_point": point_dict.get(row[3], None), |
||
| 3805 | "meter": meter_dict.get(row[4], None), |
||
| 3806 | "rated_power": row[5], |
||
| 3807 | } |
||
| 3808 | result.append(meta_result) |
||
| 3809 | |||
| 3810 | resp.text = json.dumps(result) |
||
| 3811 | |||
| 3812 | @staticmethod |
||
| 3813 | @user_logger |
||
| 3814 | def on_post(req, resp, id_): |
||
| 3815 | """Handles POST requests""" |
||
| 3816 | admin_control(req) |
||
| 3817 | try: |
||
| 3818 | raw_json = req.stream.read().decode('utf-8') |
||
| 3819 | except UnicodeDecodeError as ex: |
||
| 3820 | print("Failed to decode request") |
||
| 3821 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3822 | title='API.BAD_REQUEST', |
||
| 3823 | description='API.INVALID_ENCODING') |
||
| 3824 | except Exception as ex: |
||
| 3825 | print(str(ex)) |
||
| 3826 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3827 | title='API.BAD_REQUEST', |
||
| 3828 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3829 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3830 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3831 | description='API.INVALID_MICROGRID_ID') |
||
| 3832 | |||
| 3833 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3834 | cursor = cnx.cursor() |
||
| 3835 | |||
| 3836 | cursor.execute(" SELECT name " |
||
| 3837 | " FROM tbl_microgrids " |
||
| 3838 | " WHERE id = %s ", (id_,)) |
||
| 3839 | if cursor.fetchone() is None: |
||
| 3840 | cursor.close() |
||
| 3841 | cnx.close() |
||
| 3842 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3843 | description='API.MICROGRID_NOT_FOUND') |
||
| 3844 | |||
| 3845 | new_values = json.loads(raw_json) |
||
| 3846 | |||
| 3847 | if 'name' not in new_values['data'].keys() or \ |
||
| 3848 | not isinstance(new_values['data']['name'], str) or \ |
||
| 3849 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 3850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3851 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_NAME') |
||
| 3852 | name = str.strip(new_values['data']['name']) |
||
| 3853 | |||
| 3854 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 3855 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 3856 | new_values['data']['power_point_id'] <= 0: |
||
| 3857 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3858 | description='API.INVALID_POWER_POINT_ID') |
||
| 3859 | power_point_id = new_values['data']['power_point_id'] |
||
| 3860 | |||
| 3861 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 3862 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 3863 | new_values['data']['meter_id'] <= 0: |
||
| 3864 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3865 | description='API.INVALID_METER_ID') |
||
| 3866 | meter_id = new_values['data']['meter_id'] |
||
| 3867 | |||
| 3868 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 3869 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 3870 | isinstance(new_values['data']['rated_power'], int)): |
||
| 3871 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3872 | description='API.INVALID_RATED_POWER') |
||
| 3873 | rated_power = float(new_values['data']['rated_power']) |
||
| 3874 | |||
| 3875 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3876 | cursor = cnx.cursor() |
||
| 3877 | |||
| 3878 | cursor.execute(" SELECT name " |
||
| 3879 | " FROM tbl_microgrids " |
||
| 3880 | " WHERE id = %s ", |
||
| 3881 | (id_,)) |
||
| 3882 | if cursor.fetchone() is None: |
||
| 3883 | cursor.close() |
||
| 3884 | cnx.close() |
||
| 3885 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3886 | description='API.MICROGRID_NOT_FOUND') |
||
| 3887 | |||
| 3888 | cursor.execute(" SELECT name " |
||
| 3889 | " FROM tbl_microgrids_photovoltaics " |
||
| 3890 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 3891 | (id_, name,)) |
||
| 3892 | if cursor.fetchone() is not None: |
||
| 3893 | cursor.close() |
||
| 3894 | cnx.close() |
||
| 3895 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3896 | description='API.MICROGRID_PHOTOVOLTAIC_NAME_IS_ALREADY_IN_USE') |
||
| 3897 | |||
| 3898 | cursor.execute(" SELECT name " |
||
| 3899 | " FROM tbl_points " |
||
| 3900 | " WHERE id = %s ", |
||
| 3901 | (power_point_id,)) |
||
| 3902 | if cursor.fetchone() is None: |
||
| 3903 | cursor.close() |
||
| 3904 | cnx.close() |
||
| 3905 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3906 | description='API.POWER_POINT_NOT_FOUND') |
||
| 3907 | |||
| 3908 | cursor.execute(" SELECT name " |
||
| 3909 | " FROM tbl_meters " |
||
| 3910 | " WHERE id = %s ", |
||
| 3911 | (meter_id,)) |
||
| 3912 | if cursor.fetchone() is None: |
||
| 3913 | cursor.close() |
||
| 3914 | cnx.close() |
||
| 3915 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3916 | description='API.METER_NOT_FOUND') |
||
| 3917 | |||
| 3918 | add_values = (" INSERT INTO tbl_microgrids_photovoltaics " |
||
| 3919 | " (name, uuid, microgrid_id, power_point_id, meter_id, rated_power) " |
||
| 3920 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 3921 | cursor.execute(add_values, (name, |
||
| 3922 | str(uuid.uuid4()), |
||
| 3923 | id_, |
||
| 3924 | power_point_id, |
||
| 3925 | meter_id, |
||
| 3926 | rated_power)) |
||
| 3927 | new_id = cursor.lastrowid |
||
| 3928 | cnx.commit() |
||
| 3929 | cursor.close() |
||
| 3930 | cnx.close() |
||
| 3931 | |||
| 3932 | resp.status = falcon.HTTP_201 |
||
| 3933 | resp.location = '/microgrids' + str(id_) + '/photovoltaics/' + str(new_id) |
||
| 3934 | |||
| 3935 | |||
| 3936 | View Code Duplication | class MicrogridPhotovoltaicItem: |
|
| 3937 | def __init__(self): |
||
| 3938 | pass |
||
| 3939 | |||
| 3940 | @staticmethod |
||
| 3941 | def on_options(req, resp, id_, pid): |
||
| 3942 | _ = req |
||
| 3943 | resp.status = falcon.HTTP_200 |
||
| 3944 | _ = id_ |
||
| 3945 | |||
| 3946 | @staticmethod |
||
| 3947 | def on_get(req, resp, id_, pid): |
||
| 3948 | access_control(req) |
||
| 3949 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3950 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3951 | description='API.INVALID_MICROGRID_ID') |
||
| 3952 | if not pid.isdigit() or int(pid) <= 0: |
||
| 3953 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3954 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
||
| 3955 | |||
| 3956 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3957 | cursor = cnx.cursor() |
||
| 3958 | |||
| 3959 | cursor.execute(" SELECT name " |
||
| 3960 | " FROM tbl_microgrids " |
||
| 3961 | " WHERE id = %s ", (id_,)) |
||
| 3962 | if cursor.fetchone() is None: |
||
| 3963 | cursor.close() |
||
| 3964 | cnx.close() |
||
| 3965 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3966 | description='API.MICROGRID_NOT_FOUND') |
||
| 3967 | |||
| 3968 | # query microgrid dict |
||
| 3969 | query = (" SELECT id, name, uuid " |
||
| 3970 | " FROM tbl_microgrids ") |
||
| 3971 | cursor.execute(query) |
||
| 3972 | rows_microgrids = cursor.fetchall() |
||
| 3973 | |||
| 3974 | microgrid_dict = dict() |
||
| 3975 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 3976 | for row in rows_microgrids: |
||
| 3977 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 3978 | "name": row[1], |
||
| 3979 | "uuid": row[2]} |
||
| 3980 | # query meter dict |
||
| 3981 | query = (" SELECT id, name, uuid " |
||
| 3982 | " FROM tbl_meters ") |
||
| 3983 | cursor.execute(query) |
||
| 3984 | rows_meters = cursor.fetchall() |
||
| 3985 | |||
| 3986 | meter_dict = dict() |
||
| 3987 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3988 | for row in rows_meters: |
||
| 3989 | meter_dict[row[0]] = {"id": row[0], |
||
| 3990 | "name": row[1], |
||
| 3991 | "uuid": row[2]} |
||
| 3992 | # query point dict |
||
| 3993 | query = (" SELECT id, name " |
||
| 3994 | " FROM tbl_points ") |
||
| 3995 | cursor.execute(query) |
||
| 3996 | rows_points = cursor.fetchall() |
||
| 3997 | |||
| 3998 | point_dict = dict() |
||
| 3999 | if rows_points is not None and len(rows_points) > 0: |
||
| 4000 | for row in rows_points: |
||
| 4001 | point_dict[row[0]] = {"id": row[0], |
||
| 4002 | "name": row[1]} |
||
| 4003 | |||
| 4004 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, meter_id, rated_power " |
||
| 4005 | " FROM tbl_microgrids_photovoltaics " |
||
| 4006 | " WHERE id = %s ") |
||
| 4007 | cursor.execute(query, (pid,)) |
||
| 4008 | row = cursor.fetchone() |
||
| 4009 | cursor.close() |
||
| 4010 | cnx.close() |
||
| 4011 | |||
| 4012 | if row is None: |
||
| 4013 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4014 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 4015 | else: |
||
| 4016 | meta_result = {"id": row[0], |
||
| 4017 | "name": row[1], |
||
| 4018 | "uuid": row[2], |
||
| 4019 | "microgrid": microgrid_dict.get(row[3], None), |
||
| 4020 | "power_point": point_dict.get(row[4], None), |
||
| 4021 | "meter": meter_dict.get(row[5], None), |
||
| 4022 | "rated_power": row[6]} |
||
| 4023 | |||
| 4024 | resp.text = json.dumps(meta_result) |
||
| 4025 | |||
| 4026 | @staticmethod |
||
| 4027 | @user_logger |
||
| 4028 | def on_delete(req, resp, id_, pid): |
||
| 4029 | admin_control(req) |
||
| 4030 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4031 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4032 | description='API.INVALID_MICROGRID_ID') |
||
| 4033 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4034 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4035 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
||
| 4036 | |||
| 4037 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4038 | cursor = cnx.cursor() |
||
| 4039 | |||
| 4040 | cursor.execute(" SELECT name " |
||
| 4041 | " FROM tbl_microgrids " |
||
| 4042 | " WHERE id = %s ", (id_,)) |
||
| 4043 | if cursor.fetchone() is None: |
||
| 4044 | cursor.close() |
||
| 4045 | cnx.close() |
||
| 4046 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4047 | description='API.MICROGRID_NOT_FOUND') |
||
| 4048 | |||
| 4049 | cursor.execute(" SELECT name " |
||
| 4050 | " FROM tbl_microgrids_photovoltaics " |
||
| 4051 | " WHERE id = %s ", (pid,)) |
||
| 4052 | if cursor.fetchone() is None: |
||
| 4053 | cursor.close() |
||
| 4054 | cnx.close() |
||
| 4055 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4056 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 4057 | |||
| 4058 | cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics " |
||
| 4059 | " WHERE id = %s ", (pid,)) |
||
| 4060 | cnx.commit() |
||
| 4061 | |||
| 4062 | cursor.close() |
||
| 4063 | cnx.close() |
||
| 4064 | |||
| 4065 | resp.status = falcon.HTTP_204 |
||
| 4066 | |||
| 4067 | @staticmethod |
||
| 4068 | @user_logger |
||
| 4069 | def on_put(req, resp, id_, pid): |
||
| 4070 | """Handles PUT requests""" |
||
| 4071 | admin_control(req) |
||
| 4072 | try: |
||
| 4073 | raw_json = req.stream.read().decode('utf-8') |
||
| 4074 | except UnicodeDecodeError as ex: |
||
| 4075 | print("Failed to decode request") |
||
| 4076 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4077 | title='API.BAD_REQUEST', |
||
| 4078 | description='API.INVALID_ENCODING') |
||
| 4079 | except Exception as ex: |
||
| 4080 | print(str(ex)) |
||
| 4081 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4082 | title='API.BAD_REQUEST', |
||
| 4083 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4084 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4085 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4086 | description='API.INVALID_MICROGRID_ID') |
||
| 4087 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4088 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4089 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_ID') |
||
| 4090 | |||
| 4091 | new_values = json.loads(raw_json) |
||
| 4092 | |||
| 4093 | if 'name' not in new_values['data'].keys() or \ |
||
| 4094 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4095 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4096 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4097 | description='API.INVALID_MICROGRID_PHOTOVOLTAIC_NAME') |
||
| 4098 | name = str.strip(new_values['data']['name']) |
||
| 4099 | |||
| 4100 | if 'power_point_id' not in new_values['data'].keys() or \ |
||
| 4101 | not isinstance(new_values['data']['power_point_id'], int) or \ |
||
| 4102 | new_values['data']['power_point_id'] <= 0: |
||
| 4103 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4104 | description='API.INVALID_POWER_POINT_ID') |
||
| 4105 | power_point_id = new_values['data']['power_point_id'] |
||
| 4106 | |||
| 4107 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 4108 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 4109 | new_values['data']['meter_id'] <= 0: |
||
| 4110 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4111 | description='API.INVALID_METER_ID') |
||
| 4112 | meter_id = new_values['data']['meter_id'] |
||
| 4113 | |||
| 4114 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 4115 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 4116 | isinstance(new_values['data']['rated_power'], int)): |
||
| 4117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4118 | description='API.INVALID_RATED_POWER') |
||
| 4119 | rated_power = float(new_values['data']['rated_power']) |
||
| 4120 | |||
| 4121 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4122 | cursor = cnx.cursor() |
||
| 4123 | |||
| 4124 | cursor.execute(" SELECT name " |
||
| 4125 | " FROM tbl_microgrids " |
||
| 4126 | " WHERE id = %s ", (id_,)) |
||
| 4127 | if cursor.fetchone() is None: |
||
| 4128 | cursor.close() |
||
| 4129 | cnx.close() |
||
| 4130 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4131 | description='API.MICROGRID_NOT_FOUND') |
||
| 4132 | |||
| 4133 | cursor.execute(" SELECT name " |
||
| 4134 | " FROM tbl_microgrids_photovoltaics " |
||
| 4135 | " WHERE id = %s ", (pid,)) |
||
| 4136 | if cursor.fetchone() is None: |
||
| 4137 | cursor.close() |
||
| 4138 | cnx.close() |
||
| 4139 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4140 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 4141 | |||
| 4142 | cursor.execute(" SELECT name " |
||
| 4143 | " FROM tbl_microgrids_photovoltaics " |
||
| 4144 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 4145 | (id_, name, pid)) |
||
| 4146 | if cursor.fetchone() is not None: |
||
| 4147 | cursor.close() |
||
| 4148 | cnx.close() |
||
| 4149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4150 | description='API.MICROGRID_PHOTOVOLTAIC_NAME_IS_ALREADY_IN_USE') |
||
| 4151 | |||
| 4152 | cursor.execute(" SELECT name " |
||
| 4153 | " FROM tbl_points " |
||
| 4154 | " WHERE id = %s ", |
||
| 4155 | (power_point_id,)) |
||
| 4156 | if cursor.fetchone() is None: |
||
| 4157 | cursor.close() |
||
| 4158 | cnx.close() |
||
| 4159 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4160 | description='API.POWER_POINT_NOT_FOUND') |
||
| 4161 | |||
| 4162 | cursor.execute(" SELECT name " |
||
| 4163 | " FROM tbl_meters " |
||
| 4164 | " WHERE id = %s ", |
||
| 4165 | (meter_id,)) |
||
| 4166 | if cursor.fetchone() is None: |
||
| 4167 | cursor.close() |
||
| 4168 | cnx.close() |
||
| 4169 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4170 | description='API.METER_NOT_FOUND') |
||
| 4171 | |||
| 4172 | update_row = (" UPDATE tbl_microgrids_photovoltaics " |
||
| 4173 | " SET name = %s, microgrid_id = %s, power_point_id = %s, meter_id = %s, rated_power = %s " |
||
| 4174 | " WHERE id = %s ") |
||
| 4175 | cursor.execute(update_row, (name, |
||
| 4176 | id_, |
||
| 4177 | power_point_id, |
||
| 4178 | meter_id, |
||
| 4179 | rated_power, |
||
| 4180 | pid)) |
||
| 4181 | cnx.commit() |
||
| 4182 | |||
| 4183 | cursor.close() |
||
| 4184 | cnx.close() |
||
| 4185 | |||
| 4186 | resp.status = falcon.HTTP_200 |
||
| 4187 | |||
| 4188 | |||
| 4189 | class MicrogridPowerconversionsystemCollection: |
||
| 4190 | def __init__(self): |
||
| 4191 | pass |
||
| 4192 | |||
| 4193 | @staticmethod |
||
| 4194 | def on_options(req, resp, id_): |
||
| 4195 | _ = req |
||
| 4196 | resp.status = falcon.HTTP_200 |
||
| 4197 | _ = id_ |
||
| 4198 | |||
| 4199 | @staticmethod |
||
| 4200 | def on_get(req, resp, id_): |
||
| 4201 | access_control(req) |
||
| 4202 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4203 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4204 | description='API.INVALID_MICROGRID_ID') |
||
| 4205 | |||
| 4206 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4207 | cursor = cnx.cursor() |
||
| 4208 | |||
| 4209 | cursor.execute(" SELECT name " |
||
| 4210 | " FROM tbl_microgrids " |
||
| 4211 | " WHERE id = %s ", (id_,)) |
||
| 4212 | if cursor.fetchone() is None: |
||
| 4213 | cursor.close() |
||
| 4214 | cnx.close() |
||
| 4215 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4216 | description='API.MICROGRID_NOT_FOUND') |
||
| 4217 | |||
| 4218 | # query point dict |
||
| 4219 | query = (" SELECT id, name " |
||
| 4220 | " FROM tbl_points ") |
||
| 4221 | cursor.execute(query) |
||
| 4222 | rows_points = cursor.fetchall() |
||
| 4223 | |||
| 4224 | point_dict = dict() |
||
| 4225 | if rows_points is not None and len(rows_points) > 0: |
||
| 4226 | for row in rows_points: |
||
| 4227 | point_dict[row[0]] = {"id": row[0], |
||
| 4228 | "name": row[1]} |
||
| 4229 | # query command dict |
||
| 4230 | query = (" SELECT id, name " |
||
| 4231 | " FROM tbl_commands ") |
||
| 4232 | cursor.execute(query) |
||
| 4233 | rows_commands = cursor.fetchall() |
||
| 4234 | |||
| 4235 | command_dict = dict() |
||
| 4236 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 4237 | for row in rows_commands: |
||
| 4238 | command_dict[row[0]] = {"id": row[0], |
||
| 4239 | "name": row[1]} |
||
| 4240 | |||
| 4241 | query = (" SELECT id, name, uuid, run_state_point_id, rated_output_power, " |
||
| 4242 | " today_charge_energy_point_id, today_discharge_energy_point_id, " |
||
| 4243 | " total_charge_energy_point_id, total_discharge_energy_point_id " |
||
| 4244 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4245 | " WHERE microgrid_id = %s " |
||
| 4246 | " ORDER BY name ") |
||
| 4247 | cursor.execute(query, (id_,)) |
||
| 4248 | rows = cursor.fetchall() |
||
| 4249 | |||
| 4250 | result = list() |
||
| 4251 | if rows is not None and len(rows) > 0: |
||
| 4252 | for row in rows: |
||
| 4253 | meta_result = {"id": row[0], |
||
| 4254 | "name": row[1], |
||
| 4255 | "uuid": row[2], |
||
| 4256 | "run_state_point": point_dict.get(row[3]), |
||
| 4257 | "rated_output_power": row[4], |
||
| 4258 | "today_charge_energy_point": point_dict.get(row[5]), |
||
| 4259 | "today_discharge_energy_point": point_dict.get(row[6]), |
||
| 4260 | "total_charge_energy_point": point_dict.get(row[7]), |
||
| 4261 | "total_discharge_energy_point": point_dict.get(row[8])} |
||
| 4262 | result.append(meta_result) |
||
| 4263 | |||
| 4264 | resp.text = json.dumps(result) |
||
| 4265 | |||
| 4266 | @staticmethod |
||
| 4267 | @user_logger |
||
| 4268 | def on_post(req, resp, id_): |
||
| 4269 | """Handles POST requests""" |
||
| 4270 | admin_control(req) |
||
| 4271 | try: |
||
| 4272 | raw_json = req.stream.read().decode('utf-8') |
||
| 4273 | except UnicodeDecodeError as ex: |
||
| 4274 | print("Failed to decode request") |
||
| 4275 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4276 | title='API.BAD_REQUEST', |
||
| 4277 | description='API.INVALID_ENCODING') |
||
| 4278 | except Exception as ex: |
||
| 4279 | print(str(ex)) |
||
| 4280 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4281 | title='API.BAD_REQUEST', |
||
| 4282 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4283 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4284 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4285 | description='API.INVALID_MICROGRID_ID') |
||
| 4286 | |||
| 4287 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4288 | cursor = cnx.cursor() |
||
| 4289 | |||
| 4290 | cursor.execute(" SELECT name " |
||
| 4291 | " FROM tbl_microgrids " |
||
| 4292 | " WHERE id = %s ", (id_,)) |
||
| 4293 | if cursor.fetchone() is None: |
||
| 4294 | cursor.close() |
||
| 4295 | cnx.close() |
||
| 4296 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4297 | description='API.MICROGRID_NOT_FOUND') |
||
| 4298 | |||
| 4299 | new_values = json.loads(raw_json) |
||
| 4300 | |||
| 4301 | if 'name' not in new_values['data'].keys() or \ |
||
| 4302 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4303 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4304 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4305 | description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_NAME') |
||
| 4306 | name = str.strip(new_values['data']['name']) |
||
| 4307 | |||
| 4308 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 4309 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 4310 | new_values['data']['run_state_point_id'] <= 0: |
||
| 4311 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4312 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 4313 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 4314 | |||
| 4315 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 4316 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 4317 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 4318 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4319 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 4320 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 4321 | |||
| 4322 | if 'today_charge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4323 | not isinstance(new_values['data']['today_charge_energy_point_id'], int) or \ |
||
| 4324 | new_values['data']['today_charge_energy_point_id'] <= 0: |
||
| 4325 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4326 | description='API.INVALID_TODAY_CHARGE_ENERGY_POINT_ID') |
||
| 4327 | today_charge_energy_point_id = new_values['data']['today_charge_energy_point_id'] |
||
| 4328 | |||
| 4329 | if 'today_discharge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4330 | not isinstance(new_values['data']['today_discharge_energy_point_id'], int) or \ |
||
| 4331 | new_values['data']['today_discharge_energy_point_id'] <= 0: |
||
| 4332 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4333 | description='API.INVALID_TODAY_DISCHARGE_ENERGY_POINT_ID') |
||
| 4334 | today_discharge_energy_point_id = new_values['data']['today_discharge_energy_point_id'] |
||
| 4335 | |||
| 4336 | if 'total_charge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4337 | not isinstance(new_values['data']['total_charge_energy_point_id'], int) or \ |
||
| 4338 | new_values['data']['total_charge_energy_point_id'] <= 0: |
||
| 4339 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4340 | description='API.INVALID_TOTAL_CHARGE_POINT_ID') |
||
| 4341 | total_charge_energy_point_id = new_values['data']['total_charge_energy_point_id'] |
||
| 4342 | |||
| 4343 | if 'total_discharge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4344 | not isinstance(new_values['data']['total_discharge_energy_point_id'], int) or \ |
||
| 4345 | new_values['data']['total_discharge_energy_point_id'] <= 0: |
||
| 4346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4347 | description='API.INVALID_TOTAL_DISCHARGE_POINT_ID') |
||
| 4348 | total_discharge_energy_point_id = new_values['data']['total_discharge_energy_point_id'] |
||
| 4349 | |||
| 4350 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4351 | cursor = cnx.cursor() |
||
| 4352 | |||
| 4353 | cursor.execute(" SELECT name " |
||
| 4354 | " FROM tbl_microgrids " |
||
| 4355 | " WHERE id = %s ", |
||
| 4356 | (id_,)) |
||
| 4357 | if cursor.fetchone() is None: |
||
| 4358 | cursor.close() |
||
| 4359 | cnx.close() |
||
| 4360 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4361 | description='API.MICROGRID_NOT_FOUND') |
||
| 4362 | |||
| 4363 | cursor.execute(" SELECT name " |
||
| 4364 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4365 | " WHERE microgrid_id = %s AND name = %s ", |
||
| 4366 | (id_, name,)) |
||
| 4367 | if cursor.fetchone() is not None: |
||
| 4368 | cursor.close() |
||
| 4369 | cnx.close() |
||
| 4370 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4371 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NAME_IS_ALREADY_IN_USE') |
||
| 4372 | |||
| 4373 | add_values = (" INSERT INTO tbl_microgrids_power_conversion_systems " |
||
| 4374 | " (name, uuid, microgrid_id, run_state_point_id, rated_output_power, " |
||
| 4375 | " today_charge_energy_point_id, today_discharge_energy_point_id, " |
||
| 4376 | " total_charge_energy_point_id, total_discharge_energy_point_id) " |
||
| 4377 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 4378 | cursor.execute(add_values, (name, |
||
| 4379 | str(uuid.uuid4()), |
||
| 4380 | id_, |
||
| 4381 | run_state_point_id, |
||
| 4382 | rated_output_power, |
||
| 4383 | today_charge_energy_point_id, |
||
| 4384 | today_discharge_energy_point_id, |
||
| 4385 | total_charge_energy_point_id, |
||
| 4386 | total_discharge_energy_point_id |
||
| 4387 | )) |
||
| 4388 | new_id = cursor.lastrowid |
||
| 4389 | cnx.commit() |
||
| 4390 | cursor.close() |
||
| 4391 | cnx.close() |
||
| 4392 | |||
| 4393 | resp.status = falcon.HTTP_201 |
||
| 4394 | resp.location = '/microgridpowerconversionsystems/' + str(new_id) |
||
| 4395 | |||
| 4396 | |||
| 4397 | class MicrogridPowerconversionsystemItem: |
||
| 4398 | def __init__(self): |
||
| 4399 | pass |
||
| 4400 | |||
| 4401 | @staticmethod |
||
| 4402 | def on_options(req, resp, id_, pid): |
||
| 4403 | _ = req |
||
| 4404 | resp.status = falcon.HTTP_200 |
||
| 4405 | _ = id_ |
||
| 4406 | |||
| 4407 | @staticmethod |
||
| 4408 | def on_get(req, resp, id_, pid): |
||
| 4409 | access_control(req) |
||
| 4410 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4411 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4412 | description='API.INVALID_MICROGRID_ID') |
||
| 4413 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4414 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4415 | description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
||
| 4416 | |||
| 4417 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4418 | cursor = cnx.cursor() |
||
| 4419 | |||
| 4420 | cursor.execute(" SELECT name " |
||
| 4421 | " FROM tbl_microgrids " |
||
| 4422 | " WHERE id = %s ", (id_,)) |
||
| 4423 | if cursor.fetchone() is None: |
||
| 4424 | cursor.close() |
||
| 4425 | cnx.close() |
||
| 4426 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4427 | description='API.MICROGRID_NOT_FOUND') |
||
| 4428 | |||
| 4429 | # query microgrid dict |
||
| 4430 | query = (" SELECT id, name, uuid " |
||
| 4431 | " FROM tbl_microgrids ") |
||
| 4432 | cursor.execute(query) |
||
| 4433 | rows_microgrids = cursor.fetchall() |
||
| 4434 | |||
| 4435 | microgrid_dict = dict() |
||
| 4436 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 4437 | for row in rows_microgrids: |
||
| 4438 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 4439 | "name": row[1], |
||
| 4440 | "uuid": row[2]} |
||
| 4441 | # query meter dict |
||
| 4442 | query = (" SELECT id, name, uuid " |
||
| 4443 | " FROM tbl_meters ") |
||
| 4444 | cursor.execute(query) |
||
| 4445 | rows_meters = cursor.fetchall() |
||
| 4446 | |||
| 4447 | meter_dict = dict() |
||
| 4448 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 4449 | for row in rows_meters: |
||
| 4450 | meter_dict[row[0]] = {"id": row[0], |
||
| 4451 | "name": row[1], |
||
| 4452 | "uuid": row[2]} |
||
| 4453 | # query point dict |
||
| 4454 | query = (" SELECT id, name " |
||
| 4455 | " FROM tbl_points ") |
||
| 4456 | cursor.execute(query) |
||
| 4457 | rows_points = cursor.fetchall() |
||
| 4458 | |||
| 4459 | point_dict = dict() |
||
| 4460 | if rows_points is not None and len(rows_points) > 0: |
||
| 4461 | for row in rows_points: |
||
| 4462 | point_dict[row[0]] = {"id": row[0], |
||
| 4463 | "name": row[1]} |
||
| 4464 | |||
| 4465 | # query command dict |
||
| 4466 | query = (" SELECT id, name " |
||
| 4467 | " FROM tbl_commands ") |
||
| 4468 | cursor.execute(query) |
||
| 4469 | rows_commands = cursor.fetchall() |
||
| 4470 | |||
| 4471 | command_dict = dict() |
||
| 4472 | if rows_commands is not None and len(rows_commands) > 0: |
||
| 4473 | for row in rows_commands: |
||
| 4474 | command_dict[row[0]] = {"id": row[0], |
||
| 4475 | "name": row[1]} |
||
| 4476 | |||
| 4477 | query = (" SELECT id, name, uuid, microgrid_id, run_state_point_id, rated_output_power, " |
||
| 4478 | " today_charge_energy_point_id, today_discharge_energy_point_id, " |
||
| 4479 | " total_charge_energy_point_id, total_discharge_energy_point_id " |
||
| 4480 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4481 | " WHERE id = %s ") |
||
| 4482 | cursor.execute(query, (pid,)) |
||
| 4483 | row = cursor.fetchone() |
||
| 4484 | cursor.close() |
||
| 4485 | cnx.close() |
||
| 4486 | |||
| 4487 | if row is None: |
||
| 4488 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4489 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4490 | else: |
||
| 4491 | meta_result = {"id": row[0], |
||
| 4492 | "name": row[1], |
||
| 4493 | "uuid": row[2], |
||
| 4494 | "microgrid": microgrid_dict.get(row[3]), |
||
| 4495 | "run_state_point": point_dict.get(row[4]), |
||
| 4496 | "rated_output_power": row[5], |
||
| 4497 | "today_charge_energy_point": point_dict.get(row[6]), |
||
| 4498 | "today_discharge_energy_point": point_dict.get(row[7]), |
||
| 4499 | "total_charge_energy_point": point_dict.get(row[8]), |
||
| 4500 | "total_discharge_energy_point": point_dict.get(row[9])} |
||
| 4501 | |||
| 4502 | resp.text = json.dumps(meta_result) |
||
| 4503 | |||
| 4504 | @staticmethod |
||
| 4505 | @user_logger |
||
| 4506 | def on_delete(req, resp, id_, pid): |
||
| 4507 | admin_control(req) |
||
| 4508 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4509 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4510 | description='API.INVALID_MICROGRID_ID') |
||
| 4511 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4512 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4513 | description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
||
| 4514 | |||
| 4515 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4516 | cursor = cnx.cursor() |
||
| 4517 | |||
| 4518 | cursor.execute(" SELECT name " |
||
| 4519 | " FROM tbl_microgrids " |
||
| 4520 | " WHERE id = %s ", (id_,)) |
||
| 4521 | if cursor.fetchone() is None: |
||
| 4522 | cursor.close() |
||
| 4523 | cnx.close() |
||
| 4524 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4525 | description='API.MICROGRID_NOT_FOUND') |
||
| 4526 | |||
| 4527 | cursor.execute(" SELECT name " |
||
| 4528 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4529 | " WHERE id = %s ", (pid,)) |
||
| 4530 | if cursor.fetchone() is None: |
||
| 4531 | cursor.close() |
||
| 4532 | cnx.close() |
||
| 4533 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4534 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4535 | |||
| 4536 | cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems " |
||
| 4537 | " WHERE id = %s ", (pid,)) |
||
| 4538 | cnx.commit() |
||
| 4539 | |||
| 4540 | cursor.close() |
||
| 4541 | cnx.close() |
||
| 4542 | |||
| 4543 | resp.status = falcon.HTTP_204 |
||
| 4544 | |||
| 4545 | @staticmethod |
||
| 4546 | @user_logger |
||
| 4547 | def on_put(req, resp, id_, pid): |
||
| 4548 | """Handles PUT requests""" |
||
| 4549 | admin_control(req) |
||
| 4550 | try: |
||
| 4551 | raw_json = req.stream.read().decode('utf-8') |
||
| 4552 | except UnicodeDecodeError as ex: |
||
| 4553 | print("Failed to decode request") |
||
| 4554 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4555 | title='API.BAD_REQUEST', |
||
| 4556 | description='API.INVALID_ENCODING') |
||
| 4557 | except Exception as ex: |
||
| 4558 | print(str(ex)) |
||
| 4559 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4560 | title='API.BAD_REQUEST', |
||
| 4561 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4562 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4564 | description='API.INVALID_MICROGRID_ID') |
||
| 4565 | if not pid.isdigit() or int(pid) <= 0: |
||
| 4566 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4567 | description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_ID') |
||
| 4568 | |||
| 4569 | new_values = json.loads(raw_json) |
||
| 4570 | |||
| 4571 | if 'name' not in new_values['data'].keys() or \ |
||
| 4572 | not isinstance(new_values['data']['name'], str) or \ |
||
| 4573 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 4574 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4575 | description='API.INVALID_MICROGRID_POWER_CONVERSION_SYSTEM_NAME') |
||
| 4576 | name = str.strip(new_values['data']['name']) |
||
| 4577 | |||
| 4578 | if 'run_state_point_id' not in new_values['data'].keys() or \ |
||
| 4579 | not isinstance(new_values['data']['run_state_point_id'], int) or \ |
||
| 4580 | new_values['data']['run_state_point_id'] <= 0: |
||
| 4581 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4582 | description='API.INVALID_RUN_STATE_POINT_ID') |
||
| 4583 | run_state_point_id = new_values['data']['run_state_point_id'] |
||
| 4584 | |||
| 4585 | if 'rated_output_power' not in new_values['data'].keys() or \ |
||
| 4586 | not (isinstance(new_values['data']['rated_output_power'], float) or |
||
| 4587 | isinstance(new_values['data']['rated_output_power'], int)): |
||
| 4588 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4589 | description='API.INVALID_RATED_OUTPUT_POWER') |
||
| 4590 | rated_output_power = float(new_values['data']['rated_output_power']) |
||
| 4591 | |||
| 4592 | if 'today_charge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4593 | not isinstance(new_values['data']['today_charge_energy_point_id'], int) or \ |
||
| 4594 | new_values['data']['today_charge_energy_point_id'] <= 0: |
||
| 4595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4596 | description='API.INVALID_TODAY_CHARGE_ENERGY_POINT_ID') |
||
| 4597 | today_charge_energy_point_id = new_values['data']['today_charge_energy_point_id'] |
||
| 4598 | |||
| 4599 | if 'today_discharge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4600 | not isinstance(new_values['data']['today_discharge_energy_point_id'], int) or \ |
||
| 4601 | new_values['data']['today_discharge_energy_point_id'] <= 0: |
||
| 4602 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4603 | description='API.INVALID_TODAY_DISCHARGE_ENERGY_POINT_ID') |
||
| 4604 | today_discharge_energy_point_id = new_values['data']['today_discharge_energy_point_id'] |
||
| 4605 | |||
| 4606 | if 'total_charge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4607 | not isinstance(new_values['data']['total_charge_energy_point_id'], int) or \ |
||
| 4608 | new_values['data']['total_charge_energy_point_id'] <= 0: |
||
| 4609 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4610 | description='API.INVALID_TOTAL_CHARGE_POINT_ID') |
||
| 4611 | total_charge_energy_point_id = new_values['data']['total_charge_energy_point_id'] |
||
| 4612 | |||
| 4613 | if 'total_discharge_energy_point_id' not in new_values['data'].keys() or \ |
||
| 4614 | not isinstance(new_values['data']['total_discharge_energy_point_id'], int) or \ |
||
| 4615 | new_values['data']['total_discharge_energy_point_id'] <= 0: |
||
| 4616 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4617 | description='API.INVALID_TOTAL_DISCHARGE_POINT_ID') |
||
| 4618 | total_discharge_energy_point_id = new_values['data']['total_discharge_energy_point_id'] |
||
| 4619 | |||
| 4620 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4621 | cursor = cnx.cursor() |
||
| 4622 | |||
| 4623 | cursor.execute(" SELECT name " |
||
| 4624 | " FROM tbl_microgrids " |
||
| 4625 | " WHERE id = %s ", (id_,)) |
||
| 4626 | if cursor.fetchone() is None: |
||
| 4627 | cursor.close() |
||
| 4628 | cnx.close() |
||
| 4629 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4630 | description='API.MICROGRID_NOT_FOUND') |
||
| 4631 | |||
| 4632 | cursor.execute(" SELECT name " |
||
| 4633 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4634 | " WHERE id = %s ", (pid,)) |
||
| 4635 | if cursor.fetchone() is None: |
||
| 4636 | cursor.close() |
||
| 4637 | cnx.close() |
||
| 4638 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4639 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 4640 | |||
| 4641 | cursor.execute(" SELECT name " |
||
| 4642 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 4643 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
||
| 4644 | (id_, name, pid)) |
||
| 4645 | if cursor.fetchone() is not None: |
||
| 4646 | cursor.close() |
||
| 4647 | cnx.close() |
||
| 4648 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4649 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NAME_IS_ALREADY_IN_USE') |
||
| 4650 | |||
| 4651 | update_row = (" UPDATE tbl_microgrids_power_conversion_systems " |
||
| 4652 | " SET name = %s, microgrid_id = %s, run_state_point_id = %s, rated_output_power = %s, " |
||
| 4653 | " today_charge_energy_point_id = %s, today_discharge_energy_point_id = %s, " |
||
| 4654 | " total_charge_energy_point_id = %s, total_discharge_energy_point_id = %s " |
||
| 4655 | " WHERE id = %s ") |
||
| 4656 | cursor.execute(update_row, (name, |
||
| 4657 | id_, |
||
| 4658 | run_state_point_id, |
||
| 4659 | rated_output_power, |
||
| 4660 | today_charge_energy_point_id, |
||
| 4661 | today_discharge_energy_point_id, |
||
| 4662 | total_charge_energy_point_id, |
||
| 4663 | total_discharge_energy_point_id, |
||
| 4664 | pid)) |
||
| 4665 | cnx.commit() |
||
| 4666 | |||
| 4667 | cursor.close() |
||
| 4668 | cnx.close() |
||
| 4669 | |||
| 4670 | resp.status = falcon.HTTP_200 |
||
| 4671 | |||
| 4672 | |||
| 4673 | View Code Duplication | class MicrogridScheduleCollection: |
|
| 4674 | def __init__(self): |
||
| 4675 | pass |
||
| 4676 | |||
| 4677 | @staticmethod |
||
| 4678 | def on_options(req, resp, id_): |
||
| 4679 | _ = req |
||
| 4680 | resp.status = falcon.HTTP_200 |
||
| 4681 | _ = id_ |
||
| 4682 | |||
| 4683 | @staticmethod |
||
| 4684 | def on_get(req, resp, id_): |
||
| 4685 | access_control(req) |
||
| 4686 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4688 | description='API.INVALID_MICROGRID_ID') |
||
| 4689 | |||
| 4690 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4691 | cursor = cnx.cursor() |
||
| 4692 | |||
| 4693 | cursor.execute(" SELECT name " |
||
| 4694 | " FROM tbl_microgrids " |
||
| 4695 | " WHERE id = %s ", (id_,)) |
||
| 4696 | if cursor.fetchone() is None: |
||
| 4697 | cursor.close() |
||
| 4698 | cnx.close() |
||
| 4699 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4700 | description='API.MICROGRID_NOT_FOUND') |
||
| 4701 | |||
| 4702 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 4703 | " FROM tbl_microgrids_schedules " |
||
| 4704 | " WHERE microgrid_id = %s " |
||
| 4705 | " ORDER BY start_time_of_day ") |
||
| 4706 | cursor.execute(query, (id_,)) |
||
| 4707 | rows = cursor.fetchall() |
||
| 4708 | |||
| 4709 | result = list() |
||
| 4710 | if rows is not None and len(rows) > 0: |
||
| 4711 | for row in rows: |
||
| 4712 | meta_result = {"id": row[0], |
||
| 4713 | "start_time_of_day": str(row[1]), |
||
| 4714 | "end_time_of_day": str(row[2]), |
||
| 4715 | "peak_type": row[3], |
||
| 4716 | "power": row[4], |
||
| 4717 | } |
||
| 4718 | result.append(meta_result) |
||
| 4719 | |||
| 4720 | resp.text = json.dumps(result) |
||
| 4721 | |||
| 4722 | @staticmethod |
||
| 4723 | @user_logger |
||
| 4724 | def on_post(req, resp, id_): |
||
| 4725 | """Handles POST requests""" |
||
| 4726 | admin_control(req) |
||
| 4727 | try: |
||
| 4728 | raw_json = req.stream.read().decode('utf-8') |
||
| 4729 | except UnicodeDecodeError as ex: |
||
| 4730 | print("Failed to decode request") |
||
| 4731 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4732 | title='API.BAD_REQUEST', |
||
| 4733 | description='API.INVALID_ENCODING') |
||
| 4734 | except Exception as ex: |
||
| 4735 | print(str(ex)) |
||
| 4736 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4737 | title='API.BAD_REQUEST', |
||
| 4738 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4739 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4740 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4741 | description='API.INVALID_MICROGRID_ID') |
||
| 4742 | |||
| 4743 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4744 | cursor = cnx.cursor() |
||
| 4745 | |||
| 4746 | cursor.execute(" SELECT name " |
||
| 4747 | " FROM tbl_microgrids " |
||
| 4748 | " WHERE id = %s ", (id_,)) |
||
| 4749 | if cursor.fetchone() is None: |
||
| 4750 | cursor.close() |
||
| 4751 | cnx.close() |
||
| 4752 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4753 | description='API.MICROGRID_NOT_FOUND') |
||
| 4754 | |||
| 4755 | new_values = json.loads(raw_json) |
||
| 4756 | |||
| 4757 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4758 | cursor = cnx.cursor() |
||
| 4759 | |||
| 4760 | cursor.execute(" SELECT name " |
||
| 4761 | " FROM tbl_microgrids " |
||
| 4762 | " WHERE id = %s ", |
||
| 4763 | (id_,)) |
||
| 4764 | if cursor.fetchone() is None: |
||
| 4765 | cursor.close() |
||
| 4766 | cnx.close() |
||
| 4767 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4768 | description='API.MICROGRID_NOT_FOUND') |
||
| 4769 | |||
| 4770 | add_schedule = (" INSERT INTO tbl_microgrids_schedules " |
||
| 4771 | " (microgrid_id, start_time_of_day, end_time_of_day, peak_type, power) " |
||
| 4772 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 4773 | cursor.execute(add_schedule, (id_, |
||
| 4774 | new_values['data']['start_time_of_day'], |
||
| 4775 | new_values['data']['end_time_of_day'], |
||
| 4776 | new_values['data']['peak_type'], |
||
| 4777 | new_values['data']['power'])) |
||
| 4778 | new_id = cursor.lastrowid |
||
| 4779 | cnx.commit() |
||
| 4780 | cursor.close() |
||
| 4781 | cnx.close() |
||
| 4782 | resp.status = falcon.HTTP_201 |
||
| 4783 | resp.location = '/energystoragecontainerschedules/' + str(new_id) |
||
| 4784 | |||
| 4785 | |||
| 4786 | View Code Duplication | class MicrogridScheduleItem: |
|
| 4787 | def __init__(self): |
||
| 4788 | pass |
||
| 4789 | |||
| 4790 | @staticmethod |
||
| 4791 | def on_options(req, resp, id_, sid): |
||
| 4792 | _ = req |
||
| 4793 | resp.status = falcon.HTTP_200 |
||
| 4794 | _ = id_ |
||
| 4795 | |||
| 4796 | @staticmethod |
||
| 4797 | def on_get(req, resp, id_, sid): |
||
| 4798 | access_control(req) |
||
| 4799 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4800 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4801 | description='API.INVALID_MICROGRID_ID') |
||
| 4802 | if not sid.isdigit() or int(sid) <= 0: |
||
| 4803 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4804 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
||
| 4805 | |||
| 4806 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4807 | cursor = cnx.cursor() |
||
| 4808 | |||
| 4809 | cursor.execute(" SELECT name " |
||
| 4810 | " FROM tbl_microgrids " |
||
| 4811 | " WHERE id = %s ", (id_,)) |
||
| 4812 | if cursor.fetchone() is None: |
||
| 4813 | cursor.close() |
||
| 4814 | cnx.close() |
||
| 4815 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4816 | description='API.MICROGRID_NOT_FOUND') |
||
| 4817 | |||
| 4818 | query = (" SELECT id, name, uuid " |
||
| 4819 | " FROM tbl_microgrids ") |
||
| 4820 | cursor.execute(query) |
||
| 4821 | rows_energystoragecontainers = cursor.fetchall() |
||
| 4822 | |||
| 4823 | microgrid_dict = dict() |
||
| 4824 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
||
| 4825 | for row in rows_energystoragecontainers: |
||
| 4826 | microgrid_dict[row[0]] = {"id": row[0], |
||
| 4827 | "name": row[1], |
||
| 4828 | "uuid": row[2]} |
||
| 4829 | |||
| 4830 | query = (" SELECT id, start_time_of_day, end_time_of_day, peak_type, power " |
||
| 4831 | " FROM tbl_microgrids_schedules " |
||
| 4832 | " WHERE id = %s ") |
||
| 4833 | cursor.execute(query, (sid,)) |
||
| 4834 | row = cursor.fetchone() |
||
| 4835 | cursor.close() |
||
| 4836 | cnx.close() |
||
| 4837 | |||
| 4838 | if row is None: |
||
| 4839 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4840 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
||
| 4841 | else: |
||
| 4842 | meta_result = {"id": row[0], |
||
| 4843 | "start_time_of_day": str(row[1]), |
||
| 4844 | "end_time_of_day": str(row[2]), |
||
| 4845 | "peak_type": row[3], |
||
| 4846 | "power": row[4]} |
||
| 4847 | |||
| 4848 | resp.text = json.dumps(meta_result) |
||
| 4849 | |||
| 4850 | @staticmethod |
||
| 4851 | @user_logger |
||
| 4852 | def on_delete(req, resp, id_, sid): |
||
| 4853 | admin_control(req) |
||
| 4854 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4855 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4856 | description='API.INVALID_MICROGRID_ID') |
||
| 4857 | if not sid.isdigit() or int(sid) <= 0: |
||
| 4858 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4859 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
||
| 4860 | |||
| 4861 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4862 | cursor = cnx.cursor() |
||
| 4863 | |||
| 4864 | cursor.execute(" SELECT name " |
||
| 4865 | " FROM tbl_microgrids " |
||
| 4866 | " WHERE id = %s ", (id_,)) |
||
| 4867 | if cursor.fetchone() is None: |
||
| 4868 | cursor.close() |
||
| 4869 | cnx.close() |
||
| 4870 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4871 | description='API.MICROGRID_NOT_FOUND') |
||
| 4872 | |||
| 4873 | cursor.execute(" SELECT id " |
||
| 4874 | " FROM tbl_microgrids_schedules " |
||
| 4875 | " WHERE id = %s ", (sid,)) |
||
| 4876 | if cursor.fetchone() is None: |
||
| 4877 | cursor.close() |
||
| 4878 | cnx.close() |
||
| 4879 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4880 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
||
| 4881 | |||
| 4882 | cursor.execute(" DELETE FROM tbl_microgrids_schedules " |
||
| 4883 | " WHERE id = %s ", (sid,)) |
||
| 4884 | cnx.commit() |
||
| 4885 | |||
| 4886 | cursor.close() |
||
| 4887 | cnx.close() |
||
| 4888 | |||
| 4889 | resp.status = falcon.HTTP_204 |
||
| 4890 | |||
| 4891 | @staticmethod |
||
| 4892 | @user_logger |
||
| 4893 | def on_put(req, resp, id_, sid): |
||
| 4894 | """Handles PUT requests""" |
||
| 4895 | admin_control(req) |
||
| 4896 | try: |
||
| 4897 | raw_json = req.stream.read().decode('utf-8') |
||
| 4898 | except UnicodeDecodeError as ex: |
||
| 4899 | print("Failed to decode request") |
||
| 4900 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4901 | title='API.BAD_REQUEST', |
||
| 4902 | description='API.INVALID_ENCODING') |
||
| 4903 | except Exception as ex: |
||
| 4904 | print(str(ex)) |
||
| 4905 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4906 | title='API.BAD_REQUEST', |
||
| 4907 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4908 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4909 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4910 | description='API.INVALID_MICROGRID_ID') |
||
| 4911 | if not sid.isdigit() or int(sid) <= 0: |
||
| 4912 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4913 | description='API.INVALID_MICROGRID_SCHEDULE_ID') |
||
| 4914 | |||
| 4915 | new_values = json.loads(raw_json) |
||
| 4916 | |||
| 4917 | if 'start_time_of_day' not in new_values['data'].keys() or \ |
||
| 4918 | not isinstance(new_values['data']['start_time_of_day'], str) or \ |
||
| 4919 | len(str.strip(new_values['data']['start_time_of_day'])) == 0: |
||
| 4920 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4921 | description='API.INVALID_START_TIME_OF_DAY') |
||
| 4922 | start_time_of_day = str.strip(new_values['data']['start_time_of_day']) |
||
| 4923 | |||
| 4924 | if 'end_time_of_day' not in new_values['data'].keys() or \ |
||
| 4925 | not isinstance(new_values['data']['end_time_of_day'], str) or \ |
||
| 4926 | len(str.strip(new_values['data']['end_time_of_day'])) == 0: |
||
| 4927 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4928 | description='API.INVALID_END_TIME_OF_DAY') |
||
| 4929 | end_time_of_day = str.strip(new_values['data']['end_time_of_day']) |
||
| 4930 | |||
| 4931 | if 'peak_type' not in new_values['data'].keys() or \ |
||
| 4932 | not isinstance(new_values['data']['peak_type'], str) or \ |
||
| 4933 | len(str.strip(new_values['data']['peak_type'])) == 0: |
||
| 4934 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4935 | description='API.INVALID_PEAK_TYPE') |
||
| 4936 | peak_type = str.strip(new_values['data']['peak_type']) |
||
| 4937 | |||
| 4938 | if 'power' not in new_values['data'].keys() or \ |
||
| 4939 | not (isinstance(new_values['data']['power'], float) or |
||
| 4940 | isinstance(new_values['data']['power'], int)): |
||
| 4941 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4942 | description='API.INVALID_POWER') |
||
| 4943 | power = float(new_values['data']['power']) |
||
| 4944 | |||
| 4945 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4946 | cursor = cnx.cursor() |
||
| 4947 | |||
| 4948 | cursor.execute(" SELECT name " |
||
| 4949 | " FROM tbl_microgrids " |
||
| 4950 | " WHERE id = %s ", (id_,)) |
||
| 4951 | if cursor.fetchone() is None: |
||
| 4952 | cursor.close() |
||
| 4953 | cnx.close() |
||
| 4954 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4955 | description='API.MICROGRID_NOT_FOUND') |
||
| 4956 | |||
| 4957 | cursor.execute(" SELECT id " |
||
| 4958 | " FROM tbl_microgrids_schedules " |
||
| 4959 | " WHERE id = %s ", (sid,)) |
||
| 4960 | if cursor.fetchone() is None: |
||
| 4961 | cursor.close() |
||
| 4962 | cnx.close() |
||
| 4963 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4964 | description='API.MICROGRID_SCHEDULE_NOT_FOUND') |
||
| 4965 | |||
| 4966 | update_row = (" UPDATE tbl_microgrids_schedules " |
||
| 4967 | " SET start_time_of_day = %s, end_time_of_day = %s, peak_type = %s, power = %s " |
||
| 4968 | " WHERE id = %s ") |
||
| 4969 | cursor.execute(update_row, (start_time_of_day, |
||
| 4970 | end_time_of_day, |
||
| 4971 | peak_type, |
||
| 4972 | power, |
||
| 4973 | sid)) |
||
| 4974 | cnx.commit() |
||
| 4975 | |||
| 4976 | cursor.close() |
||
| 4977 | cnx.close() |
||
| 4978 | |||
| 4979 | resp.status = falcon.HTTP_200 |
||
| 4980 | |||
| 4981 | |||
| 4982 | View Code Duplication | class MicrogridSensorCollection: |
|
| 4983 | def __init__(self): |
||
| 4984 | pass |
||
| 4985 | |||
| 4986 | @staticmethod |
||
| 4987 | def on_options(req, resp, id_): |
||
| 4988 | _ = req |
||
| 4989 | resp.status = falcon.HTTP_200 |
||
| 4990 | _ = id_ |
||
| 4991 | |||
| 4992 | @staticmethod |
||
| 4993 | def on_get(req, resp, id_): |
||
| 4994 | access_control(req) |
||
| 4995 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4996 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4997 | description='API.INVALID_MICROGRID_ID') |
||
| 4998 | |||
| 4999 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5000 | cursor = cnx.cursor() |
||
| 5001 | |||
| 5002 | cursor.execute(" SELECT name " |
||
| 5003 | " FROM tbl_microgrids " |
||
| 5004 | " WHERE id = %s ", (id_,)) |
||
| 5005 | if cursor.fetchone() is None: |
||
| 5006 | cursor.close() |
||
| 5007 | cnx.close() |
||
| 5008 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5009 | description='API.MICROGRID_NOT_FOUND') |
||
| 5010 | |||
| 5011 | query = (" SELECT s.id, s.name, s.uuid " |
||
| 5012 | " FROM tbl_microgrids m, tbl_microgrids_sensors ms, tbl_sensors s " |
||
| 5013 | " WHERE ms.microgrid_id = m.id AND s.id = ms.sensor_id AND m.id = %s " |
||
| 5014 | " ORDER BY s.id ") |
||
| 5015 | cursor.execute(query, (id_,)) |
||
| 5016 | rows = cursor.fetchall() |
||
| 5017 | |||
| 5018 | result = list() |
||
| 5019 | if rows is not None and len(rows) > 0: |
||
| 5020 | for row in rows: |
||
| 5021 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 5022 | result.append(meta_result) |
||
| 5023 | |||
| 5024 | resp.text = json.dumps(result) |
||
| 5025 | |||
| 5026 | @staticmethod |
||
| 5027 | @user_logger |
||
| 5028 | def on_post(req, resp, id_): |
||
| 5029 | """Handles POST requests""" |
||
| 5030 | admin_control(req) |
||
| 5031 | try: |
||
| 5032 | raw_json = req.stream.read().decode('utf-8') |
||
| 5033 | except UnicodeDecodeError as ex: |
||
| 5034 | print("Failed to decode request") |
||
| 5035 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5036 | title='API.BAD_REQUEST', |
||
| 5037 | description='API.INVALID_ENCODING') |
||
| 5038 | except Exception as ex: |
||
| 5039 | print(str(ex)) |
||
| 5040 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5041 | title='API.BAD_REQUEST', |
||
| 5042 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5043 | |||
| 5044 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5045 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5046 | description='API.INVALID_MICROGRID_ID') |
||
| 5047 | |||
| 5048 | new_values = json.loads(raw_json) |
||
| 5049 | |||
| 5050 | if 'sensor_id' not in new_values['data'].keys() or \ |
||
| 5051 | not isinstance(new_values['data']['sensor_id'], int) or \ |
||
| 5052 | new_values['data']['sensor_id'] <= 0: |
||
| 5053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5054 | description='API.INVALID_SENSOR_ID') |
||
| 5055 | sensor_id = new_values['data']['sensor_id'] |
||
| 5056 | |||
| 5057 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5058 | cursor = cnx.cursor() |
||
| 5059 | |||
| 5060 | cursor.execute(" SELECT name " |
||
| 5061 | " from tbl_microgrids " |
||
| 5062 | " WHERE id = %s ", (id_,)) |
||
| 5063 | if cursor.fetchone() is None: |
||
| 5064 | cursor.close() |
||
| 5065 | cnx.close() |
||
| 5066 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5067 | description='API.MICROGRID_NOT_FOUND') |
||
| 5068 | |||
| 5069 | cursor.execute(" SELECT name " |
||
| 5070 | " FROM tbl_sensors " |
||
| 5071 | " WHERE id = %s ", (sensor_id,)) |
||
| 5072 | if cursor.fetchone() is None: |
||
| 5073 | cursor.close() |
||
| 5074 | cnx.close() |
||
| 5075 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5076 | description='API.SENSOR_NOT_FOUND') |
||
| 5077 | |||
| 5078 | query = (" SELECT id " |
||
| 5079 | " FROM tbl_microgrids_sensors " |
||
| 5080 | " WHERE microgrid_id = %s AND sensor_id = %s") |
||
| 5081 | cursor.execute(query, (id_, sensor_id,)) |
||
| 5082 | if cursor.fetchone() is not None: |
||
| 5083 | cursor.close() |
||
| 5084 | cnx.close() |
||
| 5085 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5086 | description='API.MICROGRID_SENSOR_RELATION_EXISTS') |
||
| 5087 | |||
| 5088 | add_row = (" INSERT INTO tbl_microgrids_sensors (microgrid_id, sensor_id) " |
||
| 5089 | " VALUES (%s, %s) ") |
||
| 5090 | cursor.execute(add_row, (id_, sensor_id,)) |
||
| 5091 | cnx.commit() |
||
| 5092 | cursor.close() |
||
| 5093 | cnx.close() |
||
| 5094 | |||
| 5095 | resp.status = falcon.HTTP_201 |
||
| 5096 | resp.location = '/microgrids/' + str(id_) + '/sensors/' + str(sensor_id) |
||
| 5097 | |||
| 5098 | |||
| 5099 | class MicrogridSensorItem: |
||
| 5100 | def __init__(self): |
||
| 5101 | pass |
||
| 5102 | |||
| 5103 | @staticmethod |
||
| 5104 | def on_options(req, resp, id_, sid): |
||
| 5105 | _ = req |
||
| 5106 | resp.status = falcon.HTTP_200 |
||
| 5107 | _ = id_ |
||
| 5108 | |||
| 5109 | @staticmethod |
||
| 5110 | @user_logger |
||
| 5111 | def on_delete(req, resp, id_, sid): |
||
| 5112 | admin_control(req) |
||
| 5113 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5114 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5115 | description='API.INVALID_MICROGRID_ID') |
||
| 5116 | |||
| 5117 | if not sid.isdigit() or int(sid) <= 0: |
||
| 5118 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5119 | description='API.INVALID_SENSOR_ID') |
||
| 5120 | |||
| 5121 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5122 | cursor = cnx.cursor() |
||
| 5123 | |||
| 5124 | cursor.execute(" SELECT name " |
||
| 5125 | " FROM tbl_microgrids " |
||
| 5126 | " WHERE id = %s ", (id_,)) |
||
| 5127 | if cursor.fetchone() is None: |
||
| 5128 | cursor.close() |
||
| 5129 | cnx.close() |
||
| 5130 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5131 | description='API.MICROGRID_NOT_FOUND') |
||
| 5132 | |||
| 5133 | cursor.execute(" SELECT name " |
||
| 5134 | " FROM tbl_sensors " |
||
| 5135 | " WHERE id = %s ", (sid,)) |
||
| 5136 | if cursor.fetchone() is None: |
||
| 5137 | cursor.close() |
||
| 5138 | cnx.close() |
||
| 5139 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5140 | description='API.SENSOR_NOT_FOUND') |
||
| 5141 | |||
| 5142 | cursor.execute(" SELECT id " |
||
| 5143 | " FROM tbl_microgrids_sensors " |
||
| 5144 | " WHERE microgrid_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 5145 | if cursor.fetchone() is None: |
||
| 5146 | cursor.close() |
||
| 5147 | cnx.close() |
||
| 5148 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5149 | description='API.MICROGRID_SENSOR_RELATION_NOT_FOUND') |
||
| 5150 | |||
| 5151 | cursor.execute(" DELETE FROM tbl_microgrids_sensors " |
||
| 5152 | " WHERE microgrid_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 5153 | cnx.commit() |
||
| 5154 | |||
| 5155 | cursor.close() |
||
| 5156 | cnx.close() |
||
| 5157 | |||
| 5158 | resp.status = falcon.HTTP_204 |
||
| 5159 | |||
| 5160 | |||
| 5161 | View Code Duplication | class MicrogridUserCollection: |
|
| 5162 | def __init__(self): |
||
| 5163 | pass |
||
| 5164 | |||
| 5165 | @staticmethod |
||
| 5166 | def on_options(req, resp, id_): |
||
| 5167 | _ = req |
||
| 5168 | resp.status = falcon.HTTP_200 |
||
| 5169 | _ = id_ |
||
| 5170 | |||
| 5171 | @staticmethod |
||
| 5172 | def on_get(req, resp, id_): |
||
| 5173 | access_control(req) |
||
| 5174 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5176 | description='API.INVALID_MICROGRID_ID') |
||
| 5177 | |||
| 5178 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5179 | cursor = cnx.cursor() |
||
| 5180 | cursor.execute(" SELECT name " |
||
| 5181 | " FROM tbl_microgrids " |
||
| 5182 | " WHERE id = %s ", (id_,)) |
||
| 5183 | if cursor.fetchone() is None: |
||
| 5184 | cursor.close() |
||
| 5185 | cnx.close() |
||
| 5186 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5187 | description='API.MICROGRID_NOT_FOUND') |
||
| 5188 | |||
| 5189 | query = (" SELECT u.id, u.name, u.uuid " |
||
| 5190 | " FROM tbl_microgrids m, tbl_microgrids_users mu, " |
||
| 5191 | + config.myems_user_db['database'] + ".tbl_users u " |
||
| 5192 | " WHERE mu.microgrid_id = m.id AND u.id = mu.user_id AND m.id = %s " |
||
| 5193 | " ORDER BY u.id ") |
||
| 5194 | cursor.execute(query, (id_,)) |
||
| 5195 | rows = cursor.fetchall() |
||
| 5196 | result = list() |
||
| 5197 | if rows is not None and len(rows) > 0: |
||
| 5198 | for row in rows: |
||
| 5199 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 5200 | result.append(meta_result) |
||
| 5201 | |||
| 5202 | cursor.close() |
||
| 5203 | cnx.close() |
||
| 5204 | resp.text = json.dumps(result) |
||
| 5205 | |||
| 5206 | @staticmethod |
||
| 5207 | @user_logger |
||
| 5208 | def on_post(req, resp, id_): |
||
| 5209 | """Handles POST requests""" |
||
| 5210 | admin_control(req) |
||
| 5211 | try: |
||
| 5212 | raw_json = req.stream.read().decode('utf-8') |
||
| 5213 | except UnicodeDecodeError as ex: |
||
| 5214 | print("Failed to decode request") |
||
| 5215 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5216 | title='API.BAD_REQUEST', |
||
| 5217 | description='API.INVALID_ENCODING') |
||
| 5218 | except Exception as ex: |
||
| 5219 | print(str(ex)) |
||
| 5220 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5221 | title='API.BAD_REQUEST', |
||
| 5222 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5223 | |||
| 5224 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5225 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5226 | description='API.INVALID_MICROGRID_ID') |
||
| 5227 | |||
| 5228 | new_values = json.loads(raw_json) |
||
| 5229 | if 'user_id' not in new_values['data'].keys() or \ |
||
| 5230 | not isinstance(new_values['data']['user_id'], int) or \ |
||
| 5231 | new_values['data']['user_id'] <= 0: |
||
| 5232 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5233 | description='API.INVALID_USER_ID') |
||
| 5234 | user_id = new_values['data']['user_id'] |
||
| 5235 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5236 | cursor = cnx.cursor() |
||
| 5237 | cursor.execute(" SELECT name " |
||
| 5238 | " from tbl_microgrids " |
||
| 5239 | " WHERE id = %s ", (id_,)) |
||
| 5240 | if cursor.fetchone() is None: |
||
| 5241 | cursor.close() |
||
| 5242 | cnx.close() |
||
| 5243 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5244 | description='API.MICROGRID_NOT_FOUND') |
||
| 5245 | |||
| 5246 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 5247 | cursor_user = cnx_user.cursor() |
||
| 5248 | cursor_user.execute(" SELECT name" |
||
| 5249 | " FROM tbl_users " |
||
| 5250 | " WHERE id = %s ", (user_id,)) |
||
| 5251 | if cursor_user.fetchone() is None: |
||
| 5252 | cursor_user.close() |
||
| 5253 | cnx_user.close() |
||
| 5254 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5255 | description='API.USER_NOT_FOUND') |
||
| 5256 | query = (" SELECT id " |
||
| 5257 | " FROM tbl_microgrids_users " |
||
| 5258 | " WHERE microgrid_id = %s AND user_id = %s") |
||
| 5259 | cursor.execute(query, (id_, user_id,)) |
||
| 5260 | if cursor.fetchone() is not None: |
||
| 5261 | cursor.close() |
||
| 5262 | cnx.close() |
||
| 5263 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5264 | description='API.MICROGRID_USER_RELATION_EXISTS') |
||
| 5265 | add_row = (" INSERT INTO tbl_microgrids_users (microgrid_id, user_id) " |
||
| 5266 | " VALUES (%s, %s) ") |
||
| 5267 | cursor.execute(add_row, (id_, user_id,)) |
||
| 5268 | cnx.commit() |
||
| 5269 | cursor.close() |
||
| 5270 | cnx.close() |
||
| 5271 | cursor_user.close() |
||
| 5272 | cnx_user.close() |
||
| 5273 | |||
| 5274 | resp.status = falcon.HTTP_201 |
||
| 5275 | resp.location = '/microgrids/' + str(id_) + '/users/' + str(user_id) |
||
| 5276 | |||
| 5277 | |||
| 5278 | View Code Duplication | class MicrogridUserItem: |
|
| 5279 | def __init__(self): |
||
| 5280 | pass |
||
| 5281 | |||
| 5282 | @staticmethod |
||
| 5283 | def on_options(req, resp, id_, uid): |
||
| 5284 | _ = req |
||
| 5285 | resp.status = falcon.HTTP_200 |
||
| 5286 | _ = id_ |
||
| 5287 | |||
| 5288 | @staticmethod |
||
| 5289 | @user_logger |
||
| 5290 | def on_delete(req, resp, id_, uid): |
||
| 5291 | # todo Verify if the user is bound when deleting it |
||
| 5292 | admin_control(req) |
||
| 5293 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5294 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5295 | description='API.INVALID_MICROGRID_ID') |
||
| 5296 | |||
| 5297 | if not uid.isdigit() or int(uid) <= 0: |
||
| 5298 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5299 | description='API.INVALID_USER_ID') |
||
| 5300 | |||
| 5301 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5302 | cursor = cnx.cursor() |
||
| 5303 | cursor.execute(" SELECT name " |
||
| 5304 | " FROM tbl_microgrids " |
||
| 5305 | " WHERE id = %s ", (id_,)) |
||
| 5306 | if cursor.fetchone() is None: |
||
| 5307 | cursor.close() |
||
| 5308 | cnx.close() |
||
| 5309 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5310 | description='API.MICROGRID_NOT_FOUND') |
||
| 5311 | |||
| 5312 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 5313 | cursor_user = cnx_user.cursor() |
||
| 5314 | cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,)) |
||
| 5315 | if cursor_user.fetchone() is None: |
||
| 5316 | cursor_user.close() |
||
| 5317 | cnx_user.close() |
||
| 5318 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5319 | description='API.USER_NOT_FOUND') |
||
| 5320 | |||
| 5321 | cursor.execute(" SELECT id " |
||
| 5322 | " FROM tbl_microgrids_users " |
||
| 5323 | " WHERE microgrid_id = %s AND user_id = %s ", (id_, uid)) |
||
| 5324 | if cursor.fetchone() is None: |
||
| 5325 | cursor.close() |
||
| 5326 | cnx.close() |
||
| 5327 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5328 | description='API.MICROGRID_USER_RELATION_NOT_FOUND') |
||
| 5329 | |||
| 5330 | cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s AND user_id = %s ", (id_, uid)) |
||
| 5331 | cnx.commit() |
||
| 5332 | |||
| 5333 | cursor.close() |
||
| 5334 | cnx.close() |
||
| 5335 | cursor_user.close() |
||
| 5336 | cnx_user.close() |
||
| 5337 | |||
| 5338 | resp.status = falcon.HTTP_204 |
||
| 5339 | |||
| 5340 | |||
| 5341 | class MicrogridExport: |
||
| 5342 | def __init__(self): |
||
| 5343 | pass |
||
| 5344 | |||
| 5345 | @staticmethod |
||
| 5346 | def on_options(req, resp, id_): |
||
| 5347 | _ = req |
||
| 5348 | resp.status = falcon.HTTP_200 |
||
| 5349 | _ = id_ |
||
| 5350 | |||
| 5351 | @staticmethod |
||
| 5352 | def on_get(req, resp, id_): |
||
| 5353 | access_control(req) |
||
| 5354 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5355 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5356 | description='API.INVALID_MICROGRID_ID') |
||
| 5357 | |||
| 5358 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5359 | cursor = cnx.cursor() |
||
| 5360 | |||
| 5361 | query = (" SELECT id, name, uuid " |
||
| 5362 | " FROM tbl_contacts ") |
||
| 5363 | cursor.execute(query) |
||
| 5364 | rows_contacts = cursor.fetchall() |
||
| 5365 | |||
| 5366 | contact_dict = dict() |
||
| 5367 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 5368 | for row in rows_contacts: |
||
| 5369 | contact_dict[row[0]] = {"id": row[0], |
||
| 5370 | "name": row[1], |
||
| 5371 | "uuid": row[2]} |
||
| 5372 | |||
| 5373 | query = (" SELECT id, name, uuid " |
||
| 5374 | " FROM tbl_cost_centers ") |
||
| 5375 | cursor.execute(query) |
||
| 5376 | rows_cost_centers = cursor.fetchall() |
||
| 5377 | |||
| 5378 | cost_center_dict = dict() |
||
| 5379 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 5380 | for row in rows_cost_centers: |
||
| 5381 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 5382 | "name": row[1], |
||
| 5383 | "uuid": row[2]} |
||
| 5384 | |||
| 5385 | query = (" SELECT id, name, uuid, " |
||
| 5386 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 5387 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description " |
||
| 5388 | " FROM tbl_microgrids " |
||
| 5389 | " WHERE id = %s ") |
||
| 5390 | cursor.execute(query, (id_,)) |
||
| 5391 | row = cursor.fetchone() |
||
| 5392 | cursor.close() |
||
| 5393 | cnx.close() |
||
| 5394 | |||
| 5395 | if row is None: |
||
| 5396 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5397 | description='API.MICROGRID_NOT_FOUND') |
||
| 5398 | else: |
||
| 5399 | meta_result = {"id": row[0], |
||
| 5400 | "name": row[1], |
||
| 5401 | "uuid": row[2], |
||
| 5402 | "address": row[3], |
||
| 5403 | "postal_code": row[4], |
||
| 5404 | "latitude": row[5], |
||
| 5405 | "longitude": row[6], |
||
| 5406 | "rated_capacity": row[7], |
||
| 5407 | "rated_power": row[8], |
||
| 5408 | "contact": contact_dict.get(row[9], None), |
||
| 5409 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 5410 | "serial_number": row[11], |
||
| 5411 | "svg_id": row[12], |
||
| 5412 | "is_cost_data_displayed": bool(row[13]), |
||
| 5413 | "phase_of_lifecycle": row[14], |
||
| 5414 | "description": row[15]} |
||
| 5415 | |||
| 5416 | resp.text = json.dumps(meta_result) |
||
| 5417 | |||
| 5418 | |||
| 5419 | class MicrogridImport: |
||
| 5420 | def __init__(self): |
||
| 5421 | pass |
||
| 5422 | |||
| 5423 | @staticmethod |
||
| 5424 | def on_options(req, resp): |
||
| 5425 | _ = req |
||
| 5426 | resp.status = falcon.HTTP_200 |
||
| 5427 | |||
| 5428 | @staticmethod |
||
| 5429 | @user_logger |
||
| 5430 | def on_post(req, resp): |
||
| 5431 | """Handles POST requests""" |
||
| 5432 | admin_control(req) |
||
| 5433 | try: |
||
| 5434 | raw_json = req.stream.read().decode('utf-8') |
||
| 5435 | except UnicodeDecodeError as ex: |
||
| 5436 | print("Failed to decode request") |
||
| 5437 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5438 | title='API.BAD_REQUEST', |
||
| 5439 | description='API.INVALID_ENCODING') |
||
| 5440 | except Exception as ex: |
||
| 5441 | print(str(ex)) |
||
| 5442 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5443 | title='API.BAD_REQUEST', |
||
| 5444 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5445 | |||
| 5446 | new_values = json.loads(raw_json) |
||
| 5447 | |||
| 5448 | if 'name' not in new_values.keys() or \ |
||
| 5449 | not isinstance(new_values['name'], str) or \ |
||
| 5450 | len(str.strip(new_values['name'])) == 0: |
||
| 5451 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5452 | description='API.INVALID_MICROGRID_NAME') |
||
| 5453 | name = str.strip(new_values['name']) |
||
| 5454 | |||
| 5455 | if 'address' not in new_values.keys() or \ |
||
| 5456 | not isinstance(new_values['address'], str) or \ |
||
| 5457 | len(str.strip(new_values['address'])) == 0: |
||
| 5458 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5459 | description='API.INVALID_ADDRESS_VALUE') |
||
| 5460 | address = str.strip(new_values['address']) |
||
| 5461 | |||
| 5462 | if 'postal_code' not in new_values.keys() or \ |
||
| 5463 | not isinstance(new_values['postal_code'], str) or \ |
||
| 5464 | len(str.strip(new_values['postal_code'])) == 0: |
||
| 5465 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5466 | description='API.INVALID_POSTAL_CODE_VALUE') |
||
| 5467 | postal_code = str.strip(new_values['postal_code']) |
||
| 5468 | |||
| 5469 | if 'latitude' not in new_values.keys() or \ |
||
| 5470 | not (isinstance(new_values['latitude'], float) or |
||
| 5471 | isinstance(new_values['latitude'], int)) or \ |
||
| 5472 | new_values['latitude'] < -90.0 or \ |
||
| 5473 | new_values['latitude'] > 90.0: |
||
| 5474 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5475 | description='API.INVALID_LATITUDE_VALUE') |
||
| 5476 | latitude = new_values['latitude'] |
||
| 5477 | |||
| 5478 | if 'longitude' not in new_values.keys() or \ |
||
| 5479 | not (isinstance(new_values['longitude'], float) or |
||
| 5480 | isinstance(new_values['longitude'], int)) or \ |
||
| 5481 | new_values['longitude'] < -180.0 or \ |
||
| 5482 | new_values['longitude'] > 180.0: |
||
| 5483 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5484 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 5485 | longitude = new_values['longitude'] |
||
| 5486 | |||
| 5487 | if 'rated_capacity' not in new_values.keys() or \ |
||
| 5488 | not (isinstance(new_values['rated_capacity'], float) or |
||
| 5489 | isinstance(new_values['rated_capacity'], int)) or \ |
||
| 5490 | new_values['rated_capacity'] <= 0.0: |
||
| 5491 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5492 | description='API.INVALID_RATED_CAPACITY') |
||
| 5493 | rated_capacity = new_values['rated_capacity'] |
||
| 5494 | |||
| 5495 | if 'rated_power' not in new_values.keys() or \ |
||
| 5496 | not (isinstance(new_values['rated_power'], float) or |
||
| 5497 | isinstance(new_values['rated_power'], int)) or \ |
||
| 5498 | new_values['rated_power'] <= 0.0: |
||
| 5499 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5500 | description='API.INVALID_RATED_POWER') |
||
| 5501 | rated_power = new_values['rated_power'] |
||
| 5502 | |||
| 5503 | if 'id' not in new_values['contact'].keys() or \ |
||
| 5504 | not isinstance(new_values['contact']['id'], int) or \ |
||
| 5505 | new_values['contact']['id'] <= 0: |
||
| 5506 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5507 | description='API.INVALID_CONTACT_ID') |
||
| 5508 | contact_id = new_values['contact']['id'] |
||
| 5509 | |||
| 5510 | if 'id' not in new_values['cost_center'].keys() or \ |
||
| 5511 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 5512 | new_values['cost_center']['id'] <= 0: |
||
| 5513 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5514 | description='API.INVALID_COST_CENTER_ID') |
||
| 5515 | cost_center_id = new_values['cost_center']['id'] |
||
| 5516 | |||
| 5517 | if 'serial_number' not in new_values.keys() or \ |
||
| 5518 | not isinstance(new_values['serial_number'], str) or \ |
||
| 5519 | len(str.strip(new_values['serial_number'])) == 0: |
||
| 5520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5521 | description='API.INVALID_SERIAL_NUMBER') |
||
| 5522 | serial_number = str.strip(new_values['serial_number']) |
||
| 5523 | |||
| 5524 | if 'svg_id' not in new_values.keys() or \ |
||
| 5525 | not isinstance(new_values['svg_id'], int) or \ |
||
| 5526 | new_values['svg_id'] <= 0: |
||
| 5527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5528 | description='API.INVALID_SVG_ID') |
||
| 5529 | svg_id = new_values['svg_id'] |
||
| 5530 | |||
| 5531 | if 'is_cost_data_displayed' not in new_values.keys() or \ |
||
| 5532 | not isinstance(new_values['is_cost_data_displayed'], bool): |
||
| 5533 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5534 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
| 5535 | is_cost_data_displayed = new_values['is_cost_data_displayed'] |
||
| 5536 | |||
| 5537 | if 'phase_of_lifecycle' not in new_values.keys() or \ |
||
| 5538 | not isinstance(new_values['phase_of_lifecycle'], str) or \ |
||
| 5539 | len(str.strip(new_values['phase_of_lifecycle'])) == 0: |
||
| 5540 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5541 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
| 5542 | phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle']) |
||
| 5543 | |||
| 5544 | if 'description' in new_values.keys() and \ |
||
| 5545 | new_values['description'] is not None and \ |
||
| 5546 | len(str(new_values['description'])) > 0: |
||
| 5547 | description = str.strip(new_values['description']) |
||
| 5548 | else: |
||
| 5549 | description = None |
||
| 5550 | |||
| 5551 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5552 | cursor = cnx.cursor() |
||
| 5553 | |||
| 5554 | original_name = name |
||
| 5555 | while True: |
||
| 5556 | cursor.execute(" SELECT name " |
||
| 5557 | " FROM tbl_microgrids " |
||
| 5558 | " WHERE name = %s ", (name,)) |
||
| 5559 | if cursor.fetchone() is None: |
||
| 5560 | break |
||
| 5561 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 5562 | if config.utc_offset[0] == '-': |
||
| 5563 | timezone_offset = -timezone_offset |
||
| 5564 | name = (str.strip(original_name) + |
||
| 5565 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 5566 | |||
| 5567 | cursor.execute(" SELECT name " |
||
| 5568 | " FROM tbl_contacts " |
||
| 5569 | " WHERE id = %s ", |
||
| 5570 | (new_values['contact']['id'],)) |
||
| 5571 | row = cursor.fetchone() |
||
| 5572 | if row is None: |
||
| 5573 | cursor.close() |
||
| 5574 | cnx.close() |
||
| 5575 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5576 | description='API.CONTACT_NOT_FOUND') |
||
| 5577 | |||
| 5578 | cursor.execute(" SELECT name " |
||
| 5579 | " FROM tbl_cost_centers " |
||
| 5580 | " WHERE id = %s ", |
||
| 5581 | (new_values['cost_center']['id'],)) |
||
| 5582 | row = cursor.fetchone() |
||
| 5583 | if row is None: |
||
| 5584 | cursor.close() |
||
| 5585 | cnx.close() |
||
| 5586 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5587 | description='API.COST_CENTER_NOT_FOUND') |
||
| 5588 | |||
| 5589 | cursor.execute(" SELECT name " |
||
| 5590 | " FROM tbl_svgs " |
||
| 5591 | " WHERE id = %s ", |
||
| 5592 | (svg_id,)) |
||
| 5593 | row = cursor.fetchone() |
||
| 5594 | if row is None: |
||
| 5595 | cursor.close() |
||
| 5596 | cnx.close() |
||
| 5597 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5598 | description='API.SVG_NOT_FOUND') |
||
| 5599 | |||
| 5600 | add_values = (" INSERT INTO tbl_microgrids " |
||
| 5601 | " (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 5602 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description) " |
||
| 5603 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 5604 | cursor.execute(add_values, (name, |
||
| 5605 | str(uuid.uuid4()), |
||
| 5606 | address, |
||
| 5607 | postal_code, |
||
| 5608 | latitude, |
||
| 5609 | longitude, |
||
| 5610 | rated_capacity, |
||
| 5611 | rated_power, |
||
| 5612 | contact_id, |
||
| 5613 | cost_center_id, |
||
| 5614 | serial_number, |
||
| 5615 | svg_id, |
||
| 5616 | is_cost_data_displayed, |
||
| 5617 | phase_of_lifecycle, |
||
| 5618 | description)) |
||
| 5619 | new_id = cursor.lastrowid |
||
| 5620 | cnx.commit() |
||
| 5621 | cursor.close() |
||
| 5622 | cnx.close() |
||
| 5623 | |||
| 5624 | resp.status = falcon.HTTP_201 |
||
| 5625 | resp.location = '/microgrids/' + str(new_id) |
||
| 5626 | |||
| 5627 | |||
| 5628 | class MicrogridClone: |
||
| 5629 | def __init__(self): |
||
| 5630 | pass |
||
| 5631 | |||
| 5632 | @staticmethod |
||
| 5633 | def on_options(req, resp, id_): |
||
| 5634 | _ = req |
||
| 5635 | resp.status = falcon.HTTP_200 |
||
| 5636 | _ = id_ |
||
| 5637 | |||
| 5638 | @staticmethod |
||
| 5639 | @user_logger |
||
| 5640 | def on_post(req, resp, id_): |
||
| 5641 | admin_control(req) |
||
| 5642 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5643 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5644 | description='API.INVALID_MICROGRID_ID') |
||
| 5645 | |||
| 5646 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5647 | cursor = cnx.cursor() |
||
| 5648 | |||
| 5649 | query = (" SELECT id, name, uuid " |
||
| 5650 | " FROM tbl_contacts ") |
||
| 5651 | cursor.execute(query) |
||
| 5652 | rows_contacts = cursor.fetchall() |
||
| 5653 | |||
| 5654 | contact_dict = dict() |
||
| 5655 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 5656 | for row in rows_contacts: |
||
| 5657 | contact_dict[row[0]] = {"id": row[0], |
||
| 5658 | "name": row[1], |
||
| 5659 | "uuid": row[2]} |
||
| 5660 | |||
| 5661 | query = (" SELECT id, name, uuid " |
||
| 5662 | " FROM tbl_cost_centers ") |
||
| 5663 | cursor.execute(query) |
||
| 5664 | rows_cost_centers = cursor.fetchall() |
||
| 5665 | |||
| 5666 | cost_center_dict = dict() |
||
| 5667 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 5668 | for row in rows_cost_centers: |
||
| 5669 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 5670 | "name": row[1], |
||
| 5671 | "uuid": row[2]} |
||
| 5672 | |||
| 5673 | query = (" SELECT id, name, uuid, " |
||
| 5674 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 5675 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description " |
||
| 5676 | " FROM tbl_microgrids " |
||
| 5677 | " WHERE id = %s ") |
||
| 5678 | cursor.execute(query, (id_,)) |
||
| 5679 | row = cursor.fetchone() |
||
| 5680 | |||
| 5681 | if row is None: |
||
| 5682 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5683 | description='API.MICROGRID_NOT_FOUND') |
||
| 5684 | else: |
||
| 5685 | meta_result = {"id": row[0], |
||
| 5686 | "name": row[1], |
||
| 5687 | "uuid": row[2], |
||
| 5688 | "address": row[3], |
||
| 5689 | "postal_code": row[4], |
||
| 5690 | "latitude": row[5], |
||
| 5691 | "longitude": row[6], |
||
| 5692 | "rated_capacity": row[7], |
||
| 5693 | "rated_power": row[8], |
||
| 5694 | "contact": contact_dict.get(row[9], None), |
||
| 5695 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 5696 | "serial_number": row[11], |
||
| 5697 | "svg_id": row[12], |
||
| 5698 | "is_cost_data_displayed": row[13], |
||
| 5699 | "phase_of_lifecycle": row[14], |
||
| 5700 | "description": row[15]} |
||
| 5701 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 5702 | if config.utc_offset[0] == '-': |
||
| 5703 | timezone_offset = -timezone_offset |
||
| 5704 | new_name = (str.strip(meta_result['name']) + |
||
| 5705 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 5706 | add_values = (" INSERT INTO tbl_microgrids " |
||
| 5707 | " (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
||
| 5708 | " contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, phase_of_lifecycle, description) " |
||
| 5709 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 5710 | cursor.execute(add_values, (new_name, |
||
| 5711 | str(uuid.uuid4()), |
||
| 5712 | meta_result['address'], |
||
| 5713 | meta_result['postal_code'], |
||
| 5714 | meta_result['latitude'], |
||
| 5715 | meta_result['longitude'], |
||
| 5716 | meta_result['rated_capacity'], |
||
| 5717 | meta_result['rated_power'], |
||
| 5718 | meta_result['contact']['id'], |
||
| 5719 | meta_result['cost_center']['id'], |
||
| 5720 | meta_result['serial_number'], |
||
| 5721 | meta_result['svg_id'], |
||
| 5722 | meta_result['is_cost_data_displayed'], |
||
| 5723 | meta_result['phase_of_lifecycle'], |
||
| 5724 | meta_result['description'])) |
||
| 5725 | new_id = cursor.lastrowid |
||
| 5726 | cnx.commit() |
||
| 5727 | cursor.close() |
||
| 5728 | cnx.close() |
||
| 5729 | |||
| 5730 | resp.status = falcon.HTTP_201 |
||
| 5731 | resp.location = '/microgrids/' + str(new_id) |
||
| 5732 | |||
| 5733 | View Code Duplication | class MicrogridDataSourceCollection: |
|
| 5734 | def __init__(self): |
||
| 5735 | pass |
||
| 5736 | |||
| 5737 | @staticmethod |
||
| 5738 | def on_options(req, resp, id_): |
||
| 5739 | _ = req |
||
| 5740 | _ = id_ |
||
| 5741 | resp.status = falcon.HTTP_200 |
||
| 5742 | |||
| 5743 | @staticmethod |
||
| 5744 | def on_get(req, resp, id_): |
||
| 5745 | if 'API-KEY' not in req.headers or \ |
||
| 5746 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5747 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5748 | access_control(req) |
||
| 5749 | else: |
||
| 5750 | api_key_control(req) |
||
| 5751 | |||
| 5752 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5753 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5754 | description='API.INVALID_MICROGRID_ID') |
||
| 5755 | |||
| 5756 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5757 | cursor = cnx.cursor() |
||
| 5758 | |||
| 5759 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
||
| 5760 | if cursor.fetchone() is None: |
||
| 5761 | cursor.close() |
||
| 5762 | cnx.close() |
||
| 5763 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5764 | description='API.MICROGRID_NOT_FOUND') |
||
| 5765 | |||
| 5766 | query = (" SELECT ds.id, ds.name, ds.uuid " |
||
| 5767 | " FROM tbl_microgrids mg, tbl_microgrids_data_sources mgds, tbl_data_sources ds " |
||
| 5768 | " WHERE mgds.microgrid_id = mg.id AND ds.id = mgds.data_source_id AND mg.id = %s " |
||
| 5769 | " ORDER BY ds.id ") |
||
| 5770 | cursor.execute(query, (id_,)) |
||
| 5771 | rows = cursor.fetchall() |
||
| 5772 | |||
| 5773 | result = list() |
||
| 5774 | if rows is not None and len(rows) > 0: |
||
| 5775 | for row in rows: |
||
| 5776 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 5777 | result.append(meta_result) |
||
| 5778 | |||
| 5779 | cursor.close() |
||
| 5780 | cnx.close() |
||
| 5781 | |||
| 5782 | resp.text = json.dumps(result) |
||
| 5783 | |||
| 5784 | @staticmethod |
||
| 5785 | @user_logger |
||
| 5786 | def on_post(req, resp, id_): |
||
| 5787 | admin_control(req) |
||
| 5788 | try: |
||
| 5789 | raw_json = req.stream.read().decode('utf-8') |
||
| 5790 | except UnicodeDecodeError as ex: |
||
| 5791 | print("Failed to decode request") |
||
| 5792 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5793 | title='API.BAD_REQUEST', |
||
| 5794 | description='API.INVALID_ENCODING') |
||
| 5795 | except Exception as ex: |
||
| 5796 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5797 | title='API.BAD_REQUEST', |
||
| 5798 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5799 | |||
| 5800 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5801 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5802 | description='API.INVALID_MICROGRID_ID') |
||
| 5803 | |||
| 5804 | new_values = json.loads(raw_json) |
||
| 5805 | |||
| 5806 | if 'data_source_id' not in new_values['data'].keys() or \ |
||
| 5807 | not isinstance(new_values['data']['data_source_id'], int) or \ |
||
| 5808 | new_values['data']['data_source_id'] <= 0: |
||
| 5809 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5810 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 5811 | |||
| 5812 | data_source_id = new_values['data']['data_source_id'] |
||
| 5813 | |||
| 5814 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5815 | cursor = cnx.cursor() |
||
| 5816 | |||
| 5817 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
||
| 5818 | if cursor.fetchone() is None: |
||
| 5819 | cursor.close() |
||
| 5820 | cnx.close() |
||
| 5821 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5822 | description='API.MICROGRID_NOT_FOUND') |
||
| 5823 | |||
| 5824 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
||
| 5825 | if cursor.fetchone() is None: |
||
| 5826 | cursor.close() |
||
| 5827 | cnx.close() |
||
| 5828 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5829 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 5830 | |||
| 5831 | cursor.execute(" SELECT id " |
||
| 5832 | " FROM tbl_microgrids_data_sources " |
||
| 5833 | " WHERE microgrid_id = %s AND data_source_id = %s", (id_, data_source_id)) |
||
| 5834 | if cursor.fetchone() is not None: |
||
| 5835 | cursor.close() |
||
| 5836 | cnx.close() |
||
| 5837 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5838 | description='API.MICROGRID_DATA_SOURCE_RELATION_EXISTS') |
||
| 5839 | |||
| 5840 | cursor.execute(" INSERT INTO tbl_microgrids_data_sources (microgrid_id, data_source_id) " |
||
| 5841 | " VALUES (%s, %s) ", (id_, data_source_id)) |
||
| 5842 | cnx.commit() |
||
| 5843 | cursor.close() |
||
| 5844 | cnx.close() |
||
| 5845 | |||
| 5846 | resp.status = falcon.HTTP_201 |
||
| 5847 | resp.location = '/microgrids/' + str(id_) + '/datasources/' + str(data_source_id) |
||
| 5848 | |||
| 5849 | class MicrogridDataSourceItem: |
||
| 5850 | def __init__(self): |
||
| 5851 | pass |
||
| 5852 | |||
| 5853 | @staticmethod |
||
| 5854 | def on_options(req, resp, id_, dsid): |
||
| 5855 | _ = req |
||
| 5856 | _ = id_ |
||
| 5857 | _ = dsid |
||
| 5858 | resp.status = falcon.HTTP_200 |
||
| 5859 | |||
| 5860 | @staticmethod |
||
| 5861 | @user_logger |
||
| 5862 | def on_delete(req, resp, id_, dsid): |
||
| 5863 | admin_control(req) |
||
| 5864 | |||
| 5865 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5866 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5867 | description='API.INVALID_MICROGRID_ID') |
||
| 5868 | |||
| 5869 | if not dsid.isdigit() or int(dsid) <= 0: |
||
| 5870 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5871 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 5872 | |||
| 5873 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5874 | cursor = cnx.cursor() |
||
| 5875 | |||
| 5876 | cursor.execute(" SELECT name FROM tbl_microgrids WHERE id = %s ", (id_,)) |
||
| 5877 | if cursor.fetchone() is None: |
||
| 5878 | cursor.close() |
||
| 5879 | cnx.close() |
||
| 5880 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5881 | description='API.MICROGRID_NOT_FOUND') |
||
| 5882 | |||
| 5883 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (dsid,)) |
||
| 5884 | if cursor.fetchone() is None: |
||
| 5885 | cursor.close() |
||
| 5886 | cnx.close() |
||
| 5887 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5888 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 5889 | |||
| 5890 | cursor.execute(" SELECT id " |
||
| 5891 | " FROM tbl_microgrids_data_sources " |
||
| 5892 | " WHERE microgrid_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 5893 | if cursor.fetchone() is None: |
||
| 5894 | cursor.close() |
||
| 5895 | cnx.close() |
||
| 5896 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5897 | description='API.MICROGRID_DATA_SOURCE_RELATION_NOT_FOUND') |
||
| 5898 | |||
| 5899 | cursor.execute(" DELETE FROM tbl_microgrids_data_sources " |
||
| 5900 | " WHERE microgrid_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 5901 | cnx.commit() |
||
| 5902 | cursor.close() |
||
| 5903 | cnx.close() |
||
| 5904 | |||
| 5905 | resp.status = falcon.HTTP_204 |
||
| 5906 | |||
| 5907 | View Code Duplication | class MicrogridDataSourcePointCollection: |
|
| 5908 | def __init__(self): |
||
| 5909 | pass |
||
| 5910 | |||
| 5911 | @staticmethod |
||
| 5912 | def on_options(req, resp, id_): |
||
| 5913 | _ = req |
||
| 5914 | resp.status = falcon.HTTP_200 |
||
| 5915 | _ = id_ |
||
| 5916 | |||
| 5917 | @staticmethod |
||
| 5918 | def on_get(req, resp, id_): |
||
| 5919 | if 'API-KEY' not in req.headers or \ |
||
| 5920 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5921 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5922 | access_control(req) |
||
| 5923 | else: |
||
| 5924 | api_key_control(req) |
||
| 5925 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5926 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5927 | description='API.INVALID_MICROGRID_ID') |
||
| 5928 | |||
| 5929 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5930 | cursor = cnx.cursor() |
||
| 5931 | |||
| 5932 | query = (" SELECT p.id, p.name " |
||
| 5933 | " FROM tbl_points p, tbl_microgrids_data_sources mds, tbl_data_sources ds " |
||
| 5934 | " WHERE mds.microgrid_id = %s " |
||
| 5935 | " AND mds.data_source_id = ds.id " |
||
| 5936 | " AND p.data_source_id = ds.id " |
||
| 5937 | " ORDER BY p.id ") |
||
| 5938 | cursor.execute(query, (id_,)) |
||
| 5939 | rows = cursor.fetchall() |
||
| 5940 | |||
| 5941 | result = list() |
||
| 5942 | if rows is not None and len(rows) > 0: |
||
| 5943 | for row in rows: |
||
| 5944 | meta_result = {"id": row[0], "name": row[1]} |
||
| 5945 | result.append(meta_result) |
||
| 5946 | |||
| 5947 | cursor.close() |
||
| 5948 | cnx.close() |
||
| 5949 | |||
| 5950 | resp.text = json.dumps(result) |
||
| 5951 | |||
| 5952 | View Code Duplication | class MicrogridBatteryPointCollection: |
|
| 5953 | def __init__(self): |
||
| 5954 | pass |
||
| 5955 | |||
| 5956 | @staticmethod |
||
| 5957 | def on_options(req, resp, id_, bid): |
||
| 5958 | _ = req |
||
| 5959 | resp.status = falcon.HTTP_200 |
||
| 5960 | _ = id_ |
||
| 5961 | _ = bid |
||
| 5962 | |||
| 5963 | @staticmethod |
||
| 5964 | def on_get(req, resp, id_, bid): |
||
| 5965 | if 'API-KEY' not in req.headers or \ |
||
| 5966 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5967 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5968 | access_control(req) |
||
| 5969 | else: |
||
| 5970 | api_key_control(req) |
||
| 5971 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5972 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5973 | description='API.INVALID_MICROGRID_ID') |
||
| 5974 | if not bid.isdigit() or int(bid) <= 0: |
||
| 5975 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5976 | description='API.INVALID_BATTERY_ID') |
||
| 5977 | |||
| 5978 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5979 | cursor = cnx.cursor() |
||
| 5980 | |||
| 5981 | cursor.execute(" SELECT name " |
||
| 5982 | " FROM tbl_microgrids_batteries " |
||
| 5983 | " WHERE microgrid_id = %s AND id = %s ", (id_, bid,)) |
||
| 5984 | if cursor.fetchone() is None: |
||
| 5985 | cursor.close() |
||
| 5986 | cnx.close() |
||
| 5987 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5988 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 5989 | |||
| 5990 | query = (" SELECT p.id, p.name, " |
||
| 5991 | " ds.id, ds.name, ds.uuid, " |
||
| 5992 | " p.address " |
||
| 5993 | " FROM tbl_points p, tbl_microgrids_bmses_points mp, tbl_data_sources ds " |
||
| 5994 | " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 5995 | " ORDER BY p.name ") |
||
| 5996 | cursor.execute(query, (bid,)) |
||
| 5997 | rows = cursor.fetchall() |
||
| 5998 | |||
| 5999 | result = list() |
||
| 6000 | if rows is not None and len(rows) > 0: |
||
| 6001 | for row in rows: |
||
| 6002 | meta_result = {"id": row[0], "name": row[1], |
||
| 6003 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6004 | "address": row[5]} |
||
| 6005 | result.append(meta_result) |
||
| 6006 | |||
| 6007 | resp.text = json.dumps(result) |
||
| 6008 | |||
| 6009 | @staticmethod |
||
| 6010 | @user_logger |
||
| 6011 | def on_post(req, resp, id_, bid): |
||
| 6012 | admin_control(req) |
||
| 6013 | try: |
||
| 6014 | raw_json = req.stream.read().decode('utf-8') |
||
| 6015 | except UnicodeDecodeError as ex: |
||
| 6016 | print("Failed to decode request") |
||
| 6017 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6018 | title='API.BAD_REQUEST', |
||
| 6019 | description='API.INVALID_ENCODING') |
||
| 6020 | except Exception as ex: |
||
| 6021 | print(str(ex)) |
||
| 6022 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6023 | title='API.BAD_REQUEST', |
||
| 6024 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6025 | |||
| 6026 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6027 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6028 | description='API.INVALID_MICROGRID_ID') |
||
| 6029 | if not bid.isdigit() or int(bid) <= 0: |
||
| 6030 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6031 | description='API.INVALID_BATTERY_ID') |
||
| 6032 | |||
| 6033 | new_values = json.loads(raw_json) |
||
| 6034 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6035 | cursor = cnx.cursor() |
||
| 6036 | |||
| 6037 | cursor.execute(" SELECT name " |
||
| 6038 | " FROM tbl_microgrids_batteries " |
||
| 6039 | " WHERE microgrid_id = %s AND id = %s ", (id_, bid,)) |
||
| 6040 | if cursor.fetchone() is None: |
||
| 6041 | cursor.close() |
||
| 6042 | cnx.close() |
||
| 6043 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6044 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 6045 | |||
| 6046 | cursor.execute(" SELECT name, object_type " |
||
| 6047 | " FROM tbl_points " |
||
| 6048 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 6049 | row = cursor.fetchone() |
||
| 6050 | if row is None: |
||
| 6051 | cursor.close() |
||
| 6052 | cnx.close() |
||
| 6053 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6054 | description='API.POINT_NOT_FOUND') |
||
| 6055 | |||
| 6056 | query = (" SELECT id " |
||
| 6057 | " FROM tbl_microgrids_bmses_points " |
||
| 6058 | " WHERE bms_id = %s AND point_id = %s") |
||
| 6059 | cursor.execute(query, (bid, new_values['data']['point_id'],)) |
||
| 6060 | if cursor.fetchone() is not None: |
||
| 6061 | cursor.close() |
||
| 6062 | cnx.close() |
||
| 6063 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 6064 | description='API.BATTERY_POINT_RELATION_EXISTS') |
||
| 6065 | |||
| 6066 | add_row = (" INSERT INTO tbl_microgrids_bmses_points (bms_id, point_id) " |
||
| 6067 | " VALUES (%s, %s) ") |
||
| 6068 | cursor.execute(add_row, (bid, new_values['data']['point_id'],)) |
||
| 6069 | cnx.commit() |
||
| 6070 | cursor.close() |
||
| 6071 | cnx.close() |
||
| 6072 | |||
| 6073 | resp.status = falcon.HTTP_201 |
||
| 6074 | resp.location = '/microgrids/' + str(id_) + '/batteries/' + str(bid) + '/points/' + \ |
||
| 6075 | str(new_values['data']['point_id']) |
||
| 6076 | |||
| 6077 | |||
| 6078 | View Code Duplication | class MicrogridBatteryPointItem: |
|
| 6079 | def __init__(self): |
||
| 6080 | pass |
||
| 6081 | |||
| 6082 | @staticmethod |
||
| 6083 | def on_options(req, resp, id_, bid, pid): |
||
| 6084 | _ = req |
||
| 6085 | resp.status = falcon.HTTP_200 |
||
| 6086 | _ = id_ |
||
| 6087 | _ = bid |
||
| 6088 | _ = pid |
||
| 6089 | |||
| 6090 | @staticmethod |
||
| 6091 | @user_logger |
||
| 6092 | def on_delete(req, resp, id_, bid, pid): |
||
| 6093 | admin_control(req) |
||
| 6094 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6095 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6096 | description='API.INVALID_MICROGRID_ID') |
||
| 6097 | if not bid.isdigit() or int(bid) <= 0: |
||
| 6098 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6099 | description='API.INVALID_BATTERY_ID') |
||
| 6100 | if not pid.isdigit() or int(pid) <= 0: |
||
| 6101 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6102 | description='API.INVALID_POINT_ID') |
||
| 6103 | |||
| 6104 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6105 | cursor = cnx.cursor() |
||
| 6106 | |||
| 6107 | cursor.execute(" SELECT name " |
||
| 6108 | " FROM tbl_microgrids_batteries " |
||
| 6109 | " WHERE microgrid_id = %s AND id = %s ", (id_, bid,)) |
||
| 6110 | if cursor.fetchone() is None: |
||
| 6111 | cursor.close() |
||
| 6112 | cnx.close() |
||
| 6113 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6114 | description='API.MICROGRID_BATTERY_NOT_FOUND') |
||
| 6115 | |||
| 6116 | cursor.execute(" SELECT name " |
||
| 6117 | " FROM tbl_points " |
||
| 6118 | " WHERE id = %s ", (pid,)) |
||
| 6119 | if cursor.fetchone() is None: |
||
| 6120 | cursor.close() |
||
| 6121 | cnx.close() |
||
| 6122 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6123 | description='API.POINT_NOT_FOUND') |
||
| 6124 | |||
| 6125 | cursor.execute(" SELECT id " |
||
| 6126 | " FROM tbl_microgrids_bmses_points " |
||
| 6127 | " WHERE bms_id = %s AND point_id = %s ", (bid, pid)) |
||
| 6128 | if cursor.fetchone() is None: |
||
| 6129 | cursor.close() |
||
| 6130 | cnx.close() |
||
| 6131 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6132 | description='API.BATTERY_POINT_RELATION_NOT_FOUND') |
||
| 6133 | |||
| 6134 | cursor.execute(" DELETE FROM tbl_microgrids_bmses_points " |
||
| 6135 | " WHERE bms_id = %s AND point_id = %s ", (bid, pid)) |
||
| 6136 | cnx.commit() |
||
| 6137 | |||
| 6138 | cursor.close() |
||
| 6139 | cnx.close() |
||
| 6140 | |||
| 6141 | resp.status = falcon.HTTP_204 |
||
| 6142 | |||
| 6143 | View Code Duplication | class MicrogridEVChargerPointCollection: |
|
| 6144 | def __init__(self): |
||
| 6145 | pass |
||
| 6146 | |||
| 6147 | @staticmethod |
||
| 6148 | def on_options(req, resp, id_, eid): |
||
| 6149 | _ = req |
||
| 6150 | resp.status = falcon.HTTP_200 |
||
| 6151 | _ = id_ |
||
| 6152 | _ = eid |
||
| 6153 | |||
| 6154 | @staticmethod |
||
| 6155 | def on_get(req, resp, id_, eid): |
||
| 6156 | if 'API-KEY' not in req.headers or \ |
||
| 6157 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 6158 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 6159 | access_control(req) |
||
| 6160 | else: |
||
| 6161 | api_key_control(req) |
||
| 6162 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6163 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6164 | description='API.INVALID_MICROGRID_ID') |
||
| 6165 | if not eid.isdigit() or int(eid) <= 0: |
||
| 6166 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6167 | description='API.INVALID_EVCHARGER_ID') |
||
| 6168 | |||
| 6169 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6170 | cursor = cnx.cursor() |
||
| 6171 | |||
| 6172 | cursor.execute(" SELECT name " |
||
| 6173 | " FROM tbl_microgrids_evchargers " |
||
| 6174 | " WHERE microgrid_id = %s AND id = %s ", (id_, eid,)) |
||
| 6175 | if cursor.fetchone() is None: |
||
| 6176 | cursor.close() |
||
| 6177 | cnx.close() |
||
| 6178 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6179 | description='API.MICROGRID_EV_CHARGER_NOT_FOUND') |
||
| 6180 | |||
| 6181 | query = (" SELECT p.id, p.name, " |
||
| 6182 | " ds.id, ds.name, ds.uuid, " |
||
| 6183 | " p.address " |
||
| 6184 | " FROM tbl_points p, tbl_microgrids_evchargers_points mp, tbl_data_sources ds " |
||
| 6185 | " WHERE mp.evcharger_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 6186 | " ORDER BY p.name ") |
||
| 6187 | cursor.execute(query, (eid,)) |
||
| 6188 | rows = cursor.fetchall() |
||
| 6189 | |||
| 6190 | result = list() |
||
| 6191 | if rows is not None and len(rows) > 0: |
||
| 6192 | for row in rows: |
||
| 6193 | meta_result = {"id": row[0], "name": row[1], |
||
| 6194 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6195 | "address": row[5]} |
||
| 6196 | result.append(meta_result) |
||
| 6197 | |||
| 6198 | resp.text = json.dumps(result) |
||
| 6199 | |||
| 6200 | @staticmethod |
||
| 6201 | @user_logger |
||
| 6202 | def on_post(req, resp, id_, eid): |
||
| 6203 | admin_control(req) |
||
| 6204 | try: |
||
| 6205 | raw_json = req.stream.read().decode('utf-8') |
||
| 6206 | except UnicodeDecodeError as ex: |
||
| 6207 | print("Failed to decode request") |
||
| 6208 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6209 | title='API.BAD_REQUEST', |
||
| 6210 | description='API.INVALID_ENCODING') |
||
| 6211 | except Exception as ex: |
||
| 6212 | print(str(ex)) |
||
| 6213 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6214 | title='API.BAD_REQUEST', |
||
| 6215 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6216 | |||
| 6217 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6218 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6219 | description='API.INVALID_MICROGRID_ID') |
||
| 6220 | if not eid.isdigit() or int(eid) <= 0: |
||
| 6221 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6222 | description='API.INVALID_EVCHARGER_ID') |
||
| 6223 | |||
| 6224 | new_values = json.loads(raw_json) |
||
| 6225 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6226 | cursor = cnx.cursor() |
||
| 6227 | |||
| 6228 | cursor.execute(" SELECT name " |
||
| 6229 | " FROM tbl_microgrids_evchargers " |
||
| 6230 | " WHERE microgrid_id = %s AND id = %s ", (id_, eid,)) |
||
| 6231 | if cursor.fetchone() is None: |
||
| 6232 | cursor.close() |
||
| 6233 | cnx.close() |
||
| 6234 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6235 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
||
| 6236 | |||
| 6237 | cursor.execute(" SELECT name, object_type " |
||
| 6238 | " FROM tbl_points " |
||
| 6239 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 6240 | row = cursor.fetchone() |
||
| 6241 | if row is None: |
||
| 6242 | cursor.close() |
||
| 6243 | cnx.close() |
||
| 6244 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6245 | description='API.POINT_NOT_FOUND') |
||
| 6246 | |||
| 6247 | query = (" SELECT id " |
||
| 6248 | " FROM tbl_microgrids_evchargers_points " |
||
| 6249 | " WHERE evcharger_id = %s AND point_id = %s") |
||
| 6250 | cursor.execute(query, (eid, new_values['data']['point_id'],)) |
||
| 6251 | if cursor.fetchone() is not None: |
||
| 6252 | cursor.close() |
||
| 6253 | cnx.close() |
||
| 6254 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 6255 | description='API.EVCHARGER_POINT_RELATION_EXISTS') |
||
| 6256 | |||
| 6257 | add_row = (" INSERT INTO tbl_microgrids_evchargers_points (evcharger_id, point_id) " |
||
| 6258 | " VALUES (%s, %s) ") |
||
| 6259 | cursor.execute(add_row, (eid, new_values['data']['point_id'],)) |
||
| 6260 | cnx.commit() |
||
| 6261 | cursor.close() |
||
| 6262 | cnx.close() |
||
| 6263 | |||
| 6264 | resp.status = falcon.HTTP_201 |
||
| 6265 | resp.location = '/microgrids/' + str(id_) + '/evchargers/' + str(eid) + '/points/' + \ |
||
| 6266 | str(new_values['data']['point_id']) |
||
| 6267 | |||
| 6268 | |||
| 6269 | View Code Duplication | class MicrogridEVChargerPointItem: |
|
| 6270 | def __init__(self): |
||
| 6271 | pass |
||
| 6272 | |||
| 6273 | @staticmethod |
||
| 6274 | def on_options(req, resp, id_, eid, pid): |
||
| 6275 | _ = req |
||
| 6276 | resp.status = falcon.HTTP_200 |
||
| 6277 | _ = id_ |
||
| 6278 | _ = eid |
||
| 6279 | _ = pid |
||
| 6280 | |||
| 6281 | @staticmethod |
||
| 6282 | @user_logger |
||
| 6283 | def on_delete(req, resp, id_, eid, pid): |
||
| 6284 | admin_control(req) |
||
| 6285 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6286 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6287 | description='API.INVALID_MICROGRID_ID') |
||
| 6288 | if not eid.isdigit() or int(eid) <= 0: |
||
| 6289 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6290 | description='API.INVALID_EVCHARGER_ID') |
||
| 6291 | if not pid.isdigit() or int(pid) <= 0: |
||
| 6292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6293 | description='API.INVALID_POINT_ID') |
||
| 6294 | |||
| 6295 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6296 | cursor = cnx.cursor() |
||
| 6297 | |||
| 6298 | cursor.execute(" SELECT name " |
||
| 6299 | " FROM tbl_microgrids_evchargers " |
||
| 6300 | " WHERE microgrid_id = %s AND id = %s ", (id_, eid,)) |
||
| 6301 | if cursor.fetchone() is None: |
||
| 6302 | cursor.close() |
||
| 6303 | cnx.close() |
||
| 6304 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6305 | description='API.MICROGRID_EVCHARGER_NOT_FOUND') |
||
| 6306 | |||
| 6307 | cursor.execute(" SELECT name " |
||
| 6308 | " FROM tbl_points " |
||
| 6309 | " WHERE id = %s ", (pid,)) |
||
| 6310 | if cursor.fetchone() is None: |
||
| 6311 | cursor.close() |
||
| 6312 | cnx.close() |
||
| 6313 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6314 | description='API.POINT_NOT_FOUND') |
||
| 6315 | |||
| 6316 | cursor.execute(" SELECT id " |
||
| 6317 | " FROM tbl_microgrids_evchargers_points " |
||
| 6318 | " WHERE evcharger_id = %s AND point_id = %s ", (eid, pid)) |
||
| 6319 | if cursor.fetchone() is None: |
||
| 6320 | cursor.close() |
||
| 6321 | cnx.close() |
||
| 6322 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6323 | description='API.EVCHARGER_POINT_RELATION_NOT_FOUND') |
||
| 6324 | |||
| 6325 | cursor.execute(" DELETE FROM tbl_microgrids_evchargers_points " |
||
| 6326 | " WHERE evcharger_id = %s AND point_id = %s ", (eid, pid)) |
||
| 6327 | cnx.commit() |
||
| 6328 | |||
| 6329 | cursor.close() |
||
| 6330 | cnx.close() |
||
| 6331 | |||
| 6332 | resp.status = falcon.HTTP_204 |
||
| 6333 | |||
| 6334 | View Code Duplication | class MicrogridGeneratorPointCollection: |
|
| 6335 | def __init__(self): |
||
| 6336 | pass |
||
| 6337 | |||
| 6338 | @staticmethod |
||
| 6339 | def on_options(req, resp, id_, gid): |
||
| 6340 | _ = req |
||
| 6341 | resp.status = falcon.HTTP_200 |
||
| 6342 | _ = id_ |
||
| 6343 | _ = gid |
||
| 6344 | |||
| 6345 | @staticmethod |
||
| 6346 | def on_get(req, resp, id_, gid): |
||
| 6347 | if 'API-KEY' not in req.headers or \ |
||
| 6348 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 6349 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 6350 | access_control(req) |
||
| 6351 | else: |
||
| 6352 | api_key_control(req) |
||
| 6353 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6354 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6355 | description='API.INVALID_MICROGRID_ID') |
||
| 6356 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6357 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6358 | description='API.INVALID_GENERATOR_ID') |
||
| 6359 | |||
| 6360 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6361 | cursor = cnx.cursor() |
||
| 6362 | |||
| 6363 | cursor.execute(" SELECT name " |
||
| 6364 | " FROM tbl_microgrids_generators " |
||
| 6365 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6366 | if cursor.fetchone() is None: |
||
| 6367 | cursor.close() |
||
| 6368 | cnx.close() |
||
| 6369 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6370 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 6371 | |||
| 6372 | query = (" SELECT p.id, p.name, " |
||
| 6373 | " ds.id, ds.name, ds.uuid, " |
||
| 6374 | " p.address " |
||
| 6375 | " FROM tbl_points p, tbl_microgrids_generators_points mp, tbl_data_sources ds " |
||
| 6376 | " WHERE mp.generator_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 6377 | " ORDER BY p.name ") |
||
| 6378 | cursor.execute(query, (gid,)) |
||
| 6379 | rows = cursor.fetchall() |
||
| 6380 | |||
| 6381 | result = list() |
||
| 6382 | if rows is not None and len(rows) > 0: |
||
| 6383 | for row in rows: |
||
| 6384 | meta_result = {"id": row[0], "name": row[1], |
||
| 6385 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6386 | "address": row[5]} |
||
| 6387 | result.append(meta_result) |
||
| 6388 | |||
| 6389 | resp.text = json.dumps(result) |
||
| 6390 | |||
| 6391 | @staticmethod |
||
| 6392 | @user_logger |
||
| 6393 | def on_post(req, resp, id_, gid): |
||
| 6394 | admin_control(req) |
||
| 6395 | try: |
||
| 6396 | raw_json = req.stream.read().decode('utf-8') |
||
| 6397 | except UnicodeDecodeError as ex: |
||
| 6398 | print("Failed to decode request") |
||
| 6399 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6400 | title='API.BAD_REQUEST', |
||
| 6401 | description='API.INVALID_ENCODING') |
||
| 6402 | except Exception as ex: |
||
| 6403 | print(str(ex)) |
||
| 6404 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6405 | title='API.BAD_REQUEST', |
||
| 6406 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6407 | |||
| 6408 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6409 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6410 | description='API.INVALID_MICROGRID_ID') |
||
| 6411 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6413 | description='API.INVALID_GENERATOR_ID') |
||
| 6414 | |||
| 6415 | new_values = json.loads(raw_json) |
||
| 6416 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6417 | cursor = cnx.cursor() |
||
| 6418 | |||
| 6419 | cursor.execute(" SELECT name " |
||
| 6420 | " FROM tbl_microgrids_generators " |
||
| 6421 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6422 | if cursor.fetchone() is None: |
||
| 6423 | cursor.close() |
||
| 6424 | cnx.close() |
||
| 6425 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6426 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 6427 | |||
| 6428 | cursor.execute(" SELECT name, object_type " |
||
| 6429 | " FROM tbl_points " |
||
| 6430 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 6431 | row = cursor.fetchone() |
||
| 6432 | if row is None: |
||
| 6433 | cursor.close() |
||
| 6434 | cnx.close() |
||
| 6435 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6436 | description='API.POINT_NOT_FOUND') |
||
| 6437 | |||
| 6438 | query = (" SELECT id " |
||
| 6439 | " FROM tbl_microgrids_generators_points " |
||
| 6440 | " WHERE generator_id = %s AND point_id = %s") |
||
| 6441 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
||
| 6442 | if cursor.fetchone() is not None: |
||
| 6443 | cursor.close() |
||
| 6444 | cnx.close() |
||
| 6445 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 6446 | description='API.GENERATOR_POINT_RELATION_EXISTS') |
||
| 6447 | |||
| 6448 | add_row = (" INSERT INTO tbl_microgrids_generators_points (generator_id, point_id) " |
||
| 6449 | " VALUES (%s, %s) ") |
||
| 6450 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
||
| 6451 | cnx.commit() |
||
| 6452 | cursor.close() |
||
| 6453 | cnx.close() |
||
| 6454 | |||
| 6455 | resp.status = falcon.HTTP_201 |
||
| 6456 | resp.location = '/microgrids/' + str(id_) + '/generators/' + str(gid) + '/points/' + \ |
||
| 6457 | str(new_values['data']['point_id']) |
||
| 6458 | |||
| 6459 | |||
| 6460 | View Code Duplication | class MicrogridGeneratorPointItem: |
|
| 6461 | def __init__(self): |
||
| 6462 | pass |
||
| 6463 | |||
| 6464 | @staticmethod |
||
| 6465 | def on_options(req, resp, id_, gid, pid): |
||
| 6466 | _ = req |
||
| 6467 | resp.status = falcon.HTTP_200 |
||
| 6468 | _ = id_ |
||
| 6469 | _ = gid |
||
| 6470 | _ = pid |
||
| 6471 | |||
| 6472 | @staticmethod |
||
| 6473 | @user_logger |
||
| 6474 | def on_delete(req, resp, id_, gid, pid): |
||
| 6475 | admin_control(req) |
||
| 6476 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6477 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6478 | description='API.INVALID_MICROGRID_ID') |
||
| 6479 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6480 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6481 | description='API.INVALID_GENERATOR_ID') |
||
| 6482 | if not pid.isdigit() or int(pid) <= 0: |
||
| 6483 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6484 | description='API.INVALID_POINT_ID') |
||
| 6485 | |||
| 6486 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6487 | cursor = cnx.cursor() |
||
| 6488 | |||
| 6489 | cursor.execute(" SELECT name " |
||
| 6490 | " FROM tbl_microgrids_generators " |
||
| 6491 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6492 | if cursor.fetchone() is None: |
||
| 6493 | cursor.close() |
||
| 6494 | cnx.close() |
||
| 6495 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6496 | description='API.MICROGRID_GENERATOR_NOT_FOUND') |
||
| 6497 | |||
| 6498 | cursor.execute(" SELECT name " |
||
| 6499 | " FROM tbl_points " |
||
| 6500 | " WHERE id = %s ", (pid,)) |
||
| 6501 | if cursor.fetchone() is None: |
||
| 6502 | cursor.close() |
||
| 6503 | cnx.close() |
||
| 6504 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6505 | description='API.POINT_NOT_FOUND') |
||
| 6506 | |||
| 6507 | cursor.execute(" SELECT id " |
||
| 6508 | " FROM tbl_microgrids_generators_points " |
||
| 6509 | " WHERE generator_id = %s AND point_id = %s ", (gid, pid)) |
||
| 6510 | if cursor.fetchone() is None: |
||
| 6511 | cursor.close() |
||
| 6512 | cnx.close() |
||
| 6513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6514 | description='API.GENERATOR_POINT_RELATION_NOT_FOUND') |
||
| 6515 | |||
| 6516 | cursor.execute(" DELETE FROM tbl_microgrids_generators_points " |
||
| 6517 | " WHERE generator_id = %s AND point_id = %s ", (gid, pid)) |
||
| 6518 | cnx.commit() |
||
| 6519 | |||
| 6520 | cursor.close() |
||
| 6521 | cnx.close() |
||
| 6522 | |||
| 6523 | resp.status = falcon.HTTP_204 |
||
| 6524 | |||
| 6525 | View Code Duplication | class MicrogridGridPointCollection: |
|
| 6526 | def __init__(self): |
||
| 6527 | pass |
||
| 6528 | |||
| 6529 | @staticmethod |
||
| 6530 | def on_options(req, resp, id_, gid): |
||
| 6531 | _ = req |
||
| 6532 | resp.status = falcon.HTTP_200 |
||
| 6533 | _ = id_ |
||
| 6534 | _ = gid |
||
| 6535 | |||
| 6536 | @staticmethod |
||
| 6537 | def on_get(req, resp, id_, gid): |
||
| 6538 | if 'API-KEY' not in req.headers or \ |
||
| 6539 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 6540 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 6541 | access_control(req) |
||
| 6542 | else: |
||
| 6543 | api_key_control(req) |
||
| 6544 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6545 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6546 | description='API.INVALID_MICROGRID_ID') |
||
| 6547 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6548 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6549 | description='API.INVALID_GRID_ID') |
||
| 6550 | |||
| 6551 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6552 | cursor = cnx.cursor() |
||
| 6553 | |||
| 6554 | cursor.execute(" SELECT name " |
||
| 6555 | " FROM tbl_microgrids_grids " |
||
| 6556 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6557 | if cursor.fetchone() is None: |
||
| 6558 | cursor.close() |
||
| 6559 | cnx.close() |
||
| 6560 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6561 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 6562 | |||
| 6563 | query = (" SELECT p.id, p.name, " |
||
| 6564 | " ds.id, ds.name, ds.uuid, " |
||
| 6565 | " p.address " |
||
| 6566 | " FROM tbl_points p, tbl_microgrids_grids_points mp, tbl_data_sources ds " |
||
| 6567 | " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 6568 | " ORDER BY p.name ") |
||
| 6569 | cursor.execute(query, (gid,)) |
||
| 6570 | rows = cursor.fetchall() |
||
| 6571 | |||
| 6572 | result = list() |
||
| 6573 | if rows is not None and len(rows) > 0: |
||
| 6574 | for row in rows: |
||
| 6575 | meta_result = {"id": row[0], "name": row[1], |
||
| 6576 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6577 | "address": row[5]} |
||
| 6578 | result.append(meta_result) |
||
| 6579 | |||
| 6580 | resp.text = json.dumps(result) |
||
| 6581 | |||
| 6582 | @staticmethod |
||
| 6583 | @user_logger |
||
| 6584 | def on_post(req, resp, id_, gid): |
||
| 6585 | admin_control(req) |
||
| 6586 | try: |
||
| 6587 | raw_json = req.stream.read().decode('utf-8') |
||
| 6588 | except UnicodeDecodeError as ex: |
||
| 6589 | print("Failed to decode request") |
||
| 6590 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6591 | title='API.BAD_REQUEST', |
||
| 6592 | description='API.INVALID_ENCODING') |
||
| 6593 | except Exception as ex: |
||
| 6594 | print(str(ex)) |
||
| 6595 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6596 | title='API.BAD_REQUEST', |
||
| 6597 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6598 | |||
| 6599 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6601 | description='API.INVALID_MICROGRID_ID') |
||
| 6602 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6603 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6604 | description='API.INVALID_GRID_ID') |
||
| 6605 | |||
| 6606 | new_values = json.loads(raw_json) |
||
| 6607 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6608 | cursor = cnx.cursor() |
||
| 6609 | |||
| 6610 | cursor.execute(" SELECT name " |
||
| 6611 | " FROM tbl_microgrids_grids " |
||
| 6612 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6613 | if cursor.fetchone() is None: |
||
| 6614 | cursor.close() |
||
| 6615 | cnx.close() |
||
| 6616 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6617 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 6618 | |||
| 6619 | cursor.execute(" SELECT name, object_type " |
||
| 6620 | " FROM tbl_points " |
||
| 6621 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 6622 | row = cursor.fetchone() |
||
| 6623 | if row is None: |
||
| 6624 | cursor.close() |
||
| 6625 | cnx.close() |
||
| 6626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6627 | description='API.POINT_NOT_FOUND') |
||
| 6628 | |||
| 6629 | query = (" SELECT id " |
||
| 6630 | " FROM tbl_microgrids_grids_points " |
||
| 6631 | " WHERE grid_id = %s AND point_id = %s") |
||
| 6632 | cursor.execute(query, (gid, new_values['data']['point_id'],)) |
||
| 6633 | if cursor.fetchone() is not None: |
||
| 6634 | cursor.close() |
||
| 6635 | cnx.close() |
||
| 6636 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 6637 | description='API.GRID_POINT_RELATION_EXISTS') |
||
| 6638 | |||
| 6639 | add_row = (" INSERT INTO tbl_microgrids_grids_points (grid_id, point_id) " |
||
| 6640 | " VALUES (%s, %s) ") |
||
| 6641 | cursor.execute(add_row, (gid, new_values['data']['point_id'],)) |
||
| 6642 | cnx.commit() |
||
| 6643 | cursor.close() |
||
| 6644 | cnx.close() |
||
| 6645 | |||
| 6646 | resp.status = falcon.HTTP_201 |
||
| 6647 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(gid) + '/points/' + \ |
||
| 6648 | str(new_values['data']['point_id']) |
||
| 6649 | |||
| 6650 | |||
| 6651 | View Code Duplication | class MicrogridGridPointItem: |
|
| 6652 | def __init__(self): |
||
| 6653 | pass |
||
| 6654 | |||
| 6655 | @staticmethod |
||
| 6656 | def on_options(req, resp, id_, gid, pid): |
||
| 6657 | _ = req |
||
| 6658 | resp.status = falcon.HTTP_200 |
||
| 6659 | _ = id_ |
||
| 6660 | _ = gid |
||
| 6661 | _ = pid |
||
| 6662 | |||
| 6663 | @staticmethod |
||
| 6664 | @user_logger |
||
| 6665 | def on_delete(req, resp, id_, gid, pid): |
||
| 6666 | admin_control(req) |
||
| 6667 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6668 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6669 | description='API.INVALID_MICROGRID_ID') |
||
| 6670 | if not gid.isdigit() or int(gid) <= 0: |
||
| 6671 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6672 | description='API.INVALID_GRID_ID') |
||
| 6673 | if not pid.isdigit() or int(pid) <= 0: |
||
| 6674 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6675 | description='API.INVALID_POINT_ID') |
||
| 6676 | |||
| 6677 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6678 | cursor = cnx.cursor() |
||
| 6679 | |||
| 6680 | cursor.execute(" SELECT name " |
||
| 6681 | " FROM tbl_microgrids_grids " |
||
| 6682 | " WHERE microgrid_id = %s AND id = %s ", (id_, gid,)) |
||
| 6683 | if cursor.fetchone() is None: |
||
| 6684 | cursor.close() |
||
| 6685 | cnx.close() |
||
| 6686 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6687 | description='API.MICROGRID_GRID_NOT_FOUND') |
||
| 6688 | |||
| 6689 | cursor.execute(" SELECT name " |
||
| 6690 | " FROM tbl_points " |
||
| 6691 | " WHERE id = %s ", (pid,)) |
||
| 6692 | if cursor.fetchone() is None: |
||
| 6693 | cursor.close() |
||
| 6694 | cnx.close() |
||
| 6695 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6696 | description='API.POINT_NOT_FOUND') |
||
| 6697 | |||
| 6698 | cursor.execute(" SELECT id " |
||
| 6699 | " FROM tbl_microgrids_grids_points " |
||
| 6700 | " WHERE grid_id = %s AND point_id = %s ", (gid, pid)) |
||
| 6701 | if cursor.fetchone() is None: |
||
| 6702 | cursor.close() |
||
| 6703 | cnx.close() |
||
| 6704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6705 | description='API.GRID_POINT_RELATION_NOT_FOUND') |
||
| 6706 | |||
| 6707 | cursor.execute(" DELETE FROM tbl_microgrids_grids_points " |
||
| 6708 | " WHERE grid_id = %s AND point_id = %s ", (gid, pid)) |
||
| 6709 | cnx.commit() |
||
| 6710 | |||
| 6711 | cursor.close() |
||
| 6712 | cnx.close() |
||
| 6713 | |||
| 6714 | resp.status = falcon.HTTP_204 |
||
| 6715 | |||
| 6716 | View Code Duplication | class MicrogridHeatPumpPointCollection: |
|
| 6717 | def __init__(self): |
||
| 6718 | pass |
||
| 6719 | |||
| 6720 | @staticmethod |
||
| 6721 | def on_options(req, resp, id_, hid): |
||
| 6722 | _ = req |
||
| 6723 | resp.status = falcon.HTTP_200 |
||
| 6724 | _ = id_ |
||
| 6725 | _ = hid |
||
| 6726 | |||
| 6727 | @staticmethod |
||
| 6728 | def on_get(req, resp, id_, hid): |
||
| 6729 | if 'API-KEY' not in req.headers or \ |
||
| 6730 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 6731 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 6732 | access_control(req) |
||
| 6733 | else: |
||
| 6734 | api_key_control(req) |
||
| 6735 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6736 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6737 | description='API.INVALID_MICROGRID_ID') |
||
| 6738 | if not hid.isdigit() or int(hid) <= 0: |
||
| 6739 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6740 | description='API.INVALID_HEATPUMP_ID') |
||
| 6741 | |||
| 6742 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6743 | cursor = cnx.cursor() |
||
| 6744 | |||
| 6745 | cursor.execute(" SELECT name " |
||
| 6746 | " FROM tbl_microgrids_heatpumps " |
||
| 6747 | " WHERE microgrid_id = %s AND id = %s ", (id_, hid,)) |
||
| 6748 | if cursor.fetchone() is None: |
||
| 6749 | cursor.close() |
||
| 6750 | cnx.close() |
||
| 6751 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6752 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 6753 | |||
| 6754 | query = (" SELECT p.id, p.name, " |
||
| 6755 | " ds.id, ds.name, ds.uuid, " |
||
| 6756 | " p.address " |
||
| 6757 | " FROM tbl_points p, tbl_microgrids_heatpumps_points mp, tbl_data_sources ds " |
||
| 6758 | " WHERE mp.heatpump_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 6759 | " ORDER BY p.name ") |
||
| 6760 | cursor.execute(query, (hid,)) |
||
| 6761 | rows = cursor.fetchall() |
||
| 6762 | |||
| 6763 | result = list() |
||
| 6764 | if rows is not None and len(rows) > 0: |
||
| 6765 | for row in rows: |
||
| 6766 | meta_result = {"id": row[0], "name": row[1], |
||
| 6767 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6768 | "address": row[5]} |
||
| 6769 | result.append(meta_result) |
||
| 6770 | |||
| 6771 | resp.text = json.dumps(result) |
||
| 6772 | |||
| 6773 | @staticmethod |
||
| 6774 | @user_logger |
||
| 6775 | def on_post(req, resp, id_, hid): |
||
| 6776 | admin_control(req) |
||
| 6777 | try: |
||
| 6778 | raw_json = req.stream.read().decode('utf-8') |
||
| 6779 | except UnicodeDecodeError as ex: |
||
| 6780 | print("Failed to decode request") |
||
| 6781 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6782 | title='API.BAD_REQUEST', |
||
| 6783 | description='API.INVALID_ENCODING') |
||
| 6784 | except Exception as ex: |
||
| 6785 | print(str(ex)) |
||
| 6786 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6787 | title='API.BAD_REQUEST', |
||
| 6788 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6789 | |||
| 6790 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6791 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6792 | description='API.INVALID_MICROGRID_ID') |
||
| 6793 | if not hid.isdigit() or int(hid) <= 0: |
||
| 6794 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6795 | description='API.INVALID_HEATPUMP_ID') |
||
| 6796 | |||
| 6797 | new_values = json.loads(raw_json) |
||
| 6798 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6799 | cursor = cnx.cursor() |
||
| 6800 | |||
| 6801 | cursor.execute(" SELECT name " |
||
| 6802 | " FROM tbl_microgrids_heatpumps " |
||
| 6803 | " WHERE microgrid_id = %s AND id = %s ", (id_, hid,)) |
||
| 6804 | if cursor.fetchone() is None: |
||
| 6805 | cursor.close() |
||
| 6806 | cnx.close() |
||
| 6807 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6808 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 6809 | |||
| 6810 | cursor.execute(" SELECT name, object_type " |
||
| 6811 | " FROM tbl_points " |
||
| 6812 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 6813 | row = cursor.fetchone() |
||
| 6814 | if row is None: |
||
| 6815 | cursor.close() |
||
| 6816 | cnx.close() |
||
| 6817 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6818 | description='API.POINT_NOT_FOUND') |
||
| 6819 | |||
| 6820 | query = (" SELECT id " |
||
| 6821 | " FROM tbl_microgrids_heatpumps_points " |
||
| 6822 | " WHERE heatpump_id = %s AND point_id = %s") |
||
| 6823 | cursor.execute(query, (hid, new_values['data']['point_id'],)) |
||
| 6824 | if cursor.fetchone() is not None: |
||
| 6825 | cursor.close() |
||
| 6826 | cnx.close() |
||
| 6827 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 6828 | description='API.HEATPUMP_POINT_RELATION_EXISTS') |
||
| 6829 | |||
| 6830 | add_row = (" INSERT INTO tbl_microgrids_heatpumps_points (heatpump_id, point_id) " |
||
| 6831 | " VALUES (%s, %s) ") |
||
| 6832 | cursor.execute(add_row, (hid, new_values['data']['point_id'],)) |
||
| 6833 | cnx.commit() |
||
| 6834 | cursor.close() |
||
| 6835 | cnx.close() |
||
| 6836 | |||
| 6837 | resp.status = falcon.HTTP_201 |
||
| 6838 | resp.location = '/microgrids/' + str(id_) + '/heatpumps/' + str(hid) + '/points/' + \ |
||
| 6839 | str(new_values['data']['point_id']) |
||
| 6840 | |||
| 6841 | |||
| 6842 | View Code Duplication | class MicrogridHeatPumpPointItem: |
|
| 6843 | def __init__(self): |
||
| 6844 | pass |
||
| 6845 | |||
| 6846 | @staticmethod |
||
| 6847 | def on_options(req, resp, id_, hid, pid): |
||
| 6848 | _ = req |
||
| 6849 | resp.status = falcon.HTTP_200 |
||
| 6850 | _ = id_ |
||
| 6851 | _ = hid |
||
| 6852 | _ = pid |
||
| 6853 | |||
| 6854 | @staticmethod |
||
| 6855 | @user_logger |
||
| 6856 | def on_delete(req, resp, id_, hid, pid): |
||
| 6857 | admin_control(req) |
||
| 6858 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6859 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6860 | description='API.INVALID_MICROGRID_ID') |
||
| 6861 | if not hid.isdigit() or int(hid) <= 0: |
||
| 6862 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6863 | description='API.INVALID_HEATPUMP_ID') |
||
| 6864 | if not pid.isdigit() or int(pid) <= 0: |
||
| 6865 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6866 | description='API.INVALID_POINT_ID') |
||
| 6867 | |||
| 6868 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6869 | cursor = cnx.cursor() |
||
| 6870 | |||
| 6871 | cursor.execute(" SELECT name " |
||
| 6872 | " FROM tbl_microgrids_heatpumps " |
||
| 6873 | " WHERE microgrid_id = %s AND id = %s ", (id_, hid,)) |
||
| 6874 | if cursor.fetchone() is None: |
||
| 6875 | cursor.close() |
||
| 6876 | cnx.close() |
||
| 6877 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6878 | description='API.MICROGRID_HEATPUMP_NOT_FOUND') |
||
| 6879 | |||
| 6880 | cursor.execute(" SELECT name " |
||
| 6881 | " FROM tbl_points " |
||
| 6882 | " WHERE id = %s ", (pid,)) |
||
| 6883 | if cursor.fetchone() is None: |
||
| 6884 | cursor.close() |
||
| 6885 | cnx.close() |
||
| 6886 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6887 | description='API.POINT_NOT_FOUND') |
||
| 6888 | |||
| 6889 | cursor.execute(" SELECT id " |
||
| 6890 | " FROM tbl_microgrids_heatpumps_points " |
||
| 6891 | " WHERE heatpump_id = %s AND point_id = %s ", (hid, pid)) |
||
| 6892 | if cursor.fetchone() is None: |
||
| 6893 | cursor.close() |
||
| 6894 | cnx.close() |
||
| 6895 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6896 | description='API.HEATPUMP_POINT_RELATION_NOT_FOUND') |
||
| 6897 | |||
| 6898 | cursor.execute(" DELETE FROM tbl_microgrids_heatpumps_points " |
||
| 6899 | " WHERE heatpump_id = %s AND point_id = %s ", (hid, pid)) |
||
| 6900 | cnx.commit() |
||
| 6901 | |||
| 6902 | cursor.close() |
||
| 6903 | cnx.close() |
||
| 6904 | |||
| 6905 | resp.status = falcon.HTTP_204 |
||
| 6906 | |||
| 6907 | View Code Duplication | class MicrogridLoadPointCollection: |
|
| 6908 | def __init__(self): |
||
| 6909 | pass |
||
| 6910 | |||
| 6911 | @staticmethod |
||
| 6912 | def on_options(req, resp, id_, lid): |
||
| 6913 | _ = req |
||
| 6914 | resp.status = falcon.HTTP_200 |
||
| 6915 | _ = id_ |
||
| 6916 | _ = lid |
||
| 6917 | |||
| 6918 | @staticmethod |
||
| 6919 | def on_get(req, resp, id_, lid): |
||
| 6920 | if 'API-KEY' not in req.headers or \ |
||
| 6921 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 6922 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 6923 | access_control(req) |
||
| 6924 | else: |
||
| 6925 | api_key_control(req) |
||
| 6926 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6927 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6928 | description='API.INVALID_MICROGRID_ID') |
||
| 6929 | if not lid.isdigit() or int(lid) <= 0: |
||
| 6930 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6931 | description='API.INVALID_LOAD_ID') |
||
| 6932 | |||
| 6933 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6934 | cursor = cnx.cursor() |
||
| 6935 | |||
| 6936 | cursor.execute(" SELECT name " |
||
| 6937 | " FROM tbl_microgrids_loads " |
||
| 6938 | " WHERE microgrid_id = %s AND id = %s ", (id_, lid,)) |
||
| 6939 | if cursor.fetchone() is None: |
||
| 6940 | cursor.close() |
||
| 6941 | cnx.close() |
||
| 6942 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6943 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 6944 | |||
| 6945 | query = (" SELECT p.id, p.name, " |
||
| 6946 | " ds.id, ds.name, ds.uuid, " |
||
| 6947 | " p.address " |
||
| 6948 | " FROM tbl_points p, tbl_microgrids_loads_points mp, tbl_data_sources ds " |
||
| 6949 | " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 6950 | " ORDER BY p.name ") |
||
| 6951 | cursor.execute(query, (lid,)) |
||
| 6952 | rows = cursor.fetchall() |
||
| 6953 | |||
| 6954 | result = list() |
||
| 6955 | if rows is not None and len(rows) > 0: |
||
| 6956 | for row in rows: |
||
| 6957 | meta_result = {"id": row[0], "name": row[1], |
||
| 6958 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 6959 | "address": row[5]} |
||
| 6960 | result.append(meta_result) |
||
| 6961 | |||
| 6962 | resp.text = json.dumps(result) |
||
| 6963 | |||
| 6964 | @staticmethod |
||
| 6965 | @user_logger |
||
| 6966 | def on_post(req, resp, id_, lid): |
||
| 6967 | admin_control(req) |
||
| 6968 | try: |
||
| 6969 | raw_json = req.stream.read().decode('utf-8') |
||
| 6970 | except UnicodeDecodeError as ex: |
||
| 6971 | print("Failed to decode request") |
||
| 6972 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6973 | title='API.BAD_REQUEST', |
||
| 6974 | description='API.INVALID_ENCODING') |
||
| 6975 | except Exception as ex: |
||
| 6976 | print(str(ex)) |
||
| 6977 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 6978 | title='API.BAD_REQUEST', |
||
| 6979 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 6980 | |||
| 6981 | if not id_.isdigit() or int(id_) <= 0: |
||
| 6982 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6983 | description='API.INVALID_MICROGRID_ID') |
||
| 6984 | if not lid.isdigit() or int(lid) <= 0: |
||
| 6985 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 6986 | description='API.INVALID_LOAD_ID') |
||
| 6987 | |||
| 6988 | new_values = json.loads(raw_json) |
||
| 6989 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 6990 | cursor = cnx.cursor() |
||
| 6991 | |||
| 6992 | cursor.execute(" SELECT name " |
||
| 6993 | " FROM tbl_microgrids_loads " |
||
| 6994 | " WHERE microgrid_id = %s AND id = %s ", (id_, lid,)) |
||
| 6995 | if cursor.fetchone() is None: |
||
| 6996 | cursor.close() |
||
| 6997 | cnx.close() |
||
| 6998 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 6999 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 7000 | |||
| 7001 | cursor.execute(" SELECT name, object_type " |
||
| 7002 | " FROM tbl_points " |
||
| 7003 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 7004 | row = cursor.fetchone() |
||
| 7005 | if row is None: |
||
| 7006 | cursor.close() |
||
| 7007 | cnx.close() |
||
| 7008 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7009 | description='API.POINT_NOT_FOUND') |
||
| 7010 | |||
| 7011 | query = (" SELECT id " |
||
| 7012 | " FROM tbl_microgrids_loads_points " |
||
| 7013 | " WHERE load_id = %s AND point_id = %s") |
||
| 7014 | cursor.execute(query, (lid, new_values['data']['point_id'],)) |
||
| 7015 | if cursor.fetchone() is not None: |
||
| 7016 | cursor.close() |
||
| 7017 | cnx.close() |
||
| 7018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 7019 | description='API.LOAD_POINT_RELATION_EXISTS') |
||
| 7020 | |||
| 7021 | add_row = (" INSERT INTO tbl_microgrids_loads_points (load_id, point_id) " |
||
| 7022 | " VALUES (%s, %s) ") |
||
| 7023 | cursor.execute(add_row, (lid, new_values['data']['point_id'],)) |
||
| 7024 | cnx.commit() |
||
| 7025 | cursor.close() |
||
| 7026 | cnx.close() |
||
| 7027 | |||
| 7028 | resp.status = falcon.HTTP_201 |
||
| 7029 | resp.location = '/microgrids/' + str(id_) + '/loads/' + str(lid) + '/points/' + \ |
||
| 7030 | str(new_values['data']['point_id']) |
||
| 7031 | |||
| 7032 | |||
| 7033 | View Code Duplication | class MicrogridLoadPointItem: |
|
| 7034 | def __init__(self): |
||
| 7035 | pass |
||
| 7036 | |||
| 7037 | @staticmethod |
||
| 7038 | def on_options(req, resp, id_, lid, pid): |
||
| 7039 | _ = req |
||
| 7040 | resp.status = falcon.HTTP_200 |
||
| 7041 | _ = id_ |
||
| 7042 | _ = lid |
||
| 7043 | _ = pid |
||
| 7044 | |||
| 7045 | @staticmethod |
||
| 7046 | @user_logger |
||
| 7047 | def on_delete(req, resp, id_, lid, pid): |
||
| 7048 | admin_control(req) |
||
| 7049 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7051 | description='API.INVALID_MICROGRID_ID') |
||
| 7052 | if not lid.isdigit() or int(lid) <= 0: |
||
| 7053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7054 | description='API.INVALID_LOAD_ID') |
||
| 7055 | if not pid.isdigit() or int(pid) <= 0: |
||
| 7056 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7057 | description='API.INVALID_POINT_ID') |
||
| 7058 | |||
| 7059 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7060 | cursor = cnx.cursor() |
||
| 7061 | |||
| 7062 | cursor.execute(" SELECT name " |
||
| 7063 | " FROM tbl_microgrids_loads " |
||
| 7064 | " WHERE microgrid_id = %s AND id = %s ", (id_, lid,)) |
||
| 7065 | if cursor.fetchone() is None: |
||
| 7066 | cursor.close() |
||
| 7067 | cnx.close() |
||
| 7068 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7069 | description='API.MICROGRID_LOAD_NOT_FOUND') |
||
| 7070 | |||
| 7071 | cursor.execute(" SELECT name " |
||
| 7072 | " FROM tbl_points " |
||
| 7073 | " WHERE id = %s ", (pid,)) |
||
| 7074 | if cursor.fetchone() is None: |
||
| 7075 | cursor.close() |
||
| 7076 | cnx.close() |
||
| 7077 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7078 | description='API.POINT_NOT_FOUND') |
||
| 7079 | |||
| 7080 | cursor.execute(" SELECT id " |
||
| 7081 | " FROM tbl_microgrids_loads_points " |
||
| 7082 | " WHERE load_id = %s AND point_id = %s ", (lid, pid)) |
||
| 7083 | if cursor.fetchone() is None: |
||
| 7084 | cursor.close() |
||
| 7085 | cnx.close() |
||
| 7086 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7087 | description='API.LOAD_POINT_RELATION_NOT_FOUND') |
||
| 7088 | |||
| 7089 | cursor.execute(" DELETE FROM tbl_microgrids_loads_points " |
||
| 7090 | " WHERE load_id = %s AND point_id = %s ", (lid, pid)) |
||
| 7091 | cnx.commit() |
||
| 7092 | |||
| 7093 | cursor.close() |
||
| 7094 | cnx.close() |
||
| 7095 | |||
| 7096 | resp.status = falcon.HTTP_204 |
||
| 7097 | |||
| 7098 | View Code Duplication | class MicrogridPhotovoltaicPointCollection: |
|
| 7099 | def __init__(self): |
||
| 7100 | pass |
||
| 7101 | |||
| 7102 | @staticmethod |
||
| 7103 | def on_options(req, resp, id_, pvid): |
||
| 7104 | _ = req |
||
| 7105 | resp.status = falcon.HTTP_200 |
||
| 7106 | _ = id_ |
||
| 7107 | _ = pvid |
||
| 7108 | |||
| 7109 | @staticmethod |
||
| 7110 | def on_get(req, resp, id_, pvid): |
||
| 7111 | if 'API-KEY' not in req.headers or \ |
||
| 7112 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 7113 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 7114 | access_control(req) |
||
| 7115 | else: |
||
| 7116 | api_key_control(req) |
||
| 7117 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7118 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7119 | description='API.INVALID_MICROGRID_ID') |
||
| 7120 | if not pvid.isdigit() or int(pvid) <= 0: |
||
| 7121 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7122 | description='API.INVALID_PHOTOVOLTAIC_ID') |
||
| 7123 | |||
| 7124 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7125 | cursor = cnx.cursor() |
||
| 7126 | |||
| 7127 | cursor.execute(" SELECT name " |
||
| 7128 | " FROM tbl_microgrids_photovoltaics " |
||
| 7129 | " WHERE microgrid_id = %s AND id = %s ", (id_, pvid,)) |
||
| 7130 | if cursor.fetchone() is None: |
||
| 7131 | cursor.close() |
||
| 7132 | cnx.close() |
||
| 7133 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7134 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 7135 | |||
| 7136 | query = (" SELECT p.id, p.name, " |
||
| 7137 | " ds.id, ds.name, ds.uuid, " |
||
| 7138 | " p.address " |
||
| 7139 | " FROM tbl_points p, tbl_microgrids_pvs_points mp, tbl_data_sources ds " |
||
| 7140 | " WHERE mp.pv_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 7141 | " ORDER BY p.name ") |
||
| 7142 | cursor.execute(query, (pvid,)) |
||
| 7143 | rows = cursor.fetchall() |
||
| 7144 | |||
| 7145 | result = list() |
||
| 7146 | if rows is not None and len(rows) > 0: |
||
| 7147 | for row in rows: |
||
| 7148 | meta_result = {"id": row[0], "name": row[1], |
||
| 7149 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 7150 | "address": row[5]} |
||
| 7151 | result.append(meta_result) |
||
| 7152 | |||
| 7153 | resp.text = json.dumps(result) |
||
| 7154 | |||
| 7155 | @staticmethod |
||
| 7156 | @user_logger |
||
| 7157 | def on_post(req, resp, id_, pvid): |
||
| 7158 | admin_control(req) |
||
| 7159 | try: |
||
| 7160 | raw_json = req.stream.read().decode('utf-8') |
||
| 7161 | except UnicodeDecodeError as ex: |
||
| 7162 | print("Failed to decode request") |
||
| 7163 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 7164 | title='API.BAD_REQUEST', |
||
| 7165 | description='API.INVALID_ENCODING') |
||
| 7166 | except Exception as ex: |
||
| 7167 | print(str(ex)) |
||
| 7168 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 7169 | title='API.BAD_REQUEST', |
||
| 7170 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 7171 | |||
| 7172 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7174 | description='API.INVALID_MICROGRID_ID') |
||
| 7175 | if not pvid.isdigit() or int(pvid) <= 0: |
||
| 7176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7177 | description='API.INVALID_PHOTOVOLTAIC_ID') |
||
| 7178 | |||
| 7179 | new_values = json.loads(raw_json) |
||
| 7180 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7181 | cursor = cnx.cursor() |
||
| 7182 | |||
| 7183 | cursor.execute(" SELECT name " |
||
| 7184 | " FROM tbl_microgrids_photovoltaics " |
||
| 7185 | " WHERE microgrid_id = %s AND id = %s ", (id_, pvid,)) |
||
| 7186 | if cursor.fetchone() is None: |
||
| 7187 | cursor.close() |
||
| 7188 | cnx.close() |
||
| 7189 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7190 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 7191 | |||
| 7192 | cursor.execute(" SELECT name, object_type " |
||
| 7193 | " FROM tbl_points " |
||
| 7194 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 7195 | row = cursor.fetchone() |
||
| 7196 | if row is None: |
||
| 7197 | cursor.close() |
||
| 7198 | cnx.close() |
||
| 7199 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7200 | description='API.POINT_NOT_FOUND') |
||
| 7201 | |||
| 7202 | query = (" SELECT id " |
||
| 7203 | " FROM tbl_microgrids_pvs_points " |
||
| 7204 | " WHERE pv_id = %s AND point_id = %s") |
||
| 7205 | cursor.execute(query, (pvid, new_values['data']['point_id'],)) |
||
| 7206 | if cursor.fetchone() is not None: |
||
| 7207 | cursor.close() |
||
| 7208 | cnx.close() |
||
| 7209 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 7210 | description='API.PHOTOVOLTAIC_POINT_RELATION_EXISTS') |
||
| 7211 | |||
| 7212 | add_row = (" INSERT INTO tbl_microgrids_pvs_points (pv_id, point_id) " |
||
| 7213 | " VALUES (%s, %s) ") |
||
| 7214 | cursor.execute(add_row, (pvid, new_values['data']['point_id'],)) |
||
| 7215 | cnx.commit() |
||
| 7216 | cursor.close() |
||
| 7217 | cnx.close() |
||
| 7218 | |||
| 7219 | resp.status = falcon.HTTP_201 |
||
| 7220 | resp.location = '/microgrids/' + str(id_) + '/photovoltaics/' + str(pvid) + '/points/' + \ |
||
| 7221 | str(new_values['data']['point_id']) |
||
| 7222 | |||
| 7223 | |||
| 7224 | View Code Duplication | class MicrogridPhotovoltaicPointItem: |
|
| 7225 | def __init__(self): |
||
| 7226 | pass |
||
| 7227 | |||
| 7228 | @staticmethod |
||
| 7229 | def on_options(req, resp, id_, pvid, pid): |
||
| 7230 | _ = req |
||
| 7231 | resp.status = falcon.HTTP_200 |
||
| 7232 | _ = id_ |
||
| 7233 | _ = pvid |
||
| 7234 | _ = pid |
||
| 7235 | |||
| 7236 | @staticmethod |
||
| 7237 | @user_logger |
||
| 7238 | def on_delete(req, resp, id_, pvid, pid): |
||
| 7239 | admin_control(req) |
||
| 7240 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7241 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7242 | description='API.INVALID_MICROGRID_ID') |
||
| 7243 | if not pvid.isdigit() or int(pvid) <= 0: |
||
| 7244 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7245 | description='API.INVALID_PHOTOVOLTAIC_ID') |
||
| 7246 | if not pid.isdigit() or int(pid) <= 0: |
||
| 7247 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7248 | description='API.INVALID_POINT_ID') |
||
| 7249 | |||
| 7250 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7251 | cursor = cnx.cursor() |
||
| 7252 | |||
| 7253 | cursor.execute(" SELECT name " |
||
| 7254 | " FROM tbl_microgrids_photovoltaics " |
||
| 7255 | " WHERE microgrid_id = %s AND id = %s ", (id_, pvid,)) |
||
| 7256 | if cursor.fetchone() is None: |
||
| 7257 | cursor.close() |
||
| 7258 | cnx.close() |
||
| 7259 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7260 | description='API.MICROGRID_PHOTOVOLTAIC_NOT_FOUND') |
||
| 7261 | |||
| 7262 | cursor.execute(" SELECT name " |
||
| 7263 | " FROM tbl_points " |
||
| 7264 | " WHERE id = %s ", (pid,)) |
||
| 7265 | if cursor.fetchone() is None: |
||
| 7266 | cursor.close() |
||
| 7267 | cnx.close() |
||
| 7268 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7269 | description='API.POINT_NOT_FOUND') |
||
| 7270 | |||
| 7271 | cursor.execute(" SELECT id " |
||
| 7272 | " FROM tbl_microgrids_pvs_points " |
||
| 7273 | " WHERE pv_id = %s AND point_id = %s ", (pvid, pid)) |
||
| 7274 | if cursor.fetchone() is None: |
||
| 7275 | cursor.close() |
||
| 7276 | cnx.close() |
||
| 7277 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7278 | description='API.PHOTOVOLTAIC_POINT_RELATION_NOT_FOUND') |
||
| 7279 | |||
| 7280 | cursor.execute(" DELETE FROM tbl_microgrids_pvs_points " |
||
| 7281 | " WHERE pv_id = %s AND point_id = %s ", (pvid, pid)) |
||
| 7282 | cnx.commit() |
||
| 7283 | |||
| 7284 | cursor.close() |
||
| 7285 | cnx.close() |
||
| 7286 | |||
| 7287 | resp.status = falcon.HTTP_204 |
||
| 7288 | |||
| 7289 | View Code Duplication | class MicrogridPowerConversionSystemPointCollection: |
|
| 7290 | def __init__(self): |
||
| 7291 | pass |
||
| 7292 | |||
| 7293 | @staticmethod |
||
| 7294 | def on_options(req, resp, id_, pcsid): |
||
| 7295 | _ = req |
||
| 7296 | resp.status = falcon.HTTP_200 |
||
| 7297 | _ = id_ |
||
| 7298 | _ = pcsid |
||
| 7299 | |||
| 7300 | @staticmethod |
||
| 7301 | def on_get(req, resp, id_, pcsid): |
||
| 7302 | if 'API-KEY' not in req.headers or \ |
||
| 7303 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 7304 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 7305 | access_control(req) |
||
| 7306 | else: |
||
| 7307 | api_key_control(req) |
||
| 7308 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7309 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7310 | description='API.INVALID_MICROGRID_ID') |
||
| 7311 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 7312 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7313 | description='API.INVALID_POWER_CONVERSION_SYSTEM_ID') |
||
| 7314 | |||
| 7315 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7316 | cursor = cnx.cursor() |
||
| 7317 | |||
| 7318 | cursor.execute(" SELECT name " |
||
| 7319 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 7320 | " WHERE microgrid_id = %s AND id = %s ", (id_, pcsid,)) |
||
| 7321 | if cursor.fetchone() is None: |
||
| 7322 | cursor.close() |
||
| 7323 | cnx.close() |
||
| 7324 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7325 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 7326 | |||
| 7327 | query = (" SELECT p.id, p.name, " |
||
| 7328 | " ds.id, ds.name, ds.uuid, " |
||
| 7329 | " p.address " |
||
| 7330 | " FROM tbl_points p, tbl_microgrids_pcses_points mp, tbl_data_sources ds " |
||
| 7331 | " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id " |
||
| 7332 | " ORDER BY p.name ") |
||
| 7333 | cursor.execute(query, (pcsid,)) |
||
| 7334 | rows = cursor.fetchall() |
||
| 7335 | |||
| 7336 | result = list() |
||
| 7337 | if rows is not None and len(rows) > 0: |
||
| 7338 | for row in rows: |
||
| 7339 | meta_result = {"id": row[0], "name": row[1], |
||
| 7340 | "data_source": {"id": row[2], "name": row[3], "uuid": row[4]}, |
||
| 7341 | "address": row[5]} |
||
| 7342 | result.append(meta_result) |
||
| 7343 | |||
| 7344 | resp.text = json.dumps(result) |
||
| 7345 | |||
| 7346 | @staticmethod |
||
| 7347 | @user_logger |
||
| 7348 | def on_post(req, resp, id_, pcsid): |
||
| 7349 | admin_control(req) |
||
| 7350 | try: |
||
| 7351 | raw_json = req.stream.read().decode('utf-8') |
||
| 7352 | except UnicodeDecodeError as ex: |
||
| 7353 | print("Failed to decode request") |
||
| 7354 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 7355 | title='API.BAD_REQUEST', |
||
| 7356 | description='API.INVALID_ENCODING') |
||
| 7357 | except Exception as ex: |
||
| 7358 | print(str(ex)) |
||
| 7359 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 7360 | title='API.BAD_REQUEST', |
||
| 7361 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 7362 | |||
| 7363 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7364 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7365 | description='API.INVALID_MICROGRID_ID') |
||
| 7366 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 7367 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7368 | description='API.INVALID_POWER_CONVERSION_SYSTEM_ID') |
||
| 7369 | |||
| 7370 | new_values = json.loads(raw_json) |
||
| 7371 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7372 | cursor = cnx.cursor() |
||
| 7373 | |||
| 7374 | cursor.execute(" SELECT name " |
||
| 7375 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 7376 | " WHERE microgrid_id = %s AND id = %s ", (id_, pcsid,)) |
||
| 7377 | if cursor.fetchone() is None: |
||
| 7378 | cursor.close() |
||
| 7379 | cnx.close() |
||
| 7380 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7381 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 7382 | |||
| 7383 | cursor.execute(" SELECT name, object_type " |
||
| 7384 | " FROM tbl_points " |
||
| 7385 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
| 7386 | row = cursor.fetchone() |
||
| 7387 | if row is None: |
||
| 7388 | cursor.close() |
||
| 7389 | cnx.close() |
||
| 7390 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7391 | description='API.POINT_NOT_FOUND') |
||
| 7392 | |||
| 7393 | query = (" SELECT id " |
||
| 7394 | " FROM tbl_microgrids_pcses_points " |
||
| 7395 | " WHERE pcs_id = %s AND point_id = %s") |
||
| 7396 | cursor.execute(query, (pcsid, new_values['data']['point_id'],)) |
||
| 7397 | if cursor.fetchone() is not None: |
||
| 7398 | cursor.close() |
||
| 7399 | cnx.close() |
||
| 7400 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 7401 | description='API.POWER_CONVERSION_SYSTEM_POINT_RELATION_EXISTS') |
||
| 7402 | |||
| 7403 | add_row = (" INSERT INTO tbl_microgrids_pcses_points (pcs_id, point_id) " |
||
| 7404 | " VALUES (%s, %s) ") |
||
| 7405 | cursor.execute(add_row, (pcsid, new_values['data']['point_id'],)) |
||
| 7406 | cnx.commit() |
||
| 7407 | cursor.close() |
||
| 7408 | cnx.close() |
||
| 7409 | |||
| 7410 | resp.status = falcon.HTTP_201 |
||
| 7411 | resp.location = '/microgrids/' + str(id_) + '/powerconversionsystems/' + str(pcsid) + '/points/' + \ |
||
| 7412 | str(new_values['data']['point_id']) |
||
| 7413 | |||
| 7414 | |||
| 7415 | View Code Duplication | class MicrogridPowerConversionSystemPointItem: |
|
| 7416 | def __init__(self): |
||
| 7417 | pass |
||
| 7418 | |||
| 7419 | @staticmethod |
||
| 7420 | def on_options(req, resp, id_, pcsid, pid): |
||
| 7421 | _ = req |
||
| 7422 | resp.status = falcon.HTTP_200 |
||
| 7423 | _ = id_ |
||
| 7424 | _ = pcsid |
||
| 7425 | _ = pid |
||
| 7426 | |||
| 7427 | @staticmethod |
||
| 7428 | @user_logger |
||
| 7429 | def on_delete(req, resp, id_, pcsid, pid): |
||
| 7430 | admin_control(req) |
||
| 7431 | if not id_.isdigit() or int(id_) <= 0: |
||
| 7432 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7433 | description='API.INVALID_MICROGRID_ID') |
||
| 7434 | if not pcsid.isdigit() or int(pcsid) <= 0: |
||
| 7435 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7436 | description='API.INVALID_POWER_CONVERSION_SYSTEM_ID') |
||
| 7437 | if not pid.isdigit() or int(pid) <= 0: |
||
| 7438 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 7439 | description='API.INVALID_POINT_ID') |
||
| 7440 | |||
| 7441 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 7442 | cursor = cnx.cursor() |
||
| 7443 | |||
| 7444 | cursor.execute(" SELECT name " |
||
| 7445 | " FROM tbl_microgrids_power_conversion_systems " |
||
| 7446 | " WHERE microgrid_id = %s AND id = %s ", (id_, pcsid,)) |
||
| 7447 | if cursor.fetchone() is None: |
||
| 7448 | cursor.close() |
||
| 7449 | cnx.close() |
||
| 7450 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7451 | description='API.MICROGRID_POWER_CONVERSION_SYSTEM_NOT_FOUND') |
||
| 7452 | |||
| 7453 | cursor.execute(" SELECT name " |
||
| 7454 | " FROM tbl_points " |
||
| 7455 | " WHERE id = %s ", (pid,)) |
||
| 7456 | if cursor.fetchone() is None: |
||
| 7457 | cursor.close() |
||
| 7458 | cnx.close() |
||
| 7459 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7460 | description='API.POINT_NOT_FOUND') |
||
| 7461 | |||
| 7462 | cursor.execute(" SELECT id " |
||
| 7463 | " FROM tbl_microgrids_pcses_points " |
||
| 7464 | " WHERE pcs_id = %s AND point_id = %s ", (pcsid, pid)) |
||
| 7465 | if cursor.fetchone() is None: |
||
| 7466 | cursor.close() |
||
| 7467 | cnx.close() |
||
| 7468 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 7469 | description='API.POWER_CONVERSION_SYSTEM_POINT_RELATION_NOT_FOUND') |
||
| 7470 | |||
| 7471 | cursor.execute(" DELETE FROM tbl_microgrids_pcses_points " |
||
| 7472 | " WHERE pcs_id = %s AND point_id = %s ", (pcsid, pid)) |
||
| 7473 | cnx.commit() |
||
| 7474 | |||
| 7475 | cursor.close() |
||
| 7476 | cnx.close() |
||
| 7477 | |||
| 7478 | resp.status = falcon.HTTP_204 |
||
| 7479 |