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