| Total Complexity | 385 |
| Total Lines | 1967 |
| Duplicated Lines | 97.71 % |
| 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 falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | from core.useractivity import user_logger, access_control |
||
| 7 | |||
| 8 | |||
| 9 | View Code Duplication | class CombinedEquipmentCollection: |
|
|
|
|||
| 10 | @staticmethod |
||
| 11 | def __init__(): |
||
| 12 | """ Initializes CombinedEquipmentCollection""" |
||
| 13 | pass |
||
| 14 | |||
| 15 | @staticmethod |
||
| 16 | def on_options(req, resp): |
||
| 17 | resp.status = falcon.HTTP_200 |
||
| 18 | |||
| 19 | @staticmethod |
||
| 20 | def on_get(req, resp): |
||
| 21 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 22 | cursor = cnx.cursor() |
||
| 23 | |||
| 24 | query = (" SELECT id, name, uuid " |
||
| 25 | " FROM tbl_cost_centers ") |
||
| 26 | cursor.execute(query) |
||
| 27 | rows_cost_centers = cursor.fetchall() |
||
| 28 | |||
| 29 | cost_center_dict = dict() |
||
| 30 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 31 | for row in rows_cost_centers: |
||
| 32 | cost_center_dict[row['id']] = {"id": row[0], |
||
| 33 | "name": row[1], |
||
| 34 | "uuid": row[2]} |
||
| 35 | |||
| 36 | query = (" SELECT id, name, uuid, " |
||
| 37 | " is_input_counted, is_output_counted, " |
||
| 38 | " cost_center_id, description " |
||
| 39 | " FROM tbl_combined_equipments " |
||
| 40 | " ORDER BY id ") |
||
| 41 | cursor.execute(query) |
||
| 42 | rows_combined_equipments = cursor.fetchall() |
||
| 43 | |||
| 44 | result = list() |
||
| 45 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 46 | for row in rows_combined_equipments: |
||
| 47 | cost_center = cost_center_dict.get(row[5], None) |
||
| 48 | meta_result = {"id": row[0], |
||
| 49 | "name": row[1], |
||
| 50 | "uuid": row[2], |
||
| 51 | "is_input_counted": bool(row[3]), |
||
| 52 | "is_output_counted": bool(row[4]), |
||
| 53 | "cost_center": cost_center, |
||
| 54 | "description": row[6], |
||
| 55 | "qrcode": 'combinedequipment:' + row[2]} |
||
| 56 | result.append(meta_result) |
||
| 57 | |||
| 58 | cursor.close() |
||
| 59 | cnx.close() |
||
| 60 | resp.text = json.dumps(result) |
||
| 61 | |||
| 62 | @staticmethod |
||
| 63 | @user_logger |
||
| 64 | def on_post(req, resp): |
||
| 65 | """Handles POST requests""" |
||
| 66 | access_control(req) |
||
| 67 | try: |
||
| 68 | raw_json = req.stream.read().decode('utf-8') |
||
| 69 | except Exception as ex: |
||
| 70 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 71 | |||
| 72 | new_values = json.loads(raw_json) |
||
| 73 | |||
| 74 | if 'name' not in new_values['data'].keys() or \ |
||
| 75 | not isinstance(new_values['data']['name'], str) or \ |
||
| 76 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 77 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 78 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 79 | name = str.strip(new_values['data']['name']) |
||
| 80 | |||
| 81 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 82 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 83 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 84 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 85 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 86 | |||
| 87 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 88 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 89 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 90 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 91 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 92 | |||
| 93 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 94 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 95 | new_values['data']['cost_center_id'] <= 0: |
||
| 96 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 97 | description='API.INVALID_COST_CENTER_ID') |
||
| 98 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 99 | |||
| 100 | if 'description' in new_values['data'].keys() and \ |
||
| 101 | new_values['data']['description'] is not None and \ |
||
| 102 | len(str(new_values['data']['description'])) > 0: |
||
| 103 | description = str.strip(new_values['data']['description']) |
||
| 104 | else: |
||
| 105 | description = None |
||
| 106 | |||
| 107 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 108 | cursor = cnx.cursor() |
||
| 109 | |||
| 110 | cursor.execute(" SELECT name " |
||
| 111 | " FROM tbl_combined_equipments " |
||
| 112 | " WHERE name = %s ", (name,)) |
||
| 113 | if cursor.fetchone() is not None: |
||
| 114 | cursor.close() |
||
| 115 | cnx.close() |
||
| 116 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 117 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 118 | |||
| 119 | if cost_center_id is not None: |
||
| 120 | cursor.execute(" SELECT name " |
||
| 121 | " FROM tbl_cost_centers " |
||
| 122 | " WHERE id = %s ", |
||
| 123 | (new_values['data']['cost_center_id'],)) |
||
| 124 | row = cursor.fetchone() |
||
| 125 | if row is None: |
||
| 126 | cursor.close() |
||
| 127 | cnx.close() |
||
| 128 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 129 | description='API.COST_CENTER_NOT_FOUND') |
||
| 130 | |||
| 131 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 132 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 133 | " cost_center_id, description) " |
||
| 134 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 135 | cursor.execute(add_values, (name, |
||
| 136 | str(uuid.uuid4()), |
||
| 137 | is_input_counted, |
||
| 138 | is_output_counted, |
||
| 139 | cost_center_id, |
||
| 140 | description)) |
||
| 141 | new_id = cursor.lastrowid |
||
| 142 | cnx.commit() |
||
| 143 | cursor.close() |
||
| 144 | cnx.close() |
||
| 145 | |||
| 146 | resp.status = falcon.HTTP_201 |
||
| 147 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 148 | |||
| 149 | |||
| 150 | class CombinedEquipmentItem: |
||
| 151 | @staticmethod |
||
| 152 | def __init__(): |
||
| 153 | """Initializes CombinedEquipmentItem""" |
||
| 154 | pass |
||
| 155 | |||
| 156 | @staticmethod |
||
| 157 | def on_options(req, resp, id_): |
||
| 158 | resp.status = falcon.HTTP_200 |
||
| 159 | |||
| 160 | View Code Duplication | @staticmethod |
|
| 161 | def on_get(req, resp, id_): |
||
| 162 | if not id_.isdigit() or int(id_) <= 0: |
||
| 163 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 164 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 165 | |||
| 166 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 167 | cursor = cnx.cursor() |
||
| 168 | |||
| 169 | query = (" SELECT id, name, uuid " |
||
| 170 | " FROM tbl_cost_centers ") |
||
| 171 | cursor.execute(query) |
||
| 172 | rows_cost_centers = cursor.fetchall() |
||
| 173 | |||
| 174 | cost_center_dict = dict() |
||
| 175 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 176 | for row in rows_cost_centers: |
||
| 177 | cost_center_dict[row['id']] = {"id": row[0], |
||
| 178 | "name": row[1], |
||
| 179 | "uuid": row[2]} |
||
| 180 | |||
| 181 | query = (" SELECT id, name, uuid, " |
||
| 182 | " is_input_counted, is_output_counted, " |
||
| 183 | " cost_center_id, description " |
||
| 184 | " FROM tbl_combined_equipments " |
||
| 185 | " WHERE id = %s ") |
||
| 186 | cursor.execute(query, (id_,)) |
||
| 187 | row = cursor.fetchone() |
||
| 188 | cursor.close() |
||
| 189 | cnx.close() |
||
| 190 | |||
| 191 | if row is None: |
||
| 192 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 193 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 194 | else: |
||
| 195 | cost_center = cost_center_dict.get(row[5], None) |
||
| 196 | meta_result = {"id": row[0], |
||
| 197 | "name": row[1], |
||
| 198 | "uuid": row[2], |
||
| 199 | "is_input_counted": bool(row[3]), |
||
| 200 | "is_output_counted": bool(row[4]), |
||
| 201 | "cost_center": cost_center, |
||
| 202 | "description": row[6], |
||
| 203 | "qrcode": 'combinedequipment:' + row[2]} |
||
| 204 | |||
| 205 | resp.text = json.dumps(meta_result) |
||
| 206 | |||
| 207 | View Code Duplication | @staticmethod |
|
| 208 | @user_logger |
||
| 209 | def on_delete(req, resp, id_): |
||
| 210 | access_control(req) |
||
| 211 | if not id_.isdigit() or int(id_) <= 0: |
||
| 212 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 213 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 214 | |||
| 215 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 216 | cursor = cnx.cursor() |
||
| 217 | |||
| 218 | # check relation with space |
||
| 219 | cursor.execute(" SELECT space_id " |
||
| 220 | " FROM tbl_spaces_combined_equipments " |
||
| 221 | " WHERE combined_equipment_id = %s ", |
||
| 222 | (id_,)) |
||
| 223 | rows_combined_equipments = cursor.fetchall() |
||
| 224 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 225 | cursor.close() |
||
| 226 | cnx.close() |
||
| 227 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 228 | title='API.BAD_REQUEST', |
||
| 229 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 230 | |||
| 231 | # check relation with meter |
||
| 232 | cursor.execute(" SELECT meter_id " |
||
| 233 | " FROM tbl_combined_equipments_meters " |
||
| 234 | " WHERE combined_equipment_id = %s ", |
||
| 235 | (id_,)) |
||
| 236 | rows_meters = cursor.fetchall() |
||
| 237 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 238 | cursor.close() |
||
| 239 | cnx.close() |
||
| 240 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 241 | title='API.BAD_REQUEST', |
||
| 242 | description='API.THERE_IS_RELATION_WITH_METER') |
||
| 243 | |||
| 244 | # check relation with offline meter |
||
| 245 | cursor.execute(" SELECT offline_meter_id " |
||
| 246 | " FROM tbl_combined_equipments_offline_meters " |
||
| 247 | " WHERE combined_equipment_id = %s ", |
||
| 248 | (id_,)) |
||
| 249 | rows_offline_meters = cursor.fetchall() |
||
| 250 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 251 | cursor.close() |
||
| 252 | cnx.close() |
||
| 253 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 254 | title='API.BAD_REQUEST', |
||
| 255 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METER') |
||
| 256 | |||
| 257 | # check relation with virtual meter |
||
| 258 | cursor.execute(" SELECT virtual_meter_id " |
||
| 259 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 260 | " WHERE combined_equipment_id = %s ", |
||
| 261 | (id_,)) |
||
| 262 | rows_virtual_meters = cursor.fetchall() |
||
| 263 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 264 | cursor.close() |
||
| 265 | cnx.close() |
||
| 266 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 267 | title='API.BAD_REQUEST', |
||
| 268 | description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER') |
||
| 269 | |||
| 270 | # delete all associated parameters |
||
| 271 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters WHERE combined_equipment_id = %s ", (id_,)) |
||
| 272 | cnx.commit() |
||
| 273 | |||
| 274 | cursor.execute(" DELETE FROM tbl_combined_equipments WHERE id = %s ", (id_,)) |
||
| 275 | cnx.commit() |
||
| 276 | |||
| 277 | cursor.close() |
||
| 278 | cnx.close() |
||
| 279 | |||
| 280 | resp.status = falcon.HTTP_204 |
||
| 281 | |||
| 282 | View Code Duplication | @staticmethod |
|
| 283 | @user_logger |
||
| 284 | def on_put(req, resp, id_): |
||
| 285 | """Handles PUT requests""" |
||
| 286 | access_control(req) |
||
| 287 | if not id_.isdigit() or int(id_) <= 0: |
||
| 288 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 289 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 290 | try: |
||
| 291 | raw_json = req.stream.read().decode('utf-8') |
||
| 292 | except Exception as ex: |
||
| 293 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 294 | |||
| 295 | new_values = json.loads(raw_json) |
||
| 296 | |||
| 297 | if 'name' not in new_values['data'].keys() or \ |
||
| 298 | not isinstance(new_values['data']['name'], str) or \ |
||
| 299 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 300 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 301 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
||
| 302 | name = str.strip(new_values['data']['name']) |
||
| 303 | |||
| 304 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 305 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 306 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 307 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 308 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 309 | |||
| 310 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 311 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 312 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 313 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 314 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 315 | |||
| 316 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 317 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 318 | new_values['data']['cost_center_id'] <= 0: |
||
| 319 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 320 | description='API.INVALID_COST_CENTER_ID') |
||
| 321 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 322 | |||
| 323 | if 'description' in new_values['data'].keys() and \ |
||
| 324 | new_values['data']['description'] is not None and \ |
||
| 325 | len(str(new_values['data']['description'])) > 0: |
||
| 326 | description = str.strip(new_values['data']['description']) |
||
| 327 | else: |
||
| 328 | description = None |
||
| 329 | |||
| 330 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 331 | cursor = cnx.cursor() |
||
| 332 | |||
| 333 | cursor.execute(" SELECT name " |
||
| 334 | " FROM tbl_combined_equipments " |
||
| 335 | " WHERE id = %s ", (id_,)) |
||
| 336 | if cursor.fetchone() is None: |
||
| 337 | cursor.close() |
||
| 338 | cnx.close() |
||
| 339 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 340 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 341 | |||
| 342 | cursor.execute(" SELECT name " |
||
| 343 | " FROM tbl_combined_equipments " |
||
| 344 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 345 | if cursor.fetchone() is not None: |
||
| 346 | cursor.close() |
||
| 347 | cnx.close() |
||
| 348 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 349 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
||
| 350 | |||
| 351 | cursor.execute(" SELECT name " |
||
| 352 | " FROM tbl_cost_centers " |
||
| 353 | " WHERE id = %s ", |
||
| 354 | (new_values['data']['cost_center_id'],)) |
||
| 355 | row = cursor.fetchone() |
||
| 356 | if row is None: |
||
| 357 | cursor.close() |
||
| 358 | cnx.close() |
||
| 359 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 360 | description='API.COST_CENTER_NOT_FOUND') |
||
| 361 | |||
| 362 | update_row = (" UPDATE tbl_combined_equipments " |
||
| 363 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
||
| 364 | " cost_center_id = %s, description = %s " |
||
| 365 | " WHERE id = %s ") |
||
| 366 | cursor.execute(update_row, (name, |
||
| 367 | is_input_counted, |
||
| 368 | is_output_counted, |
||
| 369 | cost_center_id, |
||
| 370 | description, |
||
| 371 | id_)) |
||
| 372 | cnx.commit() |
||
| 373 | |||
| 374 | cursor.close() |
||
| 375 | cnx.close() |
||
| 376 | |||
| 377 | resp.status = falcon.HTTP_200 |
||
| 378 | |||
| 379 | # Clone a Combined Equipment |
||
| 380 | View Code Duplication | @staticmethod |
|
| 381 | @user_logger |
||
| 382 | def on_post(req, resp, id_): |
||
| 383 | """Handles PUT requests""" |
||
| 384 | access_control(req) |
||
| 385 | if not id_.isdigit() or int(id_) <= 0: |
||
| 386 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 387 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 388 | try: |
||
| 389 | raw_json = req.stream.read().decode('utf-8') |
||
| 390 | except Exception as ex: |
||
| 391 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 392 | |||
| 393 | new_values = json.loads(raw_json) |
||
| 394 | |||
| 395 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 396 | cursor = cnx.cursor() |
||
| 397 | cursor.execute(" SELECT name " |
||
| 398 | " FROM tbl_combined_equipments " |
||
| 399 | " WHERE id = %s ", (id_,)) |
||
| 400 | if cursor.fetchone() is None: |
||
| 401 | cursor.close() |
||
| 402 | cnx.close() |
||
| 403 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 404 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 405 | |||
| 406 | query = (" SELECT name, is_input_counted, is_output_counted, " |
||
| 407 | " cost_center_id, description " |
||
| 408 | " FROM tbl_combined_equipments " |
||
| 409 | " WHERE id = %s ") |
||
| 410 | cursor.execute(query, (id_,)) |
||
| 411 | row = cursor.fetchone() |
||
| 412 | |||
| 413 | if row is None: |
||
| 414 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 415 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 416 | else: |
||
| 417 | |||
| 418 | add_values = (" INSERT INTO tbl_combined_equipments " |
||
| 419 | " (name, uuid, is_input_counted, is_output_counted, " |
||
| 420 | " cost_center_id, description) " |
||
| 421 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 422 | cursor.execute(add_values, (row[0] + ' Copy', |
||
| 423 | str(uuid.uuid4()), |
||
| 424 | row[1], |
||
| 425 | row[2], |
||
| 426 | row[3], |
||
| 427 | row[4])) |
||
| 428 | new_id = cursor.lastrowid |
||
| 429 | cnx.commit() |
||
| 430 | |||
| 431 | # clone relation with meter |
||
| 432 | cursor.execute(" SELECT meter_id, is_output " |
||
| 433 | " FROM tbl_combined_equipments_meters " |
||
| 434 | " WHERE combined_equipment_id = %s ", |
||
| 435 | (id_,)) |
||
| 436 | rows_meters = cursor.fetchall() |
||
| 437 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 438 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
||
| 439 | " VALUES ") |
||
| 440 | for row in rows_meters: |
||
| 441 | add_values += " (" + str(new_id) + "," |
||
| 442 | add_values += str(row[0]) + "," |
||
| 443 | add_values += str(bool(row[1])) + "), " |
||
| 444 | # trim ", " at the end of string and then execute |
||
| 445 | cursor.execute(add_values[:-2]) |
||
| 446 | cnx.commit() |
||
| 447 | |||
| 448 | # clone relation with offline meter |
||
| 449 | cursor.execute(" SELECT offline_meter_id, is_output " |
||
| 450 | " FROM tbl_combined_equipments_offline_meters " |
||
| 451 | " WHERE combined_equipment_id = %s ", |
||
| 452 | (id_,)) |
||
| 453 | rows_offline_meters = cursor.fetchall() |
||
| 454 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 455 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 456 | " (combined_equipment_id, offline_meter_id, is_output) " |
||
| 457 | " VALUES ") |
||
| 458 | for row in rows_offline_meters: |
||
| 459 | add_values += " (" + str(new_id) + "," |
||
| 460 | add_values += "'" + str(row[0]) + "'," |
||
| 461 | add_values += str(bool(row[1])) + "), " |
||
| 462 | # trim ", " at the end of string and then execute |
||
| 463 | cursor.execute(add_values[:-2]) |
||
| 464 | cnx.commit() |
||
| 465 | |||
| 466 | # clone relation with virtual meter |
||
| 467 | cursor.execute(" SELECT virtual_meter_id, is_output " |
||
| 468 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 469 | " WHERE combined_equipment_id = %s ", |
||
| 470 | (id_,)) |
||
| 471 | rows_virtual_meters = cursor.fetchall() |
||
| 472 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 473 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 474 | " (combined_equipment_id, virtual_meter_id, is_output) " |
||
| 475 | " VALUES ") |
||
| 476 | for row in rows_virtual_meters: |
||
| 477 | add_values += " (" + str(new_id) + "," |
||
| 478 | add_values += str(row[0]) + "," |
||
| 479 | add_values += str(bool(row[1])) + "), " |
||
| 480 | # trim ", " at the end of string and then execute |
||
| 481 | cursor.execute(add_values[:-2]) |
||
| 482 | cnx.commit() |
||
| 483 | |||
| 484 | # clone parameters |
||
| 485 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 486 | " FROM tbl_combined_equipments_parameters " |
||
| 487 | " WHERE combined_equipment_id = %s ", |
||
| 488 | (id_,)) |
||
| 489 | rows_parameters = cursor.fetchall() |
||
| 490 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 491 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
||
| 492 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
||
| 493 | " numerator_meter_uuid, denominator_meter_uuid) " |
||
| 494 | " VALUES ") |
||
| 495 | for row in rows_parameters: |
||
| 496 | add_values += " (" + str(new_id) + "," |
||
| 497 | add_values += "'" + str(row[0]) + "'," |
||
| 498 | add_values += "'" + str(row[1]) + "'," |
||
| 499 | if row[2] is not None: |
||
| 500 | add_values += "'" + str(row[2]) + "'," |
||
| 501 | else: |
||
| 502 | add_values += "null, " |
||
| 503 | |||
| 504 | if row[3] is not None: |
||
| 505 | add_values += str(row[2]) + "," |
||
| 506 | else: |
||
| 507 | add_values += "null, " |
||
| 508 | |||
| 509 | if row[4] is not None: |
||
| 510 | add_values += "'" + row[4] + "'," |
||
| 511 | else: |
||
| 512 | add_values += "null, " |
||
| 513 | if row[5] is not None: |
||
| 514 | add_values += "'" + row[5] + "'), " |
||
| 515 | else: |
||
| 516 | add_values += "null), " |
||
| 517 | |||
| 518 | # trim ", " at the end of string and then execute |
||
| 519 | cursor.execute(add_values[:-2]) |
||
| 520 | cnx.commit() |
||
| 521 | |||
| 522 | cursor.close() |
||
| 523 | cnx.close() |
||
| 524 | resp.status = falcon.HTTP_201 |
||
| 525 | resp.location = '/combinedequipments/' + str(new_id) |
||
| 526 | |||
| 527 | |||
| 528 | View Code Duplication | class CombinedEquipmentEquipmentCollection: |
|
| 529 | @staticmethod |
||
| 530 | def __init__(): |
||
| 531 | """Initializes CombinedEquipmentEquipmentCollection""" |
||
| 532 | pass |
||
| 533 | |||
| 534 | @staticmethod |
||
| 535 | def on_options(req, resp, id_): |
||
| 536 | resp.status = falcon.HTTP_200 |
||
| 537 | |||
| 538 | @staticmethod |
||
| 539 | def on_get(req, resp, id_): |
||
| 540 | if not id_.isdigit() or int(id_) <= 0: |
||
| 541 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 542 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 543 | |||
| 544 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 545 | cursor = cnx.cursor() |
||
| 546 | |||
| 547 | cursor.execute(" SELECT name " |
||
| 548 | " FROM tbl_combined_equipments " |
||
| 549 | " WHERE id = %s ", (id_,)) |
||
| 550 | if cursor.fetchone() is None: |
||
| 551 | cursor.close() |
||
| 552 | cnx.close() |
||
| 553 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 554 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 555 | |||
| 556 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 557 | " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e " |
||
| 558 | " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s " |
||
| 559 | " ORDER BY e.id ") |
||
| 560 | cursor.execute(query, (id_,)) |
||
| 561 | rows = cursor.fetchall() |
||
| 562 | |||
| 563 | result = list() |
||
| 564 | if rows is not None and len(rows) > 0: |
||
| 565 | for row in rows: |
||
| 566 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 567 | result.append(meta_result) |
||
| 568 | |||
| 569 | resp.text = json.dumps(result) |
||
| 570 | |||
| 571 | @staticmethod |
||
| 572 | @user_logger |
||
| 573 | def on_post(req, resp, id_): |
||
| 574 | """Handles POST requests""" |
||
| 575 | access_control(req) |
||
| 576 | try: |
||
| 577 | raw_json = req.stream.read().decode('utf-8') |
||
| 578 | except Exception as ex: |
||
| 579 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 580 | |||
| 581 | if not id_.isdigit() or int(id_) <= 0: |
||
| 582 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 583 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 584 | |||
| 585 | new_values = json.loads(raw_json) |
||
| 586 | |||
| 587 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
| 588 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
| 589 | new_values['data']['equipment_id'] <= 0: |
||
| 590 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 591 | description='API.INVALID_EQUIPMENT_ID') |
||
| 592 | equipment_id = new_values['data']['equipment_id'] |
||
| 593 | |||
| 594 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 595 | cursor = cnx.cursor() |
||
| 596 | |||
| 597 | cursor.execute(" SELECT name " |
||
| 598 | " from tbl_combined_equipments " |
||
| 599 | " WHERE id = %s ", (id_,)) |
||
| 600 | if cursor.fetchone() is None: |
||
| 601 | cursor.close() |
||
| 602 | cnx.close() |
||
| 603 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 604 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 605 | |||
| 606 | cursor.execute(" SELECT name " |
||
| 607 | " FROM tbl_equipments " |
||
| 608 | " WHERE id = %s ", (equipment_id,)) |
||
| 609 | if cursor.fetchone() is None: |
||
| 610 | cursor.close() |
||
| 611 | cnx.close() |
||
| 612 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 613 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 614 | |||
| 615 | query = (" SELECT id " |
||
| 616 | " FROM tbl_combined_equipments_equipments " |
||
| 617 | " WHERE combined_equipment_id = %s AND equipment_id = %s") |
||
| 618 | cursor.execute(query, (id_, equipment_id,)) |
||
| 619 | if cursor.fetchone() is not None: |
||
| 620 | cursor.close() |
||
| 621 | cnx.close() |
||
| 622 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 623 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS') |
||
| 624 | |||
| 625 | add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) " |
||
| 626 | " VALUES (%s, %s) ") |
||
| 627 | cursor.execute(add_row, (id_, equipment_id,)) |
||
| 628 | new_id = cursor.lastrowid |
||
| 629 | cnx.commit() |
||
| 630 | cursor.close() |
||
| 631 | cnx.close() |
||
| 632 | |||
| 633 | resp.status = falcon.HTTP_201 |
||
| 634 | resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id) |
||
| 635 | |||
| 636 | |||
| 637 | View Code Duplication | class CombinedEquipmentEquipmentItem: |
|
| 638 | @staticmethod |
||
| 639 | def __init__(): |
||
| 640 | """Initializes CombinedEquipmentEquipmentItem""" |
||
| 641 | pass |
||
| 642 | |||
| 643 | @staticmethod |
||
| 644 | def on_options(req, resp, id_, eid): |
||
| 645 | resp.status = falcon.HTTP_200 |
||
| 646 | |||
| 647 | @staticmethod |
||
| 648 | @user_logger |
||
| 649 | def on_delete(req, resp, id_, eid): |
||
| 650 | access_control(req) |
||
| 651 | if not id_.isdigit() or int(id_) <= 0: |
||
| 652 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 653 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 654 | |||
| 655 | if not eid.isdigit() or int(eid) <= 0: |
||
| 656 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 657 | description='API.INVALID_EQUIPMENT_ID') |
||
| 658 | |||
| 659 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 660 | cursor = cnx.cursor() |
||
| 661 | |||
| 662 | cursor.execute(" SELECT name " |
||
| 663 | " FROM tbl_combined_equipments " |
||
| 664 | " WHERE id = %s ", (id_,)) |
||
| 665 | if cursor.fetchone() is None: |
||
| 666 | cursor.close() |
||
| 667 | cnx.close() |
||
| 668 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 669 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 670 | |||
| 671 | cursor.execute(" SELECT name " |
||
| 672 | " FROM tbl_equipments " |
||
| 673 | " WHERE id = %s ", (eid,)) |
||
| 674 | if cursor.fetchone() is None: |
||
| 675 | cursor.close() |
||
| 676 | cnx.close() |
||
| 677 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 678 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 679 | |||
| 680 | cursor.execute(" SELECT id " |
||
| 681 | " FROM tbl_combined_equipments_equipments " |
||
| 682 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 683 | if cursor.fetchone() is None: |
||
| 684 | cursor.close() |
||
| 685 | cnx.close() |
||
| 686 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 687 | description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 688 | |||
| 689 | cursor.execute(" DELETE FROM tbl_combined_equipments_equipments " |
||
| 690 | " WHERE combined_equipment_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 691 | cnx.commit() |
||
| 692 | |||
| 693 | cursor.close() |
||
| 694 | cnx.close() |
||
| 695 | |||
| 696 | resp.status = falcon.HTTP_204 |
||
| 697 | |||
| 698 | |||
| 699 | View Code Duplication | class CombinedEquipmentParameterCollection: |
|
| 700 | @staticmethod |
||
| 701 | def __init__(): |
||
| 702 | """Initializes CombinedEquipmentParameterCollection""" |
||
| 703 | pass |
||
| 704 | |||
| 705 | @staticmethod |
||
| 706 | def on_options(req, resp, id_): |
||
| 707 | resp.status = falcon.HTTP_200 |
||
| 708 | |||
| 709 | @staticmethod |
||
| 710 | def on_get(req, resp, id_): |
||
| 711 | if not id_.isdigit() or int(id_) <= 0: |
||
| 712 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 713 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 714 | |||
| 715 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 716 | cursor = cnx.cursor() |
||
| 717 | |||
| 718 | cursor.execute(" SELECT name " |
||
| 719 | " FROM tbl_combined_equipments " |
||
| 720 | " WHERE id = %s ", (id_,)) |
||
| 721 | if cursor.fetchone() is None: |
||
| 722 | cursor.close() |
||
| 723 | cnx.close() |
||
| 724 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 725 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 726 | |||
| 727 | query = (" SELECT id, name " |
||
| 728 | " FROM tbl_points ") |
||
| 729 | cursor.execute(query) |
||
| 730 | rows_points = cursor.fetchall() |
||
| 731 | |||
| 732 | point_dict = dict() |
||
| 733 | if rows_points is not None and len(rows_points) > 0: |
||
| 734 | for row in rows_points: |
||
| 735 | point_dict[row['id']] = {"id": row[0], |
||
| 736 | "name": row[1]} |
||
| 737 | |||
| 738 | query = (" SELECT id, name, uuid " |
||
| 739 | " FROM tbl_meters ") |
||
| 740 | cursor.execute(query) |
||
| 741 | rows_meters = cursor.fetchall() |
||
| 742 | |||
| 743 | meter_dict = dict() |
||
| 744 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 745 | for row in rows_meters: |
||
| 746 | meter_dict[row[2]] = {"type": 'meter', |
||
| 747 | "id": row[0], |
||
| 748 | "name": row[1], |
||
| 749 | "uuid": row[2]} |
||
| 750 | |||
| 751 | query = (" SELECT id, name, uuid " |
||
| 752 | " FROM tbl_offline_meters ") |
||
| 753 | cursor.execute(query) |
||
| 754 | rows_offline_meters = cursor.fetchall() |
||
| 755 | |||
| 756 | offline_meter_dict = dict() |
||
| 757 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 758 | for row in rows_offline_meters: |
||
| 759 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 760 | "id": row[0], |
||
| 761 | "name": row[1], |
||
| 762 | "uuid": row[2]} |
||
| 763 | |||
| 764 | query = (" SELECT id, name, uuid " |
||
| 765 | " FROM tbl_virtual_meters ") |
||
| 766 | cursor.execute(query) |
||
| 767 | rows_virtual_meters = cursor.fetchall() |
||
| 768 | |||
| 769 | virtual_meter_dict = dict() |
||
| 770 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 771 | for row in rows_virtual_meters: |
||
| 772 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 773 | "id": row[0], |
||
| 774 | "name": row[1], |
||
| 775 | "uuid": row[2]} |
||
| 776 | |||
| 777 | query = (" SELECT id, name, parameter_type, " |
||
| 778 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 779 | " FROM tbl_combined_equipments_parameters " |
||
| 780 | " WHERE combined_equipment_id = %s " |
||
| 781 | " ORDER BY id ") |
||
| 782 | cursor.execute(query, (id_, )) |
||
| 783 | rows_parameters = cursor.fetchall() |
||
| 784 | |||
| 785 | result = list() |
||
| 786 | if rows_parameters is not None and len(rows_parameters) > 0: |
||
| 787 | for row in rows_parameters: |
||
| 788 | constant = None |
||
| 789 | point = None |
||
| 790 | numerator_meter = None |
||
| 791 | denominator_meter = None |
||
| 792 | if row[2] == 'point': |
||
| 793 | point = point_dict.get(row[4], None) |
||
| 794 | constant = None |
||
| 795 | numerator_meter = None |
||
| 796 | denominator_meter = None |
||
| 797 | elif row[2] == 'constant': |
||
| 798 | constant = row[3] |
||
| 799 | point = None |
||
| 800 | numerator_meter = None |
||
| 801 | denominator_meter = None |
||
| 802 | elif row[2] == 'fraction': |
||
| 803 | constant = None |
||
| 804 | point = None |
||
| 805 | # find numerator meter by uuid |
||
| 806 | numerator_meter = meter_dict.get(row[5], None) |
||
| 807 | if numerator_meter is None: |
||
| 808 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 809 | if numerator_meter is None: |
||
| 810 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 811 | # find denominator meter by uuid |
||
| 812 | denominator_meter = meter_dict.get(row[6], None) |
||
| 813 | if denominator_meter is None: |
||
| 814 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 815 | if denominator_meter is None: |
||
| 816 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 817 | |||
| 818 | meta_result = {"id": row[0], |
||
| 819 | "name": row[1], |
||
| 820 | "parameter_type": row[2], |
||
| 821 | "constant": constant, |
||
| 822 | "point": point, |
||
| 823 | "numerator_meter": numerator_meter, |
||
| 824 | "denominator_meter": denominator_meter} |
||
| 825 | result.append(meta_result) |
||
| 826 | |||
| 827 | cursor.close() |
||
| 828 | cnx.close() |
||
| 829 | resp.text = json.dumps(result) |
||
| 830 | |||
| 831 | @staticmethod |
||
| 832 | @user_logger |
||
| 833 | def on_post(req, resp, id_): |
||
| 834 | """Handles POST requests""" |
||
| 835 | access_control(req) |
||
| 836 | if not id_.isdigit() or int(id_) <= 0: |
||
| 837 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 838 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 839 | try: |
||
| 840 | raw_json = req.stream.read().decode('utf-8') |
||
| 841 | except Exception as ex: |
||
| 842 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 843 | |||
| 844 | new_values = json.loads(raw_json) |
||
| 845 | |||
| 846 | if 'name' not in new_values['data'].keys() or \ |
||
| 847 | not isinstance(new_values['data']['name'], str) or \ |
||
| 848 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 849 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 850 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 851 | name = str.strip(new_values['data']['name']) |
||
| 852 | |||
| 853 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 854 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 855 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 856 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 857 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 858 | |||
| 859 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 860 | |||
| 861 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 862 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 863 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 864 | |||
| 865 | constant = None |
||
| 866 | if 'constant' in new_values['data'].keys(): |
||
| 867 | if new_values['data']['constant'] is not None and \ |
||
| 868 | isinstance(new_values['data']['constant'], str) and \ |
||
| 869 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 870 | constant = str.strip(new_values['data']['constant']) |
||
| 871 | |||
| 872 | point_id = None |
||
| 873 | if 'point_id' in new_values['data'].keys(): |
||
| 874 | if new_values['data']['point_id'] is not None and \ |
||
| 875 | new_values['data']['point_id'] <= 0: |
||
| 876 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 877 | description='API.INVALID_POINT_ID') |
||
| 878 | point_id = new_values['data']['point_id'] |
||
| 879 | |||
| 880 | numerator_meter_uuid = None |
||
| 881 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 882 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 883 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 884 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 885 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 886 | |||
| 887 | denominator_meter_uuid = None |
||
| 888 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 889 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 890 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 891 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 892 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 893 | |||
| 894 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 895 | cursor = cnx.cursor() |
||
| 896 | |||
| 897 | cursor.execute(" SELECT name " |
||
| 898 | " FROM tbl_combined_equipments " |
||
| 899 | " WHERE id = %s ", (id_,)) |
||
| 900 | if cursor.fetchone() is None: |
||
| 901 | cursor.close() |
||
| 902 | cnx.close() |
||
| 903 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 904 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 905 | |||
| 906 | cursor.execute(" SELECT name " |
||
| 907 | " FROM tbl_combined_equipments_parameters " |
||
| 908 | " WHERE name = %s AND combined_equipment_id = %s ", (name, id_)) |
||
| 909 | if cursor.fetchone() is not None: |
||
| 910 | cursor.close() |
||
| 911 | cnx.close() |
||
| 912 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 913 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 914 | |||
| 915 | # validate by parameter type |
||
| 916 | if parameter_type == 'point': |
||
| 917 | if point_id is None: |
||
| 918 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 919 | description='API.INVALID_POINT_ID') |
||
| 920 | |||
| 921 | query = (" SELECT id, name " |
||
| 922 | " FROM tbl_points " |
||
| 923 | " WHERE id = %s ") |
||
| 924 | cursor.execute(query, (point_id, )) |
||
| 925 | if cursor.fetchone() is None: |
||
| 926 | cursor.close() |
||
| 927 | cnx.close() |
||
| 928 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 929 | description='API.POINT_NOT_FOUND') |
||
| 930 | |||
| 931 | elif parameter_type == 'constant': |
||
| 932 | if constant is None: |
||
| 933 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 934 | description='API.INVALID_CONSTANT_VALUE') |
||
| 935 | |||
| 936 | elif parameter_type == 'fraction': |
||
| 937 | |||
| 938 | query = (" SELECT id, name, uuid " |
||
| 939 | " FROM tbl_meters ") |
||
| 940 | cursor.execute(query) |
||
| 941 | rows_meters = cursor.fetchall() |
||
| 942 | |||
| 943 | meter_dict = dict() |
||
| 944 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 945 | for row in rows_meters: |
||
| 946 | meter_dict[row[2]] = {"type": 'meter', |
||
| 947 | "id": row[0], |
||
| 948 | "name": row[1], |
||
| 949 | "uuid": row[2]} |
||
| 950 | |||
| 951 | query = (" SELECT id, name, uuid " |
||
| 952 | " FROM tbl_offline_meters ") |
||
| 953 | cursor.execute(query) |
||
| 954 | rows_offline_meters = cursor.fetchall() |
||
| 955 | |||
| 956 | offline_meter_dict = dict() |
||
| 957 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 958 | for row in rows_offline_meters: |
||
| 959 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 960 | "id": row[0], |
||
| 961 | "name": row[1], |
||
| 962 | "uuid": row[2]} |
||
| 963 | |||
| 964 | query = (" SELECT id, name, uuid " |
||
| 965 | " FROM tbl_virtual_meters ") |
||
| 966 | cursor.execute(query) |
||
| 967 | rows_virtual_meters = cursor.fetchall() |
||
| 968 | |||
| 969 | virtual_meter_dict = dict() |
||
| 970 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 971 | for row in rows_virtual_meters: |
||
| 972 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 973 | "id": row[0], |
||
| 974 | "name": row[1], |
||
| 975 | "uuid": row[2]} |
||
| 976 | |||
| 977 | # validate numerator meter uuid |
||
| 978 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 979 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 980 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 981 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 982 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 983 | |||
| 984 | # validate denominator meter uuid |
||
| 985 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 986 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 987 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 988 | |||
| 989 | if denominator_meter_uuid not in meter_dict and \ |
||
| 990 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 991 | denominator_meter_uuid not in offline_meter_dict: |
||
| 992 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 993 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 994 | |||
| 995 | add_values = (" INSERT INTO tbl_combined_equipments_parameters " |
||
| 996 | " (combined_equipment_id, name, parameter_type, constant, " |
||
| 997 | " point_id, numerator_meter_uuid, denominator_meter_uuid) " |
||
| 998 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 999 | cursor.execute(add_values, (id_, |
||
| 1000 | name, |
||
| 1001 | parameter_type, |
||
| 1002 | constant, |
||
| 1003 | point_id, |
||
| 1004 | numerator_meter_uuid, |
||
| 1005 | denominator_meter_uuid)) |
||
| 1006 | new_id = cursor.lastrowid |
||
| 1007 | cnx.commit() |
||
| 1008 | cursor.close() |
||
| 1009 | cnx.close() |
||
| 1010 | |||
| 1011 | resp.status = falcon.HTTP_201 |
||
| 1012 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
||
| 1013 | |||
| 1014 | |||
| 1015 | View Code Duplication | class CombinedEquipmentParameterItem: |
|
| 1016 | @staticmethod |
||
| 1017 | def __init__(): |
||
| 1018 | """"Initializes CombinedEquipmentParameterItem""" |
||
| 1019 | pass |
||
| 1020 | |||
| 1021 | @staticmethod |
||
| 1022 | def on_options(req, resp, id_, pid): |
||
| 1023 | resp.status = falcon.HTTP_200 |
||
| 1024 | |||
| 1025 | @staticmethod |
||
| 1026 | def on_get(req, resp, id_, pid): |
||
| 1027 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1028 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1029 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1030 | |||
| 1031 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1032 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1033 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1034 | |||
| 1035 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1036 | cursor = cnx.cursor() |
||
| 1037 | |||
| 1038 | query = (" SELECT id, name " |
||
| 1039 | " FROM tbl_points ") |
||
| 1040 | cursor.execute(query) |
||
| 1041 | rows_points = cursor.fetchall() |
||
| 1042 | |||
| 1043 | point_dict = dict() |
||
| 1044 | if rows_points is not None and len(rows_points) > 0: |
||
| 1045 | for row in rows_points: |
||
| 1046 | point_dict[row[0]] = {"id": row[0], |
||
| 1047 | "name": row[1]} |
||
| 1048 | |||
| 1049 | query = (" SELECT id, name, uuid " |
||
| 1050 | " FROM tbl_meters ") |
||
| 1051 | cursor.execute(query) |
||
| 1052 | rows_meters = cursor.fetchall() |
||
| 1053 | |||
| 1054 | meter_dict = dict() |
||
| 1055 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1056 | for row in rows_meters: |
||
| 1057 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1058 | "id": row[0], |
||
| 1059 | "name": row[1], |
||
| 1060 | "uuid": row[2]} |
||
| 1061 | |||
| 1062 | query = (" SELECT id, name, uuid " |
||
| 1063 | " FROM tbl_offline_meters ") |
||
| 1064 | cursor.execute(query) |
||
| 1065 | rows_offline_meters = cursor.fetchall() |
||
| 1066 | |||
| 1067 | offline_meter_dict = dict() |
||
| 1068 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1069 | for row in rows_offline_meters: |
||
| 1070 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1071 | "id": row[0], |
||
| 1072 | "name": row[1], |
||
| 1073 | "uuid": row[2]} |
||
| 1074 | |||
| 1075 | query = (" SELECT id, name, uuid " |
||
| 1076 | " FROM tbl_virtual_meters ") |
||
| 1077 | cursor.execute(query) |
||
| 1078 | rows_virtual_meters = cursor.fetchall() |
||
| 1079 | |||
| 1080 | virtual_meter_dict = dict() |
||
| 1081 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1082 | for row in rows_virtual_meters: |
||
| 1083 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1084 | "id": row[0], |
||
| 1085 | "name": row[1], |
||
| 1086 | "uuid": row[2]} |
||
| 1087 | |||
| 1088 | query = (" SELECT id, name, parameter_type, " |
||
| 1089 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
||
| 1090 | " FROM tbl_combined_equipments_parameters " |
||
| 1091 | " WHERE combined_equipment_id = %s AND id = %s ") |
||
| 1092 | cursor.execute(query, (id_, pid)) |
||
| 1093 | row = cursor.fetchone() |
||
| 1094 | cursor.close() |
||
| 1095 | cnx.close() |
||
| 1096 | |||
| 1097 | if row is None: |
||
| 1098 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1099 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1100 | else: |
||
| 1101 | constant = None |
||
| 1102 | point = None |
||
| 1103 | numerator_meter = None |
||
| 1104 | denominator_meter = None |
||
| 1105 | if row[2] == 'point': |
||
| 1106 | point = point_dict.get(row[4], None) |
||
| 1107 | constant = None |
||
| 1108 | numerator_meter = None |
||
| 1109 | denominator_meter = None |
||
| 1110 | elif row[2] == 'constant': |
||
| 1111 | constant = row[3] |
||
| 1112 | point = None |
||
| 1113 | numerator_meter = None |
||
| 1114 | denominator_meter = None |
||
| 1115 | elif row[2] == 'fraction': |
||
| 1116 | constant = None |
||
| 1117 | point = None |
||
| 1118 | # find numerator meter by uuid |
||
| 1119 | numerator_meter = meter_dict.get(row[5], None) |
||
| 1120 | if numerator_meter is None: |
||
| 1121 | numerator_meter = virtual_meter_dict.get(row[5], None) |
||
| 1122 | if numerator_meter is None: |
||
| 1123 | numerator_meter = offline_meter_dict.get(row[5], None) |
||
| 1124 | # find denominator meter by uuid |
||
| 1125 | denominator_meter = meter_dict.get(row[6], None) |
||
| 1126 | if denominator_meter is None: |
||
| 1127 | denominator_meter = virtual_meter_dict.get(row[6], None) |
||
| 1128 | if denominator_meter is None: |
||
| 1129 | denominator_meter = offline_meter_dict.get(row[6], None) |
||
| 1130 | |||
| 1131 | meta_result = {"id": row[0], |
||
| 1132 | "name": row[1], |
||
| 1133 | "parameter_type": row[2], |
||
| 1134 | "constant": constant, |
||
| 1135 | "point": point, |
||
| 1136 | "numerator_meter": numerator_meter, |
||
| 1137 | "denominator_meter": denominator_meter} |
||
| 1138 | |||
| 1139 | resp.text = json.dumps(meta_result) |
||
| 1140 | |||
| 1141 | @staticmethod |
||
| 1142 | @user_logger |
||
| 1143 | def on_delete(req, resp, id_, pid): |
||
| 1144 | access_control(req) |
||
| 1145 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1146 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1147 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1148 | |||
| 1149 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1150 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1151 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1152 | |||
| 1153 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1154 | cursor = cnx.cursor() |
||
| 1155 | |||
| 1156 | cursor.execute(" SELECT name " |
||
| 1157 | " FROM tbl_combined_equipments " |
||
| 1158 | " WHERE id = %s ", |
||
| 1159 | (id_,)) |
||
| 1160 | row = cursor.fetchone() |
||
| 1161 | if row is None: |
||
| 1162 | cursor.close() |
||
| 1163 | cnx.close() |
||
| 1164 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1165 | title='API.NOT_FOUND', |
||
| 1166 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1167 | |||
| 1168 | cursor.execute(" SELECT name " |
||
| 1169 | " FROM tbl_combined_equipments_parameters " |
||
| 1170 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1171 | (id_, pid,)) |
||
| 1172 | row = cursor.fetchone() |
||
| 1173 | if row is None: |
||
| 1174 | cursor.close() |
||
| 1175 | cnx.close() |
||
| 1176 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1177 | title='API.NOT_FOUND', |
||
| 1178 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1179 | |||
| 1180 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
||
| 1181 | " WHERE id = %s ", (pid, )) |
||
| 1182 | cnx.commit() |
||
| 1183 | |||
| 1184 | cursor.close() |
||
| 1185 | cnx.close() |
||
| 1186 | |||
| 1187 | resp.status = falcon.HTTP_204 |
||
| 1188 | |||
| 1189 | @staticmethod |
||
| 1190 | @user_logger |
||
| 1191 | def on_put(req, resp, id_, pid): |
||
| 1192 | """Handles PUT requests""" |
||
| 1193 | access_control(req) |
||
| 1194 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1195 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1196 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1197 | |||
| 1198 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1199 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1200 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
||
| 1201 | |||
| 1202 | try: |
||
| 1203 | raw_json = req.stream.read().decode('utf-8') |
||
| 1204 | except Exception as ex: |
||
| 1205 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 1206 | |||
| 1207 | new_values = json.loads(raw_json) |
||
| 1208 | |||
| 1209 | if 'name' not in new_values['data'].keys() or \ |
||
| 1210 | not isinstance(new_values['data']['name'], str) or \ |
||
| 1211 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 1212 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1213 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
||
| 1214 | name = str.strip(new_values['data']['name']) |
||
| 1215 | |||
| 1216 | if 'parameter_type' not in new_values['data'].keys() or \ |
||
| 1217 | not isinstance(new_values['data']['parameter_type'], str) or \ |
||
| 1218 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
||
| 1219 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1220 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1221 | |||
| 1222 | parameter_type = str.strip(new_values['data']['parameter_type']) |
||
| 1223 | |||
| 1224 | if parameter_type not in ('constant', 'point', 'fraction'): |
||
| 1225 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1226 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
||
| 1227 | |||
| 1228 | constant = None |
||
| 1229 | if 'constant' in new_values['data'].keys(): |
||
| 1230 | if new_values['data']['constant'] is not None and \ |
||
| 1231 | isinstance(new_values['data']['constant'], str) and \ |
||
| 1232 | len(str.strip(new_values['data']['constant'])) > 0: |
||
| 1233 | constant = str.strip(new_values['data']['constant']) |
||
| 1234 | |||
| 1235 | point_id = None |
||
| 1236 | if 'point_id' in new_values['data'].keys(): |
||
| 1237 | if new_values['data']['point_id'] is not None and \ |
||
| 1238 | new_values['data']['point_id'] <= 0: |
||
| 1239 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1240 | description='API.INVALID_POINT_ID') |
||
| 1241 | point_id = new_values['data']['point_id'] |
||
| 1242 | |||
| 1243 | numerator_meter_uuid = None |
||
| 1244 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
||
| 1245 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
||
| 1246 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
||
| 1247 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
||
| 1248 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
||
| 1249 | |||
| 1250 | denominator_meter_uuid = None |
||
| 1251 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
||
| 1252 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
||
| 1253 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
||
| 1254 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
||
| 1255 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
||
| 1256 | |||
| 1257 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1258 | cursor = cnx.cursor() |
||
| 1259 | |||
| 1260 | cursor.execute(" SELECT name " |
||
| 1261 | " FROM tbl_combined_equipments " |
||
| 1262 | " WHERE id = %s ", (id_,)) |
||
| 1263 | if cursor.fetchone() is None: |
||
| 1264 | cursor.close() |
||
| 1265 | cnx.close() |
||
| 1266 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
||
| 1267 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1268 | |||
| 1269 | cursor.execute(" SELECT name " |
||
| 1270 | " FROM tbl_combined_equipments_parameters " |
||
| 1271 | " WHERE combined_equipment_id = %s AND id = %s ", |
||
| 1272 | (id_, pid,)) |
||
| 1273 | row = cursor.fetchone() |
||
| 1274 | if row is None: |
||
| 1275 | cursor.close() |
||
| 1276 | cnx.close() |
||
| 1277 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 1278 | title='API.NOT_FOUND', |
||
| 1279 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
||
| 1280 | |||
| 1281 | cursor.execute(" SELECT name " |
||
| 1282 | " FROM tbl_combined_equipments_parameters " |
||
| 1283 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
||
| 1284 | row = cursor.fetchone() |
||
| 1285 | if row is not None: |
||
| 1286 | cursor.close() |
||
| 1287 | cnx.close() |
||
| 1288 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1289 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
||
| 1290 | |||
| 1291 | # validate by parameter type |
||
| 1292 | if parameter_type == 'point': |
||
| 1293 | if point_id is None: |
||
| 1294 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1295 | description='API.INVALID_POINT_ID') |
||
| 1296 | |||
| 1297 | query = (" SELECT id, name " |
||
| 1298 | " FROM tbl_points " |
||
| 1299 | " WHERE id = %s ") |
||
| 1300 | cursor.execute(query, (point_id, )) |
||
| 1301 | row = cursor.fetchone() |
||
| 1302 | if row is None: |
||
| 1303 | cursor.close() |
||
| 1304 | cnx.close() |
||
| 1305 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1306 | description='API.POINT_NOT_FOUND') |
||
| 1307 | |||
| 1308 | elif parameter_type == 'constant': |
||
| 1309 | if constant is None: |
||
| 1310 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1311 | description='API.INVALID_CONSTANT_VALUE') |
||
| 1312 | |||
| 1313 | elif parameter_type == 'fraction': |
||
| 1314 | |||
| 1315 | query = (" SELECT id, name, uuid " |
||
| 1316 | " FROM tbl_meters ") |
||
| 1317 | cursor.execute(query) |
||
| 1318 | rows_meters = cursor.fetchall() |
||
| 1319 | |||
| 1320 | meter_dict = dict() |
||
| 1321 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 1322 | for row in rows_meters: |
||
| 1323 | meter_dict[row[2]] = {"type": 'meter', |
||
| 1324 | "id": row[0], |
||
| 1325 | "name": row[1], |
||
| 1326 | "uuid": row[2]} |
||
| 1327 | |||
| 1328 | query = (" SELECT id, name, uuid " |
||
| 1329 | " FROM tbl_offline_meters ") |
||
| 1330 | cursor.execute(query) |
||
| 1331 | rows_offline_meters = cursor.fetchall() |
||
| 1332 | |||
| 1333 | offline_meter_dict = dict() |
||
| 1334 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 1335 | for row in rows_offline_meters: |
||
| 1336 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
||
| 1337 | "id": row[0], |
||
| 1338 | "name": row[1], |
||
| 1339 | "uuid": row[2]} |
||
| 1340 | |||
| 1341 | query = (" SELECT id, name, uuid " |
||
| 1342 | " FROM tbl_virtual_meters ") |
||
| 1343 | cursor.execute(query) |
||
| 1344 | rows_virtual_meters = cursor.fetchall() |
||
| 1345 | |||
| 1346 | virtual_meter_dict = dict() |
||
| 1347 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 1348 | for row in rows_virtual_meters: |
||
| 1349 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
||
| 1350 | "id": row[0], |
||
| 1351 | "name": row[1], |
||
| 1352 | "uuid": row[2]} |
||
| 1353 | |||
| 1354 | # validate numerator meter uuid |
||
| 1355 | if meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1356 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
||
| 1357 | offline_meter_dict.get(numerator_meter_uuid) is None: |
||
| 1358 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1359 | description='API.INVALID_NUMERATOR_METER_UUID') |
||
| 1360 | |||
| 1361 | # validate denominator meter uuid |
||
| 1362 | if denominator_meter_uuid == numerator_meter_uuid: |
||
| 1363 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1364 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1365 | |||
| 1366 | if denominator_meter_uuid not in meter_dict and \ |
||
| 1367 | denominator_meter_uuid not in virtual_meter_dict and \ |
||
| 1368 | denominator_meter_uuid not in offline_meter_dict: |
||
| 1369 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1370 | description='API.INVALID_DENOMINATOR_METER_UUID') |
||
| 1371 | |||
| 1372 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
||
| 1373 | " SET name = %s , parameter_type = %s, constant = %s, " |
||
| 1374 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid =%s " |
||
| 1375 | " WHERE id = %s ") |
||
| 1376 | cursor.execute(add_values, (name, |
||
| 1377 | parameter_type, |
||
| 1378 | constant, |
||
| 1379 | point_id, |
||
| 1380 | numerator_meter_uuid, |
||
| 1381 | denominator_meter_uuid, |
||
| 1382 | pid)) |
||
| 1383 | cnx.commit() |
||
| 1384 | |||
| 1385 | cursor.close() |
||
| 1386 | cnx.close() |
||
| 1387 | |||
| 1388 | resp.status = falcon.HTTP_200 |
||
| 1389 | |||
| 1390 | |||
| 1391 | View Code Duplication | class CombinedEquipmentMeterCollection: |
|
| 1392 | @staticmethod |
||
| 1393 | def __init__(): |
||
| 1394 | """"Initializes CombinedEquipmentMeterCollection""" |
||
| 1395 | pass |
||
| 1396 | |||
| 1397 | @staticmethod |
||
| 1398 | def on_options(req, resp, id_): |
||
| 1399 | resp.status = falcon.HTTP_200 |
||
| 1400 | |||
| 1401 | @staticmethod |
||
| 1402 | def on_get(req, resp, id_): |
||
| 1403 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1404 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1405 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1406 | |||
| 1407 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1408 | cursor = cnx.cursor(dictionary=True) |
||
| 1409 | |||
| 1410 | cursor.execute(" SELECT name " |
||
| 1411 | " FROM tbl_combined_equipments " |
||
| 1412 | " WHERE id = %s ", (id_,)) |
||
| 1413 | if cursor.fetchone() is None: |
||
| 1414 | cursor.close() |
||
| 1415 | cnx.close() |
||
| 1416 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1417 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1418 | |||
| 1419 | query = (" SELECT id, name, uuid " |
||
| 1420 | " FROM tbl_energy_categories ") |
||
| 1421 | cursor.execute(query) |
||
| 1422 | rows_energy_categories = cursor.fetchall() |
||
| 1423 | |||
| 1424 | energy_category_dict = dict() |
||
| 1425 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1426 | for row in rows_energy_categories: |
||
| 1427 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 1428 | "name": row['name'], |
||
| 1429 | "uuid": row['uuid']} |
||
| 1430 | |||
| 1431 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1432 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
||
| 1433 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
||
| 1434 | " ORDER BY m.id ") |
||
| 1435 | cursor.execute(query, (id_,)) |
||
| 1436 | rows = cursor.fetchall() |
||
| 1437 | |||
| 1438 | result = list() |
||
| 1439 | if rows is not None and len(rows) > 0: |
||
| 1440 | for row in rows: |
||
| 1441 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 1442 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 1443 | "energy_category": energy_category, |
||
| 1444 | "is_output": bool(row['is_output'])} |
||
| 1445 | result.append(meta_result) |
||
| 1446 | |||
| 1447 | resp.text = json.dumps(result) |
||
| 1448 | |||
| 1449 | @staticmethod |
||
| 1450 | @user_logger |
||
| 1451 | def on_post(req, resp, id_): |
||
| 1452 | """Handles POST requests""" |
||
| 1453 | access_control(req) |
||
| 1454 | try: |
||
| 1455 | raw_json = req.stream.read().decode('utf-8') |
||
| 1456 | except Exception as ex: |
||
| 1457 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1458 | |||
| 1459 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1460 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1461 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1462 | |||
| 1463 | new_values = json.loads(raw_json) |
||
| 1464 | |||
| 1465 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1466 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1467 | new_values['data']['meter_id'] <= 0: |
||
| 1468 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1469 | description='API.INVALID_METER_ID') |
||
| 1470 | meter_id = new_values['data']['meter_id'] |
||
| 1471 | |||
| 1472 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1473 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1474 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1475 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1476 | is_output = new_values['data']['is_output'] |
||
| 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(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1488 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1489 | |||
| 1490 | cursor.execute(" SELECT name " |
||
| 1491 | " FROM tbl_meters " |
||
| 1492 | " WHERE id = %s ", (meter_id,)) |
||
| 1493 | if cursor.fetchone() is None: |
||
| 1494 | cursor.close() |
||
| 1495 | cnx.close() |
||
| 1496 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1497 | description='API.METER_NOT_FOUND') |
||
| 1498 | |||
| 1499 | query = (" SELECT id " |
||
| 1500 | " FROM tbl_combined_equipments_meters " |
||
| 1501 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
||
| 1502 | cursor.execute(query, (id_, meter_id,)) |
||
| 1503 | if cursor.fetchone() is not None: |
||
| 1504 | cursor.close() |
||
| 1505 | cnx.close() |
||
| 1506 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1507 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
||
| 1508 | |||
| 1509 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
||
| 1510 | " VALUES (%s, %s, %s) ") |
||
| 1511 | cursor.execute(add_row, (id_, meter_id, is_output)) |
||
| 1512 | new_id = cursor.lastrowid |
||
| 1513 | cnx.commit() |
||
| 1514 | cursor.close() |
||
| 1515 | cnx.close() |
||
| 1516 | |||
| 1517 | resp.status = falcon.HTTP_201 |
||
| 1518 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
||
| 1519 | |||
| 1520 | |||
| 1521 | View Code Duplication | class CombinedEquipmentMeterItem: |
|
| 1522 | @staticmethod |
||
| 1523 | def __init__(): |
||
| 1524 | """"Initializes CombinedEquipmentMeterItem""" |
||
| 1525 | pass |
||
| 1526 | |||
| 1527 | @staticmethod |
||
| 1528 | def on_options(req, resp, id_, mid): |
||
| 1529 | resp.status = falcon.HTTP_200 |
||
| 1530 | |||
| 1531 | @staticmethod |
||
| 1532 | @user_logger |
||
| 1533 | def on_delete(req, resp, id_, mid): |
||
| 1534 | access_control(req) |
||
| 1535 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1536 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1537 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1538 | |||
| 1539 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1540 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1541 | description='API.INVALID_METER_ID') |
||
| 1542 | |||
| 1543 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1544 | cursor = cnx.cursor() |
||
| 1545 | |||
| 1546 | cursor.execute(" SELECT name " |
||
| 1547 | " FROM tbl_combined_equipments " |
||
| 1548 | " WHERE id = %s ", (id_,)) |
||
| 1549 | if cursor.fetchone() is None: |
||
| 1550 | cursor.close() |
||
| 1551 | cnx.close() |
||
| 1552 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1553 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1554 | |||
| 1555 | cursor.execute(" SELECT name " |
||
| 1556 | " FROM tbl_meters " |
||
| 1557 | " WHERE id = %s ", (mid,)) |
||
| 1558 | if cursor.fetchone() is None: |
||
| 1559 | cursor.close() |
||
| 1560 | cnx.close() |
||
| 1561 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1562 | description='API.METER_NOT_FOUND') |
||
| 1563 | |||
| 1564 | cursor.execute(" SELECT id " |
||
| 1565 | " FROM tbl_combined_equipments_meters " |
||
| 1566 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1567 | if cursor.fetchone() is None: |
||
| 1568 | cursor.close() |
||
| 1569 | cnx.close() |
||
| 1570 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1571 | description='API.COMBINED_EQUIPMENT_METER_RELATION_NOT_FOUND') |
||
| 1572 | |||
| 1573 | cursor.execute(" DELETE FROM tbl_combined_equipments_meters " |
||
| 1574 | " WHERE combined_equipment_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1575 | cnx.commit() |
||
| 1576 | |||
| 1577 | cursor.close() |
||
| 1578 | cnx.close() |
||
| 1579 | |||
| 1580 | resp.status = falcon.HTTP_204 |
||
| 1581 | |||
| 1582 | |||
| 1583 | View Code Duplication | class CombinedEquipmentOfflineMeterCollection: |
|
| 1584 | @staticmethod |
||
| 1585 | def __init__(): |
||
| 1586 | """"Initializes CombinedEquipmentOfflineMeterCollection""" |
||
| 1587 | pass |
||
| 1588 | |||
| 1589 | @staticmethod |
||
| 1590 | def on_options(req, resp, id_): |
||
| 1591 | resp.status = falcon.HTTP_200 |
||
| 1592 | |||
| 1593 | @staticmethod |
||
| 1594 | def on_get(req, resp, id_): |
||
| 1595 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1596 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1597 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1598 | |||
| 1599 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1600 | cursor = cnx.cursor(dictionary=True) |
||
| 1601 | |||
| 1602 | cursor.execute(" SELECT name " |
||
| 1603 | " FROM tbl_combined_equipments " |
||
| 1604 | " WHERE id = %s ", (id_,)) |
||
| 1605 | if cursor.fetchone() is None: |
||
| 1606 | cursor.close() |
||
| 1607 | cnx.close() |
||
| 1608 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1609 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1610 | |||
| 1611 | query = (" SELECT id, name, uuid " |
||
| 1612 | " FROM tbl_energy_categories ") |
||
| 1613 | cursor.execute(query) |
||
| 1614 | rows_energy_categories = cursor.fetchall() |
||
| 1615 | |||
| 1616 | energy_category_dict = dict() |
||
| 1617 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1618 | for row in rows_energy_categories: |
||
| 1619 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 1620 | "name": row['name'], |
||
| 1621 | "uuid": row['uuid']} |
||
| 1622 | |||
| 1623 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1624 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
||
| 1625 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
||
| 1626 | " ORDER BY m.id ") |
||
| 1627 | cursor.execute(query, (id_,)) |
||
| 1628 | rows = cursor.fetchall() |
||
| 1629 | |||
| 1630 | result = list() |
||
| 1631 | if rows is not None and len(rows) > 0: |
||
| 1632 | for row in rows: |
||
| 1633 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 1634 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 1635 | "energy_category": energy_category, |
||
| 1636 | "is_output": bool(row['is_output'])} |
||
| 1637 | result.append(meta_result) |
||
| 1638 | |||
| 1639 | resp.text = json.dumps(result) |
||
| 1640 | |||
| 1641 | @staticmethod |
||
| 1642 | @user_logger |
||
| 1643 | def on_post(req, resp, id_): |
||
| 1644 | """Handles POST requests""" |
||
| 1645 | access_control(req) |
||
| 1646 | try: |
||
| 1647 | raw_json = req.stream.read().decode('utf-8') |
||
| 1648 | except Exception as ex: |
||
| 1649 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1650 | |||
| 1651 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1652 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1653 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1654 | |||
| 1655 | new_values = json.loads(raw_json) |
||
| 1656 | |||
| 1657 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 1658 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 1659 | new_values['data']['offline_meter_id'] <= 0: |
||
| 1660 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1661 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1662 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 1663 | |||
| 1664 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1665 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1666 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1667 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1668 | is_output = new_values['data']['is_output'] |
||
| 1669 | |||
| 1670 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1671 | cursor = cnx.cursor() |
||
| 1672 | |||
| 1673 | cursor.execute(" SELECT name " |
||
| 1674 | " from tbl_combined_equipments " |
||
| 1675 | " WHERE id = %s ", (id_,)) |
||
| 1676 | if cursor.fetchone() is None: |
||
| 1677 | cursor.close() |
||
| 1678 | cnx.close() |
||
| 1679 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1680 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1681 | |||
| 1682 | cursor.execute(" SELECT name " |
||
| 1683 | " FROM tbl_offline_meters " |
||
| 1684 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 1685 | if cursor.fetchone() is None: |
||
| 1686 | cursor.close() |
||
| 1687 | cnx.close() |
||
| 1688 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1689 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1690 | |||
| 1691 | query = (" SELECT id " |
||
| 1692 | " FROM tbl_combined_equipments_offline_meters " |
||
| 1693 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
||
| 1694 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 1695 | if cursor.fetchone() is not None: |
||
| 1696 | cursor.close() |
||
| 1697 | cnx.close() |
||
| 1698 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1699 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
||
| 1700 | |||
| 1701 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
||
| 1702 | " (combined_equipment_id, offline_meter_id, is_output ) " |
||
| 1703 | " VALUES (%s, %s, %s) ") |
||
| 1704 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
||
| 1705 | new_id = cursor.lastrowid |
||
| 1706 | cnx.commit() |
||
| 1707 | cursor.close() |
||
| 1708 | cnx.close() |
||
| 1709 | |||
| 1710 | resp.status = falcon.HTTP_201 |
||
| 1711 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 1712 | |||
| 1713 | |||
| 1714 | View Code Duplication | class CombinedEquipmentOfflineMeterItem: |
|
| 1715 | @staticmethod |
||
| 1716 | def __init__(): |
||
| 1717 | """"Initializes CombinedEquipmentOfflineMeterItem""" |
||
| 1718 | pass |
||
| 1719 | |||
| 1720 | @staticmethod |
||
| 1721 | def on_options(req, resp, id_, mid): |
||
| 1722 | resp.status = falcon.HTTP_200 |
||
| 1723 | |||
| 1724 | @staticmethod |
||
| 1725 | @user_logger |
||
| 1726 | def on_delete(req, resp, id_, mid): |
||
| 1727 | access_control(req) |
||
| 1728 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1729 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1730 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1731 | |||
| 1732 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1733 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1734 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1735 | |||
| 1736 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1737 | cursor = cnx.cursor() |
||
| 1738 | |||
| 1739 | cursor.execute(" SELECT name " |
||
| 1740 | " FROM tbl_combined_equipments " |
||
| 1741 | " WHERE id = %s ", (id_,)) |
||
| 1742 | if cursor.fetchone() is None: |
||
| 1743 | cursor.close() |
||
| 1744 | cnx.close() |
||
| 1745 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1746 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1747 | |||
| 1748 | cursor.execute(" SELECT name " |
||
| 1749 | " FROM tbl_offline_meters " |
||
| 1750 | " WHERE id = %s ", (mid,)) |
||
| 1751 | if cursor.fetchone() is None: |
||
| 1752 | cursor.close() |
||
| 1753 | cnx.close() |
||
| 1754 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1755 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1756 | |||
| 1757 | cursor.execute(" SELECT id " |
||
| 1758 | " FROM tbl_combined_equipments_offline_meters " |
||
| 1759 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1760 | if cursor.fetchone() is None: |
||
| 1761 | cursor.close() |
||
| 1762 | cnx.close() |
||
| 1763 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1764 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 1765 | |||
| 1766 | cursor.execute(" DELETE FROM tbl_combined_equipments_offline_meters " |
||
| 1767 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1768 | cnx.commit() |
||
| 1769 | |||
| 1770 | cursor.close() |
||
| 1771 | cnx.close() |
||
| 1772 | |||
| 1773 | resp.status = falcon.HTTP_204 |
||
| 1774 | |||
| 1775 | |||
| 1776 | View Code Duplication | class CombinedEquipmentVirtualMeterCollection: |
|
| 1777 | @staticmethod |
||
| 1778 | def __init__(): |
||
| 1779 | """"Initializes CombinedEquipmentVirtualMeterCollection""" |
||
| 1780 | pass |
||
| 1781 | |||
| 1782 | @staticmethod |
||
| 1783 | def on_options(req, resp, id_): |
||
| 1784 | resp.status = falcon.HTTP_200 |
||
| 1785 | |||
| 1786 | @staticmethod |
||
| 1787 | def on_get(req, resp, id_): |
||
| 1788 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1789 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1790 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1791 | |||
| 1792 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1793 | cursor = cnx.cursor(dictionary=True) |
||
| 1794 | |||
| 1795 | cursor.execute(" SELECT name " |
||
| 1796 | " FROM tbl_combined_equipments " |
||
| 1797 | " WHERE id = %s ", (id_,)) |
||
| 1798 | if cursor.fetchone() is None: |
||
| 1799 | cursor.close() |
||
| 1800 | cnx.close() |
||
| 1801 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1802 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1803 | |||
| 1804 | query = (" SELECT id, name, uuid " |
||
| 1805 | " FROM tbl_energy_categories ") |
||
| 1806 | cursor.execute(query) |
||
| 1807 | rows_energy_categories = cursor.fetchall() |
||
| 1808 | |||
| 1809 | energy_category_dict = dict() |
||
| 1810 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1811 | for row in rows_energy_categories: |
||
| 1812 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 1813 | "name": row['name'], |
||
| 1814 | "uuid": row['uuid']} |
||
| 1815 | |||
| 1816 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
||
| 1817 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
||
| 1818 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
||
| 1819 | " ORDER BY m.id ") |
||
| 1820 | cursor.execute(query, (id_,)) |
||
| 1821 | rows = cursor.fetchall() |
||
| 1822 | |||
| 1823 | result = list() |
||
| 1824 | if rows is not None and len(rows) > 0: |
||
| 1825 | for row in rows: |
||
| 1826 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 1827 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 1828 | "energy_category": energy_category, |
||
| 1829 | "is_output": bool(row['is_output'])} |
||
| 1830 | result.append(meta_result) |
||
| 1831 | |||
| 1832 | resp.text = json.dumps(result) |
||
| 1833 | |||
| 1834 | @staticmethod |
||
| 1835 | @user_logger |
||
| 1836 | def on_post(req, resp, id_): |
||
| 1837 | """Handles POST requests""" |
||
| 1838 | access_control(req) |
||
| 1839 | try: |
||
| 1840 | raw_json = req.stream.read().decode('utf-8') |
||
| 1841 | except Exception as ex: |
||
| 1842 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1843 | |||
| 1844 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1845 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1846 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1847 | |||
| 1848 | new_values = json.loads(raw_json) |
||
| 1849 | |||
| 1850 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 1851 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 1852 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 1853 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1854 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 1855 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 1856 | |||
| 1857 | if 'is_output' not in new_values['data'].keys() or \ |
||
| 1858 | not isinstance(new_values['data']['is_output'], bool): |
||
| 1859 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1860 | description='API.INVALID_IS_OUTPUT_VALUE') |
||
| 1861 | is_output = new_values['data']['is_output'] |
||
| 1862 | |||
| 1863 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1864 | cursor = cnx.cursor() |
||
| 1865 | |||
| 1866 | cursor.execute(" SELECT name " |
||
| 1867 | " from tbl_combined_equipments " |
||
| 1868 | " WHERE id = %s ", (id_,)) |
||
| 1869 | if cursor.fetchone() is None: |
||
| 1870 | cursor.close() |
||
| 1871 | cnx.close() |
||
| 1872 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1873 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1874 | |||
| 1875 | cursor.execute(" SELECT name " |
||
| 1876 | " FROM tbl_virtual_meters " |
||
| 1877 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 1878 | if cursor.fetchone() is None: |
||
| 1879 | cursor.close() |
||
| 1880 | cnx.close() |
||
| 1881 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1882 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 1883 | |||
| 1884 | query = (" SELECT id " |
||
| 1885 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 1886 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
||
| 1887 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 1888 | if cursor.fetchone() is not None: |
||
| 1889 | cursor.close() |
||
| 1890 | cnx.close() |
||
| 1891 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1892 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
||
| 1893 | |||
| 1894 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
||
| 1895 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
||
| 1896 | " VALUES (%s, %s, %s) ") |
||
| 1897 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
||
| 1898 | new_id = cursor.lastrowid |
||
| 1899 | cnx.commit() |
||
| 1900 | cursor.close() |
||
| 1901 | cnx.close() |
||
| 1902 | |||
| 1903 | resp.status = falcon.HTTP_201 |
||
| 1904 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 1905 | |||
| 1906 | |||
| 1907 | View Code Duplication | class CombinedEquipmentVirtualMeterItem: |
|
| 1908 | @staticmethod |
||
| 1909 | def __init__(): |
||
| 1910 | """"Initializes CombinedEquipmentVirtualMeterItem""" |
||
| 1911 | pass |
||
| 1912 | |||
| 1913 | @staticmethod |
||
| 1914 | def on_options(req, resp, id_, mid): |
||
| 1915 | resp.status = falcon.HTTP_200 |
||
| 1916 | |||
| 1917 | @staticmethod |
||
| 1918 | @user_logger |
||
| 1919 | def on_delete(req, resp, id_, mid): |
||
| 1920 | access_control(req) |
||
| 1921 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1922 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1923 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 1924 | |||
| 1925 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1926 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1927 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 1928 | |||
| 1929 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1930 | cursor = cnx.cursor() |
||
| 1931 | |||
| 1932 | cursor.execute(" SELECT name " |
||
| 1933 | " FROM tbl_combined_equipments " |
||
| 1934 | " WHERE id = %s ", (id_,)) |
||
| 1935 | if cursor.fetchone() is None: |
||
| 1936 | cursor.close() |
||
| 1937 | cnx.close() |
||
| 1938 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1939 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 1940 | |||
| 1941 | cursor.execute(" SELECT name " |
||
| 1942 | " FROM tbl_virtual_meters " |
||
| 1943 | " WHERE id = %s ", (mid,)) |
||
| 1944 | if cursor.fetchone() is None: |
||
| 1945 | cursor.close() |
||
| 1946 | cnx.close() |
||
| 1947 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1948 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 1949 | |||
| 1950 | cursor.execute(" SELECT id " |
||
| 1951 | " FROM tbl_combined_equipments_virtual_meters " |
||
| 1952 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 1953 | if cursor.fetchone() is None: |
||
| 1954 | cursor.close() |
||
| 1955 | cnx.close() |
||
| 1956 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1957 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 1958 | |||
| 1959 | cursor.execute(" DELETE FROM tbl_combined_equipments_virtual_meters " |
||
| 1960 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 1961 | cnx.commit() |
||
| 1962 | |||
| 1963 | cursor.close() |
||
| 1964 | cnx.close() |
||
| 1965 | |||
| 1966 | resp.status = falcon.HTTP_204 |
||
| 1967 |