| Total Complexity | 732 |
| Total Lines | 3593 |
| Duplicated Lines | 69.77 % |
| 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.combinedequipment 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 CombinedEquipmentCollection: |
||
| 11 | """ |
||
| 12 | Combined Equipment Collection Resource |
||
| 13 | |||
| 14 | This class handles CRUD operations for combined equipment collection. |
||
| 15 | It provides endpoints for listing all combined equipments and creating new ones. |
||
| 16 | Combined equipments represent groups of related equipment that work together |
||
| 17 | in the energy management system, such as HVAC systems, lighting groups, etc. |
||
| 18 | """ |
||
| 19 | |||
| 20 | def __init__(self): |
||
| 21 | pass |
||
| 22 | |||
| 23 | @staticmethod |
||
| 24 | def on_options(req, resp): |
||
| 25 | """ |
||
| 26 | Handle OPTIONS request for CORS preflight |
||
| 27 | |||
| 28 | Args: |
||
| 29 | req: Falcon request object |
||
| 30 | resp: Falcon response object |
||
| 31 | """ |
||
| 32 | _ = req |
||
| 33 | resp.status = falcon.HTTP_200 |
||
| 34 | |||
| 35 | View Code Duplication | @staticmethod |
|
|
|
|||
| 36 | def on_get(req, resp): |
||
| 37 | """ |
||
| 38 | Handle GET requests to retrieve all combined equipments |
||
| 39 | |||
| 40 | Returns a list of all combined equipments with their metadata including: |
||
| 41 | - Equipment ID, name, and UUID |
||
| 42 | - Input/output counting status |
||
| 43 | - Cost center information |
||
| 44 | - SVG diagram reference |
||
| 45 | - Camera URL for monitoring |
||
| 46 | - Description and QR code |
||
| 47 | |||
| 48 | Args: |
||
| 49 | req: Falcon request object |
||
| 50 | resp: Falcon response object |
||
| 51 | """ |
||
| 52 | # Check authentication method (API key or session) |
||
| 53 | if 'API-KEY' not in req.headers or \ |
||
| 54 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 55 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 56 | access_control(req) |
||
| 57 | else: |
||
| 58 | api_key_control(req) |
||
| 59 | |||
| 60 | search_query = req.get_param('q', default=None) |
||
| 61 | if search_query is not None: |
||
| 62 | search_query = search_query.strip() |
||
| 63 | else: |
||
| 64 | search_query = '' |
||
| 65 | |||
| 66 | # Connect to database and retrieve cost centers |
||
| 67 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 68 | cursor = cnx.cursor() |
||
| 69 | |||
| 70 | # Query to retrieve all cost centers for reference |
||
| 71 | query = (" SELECT id, name, uuid " |
||
| 72 | " FROM tbl_cost_centers ") |
||
| 73 | cursor.execute(query) |
||
| 74 | rows_cost_centers = cursor.fetchall() |
||
| 75 | |||
| 76 | # Build cost center dictionary for quick lookup |
||
| 77 | cost_center_dict = dict() |
||
| 78 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 79 | for row in rows_cost_centers: |
||
| 80 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 81 | "name": row[1], |
||
| 82 | "uuid": row[2]} |
||
| 83 | |||
| 84 | # Query to retrieve all SVG diagrams for reference |
||
| 85 | query = (" SELECT id, name, uuid " |
||
| 86 | " FROM tbl_svgs ") |
||
| 87 | cursor.execute(query) |
||
| 88 | rows_svgs = cursor.fetchall() |
||
| 89 | |||
| 90 | # Build SVG dictionary for quick lookup |
||
| 91 | svg_dict = dict() |
||
| 92 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 93 | for row in rows_svgs: |
||
| 94 | svg_dict[row[0]] = {"id": row[0], |
||
| 95 | "name": row[1], |
||
| 96 | "uuid": row[2]} |
||
| 97 | |||
| 98 | # Query to retrieve all combined equipments |
||
| 99 | query = (" SELECT id, name, uuid, " |
||
| 100 | " is_input_counted, is_output_counted, " |
||
| 101 | " cost_center_id, svg_id, camera_url, description " |
||
| 102 | " FROM tbl_combined_equipments ") |
||
| 103 | params = [] |
||
| 104 | if search_query: |
||
| 105 | query += " WHERE name LIKE %s OR description LIKE %s " |
||
| 106 | params = [f'%{search_query}%', f'%{search_query}%'] |
||
| 107 | query += " ORDER BY id " |
||
| 108 | cursor.execute(query, params) |
||
| 109 | rows_combined_equipments = cursor.fetchall() |
||
| 110 | |||
| 111 | # Build result list with all combined equipment data |
||
| 112 | result = list() |
||
| 113 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 114 | for row in rows_combined_equipments: |
||
| 115 | meta_result = {"id": row[0], |
||
| 116 | "name": row[1], |
||
| 117 | "uuid": row[2], |
||
| 118 | "is_input_counted": bool(row[3]), |
||
| 119 | "is_output_counted": bool(row[4]), |
||
| 120 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 121 | "svg": svg_dict.get(row[6], None), |
||
| 122 | "camera_url": row[7], |
||
| 123 | "description": row[8], |
||
| 124 | "qrcode": 'combinedequipment:' + row[2]} |
||
| 125 | result.append(meta_result) |
||
| 126 | |||
| 127 | cursor.close() |
||
| 128 | cnx.close() |
||
| 129 | resp.text = json.dumps(result) |
||
| 130 | |||
| 131 | View Code Duplication | @staticmethod |
|
| 132 | @user_logger |
||
| 133 | def on_post(req, resp): |
||
| 134 | """Handles POST requests""" |
||
| 135 | admin_control(req) |
||
| 136 | try: |
||
| 137 | raw_json = req.stream.read().decode('utf-8') |
||
| 138 | except Exception as ex: |
||
| 139 | print(ex) |
||
| 140 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 141 | title='API.BAD_REQUEST', |
||
| 142 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 143 | |||
| 144 | new_values = json.loads(raw_json) |
||
| 145 | |||
| 146 | if 'name' not in new_values['data'].keys() or \ |
||
| 147 | not isinstance(new_values['data']['name'], str) or \ |
||
| 148 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 150 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 151 | name = str.strip(new_values['data']['name']) |
||
| 152 | |||
| 153 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 154 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 156 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 157 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 158 | |||
| 159 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 160 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 161 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 162 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 163 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 164 | |||
| 165 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 166 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 167 | new_values['data']['cost_center_id'] <= 0: |
||
| 168 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 169 | description='API.INVALID_COST_CENTER_ID') |
||
| 170 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 171 | |||
| 172 | if 'svg_id' in new_values['data'].keys() and \ |
||
| 173 | isinstance(new_values['data']['svg_id'], int) and \ |
||
| 174 | new_values['data']['svg_id'] > 0: |
||
| 175 | svg_id = new_values['data']['svg_id'] |
||
| 176 | else: |
||
| 177 | svg_id = None |
||
| 178 | |||
| 179 | if 'camera_url' in new_values['data'].keys() and \ |
||
| 180 | new_values['data']['camera_url'] is not None and \ |
||
| 181 | len(str(new_values['data']['camera_url'])) > 0: |
||
| 182 | camera_url = str.strip(new_values['data']['camera_url']) |
||
| 183 | else: |
||
| 184 | camera_url = None |
||
| 185 | |||
| 186 | if 'description' in new_values['data'].keys() and \ |
||
| 187 | new_values['data']['description'] is not None and \ |
||
| 188 | len(str(new_values['data']['description'])) > 0: |
||
| 189 | description = str.strip(new_values['data']['description']) |
||
| 190 | else: |
||
| 191 | description = None |
||
| 192 | |||
| 193 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 194 | cursor = cnx.cursor() |
||
| 195 | |||
| 196 | cursor.execute(" SELECT name " |
||
| 197 | " FROM tbl_combined_equipments " |
||
| 198 | " WHERE name = %s ", (name,)) |
||
| 199 | if cursor.fetchone() is not None: |
||
| 200 | cursor.close() |
||
| 201 | cnx.close() |
||
| 202 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 203 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 204 | |||
| 205 | if cost_center_id is not None: |
||
| 206 | cursor.execute(" SELECT name " |
||
| 207 | " FROM tbl_cost_centers " |
||
| 208 | " WHERE id = %s ", |
||
| 209 | (new_values['data']['cost_center_id'],)) |
||
| 210 | row = cursor.fetchone() |
||
| 211 | if row is None: |
||
| 212 | cursor.close() |
||
| 213 | cnx.close() |
||
| 214 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 215 | description='API.COST_CENTER_NOT_FOUND') |
||
| 216 | |||
| 217 | if svg_id is not None: |
||
| 218 | cursor.execute(" SELECT name " |
||
| 219 | " FROM tbl_svgs " |
||
| 220 | " WHERE id = %s ", |
||
| 221 | (svg_id,)) |
||
| 222 | row = cursor.fetchone() |
||
| 223 | if row is None: |
||
| 224 | cursor.close() |
||
| 225 | cnx.close() |
||
| 226 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 227 | description='API.SVG_NOT_FOUND') |
||
| 228 | |||
| 229 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 230 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 231 | " cost_center_id, svg_id, camera_url, description) " |
||
| 232 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 233 | cursor.execute(add_values, (name, |
||
| 234 | str(uuid.uuid4()), |
||
| 235 | is_input_counted, |
||
| 236 | is_output_counted, |
||
| 237 | cost_center_id, |
||
| 238 | svg_id, |
||
| 239 | camera_url, |
||
| 240 | description)) |
||
| 241 | new_id = cursor.lastrowid |
||
| 242 | cnx.commit() |
||
| 243 | cursor.close() |
||
| 244 | cnx.close() |
||
| 245 | |||
| 246 | resp.status = falcon.HTTP_201 |
||
| 247 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 248 | |||
| 249 | |||
| 250 | class CombinedEquipmentItem: |
||
| 251 | def __init__(self): |
||
| 252 | pass |
||
| 253 | |||
| 254 | @staticmethod |
||
| 255 | def on_options(req, resp, id_): |
||
| 256 | _ = req |
||
| 257 | resp.status = falcon.HTTP_200 |
||
| 258 | _ = id_ |
||
| 259 | |||
| 260 | View Code Duplication | @staticmethod |
|
| 261 | def on_get(req, resp, id_): |
||
| 262 | if 'API-KEY' not in req.headers or \ |
||
| 263 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 264 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 265 | access_control(req) |
||
| 266 | else: |
||
| 267 | api_key_control(req) |
||
| 268 | if not id_.isdigit() or int(id_) <= 0: |
||
| 269 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 270 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 271 | |||
| 272 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 273 | cursor = cnx.cursor() |
||
| 274 | |||
| 275 | query = (" SELECT id, name, uuid " |
||
| 276 | " FROM tbl_cost_centers ") |
||
| 277 | cursor.execute(query) |
||
| 278 | rows_cost_centers = cursor.fetchall() |
||
| 279 | |||
| 280 | cost_center_dict = dict() |
||
| 281 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 282 | for row in rows_cost_centers: |
||
| 283 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 284 | "name": row[1], |
||
| 285 | "uuid": row[2]} |
||
| 286 | |||
| 287 | svg_dict = dict() |
||
| 288 | query = (" SELECT id, name, uuid " |
||
| 289 | " FROM tbl_svgs ") |
||
| 290 | cursor.execute(query) |
||
| 291 | rows_svgs = cursor.fetchall() |
||
| 292 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 293 | for row in rows_svgs: |
||
| 294 | svg_dict[row[0]] = {"id": row[0], |
||
| 295 | "name": row[1], |
||
| 296 | "uuid": row[2]} |
||
| 297 | |||
| 298 | query = (" SELECT id, name, uuid, " |
||
| 299 | " is_input_counted, is_output_counted, " |
||
| 300 | " cost_center_id, svg_id, camera_url, description " |
||
| 301 | " FROM tbl_combined_equipments " |
||
| 302 | " WHERE id = %s ") |
||
| 303 | cursor.execute(query, (id_,)) |
||
| 304 | row = cursor.fetchone() |
||
| 305 | cursor.close() |
||
| 306 | cnx.close() |
||
| 307 | |||
| 308 | if row is None: |
||
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 310 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 311 | else: |
||
| 312 | meta_result = {"id": row[0], |
||
| 313 | "name": row[1], |
||
| 314 | "uuid": row[2], |
||
| 315 | "is_input_counted": bool(row[3]), |
||
| 316 | "is_output_counted": bool(row[4]), |
||
| 317 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 318 | "svg": svg_dict.get(row[6], None), |
||
| 319 | "camera_url": row[7], |
||
| 320 | "description": row[8], |
||
| 321 | "qrcode": 'combinedequipment:' + row[2]} |
||
| 322 | |||
| 323 | resp.text = json.dumps(meta_result) |
||
| 324 | |||
| 325 | @staticmethod |
||
| 326 | @user_logger |
||
| 327 | def on_delete(req, resp, id_): |
||
| 328 | admin_control(req) |
||
| 329 | if not id_.isdigit() or int(id_) <= 0: |
||
| 330 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 331 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 332 | |||
| 333 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 334 | cursor = cnx.cursor() |
||
| 335 | |||
| 336 | # check relation with space |
||
| 337 | cursor.execute(" SELECT space_id " |
||
| 338 | " FROM tbl_spaces_combined_equipments " |
||
| 339 | " WHERE combined_equipment_id = %s ", |
||
| 340 | (id_,)) |
||
| 341 | rows_combined_equipments = cursor.fetchall() |
||
| 342 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 343 | cursor.close() |
||
| 344 | cnx.close() |
||
| 345 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 346 | title='API.BAD_REQUEST', |
||
| 347 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 348 | |||
| 349 | # delete relation with commands |
||
| 350 | cursor.execute(" DELETE FROM tbl_combined_equipments_commands WHERE combined_equipment_id = %s ", (id_,)) |
||
| 351 | |||
| 352 | # delete relation with equipments |
||
| 353 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments WHERE combined_equipment_id = %s ", (id_,)) |
||
| 354 | |||
| 355 | # delete relation with meters |
||
| 356 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
| 357 | |||
| 358 | # delete relation with offline meters |
||
| 359 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
| 360 | |||
| 361 | # delete all associated parameters |
||
| 362 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters WHERE combined_equipment_id = %s ", (id_,)) |
||
| 363 | |||
| 364 | # delete relation with virtual meter |
||
| 365 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters WHERE combined_equipment_id = %s ", (id_,)) |
||
| 366 | cnx.commit() |
||
| 367 | |||
| 368 | cursor.execute(" DELETE FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 369 | cnx.commit() |
||
| 370 | |||
| 371 | cursor.close() |
||
| 372 | cnx.close() |
||
| 373 | |||
| 374 | resp.status = falcon.HTTP_204 |
||
| 375 | |||
| 376 | View Code Duplication | @staticmethod |
|
| 377 | @user_logger |
||
| 378 | def on_put(req, resp, id_): |
||
| 379 | """Handles PUT requests""" |
||
| 380 | admin_control(req) |
||
| 381 | if not id_.isdigit() or int(id_) <= 0: |
||
| 382 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 383 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 384 | try: |
||
| 385 | raw_json = req.stream.read().decode('utf-8') |
||
| 386 | except Exception as ex: |
||
| 387 | print(ex) |
||
| 388 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 389 | title='API.BAD_REQUEST', |
||
| 390 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 391 | |||
| 392 | new_values = json.loads(raw_json) |
||
| 393 | |||
| 394 | if 'name' not in new_values['data'].keys() or \ |
||
| 395 | not isinstance(new_values['data']['name'], str) or \ |
||
| 396 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 397 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 398 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 399 | name = str.strip(new_values['data']['name']) |
||
| 400 | |||
| 401 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 402 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 403 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 404 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 405 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 406 | |||
| 407 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 408 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 409 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 410 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 411 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 412 | |||
| 413 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 414 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 415 | new_values['data']['cost_center_id'] <= 0: |
||
| 416 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 417 | description='API.INVALID_COST_CENTER_ID') |
||
| 418 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 419 | |||
| 420 | if 'svg_id' in new_values['data'].keys() and \ |
||
| 421 | isinstance(new_values['data']['svg_id'], int) and \ |
||
| 422 | new_values['data']['svg_id'] > 0: |
||
| 423 | svg_id = new_values['data']['svg_id'] |
||
| 424 | else: |
||
| 425 | svg_id = None |
||
| 426 | |||
| 427 | if 'camera_url' in new_values['data'].keys() and \ |
||
| 428 | new_values['data']['camera_url'] is not None and \ |
||
| 429 | len(str(new_values['data']['camera_url'])) > 0: |
||
| 430 | camera_url = str.strip(new_values['data']['camera_url']) |
||
| 431 | else: |
||
| 432 | camera_url = None |
||
| 433 | |||
| 434 | if 'description' in new_values['data'].keys() and \ |
||
| 435 | new_values['data']['description'] is not None and \ |
||
| 436 | len(str(new_values['data']['description'])) > 0: |
||
| 437 | description = str.strip(new_values['data']['description']) |
||
| 438 | else: |
||
| 439 | description = None |
||
| 440 | |||
| 441 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 442 | cursor = cnx.cursor() |
||
| 443 | |||
| 444 | cursor.execute(" SELECT name " |
||
| 445 | " FROM tbl_combined_equipments " |
||
| 446 | " WHERE id = %s ", (id_,)) |
||
| 447 | if cursor.fetchone() is None: |
||
| 448 | cursor.close() |
||
| 449 | cnx.close() |
||
| 450 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 451 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 452 | |||
| 453 | cursor.execute(" SELECT name " |
||
| 454 | " FROM tbl_combined_equipments " |
||
| 455 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 456 | if cursor.fetchone() is not None: |
||
| 457 | cursor.close() |
||
| 458 | cnx.close() |
||
| 459 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 460 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 461 | |||
| 462 | cursor.execute(" SELECT name " |
||
| 463 | " FROM tbl_cost_centers " |
||
| 464 | " WHERE id = %s ", |
||
| 465 | (new_values['data']['cost_center_id'],)) |
||
| 466 | row = cursor.fetchone() |
||
| 467 | if row is None: |
||
| 468 | cursor.close() |
||
| 469 | cnx.close() |
||
| 470 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 471 | description='API.COST_CENTER_NOT_FOUND') |
||
| 472 | |||
| 473 | if svg_id is not None: |
||
| 474 | cursor.execute(" SELECT name " |
||
| 475 | " FROM tbl_svgs " |
||
| 476 | " WHERE id = %s ", |
||
| 477 | (new_values['data']['svg_id'],)) |
||
| 478 | row = cursor.fetchone() |
||
| 479 | if row is None: |
||
| 480 | cursor.close() |
||
| 481 | cnx.close() |
||
| 482 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 483 | description='API.SVG_NOT_FOUND') |
||
| 484 | |||
| 485 | update_row = (" UPDATE tbl_combined_equipments " |
||
| 486 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
||
| 487 | " cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s " |
||
| 488 | " WHERE id = %s ") |
||
| 489 | cursor.execute(update_row, (name, |
||
| 490 | is_input_counted, |
||
| 491 | is_output_counted, |
||
| 492 | cost_center_id, |
||
| 493 | svg_id, |
||
| 494 | camera_url, |
||
| 495 | description, |
||
| 496 | id_)) |
||
| 497 | cnx.commit() |
||
| 498 | |||
| 499 | cursor.close() |
||
| 500 | cnx.close() |
||
| 501 | |||
| 502 | resp.status = falcon.HTTP_200 |
||
| 503 | |||
| 504 | # Clone a Combined Equipment |
||
| 505 | View Code Duplication | @staticmethod |
|
| 506 | @user_logger |
||
| 507 | def on_post(req, resp, id_): |
||
| 508 | """Handles POST requests""" |
||
| 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_COMBINED_EQUIPMENT_ID') |
||
| 513 | |||
| 514 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 515 | cursor = cnx.cursor() |
||
| 516 | cursor.execute(" SELECT name " |
||
| 517 | " FROM tbl_combined_equipments " |
||
| 518 | " WHERE id = %s ", (id_,)) |
||
| 519 | if cursor.fetchone() is None: |
||
| 520 | cursor.close() |
||
| 521 | cnx.close() |
||
| 522 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 523 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 524 | |||
| 525 | query = (" SELECT name, is_input_counted, is_output_counted, " |
||
| 526 | " cost_center_id, svg_id, camera_url, description " |
||
| 527 | " FROM tbl_combined_equipments " |
||
| 528 | " WHERE id = %s ") |
||
| 529 | cursor.execute(query, (id_,)) |
||
| 530 | row = cursor.fetchone() |
||
| 531 | |||
| 532 | if row is None: |
||
| 533 | cursor.close() |
||
| 534 | cnx.close() |
||
| 535 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 536 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 537 | else: |
||
| 538 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 539 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 540 | " cost_center_id, svg_id, camera_url, description) " |
||
| 541 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 542 | cursor.execute(add_values, (row[0] + ' Copy', |
||
| 543 | str(uuid.uuid4()), |
||
| 544 | row[1], |
||
| 545 | row[2], |
||
| 546 | row[3], |
||
| 547 | row[4], |
||
| 548 | row[5], |
||
| 549 | row[6])) |
||
| 550 | new_id = cursor.lastrowid |
||
| 551 | cnx.commit() |
||
| 552 | |||
| 553 | # clone relation with meter |
||
| 554 | cursor.execute(" SELECT meter_id, is_output " |
||
| 555 | " FROM tbl_combined_equipments_meters " |
||
| 556 | " WHERE combined_equipment_id = %s ", |
||
| 557 | (id_,)) |
||
| 558 | rows_meters = cursor.fetchall() |
||
| 559 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 560 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
||
| 561 | " VALUES ") |
||
| 562 | for row in rows_meters: |
||
| 563 | add_values += " (" + str(new_id) + "," |
||
| 564 | add_values += str(row[0]) + "," |
||
| 565 | add_values += str(bool(row[1])) + "), " |
||
| 566 | # trim ", " at the end of string and then execute |
||
| 567 | cursor.execute(add_values[:-2]) |
||
| 568 | cnx.commit() |
||
| 569 | |||
| 570 | # clone relation with offline meter |
||
| 571 | cursor.execute(" SELECT offline_meter_id, is_output " |
||
| 572 | " FROM tbl_combined_equipments_offline_meters " |
||
| 573 | " WHERE combined_equipment_id = %s ", |
||
| 574 | (id_,)) |
||
| 575 | rows_offline_meters = cursor.fetchall() |
||
| 576 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 577 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 578 | " (combined_equipment_id, offline_meter_id, is_output) " |
||
| 579 | " VALUES ") |
||
| 580 | for row in rows_offline_meters: |
||
| 581 | add_values += " (" + str(new_id) + "," |
||
| 582 | add_values += "'" + str(row[0]) + "'," |
||
| 583 | add_values += str(bool(row[1])) + "), " |
||
| 584 | # trim ", " at the end of string and then execute |
||
| 585 | cursor.execute(add_values[:-2]) |
||
| 586 | cnx.commit() |
||
| 587 | |||
| 588 | # clone relation with virtual meter |
||
| 589 | cursor.execute(" SELECT virtual_meter_id, is_output " |
||
| 590 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 591 | " WHERE combined_equipment_id = %s ", |
||
| 592 | (id_,)) |
||
| 593 | rows_virtual_meters = cursor.fetchall() |
||
| 594 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 595 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 596 | " (combined_equipment_id, virtual_meter_id, is_output) " |
||
| 597 | " VALUES ") |
||
| 598 | for row in rows_virtual_meters: |
||
| 599 | add_values += " (" + str(new_id) + "," |
||
| 600 | add_values += str(row[0]) + "," |
||
| 601 | add_values += str(bool(row[1])) + "), " |
||
| 602 | # trim ", " at the end of string and then execute |
||
| 603 | cursor.execute(add_values[:-2]) |
||
| 604 | cnx.commit() |
||
| 605 | |||
| 606 | # clone parameters |
||
| 607 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 608 | " FROM tbl_combined_equipments_parameters " |
||
| 609 | " WHERE combined_equipment_id = %s ", |
||
| 610 | (id_,)) |
||
| 611 | rows_parameters = cursor.fetchall() |
||
| 612 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 613 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
||
| 614 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
||
| 615 | " numerator_meter_uuid, denominator_meter_uuid) " |
||
| 616 | " VALUES ") |
||
| 617 | for row in rows_parameters: |
||
| 618 | add_values += " (" + str(new_id) + "," |
||
| 619 | add_values += "'" + str(row[0]) + "'," |
||
| 620 | add_values += "'" + str(row[1]) + "'," |
||
| 621 | if row[2] is not None: |
||
| 622 | add_values += "'" + str(row[2]) + "'," |
||
| 623 | else: |
||
| 624 | add_values += "null, " |
||
| 625 | |||
| 626 | if row[3] is not None: |
||
| 627 | add_values += str(row[2]) + "," |
||
| 628 | else: |
||
| 629 | add_values += "null, " |
||
| 630 | |||
| 631 | if row[4] is not None: |
||
| 632 | add_values += "'" + row[4] + "'," |
||
| 633 | else: |
||
| 634 | add_values += "null, " |
||
| 635 | if row[5] is not None: |
||
| 636 | add_values += "'" + row[5] + "'), " |
||
| 637 | else: |
||
| 638 | add_values += "null), " |
||
| 639 | |||
| 640 | # trim ", " at the end of string and then execute |
||
| 641 | cursor.execute(add_values[:-2]) |
||
| 642 | cnx.commit() |
||
| 643 | |||
| 644 | cursor.close() |
||
| 645 | cnx.close() |
||
| 646 | resp.status = falcon.HTTP_201 |
||
| 647 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 648 | |||
| 649 | |||
| 650 | View Code Duplication | class CombinedEquipmentEquipmentCollection: |
|
| 651 | def __init__(self): |
||
| 652 | pass |
||
| 653 | |||
| 654 | @staticmethod |
||
| 655 | def on_options(req, resp, id_): |
||
| 656 | _ = req |
||
| 657 | resp.status = falcon.HTTP_200 |
||
| 658 | _ = id_ |
||
| 659 | |||
| 660 | @staticmethod |
||
| 661 | def on_get(req, resp, id_): |
||
| 662 | if 'API-KEY' not in req.headers or \ |
||
| 663 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 664 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 665 | access_control(req) |
||
| 666 | else: |
||
| 667 | api_key_control(req) |
||
| 668 | if not id_.isdigit() or int(id_) <= 0: |
||
| 669 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 670 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 671 | |||
| 672 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 673 | cursor = cnx.cursor() |
||
| 674 | |||
| 675 | cursor.execute(" SELECT name " |
||
| 676 | " FROM tbl_combined_equipments " |
||
| 677 | " WHERE id = %s ", (id_,)) |
||
| 678 | if cursor.fetchone() is None: |
||
| 679 | cursor.close() |
||
| 680 | cnx.close() |
||
| 681 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 682 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 683 | |||
| 684 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 685 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 686 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 687 | " ORDER BY e.id ") |
||
| 688 | cursor.execute(query, (id_,)) |
||
| 689 | rows = cursor.fetchall() |
||
| 690 | cursor.close() |
||
| 691 | cnx.close() |
||
| 692 | |||
| 693 | result = list() |
||
| 694 | if rows is not None and len(rows) > 0: |
||
| 695 | for row in rows: |
||
| 696 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 697 | result.append(meta_result) |
||
| 698 | |||
| 699 | resp.text = json.dumps(result) |
||
| 700 | |||
| 701 | @staticmethod |
||
| 702 | @user_logger |
||
| 703 | def on_post(req, resp, id_): |
||
| 704 | """Handles POST requests""" |
||
| 705 | admin_control(req) |
||
| 706 | try: |
||
| 707 | raw_json = req.stream.read().decode('utf-8') |
||
| 708 | except Exception as ex: |
||
| 709 | print(ex) |
||
| 710 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 711 | title='API.BAD_REQUEST', |
||
| 712 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 713 | |||
| 714 | if not id_.isdigit() or int(id_) <= 0: |
||
| 715 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 716 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 717 | |||
| 718 | new_values = json.loads(raw_json) |
||
| 719 | |||
| 720 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
| 721 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
| 722 | new_values['data']['equipment_id'] <= 0: |
||
| 723 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 724 | description='API.INVALID_EQUIPMENT_ID') |
||
| 725 | equipment_id = new_values['data']['equipment_id'] |
||
| 726 | |||
| 727 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 728 | cursor = cnx.cursor() |
||
| 729 | |||
| 730 | cursor.execute(" SELECT name " |
||
| 731 | " from tbl_combined_equipments " |
||
| 732 | " WHERE id = %s ", (id_,)) |
||
| 733 | if cursor.fetchone() is None: |
||
| 734 | cursor.close() |
||
| 735 | cnx.close() |
||
| 736 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 737 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 738 | |||
| 739 | cursor.execute(" SELECT name " |
||
| 740 | " FROM tbl_equipments " |
||
| 741 | " WHERE id = %s ", (equipment_id,)) |
||
| 742 | if cursor.fetchone() is None: |
||
| 743 | cursor.close() |
||
| 744 | cnx.close() |
||
| 745 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 746 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 747 | |||
| 748 | query = (" SELECT id " |
||
| 749 | " FROM tbl_combined_equipments_equipments " |
||
| 750 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 751 | cursor.execute(query, (id_, equipment_id,)) |
||
| 752 | if cursor.fetchone() is not None: |
||
| 753 | cursor.close() |
||
| 754 | cnx.close() |
||
| 755 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 756 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 757 | |||
| 758 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 759 | " VALUES (%s, %s) ") |
||
| 760 | cursor.execute(add_row, (id_, equipment_id,)) |
||
| 761 | cnx.commit() |
||
| 762 | cursor.close() |
||
| 763 | cnx.close() |
||
| 764 | |||
| 765 | resp.status = falcon.HTTP_201 |
||
| 766 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
||
| 767 | |||
| 768 | |||
| 769 | View Code Duplication | class CombinedEquipmentEquipmentItem: |
|
| 770 | def __init__(self): |
||
| 771 | pass |
||
| 772 | |||
| 773 | @staticmethod |
||
| 774 | def on_options(req, resp, id_, eid): |
||
| 775 | _ = req |
||
| 776 | resp.status = falcon.HTTP_200 |
||
| 777 | _ = id_ |
||
| 778 | |||
| 779 | @staticmethod |
||
| 780 | @user_logger |
||
| 781 | def on_delete(req, resp, id_, eid): |
||
| 782 | admin_control(req) |
||
| 783 | if not id_.isdigit() or int(id_) <= 0: |
||
| 784 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 785 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 786 | |||
| 787 | if not eid.isdigit() or int(eid) <= 0: |
||
| 788 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 789 | description='API.INVALID_EQUIPMENT_ID') |
||
| 790 | |||
| 791 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 792 | cursor = cnx.cursor() |
||
| 793 | |||
| 794 | cursor.execute(" SELECT name " |
||
| 795 | " FROM tbl_combined_equipments " |
||
| 796 | " WHERE id = %s ", (id_,)) |
||
| 797 | if cursor.fetchone() is None: |
||
| 798 | cursor.close() |
||
| 799 | cnx.close() |
||
| 800 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 801 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 802 | |||
| 803 | cursor.execute(" SELECT name " |
||
| 804 | " FROM tbl_equipments " |
||
| 805 | " WHERE id = %s ", (eid,)) |
||
| 806 | if cursor.fetchone() is None: |
||
| 807 | cursor.close() |
||
| 808 | cnx.close() |
||
| 809 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 810 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 811 | |||
| 812 | cursor.execute(" SELECT id " |
||
| 813 | " FROM tbl_combined_equipments_equipments " |
||
| 814 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 815 | if cursor.fetchone() is None: |
||
| 816 | cursor.close() |
||
| 817 | cnx.close() |
||
| 818 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 819 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 820 | |||
| 821 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments " |
||
| 822 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 823 | cnx.commit() |
||
| 824 | |||
| 825 | cursor.close() |
||
| 826 | cnx.close() |
||
| 827 | |||
| 828 | resp.status = falcon.HTTP_204 |
||
| 829 | |||
| 830 | |||
| 831 | View Code Duplication | class CombinedEquipmentParameterCollection: |
|
| 832 | def __init__(self): |
||
| 833 | pass |
||
| 834 | |||
| 835 | @staticmethod |
||
| 836 | def on_options(req, resp, id_): |
||
| 837 | _ = req |
||
| 838 | resp.status = falcon.HTTP_200 |
||
| 839 | _ = id_ |
||
| 840 | |||
| 841 | @staticmethod |
||
| 842 | def on_get(req, resp, id_): |
||
| 843 | if 'API-KEY' not in req.headers or \ |
||
| 844 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 845 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 846 | access_control(req) |
||
| 847 | else: |
||
| 848 | api_key_control(req) |
||
| 849 | if not id_.isdigit() or int(id_) <= 0: |
||
| 850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 851 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 852 | |||
| 853 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 854 | cursor = cnx.cursor() |
||
| 855 | |||
| 856 | cursor.execute(" SELECT name " |
||
| 857 | " FROM tbl_combined_equipments " |
||
| 858 | " WHERE id = %s ", (id_,)) |
||
| 859 | if cursor.fetchone() is None: |
||
| 860 | cursor.close() |
||
| 861 | cnx.close() |
||
| 862 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 863 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 864 | |||
| 865 | query = (" SELECT id, name " |
||
| 866 | " FROM tbl_points ") |
||
| 867 | cursor.execute(query) |
||
| 868 | rows_points = cursor.fetchall() |
||
| 869 | |||
| 870 | point_dict = dict() |
||
| 871 | if rows_points is not None and len(rows_points) > 0: |
||
| 872 | for row in rows_points: |
||
| 873 | point_dict[row[0]] = {"id": row[0], |
||
| 874 | "name": row[1]} |
||
| 875 | |||
| 876 | query = (" SELECT id, name, uuid " |
||
| 877 | " FROM tbl_meters ") |
||
| 878 | cursor.execute(query) |
||
| 879 | rows_meters = cursor.fetchall() |
||
| 880 | |||
| 881 | meter_dict = dict() |
||
| 882 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 883 | for row in rows_meters: |
||
| 884 | meter_dict[row[2]] = {"type": 'meter', |
||
| 885 | "id": row[0], |
||
| 886 | "name": row[1], |
||
| 887 | "uuid": row[2]} |
||
| 888 | |||
| 889 | query = (" SELECT id, name, uuid " |
||
| 890 | " FROM tbl_offline_meters ") |
||
| 891 | cursor.execute(query) |
||
| 892 | rows_offline_meters = cursor.fetchall() |
||
| 893 | |||
| 894 | offline_meter_dict = dict() |
||
| 895 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 896 | for row in rows_offline_meters: |
||
| 897 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 898 | "id": row[0], |
||
| 899 | "name": row[1], |
||
| 900 | "uuid": row[2]} |
||
| 901 | |||
| 902 | query = (" SELECT id, name, uuid " |
||
| 903 | " FROM tbl_virtual_meters ") |
||
| 904 | cursor.execute(query) |
||
| 905 | rows_virtual_meters = cursor.fetchall() |
||
| 906 | |||
| 907 | virtual_meter_dict = dict() |
||
| 908 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 909 | for row in rows_virtual_meters: |
||
| 910 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 911 | "id": row[0], |
||
| 912 | "name": row[1], |
||
| 913 | "uuid": row[2]} |
||
| 914 | |||
| 915 | query = (" SELECT id, name, parameter_type, " |
||
| 916 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 917 | " FROM tbl_combined_equipments_parameters " |
||
| 918 | " WHERE combined_equipment_id = %s " |
||
| 919 | " ORDER BY id ") |
||
| 920 | cursor.execute(query, (id_,)) |
||
| 921 | rows_parameters = cursor.fetchall() |
||
| 922 | |||
| 923 | result = list() |
||
| 924 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 925 | for row in rows_parameters: |
||
| 926 | constant = None |
||
| 927 | point = None |
||
| 928 | numerator_meter = None |
||
| 929 | denominator_meter = None |
||
| 930 | if row[2] == 'point': |
||
| 931 | point = point_dict.get(row[4], None) |
||
| 932 | constant = None |
||
| 933 | numerator_meter = None |
||
| 934 | denominator_meter = None |
||
| 935 | elif row[2] == 'constant': |
||
| 936 | constant = row[3] |
||
| 937 | point = None |
||
| 938 | numerator_meter = None |
||
| 939 | denominator_meter = None |
||
| 940 | elif row[2] == 'fraction': |
||
| 941 | constant = None |
||
| 942 | point = None |
||
| 943 | # find numerator meter by uuid |
||
| 944 | numerator_meter = meter_dict.get(row[5], None) |
||
| 945 | if numerator_meter is None: |
||
| 946 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 947 | if numerator_meter is None: |
||
| 948 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 949 | # find denominator meter by uuid |
||
| 950 | denominator_meter = meter_dict.get(row[6], None) |
||
| 951 | if denominator_meter is None: |
||
| 952 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 953 | if denominator_meter is None: |
||
| 954 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 955 | |||
| 956 | meta_result = {"id": row[0], |
||
| 957 | "name": row[1], |
||
| 958 | "parameter_type": row[2], |
||
| 959 | "constant": constant, |
||
| 960 | "point": point, |
||
| 961 | "numerator_meter": numerator_meter, |
||
| 962 | "denominator_meter": denominator_meter} |
||
| 963 | result.append(meta_result) |
||
| 964 | |||
| 965 | cursor.close() |
||
| 966 | cnx.close() |
||
| 967 | resp.text = json.dumps(result) |
||
| 968 | |||
| 969 | @staticmethod |
||
| 970 | @user_logger |
||
| 971 | def on_post(req, resp, id_): |
||
| 972 | """Handles POST requests""" |
||
| 973 | admin_control(req) |
||
| 974 | if not id_.isdigit() or int(id_) <= 0: |
||
| 975 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 976 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 977 | try: |
||
| 978 | raw_json = req.stream.read().decode('utf-8') |
||
| 979 | except Exception as ex: |
||
| 980 | print(ex) |
||
| 981 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 982 | title='API.BAD_REQUEST', |
||
| 983 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 984 | |||
| 985 | new_values = json.loads(raw_json) |
||
| 986 | |||
| 987 | if 'name' not in new_values['data'].keys() or \ |
||
| 988 | not isinstance(new_values['data']['name'], str) or \ |
||
| 989 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 990 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 991 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 992 | name = str.strip(new_values['data']['name']) |
||
| 993 | |||
| 994 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 995 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 996 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 997 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 998 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 999 | |||
| 1000 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 1001 | |||
| 1002 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 1003 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1004 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1005 | |||
| 1006 | constant = None |
||
| 1007 | if 'constant' in new_values['data'].keys(): |
||
| 1008 | if new_values['data']['constant'] is not None and \ |
||
| 1009 | isinstance(new_values['data']['constant'], str) and \ |
||
| 1010 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 1011 | constant = str.strip(new_values['data']['constant']) |
||
| 1012 | |||
| 1013 | point_id = None |
||
| 1014 | if 'point_id' in new_values['data'].keys(): |
||
| 1015 | if new_values['data']['point_id'] is not None and \ |
||
| 1016 | new_values['data']['point_id'] <= 0: |
||
| 1017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1018 | description='API.INVALID_POINT_ID') |
||
| 1019 | point_id = new_values['data']['point_id'] |
||
| 1020 | |||
| 1021 | numerator_meter_uuid = None |
||
| 1022 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 1023 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 1024 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 1025 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 1026 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 1027 | |||
| 1028 | denominator_meter_uuid = None |
||
| 1029 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 1030 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 1031 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 1032 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 1033 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 1034 | |||
| 1035 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1036 | cursor = cnx.cursor() |
||
| 1037 | |||
| 1038 | cursor.execute(" SELECT name " |
||
| 1039 | " FROM tbl_combined_equipments " |
||
| 1040 | " WHERE id = %s ", (id_,)) |
||
| 1041 | if cursor.fetchone() is None: |
||
| 1042 | cursor.close() |
||
| 1043 | cnx.close() |
||
| 1044 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1045 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1046 | |||
| 1047 | cursor.execute(" SELECT name " |
||
| 1048 | " FROM tbl_combined_equipments_parameters " |
||
| 1049 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
||
| 1050 | if cursor.fetchone() is not None: |
||
| 1051 | cursor.close() |
||
| 1052 | cnx.close() |
||
| 1053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1054 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 1055 | |||
| 1056 | # validate by parameter type |
||
| 1057 | if parameter_type == 'point': |
||
| 1058 | if point_id is None: |
||
| 1059 | cursor.close() |
||
| 1060 | cnx.close() |
||
| 1061 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1062 | description='API.INVALID_POINT_ID') |
||
| 1063 | |||
| 1064 | query = (" SELECT id, name " |
||
| 1065 | " FROM tbl_points " |
||
| 1066 | " WHERE id = %s ") |
||
| 1067 | cursor.execute(query, (point_id,)) |
||
| 1068 | if cursor.fetchone() is None: |
||
| 1069 | cursor.close() |
||
| 1070 | cnx.close() |
||
| 1071 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1072 | description='API.POINT_NOT_FOUND') |
||
| 1073 | |||
| 1074 | elif parameter_type == 'constant': |
||
| 1075 | if constant is None: |
||
| 1076 | cursor.close() |
||
| 1077 | cnx.close() |
||
| 1078 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1079 | description='API.INVALID_CONSTANT_VALUE') |
||
| 1080 | |||
| 1081 | elif parameter_type == 'fraction': |
||
| 1082 | |||
| 1083 | query = (" SELECT id, name, uuid " |
||
| 1084 | " FROM tbl_meters ") |
||
| 1085 | cursor.execute(query) |
||
| 1086 | rows_meters = cursor.fetchall() |
||
| 1087 | |||
| 1088 | meter_dict = dict() |
||
| 1089 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1090 | for row in rows_meters: |
||
| 1091 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1092 | "id": row[0], |
||
| 1093 | "name": row[1], |
||
| 1094 | "uuid": row[2]} |
||
| 1095 | |||
| 1096 | query = (" SELECT id, name, uuid " |
||
| 1097 | " FROM tbl_offline_meters ") |
||
| 1098 | cursor.execute(query) |
||
| 1099 | rows_offline_meters = cursor.fetchall() |
||
| 1100 | |||
| 1101 | offline_meter_dict = dict() |
||
| 1102 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1103 | for row in rows_offline_meters: |
||
| 1104 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1105 | "id": row[0], |
||
| 1106 | "name": row[1], |
||
| 1107 | "uuid": row[2]} |
||
| 1108 | |||
| 1109 | query = (" SELECT id, name, uuid " |
||
| 1110 | " FROM tbl_virtual_meters ") |
||
| 1111 | cursor.execute(query) |
||
| 1112 | rows_virtual_meters = cursor.fetchall() |
||
| 1113 | |||
| 1114 | virtual_meter_dict = dict() |
||
| 1115 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1116 | for row in rows_virtual_meters: |
||
| 1117 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1118 | "id": row[0], |
||
| 1119 | "name": row[1], |
||
| 1120 | "uuid": row[2]} |
||
| 1121 | |||
| 1122 | # validate numerator meter uuid |
||
| 1123 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1124 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1125 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 1126 | cursor.close() |
||
| 1127 | cnx.close() |
||
| 1128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1129 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 1130 | |||
| 1131 | # validate denominator meter uuid |
||
| 1132 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 1133 | cursor.close() |
||
| 1134 | cnx.close() |
||
| 1135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1136 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1137 | |||
| 1138 | if denominator_meter_uuid not in meter_dict and \ |
||
| 1139 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 1140 | denominator_meter_uuid not in offline_meter_dict: |
||
| 1141 | cursor.close() |
||
| 1142 | cnx.close() |
||
| 1143 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1144 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1145 | |||
| 1146 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 1147 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 1148 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 1149 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 1150 | cursor.execute(add_values, (id_, |
||
| 1151 | name, |
||
| 1152 | parameter_type, |
||
| 1153 | constant, |
||
| 1154 | point_id, |
||
| 1155 | numerator_meter_uuid, |
||
| 1156 | denominator_meter_uuid)) |
||
| 1157 | new_id = cursor.lastrowid |
||
| 1158 | cnx.commit() |
||
| 1159 | cursor.close() |
||
| 1160 | cnx.close() |
||
| 1161 | |||
| 1162 | resp.status = falcon.HTTP_201 |
||
| 1163 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
||
| 1164 | |||
| 1165 | |||
| 1166 | View Code Duplication | class CombinedEquipmentParameterItem: |
|
| 1167 | def __init__(self): |
||
| 1168 | pass |
||
| 1169 | |||
| 1170 | @staticmethod |
||
| 1171 | def on_options(req, resp, id_, pid): |
||
| 1172 | _ = req |
||
| 1173 | resp.status = falcon.HTTP_200 |
||
| 1174 | _ = id_ |
||
| 1175 | |||
| 1176 | @staticmethod |
||
| 1177 | def on_get(req, resp, id_, pid): |
||
| 1178 | if 'API-KEY' not in req.headers or \ |
||
| 1179 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1180 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1181 | access_control(req) |
||
| 1182 | else: |
||
| 1183 | api_key_control(req) |
||
| 1184 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1185 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1186 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1187 | |||
| 1188 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1189 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1190 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1191 | |||
| 1192 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1193 | cursor = cnx.cursor() |
||
| 1194 | |||
| 1195 | query = (" SELECT id, name " |
||
| 1196 | " FROM tbl_points ") |
||
| 1197 | cursor.execute(query) |
||
| 1198 | rows_points = cursor.fetchall() |
||
| 1199 | |||
| 1200 | point_dict = dict() |
||
| 1201 | if rows_points is not None and len(rows_points) > 0: |
||
| 1202 | for row in rows_points: |
||
| 1203 | point_dict[row[0]] = {"id": row[0], |
||
| 1204 | "name": row[1]} |
||
| 1205 | |||
| 1206 | query = (" SELECT id, name, uuid " |
||
| 1207 | " FROM tbl_meters ") |
||
| 1208 | cursor.execute(query) |
||
| 1209 | rows_meters = cursor.fetchall() |
||
| 1210 | |||
| 1211 | meter_dict = dict() |
||
| 1212 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1213 | for row in rows_meters: |
||
| 1214 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1215 | "id": row[0], |
||
| 1216 | "name": row[1], |
||
| 1217 | "uuid": row[2]} |
||
| 1218 | |||
| 1219 | query = (" SELECT id, name, uuid " |
||
| 1220 | " FROM tbl_offline_meters ") |
||
| 1221 | cursor.execute(query) |
||
| 1222 | rows_offline_meters = cursor.fetchall() |
||
| 1223 | |||
| 1224 | offline_meter_dict = dict() |
||
| 1225 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1226 | for row in rows_offline_meters: |
||
| 1227 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1228 | "id": row[0], |
||
| 1229 | "name": row[1], |
||
| 1230 | "uuid": row[2]} |
||
| 1231 | |||
| 1232 | query = (" SELECT id, name, uuid " |
||
| 1233 | " FROM tbl_virtual_meters ") |
||
| 1234 | cursor.execute(query) |
||
| 1235 | rows_virtual_meters = cursor.fetchall() |
||
| 1236 | |||
| 1237 | virtual_meter_dict = dict() |
||
| 1238 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1239 | for row in rows_virtual_meters: |
||
| 1240 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1241 | "id": row[0], |
||
| 1242 | "name": row[1], |
||
| 1243 | "uuid": row[2]} |
||
| 1244 | |||
| 1245 | query = (" SELECT id, name, parameter_type, " |
||
| 1246 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 1247 | " FROM tbl_combined_equipments_parameters " |
||
| 1248 | " WHERE combined_equipment_id = %s AND id = %s ") |
||
| 1249 | cursor.execute(query, (id_, pid)) |
||
| 1250 | row = cursor.fetchone() |
||
| 1251 | cursor.close() |
||
| 1252 | cnx.close() |
||
| 1253 | |||
| 1254 | if row is None: |
||
| 1255 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1256 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1257 | else: |
||
| 1258 | constant = None |
||
| 1259 | point = None |
||
| 1260 | numerator_meter = None |
||
| 1261 | denominator_meter = None |
||
| 1262 | if row[2] == 'point': |
||
| 1263 | point = point_dict.get(row[4], None) |
||
| 1264 | constant = None |
||
| 1265 | numerator_meter = None |
||
| 1266 | denominator_meter = None |
||
| 1267 | elif row[2] == 'constant': |
||
| 1268 | constant = row[3] |
||
| 1269 | point = None |
||
| 1270 | numerator_meter = None |
||
| 1271 | denominator_meter = None |
||
| 1272 | elif row[2] == 'fraction': |
||
| 1273 | constant = None |
||
| 1274 | point = None |
||
| 1275 | # find numerator meter by uuid |
||
| 1276 | numerator_meter = meter_dict.get(row[5], None) |
||
| 1277 | if numerator_meter is None: |
||
| 1278 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 1279 | if numerator_meter is None: |
||
| 1280 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 1281 | # find denominator meter by uuid |
||
| 1282 | denominator_meter = meter_dict.get(row[6], None) |
||
| 1283 | if denominator_meter is None: |
||
| 1284 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 1285 | if denominator_meter is None: |
||
| 1286 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 1287 | |||
| 1288 | meta_result = {"id": row[0], |
||
| 1289 | "name": row[1], |
||
| 1290 | "parameter_type": row[2], |
||
| 1291 | "constant": constant, |
||
| 1292 | "point": point, |
||
| 1293 | "numerator_meter": numerator_meter, |
||
| 1294 | "denominator_meter": denominator_meter} |
||
| 1295 | |||
| 1296 | resp.text = json.dumps(meta_result) |
||
| 1297 | |||
| 1298 | @staticmethod |
||
| 1299 | @user_logger |
||
| 1300 | def on_delete(req, resp, id_, pid): |
||
| 1301 | admin_control(req) |
||
| 1302 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1303 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1304 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1305 | |||
| 1306 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1307 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1308 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1309 | |||
| 1310 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1311 | cursor = cnx.cursor() |
||
| 1312 | |||
| 1313 | cursor.execute(" SELECT name " |
||
| 1314 | " FROM tbl_combined_equipments " |
||
| 1315 | " WHERE id = %s ", |
||
| 1316 | (id_,)) |
||
| 1317 | row = cursor.fetchone() |
||
| 1318 | if row is None: |
||
| 1319 | cursor.close() |
||
| 1320 | cnx.close() |
||
| 1321 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1322 | title='API.NOT_FOUND', |
||
| 1323 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1324 | |||
| 1325 | cursor.execute(" SELECT name " |
||
| 1326 | " FROM tbl_combined_equipments_parameters " |
||
| 1327 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1328 | (id_, pid,)) |
||
| 1329 | row = cursor.fetchone() |
||
| 1330 | if row is None: |
||
| 1331 | cursor.close() |
||
| 1332 | cnx.close() |
||
| 1333 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1334 | title='API.NOT_FOUND', |
||
| 1335 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1336 | |||
| 1337 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
||
| 1338 | " WHERE id = %s ", (pid,)) |
||
| 1339 | cnx.commit() |
||
| 1340 | |||
| 1341 | cursor.close() |
||
| 1342 | cnx.close() |
||
| 1343 | |||
| 1344 | resp.status = falcon.HTTP_204 |
||
| 1345 | |||
| 1346 | @staticmethod |
||
| 1347 | @user_logger |
||
| 1348 | def on_put(req, resp, id_, pid): |
||
| 1349 | """Handles PUT requests""" |
||
| 1350 | admin_control(req) |
||
| 1351 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1352 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1353 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1354 | |||
| 1355 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1356 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1357 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1358 | |||
| 1359 | try: |
||
| 1360 | raw_json = req.stream.read().decode('utf-8') |
||
| 1361 | except Exception as ex: |
||
| 1362 | print(ex) |
||
| 1363 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1364 | title='API.BAD_REQUEST', |
||
| 1365 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1366 | |||
| 1367 | new_values = json.loads(raw_json) |
||
| 1368 | |||
| 1369 | if 'name' not in new_values['data'].keys() or \ |
||
| 1370 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1371 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1372 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1373 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 1374 | name = str.strip(new_values['data']['name']) |
||
| 1375 | |||
| 1376 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 1377 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 1378 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 1379 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1380 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1381 | |||
| 1382 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 1383 | |||
| 1384 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 1385 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1386 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1387 | |||
| 1388 | constant = None |
||
| 1389 | if 'constant' in new_values['data'].keys(): |
||
| 1390 | if new_values['data']['constant'] is not None and \ |
||
| 1391 | isinstance(new_values['data']['constant'], str) and \ |
||
| 1392 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 1393 | constant = str.strip(new_values['data']['constant']) |
||
| 1394 | |||
| 1395 | point_id = None |
||
| 1396 | if 'point_id' in new_values['data'].keys(): |
||
| 1397 | if new_values['data']['point_id'] is not None and \ |
||
| 1398 | new_values['data']['point_id'] <= 0: |
||
| 1399 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1400 | description='API.INVALID_POINT_ID') |
||
| 1401 | point_id = new_values['data']['point_id'] |
||
| 1402 | |||
| 1403 | numerator_meter_uuid = None |
||
| 1404 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 1405 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 1406 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 1407 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 1408 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 1409 | |||
| 1410 | denominator_meter_uuid = None |
||
| 1411 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 1412 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 1413 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 1414 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 1415 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 1416 | |||
| 1417 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1418 | cursor = cnx.cursor() |
||
| 1419 | |||
| 1420 | cursor.execute(" SELECT name " |
||
| 1421 | " FROM tbl_combined_equipments " |
||
| 1422 | " WHERE id = %s ", (id_,)) |
||
| 1423 | if cursor.fetchone() is None: |
||
| 1424 | cursor.close() |
||
| 1425 | cnx.close() |
||
| 1426 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1427 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1428 | |||
| 1429 | cursor.execute(" SELECT name " |
||
| 1430 | " FROM tbl_combined_equipments_parameters " |
||
| 1431 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1432 | (id_, pid,)) |
||
| 1433 | row = cursor.fetchone() |
||
| 1434 | if row is None: |
||
| 1435 | cursor.close() |
||
| 1436 | cnx.close() |
||
| 1437 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1438 | title='API.NOT_FOUND', |
||
| 1439 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1440 | |||
| 1441 | cursor.execute(" SELECT name " |
||
| 1442 | " FROM tbl_combined_equipments_parameters " |
||
| 1443 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
||
| 1444 | row = cursor.fetchone() |
||
| 1445 | if row is not None: |
||
| 1446 | cursor.close() |
||
| 1447 | cnx.close() |
||
| 1448 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1449 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 1450 | |||
| 1451 | # validate by parameter type |
||
| 1452 | if parameter_type == 'point': |
||
| 1453 | if point_id is None: |
||
| 1454 | cursor.close() |
||
| 1455 | cnx.close() |
||
| 1456 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1457 | description='API.INVALID_POINT_ID') |
||
| 1458 | |||
| 1459 | query = (" SELECT id, name " |
||
| 1460 | " FROM tbl_points " |
||
| 1461 | " WHERE id = %s ") |
||
| 1462 | cursor.execute(query, (point_id,)) |
||
| 1463 | row = cursor.fetchone() |
||
| 1464 | if row is None: |
||
| 1465 | cursor.close() |
||
| 1466 | cnx.close() |
||
| 1467 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1468 | description='API.POINT_NOT_FOUND') |
||
| 1469 | |||
| 1470 | elif parameter_type == 'constant': |
||
| 1471 | if constant is None: |
||
| 1472 | cursor.close() |
||
| 1473 | cnx.close() |
||
| 1474 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1475 | description='API.INVALID_CONSTANT_VALUE') |
||
| 1476 | |||
| 1477 | elif parameter_type == 'fraction': |
||
| 1478 | |||
| 1479 | query = (" SELECT id, name, uuid " |
||
| 1480 | " FROM tbl_meters ") |
||
| 1481 | cursor.execute(query) |
||
| 1482 | rows_meters = cursor.fetchall() |
||
| 1483 | |||
| 1484 | meter_dict = dict() |
||
| 1485 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1486 | for row in rows_meters: |
||
| 1487 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1488 | "id": row[0], |
||
| 1489 | "name": row[1], |
||
| 1490 | "uuid": row[2]} |
||
| 1491 | |||
| 1492 | query = (" SELECT id, name, uuid " |
||
| 1493 | " FROM tbl_offline_meters ") |
||
| 1494 | cursor.execute(query) |
||
| 1495 | rows_offline_meters = cursor.fetchall() |
||
| 1496 | |||
| 1497 | offline_meter_dict = dict() |
||
| 1498 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1499 | for row in rows_offline_meters: |
||
| 1500 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1501 | "id": row[0], |
||
| 1502 | "name": row[1], |
||
| 1503 | "uuid": row[2]} |
||
| 1504 | |||
| 1505 | query = (" SELECT id, name, uuid " |
||
| 1506 | " FROM tbl_virtual_meters ") |
||
| 1507 | cursor.execute(query) |
||
| 1508 | rows_virtual_meters = cursor.fetchall() |
||
| 1509 | |||
| 1510 | virtual_meter_dict = dict() |
||
| 1511 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1512 | for row in rows_virtual_meters: |
||
| 1513 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1514 | "id": row[0], |
||
| 1515 | "name": row[1], |
||
| 1516 | "uuid": row[2]} |
||
| 1517 | |||
| 1518 | # validate numerator meter uuid |
||
| 1519 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1520 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1521 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 1522 | cursor.close() |
||
| 1523 | cnx.close() |
||
| 1524 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1525 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 1526 | |||
| 1527 | # validate denominator meter uuid |
||
| 1528 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 1529 | cursor.close() |
||
| 1530 | cnx.close() |
||
| 1531 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1532 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1533 | |||
| 1534 | if denominator_meter_uuid not in meter_dict and \ |
||
| 1535 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 1536 | denominator_meter_uuid not in offline_meter_dict: |
||
| 1537 | cursor.close() |
||
| 1538 | cnx.close() |
||
| 1539 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1540 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1541 | |||
| 1542 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
||
| 1543 | " SET name = %s , parameter_type = %s, constant = %s, " |
||
| 1544 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
||
| 1545 | " WHERE id = %s ") |
||
| 1546 | cursor.execute(add_values, (name, |
||
| 1547 | parameter_type, |
||
| 1548 | constant, |
||
| 1549 | point_id, |
||
| 1550 | numerator_meter_uuid, |
||
| 1551 | denominator_meter_uuid, |
||
| 1552 | pid)) |
||
| 1553 | cnx.commit() |
||
| 1554 | |||
| 1555 | cursor.close() |
||
| 1556 | cnx.close() |
||
| 1557 | |||
| 1558 | resp.status = falcon.HTTP_200 |
||
| 1559 | |||
| 1560 | |||
| 1561 | View Code Duplication | class CombinedEquipmentMeterCollection: |
|
| 1562 | def __init__(self): |
||
| 1563 | pass |
||
| 1564 | |||
| 1565 | @staticmethod |
||
| 1566 | def on_options(req, resp, id_): |
||
| 1567 | _ = req |
||
| 1568 | resp.status = falcon.HTTP_200 |
||
| 1569 | _ = id_ |
||
| 1570 | |||
| 1571 | @staticmethod |
||
| 1572 | def on_get(req, resp, id_): |
||
| 1573 | if 'API-KEY' not in req.headers or \ |
||
| 1574 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1575 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1576 | access_control(req) |
||
| 1577 | else: |
||
| 1578 | api_key_control(req) |
||
| 1579 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1581 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1582 | |||
| 1583 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1584 | cursor = cnx.cursor() |
||
| 1585 | |||
| 1586 | cursor.execute(" SELECT name " |
||
| 1587 | " FROM tbl_combined_equipments " |
||
| 1588 | " WHERE id = %s ", (id_,)) |
||
| 1589 | if cursor.fetchone() is None: |
||
| 1590 | cursor.close() |
||
| 1591 | cnx.close() |
||
| 1592 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1593 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1594 | |||
| 1595 | query = (" SELECT id, name, uuid " |
||
| 1596 | " FROM tbl_energy_categories ") |
||
| 1597 | cursor.execute(query) |
||
| 1598 | rows_energy_categories = cursor.fetchall() |
||
| 1599 | |||
| 1600 | energy_category_dict = dict() |
||
| 1601 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1602 | for row in rows_energy_categories: |
||
| 1603 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 1604 | "name": row[1], |
||
| 1605 | "uuid": row[2]} |
||
| 1606 | |||
| 1607 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1608 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 1609 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 1610 | " ORDER BY m.id ") |
||
| 1611 | cursor.execute(query, (id_,)) |
||
| 1612 | rows = cursor.fetchall() |
||
| 1613 | |||
| 1614 | result = list() |
||
| 1615 | if rows is not None and len(rows) > 0: |
||
| 1616 | for row in rows: |
||
| 1617 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 1618 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 1619 | "is_output": bool(row[4])} |
||
| 1620 | result.append(meta_result) |
||
| 1621 | |||
| 1622 | cursor.close() |
||
| 1623 | cnx.close() |
||
| 1624 | |||
| 1625 | resp.text = json.dumps(result) |
||
| 1626 | |||
| 1627 | @staticmethod |
||
| 1628 | @user_logger |
||
| 1629 | def on_post(req, resp, id_): |
||
| 1630 | """Handles POST requests""" |
||
| 1631 | admin_control(req) |
||
| 1632 | try: |
||
| 1633 | raw_json = req.stream.read().decode('utf-8') |
||
| 1634 | except Exception as ex: |
||
| 1635 | print(ex) |
||
| 1636 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1637 | title='API.BAD_REQUEST', |
||
| 1638 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1639 | |||
| 1640 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1641 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1642 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1643 | |||
| 1644 | new_values = json.loads(raw_json) |
||
| 1645 | |||
| 1646 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1647 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1648 | new_values['data']['meter_id'] <= 0: |
||
| 1649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1650 | description='API.INVALID_METER_ID') |
||
| 1651 | meter_id = new_values['data']['meter_id'] |
||
| 1652 | |||
| 1653 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1654 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1655 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1656 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1657 | is_output = new_values['data']['is_output'] |
||
| 1658 | |||
| 1659 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1660 | cursor = cnx.cursor() |
||
| 1661 | |||
| 1662 | cursor.execute(" SELECT name " |
||
| 1663 | " from tbl_combined_equipments " |
||
| 1664 | " WHERE id = %s ", (id_,)) |
||
| 1665 | if cursor.fetchone() is None: |
||
| 1666 | cursor.close() |
||
| 1667 | cnx.close() |
||
| 1668 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1669 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1670 | |||
| 1671 | cursor.execute(" SELECT name " |
||
| 1672 | " FROM tbl_meters " |
||
| 1673 | " WHERE id = %s ", (meter_id,)) |
||
| 1674 | if cursor.fetchone() is None: |
||
| 1675 | cursor.close() |
||
| 1676 | cnx.close() |
||
| 1677 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1678 | description='API.METER_NOT_FOUND') |
||
| 1679 | |||
| 1680 | query = (" SELECT id " |
||
| 1681 | " FROM tbl_combined_equipments_meters " |
||
| 1682 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 1683 | cursor.execute(query, (id_, meter_id,)) |
||
| 1684 | if cursor.fetchone() is not None: |
||
| 1685 | cursor.close() |
||
| 1686 | cnx.close() |
||
| 1687 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1688 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 1689 | |||
| 1690 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 1691 | " VALUES (%s, %s, %s) ") |
||
| 1692 | cursor.execute(add_row, (id_, meter_id, is_output)) |
||
| 1693 | cnx.commit() |
||
| 1694 | cursor.close() |
||
| 1695 | cnx.close() |
||
| 1696 | |||
| 1697 | resp.status = falcon.HTTP_201 |
||
| 1698 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
||
| 1699 | |||
| 1700 | |||
| 1701 | View Code Duplication | class CombinedEquipmentMeterItem: |
|
| 1702 | def __init__(self): |
||
| 1703 | pass |
||
| 1704 | |||
| 1705 | @staticmethod |
||
| 1706 | def on_options(req, resp, id_, mid): |
||
| 1707 | _ = req |
||
| 1708 | resp.status = falcon.HTTP_200 |
||
| 1709 | _ = id_ |
||
| 1710 | |||
| 1711 | @staticmethod |
||
| 1712 | @user_logger |
||
| 1713 | def on_delete(req, resp, id_, mid): |
||
| 1714 | admin_control(req) |
||
| 1715 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1716 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1717 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1718 | |||
| 1719 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1720 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1721 | description='API.INVALID_METER_ID') |
||
| 1722 | |||
| 1723 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1724 | cursor = cnx.cursor() |
||
| 1725 | |||
| 1726 | cursor.execute(" SELECT name " |
||
| 1727 | " FROM tbl_combined_equipments " |
||
| 1728 | " WHERE id = %s ", (id_,)) |
||
| 1729 | if cursor.fetchone() is None: |
||
| 1730 | cursor.close() |
||
| 1731 | cnx.close() |
||
| 1732 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1733 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1734 | |||
| 1735 | cursor.execute(" SELECT name " |
||
| 1736 | " FROM tbl_meters " |
||
| 1737 | " WHERE id = %s ", (mid,)) |
||
| 1738 | if cursor.fetchone() is None: |
||
| 1739 | cursor.close() |
||
| 1740 | cnx.close() |
||
| 1741 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1742 | description='API.METER_NOT_FOUND') |
||
| 1743 | |||
| 1744 | cursor.execute(" SELECT id " |
||
| 1745 | " FROM tbl_combined_equipments_meters " |
||
| 1746 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1747 | if cursor.fetchone() is None: |
||
| 1748 | cursor.close() |
||
| 1749 | cnx.close() |
||
| 1750 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1751 | description='API.COMBINED_EQUIPMENT_METER_RELATION_NOT_FOUND') |
||
| 1752 | |||
| 1753 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters " |
||
| 1754 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1755 | cnx.commit() |
||
| 1756 | |||
| 1757 | cursor.close() |
||
| 1758 | cnx.close() |
||
| 1759 | |||
| 1760 | resp.status = falcon.HTTP_204 |
||
| 1761 | |||
| 1762 | |||
| 1763 | View Code Duplication | class CombinedEquipmentOfflineMeterCollection: |
|
| 1764 | def __init__(self): |
||
| 1765 | pass |
||
| 1766 | |||
| 1767 | @staticmethod |
||
| 1768 | def on_options(req, resp, id_): |
||
| 1769 | _ = req |
||
| 1770 | resp.status = falcon.HTTP_200 |
||
| 1771 | _ = id_ |
||
| 1772 | |||
| 1773 | @staticmethod |
||
| 1774 | def on_get(req, resp, id_): |
||
| 1775 | if 'API-KEY' not in req.headers or \ |
||
| 1776 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1777 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1778 | access_control(req) |
||
| 1779 | else: |
||
| 1780 | api_key_control(req) |
||
| 1781 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1783 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1784 | |||
| 1785 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1786 | cursor = cnx.cursor() |
||
| 1787 | |||
| 1788 | cursor.execute(" SELECT name " |
||
| 1789 | " FROM tbl_combined_equipments " |
||
| 1790 | " WHERE id = %s ", (id_,)) |
||
| 1791 | if cursor.fetchone() is None: |
||
| 1792 | cursor.close() |
||
| 1793 | cnx.close() |
||
| 1794 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1795 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1796 | |||
| 1797 | query = (" SELECT id, name, uuid " |
||
| 1798 | " FROM tbl_energy_categories ") |
||
| 1799 | cursor.execute(query) |
||
| 1800 | rows_energy_categories = cursor.fetchall() |
||
| 1801 | |||
| 1802 | energy_category_dict = dict() |
||
| 1803 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1804 | for row in rows_energy_categories: |
||
| 1805 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 1806 | "name": row[1], |
||
| 1807 | "uuid": row[2]} |
||
| 1808 | |||
| 1809 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1810 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 1811 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 1812 | " ORDER BY m.id ") |
||
| 1813 | cursor.execute(query, (id_,)) |
||
| 1814 | rows = cursor.fetchall() |
||
| 1815 | |||
| 1816 | result = list() |
||
| 1817 | if rows is not None and len(rows) > 0: |
||
| 1818 | for row in rows: |
||
| 1819 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 1820 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 1821 | "is_output": bool(row[4])} |
||
| 1822 | result.append(meta_result) |
||
| 1823 | |||
| 1824 | cursor.close() |
||
| 1825 | cnx.close() |
||
| 1826 | |||
| 1827 | resp.text = json.dumps(result) |
||
| 1828 | |||
| 1829 | @staticmethod |
||
| 1830 | @user_logger |
||
| 1831 | def on_post(req, resp, id_): |
||
| 1832 | """Handles POST requests""" |
||
| 1833 | admin_control(req) |
||
| 1834 | try: |
||
| 1835 | raw_json = req.stream.read().decode('utf-8') |
||
| 1836 | except Exception as ex: |
||
| 1837 | print(ex) |
||
| 1838 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1839 | title='API.BAD_REQUEST', |
||
| 1840 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1841 | |||
| 1842 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1843 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1844 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1845 | |||
| 1846 | new_values = json.loads(raw_json) |
||
| 1847 | |||
| 1848 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 1849 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 1850 | new_values['data']['offline_meter_id'] <= 0: |
||
| 1851 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1852 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1853 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 1854 | |||
| 1855 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1856 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1857 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1858 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1859 | is_output = new_values['data']['is_output'] |
||
| 1860 | |||
| 1861 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1862 | cursor = cnx.cursor() |
||
| 1863 | |||
| 1864 | cursor.execute(" SELECT name " |
||
| 1865 | " from tbl_combined_equipments " |
||
| 1866 | " WHERE id = %s ", (id_,)) |
||
| 1867 | if cursor.fetchone() is None: |
||
| 1868 | cursor.close() |
||
| 1869 | cnx.close() |
||
| 1870 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1871 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1872 | |||
| 1873 | cursor.execute(" SELECT name " |
||
| 1874 | " FROM tbl_offline_meters " |
||
| 1875 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 1876 | if cursor.fetchone() is None: |
||
| 1877 | cursor.close() |
||
| 1878 | cnx.close() |
||
| 1879 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1880 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1881 | |||
| 1882 | query = (" SELECT id " |
||
| 1883 | " FROM tbl_combined_equipments_offline_meters " |
||
| 1884 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 1885 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 1886 | if cursor.fetchone() is not None: |
||
| 1887 | cursor.close() |
||
| 1888 | cnx.close() |
||
| 1889 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1890 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 1891 | |||
| 1892 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 1893 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 1894 | " VALUES (%s, %s, %s) ") |
||
| 1895 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
||
| 1896 | cnx.commit() |
||
| 1897 | cursor.close() |
||
| 1898 | cnx.close() |
||
| 1899 | |||
| 1900 | resp.status = falcon.HTTP_201 |
||
| 1901 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 1902 | |||
| 1903 | |||
| 1904 | View Code Duplication | class CombinedEquipmentOfflineMeterItem: |
|
| 1905 | def __init__(self): |
||
| 1906 | pass |
||
| 1907 | |||
| 1908 | @staticmethod |
||
| 1909 | def on_options(req, resp, id_, mid): |
||
| 1910 | _ = req |
||
| 1911 | resp.status = falcon.HTTP_200 |
||
| 1912 | _ = id_ |
||
| 1913 | |||
| 1914 | @staticmethod |
||
| 1915 | @user_logger |
||
| 1916 | def on_delete(req, resp, id_, mid): |
||
| 1917 | admin_control(req) |
||
| 1918 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1919 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1920 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1921 | |||
| 1922 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1923 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1924 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1925 | |||
| 1926 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1927 | cursor = cnx.cursor() |
||
| 1928 | |||
| 1929 | cursor.execute(" SELECT name " |
||
| 1930 | " FROM tbl_combined_equipments " |
||
| 1931 | " WHERE id = %s ", (id_,)) |
||
| 1932 | if cursor.fetchone() is None: |
||
| 1933 | cursor.close() |
||
| 1934 | cnx.close() |
||
| 1935 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1936 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1937 | |||
| 1938 | cursor.execute(" SELECT name " |
||
| 1939 | " FROM tbl_offline_meters " |
||
| 1940 | " WHERE id = %s ", (mid,)) |
||
| 1941 | if cursor.fetchone() is None: |
||
| 1942 | cursor.close() |
||
| 1943 | cnx.close() |
||
| 1944 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1945 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1946 | |||
| 1947 | cursor.execute(" SELECT id " |
||
| 1948 | " FROM tbl_combined_equipments_offline_meters " |
||
| 1949 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1950 | if cursor.fetchone() is None: |
||
| 1951 | cursor.close() |
||
| 1952 | cnx.close() |
||
| 1953 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1954 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 1955 | |||
| 1956 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters " |
||
| 1957 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1958 | cnx.commit() |
||
| 1959 | |||
| 1960 | cursor.close() |
||
| 1961 | cnx.close() |
||
| 1962 | |||
| 1963 | resp.status = falcon.HTTP_204 |
||
| 1964 | |||
| 1965 | |||
| 1966 | View Code Duplication | class CombinedEquipmentVirtualMeterCollection: |
|
| 1967 | def __init__(self): |
||
| 1968 | pass |
||
| 1969 | |||
| 1970 | @staticmethod |
||
| 1971 | def on_options(req, resp, id_): |
||
| 1972 | _ = req |
||
| 1973 | resp.status = falcon.HTTP_200 |
||
| 1974 | _ = id_ |
||
| 1975 | |||
| 1976 | @staticmethod |
||
| 1977 | def on_get(req, resp, id_): |
||
| 1978 | if 'API-KEY' not in req.headers or \ |
||
| 1979 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1980 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1981 | access_control(req) |
||
| 1982 | else: |
||
| 1983 | api_key_control(req) |
||
| 1984 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1985 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1986 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1987 | |||
| 1988 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1989 | cursor = cnx.cursor() |
||
| 1990 | |||
| 1991 | cursor.execute(" SELECT name " |
||
| 1992 | " FROM tbl_combined_equipments " |
||
| 1993 | " WHERE id = %s ", (id_,)) |
||
| 1994 | if cursor.fetchone() is None: |
||
| 1995 | cursor.close() |
||
| 1996 | cnx.close() |
||
| 1997 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1998 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1999 | |||
| 2000 | query = (" SELECT id, name, uuid " |
||
| 2001 | " FROM tbl_energy_categories ") |
||
| 2002 | cursor.execute(query) |
||
| 2003 | rows_energy_categories = cursor.fetchall() |
||
| 2004 | |||
| 2005 | energy_category_dict = dict() |
||
| 2006 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2007 | for row in rows_energy_categories: |
||
| 2008 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2009 | "name": row[1], |
||
| 2010 | "uuid": row[2]} |
||
| 2011 | |||
| 2012 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2013 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 2014 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 2015 | " ORDER BY m.id ") |
||
| 2016 | cursor.execute(query, (id_,)) |
||
| 2017 | rows = cursor.fetchall() |
||
| 2018 | |||
| 2019 | result = list() |
||
| 2020 | if rows is not None and len(rows) > 0: |
||
| 2021 | for row in rows: |
||
| 2022 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2023 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2024 | "is_output": bool(row[4])} |
||
| 2025 | result.append(meta_result) |
||
| 2026 | |||
| 2027 | cursor.close() |
||
| 2028 | cnx.close() |
||
| 2029 | resp.text = json.dumps(result) |
||
| 2030 | |||
| 2031 | @staticmethod |
||
| 2032 | @user_logger |
||
| 2033 | def on_post(req, resp, id_): |
||
| 2034 | """Handles POST requests""" |
||
| 2035 | admin_control(req) |
||
| 2036 | try: |
||
| 2037 | raw_json = req.stream.read().decode('utf-8') |
||
| 2038 | except Exception as ex: |
||
| 2039 | print(ex) |
||
| 2040 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2041 | title='API.BAD_REQUEST', |
||
| 2042 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2043 | |||
| 2044 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2045 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2046 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2047 | |||
| 2048 | new_values = json.loads(raw_json) |
||
| 2049 | |||
| 2050 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 2051 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 2052 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 2053 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2054 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2055 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 2056 | |||
| 2057 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 2058 | not isinstance(new_values['data']['is_output'], bool): |
||
| 2059 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2060 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 2061 | is_output = new_values['data']['is_output'] |
||
| 2062 | |||
| 2063 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2064 | cursor = cnx.cursor() |
||
| 2065 | |||
| 2066 | cursor.execute(" SELECT name " |
||
| 2067 | " from tbl_combined_equipments " |
||
| 2068 | " WHERE id = %s ", (id_,)) |
||
| 2069 | if cursor.fetchone() is None: |
||
| 2070 | cursor.close() |
||
| 2071 | cnx.close() |
||
| 2072 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2073 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2074 | |||
| 2075 | cursor.execute(" SELECT name " |
||
| 2076 | " FROM tbl_virtual_meters " |
||
| 2077 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 2078 | if cursor.fetchone() is None: |
||
| 2079 | cursor.close() |
||
| 2080 | cnx.close() |
||
| 2081 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2082 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2083 | |||
| 2084 | query = (" SELECT id " |
||
| 2085 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 2086 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 2087 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 2088 | if cursor.fetchone() is not None: |
||
| 2089 | cursor.close() |
||
| 2090 | cnx.close() |
||
| 2091 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2092 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 2093 | |||
| 2094 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 2095 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 2096 | " VALUES (%s, %s, %s) ") |
||
| 2097 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
||
| 2098 | cnx.commit() |
||
| 2099 | cursor.close() |
||
| 2100 | cnx.close() |
||
| 2101 | |||
| 2102 | resp.status = falcon.HTTP_201 |
||
| 2103 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 2104 | |||
| 2105 | |||
| 2106 | View Code Duplication | class CombinedEquipmentVirtualMeterItem: |
|
| 2107 | def __init__(self): |
||
| 2108 | pass |
||
| 2109 | |||
| 2110 | @staticmethod |
||
| 2111 | def on_options(req, resp, id_, mid): |
||
| 2112 | _ = req |
||
| 2113 | resp.status = falcon.HTTP_200 |
||
| 2114 | _ = id_ |
||
| 2115 | |||
| 2116 | @staticmethod |
||
| 2117 | @user_logger |
||
| 2118 | def on_delete(req, resp, id_, mid): |
||
| 2119 | admin_control(req) |
||
| 2120 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2121 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2122 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2123 | |||
| 2124 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2126 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2127 | |||
| 2128 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2129 | cursor = cnx.cursor() |
||
| 2130 | |||
| 2131 | cursor.execute(" SELECT name " |
||
| 2132 | " FROM tbl_combined_equipments " |
||
| 2133 | " WHERE id = %s ", (id_,)) |
||
| 2134 | if cursor.fetchone() is None: |
||
| 2135 | cursor.close() |
||
| 2136 | cnx.close() |
||
| 2137 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2138 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2139 | |||
| 2140 | cursor.execute(" SELECT name " |
||
| 2141 | " FROM tbl_virtual_meters " |
||
| 2142 | " WHERE id = %s ", (mid,)) |
||
| 2143 | if cursor.fetchone() is None: |
||
| 2144 | cursor.close() |
||
| 2145 | cnx.close() |
||
| 2146 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2147 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2148 | |||
| 2149 | cursor.execute(" SELECT id " |
||
| 2150 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 2151 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2152 | if cursor.fetchone() is None: |
||
| 2153 | cursor.close() |
||
| 2154 | cnx.close() |
||
| 2155 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2156 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 2157 | |||
| 2158 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters " |
||
| 2159 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2160 | cnx.commit() |
||
| 2161 | |||
| 2162 | cursor.close() |
||
| 2163 | cnx.close() |
||
| 2164 | |||
| 2165 | resp.status = falcon.HTTP_204 |
||
| 2166 | |||
| 2167 | |||
| 2168 | View Code Duplication | class CombinedEquipmentCommandCollection: |
|
| 2169 | def __init__(self): |
||
| 2170 | pass |
||
| 2171 | |||
| 2172 | @staticmethod |
||
| 2173 | def on_options(req, resp, id_): |
||
| 2174 | _ = req |
||
| 2175 | resp.status = falcon.HTTP_200 |
||
| 2176 | _ = id_ |
||
| 2177 | |||
| 2178 | @staticmethod |
||
| 2179 | def on_get(req, resp, id_): |
||
| 2180 | if 'API-KEY' not in req.headers or \ |
||
| 2181 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2182 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2183 | access_control(req) |
||
| 2184 | else: |
||
| 2185 | api_key_control(req) |
||
| 2186 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2187 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2188 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2189 | |||
| 2190 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2191 | cursor = cnx.cursor() |
||
| 2192 | |||
| 2193 | cursor.execute(" SELECT name " |
||
| 2194 | " FROM tbl_combined_equipments " |
||
| 2195 | " WHERE id = %s ", (id_,)) |
||
| 2196 | if cursor.fetchone() is None: |
||
| 2197 | cursor.close() |
||
| 2198 | cnx.close() |
||
| 2199 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2200 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2201 | |||
| 2202 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2203 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 2204 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 2205 | " ORDER BY c.id ") |
||
| 2206 | cursor.execute(query, (id_,)) |
||
| 2207 | rows = cursor.fetchall() |
||
| 2208 | |||
| 2209 | result = list() |
||
| 2210 | if rows is not None and len(rows) > 0: |
||
| 2211 | for row in rows: |
||
| 2212 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2213 | result.append(meta_result) |
||
| 2214 | |||
| 2215 | resp.text = json.dumps(result) |
||
| 2216 | |||
| 2217 | @staticmethod |
||
| 2218 | @user_logger |
||
| 2219 | def on_post(req, resp, id_): |
||
| 2220 | """Handles POST requests""" |
||
| 2221 | admin_control(req) |
||
| 2222 | try: |
||
| 2223 | raw_json = req.stream.read().decode('utf-8') |
||
| 2224 | except Exception as ex: |
||
| 2225 | print(ex) |
||
| 2226 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2227 | title='API.BAD_REQUEST', |
||
| 2228 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2229 | |||
| 2230 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2231 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2232 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2233 | |||
| 2234 | new_values = json.loads(raw_json) |
||
| 2235 | |||
| 2236 | if 'command_id' not in new_values['data'].keys() or \ |
||
| 2237 | not isinstance(new_values['data']['command_id'], int) or \ |
||
| 2238 | new_values['data']['command_id'] <= 0: |
||
| 2239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2240 | description='API.INVALID_COMMAND_ID') |
||
| 2241 | command_id = new_values['data']['command_id'] |
||
| 2242 | |||
| 2243 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2244 | cursor = cnx.cursor() |
||
| 2245 | |||
| 2246 | cursor.execute(" SELECT name " |
||
| 2247 | " from tbl_combined_equipments " |
||
| 2248 | " WHERE id = %s ", (id_,)) |
||
| 2249 | if cursor.fetchone() is None: |
||
| 2250 | cursor.close() |
||
| 2251 | cnx.close() |
||
| 2252 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2253 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2254 | |||
| 2255 | cursor.execute(" SELECT name " |
||
| 2256 | " FROM tbl_commands " |
||
| 2257 | " WHERE id = %s ", (command_id,)) |
||
| 2258 | if cursor.fetchone() is None: |
||
| 2259 | cursor.close() |
||
| 2260 | cnx.close() |
||
| 2261 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2262 | description='API.COMMAND_NOT_FOUND') |
||
| 2263 | |||
| 2264 | query = (" SELECT id " |
||
| 2265 | " FROM tbl_combined_equipments_commands " |
||
| 2266 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 2267 | cursor.execute(query, (id_, command_id,)) |
||
| 2268 | if cursor.fetchone() is not None: |
||
| 2269 | cursor.close() |
||
| 2270 | cnx.close() |
||
| 2271 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2272 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 2273 | |||
| 2274 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 2275 | " VALUES (%s, %s) ") |
||
| 2276 | cursor.execute(add_row, (id_, command_id,)) |
||
| 2277 | cnx.commit() |
||
| 2278 | cursor.close() |
||
| 2279 | cnx.close() |
||
| 2280 | |||
| 2281 | resp.status = falcon.HTTP_201 |
||
| 2282 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
||
| 2283 | |||
| 2284 | |||
| 2285 | View Code Duplication | class CombinedEquipmentCommandItem: |
|
| 2286 | def __init__(self): |
||
| 2287 | pass |
||
| 2288 | |||
| 2289 | @staticmethod |
||
| 2290 | def on_options(req, resp, id_, cid): |
||
| 2291 | _ = req |
||
| 2292 | resp.status = falcon.HTTP_200 |
||
| 2293 | _ = id_ |
||
| 2294 | |||
| 2295 | @staticmethod |
||
| 2296 | @user_logger |
||
| 2297 | def on_delete(req, resp, id_, cid): |
||
| 2298 | admin_control(req) |
||
| 2299 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2301 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2302 | |||
| 2303 | if not cid.isdigit() or int(cid) <= 0: |
||
| 2304 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2305 | description='API.INVALID_COMMAND_ID') |
||
| 2306 | |||
| 2307 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2308 | cursor = cnx.cursor() |
||
| 2309 | |||
| 2310 | cursor.execute(" SELECT name " |
||
| 2311 | " FROM tbl_combined_equipments " |
||
| 2312 | " WHERE id = %s ", (id_,)) |
||
| 2313 | if cursor.fetchone() is None: |
||
| 2314 | cursor.close() |
||
| 2315 | cnx.close() |
||
| 2316 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2317 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2318 | |||
| 2319 | cursor.execute(" SELECT name " |
||
| 2320 | " FROM tbl_commands " |
||
| 2321 | " WHERE id = %s ", (cid,)) |
||
| 2322 | if cursor.fetchone() is None: |
||
| 2323 | cursor.close() |
||
| 2324 | cnx.close() |
||
| 2325 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2326 | description='API.COMMAND_NOT_FOUND') |
||
| 2327 | |||
| 2328 | cursor.execute(" SELECT id " |
||
| 2329 | " FROM tbl_combined_equipments_commands " |
||
| 2330 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2331 | if cursor.fetchone() is None: |
||
| 2332 | cursor.close() |
||
| 2333 | cnx.close() |
||
| 2334 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2335 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_NOT_FOUND') |
||
| 2336 | |||
| 2337 | cursor.execute(" DELETE FROM tbl_combined_equipments_commands " |
||
| 2338 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2339 | cnx.commit() |
||
| 2340 | |||
| 2341 | cursor.close() |
||
| 2342 | cnx.close() |
||
| 2343 | |||
| 2344 | resp.status = falcon.HTTP_204 |
||
| 2345 | |||
| 2346 | |||
| 2347 | class CombinedEquipmentExport: |
||
| 2348 | def __init__(self): |
||
| 2349 | pass |
||
| 2350 | |||
| 2351 | @staticmethod |
||
| 2352 | def on_options(req, resp, id_): |
||
| 2353 | _ = req |
||
| 2354 | resp.status = falcon.HTTP_200 |
||
| 2355 | _ = id_ |
||
| 2356 | |||
| 2357 | @staticmethod |
||
| 2358 | def on_get(req, resp, id_): |
||
| 2359 | if 'API-KEY' not in req.headers or \ |
||
| 2360 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2361 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2362 | access_control(req) |
||
| 2363 | else: |
||
| 2364 | api_key_control(req) |
||
| 2365 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2366 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2367 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2368 | |||
| 2369 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2370 | cursor = cnx.cursor() |
||
| 2371 | |||
| 2372 | query = (" SELECT id, name, uuid " |
||
| 2373 | " FROM tbl_cost_centers ") |
||
| 2374 | cursor.execute(query) |
||
| 2375 | rows_cost_centers = cursor.fetchall() |
||
| 2376 | |||
| 2377 | cost_center_dict = dict() |
||
| 2378 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 2379 | for row in rows_cost_centers: |
||
| 2380 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 2381 | "name": row[1], |
||
| 2382 | "uuid": row[2]} |
||
| 2383 | |||
| 2384 | query = (" SELECT id, name, uuid " |
||
| 2385 | " FROM tbl_svgs ") |
||
| 2386 | cursor.execute(query) |
||
| 2387 | rows_svgs = cursor.fetchall() |
||
| 2388 | |||
| 2389 | svg_dict = dict() |
||
| 2390 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 2391 | for row in rows_svgs: |
||
| 2392 | svg_dict[row[0]] = {"id": row[0], |
||
| 2393 | "name": row[1], |
||
| 2394 | "uuid": row[2]} |
||
| 2395 | |||
| 2396 | query = (" SELECT id, name, uuid, " |
||
| 2397 | " is_input_counted, is_output_counted, " |
||
| 2398 | " cost_center_id, svg_id, camera_url, description " |
||
| 2399 | " FROM tbl_combined_equipments " |
||
| 2400 | " WHERE id = %s ") |
||
| 2401 | cursor.execute(query, (id_,)) |
||
| 2402 | row = cursor.fetchone() |
||
| 2403 | |||
| 2404 | if row is None: |
||
| 2405 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2406 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2407 | else: |
||
| 2408 | meta_result = {"id": row[0], |
||
| 2409 | "name": row[1], |
||
| 2410 | "uuid": row[2], |
||
| 2411 | "is_input_counted": bool(row[3]), |
||
| 2412 | "is_output_counted": bool(row[4]), |
||
| 2413 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 2414 | "svg": svg_dict.get(row[6], None), |
||
| 2415 | "camera_url": row[7], |
||
| 2416 | "description": row[8], |
||
| 2417 | "equipments": None, |
||
| 2418 | "commands": None, |
||
| 2419 | "meters": None, |
||
| 2420 | "offline_meters": None, |
||
| 2421 | "virtual_meters": None, |
||
| 2422 | "parameters": None} |
||
| 2423 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 2424 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 2425 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 2426 | " ORDER BY e.id ") |
||
| 2427 | cursor.execute(query, (id_,)) |
||
| 2428 | rows = cursor.fetchall() |
||
| 2429 | |||
| 2430 | equipment_result = list() |
||
| 2431 | if rows is not None and len(rows) > 0: |
||
| 2432 | for row in rows: |
||
| 2433 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2434 | equipment_result.append(result) |
||
| 2435 | meta_result['equipments'] = equipment_result |
||
| 2436 | |||
| 2437 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2438 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 2439 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 2440 | " ORDER BY c.id ") |
||
| 2441 | cursor.execute(query, (id_,)) |
||
| 2442 | rows = cursor.fetchall() |
||
| 2443 | |||
| 2444 | command_result = list() |
||
| 2445 | if rows is not None and len(rows) > 0: |
||
| 2446 | for row in rows: |
||
| 2447 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2448 | command_result.append(result) |
||
| 2449 | meta_result['commands'] = command_result |
||
| 2450 | |||
| 2451 | query = (" SELECT id, name, uuid " |
||
| 2452 | " FROM tbl_energy_categories ") |
||
| 2453 | cursor.execute(query) |
||
| 2454 | rows_energy_categories = cursor.fetchall() |
||
| 2455 | |||
| 2456 | energy_category_dict = dict() |
||
| 2457 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2458 | for row in rows_energy_categories: |
||
| 2459 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2460 | "name": row[1], |
||
| 2461 | "uuid": row[2]} |
||
| 2462 | |||
| 2463 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2464 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 2465 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 2466 | " ORDER BY m.id ") |
||
| 2467 | cursor.execute(query, (id_,)) |
||
| 2468 | rows = cursor.fetchall() |
||
| 2469 | |||
| 2470 | meter_result = list() |
||
| 2471 | if rows is not None and len(rows) > 0: |
||
| 2472 | for row in rows: |
||
| 2473 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2474 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2475 | "is_output": bool(row[4])} |
||
| 2476 | meter_result.append(result) |
||
| 2477 | meta_result['meters'] = meter_result |
||
| 2478 | |||
| 2479 | query = (" SELECT id, name, uuid " |
||
| 2480 | " FROM tbl_energy_categories ") |
||
| 2481 | cursor.execute(query) |
||
| 2482 | rows_energy_categories = cursor.fetchall() |
||
| 2483 | |||
| 2484 | energy_category_dict = dict() |
||
| 2485 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2486 | for row in rows_energy_categories: |
||
| 2487 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2488 | "name": row[1], |
||
| 2489 | "uuid": row[2]} |
||
| 2490 | |||
| 2491 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2492 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 2493 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 2494 | " ORDER BY m.id ") |
||
| 2495 | cursor.execute(query, (id_,)) |
||
| 2496 | rows = cursor.fetchall() |
||
| 2497 | |||
| 2498 | offlinemeter_result = list() |
||
| 2499 | if rows is not None and len(rows) > 0: |
||
| 2500 | for row in rows: |
||
| 2501 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2502 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2503 | "is_output": bool(row[4])} |
||
| 2504 | offlinemeter_result.append(result) |
||
| 2505 | meta_result['offline_meters'] = offlinemeter_result |
||
| 2506 | |||
| 2507 | query = (" SELECT id, name, uuid " |
||
| 2508 | " FROM tbl_energy_categories ") |
||
| 2509 | cursor.execute(query) |
||
| 2510 | rows_energy_categories = cursor.fetchall() |
||
| 2511 | |||
| 2512 | energy_category_dict = dict() |
||
| 2513 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2514 | for row in rows_energy_categories: |
||
| 2515 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2516 | "name": row[1], |
||
| 2517 | "uuid": row[2]} |
||
| 2518 | |||
| 2519 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2520 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 2521 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 2522 | " ORDER BY m.id ") |
||
| 2523 | cursor.execute(query, (id_,)) |
||
| 2524 | rows = cursor.fetchall() |
||
| 2525 | |||
| 2526 | virtualmeter_result = list() |
||
| 2527 | if rows is not None and len(rows) > 0: |
||
| 2528 | for row in rows: |
||
| 2529 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2530 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2531 | "is_output": bool(row[4])} |
||
| 2532 | virtualmeter_result.append(result) |
||
| 2533 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 2534 | |||
| 2535 | query = (" SELECT id, name " |
||
| 2536 | " FROM tbl_points ") |
||
| 2537 | cursor.execute(query) |
||
| 2538 | rows_points = cursor.fetchall() |
||
| 2539 | |||
| 2540 | point_dict = dict() |
||
| 2541 | if rows_points is not None and len(rows_points) > 0: |
||
| 2542 | for row in rows_points: |
||
| 2543 | point_dict[row[0]] = {"id": row[0], |
||
| 2544 | "name": row[1]} |
||
| 2545 | |||
| 2546 | query = (" SELECT id, name, uuid " |
||
| 2547 | " FROM tbl_meters ") |
||
| 2548 | cursor.execute(query) |
||
| 2549 | rows_meters = cursor.fetchall() |
||
| 2550 | |||
| 2551 | meter_dict = dict() |
||
| 2552 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2553 | for row in rows_meters: |
||
| 2554 | meter_dict[row[2]] = {"type": 'meter', |
||
| 2555 | "id": row[0], |
||
| 2556 | "name": row[1], |
||
| 2557 | "uuid": row[2]} |
||
| 2558 | |||
| 2559 | query = (" SELECT id, name, uuid " |
||
| 2560 | " FROM tbl_offline_meters ") |
||
| 2561 | cursor.execute(query) |
||
| 2562 | rows_offline_meters = cursor.fetchall() |
||
| 2563 | |||
| 2564 | offline_meter_dict = dict() |
||
| 2565 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 2566 | for row in rows_offline_meters: |
||
| 2567 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 2568 | "id": row[0], |
||
| 2569 | "name": row[1], |
||
| 2570 | "uuid": row[2]} |
||
| 2571 | |||
| 2572 | query = (" SELECT id, name, uuid " |
||
| 2573 | " FROM tbl_virtual_meters ") |
||
| 2574 | cursor.execute(query) |
||
| 2575 | rows_virtual_meters = cursor.fetchall() |
||
| 2576 | |||
| 2577 | virtual_meter_dict = dict() |
||
| 2578 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 2579 | for row in rows_virtual_meters: |
||
| 2580 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 2581 | "id": row[0], |
||
| 2582 | "name": row[1], |
||
| 2583 | "uuid": row[2]} |
||
| 2584 | |||
| 2585 | query = (" SELECT id, name, parameter_type, " |
||
| 2586 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 2587 | " FROM tbl_combined_equipments_parameters " |
||
| 2588 | " WHERE combined_equipment_id = %s " |
||
| 2589 | " ORDER BY id ") |
||
| 2590 | cursor.execute(query, (id_,)) |
||
| 2591 | rows_parameters = cursor.fetchall() |
||
| 2592 | |||
| 2593 | parameter_result = list() |
||
| 2594 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 2595 | for row in rows_parameters: |
||
| 2596 | constant = None |
||
| 2597 | point = None |
||
| 2598 | numerator_meter = None |
||
| 2599 | denominator_meter = None |
||
| 2600 | if row[2] == 'point': |
||
| 2601 | point = point_dict.get(row[4], None) |
||
| 2602 | constant = None |
||
| 2603 | numerator_meter = None |
||
| 2604 | denominator_meter = None |
||
| 2605 | elif row[2] == 'constant': |
||
| 2606 | constant = row[3] |
||
| 2607 | point = None |
||
| 2608 | numerator_meter = None |
||
| 2609 | denominator_meter = None |
||
| 2610 | elif row[2] == 'fraction': |
||
| 2611 | constant = None |
||
| 2612 | point = None |
||
| 2613 | # find numerator meter by uuid |
||
| 2614 | numerator_meter = meter_dict.get(row[5], None) |
||
| 2615 | if numerator_meter is None: |
||
| 2616 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 2617 | if numerator_meter is None: |
||
| 2618 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 2619 | # find denominator meter by uuid |
||
| 2620 | denominator_meter = meter_dict.get(row[6], None) |
||
| 2621 | if denominator_meter is None: |
||
| 2622 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 2623 | if denominator_meter is None: |
||
| 2624 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 2625 | |||
| 2626 | result = {"id": row[0], |
||
| 2627 | "name": row[1], |
||
| 2628 | "parameter_type": row[2], |
||
| 2629 | "constant": constant, |
||
| 2630 | "point": point, |
||
| 2631 | "numerator_meter": numerator_meter, |
||
| 2632 | "denominator_meter": denominator_meter} |
||
| 2633 | parameter_result.append(result) |
||
| 2634 | meta_result['parameters'] = parameter_result |
||
| 2635 | |||
| 2636 | cursor.close() |
||
| 2637 | cnx.close() |
||
| 2638 | resp.text = json.dumps(meta_result) |
||
| 2639 | |||
| 2640 | |||
| 2641 | class CombinedEquipmentImport: |
||
| 2642 | def __init__(self): |
||
| 2643 | """ Initializes CombinedEquipmentImport""" |
||
| 2644 | pass |
||
| 2645 | |||
| 2646 | @staticmethod |
||
| 2647 | def on_options(req, resp): |
||
| 2648 | _ = req |
||
| 2649 | resp.status = falcon.HTTP_200 |
||
| 2650 | |||
| 2651 | @staticmethod |
||
| 2652 | @user_logger |
||
| 2653 | def on_post(req, resp): |
||
| 2654 | """Handles POST requests""" |
||
| 2655 | admin_control(req) |
||
| 2656 | try: |
||
| 2657 | raw_json = req.stream.read().decode('utf-8') |
||
| 2658 | except Exception as ex: |
||
| 2659 | print(ex) |
||
| 2660 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2661 | title='API.BAD_REQUEST', |
||
| 2662 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2663 | |||
| 2664 | new_values = json.loads(raw_json) |
||
| 2665 | |||
| 2666 | if 'name' not in new_values.keys() or \ |
||
| 2667 | not isinstance(new_values['name'], str) or \ |
||
| 2668 | len(str.strip(new_values['name'])) == 0: |
||
| 2669 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2670 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 2671 | name = str.strip(new_values['name']) |
||
| 2672 | |||
| 2673 | if 'is_input_counted' not in new_values.keys() or \ |
||
| 2674 | not isinstance(new_values['is_input_counted'], bool): |
||
| 2675 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2676 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 2677 | is_input_counted = new_values['is_input_counted'] |
||
| 2678 | |||
| 2679 | if 'is_output_counted' not in new_values.keys() or \ |
||
| 2680 | not isinstance(new_values['is_output_counted'], bool): |
||
| 2681 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2682 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 2683 | is_output_counted = new_values['is_output_counted'] |
||
| 2684 | |||
| 2685 | if 'id' not in new_values['cost_center'].keys() or \ |
||
| 2686 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 2687 | new_values['cost_center']['id'] <= 0: |
||
| 2688 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2689 | description='API.INVALID_COST_CENTER_ID') |
||
| 2690 | cost_center_id = new_values['cost_center']['id'] |
||
| 2691 | |||
| 2692 | if 'svg' in new_values.keys() and \ |
||
| 2693 | 'id' in new_values['svg'].keys() and \ |
||
| 2694 | isinstance(new_values['svg']['id'], int) and \ |
||
| 2695 | new_values['svg']['id'] > 0: |
||
| 2696 | svg_id = new_values['svg']['id'] |
||
| 2697 | else: |
||
| 2698 | svg_id = None |
||
| 2699 | |||
| 2700 | if 'camera_url' in new_values.keys() and \ |
||
| 2701 | new_values['camera_url'] is not None and \ |
||
| 2702 | len(str(new_values['camera_url'])) > 0: |
||
| 2703 | camera_url = str.strip(new_values['camera_url']) |
||
| 2704 | else: |
||
| 2705 | camera_url = None |
||
| 2706 | |||
| 2707 | if 'description' in new_values.keys() and \ |
||
| 2708 | new_values['description'] is not None and \ |
||
| 2709 | len(str(new_values['description'])) > 0: |
||
| 2710 | description = str.strip(new_values['description']) |
||
| 2711 | else: |
||
| 2712 | description = None |
||
| 2713 | |||
| 2714 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2715 | cursor = cnx.cursor() |
||
| 2716 | |||
| 2717 | cursor.execute(" SELECT name " |
||
| 2718 | " FROM tbl_combined_equipments " |
||
| 2719 | " WHERE name = %s ", (name,)) |
||
| 2720 | if cursor.fetchone() is not None: |
||
| 2721 | cursor.close() |
||
| 2722 | cnx.close() |
||
| 2723 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2724 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 2725 | |||
| 2726 | if cost_center_id is not None: |
||
| 2727 | cursor.execute(" SELECT name " |
||
| 2728 | " FROM tbl_cost_centers " |
||
| 2729 | " WHERE id = %s ", |
||
| 2730 | (new_values['cost_center']['id'],)) |
||
| 2731 | row = cursor.fetchone() |
||
| 2732 | if row is None: |
||
| 2733 | cursor.close() |
||
| 2734 | cnx.close() |
||
| 2735 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2736 | description='API.COST_CENTER_NOT_FOUND') |
||
| 2737 | if svg_id is not None: |
||
| 2738 | cursor.execute(" SELECT name " |
||
| 2739 | " FROM tbl_svgs " |
||
| 2740 | " WHERE id = %s ", |
||
| 2741 | (svg_id,)) |
||
| 2742 | row = cursor.fetchone() |
||
| 2743 | if row is None: |
||
| 2744 | cursor.close() |
||
| 2745 | cnx.close() |
||
| 2746 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2747 | description='API.SVG_NOT_FOUND') |
||
| 2748 | |||
| 2749 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 2750 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 2751 | " cost_center_id, svg_id, camera_url, description) " |
||
| 2752 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 2753 | cursor.execute(add_values, (name, |
||
| 2754 | str(uuid.uuid4()), |
||
| 2755 | is_input_counted, |
||
| 2756 | is_output_counted, |
||
| 2757 | cost_center_id, |
||
| 2758 | svg_id, |
||
| 2759 | camera_url, |
||
| 2760 | description)) |
||
| 2761 | new_id = cursor.lastrowid |
||
| 2762 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
| 2763 | for equipment in new_values['equipments']: |
||
| 2764 | cursor.execute(" SELECT name " |
||
| 2765 | " FROM tbl_equipments " |
||
| 2766 | " WHERE id = %s ", (equipment['id'],)) |
||
| 2767 | if cursor.fetchone() is None: |
||
| 2768 | cursor.close() |
||
| 2769 | cnx.close() |
||
| 2770 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2771 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 2772 | |||
| 2773 | query = (" SELECT id " |
||
| 2774 | " FROM tbl_combined_equipments_equipments " |
||
| 2775 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 2776 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 2777 | if cursor.fetchone() is not None: |
||
| 2778 | cursor.close() |
||
| 2779 | cnx.close() |
||
| 2780 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2781 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 2782 | |||
| 2783 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 2784 | " VALUES (%s, %s) ") |
||
| 2785 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 2786 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
| 2787 | for command in new_values['commands']: |
||
| 2788 | cursor.execute(" SELECT name " |
||
| 2789 | " FROM tbl_commands " |
||
| 2790 | " WHERE id = %s ", (command['id'],)) |
||
| 2791 | if cursor.fetchone() is None: |
||
| 2792 | cursor.close() |
||
| 2793 | cnx.close() |
||
| 2794 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2795 | description='API.COMMAND_NOT_FOUND') |
||
| 2796 | |||
| 2797 | query = (" SELECT id " |
||
| 2798 | " FROM tbl_combined_equipments_commands " |
||
| 2799 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 2800 | cursor.execute(query, (new_id, command['id'],)) |
||
| 2801 | if cursor.fetchone() is not None: |
||
| 2802 | cursor.close() |
||
| 2803 | cnx.close() |
||
| 2804 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2805 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 2806 | |||
| 2807 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 2808 | " VALUES (%s, %s) ") |
||
| 2809 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 2810 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
| 2811 | for meter in new_values['meters']: |
||
| 2812 | cursor.execute(" SELECT name " |
||
| 2813 | " FROM tbl_meters " |
||
| 2814 | " WHERE id = %s ", (meter['id'],)) |
||
| 2815 | if cursor.fetchone() is None: |
||
| 2816 | cursor.close() |
||
| 2817 | cnx.close() |
||
| 2818 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2819 | description='API.METER_NOT_FOUND') |
||
| 2820 | |||
| 2821 | query = (" SELECT id " |
||
| 2822 | " FROM tbl_combined_equipments_meters " |
||
| 2823 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 2824 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 2825 | if cursor.fetchone() is not None: |
||
| 2826 | cursor.close() |
||
| 2827 | cnx.close() |
||
| 2828 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2829 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 2830 | |||
| 2831 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 2832 | " VALUES (%s, %s, %s) ") |
||
| 2833 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
| 2834 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
| 2835 | for offline_meter in new_values['offline_meters']: |
||
| 2836 | cursor.execute(" SELECT name " |
||
| 2837 | " FROM tbl_offline_meters " |
||
| 2838 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 2839 | if cursor.fetchone() is None: |
||
| 2840 | cursor.close() |
||
| 2841 | cnx.close() |
||
| 2842 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2843 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2844 | |||
| 2845 | query = (" SELECT id " |
||
| 2846 | " FROM tbl_combined_equipments_offline_meters " |
||
| 2847 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 2848 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 2849 | if cursor.fetchone() is not None: |
||
| 2850 | cursor.close() |
||
| 2851 | cnx.close() |
||
| 2852 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2853 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 2854 | |||
| 2855 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 2856 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 2857 | " VALUES (%s, %s, %s) ") |
||
| 2858 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
| 2859 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
| 2860 | for virtual_meter in new_values['virtual_meters']: |
||
| 2861 | cursor.execute(" SELECT name " |
||
| 2862 | " FROM tbl_virtual_meters " |
||
| 2863 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 2864 | if cursor.fetchone() is None: |
||
| 2865 | cursor.close() |
||
| 2866 | cnx.close() |
||
| 2867 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2868 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2869 | |||
| 2870 | query = (" SELECT id " |
||
| 2871 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 2872 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 2873 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 2874 | if cursor.fetchone() is not None: |
||
| 2875 | cursor.close() |
||
| 2876 | cnx.close() |
||
| 2877 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2878 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 2879 | |||
| 2880 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 2881 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 2882 | " VALUES (%s, %s, %s) ") |
||
| 2883 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
| 2884 | View Code Duplication | if new_values['parameters'] is not None and len(new_values['parameters']) > 0: |
|
| 2885 | for parameters in new_values['parameters']: |
||
| 2886 | cursor.execute(" SELECT name " |
||
| 2887 | " FROM tbl_combined_equipments_parameters " |
||
| 2888 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
| 2889 | if cursor.fetchone() is not None: |
||
| 2890 | cursor.close() |
||
| 2891 | cnx.close() |
||
| 2892 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2893 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 2894 | if 'point' in parameters: |
||
| 2895 | if parameters['point'] is None: |
||
| 2896 | point_id = None |
||
| 2897 | elif parameters['point']['id'] is not None and \ |
||
| 2898 | parameters['point']['id'] <= 0: |
||
| 2899 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2900 | description='API.INVALID_POINT_ID') |
||
| 2901 | else: |
||
| 2902 | point_id = parameters['point']['id'] |
||
| 2903 | else: |
||
| 2904 | point_id = None |
||
| 2905 | numerator_meter_uuid = None |
||
| 2906 | if 'numerator_meter' in parameters: |
||
| 2907 | if parameters['numerator_meter'] is not None and \ |
||
| 2908 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
| 2909 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
| 2910 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
| 2911 | |||
| 2912 | denominator_meter_uuid = None |
||
| 2913 | if 'denominator_meter' in parameters: |
||
| 2914 | if parameters['denominator_meter'] is not None and \ |
||
| 2915 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
| 2916 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
| 2917 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
| 2918 | |||
| 2919 | # validate by parameter type |
||
| 2920 | if parameters['parameter_type'] == 'point': |
||
| 2921 | if point_id is None: |
||
| 2922 | cursor.close() |
||
| 2923 | cnx.close() |
||
| 2924 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2925 | description='API.INVALID_POINT_ID') |
||
| 2926 | |||
| 2927 | query = (" SELECT id, name " |
||
| 2928 | " FROM tbl_points " |
||
| 2929 | " WHERE id = %s ") |
||
| 2930 | cursor.execute(query, (point_id,)) |
||
| 2931 | if cursor.fetchone() is None: |
||
| 2932 | cursor.close() |
||
| 2933 | cnx.close() |
||
| 2934 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2935 | description='API.POINT_NOT_FOUND') |
||
| 2936 | |||
| 2937 | elif parameters['parameter_type'] == 'constant': |
||
| 2938 | if parameters['constant'] is None: |
||
| 2939 | cursor.close() |
||
| 2940 | cnx.close() |
||
| 2941 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2942 | description='API.INVALID_CONSTANT_VALUE') |
||
| 2943 | |||
| 2944 | elif parameters['parameter_type'] == 'fraction': |
||
| 2945 | |||
| 2946 | query = (" SELECT id, name, uuid " |
||
| 2947 | " FROM tbl_meters ") |
||
| 2948 | cursor.execute(query) |
||
| 2949 | rows_meters = cursor.fetchall() |
||
| 2950 | |||
| 2951 | meter_dict = dict() |
||
| 2952 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2953 | for row in rows_meters: |
||
| 2954 | meter_dict[row[2]] = {"type": 'meter', |
||
| 2955 | "id": row[0], |
||
| 2956 | "name": row[1], |
||
| 2957 | "uuid": row[2]} |
||
| 2958 | |||
| 2959 | query = (" SELECT id, name, uuid " |
||
| 2960 | " FROM tbl_offline_meters ") |
||
| 2961 | cursor.execute(query) |
||
| 2962 | rows_offline_meters = cursor.fetchall() |
||
| 2963 | |||
| 2964 | offline_meter_dict = dict() |
||
| 2965 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 2966 | for row in rows_offline_meters: |
||
| 2967 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 2968 | "id": row[0], |
||
| 2969 | "name": row[1], |
||
| 2970 | "uuid": row[2]} |
||
| 2971 | |||
| 2972 | query = (" SELECT id, name, uuid " |
||
| 2973 | " FROM tbl_virtual_meters ") |
||
| 2974 | cursor.execute(query) |
||
| 2975 | rows_virtual_meters = cursor.fetchall() |
||
| 2976 | |||
| 2977 | virtual_meter_dict = dict() |
||
| 2978 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 2979 | for row in rows_virtual_meters: |
||
| 2980 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 2981 | "id": row[0], |
||
| 2982 | "name": row[1], |
||
| 2983 | "uuid": row[2]} |
||
| 2984 | |||
| 2985 | # validate numerator meter uuid |
||
| 2986 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 2987 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 2988 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 2989 | cursor.close() |
||
| 2990 | cnx.close() |
||
| 2991 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2992 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 2993 | |||
| 2994 | # validate denominator meter uuid |
||
| 2995 | if denominator_meter_uuid is None: |
||
| 2996 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2997 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 2998 | |||
| 2999 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 3000 | cursor.close() |
||
| 3001 | cnx.close() |
||
| 3002 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3003 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3004 | |||
| 3005 | if denominator_meter_uuid not in meter_dict and \ |
||
| 3006 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 3007 | denominator_meter_uuid not in offline_meter_dict: |
||
| 3008 | cursor.close() |
||
| 3009 | cnx.close() |
||
| 3010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3011 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3012 | |||
| 3013 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 3014 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 3015 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 3016 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 3017 | cursor.execute(add_values, (new_id, |
||
| 3018 | parameters['name'], |
||
| 3019 | parameters['parameter_type'], |
||
| 3020 | parameters['constant'], |
||
| 3021 | point_id, |
||
| 3022 | numerator_meter_uuid, |
||
| 3023 | denominator_meter_uuid)) |
||
| 3024 | cnx.commit() |
||
| 3025 | cursor.close() |
||
| 3026 | cnx.close() |
||
| 3027 | |||
| 3028 | resp.status = falcon.HTTP_201 |
||
| 3029 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 3030 | |||
| 3031 | |||
| 3032 | class CombinedEquipmentClone: |
||
| 3033 | def __init__(self): |
||
| 3034 | pass |
||
| 3035 | |||
| 3036 | @staticmethod |
||
| 3037 | def on_options(req, resp, id_): |
||
| 3038 | _ = req |
||
| 3039 | resp.status = falcon.HTTP_200 |
||
| 3040 | _ = id_ |
||
| 3041 | |||
| 3042 | @staticmethod |
||
| 3043 | @user_logger |
||
| 3044 | def on_post(req, resp, id_): |
||
| 3045 | """Handles POST requests""" |
||
| 3046 | admin_control(req) |
||
| 3047 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3048 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3049 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 3050 | |||
| 3051 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3052 | cursor = cnx.cursor() |
||
| 3053 | |||
| 3054 | query = (" SELECT id, name, uuid " |
||
| 3055 | " FROM tbl_cost_centers ") |
||
| 3056 | cursor.execute(query) |
||
| 3057 | rows_cost_centers = cursor.fetchall() |
||
| 3058 | |||
| 3059 | cost_center_dict = dict() |
||
| 3060 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 3061 | for row in rows_cost_centers: |
||
| 3062 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 3063 | "name": row[1], |
||
| 3064 | "uuid": row[2]} |
||
| 3065 | |||
| 3066 | query = (" SELECT id, name, uuid, " |
||
| 3067 | " is_input_counted, is_output_counted, " |
||
| 3068 | " cost_center_id, svg_id, camera_url, description " |
||
| 3069 | " FROM tbl_combined_equipments " |
||
| 3070 | " WHERE id = %s ") |
||
| 3071 | cursor.execute(query, (id_,)) |
||
| 3072 | row = cursor.fetchone() |
||
| 3073 | |||
| 3074 | if row is None: |
||
| 3075 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3076 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 3077 | else: |
||
| 3078 | meta_result = {"id": row[0], |
||
| 3079 | "name": row[1], |
||
| 3080 | "uuid": row[2], |
||
| 3081 | "is_input_counted": bool(row[3]), |
||
| 3082 | "is_output_counted": bool(row[4]), |
||
| 3083 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 3084 | "svg_id": row[6], |
||
| 3085 | "camera_url": row[7], |
||
| 3086 | "description": row[8], |
||
| 3087 | "equipments": None, |
||
| 3088 | "commands": None, |
||
| 3089 | "meters": None, |
||
| 3090 | "offline_meters": None, |
||
| 3091 | "virtual_meters": None, |
||
| 3092 | "parameters": None |
||
| 3093 | } |
||
| 3094 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 3095 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 3096 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 3097 | " ORDER BY e.id ") |
||
| 3098 | cursor.execute(query, (id_,)) |
||
| 3099 | rows = cursor.fetchall() |
||
| 3100 | |||
| 3101 | equipment_result = list() |
||
| 3102 | if rows is not None and len(rows) > 0: |
||
| 3103 | for row in rows: |
||
| 3104 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3105 | equipment_result.append(result) |
||
| 3106 | meta_result['equipments'] = equipment_result |
||
| 3107 | |||
| 3108 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 3109 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 3110 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 3111 | " ORDER BY c.id ") |
||
| 3112 | cursor.execute(query, (id_,)) |
||
| 3113 | rows = cursor.fetchall() |
||
| 3114 | |||
| 3115 | command_result = list() |
||
| 3116 | if rows is not None and len(rows) > 0: |
||
| 3117 | for row in rows: |
||
| 3118 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3119 | command_result.append(result) |
||
| 3120 | meta_result['commands'] = command_result |
||
| 3121 | |||
| 3122 | query = (" SELECT id, name, uuid " |
||
| 3123 | " FROM tbl_energy_categories ") |
||
| 3124 | cursor.execute(query) |
||
| 3125 | rows_energy_categories = cursor.fetchall() |
||
| 3126 | |||
| 3127 | energy_category_dict = dict() |
||
| 3128 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3129 | for row in rows_energy_categories: |
||
| 3130 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3131 | "name": row[1], |
||
| 3132 | "uuid": row[2]} |
||
| 3133 | |||
| 3134 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3135 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 3136 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 3137 | " ORDER BY m.id ") |
||
| 3138 | cursor.execute(query, (id_,)) |
||
| 3139 | rows = cursor.fetchall() |
||
| 3140 | |||
| 3141 | meter_result = list() |
||
| 3142 | if rows is not None and len(rows) > 0: |
||
| 3143 | for row in rows: |
||
| 3144 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3145 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3146 | "is_output": bool(row[4])} |
||
| 3147 | meter_result.append(result) |
||
| 3148 | meta_result['meters'] = meter_result |
||
| 3149 | |||
| 3150 | query = (" SELECT id, name, uuid " |
||
| 3151 | " FROM tbl_energy_categories ") |
||
| 3152 | cursor.execute(query) |
||
| 3153 | rows_energy_categories = cursor.fetchall() |
||
| 3154 | |||
| 3155 | energy_category_dict = dict() |
||
| 3156 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3157 | for row in rows_energy_categories: |
||
| 3158 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3159 | "name": row[1], |
||
| 3160 | "uuid": row[2]} |
||
| 3161 | |||
| 3162 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3163 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 3164 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 3165 | " ORDER BY m.id ") |
||
| 3166 | cursor.execute(query, (id_,)) |
||
| 3167 | rows = cursor.fetchall() |
||
| 3168 | |||
| 3169 | offlinemeter_result = list() |
||
| 3170 | if rows is not None and len(rows) > 0: |
||
| 3171 | for row in rows: |
||
| 3172 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3173 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3174 | "is_output": bool(row[4])} |
||
| 3175 | offlinemeter_result.append(result) |
||
| 3176 | meta_result['offline_meters'] = offlinemeter_result |
||
| 3177 | |||
| 3178 | query = (" SELECT id, name, uuid " |
||
| 3179 | " FROM tbl_energy_categories ") |
||
| 3180 | cursor.execute(query) |
||
| 3181 | rows_energy_categories = cursor.fetchall() |
||
| 3182 | |||
| 3183 | energy_category_dict = dict() |
||
| 3184 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3185 | for row in rows_energy_categories: |
||
| 3186 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3187 | "name": row[1], |
||
| 3188 | "uuid": row[2]} |
||
| 3189 | |||
| 3190 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3191 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 3192 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 3193 | " ORDER BY m.id ") |
||
| 3194 | cursor.execute(query, (id_,)) |
||
| 3195 | rows = cursor.fetchall() |
||
| 3196 | |||
| 3197 | virtualmeter_result = list() |
||
| 3198 | if rows is not None and len(rows) > 0: |
||
| 3199 | for row in rows: |
||
| 3200 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3201 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3202 | "is_output": bool(row[4])} |
||
| 3203 | virtualmeter_result.append(result) |
||
| 3204 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 3205 | |||
| 3206 | query = (" SELECT id, name " |
||
| 3207 | " FROM tbl_points ") |
||
| 3208 | cursor.execute(query) |
||
| 3209 | rows_points = cursor.fetchall() |
||
| 3210 | |||
| 3211 | point_dict = dict() |
||
| 3212 | if rows_points is not None and len(rows_points) > 0: |
||
| 3213 | for row in rows_points: |
||
| 3214 | point_dict[row[0]] = {"id": row[0], |
||
| 3215 | "name": row[1]} |
||
| 3216 | |||
| 3217 | query = (" SELECT id, name, uuid " |
||
| 3218 | " FROM tbl_meters ") |
||
| 3219 | cursor.execute(query) |
||
| 3220 | rows_meters = cursor.fetchall() |
||
| 3221 | |||
| 3222 | meter_dict = dict() |
||
| 3223 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3224 | for row in rows_meters: |
||
| 3225 | meter_dict[row[2]] = {"type": 'meter', |
||
| 3226 | "id": row[0], |
||
| 3227 | "name": row[1], |
||
| 3228 | "uuid": row[2]} |
||
| 3229 | |||
| 3230 | query = (" SELECT id, name, uuid " |
||
| 3231 | " FROM tbl_offline_meters ") |
||
| 3232 | cursor.execute(query) |
||
| 3233 | rows_offline_meters = cursor.fetchall() |
||
| 3234 | |||
| 3235 | offline_meter_dict = dict() |
||
| 3236 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 3237 | for row in rows_offline_meters: |
||
| 3238 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 3239 | "id": row[0], |
||
| 3240 | "name": row[1], |
||
| 3241 | "uuid": row[2]} |
||
| 3242 | |||
| 3243 | query = (" SELECT id, name, uuid " |
||
| 3244 | " FROM tbl_virtual_meters ") |
||
| 3245 | cursor.execute(query) |
||
| 3246 | rows_virtual_meters = cursor.fetchall() |
||
| 3247 | |||
| 3248 | virtual_meter_dict = dict() |
||
| 3249 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 3250 | for row in rows_virtual_meters: |
||
| 3251 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 3252 | "id": row[0], |
||
| 3253 | "name": row[1], |
||
| 3254 | "uuid": row[2]} |
||
| 3255 | |||
| 3256 | query = (" SELECT id, name, parameter_type, " |
||
| 3257 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 3258 | " FROM tbl_combined_equipments_parameters " |
||
| 3259 | " WHERE combined_equipment_id = %s " |
||
| 3260 | " ORDER BY id ") |
||
| 3261 | cursor.execute(query, (id_,)) |
||
| 3262 | rows_parameters = cursor.fetchall() |
||
| 3263 | |||
| 3264 | parameter_result = list() |
||
| 3265 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 3266 | for row in rows_parameters: |
||
| 3267 | constant = None |
||
| 3268 | point = None |
||
| 3269 | numerator_meter = None |
||
| 3270 | denominator_meter = None |
||
| 3271 | if row[2] == 'point': |
||
| 3272 | point = point_dict.get(row[4], None) |
||
| 3273 | constant = None |
||
| 3274 | numerator_meter = None |
||
| 3275 | denominator_meter = None |
||
| 3276 | elif row[2] == 'constant': |
||
| 3277 | constant = row[3] |
||
| 3278 | point = None |
||
| 3279 | numerator_meter = None |
||
| 3280 | denominator_meter = None |
||
| 3281 | elif row[2] == 'fraction': |
||
| 3282 | constant = None |
||
| 3283 | point = None |
||
| 3284 | # find numerator meter by uuid |
||
| 3285 | numerator_meter = meter_dict.get(row[5], None) |
||
| 3286 | if numerator_meter is None: |
||
| 3287 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 3288 | if numerator_meter is None: |
||
| 3289 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 3290 | # find denominator meter by uuid |
||
| 3291 | denominator_meter = meter_dict.get(row[6], None) |
||
| 3292 | if denominator_meter is None: |
||
| 3293 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 3294 | if denominator_meter is None: |
||
| 3295 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 3296 | |||
| 3297 | result = {"id": row[0], |
||
| 3298 | "name": row[1], |
||
| 3299 | "parameter_type": row[2], |
||
| 3300 | "constant": constant, |
||
| 3301 | "point": point, |
||
| 3302 | "numerator_meter": numerator_meter, |
||
| 3303 | "denominator_meter": denominator_meter} |
||
| 3304 | parameter_result.append(result) |
||
| 3305 | meta_result['parameters'] = parameter_result |
||
| 3306 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 3307 | if config.utc_offset[0] == '-': |
||
| 3308 | timezone_offset = -timezone_offset |
||
| 3309 | new_name = (str.strip(meta_result['name']) + |
||
| 3310 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 3311 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 3312 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 3313 | " cost_center_id, svg_id, camera_url, description) " |
||
| 3314 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 3315 | cursor.execute(add_values, (new_name, |
||
| 3316 | str(uuid.uuid4()), |
||
| 3317 | meta_result['is_input_counted'], |
||
| 3318 | meta_result['is_output_counted'], |
||
| 3319 | meta_result['cost_center']['id'], |
||
| 3320 | meta_result['svg_id'], |
||
| 3321 | meta_result['camera_url'], |
||
| 3322 | meta_result['description'])) |
||
| 3323 | new_id = cursor.lastrowid |
||
| 3324 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
| 3325 | for equipment in meta_result['equipments']: |
||
| 3326 | cursor.execute(" SELECT name " |
||
| 3327 | " FROM tbl_equipments " |
||
| 3328 | " WHERE id = %s ", (equipment['id'],)) |
||
| 3329 | if cursor.fetchone() is None: |
||
| 3330 | cursor.close() |
||
| 3331 | cnx.close() |
||
| 3332 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3333 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 3334 | |||
| 3335 | query = (" SELECT id " |
||
| 3336 | " FROM tbl_combined_equipments_equipments " |
||
| 3337 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 3338 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 3339 | if cursor.fetchone() is not None: |
||
| 3340 | cursor.close() |
||
| 3341 | cnx.close() |
||
| 3342 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3343 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 3344 | |||
| 3345 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 3346 | " VALUES (%s, %s) ") |
||
| 3347 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 3348 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
| 3349 | for command in meta_result['commands']: |
||
| 3350 | cursor.execute(" SELECT name " |
||
| 3351 | " FROM tbl_commands " |
||
| 3352 | " WHERE id = %s ", (command['id'],)) |
||
| 3353 | if cursor.fetchone() is None: |
||
| 3354 | cursor.close() |
||
| 3355 | cnx.close() |
||
| 3356 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3357 | description='API.COMMAND_NOT_FOUND') |
||
| 3358 | |||
| 3359 | query = (" SELECT id " |
||
| 3360 | " FROM tbl_combined_equipments_commands " |
||
| 3361 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 3362 | cursor.execute(query, (new_id, command['id'],)) |
||
| 3363 | if cursor.fetchone() is not None: |
||
| 3364 | cursor.close() |
||
| 3365 | cnx.close() |
||
| 3366 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3367 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 3368 | |||
| 3369 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 3370 | " VALUES (%s, %s) ") |
||
| 3371 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 3372 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
| 3373 | for meter in meta_result['meters']: |
||
| 3374 | cursor.execute(" SELECT name " |
||
| 3375 | " FROM tbl_meters " |
||
| 3376 | " WHERE id = %s ", (meter['id'],)) |
||
| 3377 | if cursor.fetchone() is None: |
||
| 3378 | cursor.close() |
||
| 3379 | cnx.close() |
||
| 3380 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3381 | description='API.METER_NOT_FOUND') |
||
| 3382 | |||
| 3383 | query = (" SELECT id " |
||
| 3384 | " FROM tbl_combined_equipments_meters " |
||
| 3385 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 3386 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 3387 | if cursor.fetchone() is not None: |
||
| 3388 | cursor.close() |
||
| 3389 | cnx.close() |
||
| 3390 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3391 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 3392 | |||
| 3393 | add_row = ( |
||
| 3394 | " INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 3395 | " VALUES (%s, %s, %s) ") |
||
| 3396 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
| 3397 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
| 3398 | for offline_meter in meta_result['offline_meters']: |
||
| 3399 | cursor.execute(" SELECT name " |
||
| 3400 | " FROM tbl_offline_meters " |
||
| 3401 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 3402 | if cursor.fetchone() is None: |
||
| 3403 | cursor.close() |
||
| 3404 | cnx.close() |
||
| 3405 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3406 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 3407 | |||
| 3408 | query = (" SELECT id " |
||
| 3409 | " FROM tbl_combined_equipments_offline_meters " |
||
| 3410 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 3411 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 3412 | if cursor.fetchone() is not None: |
||
| 3413 | cursor.close() |
||
| 3414 | cnx.close() |
||
| 3415 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3416 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 3417 | |||
| 3418 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 3419 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 3420 | " VALUES (%s, %s, %s) ") |
||
| 3421 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
| 3422 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
| 3423 | for virtual_meter in meta_result['virtual_meters']: |
||
| 3424 | cursor.execute(" SELECT name " |
||
| 3425 | " FROM tbl_virtual_meters " |
||
| 3426 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 3427 | if cursor.fetchone() is None: |
||
| 3428 | cursor.close() |
||
| 3429 | cnx.close() |
||
| 3430 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3431 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 3432 | |||
| 3433 | query = (" SELECT id " |
||
| 3434 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 3435 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 3436 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 3437 | if cursor.fetchone() is not None: |
||
| 3438 | cursor.close() |
||
| 3439 | cnx.close() |
||
| 3440 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3441 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 3442 | |||
| 3443 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 3444 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 3445 | " VALUES (%s, %s, %s) ") |
||
| 3446 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
| 3447 | View Code Duplication | if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0: |
|
| 3448 | for parameters in meta_result['parameters']: |
||
| 3449 | cursor.execute(" SELECT name " |
||
| 3450 | " FROM tbl_combined_equipments_parameters " |
||
| 3451 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
| 3452 | if cursor.fetchone() is not None: |
||
| 3453 | cursor.close() |
||
| 3454 | cnx.close() |
||
| 3455 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3456 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 3457 | if 'point' in parameters: |
||
| 3458 | if parameters['point'] is None: |
||
| 3459 | point_id = None |
||
| 3460 | elif parameters['point']['id'] is not None and \ |
||
| 3461 | parameters['point']['id'] <= 0: |
||
| 3462 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3463 | description='API.INVALID_POINT_ID') |
||
| 3464 | else: |
||
| 3465 | point_id = parameters['point']['id'] |
||
| 3466 | else: |
||
| 3467 | point_id = None |
||
| 3468 | numerator_meter_uuid = None |
||
| 3469 | if 'numerator_meter' in parameters: |
||
| 3470 | if parameters['numerator_meter'] is not None and \ |
||
| 3471 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
| 3472 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
| 3473 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
| 3474 | |||
| 3475 | denominator_meter_uuid = None |
||
| 3476 | if 'denominator_meter' in parameters: |
||
| 3477 | if parameters['denominator_meter'] is not None and \ |
||
| 3478 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
| 3479 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
| 3480 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
| 3481 | |||
| 3482 | # validate by parameter type |
||
| 3483 | if parameters['parameter_type'] == 'point': |
||
| 3484 | if point_id is None: |
||
| 3485 | cursor.close() |
||
| 3486 | cnx.close() |
||
| 3487 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3488 | description='API.INVALID_POINT_ID') |
||
| 3489 | |||
| 3490 | query = (" SELECT id, name " |
||
| 3491 | " FROM tbl_points " |
||
| 3492 | " WHERE id = %s ") |
||
| 3493 | cursor.execute(query, (point_id,)) |
||
| 3494 | if cursor.fetchone() is None: |
||
| 3495 | cursor.close() |
||
| 3496 | cnx.close() |
||
| 3497 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3498 | description='API.POINT_NOT_FOUND') |
||
| 3499 | |||
| 3500 | elif parameters['parameter_type'] == 'constant': |
||
| 3501 | if parameters['constant'] is None: |
||
| 3502 | cursor.close() |
||
| 3503 | cnx.close() |
||
| 3504 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3505 | description='API.INVALID_CONSTANT_VALUE') |
||
| 3506 | |||
| 3507 | elif parameters['parameter_type'] == 'fraction': |
||
| 3508 | |||
| 3509 | query = (" SELECT id, name, uuid " |
||
| 3510 | " FROM tbl_meters ") |
||
| 3511 | cursor.execute(query) |
||
| 3512 | rows_meters = cursor.fetchall() |
||
| 3513 | |||
| 3514 | meter_dict = dict() |
||
| 3515 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3516 | for row in rows_meters: |
||
| 3517 | meter_dict[row[2]] = {"type": 'meter', |
||
| 3518 | "id": row[0], |
||
| 3519 | "name": row[1], |
||
| 3520 | "uuid": row[2]} |
||
| 3521 | |||
| 3522 | query = (" SELECT id, name, uuid " |
||
| 3523 | " FROM tbl_offline_meters ") |
||
| 3524 | cursor.execute(query) |
||
| 3525 | rows_offline_meters = cursor.fetchall() |
||
| 3526 | |||
| 3527 | offline_meter_dict = dict() |
||
| 3528 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 3529 | for row in rows_offline_meters: |
||
| 3530 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 3531 | "id": row[0], |
||
| 3532 | "name": row[1], |
||
| 3533 | "uuid": row[2]} |
||
| 3534 | |||
| 3535 | query = (" SELECT id, name, uuid " |
||
| 3536 | " FROM tbl_virtual_meters ") |
||
| 3537 | cursor.execute(query) |
||
| 3538 | rows_virtual_meters = cursor.fetchall() |
||
| 3539 | |||
| 3540 | virtual_meter_dict = dict() |
||
| 3541 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 3542 | for row in rows_virtual_meters: |
||
| 3543 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 3544 | "id": row[0], |
||
| 3545 | "name": row[1], |
||
| 3546 | "uuid": row[2]} |
||
| 3547 | |||
| 3548 | # validate numerator meter uuid |
||
| 3549 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3550 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3551 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 3552 | cursor.close() |
||
| 3553 | cnx.close() |
||
| 3554 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3555 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 3556 | |||
| 3557 | # validate denominator meter uuid |
||
| 3558 | if denominator_meter_uuid is None: |
||
| 3559 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3560 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3561 | |||
| 3562 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 3563 | cursor.close() |
||
| 3564 | cnx.close() |
||
| 3565 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3566 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3567 | |||
| 3568 | if denominator_meter_uuid not in meter_dict and \ |
||
| 3569 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 3570 | denominator_meter_uuid not in offline_meter_dict: |
||
| 3571 | cursor.close() |
||
| 3572 | cnx.close() |
||
| 3573 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3574 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3575 | |||
| 3576 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 3577 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 3578 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 3579 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 3580 | cursor.execute(add_values, (new_id, |
||
| 3581 | parameters['name'], |
||
| 3582 | parameters['parameter_type'], |
||
| 3583 | parameters['constant'], |
||
| 3584 | point_id, |
||
| 3585 | numerator_meter_uuid, |
||
| 3586 | denominator_meter_uuid)) |
||
| 3587 | cnx.commit() |
||
| 3588 | cursor.close() |
||
| 3589 | cnx.close() |
||
| 3590 | |||
| 3591 | resp.status = falcon.HTTP_201 |
||
| 3592 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 3593 |