| Total Complexity | 363 |
| Total Lines | 1676 |
| Duplicated Lines | 22.97 % |
| 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.energystoragepowerstation 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, api_key_control |
||
| 7 | import config |
||
| 8 | |||
| 9 | |||
| 10 | class EnergyStoragePowerStationCollection: |
||
| 11 | """ |
||
| 12 | Energy Storage Power Station Collection Resource |
||
| 13 | |||
| 14 | This class handles CRUD operations for energy storage power station collection. |
||
| 15 | It provides endpoints for listing all energy storage power stations and creating new ones. |
||
| 16 | Energy storage power stations represent large-scale energy storage facilities |
||
| 17 | that can store and discharge significant amounts of energy for grid applications. |
||
| 18 | """ |
||
| 19 | def __init__(self): |
||
| 20 | pass |
||
| 21 | |||
| 22 | @staticmethod |
||
| 23 | def on_options(req, resp): |
||
| 24 | """ |
||
| 25 | Handle OPTIONS request for CORS preflight |
||
| 26 | |||
| 27 | Args: |
||
| 28 | req: Falcon request object |
||
| 29 | resp: Falcon response object |
||
| 30 | """ |
||
| 31 | _ = req |
||
| 32 | resp.status = falcon.HTTP_200 |
||
| 33 | |||
| 34 | @staticmethod |
||
| 35 | def on_get(req, resp): |
||
| 36 | """ |
||
| 37 | Handle GET requests to retrieve all energy storage power stations |
||
| 38 | |||
| 39 | Returns a list of all energy storage power stations with their complete information including: |
||
| 40 | - Station ID, name, and UUID |
||
| 41 | - Associated contact and cost center information |
||
| 42 | - Station specifications and parameters |
||
| 43 | - Related equipment and meter associations |
||
| 44 | - SVG diagram information for visualization |
||
| 45 | |||
| 46 | Args: |
||
| 47 | req: Falcon request object |
||
| 48 | resp: Falcon response object |
||
| 49 | """ |
||
| 50 | access_control(req) |
||
| 51 | search_query = req.get_param('q', default=None) |
||
| 52 | if search_query is not None and len(search_query.strip()) > 0: |
||
| 53 | search_query = search_query.strip() |
||
| 54 | else: |
||
| 55 | search_query = '' |
||
| 56 | |||
| 57 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 58 | cursor = cnx.cursor() |
||
| 59 | |||
| 60 | # query contact dict |
||
| 61 | contact_dict = dict() |
||
| 62 | query = (" SELECT id, name, uuid " |
||
| 63 | " FROM tbl_contacts ") |
||
| 64 | cursor.execute(query) |
||
| 65 | rows_contacts = cursor.fetchall() |
||
| 66 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 67 | for row in rows_contacts: |
||
| 68 | contact_dict[row[0]] = {"id": row[0], |
||
| 69 | "name": row[1], |
||
| 70 | "uuid": row[2]} |
||
| 71 | # query cost center dict |
||
| 72 | cost_center_dict = dict() |
||
| 73 | query = (" SELECT id, name, uuid " |
||
| 74 | " FROM tbl_cost_centers ") |
||
| 75 | cursor.execute(query) |
||
| 76 | rows_cost_centers = cursor.fetchall() |
||
| 77 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 78 | for row in rows_cost_centers: |
||
| 79 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 80 | "name": row[1], |
||
| 81 | "uuid": row[2]} |
||
| 82 | |||
| 83 | # query svg dict |
||
| 84 | svg_dict = dict() |
||
| 85 | query = (" SELECT id, name, uuid " |
||
| 86 | " FROM tbl_svgs ") |
||
| 87 | cursor.execute(query) |
||
| 88 | rows_svgs = cursor.fetchall() |
||
| 89 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 90 | for row in rows_svgs: |
||
| 91 | svg_dict[row[0]] = {"id": row[0], |
||
| 92 | "name": row[1], |
||
| 93 | "uuid": row[2]} |
||
| 94 | |||
| 95 | # query point dict |
||
| 96 | point_dict = dict() |
||
| 97 | query = (" SELECT id, name " |
||
| 98 | " FROM tbl_points ") |
||
| 99 | cursor.execute(query) |
||
| 100 | rows_points = cursor.fetchall() |
||
| 101 | if rows_points is not None and len(rows_points) > 0: |
||
| 102 | for row in rows_points: |
||
| 103 | point_dict[row[0]] = {"id": row[0], |
||
| 104 | "name": row[1]} |
||
| 105 | |||
| 106 | query = (" SELECT id, name, uuid, " |
||
| 107 | " address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
| 108 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
| 109 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
| 110 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
| 111 | " FROM tbl_energy_storage_power_stations ") |
||
| 112 | params = [] |
||
| 113 | if search_query: |
||
| 114 | query += " WHERE name LIKE %s OR address LIKE %s OR description LIKE %s " |
||
| 115 | params = [f'%{search_query}%', f'%{search_query}%', f'%{search_query}%'] |
||
| 116 | query += " ORDER BY id " |
||
| 117 | |||
| 118 | cursor.execute(query, params) |
||
| 119 | rows_energy_storage_power_stations = cursor.fetchall() |
||
| 120 | |||
| 121 | result = list() |
||
| 122 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
||
| 123 | for row in rows_energy_storage_power_stations: |
||
| 124 | meta_result = {"id": row[0], |
||
| 125 | "name": row[1], |
||
| 126 | "uuid": row[2], |
||
| 127 | "address": row[3], |
||
| 128 | "latitude": row[4], |
||
| 129 | "longitude": row[5], |
||
| 130 | "latitude_point": point_dict.get(row[6], None), |
||
| 131 | "longitude_point": point_dict.get(row[7], None), |
||
| 132 | "rated_capacity": row[8], |
||
| 133 | "rated_power": row[9], |
||
| 134 | "contact": contact_dict.get(row[10], None), |
||
| 135 | "cost_center": cost_center_dict.get(row[11], None), |
||
| 136 | "svg": svg_dict.get(row[12], None), |
||
| 137 | "svg2": svg_dict.get(row[13], None), |
||
| 138 | "svg3": svg_dict.get(row[14], None), |
||
| 139 | "svg4": svg_dict.get(row[15], None), |
||
| 140 | "svg5": svg_dict.get(row[16], None), |
||
| 141 | "is_cost_data_displayed": bool(row[17]), |
||
| 142 | "phase_of_lifecycle": row[18], |
||
| 143 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
| 144 | "description": row[20], |
||
| 145 | "qrcode": 'energystoragepowerstation:' + row[2]} |
||
| 146 | result.append(meta_result) |
||
| 147 | |||
| 148 | cursor.close() |
||
| 149 | cnx.close() |
||
| 150 | resp.text = json.dumps(result) |
||
| 151 | |||
| 152 | @staticmethod |
||
| 153 | @user_logger |
||
| 154 | def on_post(req, resp): |
||
| 155 | """ |
||
| 156 | Handle POST requests to create a new energy storage power station |
||
| 157 | |||
| 158 | Creates a new energy storage power station with the provided specifications. |
||
| 159 | The station will be empty initially and equipment/meters can be added separately. |
||
| 160 | |||
| 161 | Args: |
||
| 162 | req: Falcon request object containing station data |
||
| 163 | resp: Falcon response object |
||
| 164 | """ |
||
| 165 | admin_control(req) |
||
| 166 | try: |
||
| 167 | raw_json = req.stream.read().decode('utf-8') |
||
| 168 | except UnicodeDecodeError as ex: |
||
| 169 | print("Failed to decode request") |
||
| 170 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 171 | title='API.BAD_REQUEST', |
||
| 172 | description='API.INVALID_ENCODING') |
||
| 173 | except Exception as ex: |
||
| 174 | print("Unexpected error reading request stream") |
||
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 176 | title='API.BAD_REQUEST', |
||
| 177 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 178 | |||
| 179 | new_values = json.loads(raw_json) |
||
| 180 | |||
| 181 | if 'name' not in new_values['data'].keys() or \ |
||
| 182 | not isinstance(new_values['data']['name'], str) or \ |
||
| 183 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 185 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
| 186 | name = str.strip(new_values['data']['name']) |
||
| 187 | |||
| 188 | if 'address' not in new_values['data'].keys() or \ |
||
| 189 | not isinstance(new_values['data']['address'], str) or \ |
||
| 190 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 191 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 192 | description='API.INVALID_ADDRESS_VALUE') |
||
| 193 | address = str.strip(new_values['data']['address']) |
||
| 194 | |||
| 195 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 196 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 197 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 198 | new_values['data']['latitude'] < -90.0 or \ |
||
| 199 | new_values['data']['latitude'] > 90.0: |
||
| 200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 201 | description='API.INVALID_LATITUDE_VALUE') |
||
| 202 | latitude = new_values['data']['latitude'] |
||
| 203 | |||
| 204 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 205 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 206 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 207 | new_values['data']['longitude'] < -180.0 or \ |
||
| 208 | new_values['data']['longitude'] > 180.0: |
||
| 209 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 210 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 211 | longitude = new_values['data']['longitude'] |
||
| 212 | |||
| 213 | if 'latitude_point_id' in new_values['data'].keys() and \ |
||
| 214 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
||
| 215 | new_values['data']['latitude_point_id'] > 0: |
||
| 216 | latitude_point_id = new_values['data']['latitude_point_id'] |
||
| 217 | else: |
||
| 218 | latitude_point_id = None |
||
| 219 | |||
| 220 | if 'longitude_point_id' in new_values['data'].keys() and \ |
||
| 221 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
||
| 222 | new_values['data']['longitude_point_id'] > 0: |
||
| 223 | longitude_point_id = new_values['data']['longitude_point_id'] |
||
| 224 | else: |
||
| 225 | longitude_point_id = None |
||
| 226 | |||
| 227 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 228 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 229 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 230 | new_values['data']['rated_capacity'] < 0.0: |
||
| 231 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 232 | description='API.INVALID_RATED_CAPACITY') |
||
| 233 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 234 | |||
| 235 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 236 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 237 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 238 | new_values['data']['rated_power'] < 0.0: |
||
| 239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 240 | description='API.INVALID_RATED_POWER') |
||
| 241 | rated_power = new_values['data']['rated_power'] |
||
| 242 | |||
| 243 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 244 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 245 | new_values['data']['contact_id'] <= 0: |
||
| 246 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 247 | description='API.INVALID_CONTACT_ID') |
||
| 248 | contact_id = new_values['data']['contact_id'] |
||
| 249 | |||
| 250 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 251 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 252 | new_values['data']['cost_center_id'] <= 0: |
||
| 253 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 254 | description='API.INVALID_COST_CENTER_ID') |
||
| 255 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 256 | |||
| 257 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 258 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 259 | new_values['data']['svg_id'] <= 0: |
||
| 260 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 261 | description='API.INVALID_SVG_ID') |
||
| 262 | svg_id = new_values['data']['svg_id'] |
||
| 263 | |||
| 264 | if 'svg2_id' in new_values['data'].keys() and \ |
||
| 265 | isinstance(new_values['data']['svg2_id'], int) and \ |
||
| 266 | new_values['data']['svg2_id'] > 0: |
||
| 267 | svg2_id = new_values['data']['svg2_id'] |
||
| 268 | else: |
||
| 269 | svg2_id = None |
||
| 270 | |||
| 271 | if 'svg3_id' in new_values['data'].keys() and \ |
||
| 272 | isinstance(new_values['data']['svg3_id'], int) and \ |
||
| 273 | new_values['data']['svg3_id'] > 0: |
||
| 274 | svg3_id = new_values['data']['svg3_id'] |
||
| 275 | else: |
||
| 276 | svg3_id = None |
||
| 277 | |||
| 278 | if 'svg4_id' in new_values['data'].keys() and \ |
||
| 279 | isinstance(new_values['data']['svg4_id'], int) and \ |
||
| 280 | new_values['data']['svg4_id'] > 0: |
||
| 281 | svg4_id = new_values['data']['svg4_id'] |
||
| 282 | else: |
||
| 283 | svg4_id = None |
||
| 284 | |||
| 285 | if 'svg5_id' in new_values['data'].keys() and \ |
||
| 286 | isinstance(new_values['data']['svg5_id'], int) and \ |
||
| 287 | new_values['data']['svg5_id'] > 0: |
||
| 288 | svg5_id = new_values['data']['svg5_id'] |
||
| 289 | else: |
||
| 290 | svg5_id = None |
||
| 291 | |||
| 292 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
| 293 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
| 294 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 295 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
| 296 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
| 297 | |||
| 298 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
| 299 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
| 300 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
| 301 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 302 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
| 303 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
| 304 | |||
| 305 | if 'commissioning_date' not in new_values['data'].keys() or \ |
||
| 306 | not isinstance(new_values['data']['commissioning_date'], str) or \ |
||
| 307 | len(str.strip(new_values['data']['commissioning_date'])) == 0: |
||
| 308 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 309 | description='API.INVALID_COMMISSIONING_DATE') |
||
| 310 | commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
||
| 311 | |||
| 312 | if 'description' in new_values['data'].keys() and \ |
||
| 313 | new_values['data']['description'] is not None and \ |
||
| 314 | len(str(new_values['data']['description'])) > 0: |
||
| 315 | description = str.strip(new_values['data']['description']) |
||
| 316 | else: |
||
| 317 | description = None |
||
| 318 | |||
| 319 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 320 | cursor = cnx.cursor() |
||
| 321 | |||
| 322 | cursor.execute(" SELECT name " |
||
| 323 | " FROM tbl_energy_storage_power_stations " |
||
| 324 | " WHERE name = %s ", (name,)) |
||
| 325 | if cursor.fetchone() is not None: |
||
| 326 | cursor.close() |
||
| 327 | cnx.close() |
||
| 328 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 329 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
| 330 | |||
| 331 | cursor.execute(" SELECT name " |
||
| 332 | " FROM tbl_contacts " |
||
| 333 | " WHERE id = %s ", |
||
| 334 | (contact_id,)) |
||
| 335 | row = cursor.fetchone() |
||
| 336 | if row is None: |
||
| 337 | cursor.close() |
||
| 338 | cnx.close() |
||
| 339 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 340 | description='API.CONTACT_NOT_FOUND') |
||
| 341 | |||
| 342 | cursor.execute(" SELECT name " |
||
| 343 | " FROM tbl_cost_centers " |
||
| 344 | " WHERE id = %s ", |
||
| 345 | (cost_center_id,)) |
||
| 346 | row = cursor.fetchone() |
||
| 347 | if row is None: |
||
| 348 | cursor.close() |
||
| 349 | cnx.close() |
||
| 350 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 351 | description='API.COST_CENTER_NOT_FOUND') |
||
| 352 | |||
| 353 | cursor.execute(" SELECT name " |
||
| 354 | " FROM tbl_svgs " |
||
| 355 | " WHERE id = %s ", |
||
| 356 | (svg_id,)) |
||
| 357 | row = cursor.fetchone() |
||
| 358 | if row is None: |
||
| 359 | cursor.close() |
||
| 360 | cnx.close() |
||
| 361 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 362 | description='API.SVG_NOT_FOUND') |
||
| 363 | |||
| 364 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
| 365 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
| 366 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
| 367 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
| 368 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description) " |
||
| 369 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 370 | cursor.execute(add_values, (name, |
||
| 371 | str(uuid.uuid4()), |
||
| 372 | address, |
||
| 373 | latitude, |
||
| 374 | longitude, |
||
| 375 | latitude_point_id, |
||
| 376 | longitude_point_id, |
||
| 377 | rated_capacity, |
||
| 378 | rated_power, |
||
| 379 | contact_id, |
||
| 380 | cost_center_id, |
||
| 381 | svg_id, |
||
| 382 | svg2_id, |
||
| 383 | svg3_id, |
||
| 384 | svg4_id, |
||
| 385 | svg5_id, |
||
| 386 | is_cost_data_displayed, |
||
| 387 | phase_of_lifecycle, |
||
| 388 | commissioning_date, |
||
| 389 | description)) |
||
| 390 | new_id = cursor.lastrowid |
||
| 391 | cnx.commit() |
||
| 392 | cursor.close() |
||
| 393 | cnx.close() |
||
| 394 | |||
| 395 | resp.status = falcon.HTTP_201 |
||
| 396 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
| 397 | |||
| 398 | |||
| 399 | class EnergyStoragePowerStationItem: |
||
| 400 | """ |
||
| 401 | Energy Storage Power Station Item Resource |
||
| 402 | |||
| 403 | This class handles CRUD operations for individual energy storage power stations. |
||
| 404 | It provides endpoints for retrieving, updating, and deleting specific |
||
| 405 | energy storage power stations by their ID. |
||
| 406 | """ |
||
| 407 | def __init__(self): |
||
| 408 | pass |
||
| 409 | |||
| 410 | @staticmethod |
||
| 411 | def on_options(req, resp, id_): |
||
| 412 | _ = req |
||
| 413 | resp.status = falcon.HTTP_200 |
||
| 414 | _ = id_ |
||
| 415 | |||
| 416 | @staticmethod |
||
| 417 | def on_get(req, resp, id_): |
||
| 418 | access_control(req) |
||
| 419 | if not id_.isdigit() or int(id_) <= 0: |
||
| 420 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 421 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 422 | |||
| 423 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 424 | cursor = cnx.cursor() |
||
| 425 | |||
| 426 | contact_dict = dict() |
||
| 427 | query = (" SELECT id, name, uuid " |
||
| 428 | " FROM tbl_contacts ") |
||
| 429 | cursor.execute(query) |
||
| 430 | rows_contacts = cursor.fetchall() |
||
| 431 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 432 | for row in rows_contacts: |
||
| 433 | contact_dict[row[0]] = {"id": row[0], |
||
| 434 | "name": row[1], |
||
| 435 | "uuid": row[2]} |
||
| 436 | |||
| 437 | cost_center_dict = dict() |
||
| 438 | query = (" SELECT id, name, uuid " |
||
| 439 | " FROM tbl_cost_centers ") |
||
| 440 | cursor.execute(query) |
||
| 441 | rows_cost_centers = cursor.fetchall() |
||
| 442 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 443 | for row in rows_cost_centers: |
||
| 444 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 445 | "name": row[1], |
||
| 446 | "uuid": row[2]} |
||
| 447 | |||
| 448 | svg_dict = dict() |
||
| 449 | query = (" SELECT id, name, uuid " |
||
| 450 | " FROM tbl_svgs ") |
||
| 451 | cursor.execute(query) |
||
| 452 | rows_svgs = cursor.fetchall() |
||
| 453 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 454 | for row in rows_svgs: |
||
| 455 | svg_dict[row[0]] = {"id": row[0], |
||
| 456 | "name": row[1], |
||
| 457 | "uuid": row[2]} |
||
| 458 | |||
| 459 | # query point dict |
||
| 460 | point_dict = dict() |
||
| 461 | query = (" SELECT id, name " |
||
| 462 | " FROM tbl_points ") |
||
| 463 | cursor.execute(query) |
||
| 464 | rows_points = cursor.fetchall() |
||
| 465 | if rows_points is not None and len(rows_points) > 0: |
||
| 466 | for row in rows_points: |
||
| 467 | point_dict[row[0]] = {"id": row[0], |
||
| 468 | "name": row[1]} |
||
| 469 | |||
| 470 | query = (" SELECT id, name, uuid, " |
||
| 471 | " address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
| 472 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
| 473 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
| 474 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
| 475 | " FROM tbl_energy_storage_power_stations " |
||
| 476 | " WHERE id = %s ") |
||
| 477 | cursor.execute(query, (id_,)) |
||
| 478 | row = cursor.fetchone() |
||
| 479 | cursor.close() |
||
| 480 | cnx.close() |
||
| 481 | |||
| 482 | if row is None: |
||
| 483 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 484 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 485 | else: |
||
| 486 | meta_result = {"id": row[0], |
||
| 487 | "name": row[1], |
||
| 488 | "uuid": row[2], |
||
| 489 | "address": row[3], |
||
| 490 | "latitude": row[4], |
||
| 491 | "longitude": row[5], |
||
| 492 | "latitude_point": point_dict.get(row[6], None), |
||
| 493 | "longitude_point": point_dict.get(row[7], None), |
||
| 494 | "rated_capacity": row[8], |
||
| 495 | "rated_power": row[9], |
||
| 496 | "contact": contact_dict.get(row[10], None), |
||
| 497 | "cost_center": cost_center_dict.get(row[11], None), |
||
| 498 | "svg": svg_dict.get(row[12], None), |
||
| 499 | "svg2": svg_dict.get(row[13], None), |
||
| 500 | "svg3": svg_dict.get(row[14], None), |
||
| 501 | "svg4": svg_dict.get(row[15], None), |
||
| 502 | "svg5": svg_dict.get(row[16], None), |
||
| 503 | "is_cost_data_displayed": bool(row[17]), |
||
| 504 | "phase_of_lifecycle": row[18], |
||
| 505 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
| 506 | "description": row[20], |
||
| 507 | "qrcode": 'energystoragepowerstation:' + row[2]} |
||
| 508 | |||
| 509 | resp.text = json.dumps(meta_result) |
||
| 510 | |||
| 511 | @staticmethod |
||
| 512 | @user_logger |
||
| 513 | def on_delete(req, resp, id_): |
||
| 514 | admin_control(req) |
||
| 515 | if not id_.isdigit() or int(id_) <= 0: |
||
| 516 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 517 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 518 | |||
| 519 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 520 | cursor = cnx.cursor() |
||
| 521 | |||
| 522 | cursor.execute(" SELECT name " |
||
| 523 | " FROM tbl_energy_storage_power_stations " |
||
| 524 | " WHERE id = %s ", (id_,)) |
||
| 525 | if cursor.fetchone() is None: |
||
| 526 | cursor.close() |
||
| 527 | cnx.close() |
||
| 528 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 529 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 530 | |||
| 531 | # check relation with spaces |
||
| 532 | cursor.execute(" SELECT id " |
||
| 533 | " FROM tbl_spaces_energy_storage_power_stations " |
||
| 534 | " WHERE energy_storage_power_station_id = %s ", (id_,)) |
||
| 535 | rows_spaces = cursor.fetchall() |
||
| 536 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 537 | cursor.close() |
||
| 538 | cnx.close() |
||
| 539 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 540 | title='API.BAD_REQUEST', |
||
| 541 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 542 | |||
| 543 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
||
| 544 | " WHERE energy_storage_power_station_id = %s ", (id_,)) |
||
| 545 | |||
| 546 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations " |
||
| 547 | " WHERE id = %s ", (id_,)) |
||
| 548 | cnx.commit() |
||
| 549 | |||
| 550 | cursor.close() |
||
| 551 | cnx.close() |
||
| 552 | |||
| 553 | resp.status = falcon.HTTP_204 |
||
| 554 | |||
| 555 | @staticmethod |
||
| 556 | @user_logger |
||
| 557 | def on_put(req, resp, id_): |
||
| 558 | """Handles PUT requests""" |
||
| 559 | admin_control(req) |
||
| 560 | try: |
||
| 561 | raw_json = req.stream.read().decode('utf-8') |
||
| 562 | except UnicodeDecodeError as ex: |
||
| 563 | print("Failed to decode request") |
||
| 564 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 565 | title='API.BAD_REQUEST', |
||
| 566 | description='API.INVALID_ENCODING') |
||
| 567 | except Exception as ex: |
||
| 568 | print("Unexpected error reading request stream") |
||
| 569 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 570 | title='API.BAD_REQUEST', |
||
| 571 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 572 | |||
| 573 | if not id_.isdigit() or int(id_) <= 0: |
||
| 574 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 575 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 576 | |||
| 577 | new_values = json.loads(raw_json) |
||
| 578 | |||
| 579 | if 'name' not in new_values['data'].keys() or \ |
||
| 580 | not isinstance(new_values['data']['name'], str) or \ |
||
| 581 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 582 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 583 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
| 584 | name = str.strip(new_values['data']['name']) |
||
| 585 | |||
| 586 | if 'address' not in new_values['data'].keys() or \ |
||
| 587 | not isinstance(new_values['data']['address'], str) or \ |
||
| 588 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 589 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 590 | description='API.INVALID_ADDRESS_VALUE') |
||
| 591 | address = str.strip(new_values['data']['address']) |
||
| 592 | |||
| 593 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 594 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 595 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 596 | new_values['data']['latitude'] < -90.0 or \ |
||
| 597 | new_values['data']['latitude'] > 90.0: |
||
| 598 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 599 | description='API.INVALID_LATITUDE_VALUE') |
||
| 600 | latitude = new_values['data']['latitude'] |
||
| 601 | |||
| 602 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 603 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 604 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 605 | new_values['data']['longitude'] < -180.0 or \ |
||
| 606 | new_values['data']['longitude'] > 180.0: |
||
| 607 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 608 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 609 | longitude = new_values['data']['longitude'] |
||
| 610 | |||
| 611 | if 'latitude_point_id' in new_values['data'].keys() and \ |
||
| 612 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
||
| 613 | new_values['data']['latitude_point_id'] > 0: |
||
| 614 | latitude_point_id = new_values['data']['latitude_point_id'] |
||
| 615 | else: |
||
| 616 | latitude_point_id = None |
||
| 617 | |||
| 618 | if 'longitude_point_id' in new_values['data'].keys() and \ |
||
| 619 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
||
| 620 | new_values['data']['longitude_point_id'] > 0: |
||
| 621 | longitude_point_id = new_values['data']['longitude_point_id'] |
||
| 622 | else: |
||
| 623 | longitude_point_id = None |
||
| 624 | |||
| 625 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
| 626 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
| 627 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
| 628 | new_values['data']['rated_capacity'] < 0.0: |
||
| 629 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 630 | description='API.INVALID_RATED_CAPACITY') |
||
| 631 | rated_capacity = new_values['data']['rated_capacity'] |
||
| 632 | |||
| 633 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 634 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 635 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 636 | new_values['data']['rated_power'] < 0.0: |
||
| 637 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 638 | description='API.INVALID_RATED_POWER') |
||
| 639 | rated_power = new_values['data']['rated_power'] |
||
| 640 | |||
| 641 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 642 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 643 | new_values['data']['contact_id'] <= 0: |
||
| 644 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 645 | description='API.INVALID_CONTACT_ID') |
||
| 646 | contact_id = new_values['data']['contact_id'] |
||
| 647 | |||
| 648 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 649 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 650 | new_values['data']['cost_center_id'] <= 0: |
||
| 651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 652 | description='API.INVALID_COST_CENTER_ID') |
||
| 653 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 654 | |||
| 655 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 656 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 657 | new_values['data']['svg_id'] <= 0: |
||
| 658 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 659 | description='API.INVALID_SVG_ID') |
||
| 660 | svg_id = new_values['data']['svg_id'] |
||
| 661 | |||
| 662 | if 'svg2_id' in new_values['data'].keys() and \ |
||
| 663 | isinstance(new_values['data']['svg2_id'], int) and \ |
||
| 664 | new_values['data']['svg2_id'] > 0: |
||
| 665 | svg2_id = new_values['data']['svg2_id'] |
||
| 666 | else: |
||
| 667 | svg2_id = None |
||
| 668 | |||
| 669 | if 'svg3_id' in new_values['data'].keys() and \ |
||
| 670 | isinstance(new_values['data']['svg3_id'], int) and \ |
||
| 671 | new_values['data']['svg3_id'] > 0: |
||
| 672 | svg3_id = new_values['data']['svg3_id'] |
||
| 673 | else: |
||
| 674 | svg3_id = None |
||
| 675 | |||
| 676 | if 'svg4_id' in new_values['data'].keys() and \ |
||
| 677 | isinstance(new_values['data']['svg4_id'], int) and \ |
||
| 678 | new_values['data']['svg4_id'] > 0: |
||
| 679 | svg4_id = new_values['data']['svg4_id'] |
||
| 680 | else: |
||
| 681 | svg4_id = None |
||
| 682 | |||
| 683 | if 'svg5_id' in new_values['data'].keys() and \ |
||
| 684 | isinstance(new_values['data']['svg5_id'], int) and \ |
||
| 685 | new_values['data']['svg5_id'] > 0: |
||
| 686 | svg5_id = new_values['data']['svg5_id'] |
||
| 687 | else: |
||
| 688 | svg5_id = None |
||
| 689 | |||
| 690 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
| 691 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
| 692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 693 | description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE') |
||
| 694 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
| 695 | |||
| 696 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
| 697 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
| 698 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
| 699 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 700 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
| 701 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
| 702 | |||
| 703 | if 'commissioning_date' not in new_values['data'].keys() or \ |
||
| 704 | not isinstance(new_values['data']['commissioning_date'], str) or \ |
||
| 705 | len(str.strip(new_values['data']['commissioning_date'])) == 0: |
||
| 706 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 707 | description='API.INVALID_COMMISSIONING_DATE') |
||
| 708 | commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
||
| 709 | |||
| 710 | if 'description' in new_values['data'].keys() and \ |
||
| 711 | new_values['data']['description'] is not None and \ |
||
| 712 | len(str(new_values['data']['description'])) > 0: |
||
| 713 | description = str.strip(new_values['data']['description']) |
||
| 714 | else: |
||
| 715 | description = None |
||
| 716 | |||
| 717 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 718 | cursor = cnx.cursor() |
||
| 719 | |||
| 720 | cursor.execute(" SELECT name " |
||
| 721 | " FROM tbl_energy_storage_power_stations " |
||
| 722 | " WHERE id = %s ", (id_,)) |
||
| 723 | if cursor.fetchone() is None: |
||
| 724 | cursor.close() |
||
| 725 | cnx.close() |
||
| 726 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 727 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 728 | |||
| 729 | cursor.execute(" SELECT name " |
||
| 730 | " FROM tbl_energy_storage_power_stations " |
||
| 731 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 732 | if cursor.fetchone() is not None: |
||
| 733 | cursor.close() |
||
| 734 | cnx.close() |
||
| 735 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 736 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
| 737 | |||
| 738 | cursor.execute(" SELECT name " |
||
| 739 | " FROM tbl_contacts " |
||
| 740 | " WHERE id = %s ", |
||
| 741 | (new_values['data']['contact_id'],)) |
||
| 742 | row = cursor.fetchone() |
||
| 743 | if row is None: |
||
| 744 | cursor.close() |
||
| 745 | cnx.close() |
||
| 746 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 747 | description='API.CONTACT_NOT_FOUND') |
||
| 748 | |||
| 749 | cursor.execute(" SELECT name " |
||
| 750 | " FROM tbl_cost_centers " |
||
| 751 | " WHERE id = %s ", |
||
| 752 | (new_values['data']['cost_center_id'],)) |
||
| 753 | row = cursor.fetchone() |
||
| 754 | if row is None: |
||
| 755 | cursor.close() |
||
| 756 | cnx.close() |
||
| 757 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 758 | description='API.COST_CENTER_NOT_FOUND') |
||
| 759 | |||
| 760 | cursor.execute(" SELECT name " |
||
| 761 | " FROM tbl_svgs " |
||
| 762 | " WHERE id = %s ", |
||
| 763 | (new_values['data']['svg_id'],)) |
||
| 764 | row = cursor.fetchone() |
||
| 765 | if row is None: |
||
| 766 | cursor.close() |
||
| 767 | cnx.close() |
||
| 768 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 769 | description='API.SVG_NOT_FOUND') |
||
| 770 | |||
| 771 | update_row = (" UPDATE tbl_energy_storage_power_stations " |
||
| 772 | " SET name = %s, address = %s, latitude = %s, longitude = %s, " |
||
| 773 | " latitude_point_id = %s, longitude_point_id = %s, " |
||
| 774 | " rated_capacity = %s, rated_power = %s, " |
||
| 775 | " contact_id = %s, cost_center_id = %s, " |
||
| 776 | " svg_id = %s, svg2_id = %s, svg3_id = %s, svg4_id = %s, svg5_id = %s, " |
||
| 777 | " is_cost_data_displayed = %s, phase_of_lifecycle = %s, commissioning_date = %s, " |
||
| 778 | " description = %s " |
||
| 779 | " WHERE id = %s ") |
||
| 780 | cursor.execute(update_row, (name, |
||
| 781 | address, |
||
| 782 | latitude, |
||
| 783 | longitude, |
||
| 784 | latitude_point_id, |
||
| 785 | longitude_point_id, |
||
| 786 | rated_capacity, |
||
| 787 | rated_power, |
||
| 788 | contact_id, |
||
| 789 | cost_center_id, |
||
| 790 | svg_id, |
||
| 791 | svg2_id, |
||
| 792 | svg3_id, |
||
| 793 | svg4_id, |
||
| 794 | svg5_id, |
||
| 795 | is_cost_data_displayed, |
||
| 796 | phase_of_lifecycle, |
||
| 797 | commissioning_date, |
||
| 798 | description, |
||
| 799 | id_)) |
||
| 800 | cnx.commit() |
||
| 801 | |||
| 802 | cursor.close() |
||
| 803 | cnx.close() |
||
| 804 | |||
| 805 | resp.status = falcon.HTTP_200 |
||
| 806 | |||
| 807 | |||
| 808 | View Code Duplication | class EnergyStoragePowerStationContainerCollection: |
|
|
|
|||
| 809 | def __init__(self): |
||
| 810 | pass |
||
| 811 | |||
| 812 | @staticmethod |
||
| 813 | def on_options(req, resp, id_): |
||
| 814 | _ = req |
||
| 815 | resp.status = falcon.HTTP_200 |
||
| 816 | _ = id_ |
||
| 817 | |||
| 818 | @staticmethod |
||
| 819 | def on_get(req, resp, id_): |
||
| 820 | access_control(req) |
||
| 821 | if not id_.isdigit() or int(id_) <= 0: |
||
| 822 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 823 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 824 | |||
| 825 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 826 | cursor = cnx.cursor() |
||
| 827 | |||
| 828 | cursor.execute(" SELECT name " |
||
| 829 | " FROM tbl_energy_storage_power_stations " |
||
| 830 | " WHERE id = %s ", (id_,)) |
||
| 831 | if cursor.fetchone() is None: |
||
| 832 | cursor.close() |
||
| 833 | cnx.close() |
||
| 834 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 835 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 836 | |||
| 837 | query = (" SELECT s.id, s.name, s.uuid " |
||
| 838 | " FROM tbl_energy_storage_power_stations e, " |
||
| 839 | " tbl_energy_storage_power_stations_containers es, tbl_energy_storage_containers s " |
||
| 840 | " WHERE es.energy_storage_power_station_id = e.id " |
||
| 841 | " AND s.id = es.energy_storage_container_id " |
||
| 842 | " AND e.id = %s " |
||
| 843 | " ORDER BY s.id ") |
||
| 844 | cursor.execute(query, (id_,)) |
||
| 845 | rows = cursor.fetchall() |
||
| 846 | |||
| 847 | result = list() |
||
| 848 | if rows is not None and len(rows) > 0: |
||
| 849 | for row in rows: |
||
| 850 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 851 | result.append(meta_result) |
||
| 852 | |||
| 853 | resp.text = json.dumps(result) |
||
| 854 | |||
| 855 | @staticmethod |
||
| 856 | @user_logger |
||
| 857 | def on_post(req, resp, id_): |
||
| 858 | """Handles POST requests""" |
||
| 859 | admin_control(req) |
||
| 860 | try: |
||
| 861 | raw_json = req.stream.read().decode('utf-8') |
||
| 862 | except UnicodeDecodeError as ex: |
||
| 863 | print("Failed to decode request") |
||
| 864 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 865 | title='API.BAD_REQUEST', |
||
| 866 | description='API.INVALID_ENCODING') |
||
| 867 | except Exception as ex: |
||
| 868 | print("Unexpected error reading request stream") |
||
| 869 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 870 | title='API.BAD_REQUEST', |
||
| 871 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 872 | |||
| 873 | if not id_.isdigit() or int(id_) <= 0: |
||
| 874 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 875 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 876 | |||
| 877 | new_values = json.loads(raw_json) |
||
| 878 | |||
| 879 | if 'energy_storage_container_id' not in new_values['data'].keys() or \ |
||
| 880 | not isinstance(new_values['data']['energy_storage_container_id'], int) or \ |
||
| 881 | new_values['data']['energy_storage_container_id'] <= 0: |
||
| 882 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 883 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 884 | energy_storage_container_id = new_values['data']['energy_storage_container_id'] |
||
| 885 | |||
| 886 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 887 | cursor = cnx.cursor() |
||
| 888 | |||
| 889 | cursor.execute(" SELECT name " |
||
| 890 | " FROM tbl_energy_storage_power_stations " |
||
| 891 | " WHERE id = %s ", (id_,)) |
||
| 892 | if cursor.fetchone() is None: |
||
| 893 | cursor.close() |
||
| 894 | cnx.close() |
||
| 895 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 896 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 897 | |||
| 898 | cursor.execute(" SELECT name " |
||
| 899 | " FROM tbl_energy_storage_containers " |
||
| 900 | " WHERE id = %s ", (energy_storage_container_id,)) |
||
| 901 | if cursor.fetchone() is None: |
||
| 902 | cursor.close() |
||
| 903 | cnx.close() |
||
| 904 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 905 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 906 | |||
| 907 | query = (" SELECT id " |
||
| 908 | " FROM tbl_energy_storage_power_stations_containers " |
||
| 909 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s") |
||
| 910 | cursor.execute(query, (id_, energy_storage_container_id,)) |
||
| 911 | if cursor.fetchone() is not None: |
||
| 912 | cursor.close() |
||
| 913 | cnx.close() |
||
| 914 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 915 | description='API.ENERGY_STORAGE_CONTAINER_SENSOR_RELATION_EXISTS') |
||
| 916 | |||
| 917 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_containers " |
||
| 918 | " (energy_storage_power_station_id, energy_storage_container_id) " |
||
| 919 | " VALUES (%s, %s) ") |
||
| 920 | cursor.execute(add_row, (id_, energy_storage_container_id,)) |
||
| 921 | cnx.commit() |
||
| 922 | cursor.close() |
||
| 923 | cnx.close() |
||
| 924 | |||
| 925 | resp.status = falcon.HTTP_201 |
||
| 926 | resp.location = '/energystoragepowerstationss/' + str(id_) + '/containers/' + str(energy_storage_container_id) |
||
| 927 | |||
| 928 | |||
| 929 | class EnergyStoragePowerStationContainerItem: |
||
| 930 | def __init__(self): |
||
| 931 | pass |
||
| 932 | |||
| 933 | @staticmethod |
||
| 934 | def on_options(req, resp, id_, sid): |
||
| 935 | _ = req |
||
| 936 | resp.status = falcon.HTTP_200 |
||
| 937 | _ = id_ |
||
| 938 | |||
| 939 | @staticmethod |
||
| 940 | @user_logger |
||
| 941 | def on_delete(req, resp, id_, sid): |
||
| 942 | admin_control(req) |
||
| 943 | if not id_.isdigit() or int(id_) <= 0: |
||
| 944 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 945 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 946 | |||
| 947 | if not sid.isdigit() or int(sid) <= 0: |
||
| 948 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 949 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 950 | |||
| 951 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 952 | cursor = cnx.cursor() |
||
| 953 | |||
| 954 | cursor.execute(" SELECT name " |
||
| 955 | " FROM tbl_energy_storage_power_stations " |
||
| 956 | " WHERE id = %s ", (id_,)) |
||
| 957 | if cursor.fetchone() is None: |
||
| 958 | cursor.close() |
||
| 959 | cnx.close() |
||
| 960 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 961 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 962 | |||
| 963 | cursor.execute(" SELECT name " |
||
| 964 | " FROM tbl_energy_storage_containers " |
||
| 965 | " WHERE id = %s ", (sid,)) |
||
| 966 | if cursor.fetchone() is None: |
||
| 967 | cursor.close() |
||
| 968 | cnx.close() |
||
| 969 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 970 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
| 971 | |||
| 972 | cursor.execute(" SELECT id " |
||
| 973 | " FROM tbl_energy_storage_power_stations_containers " |
||
| 974 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
||
| 975 | if cursor.fetchone() is None: |
||
| 976 | cursor.close() |
||
| 977 | cnx.close() |
||
| 978 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 979 | description='API.ENERGY_STORAGE_POWER_STATION_CONTAINER_RELATION_NOT_FOUND') |
||
| 980 | |||
| 981 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
||
| 982 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
||
| 983 | cnx.commit() |
||
| 984 | |||
| 985 | cursor.close() |
||
| 986 | cnx.close() |
||
| 987 | |||
| 988 | resp.status = falcon.HTTP_204 |
||
| 989 | |||
| 990 | |||
| 991 | View Code Duplication | class EnergyStoragePowerStationDataSourcePointCollection: |
|
| 992 | def __init__(self): |
||
| 993 | pass |
||
| 994 | |||
| 995 | @staticmethod |
||
| 996 | def on_options(req, resp, id_,): |
||
| 997 | _ = req |
||
| 998 | resp.status = falcon.HTTP_200 |
||
| 999 | _ = id_ |
||
| 1000 | |||
| 1001 | @staticmethod |
||
| 1002 | def on_get(req, resp, id_,): |
||
| 1003 | if 'API-KEY' not in req.headers or \ |
||
| 1004 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1005 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1006 | access_control(req) |
||
| 1007 | else: |
||
| 1008 | api_key_control(req) |
||
| 1009 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1011 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
| 1012 | |||
| 1013 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1014 | cursor = cnx.cursor() |
||
| 1015 | |||
| 1016 | query = (" SELECT p.id, p.name " |
||
| 1017 | " FROM tbl_points p, tbl_energy_storage_power_stations_containers espsc, " |
||
| 1018 | " tbl_energy_storage_containers_data_sources ecds, tbl_data_sources ds " |
||
| 1019 | " WHERE espsc.energy_storage_power_station_id = %s " |
||
| 1020 | " AND espsc.energy_storage_container_id = ecds.energy_storage_container_id " |
||
| 1021 | " AND ecds.data_source_id = ds.id " |
||
| 1022 | " AND p.data_source_id = ds.id " |
||
| 1023 | " ORDER BY p.id ") |
||
| 1024 | cursor.execute(query, (id_,)) |
||
| 1025 | rows = cursor.fetchall() |
||
| 1026 | |||
| 1027 | result = list() |
||
| 1028 | if rows is not None and len(rows) > 0: |
||
| 1029 | for row in rows: |
||
| 1030 | meta_result = {"id": row[0], "name": row[1]} |
||
| 1031 | result.append(meta_result) |
||
| 1032 | |||
| 1033 | resp.text = json.dumps(result) |
||
| 1034 | |||
| 1035 | |||
| 1036 | View Code Duplication | class EnergyStoragePowerStationUserCollection: |
|
| 1037 | def __init__(self): |
||
| 1038 | pass |
||
| 1039 | |||
| 1040 | @staticmethod |
||
| 1041 | def on_options(req, resp, id_): |
||
| 1042 | _ = req |
||
| 1043 | resp.status = falcon.HTTP_200 |
||
| 1044 | _ = id_ |
||
| 1045 | |||
| 1046 | @staticmethod |
||
| 1047 | def on_get(req, resp, id_): |
||
| 1048 | access_control(req) |
||
| 1049 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1051 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1052 | |||
| 1053 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1054 | cursor = cnx.cursor() |
||
| 1055 | cursor.execute(" SELECT name " |
||
| 1056 | " FROM tbl_energy_storage_power_stations " |
||
| 1057 | " WHERE id = %s ", (id_,)) |
||
| 1058 | if cursor.fetchone() is None: |
||
| 1059 | cursor.close() |
||
| 1060 | cnx.close() |
||
| 1061 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1062 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1063 | |||
| 1064 | query = (" SELECT u.id, u.name, u.uuid " |
||
| 1065 | " FROM tbl_energy_storage_power_stations m, tbl_energy_storage_power_stations_users mu, " |
||
| 1066 | + config.myems_user_db['database'] + ".tbl_users u " |
||
| 1067 | " WHERE mu.energy_storage_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
||
| 1068 | " ORDER BY u.id ") |
||
| 1069 | cursor.execute(query, (id_,)) |
||
| 1070 | rows = cursor.fetchall() |
||
| 1071 | result = list() |
||
| 1072 | if rows is not None and len(rows) > 0: |
||
| 1073 | for row in rows: |
||
| 1074 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1075 | result.append(meta_result) |
||
| 1076 | |||
| 1077 | cursor.close() |
||
| 1078 | cnx.close() |
||
| 1079 | resp.text = json.dumps(result) |
||
| 1080 | |||
| 1081 | @staticmethod |
||
| 1082 | @user_logger |
||
| 1083 | def on_post(req, resp, id_): |
||
| 1084 | """Handles POST requests""" |
||
| 1085 | admin_control(req) |
||
| 1086 | try: |
||
| 1087 | raw_json = req.stream.read().decode('utf-8') |
||
| 1088 | except UnicodeDecodeError as ex: |
||
| 1089 | print("Failed to decode request") |
||
| 1090 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1091 | title='API.BAD_REQUEST', |
||
| 1092 | description='API.INVALID_ENCODING') |
||
| 1093 | except Exception as ex: |
||
| 1094 | print("Unexpected error reading request stream") |
||
| 1095 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1096 | title='API.BAD_REQUEST', |
||
| 1097 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1098 | |||
| 1099 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1100 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1101 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1102 | |||
| 1103 | new_values = json.loads(raw_json) |
||
| 1104 | if 'user_id' not in new_values['data'].keys() or \ |
||
| 1105 | not isinstance(new_values['data']['user_id'], int) or \ |
||
| 1106 | new_values['data']['user_id'] <= 0: |
||
| 1107 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1108 | description='API.INVALID_USER_ID') |
||
| 1109 | user_id = new_values['data']['user_id'] |
||
| 1110 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1111 | cursor = cnx.cursor() |
||
| 1112 | cursor.execute(" SELECT name " |
||
| 1113 | " FROM tbl_energy_storage_power_stations " |
||
| 1114 | " WHERE id = %s ", (id_,)) |
||
| 1115 | if cursor.fetchone() is None: |
||
| 1116 | cursor.close() |
||
| 1117 | cnx.close() |
||
| 1118 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1119 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1120 | |||
| 1121 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 1122 | cursor_user = cnx_user.cursor() |
||
| 1123 | cursor_user.execute(" SELECT name" |
||
| 1124 | " FROM tbl_users " |
||
| 1125 | " WHERE id = %s ", (user_id,)) |
||
| 1126 | if cursor_user.fetchone() is None: |
||
| 1127 | cursor_user.close() |
||
| 1128 | cnx_user.close() |
||
| 1129 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1130 | description='API.USER_NOT_FOUND') |
||
| 1131 | query = (" SELECT id " |
||
| 1132 | " FROM tbl_energy_storage_power_stations_users " |
||
| 1133 | " WHERE energy_storage_power_station_id = %s AND user_id = %s") |
||
| 1134 | cursor.execute(query, (id_, user_id,)) |
||
| 1135 | if cursor.fetchone() is not None: |
||
| 1136 | cursor.close() |
||
| 1137 | cnx.close() |
||
| 1138 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1139 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_EXISTS') |
||
| 1140 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_users (energy_storage_power_station_id, user_id) " |
||
| 1141 | " VALUES (%s, %s) ") |
||
| 1142 | cursor.execute(add_row, (id_, user_id,)) |
||
| 1143 | cnx.commit() |
||
| 1144 | cursor.close() |
||
| 1145 | cnx.close() |
||
| 1146 | cursor_user.close() |
||
| 1147 | cnx_user.close() |
||
| 1148 | |||
| 1149 | resp.status = falcon.HTTP_201 |
||
| 1150 | resp.location = '/energystoragepowerstations/' + str(id_) + '/users/' + str(user_id) |
||
| 1151 | |||
| 1152 | |||
| 1153 | View Code Duplication | class EnergyStoragePowerStationUserItem: |
|
| 1154 | def __init__(self): |
||
| 1155 | pass |
||
| 1156 | |||
| 1157 | @staticmethod |
||
| 1158 | def on_options(req, resp, id_, uid): |
||
| 1159 | _ = req |
||
| 1160 | resp.status = falcon.HTTP_200 |
||
| 1161 | _ = id_ |
||
| 1162 | |||
| 1163 | @staticmethod |
||
| 1164 | @user_logger |
||
| 1165 | def on_delete(req, resp, id_, uid): |
||
| 1166 | # todo Verify if the user is bound when deleting it |
||
| 1167 | admin_control(req) |
||
| 1168 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1170 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1171 | |||
| 1172 | if not uid.isdigit() or int(uid) <= 0: |
||
| 1173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1174 | description='API.INVALID_USER_ID') |
||
| 1175 | |||
| 1176 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1177 | cursor = cnx.cursor() |
||
| 1178 | cursor.execute(" SELECT name " |
||
| 1179 | " FROM tbl_energy_storage_power_stations " |
||
| 1180 | " WHERE id = %s ", (id_,)) |
||
| 1181 | if cursor.fetchone() is None: |
||
| 1182 | cursor.close() |
||
| 1183 | cnx.close() |
||
| 1184 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1185 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1186 | |||
| 1187 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 1188 | cursor_user = cnx_user.cursor() |
||
| 1189 | cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,)) |
||
| 1190 | if cursor_user.fetchone() is None: |
||
| 1191 | cursor_user.close() |
||
| 1192 | cnx_user.close() |
||
| 1193 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1194 | description='API.USER_NOT_FOUND') |
||
| 1195 | |||
| 1196 | cursor.execute(" SELECT id " |
||
| 1197 | " FROM tbl_energy_storage_power_stations_users " |
||
| 1198 | " WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
||
| 1199 | if cursor.fetchone() is None: |
||
| 1200 | cursor.close() |
||
| 1201 | cnx.close() |
||
| 1202 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1203 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_NOT_FOUND') |
||
| 1204 | |||
| 1205 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_users " |
||
| 1206 | " WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
||
| 1207 | cnx.commit() |
||
| 1208 | |||
| 1209 | cursor.close() |
||
| 1210 | cnx.close() |
||
| 1211 | cursor_user.close() |
||
| 1212 | cnx_user.close() |
||
| 1213 | |||
| 1214 | resp.status = falcon.HTTP_204 |
||
| 1215 | |||
| 1216 | |||
| 1217 | class EnergyStoragePowerStationExport: |
||
| 1218 | def __init__(self): |
||
| 1219 | pass |
||
| 1220 | |||
| 1221 | @staticmethod |
||
| 1222 | def on_options(req, resp, id_): |
||
| 1223 | _ = req |
||
| 1224 | resp.status = falcon.HTTP_200 |
||
| 1225 | _ = id_ |
||
| 1226 | |||
| 1227 | @staticmethod |
||
| 1228 | def on_get(req, resp, id_): |
||
| 1229 | access_control(req) |
||
| 1230 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1231 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1232 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1233 | |||
| 1234 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1235 | cursor = cnx.cursor() |
||
| 1236 | |||
| 1237 | query = (" SELECT id, name, uuid " |
||
| 1238 | " FROM tbl_contacts ") |
||
| 1239 | cursor.execute(query) |
||
| 1240 | rows_contacts = cursor.fetchall() |
||
| 1241 | |||
| 1242 | contact_dict = dict() |
||
| 1243 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 1244 | for row in rows_contacts: |
||
| 1245 | contact_dict[row[0]] = {"id": row[0], |
||
| 1246 | "name": row[1], |
||
| 1247 | "uuid": row[2]} |
||
| 1248 | |||
| 1249 | query = (" SELECT id, name, uuid " |
||
| 1250 | " FROM tbl_cost_centers ") |
||
| 1251 | cursor.execute(query) |
||
| 1252 | rows_cost_centers = cursor.fetchall() |
||
| 1253 | |||
| 1254 | cost_center_dict = dict() |
||
| 1255 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 1256 | for row in rows_cost_centers: |
||
| 1257 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 1258 | "name": row[1], |
||
| 1259 | "uuid": row[2]} |
||
| 1260 | |||
| 1261 | query = (" SELECT id, name, uuid " |
||
| 1262 | " FROM tbl_svgs ") |
||
| 1263 | cursor.execute(query) |
||
| 1264 | rows_svgs = cursor.fetchall() |
||
| 1265 | |||
| 1266 | svg_dict = dict() |
||
| 1267 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 1268 | for row in rows_svgs: |
||
| 1269 | svg_dict[row[0]] = {"id": row[0], |
||
| 1270 | "name": row[1], |
||
| 1271 | "uuid": row[2]} |
||
| 1272 | |||
| 1273 | query = (" SELECT id, name, uuid, " |
||
| 1274 | " address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
||
| 1275 | " rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
| 1276 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
| 1277 | " FROM tbl_energy_storage_power_stations " |
||
| 1278 | " WHERE id = %s ") |
||
| 1279 | cursor.execute(query, (id_,)) |
||
| 1280 | row = cursor.fetchone() |
||
| 1281 | |||
| 1282 | if row is None: |
||
| 1283 | cursor.close() |
||
| 1284 | cnx.close() |
||
| 1285 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1286 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1287 | else: |
||
| 1288 | meta_result = {"id": row[0], |
||
| 1289 | "name": row[1], |
||
| 1290 | "uuid": row[2], |
||
| 1291 | "address": row[3], |
||
| 1292 | "latitude": row[4], |
||
| 1293 | "longitude": row[5], |
||
| 1294 | "latitude_point_id": row[6], |
||
| 1295 | "longitude_point_id": row[7], |
||
| 1296 | "rated_capacity": row[8], |
||
| 1297 | "rated_power": row[9], |
||
| 1298 | "contact": contact_dict.get(row[10], None), |
||
| 1299 | "cost_center": cost_center_dict.get(row[11], None), |
||
| 1300 | "svg": svg_dict.get(row[12], None), |
||
| 1301 | "svg2": svg_dict.get(row[13], None), |
||
| 1302 | "svg3": svg_dict.get(row[14], None), |
||
| 1303 | "svg4": svg_dict.get(row[15], None), |
||
| 1304 | "svg5": svg_dict.get(row[16], None), |
||
| 1305 | "is_cost_data_displayed": bool(row[17]), |
||
| 1306 | "phase_of_lifecycle": row[18], |
||
| 1307 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
| 1308 | "description": row[20]} |
||
| 1309 | |||
| 1310 | resp.text = json.dumps(meta_result) |
||
| 1311 | |||
| 1312 | |||
| 1313 | class EnergyStoragePowerStationImport: |
||
| 1314 | def __init__(self): |
||
| 1315 | pass |
||
| 1316 | |||
| 1317 | @staticmethod |
||
| 1318 | def on_options(req, resp): |
||
| 1319 | _ = req |
||
| 1320 | resp.status = falcon.HTTP_200 |
||
| 1321 | |||
| 1322 | @staticmethod |
||
| 1323 | @user_logger |
||
| 1324 | def on_post(req, resp): |
||
| 1325 | """Handles POST requests""" |
||
| 1326 | admin_control(req) |
||
| 1327 | try: |
||
| 1328 | raw_json = req.stream.read().decode('utf-8') |
||
| 1329 | except UnicodeDecodeError as ex: |
||
| 1330 | print("Failed to decode request") |
||
| 1331 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1332 | title='API.BAD_REQUEST', |
||
| 1333 | description='API.INVALID_ENCODING') |
||
| 1334 | except Exception as ex: |
||
| 1335 | print("Unexpected error reading request stream") |
||
| 1336 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1337 | title='API.BAD_REQUEST', |
||
| 1338 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1339 | |||
| 1340 | new_values = json.loads(raw_json) |
||
| 1341 | |||
| 1342 | print(new_values) |
||
| 1343 | if 'name' not in new_values.keys() or \ |
||
| 1344 | not isinstance(new_values['name'], str) or \ |
||
| 1345 | len(str.strip(new_values['name'])) == 0: |
||
| 1346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1347 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
| 1348 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 1349 | if config.utc_offset[0] == '-': |
||
| 1350 | timezone_offset = -timezone_offset |
||
| 1351 | name = str.strip(new_values['name']) + \ |
||
| 1352 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
| 1353 | |||
| 1354 | if 'address' not in new_values.keys() or \ |
||
| 1355 | not isinstance(new_values['address'], str) or \ |
||
| 1356 | len(str.strip(new_values['address'])) == 0: |
||
| 1357 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1358 | description='API.INVALID_ADDRESS_VALUE') |
||
| 1359 | address = str.strip(new_values['address']) |
||
| 1360 | |||
| 1361 | if 'latitude' not in new_values.keys() or \ |
||
| 1362 | not (isinstance(new_values['latitude'], float) or |
||
| 1363 | isinstance(new_values['latitude'], int)) or \ |
||
| 1364 | new_values['latitude'] < -90.0 or \ |
||
| 1365 | new_values['latitude'] > 90.0: |
||
| 1366 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1367 | description='API.INVALID_LATITUDE_VALUE') |
||
| 1368 | latitude = new_values['latitude'] |
||
| 1369 | |||
| 1370 | if 'longitude' not in new_values.keys() or \ |
||
| 1371 | not (isinstance(new_values['longitude'], float) or |
||
| 1372 | isinstance(new_values['longitude'], int)) or \ |
||
| 1373 | new_values['longitude'] < -180.0 or \ |
||
| 1374 | new_values['longitude'] > 180.0: |
||
| 1375 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1376 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 1377 | longitude = new_values['longitude'] |
||
| 1378 | |||
| 1379 | latitude_point_id = None |
||
| 1380 | longitude_point_id = None |
||
| 1381 | |||
| 1382 | if 'rated_capacity' not in new_values.keys() or \ |
||
| 1383 | not (isinstance(new_values['rated_capacity'], float) or |
||
| 1384 | isinstance(new_values['rated_capacity'], int)) or \ |
||
| 1385 | new_values['rated_capacity'] < 0.0: |
||
| 1386 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1387 | description='API.INVALID_RATED_CAPACITY') |
||
| 1388 | rated_capacity = new_values['rated_capacity'] |
||
| 1389 | |||
| 1390 | if 'rated_power' not in new_values.keys() or \ |
||
| 1391 | not (isinstance(new_values['rated_power'], float) or |
||
| 1392 | isinstance(new_values['rated_power'], int)) or \ |
||
| 1393 | new_values['rated_power'] < 0.0: |
||
| 1394 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1395 | description='API.INVALID_RATED_POWER') |
||
| 1396 | rated_power = new_values['rated_power'] |
||
| 1397 | |||
| 1398 | if 'contact' not in new_values.keys() or \ |
||
| 1399 | 'id' not in new_values['contact'].keys() or \ |
||
| 1400 | not isinstance(new_values['contact']['id'], int) or \ |
||
| 1401 | new_values['contact']['id'] <= 0: |
||
| 1402 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1403 | description='API.INVALID_CONTACT_ID') |
||
| 1404 | contact_id = new_values['contact']['id'] |
||
| 1405 | |||
| 1406 | if 'cost_center' not in new_values.keys() or \ |
||
| 1407 | 'id' not in new_values['cost_center'].keys() or \ |
||
| 1408 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 1409 | new_values['cost_center']['id'] <= 0: |
||
| 1410 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1411 | description='API.INVALID_COST_CENTER_ID') |
||
| 1412 | cost_center_id = new_values['cost_center']['id'] |
||
| 1413 | |||
| 1414 | if 'svg' not in new_values.keys() or \ |
||
| 1415 | 'id' not in new_values['svg'].keys() or \ |
||
| 1416 | not isinstance(new_values['svg']['id'], int) or \ |
||
| 1417 | new_values['svg']['id'] <= 0: |
||
| 1418 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1419 | description='API.INVALID_SVG_ID') |
||
| 1420 | svg_id = new_values['svg']['id'] |
||
| 1421 | |||
| 1422 | View Code Duplication | if 'svg2' in new_values.keys() and \ |
|
| 1423 | new_values['svg2'] is not None and \ |
||
| 1424 | 'id' in new_values['svg2'].keys() and \ |
||
| 1425 | new_values['svg2']['id'] is not None: |
||
| 1426 | if not isinstance(new_values['svg2']['id'], int) or \ |
||
| 1427 | new_values['svg2']['id'] <= 0: |
||
| 1428 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1429 | description='API.INVALID_SVG2_ID') |
||
| 1430 | svg2_id = new_values['svg2']['id'] |
||
| 1431 | else: |
||
| 1432 | svg2_id = None |
||
| 1433 | |||
| 1434 | View Code Duplication | if 'svg3' in new_values.keys() and \ |
|
| 1435 | new_values['svg3'] is not None and \ |
||
| 1436 | 'id' in new_values['svg3'].keys() and \ |
||
| 1437 | new_values['svg3']['id'] is not None: |
||
| 1438 | if not isinstance(new_values['svg3']['id'], int) or \ |
||
| 1439 | new_values['svg3']['id'] <= 0: |
||
| 1440 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1441 | description='API.INVALID_SVG3_ID') |
||
| 1442 | svg3_id = new_values['svg3']['id'] |
||
| 1443 | else: |
||
| 1444 | svg3_id = None |
||
| 1445 | |||
| 1446 | View Code Duplication | if 'svg4' in new_values.keys() and \ |
|
| 1447 | new_values['svg4'] is not None and \ |
||
| 1448 | 'id' in new_values['svg4'].keys() and \ |
||
| 1449 | new_values['svg4']['id'] is not None: |
||
| 1450 | if not isinstance(new_values['svg4']['id'], int) or \ |
||
| 1451 | new_values['svg4']['id'] <= 0: |
||
| 1452 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1453 | description='API.INVALID_SVG4_ID') |
||
| 1454 | svg4_id = new_values['svg4']['id'] |
||
| 1455 | else: |
||
| 1456 | svg4_id = None |
||
| 1457 | |||
| 1458 | View Code Duplication | if 'svg5' in new_values.keys() and \ |
|
| 1459 | new_values['svg5'] is not None and \ |
||
| 1460 | 'id' in new_values['svg5'].keys() and \ |
||
| 1461 | new_values['svg5']['id'] is not None: |
||
| 1462 | if not isinstance(new_values['svg5']['id'], int) or \ |
||
| 1463 | new_values['svg5']['id'] <= 0: |
||
| 1464 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1465 | description='API.INVALID_SVG5_ID') |
||
| 1466 | svg5_id = new_values['svg5']['id'] |
||
| 1467 | else: |
||
| 1468 | svg5_id = None |
||
| 1469 | |||
| 1470 | if 'is_cost_data_displayed' not in new_values.keys() or \ |
||
| 1471 | not isinstance(new_values['is_cost_data_displayed'], bool): |
||
| 1472 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1473 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
| 1474 | is_cost_data_displayed = new_values['is_cost_data_displayed'] |
||
| 1475 | |||
| 1476 | if 'phase_of_lifecycle' not in new_values.keys() or \ |
||
| 1477 | not isinstance(new_values['phase_of_lifecycle'], str) or \ |
||
| 1478 | len(str.strip(new_values['phase_of_lifecycle'])) == 0: |
||
| 1479 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1480 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
| 1481 | phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle']) |
||
| 1482 | |||
| 1483 | if 'commissioning_date' not in new_values.keys() or \ |
||
| 1484 | not isinstance(new_values['commissioning_date'], str) or \ |
||
| 1485 | len(str.strip(new_values['commissioning_date'])) == 0: |
||
| 1486 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1487 | description='API.INVALID_COMMISSIONING_DATE') |
||
| 1488 | commissioning_date = datetime.strptime(new_values['commissioning_date'], '%Y-%m-%d') |
||
| 1489 | |||
| 1490 | if 'description' in new_values.keys() and \ |
||
| 1491 | new_values['description'] is not None and \ |
||
| 1492 | len(str(new_values['description'])) > 0: |
||
| 1493 | description = str.strip(new_values['description']) |
||
| 1494 | else: |
||
| 1495 | description = None |
||
| 1496 | |||
| 1497 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1498 | cursor = cnx.cursor() |
||
| 1499 | |||
| 1500 | cursor.execute(" SELECT name " |
||
| 1501 | " FROM tbl_energy_storage_power_stations " |
||
| 1502 | " WHERE name = %s ", (name,)) |
||
| 1503 | if cursor.fetchone() is not None: |
||
| 1504 | cursor.close() |
||
| 1505 | cnx.close() |
||
| 1506 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1507 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
| 1508 | |||
| 1509 | cursor.execute(" SELECT name " |
||
| 1510 | " FROM tbl_contacts " |
||
| 1511 | " WHERE id = %s ", |
||
| 1512 | (contact_id,)) |
||
| 1513 | row = cursor.fetchone() |
||
| 1514 | if row is None: |
||
| 1515 | cursor.close() |
||
| 1516 | cnx.close() |
||
| 1517 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1518 | description='API.CONTACT_NOT_FOUND') |
||
| 1519 | |||
| 1520 | cursor.execute(" SELECT name " |
||
| 1521 | " FROM tbl_cost_centers " |
||
| 1522 | " WHERE id = %s ", |
||
| 1523 | (cost_center_id,)) |
||
| 1524 | row = cursor.fetchone() |
||
| 1525 | if row is None: |
||
| 1526 | cursor.close() |
||
| 1527 | cnx.close() |
||
| 1528 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1529 | description='API.COST_CENTER_NOT_FOUND') |
||
| 1530 | |||
| 1531 | cursor.execute(" SELECT name " |
||
| 1532 | " FROM tbl_svgs " |
||
| 1533 | " WHERE id = %s ", |
||
| 1534 | (svg_id,)) |
||
| 1535 | row = cursor.fetchone() |
||
| 1536 | if row is None: |
||
| 1537 | cursor.close() |
||
| 1538 | cnx.close() |
||
| 1539 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1540 | description='API.SVG_NOT_FOUND') |
||
| 1541 | |||
| 1542 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
| 1543 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
| 1544 | " rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
||
| 1545 | " svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
||
| 1546 | " description) " |
||
| 1547 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 1548 | |||
| 1549 | cursor.execute(add_values, (name, |
||
| 1550 | str(uuid.uuid4()), |
||
| 1551 | address, |
||
| 1552 | latitude, |
||
| 1553 | longitude, |
||
| 1554 | latitude_point_id, |
||
| 1555 | longitude_point_id, |
||
| 1556 | rated_capacity, |
||
| 1557 | rated_power, |
||
| 1558 | contact_id, |
||
| 1559 | cost_center_id, |
||
| 1560 | svg_id, |
||
| 1561 | svg2_id, |
||
| 1562 | svg3_id, |
||
| 1563 | svg4_id, |
||
| 1564 | svg5_id, |
||
| 1565 | is_cost_data_displayed, |
||
| 1566 | phase_of_lifecycle, |
||
| 1567 | commissioning_date, |
||
| 1568 | description)) |
||
| 1569 | |||
| 1570 | new_id = cursor.lastrowid |
||
| 1571 | cnx.commit() |
||
| 1572 | cursor.close() |
||
| 1573 | cnx.close() |
||
| 1574 | |||
| 1575 | resp.status = falcon.HTTP_201 |
||
| 1576 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
| 1577 | |||
| 1578 | |||
| 1579 | class EnergyStoragePowerStationClone: |
||
| 1580 | def __init__(self): |
||
| 1581 | pass |
||
| 1582 | |||
| 1583 | @staticmethod |
||
| 1584 | def on_options(req, resp, id_): |
||
| 1585 | _ = req |
||
| 1586 | resp.status = falcon.HTTP_200 |
||
| 1587 | _ = id_ |
||
| 1588 | |||
| 1589 | @staticmethod |
||
| 1590 | @user_logger |
||
| 1591 | def on_post(req, resp, id_): |
||
| 1592 | """Handles POST requests""" |
||
| 1593 | admin_control(req) |
||
| 1594 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1596 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1597 | |||
| 1598 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1599 | cursor = cnx.cursor() |
||
| 1600 | |||
| 1601 | query = (" SELECT id, name, uuid, " |
||
| 1602 | " address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
||
| 1603 | " rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
| 1604 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
| 1605 | " FROM tbl_energy_storage_power_stations " |
||
| 1606 | " WHERE id = %s ") |
||
| 1607 | cursor.execute(query, (id_,)) |
||
| 1608 | row = cursor.fetchone() |
||
| 1609 | |||
| 1610 | if row is None: |
||
| 1611 | cursor.close() |
||
| 1612 | cnx.close() |
||
| 1613 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1614 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1615 | else: |
||
| 1616 | meta_result = {"name": row[1], |
||
| 1617 | "uuid": row[2], |
||
| 1618 | "address": row[3], |
||
| 1619 | "latitude": row[4], |
||
| 1620 | "longitude": row[5], |
||
| 1621 | "latitude_point_id": row[6], |
||
| 1622 | "longitude_point_id": row[7], |
||
| 1623 | "rated_capacity": row[8], |
||
| 1624 | "rated_power": row[9], |
||
| 1625 | "contact_id": row[10], |
||
| 1626 | "cost_center_id": row[11], |
||
| 1627 | "svg_id": row[12], |
||
| 1628 | "svg2_id": row[13], |
||
| 1629 | "svg3_id": row[14], |
||
| 1630 | "svg4_id": row[15], |
||
| 1631 | "svg5_id": row[16], |
||
| 1632 | "is_cost_data_displayed": row[17], |
||
| 1633 | "phase_of_lifecycle": row[18], |
||
| 1634 | "commissioning_date": row[19], |
||
| 1635 | "description": row[20]} |
||
| 1636 | |||
| 1637 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 1638 | if config.utc_offset[0] == '-': |
||
| 1639 | timezone_offset = -timezone_offset |
||
| 1640 | new_name = str.strip(meta_result['name']) + \ |
||
| 1641 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
| 1642 | |||
| 1643 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
| 1644 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
| 1645 | " rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
||
| 1646 | " svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
||
| 1647 | " description) " |
||
| 1648 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 1649 | cursor.execute(add_values, (new_name, |
||
| 1650 | str(uuid.uuid4()), |
||
| 1651 | meta_result['address'], |
||
| 1652 | meta_result['latitude'], |
||
| 1653 | meta_result['longitude'], |
||
| 1654 | meta_result['latitude_point_id'], |
||
| 1655 | meta_result['longitude_point_id'], |
||
| 1656 | meta_result['rated_capacity'], |
||
| 1657 | meta_result['rated_power'], |
||
| 1658 | meta_result['contact_id'], |
||
| 1659 | meta_result['cost_center_id'], |
||
| 1660 | meta_result['svg_id'], |
||
| 1661 | meta_result['svg2_id'], |
||
| 1662 | meta_result['svg3_id'], |
||
| 1663 | meta_result['svg4_id'], |
||
| 1664 | meta_result['svg5_id'], |
||
| 1665 | meta_result['is_cost_data_displayed'], |
||
| 1666 | meta_result['phase_of_lifecycle'], |
||
| 1667 | meta_result['commissioning_date'], |
||
| 1668 | meta_result['description'])) |
||
| 1669 | new_id = cursor.lastrowid |
||
| 1670 | cnx.commit() |
||
| 1671 | cursor.close() |
||
| 1672 | cnx.close() |
||
| 1673 | |||
| 1674 | resp.status = falcon.HTTP_201 |
||
| 1675 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
| 1676 |