| Total Complexity | 1126 |
| Total Lines | 5867 |
| Duplicated Lines | 58.04 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like core.space often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import uuid |
||
| 2 | from datetime import datetime, timedelta |
||
| 3 | import falcon |
||
| 4 | import mysql.connector |
||
| 5 | import simplejson as json |
||
| 6 | from anytree import AnyNode, LevelOrderIter |
||
| 7 | from anytree.exporter import JsonExporter |
||
| 8 | from core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
| 9 | import config |
||
| 10 | |||
| 11 | |||
| 12 | class SpaceCollection: |
||
| 13 | def __init__(self): |
||
| 14 | """Initializes Class""" |
||
| 15 | pass |
||
| 16 | |||
| 17 | @staticmethod |
||
| 18 | def on_options(req, resp): |
||
| 19 | resp.status = falcon.HTTP_200 |
||
| 20 | |||
| 21 | @staticmethod |
||
| 22 | def on_get(req, resp): |
||
| 23 | if 'API-KEY' not in req.headers or \ |
||
| 24 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 25 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 26 | access_control(req) |
||
| 27 | else: |
||
| 28 | api_key_control(req) |
||
| 29 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 30 | cursor = cnx.cursor() |
||
| 31 | |||
| 32 | query = (" SELECT id, name, uuid " |
||
| 33 | " FROM tbl_spaces ") |
||
| 34 | cursor.execute(query) |
||
| 35 | rows_spaces = cursor.fetchall() |
||
| 36 | |||
| 37 | space_dict = dict() |
||
| 38 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 39 | for row in rows_spaces: |
||
| 40 | space_dict[row[0]] = {"id": row[0], |
||
| 41 | "name": row[1], |
||
| 42 | "uuid": row[2]} |
||
| 43 | |||
| 44 | query = (" SELECT id, name, utc_offset " |
||
| 45 | " FROM tbl_timezones ") |
||
| 46 | cursor.execute(query) |
||
| 47 | rows_timezones = cursor.fetchall() |
||
| 48 | |||
| 49 | timezone_dict = dict() |
||
| 50 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 51 | for row in rows_timezones: |
||
| 52 | timezone_dict[row[0]] = {"id": row[0], |
||
| 53 | "name": row[1], |
||
| 54 | "utc_offset": row[2]} |
||
| 55 | |||
| 56 | query = (" SELECT id, name, uuid " |
||
| 57 | " FROM tbl_contacts ") |
||
| 58 | cursor.execute(query) |
||
| 59 | rows_contacts = cursor.fetchall() |
||
| 60 | |||
| 61 | contact_dict = dict() |
||
| 62 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 63 | for row in rows_contacts: |
||
| 64 | contact_dict[row[0]] = {"id": row[0], |
||
| 65 | "name": row[1], |
||
| 66 | "uuid": row[2]} |
||
| 67 | |||
| 68 | query = (" SELECT id, name, uuid " |
||
| 69 | " FROM tbl_cost_centers ") |
||
| 70 | cursor.execute(query) |
||
| 71 | rows_cost_centers = cursor.fetchall() |
||
| 72 | |||
| 73 | cost_center_dict = dict() |
||
| 74 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 75 | for row in rows_cost_centers: |
||
| 76 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 77 | "name": row[1], |
||
| 78 | "uuid": row[2]} |
||
| 79 | |||
| 80 | query = (" SELECT id, name, uuid, " |
||
| 81 | " parent_space_id, area, number_of_occupants, timezone_id, is_input_counted, is_output_counted, " |
||
| 82 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 83 | " FROM tbl_spaces " |
||
| 84 | " ORDER BY id ") |
||
| 85 | cursor.execute(query) |
||
| 86 | rows_spaces = cursor.fetchall() |
||
| 87 | |||
| 88 | result = list() |
||
| 89 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
|
|
|||
| 90 | for row in rows_spaces: |
||
| 91 | meta_result = {"id": row[0], |
||
| 92 | "name": row[1], |
||
| 93 | "uuid": row[2], |
||
| 94 | "parent_space": space_dict.get(row[3], None), |
||
| 95 | "area": row[4], |
||
| 96 | "number_of_occupants": row[5], |
||
| 97 | "timezone": timezone_dict.get(row[6], None), |
||
| 98 | "is_input_counted": bool(row[7]), |
||
| 99 | "is_output_counted": bool(row[8]), |
||
| 100 | "contact": contact_dict.get(row[9], None), |
||
| 101 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 102 | "latitude": row[11], |
||
| 103 | "longitude": row[12], |
||
| 104 | "description": row[13], |
||
| 105 | "qrcode": "space:" + row[2]} |
||
| 106 | result.append(meta_result) |
||
| 107 | |||
| 108 | cursor.close() |
||
| 109 | cnx.close() |
||
| 110 | resp.text = json.dumps(result) |
||
| 111 | |||
| 112 | @staticmethod |
||
| 113 | @user_logger |
||
| 114 | def on_post(req, resp): |
||
| 115 | """Handles POST requests""" |
||
| 116 | admin_control(req) |
||
| 117 | try: |
||
| 118 | raw_json = req.stream.read().decode('utf-8') |
||
| 119 | new_values = json.loads(raw_json) |
||
| 120 | except Exception as ex: |
||
| 121 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 122 | title='API.BAD_REQUEST', |
||
| 123 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 124 | |||
| 125 | if 'name' not in new_values['data'].keys() or \ |
||
| 126 | not isinstance(new_values['data']['name'], str) or \ |
||
| 127 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 129 | description='API.INVALID_SPACE_NAME') |
||
| 130 | name = str.strip(new_values['data']['name']) |
||
| 131 | |||
| 132 | if 'parent_space_id' in new_values['data'].keys(): |
||
| 133 | if new_values['data']['parent_space_id'] <= 0: |
||
| 134 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 135 | description='API.INVALID_PARENT_SPACE_ID') |
||
| 136 | parent_space_id = new_values['data']['parent_space_id'] |
||
| 137 | else: |
||
| 138 | parent_space_id = None |
||
| 139 | |||
| 140 | if 'area' not in new_values['data'].keys() or \ |
||
| 141 | not (isinstance(new_values['data']['area'], float) or |
||
| 142 | isinstance(new_values['data']['area'], int)) or \ |
||
| 143 | new_values['data']['area'] <= 0.0: |
||
| 144 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 145 | description='API.INVALID_AREA_VALUE') |
||
| 146 | area = new_values['data']['area'] |
||
| 147 | |||
| 148 | if 'number_of_occupants' not in new_values['data'].keys() or \ |
||
| 149 | not (isinstance(new_values['data']['number_of_occupants'], float) or |
||
| 150 | isinstance(new_values['data']['number_of_occupants'], int)) or \ |
||
| 151 | new_values['data']['number_of_occupants'] <= 0: |
||
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 153 | description='API.INVALID_NUMBER_OF_OCCUPANTS') |
||
| 154 | number_of_occupants = new_values['data']['number_of_occupants'] |
||
| 155 | |||
| 156 | if 'timezone_id' not in new_values['data'].keys() or \ |
||
| 157 | not isinstance(new_values['data']['timezone_id'], int) or \ |
||
| 158 | new_values['data']['timezone_id'] <= 0: |
||
| 159 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 160 | description='API.INVALID_TIMEZONE_ID') |
||
| 161 | timezone_id = new_values['data']['timezone_id'] |
||
| 162 | |||
| 163 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 164 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 166 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 167 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 168 | |||
| 169 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 170 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 171 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 172 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 173 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 174 | |||
| 175 | if 'contact_id' in new_values['data'].keys(): |
||
| 176 | if new_values['data']['contact_id'] <= 0: |
||
| 177 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 178 | description='API.INVALID_CONTACT_ID') |
||
| 179 | contact_id = new_values['data']['contact_id'] |
||
| 180 | else: |
||
| 181 | contact_id = None |
||
| 182 | |||
| 183 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 184 | if new_values['data']['cost_center_id'] <= 0: |
||
| 185 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 186 | description='API.INVALID_COST_CENTER_ID') |
||
| 187 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 188 | else: |
||
| 189 | cost_center_id = None |
||
| 190 | |||
| 191 | View Code Duplication | if 'latitude' in new_values['data'].keys() and new_values['data']['latitude'] is not None: |
|
| 192 | if not (isinstance(new_values['data']['latitude'], float) or |
||
| 193 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 194 | new_values['data']['latitude'] < -90.0 or \ |
||
| 195 | new_values['data']['latitude'] > 90.0: |
||
| 196 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 197 | description='API.INVALID_LATITUDE_VALUE') |
||
| 198 | latitude = new_values['data']['latitude'] |
||
| 199 | else: |
||
| 200 | latitude = None |
||
| 201 | |||
| 202 | View Code Duplication | if 'longitude' in new_values['data'].keys() and new_values['data']['longitude'] is not None: |
|
| 203 | if not (isinstance(new_values['data']['longitude'], float) or |
||
| 204 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 205 | new_values['data']['longitude'] < -180.0 or \ |
||
| 206 | new_values['data']['longitude'] > 180.0: |
||
| 207 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 208 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 209 | longitude = new_values['data']['longitude'] |
||
| 210 | else: |
||
| 211 | longitude = None |
||
| 212 | |||
| 213 | if 'description' in new_values['data'].keys() and \ |
||
| 214 | new_values['data']['description'] is not None and \ |
||
| 215 | len(str(new_values['data']['description'])) > 0: |
||
| 216 | description = str.strip(new_values['data']['description']) |
||
| 217 | else: |
||
| 218 | description = None |
||
| 219 | |||
| 220 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 221 | cursor = cnx.cursor() |
||
| 222 | |||
| 223 | cursor.execute(" SELECT name " |
||
| 224 | " FROM tbl_spaces " |
||
| 225 | " WHERE name = %s ", (name,)) |
||
| 226 | if cursor.fetchone() is not None: |
||
| 227 | cursor.close() |
||
| 228 | cnx.close() |
||
| 229 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 230 | description='API.SPACE_NAME_IS_ALREADY_IN_USE') |
||
| 231 | |||
| 232 | View Code Duplication | if parent_space_id is not None: |
|
| 233 | cursor.execute(" SELECT name " |
||
| 234 | " FROM tbl_spaces " |
||
| 235 | " WHERE id = %s ", |
||
| 236 | (new_values['data']['parent_space_id'],)) |
||
| 237 | row = cursor.fetchone() |
||
| 238 | if row is None: |
||
| 239 | cursor.close() |
||
| 240 | cnx.close() |
||
| 241 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 242 | description='API.PARENT_SPACE_NOT_FOUND') |
||
| 243 | |||
| 244 | cursor.execute(" SELECT name " |
||
| 245 | " FROM tbl_timezones " |
||
| 246 | " WHERE id = %s ", |
||
| 247 | (new_values['data']['timezone_id'],)) |
||
| 248 | if cursor.fetchone() is None: |
||
| 249 | cursor.close() |
||
| 250 | cnx.close() |
||
| 251 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 252 | description='API.TIMEZONE_NOT_FOUND') |
||
| 253 | View Code Duplication | if contact_id is not None: |
|
| 254 | cursor.execute(" SELECT name " |
||
| 255 | " FROM tbl_contacts " |
||
| 256 | " WHERE id = %s ", |
||
| 257 | (new_values['data']['contact_id'],)) |
||
| 258 | row = cursor.fetchone() |
||
| 259 | if row is None: |
||
| 260 | cursor.close() |
||
| 261 | cnx.close() |
||
| 262 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 263 | description='API.CONTACT_NOT_FOUND') |
||
| 264 | |||
| 265 | if cost_center_id is not None: |
||
| 266 | cursor.execute(" SELECT name " |
||
| 267 | " FROM tbl_cost_centers " |
||
| 268 | " WHERE id = %s ", |
||
| 269 | (new_values['data']['cost_center_id'],)) |
||
| 270 | row = cursor.fetchone() |
||
| 271 | if row is None: |
||
| 272 | cursor.close() |
||
| 273 | cnx.close() |
||
| 274 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 275 | description='API.COST_CENTER_NOT_FOUND') |
||
| 276 | |||
| 277 | add_values = (" INSERT INTO tbl_spaces " |
||
| 278 | " (name, uuid, parent_space_id, area, number_of_occupants, timezone_id, is_input_counted, " |
||
| 279 | " is_output_counted, contact_id, cost_center_id, latitude, longitude, description) " |
||
| 280 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 281 | cursor.execute(add_values, (name, |
||
| 282 | str(uuid.uuid4()), |
||
| 283 | parent_space_id, |
||
| 284 | area, |
||
| 285 | number_of_occupants, |
||
| 286 | timezone_id, |
||
| 287 | is_input_counted, |
||
| 288 | is_output_counted, |
||
| 289 | contact_id, |
||
| 290 | cost_center_id, |
||
| 291 | latitude, |
||
| 292 | longitude, |
||
| 293 | description)) |
||
| 294 | new_id = cursor.lastrowid |
||
| 295 | cnx.commit() |
||
| 296 | cursor.close() |
||
| 297 | cnx.close() |
||
| 298 | |||
| 299 | resp.status = falcon.HTTP_201 |
||
| 300 | resp.location = '/spaces/' + str(new_id) |
||
| 301 | |||
| 302 | |||
| 303 | class SpaceItem: |
||
| 304 | def __init__(self): |
||
| 305 | """Initializes Class""" |
||
| 306 | pass |
||
| 307 | |||
| 308 | @staticmethod |
||
| 309 | def on_options(req, resp, id_): |
||
| 310 | resp.status = falcon.HTTP_200 |
||
| 311 | |||
| 312 | @staticmethod |
||
| 313 | def on_get(req, resp, id_): |
||
| 314 | if 'API-KEY' not in req.headers or \ |
||
| 315 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 316 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 317 | access_control(req) |
||
| 318 | else: |
||
| 319 | api_key_control(req) |
||
| 320 | if not id_.isdigit() or int(id_) <= 0: |
||
| 321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 322 | description='API.INVALID_METER_ID') |
||
| 323 | |||
| 324 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 325 | cursor = cnx.cursor() |
||
| 326 | |||
| 327 | query = (" SELECT id, name, uuid " |
||
| 328 | " FROM tbl_spaces ") |
||
| 329 | cursor.execute(query) |
||
| 330 | rows_spaces = cursor.fetchall() |
||
| 331 | |||
| 332 | space_dict = dict() |
||
| 333 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 334 | for row in rows_spaces: |
||
| 335 | space_dict[row[0]] = {"id": row[0], |
||
| 336 | "name": row[1], |
||
| 337 | "uuid": row[2]} |
||
| 338 | |||
| 339 | query = (" SELECT id, name, utc_offset " |
||
| 340 | " FROM tbl_timezones ") |
||
| 341 | cursor.execute(query) |
||
| 342 | rows_timezones = cursor.fetchall() |
||
| 343 | |||
| 344 | timezone_dict = dict() |
||
| 345 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 346 | for row in rows_timezones: |
||
| 347 | timezone_dict[row[0]] = {"id": row[0], |
||
| 348 | "name": row[1], |
||
| 349 | "utc_offset": row[2]} |
||
| 350 | |||
| 351 | query = (" SELECT id, name, uuid " |
||
| 352 | " FROM tbl_contacts ") |
||
| 353 | cursor.execute(query) |
||
| 354 | rows_contacts = cursor.fetchall() |
||
| 355 | |||
| 356 | contact_dict = dict() |
||
| 357 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 358 | for row in rows_contacts: |
||
| 359 | contact_dict[row[0]] = {"id": row[0], |
||
| 360 | "name": row[1], |
||
| 361 | "uuid": row[2]} |
||
| 362 | |||
| 363 | query = (" SELECT id, name, uuid " |
||
| 364 | " FROM tbl_cost_centers ") |
||
| 365 | cursor.execute(query) |
||
| 366 | rows_cost_centers = cursor.fetchall() |
||
| 367 | |||
| 368 | cost_center_dict = dict() |
||
| 369 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 370 | for row in rows_cost_centers: |
||
| 371 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 372 | "name": row[1], |
||
| 373 | "uuid": row[2]} |
||
| 374 | |||
| 375 | query = (" SELECT id, name, uuid, " |
||
| 376 | " parent_space_id, area, number_of_occupants, timezone_id, is_input_counted, is_output_counted, " |
||
| 377 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 378 | " FROM tbl_spaces " |
||
| 379 | " WHERE id = %s ") |
||
| 380 | cursor.execute(query, (id_,)) |
||
| 381 | row = cursor.fetchone() |
||
| 382 | cursor.close() |
||
| 383 | cnx.close() |
||
| 384 | |||
| 385 | if row is None: |
||
| 386 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 387 | description='API.SPACE_NOT_FOUND') |
||
| 388 | else: |
||
| 389 | meta_result = {"id": row[0], |
||
| 390 | "name": row[1], |
||
| 391 | "uuid": row[2], |
||
| 392 | "parent_space_id": space_dict.get(row[3], None), |
||
| 393 | "area": row[4], |
||
| 394 | "number_of_occupants": row[5], |
||
| 395 | "timezone": timezone_dict.get(row[6], None), |
||
| 396 | "is_input_counted": bool(row[7]), |
||
| 397 | "is_output_counted": bool(row[8]), |
||
| 398 | "contact": contact_dict.get(row[9], None), |
||
| 399 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 400 | "latitude": row[11], |
||
| 401 | "longitude": row[12], |
||
| 402 | "description": row[13], |
||
| 403 | "qrcode": "space:" + row[2]} |
||
| 404 | |||
| 405 | resp.text = json.dumps(meta_result) |
||
| 406 | |||
| 407 | @staticmethod |
||
| 408 | @user_logger |
||
| 409 | def on_delete(req, resp, id_): |
||
| 410 | admin_control(req) |
||
| 411 | if not id_.isdigit() or int(id_) <= 0: |
||
| 412 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 413 | description='API.INVALID_SPACE_ID') |
||
| 414 | if int(id_) == 1: |
||
| 415 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 416 | description='API.THIS_SPACE_CANNOT_BE_DELETED') |
||
| 417 | |||
| 418 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 419 | cursor = cnx.cursor() |
||
| 420 | |||
| 421 | cursor.execute(" SELECT name " |
||
| 422 | " FROM tbl_spaces " |
||
| 423 | " WHERE id = %s ", (id_,)) |
||
| 424 | if cursor.fetchone() is None: |
||
| 425 | cursor.close() |
||
| 426 | cnx.close() |
||
| 427 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 428 | description='API.SPACE_NOT_FOUND') |
||
| 429 | |||
| 430 | # checkout relation with children spaces |
||
| 431 | cursor.execute(" SELECT id " |
||
| 432 | " FROM tbl_spaces " |
||
| 433 | " WHERE parent_space_id = %s ", |
||
| 434 | (id_,)) |
||
| 435 | rows_spaces = cursor.fetchall() |
||
| 436 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 437 | cursor.close() |
||
| 438 | cnx.close() |
||
| 439 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 440 | title='API.BAD_REQUEST', |
||
| 441 | description='API.THERE_IS_RELATION_WITH_CHILDREN_SPACES') |
||
| 442 | |||
| 443 | # delete relation with combined equipment |
||
| 444 | cursor.execute(" DELETE FROM tbl_spaces_combined_equipments WHERE space_id = %s ", (id_,)) |
||
| 445 | |||
| 446 | # delete relation with commands |
||
| 447 | cursor.execute(" DELETE FROM tbl_spaces_commands WHERE space_id = %s ", (id_,)) |
||
| 448 | |||
| 449 | # delete relation with equipments |
||
| 450 | cursor.execute(" DELETE FROM tbl_spaces_equipments WHERE space_id = %s ", (id_,)) |
||
| 451 | |||
| 452 | # delete relation with meters |
||
| 453 | cursor.execute(" DELETE FROM tbl_spaces_meters WHERE space_id = %s ", (id_,)) |
||
| 454 | |||
| 455 | # delete relation with offline meters |
||
| 456 | cursor.execute(" DELETE FROM tbl_spaces_offline_meters WHERE space_id = %s ", (id_,)) |
||
| 457 | |||
| 458 | # delete relation with points |
||
| 459 | cursor.execute(" DELETE FROM tbl_spaces_points WHERE space_id = %s ", (id_,)) |
||
| 460 | |||
| 461 | # delete relation with sensors |
||
| 462 | cursor.execute(" DELETE FROM tbl_spaces_sensors WHERE space_id = %s ", (id_,)) |
||
| 463 | |||
| 464 | # delete relation with shopfloors |
||
| 465 | cursor.execute(" DELETE FROM tbl_spaces_shopfloors WHERE space_id = %s ", (id_,)) |
||
| 466 | |||
| 467 | # delete relation with stores |
||
| 468 | cursor.execute(" DELETE FROM tbl_spaces_stores WHERE space_id = %s ", (id_,)) |
||
| 469 | |||
| 470 | # delete relation with tenants |
||
| 471 | cursor.execute(" DELETE FROM tbl_spaces_tenants WHERE space_id = %s ", (id_,)) |
||
| 472 | |||
| 473 | # delete relation with virtual meters |
||
| 474 | cursor.execute(" DELETE FROM tbl_spaces_virtual_meters WHERE space_id = %s ", (id_,)) |
||
| 475 | |||
| 476 | # delete relation with working calendars |
||
| 477 | cursor.execute(" DELETE FROM tbl_spaces_working_calendars WHERE space_id = %s ", (id_,)) |
||
| 478 | |||
| 479 | cursor.execute(" DELETE FROM tbl_spaces WHERE id = %s ", (id_,)) |
||
| 480 | cnx.commit() |
||
| 481 | |||
| 482 | cursor.close() |
||
| 483 | cnx.close() |
||
| 484 | |||
| 485 | resp.status = falcon.HTTP_204 |
||
| 486 | |||
| 487 | @staticmethod |
||
| 488 | @user_logger |
||
| 489 | def on_put(req, resp, id_): |
||
| 490 | """Handles PUT requests""" |
||
| 491 | admin_control(req) |
||
| 492 | try: |
||
| 493 | raw_json = req.stream.read().decode('utf-8') |
||
| 494 | except Exception as ex: |
||
| 495 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 496 | title='API.BAD_REQUEST', |
||
| 497 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 498 | |||
| 499 | if not id_.isdigit() or int(id_) <= 0: |
||
| 500 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 501 | description='API.INVALID_SPACE_ID') |
||
| 502 | |||
| 503 | new_values = json.loads(raw_json) |
||
| 504 | |||
| 505 | if 'name' not in new_values['data'].keys() or \ |
||
| 506 | not isinstance(new_values['data']['name'], str) or \ |
||
| 507 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 508 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 509 | description='API.INVALID_SPACE_NAME') |
||
| 510 | name = str.strip(new_values['data']['name']) |
||
| 511 | |||
| 512 | if int(id_) == 1: |
||
| 513 | parent_space_id = None |
||
| 514 | else: |
||
| 515 | if 'parent_space_id' not in new_values['data'].keys() or \ |
||
| 516 | new_values['data']['parent_space_id'] is None or \ |
||
| 517 | not isinstance(new_values['data']['parent_space_id'], int) or \ |
||
| 518 | int(new_values['data']['parent_space_id']) <= 0: |
||
| 519 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 520 | description='API.INVALID_TIMEZONE_ID') |
||
| 521 | parent_space_id = int(new_values['data']['parent_space_id']) |
||
| 522 | |||
| 523 | if 'area' not in new_values['data'].keys() or \ |
||
| 524 | not (isinstance(new_values['data']['area'], float) or |
||
| 525 | isinstance(new_values['data']['area'], int)) or \ |
||
| 526 | new_values['data']['area'] <= 0.0: |
||
| 527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 528 | description='API.INVALID_AREA_VALUE') |
||
| 529 | area = new_values['data']['area'] |
||
| 530 | |||
| 531 | if 'number_of_occupants' not in new_values['data'].keys() or \ |
||
| 532 | not (isinstance(new_values['data']['number_of_occupants'], float) or |
||
| 533 | isinstance(new_values['data']['number_of_occupants'], int)) or \ |
||
| 534 | new_values['data']['number_of_occupants'] <= 0: |
||
| 535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 536 | description='API.INVALID_NUMBER_OF_OCCUPANTS') |
||
| 537 | number_of_occupants = new_values['data']['number_of_occupants'] |
||
| 538 | |||
| 539 | if 'timezone_id' not in new_values['data'].keys() or \ |
||
| 540 | not isinstance(new_values['data']['timezone_id'], int) or \ |
||
| 541 | new_values['data']['timezone_id'] <= 0: |
||
| 542 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 543 | description='API.INVALID_TIMEZONE_ID') |
||
| 544 | timezone_id = new_values['data']['timezone_id'] |
||
| 545 | |||
| 546 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 547 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 548 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 549 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 550 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 551 | |||
| 552 | if 'is_output_counted' not in new_values['data'].keys() or \ |
||
| 553 | not isinstance(new_values['data']['is_output_counted'], bool): |
||
| 554 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 555 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 556 | is_output_counted = new_values['data']['is_output_counted'] |
||
| 557 | |||
| 558 | if 'contact_id' in new_values['data'].keys() and new_values['data']['contact_id'] is not None: |
||
| 559 | if new_values['data']['contact_id'] <= 0: |
||
| 560 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 561 | description='API.INVALID_CONTACT_ID') |
||
| 562 | contact_id = new_values['data']['contact_id'] |
||
| 563 | else: |
||
| 564 | contact_id = None |
||
| 565 | |||
| 566 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 567 | if new_values['data']['cost_center_id'] <= 0: |
||
| 568 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 569 | description='API.INVALID_COST_CENTER_ID') |
||
| 570 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 571 | else: |
||
| 572 | cost_center_id = None |
||
| 573 | |||
| 574 | View Code Duplication | if 'latitude' in new_values['data'].keys(): |
|
| 575 | if new_values['data']['latitude'] is not None: |
||
| 576 | if not (isinstance(new_values['data']['latitude'], float) or |
||
| 577 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 578 | new_values['data']['latitude'] < -90.0 or \ |
||
| 579 | new_values['data']['latitude'] > 90.0: |
||
| 580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 581 | description='API.INVALID_LATITUDE_VALUE') |
||
| 582 | latitude = new_values['data']['latitude'] |
||
| 583 | else: |
||
| 584 | latitude = None |
||
| 585 | else: |
||
| 586 | latitude = None |
||
| 587 | |||
| 588 | View Code Duplication | if 'longitude' in new_values['data'].keys(): |
|
| 589 | if new_values['data']['latitude'] is not None: |
||
| 590 | if not (isinstance(new_values['data']['longitude'], float) or |
||
| 591 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 592 | new_values['data']['longitude'] < -180.0 or \ |
||
| 593 | new_values['data']['longitude'] > 180.0: |
||
| 594 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 595 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 596 | longitude = new_values['data']['longitude'] |
||
| 597 | else: |
||
| 598 | longitude = None |
||
| 599 | else: |
||
| 600 | longitude = None |
||
| 601 | |||
| 602 | if 'description' in new_values['data'].keys() and \ |
||
| 603 | new_values['data']['description'] is not None and \ |
||
| 604 | len(str(new_values['data']['description'])) > 0: |
||
| 605 | description = str.strip(new_values['data']['description']) |
||
| 606 | else: |
||
| 607 | description = None |
||
| 608 | |||
| 609 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 610 | cursor = cnx.cursor() |
||
| 611 | |||
| 612 | cursor.execute(" SELECT name " |
||
| 613 | " FROM tbl_spaces " |
||
| 614 | " WHERE id = %s ", (id_,)) |
||
| 615 | if cursor.fetchone() is None: |
||
| 616 | cursor.close() |
||
| 617 | cnx.close() |
||
| 618 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 619 | description='API.SPACE_NOT_FOUND') |
||
| 620 | |||
| 621 | cursor.execute(" SELECT name " |
||
| 622 | " FROM tbl_spaces " |
||
| 623 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 624 | if cursor.fetchone() is not None: |
||
| 625 | cursor.close() |
||
| 626 | cnx.close() |
||
| 627 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 628 | description='API.SPACE_NAME_IS_ALREADY_IN_USE') |
||
| 629 | |||
| 630 | View Code Duplication | if parent_space_id is not None: |
|
| 631 | cursor.execute(" SELECT name " |
||
| 632 | " FROM tbl_spaces " |
||
| 633 | " WHERE id = %s ", |
||
| 634 | (new_values['data']['parent_space_id'],)) |
||
| 635 | row = cursor.fetchone() |
||
| 636 | if row is None: |
||
| 637 | cursor.close() |
||
| 638 | cnx.close() |
||
| 639 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 640 | description='API.PARENT_SPACE_NOT_FOUND') |
||
| 641 | |||
| 642 | cursor.execute(" SELECT name " |
||
| 643 | " FROM tbl_timezones " |
||
| 644 | " WHERE id = %s ", |
||
| 645 | (new_values['data']['timezone_id'],)) |
||
| 646 | if cursor.fetchone() is None: |
||
| 647 | cursor.close() |
||
| 648 | cnx.close() |
||
| 649 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 650 | description='API.TIMEZONE_NOT_FOUND') |
||
| 651 | View Code Duplication | if contact_id is not None: |
|
| 652 | cursor.execute(" SELECT name " |
||
| 653 | " FROM tbl_contacts " |
||
| 654 | " WHERE id = %s ", |
||
| 655 | (new_values['data']['contact_id'],)) |
||
| 656 | row = cursor.fetchone() |
||
| 657 | if row is None: |
||
| 658 | cursor.close() |
||
| 659 | cnx.close() |
||
| 660 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 661 | description='API.CONTACT_NOT_FOUND') |
||
| 662 | |||
| 663 | if cost_center_id is not None: |
||
| 664 | cursor.execute(" SELECT name " |
||
| 665 | " FROM tbl_cost_centers " |
||
| 666 | " WHERE id = %s ", |
||
| 667 | (new_values['data']['cost_center_id'],)) |
||
| 668 | row = cursor.fetchone() |
||
| 669 | if row is None: |
||
| 670 | cursor.close() |
||
| 671 | cnx.close() |
||
| 672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 673 | description='API.COST_CENTER_NOT_FOUND') |
||
| 674 | |||
| 675 | update_row = (" UPDATE tbl_spaces " |
||
| 676 | " SET name = %s, parent_space_id = %s, area = %s, number_of_occupants = %s, timezone_id = %s, " |
||
| 677 | " is_input_counted = %s, is_output_counted = %s, contact_id = %s, cost_center_id = %s, " |
||
| 678 | " latitude = %s, longitude = %s, description = %s " |
||
| 679 | " WHERE id = %s ") |
||
| 680 | cursor.execute(update_row, (name, |
||
| 681 | parent_space_id, |
||
| 682 | area, |
||
| 683 | number_of_occupants, |
||
| 684 | timezone_id, |
||
| 685 | is_input_counted, |
||
| 686 | is_output_counted, |
||
| 687 | contact_id, |
||
| 688 | cost_center_id, |
||
| 689 | latitude, |
||
| 690 | longitude, |
||
| 691 | description, |
||
| 692 | id_)) |
||
| 693 | cnx.commit() |
||
| 694 | |||
| 695 | cursor.close() |
||
| 696 | cnx.close() |
||
| 697 | |||
| 698 | resp.status = falcon.HTTP_200 |
||
| 699 | |||
| 700 | |||
| 701 | class SpaceChildrenCollection: |
||
| 702 | def __init__(self): |
||
| 703 | """Initializes Class""" |
||
| 704 | pass |
||
| 705 | |||
| 706 | @staticmethod |
||
| 707 | def on_options(req, resp, id_): |
||
| 708 | resp.status = falcon.HTTP_200 |
||
| 709 | |||
| 710 | @staticmethod |
||
| 711 | def on_get(req, resp, id_): |
||
| 712 | if 'API-KEY' not in req.headers or \ |
||
| 713 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 714 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 715 | access_control(req) |
||
| 716 | else: |
||
| 717 | api_key_control(req) |
||
| 718 | if not id_.isdigit() or int(id_) <= 0: |
||
| 719 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 720 | description='API.INVALID_SPACE_ID') |
||
| 721 | |||
| 722 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 723 | cursor = cnx.cursor() |
||
| 724 | |||
| 725 | query = (" SELECT id, name, uuid, " |
||
| 726 | " parent_space_id, area, number_of_occupants, timezone_id, is_input_counted, is_output_counted, " |
||
| 727 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 728 | " FROM tbl_spaces " |
||
| 729 | " WHERE id = %s ") |
||
| 730 | cursor.execute(query, (id_,)) |
||
| 731 | row_current_space = cursor.fetchone() |
||
| 732 | if row_current_space is None: |
||
| 733 | cursor.close() |
||
| 734 | cnx.close() |
||
| 735 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 736 | description='API.SPACE_NOT_FOUND') |
||
| 737 | # note: row_current_space will be used at the end |
||
| 738 | |||
| 739 | query = (" SELECT id, name, uuid " |
||
| 740 | " FROM tbl_spaces ") |
||
| 741 | cursor.execute(query) |
||
| 742 | rows_spaces = cursor.fetchall() |
||
| 743 | |||
| 744 | space_dict = dict() |
||
| 745 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 746 | for row in rows_spaces: |
||
| 747 | space_dict[row[0]] = {"id": row[0], |
||
| 748 | "name": row[1], |
||
| 749 | "uuid": row[2]} |
||
| 750 | |||
| 751 | query = (" SELECT id, name, utc_offset " |
||
| 752 | " FROM tbl_timezones ") |
||
| 753 | cursor.execute(query) |
||
| 754 | rows_timezones = cursor.fetchall() |
||
| 755 | |||
| 756 | timezone_dict = dict() |
||
| 757 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 758 | for row in rows_timezones: |
||
| 759 | timezone_dict[row[0]] = {"id": row[0], |
||
| 760 | "name": row[1], |
||
| 761 | "utc_offset": row[2]} |
||
| 762 | |||
| 763 | query = (" SELECT id, name, uuid " |
||
| 764 | " FROM tbl_contacts ") |
||
| 765 | cursor.execute(query) |
||
| 766 | rows_contacts = cursor.fetchall() |
||
| 767 | |||
| 768 | contact_dict = dict() |
||
| 769 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 770 | for row in rows_contacts: |
||
| 771 | contact_dict[row[0]] = {"id": row[0], |
||
| 772 | "name": row[1], |
||
| 773 | "uuid": row[2]} |
||
| 774 | |||
| 775 | query = (" SELECT id, name, uuid " |
||
| 776 | " FROM tbl_cost_centers ") |
||
| 777 | cursor.execute(query) |
||
| 778 | rows_cost_centers = cursor.fetchall() |
||
| 779 | |||
| 780 | cost_center_dict = dict() |
||
| 781 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 782 | for row in rows_cost_centers: |
||
| 783 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 784 | "name": row[1], |
||
| 785 | "uuid": row[2]} |
||
| 786 | result = dict() |
||
| 787 | result['current'] = dict() |
||
| 788 | result['current']['id'] = row_current_space[0] |
||
| 789 | result['current']['name'] = row_current_space[1] |
||
| 790 | result['current']['uuid'] = row_current_space[2] |
||
| 791 | result['current']['parent_space'] = space_dict.get(row_current_space[3], None) |
||
| 792 | result['current']['area'] = row_current_space[4] |
||
| 793 | result['current']['number_of_occupants'] = row_current_space[5] |
||
| 794 | result['current']['timezone'] = timezone_dict.get(row_current_space[6], None) |
||
| 795 | result['current']['is_input_counted'] = bool(row_current_space[7]) |
||
| 796 | result['current']['is_output_counted'] = bool(row_current_space[8]) |
||
| 797 | result['current']['contact'] = contact_dict.get(row_current_space[9], None) |
||
| 798 | result['current']['cost_center'] = cost_center_dict.get(row_current_space[10], None) |
||
| 799 | result['current']['latitude'] = row_current_space[11] |
||
| 800 | result['current']['longitude'] = row_current_space[12] |
||
| 801 | result['current']['description'] = row_current_space[13] |
||
| 802 | result['current']['qrcode'] = 'space:' + row_current_space[2] |
||
| 803 | |||
| 804 | result['children'] = list() |
||
| 805 | |||
| 806 | query = (" SELECT id, name, uuid, " |
||
| 807 | " parent_space_id, area, number_of_occupants, timezone_id, is_input_counted, is_output_counted, " |
||
| 808 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 809 | " FROM tbl_spaces " |
||
| 810 | " WHERE parent_space_id = %s " |
||
| 811 | " ORDER BY id ") |
||
| 812 | cursor.execute(query, (id_, )) |
||
| 813 | rows_spaces = cursor.fetchall() |
||
| 814 | |||
| 815 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
| 816 | for row in rows_spaces: |
||
| 817 | meta_result = {"id": row[0], |
||
| 818 | "name": row[1], |
||
| 819 | "uuid": row[2], |
||
| 820 | "parent_space": space_dict.get(row[3], None), |
||
| 821 | "area": row[4], |
||
| 822 | "number_of_occupants": row[5], |
||
| 823 | "timezone": timezone_dict.get(row[6], None), |
||
| 824 | "is_input_counted": bool(row[7]), |
||
| 825 | "is_output_counted": bool(row[8]), |
||
| 826 | "contact": contact_dict.get(row[9], None), |
||
| 827 | "cost_center": cost_center_dict.get(row[10], None), |
||
| 828 | "latitude": row[11], |
||
| 829 | "longitude": row[12], |
||
| 830 | "description": row[13], |
||
| 831 | "qrcode": 'space:' + row[2]} |
||
| 832 | result['children'].append(meta_result) |
||
| 833 | |||
| 834 | cursor.close() |
||
| 835 | cnx.close() |
||
| 836 | resp.text = json.dumps(result) |
||
| 837 | |||
| 838 | |||
| 839 | View Code Duplication | class SpaceCombinedEquipmentCollection: |
|
| 840 | def __init__(self): |
||
| 841 | """Initializes Class""" |
||
| 842 | pass |
||
| 843 | |||
| 844 | @staticmethod |
||
| 845 | def on_options(req, resp, id_): |
||
| 846 | resp.status = falcon.HTTP_200 |
||
| 847 | |||
| 848 | @staticmethod |
||
| 849 | def on_get(req, resp, id_): |
||
| 850 | if 'API-KEY' not in req.headers or \ |
||
| 851 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 852 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 853 | access_control(req) |
||
| 854 | else: |
||
| 855 | api_key_control(req) |
||
| 856 | if not id_.isdigit() or int(id_) <= 0: |
||
| 857 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 858 | description='API.INVALID_SPACE_ID') |
||
| 859 | |||
| 860 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 861 | cursor = cnx.cursor() |
||
| 862 | |||
| 863 | cursor.execute(" SELECT name " |
||
| 864 | " FROM tbl_spaces " |
||
| 865 | " WHERE id = %s ", (id_,)) |
||
| 866 | if cursor.fetchone() is None: |
||
| 867 | cursor.close() |
||
| 868 | cnx.close() |
||
| 869 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 870 | description='API.SPACE_NOT_FOUND') |
||
| 871 | |||
| 872 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 873 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
||
| 874 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
||
| 875 | " ORDER BY e.id ") |
||
| 876 | cursor.execute(query, (id_,)) |
||
| 877 | rows = cursor.fetchall() |
||
| 878 | |||
| 879 | result = list() |
||
| 880 | if rows is not None and len(rows) > 0: |
||
| 881 | for row in rows: |
||
| 882 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 883 | result.append(meta_result) |
||
| 884 | |||
| 885 | resp.text = json.dumps(result) |
||
| 886 | |||
| 887 | @staticmethod |
||
| 888 | @user_logger |
||
| 889 | def on_post(req, resp, id_): |
||
| 890 | """Handles POST requests""" |
||
| 891 | admin_control(req) |
||
| 892 | try: |
||
| 893 | raw_json = req.stream.read().decode('utf-8') |
||
| 894 | except Exception as ex: |
||
| 895 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 896 | title='API.BAD_REQUEST', |
||
| 897 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 898 | |||
| 899 | if not id_.isdigit() or int(id_) <= 0: |
||
| 900 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 901 | description='API.INVALID_SPACE_ID') |
||
| 902 | |||
| 903 | new_values = json.loads(raw_json) |
||
| 904 | |||
| 905 | if 'combined_equipment_id' not in new_values['data'].keys() or \ |
||
| 906 | not isinstance(new_values['data']['combined_equipment_id'], int) or \ |
||
| 907 | new_values['data']['combined_equipment_id'] <= 0: |
||
| 908 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 909 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 910 | combined_equipment_id = new_values['data']['combined_equipment_id'] |
||
| 911 | |||
| 912 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 913 | cursor = cnx.cursor() |
||
| 914 | |||
| 915 | cursor.execute(" SELECT name " |
||
| 916 | " from tbl_spaces " |
||
| 917 | " WHERE id = %s ", (id_,)) |
||
| 918 | if cursor.fetchone() is None: |
||
| 919 | cursor.close() |
||
| 920 | cnx.close() |
||
| 921 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 922 | description='API.SPACE_NOT_FOUND') |
||
| 923 | |||
| 924 | cursor.execute(" SELECT name " |
||
| 925 | " FROM tbl_combined_equipments " |
||
| 926 | " WHERE id = %s ", (combined_equipment_id,)) |
||
| 927 | if cursor.fetchone() is None: |
||
| 928 | cursor.close() |
||
| 929 | cnx.close() |
||
| 930 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 931 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 932 | |||
| 933 | query = (" SELECT id " |
||
| 934 | " FROM tbl_spaces_combined_equipments " |
||
| 935 | " WHERE space_id = %s AND combined_equipment_id = %s") |
||
| 936 | cursor.execute(query, (id_, combined_equipment_id,)) |
||
| 937 | if cursor.fetchone() is not None: |
||
| 938 | cursor.close() |
||
| 939 | cnx.close() |
||
| 940 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 941 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
||
| 942 | |||
| 943 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
||
| 944 | " VALUES (%s, %s) ") |
||
| 945 | cursor.execute(add_row, (id_, combined_equipment_id,)) |
||
| 946 | cnx.commit() |
||
| 947 | cursor.close() |
||
| 948 | cnx.close() |
||
| 949 | |||
| 950 | resp.status = falcon.HTTP_201 |
||
| 951 | resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id) |
||
| 952 | |||
| 953 | |||
| 954 | View Code Duplication | class SpaceCombinedEquipmentItem: |
|
| 955 | def __init__(self): |
||
| 956 | """Initializes Class""" |
||
| 957 | pass |
||
| 958 | |||
| 959 | @staticmethod |
||
| 960 | def on_options(req, resp, id_, eid): |
||
| 961 | resp.status = falcon.HTTP_200 |
||
| 962 | |||
| 963 | @staticmethod |
||
| 964 | @user_logger |
||
| 965 | def on_delete(req, resp, id_, eid): |
||
| 966 | admin_control(req) |
||
| 967 | if not id_.isdigit() or int(id_) <= 0: |
||
| 968 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 969 | description='API.INVALID_SPACE_ID') |
||
| 970 | |||
| 971 | if not eid.isdigit() or int(eid) <= 0: |
||
| 972 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 973 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
||
| 974 | |||
| 975 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 976 | cursor = cnx.cursor() |
||
| 977 | |||
| 978 | cursor.execute(" SELECT name " |
||
| 979 | " FROM tbl_spaces " |
||
| 980 | " WHERE id = %s ", (id_,)) |
||
| 981 | if cursor.fetchone() is None: |
||
| 982 | cursor.close() |
||
| 983 | cnx.close() |
||
| 984 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 985 | description='API.SPACE_NOT_FOUND') |
||
| 986 | |||
| 987 | cursor.execute(" SELECT name " |
||
| 988 | " FROM tbl_combined_equipments " |
||
| 989 | " WHERE id = %s ", (eid,)) |
||
| 990 | if cursor.fetchone() is None: |
||
| 991 | cursor.close() |
||
| 992 | cnx.close() |
||
| 993 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 994 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 995 | |||
| 996 | cursor.execute(" SELECT id " |
||
| 997 | " FROM tbl_spaces_combined_equipments " |
||
| 998 | " WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid)) |
||
| 999 | if cursor.fetchone() is None: |
||
| 1000 | cursor.close() |
||
| 1001 | cnx.close() |
||
| 1002 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1003 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 1004 | |||
| 1005 | cursor.execute(" DELETE FROM tbl_spaces_combined_equipments " |
||
| 1006 | " WHERE space_id = %s AND combined_equipment_id = %s ", (id_, eid)) |
||
| 1007 | cnx.commit() |
||
| 1008 | |||
| 1009 | cursor.close() |
||
| 1010 | cnx.close() |
||
| 1011 | |||
| 1012 | resp.status = falcon.HTTP_204 |
||
| 1013 | |||
| 1014 | |||
| 1015 | View Code Duplication | class SpaceEnergyStoragePowerStationCollection: |
|
| 1016 | def __init__(self): |
||
| 1017 | """Initializes Class""" |
||
| 1018 | pass |
||
| 1019 | |||
| 1020 | @staticmethod |
||
| 1021 | def on_options(req, resp, id_): |
||
| 1022 | resp.status = falcon.HTTP_200 |
||
| 1023 | |||
| 1024 | @staticmethod |
||
| 1025 | def on_get(req, resp, id_): |
||
| 1026 | # NOTE: DO NOT allow to be called by api_key |
||
| 1027 | access_control(req) |
||
| 1028 | |||
| 1029 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1030 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1031 | description='API.INVALID_SPACE_ID') |
||
| 1032 | |||
| 1033 | if 'USER-UUID' not in req.headers or \ |
||
| 1034 | not isinstance(req.headers['USER-UUID'], str) or \ |
||
| 1035 | len(str.strip(req.headers['USER-UUID'])) == 0: |
||
| 1036 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1037 | description='API.INVALID_USER_UUID') |
||
| 1038 | user_uuid = str.strip(req.headers['USER-UUID']) |
||
| 1039 | |||
| 1040 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 1041 | cursor_user = cnx_user.cursor() |
||
| 1042 | cursor_user.execute(" SELECT id FROM tbl_users WHERE uuid = %s ", (user_uuid,)) |
||
| 1043 | row_user = cursor_user.fetchone() |
||
| 1044 | if row_user is None or len(row_user) != 1: |
||
| 1045 | cursor_user.close() |
||
| 1046 | cnx_user.close() |
||
| 1047 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1048 | description='API.USER_NOT_FOUND') |
||
| 1049 | else: |
||
| 1050 | user_id = row_user[0] |
||
| 1051 | |||
| 1052 | # get privilege |
||
| 1053 | query = (" SELECT is_admin " |
||
| 1054 | " FROM tbl_users " |
||
| 1055 | " WHERE uuid = %s ") |
||
| 1056 | cursor_user.execute(query, (user_uuid,)) |
||
| 1057 | row = cursor_user.fetchone() |
||
| 1058 | if row is None: |
||
| 1059 | cursor_user.close() |
||
| 1060 | cnx_user.close() |
||
| 1061 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.USER_NOT_FOUND') |
||
| 1062 | else: |
||
| 1063 | is_admin = bool(row[0]) |
||
| 1064 | |||
| 1065 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1066 | cursor = cnx.cursor() |
||
| 1067 | |||
| 1068 | cursor.execute(" SELECT name " |
||
| 1069 | " FROM tbl_spaces " |
||
| 1070 | " WHERE id = %s ", (id_,)) |
||
| 1071 | if cursor.fetchone() is None: |
||
| 1072 | cursor.close() |
||
| 1073 | cnx.close() |
||
| 1074 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1075 | description='API.SPACE_NOT_FOUND') |
||
| 1076 | |||
| 1077 | if is_admin: |
||
| 1078 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 1079 | " FROM tbl_spaces s, tbl_spaces_energy_storage_power_stations se, " |
||
| 1080 | " tbl_energy_storage_power_stations e " |
||
| 1081 | " WHERE se.space_id = s.id AND e.id = se.energy_storage_power_station_id AND s.id = %s " |
||
| 1082 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 1083 | cursor.execute(query, (id_,)) |
||
| 1084 | else: |
||
| 1085 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 1086 | " FROM tbl_spaces s, tbl_spaces_energy_storage_power_stations se, " |
||
| 1087 | " tbl_energy_storage_power_stations e, tbl_energy_storage_power_stations_users su " |
||
| 1088 | " WHERE se.space_id = s.id AND e.id = se.energy_storage_power_station_id AND " |
||
| 1089 | " e.id = su.energy_storage_power_station_id AND s.id = %s AND su.user_id = %s " |
||
| 1090 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 1091 | cursor.execute(query, (id_, user_id)) |
||
| 1092 | rows = cursor.fetchall() |
||
| 1093 | |||
| 1094 | result = list() |
||
| 1095 | if rows is not None and len(rows) > 0: |
||
| 1096 | for row in rows: |
||
| 1097 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1098 | result.append(meta_result) |
||
| 1099 | |||
| 1100 | resp.text = json.dumps(result) |
||
| 1101 | |||
| 1102 | @staticmethod |
||
| 1103 | @user_logger |
||
| 1104 | def on_post(req, resp, id_): |
||
| 1105 | """Handles POST requests""" |
||
| 1106 | admin_control(req) |
||
| 1107 | try: |
||
| 1108 | raw_json = req.stream.read().decode('utf-8') |
||
| 1109 | except Exception as ex: |
||
| 1110 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1111 | title='API.BAD_REQUEST', |
||
| 1112 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1113 | |||
| 1114 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1115 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1116 | description='API.INVALID_SPACE_ID') |
||
| 1117 | |||
| 1118 | new_values = json.loads(raw_json) |
||
| 1119 | |||
| 1120 | if 'energy_storage_power_station_id' not in new_values['data'].keys() or \ |
||
| 1121 | not isinstance(new_values['data']['energy_storage_power_station_id'], int) or \ |
||
| 1122 | new_values['data']['energy_storage_power_station_id'] <= 0: |
||
| 1123 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1124 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1125 | energy_storage_power_station_id = new_values['data']['energy_storage_power_station_id'] |
||
| 1126 | |||
| 1127 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1128 | cursor = cnx.cursor() |
||
| 1129 | |||
| 1130 | cursor.execute(" SELECT name " |
||
| 1131 | " from tbl_spaces " |
||
| 1132 | " WHERE id = %s ", (id_,)) |
||
| 1133 | if cursor.fetchone() is None: |
||
| 1134 | cursor.close() |
||
| 1135 | cnx.close() |
||
| 1136 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1137 | description='API.SPACE_NOT_FOUND') |
||
| 1138 | |||
| 1139 | cursor.execute(" SELECT name " |
||
| 1140 | " FROM tbl_energy_storage_power_stations " |
||
| 1141 | " WHERE id = %s ", (energy_storage_power_station_id,)) |
||
| 1142 | if cursor.fetchone() is None: |
||
| 1143 | cursor.close() |
||
| 1144 | cnx.close() |
||
| 1145 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1146 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1147 | |||
| 1148 | query = (" SELECT id " |
||
| 1149 | " FROM tbl_spaces_energy_storage_power_stations " |
||
| 1150 | " WHERE space_id = %s AND energy_storage_power_station_id = %s") |
||
| 1151 | cursor.execute(query, (id_, energy_storage_power_station_id,)) |
||
| 1152 | if cursor.fetchone() is not None: |
||
| 1153 | cursor.close() |
||
| 1154 | cnx.close() |
||
| 1155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1156 | description='API.SPACE_ENERGY_STORAGE_POWER_STATION_RELATION_EXISTS') |
||
| 1157 | |||
| 1158 | add_row = (" INSERT INTO tbl_spaces_energy_storage_power_stations (space_id, energy_storage_power_station_id) " |
||
| 1159 | " VALUES (%s, %s) ") |
||
| 1160 | cursor.execute(add_row, (id_, energy_storage_power_station_id,)) |
||
| 1161 | cnx.commit() |
||
| 1162 | cursor.close() |
||
| 1163 | cnx.close() |
||
| 1164 | |||
| 1165 | resp.status = falcon.HTTP_201 |
||
| 1166 | resp.location = '/spaces/' + str(id_) + '/energystoragepowerstations/' + str(energy_storage_power_station_id) |
||
| 1167 | |||
| 1168 | |||
| 1169 | View Code Duplication | class SpaceEnergyStoragePowerStationItem: |
|
| 1170 | def __init__(self): |
||
| 1171 | """Initializes Class""" |
||
| 1172 | pass |
||
| 1173 | |||
| 1174 | @staticmethod |
||
| 1175 | def on_options(req, resp, id_, eid): |
||
| 1176 | resp.status = falcon.HTTP_200 |
||
| 1177 | |||
| 1178 | @staticmethod |
||
| 1179 | @user_logger |
||
| 1180 | def on_delete(req, resp, id_, eid): |
||
| 1181 | admin_control(req) |
||
| 1182 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1183 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1184 | description='API.INVALID_SPACE_ID') |
||
| 1185 | |||
| 1186 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1187 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1188 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
| 1189 | |||
| 1190 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1191 | cursor = cnx.cursor() |
||
| 1192 | |||
| 1193 | cursor.execute(" SELECT name " |
||
| 1194 | " FROM tbl_spaces " |
||
| 1195 | " WHERE id = %s ", (id_,)) |
||
| 1196 | if cursor.fetchone() is None: |
||
| 1197 | cursor.close() |
||
| 1198 | cnx.close() |
||
| 1199 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1200 | description='API.SPACE_NOT_FOUND') |
||
| 1201 | |||
| 1202 | cursor.execute(" SELECT name " |
||
| 1203 | " FROM tbl_energy_storage_power_stations " |
||
| 1204 | " WHERE id = %s ", (eid,)) |
||
| 1205 | if cursor.fetchone() is None: |
||
| 1206 | cursor.close() |
||
| 1207 | cnx.close() |
||
| 1208 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1209 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
| 1210 | |||
| 1211 | cursor.execute(" SELECT id " |
||
| 1212 | " FROM tbl_spaces_energy_storage_power_stations " |
||
| 1213 | " WHERE space_id = %s AND energy_storage_power_station_id = %s ", (id_, eid)) |
||
| 1214 | if cursor.fetchone() is None: |
||
| 1215 | cursor.close() |
||
| 1216 | cnx.close() |
||
| 1217 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1218 | description='API.SPACE_ENERGY_STORAGE_POWER_STATION_RELATION_NOT_FOUND') |
||
| 1219 | |||
| 1220 | cursor.execute(" DELETE FROM tbl_spaces_energy_storage_power_stations " |
||
| 1221 | " WHERE space_id = %s AND energy_storage_power_station_id = %s ", (id_, eid)) |
||
| 1222 | cnx.commit() |
||
| 1223 | |||
| 1224 | cursor.close() |
||
| 1225 | cnx.close() |
||
| 1226 | |||
| 1227 | resp.status = falcon.HTTP_204 |
||
| 1228 | |||
| 1229 | |||
| 1230 | View Code Duplication | class SpaceEquipmentCollection: |
|
| 1231 | def __init__(self): |
||
| 1232 | """Initializes Class""" |
||
| 1233 | pass |
||
| 1234 | |||
| 1235 | @staticmethod |
||
| 1236 | def on_options(req, resp, id_): |
||
| 1237 | resp.status = falcon.HTTP_200 |
||
| 1238 | |||
| 1239 | @staticmethod |
||
| 1240 | def on_get(req, resp, id_): |
||
| 1241 | if 'API-KEY' not in req.headers or \ |
||
| 1242 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1243 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1244 | access_control(req) |
||
| 1245 | else: |
||
| 1246 | api_key_control(req) |
||
| 1247 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1249 | description='API.INVALID_SPACE_ID') |
||
| 1250 | |||
| 1251 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1252 | cursor = cnx.cursor() |
||
| 1253 | |||
| 1254 | cursor.execute(" SELECT name " |
||
| 1255 | " FROM tbl_spaces " |
||
| 1256 | " WHERE id = %s ", (id_,)) |
||
| 1257 | if cursor.fetchone() is None: |
||
| 1258 | cursor.close() |
||
| 1259 | cnx.close() |
||
| 1260 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1261 | description='API.SPACE_NOT_FOUND') |
||
| 1262 | |||
| 1263 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 1264 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
||
| 1265 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 1266 | " ORDER BY e.id ") |
||
| 1267 | cursor.execute(query, (id_,)) |
||
| 1268 | rows = cursor.fetchall() |
||
| 1269 | |||
| 1270 | result = list() |
||
| 1271 | if rows is not None and len(rows) > 0: |
||
| 1272 | for row in rows: |
||
| 1273 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1274 | result.append(meta_result) |
||
| 1275 | |||
| 1276 | resp.text = json.dumps(result) |
||
| 1277 | |||
| 1278 | @staticmethod |
||
| 1279 | @user_logger |
||
| 1280 | def on_post(req, resp, id_): |
||
| 1281 | """Handles POST requests""" |
||
| 1282 | admin_control(req) |
||
| 1283 | try: |
||
| 1284 | raw_json = req.stream.read().decode('utf-8') |
||
| 1285 | except Exception as ex: |
||
| 1286 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1287 | title='API.BAD_REQUEST', |
||
| 1288 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1289 | |||
| 1290 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1291 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1292 | description='API.INVALID_SPACE_ID') |
||
| 1293 | |||
| 1294 | new_values = json.loads(raw_json) |
||
| 1295 | |||
| 1296 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
| 1297 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
| 1298 | new_values['data']['equipment_id'] <= 0: |
||
| 1299 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1300 | description='API.INVALID_EQUIPMENT_ID') |
||
| 1301 | equipment_id = new_values['data']['equipment_id'] |
||
| 1302 | |||
| 1303 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1304 | cursor = cnx.cursor() |
||
| 1305 | |||
| 1306 | cursor.execute(" SELECT name " |
||
| 1307 | " from tbl_spaces " |
||
| 1308 | " WHERE id = %s ", (id_,)) |
||
| 1309 | if cursor.fetchone() is None: |
||
| 1310 | cursor.close() |
||
| 1311 | cnx.close() |
||
| 1312 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1313 | description='API.SPACE_NOT_FOUND') |
||
| 1314 | |||
| 1315 | cursor.execute(" SELECT name " |
||
| 1316 | " FROM tbl_equipments " |
||
| 1317 | " WHERE id = %s ", (equipment_id,)) |
||
| 1318 | if cursor.fetchone() is None: |
||
| 1319 | cursor.close() |
||
| 1320 | cnx.close() |
||
| 1321 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1322 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 1323 | |||
| 1324 | query = (" SELECT id " |
||
| 1325 | " FROM tbl_spaces_equipments " |
||
| 1326 | " WHERE space_id = %s AND equipment_id = %s") |
||
| 1327 | cursor.execute(query, (id_, equipment_id,)) |
||
| 1328 | if cursor.fetchone() is not None: |
||
| 1329 | cursor.close() |
||
| 1330 | cnx.close() |
||
| 1331 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1332 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
||
| 1333 | |||
| 1334 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
||
| 1335 | " VALUES (%s, %s) ") |
||
| 1336 | cursor.execute(add_row, (id_, equipment_id,)) |
||
| 1337 | cnx.commit() |
||
| 1338 | cursor.close() |
||
| 1339 | cnx.close() |
||
| 1340 | |||
| 1341 | resp.status = falcon.HTTP_201 |
||
| 1342 | resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id) |
||
| 1343 | |||
| 1344 | |||
| 1345 | View Code Duplication | class SpaceEquipmentItem: |
|
| 1346 | def __init__(self): |
||
| 1347 | """Initializes Class""" |
||
| 1348 | pass |
||
| 1349 | |||
| 1350 | @staticmethod |
||
| 1351 | def on_options(req, resp, id_, eid): |
||
| 1352 | resp.status = falcon.HTTP_200 |
||
| 1353 | |||
| 1354 | @staticmethod |
||
| 1355 | @user_logger |
||
| 1356 | def on_delete(req, resp, id_, eid): |
||
| 1357 | admin_control(req) |
||
| 1358 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1359 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1360 | description='API.INVALID_SPACE_ID') |
||
| 1361 | |||
| 1362 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1363 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1364 | description='API.INVALID_EQUIPMENT_ID') |
||
| 1365 | |||
| 1366 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1367 | cursor = cnx.cursor() |
||
| 1368 | |||
| 1369 | cursor.execute(" SELECT name " |
||
| 1370 | " FROM tbl_spaces " |
||
| 1371 | " WHERE id = %s ", (id_,)) |
||
| 1372 | if cursor.fetchone() is None: |
||
| 1373 | cursor.close() |
||
| 1374 | cnx.close() |
||
| 1375 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1376 | description='API.SPACE_NOT_FOUND') |
||
| 1377 | |||
| 1378 | cursor.execute(" SELECT name " |
||
| 1379 | " FROM tbl_equipments " |
||
| 1380 | " WHERE id = %s ", (eid,)) |
||
| 1381 | if cursor.fetchone() is None: |
||
| 1382 | cursor.close() |
||
| 1383 | cnx.close() |
||
| 1384 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1385 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 1386 | |||
| 1387 | cursor.execute(" SELECT id " |
||
| 1388 | " FROM tbl_spaces_equipments " |
||
| 1389 | " WHERE space_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 1390 | if cursor.fetchone() is None: |
||
| 1391 | cursor.close() |
||
| 1392 | cnx.close() |
||
| 1393 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1394 | description='API.SPACE_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 1395 | |||
| 1396 | cursor.execute(" DELETE FROM tbl_spaces_equipments WHERE space_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 1397 | cnx.commit() |
||
| 1398 | |||
| 1399 | cursor.close() |
||
| 1400 | cnx.close() |
||
| 1401 | |||
| 1402 | resp.status = falcon.HTTP_204 |
||
| 1403 | |||
| 1404 | |||
| 1405 | View Code Duplication | class SpaceHybridPowerStationCollection: |
|
| 1406 | def __init__(self): |
||
| 1407 | """Initializes Class""" |
||
| 1408 | pass |
||
| 1409 | |||
| 1410 | @staticmethod |
||
| 1411 | def on_options(req, resp, id_): |
||
| 1412 | resp.status = falcon.HTTP_200 |
||
| 1413 | |||
| 1414 | @staticmethod |
||
| 1415 | def on_get(req, resp, id_): |
||
| 1416 | # NOTE: DO NOT allow to be called by api_key |
||
| 1417 | access_control(req) |
||
| 1418 | |||
| 1419 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1420 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1421 | description='API.INVALID_SPACE_ID') |
||
| 1422 | |||
| 1423 | if 'USER-UUID' not in req.headers or \ |
||
| 1424 | not isinstance(req.headers['USER-UUID'], str) or \ |
||
| 1425 | len(str.strip(req.headers['USER-UUID'])) == 0: |
||
| 1426 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1427 | description='API.INVALID_USER_UUID') |
||
| 1428 | user_uuid = str.strip(req.headers['USER-UUID']) |
||
| 1429 | |||
| 1430 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
| 1431 | cursor_user = cnx_user.cursor() |
||
| 1432 | cursor_user.execute(" SELECT id FROM tbl_users WHERE uuid = %s ", (user_uuid,)) |
||
| 1433 | row_user = cursor_user.fetchone() |
||
| 1434 | if row_user is None or len(row_user) != 1: |
||
| 1435 | cursor_user.close() |
||
| 1436 | cnx_user.close() |
||
| 1437 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1438 | description='API.USER_NOT_FOUND') |
||
| 1439 | else: |
||
| 1440 | user_id = row_user[0] |
||
| 1441 | |||
| 1442 | # get privilege |
||
| 1443 | query = (" SELECT is_admin " |
||
| 1444 | " FROM tbl_users " |
||
| 1445 | " WHERE uuid = %s ") |
||
| 1446 | cursor_user.execute(query, (user_uuid,)) |
||
| 1447 | row = cursor_user.fetchone() |
||
| 1448 | if row is None: |
||
| 1449 | cursor_user.close() |
||
| 1450 | cnx_user.close() |
||
| 1451 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.USER_NOT_FOUND') |
||
| 1452 | else: |
||
| 1453 | is_admin = bool(row[0]) |
||
| 1454 | |||
| 1455 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1456 | cursor = cnx.cursor() |
||
| 1457 | |||
| 1458 | cursor.execute(" SELECT name " |
||
| 1459 | " FROM tbl_spaces " |
||
| 1460 | " WHERE id = %s ", (id_,)) |
||
| 1461 | if cursor.fetchone() is None: |
||
| 1462 | cursor.close() |
||
| 1463 | cnx.close() |
||
| 1464 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1465 | description='API.SPACE_NOT_FOUND') |
||
| 1466 | |||
| 1467 | if is_admin: |
||
| 1468 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 1469 | " FROM tbl_spaces s, tbl_spaces_hybrid_power_stations se, " |
||
| 1470 | " tbl_hybrid_power_stations e " |
||
| 1471 | " WHERE se.space_id = s.id AND e.id = se.hybrid_power_station_id AND s.id = %s " |
||
| 1472 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 1473 | cursor.execute(query, (id_,)) |
||
| 1474 | else: |
||
| 1475 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 1476 | " FROM tbl_spaces s, tbl_spaces_hybrid_power_stations se, " |
||
| 1477 | " tbl_hybrid_power_stations e, tbl_hybrid_power_stations_users su " |
||
| 1478 | " WHERE se.space_id = s.id AND e.id = se.hybrid_power_station_id AND " |
||
| 1479 | " e.id = su.hybrid_power_station_id AND s.id = %s AND su.user_id = %s " |
||
| 1480 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 1481 | cursor.execute(query, (id_, user_id)) |
||
| 1482 | rows = cursor.fetchall() |
||
| 1483 | |||
| 1484 | result = list() |
||
| 1485 | if rows is not None and len(rows) > 0: |
||
| 1486 | for row in rows: |
||
| 1487 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1488 | result.append(meta_result) |
||
| 1489 | |||
| 1490 | resp.text = json.dumps(result) |
||
| 1491 | |||
| 1492 | @staticmethod |
||
| 1493 | @user_logger |
||
| 1494 | def on_post(req, resp, id_): |
||
| 1495 | """Handles POST requests""" |
||
| 1496 | admin_control(req) |
||
| 1497 | try: |
||
| 1498 | raw_json = req.stream.read().decode('utf-8') |
||
| 1499 | except Exception as ex: |
||
| 1500 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1501 | title='API.BAD_REQUEST', |
||
| 1502 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1503 | |||
| 1504 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1505 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1506 | description='API.INVALID_SPACE_ID') |
||
| 1507 | |||
| 1508 | new_values = json.loads(raw_json) |
||
| 1509 | |||
| 1510 | if 'hybrid_power_station_id' not in new_values['data'].keys() or \ |
||
| 1511 | not isinstance(new_values['data']['hybrid_power_station_id'], int) or \ |
||
| 1512 | new_values['data']['hybrid_power_station_id'] <= 0: |
||
| 1513 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1514 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
||
| 1515 | hybrid_power_station_id = new_values['data']['hybrid_power_station_id'] |
||
| 1516 | |||
| 1517 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1518 | cursor = cnx.cursor() |
||
| 1519 | |||
| 1520 | cursor.execute(" SELECT name " |
||
| 1521 | " from tbl_spaces " |
||
| 1522 | " WHERE id = %s ", (id_,)) |
||
| 1523 | if cursor.fetchone() is None: |
||
| 1524 | cursor.close() |
||
| 1525 | cnx.close() |
||
| 1526 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1527 | description='API.SPACE_NOT_FOUND') |
||
| 1528 | |||
| 1529 | cursor.execute(" SELECT name " |
||
| 1530 | " FROM tbl_hybrid_power_stations " |
||
| 1531 | " WHERE id = %s ", (hybrid_power_station_id,)) |
||
| 1532 | if cursor.fetchone() is None: |
||
| 1533 | cursor.close() |
||
| 1534 | cnx.close() |
||
| 1535 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1536 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
||
| 1537 | |||
| 1538 | query = (" SELECT id " |
||
| 1539 | " FROM tbl_spaces_hybrid_power_stations " |
||
| 1540 | " WHERE space_id = %s AND hybrid_power_station_id = %s") |
||
| 1541 | cursor.execute(query, (id_, hybrid_power_station_id,)) |
||
| 1542 | if cursor.fetchone() is not None: |
||
| 1543 | cursor.close() |
||
| 1544 | cnx.close() |
||
| 1545 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1546 | description='API.SPACE_HYBRID_POWER_STATION_RELATION_EXISTS') |
||
| 1547 | |||
| 1548 | add_row = (" INSERT INTO tbl_spaces_hybrid_power_stations (space_id, hybrid_power_station_id) " |
||
| 1549 | " VALUES (%s, %s) ") |
||
| 1550 | cursor.execute(add_row, (id_, hybrid_power_station_id,)) |
||
| 1551 | cnx.commit() |
||
| 1552 | cursor.close() |
||
| 1553 | cnx.close() |
||
| 1554 | |||
| 1555 | resp.status = falcon.HTTP_201 |
||
| 1556 | resp.location = '/spaces/' + str(id_) + '/energystoragepowerstations/' + str(hybrid_power_station_id) |
||
| 1557 | |||
| 1558 | |||
| 1559 | View Code Duplication | class SpaceHybridPowerStationItem: |
|
| 1560 | def __init__(self): |
||
| 1561 | """Initializes Class""" |
||
| 1562 | pass |
||
| 1563 | |||
| 1564 | @staticmethod |
||
| 1565 | def on_options(req, resp, id_, eid): |
||
| 1566 | resp.status = falcon.HTTP_200 |
||
| 1567 | |||
| 1568 | @staticmethod |
||
| 1569 | @user_logger |
||
| 1570 | def on_delete(req, resp, id_, eid): |
||
| 1571 | admin_control(req) |
||
| 1572 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1573 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1574 | description='API.INVALID_SPACE_ID') |
||
| 1575 | |||
| 1576 | if not eid.isdigit() or int(eid) <= 0: |
||
| 1577 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1578 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
||
| 1579 | |||
| 1580 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1581 | cursor = cnx.cursor() |
||
| 1582 | |||
| 1583 | cursor.execute(" SELECT name " |
||
| 1584 | " FROM tbl_spaces " |
||
| 1585 | " WHERE id = %s ", (id_,)) |
||
| 1586 | if cursor.fetchone() is None: |
||
| 1587 | cursor.close() |
||
| 1588 | cnx.close() |
||
| 1589 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1590 | description='API.SPACE_NOT_FOUND') |
||
| 1591 | |||
| 1592 | cursor.execute(" SELECT name " |
||
| 1593 | " FROM tbl_hybrid_power_stations " |
||
| 1594 | " WHERE id = %s ", (eid,)) |
||
| 1595 | if cursor.fetchone() is None: |
||
| 1596 | cursor.close() |
||
| 1597 | cnx.close() |
||
| 1598 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1599 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
||
| 1600 | |||
| 1601 | cursor.execute(" SELECT id " |
||
| 1602 | " FROM tbl_spaces_hybrid_power_stations " |
||
| 1603 | " WHERE space_id = %s AND hybrid_power_station_id = %s ", (id_, eid)) |
||
| 1604 | if cursor.fetchone() is None: |
||
| 1605 | cursor.close() |
||
| 1606 | cnx.close() |
||
| 1607 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1608 | description='API.SPACE_HYBRID_POWER_STATION_RELATION_NOT_FOUND') |
||
| 1609 | |||
| 1610 | cursor.execute(" DELETE FROM tbl_spaces_hybrid_power_stations " |
||
| 1611 | " WHERE space_id = %s AND hybrid_power_station_id = %s ", (id_, eid)) |
||
| 1612 | cnx.commit() |
||
| 1613 | |||
| 1614 | cursor.close() |
||
| 1615 | cnx.close() |
||
| 1616 | |||
| 1617 | resp.status = falcon.HTTP_204 |
||
| 1618 | |||
| 1619 | |||
| 1620 | View Code Duplication | class SpaceMeterCollection: |
|
| 1621 | def __init__(self): |
||
| 1622 | """Initializes Class""" |
||
| 1623 | pass |
||
| 1624 | |||
| 1625 | @staticmethod |
||
| 1626 | def on_options(req, resp, id_): |
||
| 1627 | resp.status = falcon.HTTP_200 |
||
| 1628 | |||
| 1629 | @staticmethod |
||
| 1630 | def on_get(req, resp, id_): |
||
| 1631 | if 'API-KEY' not in req.headers or \ |
||
| 1632 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1633 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1634 | access_control(req) |
||
| 1635 | else: |
||
| 1636 | api_key_control(req) |
||
| 1637 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1638 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1639 | description='API.INVALID_SPACE_ID') |
||
| 1640 | |||
| 1641 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1642 | cursor = cnx.cursor() |
||
| 1643 | |||
| 1644 | cursor.execute(" SELECT name " |
||
| 1645 | " FROM tbl_spaces " |
||
| 1646 | " WHERE id = %s ", (id_,)) |
||
| 1647 | if cursor.fetchone() is None: |
||
| 1648 | cursor.close() |
||
| 1649 | cnx.close() |
||
| 1650 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1651 | description='API.SPACE_NOT_FOUND') |
||
| 1652 | |||
| 1653 | query = (" SELECT id, name, uuid " |
||
| 1654 | " FROM tbl_energy_categories ") |
||
| 1655 | cursor.execute(query) |
||
| 1656 | rows_energy_categories = cursor.fetchall() |
||
| 1657 | |||
| 1658 | energy_category_dict = dict() |
||
| 1659 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1660 | for row in rows_energy_categories: |
||
| 1661 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 1662 | "name": row[1], |
||
| 1663 | "uuid": row[2]} |
||
| 1664 | |||
| 1665 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 1666 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
||
| 1667 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 1668 | " ORDER BY m.id ") |
||
| 1669 | cursor.execute(query, (id_,)) |
||
| 1670 | rows = cursor.fetchall() |
||
| 1671 | |||
| 1672 | result = list() |
||
| 1673 | if rows is not None and len(rows) > 0: |
||
| 1674 | for row in rows: |
||
| 1675 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 1676 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 1677 | result.append(meta_result) |
||
| 1678 | |||
| 1679 | resp.text = json.dumps(result) |
||
| 1680 | |||
| 1681 | @staticmethod |
||
| 1682 | @user_logger |
||
| 1683 | def on_post(req, resp, id_): |
||
| 1684 | """Handles POST requests""" |
||
| 1685 | admin_control(req) |
||
| 1686 | try: |
||
| 1687 | raw_json = req.stream.read().decode('utf-8') |
||
| 1688 | except Exception as ex: |
||
| 1689 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1690 | title='API.BAD_REQUEST', |
||
| 1691 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1692 | |||
| 1693 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1694 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1695 | description='API.INVALID_SPACE_ID') |
||
| 1696 | |||
| 1697 | new_values = json.loads(raw_json) |
||
| 1698 | |||
| 1699 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 1700 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 1701 | new_values['data']['meter_id'] <= 0: |
||
| 1702 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1703 | description='API.INVALID_METER_ID') |
||
| 1704 | meter_id = new_values['data']['meter_id'] |
||
| 1705 | |||
| 1706 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1707 | cursor = cnx.cursor() |
||
| 1708 | |||
| 1709 | cursor.execute(" SELECT name " |
||
| 1710 | " from tbl_spaces " |
||
| 1711 | " WHERE id = %s ", (id_,)) |
||
| 1712 | if cursor.fetchone() is None: |
||
| 1713 | cursor.close() |
||
| 1714 | cnx.close() |
||
| 1715 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1716 | description='API.SPACE_NOT_FOUND') |
||
| 1717 | |||
| 1718 | cursor.execute(" SELECT name " |
||
| 1719 | " FROM tbl_meters " |
||
| 1720 | " WHERE id = %s ", (meter_id,)) |
||
| 1721 | if cursor.fetchone() is None: |
||
| 1722 | cursor.close() |
||
| 1723 | cnx.close() |
||
| 1724 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1725 | description='API.METER_NOT_FOUND') |
||
| 1726 | |||
| 1727 | query = (" SELECT id " |
||
| 1728 | " FROM tbl_spaces_meters " |
||
| 1729 | " WHERE space_id = %s AND meter_id = %s") |
||
| 1730 | cursor.execute(query, (id_, meter_id,)) |
||
| 1731 | if cursor.fetchone() is not None: |
||
| 1732 | cursor.close() |
||
| 1733 | cnx.close() |
||
| 1734 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1735 | description='API.SPACE_METER_RELATION_EXISTS') |
||
| 1736 | |||
| 1737 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
||
| 1738 | " VALUES (%s, %s) ") |
||
| 1739 | cursor.execute(add_row, (id_, meter_id,)) |
||
| 1740 | cnx.commit() |
||
| 1741 | cursor.close() |
||
| 1742 | cnx.close() |
||
| 1743 | |||
| 1744 | resp.status = falcon.HTTP_201 |
||
| 1745 | resp.location = '/spaces/' + str(id_) + '/meters/' + str(meter_id) |
||
| 1746 | |||
| 1747 | |||
| 1748 | View Code Duplication | class SpaceMeterItem: |
|
| 1749 | def __init__(self): |
||
| 1750 | """Initializes Class""" |
||
| 1751 | pass |
||
| 1752 | |||
| 1753 | @staticmethod |
||
| 1754 | def on_options(req, resp, id_, mid): |
||
| 1755 | resp.status = falcon.HTTP_200 |
||
| 1756 | |||
| 1757 | @staticmethod |
||
| 1758 | @user_logger |
||
| 1759 | def on_delete(req, resp, id_, mid): |
||
| 1760 | admin_control(req) |
||
| 1761 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1762 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1763 | description='API.INVALID_SPACE_ID') |
||
| 1764 | |||
| 1765 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1766 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1767 | description='API.INVALID_METER_ID') |
||
| 1768 | |||
| 1769 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1770 | cursor = cnx.cursor() |
||
| 1771 | |||
| 1772 | cursor.execute(" SELECT name " |
||
| 1773 | " FROM tbl_spaces " |
||
| 1774 | " WHERE id = %s ", (id_,)) |
||
| 1775 | if cursor.fetchone() is None: |
||
| 1776 | cursor.close() |
||
| 1777 | cnx.close() |
||
| 1778 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1779 | description='API.SPACE_NOT_FOUND') |
||
| 1780 | |||
| 1781 | cursor.execute(" SELECT name " |
||
| 1782 | " FROM tbl_meters " |
||
| 1783 | " WHERE id = %s ", (mid,)) |
||
| 1784 | if cursor.fetchone() is None: |
||
| 1785 | cursor.close() |
||
| 1786 | cnx.close() |
||
| 1787 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1788 | description='API.METER_NOT_FOUND') |
||
| 1789 | |||
| 1790 | cursor.execute(" SELECT id " |
||
| 1791 | " FROM tbl_spaces_meters " |
||
| 1792 | " WHERE space_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1793 | # use fetchall to avoid 'Unread result found' error in case there are duplicate rows |
||
| 1794 | rows = cursor.fetchall() |
||
| 1795 | if rows is None or len(rows) == 0: |
||
| 1796 | cursor.close() |
||
| 1797 | cnx.close() |
||
| 1798 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1799 | description='API.SPACE_METER_RELATION_NOT_FOUND') |
||
| 1800 | |||
| 1801 | cursor.execute(" DELETE FROM tbl_spaces_meters WHERE space_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 1802 | cnx.commit() |
||
| 1803 | |||
| 1804 | cursor.close() |
||
| 1805 | cnx.close() |
||
| 1806 | |||
| 1807 | resp.status = falcon.HTTP_204 |
||
| 1808 | |||
| 1809 | |||
| 1810 | View Code Duplication | class SpaceMicrogridCollection: |
|
| 1811 | def __init__(self): |
||
| 1812 | """Initializes Class""" |
||
| 1813 | pass |
||
| 1814 | |||
| 1815 | @staticmethod |
||
| 1816 | def on_options(req, resp, id_): |
||
| 1817 | resp.status = falcon.HTTP_200 |
||
| 1818 | |||
| 1819 | @staticmethod |
||
| 1820 | def on_get(req, resp, id_): |
||
| 1821 | if 'API-KEY' not in req.headers or \ |
||
| 1822 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1823 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1824 | access_control(req) |
||
| 1825 | else: |
||
| 1826 | api_key_control(req) |
||
| 1827 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1828 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1829 | description='API.INVALID_SPACE_ID') |
||
| 1830 | |||
| 1831 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1832 | cursor = cnx.cursor() |
||
| 1833 | |||
| 1834 | cursor.execute(" SELECT name " |
||
| 1835 | " FROM tbl_spaces " |
||
| 1836 | " WHERE id = %s ", (id_,)) |
||
| 1837 | if cursor.fetchone() is None: |
||
| 1838 | cursor.close() |
||
| 1839 | cnx.close() |
||
| 1840 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1841 | description='API.SPACE_NOT_FOUND') |
||
| 1842 | |||
| 1843 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 1844 | " FROM tbl_spaces s, tbl_spaces_microgrids se, tbl_microgrids e " |
||
| 1845 | " WHERE se.space_id = s.id AND e.id = se.microgrid_id AND s.id = %s " |
||
| 1846 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 1847 | cursor.execute(query, (id_,)) |
||
| 1848 | rows = cursor.fetchall() |
||
| 1849 | |||
| 1850 | result = list() |
||
| 1851 | if rows is not None and len(rows) > 0: |
||
| 1852 | for row in rows: |
||
| 1853 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1854 | result.append(meta_result) |
||
| 1855 | |||
| 1856 | resp.text = json.dumps(result) |
||
| 1857 | |||
| 1858 | @staticmethod |
||
| 1859 | @user_logger |
||
| 1860 | def on_post(req, resp, id_): |
||
| 1861 | """Handles POST requests""" |
||
| 1862 | admin_control(req) |
||
| 1863 | try: |
||
| 1864 | raw_json = req.stream.read().decode('utf-8') |
||
| 1865 | except Exception as ex: |
||
| 1866 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1867 | title='API.BAD_REQUEST', |
||
| 1868 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1869 | |||
| 1870 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1871 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1872 | description='API.INVALID_SPACE_ID') |
||
| 1873 | |||
| 1874 | new_values = json.loads(raw_json) |
||
| 1875 | |||
| 1876 | if 'microgrid_id' not in new_values['data'].keys() or \ |
||
| 1877 | not isinstance(new_values['data']['microgrid_id'], int) or \ |
||
| 1878 | new_values['data']['microgrid_id'] <= 0: |
||
| 1879 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1880 | description='API.INVALID_MICROGRID_ID') |
||
| 1881 | microgrid_id = new_values['data']['microgrid_id'] |
||
| 1882 | |||
| 1883 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1884 | cursor = cnx.cursor() |
||
| 1885 | |||
| 1886 | cursor.execute(" SELECT name " |
||
| 1887 | " from tbl_spaces " |
||
| 1888 | " WHERE id = %s ", (id_,)) |
||
| 1889 | if cursor.fetchone() is None: |
||
| 1890 | cursor.close() |
||
| 1891 | cnx.close() |
||
| 1892 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1893 | description='API.SPACE_NOT_FOUND') |
||
| 1894 | |||
| 1895 | cursor.execute(" SELECT name " |
||
| 1896 | " FROM tbl_microgrids " |
||
| 1897 | " WHERE id = %s ", (microgrid_id,)) |
||
| 1898 | if cursor.fetchone() is None: |
||
| 1899 | cursor.close() |
||
| 1900 | cnx.close() |
||
| 1901 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1902 | description='API.MICROGRID_NOT_FOUND') |
||
| 1903 | |||
| 1904 | query = (" SELECT id " |
||
| 1905 | " FROM tbl_spaces_microgrids " |
||
| 1906 | " WHERE space_id = %s AND microgrid_id = %s") |
||
| 1907 | cursor.execute(query, (id_, microgrid_id,)) |
||
| 1908 | if cursor.fetchone() is not None: |
||
| 1909 | cursor.close() |
||
| 1910 | cnx.close() |
||
| 1911 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1912 | description='API.SPACE_MICROGRID_RELATION_EXISTS') |
||
| 1913 | |||
| 1914 | add_row = (" INSERT INTO tbl_spaces_microgrids (space_id, microgrid_id) " |
||
| 1915 | " VALUES (%s, %s) ") |
||
| 1916 | cursor.execute(add_row, (id_, microgrid_id,)) |
||
| 1917 | cnx.commit() |
||
| 1918 | cursor.close() |
||
| 1919 | cnx.close() |
||
| 1920 | |||
| 1921 | resp.status = falcon.HTTP_201 |
||
| 1922 | resp.location = '/spaces/' + str(id_) + '/microgrids/' + str(microgrid_id) |
||
| 1923 | |||
| 1924 | |||
| 1925 | View Code Duplication | class SpaceMicrogridItem: |
|
| 1926 | def __init__(self): |
||
| 1927 | """Initializes Class""" |
||
| 1928 | pass |
||
| 1929 | |||
| 1930 | @staticmethod |
||
| 1931 | def on_options(req, resp, id_, mid): |
||
| 1932 | resp.status = falcon.HTTP_200 |
||
| 1933 | |||
| 1934 | @staticmethod |
||
| 1935 | @user_logger |
||
| 1936 | def on_delete(req, resp, id_, mid): |
||
| 1937 | admin_control(req) |
||
| 1938 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1939 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1940 | description='API.INVALID_SPACE_ID') |
||
| 1941 | |||
| 1942 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1943 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1944 | description='API.INVALID_MICROGRID_ID') |
||
| 1945 | |||
| 1946 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1947 | cursor = cnx.cursor() |
||
| 1948 | |||
| 1949 | cursor.execute(" SELECT name " |
||
| 1950 | " FROM tbl_spaces " |
||
| 1951 | " WHERE id = %s ", (id_,)) |
||
| 1952 | if cursor.fetchone() is None: |
||
| 1953 | cursor.close() |
||
| 1954 | cnx.close() |
||
| 1955 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1956 | description='API.SPACE_NOT_FOUND') |
||
| 1957 | |||
| 1958 | cursor.execute(" SELECT name " |
||
| 1959 | " FROM tbl_microgrids " |
||
| 1960 | " WHERE id = %s ", (mid,)) |
||
| 1961 | if cursor.fetchone() is None: |
||
| 1962 | cursor.close() |
||
| 1963 | cnx.close() |
||
| 1964 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1965 | description='API.MICROGRID_NOT_FOUND') |
||
| 1966 | |||
| 1967 | cursor.execute(" SELECT id " |
||
| 1968 | " FROM tbl_spaces_microgrids " |
||
| 1969 | " WHERE space_id = %s AND microgrid_id = %s ", (id_, mid)) |
||
| 1970 | if cursor.fetchone() is None: |
||
| 1971 | cursor.close() |
||
| 1972 | cnx.close() |
||
| 1973 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1974 | description='API.SPACE_MICROGRID_RELATION_NOT_FOUND') |
||
| 1975 | |||
| 1976 | cursor.execute(" DELETE FROM tbl_spaces_microgrids " |
||
| 1977 | " WHERE space_id = %s AND microgrid_id = %s ", (id_, mid)) |
||
| 1978 | cnx.commit() |
||
| 1979 | |||
| 1980 | cursor.close() |
||
| 1981 | cnx.close() |
||
| 1982 | |||
| 1983 | resp.status = falcon.HTTP_204 |
||
| 1984 | |||
| 1985 | |||
| 1986 | View Code Duplication | class SpaceOfflineMeterCollection: |
|
| 1987 | def __init__(self): |
||
| 1988 | """Initializes Class""" |
||
| 1989 | pass |
||
| 1990 | |||
| 1991 | @staticmethod |
||
| 1992 | def on_options(req, resp, id_): |
||
| 1993 | resp.status = falcon.HTTP_200 |
||
| 1994 | |||
| 1995 | @staticmethod |
||
| 1996 | def on_get(req, resp, id_): |
||
| 1997 | if 'API-KEY' not in req.headers or \ |
||
| 1998 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1999 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2000 | access_control(req) |
||
| 2001 | else: |
||
| 2002 | api_key_control(req) |
||
| 2003 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2004 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2005 | description='API.INVALID_SPACE_ID') |
||
| 2006 | |||
| 2007 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2008 | cursor = cnx.cursor() |
||
| 2009 | |||
| 2010 | cursor.execute(" SELECT name " |
||
| 2011 | " FROM tbl_spaces " |
||
| 2012 | " WHERE id = %s ", (id_,)) |
||
| 2013 | if cursor.fetchone() is None: |
||
| 2014 | cursor.close() |
||
| 2015 | cnx.close() |
||
| 2016 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2017 | description='API.SPACE_NOT_FOUND') |
||
| 2018 | |||
| 2019 | query = (" SELECT id, name, uuid " |
||
| 2020 | " FROM tbl_energy_categories ") |
||
| 2021 | cursor.execute(query) |
||
| 2022 | rows_energy_categories = cursor.fetchall() |
||
| 2023 | |||
| 2024 | energy_category_dict = dict() |
||
| 2025 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2026 | for row in rows_energy_categories: |
||
| 2027 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2028 | "name": row[1], |
||
| 2029 | "uuid": row[2]} |
||
| 2030 | |||
| 2031 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2032 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
||
| 2033 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 2034 | " ORDER BY m.id ") |
||
| 2035 | cursor.execute(query, (id_,)) |
||
| 2036 | rows = cursor.fetchall() |
||
| 2037 | |||
| 2038 | result = list() |
||
| 2039 | if rows is not None and len(rows) > 0: |
||
| 2040 | for row in rows: |
||
| 2041 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2042 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2043 | result.append(meta_result) |
||
| 2044 | |||
| 2045 | resp.text = json.dumps(result) |
||
| 2046 | |||
| 2047 | @staticmethod |
||
| 2048 | @user_logger |
||
| 2049 | def on_post(req, resp, id_): |
||
| 2050 | """Handles POST requests""" |
||
| 2051 | admin_control(req) |
||
| 2052 | try: |
||
| 2053 | raw_json = req.stream.read().decode('utf-8') |
||
| 2054 | except Exception as ex: |
||
| 2055 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2056 | title='API.BAD_REQUEST', |
||
| 2057 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2058 | |||
| 2059 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2060 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2061 | description='API.INVALID_SPACE_ID') |
||
| 2062 | |||
| 2063 | new_values = json.loads(raw_json) |
||
| 2064 | |||
| 2065 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 2066 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 2067 | new_values['data']['offline_meter_id'] <= 0: |
||
| 2068 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2069 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 2070 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 2071 | |||
| 2072 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2073 | cursor = cnx.cursor() |
||
| 2074 | |||
| 2075 | cursor.execute(" SELECT name " |
||
| 2076 | " from tbl_spaces " |
||
| 2077 | " WHERE id = %s ", (id_,)) |
||
| 2078 | if cursor.fetchone() is None: |
||
| 2079 | cursor.close() |
||
| 2080 | cnx.close() |
||
| 2081 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2082 | description='API.SPACE_NOT_FOUND') |
||
| 2083 | |||
| 2084 | cursor.execute(" SELECT name " |
||
| 2085 | " FROM tbl_offline_meters " |
||
| 2086 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 2087 | if cursor.fetchone() is None: |
||
| 2088 | cursor.close() |
||
| 2089 | cnx.close() |
||
| 2090 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2091 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2092 | |||
| 2093 | query = (" SELECT id " |
||
| 2094 | " FROM tbl_spaces_offline_meters " |
||
| 2095 | " WHERE space_id = %s AND offline_meter_id = %s") |
||
| 2096 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 2097 | if cursor.fetchone() is not None: |
||
| 2098 | cursor.close() |
||
| 2099 | cnx.close() |
||
| 2100 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2101 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTS') |
||
| 2102 | |||
| 2103 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
||
| 2104 | " VALUES (%s, %s) ") |
||
| 2105 | cursor.execute(add_row, (id_, offline_meter_id,)) |
||
| 2106 | cnx.commit() |
||
| 2107 | cursor.close() |
||
| 2108 | cnx.close() |
||
| 2109 | |||
| 2110 | resp.status = falcon.HTTP_201 |
||
| 2111 | resp.location = '/spaces/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 2112 | |||
| 2113 | |||
| 2114 | View Code Duplication | class SpaceOfflineMeterItem: |
|
| 2115 | def __init__(self): |
||
| 2116 | """Initializes Class""" |
||
| 2117 | pass |
||
| 2118 | |||
| 2119 | @staticmethod |
||
| 2120 | def on_options(req, resp, id_, mid): |
||
| 2121 | resp.status = falcon.HTTP_200 |
||
| 2122 | |||
| 2123 | @staticmethod |
||
| 2124 | @user_logger |
||
| 2125 | def on_delete(req, resp, id_, mid): |
||
| 2126 | admin_control(req) |
||
| 2127 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2129 | description='API.INVALID_SPACE_ID') |
||
| 2130 | |||
| 2131 | if not mid.isdigit() or int(mid) <= 0: |
||
| 2132 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2133 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 2134 | |||
| 2135 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2136 | cursor = cnx.cursor() |
||
| 2137 | |||
| 2138 | cursor.execute(" SELECT name " |
||
| 2139 | " FROM tbl_spaces " |
||
| 2140 | " WHERE id = %s ", (id_,)) |
||
| 2141 | if cursor.fetchone() is None: |
||
| 2142 | cursor.close() |
||
| 2143 | cnx.close() |
||
| 2144 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2145 | description='API.SPACE_NOT_FOUND') |
||
| 2146 | |||
| 2147 | cursor.execute(" SELECT name " |
||
| 2148 | " FROM tbl_offline_meters " |
||
| 2149 | " WHERE id = %s ", (mid,)) |
||
| 2150 | if cursor.fetchone() is None: |
||
| 2151 | cursor.close() |
||
| 2152 | cnx.close() |
||
| 2153 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2154 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2155 | |||
| 2156 | cursor.execute(" SELECT id " |
||
| 2157 | " FROM tbl_spaces_offline_meters " |
||
| 2158 | " WHERE space_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 2159 | if cursor.fetchone() is None: |
||
| 2160 | cursor.close() |
||
| 2161 | cnx.close() |
||
| 2162 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2163 | description='API.SPACE_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 2164 | |||
| 2165 | cursor.execute(" DELETE FROM tbl_spaces_offline_meters " |
||
| 2166 | " WHERE space_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 2167 | cnx.commit() |
||
| 2168 | |||
| 2169 | cursor.close() |
||
| 2170 | cnx.close() |
||
| 2171 | |||
| 2172 | resp.status = falcon.HTTP_204 |
||
| 2173 | |||
| 2174 | |||
| 2175 | View Code Duplication | class SpacePhotovoltaicPowerStationCollection: |
|
| 2176 | def __init__(self): |
||
| 2177 | """Initializes Class""" |
||
| 2178 | pass |
||
| 2179 | |||
| 2180 | @staticmethod |
||
| 2181 | def on_options(req, resp, id_): |
||
| 2182 | resp.status = falcon.HTTP_200 |
||
| 2183 | |||
| 2184 | @staticmethod |
||
| 2185 | def on_get(req, resp, id_): |
||
| 2186 | if 'API-KEY' not in req.headers or \ |
||
| 2187 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2188 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2189 | access_control(req) |
||
| 2190 | else: |
||
| 2191 | api_key_control(req) |
||
| 2192 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2193 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2194 | description='API.INVALID_SPACE_ID') |
||
| 2195 | |||
| 2196 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2197 | cursor = cnx.cursor() |
||
| 2198 | |||
| 2199 | cursor.execute(" SELECT name " |
||
| 2200 | " FROM tbl_spaces " |
||
| 2201 | " WHERE id = %s ", (id_,)) |
||
| 2202 | if cursor.fetchone() is None: |
||
| 2203 | cursor.close() |
||
| 2204 | cnx.close() |
||
| 2205 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2206 | description='API.SPACE_NOT_FOUND') |
||
| 2207 | |||
| 2208 | query = (" SELECT e.id, e.name, e.uuid, e.phase_of_lifecycle " |
||
| 2209 | " FROM tbl_spaces s, tbl_spaces_photovoltaic_power_stations se, tbl_photovoltaic_power_stations e " |
||
| 2210 | " WHERE se.space_id = s.id AND e.id = se.photovoltaic_power_station_id AND s.id = %s " |
||
| 2211 | " ORDER BY e.phase_of_lifecycle, e.id ") |
||
| 2212 | cursor.execute(query, (id_,)) |
||
| 2213 | rows = cursor.fetchall() |
||
| 2214 | |||
| 2215 | result = list() |
||
| 2216 | if rows is not None and len(rows) > 0: |
||
| 2217 | for row in rows: |
||
| 2218 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2219 | result.append(meta_result) |
||
| 2220 | |||
| 2221 | resp.text = json.dumps(result) |
||
| 2222 | |||
| 2223 | @staticmethod |
||
| 2224 | @user_logger |
||
| 2225 | def on_post(req, resp, id_): |
||
| 2226 | """Handles POST requests""" |
||
| 2227 | admin_control(req) |
||
| 2228 | try: |
||
| 2229 | raw_json = req.stream.read().decode('utf-8') |
||
| 2230 | except Exception as ex: |
||
| 2231 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2232 | title='API.BAD_REQUEST', |
||
| 2233 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2234 | |||
| 2235 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2236 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2237 | description='API.INVALID_SPACE_ID') |
||
| 2238 | |||
| 2239 | new_values = json.loads(raw_json) |
||
| 2240 | |||
| 2241 | if 'photovoltaic_power_station_id' not in new_values['data'].keys() or \ |
||
| 2242 | not isinstance(new_values['data']['photovoltaic_power_station_id'], int) or \ |
||
| 2243 | new_values['data']['photovoltaic_power_station_id'] <= 0: |
||
| 2244 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2245 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
||
| 2246 | photovoltaic_power_station_id = new_values['data']['photovoltaic_power_station_id'] |
||
| 2247 | |||
| 2248 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2249 | cursor = cnx.cursor() |
||
| 2250 | |||
| 2251 | cursor.execute(" SELECT name " |
||
| 2252 | " from tbl_spaces " |
||
| 2253 | " WHERE id = %s ", (id_,)) |
||
| 2254 | if cursor.fetchone() is None: |
||
| 2255 | cursor.close() |
||
| 2256 | cnx.close() |
||
| 2257 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2258 | description='API.SPACE_NOT_FOUND') |
||
| 2259 | |||
| 2260 | cursor.execute(" SELECT name " |
||
| 2261 | " FROM tbl_photovoltaic_power_stations " |
||
| 2262 | " WHERE id = %s ", (photovoltaic_power_station_id,)) |
||
| 2263 | if cursor.fetchone() is None: |
||
| 2264 | cursor.close() |
||
| 2265 | cnx.close() |
||
| 2266 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2267 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
||
| 2268 | |||
| 2269 | query = (" SELECT id " |
||
| 2270 | " FROM tbl_spaces_photovoltaic_power_stations " |
||
| 2271 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s") |
||
| 2272 | cursor.execute(query, (id_, photovoltaic_power_station_id,)) |
||
| 2273 | if cursor.fetchone() is not None: |
||
| 2274 | cursor.close() |
||
| 2275 | cnx.close() |
||
| 2276 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2277 | description='API.SPACE_PHOTOVOLTAIC_POWER_STATION_RELATION_EXISTS') |
||
| 2278 | |||
| 2279 | add_row = (" INSERT INTO tbl_spaces_photovoltaic_power_stations (space_id, photovoltaic_power_station_id) " |
||
| 2280 | " VALUES (%s, %s) ") |
||
| 2281 | cursor.execute(add_row, (id_, photovoltaic_power_station_id,)) |
||
| 2282 | cnx.commit() |
||
| 2283 | cursor.close() |
||
| 2284 | cnx.close() |
||
| 2285 | |||
| 2286 | resp.status = falcon.HTTP_201 |
||
| 2287 | resp.location = '/spaces/' + str(id_) + '/photovoltaicpowerstations/' + str(photovoltaic_power_station_id) |
||
| 2288 | |||
| 2289 | |||
| 2290 | View Code Duplication | class SpacePhotovoltaicPowerStationItem: |
|
| 2291 | def __init__(self): |
||
| 2292 | """Initializes Class""" |
||
| 2293 | pass |
||
| 2294 | |||
| 2295 | @staticmethod |
||
| 2296 | def on_options(req, resp, id_, eid): |
||
| 2297 | resp.status = falcon.HTTP_200 |
||
| 2298 | |||
| 2299 | @staticmethod |
||
| 2300 | @user_logger |
||
| 2301 | def on_delete(req, resp, id_, eid): |
||
| 2302 | admin_control(req) |
||
| 2303 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2304 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2305 | description='API.INVALID_SPACE_ID') |
||
| 2306 | |||
| 2307 | if not eid.isdigit() or int(eid) <= 0: |
||
| 2308 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2309 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
||
| 2310 | |||
| 2311 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2312 | cursor = cnx.cursor() |
||
| 2313 | |||
| 2314 | cursor.execute(" SELECT name " |
||
| 2315 | " FROM tbl_spaces " |
||
| 2316 | " WHERE id = %s ", (id_,)) |
||
| 2317 | if cursor.fetchone() is None: |
||
| 2318 | cursor.close() |
||
| 2319 | cnx.close() |
||
| 2320 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2321 | description='API.SPACE_NOT_FOUND') |
||
| 2322 | |||
| 2323 | cursor.execute(" SELECT name " |
||
| 2324 | " FROM tbl_photovoltaic_power_stations " |
||
| 2325 | " WHERE id = %s ", (eid,)) |
||
| 2326 | if cursor.fetchone() is None: |
||
| 2327 | cursor.close() |
||
| 2328 | cnx.close() |
||
| 2329 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2330 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
||
| 2331 | |||
| 2332 | cursor.execute(" SELECT id " |
||
| 2333 | " FROM tbl_spaces_photovoltaic_power_stations " |
||
| 2334 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s ", (id_, eid)) |
||
| 2335 | if cursor.fetchone() is None: |
||
| 2336 | cursor.close() |
||
| 2337 | cnx.close() |
||
| 2338 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2339 | description='API.SPACE_PHOTOVOLTAIC_POWER_STATION_RELATION_NOT_FOUND') |
||
| 2340 | |||
| 2341 | cursor.execute(" DELETE FROM tbl_spaces_photovoltaic_power_stations " |
||
| 2342 | " WHERE space_id = %s AND photovoltaic_power_station_id = %s ", (id_, eid)) |
||
| 2343 | cnx.commit() |
||
| 2344 | |||
| 2345 | cursor.close() |
||
| 2346 | cnx.close() |
||
| 2347 | |||
| 2348 | resp.status = falcon.HTTP_204 |
||
| 2349 | |||
| 2350 | |||
| 2351 | View Code Duplication | class SpacePointCollection: |
|
| 2352 | def __init__(self): |
||
| 2353 | """Initializes Class""" |
||
| 2354 | pass |
||
| 2355 | |||
| 2356 | @staticmethod |
||
| 2357 | def on_options(req, resp, id_): |
||
| 2358 | resp.status = falcon.HTTP_200 |
||
| 2359 | |||
| 2360 | @staticmethod |
||
| 2361 | def on_get(req, resp, id_): |
||
| 2362 | if 'API-KEY' not in req.headers or \ |
||
| 2363 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2364 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2365 | access_control(req) |
||
| 2366 | else: |
||
| 2367 | api_key_control(req) |
||
| 2368 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2369 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2370 | description='API.INVALID_SPACE_ID') |
||
| 2371 | |||
| 2372 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2373 | cursor = cnx.cursor() |
||
| 2374 | |||
| 2375 | cursor.execute(" SELECT name " |
||
| 2376 | " FROM tbl_spaces " |
||
| 2377 | " WHERE id = %s ", (id_,)) |
||
| 2378 | if cursor.fetchone() is None: |
||
| 2379 | cursor.close() |
||
| 2380 | cnx.close() |
||
| 2381 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2382 | description='API.SPACE_NOT_FOUND') |
||
| 2383 | |||
| 2384 | query = (" SELECT id, name, uuid " |
||
| 2385 | " FROM tbl_data_sources ") |
||
| 2386 | cursor.execute(query) |
||
| 2387 | rows_data_sources = cursor.fetchall() |
||
| 2388 | |||
| 2389 | data_source_dict = dict() |
||
| 2390 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 2391 | for row in rows_data_sources: |
||
| 2392 | data_source_dict[row[0]] = {"id": row[0], |
||
| 2393 | "name": row[1], |
||
| 2394 | "uuid": row[2]} |
||
| 2395 | |||
| 2396 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 2397 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
||
| 2398 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 2399 | " ORDER BY p.id ") |
||
| 2400 | cursor.execute(query, (id_,)) |
||
| 2401 | rows = cursor.fetchall() |
||
| 2402 | |||
| 2403 | result = list() |
||
| 2404 | if rows is not None and len(rows) > 0: |
||
| 2405 | for row in rows: |
||
| 2406 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
| 2407 | result.append(meta_result) |
||
| 2408 | |||
| 2409 | resp.text = json.dumps(result) |
||
| 2410 | |||
| 2411 | @staticmethod |
||
| 2412 | @user_logger |
||
| 2413 | def on_post(req, resp, id_): |
||
| 2414 | """Handles POST requests""" |
||
| 2415 | admin_control(req) |
||
| 2416 | try: |
||
| 2417 | raw_json = req.stream.read().decode('utf-8') |
||
| 2418 | except Exception as ex: |
||
| 2419 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2420 | title='API.BAD_REQUEST', |
||
| 2421 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2422 | |||
| 2423 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2424 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2425 | description='API.INVALID_SPACE_ID') |
||
| 2426 | |||
| 2427 | new_values = json.loads(raw_json) |
||
| 2428 | |||
| 2429 | if 'point_id' not in new_values['data'].keys() or \ |
||
| 2430 | not isinstance(new_values['data']['point_id'], int) or \ |
||
| 2431 | new_values['data']['point_id'] <= 0: |
||
| 2432 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2433 | description='API.INVALID_POINT_ID') |
||
| 2434 | point_id = new_values['data']['point_id'] |
||
| 2435 | |||
| 2436 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2437 | cursor = cnx.cursor() |
||
| 2438 | |||
| 2439 | cursor.execute(" SELECT name " |
||
| 2440 | " from tbl_spaces " |
||
| 2441 | " WHERE id = %s ", (id_,)) |
||
| 2442 | if cursor.fetchone() is None: |
||
| 2443 | cursor.close() |
||
| 2444 | cnx.close() |
||
| 2445 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2446 | description='API.SPACE_NOT_FOUND') |
||
| 2447 | |||
| 2448 | cursor.execute(" SELECT name " |
||
| 2449 | " FROM tbl_points " |
||
| 2450 | " WHERE id = %s ", (point_id,)) |
||
| 2451 | if cursor.fetchone() is None: |
||
| 2452 | cursor.close() |
||
| 2453 | cnx.close() |
||
| 2454 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2455 | description='API.POINT_NOT_FOUND') |
||
| 2456 | |||
| 2457 | query = (" SELECT id " |
||
| 2458 | " FROM tbl_spaces_points " |
||
| 2459 | " WHERE space_id = %s AND point_id = %s") |
||
| 2460 | cursor.execute(query, (id_, point_id,)) |
||
| 2461 | if cursor.fetchone() is not None: |
||
| 2462 | cursor.close() |
||
| 2463 | cnx.close() |
||
| 2464 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2465 | description='API.SPACE_POINT_RELATION_EXISTS') |
||
| 2466 | |||
| 2467 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
||
| 2468 | " VALUES (%s, %s) ") |
||
| 2469 | cursor.execute(add_row, (id_, point_id,)) |
||
| 2470 | cnx.commit() |
||
| 2471 | cursor.close() |
||
| 2472 | cnx.close() |
||
| 2473 | |||
| 2474 | resp.status = falcon.HTTP_201 |
||
| 2475 | resp.location = '/spaces/' + str(id_) + '/points/' + str(point_id) |
||
| 2476 | |||
| 2477 | |||
| 2478 | View Code Duplication | class SpacePointItem: |
|
| 2479 | def __init__(self): |
||
| 2480 | """Initializes Class""" |
||
| 2481 | pass |
||
| 2482 | |||
| 2483 | @staticmethod |
||
| 2484 | def on_options(req, resp, id_, pid): |
||
| 2485 | resp.status = falcon.HTTP_200 |
||
| 2486 | |||
| 2487 | @staticmethod |
||
| 2488 | @user_logger |
||
| 2489 | def on_delete(req, resp, id_, pid): |
||
| 2490 | admin_control(req) |
||
| 2491 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2492 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2493 | description='API.INVALID_SPACE_ID') |
||
| 2494 | |||
| 2495 | if not pid.isdigit() or int(pid) <= 0: |
||
| 2496 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2497 | description='API.INVALID_POINT_ID') |
||
| 2498 | |||
| 2499 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2500 | cursor = cnx.cursor() |
||
| 2501 | |||
| 2502 | cursor.execute(" SELECT name " |
||
| 2503 | " FROM tbl_spaces " |
||
| 2504 | " WHERE id = %s ", (id_,)) |
||
| 2505 | if cursor.fetchone() is None: |
||
| 2506 | cursor.close() |
||
| 2507 | cnx.close() |
||
| 2508 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2509 | description='API.SPACE_NOT_FOUND') |
||
| 2510 | |||
| 2511 | cursor.execute(" SELECT name " |
||
| 2512 | " FROM tbl_points " |
||
| 2513 | " WHERE id = %s ", (pid,)) |
||
| 2514 | if cursor.fetchone() is None: |
||
| 2515 | cursor.close() |
||
| 2516 | cnx.close() |
||
| 2517 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2518 | description='API.POINT_NOT_FOUND') |
||
| 2519 | |||
| 2520 | cursor.execute(" SELECT id " |
||
| 2521 | " FROM tbl_spaces_points " |
||
| 2522 | " WHERE space_id = %s AND point_id = %s ", (id_, pid)) |
||
| 2523 | if cursor.fetchone() is None: |
||
| 2524 | cursor.close() |
||
| 2525 | cnx.close() |
||
| 2526 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2527 | description='API.SPACE_POINT_RELATION_NOT_FOUND') |
||
| 2528 | |||
| 2529 | cursor.execute(" DELETE FROM tbl_spaces_points " |
||
| 2530 | " WHERE space_id = %s AND point_id = %s ", (id_, pid)) |
||
| 2531 | cnx.commit() |
||
| 2532 | |||
| 2533 | cursor.close() |
||
| 2534 | cnx.close() |
||
| 2535 | |||
| 2536 | resp.status = falcon.HTTP_204 |
||
| 2537 | |||
| 2538 | |||
| 2539 | View Code Duplication | class SpaceSensorCollection: |
|
| 2540 | def __init__(self): |
||
| 2541 | """Initializes Class""" |
||
| 2542 | pass |
||
| 2543 | |||
| 2544 | @staticmethod |
||
| 2545 | def on_options(req, resp, id_): |
||
| 2546 | resp.status = falcon.HTTP_200 |
||
| 2547 | |||
| 2548 | @staticmethod |
||
| 2549 | def on_get(req, resp, id_): |
||
| 2550 | if 'API-KEY' not in req.headers or \ |
||
| 2551 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2552 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2553 | access_control(req) |
||
| 2554 | else: |
||
| 2555 | api_key_control(req) |
||
| 2556 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2557 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2558 | description='API.INVALID_SPACE_ID') |
||
| 2559 | |||
| 2560 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2561 | cursor = cnx.cursor() |
||
| 2562 | |||
| 2563 | cursor.execute(" SELECT name " |
||
| 2564 | " FROM tbl_spaces " |
||
| 2565 | " WHERE id = %s ", (id_,)) |
||
| 2566 | if cursor.fetchone() is None: |
||
| 2567 | cursor.close() |
||
| 2568 | cnx.close() |
||
| 2569 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2570 | description='API.SPACE_NOT_FOUND') |
||
| 2571 | |||
| 2572 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 2573 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
||
| 2574 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 2575 | " ORDER BY se.id ") |
||
| 2576 | cursor.execute(query, (id_,)) |
||
| 2577 | rows = cursor.fetchall() |
||
| 2578 | |||
| 2579 | result = list() |
||
| 2580 | if rows is not None and len(rows) > 0: |
||
| 2581 | for row in rows: |
||
| 2582 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2583 | result.append(meta_result) |
||
| 2584 | |||
| 2585 | resp.text = json.dumps(result) |
||
| 2586 | |||
| 2587 | @staticmethod |
||
| 2588 | @user_logger |
||
| 2589 | def on_post(req, resp, id_): |
||
| 2590 | """Handles POST requests""" |
||
| 2591 | admin_control(req) |
||
| 2592 | try: |
||
| 2593 | raw_json = req.stream.read().decode('utf-8') |
||
| 2594 | except Exception as ex: |
||
| 2595 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2596 | title='API.BAD_REQUEST', |
||
| 2597 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2598 | |||
| 2599 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2601 | description='API.INVALID_SPACE_ID') |
||
| 2602 | |||
| 2603 | new_values = json.loads(raw_json) |
||
| 2604 | |||
| 2605 | if 'sensor_id' not in new_values['data'].keys() or \ |
||
| 2606 | not isinstance(new_values['data']['sensor_id'], int) or \ |
||
| 2607 | new_values['data']['sensor_id'] <= 0: |
||
| 2608 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2609 | description='API.INVALID_SENSOR_ID') |
||
| 2610 | sensor_id = new_values['data']['sensor_id'] |
||
| 2611 | |||
| 2612 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2613 | cursor = cnx.cursor() |
||
| 2614 | |||
| 2615 | cursor.execute(" SELECT name " |
||
| 2616 | " from tbl_spaces " |
||
| 2617 | " WHERE id = %s ", (id_,)) |
||
| 2618 | if cursor.fetchone() is None: |
||
| 2619 | cursor.close() |
||
| 2620 | cnx.close() |
||
| 2621 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2622 | description='API.SPACE_NOT_FOUND') |
||
| 2623 | |||
| 2624 | cursor.execute(" SELECT name " |
||
| 2625 | " FROM tbl_sensors " |
||
| 2626 | " WHERE id = %s ", (sensor_id,)) |
||
| 2627 | if cursor.fetchone() is None: |
||
| 2628 | cursor.close() |
||
| 2629 | cnx.close() |
||
| 2630 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2631 | description='API.SENSOR_NOT_FOUND') |
||
| 2632 | |||
| 2633 | query = (" SELECT id " |
||
| 2634 | " FROM tbl_spaces_sensors " |
||
| 2635 | " WHERE space_id = %s AND sensor_id = %s") |
||
| 2636 | cursor.execute(query, (id_, sensor_id,)) |
||
| 2637 | if cursor.fetchone() is not None: |
||
| 2638 | cursor.close() |
||
| 2639 | cnx.close() |
||
| 2640 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2641 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
||
| 2642 | |||
| 2643 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
||
| 2644 | " VALUES (%s, %s) ") |
||
| 2645 | cursor.execute(add_row, (id_, sensor_id,)) |
||
| 2646 | cnx.commit() |
||
| 2647 | cursor.close() |
||
| 2648 | cnx.close() |
||
| 2649 | |||
| 2650 | resp.status = falcon.HTTP_201 |
||
| 2651 | resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id) |
||
| 2652 | |||
| 2653 | |||
| 2654 | View Code Duplication | class SpaceSensorItem: |
|
| 2655 | def __init__(self): |
||
| 2656 | """Initializes Class""" |
||
| 2657 | pass |
||
| 2658 | |||
| 2659 | @staticmethod |
||
| 2660 | def on_options(req, resp, id_, sid): |
||
| 2661 | resp.status = falcon.HTTP_200 |
||
| 2662 | |||
| 2663 | @staticmethod |
||
| 2664 | @user_logger |
||
| 2665 | def on_delete(req, resp, id_, sid): |
||
| 2666 | admin_control(req) |
||
| 2667 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2668 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2669 | description='API.INVALID_SPACE_ID') |
||
| 2670 | |||
| 2671 | if not sid.isdigit() or int(sid) <= 0: |
||
| 2672 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2673 | description='API.INVALID_SENSOR_ID') |
||
| 2674 | |||
| 2675 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2676 | cursor = cnx.cursor() |
||
| 2677 | |||
| 2678 | cursor.execute(" SELECT name " |
||
| 2679 | " FROM tbl_spaces " |
||
| 2680 | " WHERE id = %s ", (id_,)) |
||
| 2681 | if cursor.fetchone() is None: |
||
| 2682 | cursor.close() |
||
| 2683 | cnx.close() |
||
| 2684 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2685 | description='API.SPACE_NOT_FOUND') |
||
| 2686 | |||
| 2687 | cursor.execute(" SELECT name " |
||
| 2688 | " FROM tbl_sensors " |
||
| 2689 | " WHERE id = %s ", (sid,)) |
||
| 2690 | if cursor.fetchone() is None: |
||
| 2691 | cursor.close() |
||
| 2692 | cnx.close() |
||
| 2693 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2694 | description='API.SENSOR_NOT_FOUND') |
||
| 2695 | |||
| 2696 | cursor.execute(" SELECT id " |
||
| 2697 | " FROM tbl_spaces_sensors " |
||
| 2698 | " WHERE space_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 2699 | if cursor.fetchone() is None: |
||
| 2700 | cursor.close() |
||
| 2701 | cnx.close() |
||
| 2702 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2703 | description='API.SPACE_SENSOR_RELATION_NOT_FOUND') |
||
| 2704 | |||
| 2705 | cursor.execute(" DELETE FROM tbl_spaces_sensors WHERE space_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 2706 | cnx.commit() |
||
| 2707 | |||
| 2708 | cursor.close() |
||
| 2709 | cnx.close() |
||
| 2710 | |||
| 2711 | resp.status = falcon.HTTP_204 |
||
| 2712 | |||
| 2713 | |||
| 2714 | View Code Duplication | class SpaceShopfloorCollection: |
|
| 2715 | def __init__(self): |
||
| 2716 | """Initializes Class""" |
||
| 2717 | pass |
||
| 2718 | |||
| 2719 | @staticmethod |
||
| 2720 | def on_options(req, resp, id_): |
||
| 2721 | resp.status = falcon.HTTP_200 |
||
| 2722 | |||
| 2723 | @staticmethod |
||
| 2724 | def on_get(req, resp, id_): |
||
| 2725 | if 'API-KEY' not in req.headers or \ |
||
| 2726 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2727 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2728 | access_control(req) |
||
| 2729 | else: |
||
| 2730 | api_key_control(req) |
||
| 2731 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2732 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2733 | description='API.INVALID_SPACE_ID') |
||
| 2734 | |||
| 2735 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2736 | cursor = cnx.cursor() |
||
| 2737 | |||
| 2738 | cursor.execute(" SELECT name " |
||
| 2739 | " FROM tbl_spaces " |
||
| 2740 | " WHERE id = %s ", (id_,)) |
||
| 2741 | if cursor.fetchone() is None: |
||
| 2742 | cursor.close() |
||
| 2743 | cnx.close() |
||
| 2744 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2745 | description='API.SPACE_NOT_FOUND') |
||
| 2746 | |||
| 2747 | query = (" SELECT sf.id, sf.name, sf.uuid " |
||
| 2748 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
||
| 2749 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
||
| 2750 | " ORDER BY sf.id ") |
||
| 2751 | cursor.execute(query, (id_,)) |
||
| 2752 | rows = cursor.fetchall() |
||
| 2753 | |||
| 2754 | result = list() |
||
| 2755 | if rows is not None and len(rows) > 0: |
||
| 2756 | for row in rows: |
||
| 2757 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2758 | result.append(meta_result) |
||
| 2759 | |||
| 2760 | resp.text = json.dumps(result) |
||
| 2761 | |||
| 2762 | @staticmethod |
||
| 2763 | @user_logger |
||
| 2764 | def on_post(req, resp, id_): |
||
| 2765 | """Handles POST requests""" |
||
| 2766 | admin_control(req) |
||
| 2767 | try: |
||
| 2768 | raw_json = req.stream.read().decode('utf-8') |
||
| 2769 | except Exception as ex: |
||
| 2770 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2771 | title='API.BAD_REQUEST', |
||
| 2772 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2773 | |||
| 2774 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2775 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2776 | description='API.INVALID_SPACE_ID') |
||
| 2777 | |||
| 2778 | new_values = json.loads(raw_json) |
||
| 2779 | |||
| 2780 | if 'shopfloor_id' not in new_values['data'].keys() or \ |
||
| 2781 | not isinstance(new_values['data']['shopfloor_id'], int) or \ |
||
| 2782 | new_values['data']['shopfloor_id'] <= 0: |
||
| 2783 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2784 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 2785 | shopfloor_id = new_values['data']['shopfloor_id'] |
||
| 2786 | |||
| 2787 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2788 | cursor = cnx.cursor() |
||
| 2789 | |||
| 2790 | cursor.execute(" SELECT name " |
||
| 2791 | " from tbl_spaces " |
||
| 2792 | " WHERE id = %s ", (id_,)) |
||
| 2793 | if cursor.fetchone() is None: |
||
| 2794 | cursor.close() |
||
| 2795 | cnx.close() |
||
| 2796 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2797 | description='API.SPACE_NOT_FOUND') |
||
| 2798 | |||
| 2799 | cursor.execute(" SELECT name " |
||
| 2800 | " FROM tbl_shopfloors " |
||
| 2801 | " WHERE id = %s ", (shopfloor_id,)) |
||
| 2802 | if cursor.fetchone() is None: |
||
| 2803 | cursor.close() |
||
| 2804 | cnx.close() |
||
| 2805 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2806 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2807 | |||
| 2808 | query = (" SELECT id " |
||
| 2809 | " FROM tbl_spaces_shopfloors " |
||
| 2810 | " WHERE space_id = %s AND shopfloor_id = %s") |
||
| 2811 | cursor.execute(query, (id_, shopfloor_id,)) |
||
| 2812 | if cursor.fetchone() is not None: |
||
| 2813 | cursor.close() |
||
| 2814 | cnx.close() |
||
| 2815 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2816 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
||
| 2817 | |||
| 2818 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
||
| 2819 | " VALUES (%s, %s) ") |
||
| 2820 | cursor.execute(add_row, (id_, shopfloor_id,)) |
||
| 2821 | cnx.commit() |
||
| 2822 | cursor.close() |
||
| 2823 | cnx.close() |
||
| 2824 | |||
| 2825 | resp.status = falcon.HTTP_201 |
||
| 2826 | resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id) |
||
| 2827 | |||
| 2828 | |||
| 2829 | View Code Duplication | class SpaceShopfloorItem: |
|
| 2830 | def __init__(self): |
||
| 2831 | """Initializes Class""" |
||
| 2832 | pass |
||
| 2833 | |||
| 2834 | @staticmethod |
||
| 2835 | def on_options(req, resp, id_, sid): |
||
| 2836 | resp.status = falcon.HTTP_200 |
||
| 2837 | |||
| 2838 | @staticmethod |
||
| 2839 | @user_logger |
||
| 2840 | def on_delete(req, resp, id_, sid): |
||
| 2841 | admin_control(req) |
||
| 2842 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2843 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2844 | description='API.INVALID_SPACE_ID') |
||
| 2845 | |||
| 2846 | if not sid.isdigit() or int(sid) <= 0: |
||
| 2847 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2848 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 2849 | |||
| 2850 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2851 | cursor = cnx.cursor() |
||
| 2852 | |||
| 2853 | cursor.execute(" SELECT name " |
||
| 2854 | " FROM tbl_spaces " |
||
| 2855 | " WHERE id = %s ", (id_,)) |
||
| 2856 | if cursor.fetchone() is None: |
||
| 2857 | cursor.close() |
||
| 2858 | cnx.close() |
||
| 2859 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2860 | description='API.SPACE_NOT_FOUND') |
||
| 2861 | |||
| 2862 | cursor.execute(" SELECT name " |
||
| 2863 | " FROM tbl_shopfloors " |
||
| 2864 | " WHERE id = %s ", (sid,)) |
||
| 2865 | if cursor.fetchone() is None: |
||
| 2866 | cursor.close() |
||
| 2867 | cnx.close() |
||
| 2868 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2869 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2870 | |||
| 2871 | cursor.execute(" SELECT id " |
||
| 2872 | " FROM tbl_spaces_shopfloors " |
||
| 2873 | " WHERE space_id = %s AND shopfloor_id = %s ", (id_, sid)) |
||
| 2874 | if cursor.fetchone() is None: |
||
| 2875 | cursor.close() |
||
| 2876 | cnx.close() |
||
| 2877 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2878 | description='API.SPACE_SHOPFLOOR_RELATION_NOT_FOUND') |
||
| 2879 | |||
| 2880 | cursor.execute(" DELETE FROM tbl_spaces_shopfloors WHERE space_id = %s AND shopfloor_id = %s ", (id_, sid)) |
||
| 2881 | cnx.commit() |
||
| 2882 | |||
| 2883 | cursor.close() |
||
| 2884 | cnx.close() |
||
| 2885 | |||
| 2886 | resp.status = falcon.HTTP_204 |
||
| 2887 | |||
| 2888 | |||
| 2889 | View Code Duplication | class SpaceStoreCollection: |
|
| 2890 | def __init__(self): |
||
| 2891 | """Initializes Class""" |
||
| 2892 | pass |
||
| 2893 | |||
| 2894 | @staticmethod |
||
| 2895 | def on_options(req, resp, id_): |
||
| 2896 | resp.status = falcon.HTTP_200 |
||
| 2897 | |||
| 2898 | @staticmethod |
||
| 2899 | def on_get(req, resp, id_): |
||
| 2900 | if 'API-KEY' not in req.headers or \ |
||
| 2901 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2902 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2903 | access_control(req) |
||
| 2904 | else: |
||
| 2905 | api_key_control(req) |
||
| 2906 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2907 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2908 | description='API.INVALID_SPACE_ID') |
||
| 2909 | |||
| 2910 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2911 | cursor = cnx.cursor() |
||
| 2912 | |||
| 2913 | cursor.execute(" SELECT name " |
||
| 2914 | " FROM tbl_spaces " |
||
| 2915 | " WHERE id = %s ", (id_,)) |
||
| 2916 | if cursor.fetchone() is None: |
||
| 2917 | cursor.close() |
||
| 2918 | cnx.close() |
||
| 2919 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2920 | description='API.SPACE_NOT_FOUND') |
||
| 2921 | |||
| 2922 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 2923 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
||
| 2924 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
||
| 2925 | " ORDER BY t.id ") |
||
| 2926 | cursor.execute(query, (id_,)) |
||
| 2927 | rows = cursor.fetchall() |
||
| 2928 | |||
| 2929 | result = list() |
||
| 2930 | if rows is not None and len(rows) > 0: |
||
| 2931 | for row in rows: |
||
| 2932 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2933 | result.append(meta_result) |
||
| 2934 | |||
| 2935 | resp.text = json.dumps(result) |
||
| 2936 | |||
| 2937 | @staticmethod |
||
| 2938 | @user_logger |
||
| 2939 | def on_post(req, resp, id_): |
||
| 2940 | """Handles POST requests""" |
||
| 2941 | admin_control(req) |
||
| 2942 | try: |
||
| 2943 | raw_json = req.stream.read().decode('utf-8') |
||
| 2944 | except Exception as ex: |
||
| 2945 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2946 | title='API.BAD_REQUEST', |
||
| 2947 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2948 | |||
| 2949 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2950 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2951 | description='API.INVALID_SPACE_ID') |
||
| 2952 | |||
| 2953 | new_values = json.loads(raw_json) |
||
| 2954 | |||
| 2955 | if 'store_id' not in new_values['data'].keys() or \ |
||
| 2956 | not isinstance(new_values['data']['store_id'], int) or \ |
||
| 2957 | new_values['data']['store_id'] <= 0: |
||
| 2958 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2959 | description='API.INVALID_STORE_ID') |
||
| 2960 | store_id = new_values['data']['store_id'] |
||
| 2961 | |||
| 2962 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2963 | cursor = cnx.cursor() |
||
| 2964 | |||
| 2965 | cursor.execute(" SELECT name " |
||
| 2966 | " from tbl_spaces " |
||
| 2967 | " WHERE id = %s ", (id_,)) |
||
| 2968 | if cursor.fetchone() is None: |
||
| 2969 | cursor.close() |
||
| 2970 | cnx.close() |
||
| 2971 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2972 | description='API.SPACE_NOT_FOUND') |
||
| 2973 | |||
| 2974 | cursor.execute(" SELECT name " |
||
| 2975 | " FROM tbl_stores " |
||
| 2976 | " WHERE id = %s ", (store_id,)) |
||
| 2977 | if cursor.fetchone() is None: |
||
| 2978 | cursor.close() |
||
| 2979 | cnx.close() |
||
| 2980 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2981 | description='API.STORE_NOT_FOUND') |
||
| 2982 | |||
| 2983 | query = (" SELECT id " |
||
| 2984 | " FROM tbl_spaces_stores " |
||
| 2985 | " WHERE space_id = %s AND store_id = %s") |
||
| 2986 | cursor.execute(query, (id_, store_id,)) |
||
| 2987 | if cursor.fetchone() is not None: |
||
| 2988 | cursor.close() |
||
| 2989 | cnx.close() |
||
| 2990 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2991 | description='API.SPACE_STORE_RELATION_EXISTS') |
||
| 2992 | |||
| 2993 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
||
| 2994 | " VALUES (%s, %s) ") |
||
| 2995 | cursor.execute(add_row, (id_, store_id,)) |
||
| 2996 | cnx.commit() |
||
| 2997 | cursor.close() |
||
| 2998 | cnx.close() |
||
| 2999 | |||
| 3000 | resp.status = falcon.HTTP_201 |
||
| 3001 | resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id) |
||
| 3002 | |||
| 3003 | |||
| 3004 | View Code Duplication | class SpaceStoreItem: |
|
| 3005 | def __init__(self): |
||
| 3006 | """Initializes Class""" |
||
| 3007 | pass |
||
| 3008 | |||
| 3009 | @staticmethod |
||
| 3010 | def on_options(req, resp, id_, tid): |
||
| 3011 | resp.status = falcon.HTTP_200 |
||
| 3012 | |||
| 3013 | @staticmethod |
||
| 3014 | @user_logger |
||
| 3015 | def on_delete(req, resp, id_, tid): |
||
| 3016 | admin_control(req) |
||
| 3017 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3018 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3019 | description='API.INVALID_SPACE_ID') |
||
| 3020 | |||
| 3021 | if not tid.isdigit() or int(tid) <= 0: |
||
| 3022 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3023 | description='API.INVALID_STORE_ID') |
||
| 3024 | |||
| 3025 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3026 | cursor = cnx.cursor() |
||
| 3027 | |||
| 3028 | cursor.execute(" SELECT name " |
||
| 3029 | " FROM tbl_spaces " |
||
| 3030 | " WHERE id = %s ", (id_,)) |
||
| 3031 | if cursor.fetchone() is None: |
||
| 3032 | cursor.close() |
||
| 3033 | cnx.close() |
||
| 3034 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3035 | description='API.SPACE_NOT_FOUND') |
||
| 3036 | |||
| 3037 | cursor.execute(" SELECT name " |
||
| 3038 | " FROM tbl_stores " |
||
| 3039 | " WHERE id = %s ", (tid,)) |
||
| 3040 | if cursor.fetchone() is None: |
||
| 3041 | cursor.close() |
||
| 3042 | cnx.close() |
||
| 3043 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3044 | description='API.STORE_NOT_FOUND') |
||
| 3045 | |||
| 3046 | cursor.execute(" SELECT id " |
||
| 3047 | " FROM tbl_spaces_stores " |
||
| 3048 | " WHERE space_id = %s AND store_id = %s ", (id_, tid)) |
||
| 3049 | if cursor.fetchone() is None: |
||
| 3050 | cursor.close() |
||
| 3051 | cnx.close() |
||
| 3052 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3053 | description='API.SPACE_STORE_RELATION_NOT_FOUND') |
||
| 3054 | |||
| 3055 | cursor.execute(" DELETE FROM tbl_spaces_stores WHERE space_id = %s AND store_id = %s ", (id_, tid)) |
||
| 3056 | cnx.commit() |
||
| 3057 | |||
| 3058 | cursor.close() |
||
| 3059 | cnx.close() |
||
| 3060 | |||
| 3061 | resp.status = falcon.HTTP_204 |
||
| 3062 | |||
| 3063 | |||
| 3064 | View Code Duplication | class SpaceTenantCollection: |
|
| 3065 | def __init__(self): |
||
| 3066 | """Initializes Class""" |
||
| 3067 | pass |
||
| 3068 | |||
| 3069 | @staticmethod |
||
| 3070 | def on_options(req, resp, id_): |
||
| 3071 | resp.status = falcon.HTTP_200 |
||
| 3072 | |||
| 3073 | @staticmethod |
||
| 3074 | def on_get(req, resp, id_): |
||
| 3075 | if 'API-KEY' not in req.headers or \ |
||
| 3076 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3077 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3078 | access_control(req) |
||
| 3079 | else: |
||
| 3080 | api_key_control(req) |
||
| 3081 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3082 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3083 | description='API.INVALID_SPACE_ID') |
||
| 3084 | |||
| 3085 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3086 | cursor = cnx.cursor() |
||
| 3087 | |||
| 3088 | cursor.execute(" SELECT name " |
||
| 3089 | " FROM tbl_spaces " |
||
| 3090 | " WHERE id = %s ", (id_,)) |
||
| 3091 | if cursor.fetchone() is None: |
||
| 3092 | cursor.close() |
||
| 3093 | cnx.close() |
||
| 3094 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3095 | description='API.SPACE_NOT_FOUND') |
||
| 3096 | |||
| 3097 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 3098 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
||
| 3099 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
||
| 3100 | " ORDER BY t.id ") |
||
| 3101 | cursor.execute(query, (id_,)) |
||
| 3102 | rows = cursor.fetchall() |
||
| 3103 | |||
| 3104 | result = list() |
||
| 3105 | if rows is not None and len(rows) > 0: |
||
| 3106 | for row in rows: |
||
| 3107 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3108 | result.append(meta_result) |
||
| 3109 | |||
| 3110 | resp.text = json.dumps(result) |
||
| 3111 | |||
| 3112 | @staticmethod |
||
| 3113 | @user_logger |
||
| 3114 | def on_post(req, resp, id_): |
||
| 3115 | """Handles POST requests""" |
||
| 3116 | admin_control(req) |
||
| 3117 | try: |
||
| 3118 | raw_json = req.stream.read().decode('utf-8') |
||
| 3119 | except Exception as ex: |
||
| 3120 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3121 | title='API.BAD_REQUEST', |
||
| 3122 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3123 | |||
| 3124 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3126 | description='API.INVALID_SPACE_ID') |
||
| 3127 | |||
| 3128 | new_values = json.loads(raw_json) |
||
| 3129 | |||
| 3130 | if 'tenant_id' not in new_values['data'].keys() or \ |
||
| 3131 | not isinstance(new_values['data']['tenant_id'], int) or \ |
||
| 3132 | new_values['data']['tenant_id'] <= 0: |
||
| 3133 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3134 | description='API.INVALID_TENANT_ID') |
||
| 3135 | tenant_id = new_values['data']['tenant_id'] |
||
| 3136 | |||
| 3137 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3138 | cursor = cnx.cursor() |
||
| 3139 | |||
| 3140 | cursor.execute(" SELECT name " |
||
| 3141 | " from tbl_spaces " |
||
| 3142 | " WHERE id = %s ", (id_,)) |
||
| 3143 | if cursor.fetchone() is None: |
||
| 3144 | cursor.close() |
||
| 3145 | cnx.close() |
||
| 3146 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3147 | description='API.SPACE_NOT_FOUND') |
||
| 3148 | |||
| 3149 | cursor.execute(" SELECT name " |
||
| 3150 | " FROM tbl_tenants " |
||
| 3151 | " WHERE id = %s ", (tenant_id,)) |
||
| 3152 | if cursor.fetchone() is None: |
||
| 3153 | cursor.close() |
||
| 3154 | cnx.close() |
||
| 3155 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3156 | description='API.TENANT_NOT_FOUND') |
||
| 3157 | |||
| 3158 | query = (" SELECT id " |
||
| 3159 | " FROM tbl_spaces_tenants " |
||
| 3160 | " WHERE space_id = %s AND tenant_id = %s") |
||
| 3161 | cursor.execute(query, (id_, tenant_id,)) |
||
| 3162 | if cursor.fetchone() is not None: |
||
| 3163 | cursor.close() |
||
| 3164 | cnx.close() |
||
| 3165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3166 | description='API.SPACE_TENANT_RELATION_EXISTS') |
||
| 3167 | |||
| 3168 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
||
| 3169 | " VALUES (%s, %s) ") |
||
| 3170 | cursor.execute(add_row, (id_, tenant_id,)) |
||
| 3171 | cnx.commit() |
||
| 3172 | cursor.close() |
||
| 3173 | cnx.close() |
||
| 3174 | |||
| 3175 | resp.status = falcon.HTTP_201 |
||
| 3176 | resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id) |
||
| 3177 | |||
| 3178 | |||
| 3179 | View Code Duplication | class SpaceTenantItem: |
|
| 3180 | def __init__(self): |
||
| 3181 | """Initializes Class""" |
||
| 3182 | pass |
||
| 3183 | |||
| 3184 | @staticmethod |
||
| 3185 | def on_options(req, resp, id_, tid): |
||
| 3186 | resp.status = falcon.HTTP_200 |
||
| 3187 | |||
| 3188 | @staticmethod |
||
| 3189 | @user_logger |
||
| 3190 | def on_delete(req, resp, id_, tid): |
||
| 3191 | admin_control(req) |
||
| 3192 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3193 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3194 | description='API.INVALID_SPACE_ID') |
||
| 3195 | |||
| 3196 | if not tid.isdigit() or int(tid) <= 0: |
||
| 3197 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3198 | description='API.INVALID_TENANT_ID') |
||
| 3199 | |||
| 3200 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3201 | cursor = cnx.cursor() |
||
| 3202 | |||
| 3203 | cursor.execute(" SELECT name " |
||
| 3204 | " FROM tbl_spaces " |
||
| 3205 | " WHERE id = %s ", (id_,)) |
||
| 3206 | if cursor.fetchone() is None: |
||
| 3207 | cursor.close() |
||
| 3208 | cnx.close() |
||
| 3209 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3210 | description='API.SPACE_NOT_FOUND') |
||
| 3211 | |||
| 3212 | cursor.execute(" SELECT name " |
||
| 3213 | " FROM tbl_tenants " |
||
| 3214 | " WHERE id = %s ", (tid,)) |
||
| 3215 | if cursor.fetchone() is None: |
||
| 3216 | cursor.close() |
||
| 3217 | cnx.close() |
||
| 3218 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3219 | description='API.TENANT_NOT_FOUND') |
||
| 3220 | |||
| 3221 | cursor.execute(" SELECT id " |
||
| 3222 | " FROM tbl_spaces_tenants " |
||
| 3223 | " WHERE space_id = %s AND tenant_id = %s ", (id_, tid)) |
||
| 3224 | if cursor.fetchone() is None: |
||
| 3225 | cursor.close() |
||
| 3226 | cnx.close() |
||
| 3227 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3228 | description='API.SPACE_TENANT_RELATION_NOT_FOUND') |
||
| 3229 | |||
| 3230 | cursor.execute(" DELETE FROM tbl_spaces_tenants WHERE space_id = %s AND tenant_id = %s ", (id_, tid)) |
||
| 3231 | cnx.commit() |
||
| 3232 | |||
| 3233 | cursor.close() |
||
| 3234 | cnx.close() |
||
| 3235 | |||
| 3236 | resp.status = falcon.HTTP_204 |
||
| 3237 | |||
| 3238 | |||
| 3239 | View Code Duplication | class SpaceVirtualMeterCollection: |
|
| 3240 | def __init__(self): |
||
| 3241 | """Initializes Class""" |
||
| 3242 | pass |
||
| 3243 | |||
| 3244 | @staticmethod |
||
| 3245 | def on_options(req, resp, id_): |
||
| 3246 | resp.status = falcon.HTTP_200 |
||
| 3247 | |||
| 3248 | @staticmethod |
||
| 3249 | def on_get(req, resp, id_): |
||
| 3250 | if 'API-KEY' not in req.headers or \ |
||
| 3251 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3252 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3253 | access_control(req) |
||
| 3254 | else: |
||
| 3255 | api_key_control(req) |
||
| 3256 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3257 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3258 | description='API.INVALID_SPACE_ID') |
||
| 3259 | |||
| 3260 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3261 | cursor = cnx.cursor() |
||
| 3262 | |||
| 3263 | cursor.execute(" SELECT name " |
||
| 3264 | " FROM tbl_spaces " |
||
| 3265 | " WHERE id = %s ", (id_,)) |
||
| 3266 | if cursor.fetchone() is None: |
||
| 3267 | cursor.close() |
||
| 3268 | cnx.close() |
||
| 3269 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3270 | description='API.SPACE_NOT_FOUND') |
||
| 3271 | |||
| 3272 | query = (" SELECT id, name, uuid " |
||
| 3273 | " FROM tbl_energy_categories ") |
||
| 3274 | cursor.execute(query) |
||
| 3275 | rows_energy_categories = cursor.fetchall() |
||
| 3276 | |||
| 3277 | energy_category_dict = dict() |
||
| 3278 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3279 | for row in rows_energy_categories: |
||
| 3280 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 3281 | "name": row[1], |
||
| 3282 | "uuid": row[2]} |
||
| 3283 | |||
| 3284 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 3285 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
||
| 3286 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 3287 | " ORDER BY m.id ") |
||
| 3288 | cursor.execute(query, (id_,)) |
||
| 3289 | rows = cursor.fetchall() |
||
| 3290 | |||
| 3291 | result = list() |
||
| 3292 | if rows is not None and len(rows) > 0: |
||
| 3293 | for row in rows: |
||
| 3294 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 3295 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 3296 | result.append(meta_result) |
||
| 3297 | |||
| 3298 | resp.text = json.dumps(result) |
||
| 3299 | |||
| 3300 | @staticmethod |
||
| 3301 | @user_logger |
||
| 3302 | def on_post(req, resp, id_): |
||
| 3303 | """Handles POST requests""" |
||
| 3304 | admin_control(req) |
||
| 3305 | try: |
||
| 3306 | raw_json = req.stream.read().decode('utf-8') |
||
| 3307 | except Exception as ex: |
||
| 3308 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3309 | title='API.BAD_REQUEST', |
||
| 3310 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3311 | |||
| 3312 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3313 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3314 | description='API.INVALID_SPACE_ID') |
||
| 3315 | |||
| 3316 | new_values = json.loads(raw_json) |
||
| 3317 | |||
| 3318 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 3319 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 3320 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 3321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3322 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 3323 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 3324 | |||
| 3325 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3326 | cursor = cnx.cursor() |
||
| 3327 | |||
| 3328 | cursor.execute(" SELECT name " |
||
| 3329 | " from tbl_spaces " |
||
| 3330 | " WHERE id = %s ", (id_,)) |
||
| 3331 | if cursor.fetchone() is None: |
||
| 3332 | cursor.close() |
||
| 3333 | cnx.close() |
||
| 3334 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3335 | description='API.SPACE_NOT_FOUND') |
||
| 3336 | |||
| 3337 | cursor.execute(" SELECT name " |
||
| 3338 | " FROM tbl_virtual_meters " |
||
| 3339 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 3340 | if cursor.fetchone() is None: |
||
| 3341 | cursor.close() |
||
| 3342 | cnx.close() |
||
| 3343 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3344 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 3345 | |||
| 3346 | query = (" SELECT id " |
||
| 3347 | " FROM tbl_spaces_virtual_meters " |
||
| 3348 | " WHERE space_id = %s AND virtual_meter_id = %s") |
||
| 3349 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 3350 | if cursor.fetchone() is not None: |
||
| 3351 | cursor.close() |
||
| 3352 | cnx.close() |
||
| 3353 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3354 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTS') |
||
| 3355 | |||
| 3356 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
||
| 3357 | " VALUES (%s, %s) ") |
||
| 3358 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
||
| 3359 | cnx.commit() |
||
| 3360 | cursor.close() |
||
| 3361 | cnx.close() |
||
| 3362 | |||
| 3363 | resp.status = falcon.HTTP_201 |
||
| 3364 | resp.location = '/spaces/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 3365 | |||
| 3366 | |||
| 3367 | View Code Duplication | class SpaceVirtualMeterItem: |
|
| 3368 | def __init__(self): |
||
| 3369 | """Initializes Class""" |
||
| 3370 | pass |
||
| 3371 | |||
| 3372 | @staticmethod |
||
| 3373 | def on_options(req, resp, id_, mid): |
||
| 3374 | resp.status = falcon.HTTP_200 |
||
| 3375 | |||
| 3376 | @staticmethod |
||
| 3377 | @user_logger |
||
| 3378 | def on_delete(req, resp, id_, mid): |
||
| 3379 | admin_control(req) |
||
| 3380 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3381 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3382 | description='API.INVALID_SPACE_ID') |
||
| 3383 | |||
| 3384 | if not mid.isdigit() or int(mid) <= 0: |
||
| 3385 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3386 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 3387 | |||
| 3388 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3389 | cursor = cnx.cursor() |
||
| 3390 | |||
| 3391 | cursor.execute(" SELECT name " |
||
| 3392 | " FROM tbl_spaces " |
||
| 3393 | " WHERE id = %s ", (id_,)) |
||
| 3394 | if cursor.fetchone() is None: |
||
| 3395 | cursor.close() |
||
| 3396 | cnx.close() |
||
| 3397 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3398 | description='API.SPACE_NOT_FOUND') |
||
| 3399 | |||
| 3400 | cursor.execute(" SELECT name " |
||
| 3401 | " FROM tbl_virtual_meters " |
||
| 3402 | " WHERE id = %s ", (mid,)) |
||
| 3403 | if cursor.fetchone() is None: |
||
| 3404 | cursor.close() |
||
| 3405 | cnx.close() |
||
| 3406 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3407 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 3408 | |||
| 3409 | cursor.execute(" SELECT id " |
||
| 3410 | " FROM tbl_spaces_virtual_meters " |
||
| 3411 | " WHERE space_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 3412 | if cursor.fetchone() is None: |
||
| 3413 | cursor.close() |
||
| 3414 | cnx.close() |
||
| 3415 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3416 | description='API.SPACE_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 3417 | |||
| 3418 | cursor.execute(" DELETE FROM tbl_spaces_virtual_meters " |
||
| 3419 | " WHERE space_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 3420 | cnx.commit() |
||
| 3421 | |||
| 3422 | cursor.close() |
||
| 3423 | cnx.close() |
||
| 3424 | |||
| 3425 | resp.status = falcon.HTTP_204 |
||
| 3426 | |||
| 3427 | |||
| 3428 | class SpaceTreeCollection: |
||
| 3429 | def __init__(self): |
||
| 3430 | """Initializes Class""" |
||
| 3431 | pass |
||
| 3432 | |||
| 3433 | @staticmethod |
||
| 3434 | def on_options(req, resp): |
||
| 3435 | resp.status = falcon.HTTP_200 |
||
| 3436 | |||
| 3437 | @staticmethod |
||
| 3438 | def on_get(req, resp): |
||
| 3439 | access_control(req) |
||
| 3440 | if 'USER-UUID' not in req.headers or \ |
||
| 3441 | not isinstance(req.headers['USER-UUID'], str) or \ |
||
| 3442 | len(str.strip(req.headers['USER-UUID'])) == 0: |
||
| 3443 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3444 | description='API.INVALID_USER_UUID') |
||
| 3445 | user_uuid = str.strip(req.headers['USER-UUID']) |
||
| 3446 | |||
| 3447 | if 'TOKEN' not in req.headers or \ |
||
| 3448 | not isinstance(req.headers['TOKEN'], str) or \ |
||
| 3449 | len(str.strip(req.headers['TOKEN'])) == 0: |
||
| 3450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3451 | description='API.INVALID_TOKEN') |
||
| 3452 | token = str.strip(req.headers['TOKEN']) |
||
| 3453 | |||
| 3454 | # Verify User Session |
||
| 3455 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 3456 | cursor = cnx.cursor() |
||
| 3457 | query = (" SELECT utc_expires " |
||
| 3458 | " FROM tbl_sessions " |
||
| 3459 | " WHERE user_uuid = %s AND token = %s") |
||
| 3460 | cursor.execute(query, (user_uuid, token,)) |
||
| 3461 | row = cursor.fetchone() |
||
| 3462 | |||
| 3463 | if row is None: |
||
| 3464 | cursor.close() |
||
| 3465 | cnx.close() |
||
| 3466 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3467 | description='API.USER_SESSION_NOT_FOUND') |
||
| 3468 | else: |
||
| 3469 | utc_expires = row[0] |
||
| 3470 | if datetime.utcnow() > utc_expires: |
||
| 3471 | cursor.close() |
||
| 3472 | cnx.close() |
||
| 3473 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3474 | description='API.USER_SESSION_TIMEOUT') |
||
| 3475 | # get privilege |
||
| 3476 | query = (" SELECT is_admin, privilege_id " |
||
| 3477 | " FROM tbl_users " |
||
| 3478 | " WHERE uuid = %s ") |
||
| 3479 | cursor.execute(query, (user_uuid,)) |
||
| 3480 | row = cursor.fetchone() |
||
| 3481 | if row is None: |
||
| 3482 | cursor.close() |
||
| 3483 | cnx.close() |
||
| 3484 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.USER_NOT_FOUND') |
||
| 3485 | else: |
||
| 3486 | is_admin = bool(row[0]) |
||
| 3487 | privilege_id = row[1] |
||
| 3488 | |||
| 3489 | # get space_id in privilege |
||
| 3490 | if is_admin: |
||
| 3491 | space_id = 1 |
||
| 3492 | elif privilege_id is None: |
||
| 3493 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3494 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 3495 | else: |
||
| 3496 | query = (" SELECT data " |
||
| 3497 | " FROM tbl_privileges " |
||
| 3498 | " WHERE id = %s ") |
||
| 3499 | cursor.execute(query, (privilege_id,)) |
||
| 3500 | row = cursor.fetchone() |
||
| 3501 | cursor.close() |
||
| 3502 | cnx.close() |
||
| 3503 | |||
| 3504 | if row is None: |
||
| 3505 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3506 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 3507 | try: |
||
| 3508 | data = json.loads(row[0]) |
||
| 3509 | except Exception as ex: |
||
| 3510 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', description=str(ex)) |
||
| 3511 | |||
| 3512 | if 'spaces' not in data or len(data['spaces']) == 0: |
||
| 3513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3514 | description='API.SPACE_NOT_FOUND_IN_PRIVILEGE') |
||
| 3515 | |||
| 3516 | space_id = data['spaces'][0] |
||
| 3517 | if space_id is None: |
||
| 3518 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3519 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 3520 | # get all spaces |
||
| 3521 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3522 | cursor = cnx.cursor() |
||
| 3523 | |||
| 3524 | query = (" SELECT id, name, parent_space_id " |
||
| 3525 | " FROM tbl_spaces " |
||
| 3526 | " ORDER BY id ") |
||
| 3527 | cursor.execute(query) |
||
| 3528 | rows_spaces = cursor.fetchall() |
||
| 3529 | node_dict = dict() |
||
| 3530 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 3531 | for row in rows_spaces: |
||
| 3532 | parent_node = node_dict[row[2]] if row[2] is not None else None |
||
| 3533 | node_dict[row[0]] = AnyNode(id=row[0], parent=parent_node, name=row[1]) |
||
| 3534 | |||
| 3535 | cursor.close() |
||
| 3536 | cnx.close() |
||
| 3537 | resp.text = JsonExporter(sort_keys=True).export(node_dict[space_id], ) |
||
| 3538 | |||
| 3539 | |||
| 3540 | # Get energy categories of all meters in the space tree |
||
| 3541 | class SpaceTreeMetersEnergyCategoryCollection: |
||
| 3542 | def __init__(self): |
||
| 3543 | """Initializes Class""" |
||
| 3544 | pass |
||
| 3545 | |||
| 3546 | @staticmethod |
||
| 3547 | def on_options(req, resp, id_): |
||
| 3548 | resp.status = falcon.HTTP_200 |
||
| 3549 | |||
| 3550 | @staticmethod |
||
| 3551 | def on_get(req, resp, id_): |
||
| 3552 | if 'API-KEY' not in req.headers or \ |
||
| 3553 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3554 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3555 | access_control(req) |
||
| 3556 | else: |
||
| 3557 | api_key_control(req) |
||
| 3558 | ################################################################################################################ |
||
| 3559 | # Step 1: valid parameters |
||
| 3560 | ################################################################################################################ |
||
| 3561 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3562 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3563 | description='API.INVALID_SPACE_ID') |
||
| 3564 | |||
| 3565 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3566 | cursor = cnx.cursor() |
||
| 3567 | |||
| 3568 | cursor.execute(" SELECT name " |
||
| 3569 | " FROM tbl_spaces " |
||
| 3570 | " WHERE id = %s ", (id_,)) |
||
| 3571 | if cursor.fetchone() is None: |
||
| 3572 | cursor.close() |
||
| 3573 | cnx.close() |
||
| 3574 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3575 | description='API.SPACE_NOT_FOUND') |
||
| 3576 | |||
| 3577 | ################################################################################################################ |
||
| 3578 | # Step 2: build a space tree |
||
| 3579 | ################################################################################################################ |
||
| 3580 | query = (" SELECT id, name, parent_space_id " |
||
| 3581 | " FROM tbl_spaces " |
||
| 3582 | " ORDER BY id ") |
||
| 3583 | cursor.execute(query) |
||
| 3584 | rows_spaces = cursor.fetchall() |
||
| 3585 | node_dict = dict() |
||
| 3586 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 3587 | for row in rows_spaces: |
||
| 3588 | parent_node = node_dict[row[2]] if row[2] is not None else None |
||
| 3589 | node_dict[row[0]] = AnyNode(id=row[0], parent=parent_node, name=row[1]) |
||
| 3590 | ################################################################################################################ |
||
| 3591 | # Step 3: query energy categories of all meters in the space tree |
||
| 3592 | ################################################################################################################ |
||
| 3593 | space_dict = dict() |
||
| 3594 | |||
| 3595 | for node in LevelOrderIter(node_dict[int(id_)]): |
||
| 3596 | space_dict[node.id] = node.name |
||
| 3597 | |||
| 3598 | cursor.execute(" SELECT distinct(m.energy_category_id), ec.name AS energy_category_name, ec.uuid " |
||
| 3599 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m, tbl_energy_categories ec " |
||
| 3600 | " WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") " |
||
| 3601 | " AND sm.space_id = s.id AND sm.meter_id = m.id AND m.energy_category_id = ec.id ", ) |
||
| 3602 | rows_energy_categories = cursor.fetchall() |
||
| 3603 | |||
| 3604 | result = list() |
||
| 3605 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 3606 | for row in rows_energy_categories: |
||
| 3607 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3608 | result.append(meta_result) |
||
| 3609 | |||
| 3610 | resp.text = json.dumps(result) |
||
| 3611 | |||
| 3612 | |||
| 3613 | View Code Duplication | class SpaceWorkingCalendarCollection: |
|
| 3614 | def __init__(self): |
||
| 3615 | """Initializes SpaceWorkingCalendarCollection Class""" |
||
| 3616 | pass |
||
| 3617 | |||
| 3618 | @staticmethod |
||
| 3619 | def on_options(req, resp, id_): |
||
| 3620 | resp.status = falcon.HTTP_200 |
||
| 3621 | |||
| 3622 | @staticmethod |
||
| 3623 | def on_get(req, resp, id_): |
||
| 3624 | if 'API-KEY' not in req.headers or \ |
||
| 3625 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3626 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3627 | access_control(req) |
||
| 3628 | else: |
||
| 3629 | api_key_control(req) |
||
| 3630 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3631 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3632 | description='API.INVALID_SPACE_ID') |
||
| 3633 | |||
| 3634 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3635 | cursor = cnx.cursor() |
||
| 3636 | |||
| 3637 | cursor.execute(" SELECT name " |
||
| 3638 | " FROM tbl_spaces " |
||
| 3639 | " WHERE id = %s ", (id_,)) |
||
| 3640 | if cursor.fetchone() is None: |
||
| 3641 | cursor.close() |
||
| 3642 | cnx.close() |
||
| 3643 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3644 | description='API.SPACE_NOT_FOUND') |
||
| 3645 | |||
| 3646 | query = (" SELECT wc.id, wc.name, wc.description " |
||
| 3647 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
||
| 3648 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 3649 | " ORDER BY wc.id ") |
||
| 3650 | cursor.execute(query, (id_,)) |
||
| 3651 | rows = cursor.fetchall() |
||
| 3652 | |||
| 3653 | result = list() |
||
| 3654 | if rows is not None and len(rows) > 0: |
||
| 3655 | for row in rows: |
||
| 3656 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
||
| 3657 | result.append(meta_result) |
||
| 3658 | |||
| 3659 | resp.text = json.dumps(result) |
||
| 3660 | |||
| 3661 | @staticmethod |
||
| 3662 | @user_logger |
||
| 3663 | def on_post(req, resp, id_): |
||
| 3664 | """Handles POST requests""" |
||
| 3665 | admin_control(req) |
||
| 3666 | try: |
||
| 3667 | raw_json = req.stream.read().decode('utf-8') |
||
| 3668 | except Exception as ex: |
||
| 3669 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3670 | title='API.BAD_REQUEST', |
||
| 3671 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3672 | |||
| 3673 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3674 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3675 | description='API.INVALID_SPACE_ID') |
||
| 3676 | |||
| 3677 | new_values = json.loads(raw_json) |
||
| 3678 | |||
| 3679 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
||
| 3680 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
||
| 3681 | new_values['data']['working_calendar_id'] <= 0: |
||
| 3682 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3683 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
| 3684 | working_calendar_id = new_values['data']['working_calendar_id'] |
||
| 3685 | |||
| 3686 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3687 | cursor = cnx.cursor() |
||
| 3688 | |||
| 3689 | cursor.execute(" SELECT name " |
||
| 3690 | " from tbl_spaces " |
||
| 3691 | " WHERE id = %s ", (id_,)) |
||
| 3692 | if cursor.fetchone() is None: |
||
| 3693 | cursor.close() |
||
| 3694 | cnx.close() |
||
| 3695 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3696 | description='API.SPACE_NOT_FOUND') |
||
| 3697 | |||
| 3698 | cursor.execute(" SELECT name " |
||
| 3699 | " FROM tbl_working_calendars " |
||
| 3700 | " WHERE id = %s ", (working_calendar_id,)) |
||
| 3701 | if cursor.fetchone() is None: |
||
| 3702 | cursor.close() |
||
| 3703 | cnx.close() |
||
| 3704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3705 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 3706 | |||
| 3707 | query = (" SELECT id " |
||
| 3708 | " FROM tbl_spaces_working_calendars " |
||
| 3709 | " WHERE space_id = %s AND working_calendar_id = %s") |
||
| 3710 | cursor.execute(query, (id_, working_calendar_id,)) |
||
| 3711 | if cursor.fetchone() is not None: |
||
| 3712 | cursor.close() |
||
| 3713 | cnx.close() |
||
| 3714 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3715 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 3716 | |||
| 3717 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
||
| 3718 | " VALUES (%s, %s) ") |
||
| 3719 | cursor.execute(add_row, (id_, working_calendar_id,)) |
||
| 3720 | cnx.commit() |
||
| 3721 | cursor.close() |
||
| 3722 | cnx.close() |
||
| 3723 | |||
| 3724 | resp.status = falcon.HTTP_201 |
||
| 3725 | resp.location = '/spaces/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
||
| 3726 | |||
| 3727 | |||
| 3728 | View Code Duplication | class SpaceWorkingCalendarItem: |
|
| 3729 | def __init__(self): |
||
| 3730 | """Initializes SpaceWorkingCalendarItem Class""" |
||
| 3731 | pass |
||
| 3732 | |||
| 3733 | @staticmethod |
||
| 3734 | def on_options(req, resp, id_, wcid): |
||
| 3735 | resp.status = falcon.HTTP_200 |
||
| 3736 | |||
| 3737 | @staticmethod |
||
| 3738 | @user_logger |
||
| 3739 | def on_delete(req, resp, id_, wcid): |
||
| 3740 | admin_control(req) |
||
| 3741 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3742 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3743 | description='API.INVALID_SPACE_ID') |
||
| 3744 | |||
| 3745 | if not wcid.isdigit() or int(wcid) <= 0: |
||
| 3746 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3747 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
| 3748 | |||
| 3749 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3750 | cursor = cnx.cursor() |
||
| 3751 | |||
| 3752 | cursor.execute(" SELECT name " |
||
| 3753 | " FROM tbl_spaces " |
||
| 3754 | " WHERE id = %s ", (id_,)) |
||
| 3755 | if cursor.fetchone() is None: |
||
| 3756 | cursor.close() |
||
| 3757 | cnx.close() |
||
| 3758 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3759 | description='API.SPACE_NOT_FOUND') |
||
| 3760 | |||
| 3761 | cursor.execute(" SELECT name " |
||
| 3762 | " FROM tbl_working_calendars " |
||
| 3763 | " WHERE id = %s ", (wcid,)) |
||
| 3764 | if cursor.fetchone() is None: |
||
| 3765 | cursor.close() |
||
| 3766 | cnx.close() |
||
| 3767 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3768 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 3769 | |||
| 3770 | cursor.execute(" SELECT id " |
||
| 3771 | " FROM tbl_spaces_working_calendars " |
||
| 3772 | " WHERE space_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
| 3773 | if cursor.fetchone() is None: |
||
| 3774 | cursor.close() |
||
| 3775 | cnx.close() |
||
| 3776 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3777 | description='API.SPACE_WORKING_CALENDAR_RELATION_NOT_FOUND') |
||
| 3778 | |||
| 3779 | cursor.execute(" DELETE FROM tbl_spaces_working_calendars " |
||
| 3780 | " WHERE space_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
| 3781 | cnx.commit() |
||
| 3782 | |||
| 3783 | cursor.close() |
||
| 3784 | cnx.close() |
||
| 3785 | |||
| 3786 | resp.status = falcon.HTTP_204 |
||
| 3787 | |||
| 3788 | |||
| 3789 | View Code Duplication | class SpaceCommandCollection: |
|
| 3790 | def __init__(self): |
||
| 3791 | """Initializes Class""" |
||
| 3792 | pass |
||
| 3793 | |||
| 3794 | @staticmethod |
||
| 3795 | def on_options(req, resp, id_): |
||
| 3796 | resp.status = falcon.HTTP_200 |
||
| 3797 | |||
| 3798 | @staticmethod |
||
| 3799 | def on_get(req, resp, id_): |
||
| 3800 | if 'API-KEY' not in req.headers or \ |
||
| 3801 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3802 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3803 | access_control(req) |
||
| 3804 | else: |
||
| 3805 | api_key_control(req) |
||
| 3806 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3807 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3808 | description='API.INVALID_SPACE_ID') |
||
| 3809 | |||
| 3810 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3811 | cursor = cnx.cursor() |
||
| 3812 | |||
| 3813 | cursor.execute(" SELECT name " |
||
| 3814 | " FROM tbl_spaces " |
||
| 3815 | " WHERE id = %s ", (id_,)) |
||
| 3816 | if cursor.fetchone() is None: |
||
| 3817 | cursor.close() |
||
| 3818 | cnx.close() |
||
| 3819 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3820 | description='API.SPACE_NOT_FOUND') |
||
| 3821 | |||
| 3822 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 3823 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
||
| 3824 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 3825 | " ORDER BY c.id ") |
||
| 3826 | cursor.execute(query, (id_,)) |
||
| 3827 | rows = cursor.fetchall() |
||
| 3828 | |||
| 3829 | result = list() |
||
| 3830 | if rows is not None and len(rows) > 0: |
||
| 3831 | for row in rows: |
||
| 3832 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 3833 | result.append(meta_result) |
||
| 3834 | |||
| 3835 | resp.text = json.dumps(result) |
||
| 3836 | |||
| 3837 | @staticmethod |
||
| 3838 | @user_logger |
||
| 3839 | def on_post(req, resp, id_): |
||
| 3840 | """Handles POST requests""" |
||
| 3841 | admin_control(req) |
||
| 3842 | try: |
||
| 3843 | raw_json = req.stream.read().decode('utf-8') |
||
| 3844 | except Exception as ex: |
||
| 3845 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 3846 | title='API.BAD_REQUEST', |
||
| 3847 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 3848 | |||
| 3849 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3850 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3851 | description='API.INVALID_SPACE_ID') |
||
| 3852 | |||
| 3853 | new_values = json.loads(raw_json) |
||
| 3854 | |||
| 3855 | if 'command_id' not in new_values['data'].keys() or \ |
||
| 3856 | not isinstance(new_values['data']['command_id'], int) or \ |
||
| 3857 | new_values['data']['command_id'] <= 0: |
||
| 3858 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3859 | description='API.INVALID_COMMAND_ID') |
||
| 3860 | command_id = new_values['data']['command_id'] |
||
| 3861 | |||
| 3862 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3863 | cursor = cnx.cursor() |
||
| 3864 | |||
| 3865 | cursor.execute(" SELECT name " |
||
| 3866 | " from tbl_spaces " |
||
| 3867 | " WHERE id = %s ", (id_,)) |
||
| 3868 | if cursor.fetchone() is None: |
||
| 3869 | cursor.close() |
||
| 3870 | cnx.close() |
||
| 3871 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3872 | description='API.SPACE_NOT_FOUND') |
||
| 3873 | |||
| 3874 | cursor.execute(" SELECT name " |
||
| 3875 | " FROM tbl_commands " |
||
| 3876 | " WHERE id = %s ", (command_id,)) |
||
| 3877 | if cursor.fetchone() is None: |
||
| 3878 | cursor.close() |
||
| 3879 | cnx.close() |
||
| 3880 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3881 | description='API.COMMAND_NOT_FOUND') |
||
| 3882 | |||
| 3883 | query = (" SELECT id " |
||
| 3884 | " FROM tbl_spaces_commands " |
||
| 3885 | " WHERE space_id = %s AND command_id = %s") |
||
| 3886 | cursor.execute(query, (id_, command_id,)) |
||
| 3887 | if cursor.fetchone() is not None: |
||
| 3888 | cursor.close() |
||
| 3889 | cnx.close() |
||
| 3890 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3891 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
||
| 3892 | |||
| 3893 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
||
| 3894 | " VALUES (%s, %s) ") |
||
| 3895 | cursor.execute(add_row, (id_, command_id,)) |
||
| 3896 | cnx.commit() |
||
| 3897 | cursor.close() |
||
| 3898 | cnx.close() |
||
| 3899 | |||
| 3900 | resp.status = falcon.HTTP_201 |
||
| 3901 | resp.location = '/spaces/' + str(id_) + '/commands/' + str(command_id) |
||
| 3902 | |||
| 3903 | |||
| 3904 | View Code Duplication | class SpaceCommandItem: |
|
| 3905 | def __init__(self): |
||
| 3906 | """Initializes Class""" |
||
| 3907 | pass |
||
| 3908 | |||
| 3909 | @staticmethod |
||
| 3910 | def on_options(req, resp, id_, cid): |
||
| 3911 | resp.status = falcon.HTTP_200 |
||
| 3912 | |||
| 3913 | @staticmethod |
||
| 3914 | @user_logger |
||
| 3915 | def on_delete(req, resp, id_, cid): |
||
| 3916 | admin_control(req) |
||
| 3917 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3918 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3919 | description='API.INVALID_SPACE_ID') |
||
| 3920 | |||
| 3921 | if not cid.isdigit() or int(cid) <= 0: |
||
| 3922 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3923 | description='API.INVALID_COMMAND_ID') |
||
| 3924 | |||
| 3925 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3926 | cursor = cnx.cursor() |
||
| 3927 | |||
| 3928 | cursor.execute(" SELECT name " |
||
| 3929 | " FROM tbl_spaces " |
||
| 3930 | " WHERE id = %s ", (id_,)) |
||
| 3931 | if cursor.fetchone() is None: |
||
| 3932 | cursor.close() |
||
| 3933 | cnx.close() |
||
| 3934 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3935 | description='API.SPACE_NOT_FOUND') |
||
| 3936 | |||
| 3937 | cursor.execute(" SELECT name " |
||
| 3938 | " FROM tbl_commands " |
||
| 3939 | " WHERE id = %s ", (cid,)) |
||
| 3940 | if cursor.fetchone() is None: |
||
| 3941 | cursor.close() |
||
| 3942 | cnx.close() |
||
| 3943 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3944 | description='API.COMMAND_NOT_FOUND') |
||
| 3945 | |||
| 3946 | cursor.execute(" SELECT id " |
||
| 3947 | " FROM tbl_spaces_commands " |
||
| 3948 | " WHERE space_id = %s AND command_id = %s ", (id_, cid)) |
||
| 3949 | if cursor.fetchone() is None: |
||
| 3950 | cursor.close() |
||
| 3951 | cnx.close() |
||
| 3952 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3953 | description='API.SPACE_COMMAND_RELATION_NOT_FOUND') |
||
| 3954 | |||
| 3955 | cursor.execute(" DELETE FROM tbl_spaces_commands WHERE space_id = %s AND command_id = %s ", (id_, cid)) |
||
| 3956 | cnx.commit() |
||
| 3957 | |||
| 3958 | cursor.close() |
||
| 3959 | cnx.close() |
||
| 3960 | |||
| 3961 | resp.status = falcon.HTTP_204 |
||
| 3962 | |||
| 3963 | |||
| 3964 | class SpaceExport: |
||
| 3965 | def __init__(self): |
||
| 3966 | """Initializes Class""" |
||
| 3967 | pass |
||
| 3968 | |||
| 3969 | @staticmethod |
||
| 3970 | def on_options(req, resp, id_): |
||
| 3971 | resp.status = falcon.HTTP_200 |
||
| 3972 | |||
| 3973 | @staticmethod |
||
| 3974 | def on_get(req, resp, id_): |
||
| 3975 | if 'API-KEY' not in req.headers or \ |
||
| 3976 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 3977 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 3978 | access_control(req) |
||
| 3979 | else: |
||
| 3980 | api_key_control(req) |
||
| 3981 | if not id_.isdigit() or int(id_) <= 0: |
||
| 3982 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 3983 | description='API.INVALID_METER_ID') |
||
| 3984 | |||
| 3985 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 3986 | cursor = cnx.cursor() |
||
| 3987 | |||
| 3988 | query = (" SELECT id, name, uuid " |
||
| 3989 | " FROM tbl_spaces ") |
||
| 3990 | cursor.execute(query) |
||
| 3991 | rows_spaces = cursor.fetchall() |
||
| 3992 | |||
| 3993 | space_dict = dict() |
||
| 3994 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 3995 | for row in rows_spaces: |
||
| 3996 | space_dict[row[0]] = {"id": row[0], |
||
| 3997 | "name": row[1], |
||
| 3998 | "uuid": row[2]} |
||
| 3999 | |||
| 4000 | query = (" SELECT id, name, utc_offset " |
||
| 4001 | " FROM tbl_timezones ") |
||
| 4002 | cursor.execute(query) |
||
| 4003 | rows_timezones = cursor.fetchall() |
||
| 4004 | |||
| 4005 | timezone_dict = dict() |
||
| 4006 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 4007 | for row in rows_timezones: |
||
| 4008 | timezone_dict[row[0]] = {"id": row[0], |
||
| 4009 | "name": row[1], |
||
| 4010 | "utc_offset": row[2]} |
||
| 4011 | |||
| 4012 | query = (" SELECT id, name, uuid " |
||
| 4013 | " FROM tbl_contacts ") |
||
| 4014 | cursor.execute(query) |
||
| 4015 | rows_contacts = cursor.fetchall() |
||
| 4016 | |||
| 4017 | contact_dict = dict() |
||
| 4018 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 4019 | for row in rows_contacts: |
||
| 4020 | contact_dict[row[0]] = {"id": row[0], |
||
| 4021 | "name": row[1], |
||
| 4022 | "uuid": row[2]} |
||
| 4023 | |||
| 4024 | query = (" SELECT id, name, uuid " |
||
| 4025 | " FROM tbl_cost_centers ") |
||
| 4026 | cursor.execute(query) |
||
| 4027 | rows_cost_centers = cursor.fetchall() |
||
| 4028 | |||
| 4029 | cost_center_dict = dict() |
||
| 4030 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 4031 | for row in rows_cost_centers: |
||
| 4032 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 4033 | "name": row[1], |
||
| 4034 | "uuid": row[2]} |
||
| 4035 | |||
| 4036 | query = (" SELECT id, name, uuid, " |
||
| 4037 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 4038 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 4039 | " FROM tbl_spaces " |
||
| 4040 | " WHERE id = %s ") |
||
| 4041 | cursor.execute(query, (id_,)) |
||
| 4042 | row = cursor.fetchone() |
||
| 4043 | |||
| 4044 | if row is None: |
||
| 4045 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4046 | description='API.SPACE_NOT_FOUND') |
||
| 4047 | else: |
||
| 4048 | meta_result = {"id": row[0], |
||
| 4049 | "name": row[1], |
||
| 4050 | "uuid": row[2], |
||
| 4051 | "parent_space_id": space_dict.get(row[3], None), |
||
| 4052 | "area": row[4], |
||
| 4053 | "timezone": timezone_dict.get(row[5], None), |
||
| 4054 | "is_input_counted": bool(row[6]), |
||
| 4055 | "is_output_counted": bool(row[7]), |
||
| 4056 | "contact": contact_dict.get(row[8], None), |
||
| 4057 | "cost_center": cost_center_dict.get(row[9], None), |
||
| 4058 | "latitude": row[10], |
||
| 4059 | "longitude": row[11], |
||
| 4060 | "description": row[12], |
||
| 4061 | "children": None, |
||
| 4062 | "commands": None, |
||
| 4063 | "meters": None, |
||
| 4064 | "offline_meters": None, |
||
| 4065 | "virtual_meters": None, |
||
| 4066 | "shopfloors": None, |
||
| 4067 | "combined_equipments": None, |
||
| 4068 | "equipments": None, |
||
| 4069 | "points": None, |
||
| 4070 | "sensors": None, |
||
| 4071 | "tenants": None, |
||
| 4072 | "stores": None, |
||
| 4073 | "working_calendars": None |
||
| 4074 | } |
||
| 4075 | query = (" SELECT id, name, uuid, " |
||
| 4076 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 4077 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 4078 | " FROM tbl_spaces " |
||
| 4079 | " WHERE id = %s ") |
||
| 4080 | cursor.execute(query, (id_,)) |
||
| 4081 | row_current_space = cursor.fetchone() |
||
| 4082 | if row_current_space is None: |
||
| 4083 | cursor.close() |
||
| 4084 | cnx.close() |
||
| 4085 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4086 | description='API.SPACE_NOT_FOUND') |
||
| 4087 | # note: row_current_space will be used at the end |
||
| 4088 | |||
| 4089 | query = (" SELECT id, name, uuid " |
||
| 4090 | " FROM tbl_spaces ") |
||
| 4091 | cursor.execute(query) |
||
| 4092 | rows_spaces = cursor.fetchall() |
||
| 4093 | |||
| 4094 | space_dict = dict() |
||
| 4095 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 4096 | for row in rows_spaces: |
||
| 4097 | space_dict[row[0]] = {"id": row[0], |
||
| 4098 | "name": row[1], |
||
| 4099 | "uuid": row[2]} |
||
| 4100 | |||
| 4101 | query = (" SELECT id, name, utc_offset " |
||
| 4102 | " FROM tbl_timezones ") |
||
| 4103 | cursor.execute(query) |
||
| 4104 | rows_timezones = cursor.fetchall() |
||
| 4105 | |||
| 4106 | timezone_dict = dict() |
||
| 4107 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 4108 | for row in rows_timezones: |
||
| 4109 | timezone_dict[row[0]] = {"id": row[0], |
||
| 4110 | "name": row[1], |
||
| 4111 | "utc_offset": row[2]} |
||
| 4112 | |||
| 4113 | query = (" SELECT id, name, uuid " |
||
| 4114 | " FROM tbl_contacts ") |
||
| 4115 | cursor.execute(query) |
||
| 4116 | rows_contacts = cursor.fetchall() |
||
| 4117 | |||
| 4118 | contact_dict = dict() |
||
| 4119 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 4120 | for row in rows_contacts: |
||
| 4121 | contact_dict[row[0]] = {"id": row[0], |
||
| 4122 | "name": row[1], |
||
| 4123 | "uuid": row[2]} |
||
| 4124 | |||
| 4125 | query = (" SELECT id, name, uuid " |
||
| 4126 | " FROM tbl_cost_centers ") |
||
| 4127 | cursor.execute(query) |
||
| 4128 | rows_cost_centers = cursor.fetchall() |
||
| 4129 | |||
| 4130 | cost_center_dict = dict() |
||
| 4131 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 4132 | for row in rows_cost_centers: |
||
| 4133 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 4134 | "name": row[1], |
||
| 4135 | "uuid": row[2]} |
||
| 4136 | result = dict() |
||
| 4137 | result['current'] = dict() |
||
| 4138 | result['current']['id'] = row_current_space[0] |
||
| 4139 | result['current']['name'] = row_current_space[1] |
||
| 4140 | result['current']['uuid'] = row_current_space[2] |
||
| 4141 | result['current']['parent_space'] = space_dict.get(row_current_space[3], None) |
||
| 4142 | result['current']['area'] = row_current_space[4] |
||
| 4143 | result['current']['timezone'] = timezone_dict.get(row_current_space[5], None) |
||
| 4144 | result['current']['is_input_counted'] = bool(row_current_space[6]) |
||
| 4145 | result['current']['is_output_counted'] = bool(row_current_space[7]) |
||
| 4146 | result['current']['contact'] = contact_dict.get(row_current_space[8], None) |
||
| 4147 | result['current']['cost_center'] = cost_center_dict.get(row_current_space[9], None) |
||
| 4148 | result['current']['latitude'] = row_current_space[10] |
||
| 4149 | result['current']['longitude'] = row_current_space[11] |
||
| 4150 | result['current']['description'] = row_current_space[12] |
||
| 4151 | result['current']['qrcode'] = 'space:' + row_current_space[2] |
||
| 4152 | |||
| 4153 | result['children'] = list() |
||
| 4154 | |||
| 4155 | query = (" SELECT id, name, uuid, " |
||
| 4156 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 4157 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 4158 | " FROM tbl_spaces " |
||
| 4159 | " WHERE parent_space_id = %s " |
||
| 4160 | " ORDER BY id ") |
||
| 4161 | cursor.execute(query, (id_,)) |
||
| 4162 | rows_spaces = cursor.fetchall() |
||
| 4163 | |||
| 4164 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
| 4165 | for row in rows_spaces: |
||
| 4166 | children_result = {"id": row[0], |
||
| 4167 | "name": row[1], |
||
| 4168 | "uuid": row[2], |
||
| 4169 | "parent_space": space_dict.get(row[3], None), |
||
| 4170 | "area": row[4], |
||
| 4171 | "timezone": timezone_dict.get(row[5], None), |
||
| 4172 | "is_input_counted": bool(row[6]), |
||
| 4173 | "is_output_counted": bool(row[7]), |
||
| 4174 | "contact": contact_dict.get(row[8], None), |
||
| 4175 | "cost_center": cost_center_dict.get(row[9], None), |
||
| 4176 | "latitude": row[10], |
||
| 4177 | "longitude": row[11], |
||
| 4178 | "description": row[12]} |
||
| 4179 | result['children'].append(children_result) |
||
| 4180 | meta_result['children'] = result['children'] |
||
| 4181 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 4182 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
||
| 4183 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 4184 | " ORDER BY c.id ") |
||
| 4185 | cursor.execute(query, (id_,)) |
||
| 4186 | rows = cursor.fetchall() |
||
| 4187 | |||
| 4188 | command_result = list() |
||
| 4189 | if rows is not None and len(rows) > 0: |
||
| 4190 | for row in rows: |
||
| 4191 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4192 | command_result.append(result) |
||
| 4193 | meta_result['commands'] = command_result |
||
| 4194 | query = (" SELECT id, name, uuid " |
||
| 4195 | " FROM tbl_energy_categories ") |
||
| 4196 | cursor.execute(query) |
||
| 4197 | rows_energy_categories = cursor.fetchall() |
||
| 4198 | |||
| 4199 | energy_category_dict = dict() |
||
| 4200 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 4201 | for row in rows_energy_categories: |
||
| 4202 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 4203 | "name": row[1], |
||
| 4204 | "uuid": row[2]} |
||
| 4205 | |||
| 4206 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 4207 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
||
| 4208 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 4209 | " ORDER BY m.id ") |
||
| 4210 | cursor.execute(query, (id_,)) |
||
| 4211 | rows = cursor.fetchall() |
||
| 4212 | |||
| 4213 | meter_result = list() |
||
| 4214 | if rows is not None and len(rows) > 0: |
||
| 4215 | for row in rows: |
||
| 4216 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 4217 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 4218 | meter_result.append(result) |
||
| 4219 | meta_result['meters'] = meter_result |
||
| 4220 | query = (" SELECT id, name, uuid " |
||
| 4221 | " FROM tbl_energy_categories ") |
||
| 4222 | cursor.execute(query) |
||
| 4223 | rows_energy_categories = cursor.fetchall() |
||
| 4224 | |||
| 4225 | energy_category_dict = dict() |
||
| 4226 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 4227 | for row in rows_energy_categories: |
||
| 4228 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 4229 | "name": row[1], |
||
| 4230 | "uuid": row[2]} |
||
| 4231 | |||
| 4232 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 4233 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
||
| 4234 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 4235 | " ORDER BY m.id ") |
||
| 4236 | cursor.execute(query, (id_,)) |
||
| 4237 | rows = cursor.fetchall() |
||
| 4238 | |||
| 4239 | offlinemeter_result = list() |
||
| 4240 | if rows is not None and len(rows) > 0: |
||
| 4241 | for row in rows: |
||
| 4242 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 4243 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 4244 | offlinemeter_result.append(result) |
||
| 4245 | meta_result['offline_meters'] = offlinemeter_result |
||
| 4246 | query = (" SELECT id, name, uuid " |
||
| 4247 | " FROM tbl_energy_categories ") |
||
| 4248 | cursor.execute(query) |
||
| 4249 | rows_energy_categories = cursor.fetchall() |
||
| 4250 | |||
| 4251 | energy_category_dict = dict() |
||
| 4252 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 4253 | for row in rows_energy_categories: |
||
| 4254 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 4255 | "name": row[1], |
||
| 4256 | "uuid": row[2]} |
||
| 4257 | |||
| 4258 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 4259 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
||
| 4260 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 4261 | " ORDER BY m.id ") |
||
| 4262 | cursor.execute(query, (id_,)) |
||
| 4263 | rows = cursor.fetchall() |
||
| 4264 | |||
| 4265 | virtualmeter_result = list() |
||
| 4266 | if rows is not None and len(rows) > 0: |
||
| 4267 | for row in rows: |
||
| 4268 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 4269 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 4270 | virtualmeter_result.append(result) |
||
| 4271 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 4272 | query = (" SELECT sf.id, sf.name, sf.uuid " |
||
| 4273 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
||
| 4274 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
||
| 4275 | " ORDER BY sf.id ") |
||
| 4276 | cursor.execute(query, (id_,)) |
||
| 4277 | rows = cursor.fetchall() |
||
| 4278 | |||
| 4279 | shopfloor_result = list() |
||
| 4280 | if rows is not None and len(rows) > 0: |
||
| 4281 | for row in rows: |
||
| 4282 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4283 | shopfloor_result.append(result) |
||
| 4284 | meta_result['shopfloors'] = shopfloor_result |
||
| 4285 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 4286 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
||
| 4287 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
||
| 4288 | " ORDER BY e.id ") |
||
| 4289 | cursor.execute(query, (id_,)) |
||
| 4290 | rows = cursor.fetchall() |
||
| 4291 | |||
| 4292 | combinedequipment_result = list() |
||
| 4293 | if rows is not None and len(rows) > 0: |
||
| 4294 | for row in rows: |
||
| 4295 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4296 | combinedequipment_result.append(result) |
||
| 4297 | meta_result['combined_equipments'] = combinedequipment_result |
||
| 4298 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 4299 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
||
| 4300 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 4301 | " ORDER BY e.id ") |
||
| 4302 | cursor.execute(query, (id_,)) |
||
| 4303 | rows = cursor.fetchall() |
||
| 4304 | |||
| 4305 | equipment_result = list() |
||
| 4306 | if rows is not None and len(rows) > 0: |
||
| 4307 | for row in rows: |
||
| 4308 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4309 | equipment_result.append(result) |
||
| 4310 | meta_result['equipments'] = equipment_result |
||
| 4311 | query = (" SELECT id, name, uuid " |
||
| 4312 | " FROM tbl_data_sources ") |
||
| 4313 | cursor.execute(query) |
||
| 4314 | rows_data_sources = cursor.fetchall() |
||
| 4315 | |||
| 4316 | data_source_dict = dict() |
||
| 4317 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 4318 | for row in rows_data_sources: |
||
| 4319 | data_source_dict[row[0]] = {"id": row[0], |
||
| 4320 | "name": row[1], |
||
| 4321 | "uuid": row[2]} |
||
| 4322 | |||
| 4323 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 4324 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
||
| 4325 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 4326 | " ORDER BY p.id ") |
||
| 4327 | cursor.execute(query, (id_,)) |
||
| 4328 | rows = cursor.fetchall() |
||
| 4329 | |||
| 4330 | point_result = list() |
||
| 4331 | if rows is not None and len(rows) > 0: |
||
| 4332 | for row in rows: |
||
| 4333 | result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
| 4334 | point_result.append(result) |
||
| 4335 | meta_result['points'] = point_result |
||
| 4336 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 4337 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
||
| 4338 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 4339 | " ORDER BY se.id ") |
||
| 4340 | cursor.execute(query, (id_,)) |
||
| 4341 | rows = cursor.fetchall() |
||
| 4342 | |||
| 4343 | sensor_result = list() |
||
| 4344 | if rows is not None and len(rows) > 0: |
||
| 4345 | for row in rows: |
||
| 4346 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4347 | sensor_result.append(result) |
||
| 4348 | meta_result['sensors'] = sensor_result |
||
| 4349 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 4350 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
||
| 4351 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
||
| 4352 | " ORDER BY t.id ") |
||
| 4353 | cursor.execute(query, (id_,)) |
||
| 4354 | rows = cursor.fetchall() |
||
| 4355 | |||
| 4356 | tenant_result = list() |
||
| 4357 | if rows is not None and len(rows) > 0: |
||
| 4358 | for row in rows: |
||
| 4359 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4360 | tenant_result.append(result) |
||
| 4361 | meta_result['tenants'] = tenant_result |
||
| 4362 | query = (" SELECT t.id, t.name, t.uuid " |
||
| 4363 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
||
| 4364 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
||
| 4365 | " ORDER BY t.id ") |
||
| 4366 | cursor.execute(query, (id_,)) |
||
| 4367 | rows = cursor.fetchall() |
||
| 4368 | |||
| 4369 | store_result = list() |
||
| 4370 | if rows is not None and len(rows) > 0: |
||
| 4371 | for row in rows: |
||
| 4372 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 4373 | store_result.append(result) |
||
| 4374 | meta_result['stores'] = store_result |
||
| 4375 | query = (" SELECT wc.id, wc.name, wc.description " |
||
| 4376 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
||
| 4377 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 4378 | " ORDER BY wc.id ") |
||
| 4379 | cursor.execute(query, (id_,)) |
||
| 4380 | rows = cursor.fetchall() |
||
| 4381 | |||
| 4382 | workingcalendar_result = list() |
||
| 4383 | if rows is not None and len(rows) > 0: |
||
| 4384 | for row in rows: |
||
| 4385 | result = {"id": row[0], "name": row[1], "description": row[2]} |
||
| 4386 | workingcalendar_result.append(result) |
||
| 4387 | meta_result['working_calendars'] = workingcalendar_result |
||
| 4388 | cursor.close() |
||
| 4389 | cnx.close() |
||
| 4390 | resp.text = json.dumps(meta_result) |
||
| 4391 | |||
| 4392 | |||
| 4393 | class SpaceImport: |
||
| 4394 | def __init__(self): |
||
| 4395 | """Initializes Class""" |
||
| 4396 | pass |
||
| 4397 | |||
| 4398 | @staticmethod |
||
| 4399 | def on_options(req, resp): |
||
| 4400 | resp.status = falcon.HTTP_200 |
||
| 4401 | |||
| 4402 | @staticmethod |
||
| 4403 | @user_logger |
||
| 4404 | def on_post(req, resp): |
||
| 4405 | """Handles POST requests""" |
||
| 4406 | admin_control(req) |
||
| 4407 | try: |
||
| 4408 | raw_json = req.stream.read().decode('utf-8') |
||
| 4409 | new_values = json.loads(raw_json) |
||
| 4410 | except Exception as ex: |
||
| 4411 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 4412 | title='API.BAD_REQUEST', |
||
| 4413 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 4414 | |||
| 4415 | if 'name' not in new_values.keys() or \ |
||
| 4416 | not isinstance(new_values['name'], str) or \ |
||
| 4417 | len(str.strip(new_values['name'])) == 0: |
||
| 4418 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4419 | description='API.INVALID_SPACE_NAME') |
||
| 4420 | name = str.strip(new_values['name']) |
||
| 4421 | |||
| 4422 | if 'id' in new_values['parent_space_id'].keys(): |
||
| 4423 | if new_values['parent_space_id']['id'] <= 0: |
||
| 4424 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4425 | description='API.INVALID_PARENT_SPACE_ID') |
||
| 4426 | parent_space_id = new_values['parent_space_id']['id'] |
||
| 4427 | else: |
||
| 4428 | parent_space_id = None |
||
| 4429 | |||
| 4430 | if 'area' not in new_values.keys() or \ |
||
| 4431 | not (isinstance(new_values['area'], float) or |
||
| 4432 | isinstance(new_values['area'], int)) or \ |
||
| 4433 | new_values['area'] <= 0.0: |
||
| 4434 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4435 | description='API.INVALID_AREA_VALUE') |
||
| 4436 | area = new_values['area'] |
||
| 4437 | |||
| 4438 | if 'id' not in new_values['timezone'].keys() or \ |
||
| 4439 | not isinstance(new_values['timezone']['id'], int) or \ |
||
| 4440 | new_values['timezone']['id'] <= 0: |
||
| 4441 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4442 | description='API.INVALID_TIMEZONE_ID') |
||
| 4443 | timezone_id = new_values['timezone']['id'] |
||
| 4444 | |||
| 4445 | if 'is_input_counted' not in new_values.keys() or \ |
||
| 4446 | not isinstance(new_values['is_input_counted'], bool): |
||
| 4447 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4448 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 4449 | is_input_counted = new_values['is_input_counted'] |
||
| 4450 | |||
| 4451 | if 'is_output_counted' not in new_values.keys() or \ |
||
| 4452 | not isinstance(new_values['is_output_counted'], bool): |
||
| 4453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4454 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
||
| 4455 | is_output_counted = new_values['is_output_counted'] |
||
| 4456 | |||
| 4457 | if 'id' in new_values['contact'].keys(): |
||
| 4458 | if new_values['contact']['id'] <= 0: |
||
| 4459 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4460 | description='API.INVALID_CONTACT_ID') |
||
| 4461 | contact_id = new_values['contact']['id'] |
||
| 4462 | else: |
||
| 4463 | contact_id = None |
||
| 4464 | |||
| 4465 | if 'id' in new_values['cost_center'].keys(): |
||
| 4466 | if new_values['cost_center']['id'] <= 0: |
||
| 4467 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4468 | description='API.INVALID_COST_CENTER_ID') |
||
| 4469 | cost_center_id = new_values['cost_center']['id'] |
||
| 4470 | else: |
||
| 4471 | cost_center_id = None |
||
| 4472 | |||
| 4473 | View Code Duplication | if 'latitude' in new_values.keys() and new_values['latitude'] is not None: |
|
| 4474 | if not (isinstance(new_values['latitude'], float) or |
||
| 4475 | isinstance(new_values['latitude'], int)) or \ |
||
| 4476 | new_values['latitude'] < -90.0 or \ |
||
| 4477 | new_values['latitude'] > 90.0: |
||
| 4478 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4479 | description='API.INVALID_LATITUDE_VALUE') |
||
| 4480 | latitude = new_values['latitude'] |
||
| 4481 | else: |
||
| 4482 | latitude = None |
||
| 4483 | |||
| 4484 | View Code Duplication | if 'longitude' in new_values.keys() and new_values['longitude'] is not None: |
|
| 4485 | if not (isinstance(new_values['longitude'], float) or |
||
| 4486 | isinstance(new_values['longitude'], int)) or \ |
||
| 4487 | new_values['longitude'] < -180.0 or \ |
||
| 4488 | new_values['longitude'] > 180.0: |
||
| 4489 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4490 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 4491 | longitude = new_values['longitude'] |
||
| 4492 | else: |
||
| 4493 | longitude = None |
||
| 4494 | |||
| 4495 | if 'description' in new_values.keys() and \ |
||
| 4496 | new_values['description'] is not None and \ |
||
| 4497 | len(str(new_values['description'])) > 0: |
||
| 4498 | description = str.strip(new_values['description']) |
||
| 4499 | else: |
||
| 4500 | description = None |
||
| 4501 | |||
| 4502 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4503 | cursor = cnx.cursor() |
||
| 4504 | |||
| 4505 | cursor.execute(" SELECT name " |
||
| 4506 | " FROM tbl_spaces " |
||
| 4507 | " WHERE name = %s ", (name,)) |
||
| 4508 | if cursor.fetchone() is not None: |
||
| 4509 | cursor.close() |
||
| 4510 | cnx.close() |
||
| 4511 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4512 | description='API.SPACE_NAME_IS_ALREADY_IN_USE') |
||
| 4513 | |||
| 4514 | View Code Duplication | if parent_space_id is not None: |
|
| 4515 | cursor.execute(" SELECT name " |
||
| 4516 | " FROM tbl_spaces " |
||
| 4517 | " WHERE id = %s ", |
||
| 4518 | (new_values['parent_space_id']['id'],)) |
||
| 4519 | row = cursor.fetchone() |
||
| 4520 | if row is None: |
||
| 4521 | cursor.close() |
||
| 4522 | cnx.close() |
||
| 4523 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4524 | description='API.PARENT_SPACE_NOT_FOUND') |
||
| 4525 | |||
| 4526 | cursor.execute(" SELECT name " |
||
| 4527 | " FROM tbl_timezones " |
||
| 4528 | " WHERE id = %s ", |
||
| 4529 | (new_values['timezone']['id'],)) |
||
| 4530 | if cursor.fetchone() is None: |
||
| 4531 | cursor.close() |
||
| 4532 | cnx.close() |
||
| 4533 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4534 | description='API.TIMEZONE_NOT_FOUND') |
||
| 4535 | View Code Duplication | if contact_id is not None: |
|
| 4536 | cursor.execute(" SELECT name " |
||
| 4537 | " FROM tbl_contacts " |
||
| 4538 | " WHERE id = %s ", |
||
| 4539 | (new_values['contact']['id'],)) |
||
| 4540 | row = cursor.fetchone() |
||
| 4541 | if row is None: |
||
| 4542 | cursor.close() |
||
| 4543 | cnx.close() |
||
| 4544 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4545 | description='API.CONTACT_NOT_FOUND') |
||
| 4546 | |||
| 4547 | if cost_center_id is not None: |
||
| 4548 | cursor.execute(" SELECT name " |
||
| 4549 | " FROM tbl_cost_centers " |
||
| 4550 | " WHERE id = %s ", |
||
| 4551 | (new_values['cost_center']['id'],)) |
||
| 4552 | row = cursor.fetchone() |
||
| 4553 | if row is None: |
||
| 4554 | cursor.close() |
||
| 4555 | cnx.close() |
||
| 4556 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4557 | description='API.COST_CENTER_NOT_FOUND') |
||
| 4558 | |||
| 4559 | add_values = (" INSERT INTO tbl_spaces " |
||
| 4560 | " (name, uuid, parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 4561 | " contact_id, cost_center_id, latitude, longitude, description) " |
||
| 4562 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 4563 | cursor.execute(add_values, (name, |
||
| 4564 | str(uuid.uuid4()), |
||
| 4565 | parent_space_id, |
||
| 4566 | area, |
||
| 4567 | timezone_id, |
||
| 4568 | is_input_counted, |
||
| 4569 | is_output_counted, |
||
| 4570 | contact_id, |
||
| 4571 | cost_center_id, |
||
| 4572 | latitude, |
||
| 4573 | longitude, |
||
| 4574 | description)) |
||
| 4575 | new_id = cursor.lastrowid |
||
| 4576 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
| 4577 | for command in new_values['commands']: |
||
| 4578 | cursor.execute(" SELECT name " |
||
| 4579 | " FROM tbl_commands " |
||
| 4580 | " WHERE id = %s ", (command['id'],)) |
||
| 4581 | if cursor.fetchone() is None: |
||
| 4582 | cursor.close() |
||
| 4583 | cnx.close() |
||
| 4584 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4585 | description='API.COMMAND_NOT_FOUND') |
||
| 4586 | |||
| 4587 | query = (" SELECT id " |
||
| 4588 | " FROM tbl_spaces_commands " |
||
| 4589 | " WHERE space_id = %s AND command_id = %s") |
||
| 4590 | cursor.execute(query, (new_id, command['id'],)) |
||
| 4591 | if cursor.fetchone() is not None: |
||
| 4592 | cursor.close() |
||
| 4593 | cnx.close() |
||
| 4594 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4595 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
||
| 4596 | |||
| 4597 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
||
| 4598 | " VALUES (%s, %s) ") |
||
| 4599 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 4600 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
| 4601 | for meter in new_values['meters']: |
||
| 4602 | cursor.execute(" SELECT name " |
||
| 4603 | " FROM tbl_meters " |
||
| 4604 | " WHERE id = %s ", (meter['id'],)) |
||
| 4605 | if cursor.fetchone() is None: |
||
| 4606 | cursor.close() |
||
| 4607 | cnx.close() |
||
| 4608 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4609 | description='API.METER_NOT_FOUND') |
||
| 4610 | |||
| 4611 | query = (" SELECT id " |
||
| 4612 | " FROM tbl_spaces_meters " |
||
| 4613 | " WHERE space_id = %s AND meter_id = %s") |
||
| 4614 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 4615 | if cursor.fetchone() is not None: |
||
| 4616 | cursor.close() |
||
| 4617 | cnx.close() |
||
| 4618 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4619 | description='API.SPACE_METER_RELATION_EXISTS') |
||
| 4620 | |||
| 4621 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
||
| 4622 | " VALUES (%s, %s) ") |
||
| 4623 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
| 4624 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
| 4625 | for offline_meter in new_values['offline_meters']: |
||
| 4626 | cursor.execute(" SELECT name " |
||
| 4627 | " FROM tbl_offline_meters " |
||
| 4628 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 4629 | if cursor.fetchone() is None: |
||
| 4630 | cursor.close() |
||
| 4631 | cnx.close() |
||
| 4632 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4633 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 4634 | |||
| 4635 | query = (" SELECT id " |
||
| 4636 | " FROM tbl_spaces_offline_meters " |
||
| 4637 | " WHERE space_id = %s AND offline_meter_id = %s") |
||
| 4638 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 4639 | if cursor.fetchone() is not None: |
||
| 4640 | cursor.close() |
||
| 4641 | cnx.close() |
||
| 4642 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4643 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTS') |
||
| 4644 | |||
| 4645 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
||
| 4646 | " VALUES (%s, %s) ") |
||
| 4647 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
| 4648 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
| 4649 | for virtual_meter in new_values['virtual_meters']: |
||
| 4650 | cursor.execute(" SELECT name " |
||
| 4651 | " FROM tbl_virtual_meters " |
||
| 4652 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 4653 | if cursor.fetchone() is None: |
||
| 4654 | cursor.close() |
||
| 4655 | cnx.close() |
||
| 4656 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4657 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 4658 | |||
| 4659 | query = (" SELECT id " |
||
| 4660 | " FROM tbl_spaces_virtual_meters " |
||
| 4661 | " WHERE space_id = %s AND virtual_meter_id = %s") |
||
| 4662 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 4663 | if cursor.fetchone() is not None: |
||
| 4664 | cursor.close() |
||
| 4665 | cnx.close() |
||
| 4666 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4667 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTS') |
||
| 4668 | |||
| 4669 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
||
| 4670 | " VALUES (%s, %s) ") |
||
| 4671 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
| 4672 | if new_values['shopfloors'] is not None and len(new_values['shopfloors']) > 0: |
||
| 4673 | for shopfloor in new_values['shopfloors']: |
||
| 4674 | cursor.execute(" SELECT name " |
||
| 4675 | " FROM tbl_shopfloors " |
||
| 4676 | " WHERE id = %s ", (shopfloor['id'],)) |
||
| 4677 | if cursor.fetchone() is None: |
||
| 4678 | cursor.close() |
||
| 4679 | cnx.close() |
||
| 4680 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4681 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 4682 | |||
| 4683 | query = (" SELECT id " |
||
| 4684 | " FROM tbl_spaces_shopfloors " |
||
| 4685 | " WHERE space_id = %s AND shopfloor_id = %s") |
||
| 4686 | cursor.execute(query, (new_id, shopfloor['id'],)) |
||
| 4687 | if cursor.fetchone() is not None: |
||
| 4688 | cursor.close() |
||
| 4689 | cnx.close() |
||
| 4690 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4691 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
||
| 4692 | |||
| 4693 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
||
| 4694 | " VALUES (%s, %s) ") |
||
| 4695 | cursor.execute(add_row, (new_id, shopfloor['id'],)) |
||
| 4696 | if new_values['combined_equipments'] is not None and len(new_values['combined_equipments']) > 0: |
||
| 4697 | for combined_equipment in new_values['combined_equipments']: |
||
| 4698 | cursor.execute(" SELECT name " |
||
| 4699 | " FROM tbl_combined_equipments " |
||
| 4700 | " WHERE id = %s ", (combined_equipment['id'],)) |
||
| 4701 | if cursor.fetchone() is None: |
||
| 4702 | cursor.close() |
||
| 4703 | cnx.close() |
||
| 4704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4705 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 4706 | |||
| 4707 | query = (" SELECT id " |
||
| 4708 | " FROM tbl_spaces_combined_equipments " |
||
| 4709 | " WHERE space_id = %s AND combined_equipment_id = %s") |
||
| 4710 | cursor.execute(query, (new_id, combined_equipment['id'],)) |
||
| 4711 | if cursor.fetchone() is not None: |
||
| 4712 | cursor.close() |
||
| 4713 | cnx.close() |
||
| 4714 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4715 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
||
| 4716 | |||
| 4717 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
||
| 4718 | " VALUES (%s, %s) ") |
||
| 4719 | cursor.execute(add_row, (new_id, combined_equipment['id'],)) |
||
| 4720 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
| 4721 | for equipment in new_values['equipments']: |
||
| 4722 | cursor.execute(" SELECT name " |
||
| 4723 | " FROM tbl_equipments " |
||
| 4724 | " WHERE id = %s ", (equipment['id'],)) |
||
| 4725 | if cursor.fetchone() is None: |
||
| 4726 | cursor.close() |
||
| 4727 | cnx.close() |
||
| 4728 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4729 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 4730 | |||
| 4731 | query = (" SELECT id " |
||
| 4732 | " FROM tbl_spaces_equipments " |
||
| 4733 | " WHERE space_id = %s AND equipment_id = %s") |
||
| 4734 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 4735 | if cursor.fetchone() is not None: |
||
| 4736 | cursor.close() |
||
| 4737 | cnx.close() |
||
| 4738 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4739 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
||
| 4740 | |||
| 4741 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
||
| 4742 | " VALUES (%s, %s) ") |
||
| 4743 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 4744 | if new_values['points'] is not None and len(new_values['points']) > 0: |
||
| 4745 | for point in new_values['points']: |
||
| 4746 | cursor.execute(" SELECT name " |
||
| 4747 | " FROM tbl_points " |
||
| 4748 | " WHERE id = %s ", (point['id'],)) |
||
| 4749 | if cursor.fetchone() is None: |
||
| 4750 | cursor.close() |
||
| 4751 | cnx.close() |
||
| 4752 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4753 | description='API.POINT_NOT_FOUND') |
||
| 4754 | |||
| 4755 | query = (" SELECT id " |
||
| 4756 | " FROM tbl_spaces_points " |
||
| 4757 | " WHERE space_id = %s AND point_id = %s") |
||
| 4758 | cursor.execute(query, (new_id, point['id'],)) |
||
| 4759 | if cursor.fetchone() is not None: |
||
| 4760 | cursor.close() |
||
| 4761 | cnx.close() |
||
| 4762 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4763 | description='API.SPACE_POINT_RELATION_EXISTS') |
||
| 4764 | |||
| 4765 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
||
| 4766 | " VALUES (%s, %s) ") |
||
| 4767 | cursor.execute(add_row, (new_id, point['id'],)) |
||
| 4768 | if new_values['sensors'] is not None and len(new_values['sensors']) > 0: |
||
| 4769 | for sensor in new_values['sensors']: |
||
| 4770 | cursor.execute(" SELECT name " |
||
| 4771 | " FROM tbl_sensors " |
||
| 4772 | " WHERE id = %s ", (sensor['id'],)) |
||
| 4773 | if cursor.fetchone() is None: |
||
| 4774 | cursor.close() |
||
| 4775 | cnx.close() |
||
| 4776 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4777 | description='API.SENSOR_NOT_FOUND') |
||
| 4778 | |||
| 4779 | query = (" SELECT id " |
||
| 4780 | " FROM tbl_spaces_sensors " |
||
| 4781 | " WHERE space_id = %s AND sensor_id = %s") |
||
| 4782 | cursor.execute(query, (new_id, sensor['id'],)) |
||
| 4783 | if cursor.fetchone() is not None: |
||
| 4784 | cursor.close() |
||
| 4785 | cnx.close() |
||
| 4786 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4787 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
||
| 4788 | |||
| 4789 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
||
| 4790 | " VALUES (%s, %s) ") |
||
| 4791 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
| 4792 | if new_values['tenants'] is not None and len(new_values['tenants']) > 0: |
||
| 4793 | for tenant in new_values['tenants']: |
||
| 4794 | cursor.execute(" SELECT name " |
||
| 4795 | " FROM tbl_tenants " |
||
| 4796 | " WHERE id = %s ", (tenant['id'],)) |
||
| 4797 | if cursor.fetchone() is None: |
||
| 4798 | cursor.close() |
||
| 4799 | cnx.close() |
||
| 4800 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4801 | description='API.TENANT_NOT_FOUND') |
||
| 4802 | |||
| 4803 | query = (" SELECT id " |
||
| 4804 | " FROM tbl_spaces_tenants " |
||
| 4805 | " WHERE space_id = %s AND tenant_id = %s") |
||
| 4806 | cursor.execute(query, (new_id, tenant['id'],)) |
||
| 4807 | if cursor.fetchone() is not None: |
||
| 4808 | cursor.close() |
||
| 4809 | cnx.close() |
||
| 4810 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4811 | description='API.SPACE_TENANT_RELATION_EXISTS') |
||
| 4812 | |||
| 4813 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
||
| 4814 | " VALUES (%s, %s) ") |
||
| 4815 | cursor.execute(add_row, (new_id, tenant['id'],)) |
||
| 4816 | if new_values['stores'] is not None and len(new_values['stores']) > 0: |
||
| 4817 | for store in new_values['stores']: |
||
| 4818 | cursor.execute(" SELECT name " |
||
| 4819 | " FROM tbl_stores " |
||
| 4820 | " WHERE id = %s ", (store['id'],)) |
||
| 4821 | if cursor.fetchone() is None: |
||
| 4822 | cursor.close() |
||
| 4823 | cnx.close() |
||
| 4824 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4825 | description='API.STORE_NOT_FOUND') |
||
| 4826 | |||
| 4827 | query = (" SELECT id " |
||
| 4828 | " FROM tbl_spaces_stores " |
||
| 4829 | " WHERE space_id = %s AND store_id = %s") |
||
| 4830 | cursor.execute(query, (new_id, store['id'],)) |
||
| 4831 | if cursor.fetchone() is not None: |
||
| 4832 | cursor.close() |
||
| 4833 | cnx.close() |
||
| 4834 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4835 | description='API.SPACE_STORE_RELATION_EXISTS') |
||
| 4836 | |||
| 4837 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
||
| 4838 | " VALUES (%s, %s) ") |
||
| 4839 | cursor.execute(add_row, (new_id, store['id'],)) |
||
| 4840 | if new_values['working_calendars'] is not None and len(new_values['working_calendars']) > 0: |
||
| 4841 | for working_calendar in new_values['working_calendars']: |
||
| 4842 | cursor.execute(" SELECT name " |
||
| 4843 | " FROM tbl_working_calendars " |
||
| 4844 | " WHERE id = %s ", (working_calendar['id'],)) |
||
| 4845 | if cursor.fetchone() is None: |
||
| 4846 | cursor.close() |
||
| 4847 | cnx.close() |
||
| 4848 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4849 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 4850 | |||
| 4851 | query = (" SELECT id " |
||
| 4852 | " FROM tbl_spaces_working_calendars " |
||
| 4853 | " WHERE space_id = %s AND working_calendar_id = %s") |
||
| 4854 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
| 4855 | if cursor.fetchone() is not None: |
||
| 4856 | cursor.close() |
||
| 4857 | cnx.close() |
||
| 4858 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 4859 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 4860 | |||
| 4861 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
||
| 4862 | " VALUES (%s, %s) ") |
||
| 4863 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
| 4864 | cnx.commit() |
||
| 4865 | cursor.close() |
||
| 4866 | cnx.close() |
||
| 4867 | |||
| 4868 | resp.status = falcon.HTTP_201 |
||
| 4869 | resp.location = '/spaces/' + str(new_id) |
||
| 4870 | |||
| 4871 | |||
| 4872 | class SpaceClone: |
||
| 4873 | def __init__(self): |
||
| 4874 | """Initializes Class""" |
||
| 4875 | pass |
||
| 4876 | |||
| 4877 | @staticmethod |
||
| 4878 | def on_options(req, resp, id_): |
||
| 4879 | resp.status = falcon.HTTP_200 |
||
| 4880 | |||
| 4881 | @staticmethod |
||
| 4882 | @user_logger |
||
| 4883 | def on_post(req, resp, id_): |
||
| 4884 | # check parameters |
||
| 4885 | if 'API-KEY' not in req.headers or \ |
||
| 4886 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 4887 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 4888 | access_control(req) |
||
| 4889 | else: |
||
| 4890 | api_key_control(req) |
||
| 4891 | if not id_.isdigit() or int(id_) <= 0: |
||
| 4892 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4893 | description='API.INVALID_SPACE_ID') |
||
| 4894 | if int(id_) == 1: |
||
| 4895 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 4896 | description='API.THIS_SPACE_CANNOT_BE_CLONED') |
||
| 4897 | # connect the database |
||
| 4898 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 4899 | cursor = cnx.cursor() |
||
| 4900 | # query all spaces |
||
| 4901 | query = (" SELECT id, name, uuid " |
||
| 4902 | " FROM tbl_spaces ") |
||
| 4903 | cursor.execute(query) |
||
| 4904 | rows_spaces = cursor.fetchall() |
||
| 4905 | |||
| 4906 | space_dict = dict() |
||
| 4907 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 4908 | for row in rows_spaces: |
||
| 4909 | space_dict[row[0]] = {"id": row[0], |
||
| 4910 | "name": row[1], |
||
| 4911 | "uuid": row[2]} |
||
| 4912 | # query all timezones |
||
| 4913 | query = (" SELECT id, name, utc_offset " |
||
| 4914 | " FROM tbl_timezones ") |
||
| 4915 | cursor.execute(query) |
||
| 4916 | rows_timezones = cursor.fetchall() |
||
| 4917 | |||
| 4918 | timezone_dict = dict() |
||
| 4919 | if rows_timezones is not None and len(rows_timezones) > 0: |
||
| 4920 | for row in rows_timezones: |
||
| 4921 | timezone_dict[row[0]] = {"id": row[0], |
||
| 4922 | "name": row[1], |
||
| 4923 | "utc_offset": row[2]} |
||
| 4924 | # query all contacts |
||
| 4925 | query = (" SELECT id, name, uuid " |
||
| 4926 | " FROM tbl_contacts ") |
||
| 4927 | cursor.execute(query) |
||
| 4928 | rows_contacts = cursor.fetchall() |
||
| 4929 | |||
| 4930 | contact_dict = dict() |
||
| 4931 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 4932 | for row in rows_contacts: |
||
| 4933 | contact_dict[row[0]] = {"id": row[0], |
||
| 4934 | "name": row[1], |
||
| 4935 | "uuid": row[2]} |
||
| 4936 | # query all cost centers |
||
| 4937 | query = (" SELECT id, name, uuid " |
||
| 4938 | " FROM tbl_cost_centers ") |
||
| 4939 | cursor.execute(query) |
||
| 4940 | rows_cost_centers = cursor.fetchall() |
||
| 4941 | |||
| 4942 | cost_center_dict = dict() |
||
| 4943 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 4944 | for row in rows_cost_centers: |
||
| 4945 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 4946 | "name": row[1], |
||
| 4947 | "uuid": row[2]} |
||
| 4948 | |||
| 4949 | # query the source space |
||
| 4950 | query = (" SELECT id, name, uuid, " |
||
| 4951 | " parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 4952 | " contact_id, cost_center_id, latitude, longitude, description " |
||
| 4953 | " FROM tbl_spaces " |
||
| 4954 | " WHERE id = %s ") |
||
| 4955 | cursor.execute(query, (id_,)) |
||
| 4956 | row = cursor.fetchone() |
||
| 4957 | |||
| 4958 | if row is None: |
||
| 4959 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 4960 | description='API.SPACE_NOT_FOUND') |
||
| 4961 | else: |
||
| 4962 | # save the source space properties to meta_result |
||
| 4963 | meta_result = {"id": row[0], |
||
| 4964 | "name": row[1], |
||
| 4965 | "uuid": row[2], |
||
| 4966 | "parent_space_id": row[3], |
||
| 4967 | "area": row[4], |
||
| 4968 | "timezone_id": row[5], |
||
| 4969 | "is_input_counted": bool(row[6]), |
||
| 4970 | "is_output_counted": bool(row[7]), |
||
| 4971 | "contact_id": row[8], |
||
| 4972 | "cost_center_id": row[9], |
||
| 4973 | "latitude": row[10], |
||
| 4974 | "longitude": row[11], |
||
| 4975 | "description": row[12], |
||
| 4976 | "commands": None, |
||
| 4977 | "meters": None, |
||
| 4978 | "offline_meters": None, |
||
| 4979 | "virtual_meters": None, |
||
| 4980 | "shopfloors": None, |
||
| 4981 | "combined_equipments": None, |
||
| 4982 | "equipments": None, |
||
| 4983 | "points": None, |
||
| 4984 | "sensors": None, |
||
| 4985 | "tenants": None, |
||
| 4986 | "stores": None, |
||
| 4987 | "working_calendars": None |
||
| 4988 | } |
||
| 4989 | |||
| 4990 | # query associated commands |
||
| 4991 | query = (" SELECT c.id, c.name " |
||
| 4992 | " FROM tbl_spaces s, tbl_spaces_commands sc, tbl_commands c " |
||
| 4993 | " WHERE sc.space_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 4994 | " ORDER BY c.id ") |
||
| 4995 | cursor.execute(query, (id_,)) |
||
| 4996 | rows = cursor.fetchall() |
||
| 4997 | |||
| 4998 | command_list = list() |
||
| 4999 | if rows is not None and len(rows) > 0: |
||
| 5000 | for row in rows: |
||
| 5001 | result = {"id": row[0], "name": row[1]} |
||
| 5002 | command_list.append(result) |
||
| 5003 | meta_result['commands'] = command_list |
||
| 5004 | |||
| 5005 | # query associated meters |
||
| 5006 | query = (" SELECT m.id, m.name " |
||
| 5007 | " FROM tbl_spaces s, tbl_spaces_meters sm, tbl_meters m " |
||
| 5008 | " WHERE sm.space_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 5009 | " ORDER BY m.id ") |
||
| 5010 | cursor.execute(query, (id_,)) |
||
| 5011 | rows = cursor.fetchall() |
||
| 5012 | |||
| 5013 | meter_list = list() |
||
| 5014 | if rows is not None and len(rows) > 0: |
||
| 5015 | for row in rows: |
||
| 5016 | result = {"id": row[0], "name": row[1]} |
||
| 5017 | meter_list.append(result) |
||
| 5018 | meta_result['meters'] = meter_list |
||
| 5019 | |||
| 5020 | # query associated offline meters |
||
| 5021 | query = (" SELECT m.id, m.name " |
||
| 5022 | " FROM tbl_spaces s, tbl_spaces_offline_meters sm, tbl_offline_meters m " |
||
| 5023 | " WHERE sm.space_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 5024 | " ORDER BY m.id ") |
||
| 5025 | cursor.execute(query, (id_,)) |
||
| 5026 | rows = cursor.fetchall() |
||
| 5027 | |||
| 5028 | offline_meter_list = list() |
||
| 5029 | if rows is not None and len(rows) > 0: |
||
| 5030 | for row in rows: |
||
| 5031 | result = {"id": row[0], "name": row[1]} |
||
| 5032 | offline_meter_list.append(result) |
||
| 5033 | meta_result['offline_meters'] = offline_meter_list |
||
| 5034 | |||
| 5035 | # query associated virtual meters |
||
| 5036 | query = (" SELECT m.id, m.name " |
||
| 5037 | " FROM tbl_spaces s, tbl_spaces_virtual_meters sm, tbl_virtual_meters m " |
||
| 5038 | " WHERE sm.space_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 5039 | " ORDER BY m.id ") |
||
| 5040 | cursor.execute(query, (id_,)) |
||
| 5041 | rows = cursor.fetchall() |
||
| 5042 | |||
| 5043 | virtual_meter_list = list() |
||
| 5044 | if rows is not None and len(rows) > 0: |
||
| 5045 | for row in rows: |
||
| 5046 | result = {"id": row[0], "name": row[1]} |
||
| 5047 | virtual_meter_list.append(result) |
||
| 5048 | meta_result['virtual_meters'] = virtual_meter_list |
||
| 5049 | |||
| 5050 | # query associated shopfloors |
||
| 5051 | query = (" SELECT sf.id, sf.name " |
||
| 5052 | " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf " |
||
| 5053 | " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s " |
||
| 5054 | " ORDER BY sf.id ") |
||
| 5055 | cursor.execute(query, (id_,)) |
||
| 5056 | rows = cursor.fetchall() |
||
| 5057 | |||
| 5058 | shopfloor_list = list() |
||
| 5059 | if rows is not None and len(rows) > 0: |
||
| 5060 | for row in rows: |
||
| 5061 | result = {"id": row[0], "name": row[1]} |
||
| 5062 | shopfloor_list.append(result) |
||
| 5063 | meta_result['shopfloors'] = shopfloor_list |
||
| 5064 | |||
| 5065 | # query associated combined equipments |
||
| 5066 | query = (" SELECT e.id, e.name " |
||
| 5067 | " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e " |
||
| 5068 | " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s " |
||
| 5069 | " ORDER BY e.id ") |
||
| 5070 | cursor.execute(query, (id_,)) |
||
| 5071 | rows = cursor.fetchall() |
||
| 5072 | |||
| 5073 | combined_equipment_list = list() |
||
| 5074 | if rows is not None and len(rows) > 0: |
||
| 5075 | for row in rows: |
||
| 5076 | result = {"id": row[0], "name": row[1]} |
||
| 5077 | combined_equipment_list.append(result) |
||
| 5078 | meta_result['combined_equipments'] = combined_equipment_list |
||
| 5079 | |||
| 5080 | # query associated equipments |
||
| 5081 | query = (" SELECT e.id, e.name " |
||
| 5082 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e " |
||
| 5083 | " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 5084 | " ORDER BY e.id ") |
||
| 5085 | cursor.execute(query, (id_,)) |
||
| 5086 | rows = cursor.fetchall() |
||
| 5087 | |||
| 5088 | equipment_list = list() |
||
| 5089 | if rows is not None and len(rows) > 0: |
||
| 5090 | for row in rows: |
||
| 5091 | result = {"id": row[0], "name": row[1]} |
||
| 5092 | equipment_list.append(result) |
||
| 5093 | meta_result['equipments'] = equipment_list |
||
| 5094 | |||
| 5095 | # query associated points |
||
| 5096 | query = (" SELECT p.id, p.name " |
||
| 5097 | " FROM tbl_spaces s, tbl_spaces_points sp, tbl_points p " |
||
| 5098 | " WHERE sp.space_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 5099 | " ORDER BY p.id ") |
||
| 5100 | cursor.execute(query, (id_,)) |
||
| 5101 | rows = cursor.fetchall() |
||
| 5102 | |||
| 5103 | point_result = list() |
||
| 5104 | if rows is not None and len(rows) > 0: |
||
| 5105 | for row in rows: |
||
| 5106 | result = {"id": row[0], "name": row[1]} |
||
| 5107 | point_result.append(result) |
||
| 5108 | meta_result['points'] = point_result |
||
| 5109 | |||
| 5110 | # query associated sensors |
||
| 5111 | query = (" SELECT se.id, se.name " |
||
| 5112 | " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se " |
||
| 5113 | " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 5114 | " ORDER BY se.id ") |
||
| 5115 | cursor.execute(query, (id_,)) |
||
| 5116 | rows = cursor.fetchall() |
||
| 5117 | |||
| 5118 | sensor_list = list() |
||
| 5119 | if rows is not None and len(rows) > 0: |
||
| 5120 | for row in rows: |
||
| 5121 | result = {"id": row[0], "name": row[1]} |
||
| 5122 | sensor_list.append(result) |
||
| 5123 | meta_result['sensors'] = sensor_list |
||
| 5124 | |||
| 5125 | # query associated tenants |
||
| 5126 | query = (" SELECT t.id, t.name " |
||
| 5127 | " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t " |
||
| 5128 | " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s " |
||
| 5129 | " ORDER BY t.id ") |
||
| 5130 | cursor.execute(query, (id_,)) |
||
| 5131 | rows = cursor.fetchall() |
||
| 5132 | |||
| 5133 | tenant_list = list() |
||
| 5134 | if rows is not None and len(rows) > 0: |
||
| 5135 | for row in rows: |
||
| 5136 | result = {"id": row[0], "name": row[1]} |
||
| 5137 | tenant_list.append(result) |
||
| 5138 | meta_result['tenants'] = tenant_list |
||
| 5139 | |||
| 5140 | # query associated stores |
||
| 5141 | query = (" SELECT t.id, t.name " |
||
| 5142 | " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t " |
||
| 5143 | " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s " |
||
| 5144 | " ORDER BY t.id ") |
||
| 5145 | cursor.execute(query, (id_,)) |
||
| 5146 | rows = cursor.fetchall() |
||
| 5147 | |||
| 5148 | store_list = list() |
||
| 5149 | if rows is not None and len(rows) > 0: |
||
| 5150 | for row in rows: |
||
| 5151 | result = {"id": row[0], "name": row[1]} |
||
| 5152 | store_list.append(result) |
||
| 5153 | meta_result['stores'] = store_list |
||
| 5154 | |||
| 5155 | # query associated working calendars |
||
| 5156 | query = (" SELECT wc.id, wc.name " |
||
| 5157 | " FROM tbl_spaces s, tbl_spaces_working_calendars swc, tbl_working_calendars wc " |
||
| 5158 | " WHERE swc.space_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 5159 | " ORDER BY wc.id ") |
||
| 5160 | cursor.execute(query, (id_,)) |
||
| 5161 | rows = cursor.fetchall() |
||
| 5162 | |||
| 5163 | working_calendar_list = list() |
||
| 5164 | if rows is not None and len(rows) > 0: |
||
| 5165 | for row in rows: |
||
| 5166 | result = {"id": row[0], "name": row[1]} |
||
| 5167 | working_calendar_list.append(result) |
||
| 5168 | meta_result['working_calendars'] = working_calendar_list |
||
| 5169 | |||
| 5170 | # generate name for new space |
||
| 5171 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 5172 | if config.utc_offset[0] == '-': |
||
| 5173 | timezone_offset = -timezone_offset |
||
| 5174 | new_name = (str.strip(meta_result['name']) + |
||
| 5175 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 5176 | |||
| 5177 | # save new space to database |
||
| 5178 | add_values = (" INSERT INTO tbl_spaces " |
||
| 5179 | " (name, uuid, parent_space_id, area, timezone_id, is_input_counted, is_output_counted, " |
||
| 5180 | " contact_id, cost_center_id, latitude, longitude, description) " |
||
| 5181 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 5182 | cursor.execute(add_values, (new_name, |
||
| 5183 | str(uuid.uuid4()), |
||
| 5184 | meta_result['parent_space_id'], |
||
| 5185 | meta_result['area'], |
||
| 5186 | meta_result['timezone_id'], |
||
| 5187 | meta_result['is_input_counted'], |
||
| 5188 | meta_result['is_output_counted'], |
||
| 5189 | meta_result['contact_id'], |
||
| 5190 | meta_result['cost_center_id'], |
||
| 5191 | meta_result['latitude'], |
||
| 5192 | meta_result['longitude'], |
||
| 5193 | meta_result['description'])) |
||
| 5194 | new_id = cursor.lastrowid |
||
| 5195 | |||
| 5196 | # associate commands with new space |
||
| 5197 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
| 5198 | for command in meta_result['commands']: |
||
| 5199 | cursor.execute(" SELECT name " |
||
| 5200 | " FROM tbl_commands " |
||
| 5201 | " WHERE id = %s ", (command['id'],)) |
||
| 5202 | if cursor.fetchone() is None: |
||
| 5203 | cursor.close() |
||
| 5204 | cnx.close() |
||
| 5205 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5206 | description='API.COMMAND_NOT_FOUND') |
||
| 5207 | |||
| 5208 | query = (" SELECT id " |
||
| 5209 | " FROM tbl_spaces_commands " |
||
| 5210 | " WHERE space_id = %s AND command_id = %s") |
||
| 5211 | cursor.execute(query, (new_id, command['id'],)) |
||
| 5212 | if cursor.fetchone() is not None: |
||
| 5213 | cursor.close() |
||
| 5214 | cnx.close() |
||
| 5215 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5216 | description='API.SPACE_COMMAND_RELATION_EXISTS') |
||
| 5217 | |||
| 5218 | add_row = (" INSERT INTO tbl_spaces_commands (space_id, command_id) " |
||
| 5219 | " VALUES (%s, %s) ") |
||
| 5220 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 5221 | |||
| 5222 | # associate meters with new space |
||
| 5223 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
| 5224 | for meter in meta_result['meters']: |
||
| 5225 | cursor.execute(" SELECT name " |
||
| 5226 | " FROM tbl_meters " |
||
| 5227 | " WHERE id = %s ", (meter['id'],)) |
||
| 5228 | if cursor.fetchone() is None: |
||
| 5229 | cursor.close() |
||
| 5230 | cnx.close() |
||
| 5231 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5232 | description='API.METER_NOT_FOUND') |
||
| 5233 | |||
| 5234 | query = (" SELECT id " |
||
| 5235 | " FROM tbl_spaces_meters " |
||
| 5236 | " WHERE space_id = %s AND meter_id = %s") |
||
| 5237 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 5238 | if cursor.fetchone() is not None: |
||
| 5239 | cursor.close() |
||
| 5240 | cnx.close() |
||
| 5241 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5242 | description='API.SPACE_METER_RELATION_EXISTS') |
||
| 5243 | |||
| 5244 | add_row = (" INSERT INTO tbl_spaces_meters (space_id, meter_id) " |
||
| 5245 | " VALUES (%s, %s) ") |
||
| 5246 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
| 5247 | |||
| 5248 | # associate offline meters with new space |
||
| 5249 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
| 5250 | for offline_meter in meta_result['offline_meters']: |
||
| 5251 | cursor.execute(" SELECT name " |
||
| 5252 | " FROM tbl_offline_meters " |
||
| 5253 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 5254 | if cursor.fetchone() is None: |
||
| 5255 | cursor.close() |
||
| 5256 | cnx.close() |
||
| 5257 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5258 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 5259 | |||
| 5260 | query = (" SELECT id " |
||
| 5261 | " FROM tbl_spaces_offline_meters " |
||
| 5262 | " WHERE space_id = %s AND offline_meter_id = %s") |
||
| 5263 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 5264 | if cursor.fetchone() is not None: |
||
| 5265 | cursor.close() |
||
| 5266 | cnx.close() |
||
| 5267 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5268 | description='API.SPACE_OFFLINE_METER_RELATION_EXISTS') |
||
| 5269 | |||
| 5270 | add_row = (" INSERT INTO tbl_spaces_offline_meters (space_id, offline_meter_id) " |
||
| 5271 | " VALUES (%s, %s) ") |
||
| 5272 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
| 5273 | |||
| 5274 | # associate virtual meters with new space |
||
| 5275 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
| 5276 | for virtual_meter in meta_result['virtual_meters']: |
||
| 5277 | cursor.execute(" SELECT name " |
||
| 5278 | " FROM tbl_virtual_meters " |
||
| 5279 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 5280 | if cursor.fetchone() is None: |
||
| 5281 | cursor.close() |
||
| 5282 | cnx.close() |
||
| 5283 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5284 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 5285 | |||
| 5286 | query = (" SELECT id " |
||
| 5287 | " FROM tbl_spaces_virtual_meters " |
||
| 5288 | " WHERE space_id = %s AND virtual_meter_id = %s") |
||
| 5289 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 5290 | if cursor.fetchone() is not None: |
||
| 5291 | cursor.close() |
||
| 5292 | cnx.close() |
||
| 5293 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5294 | description='API.SPACE_VIRTUAL_METER_RELATION_EXISTS') |
||
| 5295 | |||
| 5296 | add_row = (" INSERT INTO tbl_spaces_virtual_meters (space_id, virtual_meter_id) " |
||
| 5297 | " VALUES (%s, %s) ") |
||
| 5298 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
| 5299 | |||
| 5300 | # associate shopfloors with new space |
||
| 5301 | if meta_result['shopfloors'] is not None and len(meta_result['shopfloors']) > 0: |
||
| 5302 | for shopfloor in meta_result['shopfloors']: |
||
| 5303 | cursor.execute(" SELECT name " |
||
| 5304 | " FROM tbl_shopfloors " |
||
| 5305 | " WHERE id = %s ", (shopfloor['id'],)) |
||
| 5306 | if cursor.fetchone() is None: |
||
| 5307 | cursor.close() |
||
| 5308 | cnx.close() |
||
| 5309 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5310 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 5311 | |||
| 5312 | query = (" SELECT id " |
||
| 5313 | " FROM tbl_spaces_shopfloors " |
||
| 5314 | " WHERE space_id = %s AND shopfloor_id = %s") |
||
| 5315 | cursor.execute(query, (new_id, shopfloor['id'],)) |
||
| 5316 | if cursor.fetchone() is not None: |
||
| 5317 | cursor.close() |
||
| 5318 | cnx.close() |
||
| 5319 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5320 | description='API.SPACE_SHOPFLOOR_RELATION_EXISTS') |
||
| 5321 | |||
| 5322 | add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) " |
||
| 5323 | " VALUES (%s, %s) ") |
||
| 5324 | cursor.execute(add_row, (new_id, shopfloor['id'],)) |
||
| 5325 | |||
| 5326 | # associate combined equipments with new space |
||
| 5327 | if meta_result['combined_equipments'] is not None and len(meta_result['combined_equipments']) > 0: |
||
| 5328 | for combined_equipment in meta_result['combined_equipments']: |
||
| 5329 | cursor.execute(" SELECT name " |
||
| 5330 | " FROM tbl_combined_equipments " |
||
| 5331 | " WHERE id = %s ", (combined_equipment['id'],)) |
||
| 5332 | if cursor.fetchone() is None: |
||
| 5333 | cursor.close() |
||
| 5334 | cnx.close() |
||
| 5335 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5336 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
||
| 5337 | |||
| 5338 | query = (" SELECT id " |
||
| 5339 | " FROM tbl_spaces_combined_equipments " |
||
| 5340 | " WHERE space_id = %s AND combined_equipment_id = %s") |
||
| 5341 | cursor.execute(query, (new_id, combined_equipment['id'],)) |
||
| 5342 | if cursor.fetchone() is not None: |
||
| 5343 | cursor.close() |
||
| 5344 | cnx.close() |
||
| 5345 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5346 | description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS') |
||
| 5347 | |||
| 5348 | add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) " |
||
| 5349 | " VALUES (%s, %s) ") |
||
| 5350 | cursor.execute(add_row, (new_id, combined_equipment['id'],)) |
||
| 5351 | |||
| 5352 | # associate equipments with new space |
||
| 5353 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
| 5354 | for equipment in meta_result['equipments']: |
||
| 5355 | cursor.execute(" SELECT name " |
||
| 5356 | " FROM tbl_equipments " |
||
| 5357 | " WHERE id = %s ", (equipment['id'],)) |
||
| 5358 | if cursor.fetchone() is None: |
||
| 5359 | cursor.close() |
||
| 5360 | cnx.close() |
||
| 5361 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5362 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 5363 | |||
| 5364 | query = (" SELECT id " |
||
| 5365 | " FROM tbl_spaces_equipments " |
||
| 5366 | " WHERE space_id = %s AND equipment_id = %s") |
||
| 5367 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 5368 | if cursor.fetchone() is not None: |
||
| 5369 | cursor.close() |
||
| 5370 | cnx.close() |
||
| 5371 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5372 | description='API.SPACE_EQUIPMENT_RELATION_EXISTS') |
||
| 5373 | |||
| 5374 | add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) " |
||
| 5375 | " VALUES (%s, %s) ") |
||
| 5376 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 5377 | |||
| 5378 | # associate points with new space |
||
| 5379 | if meta_result['points'] is not None and len(meta_result['points']) > 0: |
||
| 5380 | for point in meta_result['points']: |
||
| 5381 | cursor.execute(" SELECT name " |
||
| 5382 | " FROM tbl_points " |
||
| 5383 | " WHERE id = %s ", (point['id'],)) |
||
| 5384 | if cursor.fetchone() is None: |
||
| 5385 | cursor.close() |
||
| 5386 | cnx.close() |
||
| 5387 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5388 | description='API.POINT_NOT_FOUND') |
||
| 5389 | |||
| 5390 | query = (" SELECT id " |
||
| 5391 | " FROM tbl_spaces_points " |
||
| 5392 | " WHERE space_id = %s AND point_id = %s") |
||
| 5393 | cursor.execute(query, (new_id, point['id'],)) |
||
| 5394 | if cursor.fetchone() is not None: |
||
| 5395 | cursor.close() |
||
| 5396 | cnx.close() |
||
| 5397 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5398 | description='API.SPACE_POINT_RELATION_EXISTS') |
||
| 5399 | |||
| 5400 | add_row = (" INSERT INTO tbl_spaces_points (space_id, point_id) " |
||
| 5401 | " VALUES (%s, %s) ") |
||
| 5402 | cursor.execute(add_row, (new_id, point['id'],)) |
||
| 5403 | |||
| 5404 | # associate sensors with new space |
||
| 5405 | if meta_result['sensors'] is not None and len(meta_result['sensors']) > 0: |
||
| 5406 | for sensor in meta_result['sensors']: |
||
| 5407 | cursor.execute(" SELECT name " |
||
| 5408 | " FROM tbl_sensors " |
||
| 5409 | " WHERE id = %s ", (sensor['id'],)) |
||
| 5410 | if cursor.fetchone() is None: |
||
| 5411 | cursor.close() |
||
| 5412 | cnx.close() |
||
| 5413 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5414 | description='API.SENSOR_NOT_FOUND') |
||
| 5415 | |||
| 5416 | query = (" SELECT id " |
||
| 5417 | " FROM tbl_spaces_sensors " |
||
| 5418 | " WHERE space_id = %s AND sensor_id = %s") |
||
| 5419 | cursor.execute(query, (new_id, sensor['id'],)) |
||
| 5420 | if cursor.fetchone() is not None: |
||
| 5421 | cursor.close() |
||
| 5422 | cnx.close() |
||
| 5423 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5424 | description='API.SPACE_SENSOR_RELATION_EXISTS') |
||
| 5425 | |||
| 5426 | add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) " |
||
| 5427 | " VALUES (%s, %s) ") |
||
| 5428 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
| 5429 | |||
| 5430 | # associate tenants with new space |
||
| 5431 | if meta_result['tenants'] is not None and len(meta_result['tenants']) > 0: |
||
| 5432 | for tenant in meta_result['tenants']: |
||
| 5433 | cursor.execute(" SELECT name " |
||
| 5434 | " FROM tbl_tenants " |
||
| 5435 | " WHERE id = %s ", (tenant['id'],)) |
||
| 5436 | if cursor.fetchone() is None: |
||
| 5437 | cursor.close() |
||
| 5438 | cnx.close() |
||
| 5439 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5440 | description='API.TENANT_NOT_FOUND') |
||
| 5441 | |||
| 5442 | query = (" SELECT id " |
||
| 5443 | " FROM tbl_spaces_tenants " |
||
| 5444 | " WHERE space_id = %s AND tenant_id = %s") |
||
| 5445 | cursor.execute(query, (new_id, tenant['id'],)) |
||
| 5446 | if cursor.fetchone() is not None: |
||
| 5447 | cursor.close() |
||
| 5448 | cnx.close() |
||
| 5449 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5450 | description='API.SPACE_TENANT_RELATION_EXISTS') |
||
| 5451 | |||
| 5452 | add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) " |
||
| 5453 | " VALUES (%s, %s) ") |
||
| 5454 | cursor.execute(add_row, (new_id, tenant['id'],)) |
||
| 5455 | |||
| 5456 | # associate stores with new space |
||
| 5457 | if meta_result['stores'] is not None and len(meta_result['stores']) > 0: |
||
| 5458 | for store in meta_result['stores']: |
||
| 5459 | cursor.execute(" SELECT name " |
||
| 5460 | " FROM tbl_stores " |
||
| 5461 | " WHERE id = %s ", (store['id'],)) |
||
| 5462 | if cursor.fetchone() is None: |
||
| 5463 | cursor.close() |
||
| 5464 | cnx.close() |
||
| 5465 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5466 | description='API.STORE_NOT_FOUND') |
||
| 5467 | |||
| 5468 | query = (" SELECT id " |
||
| 5469 | " FROM tbl_spaces_stores " |
||
| 5470 | " WHERE space_id = %s AND store_id = %s") |
||
| 5471 | cursor.execute(query, (new_id, store['id'],)) |
||
| 5472 | if cursor.fetchone() is not None: |
||
| 5473 | cursor.close() |
||
| 5474 | cnx.close() |
||
| 5475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5476 | description='API.SPACE_STORE_RELATION_EXISTS') |
||
| 5477 | |||
| 5478 | add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) " |
||
| 5479 | " VALUES (%s, %s) ") |
||
| 5480 | cursor.execute(add_row, (new_id, store['id'],)) |
||
| 5481 | |||
| 5482 | # associate working calendars with new space |
||
| 5483 | if meta_result['working_calendars'] is not None and len(meta_result['working_calendars']) > 0: |
||
| 5484 | for working_calendar in meta_result['working_calendars']: |
||
| 5485 | cursor.execute(" SELECT name " |
||
| 5486 | " FROM tbl_working_calendars " |
||
| 5487 | " WHERE id = %s ", (working_calendar['id'],)) |
||
| 5488 | if cursor.fetchone() is None: |
||
| 5489 | cursor.close() |
||
| 5490 | cnx.close() |
||
| 5491 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5492 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 5493 | |||
| 5494 | query = (" SELECT id " |
||
| 5495 | " FROM tbl_spaces_working_calendars " |
||
| 5496 | " WHERE space_id = %s AND working_calendar_id = %s") |
||
| 5497 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
| 5498 | if cursor.fetchone() is not None: |
||
| 5499 | cursor.close() |
||
| 5500 | cnx.close() |
||
| 5501 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5502 | description='API.SPACE_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 5503 | |||
| 5504 | add_row = (" INSERT INTO tbl_spaces_working_calendars (space_id, working_calendar_id) " |
||
| 5505 | " VALUES (%s, %s) ") |
||
| 5506 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
| 5507 | |||
| 5508 | # todo: associate more objects with new space |
||
| 5509 | |||
| 5510 | cnx.commit() |
||
| 5511 | cursor.close() |
||
| 5512 | cnx.close() |
||
| 5513 | |||
| 5514 | resp.status = falcon.HTTP_201 |
||
| 5515 | resp.location = '/spaces/' + str(new_id) |
||
| 5516 | |||
| 5517 | |||
| 5518 | View Code Duplication | class SpaceEnergyFlowDiagramCollection: |
|
| 5519 | def __init__(self): |
||
| 5520 | """Initializes Class""" |
||
| 5521 | pass |
||
| 5522 | |||
| 5523 | @staticmethod |
||
| 5524 | def on_options(req, resp, id_): |
||
| 5525 | resp.status = falcon.HTTP_200 |
||
| 5526 | |||
| 5527 | @staticmethod |
||
| 5528 | def on_get(req, resp, id_): |
||
| 5529 | if 'API-KEY' not in req.headers or \ |
||
| 5530 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5531 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5532 | access_control(req) |
||
| 5533 | else: |
||
| 5534 | api_key_control(req) |
||
| 5535 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5536 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5537 | description='API.INVALID_SPACE_ID') |
||
| 5538 | |||
| 5539 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5540 | cursor = cnx.cursor() |
||
| 5541 | |||
| 5542 | cursor.execute(" SELECT name " |
||
| 5543 | " FROM tbl_spaces " |
||
| 5544 | " WHERE id = %s ", (id_,)) |
||
| 5545 | if cursor.fetchone() is None: |
||
| 5546 | cursor.close() |
||
| 5547 | cnx.close() |
||
| 5548 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5549 | description='API.SPACE_NOT_FOUND') |
||
| 5550 | |||
| 5551 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 5552 | " FROM tbl_spaces s, tbl_spaces_energy_flow_diagrams se, tbl_energy_flow_diagrams e " |
||
| 5553 | " WHERE se.space_id = s.id AND e.id = se.energy_flow_diagram_id AND s.id = %s " |
||
| 5554 | " ORDER BY e.id ") |
||
| 5555 | cursor.execute(query, (id_,)) |
||
| 5556 | rows = cursor.fetchall() |
||
| 5557 | |||
| 5558 | result = list() |
||
| 5559 | if rows is not None and len(rows) > 0: |
||
| 5560 | for row in rows: |
||
| 5561 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 5562 | result.append(meta_result) |
||
| 5563 | |||
| 5564 | resp.text = json.dumps(result) |
||
| 5565 | |||
| 5566 | @staticmethod |
||
| 5567 | @user_logger |
||
| 5568 | def on_post(req, resp, id_): |
||
| 5569 | """Handles POST requests""" |
||
| 5570 | admin_control(req) |
||
| 5571 | try: |
||
| 5572 | raw_json = req.stream.read().decode('utf-8') |
||
| 5573 | except Exception as ex: |
||
| 5574 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5575 | title='API.BAD_REQUEST', |
||
| 5576 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5577 | |||
| 5578 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5579 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5580 | description='API.INVALID_SPACE_ID') |
||
| 5581 | |||
| 5582 | new_values = json.loads(raw_json) |
||
| 5583 | |||
| 5584 | if 'energy_flow_diagram_id' not in new_values['data'].keys() or \ |
||
| 5585 | not isinstance(new_values['data']['energy_flow_diagram_id'], int) or \ |
||
| 5586 | new_values['data']['energy_flow_diagram_id'] <= 0: |
||
| 5587 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5588 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 5589 | energy_flow_diagram_id = new_values['data']['energy_flow_diagram_id'] |
||
| 5590 | |||
| 5591 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5592 | cursor = cnx.cursor() |
||
| 5593 | |||
| 5594 | cursor.execute(" SELECT name " |
||
| 5595 | " from tbl_spaces " |
||
| 5596 | " WHERE id = %s ", (id_,)) |
||
| 5597 | if cursor.fetchone() is None: |
||
| 5598 | cursor.close() |
||
| 5599 | cnx.close() |
||
| 5600 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5601 | description='API.SPACE_NOT_FOUND') |
||
| 5602 | |||
| 5603 | cursor.execute(" SELECT name " |
||
| 5604 | " FROM tbl_energy_flow_diagrams " |
||
| 5605 | " WHERE id = %s ", (energy_flow_diagram_id,)) |
||
| 5606 | if cursor.fetchone() is None: |
||
| 5607 | cursor.close() |
||
| 5608 | cnx.close() |
||
| 5609 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5610 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 5611 | |||
| 5612 | query = (" SELECT id " |
||
| 5613 | " FROM tbl_spaces_energy_flow_diagrams " |
||
| 5614 | " WHERE space_id = %s AND energy_flow_diagram_id = %s") |
||
| 5615 | cursor.execute(query, (id_, energy_flow_diagram_id,)) |
||
| 5616 | if cursor.fetchone() is not None: |
||
| 5617 | cursor.close() |
||
| 5618 | cnx.close() |
||
| 5619 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5620 | description='API.SPACE_ENERGY_FLOW_DIAGRAM_RELATION_EXISTS') |
||
| 5621 | |||
| 5622 | add_row = (" INSERT INTO tbl_spaces_energy_flow_diagrams (space_id, energy_flow_diagram_id) " |
||
| 5623 | " VALUES (%s, %s) ") |
||
| 5624 | cursor.execute(add_row, (id_, energy_flow_diagram_id,)) |
||
| 5625 | cnx.commit() |
||
| 5626 | cursor.close() |
||
| 5627 | cnx.close() |
||
| 5628 | |||
| 5629 | resp.status = falcon.HTTP_201 |
||
| 5630 | resp.location = '/spaces/' + str(id_) + '/energyflowdiagrams/' + str(energy_flow_diagram_id) |
||
| 5631 | |||
| 5632 | |||
| 5633 | View Code Duplication | class SpaceEnergyFlowDiagramItem: |
|
| 5634 | def __init__(self): |
||
| 5635 | """Initializes Class""" |
||
| 5636 | pass |
||
| 5637 | |||
| 5638 | @staticmethod |
||
| 5639 | def on_options(req, resp, id_, eid): |
||
| 5640 | resp.status = falcon.HTTP_200 |
||
| 5641 | |||
| 5642 | @staticmethod |
||
| 5643 | @user_logger |
||
| 5644 | def on_delete(req, resp, id_, eid): |
||
| 5645 | admin_control(req) |
||
| 5646 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5647 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5648 | description='API.INVALID_SPACE_ID') |
||
| 5649 | |||
| 5650 | if not eid.isdigit() or int(eid) <= 0: |
||
| 5651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5652 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
||
| 5653 | |||
| 5654 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5655 | cursor = cnx.cursor() |
||
| 5656 | |||
| 5657 | cursor.execute(" SELECT name " |
||
| 5658 | " FROM tbl_spaces " |
||
| 5659 | " WHERE id = %s ", (id_,)) |
||
| 5660 | if cursor.fetchone() is None: |
||
| 5661 | cursor.close() |
||
| 5662 | cnx.close() |
||
| 5663 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5664 | description='API.SPACE_NOT_FOUND') |
||
| 5665 | |||
| 5666 | cursor.execute(" SELECT name " |
||
| 5667 | " FROM tbl_energy_flow_diagrams " |
||
| 5668 | " WHERE id = %s ", (eid,)) |
||
| 5669 | if cursor.fetchone() is None: |
||
| 5670 | cursor.close() |
||
| 5671 | cnx.close() |
||
| 5672 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5673 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
||
| 5674 | |||
| 5675 | cursor.execute(" SELECT id " |
||
| 5676 | " FROM tbl_spaces_energy_flow_diagrams " |
||
| 5677 | " WHERE space_id = %s AND energy_flow_diagram_id = %s ", (id_, eid)) |
||
| 5678 | if cursor.fetchone() is None: |
||
| 5679 | cursor.close() |
||
| 5680 | cnx.close() |
||
| 5681 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5682 | description='API.SPACE_ENERGY_FLOW_DIAGRAM_RELATION_NOT_FOUND') |
||
| 5683 | |||
| 5684 | cursor.execute(" DELETE FROM tbl_spaces_energy_flow_diagrams " |
||
| 5685 | " WHERE space_id = %s AND energy_flow_diagram_id = %s ", (id_, eid)) |
||
| 5686 | cnx.commit() |
||
| 5687 | |||
| 5688 | cursor.close() |
||
| 5689 | cnx.close() |
||
| 5690 | |||
| 5691 | resp.status = falcon.HTTP_204 |
||
| 5692 | |||
| 5693 | View Code Duplication | class DistributionSystemCollection: |
|
| 5694 | def __init__(self): |
||
| 5695 | """Initializes Class""" |
||
| 5696 | pass |
||
| 5697 | |||
| 5698 | @staticmethod |
||
| 5699 | def on_options(req, resp, id_): |
||
| 5700 | resp.status = falcon.HTTP_200 |
||
| 5701 | |||
| 5702 | @staticmethod |
||
| 5703 | def on_get(req, resp, id_): |
||
| 5704 | if 'API-KEY' not in req.headers or \ |
||
| 5705 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 5706 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 5707 | access_control(req) |
||
| 5708 | else: |
||
| 5709 | api_key_control(req) |
||
| 5710 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5711 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5712 | description='API.INVALID_SPACE_ID') |
||
| 5713 | |||
| 5714 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5715 | cursor = cnx.cursor() |
||
| 5716 | |||
| 5717 | cursor.execute(" SELECT name " |
||
| 5718 | " FROM tbl_spaces " |
||
| 5719 | " WHERE id = %s ", (id_,)) |
||
| 5720 | if cursor.fetchone() is None: |
||
| 5721 | cursor.close() |
||
| 5722 | cnx.close() |
||
| 5723 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5724 | description='API.SPACE_NOT_FOUND') |
||
| 5725 | |||
| 5726 | query = (" SELECT d.id, d.name, d.uuid " |
||
| 5727 | " FROM tbl_spaces s, tbl_spaces_distribution_systems sd, tbl_distribution_systems d " |
||
| 5728 | " WHERE sd.space_id = s.id AND d.id = sd.distribution_system_id AND s.id = %s " |
||
| 5729 | " ORDER BY d.id ") |
||
| 5730 | cursor.execute(query, (id_,)) |
||
| 5731 | rows = cursor.fetchall() |
||
| 5732 | |||
| 5733 | result = list() |
||
| 5734 | if rows is not None and len(rows) > 0: |
||
| 5735 | for row in rows: |
||
| 5736 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 5737 | result.append(meta_result) |
||
| 5738 | |||
| 5739 | resp.text = json.dumps(result) |
||
| 5740 | |||
| 5741 | @staticmethod |
||
| 5742 | @user_logger |
||
| 5743 | def on_post(req, resp, id_): |
||
| 5744 | """Handles POST requests""" |
||
| 5745 | admin_control(req) |
||
| 5746 | try: |
||
| 5747 | raw_json = req.stream.read().decode('utf-8') |
||
| 5748 | except Exception as ex: |
||
| 5749 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 5750 | title='API.BAD_REQUEST', |
||
| 5751 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 5752 | |||
| 5753 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5754 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5755 | description='API.INVALID_SPACE_ID') |
||
| 5756 | |||
| 5757 | new_values = json.loads(raw_json) |
||
| 5758 | |||
| 5759 | if 'distribution_system_id' not in new_values['data'].keys() or \ |
||
| 5760 | not isinstance(new_values['data']['distribution_system_id'], int) or \ |
||
| 5761 | new_values['data']['distribution_system_id'] <= 0: |
||
| 5762 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5763 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
||
| 5764 | distribution_system_id = new_values['data']['distribution_system_id'] |
||
| 5765 | |||
| 5766 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5767 | cursor = cnx.cursor() |
||
| 5768 | |||
| 5769 | cursor.execute(" SELECT name " |
||
| 5770 | " from tbl_spaces " |
||
| 5771 | " WHERE id = %s ", (id_,)) |
||
| 5772 | if cursor.fetchone() is None: |
||
| 5773 | cursor.close() |
||
| 5774 | cnx.close() |
||
| 5775 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5776 | description='API.SPACE_NOT_FOUND') |
||
| 5777 | |||
| 5778 | cursor.execute(" SELECT name " |
||
| 5779 | " FROM tbl_distribution_systems " |
||
| 5780 | " WHERE id = %s ", (distribution_system_id,)) |
||
| 5781 | if cursor.fetchone() is None: |
||
| 5782 | cursor.close() |
||
| 5783 | cnx.close() |
||
| 5784 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5785 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
||
| 5786 | |||
| 5787 | query = (" SELECT id " |
||
| 5788 | " FROM tbl_spaces_distribution_systems " |
||
| 5789 | " WHERE space_id = %s AND distribution_system_id = %s") |
||
| 5790 | cursor.execute(query, (id_, distribution_system_id,)) |
||
| 5791 | if cursor.fetchone() is not None: |
||
| 5792 | cursor.close() |
||
| 5793 | cnx.close() |
||
| 5794 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 5795 | description='API.SPACE_DISTRIBUTION_SYSTEM_RELATION_EXISTS') |
||
| 5796 | |||
| 5797 | add_row = (" INSERT INTO tbl_spaces_distribution_systems (space_id, distribution_system_id) " |
||
| 5798 | " VALUES (%s, %s) ") |
||
| 5799 | cursor.execute(add_row, (id_, distribution_system_id,)) |
||
| 5800 | cnx.commit() |
||
| 5801 | cursor.close() |
||
| 5802 | cnx.close() |
||
| 5803 | |||
| 5804 | resp.status = falcon.HTTP_201 |
||
| 5805 | resp.location = '/spaces/' + str(id_) + '/distributionsystems/' + str(distribution_system_id) |
||
| 5806 | |||
| 5807 | |||
| 5808 | View Code Duplication | class DistributionSystemItem: |
|
| 5809 | def __init__(self): |
||
| 5810 | """Initializes Class""" |
||
| 5811 | pass |
||
| 5812 | |||
| 5813 | @staticmethod |
||
| 5814 | def on_options(req, resp, id_, did): |
||
| 5815 | resp.status = falcon.HTTP_200 |
||
| 5816 | |||
| 5817 | @staticmethod |
||
| 5818 | @user_logger |
||
| 5819 | def on_delete(req, resp, id_, did): |
||
| 5820 | admin_control(req) |
||
| 5821 | if not id_.isdigit() or int(id_) <= 0: |
||
| 5822 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5823 | description='API.INVALID_SPACE_ID') |
||
| 5824 | |||
| 5825 | if not did.isdigit() or int(did) <= 0: |
||
| 5826 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 5827 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
||
| 5828 | |||
| 5829 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 5830 | cursor = cnx.cursor() |
||
| 5831 | |||
| 5832 | cursor.execute(" SELECT name " |
||
| 5833 | " FROM tbl_spaces " |
||
| 5834 | " WHERE id = %s ", (id_,)) |
||
| 5835 | if cursor.fetchone() is None: |
||
| 5836 | cursor.close() |
||
| 5837 | cnx.close() |
||
| 5838 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5839 | description='API.SPACE_NOT_FOUND') |
||
| 5840 | |||
| 5841 | cursor.execute(" SELECT name " |
||
| 5842 | " FROM tbl_distribution_systems " |
||
| 5843 | " WHERE id = %s ", (did,)) |
||
| 5844 | if cursor.fetchone() is None: |
||
| 5845 | cursor.close() |
||
| 5846 | cnx.close() |
||
| 5847 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5848 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
||
| 5849 | |||
| 5850 | cursor.execute(" SELECT id " |
||
| 5851 | " FROM tbl_spaces_distribution_systems " |
||
| 5852 | " WHERE space_id = %s AND distribution_system_id = %s ", (id_, did)) |
||
| 5853 | if cursor.fetchone() is None: |
||
| 5854 | cursor.close() |
||
| 5855 | cnx.close() |
||
| 5856 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 5857 | description='API.SPACE_DISTRIBUTION_SYSTEM_RELATION_NOT_FOUND') |
||
| 5858 | |||
| 5859 | cursor.execute(" DELETE FROM tbl_spaces_distribution_systems " |
||
| 5860 | " WHERE space_id = %s AND distribution_system_id = %s ", (id_, did)) |
||
| 5861 | cnx.commit() |
||
| 5862 | |||
| 5863 | cursor.close() |
||
| 5864 | cnx.close() |
||
| 5865 | |||
| 5866 | resp.status = falcon.HTTP_204 |