myems /
myems-api
| 1 | import falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | from datetime import datetime |
||
| 7 | from anytree import AnyNode |
||
| 8 | from anytree.exporter import JsonExporter |
||
| 9 | |||
| 10 | |||
| 11 | class SpaceCollection: |
||
| 12 | @staticmethod |
||
| 13 | def __init__(): |
||
| 14 | pass |
||
| 15 | |||
| 16 | @staticmethod |
||
| 17 | def on_options(req, resp): |
||
| 18 | resp.status = falcon.HTTP_200 |
||
| 19 | |||
| 20 | @staticmethod |
||
| 21 | def on_get(req, resp): |
||
| 22 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 23 | cursor = cnx.cursor(dictionary=True) |
||
| 24 | |||
| 25 | query = (" SELECT id, name, uuid " |
||
| 26 | " FROM tbl_spaces ") |
||
| 27 | cursor.execute(query) |
||
| 28 | rows_spaces = cursor.fetchall() |
||
| 29 | |||
| 30 | space_dict = dict() |
||
| 31 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 32 | for row in rows_spaces: |
||
| 33 | space_dict[row['id']] = {"id": row['id'], |
||
| 34 | "name": row['name'], |
||
| 35 | "uuid": row['uuid']} |
||
| 36 | |||
| 37 | query = (" SELECT id, name, utc_offset " |
||
| 38 | " FROM tbl_timezones ") |
||
| 39 | cursor.execute(query) |
||
| 40 | rows_timezones = cursor.fetchall() |
||
| 41 | |||
| 42 | timezone_dict = dict() |
||
| 43 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 44 | for row in rows_timezones: |
||
| 45 | timezone_dict[row['id']] = {"id": row['id'], |
||
| 46 | "name": row['name'], |
||
| 47 | "utc_offset": row['utc_offset']} |
||
| 48 | |||
| 49 | query = (" SELECT id, name, uuid " |
||
| 50 | " FROM tbl_contacts ") |
||
| 51 | cursor.execute(query) |
||
| 52 | rows_contacts = cursor.fetchall() |
||
| 53 | |||
| 54 | contact_dict = dict() |
||
| 55 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 56 | for row in rows_contacts: |
||
| 57 | contact_dict[row['id']] = {"id": row['id'], |
||
| 58 | "name": row['name'], |
||
| 59 | "uuid": row['uuid']} |
||
| 60 | |||
| 61 | query = (" SELECT id, name, uuid " |
||
| 62 | " FROM tbl_cost_centers ") |
||
| 63 | cursor.execute(query) |
||
| 64 | rows_cost_centers = cursor.fetchall() |
||
| 65 | |||
| 66 | cost_center_dict = dict() |
||
| 67 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 68 | for row in rows_cost_centers: |
||
| 69 | cost_center_dict[row['id']] = {"id": row['id'], |
||
| 70 | "name": row['name'], |
||
| 71 | "uuid": row['uuid']} |
||
| 72 | |||
| 73 | query = (" SELECT id, name, uuid, " |
||
| 74 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 75 | " contact_id, cost_center_id, description " |
||
| 76 | " FROM tbl_spaces " |
||
| 77 | " ORDER BY id ") |
||
| 78 | cursor.execute(query) |
||
| 79 | rows_spaces = cursor.fetchall() |
||
| 80 | |||
| 81 | result = list() |
||
| 82 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 83 | for row in rows_spaces: |
||
| 84 | timezone = timezone_dict.get(row['timezone_id'], None) |
||
| 85 | contact = contact_dict.get(row['contact_id'], None) |
||
| 86 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
| 87 | parent_space = space_dict.get(row['parent_space_id'], None) |
||
| 88 | meta_result = {"id": row['id'], |
||
| 89 | "name": row['name'], |
||
| 90 | "uuid": row['uuid'], |
||
| 91 | "parent_space": parent_space, |
||
| 92 | "area": row['area'], |
||
| 93 | "timezone": timezone, |
||
| 94 | "is_input_counted": bool(row['is_input_counted']), |
||
| 95 | "is_output_counted": bool(row['is_output_counted']), |
||
| 96 | "contact": contact, |
||
| 97 | "cost_center": cost_center, |
||
| 98 | "description": row['description']} |
||
| 99 | result.append(meta_result) |
||
| 100 | |||
| 101 | cursor.close() |
||
| 102 | cnx.disconnect() |
||
| 103 | resp.body = json.dumps(result) |
||
| 104 | |||
| 105 | @staticmethod |
||
| 106 | def on_post(req, resp): |
||
| 107 | """Handles POST requests""" |
||
| 108 | try: |
||
| 109 | raw_json = req.stream.read().decode('utf-8') |
||
| 110 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 111 | except Exception as ex: |
||
| 112 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 113 | |||
| 114 | if 'name' not in new_values['data'].keys() or \ |
||
| 115 | not isinstance(new_values['data']['name'], str) or \ |
||
| 116 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 117 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 118 | description='API.INVALID_SPACE_NAME') |
||
| 119 | name = str.strip(new_values['data']['name']) |
||
| 120 | |||
| 121 | if 'parent_space_id' in new_values['data'].keys(): |
||
| 122 | if new_values['data']['parent_space_id'] <= 0: |
||
| 123 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 124 | description='API.INVALID_PARENT_SPACE_ID') |
||
| 125 | parent_space_id = new_values['data']['parent_space_id'] |
||
| 126 | else: |
||
| 127 | parent_space_id = None |
||
| 128 | |||
| 129 | if 'area' not in new_values['data'].keys() or \ |
||
| 130 | not (isinstance(new_values['data']['area'], float) or |
||
| 131 | isinstance(new_values['data']['area'], int)): |
||
| 132 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 133 | description='API.INVALID_AREA_VALUE') |
||
| 134 | area = new_values['data']['area'] |
||
| 135 | |||
| 136 | if 'timezone_id' not in new_values['data'].keys() or \ |
||
| 137 | not isinstance(new_values['data']['timezone_id'], int) or \ |
||
| 138 | new_values['data']['timezone_id'] <= 0: |
||
| 139 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 140 | description='API.INVALID_TIMEZONE_ID') |
||
| 141 | timezone_id = new_values['data']['timezone_id'] |
||
| 142 | |||
| 143 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 144 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 145 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 146 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 147 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 148 | |||
| 149 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 150 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 151 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 152 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 153 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 154 | |||
| 155 | if 'contact_id' in new_values['data'].keys(): |
||
| 156 | if new_values['data']['contact_id'] <= 0: |
||
| 157 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 158 | description='API.INVALID_CONTACT_ID') |
||
| 159 | contact_id = new_values['data']['contact_id'] |
||
| 160 | else: |
||
| 161 | contact_id = None |
||
| 162 | |||
| 163 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 164 | if new_values['data']['cost_center_id'] <= 0: |
||
| 165 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 166 | description='API.INVALID_COST_CENTER_ID') |
||
| 167 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 168 | else: |
||
| 169 | cost_center_id = None |
||
| 170 | |||
| 171 | if 'description' in new_values['data'].keys() and \ |
||
| 172 | new_values['data']['description'] is not None and \ |
||
| 173 | len(str(new_values['data']['description'])) > 0: |
||
| 174 | description = str.strip(new_values['data']['description']) |
||
| 175 | else: |
||
| 176 | description = None |
||
| 177 | |||
| 178 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 179 | cursor = cnx.cursor() |
||
| 180 | |||
| 181 | cursor.execute(" SELECT name " |
||
| 182 | " FROM tbl_spaces " |
||
| 183 | " WHERE name = %s ", (name,)) |
||
| 184 | if cursor.fetchone() is not None: |
||
| 185 | cursor.close() |
||
| 186 | cnx.disconnect() |
||
| 187 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 188 | description='API.SPACE_NAME_IS_ALREADY_IN_USE') |
||
| 189 | |||
| 190 | View Code Duplication | if parent_space_id is not None: |
|
|
0 ignored issues
–
show
|
|||
| 191 | cursor.execute(" SELECT name " |
||
| 192 | " FROM tbl_spaces " |
||
| 193 | " WHERE id = %s ", |
||
| 194 | (new_values['data']['parent_space_id'],)) |
||
| 195 | row = cursor.fetchone() |
||
| 196 | if row is None: |
||
| 197 | cursor.close() |
||
| 198 | cnx.disconnect() |
||
| 199 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 200 | description='API.PARENT_SPACE_NOT_FOUND') |
||
| 201 | |||
| 202 | cursor.execute(" SELECT name " |
||
| 203 | " FROM tbl_timezones " |
||
| 204 | " WHERE id = %s ", |
||
| 205 | (new_values['data']['timezone_id'],)) |
||
| 206 | if cursor.fetchone() is None: |
||
| 207 | cursor.close() |
||
| 208 | cnx.disconnect() |
||
| 209 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 210 | description='API.TIMEZONE_NOT_FOUND') |
||
| 211 | View Code Duplication | if contact_id is not None: |
|
|
0 ignored issues
–
show
|
|||
| 212 | cursor.execute(" SELECT name " |
||
| 213 | " FROM tbl_contacts " |
||
| 214 | " WHERE id = %s ", |
||
| 215 | (new_values['data']['contact_id'],)) |
||
| 216 | row = cursor.fetchone() |
||
| 217 | if row is None: |
||
| 218 | cursor.close() |
||
| 219 | cnx.disconnect() |
||
| 220 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 221 | description='API.CONTACT_NOT_FOUND') |
||
| 222 | |||
| 223 | if cost_center_id is not None: |
||
| 224 | cursor.execute(" SELECT name " |
||
| 225 | " FROM tbl_cost_centers " |
||
| 226 | " WHERE id = %s ", |
||
| 227 | (new_values['data']['cost_center_id'],)) |
||
| 228 | row = cursor.fetchone() |
||
| 229 | if row is None: |
||
| 230 | cursor.close() |
||
| 231 | cnx.disconnect() |
||
| 232 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 233 | description='API.COST_CENTER_NOT_FOUND') |
||
| 234 | |||
| 235 | add_values = (" INSERT INTO tbl_spaces " |
||
| 236 | " (name, uuid, parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 237 | " contact_id, cost_center_id, description) " |
||
| 238 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 239 | cursor.execute(add_values, (name, |
||
| 240 | str(uuid.uuid4()), |
||
| 241 | parent_space_id, |
||
| 242 | area, |
||
| 243 | timezone_id, |
||
| 244 | is_input_counted, |
||
| 245 | is_output_counted, |
||
| 246 | contact_id, |
||
| 247 | cost_center_id, |
||
| 248 | description)) |
||
| 249 | new_id = cursor.lastrowid |
||
| 250 | cnx.commit() |
||
| 251 | cursor.close() |
||
| 252 | cnx.disconnect() |
||
| 253 | |||
| 254 | resp.status = falcon.HTTP_201 |
||
| 255 | resp.location = '/spaces/' + str(new_id) |
||
| 256 | |||
| 257 | |||
| 258 | class SpaceItem: |
||
| 259 | @staticmethod |
||
| 260 | def __init__(): |
||
| 261 | pass |
||
| 262 | |||
| 263 | @staticmethod |
||
| 264 | def on_options(req, resp, id_): |
||
| 265 | resp.status = falcon.HTTP_200 |
||
| 266 | |||
| 267 | @staticmethod |
||
| 268 | def on_get(req, resp, id_): |
||
| 269 | if not id_.isdigit() or int(id_) <= 0: |
||
| 270 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 271 | description='API.INVALID_METER_ID') |
||
| 272 | |||
| 273 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 274 | cursor = cnx.cursor(dictionary=True) |
||
| 275 | |||
| 276 | query = (" SELECT id, name, uuid " |
||
| 277 | " FROM tbl_spaces ") |
||
| 278 | cursor.execute(query) |
||
| 279 | rows_spaces = cursor.fetchall() |
||
| 280 | |||
| 281 | space_dict = dict() |
||
| 282 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 283 | for row in rows_spaces: |
||
| 284 | space_dict[row['id']] = {"id": row['id'], |
||
| 285 | "name": row['name'], |
||
| 286 | "uuid": row['uuid']} |
||
| 287 | |||
| 288 | query = (" SELECT id, name, utc_offset " |
||
| 289 | " FROM tbl_timezones ") |
||
| 290 | cursor.execute(query) |
||
| 291 | rows_timezones = cursor.fetchall() |
||
| 292 | |||
| 293 | timezone_dict = dict() |
||
| 294 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 295 | for row in rows_timezones: |
||
| 296 | timezone_dict[row['id']] = {"id": row['id'], |
||
| 297 | "name": row['name'], |
||
| 298 | "utc_offset": row['utc_offset']} |
||
| 299 | |||
| 300 | query = (" SELECT id, name, uuid " |
||
| 301 | " FROM tbl_contacts ") |
||
| 302 | cursor.execute(query) |
||
| 303 | rows_contacts = cursor.fetchall() |
||
| 304 | |||
| 305 | contact_dict = dict() |
||
| 306 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 307 | for row in rows_contacts: |
||
| 308 | contact_dict[row['id']] = {"id": row['id'], |
||
| 309 | "name": row['name'], |
||
| 310 | "uuid": row['uuid']} |
||
| 311 | |||
| 312 | query = (" SELECT id, name, uuid " |
||
| 313 | " FROM tbl_cost_centers ") |
||
| 314 | cursor.execute(query) |
||
| 315 | rows_cost_centers = cursor.fetchall() |
||
| 316 | |||
| 317 | cost_center_dict = dict() |
||
| 318 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 319 | for row in rows_cost_centers: |
||
| 320 | cost_center_dict[row['id']] = {"id": row['id'], |
||
| 321 | "name": row['name'], |
||
| 322 | "uuid": row['uuid']} |
||
| 323 | |||
| 324 | query = (" SELECT id, name, uuid, " |
||
| 325 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 326 | " contact_id, cost_center_id, description " |
||
| 327 | " FROM tbl_spaces " |
||
| 328 | " WHERE id = %s ") |
||
| 329 | cursor.execute(query, (id_,)) |
||
| 330 | row = cursor.fetchone() |
||
| 331 | cursor.close() |
||
| 332 | cnx.disconnect() |
||
| 333 | |||
| 334 | if row is None: |
||
| 335 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 336 | description='API.SPACE_NOT_FOUND') |
||
| 337 | else: |
||
| 338 | parent_space = space_dict.get(row['parent_space_id'], None) |
||
| 339 | timezone = timezone_dict.get(row['timezone_id'], None) |
||
| 340 | contact = contact_dict.get(row['contact_id'], None) |
||
| 341 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
| 342 | meta_result = {"id": row['id'], |
||
| 343 | "name": row['name'], |
||
| 344 | "uuid": row['uuid'], |
||
| 345 | "parent_space_id": parent_space, |
||
| 346 | "area": row['area'], |
||
| 347 | "timezone": timezone, |
||
| 348 | "is_input_counted": bool(row['is_input_counted']), |
||
| 349 | "is_output_counted": bool(row['is_output_counted']), |
||
| 350 | "contact": contact, |
||
| 351 | "cost_center": cost_center, |
||
| 352 | "description": row['description']} |
||
| 353 | |||
| 354 | resp.body = json.dumps(meta_result) |
||
| 355 | |||
| 356 | @staticmethod |
||
| 357 | def on_delete(req, resp, id_): |
||
| 358 | if not id_.isdigit() or int(id_) <= 0: |
||
| 359 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 360 | description='API.INVALID_SPACE_ID') |
||
| 361 | if int(id_) == 1: |
||
| 362 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 363 | description='API.THIS_SPACE_CAN_NOT_BE_DELETED') |
||
| 364 | |||
| 365 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 366 | cursor = cnx.cursor() |
||
| 367 | |||
| 368 | cursor.execute(" SELECT name " |
||
| 369 | " FROM tbl_spaces " |
||
| 370 | " WHERE id = %s ", (id_,)) |
||
| 371 | if cursor.fetchone() is None: |
||
| 372 | cursor.close() |
||
| 373 | cnx.disconnect() |
||
| 374 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 375 | description='API.SPACE_NOT_FOUND') |
||
| 376 | |||
| 377 | # checkout relation with children spaces |
||
| 378 | cursor.execute(" SELECT id " |
||
| 379 | " FROM tbl_spaces " |
||
| 380 | " WHERE parent_space_id = %s ", |
||
| 381 | (id_,)) |
||
| 382 | rows_spaces = cursor.fetchall() |
||
| 383 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 384 | cursor.close() |
||
| 385 | cnx.disconnect() |
||
| 386 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 387 | title='API.BAD_REQUEST', |
||
| 388 | description='API.THERE_IS_RELATION_WITH_CHILDREN_SPACES') |
||
| 389 | |||
| 390 | # check relation with equipment |
||
| 391 | cursor.execute(" SELECT equipment_id " |
||
| 392 | " FROM tbl_spaces_equipments " |
||
| 393 | " WHERE space_id = %s ", |
||
| 394 | (id_,)) |
||
| 395 | rows_equipments = cursor.fetchall() |
||
| 396 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
| 397 | cursor.close() |
||
| 398 | cnx.disconnect() |
||
| 399 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 400 | title='API.BAD_REQUEST', |
||
| 401 | description='API.THERE_IS_RELATION_WITH_EQUIPMENTS') |
||
| 402 | |||
| 403 | # check relation with combined equipment |
||
| 404 | cursor.execute(" SELECT combined_equipment_id " |
||
| 405 | " FROM tbl_spaces_combined_equipments " |
||
| 406 | " WHERE space_id = %s ", |
||
| 407 | (id_,)) |
||
| 408 | rows_combined_equipments = cursor.fetchall() |
||
| 409 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 410 | cursor.close() |
||
| 411 | cnx.disconnect() |
||
| 412 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 413 | title='API.BAD_REQUEST', |
||
| 414 | description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
||
| 415 | |||
| 416 | # check relation with meter |
||
| 417 | cursor.execute(" SELECT meter_id " |
||
| 418 | " FROM tbl_spaces_meters " |
||
| 419 | " WHERE space_id = %s ", |
||
| 420 | (id_,)) |
||
| 421 | rows_meters = cursor.fetchall() |
||
| 422 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 423 | cursor.close() |
||
| 424 | cnx.disconnect() |
||
| 425 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 426 | title='API.BAD_REQUEST', |
||
| 427 | description='API.THERE_IS_RELATION_WITH_METERS') |
||
| 428 | |||
| 429 | # check relation with offline meter |
||
| 430 | cursor.execute(" SELECT offline_meter_id " |
||
| 431 | " FROM tbl_spaces_offline_meters " |
||
| 432 | " WHERE space_id = %s ", |
||
| 433 | (id_,)) |
||
| 434 | rows_offline_meters = cursor.fetchall() |
||
| 435 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 436 | cursor.close() |
||
| 437 | cnx.disconnect() |
||
| 438 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 439 | title='API.BAD_REQUEST', |
||
| 440 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') |
||
| 441 | |||
| 442 | # check relation with points |
||
| 443 | cursor.execute(" SELECT point_id " |
||
| 444 | " FROM tbl_spaces_points " |
||
| 445 | " WHERE space_id = %s ", (id_,)) |
||
| 446 | rows_points = cursor.fetchall() |
||
| 447 | if rows_points is not None and len(rows_points) > 0: |
||
| 448 | cursor.close() |
||
| 449 | cnx.disconnect() |
||
| 450 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 451 | title='API.BAD_REQUEST', |
||
| 452 | description='API.THERE_IS_RELATION_WITH_POINTS') |
||
| 453 | |||
| 454 | # check relation with sensor |
||
| 455 | cursor.execute(" SELECT sensor_id " |
||
| 456 | " FROM tbl_spaces_sensors " |
||
| 457 | " WHERE space_id = %s ", |
||
| 458 | (id_,)) |
||
| 459 | rows_sensors = cursor.fetchall() |
||
| 460 | if rows_sensors is not None and len(rows_sensors) > 0: |
||
| 461 | cursor.close() |
||
| 462 | cnx.disconnect() |
||
| 463 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 464 | title='API.BAD_REQUEST', |
||
| 465 | description='API.THERE_IS_RELATION_WITH_SENSORS') |
||
| 466 | |||
| 467 | # check relation with store |
||
| 468 | cursor.execute(" SELECT id " |
||
| 469 | " FROM tbl_spaces_stores " |
||
| 470 | " WHERE space_id = %s ", (id_,)) |
||
| 471 | rows_stores = cursor.fetchall() |
||
| 472 | if rows_stores is not None and len(rows_stores) > 0: |
||
| 473 | cursor.close() |
||
| 474 | cnx.disconnect() |
||
| 475 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 476 | title='API.BAD_REQUEST', |
||
| 477 | description='API.THERE_IS_RELATION_WITH_STORES') |
||
| 478 | |||
| 479 | # check relation with tenant |
||
| 480 | cursor.execute(" SELECT id " |
||
| 481 | " FROM tbl_spaces_tenants " |
||
| 482 | " WHERE space_id = %s ", (id_,)) |
||
| 483 | rows_tenants = cursor.fetchall() |
||
| 484 | if rows_tenants is not None and len(rows_tenants) > 0: |
||
| 485 | cursor.close() |
||
| 486 | cnx.disconnect() |
||
| 487 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 488 | title='API.BAD_REQUEST', |
||
| 489 | description='API.THERE_IS_RELATION_WITH_TENANTS') |
||
| 490 | |||
| 491 | # check relation with virtual meter |
||
| 492 | cursor.execute(" SELECT virtual_meter_id " |
||
| 493 | " FROM tbl_spaces_virtual_meters " |
||
| 494 | " WHERE space_id = %s ", |
||
| 495 | (id_,)) |
||
| 496 | rows_virtual_meters = cursor.fetchall() |
||
| 497 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 498 | cursor.close() |
||
| 499 | cnx.disconnect() |
||
| 500 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 501 | title='API.BAD_REQUEST', |
||
| 502 | description='API.THERE_IS_RELATION_WITH_VIRTUAL_METERS') |
||
| 503 | |||
| 504 | cursor.execute(" DELETE FROM tbl_spaces WHERE id = %s ", (id_,)) |
||
| 505 | cnx.commit() |
||
| 506 | |||
| 507 | cursor.close() |
||
| 508 | cnx.disconnect() |
||
| 509 | |||
| 510 | resp.status = falcon.HTTP_204 |
||
| 511 | |||
| 512 | @staticmethod |
||
| 513 | def on_put(req, resp, id_): |
||
| 514 | """Handles PUT requests""" |
||
| 515 | try: |
||
| 516 | raw_json = req.stream.read().decode('utf-8') |
||
| 517 | except Exception as ex: |
||
| 518 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 519 | |||
| 520 | if not id_.isdigit() or int(id_) <= 0: |
||
| 521 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 522 | description='API.INVALID_SPACE_ID') |
||
| 523 | |||
| 524 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 525 | |||
| 526 | if 'name' not in new_values['data'].keys() or \ |
||
| 527 | not isinstance(new_values['data']['name'], str) or \ |
||
| 528 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 529 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 530 | description='API.INVALID_SPACE_NAME') |
||
| 531 | name = str.strip(new_values['data']['name']) |
||
| 532 | |||
| 533 | if int(id_) == 1: |
||
| 534 | parent_space_id = None |
||
| 535 | else: |
||
| 536 | if 'parent_space_id' not in new_values['data'].keys() or \ |
||
| 537 | new_values['data']['parent_space_id'] is None or \ |
||
| 538 | not isinstance(new_values['data']['parent_space_id'], int) or \ |
||
| 539 | int(new_values['data']['parent_space_id']) <= 0: |
||
| 540 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 541 | description='API.INVALID_TIMEZONE_ID') |
||
| 542 | parent_space_id = int(new_values['data']['parent_space_id']) |
||
| 543 | |||
| 544 | if 'area' not in new_values['data'].keys() or \ |
||
| 545 | not (isinstance(new_values['data']['area'], float) or |
||
| 546 | isinstance(new_values['data']['area'], int)): |
||
| 547 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 548 | description='API.INVALID_AREA_VALUE') |
||
| 549 | area = new_values['data']['area'] |
||
| 550 | |||
| 551 | if 'timezone_id' not in new_values['data'].keys() or \ |
||
| 552 | not isinstance(new_values['data']['timezone_id'], int) or \ |
||
| 553 | new_values['data']['timezone_id'] <= 0: |
||
| 554 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 555 | description='API.INVALID_TIMEZONE_ID') |
||
| 556 | timezone_id = new_values['data']['timezone_id'] |
||
| 557 | |||
| 558 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 559 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 560 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 561 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 562 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 563 | |||
| 564 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 565 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 566 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 567 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 568 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 569 | |||
| 570 | if 'contact_id' in new_values['data'].keys(): |
||
| 571 | if new_values['data']['contact_id'] <= 0: |
||
| 572 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 573 | description='API.INVALID_CONTACT_ID') |
||
| 574 | contact_id = new_values['data']['contact_id'] |
||
| 575 | else: |
||
| 576 | contact_id = None |
||
| 577 | |||
| 578 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 579 | if new_values['data']['cost_center_id'] <= 0: |
||
| 580 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 581 | description='API.INVALID_COST_CENTER_ID') |
||
| 582 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 583 | else: |
||
| 584 | cost_center_id = None |
||
| 585 | |||
| 586 | if 'description' in new_values['data'].keys() and \ |
||
| 587 | new_values['data']['description'] is not None and \ |
||
| 588 | len(str(new_values['data']['description'])) > 0: |
||
| 589 | description = str.strip(new_values['data']['description']) |
||
| 590 | else: |
||
| 591 | description = None |
||
| 592 | |||
| 593 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 594 | cursor = cnx.cursor() |
||
| 595 | |||
| 596 | cursor.execute(" SELECT name " |
||
| 597 | " FROM tbl_spaces " |
||
| 598 | " WHERE id = %s ", (id_,)) |
||
| 599 | if cursor.fetchone() is None: |
||
| 600 | cursor.close() |
||
| 601 | cnx.disconnect() |
||
| 602 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 603 | description='API.SPACE_NOT_FOUND') |
||
| 604 | |||
| 605 | cursor.execute(" SELECT name " |
||
| 606 | " FROM tbl_spaces " |
||
| 607 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 608 | if cursor.fetchone() is not None: |
||
| 609 | cursor.close() |
||
| 610 | cnx.disconnect() |
||
| 611 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 612 | description='API.SPACE_NAME_IS_ALREADY_IN_USE') |
||
| 613 | |||
| 614 | View Code Duplication | if parent_space_id is not None: |
|
|
0 ignored issues
–
show
|
|||
| 615 | cursor.execute(" SELECT name " |
||
| 616 | " FROM tbl_spaces " |
||
| 617 | " WHERE id = %s ", |
||
| 618 | (new_values['data']['parent_space_id'],)) |
||
| 619 | row = cursor.fetchone() |
||
| 620 | if row is None: |
||
| 621 | cursor.close() |
||
| 622 | cnx.disconnect() |
||
| 623 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 624 | description='API.PARENT_SPACE_NOT_FOUND') |
||
| 625 | |||
| 626 | cursor.execute(" SELECT name " |
||
| 627 | " FROM tbl_timezones " |
||
| 628 | " WHERE id = %s ", |
||
| 629 | (new_values['data']['timezone_id'],)) |
||
| 630 | if cursor.fetchone() is None: |
||
| 631 | cursor.close() |
||
| 632 | cnx.disconnect() |
||
| 633 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 634 | description='API.TIMEZONE_NOT_FOUND') |
||
| 635 | View Code Duplication | if contact_id is not None: |
|
|
0 ignored issues
–
show
|
|||
| 636 | cursor.execute(" SELECT name " |
||
| 637 | " FROM tbl_contacts " |
||
| 638 | " WHERE id = %s ", |
||
| 639 | (new_values['data']['contact_id'],)) |
||
| 640 | row = cursor.fetchone() |
||
| 641 | if row is None: |
||
| 642 | cursor.close() |
||
| 643 | cnx.disconnect() |
||
| 644 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 645 | description='API.CONTACT_NOT_FOUND') |
||
| 646 | |||
| 647 | if cost_center_id is not None: |
||
| 648 | cursor.execute(" SELECT name " |
||
| 649 | " FROM tbl_cost_centers " |
||
| 650 | " WHERE id = %s ", |
||
| 651 | (new_values['data']['cost_center_id'],)) |
||
| 652 | row = cursor.fetchone() |
||
| 653 | if row is None: |
||
| 654 | cursor.close() |
||
| 655 | cnx.disconnect() |
||
| 656 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 657 | description='API.COST_CENTER_NOT_FOUND') |
||
| 658 | |||
| 659 | update_row = (" UPDATE tbl_spaces " |
||
| 660 | " SET name = %s, parent_space_id = %s, area = %s, timezone_id = %s, " |
||
| 661 | " is_input_counted = %s, is_output_counted = %s, contact_id = %s, cost_center_id = %s, " |
||
| 662 | " description = %s " |
||
| 663 | " WHERE id = %s ") |
||
| 664 | cursor.execute(update_row, (name, |
||
| 665 | parent_space_id, |
||
| 666 | area, |
||
| 667 | timezone_id, |
||
| 668 | is_input_counted, |
||
| 669 | is_output_counted, |
||
| 670 | contact_id, |
||
| 671 | cost_center_id, |
||
| 672 | description, |
||
| 673 | id_)) |
||
| 674 | cnx.commit() |
||
| 675 | |||
| 676 | cursor.close() |
||
| 677 | cnx.disconnect() |
||
| 678 | |||
| 679 | resp.status = falcon.HTTP_200 |
||
| 680 | |||
| 681 | |||
| 682 | class SpaceChildrenCollection: |
||
| 683 | @staticmethod |
||
| 684 | def __init__(): |
||
| 685 | pass |
||
| 686 | |||
| 687 | @staticmethod |
||
| 688 | def on_options(req, resp, id_): |
||
| 689 | resp.status = falcon.HTTP_200 |
||
| 690 | |||
| 691 | @staticmethod |
||
| 692 | def on_get(req, resp, id_): |
||
| 693 | if not id_.isdigit() or int(id_) <= 0: |
||
| 694 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 695 | description='API.INVALID_SPACE_ID') |
||
| 696 | |||
| 697 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 698 | cursor = cnx.cursor(dictionary=True) |
||
| 699 | |||
| 700 | cursor.execute(" SELECT name " |
||
| 701 | " FROM tbl_spaces " |
||
| 702 | " WHERE id = %s ", (id_,)) |
||
| 703 | if cursor.fetchone() is None: |
||
| 704 | cursor.close() |
||
| 705 | cnx.disconnect() |
||
| 706 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 707 | description='API.SPACE_NOT_FOUND') |
||
| 708 | |||
| 709 | query = (" SELECT id, name, uuid " |
||
| 710 | " FROM tbl_spaces ") |
||
| 711 | cursor.execute(query) |
||
| 712 | rows_spaces = cursor.fetchall() |
||
| 713 | |||
| 714 | space_dict = dict() |
||
| 715 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 716 | for row in rows_spaces: |
||
| 717 | space_dict[row['id']] = {"id": row['id'], |
||
| 718 | "name": row['name'], |
||
| 719 | "uuid": row['uuid']} |
||
| 720 | |||
| 721 | query = (" SELECT id, name, utc_offset " |
||
| 722 | " FROM tbl_timezones ") |
||
| 723 | cursor.execute(query) |
||
| 724 | rows_timezones = cursor.fetchall() |
||
| 725 | |||
| 726 | timezone_dict = dict() |
||
| 727 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 728 | for row in rows_timezones: |
||
| 729 | timezone_dict[row['id']] = {"id": row['id'], |
||
| 730 | "name": row['name'], |
||
| 731 | "utc_offset": row['utc_offset']} |
||
| 732 | |||
| 733 | query = (" SELECT id, name, uuid " |
||
| 734 | " FROM tbl_contacts ") |
||
| 735 | cursor.execute(query) |
||
| 736 | rows_contacts = cursor.fetchall() |
||
| 737 | |||
| 738 | contact_dict = dict() |
||
| 739 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 740 | for row in rows_contacts: |
||
| 741 | contact_dict[row['id']] = {"id": row['id'], |
||
| 742 | "name": row['name'], |
||
| 743 | "uuid": row['uuid']} |
||
| 744 | |||
| 745 | query = (" SELECT id, name, uuid " |
||
| 746 | " FROM tbl_cost_centers ") |
||
| 747 | cursor.execute(query) |
||
| 748 | rows_cost_centers = cursor.fetchall() |
||
| 749 | |||
| 750 | cost_center_dict = dict() |
||
| 751 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 752 | for row in rows_cost_centers: |
||
| 753 | cost_center_dict[row['id']] = {"id": row['id'], |
||
| 754 | "name": row['name'], |
||
| 755 | "uuid": row['uuid']} |
||
| 756 | |||
| 757 | query = (" SELECT id, name, uuid, " |
||
| 758 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 759 | " contact_id, cost_center_id, description " |
||
| 760 | " FROM tbl_spaces " |
||
| 761 | " WHERE parent_space_id = %s " |
||
| 762 | " ORDER BY id ") |
||
| 763 | cursor.execute(query, (id_, )) |
||
| 764 | rows_spaces = cursor.fetchall() |
||
| 765 | |||
| 766 | result = list() |
||
| 767 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
|
0 ignored issues
–
show
|
|||
| 768 | for row in rows_spaces: |
||
| 769 | timezone = timezone_dict.get(row['timezone_id'], None) |
||
| 770 | contact = contact_dict.get(row['contact_id'], None) |
||
| 771 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
| 772 | parent_space = space_dict.get(row['parent_space_id'], None) |
||
| 773 | meta_result = {"id": row['id'], |
||
| 774 | "name": row['name'], |
||
| 775 | "uuid": row['uuid'], |
||
| 776 | "parent_space": parent_space, |
||
| 777 | "area": row['area'], |
||
| 778 | "timezone": timezone, |
||
| 779 | "is_input_counted": bool(row['is_input_counted']), |
||
| 780 | "is_output_counted": bool(row['is_output_counted']), |
||
| 781 | "contact": contact, |
||
| 782 | "cost_center": cost_center, |
||
| 783 | "description": row['description']} |
||
| 784 | result.append(meta_result) |
||
| 785 | |||
| 786 | cursor.close() |
||
| 787 | cnx.disconnect() |
||
| 788 | resp.body = json.dumps(result) |
||
| 789 | |||
| 790 | |||
| 791 | View Code Duplication | class SpaceCombinedEquipmentCollection: |
|
|
0 ignored issues
–
show
|
|||
| 792 | @staticmethod |
||
| 793 | def __init__(): |
||
| 794 | pass |
||
| 795 | |||
| 796 | @staticmethod |
||
| 797 | def on_options(req, resp, id_): |
||
| 798 | resp.status = falcon.HTTP_200 |
||
| 799 | |||
| 800 | @staticmethod |
||
| 801 | def on_get(req, resp, id_): |
||
| 802 | if not id_.isdigit() or int(id_) <= 0: |
||
| 803 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 804 | description='API.INVALID_SPACE_ID') |
||
| 805 | |||
| 806 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 807 | cursor = cnx.cursor() |
||
| 808 | |||
| 809 | cursor.execute(" SELECT name " |
||
| 810 | " FROM tbl_spaces " |
||
| 811 | " WHERE id = %s ", (id_,)) |
||
| 812 | if cursor.fetchone() is None: |
||
| 813 | cursor.close() |
||
| 814 | cnx.disconnect() |
||
| 815 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 816 | description='API.SPACE_NOT_FOUND') |
||
| 817 | |||
| 818 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 819 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
||
| 820 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
||
| 821 | " ORDER BY e.id ") |
||
| 822 | cursor.execute(query, (id_,)) |
||
| 823 | rows = cursor.fetchall() |
||
| 824 | |||
| 825 | result = list() |
||
| 826 | if rows is not None and len(rows) > 0: |
||
| 827 | for row in rows: |
||
| 828 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 829 | result.append(meta_result) |
||
| 830 | |||
| 831 | resp.body = json.dumps(result) |
||
| 832 | |||
| 833 | @staticmethod |
||
| 834 | def on_post(req, resp, id_): |
||
| 835 | """Handles POST requests""" |
||
| 836 | try: |
||
| 837 | raw_json = req.stream.read().decode('utf-8') |
||
| 838 | except Exception as ex: |
||
| 839 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 840 | |||
| 841 | if not id_.isdigit() or int(id_) <= 0: |
||
| 842 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 843 | description='API.INVALID_SPACE_ID') |
||
| 844 | |||
| 845 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 846 | |||
| 847 | if 'combined_equipment_id' not in new_values['data'].keys() or \ |
||
| 848 | not isinstance(new_values['data']['combined_equipment_id'], int) or \ |
||
| 849 | new_values['data']['combined_equipment_id'] <= 0: |
||
| 850 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 851 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 852 | combined_equipment_id = new_values['data']['combined_equipment_id'] |
||
| 853 | |||
| 854 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 855 | cursor = cnx.cursor() |
||
| 856 | |||
| 857 | cursor.execute(" SELECT name " |
||
| 858 | " from tbl_spaces " |
||
| 859 | " WHERE id = %s ", (id_,)) |
||
| 860 | if cursor.fetchone() is None: |
||
| 861 | cursor.close() |
||
| 862 | cnx.disconnect() |
||
| 863 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 864 | description='API.SPACE_NOT_FOUND') |
||
| 865 | |||
| 866 | cursor.execute(" SELECT name " |
||
| 867 | " FROM tbl_combined_equipments " |
||
| 868 | " WHERE id = %s ", (combined_equipment_id,)) |
||
| 869 | if cursor.fetchone() is None: |
||
| 870 | cursor.close() |
||
| 871 | cnx.disconnect() |
||
| 872 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 873 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 874 | |||
| 875 | query = (" SELECT id " |
||
| 876 | " FROM tbl_spaces_combined_equipments " |
||
| 877 | " WHERE space_id = %s AND combined_equipment_id = %s") |
||
| 878 | cursor.execute(query, (id_, combined_equipment_id,)) |
||
| 879 | if cursor.fetchone() is not None: |
||
| 880 | cursor.close() |
||
| 881 | cnx.disconnect() |
||
| 882 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 883 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTED') |
||
| 884 | |||
| 885 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
||
| 886 | " VALUES (%s, %s) ") |
||
| 887 | cursor.execute(add_row, (id_, combined_equipment_id,)) |
||
| 888 | new_id = cursor.lastrowid |
||
| 889 | cnx.commit() |
||
| 890 | cursor.close() |
||
| 891 | cnx.disconnect() |
||
| 892 | |||
| 893 | resp.status = falcon.HTTP_201 |
||
| 894 | resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id) |
||
| 895 | |||
| 896 | |||
| 897 | View Code Duplication | class SpaceCombinedEquipmentItem: |
|
|
0 ignored issues
–
show
|
|||
| 898 | @staticmethod |
||
| 899 | def __init__(): |
||
| 900 | pass |
||
| 901 | |||
| 902 | @staticmethod |
||
| 903 | def on_options(req, resp, id_, eid): |
||
| 904 | resp.status = falcon.HTTP_200 |
||
| 905 | |||
| 906 | @staticmethod |
||
| 907 | def on_delete(req, resp, id_, eid): |
||
| 908 | if not id_.isdigit() or int(id_) <= 0: |
||
| 909 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 910 | description='API.INVALID_SPACE_ID') |
||
| 911 | |||
| 912 | if not eid.isdigit() or int(eid) <= 0: |
||
| 913 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 914 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 915 | |||
| 916 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 917 | cursor = cnx.cursor() |
||
| 918 | |||
| 919 | cursor.execute(" SELECT name " |
||
| 920 | " FROM tbl_spaces " |
||
| 921 | " WHERE id = %s ", (id_,)) |
||
| 922 | if cursor.fetchone() is None: |
||
| 923 | cursor.close() |
||
| 924 | cnx.disconnect() |
||
| 925 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 926 | description='API.SPACE_NOT_FOUND') |
||
| 927 | |||
| 928 | cursor.execute(" SELECT name " |
||
| 929 | " FROM tbl_combined_equipments " |
||
| 930 | " WHERE id = %s ", (eid,)) |
||
| 931 | if cursor.fetchone() is None: |
||
| 932 | cursor.close() |
||
| 933 | cnx.disconnect() |
||
| 934 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 935 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 936 | |||
| 937 | cursor.execute(" SELECT id " |
||
| 938 | " FROM tbl_spaces_combined_equipments " |
||
| 939 | " WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid)) |
||
| 940 | if cursor.fetchone() is None: |
||
| 941 | cursor.close() |
||
| 942 | cnx.disconnect() |
||
| 943 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 944 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 945 | |||
| 946 | cursor.execute(" DELETE FROM tbl_spaces_combined_equipments " |
||
| 947 | " WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid)) |
||
| 948 | cnx.commit() |
||
| 949 | |||
| 950 | cursor.close() |
||
| 951 | cnx.disconnect() |
||
| 952 | |||
| 953 | resp.status = falcon.HTTP_204 |
||
| 954 | |||
| 955 | |||
| 956 | View Code Duplication | class SpaceEquipmentCollection: |
|
|
0 ignored issues
–
show
|
|||
| 957 | @staticmethod |
||
| 958 | def __init__(): |
||
| 959 | pass |
||
| 960 | |||
| 961 | @staticmethod |
||
| 962 | def on_options(req, resp, id_): |
||
| 963 | resp.status = falcon.HTTP_200 |
||
| 964 | |||
| 965 | @staticmethod |
||
| 966 | def on_get(req, resp, id_): |
||
| 967 | if not id_.isdigit() or int(id_) <= 0: |
||
| 968 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 969 | description='API.INVALID_SPACE_ID') |
||
| 970 | |||
| 971 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 972 | cursor = cnx.cursor() |
||
| 973 | |||
| 974 | cursor.execute(" SELECT name " |
||
| 975 | " FROM tbl_spaces " |
||
| 976 | " WHERE id = %s ", (id_,)) |
||
| 977 | if cursor.fetchone() is None: |
||
| 978 | cursor.close() |
||
| 979 | cnx.disconnect() |
||
| 980 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 981 | description='API.SPACE_NOT_FOUND') |
||
| 982 | |||
| 983 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 984 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
||
| 985 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 986 | " ORDER BY e.id ") |
||
| 987 | cursor.execute(query, (id_,)) |
||
| 988 | rows = cursor.fetchall() |
||
| 989 | |||
| 990 | result = list() |
||
| 991 | if rows is not None and len(rows) > 0: |
||
| 992 | for row in rows: |
||
| 993 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 994 | result.append(meta_result) |
||
| 995 | |||
| 996 | resp.body = json.dumps(result) |
||
| 997 | |||
| 998 | @staticmethod |
||
| 999 | def on_post(req, resp, id_): |
||
| 1000 | """Handles POST requests""" |
||
| 1001 | try: |
||
| 1002 | raw_json = req.stream.read().decode('utf-8') |
||
| 1003 | except Exception as ex: |
||
| 1004 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1005 | |||
| 1006 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1007 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1008 | description='API.INVALID_SPACE_ID') |
||
| 1009 | |||
| 1010 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1011 | |||
| 1012 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
| 1013 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
| 1014 | new_values['data']['equipment_id'] <= 0: |
||
| 1015 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1016 | description='API.INVALID_EQUIPMENT_ID') |
||
| 1017 | equipment_id = new_values['data']['equipment_id'] |
||
| 1018 | |||
| 1019 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1020 | cursor = cnx.cursor() |
||
| 1021 | |||
| 1022 | cursor.execute(" SELECT name " |
||
| 1023 | " from tbl_spaces " |
||
| 1024 | " WHERE id = %s ", (id_,)) |
||
| 1025 | if cursor.fetchone() is None: |
||
| 1026 | cursor.close() |
||
| 1027 | cnx.disconnect() |
||
| 1028 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1029 | description='API.SPACE_NOT_FOUND') |
||
| 1030 | |||
| 1031 | cursor.execute(" SELECT name " |
||
| 1032 | " FROM tbl_equipments " |
||
| 1033 | " WHERE id = %s ", (equipment_id,)) |
||
| 1034 | if cursor.fetchone() is None: |
||
| 1035 | cursor.close() |
||
| 1036 | cnx.disconnect() |
||
| 1037 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1038 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 1039 | |||
| 1040 | query = (" SELECT id " |
||
| 1041 | " FROM tbl_spaces_equipments " |
||
| 1042 | " WHERE space_id = %s AND equipment_id = %s") |
||
| 1043 | cursor.execute(query, (id_, equipment_id,)) |
||
| 1044 | if cursor.fetchone() is not None: |
||
| 1045 | cursor.close() |
||
| 1046 | cnx.disconnect() |
||
| 1047 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1048 | description='API.SPACE_EQUIPMENT_RELATION_EXISTED') |
||
| 1049 | |||
| 1050 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
||
| 1051 | " VALUES (%s, %s) ") |
||
| 1052 | cursor.execute(add_row, (id_, equipment_id,)) |
||
| 1053 | new_id = cursor.lastrowid |
||
| 1054 | cnx.commit() |
||
| 1055 | cursor.close() |
||
| 1056 | cnx.disconnect() |
||
| 1057 | |||
| 1058 | resp.status = falcon.HTTP_201 |
||
| 1059 | resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id) |
||
| 1060 | |||
| 1061 | |||
| 1062 | View Code Duplication | class SpaceEquipmentItem: |
|
|
0 ignored issues
–
show
|
|||
| 1063 | @staticmethod |
||
| 1064 | def __init__(): |
||
| 1065 | pass |
||
| 1066 | |||
| 1067 | @staticmethod |
||
| 1068 | def on_options(req, resp, id_, eid): |
||
| 1069 | resp.status = falcon.HTTP_200 |
||
| 1070 | |||
| 1071 | @staticmethod |
||
| 1072 | def on_delete(req, resp, id_, eid): |
||
| 1073 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1074 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1075 | description='API.INVALID_SPACE_ID') |
||
| 1076 | |||
| 1077 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1078 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1079 | description='API.INVALID_EQUIPMENT_ID') |
||
| 1080 | |||
| 1081 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1082 | cursor = cnx.cursor() |
||
| 1083 | |||
| 1084 | cursor.execute(" SELECT name " |
||
| 1085 | " FROM tbl_spaces " |
||
| 1086 | " WHERE id = %s ", (id_,)) |
||
| 1087 | if cursor.fetchone() is None: |
||
| 1088 | cursor.close() |
||
| 1089 | cnx.disconnect() |
||
| 1090 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1091 | description='API.SPACE_NOT_FOUND') |
||
| 1092 | |||
| 1093 | cursor.execute(" SELECT name " |
||
| 1094 | " FROM tbl_equipments " |
||
| 1095 | " WHERE id = %s ", (eid,)) |
||
| 1096 | if cursor.fetchone() is None: |
||
| 1097 | cursor.close() |
||
| 1098 | cnx.disconnect() |
||
| 1099 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1100 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 1101 | |||
| 1102 | cursor.execute(" SELECT id " |
||
| 1103 | " FROM tbl_spaces_equipments " |
||
| 1104 | " WHERE space_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 1105 | if cursor.fetchone() is None: |
||
| 1106 | cursor.close() |
||
| 1107 | cnx.disconnect() |
||
| 1108 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1109 | description='API.SPACE_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 1110 | |||
| 1111 | cursor.execute(" DELETE FROM tbl_spaces_equipments WHERE space_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 1112 | cnx.commit() |
||
| 1113 | |||
| 1114 | cursor.close() |
||
| 1115 | cnx.disconnect() |
||
| 1116 | |||
| 1117 | resp.status = falcon.HTTP_204 |
||
| 1118 | |||
| 1119 | |||
| 1120 | View Code Duplication | class SpaceMeterCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1121 | @staticmethod |
||
| 1122 | def __init__(): |
||
| 1123 | pass |
||
| 1124 | |||
| 1125 | @staticmethod |
||
| 1126 | def on_options(req, resp, id_): |
||
| 1127 | resp.status = falcon.HTTP_200 |
||
| 1128 | |||
| 1129 | @staticmethod |
||
| 1130 | def on_get(req, resp, id_): |
||
| 1131 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1132 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1133 | description='API.INVALID_SPACE_ID') |
||
| 1134 | |||
| 1135 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1136 | cursor = cnx.cursor(dictionary=True) |
||
| 1137 | |||
| 1138 | cursor.execute(" SELECT name " |
||
| 1139 | " FROM tbl_spaces " |
||
| 1140 | " WHERE id = %s ", (id_,)) |
||
| 1141 | if cursor.fetchone() is None: |
||
| 1142 | cursor.close() |
||
| 1143 | cnx.disconnect() |
||
| 1144 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1145 | description='API.SPACE_NOT_FOUND') |
||
| 1146 | |||
| 1147 | query = (" SELECT id, name, uuid " |
||
| 1148 | " FROM tbl_energy_categories ") |
||
| 1149 | cursor.execute(query) |
||
| 1150 | rows_energy_categories = cursor.fetchall() |
||
| 1151 | |||
| 1152 | energy_category_dict = dict() |
||
| 1153 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1154 | for row in rows_energy_categories: |
||
| 1155 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 1156 | "name": row['name'], |
||
| 1157 | "uuid": row['uuid']} |
||
| 1158 | |||
| 1159 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 1160 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
||
| 1161 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 1162 | " ORDER BY m.id ") |
||
| 1163 | cursor.execute(query, (id_,)) |
||
| 1164 | rows = cursor.fetchall() |
||
| 1165 | |||
| 1166 | result = list() |
||
| 1167 | if rows is not None and len(rows) > 0: |
||
| 1168 | for row in rows: |
||
| 1169 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 1170 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 1171 | "energy_category": energy_category} |
||
| 1172 | result.append(meta_result) |
||
| 1173 | |||
| 1174 | resp.body = json.dumps(result) |
||
| 1175 | |||
| 1176 | @staticmethod |
||
| 1177 | def on_post(req, resp, id_): |
||
| 1178 | """Handles POST requests""" |
||
| 1179 | try: |
||
| 1180 | raw_json = req.stream.read().decode('utf-8') |
||
| 1181 | except Exception as ex: |
||
| 1182 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1183 | |||
| 1184 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1185 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1186 | description='API.INVALID_SPACE_ID') |
||
| 1187 | |||
| 1188 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1189 | |||
| 1190 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1191 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1192 | new_values['data']['meter_id'] <= 0: |
||
| 1193 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1194 | description='API.INVALID_METER_ID') |
||
| 1195 | meter_id = new_values['data']['meter_id'] |
||
| 1196 | |||
| 1197 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1198 | cursor = cnx.cursor() |
||
| 1199 | |||
| 1200 | cursor.execute(" SELECT name " |
||
| 1201 | " from tbl_spaces " |
||
| 1202 | " WHERE id = %s ", (id_,)) |
||
| 1203 | if cursor.fetchone() is None: |
||
| 1204 | cursor.close() |
||
| 1205 | cnx.disconnect() |
||
| 1206 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1207 | description='API.SPACE_NOT_FOUND') |
||
| 1208 | |||
| 1209 | cursor.execute(" SELECT name " |
||
| 1210 | " FROM tbl_meters " |
||
| 1211 | " WHERE id = %s ", (meter_id,)) |
||
| 1212 | if cursor.fetchone() is None: |
||
| 1213 | cursor.close() |
||
| 1214 | cnx.disconnect() |
||
| 1215 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1216 | description='API.METER_NOT_FOUND') |
||
| 1217 | |||
| 1218 | query = (" SELECT id " |
||
| 1219 | " FROM tbl_spaces_meters " |
||
| 1220 | " WHERE space_id = %s AND meter_id = %s") |
||
| 1221 | cursor.execute(query, (id_, meter_id,)) |
||
| 1222 | if cursor.fetchone() is not None: |
||
| 1223 | cursor.close() |
||
| 1224 | cnx.disconnect() |
||
| 1225 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1226 | description='API.SPACE_METER_RELATION_EXISTED') |
||
| 1227 | |||
| 1228 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
||
| 1229 | " VALUES (%s, %s) ") |
||
| 1230 | cursor.execute(add_row, (id_, meter_id,)) |
||
| 1231 | new_id = cursor.lastrowid |
||
| 1232 | cnx.commit() |
||
| 1233 | cursor.close() |
||
| 1234 | cnx.disconnect() |
||
| 1235 | |||
| 1236 | resp.status = falcon.HTTP_201 |
||
| 1237 | resp.location = '/spaces/' + str(id_) + '/meters/' + str(meter_id) |
||
| 1238 | |||
| 1239 | |||
| 1240 | View Code Duplication | class SpaceMeterItem: |
|
|
0 ignored issues
–
show
|
|||
| 1241 | @staticmethod |
||
| 1242 | def __init__(): |
||
| 1243 | pass |
||
| 1244 | |||
| 1245 | @staticmethod |
||
| 1246 | def on_options(req, resp, id_, mid): |
||
| 1247 | resp.status = falcon.HTTP_200 |
||
| 1248 | |||
| 1249 | @staticmethod |
||
| 1250 | def on_delete(req, resp, id_, mid): |
||
| 1251 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1252 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1253 | description='API.INVALID_SPACE_ID') |
||
| 1254 | |||
| 1255 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1256 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1257 | description='API.INVALID_METER_ID') |
||
| 1258 | |||
| 1259 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1260 | cursor = cnx.cursor() |
||
| 1261 | |||
| 1262 | cursor.execute(" SELECT name " |
||
| 1263 | " FROM tbl_spaces " |
||
| 1264 | " WHERE id = %s ", (id_,)) |
||
| 1265 | if cursor.fetchone() is None: |
||
| 1266 | cursor.close() |
||
| 1267 | cnx.disconnect() |
||
| 1268 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1269 | description='API.SPACE_NOT_FOUND') |
||
| 1270 | |||
| 1271 | cursor.execute(" SELECT name " |
||
| 1272 | " FROM tbl_meters " |
||
| 1273 | " WHERE id = %s ", (mid,)) |
||
| 1274 | if cursor.fetchone() is None: |
||
| 1275 | cursor.close() |
||
| 1276 | cnx.disconnect() |
||
| 1277 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1278 | description='API.METER_NOT_FOUND') |
||
| 1279 | |||
| 1280 | cursor.execute(" SELECT id " |
||
| 1281 | " FROM tbl_spaces_meters " |
||
| 1282 | " WHERE space_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1283 | if cursor.fetchone() is None: |
||
| 1284 | cursor.close() |
||
| 1285 | cnx.disconnect() |
||
| 1286 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1287 | description='API.SPACE_METER_RELATION_NOT_FOUND') |
||
| 1288 | |||
| 1289 | cursor.execute(" DELETE FROM tbl_spaces_meters WHERE space_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1290 | cnx.commit() |
||
| 1291 | |||
| 1292 | cursor.close() |
||
| 1293 | cnx.disconnect() |
||
| 1294 | |||
| 1295 | resp.status = falcon.HTTP_204 |
||
| 1296 | |||
| 1297 | |||
| 1298 | View Code Duplication | class SpaceOfflineMeterCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1299 | @staticmethod |
||
| 1300 | def __init__(): |
||
| 1301 | pass |
||
| 1302 | |||
| 1303 | @staticmethod |
||
| 1304 | def on_options(req, resp, id_): |
||
| 1305 | resp.status = falcon.HTTP_200 |
||
| 1306 | |||
| 1307 | @staticmethod |
||
| 1308 | def on_get(req, resp, id_): |
||
| 1309 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1310 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1311 | description='API.INVALID_SPACE_ID') |
||
| 1312 | |||
| 1313 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1314 | cursor = cnx.cursor(dictionary=True) |
||
| 1315 | |||
| 1316 | cursor.execute(" SELECT name " |
||
| 1317 | " FROM tbl_spaces " |
||
| 1318 | " WHERE id = %s ", (id_,)) |
||
| 1319 | if cursor.fetchone() is None: |
||
| 1320 | cursor.close() |
||
| 1321 | cnx.disconnect() |
||
| 1322 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1323 | description='API.SPACE_NOT_FOUND') |
||
| 1324 | |||
| 1325 | query = (" SELECT id, name, uuid " |
||
| 1326 | " FROM tbl_energy_categories ") |
||
| 1327 | cursor.execute(query) |
||
| 1328 | rows_energy_categories = cursor.fetchall() |
||
| 1329 | |||
| 1330 | energy_category_dict = dict() |
||
| 1331 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1332 | for row in rows_energy_categories: |
||
| 1333 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 1334 | "name": row['name'], |
||
| 1335 | "uuid": row['uuid']} |
||
| 1336 | |||
| 1337 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 1338 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
||
| 1339 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 1340 | " ORDER BY m.id ") |
||
| 1341 | cursor.execute(query, (id_,)) |
||
| 1342 | rows = cursor.fetchall() |
||
| 1343 | |||
| 1344 | result = list() |
||
| 1345 | if rows is not None and len(rows) > 0: |
||
| 1346 | for row in rows: |
||
| 1347 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 1348 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 1349 | "energy_category": energy_category} |
||
| 1350 | result.append(meta_result) |
||
| 1351 | |||
| 1352 | resp.body = json.dumps(result) |
||
| 1353 | |||
| 1354 | @staticmethod |
||
| 1355 | def on_post(req, resp, id_): |
||
| 1356 | """Handles POST requests""" |
||
| 1357 | try: |
||
| 1358 | raw_json = req.stream.read().decode('utf-8') |
||
| 1359 | except Exception as ex: |
||
| 1360 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1361 | |||
| 1362 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1363 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1364 | description='API.INVALID_SPACE_ID') |
||
| 1365 | |||
| 1366 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1367 | |||
| 1368 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 1369 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 1370 | new_values['data']['offline_meter_id'] <= 0: |
||
| 1371 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1372 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1373 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 1374 | |||
| 1375 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1376 | cursor = cnx.cursor() |
||
| 1377 | |||
| 1378 | cursor.execute(" SELECT name " |
||
| 1379 | " from tbl_spaces " |
||
| 1380 | " WHERE id = %s ", (id_,)) |
||
| 1381 | if cursor.fetchone() is None: |
||
| 1382 | cursor.close() |
||
| 1383 | cnx.disconnect() |
||
| 1384 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1385 | description='API.SPACE_NOT_FOUND') |
||
| 1386 | |||
| 1387 | cursor.execute(" SELECT name " |
||
| 1388 | " FROM tbl_offline_meters " |
||
| 1389 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 1390 | if cursor.fetchone() is None: |
||
| 1391 | cursor.close() |
||
| 1392 | cnx.disconnect() |
||
| 1393 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1394 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1395 | |||
| 1396 | query = (" SELECT id " |
||
| 1397 | " FROM tbl_spaces_offline_meters " |
||
| 1398 | " WHERE space_id = %s AND offline_meter_id = %s") |
||
| 1399 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 1400 | if cursor.fetchone() is not None: |
||
| 1401 | cursor.close() |
||
| 1402 | cnx.disconnect() |
||
| 1403 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1404 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTED') |
||
| 1405 | |||
| 1406 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
||
| 1407 | " VALUES (%s, %s) ") |
||
| 1408 | cursor.execute(add_row, (id_, offline_meter_id,)) |
||
| 1409 | new_id = cursor.lastrowid |
||
| 1410 | cnx.commit() |
||
| 1411 | cursor.close() |
||
| 1412 | cnx.disconnect() |
||
| 1413 | |||
| 1414 | resp.status = falcon.HTTP_201 |
||
| 1415 | resp.location = '/spaces/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 1416 | |||
| 1417 | |||
| 1418 | View Code Duplication | class SpaceOfflineMeterItem: |
|
|
0 ignored issues
–
show
|
|||
| 1419 | @staticmethod |
||
| 1420 | def __init__(): |
||
| 1421 | pass |
||
| 1422 | |||
| 1423 | @staticmethod |
||
| 1424 | def on_options(req, resp, id_, mid): |
||
| 1425 | resp.status = falcon.HTTP_200 |
||
| 1426 | |||
| 1427 | @staticmethod |
||
| 1428 | def on_delete(req, resp, id_, mid): |
||
| 1429 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1430 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1431 | description='API.INVALID_SPACE_ID') |
||
| 1432 | |||
| 1433 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1434 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1435 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1436 | |||
| 1437 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1438 | cursor = cnx.cursor() |
||
| 1439 | |||
| 1440 | cursor.execute(" SELECT name " |
||
| 1441 | " FROM tbl_spaces " |
||
| 1442 | " WHERE id = %s ", (id_,)) |
||
| 1443 | if cursor.fetchone() is None: |
||
| 1444 | cursor.close() |
||
| 1445 | cnx.disconnect() |
||
| 1446 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1447 | description='API.SPACE_NOT_FOUND') |
||
| 1448 | |||
| 1449 | cursor.execute(" SELECT name " |
||
| 1450 | " FROM tbl_offline_meters " |
||
| 1451 | " WHERE id = %s ", (mid,)) |
||
| 1452 | if cursor.fetchone() is None: |
||
| 1453 | cursor.close() |
||
| 1454 | cnx.disconnect() |
||
| 1455 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1456 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1457 | |||
| 1458 | cursor.execute(" SELECT id " |
||
| 1459 | " FROM tbl_spaces_offline_meters " |
||
| 1460 | " WHERE space_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1461 | if cursor.fetchone() is None: |
||
| 1462 | cursor.close() |
||
| 1463 | cnx.disconnect() |
||
| 1464 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1465 | description='API.SPACE_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 1466 | |||
| 1467 | cursor.execute(" DELETE FROM tbl_spaces_offline_meters " |
||
| 1468 | " WHERE space_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1469 | cnx.commit() |
||
| 1470 | |||
| 1471 | cursor.close() |
||
| 1472 | cnx.disconnect() |
||
| 1473 | |||
| 1474 | resp.status = falcon.HTTP_204 |
||
| 1475 | |||
| 1476 | |||
| 1477 | View Code Duplication | class SpacePointCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1478 | @staticmethod |
||
| 1479 | def __init__(): |
||
| 1480 | pass |
||
| 1481 | |||
| 1482 | @staticmethod |
||
| 1483 | def on_options(req, resp, id_): |
||
| 1484 | resp.status = falcon.HTTP_200 |
||
| 1485 | |||
| 1486 | @staticmethod |
||
| 1487 | def on_get(req, resp, id_): |
||
| 1488 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1489 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1490 | description='API.INVALID_SPACE_ID') |
||
| 1491 | |||
| 1492 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1493 | cursor = cnx.cursor(dictionary=True) |
||
| 1494 | |||
| 1495 | cursor.execute(" SELECT name " |
||
| 1496 | " FROM tbl_spaces " |
||
| 1497 | " WHERE id = %s ", (id_,)) |
||
| 1498 | if cursor.fetchone() is None: |
||
| 1499 | cursor.close() |
||
| 1500 | cnx.disconnect() |
||
| 1501 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1502 | description='API.SPACE_NOT_FOUND') |
||
| 1503 | |||
| 1504 | query = (" SELECT id, name, uuid " |
||
| 1505 | " FROM tbl_data_sources ") |
||
| 1506 | cursor.execute(query) |
||
| 1507 | rows_data_sources = cursor.fetchall() |
||
| 1508 | |||
| 1509 | data_source_dict = dict() |
||
| 1510 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 1511 | for row in rows_data_sources: |
||
| 1512 | data_source_dict[row['id']] = {"id": row['id'], |
||
| 1513 | "name": row['name'], |
||
| 1514 | "uuid": row['uuid']} |
||
| 1515 | |||
| 1516 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 1517 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
||
| 1518 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 1519 | " ORDER BY p.id ") |
||
| 1520 | cursor.execute(query, (id_,)) |
||
| 1521 | rows = cursor.fetchall() |
||
| 1522 | |||
| 1523 | result = list() |
||
| 1524 | if rows is not None and len(rows) > 0: |
||
| 1525 | for row in rows: |
||
| 1526 | data_source = data_source_dict.get(row['data_source_id'], None) |
||
| 1527 | meta_result = {"id": row['id'], "name": row['name'], "data_source": data_source} |
||
| 1528 | result.append(meta_result) |
||
| 1529 | |||
| 1530 | resp.body = json.dumps(result) |
||
| 1531 | |||
| 1532 | @staticmethod |
||
| 1533 | def on_post(req, resp, id_): |
||
| 1534 | """Handles POST requests""" |
||
| 1535 | try: |
||
| 1536 | raw_json = req.stream.read().decode('utf-8') |
||
| 1537 | except Exception as ex: |
||
| 1538 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1539 | |||
| 1540 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1541 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1542 | description='API.INVALID_SPACE_ID') |
||
| 1543 | |||
| 1544 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1545 | |||
| 1546 | if 'point_id' not in new_values['data'].keys() or \ |
||
| 1547 | not isinstance(new_values['data']['point_id'], int) or \ |
||
| 1548 | new_values['data']['point_id'] <= 0: |
||
| 1549 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1550 | description='API.INVALID_POINT_ID') |
||
| 1551 | point_id = new_values['data']['point_id'] |
||
| 1552 | |||
| 1553 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1554 | cursor = cnx.cursor() |
||
| 1555 | |||
| 1556 | cursor.execute(" SELECT name " |
||
| 1557 | " from tbl_spaces " |
||
| 1558 | " WHERE id = %s ", (id_,)) |
||
| 1559 | if cursor.fetchone() is None: |
||
| 1560 | cursor.close() |
||
| 1561 | cnx.disconnect() |
||
| 1562 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1563 | description='API.SPACE_NOT_FOUND') |
||
| 1564 | |||
| 1565 | cursor.execute(" SELECT name " |
||
| 1566 | " FROM tbl_points " |
||
| 1567 | " WHERE id = %s ", (point_id,)) |
||
| 1568 | if cursor.fetchone() is None: |
||
| 1569 | cursor.close() |
||
| 1570 | cnx.disconnect() |
||
| 1571 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1572 | description='API.POINT_NOT_FOUND') |
||
| 1573 | |||
| 1574 | query = (" SELECT id " |
||
| 1575 | " FROM tbl_spaces_points " |
||
| 1576 | " WHERE space_id = %s AND point_id = %s") |
||
| 1577 | cursor.execute(query, (id_, point_id,)) |
||
| 1578 | if cursor.fetchone() is not None: |
||
| 1579 | cursor.close() |
||
| 1580 | cnx.disconnect() |
||
| 1581 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1582 | description='API.SPACE_POINT_RELATION_EXISTED') |
||
| 1583 | |||
| 1584 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
||
| 1585 | " VALUES (%s, %s) ") |
||
| 1586 | cursor.execute(add_row, (id_, point_id,)) |
||
| 1587 | new_id = cursor.lastrowid |
||
| 1588 | cnx.commit() |
||
| 1589 | cursor.close() |
||
| 1590 | cnx.disconnect() |
||
| 1591 | |||
| 1592 | resp.status = falcon.HTTP_201 |
||
| 1593 | resp.location = '/spaces/' + str(id_) + '/points/' + str(point_id) |
||
| 1594 | |||
| 1595 | |||
| 1596 | View Code Duplication | class SpacePointItem: |
|
|
0 ignored issues
–
show
|
|||
| 1597 | @staticmethod |
||
| 1598 | def __init__(): |
||
| 1599 | pass |
||
| 1600 | |||
| 1601 | @staticmethod |
||
| 1602 | def on_options(req, resp, id_, pid): |
||
| 1603 | resp.status = falcon.HTTP_200 |
||
| 1604 | |||
| 1605 | @staticmethod |
||
| 1606 | def on_delete(req, resp, id_, pid): |
||
| 1607 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1608 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1609 | description='API.INVALID_SPACE_ID') |
||
| 1610 | |||
| 1611 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1612 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1613 | description='API.INVALID_POINT_ID') |
||
| 1614 | |||
| 1615 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1616 | cursor = cnx.cursor() |
||
| 1617 | |||
| 1618 | cursor.execute(" SELECT name " |
||
| 1619 | " FROM tbl_spaces " |
||
| 1620 | " WHERE id = %s ", (id_,)) |
||
| 1621 | if cursor.fetchone() is None: |
||
| 1622 | cursor.close() |
||
| 1623 | cnx.disconnect() |
||
| 1624 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1625 | description='API.SPACE_NOT_FOUND') |
||
| 1626 | |||
| 1627 | cursor.execute(" SELECT name " |
||
| 1628 | " FROM tbl_points " |
||
| 1629 | " WHERE id = %s ", (pid,)) |
||
| 1630 | if cursor.fetchone() is None: |
||
| 1631 | cursor.close() |
||
| 1632 | cnx.disconnect() |
||
| 1633 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1634 | description='API.POINT_NOT_FOUND') |
||
| 1635 | |||
| 1636 | cursor.execute(" SELECT id " |
||
| 1637 | " FROM tbl_spaces_points " |
||
| 1638 | " WHERE space_id = %s AND point_id = %s ", (id_, pid)) |
||
| 1639 | if cursor.fetchone() is None: |
||
| 1640 | cursor.close() |
||
| 1641 | cnx.disconnect() |
||
| 1642 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1643 | description='API.SPACE_POINT_RELATION_NOT_FOUND') |
||
| 1644 | |||
| 1645 | cursor.execute(" DELETE FROM tbl_spaces_points " |
||
| 1646 | " WHERE space_id = %s AND point_id = %s ", (id_, pid)) |
||
| 1647 | cnx.commit() |
||
| 1648 | |||
| 1649 | cursor.close() |
||
| 1650 | cnx.disconnect() |
||
| 1651 | |||
| 1652 | resp.status = falcon.HTTP_204 |
||
| 1653 | |||
| 1654 | |||
| 1655 | View Code Duplication | class SpaceSensorCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1656 | @staticmethod |
||
| 1657 | def __init__(): |
||
| 1658 | pass |
||
| 1659 | |||
| 1660 | @staticmethod |
||
| 1661 | def on_options(req, resp, id_): |
||
| 1662 | resp.status = falcon.HTTP_200 |
||
| 1663 | |||
| 1664 | @staticmethod |
||
| 1665 | def on_get(req, resp, id_): |
||
| 1666 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1667 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1668 | description='API.INVALID_SPACE_ID') |
||
| 1669 | |||
| 1670 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1671 | cursor = cnx.cursor() |
||
| 1672 | |||
| 1673 | cursor.execute(" SELECT name " |
||
| 1674 | " FROM tbl_spaces " |
||
| 1675 | " WHERE id = %s ", (id_,)) |
||
| 1676 | if cursor.fetchone() is None: |
||
| 1677 | cursor.close() |
||
| 1678 | cnx.disconnect() |
||
| 1679 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1680 | description='API.SPACE_NOT_FOUND') |
||
| 1681 | |||
| 1682 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 1683 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
||
| 1684 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 1685 | " ORDER BY se.id ") |
||
| 1686 | cursor.execute(query, (id_,)) |
||
| 1687 | rows = cursor.fetchall() |
||
| 1688 | |||
| 1689 | result = list() |
||
| 1690 | if rows is not None and len(rows) > 0: |
||
| 1691 | for row in rows: |
||
| 1692 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1693 | result.append(meta_result) |
||
| 1694 | |||
| 1695 | resp.body = json.dumps(result) |
||
| 1696 | |||
| 1697 | @staticmethod |
||
| 1698 | def on_post(req, resp, id_): |
||
| 1699 | """Handles POST requests""" |
||
| 1700 | try: |
||
| 1701 | raw_json = req.stream.read().decode('utf-8') |
||
| 1702 | except Exception as ex: |
||
| 1703 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1704 | |||
| 1705 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1706 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1707 | description='API.INVALID_SPACE_ID') |
||
| 1708 | |||
| 1709 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1710 | |||
| 1711 | if 'sensor_id' not in new_values['data'].keys() or \ |
||
| 1712 | not isinstance(new_values['data']['sensor_id'], int) or \ |
||
| 1713 | new_values['data']['sensor_id'] <= 0: |
||
| 1714 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1715 | description='API.INVALID_SENSOR_ID') |
||
| 1716 | sensor_id = new_values['data']['sensor_id'] |
||
| 1717 | |||
| 1718 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1719 | cursor = cnx.cursor() |
||
| 1720 | |||
| 1721 | cursor.execute(" SELECT name " |
||
| 1722 | " from tbl_spaces " |
||
| 1723 | " WHERE id = %s ", (id_,)) |
||
| 1724 | if cursor.fetchone() is None: |
||
| 1725 | cursor.close() |
||
| 1726 | cnx.disconnect() |
||
| 1727 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1728 | description='API.SPACE_NOT_FOUND') |
||
| 1729 | |||
| 1730 | cursor.execute(" SELECT name " |
||
| 1731 | " FROM tbl_sensors " |
||
| 1732 | " WHERE id = %s ", (sensor_id,)) |
||
| 1733 | if cursor.fetchone() is None: |
||
| 1734 | cursor.close() |
||
| 1735 | cnx.disconnect() |
||
| 1736 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1737 | description='API.SENSOR_NOT_FOUND') |
||
| 1738 | |||
| 1739 | query = (" SELECT id " |
||
| 1740 | " FROM tbl_spaces_sensors " |
||
| 1741 | " WHERE space_id = %s AND sensor_id = %s") |
||
| 1742 | cursor.execute(query, (id_, sensor_id,)) |
||
| 1743 | if cursor.fetchone() is not None: |
||
| 1744 | cursor.close() |
||
| 1745 | cnx.disconnect() |
||
| 1746 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1747 | description='API.SPACE_SENSOR_RELATION_EXISTED') |
||
| 1748 | |||
| 1749 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
||
| 1750 | " VALUES (%s, %s) ") |
||
| 1751 | cursor.execute(add_row, (id_, sensor_id,)) |
||
| 1752 | new_id = cursor.lastrowid |
||
| 1753 | cnx.commit() |
||
| 1754 | cursor.close() |
||
| 1755 | cnx.disconnect() |
||
| 1756 | |||
| 1757 | resp.status = falcon.HTTP_201 |
||
| 1758 | resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id) |
||
| 1759 | |||
| 1760 | |||
| 1761 | View Code Duplication | class SpaceSensorItem: |
|
|
0 ignored issues
–
show
|
|||
| 1762 | @staticmethod |
||
| 1763 | def __init__(): |
||
| 1764 | pass |
||
| 1765 | |||
| 1766 | @staticmethod |
||
| 1767 | def on_options(req, resp, id_, sid): |
||
| 1768 | resp.status = falcon.HTTP_200 |
||
| 1769 | |||
| 1770 | @staticmethod |
||
| 1771 | def on_delete(req, resp, id_, sid): |
||
| 1772 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1773 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1774 | description='API.INVALID_SPACE_ID') |
||
| 1775 | |||
| 1776 | if not sid.isdigit() or int(sid) <= 0: |
||
| 1777 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1778 | description='API.INVALID_SENSOR_ID') |
||
| 1779 | |||
| 1780 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1781 | cursor = cnx.cursor() |
||
| 1782 | |||
| 1783 | cursor.execute(" SELECT name " |
||
| 1784 | " FROM tbl_spaces " |
||
| 1785 | " WHERE id = %s ", (id_,)) |
||
| 1786 | if cursor.fetchone() is None: |
||
| 1787 | cursor.close() |
||
| 1788 | cnx.disconnect() |
||
| 1789 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1790 | description='API.SPACE_NOT_FOUND') |
||
| 1791 | |||
| 1792 | cursor.execute(" SELECT name " |
||
| 1793 | " FROM tbl_sensors " |
||
| 1794 | " WHERE id = %s ", (sid,)) |
||
| 1795 | if cursor.fetchone() is None: |
||
| 1796 | cursor.close() |
||
| 1797 | cnx.disconnect() |
||
| 1798 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1799 | description='API.SENSOR_NOT_FOUND') |
||
| 1800 | |||
| 1801 | cursor.execute(" SELECT id " |
||
| 1802 | " FROM tbl_spaces_sensors " |
||
| 1803 | " WHERE space_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 1804 | if cursor.fetchone() is None: |
||
| 1805 | cursor.close() |
||
| 1806 | cnx.disconnect() |
||
| 1807 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1808 | description='API.SPACE_SENSOR_RELATION_NOT_FOUND') |
||
| 1809 | |||
| 1810 | cursor.execute(" DELETE FROM tbl_spaces_sensors WHERE space_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 1811 | cnx.commit() |
||
| 1812 | |||
| 1813 | cursor.close() |
||
| 1814 | cnx.disconnect() |
||
| 1815 | |||
| 1816 | resp.status = falcon.HTTP_204 |
||
| 1817 | |||
| 1818 | |||
| 1819 | View Code Duplication | class SpaceShopfloorCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1820 | @staticmethod |
||
| 1821 | def __init__(): |
||
| 1822 | pass |
||
| 1823 | |||
| 1824 | @staticmethod |
||
| 1825 | def on_options(req, resp, id_): |
||
| 1826 | resp.status = falcon.HTTP_200 |
||
| 1827 | |||
| 1828 | @staticmethod |
||
| 1829 | def on_get(req, resp, id_): |
||
| 1830 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1831 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1832 | description='API.INVALID_SPACE_ID') |
||
| 1833 | |||
| 1834 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1835 | cursor = cnx.cursor() |
||
| 1836 | |||
| 1837 | cursor.execute(" SELECT name " |
||
| 1838 | " FROM tbl_spaces " |
||
| 1839 | " WHERE id = %s ", (id_,)) |
||
| 1840 | if cursor.fetchone() is None: |
||
| 1841 | cursor.close() |
||
| 1842 | cnx.disconnect() |
||
| 1843 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1844 | description='API.SPACE_NOT_FOUND') |
||
| 1845 | |||
| 1846 | query = (" SELECT sf.id, sf.name, sf.uuid " |
||
| 1847 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
||
| 1848 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
||
| 1849 | " ORDER BY sf.id ") |
||
| 1850 | cursor.execute(query, (id_,)) |
||
| 1851 | rows = cursor.fetchall() |
||
| 1852 | |||
| 1853 | result = list() |
||
| 1854 | if rows is not None and len(rows) > 0: |
||
| 1855 | for row in rows: |
||
| 1856 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1857 | result.append(meta_result) |
||
| 1858 | |||
| 1859 | resp.body = json.dumps(result) |
||
| 1860 | |||
| 1861 | @staticmethod |
||
| 1862 | def on_post(req, resp, id_): |
||
| 1863 | """Handles POST requests""" |
||
| 1864 | try: |
||
| 1865 | raw_json = req.stream.read().decode('utf-8') |
||
| 1866 | except Exception as ex: |
||
| 1867 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 1868 | |||
| 1869 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1870 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1871 | description='API.INVALID_SPACE_ID') |
||
| 1872 | |||
| 1873 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 1874 | |||
| 1875 | if 'shopfloor_id' not in new_values['data'].keys() or \ |
||
| 1876 | not isinstance(new_values['data']['shopfloor_id'], int) or \ |
||
| 1877 | new_values['data']['shopfloor_id'] <= 0: |
||
| 1878 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1879 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1880 | shopfloor_id = new_values['data']['shopfloor_id'] |
||
| 1881 | |||
| 1882 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1883 | cursor = cnx.cursor() |
||
| 1884 | |||
| 1885 | cursor.execute(" SELECT name " |
||
| 1886 | " from tbl_spaces " |
||
| 1887 | " WHERE id = %s ", (id_,)) |
||
| 1888 | if cursor.fetchone() is None: |
||
| 1889 | cursor.close() |
||
| 1890 | cnx.disconnect() |
||
| 1891 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1892 | description='API.SPACE_NOT_FOUND') |
||
| 1893 | |||
| 1894 | cursor.execute(" SELECT name " |
||
| 1895 | " FROM tbl_shopfloors " |
||
| 1896 | " WHERE id = %s ", (shopfloor_id,)) |
||
| 1897 | if cursor.fetchone() is None: |
||
| 1898 | cursor.close() |
||
| 1899 | cnx.disconnect() |
||
| 1900 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1901 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1902 | |||
| 1903 | query = (" SELECT id " |
||
| 1904 | " FROM tbl_spaces_shopfloors " |
||
| 1905 | " WHERE space_id = %s AND shopfloor_id = %s") |
||
| 1906 | cursor.execute(query, (id_, shopfloor_id,)) |
||
| 1907 | if cursor.fetchone() is not None: |
||
| 1908 | cursor.close() |
||
| 1909 | cnx.disconnect() |
||
| 1910 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 1911 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTED') |
||
| 1912 | |||
| 1913 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
||
| 1914 | " VALUES (%s, %s) ") |
||
| 1915 | cursor.execute(add_row, (id_, shopfloor_id,)) |
||
| 1916 | new_id = cursor.lastrowid |
||
| 1917 | cnx.commit() |
||
| 1918 | cursor.close() |
||
| 1919 | cnx.disconnect() |
||
| 1920 | |||
| 1921 | resp.status = falcon.HTTP_201 |
||
| 1922 | resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id) |
||
| 1923 | |||
| 1924 | |||
| 1925 | View Code Duplication | class SpaceShopfloorItem: |
|
|
0 ignored issues
–
show
|
|||
| 1926 | @staticmethod |
||
| 1927 | def __init__(): |
||
| 1928 | pass |
||
| 1929 | |||
| 1930 | @staticmethod |
||
| 1931 | def on_options(req, resp, id_, sid): |
||
| 1932 | resp.status = falcon.HTTP_200 |
||
| 1933 | |||
| 1934 | @staticmethod |
||
| 1935 | def on_delete(req, resp, id_, sid): |
||
| 1936 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1937 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1938 | description='API.INVALID_SPACE_ID') |
||
| 1939 | |||
| 1940 | if not sid.isdigit() or int(sid) <= 0: |
||
| 1941 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1942 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1943 | |||
| 1944 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1945 | cursor = cnx.cursor() |
||
| 1946 | |||
| 1947 | cursor.execute(" SELECT name " |
||
| 1948 | " FROM tbl_spaces " |
||
| 1949 | " WHERE id = %s ", (id_,)) |
||
| 1950 | if cursor.fetchone() is None: |
||
| 1951 | cursor.close() |
||
| 1952 | cnx.disconnect() |
||
| 1953 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1954 | description='API.SPACE_NOT_FOUND') |
||
| 1955 | |||
| 1956 | cursor.execute(" SELECT name " |
||
| 1957 | " FROM tbl_shopfloors " |
||
| 1958 | " WHERE id = %s ", (sid,)) |
||
| 1959 | if cursor.fetchone() is None: |
||
| 1960 | cursor.close() |
||
| 1961 | cnx.disconnect() |
||
| 1962 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1963 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1964 | |||
| 1965 | cursor.execute(" SELECT id " |
||
| 1966 | " FROM tbl_spaces_shopfloors " |
||
| 1967 | " WHERE space_id = %s AND shopfloor_id = %s ", (id_, sid)) |
||
| 1968 | if cursor.fetchone() is None: |
||
| 1969 | cursor.close() |
||
| 1970 | cnx.disconnect() |
||
| 1971 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1972 | description='API.SPACE_SHOPFLOOR_RELATION_NOT_FOUND') |
||
| 1973 | |||
| 1974 | cursor.execute(" DELETE FROM tbl_spaces_shopfloors WHERE space_id = %s AND shopfloor_id = %s ", (id_, sid)) |
||
| 1975 | cnx.commit() |
||
| 1976 | |||
| 1977 | cursor.close() |
||
| 1978 | cnx.disconnect() |
||
| 1979 | |||
| 1980 | resp.status = falcon.HTTP_204 |
||
| 1981 | |||
| 1982 | |||
| 1983 | View Code Duplication | class SpaceStoreCollection: |
|
|
0 ignored issues
–
show
|
|||
| 1984 | @staticmethod |
||
| 1985 | def __init__(): |
||
| 1986 | pass |
||
| 1987 | |||
| 1988 | @staticmethod |
||
| 1989 | def on_options(req, resp, id_): |
||
| 1990 | resp.status = falcon.HTTP_200 |
||
| 1991 | |||
| 1992 | @staticmethod |
||
| 1993 | def on_get(req, resp, id_): |
||
| 1994 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1995 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1996 | description='API.INVALID_SPACE_ID') |
||
| 1997 | |||
| 1998 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1999 | cursor = cnx.cursor() |
||
| 2000 | |||
| 2001 | cursor.execute(" SELECT name " |
||
| 2002 | " FROM tbl_spaces " |
||
| 2003 | " WHERE id = %s ", (id_,)) |
||
| 2004 | if cursor.fetchone() is None: |
||
| 2005 | cursor.close() |
||
| 2006 | cnx.disconnect() |
||
| 2007 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2008 | description='API.SPACE_NOT_FOUND') |
||
| 2009 | |||
| 2010 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 2011 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
||
| 2012 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
||
| 2013 | " ORDER BY t.id ") |
||
| 2014 | cursor.execute(query, (id_,)) |
||
| 2015 | rows = cursor.fetchall() |
||
| 2016 | |||
| 2017 | result = list() |
||
| 2018 | if rows is not None and len(rows) > 0: |
||
| 2019 | for row in rows: |
||
| 2020 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2021 | result.append(meta_result) |
||
| 2022 | |||
| 2023 | resp.body = json.dumps(result) |
||
| 2024 | |||
| 2025 | @staticmethod |
||
| 2026 | def on_post(req, resp, id_): |
||
| 2027 | """Handles POST requests""" |
||
| 2028 | try: |
||
| 2029 | raw_json = req.stream.read().decode('utf-8') |
||
| 2030 | except Exception as ex: |
||
| 2031 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 2032 | |||
| 2033 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2034 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2035 | description='API.INVALID_SPACE_ID') |
||
| 2036 | |||
| 2037 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 2038 | |||
| 2039 | if 'store_id' not in new_values['data'].keys() or \ |
||
| 2040 | not isinstance(new_values['data']['store_id'], int) or \ |
||
| 2041 | new_values['data']['store_id'] <= 0: |
||
| 2042 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2043 | description='API.INVALID_STORE_ID') |
||
| 2044 | store_id = new_values['data']['store_id'] |
||
| 2045 | |||
| 2046 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2047 | cursor = cnx.cursor() |
||
| 2048 | |||
| 2049 | cursor.execute(" SELECT name " |
||
| 2050 | " from tbl_spaces " |
||
| 2051 | " WHERE id = %s ", (id_,)) |
||
| 2052 | if cursor.fetchone() is None: |
||
| 2053 | cursor.close() |
||
| 2054 | cnx.disconnect() |
||
| 2055 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2056 | description='API.SPACE_NOT_FOUND') |
||
| 2057 | |||
| 2058 | cursor.execute(" SELECT name " |
||
| 2059 | " FROM tbl_stores " |
||
| 2060 | " WHERE id = %s ", (store_id,)) |
||
| 2061 | if cursor.fetchone() is None: |
||
| 2062 | cursor.close() |
||
| 2063 | cnx.disconnect() |
||
| 2064 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2065 | description='API.STORE_NOT_FOUND') |
||
| 2066 | |||
| 2067 | query = (" SELECT id " |
||
| 2068 | " FROM tbl_spaces_stores " |
||
| 2069 | " WHERE space_id = %s AND store_id = %s") |
||
| 2070 | cursor.execute(query, (id_, store_id,)) |
||
| 2071 | if cursor.fetchone() is not None: |
||
| 2072 | cursor.close() |
||
| 2073 | cnx.disconnect() |
||
| 2074 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 2075 | description='API.SPACE_STORE_RELATION_EXISTED') |
||
| 2076 | |||
| 2077 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
||
| 2078 | " VALUES (%s, %s) ") |
||
| 2079 | cursor.execute(add_row, (id_, store_id,)) |
||
| 2080 | new_id = cursor.lastrowid |
||
| 2081 | cnx.commit() |
||
| 2082 | cursor.close() |
||
| 2083 | cnx.disconnect() |
||
| 2084 | |||
| 2085 | resp.status = falcon.HTTP_201 |
||
| 2086 | resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id) |
||
| 2087 | |||
| 2088 | |||
| 2089 | View Code Duplication | class SpaceStoreItem: |
|
|
0 ignored issues
–
show
|
|||
| 2090 | @staticmethod |
||
| 2091 | def __init__(): |
||
| 2092 | pass |
||
| 2093 | |||
| 2094 | @staticmethod |
||
| 2095 | def on_options(req, resp, id_, tid): |
||
| 2096 | resp.status = falcon.HTTP_200 |
||
| 2097 | |||
| 2098 | @staticmethod |
||
| 2099 | def on_delete(req, resp, id_, tid): |
||
| 2100 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2101 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2102 | description='API.INVALID_SPACE_ID') |
||
| 2103 | |||
| 2104 | if not tid.isdigit() or int(tid) <= 0: |
||
| 2105 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2106 | description='API.INVALID_STORE_ID') |
||
| 2107 | |||
| 2108 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2109 | cursor = cnx.cursor() |
||
| 2110 | |||
| 2111 | cursor.execute(" SELECT name " |
||
| 2112 | " FROM tbl_spaces " |
||
| 2113 | " WHERE id = %s ", (id_,)) |
||
| 2114 | if cursor.fetchone() is None: |
||
| 2115 | cursor.close() |
||
| 2116 | cnx.disconnect() |
||
| 2117 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2118 | description='API.SPACE_NOT_FOUND') |
||
| 2119 | |||
| 2120 | cursor.execute(" SELECT name " |
||
| 2121 | " FROM tbl_stores " |
||
| 2122 | " WHERE id = %s ", (tid,)) |
||
| 2123 | if cursor.fetchone() is None: |
||
| 2124 | cursor.close() |
||
| 2125 | cnx.disconnect() |
||
| 2126 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2127 | description='API.STORE_NOT_FOUND') |
||
| 2128 | |||
| 2129 | cursor.execute(" SELECT id " |
||
| 2130 | " FROM tbl_spaces_stores " |
||
| 2131 | " WHERE space_id = %s AND store_id = %s ", (id_, tid)) |
||
| 2132 | if cursor.fetchone() is None: |
||
| 2133 | cursor.close() |
||
| 2134 | cnx.disconnect() |
||
| 2135 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2136 | description='API.SPACE_STORE_RELATION_NOT_FOUND') |
||
| 2137 | |||
| 2138 | cursor.execute(" DELETE FROM tbl_spaces_stores WHERE space_id = %s AND store_id = %s ", (id_, tid)) |
||
| 2139 | cnx.commit() |
||
| 2140 | |||
| 2141 | cursor.close() |
||
| 2142 | cnx.disconnect() |
||
| 2143 | |||
| 2144 | resp.status = falcon.HTTP_204 |
||
| 2145 | |||
| 2146 | |||
| 2147 | View Code Duplication | class SpaceTenantCollection: |
|
|
0 ignored issues
–
show
|
|||
| 2148 | @staticmethod |
||
| 2149 | def __init__(): |
||
| 2150 | pass |
||
| 2151 | |||
| 2152 | @staticmethod |
||
| 2153 | def on_options(req, resp, id_): |
||
| 2154 | resp.status = falcon.HTTP_200 |
||
| 2155 | |||
| 2156 | @staticmethod |
||
| 2157 | def on_get(req, resp, id_): |
||
| 2158 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2159 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2160 | description='API.INVALID_SPACE_ID') |
||
| 2161 | |||
| 2162 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2163 | cursor = cnx.cursor() |
||
| 2164 | |||
| 2165 | cursor.execute(" SELECT name " |
||
| 2166 | " FROM tbl_spaces " |
||
| 2167 | " WHERE id = %s ", (id_,)) |
||
| 2168 | if cursor.fetchone() is None: |
||
| 2169 | cursor.close() |
||
| 2170 | cnx.disconnect() |
||
| 2171 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2172 | description='API.SPACE_NOT_FOUND') |
||
| 2173 | |||
| 2174 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 2175 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
||
| 2176 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
||
| 2177 | " ORDER BY t.id ") |
||
| 2178 | cursor.execute(query, (id_,)) |
||
| 2179 | rows = cursor.fetchall() |
||
| 2180 | |||
| 2181 | result = list() |
||
| 2182 | if rows is not None and len(rows) > 0: |
||
| 2183 | for row in rows: |
||
| 2184 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2185 | result.append(meta_result) |
||
| 2186 | |||
| 2187 | resp.body = json.dumps(result) |
||
| 2188 | |||
| 2189 | @staticmethod |
||
| 2190 | def on_post(req, resp, id_): |
||
| 2191 | """Handles POST requests""" |
||
| 2192 | try: |
||
| 2193 | raw_json = req.stream.read().decode('utf-8') |
||
| 2194 | except Exception as ex: |
||
| 2195 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 2196 | |||
| 2197 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2198 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2199 | description='API.INVALID_SPACE_ID') |
||
| 2200 | |||
| 2201 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 2202 | |||
| 2203 | if 'tenant_id' not in new_values['data'].keys() or \ |
||
| 2204 | not isinstance(new_values['data']['tenant_id'], int) or \ |
||
| 2205 | new_values['data']['tenant_id'] <= 0: |
||
| 2206 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2207 | description='API.INVALID_TENANT_ID') |
||
| 2208 | tenant_id = new_values['data']['tenant_id'] |
||
| 2209 | |||
| 2210 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2211 | cursor = cnx.cursor() |
||
| 2212 | |||
| 2213 | cursor.execute(" SELECT name " |
||
| 2214 | " from tbl_spaces " |
||
| 2215 | " WHERE id = %s ", (id_,)) |
||
| 2216 | if cursor.fetchone() is None: |
||
| 2217 | cursor.close() |
||
| 2218 | cnx.disconnect() |
||
| 2219 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2220 | description='API.SPACE_NOT_FOUND') |
||
| 2221 | |||
| 2222 | cursor.execute(" SELECT name " |
||
| 2223 | " FROM tbl_tenants " |
||
| 2224 | " WHERE id = %s ", (tenant_id,)) |
||
| 2225 | if cursor.fetchone() is None: |
||
| 2226 | cursor.close() |
||
| 2227 | cnx.disconnect() |
||
| 2228 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2229 | description='API.TENANT_NOT_FOUND') |
||
| 2230 | |||
| 2231 | query = (" SELECT id " |
||
| 2232 | " FROM tbl_spaces_tenants " |
||
| 2233 | " WHERE space_id = %s AND tenant_id = %s") |
||
| 2234 | cursor.execute(query, (id_, tenant_id,)) |
||
| 2235 | if cursor.fetchone() is not None: |
||
| 2236 | cursor.close() |
||
| 2237 | cnx.disconnect() |
||
| 2238 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 2239 | description='API.SPACE_TENANT_RELATION_EXISTED') |
||
| 2240 | |||
| 2241 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
||
| 2242 | " VALUES (%s, %s) ") |
||
| 2243 | cursor.execute(add_row, (id_, tenant_id,)) |
||
| 2244 | new_id = cursor.lastrowid |
||
| 2245 | cnx.commit() |
||
| 2246 | cursor.close() |
||
| 2247 | cnx.disconnect() |
||
| 2248 | |||
| 2249 | resp.status = falcon.HTTP_201 |
||
| 2250 | resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id) |
||
| 2251 | |||
| 2252 | |||
| 2253 | View Code Duplication | class SpaceTenantItem: |
|
|
0 ignored issues
–
show
|
|||
| 2254 | @staticmethod |
||
| 2255 | def __init__(): |
||
| 2256 | pass |
||
| 2257 | |||
| 2258 | @staticmethod |
||
| 2259 | def on_options(req, resp, id_, tid): |
||
| 2260 | resp.status = falcon.HTTP_200 |
||
| 2261 | |||
| 2262 | @staticmethod |
||
| 2263 | def on_delete(req, resp, id_, tid): |
||
| 2264 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2265 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2266 | description='API.INVALID_SPACE_ID') |
||
| 2267 | |||
| 2268 | if not tid.isdigit() or int(tid) <= 0: |
||
| 2269 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2270 | description='API.INVALID_TENANT_ID') |
||
| 2271 | |||
| 2272 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2273 | cursor = cnx.cursor() |
||
| 2274 | |||
| 2275 | cursor.execute(" SELECT name " |
||
| 2276 | " FROM tbl_spaces " |
||
| 2277 | " WHERE id = %s ", (id_,)) |
||
| 2278 | if cursor.fetchone() is None: |
||
| 2279 | cursor.close() |
||
| 2280 | cnx.disconnect() |
||
| 2281 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2282 | description='API.SPACE_NOT_FOUND') |
||
| 2283 | |||
| 2284 | cursor.execute(" SELECT name " |
||
| 2285 | " FROM tbl_tenants " |
||
| 2286 | " WHERE id = %s ", (tid,)) |
||
| 2287 | if cursor.fetchone() is None: |
||
| 2288 | cursor.close() |
||
| 2289 | cnx.disconnect() |
||
| 2290 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2291 | description='API.TENANT_NOT_FOUND') |
||
| 2292 | |||
| 2293 | cursor.execute(" SELECT id " |
||
| 2294 | " FROM tbl_spaces_tenants " |
||
| 2295 | " WHERE space_id = %s AND tenant_id = %s ", (id_, tid)) |
||
| 2296 | if cursor.fetchone() is None: |
||
| 2297 | cursor.close() |
||
| 2298 | cnx.disconnect() |
||
| 2299 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2300 | description='API.SPACE_TENANT_RELATION_NOT_FOUND') |
||
| 2301 | |||
| 2302 | cursor.execute(" DELETE FROM tbl_spaces_tenants WHERE space_id = %s AND tenant_id = %s ", (id_, tid)) |
||
| 2303 | cnx.commit() |
||
| 2304 | |||
| 2305 | cursor.close() |
||
| 2306 | cnx.disconnect() |
||
| 2307 | |||
| 2308 | resp.status = falcon.HTTP_204 |
||
| 2309 | |||
| 2310 | |||
| 2311 | View Code Duplication | class SpaceVirtualMeterCollection: |
|
|
0 ignored issues
–
show
|
|||
| 2312 | @staticmethod |
||
| 2313 | def __init__(): |
||
| 2314 | pass |
||
| 2315 | |||
| 2316 | @staticmethod |
||
| 2317 | def on_options(req, resp, id_): |
||
| 2318 | resp.status = falcon.HTTP_200 |
||
| 2319 | |||
| 2320 | @staticmethod |
||
| 2321 | def on_get(req, resp, id_): |
||
| 2322 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2323 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2324 | description='API.INVALID_SPACE_ID') |
||
| 2325 | |||
| 2326 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2327 | cursor = cnx.cursor(dictionary=True) |
||
| 2328 | |||
| 2329 | cursor.execute(" SELECT name " |
||
| 2330 | " FROM tbl_spaces " |
||
| 2331 | " WHERE id = %s ", (id_,)) |
||
| 2332 | if cursor.fetchone() is None: |
||
| 2333 | cursor.close() |
||
| 2334 | cnx.disconnect() |
||
| 2335 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2336 | description='API.SPACE_NOT_FOUND') |
||
| 2337 | |||
| 2338 | query = (" SELECT id, name, uuid " |
||
| 2339 | " FROM tbl_energy_categories ") |
||
| 2340 | cursor.execute(query) |
||
| 2341 | rows_energy_categories = cursor.fetchall() |
||
| 2342 | |||
| 2343 | energy_category_dict = dict() |
||
| 2344 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2345 | for row in rows_energy_categories: |
||
| 2346 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 2347 | "name": row['name'], |
||
| 2348 | "uuid": row['uuid']} |
||
| 2349 | |||
| 2350 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2351 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
||
| 2352 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 2353 | " ORDER BY m.id ") |
||
| 2354 | cursor.execute(query, (id_,)) |
||
| 2355 | rows = cursor.fetchall() |
||
| 2356 | |||
| 2357 | result = list() |
||
| 2358 | if rows is not None and len(rows) > 0: |
||
| 2359 | for row in rows: |
||
| 2360 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 2361 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 2362 | "energy_category": energy_category} |
||
| 2363 | result.append(meta_result) |
||
| 2364 | |||
| 2365 | resp.body = json.dumps(result) |
||
| 2366 | |||
| 2367 | @staticmethod |
||
| 2368 | def on_post(req, resp, id_): |
||
| 2369 | """Handles POST requests""" |
||
| 2370 | try: |
||
| 2371 | raw_json = req.stream.read().decode('utf-8') |
||
| 2372 | except Exception as ex: |
||
| 2373 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 2374 | |||
| 2375 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2376 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2377 | description='API.INVALID_SPACE_ID') |
||
| 2378 | |||
| 2379 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 2380 | |||
| 2381 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 2382 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 2383 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 2384 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2385 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2386 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 2387 | |||
| 2388 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2389 | cursor = cnx.cursor() |
||
| 2390 | |||
| 2391 | cursor.execute(" SELECT name " |
||
| 2392 | " from tbl_spaces " |
||
| 2393 | " WHERE id = %s ", (id_,)) |
||
| 2394 | if cursor.fetchone() is None: |
||
| 2395 | cursor.close() |
||
| 2396 | cnx.disconnect() |
||
| 2397 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2398 | description='API.SPACE_NOT_FOUND') |
||
| 2399 | |||
| 2400 | cursor.execute(" SELECT name " |
||
| 2401 | " FROM tbl_virtual_meters " |
||
| 2402 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 2403 | if cursor.fetchone() is None: |
||
| 2404 | cursor.close() |
||
| 2405 | cnx.disconnect() |
||
| 2406 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2407 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2408 | |||
| 2409 | query = (" SELECT id " |
||
| 2410 | " FROM tbl_spaces_virtual_meters " |
||
| 2411 | " WHERE space_id = %s AND virtual_meter_id = %s") |
||
| 2412 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 2413 | if cursor.fetchone() is not None: |
||
| 2414 | cursor.close() |
||
| 2415 | cnx.disconnect() |
||
| 2416 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 2417 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTED') |
||
| 2418 | |||
| 2419 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
||
| 2420 | " VALUES (%s, %s) ") |
||
| 2421 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
||
| 2422 | new_id = cursor.lastrowid |
||
| 2423 | cnx.commit() |
||
| 2424 | cursor.close() |
||
| 2425 | cnx.disconnect() |
||
| 2426 | |||
| 2427 | resp.status = falcon.HTTP_201 |
||
| 2428 | resp.location = '/spaces/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 2429 | |||
| 2430 | |||
| 2431 | View Code Duplication | class SpaceVirtualMeterItem: |
|
|
0 ignored issues
–
show
|
|||
| 2432 | @staticmethod |
||
| 2433 | def __init__(): |
||
| 2434 | pass |
||
| 2435 | |||
| 2436 | @staticmethod |
||
| 2437 | def on_options(req, resp, id_, mid): |
||
| 2438 | resp.status = falcon.HTTP_200 |
||
| 2439 | |||
| 2440 | @staticmethod |
||
| 2441 | def on_delete(req, resp, id_, mid): |
||
| 2442 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2443 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2444 | description='API.INVALID_SPACE_ID') |
||
| 2445 | |||
| 2446 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2447 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2448 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 2449 | |||
| 2450 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2451 | cursor = cnx.cursor() |
||
| 2452 | |||
| 2453 | cursor.execute(" SELECT name " |
||
| 2454 | " FROM tbl_spaces " |
||
| 2455 | " WHERE id = %s ", (id_,)) |
||
| 2456 | if cursor.fetchone() is None: |
||
| 2457 | cursor.close() |
||
| 2458 | cnx.disconnect() |
||
| 2459 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2460 | description='API.SPACE_NOT_FOUND') |
||
| 2461 | |||
| 2462 | cursor.execute(" SELECT name " |
||
| 2463 | " FROM tbl_virtual_meters " |
||
| 2464 | " WHERE id = %s ", (mid,)) |
||
| 2465 | if cursor.fetchone() is None: |
||
| 2466 | cursor.close() |
||
| 2467 | cnx.disconnect() |
||
| 2468 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2469 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2470 | |||
| 2471 | cursor.execute(" SELECT id " |
||
| 2472 | " FROM tbl_spaces_virtual_meters " |
||
| 2473 | " WHERE space_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2474 | if cursor.fetchone() is None: |
||
| 2475 | cursor.close() |
||
| 2476 | cnx.disconnect() |
||
| 2477 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2478 | description='API.SPACE_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 2479 | |||
| 2480 | cursor.execute(" DELETE FROM tbl_spaces_virtual_meters " |
||
| 2481 | " WHERE space_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 2482 | cnx.commit() |
||
| 2483 | |||
| 2484 | cursor.close() |
||
| 2485 | cnx.disconnect() |
||
| 2486 | |||
| 2487 | resp.status = falcon.HTTP_204 |
||
| 2488 | |||
| 2489 | |||
| 2490 | class SpaceTreeCollection: |
||
| 2491 | @staticmethod |
||
| 2492 | def __init__(): |
||
| 2493 | pass |
||
| 2494 | |||
| 2495 | @staticmethod |
||
| 2496 | def on_options(req, resp): |
||
| 2497 | resp.status = falcon.HTTP_200 |
||
| 2498 | |||
| 2499 | @staticmethod |
||
| 2500 | def on_get(req, resp): |
||
| 2501 | if 'USER-UUID' not in req.headers or \ |
||
| 2502 | not isinstance(req.headers['USER-UUID'], str) or \ |
||
| 2503 | len(str.strip(req.headers['USER-UUID'])) == 0: |
||
| 2504 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2505 | description='API.INVALID_USER_UUID') |
||
| 2506 | user_uuid = str.strip(req.headers['USER-UUID']) |
||
| 2507 | |||
| 2508 | if 'TOKEN' not in req.headers or \ |
||
| 2509 | not isinstance(req.headers['TOKEN'], str) or \ |
||
| 2510 | len(str.strip(req.headers['TOKEN'])) == 0: |
||
| 2511 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2512 | description='API.INVALID_TOKEN') |
||
| 2513 | token = str.strip(req.headers['TOKEN']) |
||
| 2514 | |||
| 2515 | # Verify User Session |
||
| 2516 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 2517 | cursor = cnx.cursor() |
||
| 2518 | query = (" SELECT utc_expires " |
||
| 2519 | " FROM tbl_sessions " |
||
| 2520 | " WHERE user_uuid = %s AND token = %s") |
||
| 2521 | cursor.execute(query, (user_uuid, token,)) |
||
| 2522 | row = cursor.fetchone() |
||
| 2523 | |||
| 2524 | if row is None: |
||
| 2525 | cursor.close() |
||
| 2526 | cnx.disconnect() |
||
| 2527 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2528 | description='API.USER_SESSION_NOT_FOUND') |
||
| 2529 | else: |
||
| 2530 | utc_expires = row[0] |
||
| 2531 | if datetime.utcnow() > utc_expires: |
||
| 2532 | cursor.close() |
||
| 2533 | cnx.disconnect() |
||
| 2534 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2535 | description='API.USER_SESSION_TIMEOUT') |
||
| 2536 | # get privilege |
||
| 2537 | query = (" SELECT is_admin, privilege_id " |
||
| 2538 | " FROM tbl_users " |
||
| 2539 | " WHERE uuid = %s ") |
||
| 2540 | cursor.execute(query, (user_uuid,)) |
||
| 2541 | row = cursor.fetchone() |
||
| 2542 | if row is None: |
||
| 2543 | cursor.close() |
||
| 2544 | cnx.disconnect() |
||
| 2545 | raise falcon.HTTPError(falcon.HTTP_404, 'API.NOT_FOUND', 'API.USER_NOT_FOUND') |
||
| 2546 | else: |
||
| 2547 | is_admin = bool(row[0]) |
||
| 2548 | privilege_id = row[1] |
||
| 2549 | |||
| 2550 | # get space_id in privilege |
||
| 2551 | if is_admin: |
||
| 2552 | space_id = 1 |
||
| 2553 | elif privilege_id is None: |
||
| 2554 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2555 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 2556 | else: |
||
| 2557 | query = (" SELECT data " |
||
| 2558 | " FROM tbl_privileges " |
||
| 2559 | " WHERE id =%s ") |
||
| 2560 | cursor.execute(query, (privilege_id,)) |
||
| 2561 | row = cursor.fetchone() |
||
| 2562 | cursor.close() |
||
| 2563 | cnx.disconnect() |
||
| 2564 | |||
| 2565 | if row is None: |
||
| 2566 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2567 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 2568 | try: |
||
| 2569 | data = json.loads(row[0]) |
||
| 2570 | except Exception as ex: |
||
| 2571 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 2572 | |||
| 2573 | if 'spaces' not in data or len(data['spaces']) == 0: |
||
| 2574 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2575 | description='API.SPACE_NOT_FOUND_IN_PRIVILEGE') |
||
| 2576 | |||
| 2577 | space_id = data['spaces'][0] |
||
| 2578 | if space_id is None: |
||
| 2579 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2580 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 2581 | # get all spaces |
||
| 2582 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2583 | cursor = cnx.cursor(dictionary=True) |
||
| 2584 | |||
| 2585 | query = (" SELECT id, name, parent_space_id " |
||
| 2586 | " FROM tbl_spaces " |
||
| 2587 | " ORDER BY id ") |
||
| 2588 | cursor.execute(query) |
||
| 2589 | rows_spaces = cursor.fetchall() |
||
| 2590 | node_dict = dict() |
||
| 2591 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 2592 | for row in rows_spaces: |
||
| 2593 | parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
||
| 2594 | node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
||
| 2595 | |||
| 2596 | cursor.close() |
||
| 2597 | cnx.disconnect() |
||
| 2598 | resp.body = JsonExporter(sort_keys=True).export(node_dict[space_id], ) |
||
| 2599 |