| Total Complexity | 786 |
| Total Lines | 3885 |
| Duplicated Lines | 49.55 % |
| 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 | 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 | 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 | cursor.execute(" SELECT data_source_id " |
||
| 866 | " FROM tbl_combined_equipments_data_sources " |
||
| 867 | " WHERE combined_equipment_id = %s ", (id_,)) |
||
| 868 | rows_data_sources = cursor.fetchall() |
||
| 869 | data_source_ids = tuple({row[0] for row in rows_data_sources if row[0] is not None}) |
||
| 870 | |||
| 871 | if len(data_source_ids) > 0: |
||
| 872 | format_strings = ','.join(['%s'] * len(data_source_ids)) |
||
| 873 | query = (" SELECT id, name " |
||
| 874 | " FROM tbl_points " |
||
| 875 | " WHERE data_source_id IN (" + format_strings + ") ") |
||
| 876 | cursor.execute(query, data_source_ids) |
||
| 877 | rows_points = cursor.fetchall() |
||
| 878 | else: |
||
| 879 | rows_points = list() |
||
| 880 | |||
| 881 | point_dict = dict() |
||
| 882 | if rows_points is not None and len(rows_points) > 0: |
||
| 883 | for row in rows_points: |
||
| 884 | point_dict[row[0]] = {"id": row[0], |
||
| 885 | "name": row[1]} |
||
| 886 | |||
| 887 | query = (" SELECT id, name, uuid " |
||
| 888 | " FROM tbl_meters ") |
||
| 889 | cursor.execute(query) |
||
| 890 | rows_meters = cursor.fetchall() |
||
| 891 | |||
| 892 | meter_dict = dict() |
||
| 893 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 894 | for row in rows_meters: |
||
| 895 | meter_dict[row[2]] = {"type": 'meter', |
||
| 896 | "id": row[0], |
||
| 897 | "name": row[1], |
||
| 898 | "uuid": row[2]} |
||
| 899 | |||
| 900 | query = (" SELECT id, name, uuid " |
||
| 901 | " FROM tbl_offline_meters ") |
||
| 902 | cursor.execute(query) |
||
| 903 | rows_offline_meters = cursor.fetchall() |
||
| 904 | |||
| 905 | offline_meter_dict = dict() |
||
| 906 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 907 | for row in rows_offline_meters: |
||
| 908 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 909 | "id": row[0], |
||
| 910 | "name": row[1], |
||
| 911 | "uuid": row[2]} |
||
| 912 | |||
| 913 | query = (" SELECT id, name, uuid " |
||
| 914 | " FROM tbl_virtual_meters ") |
||
| 915 | cursor.execute(query) |
||
| 916 | rows_virtual_meters = cursor.fetchall() |
||
| 917 | |||
| 918 | virtual_meter_dict = dict() |
||
| 919 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 920 | for row in rows_virtual_meters: |
||
| 921 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 922 | "id": row[0], |
||
| 923 | "name": row[1], |
||
| 924 | "uuid": row[2]} |
||
| 925 | |||
| 926 | query = (" SELECT id, name, parameter_type, " |
||
| 927 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 928 | " FROM tbl_combined_equipments_parameters " |
||
| 929 | " WHERE combined_equipment_id = %s " |
||
| 930 | " ORDER BY id ") |
||
| 931 | cursor.execute(query, (id_,)) |
||
| 932 | rows_parameters = cursor.fetchall() |
||
| 933 | |||
| 934 | result = list() |
||
| 935 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 936 | for row in rows_parameters: |
||
| 937 | constant = None |
||
| 938 | point = None |
||
| 939 | numerator_meter = None |
||
| 940 | denominator_meter = None |
||
| 941 | View Code Duplication | if row[2] == 'point': |
|
| 942 | point = point_dict.get(row[4], None) |
||
| 943 | if point is None and row[4] is not None: |
||
| 944 | query = (" SELECT id, name " |
||
| 945 | " FROM tbl_points " |
||
| 946 | " WHERE id = %s ") |
||
| 947 | cursor.execute(query, (row[4],)) |
||
| 948 | point_row = cursor.fetchone() |
||
| 949 | if point_row is not None: |
||
| 950 | point = {"id": point_row[0], |
||
| 951 | "name": point_row[1]} |
||
| 952 | constant = None |
||
| 953 | numerator_meter = None |
||
| 954 | denominator_meter = None |
||
| 955 | elif row[2] == 'constant': |
||
| 956 | constant = row[3] |
||
| 957 | point = None |
||
| 958 | numerator_meter = None |
||
| 959 | denominator_meter = None |
||
| 960 | elif row[2] == 'fraction': |
||
| 961 | constant = None |
||
| 962 | point = None |
||
| 963 | # find numerator meter by uuid |
||
| 964 | numerator_meter = meter_dict.get(row[5], None) |
||
| 965 | if numerator_meter is None: |
||
| 966 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 967 | if numerator_meter is None: |
||
| 968 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 969 | # find denominator meter by uuid |
||
| 970 | denominator_meter = meter_dict.get(row[6], None) |
||
| 971 | if denominator_meter is None: |
||
| 972 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 973 | if denominator_meter is None: |
||
| 974 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 975 | |||
| 976 | meta_result = {"id": row[0], |
||
| 977 | "name": row[1], |
||
| 978 | "parameter_type": row[2], |
||
| 979 | "constant": constant, |
||
| 980 | "point": point, |
||
| 981 | "numerator_meter": numerator_meter, |
||
| 982 | "denominator_meter": denominator_meter} |
||
| 983 | result.append(meta_result) |
||
| 984 | |||
| 985 | cursor.close() |
||
| 986 | cnx.close() |
||
| 987 | resp.text = json.dumps(result) |
||
| 988 | |||
| 989 | @staticmethod |
||
| 990 | @user_logger |
||
| 991 | def on_post(req, resp, id_): |
||
| 992 | """Handles POST requests""" |
||
| 993 | admin_control(req) |
||
| 994 | if not id_.isdigit() or int(id_) <= 0: |
||
| 995 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 996 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 997 | try: |
||
| 998 | raw_json = req.stream.read().decode('utf-8') |
||
| 999 | except Exception as ex: |
||
| 1000 | print(ex) |
||
| 1001 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1002 | title='API.BAD_REQUEST', |
||
| 1003 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1004 | |||
| 1005 | new_values = json.loads(raw_json) |
||
| 1006 | |||
| 1007 | if 'name' not in new_values['data'].keys() or \ |
||
| 1008 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1009 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1011 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 1012 | name = str.strip(new_values['data']['name']) |
||
| 1013 | |||
| 1014 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 1015 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 1016 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 1017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1018 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1019 | |||
| 1020 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 1021 | |||
| 1022 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 1023 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1024 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1025 | |||
| 1026 | constant = None |
||
| 1027 | if 'constant' in new_values['data'].keys(): |
||
| 1028 | if new_values['data']['constant'] is not None and \ |
||
| 1029 | isinstance(new_values['data']['constant'], str) and \ |
||
| 1030 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 1031 | constant = str.strip(new_values['data']['constant']) |
||
| 1032 | |||
| 1033 | point_id = None |
||
| 1034 | if 'point_id' in new_values['data'].keys(): |
||
| 1035 | if new_values['data']['point_id'] is not None and \ |
||
| 1036 | new_values['data']['point_id'] <= 0: |
||
| 1037 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1038 | description='API.INVALID_POINT_ID') |
||
| 1039 | point_id = new_values['data']['point_id'] |
||
| 1040 | |||
| 1041 | numerator_meter_uuid = None |
||
| 1042 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 1043 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 1044 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 1045 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 1046 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 1047 | |||
| 1048 | denominator_meter_uuid = None |
||
| 1049 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 1050 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 1051 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 1052 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 1053 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 1054 | |||
| 1055 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1056 | cursor = cnx.cursor() |
||
| 1057 | |||
| 1058 | cursor.execute(" SELECT name " |
||
| 1059 | " FROM tbl_combined_equipments " |
||
| 1060 | " WHERE id = %s ", (id_,)) |
||
| 1061 | if cursor.fetchone() is None: |
||
| 1062 | cursor.close() |
||
| 1063 | cnx.close() |
||
| 1064 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1065 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1066 | |||
| 1067 | cursor.execute(" SELECT data_source_id " |
||
| 1068 | " FROM tbl_combined_equipments_data_sources " |
||
| 1069 | " WHERE combined_equipment_id = %s ", (id_,)) |
||
| 1070 | rows_data_sources = cursor.fetchall() |
||
| 1071 | data_source_ids = tuple({row[0] for row in rows_data_sources if row[0] is not None}) |
||
| 1072 | |||
| 1073 | cursor.execute(" SELECT data_source_id " |
||
| 1074 | " FROM tbl_combined_equipments_data_sources " |
||
| 1075 | " WHERE combined_equipment_id = %s ", (id_,)) |
||
| 1076 | rows_data_sources = cursor.fetchall() |
||
| 1077 | data_source_ids = tuple({row[0] for row in rows_data_sources if row[0] is not None}) |
||
| 1078 | |||
| 1079 | cursor.execute(" SELECT name " |
||
| 1080 | " FROM tbl_combined_equipments_parameters " |
||
| 1081 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
||
| 1082 | if cursor.fetchone() is not None: |
||
| 1083 | cursor.close() |
||
| 1084 | cnx.close() |
||
| 1085 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1086 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 1087 | |||
| 1088 | # validate by parameter type |
||
| 1089 | View Code Duplication | if parameter_type == 'point': |
|
| 1090 | if point_id is None: |
||
| 1091 | cursor.close() |
||
| 1092 | cnx.close() |
||
| 1093 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1094 | description='API.INVALID_POINT_ID') |
||
| 1095 | |||
| 1096 | if len(data_source_ids) == 0: |
||
| 1097 | cursor.close() |
||
| 1098 | cnx.close() |
||
| 1099 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1100 | description='API.POINT_NOT_FOUND') |
||
| 1101 | |||
| 1102 | format_strings = ','.join(['%s'] * len(data_source_ids)) |
||
| 1103 | query = (" SELECT id, name " |
||
| 1104 | " FROM tbl_points " |
||
| 1105 | " WHERE id = %s AND data_source_id IN (" + format_strings + ") ") |
||
| 1106 | cursor.execute(query, (point_id,) + data_source_ids) |
||
| 1107 | if cursor.fetchone() is None: |
||
| 1108 | cursor.close() |
||
| 1109 | cnx.close() |
||
| 1110 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1111 | description='API.POINT_NOT_FOUND') |
||
| 1112 | |||
| 1113 | elif parameter_type == 'constant': |
||
| 1114 | if constant is None: |
||
| 1115 | cursor.close() |
||
| 1116 | cnx.close() |
||
| 1117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1118 | description='API.INVALID_CONSTANT_VALUE') |
||
| 1119 | |||
| 1120 | elif parameter_type == 'fraction': |
||
| 1121 | |||
| 1122 | query = (" SELECT id, name, uuid " |
||
| 1123 | " FROM tbl_meters ") |
||
| 1124 | cursor.execute(query) |
||
| 1125 | rows_meters = cursor.fetchall() |
||
| 1126 | |||
| 1127 | meter_dict = dict() |
||
| 1128 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1129 | for row in rows_meters: |
||
| 1130 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1131 | "id": row[0], |
||
| 1132 | "name": row[1], |
||
| 1133 | "uuid": row[2]} |
||
| 1134 | |||
| 1135 | query = (" SELECT id, name, uuid " |
||
| 1136 | " FROM tbl_offline_meters ") |
||
| 1137 | cursor.execute(query) |
||
| 1138 | rows_offline_meters = cursor.fetchall() |
||
| 1139 | |||
| 1140 | offline_meter_dict = dict() |
||
| 1141 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1142 | for row in rows_offline_meters: |
||
| 1143 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1144 | "id": row[0], |
||
| 1145 | "name": row[1], |
||
| 1146 | "uuid": row[2]} |
||
| 1147 | |||
| 1148 | query = (" SELECT id, name, uuid " |
||
| 1149 | " FROM tbl_virtual_meters ") |
||
| 1150 | cursor.execute(query) |
||
| 1151 | rows_virtual_meters = cursor.fetchall() |
||
| 1152 | |||
| 1153 | virtual_meter_dict = dict() |
||
| 1154 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1155 | for row in rows_virtual_meters: |
||
| 1156 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1157 | "id": row[0], |
||
| 1158 | "name": row[1], |
||
| 1159 | "uuid": row[2]} |
||
| 1160 | |||
| 1161 | # validate numerator meter uuid |
||
| 1162 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1163 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1164 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 1165 | cursor.close() |
||
| 1166 | cnx.close() |
||
| 1167 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1168 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 1169 | |||
| 1170 | # validate denominator meter uuid |
||
| 1171 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 1172 | cursor.close() |
||
| 1173 | cnx.close() |
||
| 1174 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1175 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1176 | |||
| 1177 | if denominator_meter_uuid not in meter_dict and \ |
||
| 1178 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 1179 | denominator_meter_uuid not in offline_meter_dict: |
||
| 1180 | cursor.close() |
||
| 1181 | cnx.close() |
||
| 1182 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1183 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1184 | |||
| 1185 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 1186 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 1187 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 1188 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 1189 | cursor.execute(add_values, (id_, |
||
| 1190 | name, |
||
| 1191 | parameter_type, |
||
| 1192 | constant, |
||
| 1193 | point_id, |
||
| 1194 | numerator_meter_uuid, |
||
| 1195 | denominator_meter_uuid)) |
||
| 1196 | new_id = cursor.lastrowid |
||
| 1197 | cnx.commit() |
||
| 1198 | cursor.close() |
||
| 1199 | cnx.close() |
||
| 1200 | |||
| 1201 | resp.status = falcon.HTTP_201 |
||
| 1202 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
||
| 1203 | |||
| 1204 | |||
| 1205 | class CombinedEquipmentParameterItem: |
||
| 1206 | def __init__(self): |
||
| 1207 | pass |
||
| 1208 | |||
| 1209 | @staticmethod |
||
| 1210 | def on_options(req, resp, id_, pid): |
||
| 1211 | _ = req |
||
| 1212 | resp.status = falcon.HTTP_200 |
||
| 1213 | _ = id_ |
||
| 1214 | |||
| 1215 | @staticmethod |
||
| 1216 | def on_get(req, resp, id_, pid): |
||
| 1217 | if 'API-KEY' not in req.headers or \ |
||
| 1218 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1219 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1220 | access_control(req) |
||
| 1221 | else: |
||
| 1222 | api_key_control(req) |
||
| 1223 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1224 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1225 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1226 | |||
| 1227 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1228 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1229 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1230 | |||
| 1231 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1232 | cursor = cnx.cursor() |
||
| 1233 | |||
| 1234 | cursor.execute(" SELECT data_source_id " |
||
| 1235 | " FROM tbl_combined_equipments_data_sources " |
||
| 1236 | " WHERE combined_equipment_id = %s ", (id_,)) |
||
| 1237 | rows_data_sources = cursor.fetchall() |
||
| 1238 | data_source_ids = tuple({row[0] for row in rows_data_sources if row[0] is not None}) |
||
| 1239 | |||
| 1240 | if len(data_source_ids) > 0: |
||
| 1241 | format_strings = ','.join(['%s'] * len(data_source_ids)) |
||
| 1242 | query = (" SELECT id, name " |
||
| 1243 | " FROM tbl_points " |
||
| 1244 | " WHERE data_source_id IN (" + format_strings + ") ") |
||
| 1245 | cursor.execute(query, data_source_ids) |
||
| 1246 | rows_points = cursor.fetchall() |
||
| 1247 | else: |
||
| 1248 | rows_points = list() |
||
| 1249 | |||
| 1250 | point_dict = dict() |
||
| 1251 | if rows_points is not None and len(rows_points) > 0: |
||
| 1252 | for row in rows_points: |
||
| 1253 | point_dict[row[0]] = {"id": row[0], |
||
| 1254 | "name": row[1]} |
||
| 1255 | |||
| 1256 | query = (" SELECT id, name, uuid " |
||
| 1257 | " FROM tbl_meters ") |
||
| 1258 | cursor.execute(query) |
||
| 1259 | rows_meters = cursor.fetchall() |
||
| 1260 | |||
| 1261 | meter_dict = dict() |
||
| 1262 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1263 | for row in rows_meters: |
||
| 1264 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1265 | "id": row[0], |
||
| 1266 | "name": row[1], |
||
| 1267 | "uuid": row[2]} |
||
| 1268 | |||
| 1269 | query = (" SELECT id, name, uuid " |
||
| 1270 | " FROM tbl_offline_meters ") |
||
| 1271 | cursor.execute(query) |
||
| 1272 | rows_offline_meters = cursor.fetchall() |
||
| 1273 | |||
| 1274 | offline_meter_dict = dict() |
||
| 1275 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1276 | for row in rows_offline_meters: |
||
| 1277 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1278 | "id": row[0], |
||
| 1279 | "name": row[1], |
||
| 1280 | "uuid": row[2]} |
||
| 1281 | |||
| 1282 | query = (" SELECT id, name, uuid " |
||
| 1283 | " FROM tbl_virtual_meters ") |
||
| 1284 | cursor.execute(query) |
||
| 1285 | rows_virtual_meters = cursor.fetchall() |
||
| 1286 | |||
| 1287 | virtual_meter_dict = dict() |
||
| 1288 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1289 | for row in rows_virtual_meters: |
||
| 1290 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1291 | "id": row[0], |
||
| 1292 | "name": row[1], |
||
| 1293 | "uuid": row[2]} |
||
| 1294 | |||
| 1295 | query = (" SELECT id, name, parameter_type, " |
||
| 1296 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 1297 | " FROM tbl_combined_equipments_parameters " |
||
| 1298 | " WHERE combined_equipment_id = %s AND id = %s ") |
||
| 1299 | cursor.execute(query, (id_, pid)) |
||
| 1300 | row = cursor.fetchone() |
||
| 1301 | |||
| 1302 | if row is None: |
||
| 1303 | cursor.close() |
||
| 1304 | cnx.close() |
||
| 1305 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1306 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1307 | else: |
||
| 1308 | constant = None |
||
| 1309 | point = None |
||
| 1310 | numerator_meter = None |
||
| 1311 | denominator_meter = None |
||
| 1312 | View Code Duplication | if row[2] == 'point': |
|
| 1313 | point = point_dict.get(row[4], None) |
||
| 1314 | if point is None and row[4] is not None: |
||
| 1315 | query = (" SELECT id, name " |
||
| 1316 | " FROM tbl_points " |
||
| 1317 | " WHERE id = %s ") |
||
| 1318 | cursor.execute(query, (row[4],)) |
||
| 1319 | point_row = cursor.fetchone() |
||
| 1320 | if point_row is not None: |
||
| 1321 | point = {"id": point_row[0], |
||
| 1322 | "name": point_row[1]} |
||
| 1323 | constant = None |
||
| 1324 | numerator_meter = None |
||
| 1325 | denominator_meter = None |
||
| 1326 | elif row[2] == 'constant': |
||
| 1327 | constant = row[3] |
||
| 1328 | point = None |
||
| 1329 | numerator_meter = None |
||
| 1330 | denominator_meter = None |
||
| 1331 | elif row[2] == 'fraction': |
||
| 1332 | constant = None |
||
| 1333 | point = None |
||
| 1334 | # find numerator meter by uuid |
||
| 1335 | numerator_meter = meter_dict.get(row[5], None) |
||
| 1336 | if numerator_meter is None: |
||
| 1337 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 1338 | if numerator_meter is None: |
||
| 1339 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 1340 | # find denominator meter by uuid |
||
| 1341 | denominator_meter = meter_dict.get(row[6], None) |
||
| 1342 | if denominator_meter is None: |
||
| 1343 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 1344 | if denominator_meter is None: |
||
| 1345 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 1346 | |||
| 1347 | meta_result = {"id": row[0], |
||
| 1348 | "name": row[1], |
||
| 1349 | "parameter_type": row[2], |
||
| 1350 | "constant": constant, |
||
| 1351 | "point": point, |
||
| 1352 | "numerator_meter": numerator_meter, |
||
| 1353 | "denominator_meter": denominator_meter} |
||
| 1354 | |||
| 1355 | cursor.close() |
||
| 1356 | cnx.close() |
||
| 1357 | resp.text = json.dumps(meta_result) |
||
| 1358 | |||
| 1359 | @staticmethod |
||
| 1360 | @user_logger |
||
| 1361 | def on_delete(req, resp, id_, pid): |
||
| 1362 | admin_control(req) |
||
| 1363 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1364 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1365 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1366 | |||
| 1367 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1368 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1369 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1370 | |||
| 1371 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1372 | cursor = cnx.cursor() |
||
| 1373 | |||
| 1374 | cursor.execute(" SELECT name " |
||
| 1375 | " FROM tbl_combined_equipments " |
||
| 1376 | " WHERE id = %s ", |
||
| 1377 | (id_,)) |
||
| 1378 | row = cursor.fetchone() |
||
| 1379 | if row is None: |
||
| 1380 | cursor.close() |
||
| 1381 | cnx.close() |
||
| 1382 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1383 | title='API.NOT_FOUND', |
||
| 1384 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1385 | |||
| 1386 | cursor.execute(" SELECT name " |
||
| 1387 | " FROM tbl_combined_equipments_parameters " |
||
| 1388 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1389 | (id_, pid,)) |
||
| 1390 | row = cursor.fetchone() |
||
| 1391 | if row is None: |
||
| 1392 | cursor.close() |
||
| 1393 | cnx.close() |
||
| 1394 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1395 | title='API.NOT_FOUND', |
||
| 1396 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1397 | |||
| 1398 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
||
| 1399 | " WHERE id = %s ", (pid,)) |
||
| 1400 | cnx.commit() |
||
| 1401 | |||
| 1402 | cursor.close() |
||
| 1403 | cnx.close() |
||
| 1404 | |||
| 1405 | resp.status = falcon.HTTP_204 |
||
| 1406 | |||
| 1407 | @staticmethod |
||
| 1408 | @user_logger |
||
| 1409 | def on_put(req, resp, id_, pid): |
||
| 1410 | """Handles PUT requests""" |
||
| 1411 | admin_control(req) |
||
| 1412 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1414 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1415 | |||
| 1416 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1417 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1418 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1419 | |||
| 1420 | try: |
||
| 1421 | raw_json = req.stream.read().decode('utf-8') |
||
| 1422 | except Exception as ex: |
||
| 1423 | print(ex) |
||
| 1424 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1425 | title='API.BAD_REQUEST', |
||
| 1426 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1427 | |||
| 1428 | new_values = json.loads(raw_json) |
||
| 1429 | |||
| 1430 | if 'name' not in new_values['data'].keys() or \ |
||
| 1431 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1432 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1433 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1434 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 1435 | name = str.strip(new_values['data']['name']) |
||
| 1436 | |||
| 1437 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 1438 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 1439 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 1440 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1441 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1442 | |||
| 1443 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 1444 | |||
| 1445 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 1446 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1447 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1448 | |||
| 1449 | constant = None |
||
| 1450 | if 'constant' in new_values['data'].keys(): |
||
| 1451 | if new_values['data']['constant'] is not None and \ |
||
| 1452 | isinstance(new_values['data']['constant'], str) and \ |
||
| 1453 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 1454 | constant = str.strip(new_values['data']['constant']) |
||
| 1455 | |||
| 1456 | point_id = None |
||
| 1457 | if 'point_id' in new_values['data'].keys(): |
||
| 1458 | if new_values['data']['point_id'] is not None and \ |
||
| 1459 | new_values['data']['point_id'] <= 0: |
||
| 1460 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1461 | description='API.INVALID_POINT_ID') |
||
| 1462 | point_id = new_values['data']['point_id'] |
||
| 1463 | |||
| 1464 | numerator_meter_uuid = None |
||
| 1465 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 1466 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 1467 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 1468 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 1469 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 1470 | |||
| 1471 | denominator_meter_uuid = None |
||
| 1472 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 1473 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 1474 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 1475 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 1476 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 1477 | |||
| 1478 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1479 | cursor = cnx.cursor() |
||
| 1480 | |||
| 1481 | cursor.execute(" SELECT name " |
||
| 1482 | " FROM tbl_combined_equipments " |
||
| 1483 | " WHERE id = %s ", (id_,)) |
||
| 1484 | if cursor.fetchone() is None: |
||
| 1485 | cursor.close() |
||
| 1486 | cnx.close() |
||
| 1487 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1488 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1489 | |||
| 1490 | cursor.execute(" SELECT name " |
||
| 1491 | " FROM tbl_combined_equipments_parameters " |
||
| 1492 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1493 | (id_, pid,)) |
||
| 1494 | row = cursor.fetchone() |
||
| 1495 | if row is None: |
||
| 1496 | cursor.close() |
||
| 1497 | cnx.close() |
||
| 1498 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1499 | title='API.NOT_FOUND', |
||
| 1500 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1501 | |||
| 1502 | cursor.execute(" SELECT name " |
||
| 1503 | " FROM tbl_combined_equipments_parameters " |
||
| 1504 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
||
| 1505 | row = cursor.fetchone() |
||
| 1506 | if row is not None: |
||
| 1507 | cursor.close() |
||
| 1508 | cnx.close() |
||
| 1509 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1510 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 1511 | |||
| 1512 | # validate by parameter type |
||
| 1513 | View Code Duplication | if parameter_type == 'point': |
|
| 1514 | if point_id is None: |
||
| 1515 | cursor.close() |
||
| 1516 | cnx.close() |
||
| 1517 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1518 | description='API.INVALID_POINT_ID') |
||
| 1519 | |||
| 1520 | if len(data_source_ids) == 0: |
||
| 1521 | cursor.close() |
||
| 1522 | cnx.close() |
||
| 1523 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1524 | description='API.POINT_NOT_FOUND') |
||
| 1525 | |||
| 1526 | format_strings = ','.join(['%s'] * len(data_source_ids)) |
||
| 1527 | query = (" SELECT id, name " |
||
| 1528 | " FROM tbl_points " |
||
| 1529 | " WHERE id = %s AND data_source_id IN (" + format_strings + ") ") |
||
| 1530 | cursor.execute(query, (point_id,) + data_source_ids) |
||
| 1531 | row = cursor.fetchone() |
||
| 1532 | if row is None: |
||
| 1533 | cursor.close() |
||
| 1534 | cnx.close() |
||
| 1535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1536 | description='API.POINT_NOT_FOUND') |
||
| 1537 | |||
| 1538 | elif parameter_type == 'constant': |
||
| 1539 | if constant is None: |
||
| 1540 | cursor.close() |
||
| 1541 | cnx.close() |
||
| 1542 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1543 | description='API.INVALID_CONSTANT_VALUE') |
||
| 1544 | |||
| 1545 | elif parameter_type == 'fraction': |
||
| 1546 | |||
| 1547 | query = (" SELECT id, name, uuid " |
||
| 1548 | " FROM tbl_meters ") |
||
| 1549 | cursor.execute(query) |
||
| 1550 | rows_meters = cursor.fetchall() |
||
| 1551 | |||
| 1552 | meter_dict = dict() |
||
| 1553 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1554 | for row in rows_meters: |
||
| 1555 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1556 | "id": row[0], |
||
| 1557 | "name": row[1], |
||
| 1558 | "uuid": row[2]} |
||
| 1559 | |||
| 1560 | query = (" SELECT id, name, uuid " |
||
| 1561 | " FROM tbl_offline_meters ") |
||
| 1562 | cursor.execute(query) |
||
| 1563 | rows_offline_meters = cursor.fetchall() |
||
| 1564 | |||
| 1565 | offline_meter_dict = dict() |
||
| 1566 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1567 | for row in rows_offline_meters: |
||
| 1568 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1569 | "id": row[0], |
||
| 1570 | "name": row[1], |
||
| 1571 | "uuid": row[2]} |
||
| 1572 | |||
| 1573 | query = (" SELECT id, name, uuid " |
||
| 1574 | " FROM tbl_virtual_meters ") |
||
| 1575 | cursor.execute(query) |
||
| 1576 | rows_virtual_meters = cursor.fetchall() |
||
| 1577 | |||
| 1578 | virtual_meter_dict = dict() |
||
| 1579 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1580 | for row in rows_virtual_meters: |
||
| 1581 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1582 | "id": row[0], |
||
| 1583 | "name": row[1], |
||
| 1584 | "uuid": row[2]} |
||
| 1585 | |||
| 1586 | # validate numerator meter uuid |
||
| 1587 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1588 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1589 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 1590 | cursor.close() |
||
| 1591 | cnx.close() |
||
| 1592 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1593 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 1594 | |||
| 1595 | # validate denominator meter uuid |
||
| 1596 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 1597 | cursor.close() |
||
| 1598 | cnx.close() |
||
| 1599 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1600 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1601 | |||
| 1602 | if denominator_meter_uuid not in meter_dict and \ |
||
| 1603 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 1604 | denominator_meter_uuid not in offline_meter_dict: |
||
| 1605 | cursor.close() |
||
| 1606 | cnx.close() |
||
| 1607 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1608 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1609 | |||
| 1610 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
||
| 1611 | " SET name = %s , parameter_type = %s, constant = %s, " |
||
| 1612 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
||
| 1613 | " WHERE id = %s ") |
||
| 1614 | cursor.execute(add_values, (name, |
||
| 1615 | parameter_type, |
||
| 1616 | constant, |
||
| 1617 | point_id, |
||
| 1618 | numerator_meter_uuid, |
||
| 1619 | denominator_meter_uuid, |
||
| 1620 | pid)) |
||
| 1621 | cnx.commit() |
||
| 1622 | |||
| 1623 | cursor.close() |
||
| 1624 | cnx.close() |
||
| 1625 | |||
| 1626 | resp.status = falcon.HTTP_200 |
||
| 1627 | |||
| 1628 | |||
| 1629 | View Code Duplication | class CombinedEquipmentDataSourceCollection: |
|
| 1630 | def __init__(self): |
||
| 1631 | pass |
||
| 1632 | |||
| 1633 | @staticmethod |
||
| 1634 | def on_options(req, resp, id_): |
||
| 1635 | _ = req |
||
| 1636 | resp.status = falcon.HTTP_200 |
||
| 1637 | _ = id_ |
||
| 1638 | |||
| 1639 | @staticmethod |
||
| 1640 | def on_get(req, resp, id_): |
||
| 1641 | if 'API-KEY' not in req.headers or \ |
||
| 1642 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1643 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1644 | access_control(req) |
||
| 1645 | else: |
||
| 1646 | api_key_control(req) |
||
| 1647 | |||
| 1648 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1650 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1651 | |||
| 1652 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1653 | cursor = cnx.cursor() |
||
| 1654 | |||
| 1655 | cursor.execute(" SELECT name FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 1656 | if cursor.fetchone() is None: |
||
| 1657 | cursor.close() |
||
| 1658 | cnx.close() |
||
| 1659 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1660 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1661 | |||
| 1662 | query = (" SELECT ds.id, ds.name, ds.uuid " |
||
| 1663 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_data_sources ceds, tbl_data_sources ds " |
||
| 1664 | " WHERE ceds.combined_equipment_id = ce.id AND ds.id = ceds.data_source_id AND ce.id = %s " |
||
| 1665 | " ORDER BY ds.id ") |
||
| 1666 | cursor.execute(query, (id_,)) |
||
| 1667 | rows = cursor.fetchall() |
||
| 1668 | |||
| 1669 | result = list() |
||
| 1670 | if rows is not None and len(rows) > 0: |
||
| 1671 | for row in rows: |
||
| 1672 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1673 | result.append(meta_result) |
||
| 1674 | |||
| 1675 | cursor.close() |
||
| 1676 | cnx.close() |
||
| 1677 | |||
| 1678 | resp.text = json.dumps(result) |
||
| 1679 | |||
| 1680 | @staticmethod |
||
| 1681 | @user_logger |
||
| 1682 | def on_post(req, resp, id_): |
||
| 1683 | admin_control(req) |
||
| 1684 | try: |
||
| 1685 | raw_json = req.stream.read().decode('utf-8') |
||
| 1686 | except Exception as ex: |
||
| 1687 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1688 | title='API.BAD_REQUEST', |
||
| 1689 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1690 | |||
| 1691 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1693 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1694 | |||
| 1695 | new_values = json.loads(raw_json) |
||
| 1696 | |||
| 1697 | if 'data_source_id' not in new_values['data'].keys() or \ |
||
| 1698 | not isinstance(new_values['data']['data_source_id'], int) or \ |
||
| 1699 | new_values['data']['data_source_id'] <= 0: |
||
| 1700 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1701 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 1702 | |||
| 1703 | data_source_id = new_values['data']['data_source_id'] |
||
| 1704 | |||
| 1705 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1706 | cursor = cnx.cursor() |
||
| 1707 | |||
| 1708 | cursor.execute(" SELECT name FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 1709 | if cursor.fetchone() is None: |
||
| 1710 | cursor.close() |
||
| 1711 | cnx.close() |
||
| 1712 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1713 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1714 | |||
| 1715 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (data_source_id,)) |
||
| 1716 | if cursor.fetchone() is None: |
||
| 1717 | cursor.close() |
||
| 1718 | cnx.close() |
||
| 1719 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1720 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 1721 | |||
| 1722 | cursor.execute(" SELECT id " |
||
| 1723 | " FROM tbl_combined_equipments_data_sources " |
||
| 1724 | " WHERE combined_equipment_id = %s AND data_source_id = %s", (id_, data_source_id)) |
||
| 1725 | if cursor.fetchone() is not None: |
||
| 1726 | cursor.close() |
||
| 1727 | cnx.close() |
||
| 1728 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1729 | description='API.COMBINED_EQUIPMENT_DATA_SOURCE_RELATION_EXISTS') |
||
| 1730 | |||
| 1731 | cursor.execute(" INSERT INTO tbl_combined_equipments_data_sources (combined_equipment_id, data_source_id) " |
||
| 1732 | " VALUES (%s, %s) ", (id_, data_source_id)) |
||
| 1733 | cnx.commit() |
||
| 1734 | cursor.close() |
||
| 1735 | cnx.close() |
||
| 1736 | |||
| 1737 | resp.status = falcon.HTTP_201 |
||
| 1738 | resp.location = '/combinedequipments/' + str(id_) + '/datasources/' + str(data_source_id) |
||
| 1739 | |||
| 1740 | |||
| 1741 | class CombinedEquipmentDataSourceItem: |
||
| 1742 | def __init__(self): |
||
| 1743 | pass |
||
| 1744 | |||
| 1745 | @staticmethod |
||
| 1746 | def on_options(req, resp, id_, dsid): |
||
| 1747 | _ = req |
||
| 1748 | _ = id_ |
||
| 1749 | _ = dsid |
||
| 1750 | resp.status = falcon.HTTP_200 |
||
| 1751 | |||
| 1752 | @staticmethod |
||
| 1753 | @user_logger |
||
| 1754 | def on_delete(req, resp, id_, dsid): |
||
| 1755 | admin_control(req) |
||
| 1756 | |||
| 1757 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1758 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1759 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1760 | |||
| 1761 | if not dsid.isdigit() or int(dsid) <= 0: |
||
| 1762 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1763 | description='API.INVALID_DATA_SOURCE_ID') |
||
| 1764 | |||
| 1765 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1766 | cursor = cnx.cursor() |
||
| 1767 | |||
| 1768 | cursor.execute(" SELECT name FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 1769 | if cursor.fetchone() is None: |
||
| 1770 | cursor.close() |
||
| 1771 | cnx.close() |
||
| 1772 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1773 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1774 | |||
| 1775 | cursor.execute(" SELECT name FROM tbl_data_sources WHERE id = %s ", (dsid,)) |
||
| 1776 | if cursor.fetchone() is None: |
||
| 1777 | cursor.close() |
||
| 1778 | cnx.close() |
||
| 1779 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1780 | description='API.DATA_SOURCE_NOT_FOUND') |
||
| 1781 | |||
| 1782 | cursor.execute(" SELECT id " |
||
| 1783 | " FROM tbl_combined_equipments_data_sources " |
||
| 1784 | " WHERE combined_equipment_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 1785 | if cursor.fetchone() is None: |
||
| 1786 | cursor.close() |
||
| 1787 | cnx.close() |
||
| 1788 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1789 | description='API.COMBINED_EQUIPMENT_DATA_SOURCE_RELATION_NOT_FOUND') |
||
| 1790 | |||
| 1791 | cursor.execute(" DELETE FROM tbl_combined_equipments_data_sources " |
||
| 1792 | " WHERE combined_equipment_id = %s AND data_source_id = %s ", (id_, dsid)) |
||
| 1793 | cnx.commit() |
||
| 1794 | cursor.close() |
||
| 1795 | cnx.close() |
||
| 1796 | |||
| 1797 | resp.status = falcon.HTTP_204 |
||
| 1798 | |||
| 1799 | |||
| 1800 | class CombinedEquipmentDataSourcePointCollection: |
||
| 1801 | def __init__(self): |
||
| 1802 | pass |
||
| 1803 | |||
| 1804 | @staticmethod |
||
| 1805 | def on_options(req, resp, id_): |
||
| 1806 | _ = req |
||
| 1807 | resp.status = falcon.HTTP_200 |
||
| 1808 | _ = id_ |
||
| 1809 | |||
| 1810 | @staticmethod |
||
| 1811 | def on_get(req, resp, id_): |
||
| 1812 | if 'API-KEY' not in req.headers or \ |
||
| 1813 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1814 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1815 | access_control(req) |
||
| 1816 | else: |
||
| 1817 | api_key_control(req) |
||
| 1818 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1819 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1820 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1821 | |||
| 1822 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1823 | cursor = cnx.cursor() |
||
| 1824 | |||
| 1825 | cursor.execute(" SELECT name FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 1826 | if cursor.fetchone() is None: |
||
| 1827 | cursor.close() |
||
| 1828 | cnx.close() |
||
| 1829 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1830 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1831 | |||
| 1832 | query = (" SELECT p.id, p.name " |
||
| 1833 | " FROM tbl_points p, tbl_combined_equipments_data_sources ceds, tbl_data_sources ds " |
||
| 1834 | " WHERE ceds.combined_equipment_id = %s " |
||
| 1835 | " AND ceds.data_source_id = ds.id " |
||
| 1836 | " AND p.data_source_id = ds.id " |
||
| 1837 | " ORDER BY p.id ") |
||
| 1838 | cursor.execute(query, (id_,)) |
||
| 1839 | rows = cursor.fetchall() |
||
| 1840 | |||
| 1841 | result = list() |
||
| 1842 | if rows is not None and len(rows) > 0: |
||
| 1843 | for row in rows: |
||
| 1844 | meta_result = {"id": row[0], "name": row[1]} |
||
| 1845 | result.append(meta_result) |
||
| 1846 | |||
| 1847 | cursor.close() |
||
| 1848 | cnx.close() |
||
| 1849 | |||
| 1850 | resp.text = json.dumps(result) |
||
| 1851 | |||
| 1852 | |||
| 1853 | View Code Duplication | class CombinedEquipmentMeterCollection: |
|
| 1854 | def __init__(self): |
||
| 1855 | pass |
||
| 1856 | |||
| 1857 | @staticmethod |
||
| 1858 | def on_options(req, resp, id_): |
||
| 1859 | _ = req |
||
| 1860 | resp.status = falcon.HTTP_200 |
||
| 1861 | _ = id_ |
||
| 1862 | |||
| 1863 | @staticmethod |
||
| 1864 | def on_get(req, resp, id_): |
||
| 1865 | if 'API-KEY' not in req.headers or \ |
||
| 1866 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1867 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1868 | access_control(req) |
||
| 1869 | else: |
||
| 1870 | api_key_control(req) |
||
| 1871 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1872 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1873 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1874 | |||
| 1875 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1876 | cursor = cnx.cursor() |
||
| 1877 | |||
| 1878 | cursor.execute(" SELECT name " |
||
| 1879 | " FROM tbl_combined_equipments " |
||
| 1880 | " WHERE id = %s ", (id_,)) |
||
| 1881 | if cursor.fetchone() is None: |
||
| 1882 | cursor.close() |
||
| 1883 | cnx.close() |
||
| 1884 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1885 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1886 | |||
| 1887 | query = (" SELECT id, name, uuid " |
||
| 1888 | " FROM tbl_energy_categories ") |
||
| 1889 | cursor.execute(query) |
||
| 1890 | rows_energy_categories = cursor.fetchall() |
||
| 1891 | |||
| 1892 | energy_category_dict = dict() |
||
| 1893 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1894 | for row in rows_energy_categories: |
||
| 1895 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 1896 | "name": row[1], |
||
| 1897 | "uuid": row[2]} |
||
| 1898 | |||
| 1899 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1900 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 1901 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 1902 | " ORDER BY m.id ") |
||
| 1903 | cursor.execute(query, (id_,)) |
||
| 1904 | rows = cursor.fetchall() |
||
| 1905 | |||
| 1906 | result = list() |
||
| 1907 | if rows is not None and len(rows) > 0: |
||
| 1908 | for row in rows: |
||
| 1909 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 1910 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 1911 | "is_output": bool(row[4])} |
||
| 1912 | result.append(meta_result) |
||
| 1913 | |||
| 1914 | cursor.close() |
||
| 1915 | cnx.close() |
||
| 1916 | |||
| 1917 | resp.text = json.dumps(result) |
||
| 1918 | |||
| 1919 | @staticmethod |
||
| 1920 | @user_logger |
||
| 1921 | def on_post(req, resp, id_): |
||
| 1922 | """Handles POST requests""" |
||
| 1923 | admin_control(req) |
||
| 1924 | try: |
||
| 1925 | raw_json = req.stream.read().decode('utf-8') |
||
| 1926 | except Exception as ex: |
||
| 1927 | print(ex) |
||
| 1928 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1929 | title='API.BAD_REQUEST', |
||
| 1930 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1931 | |||
| 1932 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1933 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1934 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1935 | |||
| 1936 | new_values = json.loads(raw_json) |
||
| 1937 | |||
| 1938 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1939 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1940 | new_values['data']['meter_id'] <= 0: |
||
| 1941 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1942 | description='API.INVALID_METER_ID') |
||
| 1943 | meter_id = new_values['data']['meter_id'] |
||
| 1944 | |||
| 1945 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1946 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1947 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1948 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1949 | is_output = new_values['data']['is_output'] |
||
| 1950 | |||
| 1951 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1952 | cursor = cnx.cursor() |
||
| 1953 | |||
| 1954 | cursor.execute(" SELECT name " |
||
| 1955 | " from tbl_combined_equipments " |
||
| 1956 | " WHERE id = %s ", (id_,)) |
||
| 1957 | if cursor.fetchone() is None: |
||
| 1958 | cursor.close() |
||
| 1959 | cnx.close() |
||
| 1960 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1961 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1962 | |||
| 1963 | cursor.execute(" SELECT name " |
||
| 1964 | " FROM tbl_meters " |
||
| 1965 | " WHERE id = %s ", (meter_id,)) |
||
| 1966 | if cursor.fetchone() is None: |
||
| 1967 | cursor.close() |
||
| 1968 | cnx.close() |
||
| 1969 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1970 | description='API.METER_NOT_FOUND') |
||
| 1971 | |||
| 1972 | query = (" SELECT id " |
||
| 1973 | " FROM tbl_combined_equipments_meters " |
||
| 1974 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 1975 | cursor.execute(query, (id_, meter_id,)) |
||
| 1976 | if cursor.fetchone() is not None: |
||
| 1977 | cursor.close() |
||
| 1978 | cnx.close() |
||
| 1979 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1980 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 1981 | |||
| 1982 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 1983 | " VALUES (%s, %s, %s) ") |
||
| 1984 | cursor.execute(add_row, (id_, meter_id, is_output)) |
||
| 1985 | cnx.commit() |
||
| 1986 | cursor.close() |
||
| 1987 | cnx.close() |
||
| 1988 | |||
| 1989 | resp.status = falcon.HTTP_201 |
||
| 1990 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
||
| 1991 | |||
| 1992 | |||
| 1993 | class CombinedEquipmentMeterItem: |
||
| 1994 | def __init__(self): |
||
| 1995 | pass |
||
| 1996 | |||
| 1997 | @staticmethod |
||
| 1998 | def on_options(req, resp, id_, mid): |
||
| 1999 | _ = req |
||
| 2000 | resp.status = falcon.HTTP_200 |
||
| 2001 | _ = id_ |
||
| 2002 | |||
| 2003 | @staticmethod |
||
| 2004 | @user_logger |
||
| 2005 | def on_delete(req, resp, id_, mid): |
||
| 2006 | admin_control(req) |
||
| 2007 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2008 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2009 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2010 | |||
| 2011 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2012 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2013 | description='API.INVALID_METER_ID') |
||
| 2014 | |||
| 2015 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2016 | cursor = cnx.cursor() |
||
| 2017 | |||
| 2018 | cursor.execute(" SELECT name " |
||
| 2019 | " FROM tbl_combined_equipments " |
||
| 2020 | " WHERE id = %s ", (id_,)) |
||
| 2021 | if cursor.fetchone() is None: |
||
| 2022 | cursor.close() |
||
| 2023 | cnx.close() |
||
| 2024 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2025 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2026 | |||
| 2027 | cursor.execute(" SELECT name " |
||
| 2028 | " FROM tbl_meters " |
||
| 2029 | " WHERE id = %s ", (mid,)) |
||
| 2030 | if cursor.fetchone() is None: |
||
| 2031 | cursor.close() |
||
| 2032 | cnx.close() |
||
| 2033 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2034 | description='API.METER_NOT_FOUND') |
||
| 2035 | |||
| 2036 | cursor.execute(" SELECT id " |
||
| 2037 | " FROM tbl_combined_equipments_meters " |
||
| 2038 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 2039 | if cursor.fetchone() is None: |
||
| 2040 | cursor.close() |
||
| 2041 | cnx.close() |
||
| 2042 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2043 | description='API.COMBINED_EQUIPMENT_METER_RELATION_NOT_FOUND') |
||
| 2044 | |||
| 2045 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters " |
||
| 2046 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 2047 | cnx.commit() |
||
| 2048 | |||
| 2049 | cursor.close() |
||
| 2050 | cnx.close() |
||
| 2051 | |||
| 2052 | resp.status = falcon.HTTP_204 |
||
| 2053 | |||
| 2054 | |||
| 2055 | View Code Duplication | class CombinedEquipmentOfflineMeterCollection: |
|
| 2056 | def __init__(self): |
||
| 2057 | pass |
||
| 2058 | |||
| 2059 | @staticmethod |
||
| 2060 | def on_options(req, resp, id_): |
||
| 2061 | _ = req |
||
| 2062 | resp.status = falcon.HTTP_200 |
||
| 2063 | _ = id_ |
||
| 2064 | |||
| 2065 | @staticmethod |
||
| 2066 | def on_get(req, resp, id_): |
||
| 2067 | if 'API-KEY' not in req.headers or \ |
||
| 2068 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2069 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2070 | access_control(req) |
||
| 2071 | else: |
||
| 2072 | api_key_control(req) |
||
| 2073 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2074 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2075 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2076 | |||
| 2077 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2078 | cursor = cnx.cursor() |
||
| 2079 | |||
| 2080 | cursor.execute(" SELECT name " |
||
| 2081 | " FROM tbl_combined_equipments " |
||
| 2082 | " WHERE id = %s ", (id_,)) |
||
| 2083 | if cursor.fetchone() is None: |
||
| 2084 | cursor.close() |
||
| 2085 | cnx.close() |
||
| 2086 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2087 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2088 | |||
| 2089 | query = (" SELECT id, name, uuid " |
||
| 2090 | " FROM tbl_energy_categories ") |
||
| 2091 | cursor.execute(query) |
||
| 2092 | rows_energy_categories = cursor.fetchall() |
||
| 2093 | |||
| 2094 | energy_category_dict = dict() |
||
| 2095 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2096 | for row in rows_energy_categories: |
||
| 2097 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2098 | "name": row[1], |
||
| 2099 | "uuid": row[2]} |
||
| 2100 | |||
| 2101 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2102 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 2103 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 2104 | " ORDER BY m.id ") |
||
| 2105 | cursor.execute(query, (id_,)) |
||
| 2106 | rows = cursor.fetchall() |
||
| 2107 | |||
| 2108 | result = list() |
||
| 2109 | if rows is not None and len(rows) > 0: |
||
| 2110 | for row in rows: |
||
| 2111 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2112 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2113 | "is_output": bool(row[4])} |
||
| 2114 | result.append(meta_result) |
||
| 2115 | |||
| 2116 | cursor.close() |
||
| 2117 | cnx.close() |
||
| 2118 | |||
| 2119 | resp.text = json.dumps(result) |
||
| 2120 | |||
| 2121 | @staticmethod |
||
| 2122 | @user_logger |
||
| 2123 | def on_post(req, resp, id_): |
||
| 2124 | """Handles POST requests""" |
||
| 2125 | admin_control(req) |
||
| 2126 | try: |
||
| 2127 | raw_json = req.stream.read().decode('utf-8') |
||
| 2128 | except Exception as ex: |
||
| 2129 | print(ex) |
||
| 2130 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2131 | title='API.BAD_REQUEST', |
||
| 2132 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2133 | |||
| 2134 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2136 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2137 | |||
| 2138 | new_values = json.loads(raw_json) |
||
| 2139 | |||
| 2140 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 2141 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 2142 | new_values['data']['offline_meter_id'] <= 0: |
||
| 2143 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2144 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 2145 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 2146 | |||
| 2147 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 2148 | not isinstance(new_values['data']['is_output'], bool): |
||
| 2149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2150 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 2151 | is_output = new_values['data']['is_output'] |
||
| 2152 | |||
| 2153 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2154 | cursor = cnx.cursor() |
||
| 2155 | |||
| 2156 | cursor.execute(" SELECT name " |
||
| 2157 | " from tbl_combined_equipments " |
||
| 2158 | " WHERE id = %s ", (id_,)) |
||
| 2159 | if cursor.fetchone() is None: |
||
| 2160 | cursor.close() |
||
| 2161 | cnx.close() |
||
| 2162 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2163 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2164 | |||
| 2165 | cursor.execute(" SELECT name " |
||
| 2166 | " FROM tbl_offline_meters " |
||
| 2167 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 2168 | if cursor.fetchone() is None: |
||
| 2169 | cursor.close() |
||
| 2170 | cnx.close() |
||
| 2171 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2172 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2173 | |||
| 2174 | query = (" SELECT id " |
||
| 2175 | " FROM tbl_combined_equipments_offline_meters " |
||
| 2176 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 2177 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 2178 | if cursor.fetchone() is not None: |
||
| 2179 | cursor.close() |
||
| 2180 | cnx.close() |
||
| 2181 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2182 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 2183 | |||
| 2184 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 2185 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 2186 | " VALUES (%s, %s, %s) ") |
||
| 2187 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
||
| 2188 | cnx.commit() |
||
| 2189 | cursor.close() |
||
| 2190 | cnx.close() |
||
| 2191 | |||
| 2192 | resp.status = falcon.HTTP_201 |
||
| 2193 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 2194 | |||
| 2195 | |||
| 2196 | class CombinedEquipmentOfflineMeterItem: |
||
| 2197 | def __init__(self): |
||
| 2198 | pass |
||
| 2199 | |||
| 2200 | @staticmethod |
||
| 2201 | def on_options(req, resp, id_, mid): |
||
| 2202 | _ = req |
||
| 2203 | resp.status = falcon.HTTP_200 |
||
| 2204 | _ = id_ |
||
| 2205 | |||
| 2206 | @staticmethod |
||
| 2207 | @user_logger |
||
| 2208 | def on_delete(req, resp, id_, mid): |
||
| 2209 | admin_control(req) |
||
| 2210 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2211 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2212 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2213 | |||
| 2214 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2215 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2216 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 2217 | |||
| 2218 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2219 | cursor = cnx.cursor() |
||
| 2220 | |||
| 2221 | cursor.execute(" SELECT name " |
||
| 2222 | " FROM tbl_combined_equipments " |
||
| 2223 | " WHERE id = %s ", (id_,)) |
||
| 2224 | if cursor.fetchone() is None: |
||
| 2225 | cursor.close() |
||
| 2226 | cnx.close() |
||
| 2227 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2228 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2229 | |||
| 2230 | cursor.execute(" SELECT name " |
||
| 2231 | " FROM tbl_offline_meters " |
||
| 2232 | " WHERE id = %s ", (mid,)) |
||
| 2233 | if cursor.fetchone() is None: |
||
| 2234 | cursor.close() |
||
| 2235 | cnx.close() |
||
| 2236 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2237 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2238 | |||
| 2239 | cursor.execute(" SELECT id " |
||
| 2240 | " FROM tbl_combined_equipments_offline_meters " |
||
| 2241 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 2242 | if cursor.fetchone() is None: |
||
| 2243 | cursor.close() |
||
| 2244 | cnx.close() |
||
| 2245 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2246 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 2247 | |||
| 2248 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters " |
||
| 2249 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 2250 | cnx.commit() |
||
| 2251 | |||
| 2252 | cursor.close() |
||
| 2253 | cnx.close() |
||
| 2254 | |||
| 2255 | resp.status = falcon.HTTP_204 |
||
| 2256 | |||
| 2257 | |||
| 2258 | View Code Duplication | class CombinedEquipmentVirtualMeterCollection: |
|
| 2259 | def __init__(self): |
||
| 2260 | pass |
||
| 2261 | |||
| 2262 | @staticmethod |
||
| 2263 | def on_options(req, resp, id_): |
||
| 2264 | _ = req |
||
| 2265 | resp.status = falcon.HTTP_200 |
||
| 2266 | _ = id_ |
||
| 2267 | |||
| 2268 | @staticmethod |
||
| 2269 | def on_get(req, resp, id_): |
||
| 2270 | if 'API-KEY' not in req.headers or \ |
||
| 2271 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2272 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2273 | access_control(req) |
||
| 2274 | else: |
||
| 2275 | api_key_control(req) |
||
| 2276 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2277 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2278 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2279 | |||
| 2280 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2281 | cursor = cnx.cursor() |
||
| 2282 | |||
| 2283 | cursor.execute(" SELECT name " |
||
| 2284 | " FROM tbl_combined_equipments " |
||
| 2285 | " WHERE id = %s ", (id_,)) |
||
| 2286 | if cursor.fetchone() is None: |
||
| 2287 | cursor.close() |
||
| 2288 | cnx.close() |
||
| 2289 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2290 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2291 | |||
| 2292 | query = (" SELECT id, name, uuid " |
||
| 2293 | " FROM tbl_energy_categories ") |
||
| 2294 | cursor.execute(query) |
||
| 2295 | rows_energy_categories = cursor.fetchall() |
||
| 2296 | |||
| 2297 | energy_category_dict = dict() |
||
| 2298 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2299 | for row in rows_energy_categories: |
||
| 2300 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2301 | "name": row[1], |
||
| 2302 | "uuid": row[2]} |
||
| 2303 | |||
| 2304 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2305 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 2306 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 2307 | " ORDER BY m.id ") |
||
| 2308 | cursor.execute(query, (id_,)) |
||
| 2309 | rows = cursor.fetchall() |
||
| 2310 | |||
| 2311 | result = list() |
||
| 2312 | if rows is not None and len(rows) > 0: |
||
| 2313 | for row in rows: |
||
| 2314 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2315 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2316 | "is_output": bool(row[4])} |
||
| 2317 | result.append(meta_result) |
||
| 2318 | |||
| 2319 | cursor.close() |
||
| 2320 | cnx.close() |
||
| 2321 | resp.text = json.dumps(result) |
||
| 2322 | |||
| 2323 | @staticmethod |
||
| 2324 | @user_logger |
||
| 2325 | def on_post(req, resp, id_): |
||
| 2326 | """Handles POST requests""" |
||
| 2327 | admin_control(req) |
||
| 2328 | try: |
||
| 2329 | raw_json = req.stream.read().decode('utf-8') |
||
| 2330 | except Exception as ex: |
||
| 2331 | print(ex) |
||
| 2332 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2333 | title='API.BAD_REQUEST', |
||
| 2334 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2335 | |||
| 2336 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2337 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2338 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2339 | |||
| 2340 | new_values = json.loads(raw_json) |
||
| 2341 | |||
| 2342 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 2343 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 2344 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 2345 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2346 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2347 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 2348 | |||
| 2349 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 2350 | not isinstance(new_values['data']['is_output'], bool): |
||
| 2351 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2352 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 2353 | is_output = new_values['data']['is_output'] |
||
| 2354 | |||
| 2355 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2356 | cursor = cnx.cursor() |
||
| 2357 | |||
| 2358 | cursor.execute(" SELECT name " |
||
| 2359 | " from tbl_combined_equipments " |
||
| 2360 | " WHERE id = %s ", (id_,)) |
||
| 2361 | if cursor.fetchone() is None: |
||
| 2362 | cursor.close() |
||
| 2363 | cnx.close() |
||
| 2364 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2365 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2366 | |||
| 2367 | cursor.execute(" SELECT name " |
||
| 2368 | " FROM tbl_virtual_meters " |
||
| 2369 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 2370 | if cursor.fetchone() is None: |
||
| 2371 | cursor.close() |
||
| 2372 | cnx.close() |
||
| 2373 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2374 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2375 | |||
| 2376 | query = (" SELECT id " |
||
| 2377 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 2378 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 2379 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 2380 | if cursor.fetchone() is not None: |
||
| 2381 | cursor.close() |
||
| 2382 | cnx.close() |
||
| 2383 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2384 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 2385 | |||
| 2386 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 2387 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 2388 | " VALUES (%s, %s, %s) ") |
||
| 2389 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
||
| 2390 | cnx.commit() |
||
| 2391 | cursor.close() |
||
| 2392 | cnx.close() |
||
| 2393 | |||
| 2394 | resp.status = falcon.HTTP_201 |
||
| 2395 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 2396 | |||
| 2397 | |||
| 2398 | class CombinedEquipmentVirtualMeterItem: |
||
| 2399 | def __init__(self): |
||
| 2400 | pass |
||
| 2401 | |||
| 2402 | @staticmethod |
||
| 2403 | def on_options(req, resp, id_, mid): |
||
| 2404 | _ = req |
||
| 2405 | resp.status = falcon.HTTP_200 |
||
| 2406 | _ = id_ |
||
| 2407 | |||
| 2408 | @staticmethod |
||
| 2409 | @user_logger |
||
| 2410 | def on_delete(req, resp, id_, mid): |
||
| 2411 | admin_control(req) |
||
| 2412 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2414 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2415 | |||
| 2416 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2417 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2418 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2419 | |||
| 2420 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2421 | cursor = cnx.cursor() |
||
| 2422 | |||
| 2423 | cursor.execute(" SELECT name " |
||
| 2424 | " FROM tbl_combined_equipments " |
||
| 2425 | " WHERE id = %s ", (id_,)) |
||
| 2426 | if cursor.fetchone() is None: |
||
| 2427 | cursor.close() |
||
| 2428 | cnx.close() |
||
| 2429 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2430 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2431 | |||
| 2432 | cursor.execute(" SELECT name " |
||
| 2433 | " FROM tbl_virtual_meters " |
||
| 2434 | " WHERE id = %s ", (mid,)) |
||
| 2435 | if cursor.fetchone() is None: |
||
| 2436 | cursor.close() |
||
| 2437 | cnx.close() |
||
| 2438 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2439 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2440 | |||
| 2441 | cursor.execute(" SELECT id " |
||
| 2442 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 2443 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2444 | if cursor.fetchone() is None: |
||
| 2445 | cursor.close() |
||
| 2446 | cnx.close() |
||
| 2447 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2448 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 2449 | |||
| 2450 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters " |
||
| 2451 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2452 | cnx.commit() |
||
| 2453 | |||
| 2454 | cursor.close() |
||
| 2455 | cnx.close() |
||
| 2456 | |||
| 2457 | resp.status = falcon.HTTP_204 |
||
| 2458 | |||
| 2459 | |||
| 2460 | View Code Duplication | class CombinedEquipmentCommandCollection: |
|
| 2461 | def __init__(self): |
||
| 2462 | pass |
||
| 2463 | |||
| 2464 | @staticmethod |
||
| 2465 | def on_options(req, resp, id_): |
||
| 2466 | _ = req |
||
| 2467 | resp.status = falcon.HTTP_200 |
||
| 2468 | _ = id_ |
||
| 2469 | |||
| 2470 | @staticmethod |
||
| 2471 | def on_get(req, resp, id_): |
||
| 2472 | if 'API-KEY' not in req.headers or \ |
||
| 2473 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2474 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2475 | access_control(req) |
||
| 2476 | else: |
||
| 2477 | api_key_control(req) |
||
| 2478 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2479 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2480 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2481 | |||
| 2482 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2483 | cursor = cnx.cursor() |
||
| 2484 | |||
| 2485 | cursor.execute(" SELECT name " |
||
| 2486 | " FROM tbl_combined_equipments " |
||
| 2487 | " WHERE id = %s ", (id_,)) |
||
| 2488 | if cursor.fetchone() is None: |
||
| 2489 | cursor.close() |
||
| 2490 | cnx.close() |
||
| 2491 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2492 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2493 | |||
| 2494 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2495 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 2496 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 2497 | " ORDER BY c.id ") |
||
| 2498 | cursor.execute(query, (id_,)) |
||
| 2499 | rows = cursor.fetchall() |
||
| 2500 | |||
| 2501 | result = list() |
||
| 2502 | if rows is not None and len(rows) > 0: |
||
| 2503 | for row in rows: |
||
| 2504 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2505 | result.append(meta_result) |
||
| 2506 | |||
| 2507 | resp.text = json.dumps(result) |
||
| 2508 | |||
| 2509 | @staticmethod |
||
| 2510 | @user_logger |
||
| 2511 | def on_post(req, resp, id_): |
||
| 2512 | """Handles POST requests""" |
||
| 2513 | admin_control(req) |
||
| 2514 | try: |
||
| 2515 | raw_json = req.stream.read().decode('utf-8') |
||
| 2516 | except Exception as ex: |
||
| 2517 | print(ex) |
||
| 2518 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2519 | title='API.BAD_REQUEST', |
||
| 2520 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2521 | |||
| 2522 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2523 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2524 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2525 | |||
| 2526 | new_values = json.loads(raw_json) |
||
| 2527 | |||
| 2528 | if 'command_id' not in new_values['data'].keys() or \ |
||
| 2529 | not isinstance(new_values['data']['command_id'], int) or \ |
||
| 2530 | new_values['data']['command_id'] <= 0: |
||
| 2531 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2532 | description='API.INVALID_COMMAND_ID') |
||
| 2533 | command_id = new_values['data']['command_id'] |
||
| 2534 | |||
| 2535 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2536 | cursor = cnx.cursor() |
||
| 2537 | |||
| 2538 | cursor.execute(" SELECT name " |
||
| 2539 | " from tbl_combined_equipments " |
||
| 2540 | " WHERE id = %s ", (id_,)) |
||
| 2541 | if cursor.fetchone() is None: |
||
| 2542 | cursor.close() |
||
| 2543 | cnx.close() |
||
| 2544 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2545 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2546 | |||
| 2547 | cursor.execute(" SELECT name " |
||
| 2548 | " FROM tbl_commands " |
||
| 2549 | " WHERE id = %s ", (command_id,)) |
||
| 2550 | if cursor.fetchone() is None: |
||
| 2551 | cursor.close() |
||
| 2552 | cnx.close() |
||
| 2553 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2554 | description='API.COMMAND_NOT_FOUND') |
||
| 2555 | |||
| 2556 | query = (" SELECT id " |
||
| 2557 | " FROM tbl_combined_equipments_commands " |
||
| 2558 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 2559 | cursor.execute(query, (id_, command_id,)) |
||
| 2560 | if cursor.fetchone() is not None: |
||
| 2561 | cursor.close() |
||
| 2562 | cnx.close() |
||
| 2563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2564 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 2565 | |||
| 2566 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 2567 | " VALUES (%s, %s) ") |
||
| 2568 | cursor.execute(add_row, (id_, command_id,)) |
||
| 2569 | cnx.commit() |
||
| 2570 | cursor.close() |
||
| 2571 | cnx.close() |
||
| 2572 | |||
| 2573 | resp.status = falcon.HTTP_201 |
||
| 2574 | resp.location = '/combinedequipments/' + str(id_) + '/commands/' + str(command_id) |
||
| 2575 | |||
| 2576 | |||
| 2577 | class CombinedEquipmentCommandItem: |
||
| 2578 | def __init__(self): |
||
| 2579 | pass |
||
| 2580 | |||
| 2581 | @staticmethod |
||
| 2582 | def on_options(req, resp, id_, cid): |
||
| 2583 | _ = req |
||
| 2584 | resp.status = falcon.HTTP_200 |
||
| 2585 | _ = id_ |
||
| 2586 | |||
| 2587 | @staticmethod |
||
| 2588 | @user_logger |
||
| 2589 | def on_delete(req, resp, id_, cid): |
||
| 2590 | admin_control(req) |
||
| 2591 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2592 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2593 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2594 | |||
| 2595 | if not cid.isdigit() or int(cid) <= 0: |
||
| 2596 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2597 | description='API.INVALID_COMMAND_ID') |
||
| 2598 | |||
| 2599 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2600 | cursor = cnx.cursor() |
||
| 2601 | |||
| 2602 | cursor.execute(" SELECT name " |
||
| 2603 | " FROM tbl_combined_equipments " |
||
| 2604 | " WHERE id = %s ", (id_,)) |
||
| 2605 | if cursor.fetchone() is None: |
||
| 2606 | cursor.close() |
||
| 2607 | cnx.close() |
||
| 2608 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2609 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2610 | |||
| 2611 | cursor.execute(" SELECT name " |
||
| 2612 | " FROM tbl_commands " |
||
| 2613 | " WHERE id = %s ", (cid,)) |
||
| 2614 | if cursor.fetchone() is None: |
||
| 2615 | cursor.close() |
||
| 2616 | cnx.close() |
||
| 2617 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2618 | description='API.COMMAND_NOT_FOUND') |
||
| 2619 | |||
| 2620 | cursor.execute(" SELECT id " |
||
| 2621 | " FROM tbl_combined_equipments_commands " |
||
| 2622 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2623 | if cursor.fetchone() is None: |
||
| 2624 | cursor.close() |
||
| 2625 | cnx.close() |
||
| 2626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2627 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_NOT_FOUND') |
||
| 2628 | |||
| 2629 | cursor.execute(" DELETE FROM tbl_combined_equipments_commands " |
||
| 2630 | " WHERE combined_equipment_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2631 | cnx.commit() |
||
| 2632 | |||
| 2633 | cursor.close() |
||
| 2634 | cnx.close() |
||
| 2635 | |||
| 2636 | resp.status = falcon.HTTP_204 |
||
| 2637 | |||
| 2638 | |||
| 2639 | class CombinedEquipmentExport: |
||
| 2640 | def __init__(self): |
||
| 2641 | pass |
||
| 2642 | |||
| 2643 | @staticmethod |
||
| 2644 | def on_options(req, resp, id_): |
||
| 2645 | _ = req |
||
| 2646 | resp.status = falcon.HTTP_200 |
||
| 2647 | _ = id_ |
||
| 2648 | |||
| 2649 | @staticmethod |
||
| 2650 | def on_get(req, resp, id_): |
||
| 2651 | if 'API-KEY' not in req.headers or \ |
||
| 2652 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2653 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2654 | access_control(req) |
||
| 2655 | else: |
||
| 2656 | api_key_control(req) |
||
| 2657 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2658 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2659 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 2660 | |||
| 2661 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2662 | cursor = cnx.cursor() |
||
| 2663 | |||
| 2664 | query = (" SELECT id, name, uuid " |
||
| 2665 | " FROM tbl_cost_centers ") |
||
| 2666 | cursor.execute(query) |
||
| 2667 | rows_cost_centers = cursor.fetchall() |
||
| 2668 | |||
| 2669 | cost_center_dict = dict() |
||
| 2670 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 2671 | for row in rows_cost_centers: |
||
| 2672 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 2673 | "name": row[1], |
||
| 2674 | "uuid": row[2]} |
||
| 2675 | |||
| 2676 | query = (" SELECT id, name, uuid " |
||
| 2677 | " FROM tbl_svgs ") |
||
| 2678 | cursor.execute(query) |
||
| 2679 | rows_svgs = cursor.fetchall() |
||
| 2680 | |||
| 2681 | svg_dict = dict() |
||
| 2682 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 2683 | for row in rows_svgs: |
||
| 2684 | svg_dict[row[0]] = {"id": row[0], |
||
| 2685 | "name": row[1], |
||
| 2686 | "uuid": row[2]} |
||
| 2687 | |||
| 2688 | query = (" SELECT id, name, uuid, " |
||
| 2689 | " is_input_counted, is_output_counted, " |
||
| 2690 | " cost_center_id, svg_id, camera_url, description " |
||
| 2691 | " FROM tbl_combined_equipments " |
||
| 2692 | " WHERE id = %s ") |
||
| 2693 | cursor.execute(query, (id_,)) |
||
| 2694 | row = cursor.fetchone() |
||
| 2695 | |||
| 2696 | if row is None: |
||
| 2697 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2698 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 2699 | else: |
||
| 2700 | meta_result = {"id": row[0], |
||
| 2701 | "name": row[1], |
||
| 2702 | "uuid": row[2], |
||
| 2703 | "is_input_counted": bool(row[3]), |
||
| 2704 | "is_output_counted": bool(row[4]), |
||
| 2705 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 2706 | "svg": svg_dict.get(row[6], None), |
||
| 2707 | "camera_url": row[7], |
||
| 2708 | "description": row[8], |
||
| 2709 | "equipments": None, |
||
| 2710 | "commands": None, |
||
| 2711 | "meters": None, |
||
| 2712 | "offline_meters": None, |
||
| 2713 | "virtual_meters": None, |
||
| 2714 | "parameters": None} |
||
| 2715 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 2716 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 2717 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 2718 | " ORDER BY e.id ") |
||
| 2719 | cursor.execute(query, (id_,)) |
||
| 2720 | rows = cursor.fetchall() |
||
| 2721 | |||
| 2722 | equipment_result = list() |
||
| 2723 | if rows is not None and len(rows) > 0: |
||
| 2724 | for row in rows: |
||
| 2725 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2726 | equipment_result.append(result) |
||
| 2727 | meta_result['equipments'] = equipment_result |
||
| 2728 | |||
| 2729 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2730 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 2731 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 2732 | " ORDER BY c.id ") |
||
| 2733 | cursor.execute(query, (id_,)) |
||
| 2734 | rows = cursor.fetchall() |
||
| 2735 | |||
| 2736 | command_result = list() |
||
| 2737 | if rows is not None and len(rows) > 0: |
||
| 2738 | for row in rows: |
||
| 2739 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2740 | command_result.append(result) |
||
| 2741 | meta_result['commands'] = command_result |
||
| 2742 | |||
| 2743 | query = (" SELECT id, name, uuid " |
||
| 2744 | " FROM tbl_energy_categories ") |
||
| 2745 | cursor.execute(query) |
||
| 2746 | rows_energy_categories = cursor.fetchall() |
||
| 2747 | |||
| 2748 | energy_category_dict = dict() |
||
| 2749 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2750 | for row in rows_energy_categories: |
||
| 2751 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2752 | "name": row[1], |
||
| 2753 | "uuid": row[2]} |
||
| 2754 | |||
| 2755 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2756 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 2757 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 2758 | " ORDER BY m.id ") |
||
| 2759 | cursor.execute(query, (id_,)) |
||
| 2760 | rows = cursor.fetchall() |
||
| 2761 | |||
| 2762 | meter_result = list() |
||
| 2763 | if rows is not None and len(rows) > 0: |
||
| 2764 | for row in rows: |
||
| 2765 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2766 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2767 | "is_output": bool(row[4])} |
||
| 2768 | meter_result.append(result) |
||
| 2769 | meta_result['meters'] = meter_result |
||
| 2770 | |||
| 2771 | query = (" SELECT id, name, uuid " |
||
| 2772 | " FROM tbl_energy_categories ") |
||
| 2773 | cursor.execute(query) |
||
| 2774 | rows_energy_categories = cursor.fetchall() |
||
| 2775 | |||
| 2776 | energy_category_dict = dict() |
||
| 2777 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2778 | for row in rows_energy_categories: |
||
| 2779 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2780 | "name": row[1], |
||
| 2781 | "uuid": row[2]} |
||
| 2782 | |||
| 2783 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2784 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 2785 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 2786 | " ORDER BY m.id ") |
||
| 2787 | cursor.execute(query, (id_,)) |
||
| 2788 | rows = cursor.fetchall() |
||
| 2789 | |||
| 2790 | offlinemeter_result = list() |
||
| 2791 | if rows is not None and len(rows) > 0: |
||
| 2792 | for row in rows: |
||
| 2793 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2794 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2795 | "is_output": bool(row[4])} |
||
| 2796 | offlinemeter_result.append(result) |
||
| 2797 | meta_result['offline_meters'] = offlinemeter_result |
||
| 2798 | |||
| 2799 | query = (" SELECT id, name, uuid " |
||
| 2800 | " FROM tbl_energy_categories ") |
||
| 2801 | cursor.execute(query) |
||
| 2802 | rows_energy_categories = cursor.fetchall() |
||
| 2803 | |||
| 2804 | energy_category_dict = dict() |
||
| 2805 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2806 | for row in rows_energy_categories: |
||
| 2807 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2808 | "name": row[1], |
||
| 2809 | "uuid": row[2]} |
||
| 2810 | |||
| 2811 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 2812 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 2813 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 2814 | " ORDER BY m.id ") |
||
| 2815 | cursor.execute(query, (id_,)) |
||
| 2816 | rows = cursor.fetchall() |
||
| 2817 | |||
| 2818 | virtualmeter_result = list() |
||
| 2819 | if rows is not None and len(rows) > 0: |
||
| 2820 | for row in rows: |
||
| 2821 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2822 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 2823 | "is_output": bool(row[4])} |
||
| 2824 | virtualmeter_result.append(result) |
||
| 2825 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 2826 | |||
| 2827 | query = (" SELECT id, name " |
||
| 2828 | " FROM tbl_points ") |
||
| 2829 | cursor.execute(query) |
||
| 2830 | rows_points = cursor.fetchall() |
||
| 2831 | |||
| 2832 | point_dict = dict() |
||
| 2833 | if rows_points is not None and len(rows_points) > 0: |
||
| 2834 | for row in rows_points: |
||
| 2835 | point_dict[row[0]] = {"id": row[0], |
||
| 2836 | "name": row[1]} |
||
| 2837 | |||
| 2838 | query = (" SELECT id, name, uuid " |
||
| 2839 | " FROM tbl_meters ") |
||
| 2840 | cursor.execute(query) |
||
| 2841 | rows_meters = cursor.fetchall() |
||
| 2842 | |||
| 2843 | meter_dict = dict() |
||
| 2844 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 2845 | for row in rows_meters: |
||
| 2846 | meter_dict[row[2]] = {"type": 'meter', |
||
| 2847 | "id": row[0], |
||
| 2848 | "name": row[1], |
||
| 2849 | "uuid": row[2]} |
||
| 2850 | |||
| 2851 | query = (" SELECT id, name, uuid " |
||
| 2852 | " FROM tbl_offline_meters ") |
||
| 2853 | cursor.execute(query) |
||
| 2854 | rows_offline_meters = cursor.fetchall() |
||
| 2855 | |||
| 2856 | offline_meter_dict = dict() |
||
| 2857 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 2858 | for row in rows_offline_meters: |
||
| 2859 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 2860 | "id": row[0], |
||
| 2861 | "name": row[1], |
||
| 2862 | "uuid": row[2]} |
||
| 2863 | |||
| 2864 | query = (" SELECT id, name, uuid " |
||
| 2865 | " FROM tbl_virtual_meters ") |
||
| 2866 | cursor.execute(query) |
||
| 2867 | rows_virtual_meters = cursor.fetchall() |
||
| 2868 | |||
| 2869 | virtual_meter_dict = dict() |
||
| 2870 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 2871 | for row in rows_virtual_meters: |
||
| 2872 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 2873 | "id": row[0], |
||
| 2874 | "name": row[1], |
||
| 2875 | "uuid": row[2]} |
||
| 2876 | |||
| 2877 | query = (" SELECT id, name, parameter_type, " |
||
| 2878 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 2879 | " FROM tbl_combined_equipments_parameters " |
||
| 2880 | " WHERE combined_equipment_id = %s " |
||
| 2881 | " ORDER BY id ") |
||
| 2882 | cursor.execute(query, (id_,)) |
||
| 2883 | rows_parameters = cursor.fetchall() |
||
| 2884 | |||
| 2885 | parameter_result = list() |
||
| 2886 | View Code Duplication | if rows_parameters is not None and len(rows_parameters) > 0: |
|
| 2887 | for row in rows_parameters: |
||
| 2888 | constant = None |
||
| 2889 | point = None |
||
| 2890 | numerator_meter = None |
||
| 2891 | denominator_meter = None |
||
| 2892 | if row[2] == 'point': |
||
| 2893 | point = point_dict.get(row[4], None) |
||
| 2894 | constant = None |
||
| 2895 | numerator_meter = None |
||
| 2896 | denominator_meter = None |
||
| 2897 | elif row[2] == 'constant': |
||
| 2898 | constant = row[3] |
||
| 2899 | point = None |
||
| 2900 | numerator_meter = None |
||
| 2901 | denominator_meter = None |
||
| 2902 | elif row[2] == 'fraction': |
||
| 2903 | constant = None |
||
| 2904 | point = None |
||
| 2905 | # find numerator meter by uuid |
||
| 2906 | numerator_meter = meter_dict.get(row[5], None) |
||
| 2907 | if numerator_meter is None: |
||
| 2908 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 2909 | if numerator_meter is None: |
||
| 2910 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 2911 | # find denominator meter by uuid |
||
| 2912 | denominator_meter = meter_dict.get(row[6], None) |
||
| 2913 | if denominator_meter is None: |
||
| 2914 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 2915 | if denominator_meter is None: |
||
| 2916 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 2917 | |||
| 2918 | result = {"id": row[0], |
||
| 2919 | "name": row[1], |
||
| 2920 | "parameter_type": row[2], |
||
| 2921 | "constant": constant, |
||
| 2922 | "point": point, |
||
| 2923 | "numerator_meter": numerator_meter, |
||
| 2924 | "denominator_meter": denominator_meter} |
||
| 2925 | parameter_result.append(result) |
||
| 2926 | meta_result['parameters'] = parameter_result |
||
| 2927 | |||
| 2928 | cursor.close() |
||
| 2929 | cnx.close() |
||
| 2930 | resp.text = json.dumps(meta_result) |
||
| 2931 | |||
| 2932 | |||
| 2933 | class CombinedEquipmentImport: |
||
| 2934 | def __init__(self): |
||
| 2935 | """ Initializes CombinedEquipmentImport""" |
||
| 2936 | pass |
||
| 2937 | |||
| 2938 | @staticmethod |
||
| 2939 | def on_options(req, resp): |
||
| 2940 | _ = req |
||
| 2941 | resp.status = falcon.HTTP_200 |
||
| 2942 | |||
| 2943 | @staticmethod |
||
| 2944 | @user_logger |
||
| 2945 | def on_post(req, resp): |
||
| 2946 | """Handles POST requests""" |
||
| 2947 | admin_control(req) |
||
| 2948 | try: |
||
| 2949 | raw_json = req.stream.read().decode('utf-8') |
||
| 2950 | except Exception as ex: |
||
| 2951 | print(ex) |
||
| 2952 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2953 | title='API.BAD_REQUEST', |
||
| 2954 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2955 | |||
| 2956 | new_values = json.loads(raw_json) |
||
| 2957 | |||
| 2958 | if 'name' not in new_values.keys() or \ |
||
| 2959 | not isinstance(new_values['name'], str) or \ |
||
| 2960 | len(str.strip(new_values['name'])) == 0: |
||
| 2961 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2962 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 2963 | name = str.strip(new_values['name']) |
||
| 2964 | |||
| 2965 | if 'is_input_counted' not in new_values.keys() or \ |
||
| 2966 | not isinstance(new_values['is_input_counted'], bool): |
||
| 2967 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2968 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 2969 | is_input_counted = new_values['is_input_counted'] |
||
| 2970 | |||
| 2971 | if 'is_output_counted' not in new_values.keys() or \ |
||
| 2972 | not isinstance(new_values['is_output_counted'], bool): |
||
| 2973 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2974 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 2975 | is_output_counted = new_values['is_output_counted'] |
||
| 2976 | |||
| 2977 | if 'id' not in new_values['cost_center'].keys() or \ |
||
| 2978 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 2979 | new_values['cost_center']['id'] <= 0: |
||
| 2980 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2981 | description='API.INVALID_COST_CENTER_ID') |
||
| 2982 | cost_center_id = new_values['cost_center']['id'] |
||
| 2983 | |||
| 2984 | if 'svg' in new_values.keys() and \ |
||
| 2985 | 'id' in new_values['svg'].keys() and \ |
||
| 2986 | isinstance(new_values['svg']['id'], int) and \ |
||
| 2987 | new_values['svg']['id'] > 0: |
||
| 2988 | svg_id = new_values['svg']['id'] |
||
| 2989 | else: |
||
| 2990 | svg_id = None |
||
| 2991 | |||
| 2992 | if 'camera_url' in new_values.keys() and \ |
||
| 2993 | new_values['camera_url'] is not None and \ |
||
| 2994 | len(str(new_values['camera_url'])) > 0: |
||
| 2995 | camera_url = str.strip(new_values['camera_url']) |
||
| 2996 | else: |
||
| 2997 | camera_url = None |
||
| 2998 | |||
| 2999 | if 'description' in new_values.keys() and \ |
||
| 3000 | new_values['description'] is not None and \ |
||
| 3001 | len(str(new_values['description'])) > 0: |
||
| 3002 | description = str.strip(new_values['description']) |
||
| 3003 | else: |
||
| 3004 | description = None |
||
| 3005 | |||
| 3006 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3007 | cursor = cnx.cursor() |
||
| 3008 | |||
| 3009 | cursor.execute(" SELECT name " |
||
| 3010 | " FROM tbl_combined_equipments " |
||
| 3011 | " WHERE name = %s ", (name,)) |
||
| 3012 | if cursor.fetchone() is not None: |
||
| 3013 | cursor.close() |
||
| 3014 | cnx.close() |
||
| 3015 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3016 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 3017 | |||
| 3018 | if cost_center_id is not None: |
||
| 3019 | cursor.execute(" SELECT name " |
||
| 3020 | " FROM tbl_cost_centers " |
||
| 3021 | " WHERE id = %s ", |
||
| 3022 | (new_values['cost_center']['id'],)) |
||
| 3023 | row = cursor.fetchone() |
||
| 3024 | if row is None: |
||
| 3025 | cursor.close() |
||
| 3026 | cnx.close() |
||
| 3027 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3028 | description='API.COST_CENTER_NOT_FOUND') |
||
| 3029 | if svg_id is not None: |
||
| 3030 | cursor.execute(" SELECT name " |
||
| 3031 | " FROM tbl_svgs " |
||
| 3032 | " WHERE id = %s ", |
||
| 3033 | (svg_id,)) |
||
| 3034 | row = cursor.fetchone() |
||
| 3035 | if row is None: |
||
| 3036 | cursor.close() |
||
| 3037 | cnx.close() |
||
| 3038 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3039 | description='API.SVG_NOT_FOUND') |
||
| 3040 | |||
| 3041 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 3042 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 3043 | " cost_center_id, svg_id, camera_url, description) " |
||
| 3044 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 3045 | cursor.execute(add_values, (name, |
||
| 3046 | str(uuid.uuid4()), |
||
| 3047 | is_input_counted, |
||
| 3048 | is_output_counted, |
||
| 3049 | cost_center_id, |
||
| 3050 | svg_id, |
||
| 3051 | camera_url, |
||
| 3052 | description)) |
||
| 3053 | new_id = cursor.lastrowid |
||
| 3054 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
| 3055 | for equipment in new_values['equipments']: |
||
| 3056 | cursor.execute(" SELECT name " |
||
| 3057 | " FROM tbl_equipments " |
||
| 3058 | " WHERE id = %s ", (equipment['id'],)) |
||
| 3059 | if cursor.fetchone() is None: |
||
| 3060 | cursor.close() |
||
| 3061 | cnx.close() |
||
| 3062 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3063 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 3064 | |||
| 3065 | query = (" SELECT id " |
||
| 3066 | " FROM tbl_combined_equipments_equipments " |
||
| 3067 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 3068 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 3069 | if cursor.fetchone() is not None: |
||
| 3070 | cursor.close() |
||
| 3071 | cnx.close() |
||
| 3072 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3073 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 3074 | |||
| 3075 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 3076 | " VALUES (%s, %s) ") |
||
| 3077 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 3078 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
| 3079 | for command in new_values['commands']: |
||
| 3080 | cursor.execute(" SELECT name " |
||
| 3081 | " FROM tbl_commands " |
||
| 3082 | " WHERE id = %s ", (command['id'],)) |
||
| 3083 | if cursor.fetchone() is None: |
||
| 3084 | cursor.close() |
||
| 3085 | cnx.close() |
||
| 3086 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3087 | description='API.COMMAND_NOT_FOUND') |
||
| 3088 | |||
| 3089 | query = (" SELECT id " |
||
| 3090 | " FROM tbl_combined_equipments_commands " |
||
| 3091 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 3092 | cursor.execute(query, (new_id, command['id'],)) |
||
| 3093 | if cursor.fetchone() is not None: |
||
| 3094 | cursor.close() |
||
| 3095 | cnx.close() |
||
| 3096 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3097 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 3098 | |||
| 3099 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 3100 | " VALUES (%s, %s) ") |
||
| 3101 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 3102 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
| 3103 | for meter in new_values['meters']: |
||
| 3104 | cursor.execute(" SELECT name " |
||
| 3105 | " FROM tbl_meters " |
||
| 3106 | " WHERE id = %s ", (meter['id'],)) |
||
| 3107 | if cursor.fetchone() is None: |
||
| 3108 | cursor.close() |
||
| 3109 | cnx.close() |
||
| 3110 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3111 | description='API.METER_NOT_FOUND') |
||
| 3112 | |||
| 3113 | query = (" SELECT id " |
||
| 3114 | " FROM tbl_combined_equipments_meters " |
||
| 3115 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 3116 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 3117 | if cursor.fetchone() is not None: |
||
| 3118 | cursor.close() |
||
| 3119 | cnx.close() |
||
| 3120 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3121 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 3122 | |||
| 3123 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 3124 | " VALUES (%s, %s, %s) ") |
||
| 3125 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
| 3126 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
| 3127 | for offline_meter in new_values['offline_meters']: |
||
| 3128 | cursor.execute(" SELECT name " |
||
| 3129 | " FROM tbl_offline_meters " |
||
| 3130 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 3131 | if cursor.fetchone() is None: |
||
| 3132 | cursor.close() |
||
| 3133 | cnx.close() |
||
| 3134 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3135 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 3136 | |||
| 3137 | query = (" SELECT id " |
||
| 3138 | " FROM tbl_combined_equipments_offline_meters " |
||
| 3139 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 3140 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 3141 | if cursor.fetchone() is not None: |
||
| 3142 | cursor.close() |
||
| 3143 | cnx.close() |
||
| 3144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3145 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 3146 | |||
| 3147 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 3148 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 3149 | " VALUES (%s, %s, %s) ") |
||
| 3150 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
| 3151 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
| 3152 | for virtual_meter in new_values['virtual_meters']: |
||
| 3153 | cursor.execute(" SELECT name " |
||
| 3154 | " FROM tbl_virtual_meters " |
||
| 3155 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 3156 | if cursor.fetchone() is None: |
||
| 3157 | cursor.close() |
||
| 3158 | cnx.close() |
||
| 3159 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3160 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 3161 | |||
| 3162 | query = (" SELECT id " |
||
| 3163 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 3164 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 3165 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 3166 | if cursor.fetchone() is not None: |
||
| 3167 | cursor.close() |
||
| 3168 | cnx.close() |
||
| 3169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3170 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 3171 | |||
| 3172 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 3173 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 3174 | " VALUES (%s, %s, %s) ") |
||
| 3175 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
| 3176 | View Code Duplication | if new_values['parameters'] is not None and len(new_values['parameters']) > 0: |
|
| 3177 | for parameters in new_values['parameters']: |
||
| 3178 | cursor.execute(" SELECT name " |
||
| 3179 | " FROM tbl_combined_equipments_parameters " |
||
| 3180 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
| 3181 | if cursor.fetchone() is not None: |
||
| 3182 | cursor.close() |
||
| 3183 | cnx.close() |
||
| 3184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3185 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 3186 | if 'point' in parameters: |
||
| 3187 | if parameters['point'] is None: |
||
| 3188 | point_id = None |
||
| 3189 | elif parameters['point']['id'] is not None and \ |
||
| 3190 | parameters['point']['id'] <= 0: |
||
| 3191 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3192 | description='API.INVALID_POINT_ID') |
||
| 3193 | else: |
||
| 3194 | point_id = parameters['point']['id'] |
||
| 3195 | else: |
||
| 3196 | point_id = None |
||
| 3197 | numerator_meter_uuid = None |
||
| 3198 | if 'numerator_meter' in parameters: |
||
| 3199 | if parameters['numerator_meter'] is not None and \ |
||
| 3200 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
| 3201 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
| 3202 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
| 3203 | |||
| 3204 | denominator_meter_uuid = None |
||
| 3205 | if 'denominator_meter' in parameters: |
||
| 3206 | if parameters['denominator_meter'] is not None and \ |
||
| 3207 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
| 3208 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
| 3209 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
| 3210 | |||
| 3211 | # validate by parameter type |
||
| 3212 | if parameters['parameter_type'] == 'point': |
||
| 3213 | if point_id is None: |
||
| 3214 | cursor.close() |
||
| 3215 | cnx.close() |
||
| 3216 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3217 | description='API.INVALID_POINT_ID') |
||
| 3218 | |||
| 3219 | query = (" SELECT id, name " |
||
| 3220 | " FROM tbl_points " |
||
| 3221 | " WHERE id = %s ") |
||
| 3222 | cursor.execute(query, (point_id,)) |
||
| 3223 | if cursor.fetchone() is None: |
||
| 3224 | cursor.close() |
||
| 3225 | cnx.close() |
||
| 3226 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3227 | description='API.POINT_NOT_FOUND') |
||
| 3228 | |||
| 3229 | elif parameters['parameter_type'] == 'constant': |
||
| 3230 | if parameters['constant'] is None: |
||
| 3231 | cursor.close() |
||
| 3232 | cnx.close() |
||
| 3233 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3234 | description='API.INVALID_CONSTANT_VALUE') |
||
| 3235 | |||
| 3236 | elif parameters['parameter_type'] == 'fraction': |
||
| 3237 | |||
| 3238 | query = (" SELECT id, name, uuid " |
||
| 3239 | " FROM tbl_meters ") |
||
| 3240 | cursor.execute(query) |
||
| 3241 | rows_meters = cursor.fetchall() |
||
| 3242 | |||
| 3243 | meter_dict = dict() |
||
| 3244 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3245 | for row in rows_meters: |
||
| 3246 | meter_dict[row[2]] = {"type": 'meter', |
||
| 3247 | "id": row[0], |
||
| 3248 | "name": row[1], |
||
| 3249 | "uuid": row[2]} |
||
| 3250 | |||
| 3251 | query = (" SELECT id, name, uuid " |
||
| 3252 | " FROM tbl_offline_meters ") |
||
| 3253 | cursor.execute(query) |
||
| 3254 | rows_offline_meters = cursor.fetchall() |
||
| 3255 | |||
| 3256 | offline_meter_dict = dict() |
||
| 3257 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 3258 | for row in rows_offline_meters: |
||
| 3259 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 3260 | "id": row[0], |
||
| 3261 | "name": row[1], |
||
| 3262 | "uuid": row[2]} |
||
| 3263 | |||
| 3264 | query = (" SELECT id, name, uuid " |
||
| 3265 | " FROM tbl_virtual_meters ") |
||
| 3266 | cursor.execute(query) |
||
| 3267 | rows_virtual_meters = cursor.fetchall() |
||
| 3268 | |||
| 3269 | virtual_meter_dict = dict() |
||
| 3270 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 3271 | for row in rows_virtual_meters: |
||
| 3272 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 3273 | "id": row[0], |
||
| 3274 | "name": row[1], |
||
| 3275 | "uuid": row[2]} |
||
| 3276 | |||
| 3277 | # validate numerator meter uuid |
||
| 3278 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3279 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3280 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 3281 | cursor.close() |
||
| 3282 | cnx.close() |
||
| 3283 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3284 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 3285 | |||
| 3286 | # validate denominator meter uuid |
||
| 3287 | if denominator_meter_uuid is None: |
||
| 3288 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3289 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3290 | |||
| 3291 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 3292 | cursor.close() |
||
| 3293 | cnx.close() |
||
| 3294 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3295 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3296 | |||
| 3297 | if denominator_meter_uuid not in meter_dict and \ |
||
| 3298 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 3299 | denominator_meter_uuid not in offline_meter_dict: |
||
| 3300 | cursor.close() |
||
| 3301 | cnx.close() |
||
| 3302 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3303 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3304 | |||
| 3305 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 3306 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 3307 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 3308 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 3309 | cursor.execute(add_values, (new_id, |
||
| 3310 | parameters['name'], |
||
| 3311 | parameters['parameter_type'], |
||
| 3312 | parameters['constant'], |
||
| 3313 | point_id, |
||
| 3314 | numerator_meter_uuid, |
||
| 3315 | denominator_meter_uuid)) |
||
| 3316 | cnx.commit() |
||
| 3317 | cursor.close() |
||
| 3318 | cnx.close() |
||
| 3319 | |||
| 3320 | resp.status = falcon.HTTP_201 |
||
| 3321 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 3322 | |||
| 3323 | |||
| 3324 | class CombinedEquipmentClone: |
||
| 3325 | def __init__(self): |
||
| 3326 | pass |
||
| 3327 | |||
| 3328 | @staticmethod |
||
| 3329 | def on_options(req, resp, id_): |
||
| 3330 | _ = req |
||
| 3331 | resp.status = falcon.HTTP_200 |
||
| 3332 | _ = id_ |
||
| 3333 | |||
| 3334 | @staticmethod |
||
| 3335 | @user_logger |
||
| 3336 | def on_post(req, resp, id_): |
||
| 3337 | """Handles POST requests""" |
||
| 3338 | admin_control(req) |
||
| 3339 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3340 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3341 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 3342 | |||
| 3343 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3344 | cursor = cnx.cursor() |
||
| 3345 | |||
| 3346 | query = (" SELECT id, name, uuid " |
||
| 3347 | " FROM tbl_cost_centers ") |
||
| 3348 | cursor.execute(query) |
||
| 3349 | rows_cost_centers = cursor.fetchall() |
||
| 3350 | |||
| 3351 | cost_center_dict = dict() |
||
| 3352 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 3353 | for row in rows_cost_centers: |
||
| 3354 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 3355 | "name": row[1], |
||
| 3356 | "uuid": row[2]} |
||
| 3357 | |||
| 3358 | query = (" SELECT id, name, uuid, " |
||
| 3359 | " is_input_counted, is_output_counted, " |
||
| 3360 | " cost_center_id, svg_id, camera_url, description " |
||
| 3361 | " FROM tbl_combined_equipments " |
||
| 3362 | " WHERE id = %s ") |
||
| 3363 | cursor.execute(query, (id_,)) |
||
| 3364 | row = cursor.fetchone() |
||
| 3365 | |||
| 3366 | if row is None: |
||
| 3367 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3368 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 3369 | else: |
||
| 3370 | meta_result = {"id": row[0], |
||
| 3371 | "name": row[1], |
||
| 3372 | "uuid": row[2], |
||
| 3373 | "is_input_counted": bool(row[3]), |
||
| 3374 | "is_output_counted": bool(row[4]), |
||
| 3375 | "cost_center": cost_center_dict.get(row[5], None), |
||
| 3376 | "svg_id": row[6], |
||
| 3377 | "camera_url": row[7], |
||
| 3378 | "description": row[8], |
||
| 3379 | "equipments": None, |
||
| 3380 | "commands": None, |
||
| 3381 | "meters": None, |
||
| 3382 | "offline_meters": None, |
||
| 3383 | "virtual_meters": None, |
||
| 3384 | "parameters": None |
||
| 3385 | } |
||
| 3386 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 3387 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 3388 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 3389 | " ORDER BY e.id ") |
||
| 3390 | cursor.execute(query, (id_,)) |
||
| 3391 | rows = cursor.fetchall() |
||
| 3392 | |||
| 3393 | equipment_result = list() |
||
| 3394 | if rows is not None and len(rows) > 0: |
||
| 3395 | for row in rows: |
||
| 3396 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3397 | equipment_result.append(result) |
||
| 3398 | meta_result['equipments'] = equipment_result |
||
| 3399 | |||
| 3400 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 3401 | " FROM tbl_combined_equipments ce, tbl_combined_equipments_commands cec, tbl_commands c " |
||
| 3402 | " WHERE cec.combined_equipment_id = ce.id AND c.id = cec.command_id AND ce.id = %s " |
||
| 3403 | " ORDER BY c.id ") |
||
| 3404 | cursor.execute(query, (id_,)) |
||
| 3405 | rows = cursor.fetchall() |
||
| 3406 | |||
| 3407 | command_result = list() |
||
| 3408 | if rows is not None and len(rows) > 0: |
||
| 3409 | for row in rows: |
||
| 3410 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3411 | command_result.append(result) |
||
| 3412 | meta_result['commands'] = command_result |
||
| 3413 | |||
| 3414 | query = (" SELECT id, name, uuid " |
||
| 3415 | " FROM tbl_energy_categories ") |
||
| 3416 | cursor.execute(query) |
||
| 3417 | rows_energy_categories = cursor.fetchall() |
||
| 3418 | |||
| 3419 | energy_category_dict = dict() |
||
| 3420 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3421 | for row in rows_energy_categories: |
||
| 3422 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3423 | "name": row[1], |
||
| 3424 | "uuid": row[2]} |
||
| 3425 | |||
| 3426 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3427 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 3428 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 3429 | " ORDER BY m.id ") |
||
| 3430 | cursor.execute(query, (id_,)) |
||
| 3431 | rows = cursor.fetchall() |
||
| 3432 | |||
| 3433 | meter_result = list() |
||
| 3434 | if rows is not None and len(rows) > 0: |
||
| 3435 | for row in rows: |
||
| 3436 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3437 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3438 | "is_output": bool(row[4])} |
||
| 3439 | meter_result.append(result) |
||
| 3440 | meta_result['meters'] = meter_result |
||
| 3441 | |||
| 3442 | query = (" SELECT id, name, uuid " |
||
| 3443 | " FROM tbl_energy_categories ") |
||
| 3444 | cursor.execute(query) |
||
| 3445 | rows_energy_categories = cursor.fetchall() |
||
| 3446 | |||
| 3447 | energy_category_dict = dict() |
||
| 3448 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3449 | for row in rows_energy_categories: |
||
| 3450 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3451 | "name": row[1], |
||
| 3452 | "uuid": row[2]} |
||
| 3453 | |||
| 3454 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3455 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 3456 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 3457 | " ORDER BY m.id ") |
||
| 3458 | cursor.execute(query, (id_,)) |
||
| 3459 | rows = cursor.fetchall() |
||
| 3460 | |||
| 3461 | offlinemeter_result = list() |
||
| 3462 | if rows is not None and len(rows) > 0: |
||
| 3463 | for row in rows: |
||
| 3464 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3465 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3466 | "is_output": bool(row[4])} |
||
| 3467 | offlinemeter_result.append(result) |
||
| 3468 | meta_result['offline_meters'] = offlinemeter_result |
||
| 3469 | |||
| 3470 | query = (" SELECT id, name, uuid " |
||
| 3471 | " FROM tbl_energy_categories ") |
||
| 3472 | cursor.execute(query) |
||
| 3473 | rows_energy_categories = cursor.fetchall() |
||
| 3474 | |||
| 3475 | energy_category_dict = dict() |
||
| 3476 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3477 | for row in rows_energy_categories: |
||
| 3478 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3479 | "name": row[1], |
||
| 3480 | "uuid": row[2]} |
||
| 3481 | |||
| 3482 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 3483 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 3484 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 3485 | " ORDER BY m.id ") |
||
| 3486 | cursor.execute(query, (id_,)) |
||
| 3487 | rows = cursor.fetchall() |
||
| 3488 | |||
| 3489 | virtualmeter_result = list() |
||
| 3490 | if rows is not None and len(rows) > 0: |
||
| 3491 | for row in rows: |
||
| 3492 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3493 | "energy_category": energy_category_dict.get(row[3], None), |
||
| 3494 | "is_output": bool(row[4])} |
||
| 3495 | virtualmeter_result.append(result) |
||
| 3496 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 3497 | |||
| 3498 | query = (" SELECT id, name " |
||
| 3499 | " FROM tbl_points ") |
||
| 3500 | cursor.execute(query) |
||
| 3501 | rows_points = cursor.fetchall() |
||
| 3502 | |||
| 3503 | point_dict = dict() |
||
| 3504 | if rows_points is not None and len(rows_points) > 0: |
||
| 3505 | for row in rows_points: |
||
| 3506 | point_dict[row[0]] = {"id": row[0], |
||
| 3507 | "name": row[1]} |
||
| 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 | query = (" SELECT id, name, parameter_type, " |
||
| 3549 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 3550 | " FROM tbl_combined_equipments_parameters " |
||
| 3551 | " WHERE combined_equipment_id = %s " |
||
| 3552 | " ORDER BY id ") |
||
| 3553 | cursor.execute(query, (id_,)) |
||
| 3554 | rows_parameters = cursor.fetchall() |
||
| 3555 | |||
| 3556 | parameter_result = list() |
||
| 3557 | View Code Duplication | if rows_parameters is not None and len(rows_parameters) > 0: |
|
| 3558 | for row in rows_parameters: |
||
| 3559 | constant = None |
||
| 3560 | point = None |
||
| 3561 | numerator_meter = None |
||
| 3562 | denominator_meter = None |
||
| 3563 | if row[2] == 'point': |
||
| 3564 | point = point_dict.get(row[4], None) |
||
| 3565 | constant = None |
||
| 3566 | numerator_meter = None |
||
| 3567 | denominator_meter = None |
||
| 3568 | elif row[2] == 'constant': |
||
| 3569 | constant = row[3] |
||
| 3570 | point = None |
||
| 3571 | numerator_meter = None |
||
| 3572 | denominator_meter = None |
||
| 3573 | elif row[2] == 'fraction': |
||
| 3574 | constant = None |
||
| 3575 | point = None |
||
| 3576 | # find numerator meter by uuid |
||
| 3577 | numerator_meter = meter_dict.get(row[5], None) |
||
| 3578 | if numerator_meter is None: |
||
| 3579 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 3580 | if numerator_meter is None: |
||
| 3581 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 3582 | # find denominator meter by uuid |
||
| 3583 | denominator_meter = meter_dict.get(row[6], None) |
||
| 3584 | if denominator_meter is None: |
||
| 3585 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 3586 | if denominator_meter is None: |
||
| 3587 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 3588 | |||
| 3589 | result = {"id": row[0], |
||
| 3590 | "name": row[1], |
||
| 3591 | "parameter_type": row[2], |
||
| 3592 | "constant": constant, |
||
| 3593 | "point": point, |
||
| 3594 | "numerator_meter": numerator_meter, |
||
| 3595 | "denominator_meter": denominator_meter} |
||
| 3596 | parameter_result.append(result) |
||
| 3597 | meta_result['parameters'] = parameter_result |
||
| 3598 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 3599 | if config.utc_offset[0] == '-': |
||
| 3600 | timezone_offset = -timezone_offset |
||
| 3601 | new_name = (str.strip(meta_result['name']) + |
||
| 3602 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 3603 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 3604 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 3605 | " cost_center_id, svg_id, camera_url, description) " |
||
| 3606 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 3607 | cursor.execute(add_values, (new_name, |
||
| 3608 | str(uuid.uuid4()), |
||
| 3609 | meta_result['is_input_counted'], |
||
| 3610 | meta_result['is_output_counted'], |
||
| 3611 | meta_result['cost_center']['id'], |
||
| 3612 | meta_result['svg_id'], |
||
| 3613 | meta_result['camera_url'], |
||
| 3614 | meta_result['description'])) |
||
| 3615 | new_id = cursor.lastrowid |
||
| 3616 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
| 3617 | for equipment in meta_result['equipments']: |
||
| 3618 | cursor.execute(" SELECT name " |
||
| 3619 | " FROM tbl_equipments " |
||
| 3620 | " WHERE id = %s ", (equipment['id'],)) |
||
| 3621 | if cursor.fetchone() is None: |
||
| 3622 | cursor.close() |
||
| 3623 | cnx.close() |
||
| 3624 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3625 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 3626 | |||
| 3627 | query = (" SELECT id " |
||
| 3628 | " FROM tbl_combined_equipments_equipments " |
||
| 3629 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 3630 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 3631 | if cursor.fetchone() is not None: |
||
| 3632 | cursor.close() |
||
| 3633 | cnx.close() |
||
| 3634 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3635 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 3636 | |||
| 3637 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 3638 | " VALUES (%s, %s) ") |
||
| 3639 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 3640 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
| 3641 | for command in meta_result['commands']: |
||
| 3642 | cursor.execute(" SELECT name " |
||
| 3643 | " FROM tbl_commands " |
||
| 3644 | " WHERE id = %s ", (command['id'],)) |
||
| 3645 | if cursor.fetchone() is None: |
||
| 3646 | cursor.close() |
||
| 3647 | cnx.close() |
||
| 3648 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3649 | description='API.COMMAND_NOT_FOUND') |
||
| 3650 | |||
| 3651 | query = (" SELECT id " |
||
| 3652 | " FROM tbl_combined_equipments_commands " |
||
| 3653 | " WHERE combined_equipment_id = %s AND command_id = %s") |
||
| 3654 | cursor.execute(query, (new_id, command['id'],)) |
||
| 3655 | if cursor.fetchone() is not None: |
||
| 3656 | cursor.close() |
||
| 3657 | cnx.close() |
||
| 3658 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3659 | description='API.COMBINED_EQUIPMENT_COMMAND_RELATION_EXISTS') |
||
| 3660 | |||
| 3661 | add_row = (" INSERT INTO tbl_combined_equipments_commands (combined_equipment_id, command_id) " |
||
| 3662 | " VALUES (%s, %s) ") |
||
| 3663 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 3664 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
| 3665 | for meter in meta_result['meters']: |
||
| 3666 | cursor.execute(" SELECT name " |
||
| 3667 | " FROM tbl_meters " |
||
| 3668 | " WHERE id = %s ", (meter['id'],)) |
||
| 3669 | if cursor.fetchone() is None: |
||
| 3670 | cursor.close() |
||
| 3671 | cnx.close() |
||
| 3672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3673 | description='API.METER_NOT_FOUND') |
||
| 3674 | |||
| 3675 | query = (" SELECT id " |
||
| 3676 | " FROM tbl_combined_equipments_meters " |
||
| 3677 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 3678 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 3679 | if cursor.fetchone() is not None: |
||
| 3680 | cursor.close() |
||
| 3681 | cnx.close() |
||
| 3682 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3683 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 3684 | |||
| 3685 | add_row = ( |
||
| 3686 | " INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 3687 | " VALUES (%s, %s, %s) ") |
||
| 3688 | cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) |
||
| 3689 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
| 3690 | for offline_meter in meta_result['offline_meters']: |
||
| 3691 | cursor.execute(" SELECT name " |
||
| 3692 | " FROM tbl_offline_meters " |
||
| 3693 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 3694 | if cursor.fetchone() is None: |
||
| 3695 | cursor.close() |
||
| 3696 | cnx.close() |
||
| 3697 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3698 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 3699 | |||
| 3700 | query = (" SELECT id " |
||
| 3701 | " FROM tbl_combined_equipments_offline_meters " |
||
| 3702 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 3703 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 3704 | if cursor.fetchone() is not None: |
||
| 3705 | cursor.close() |
||
| 3706 | cnx.close() |
||
| 3707 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3708 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 3709 | |||
| 3710 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 3711 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 3712 | " VALUES (%s, %s, %s) ") |
||
| 3713 | cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) |
||
| 3714 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
| 3715 | for virtual_meter in meta_result['virtual_meters']: |
||
| 3716 | cursor.execute(" SELECT name " |
||
| 3717 | " FROM tbl_virtual_meters " |
||
| 3718 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 3719 | if cursor.fetchone() is None: |
||
| 3720 | cursor.close() |
||
| 3721 | cnx.close() |
||
| 3722 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3723 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 3724 | |||
| 3725 | query = (" SELECT id " |
||
| 3726 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 3727 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 3728 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 3729 | if cursor.fetchone() is not None: |
||
| 3730 | cursor.close() |
||
| 3731 | cnx.close() |
||
| 3732 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3733 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 3734 | |||
| 3735 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 3736 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 3737 | " VALUES (%s, %s, %s) ") |
||
| 3738 | cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) |
||
| 3739 | View Code Duplication | if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0: |
|
| 3740 | for parameters in meta_result['parameters']: |
||
| 3741 | cursor.execute(" SELECT name " |
||
| 3742 | " FROM tbl_combined_equipments_parameters " |
||
| 3743 | " WHERE name = %s AND combined_equipment_id = %s ", (parameters['name'], new_id)) |
||
| 3744 | if cursor.fetchone() is not None: |
||
| 3745 | cursor.close() |
||
| 3746 | cnx.close() |
||
| 3747 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3748 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 3749 | if 'point' in parameters: |
||
| 3750 | if parameters['point'] is None: |
||
| 3751 | point_id = None |
||
| 3752 | elif parameters['point']['id'] is not None and \ |
||
| 3753 | parameters['point']['id'] <= 0: |
||
| 3754 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3755 | description='API.INVALID_POINT_ID') |
||
| 3756 | else: |
||
| 3757 | point_id = parameters['point']['id'] |
||
| 3758 | else: |
||
| 3759 | point_id = None |
||
| 3760 | numerator_meter_uuid = None |
||
| 3761 | if 'numerator_meter' in parameters: |
||
| 3762 | if parameters['numerator_meter'] is not None and \ |
||
| 3763 | isinstance(parameters['numerator_meter']['uuid'], str) and \ |
||
| 3764 | len(str.strip(parameters['numerator_meter']['uuid'])) > 0: |
||
| 3765 | numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) |
||
| 3766 | |||
| 3767 | denominator_meter_uuid = None |
||
| 3768 | if 'denominator_meter' in parameters: |
||
| 3769 | if parameters['denominator_meter'] is not None and \ |
||
| 3770 | isinstance(parameters['denominator_meter']['uuid'], str) and \ |
||
| 3771 | len(str.strip(parameters['denominator_meter']['uuid'])) > 0: |
||
| 3772 | denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) |
||
| 3773 | |||
| 3774 | # validate by parameter type |
||
| 3775 | if parameters['parameter_type'] == 'point': |
||
| 3776 | if point_id is None: |
||
| 3777 | cursor.close() |
||
| 3778 | cnx.close() |
||
| 3779 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3780 | description='API.INVALID_POINT_ID') |
||
| 3781 | |||
| 3782 | query = (" SELECT id, name " |
||
| 3783 | " FROM tbl_points " |
||
| 3784 | " WHERE id = %s ") |
||
| 3785 | cursor.execute(query, (point_id,)) |
||
| 3786 | if cursor.fetchone() is None: |
||
| 3787 | cursor.close() |
||
| 3788 | cnx.close() |
||
| 3789 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3790 | description='API.POINT_NOT_FOUND') |
||
| 3791 | |||
| 3792 | elif parameters['parameter_type'] == 'constant': |
||
| 3793 | if parameters['constant'] is None: |
||
| 3794 | cursor.close() |
||
| 3795 | cnx.close() |
||
| 3796 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3797 | description='API.INVALID_CONSTANT_VALUE') |
||
| 3798 | |||
| 3799 | elif parameters['parameter_type'] == 'fraction': |
||
| 3800 | |||
| 3801 | query = (" SELECT id, name, uuid " |
||
| 3802 | " FROM tbl_meters ") |
||
| 3803 | cursor.execute(query) |
||
| 3804 | rows_meters = cursor.fetchall() |
||
| 3805 | |||
| 3806 | meter_dict = dict() |
||
| 3807 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 3808 | for row in rows_meters: |
||
| 3809 | meter_dict[row[2]] = {"type": 'meter', |
||
| 3810 | "id": row[0], |
||
| 3811 | "name": row[1], |
||
| 3812 | "uuid": row[2]} |
||
| 3813 | |||
| 3814 | query = (" SELECT id, name, uuid " |
||
| 3815 | " FROM tbl_offline_meters ") |
||
| 3816 | cursor.execute(query) |
||
| 3817 | rows_offline_meters = cursor.fetchall() |
||
| 3818 | |||
| 3819 | offline_meter_dict = dict() |
||
| 3820 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 3821 | for row in rows_offline_meters: |
||
| 3822 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 3823 | "id": row[0], |
||
| 3824 | "name": row[1], |
||
| 3825 | "uuid": row[2]} |
||
| 3826 | |||
| 3827 | query = (" SELECT id, name, uuid " |
||
| 3828 | " FROM tbl_virtual_meters ") |
||
| 3829 | cursor.execute(query) |
||
| 3830 | rows_virtual_meters = cursor.fetchall() |
||
| 3831 | |||
| 3832 | virtual_meter_dict = dict() |
||
| 3833 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 3834 | for row in rows_virtual_meters: |
||
| 3835 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 3836 | "id": row[0], |
||
| 3837 | "name": row[1], |
||
| 3838 | "uuid": row[2]} |
||
| 3839 | |||
| 3840 | # validate numerator meter uuid |
||
| 3841 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3842 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 3843 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 3844 | cursor.close() |
||
| 3845 | cnx.close() |
||
| 3846 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3847 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 3848 | |||
| 3849 | # validate denominator meter uuid |
||
| 3850 | if denominator_meter_uuid is None: |
||
| 3851 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3852 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3853 | |||
| 3854 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 3855 | cursor.close() |
||
| 3856 | cnx.close() |
||
| 3857 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3858 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3859 | |||
| 3860 | if denominator_meter_uuid not in meter_dict and \ |
||
| 3861 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 3862 | denominator_meter_uuid not in offline_meter_dict: |
||
| 3863 | cursor.close() |
||
| 3864 | cnx.close() |
||
| 3865 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3866 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 3867 | |||
| 3868 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 3869 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 3870 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 3871 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 3872 | cursor.execute(add_values, (new_id, |
||
| 3873 | parameters['name'], |
||
| 3874 | parameters['parameter_type'], |
||
| 3875 | parameters['constant'], |
||
| 3876 | point_id, |
||
| 3877 | numerator_meter_uuid, |
||
| 3878 | denominator_meter_uuid)) |
||
| 3879 | cnx.commit() |
||
| 3880 | cursor.close() |
||
| 3881 | cnx.close() |
||
| 3882 | |||
| 3883 | resp.status = falcon.HTTP_201 |
||
| 3884 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 3885 |