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