| Total Complexity | 574 |
| Total Lines | 3060 |
| Duplicated Lines | 41.08 % |
| 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.shopfloor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import uuid |
||
| 2 | from datetime import datetime, timedelta |
||
| 3 | import falcon |
||
| 4 | import mysql.connector |
||
| 5 | import simplejson as json |
||
| 6 | from core.useractivity import user_logger, admin_control, access_control, api_key_control |
||
| 7 | import config |
||
| 8 | |||
| 9 | |||
| 10 | class ShopfloorCollection: |
||
| 11 | """ |
||
| 12 | Shopfloor Collection Resource |
||
| 13 | |||
| 14 | This class handles CRUD operations for shopfloor collection. |
||
| 15 | It provides endpoints for listing all shopfloors and creating new ones. |
||
| 16 | Shopfloors represent manufacturing or production areas within facilities, |
||
| 17 | containing equipment, sensors, meters, and other operational components |
||
| 18 | for monitoring and managing industrial processes. |
||
| 19 | """ |
||
| 20 | def __init__(self): |
||
| 21 | pass |
||
| 22 | |||
| 23 | @staticmethod |
||
| 24 | def on_options(req, resp): |
||
| 25 | """ |
||
| 26 | Handle OPTIONS request for CORS preflight |
||
| 27 | |||
| 28 | Args: |
||
| 29 | req: Falcon request object |
||
| 30 | resp: Falcon response object |
||
| 31 | """ |
||
| 32 | resp.status = falcon.HTTP_200 |
||
| 33 | _ = req |
||
| 34 | |||
| 35 | View Code Duplication | @staticmethod |
|
|
|
|||
| 36 | def on_get(req, resp): |
||
| 37 | """ |
||
| 38 | Handle GET requests to retrieve all shopfloors |
||
| 39 | |||
| 40 | Returns a list of all shopfloors with their complete information including: |
||
| 41 | - Shopfloor ID, name, and UUID |
||
| 42 | - Associated contact and cost center information |
||
| 43 | - Shopfloor specifications and parameters |
||
| 44 | - Related equipment, sensors, and meter associations |
||
| 45 | - Working calendar and command configurations |
||
| 46 | |||
| 47 | Args: |
||
| 48 | req: Falcon request object |
||
| 49 | resp: Falcon response object |
||
| 50 | """ |
||
| 51 | # Check authentication method (API key or session) |
||
| 52 | if 'API-KEY' not in req.headers or \ |
||
| 53 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 54 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 55 | access_control(req) |
||
| 56 | else: |
||
| 57 | api_key_control(req) |
||
| 58 | |||
| 59 | search_query = req.get_param('q', default=None) |
||
| 60 | if search_query is not None and len(search_query.strip()) > 0: |
||
| 61 | search_query = search_query.strip() |
||
| 62 | else: |
||
| 63 | search_query = '' |
||
| 64 | |||
| 65 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 66 | cursor = cnx.cursor() |
||
| 67 | |||
| 68 | query = (" SELECT id, name, uuid " |
||
| 69 | " FROM tbl_contacts ") |
||
| 70 | cursor.execute(query) |
||
| 71 | rows_contacts = cursor.fetchall() |
||
| 72 | |||
| 73 | contact_dict = dict() |
||
| 74 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 75 | for row in rows_contacts: |
||
| 76 | contact_dict[row[0]] = {"id": row[0], |
||
| 77 | "name": row[1], |
||
| 78 | "uuid": row[2]} |
||
| 79 | |||
| 80 | query = (" SELECT id, name, uuid " |
||
| 81 | " FROM tbl_cost_centers ") |
||
| 82 | cursor.execute(query) |
||
| 83 | rows_cost_centers = cursor.fetchall() |
||
| 84 | |||
| 85 | cost_center_dict = dict() |
||
| 86 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 87 | for row in rows_cost_centers: |
||
| 88 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 89 | "name": row[1], |
||
| 90 | "uuid": row[2]} |
||
| 91 | |||
| 92 | query = (" SELECT id, name, uuid, " |
||
| 93 | " area, is_input_counted, " |
||
| 94 | " contact_id, cost_center_id, description " |
||
| 95 | " FROM tbl_shopfloors ") |
||
| 96 | params = [] |
||
| 97 | if search_query: |
||
| 98 | query += " WHERE name LIKE %s OR description LIKE %s " |
||
| 99 | params = [f'%{search_query}%', f'%{search_query}%'] |
||
| 100 | query += " ORDER BY id " |
||
| 101 | cursor.execute(query, params) |
||
| 102 | rows_shopfloors = cursor.fetchall() |
||
| 103 | |||
| 104 | result = list() |
||
| 105 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
| 106 | for row in rows_shopfloors: |
||
| 107 | meta_result = {"id": row[0], |
||
| 108 | "name": row[1], |
||
| 109 | "uuid": row[2], |
||
| 110 | "area": row[3], |
||
| 111 | "is_input_counted": bool(row[4]), |
||
| 112 | "contact": contact_dict.get(row[5], None), |
||
| 113 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 114 | "description": row[7], |
||
| 115 | "qrcode": "shopfloor:" + row[2]} |
||
| 116 | result.append(meta_result) |
||
| 117 | |||
| 118 | cursor.close() |
||
| 119 | cnx.close() |
||
| 120 | resp.text = json.dumps(result) |
||
| 121 | |||
| 122 | @staticmethod |
||
| 123 | @user_logger |
||
| 124 | def on_post(req, resp): |
||
| 125 | """Handles POST requests""" |
||
| 126 | admin_control(req) |
||
| 127 | try: |
||
| 128 | raw_json = req.stream.read().decode('utf-8') |
||
| 129 | except UnicodeDecodeError as ex: |
||
| 130 | print("Failed to decode request") |
||
| 131 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 132 | title='API.BAD_REQUEST', |
||
| 133 | description='API.INVALID_ENCODING') |
||
| 134 | except Exception as ex: |
||
| 135 | print("Unexpected error reading request stream") |
||
| 136 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 137 | title='API.BAD_REQUEST', |
||
| 138 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 139 | |||
| 140 | new_values = json.loads(raw_json) |
||
| 141 | |||
| 142 | if 'name' not in new_values['data'].keys() or \ |
||
| 143 | not isinstance(new_values['data']['name'], str) or \ |
||
| 144 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 146 | description='API.INVALID_SHOPFLOOR_NAME') |
||
| 147 | name = str.strip(new_values['data']['name']) |
||
| 148 | |||
| 149 | if 'area' not in new_values['data'].keys() or \ |
||
| 150 | not (isinstance(new_values['data']['area'], float) or |
||
| 151 | isinstance(new_values['data']['area'], int)) or \ |
||
| 152 | new_values['data']['area'] <= 0.0: |
||
| 153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 154 | description='API.INVALID_AREA_VALUE') |
||
| 155 | area = new_values['data']['area'] |
||
| 156 | |||
| 157 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 158 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 159 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 160 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 161 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 162 | |||
| 163 | if 'contact_id' in new_values['data'].keys(): |
||
| 164 | if new_values['data']['contact_id'] <= 0: |
||
| 165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 166 | description='API.INVALID_CONTACT_ID') |
||
| 167 | contact_id = new_values['data']['contact_id'] |
||
| 168 | else: |
||
| 169 | contact_id = None |
||
| 170 | |||
| 171 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 172 | if new_values['data']['cost_center_id'] <= 0: |
||
| 173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 174 | description='API.INVALID_COST_CENTER_ID') |
||
| 175 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 176 | else: |
||
| 177 | cost_center_id = None |
||
| 178 | |||
| 179 | if 'description' in new_values['data'].keys() and \ |
||
| 180 | new_values['data']['description'] is not None and \ |
||
| 181 | len(str(new_values['data']['description'])) > 0: |
||
| 182 | description = str.strip(new_values['data']['description']) |
||
| 183 | else: |
||
| 184 | description = None |
||
| 185 | |||
| 186 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 187 | cursor = cnx.cursor() |
||
| 188 | |||
| 189 | cursor.execute(" SELECT name " |
||
| 190 | " FROM tbl_shopfloors " |
||
| 191 | " WHERE name = %s ", (name,)) |
||
| 192 | if cursor.fetchone() is not None: |
||
| 193 | cursor.close() |
||
| 194 | cnx.close() |
||
| 195 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 196 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
| 197 | |||
| 198 | View Code Duplication | if contact_id is not None: |
|
| 199 | cursor.execute(" SELECT name " |
||
| 200 | " FROM tbl_contacts " |
||
| 201 | " WHERE id = %s ", |
||
| 202 | (new_values['data']['contact_id'],)) |
||
| 203 | row = cursor.fetchone() |
||
| 204 | if row is None: |
||
| 205 | cursor.close() |
||
| 206 | cnx.close() |
||
| 207 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 208 | description='API.CONTACT_NOT_FOUND') |
||
| 209 | |||
| 210 | if cost_center_id is not None: |
||
| 211 | cursor.execute(" SELECT name " |
||
| 212 | " FROM tbl_cost_centers " |
||
| 213 | " WHERE id = %s ", |
||
| 214 | (new_values['data']['cost_center_id'],)) |
||
| 215 | row = cursor.fetchone() |
||
| 216 | if row is None: |
||
| 217 | cursor.close() |
||
| 218 | cnx.close() |
||
| 219 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 220 | description='API.COST_CENTER_NOT_FOUND') |
||
| 221 | |||
| 222 | add_values = (" INSERT INTO tbl_shopfloors " |
||
| 223 | " (name, uuid, area, is_input_counted, " |
||
| 224 | " contact_id, cost_center_id, description) " |
||
| 225 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 226 | cursor.execute(add_values, (name, |
||
| 227 | str(uuid.uuid4()), |
||
| 228 | area, |
||
| 229 | is_input_counted, |
||
| 230 | contact_id, |
||
| 231 | cost_center_id, |
||
| 232 | description)) |
||
| 233 | new_id = cursor.lastrowid |
||
| 234 | cnx.commit() |
||
| 235 | cursor.close() |
||
| 236 | cnx.close() |
||
| 237 | |||
| 238 | resp.status = falcon.HTTP_201 |
||
| 239 | resp.location = '/shopfloors/' + str(new_id) |
||
| 240 | |||
| 241 | |||
| 242 | class ShopfloorItem: |
||
| 243 | def __init__(self): |
||
| 244 | pass |
||
| 245 | |||
| 246 | @staticmethod |
||
| 247 | def on_options(req, resp, id_): |
||
| 248 | resp.status = falcon.HTTP_200 |
||
| 249 | _ = req |
||
| 250 | _ = id_ |
||
| 251 | |||
| 252 | View Code Duplication | @staticmethod |
|
| 253 | def on_get(req, resp, id_): |
||
| 254 | if 'API-KEY' not in req.headers or \ |
||
| 255 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 256 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 257 | access_control(req) |
||
| 258 | else: |
||
| 259 | api_key_control(req) |
||
| 260 | if not id_.isdigit() or int(id_) <= 0: |
||
| 261 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 262 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 263 | |||
| 264 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 265 | cursor = cnx.cursor() |
||
| 266 | |||
| 267 | query = (" SELECT id, name, uuid " |
||
| 268 | " FROM tbl_contacts ") |
||
| 269 | cursor.execute(query) |
||
| 270 | rows_contacts = cursor.fetchall() |
||
| 271 | |||
| 272 | contact_dict = dict() |
||
| 273 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 274 | for row in rows_contacts: |
||
| 275 | contact_dict[row[0]] = {"id": row[0], |
||
| 276 | "name": row[1], |
||
| 277 | "uuid": row[2]} |
||
| 278 | |||
| 279 | query = (" SELECT id, name, uuid " |
||
| 280 | " FROM tbl_cost_centers ") |
||
| 281 | cursor.execute(query) |
||
| 282 | rows_cost_centers = cursor.fetchall() |
||
| 283 | |||
| 284 | cost_center_dict = dict() |
||
| 285 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 286 | for row in rows_cost_centers: |
||
| 287 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 288 | "name": row[1], |
||
| 289 | "uuid": row[2]} |
||
| 290 | |||
| 291 | query = (" SELECT id, name, uuid, " |
||
| 292 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
| 293 | " FROM tbl_shopfloors " |
||
| 294 | " WHERE id = %s ") |
||
| 295 | cursor.execute(query, (id_,)) |
||
| 296 | row = cursor.fetchone() |
||
| 297 | cursor.close() |
||
| 298 | cnx.close() |
||
| 299 | |||
| 300 | if row is None: |
||
| 301 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 302 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 303 | else: |
||
| 304 | meta_result = {"id": row[0], |
||
| 305 | "name": row[1], |
||
| 306 | "uuid": row[2], |
||
| 307 | "area": row[3], |
||
| 308 | "is_input_counted": bool(row[4]), |
||
| 309 | "contact": contact_dict.get(row[5], None), |
||
| 310 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 311 | "description": row[7], |
||
| 312 | "qrcode": "shopfloor:" + row[2]} |
||
| 313 | |||
| 314 | resp.text = json.dumps(meta_result) |
||
| 315 | |||
| 316 | View Code Duplication | @staticmethod |
|
| 317 | @user_logger |
||
| 318 | def on_delete(req, resp, id_): |
||
| 319 | admin_control(req) |
||
| 320 | if not id_.isdigit() or int(id_) <= 0: |
||
| 321 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 322 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 323 | |||
| 324 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 325 | cursor = cnx.cursor() |
||
| 326 | |||
| 327 | cursor.execute(" SELECT name " |
||
| 328 | " FROM tbl_shopfloors " |
||
| 329 | " WHERE id = %s ", (id_,)) |
||
| 330 | if cursor.fetchone() is None: |
||
| 331 | cursor.close() |
||
| 332 | cnx.close() |
||
| 333 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 334 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 335 | |||
| 336 | # check relation with spaces |
||
| 337 | cursor.execute(" SELECT space_id " |
||
| 338 | " FROM tbl_spaces_shopfloors " |
||
| 339 | " WHERE shopfloor_id = %s ", |
||
| 340 | (id_,)) |
||
| 341 | rows_spaces = cursor.fetchall() |
||
| 342 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
| 343 | cursor.close() |
||
| 344 | cnx.close() |
||
| 345 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 346 | title='API.BAD_REQUEST', |
||
| 347 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 348 | |||
| 349 | # delete relation with equipments |
||
| 350 | cursor.execute(" DELETE FROM tbl_shopfloors_equipments WHERE shopfloor_id = %s ", (id_,)) |
||
| 351 | |||
| 352 | # delete relation with meters |
||
| 353 | cursor.execute(" DELETE FROM tbl_shopfloors_meters WHERE shopfloor_id = %s ", (id_,)) |
||
| 354 | |||
| 355 | # delete relation with offline meters |
||
| 356 | cursor.execute(" DELETE FROM tbl_shopfloors_offline_meters WHERE shopfloor_id = %s ", (id_,)) |
||
| 357 | |||
| 358 | # delete relation with points |
||
| 359 | cursor.execute(" DELETE FROM tbl_shopfloors_points WHERE shopfloor_id = %s ", (id_,)) |
||
| 360 | |||
| 361 | # delete relation with sensor |
||
| 362 | cursor.execute(" DELETE FROM tbl_shopfloors_sensors WHERE shopfloor_id = %s ", (id_,)) |
||
| 363 | |||
| 364 | # delete relation with virtual meter |
||
| 365 | cursor.execute(" DELETE FROM tbl_shopfloors_virtual_meters WHERE shopfloor_id = %s ", (id_,)) |
||
| 366 | |||
| 367 | # delete relation with command |
||
| 368 | cursor.execute(" DELETE FROM tbl_shopfloors_commands WHERE shopfloor_id = %s ", (id_,)) |
||
| 369 | |||
| 370 | # delete relation with working calendar |
||
| 371 | cursor.execute(" DELETE FROM tbl_shopfloors_working_calendars WHERE shopfloor_id = %s ", (id_,)) |
||
| 372 | |||
| 373 | cursor.execute(" DELETE FROM tbl_shopfloors WHERE id = %s ", (id_,)) |
||
| 374 | cnx.commit() |
||
| 375 | |||
| 376 | cursor.close() |
||
| 377 | cnx.close() |
||
| 378 | |||
| 379 | resp.status = falcon.HTTP_204 |
||
| 380 | |||
| 381 | @staticmethod |
||
| 382 | @user_logger |
||
| 383 | def on_put(req, resp, id_): |
||
| 384 | """Handles PUT requests""" |
||
| 385 | admin_control(req) |
||
| 386 | try: |
||
| 387 | raw_json = req.stream.read().decode('utf-8') |
||
| 388 | except UnicodeDecodeError as ex: |
||
| 389 | print("Failed to decode request") |
||
| 390 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 391 | title='API.BAD_REQUEST', |
||
| 392 | description='API.INVALID_ENCODING') |
||
| 393 | except Exception as ex: |
||
| 394 | print("Unexpected error reading request stream") |
||
| 395 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 396 | title='API.BAD_REQUEST', |
||
| 397 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 398 | |||
| 399 | if not id_.isdigit() or int(id_) <= 0: |
||
| 400 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 401 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 402 | |||
| 403 | new_values = json.loads(raw_json) |
||
| 404 | |||
| 405 | if 'name' not in new_values['data'].keys() or \ |
||
| 406 | not isinstance(new_values['data']['name'], str) or \ |
||
| 407 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 408 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 409 | description='API.INVALID_SHOPFLOOR_NAME') |
||
| 410 | name = str.strip(new_values['data']['name']) |
||
| 411 | |||
| 412 | if 'area' not in new_values['data'].keys() or \ |
||
| 413 | not (isinstance(new_values['data']['area'], float) or |
||
| 414 | isinstance(new_values['data']['area'], int)) or \ |
||
| 415 | new_values['data']['area'] <= 0.0: |
||
| 416 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 417 | description='API.INVALID_AREA_VALUE') |
||
| 418 | area = new_values['data']['area'] |
||
| 419 | |||
| 420 | if 'is_input_counted' not in new_values['data'].keys() or \ |
||
| 421 | not isinstance(new_values['data']['is_input_counted'], bool): |
||
| 422 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 423 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 424 | is_input_counted = new_values['data']['is_input_counted'] |
||
| 425 | |||
| 426 | if 'contact_id' in new_values['data'].keys(): |
||
| 427 | if new_values['data']['contact_id'] <= 0: |
||
| 428 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 429 | description='API.INVALID_CONTACT_ID') |
||
| 430 | contact_id = new_values['data']['contact_id'] |
||
| 431 | else: |
||
| 432 | contact_id = None |
||
| 433 | |||
| 434 | if 'cost_center_id' in new_values['data'].keys(): |
||
| 435 | if new_values['data']['cost_center_id'] <= 0: |
||
| 436 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 437 | description='API.INVALID_COST_CENTER_ID') |
||
| 438 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 439 | else: |
||
| 440 | cost_center_id = None |
||
| 441 | |||
| 442 | if 'description' in new_values['data'].keys() and \ |
||
| 443 | new_values['data']['description'] is not None and \ |
||
| 444 | len(str(new_values['data']['description'])) > 0: |
||
| 445 | description = str.strip(new_values['data']['description']) |
||
| 446 | else: |
||
| 447 | description = None |
||
| 448 | |||
| 449 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 450 | cursor = cnx.cursor() |
||
| 451 | |||
| 452 | cursor.execute(" SELECT name " |
||
| 453 | " FROM tbl_shopfloors " |
||
| 454 | " WHERE id = %s ", (id_,)) |
||
| 455 | if cursor.fetchone() is None: |
||
| 456 | cursor.close() |
||
| 457 | cnx.close() |
||
| 458 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 459 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 460 | |||
| 461 | cursor.execute(" SELECT name " |
||
| 462 | " FROM tbl_shopfloors " |
||
| 463 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 464 | if cursor.fetchone() is not None: |
||
| 465 | cursor.close() |
||
| 466 | cnx.close() |
||
| 467 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 468 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
| 469 | |||
| 470 | View Code Duplication | if contact_id is not None: |
|
| 471 | cursor.execute(" SELECT name " |
||
| 472 | " FROM tbl_contacts " |
||
| 473 | " WHERE id = %s ", |
||
| 474 | (new_values['data']['contact_id'],)) |
||
| 475 | row = cursor.fetchone() |
||
| 476 | if row is None: |
||
| 477 | cursor.close() |
||
| 478 | cnx.close() |
||
| 479 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 480 | description='API.CONTACT_NOT_FOUND') |
||
| 481 | |||
| 482 | if cost_center_id is not None: |
||
| 483 | cursor.execute(" SELECT name " |
||
| 484 | " FROM tbl_cost_centers " |
||
| 485 | " WHERE id = %s ", |
||
| 486 | (new_values['data']['cost_center_id'],)) |
||
| 487 | row = cursor.fetchone() |
||
| 488 | if row is None: |
||
| 489 | cursor.close() |
||
| 490 | cnx.close() |
||
| 491 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 492 | description='API.COST_CENTER_NOT_FOUND') |
||
| 493 | |||
| 494 | update_row = (" UPDATE tbl_shopfloors " |
||
| 495 | " SET name = %s, area = %s, is_input_counted = %s, contact_id = %s, cost_center_id = %s, " |
||
| 496 | " description = %s " |
||
| 497 | " WHERE id = %s ") |
||
| 498 | cursor.execute(update_row, (name, |
||
| 499 | area, |
||
| 500 | is_input_counted, |
||
| 501 | contact_id, |
||
| 502 | cost_center_id, |
||
| 503 | description, |
||
| 504 | id_)) |
||
| 505 | cnx.commit() |
||
| 506 | |||
| 507 | cursor.close() |
||
| 508 | cnx.close() |
||
| 509 | |||
| 510 | resp.status = falcon.HTTP_200 |
||
| 511 | |||
| 512 | |||
| 513 | View Code Duplication | class ShopfloorEquipmentCollection: |
|
| 514 | def __init__(self): |
||
| 515 | pass |
||
| 516 | |||
| 517 | @staticmethod |
||
| 518 | def on_options(req, resp, id_): |
||
| 519 | resp.status = falcon.HTTP_200 |
||
| 520 | _ = req |
||
| 521 | _ = id_ |
||
| 522 | |||
| 523 | @staticmethod |
||
| 524 | def on_get(req, resp, id_): |
||
| 525 | if 'API-KEY' not in req.headers or \ |
||
| 526 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 527 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 528 | access_control(req) |
||
| 529 | else: |
||
| 530 | api_key_control(req) |
||
| 531 | if not id_.isdigit() or int(id_) <= 0: |
||
| 532 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 533 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 534 | |||
| 535 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 536 | cursor = cnx.cursor() |
||
| 537 | |||
| 538 | cursor.execute(" SELECT name " |
||
| 539 | " FROM tbl_shopfloors " |
||
| 540 | " WHERE id = %s ", (id_,)) |
||
| 541 | if cursor.fetchone() is None: |
||
| 542 | cursor.close() |
||
| 543 | cnx.close() |
||
| 544 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 545 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 546 | |||
| 547 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 548 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
| 549 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 550 | " ORDER BY e.id ") |
||
| 551 | cursor.execute(query, (id_,)) |
||
| 552 | rows = cursor.fetchall() |
||
| 553 | |||
| 554 | result = list() |
||
| 555 | if rows is not None and len(rows) > 0: |
||
| 556 | for row in rows: |
||
| 557 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 558 | result.append(meta_result) |
||
| 559 | |||
| 560 | resp.text = json.dumps(result) |
||
| 561 | |||
| 562 | @staticmethod |
||
| 563 | @user_logger |
||
| 564 | def on_post(req, resp, id_): |
||
| 565 | """Handles POST requests""" |
||
| 566 | admin_control(req) |
||
| 567 | try: |
||
| 568 | raw_json = req.stream.read().decode('utf-8') |
||
| 569 | except UnicodeDecodeError as ex: |
||
| 570 | print("Failed to decode request") |
||
| 571 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 572 | title='API.BAD_REQUEST', |
||
| 573 | description='API.INVALID_ENCODING') |
||
| 574 | except Exception as ex: |
||
| 575 | print("Unexpected error reading request stream") |
||
| 576 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 577 | title='API.BAD_REQUEST', |
||
| 578 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 579 | |||
| 580 | if not id_.isdigit() or int(id_) <= 0: |
||
| 581 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 582 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 583 | |||
| 584 | new_values = json.loads(raw_json) |
||
| 585 | |||
| 586 | if 'equipment_id' not in new_values['data'].keys() or \ |
||
| 587 | not isinstance(new_values['data']['equipment_id'], int) or \ |
||
| 588 | new_values['data']['equipment_id'] <= 0: |
||
| 589 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 590 | description='API.INVALID_EQUIPMENT_ID') |
||
| 591 | equipment_id = new_values['data']['equipment_id'] |
||
| 592 | |||
| 593 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 594 | cursor = cnx.cursor() |
||
| 595 | |||
| 596 | cursor.execute(" SELECT name " |
||
| 597 | " from tbl_shopfloors " |
||
| 598 | " WHERE id = %s ", (id_,)) |
||
| 599 | if cursor.fetchone() is None: |
||
| 600 | cursor.close() |
||
| 601 | cnx.close() |
||
| 602 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 603 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 604 | |||
| 605 | cursor.execute(" SELECT name " |
||
| 606 | " FROM tbl_equipments " |
||
| 607 | " WHERE id = %s ", (equipment_id,)) |
||
| 608 | if cursor.fetchone() is None: |
||
| 609 | cursor.close() |
||
| 610 | cnx.close() |
||
| 611 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 612 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 613 | |||
| 614 | query = (" SELECT id " |
||
| 615 | " FROM tbl_shopfloors_equipments " |
||
| 616 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
| 617 | cursor.execute(query, (id_, equipment_id,)) |
||
| 618 | if cursor.fetchone() is not None: |
||
| 619 | cursor.close() |
||
| 620 | cnx.close() |
||
| 621 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 622 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
| 623 | |||
| 624 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
| 625 | " VALUES (%s, %s) ") |
||
| 626 | cursor.execute(add_row, (id_, equipment_id,)) |
||
| 627 | cnx.commit() |
||
| 628 | cursor.close() |
||
| 629 | cnx.close() |
||
| 630 | |||
| 631 | resp.status = falcon.HTTP_201 |
||
| 632 | resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id) |
||
| 633 | |||
| 634 | |||
| 635 | class ShopfloorEquipmentItem: |
||
| 636 | def __init__(self): |
||
| 637 | pass |
||
| 638 | |||
| 639 | @staticmethod |
||
| 640 | def on_options(req, resp, id_, eid): |
||
| 641 | resp.status = falcon.HTTP_200 |
||
| 642 | _ = req |
||
| 643 | _ = id_ |
||
| 644 | _ = eid |
||
| 645 | |||
| 646 | @staticmethod |
||
| 647 | @user_logger |
||
| 648 | def on_delete(req, resp, id_, eid): |
||
| 649 | admin_control(req) |
||
| 650 | if not id_.isdigit() or int(id_) <= 0: |
||
| 651 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 652 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 653 | |||
| 654 | if not eid.isdigit() or int(eid) <= 0: |
||
| 655 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 656 | description='API.INVALID_EQUIPMENT_ID') |
||
| 657 | |||
| 658 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 659 | cursor = cnx.cursor() |
||
| 660 | |||
| 661 | cursor.execute(" SELECT name " |
||
| 662 | " FROM tbl_shopfloors " |
||
| 663 | " WHERE id = %s ", (id_,)) |
||
| 664 | if cursor.fetchone() is None: |
||
| 665 | cursor.close() |
||
| 666 | cnx.close() |
||
| 667 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 668 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 669 | |||
| 670 | cursor.execute(" SELECT name " |
||
| 671 | " FROM tbl_equipments " |
||
| 672 | " WHERE id = %s ", (eid,)) |
||
| 673 | if cursor.fetchone() is None: |
||
| 674 | cursor.close() |
||
| 675 | cnx.close() |
||
| 676 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 677 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 678 | |||
| 679 | cursor.execute(" SELECT id " |
||
| 680 | " FROM tbl_shopfloors_equipments " |
||
| 681 | " WHERE shopfloor_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 682 | if cursor.fetchone() is None: |
||
| 683 | cursor.close() |
||
| 684 | cnx.close() |
||
| 685 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 686 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_NOT_FOUND') |
||
| 687 | |||
| 688 | cursor.execute(" DELETE FROM tbl_shopfloors_equipments " |
||
| 689 | " WHERE shopfloor_id = %s AND equipment_id = %s ", (id_, eid)) |
||
| 690 | cnx.commit() |
||
| 691 | |||
| 692 | cursor.close() |
||
| 693 | cnx.close() |
||
| 694 | |||
| 695 | resp.status = falcon.HTTP_204 |
||
| 696 | |||
| 697 | |||
| 698 | View Code Duplication | class ShopfloorMeterCollection: |
|
| 699 | def __init__(self): |
||
| 700 | pass |
||
| 701 | |||
| 702 | @staticmethod |
||
| 703 | def on_options(req, resp, id_): |
||
| 704 | resp.status = falcon.HTTP_200 |
||
| 705 | _ = req |
||
| 706 | _ = id_ |
||
| 707 | |||
| 708 | @staticmethod |
||
| 709 | def on_get(req, resp, id_): |
||
| 710 | if 'API-KEY' not in req.headers or \ |
||
| 711 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 712 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 713 | access_control(req) |
||
| 714 | else: |
||
| 715 | api_key_control(req) |
||
| 716 | if not id_.isdigit() or int(id_) <= 0: |
||
| 717 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 718 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 719 | |||
| 720 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 721 | cursor = cnx.cursor() |
||
| 722 | |||
| 723 | cursor.execute(" SELECT name " |
||
| 724 | " FROM tbl_shopfloors " |
||
| 725 | " WHERE id = %s ", (id_,)) |
||
| 726 | if cursor.fetchone() is None: |
||
| 727 | cursor.close() |
||
| 728 | cnx.close() |
||
| 729 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 730 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 731 | |||
| 732 | query = (" SELECT id, name, uuid " |
||
| 733 | " FROM tbl_energy_categories ") |
||
| 734 | cursor.execute(query) |
||
| 735 | rows_energy_categories = cursor.fetchall() |
||
| 736 | |||
| 737 | energy_category_dict = dict() |
||
| 738 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 739 | for row in rows_energy_categories: |
||
| 740 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 741 | "name": row[1], |
||
| 742 | "uuid": row[2]} |
||
| 743 | |||
| 744 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 745 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
| 746 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 747 | " ORDER BY m.id ") |
||
| 748 | cursor.execute(query, (id_,)) |
||
| 749 | rows = cursor.fetchall() |
||
| 750 | |||
| 751 | result = list() |
||
| 752 | if rows is not None and len(rows) > 0: |
||
| 753 | for row in rows: |
||
| 754 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 755 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 756 | result.append(meta_result) |
||
| 757 | |||
| 758 | resp.text = json.dumps(result) |
||
| 759 | |||
| 760 | @staticmethod |
||
| 761 | @user_logger |
||
| 762 | def on_post(req, resp, id_): |
||
| 763 | """Handles POST requests""" |
||
| 764 | admin_control(req) |
||
| 765 | try: |
||
| 766 | raw_json = req.stream.read().decode('utf-8') |
||
| 767 | except UnicodeDecodeError as ex: |
||
| 768 | print("Failed to decode request") |
||
| 769 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 770 | title='API.BAD_REQUEST', |
||
| 771 | description='API.INVALID_ENCODING') |
||
| 772 | except Exception as ex: |
||
| 773 | print("Unexpected error reading request stream") |
||
| 774 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 775 | title='API.BAD_REQUEST', |
||
| 776 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 777 | |||
| 778 | if not id_.isdigit() or int(id_) <= 0: |
||
| 779 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 780 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 781 | |||
| 782 | new_values = json.loads(raw_json) |
||
| 783 | |||
| 784 | if 'meter_id' not in new_values['data'].keys() or \ |
||
| 785 | not isinstance(new_values['data']['meter_id'], int) or \ |
||
| 786 | new_values['data']['meter_id'] <= 0: |
||
| 787 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 788 | description='API.INVALID_METER_ID') |
||
| 789 | meter_id = new_values['data']['meter_id'] |
||
| 790 | |||
| 791 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 792 | cursor = cnx.cursor() |
||
| 793 | |||
| 794 | cursor.execute(" SELECT name " |
||
| 795 | " from tbl_shopfloors " |
||
| 796 | " WHERE id = %s ", (id_,)) |
||
| 797 | if cursor.fetchone() is None: |
||
| 798 | cursor.close() |
||
| 799 | cnx.close() |
||
| 800 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 801 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 802 | |||
| 803 | cursor.execute(" SELECT name " |
||
| 804 | " FROM tbl_meters " |
||
| 805 | " WHERE id = %s ", (meter_id,)) |
||
| 806 | if cursor.fetchone() is None: |
||
| 807 | cursor.close() |
||
| 808 | cnx.close() |
||
| 809 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 810 | description='API.METER_NOT_FOUND') |
||
| 811 | |||
| 812 | query = (" SELECT id " |
||
| 813 | " FROM tbl_shopfloors_meters " |
||
| 814 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
| 815 | cursor.execute(query, (id_, meter_id,)) |
||
| 816 | if cursor.fetchone() is not None: |
||
| 817 | cursor.close() |
||
| 818 | cnx.close() |
||
| 819 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 820 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
| 821 | |||
| 822 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
| 823 | " VALUES (%s, %s) ") |
||
| 824 | cursor.execute(add_row, (id_, meter_id,)) |
||
| 825 | cnx.commit() |
||
| 826 | cursor.close() |
||
| 827 | cnx.close() |
||
| 828 | |||
| 829 | resp.status = falcon.HTTP_201 |
||
| 830 | resp.location = '/shopfloors/' + str(id_) + '/meters/' + str(meter_id) |
||
| 831 | |||
| 832 | |||
| 833 | class ShopfloorMeterItem: |
||
| 834 | def __init__(self): |
||
| 835 | pass |
||
| 836 | |||
| 837 | @staticmethod |
||
| 838 | def on_options(req, resp, id_, mid): |
||
| 839 | resp.status = falcon.HTTP_200 |
||
| 840 | _ = req |
||
| 841 | _ = id_ |
||
| 842 | _ = mid |
||
| 843 | |||
| 844 | @staticmethod |
||
| 845 | @user_logger |
||
| 846 | def on_delete(req, resp, id_, mid): |
||
| 847 | admin_control(req) |
||
| 848 | if not id_.isdigit() or int(id_) <= 0: |
||
| 849 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 850 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 851 | |||
| 852 | if not mid.isdigit() or int(mid) <= 0: |
||
| 853 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 854 | description='API.INVALID_METER_ID') |
||
| 855 | |||
| 856 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 857 | cursor = cnx.cursor() |
||
| 858 | |||
| 859 | cursor.execute(" SELECT name " |
||
| 860 | " FROM tbl_shopfloors " |
||
| 861 | " WHERE id = %s ", (id_,)) |
||
| 862 | if cursor.fetchone() is None: |
||
| 863 | cursor.close() |
||
| 864 | cnx.close() |
||
| 865 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 866 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 867 | |||
| 868 | cursor.execute(" SELECT name " |
||
| 869 | " FROM tbl_meters " |
||
| 870 | " WHERE id = %s ", (mid,)) |
||
| 871 | if cursor.fetchone() is None: |
||
| 872 | cursor.close() |
||
| 873 | cnx.close() |
||
| 874 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 875 | description='API.METER_NOT_FOUND') |
||
| 876 | |||
| 877 | cursor.execute(" SELECT id " |
||
| 878 | " FROM tbl_shopfloors_meters " |
||
| 879 | " WHERE shopfloor_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 880 | if cursor.fetchone() is None: |
||
| 881 | cursor.close() |
||
| 882 | cnx.close() |
||
| 883 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 884 | description='API.SHOPFLOOR_METER_RELATION_NOT_FOUND') |
||
| 885 | |||
| 886 | cursor.execute(" DELETE FROM tbl_shopfloors_meters WHERE shopfloor_id = %s AND meter_id = %s ", (id_, mid)) |
||
| 887 | cnx.commit() |
||
| 888 | |||
| 889 | cursor.close() |
||
| 890 | cnx.close() |
||
| 891 | |||
| 892 | resp.status = falcon.HTTP_204 |
||
| 893 | |||
| 894 | |||
| 895 | View Code Duplication | class ShopfloorOfflineMeterCollection: |
|
| 896 | def __init__(self): |
||
| 897 | pass |
||
| 898 | |||
| 899 | @staticmethod |
||
| 900 | def on_options(req, resp, id_): |
||
| 901 | resp.status = falcon.HTTP_200 |
||
| 902 | _ = req |
||
| 903 | _ = id_ |
||
| 904 | |||
| 905 | @staticmethod |
||
| 906 | def on_get(req, resp, id_): |
||
| 907 | if 'API-KEY' not in req.headers or \ |
||
| 908 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 909 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 910 | access_control(req) |
||
| 911 | else: |
||
| 912 | api_key_control(req) |
||
| 913 | if not id_.isdigit() or int(id_) <= 0: |
||
| 914 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 915 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 916 | |||
| 917 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 918 | cursor = cnx.cursor() |
||
| 919 | |||
| 920 | cursor.execute(" SELECT name " |
||
| 921 | " FROM tbl_shopfloors " |
||
| 922 | " WHERE id = %s ", (id_,)) |
||
| 923 | if cursor.fetchone() is None: |
||
| 924 | cursor.close() |
||
| 925 | cnx.close() |
||
| 926 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 927 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 928 | |||
| 929 | query = (" SELECT id, name, uuid " |
||
| 930 | " FROM tbl_energy_categories ") |
||
| 931 | cursor.execute(query) |
||
| 932 | rows_energy_categories = cursor.fetchall() |
||
| 933 | |||
| 934 | energy_category_dict = dict() |
||
| 935 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 936 | for row in rows_energy_categories: |
||
| 937 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 938 | "name": row[1], |
||
| 939 | "uuid": row[2]} |
||
| 940 | |||
| 941 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 942 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
| 943 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 944 | " ORDER BY m.id ") |
||
| 945 | cursor.execute(query, (id_,)) |
||
| 946 | rows = cursor.fetchall() |
||
| 947 | |||
| 948 | result = list() |
||
| 949 | if rows is not None and len(rows) > 0: |
||
| 950 | for row in rows: |
||
| 951 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 952 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 953 | result.append(meta_result) |
||
| 954 | |||
| 955 | resp.text = json.dumps(result) |
||
| 956 | |||
| 957 | @staticmethod |
||
| 958 | @user_logger |
||
| 959 | def on_post(req, resp, id_): |
||
| 960 | """Handles POST requests""" |
||
| 961 | admin_control(req) |
||
| 962 | try: |
||
| 963 | raw_json = req.stream.read().decode('utf-8') |
||
| 964 | except UnicodeDecodeError as ex: |
||
| 965 | print("Failed to decode request") |
||
| 966 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 967 | title='API.BAD_REQUEST', |
||
| 968 | description='API.INVALID_ENCODING') |
||
| 969 | except Exception as ex: |
||
| 970 | print("Unexpected error reading request stream") |
||
| 971 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 972 | title='API.BAD_REQUEST', |
||
| 973 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 974 | |||
| 975 | if not id_.isdigit() or int(id_) <= 0: |
||
| 976 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 977 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 978 | |||
| 979 | new_values = json.loads(raw_json) |
||
| 980 | |||
| 981 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
||
| 982 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
||
| 983 | new_values['data']['offline_meter_id'] <= 0: |
||
| 984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 985 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 986 | offline_meter_id = new_values['data']['offline_meter_id'] |
||
| 987 | |||
| 988 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 989 | cursor = cnx.cursor() |
||
| 990 | |||
| 991 | cursor.execute(" SELECT name " |
||
| 992 | " from tbl_shopfloors " |
||
| 993 | " WHERE id = %s ", (id_,)) |
||
| 994 | if cursor.fetchone() is None: |
||
| 995 | cursor.close() |
||
| 996 | cnx.close() |
||
| 997 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 998 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 999 | |||
| 1000 | cursor.execute(" SELECT name " |
||
| 1001 | " FROM tbl_offline_meters " |
||
| 1002 | " WHERE id = %s ", (offline_meter_id,)) |
||
| 1003 | if cursor.fetchone() is None: |
||
| 1004 | cursor.close() |
||
| 1005 | cnx.close() |
||
| 1006 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1007 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1008 | |||
| 1009 | query = (" SELECT id " |
||
| 1010 | " FROM tbl_shopfloors_offline_meters " |
||
| 1011 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
| 1012 | cursor.execute(query, (id_, offline_meter_id,)) |
||
| 1013 | if cursor.fetchone() is not None: |
||
| 1014 | cursor.close() |
||
| 1015 | cnx.close() |
||
| 1016 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1017 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
| 1018 | |||
| 1019 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
| 1020 | " VALUES (%s, %s) ") |
||
| 1021 | cursor.execute(add_row, (id_, offline_meter_id,)) |
||
| 1022 | cnx.commit() |
||
| 1023 | cursor.close() |
||
| 1024 | cnx.close() |
||
| 1025 | |||
| 1026 | resp.status = falcon.HTTP_201 |
||
| 1027 | resp.location = '/shopfloors/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
||
| 1028 | |||
| 1029 | |||
| 1030 | class ShopfloorOfflineMeterItem: |
||
| 1031 | def __init__(self): |
||
| 1032 | pass |
||
| 1033 | |||
| 1034 | @staticmethod |
||
| 1035 | def on_options(req, resp, id_, mid): |
||
| 1036 | resp.status = falcon.HTTP_200 |
||
| 1037 | _ = req |
||
| 1038 | _ = id_ |
||
| 1039 | _ = mid |
||
| 1040 | |||
| 1041 | @staticmethod |
||
| 1042 | @user_logger |
||
| 1043 | def on_delete(req, resp, id_, mid): |
||
| 1044 | admin_control(req) |
||
| 1045 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1046 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1047 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1048 | |||
| 1049 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1050 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1051 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 1052 | |||
| 1053 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1054 | cursor = cnx.cursor() |
||
| 1055 | |||
| 1056 | cursor.execute(" SELECT name " |
||
| 1057 | " FROM tbl_shopfloors " |
||
| 1058 | " WHERE id = %s ", (id_,)) |
||
| 1059 | if cursor.fetchone() is None: |
||
| 1060 | cursor.close() |
||
| 1061 | cnx.close() |
||
| 1062 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1063 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1064 | |||
| 1065 | cursor.execute(" SELECT name " |
||
| 1066 | " FROM tbl_offline_meters " |
||
| 1067 | " WHERE id = %s ", (mid,)) |
||
| 1068 | if cursor.fetchone() is None: |
||
| 1069 | cursor.close() |
||
| 1070 | cnx.close() |
||
| 1071 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1072 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 1073 | |||
| 1074 | cursor.execute(" SELECT id " |
||
| 1075 | " FROM tbl_shopfloors_offline_meters " |
||
| 1076 | " WHERE shopfloor_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1077 | if cursor.fetchone() is None: |
||
| 1078 | cursor.close() |
||
| 1079 | cnx.close() |
||
| 1080 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1081 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_NOT_FOUND') |
||
| 1082 | |||
| 1083 | cursor.execute(" DELETE FROM tbl_shopfloors_offline_meters " |
||
| 1084 | " WHERE shopfloor_id = %s AND offline_meter_id = %s ", (id_, mid)) |
||
| 1085 | cnx.commit() |
||
| 1086 | |||
| 1087 | cursor.close() |
||
| 1088 | cnx.close() |
||
| 1089 | |||
| 1090 | resp.status = falcon.HTTP_204 |
||
| 1091 | |||
| 1092 | |||
| 1093 | View Code Duplication | class ShopfloorPointCollection: |
|
| 1094 | def __init__(self): |
||
| 1095 | pass |
||
| 1096 | |||
| 1097 | @staticmethod |
||
| 1098 | def on_options(req, resp, id_): |
||
| 1099 | resp.status = falcon.HTTP_200 |
||
| 1100 | _ = req |
||
| 1101 | _ = id_ |
||
| 1102 | |||
| 1103 | @staticmethod |
||
| 1104 | def on_get(req, resp, id_): |
||
| 1105 | if 'API-KEY' not in req.headers or \ |
||
| 1106 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1107 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1108 | access_control(req) |
||
| 1109 | else: |
||
| 1110 | api_key_control(req) |
||
| 1111 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1113 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1114 | |||
| 1115 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1116 | cursor = cnx.cursor() |
||
| 1117 | |||
| 1118 | cursor.execute(" SELECT name " |
||
| 1119 | " FROM tbl_shopfloors " |
||
| 1120 | " WHERE id = %s ", (id_,)) |
||
| 1121 | if cursor.fetchone() is None: |
||
| 1122 | cursor.close() |
||
| 1123 | cnx.close() |
||
| 1124 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1125 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1126 | |||
| 1127 | query = (" SELECT id, name, uuid " |
||
| 1128 | " FROM tbl_data_sources ") |
||
| 1129 | cursor.execute(query) |
||
| 1130 | rows_data_sources = cursor.fetchall() |
||
| 1131 | |||
| 1132 | data_source_dict = dict() |
||
| 1133 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 1134 | for row in rows_data_sources: |
||
| 1135 | data_source_dict[row[0]] = {"id": row[0], |
||
| 1136 | "name": row[1], |
||
| 1137 | "uuid": row[2]} |
||
| 1138 | |||
| 1139 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 1140 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
| 1141 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 1142 | " ORDER BY p.id ") |
||
| 1143 | cursor.execute(query, (id_,)) |
||
| 1144 | rows = cursor.fetchall() |
||
| 1145 | |||
| 1146 | result = list() |
||
| 1147 | if rows is not None and len(rows) > 0: |
||
| 1148 | for row in rows: |
||
| 1149 | meta_result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
| 1150 | result.append(meta_result) |
||
| 1151 | |||
| 1152 | resp.text = json.dumps(result) |
||
| 1153 | |||
| 1154 | @staticmethod |
||
| 1155 | @user_logger |
||
| 1156 | def on_post(req, resp, id_): |
||
| 1157 | """Handles POST requests""" |
||
| 1158 | admin_control(req) |
||
| 1159 | try: |
||
| 1160 | raw_json = req.stream.read().decode('utf-8') |
||
| 1161 | except UnicodeDecodeError as ex: |
||
| 1162 | print("Failed to decode request") |
||
| 1163 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1164 | title='API.BAD_REQUEST', |
||
| 1165 | description='API.INVALID_ENCODING') |
||
| 1166 | except Exception as ex: |
||
| 1167 | print("Unexpected error reading request stream") |
||
| 1168 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1169 | title='API.BAD_REQUEST', |
||
| 1170 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1171 | |||
| 1172 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1173 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1174 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1175 | |||
| 1176 | new_values = json.loads(raw_json) |
||
| 1177 | |||
| 1178 | if 'point_id' not in new_values['data'].keys() or \ |
||
| 1179 | not isinstance(new_values['data']['point_id'], int) or \ |
||
| 1180 | new_values['data']['point_id'] <= 0: |
||
| 1181 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1182 | description='API.INVALID_POINT_ID') |
||
| 1183 | point_id = new_values['data']['point_id'] |
||
| 1184 | |||
| 1185 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1186 | cursor = cnx.cursor() |
||
| 1187 | |||
| 1188 | cursor.execute(" SELECT name " |
||
| 1189 | " from tbl_shopfloors " |
||
| 1190 | " WHERE id = %s ", (id_,)) |
||
| 1191 | if cursor.fetchone() is None: |
||
| 1192 | cursor.close() |
||
| 1193 | cnx.close() |
||
| 1194 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1195 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1196 | |||
| 1197 | cursor.execute(" SELECT name " |
||
| 1198 | " FROM tbl_points " |
||
| 1199 | " WHERE id = %s ", (point_id,)) |
||
| 1200 | if cursor.fetchone() is None: |
||
| 1201 | cursor.close() |
||
| 1202 | cnx.close() |
||
| 1203 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1204 | description='API.POINT_NOT_FOUND') |
||
| 1205 | |||
| 1206 | query = (" SELECT id " |
||
| 1207 | " FROM tbl_shopfloors_points " |
||
| 1208 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
| 1209 | cursor.execute(query, (id_, point_id,)) |
||
| 1210 | if cursor.fetchone() is not None: |
||
| 1211 | cursor.close() |
||
| 1212 | cnx.close() |
||
| 1213 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1214 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
| 1215 | |||
| 1216 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
| 1217 | " VALUES (%s, %s) ") |
||
| 1218 | cursor.execute(add_row, (id_, point_id,)) |
||
| 1219 | cnx.commit() |
||
| 1220 | cursor.close() |
||
| 1221 | cnx.close() |
||
| 1222 | |||
| 1223 | resp.status = falcon.HTTP_201 |
||
| 1224 | resp.location = '/shopfloors/' + str(id_) + '/points/' + str(point_id) |
||
| 1225 | |||
| 1226 | |||
| 1227 | class ShopfloorPointItem: |
||
| 1228 | def __init__(self): |
||
| 1229 | pass |
||
| 1230 | |||
| 1231 | @staticmethod |
||
| 1232 | def on_options(req, resp, id_, pid): |
||
| 1233 | resp.status = falcon.HTTP_200 |
||
| 1234 | _ = req |
||
| 1235 | _ = id_ |
||
| 1236 | _ = pid |
||
| 1237 | |||
| 1238 | @staticmethod |
||
| 1239 | @user_logger |
||
| 1240 | def on_delete(req, resp, id_, pid): |
||
| 1241 | admin_control(req) |
||
| 1242 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1243 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1244 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1245 | |||
| 1246 | if not pid.isdigit() or int(pid) <= 0: |
||
| 1247 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1248 | description='API.INVALID_POINT_ID') |
||
| 1249 | |||
| 1250 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1251 | cursor = cnx.cursor() |
||
| 1252 | |||
| 1253 | cursor.execute(" SELECT name " |
||
| 1254 | " FROM tbl_shopfloors " |
||
| 1255 | " WHERE id = %s ", (id_,)) |
||
| 1256 | if cursor.fetchone() is None: |
||
| 1257 | cursor.close() |
||
| 1258 | cnx.close() |
||
| 1259 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1260 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1261 | |||
| 1262 | cursor.execute(" SELECT name " |
||
| 1263 | " FROM tbl_points " |
||
| 1264 | " WHERE id = %s ", (pid,)) |
||
| 1265 | if cursor.fetchone() is None: |
||
| 1266 | cursor.close() |
||
| 1267 | cnx.close() |
||
| 1268 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1269 | description='API.POINT_NOT_FOUND') |
||
| 1270 | |||
| 1271 | cursor.execute(" SELECT id " |
||
| 1272 | " FROM tbl_shopfloors_points " |
||
| 1273 | " WHERE shopfloor_id = %s AND point_id = %s ", (id_, pid)) |
||
| 1274 | if cursor.fetchone() is None: |
||
| 1275 | cursor.close() |
||
| 1276 | cnx.close() |
||
| 1277 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1278 | description='API.SHOPFLOOR_POINT_RELATION_NOT_FOUND') |
||
| 1279 | |||
| 1280 | cursor.execute(" DELETE FROM tbl_shopfloors_points " |
||
| 1281 | " WHERE shopfloor_id = %s AND point_id = %s ", (id_, pid)) |
||
| 1282 | cnx.commit() |
||
| 1283 | |||
| 1284 | cursor.close() |
||
| 1285 | cnx.close() |
||
| 1286 | |||
| 1287 | resp.status = falcon.HTTP_204 |
||
| 1288 | |||
| 1289 | |||
| 1290 | View Code Duplication | class ShopfloorSensorCollection: |
|
| 1291 | def __init__(self): |
||
| 1292 | pass |
||
| 1293 | |||
| 1294 | @staticmethod |
||
| 1295 | def on_options(req, resp, id_): |
||
| 1296 | resp.status = falcon.HTTP_200 |
||
| 1297 | _ = req |
||
| 1298 | _ = id_ |
||
| 1299 | |||
| 1300 | @staticmethod |
||
| 1301 | def on_get(req, resp, id_): |
||
| 1302 | if 'API-KEY' not in req.headers or \ |
||
| 1303 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1304 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1305 | access_control(req) |
||
| 1306 | else: |
||
| 1307 | api_key_control(req) |
||
| 1308 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1309 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1310 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1311 | |||
| 1312 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1313 | cursor = cnx.cursor() |
||
| 1314 | |||
| 1315 | cursor.execute(" SELECT name " |
||
| 1316 | " FROM tbl_shopfloors " |
||
| 1317 | " WHERE id = %s ", (id_,)) |
||
| 1318 | if cursor.fetchone() is None: |
||
| 1319 | cursor.close() |
||
| 1320 | cnx.close() |
||
| 1321 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1322 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1323 | |||
| 1324 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 1325 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
| 1326 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 1327 | " ORDER BY se.id ") |
||
| 1328 | cursor.execute(query, (id_,)) |
||
| 1329 | rows = cursor.fetchall() |
||
| 1330 | |||
| 1331 | result = list() |
||
| 1332 | if rows is not None and len(rows) > 0: |
||
| 1333 | for row in rows: |
||
| 1334 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1335 | result.append(meta_result) |
||
| 1336 | |||
| 1337 | resp.text = json.dumps(result) |
||
| 1338 | |||
| 1339 | @staticmethod |
||
| 1340 | @user_logger |
||
| 1341 | def on_post(req, resp, id_): |
||
| 1342 | """Handles POST requests""" |
||
| 1343 | admin_control(req) |
||
| 1344 | try: |
||
| 1345 | raw_json = req.stream.read().decode('utf-8') |
||
| 1346 | except UnicodeDecodeError as ex: |
||
| 1347 | print("Failed to decode request") |
||
| 1348 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1349 | title='API.BAD_REQUEST', |
||
| 1350 | description='API.INVALID_ENCODING') |
||
| 1351 | except Exception as ex: |
||
| 1352 | print("Unexpected error reading request stream") |
||
| 1353 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1354 | title='API.BAD_REQUEST', |
||
| 1355 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1356 | |||
| 1357 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1358 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1359 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1360 | |||
| 1361 | new_values = json.loads(raw_json) |
||
| 1362 | |||
| 1363 | if 'sensor_id' not in new_values['data'].keys() or \ |
||
| 1364 | not isinstance(new_values['data']['sensor_id'], int) or \ |
||
| 1365 | new_values['data']['sensor_id'] <= 0: |
||
| 1366 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1367 | description='API.INVALID_SENSOR_ID') |
||
| 1368 | sensor_id = new_values['data']['sensor_id'] |
||
| 1369 | |||
| 1370 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1371 | cursor = cnx.cursor() |
||
| 1372 | |||
| 1373 | cursor.execute(" SELECT name " |
||
| 1374 | " from tbl_shopfloors " |
||
| 1375 | " WHERE id = %s ", (id_,)) |
||
| 1376 | if cursor.fetchone() is None: |
||
| 1377 | cursor.close() |
||
| 1378 | cnx.close() |
||
| 1379 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1380 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1381 | |||
| 1382 | cursor.execute(" SELECT name " |
||
| 1383 | " FROM tbl_sensors " |
||
| 1384 | " WHERE id = %s ", (sensor_id,)) |
||
| 1385 | if cursor.fetchone() is None: |
||
| 1386 | cursor.close() |
||
| 1387 | cnx.close() |
||
| 1388 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1389 | description='API.SENSOR_NOT_FOUND') |
||
| 1390 | |||
| 1391 | query = (" SELECT id " |
||
| 1392 | " FROM tbl_shopfloors_sensors " |
||
| 1393 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
| 1394 | cursor.execute(query, (id_, sensor_id,)) |
||
| 1395 | if cursor.fetchone() is not None: |
||
| 1396 | cursor.close() |
||
| 1397 | cnx.close() |
||
| 1398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1399 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
| 1400 | |||
| 1401 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
| 1402 | " VALUES (%s, %s) ") |
||
| 1403 | cursor.execute(add_row, (id_, sensor_id,)) |
||
| 1404 | cnx.commit() |
||
| 1405 | cursor.close() |
||
| 1406 | cnx.close() |
||
| 1407 | |||
| 1408 | resp.status = falcon.HTTP_201 |
||
| 1409 | resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id) |
||
| 1410 | |||
| 1411 | |||
| 1412 | class ShopfloorSensorItem: |
||
| 1413 | def __init__(self): |
||
| 1414 | pass |
||
| 1415 | |||
| 1416 | @staticmethod |
||
| 1417 | def on_options(req, resp, id_, sid): |
||
| 1418 | resp.status = falcon.HTTP_200 |
||
| 1419 | _ = req |
||
| 1420 | _ = id_ |
||
| 1421 | _ = sid |
||
| 1422 | |||
| 1423 | @staticmethod |
||
| 1424 | @user_logger |
||
| 1425 | def on_delete(req, resp, id_, sid): |
||
| 1426 | admin_control(req) |
||
| 1427 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1428 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1429 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1430 | |||
| 1431 | if not sid.isdigit() or int(sid) <= 0: |
||
| 1432 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1433 | description='API.INVALID_SENSOR_ID') |
||
| 1434 | |||
| 1435 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1436 | cursor = cnx.cursor() |
||
| 1437 | |||
| 1438 | cursor.execute(" SELECT name " |
||
| 1439 | " FROM tbl_shopfloors " |
||
| 1440 | " WHERE id = %s ", (id_,)) |
||
| 1441 | if cursor.fetchone() is None: |
||
| 1442 | cursor.close() |
||
| 1443 | cnx.close() |
||
| 1444 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1445 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1446 | |||
| 1447 | cursor.execute(" SELECT name " |
||
| 1448 | " FROM tbl_sensors " |
||
| 1449 | " WHERE id = %s ", (sid,)) |
||
| 1450 | if cursor.fetchone() is None: |
||
| 1451 | cursor.close() |
||
| 1452 | cnx.close() |
||
| 1453 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1454 | description='API.SENSOR_NOT_FOUND') |
||
| 1455 | |||
| 1456 | cursor.execute(" SELECT id " |
||
| 1457 | " FROM tbl_shopfloors_sensors " |
||
| 1458 | " WHERE shopfloor_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 1459 | if cursor.fetchone() is None: |
||
| 1460 | cursor.close() |
||
| 1461 | cnx.close() |
||
| 1462 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1463 | description='API.SHOPFLOOR_SENSOR_RELATION_NOT_FOUND') |
||
| 1464 | |||
| 1465 | cursor.execute(" DELETE FROM tbl_shopfloors_sensors WHERE shopfloor_id = %s AND sensor_id = %s ", (id_, sid)) |
||
| 1466 | cnx.commit() |
||
| 1467 | |||
| 1468 | cursor.close() |
||
| 1469 | cnx.close() |
||
| 1470 | |||
| 1471 | resp.status = falcon.HTTP_204 |
||
| 1472 | |||
| 1473 | |||
| 1474 | View Code Duplication | class ShopfloorVirtualMeterCollection: |
|
| 1475 | def __init__(self): |
||
| 1476 | pass |
||
| 1477 | |||
| 1478 | @staticmethod |
||
| 1479 | def on_options(req, resp, id_): |
||
| 1480 | resp.status = falcon.HTTP_200 |
||
| 1481 | _ = req |
||
| 1482 | _ = id_ |
||
| 1483 | |||
| 1484 | @staticmethod |
||
| 1485 | def on_get(req, resp, id_): |
||
| 1486 | if 'API-KEY' not in req.headers or \ |
||
| 1487 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1488 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1489 | access_control(req) |
||
| 1490 | else: |
||
| 1491 | api_key_control(req) |
||
| 1492 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1493 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1494 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1495 | |||
| 1496 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1497 | cursor = cnx.cursor() |
||
| 1498 | |||
| 1499 | cursor.execute(" SELECT name " |
||
| 1500 | " FROM tbl_shopfloors " |
||
| 1501 | " WHERE id = %s ", (id_,)) |
||
| 1502 | if cursor.fetchone() is None: |
||
| 1503 | cursor.close() |
||
| 1504 | cnx.close() |
||
| 1505 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1506 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1507 | |||
| 1508 | query = (" SELECT id, name, uuid " |
||
| 1509 | " FROM tbl_energy_categories ") |
||
| 1510 | cursor.execute(query) |
||
| 1511 | rows_energy_categories = cursor.fetchall() |
||
| 1512 | |||
| 1513 | energy_category_dict = dict() |
||
| 1514 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 1515 | for row in rows_energy_categories: |
||
| 1516 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 1517 | "name": row[1], |
||
| 1518 | "uuid": row[2]} |
||
| 1519 | |||
| 1520 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 1521 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
| 1522 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 1523 | " ORDER BY m.id ") |
||
| 1524 | cursor.execute(query, (id_,)) |
||
| 1525 | rows = cursor.fetchall() |
||
| 1526 | |||
| 1527 | result = list() |
||
| 1528 | if rows is not None and len(rows) > 0: |
||
| 1529 | for row in rows: |
||
| 1530 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 1531 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 1532 | result.append(meta_result) |
||
| 1533 | |||
| 1534 | resp.text = json.dumps(result) |
||
| 1535 | |||
| 1536 | @staticmethod |
||
| 1537 | @user_logger |
||
| 1538 | def on_post(req, resp, id_): |
||
| 1539 | """Handles POST requests""" |
||
| 1540 | admin_control(req) |
||
| 1541 | try: |
||
| 1542 | raw_json = req.stream.read().decode('utf-8') |
||
| 1543 | except UnicodeDecodeError as ex: |
||
| 1544 | print("Failed to decode request") |
||
| 1545 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1546 | title='API.BAD_REQUEST', |
||
| 1547 | description='API.INVALID_ENCODING') |
||
| 1548 | except Exception as ex: |
||
| 1549 | print("Unexpected error reading request stream") |
||
| 1550 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1551 | title='API.BAD_REQUEST', |
||
| 1552 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1553 | |||
| 1554 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1555 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1556 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1557 | |||
| 1558 | new_values = json.loads(raw_json) |
||
| 1559 | |||
| 1560 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
||
| 1561 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
||
| 1562 | new_values['data']['virtual_meter_id'] <= 0: |
||
| 1563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1564 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 1565 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
||
| 1566 | |||
| 1567 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1568 | cursor = cnx.cursor() |
||
| 1569 | |||
| 1570 | cursor.execute(" SELECT name " |
||
| 1571 | " from tbl_shopfloors " |
||
| 1572 | " WHERE id = %s ", (id_,)) |
||
| 1573 | if cursor.fetchone() is None: |
||
| 1574 | cursor.close() |
||
| 1575 | cnx.close() |
||
| 1576 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1577 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1578 | |||
| 1579 | cursor.execute(" SELECT name " |
||
| 1580 | " FROM tbl_virtual_meters " |
||
| 1581 | " WHERE id = %s ", (virtual_meter_id,)) |
||
| 1582 | if cursor.fetchone() is None: |
||
| 1583 | cursor.close() |
||
| 1584 | cnx.close() |
||
| 1585 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1586 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 1587 | |||
| 1588 | query = (" SELECT id " |
||
| 1589 | " FROM tbl_shopfloors_virtual_meters " |
||
| 1590 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
| 1591 | cursor.execute(query, (id_, virtual_meter_id,)) |
||
| 1592 | if cursor.fetchone() is not None: |
||
| 1593 | cursor.close() |
||
| 1594 | cnx.close() |
||
| 1595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1596 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
| 1597 | |||
| 1598 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
| 1599 | " VALUES (%s, %s) ") |
||
| 1600 | cursor.execute(add_row, (id_, virtual_meter_id,)) |
||
| 1601 | cnx.commit() |
||
| 1602 | cursor.close() |
||
| 1603 | cnx.close() |
||
| 1604 | |||
| 1605 | resp.status = falcon.HTTP_201 |
||
| 1606 | resp.location = '/shopfloors/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
||
| 1607 | |||
| 1608 | |||
| 1609 | class ShopfloorVirtualMeterItem: |
||
| 1610 | def __init__(self): |
||
| 1611 | pass |
||
| 1612 | |||
| 1613 | @staticmethod |
||
| 1614 | def on_options(req, resp, id_, mid): |
||
| 1615 | resp.status = falcon.HTTP_200 |
||
| 1616 | _ = req |
||
| 1617 | _ = id_ |
||
| 1618 | _ = mid |
||
| 1619 | |||
| 1620 | @staticmethod |
||
| 1621 | @user_logger |
||
| 1622 | def on_delete(req, resp, id_, mid): |
||
| 1623 | admin_control(req) |
||
| 1624 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1625 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1626 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1627 | |||
| 1628 | if not mid.isdigit() or int(mid) <= 0: |
||
| 1629 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1630 | description='API.INVALID_VIRTUAL_METER_ID') |
||
| 1631 | |||
| 1632 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1633 | cursor = cnx.cursor() |
||
| 1634 | |||
| 1635 | cursor.execute(" SELECT name " |
||
| 1636 | " FROM tbl_shopfloors " |
||
| 1637 | " WHERE id = %s ", (id_,)) |
||
| 1638 | if cursor.fetchone() is None: |
||
| 1639 | cursor.close() |
||
| 1640 | cnx.close() |
||
| 1641 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1642 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1643 | |||
| 1644 | cursor.execute(" SELECT name " |
||
| 1645 | " FROM tbl_virtual_meters " |
||
| 1646 | " WHERE id = %s ", (mid,)) |
||
| 1647 | if cursor.fetchone() is None: |
||
| 1648 | cursor.close() |
||
| 1649 | cnx.close() |
||
| 1650 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1651 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 1652 | |||
| 1653 | cursor.execute(" SELECT id " |
||
| 1654 | " FROM tbl_shopfloors_virtual_meters " |
||
| 1655 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 1656 | if cursor.fetchone() is None: |
||
| 1657 | cursor.close() |
||
| 1658 | cnx.close() |
||
| 1659 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1660 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_NOT_FOUND') |
||
| 1661 | |||
| 1662 | cursor.execute(" DELETE FROM tbl_shopfloors_virtual_meters " |
||
| 1663 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
||
| 1664 | cnx.commit() |
||
| 1665 | |||
| 1666 | cursor.close() |
||
| 1667 | cnx.close() |
||
| 1668 | |||
| 1669 | resp.status = falcon.HTTP_204 |
||
| 1670 | |||
| 1671 | |||
| 1672 | View Code Duplication | class ShopfloorWorkingCalendarCollection: |
|
| 1673 | def __init__(self): |
||
| 1674 | pass |
||
| 1675 | |||
| 1676 | @staticmethod |
||
| 1677 | def on_options(req, resp, id_): |
||
| 1678 | resp.status = falcon.HTTP_200 |
||
| 1679 | _ = req |
||
| 1680 | _ = id_ |
||
| 1681 | |||
| 1682 | @staticmethod |
||
| 1683 | def on_get(req, resp, id_): |
||
| 1684 | if 'API-KEY' not in req.headers or \ |
||
| 1685 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1686 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1687 | access_control(req) |
||
| 1688 | else: |
||
| 1689 | api_key_control(req) |
||
| 1690 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1691 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1692 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1693 | |||
| 1694 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1695 | cursor = cnx.cursor() |
||
| 1696 | |||
| 1697 | cursor.execute(" SELECT name " |
||
| 1698 | " FROM tbl_shopfloors " |
||
| 1699 | " WHERE id = %s ", (id_,)) |
||
| 1700 | if cursor.fetchone() is None: |
||
| 1701 | cursor.close() |
||
| 1702 | cnx.close() |
||
| 1703 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1704 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1705 | |||
| 1706 | query = (" SELECT wc.id, wc.name, wc.description " |
||
| 1707 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
| 1708 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 1709 | " ORDER BY wc.id ") |
||
| 1710 | cursor.execute(query, (id_,)) |
||
| 1711 | rows = cursor.fetchall() |
||
| 1712 | |||
| 1713 | result = list() |
||
| 1714 | if rows is not None and len(rows) > 0: |
||
| 1715 | for row in rows: |
||
| 1716 | meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
||
| 1717 | result.append(meta_result) |
||
| 1718 | |||
| 1719 | resp.text = json.dumps(result) |
||
| 1720 | |||
| 1721 | @staticmethod |
||
| 1722 | @user_logger |
||
| 1723 | def on_post(req, resp, id_): |
||
| 1724 | """Handles POST requests""" |
||
| 1725 | admin_control(req) |
||
| 1726 | try: |
||
| 1727 | raw_json = req.stream.read().decode('utf-8') |
||
| 1728 | except UnicodeDecodeError as ex: |
||
| 1729 | print("Failed to decode request") |
||
| 1730 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1731 | title='API.BAD_REQUEST', |
||
| 1732 | description='API.INVALID_ENCODING') |
||
| 1733 | except Exception as ex: |
||
| 1734 | print("Unexpected error reading request stream") |
||
| 1735 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1736 | title='API.BAD_REQUEST', |
||
| 1737 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1738 | |||
| 1739 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1740 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1741 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1742 | |||
| 1743 | new_values = json.loads(raw_json) |
||
| 1744 | |||
| 1745 | if 'working_calendar_id' not in new_values['data'].keys() or \ |
||
| 1746 | not isinstance(new_values['data']['working_calendar_id'], int) or \ |
||
| 1747 | new_values['data']['working_calendar_id'] <= 0: |
||
| 1748 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1749 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
| 1750 | working_calendar_id = new_values['data']['working_calendar_id'] |
||
| 1751 | |||
| 1752 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1753 | cursor = cnx.cursor() |
||
| 1754 | |||
| 1755 | cursor.execute(" SELECT name " |
||
| 1756 | " from tbl_shopfloors " |
||
| 1757 | " WHERE id = %s ", (id_,)) |
||
| 1758 | if cursor.fetchone() is None: |
||
| 1759 | cursor.close() |
||
| 1760 | cnx.close() |
||
| 1761 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1762 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1763 | |||
| 1764 | cursor.execute(" SELECT name " |
||
| 1765 | " FROM tbl_working_calendars " |
||
| 1766 | " WHERE id = %s ", (working_calendar_id,)) |
||
| 1767 | if cursor.fetchone() is None: |
||
| 1768 | cursor.close() |
||
| 1769 | cnx.close() |
||
| 1770 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1771 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 1772 | |||
| 1773 | query = (" SELECT id " |
||
| 1774 | " FROM tbl_shopfloors_working_calendars " |
||
| 1775 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
| 1776 | cursor.execute(query, (id_, working_calendar_id,)) |
||
| 1777 | if cursor.fetchone() is not None: |
||
| 1778 | cursor.close() |
||
| 1779 | cnx.close() |
||
| 1780 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1781 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 1782 | |||
| 1783 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
| 1784 | " VALUES (%s, %s) ") |
||
| 1785 | cursor.execute(add_row, (id_, working_calendar_id,)) |
||
| 1786 | cnx.commit() |
||
| 1787 | cursor.close() |
||
| 1788 | cnx.close() |
||
| 1789 | |||
| 1790 | resp.status = falcon.HTTP_201 |
||
| 1791 | resp.location = '/shopfloors/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
||
| 1792 | |||
| 1793 | |||
| 1794 | class ShopfloorWorkingCalendarItem: |
||
| 1795 | def __init__(self): |
||
| 1796 | pass |
||
| 1797 | |||
| 1798 | @staticmethod |
||
| 1799 | def on_options(req, resp, id_, wcid): |
||
| 1800 | resp.status = falcon.HTTP_200 |
||
| 1801 | _ = req |
||
| 1802 | _ = id_ |
||
| 1803 | _ = wcid |
||
| 1804 | |||
| 1805 | @staticmethod |
||
| 1806 | @user_logger |
||
| 1807 | def on_delete(req, resp, id_, wcid): |
||
| 1808 | admin_control(req) |
||
| 1809 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1810 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1811 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1812 | |||
| 1813 | if not wcid.isdigit() or int(wcid) <= 0: |
||
| 1814 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1815 | description='API.INVALID_WORKING_CALENDAR_ID') |
||
| 1816 | |||
| 1817 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1818 | cursor = cnx.cursor() |
||
| 1819 | |||
| 1820 | cursor.execute(" SELECT name " |
||
| 1821 | " FROM tbl_shopfloors " |
||
| 1822 | " WHERE id = %s ", (id_,)) |
||
| 1823 | if cursor.fetchone() is None: |
||
| 1824 | cursor.close() |
||
| 1825 | cnx.close() |
||
| 1826 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1827 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1828 | |||
| 1829 | cursor.execute(" SELECT name " |
||
| 1830 | " FROM tbl_working_calendars " |
||
| 1831 | " WHERE id = %s ", (wcid,)) |
||
| 1832 | if cursor.fetchone() is None: |
||
| 1833 | cursor.close() |
||
| 1834 | cnx.close() |
||
| 1835 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1836 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 1837 | |||
| 1838 | cursor.execute(" SELECT id " |
||
| 1839 | " FROM tbl_shopfloors_working_calendars " |
||
| 1840 | " WHERE shopfloor_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
| 1841 | if cursor.fetchone() is None: |
||
| 1842 | cursor.close() |
||
| 1843 | cnx.close() |
||
| 1844 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1845 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_NOT_FOUND') |
||
| 1846 | |||
| 1847 | cursor.execute(" DELETE FROM tbl_shopfloors_working_calendars " |
||
| 1848 | " WHERE shopfloor_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
||
| 1849 | cnx.commit() |
||
| 1850 | |||
| 1851 | cursor.close() |
||
| 1852 | cnx.close() |
||
| 1853 | |||
| 1854 | resp.status = falcon.HTTP_204 |
||
| 1855 | |||
| 1856 | |||
| 1857 | View Code Duplication | class ShopfloorCommandCollection: |
|
| 1858 | def __init__(self): |
||
| 1859 | pass |
||
| 1860 | |||
| 1861 | @staticmethod |
||
| 1862 | def on_options(req, resp, id_): |
||
| 1863 | resp.status = falcon.HTTP_200 |
||
| 1864 | _ = req |
||
| 1865 | _ = id_ |
||
| 1866 | |||
| 1867 | @staticmethod |
||
| 1868 | def on_get(req, resp, id_): |
||
| 1869 | if 'API-KEY' not in req.headers or \ |
||
| 1870 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 1871 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 1872 | access_control(req) |
||
| 1873 | else: |
||
| 1874 | api_key_control(req) |
||
| 1875 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1876 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1877 | description='API.INVALID_STORE_ID') |
||
| 1878 | |||
| 1879 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1880 | cursor = cnx.cursor() |
||
| 1881 | |||
| 1882 | cursor.execute(" SELECT name " |
||
| 1883 | " FROM tbl_shopfloors " |
||
| 1884 | " WHERE id = %s ", (id_,)) |
||
| 1885 | if cursor.fetchone() is None: |
||
| 1886 | cursor.close() |
||
| 1887 | cnx.close() |
||
| 1888 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1889 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1890 | |||
| 1891 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 1892 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
| 1893 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 1894 | " ORDER BY c.id ") |
||
| 1895 | cursor.execute(query, (id_,)) |
||
| 1896 | rows = cursor.fetchall() |
||
| 1897 | |||
| 1898 | result = list() |
||
| 1899 | if rows is not None and len(rows) > 0: |
||
| 1900 | for row in rows: |
||
| 1901 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 1902 | result.append(meta_result) |
||
| 1903 | |||
| 1904 | resp.text = json.dumps(result) |
||
| 1905 | |||
| 1906 | @staticmethod |
||
| 1907 | @user_logger |
||
| 1908 | def on_post(req, resp, id_): |
||
| 1909 | """Handles POST requests""" |
||
| 1910 | admin_control(req) |
||
| 1911 | try: |
||
| 1912 | raw_json = req.stream.read().decode('utf-8') |
||
| 1913 | except UnicodeDecodeError as ex: |
||
| 1914 | print("Failed to decode request") |
||
| 1915 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1916 | title='API.BAD_REQUEST', |
||
| 1917 | description='API.INVALID_ENCODING') |
||
| 1918 | except Exception as ex: |
||
| 1919 | print("Unexpected error reading request stream") |
||
| 1920 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 1921 | title='API.BAD_REQUEST', |
||
| 1922 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 1923 | |||
| 1924 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1925 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1926 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1927 | |||
| 1928 | new_values = json.loads(raw_json) |
||
| 1929 | |||
| 1930 | if 'command_id' not in new_values['data'].keys() or \ |
||
| 1931 | not isinstance(new_values['data']['command_id'], int) or \ |
||
| 1932 | new_values['data']['command_id'] <= 0: |
||
| 1933 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1934 | description='API.INVALID_COMMAND_ID') |
||
| 1935 | command_id = new_values['data']['command_id'] |
||
| 1936 | |||
| 1937 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 1938 | cursor = cnx.cursor() |
||
| 1939 | |||
| 1940 | cursor.execute(" SELECT name " |
||
| 1941 | " from tbl_shopfloors " |
||
| 1942 | " WHERE id = %s ", (id_,)) |
||
| 1943 | if cursor.fetchone() is None: |
||
| 1944 | cursor.close() |
||
| 1945 | cnx.close() |
||
| 1946 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1947 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 1948 | |||
| 1949 | cursor.execute(" SELECT name " |
||
| 1950 | " FROM tbl_commands " |
||
| 1951 | " WHERE id = %s ", (command_id,)) |
||
| 1952 | if cursor.fetchone() is None: |
||
| 1953 | cursor.close() |
||
| 1954 | cnx.close() |
||
| 1955 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 1956 | description='API.COMMAND_NOT_FOUND') |
||
| 1957 | |||
| 1958 | query = (" SELECT id " |
||
| 1959 | " FROM tbl_shopfloors_commands " |
||
| 1960 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
| 1961 | cursor.execute(query, (id_, command_id,)) |
||
| 1962 | if cursor.fetchone() is not None: |
||
| 1963 | cursor.close() |
||
| 1964 | cnx.close() |
||
| 1965 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 1966 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
| 1967 | |||
| 1968 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
| 1969 | " VALUES (%s, %s) ") |
||
| 1970 | cursor.execute(add_row, (id_, command_id,)) |
||
| 1971 | cnx.commit() |
||
| 1972 | cursor.close() |
||
| 1973 | cnx.close() |
||
| 1974 | |||
| 1975 | resp.status = falcon.HTTP_201 |
||
| 1976 | resp.location = '/shopfloors/' + str(id_) + '/commands/' + str(command_id) |
||
| 1977 | |||
| 1978 | |||
| 1979 | class ShopfloorCommandItem: |
||
| 1980 | def __init__(self): |
||
| 1981 | pass |
||
| 1982 | |||
| 1983 | @staticmethod |
||
| 1984 | def on_options(req, resp, id_, cid): |
||
| 1985 | resp.status = falcon.HTTP_200 |
||
| 1986 | _ = req |
||
| 1987 | _ = id_ |
||
| 1988 | _ = cid |
||
| 1989 | |||
| 1990 | @staticmethod |
||
| 1991 | @user_logger |
||
| 1992 | def on_delete(req, resp, id_, cid): |
||
| 1993 | admin_control(req) |
||
| 1994 | if not id_.isdigit() or int(id_) <= 0: |
||
| 1995 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 1996 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 1997 | |||
| 1998 | if not cid.isdigit() or int(cid) <= 0: |
||
| 1999 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2000 | description='API.INVALID_COMMAND_ID') |
||
| 2001 | |||
| 2002 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2003 | cursor = cnx.cursor() |
||
| 2004 | |||
| 2005 | cursor.execute(" SELECT name " |
||
| 2006 | " FROM tbl_shopfloors " |
||
| 2007 | " WHERE id = %s ", (id_,)) |
||
| 2008 | if cursor.fetchone() is None: |
||
| 2009 | cursor.close() |
||
| 2010 | cnx.close() |
||
| 2011 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2012 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2013 | |||
| 2014 | cursor.execute(" SELECT name " |
||
| 2015 | " FROM tbl_commands " |
||
| 2016 | " WHERE id = %s ", (cid,)) |
||
| 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.COMMAND_NOT_FOUND') |
||
| 2022 | |||
| 2023 | cursor.execute(" SELECT id " |
||
| 2024 | " FROM tbl_shopfloors_commands " |
||
| 2025 | " WHERE shopfloor_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2026 | if cursor.fetchone() is None: |
||
| 2027 | cursor.close() |
||
| 2028 | cnx.close() |
||
| 2029 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2030 | description='API.SHOPFLOOR_COMMAND_RELATION_NOT_FOUND') |
||
| 2031 | |||
| 2032 | cursor.execute(" DELETE FROM tbl_shopfloors_commands WHERE shopfloor_id = %s AND command_id = %s ", (id_, cid)) |
||
| 2033 | cnx.commit() |
||
| 2034 | |||
| 2035 | cursor.close() |
||
| 2036 | cnx.close() |
||
| 2037 | |||
| 2038 | resp.status = falcon.HTTP_204 |
||
| 2039 | |||
| 2040 | |||
| 2041 | class ShopfloorExport: |
||
| 2042 | def __init__(self): |
||
| 2043 | pass |
||
| 2044 | |||
| 2045 | @staticmethod |
||
| 2046 | def on_options(req, resp, id_): |
||
| 2047 | resp.status = falcon.HTTP_200 |
||
| 2048 | _ = req |
||
| 2049 | _ = id_ |
||
| 2050 | |||
| 2051 | @staticmethod |
||
| 2052 | def on_get(req, resp, id_): |
||
| 2053 | if 'API-KEY' not in req.headers or \ |
||
| 2054 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 2055 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 2056 | access_control(req) |
||
| 2057 | else: |
||
| 2058 | api_key_control(req) |
||
| 2059 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2060 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2061 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 2062 | |||
| 2063 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2064 | cursor = cnx.cursor() |
||
| 2065 | |||
| 2066 | query = (" SELECT id, name, uuid " |
||
| 2067 | " FROM tbl_contacts ") |
||
| 2068 | cursor.execute(query) |
||
| 2069 | rows_contacts = cursor.fetchall() |
||
| 2070 | |||
| 2071 | contact_dict = dict() |
||
| 2072 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 2073 | for row in rows_contacts: |
||
| 2074 | contact_dict[row[0]] = {"id": row[0], |
||
| 2075 | "name": row[1], |
||
| 2076 | "uuid": row[2]} |
||
| 2077 | |||
| 2078 | query = (" SELECT id, name, uuid " |
||
| 2079 | " FROM tbl_cost_centers ") |
||
| 2080 | cursor.execute(query) |
||
| 2081 | rows_cost_centers = cursor.fetchall() |
||
| 2082 | |||
| 2083 | cost_center_dict = dict() |
||
| 2084 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 2085 | for row in rows_cost_centers: |
||
| 2086 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 2087 | "name": row[1], |
||
| 2088 | "uuid": row[2]} |
||
| 2089 | |||
| 2090 | query = (" SELECT id, name, uuid, " |
||
| 2091 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
| 2092 | " FROM tbl_shopfloors " |
||
| 2093 | " WHERE id = %s ") |
||
| 2094 | cursor.execute(query, (id_,)) |
||
| 2095 | row = cursor.fetchone() |
||
| 2096 | |||
| 2097 | if row is None: |
||
| 2098 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2099 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2100 | else: |
||
| 2101 | meta_result = {"id": row[0], |
||
| 2102 | "name": row[1], |
||
| 2103 | "uuid": row[2], |
||
| 2104 | "area": row[3], |
||
| 2105 | "is_input_counted": bool(row[4]), |
||
| 2106 | "contact": contact_dict.get(row[5], None), |
||
| 2107 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 2108 | "description": row[7], |
||
| 2109 | "equipments": None, |
||
| 2110 | "commands": None, |
||
| 2111 | "meters": None, |
||
| 2112 | "offline_meters": None, |
||
| 2113 | "virtual_meters": None, |
||
| 2114 | "points": None, |
||
| 2115 | "sensors": None, |
||
| 2116 | "working_calendars": None |
||
| 2117 | } |
||
| 2118 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 2119 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
| 2120 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 2121 | " ORDER BY e.id ") |
||
| 2122 | cursor.execute(query, (id_,)) |
||
| 2123 | rows = cursor.fetchall() |
||
| 2124 | |||
| 2125 | equipment_result = list() |
||
| 2126 | if rows is not None and len(rows) > 0: |
||
| 2127 | for row in rows: |
||
| 2128 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2129 | equipment_result.append(result) |
||
| 2130 | meta_result['equipments'] = equipment_result |
||
| 2131 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2132 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
| 2133 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 2134 | " ORDER BY c.id ") |
||
| 2135 | cursor.execute(query, (id_,)) |
||
| 2136 | rows = cursor.fetchall() |
||
| 2137 | |||
| 2138 | command_result = list() |
||
| 2139 | if rows is not None and len(rows) > 0: |
||
| 2140 | for row in rows: |
||
| 2141 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2142 | command_result.append(result) |
||
| 2143 | meta_result['commands'] = command_result |
||
| 2144 | query = (" SELECT id, name, uuid " |
||
| 2145 | " FROM tbl_energy_categories ") |
||
| 2146 | cursor.execute(query) |
||
| 2147 | rows_energy_categories = cursor.fetchall() |
||
| 2148 | |||
| 2149 | energy_category_dict = dict() |
||
| 2150 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2151 | for row in rows_energy_categories: |
||
| 2152 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2153 | "name": row[1], |
||
| 2154 | "uuid": row[2]} |
||
| 2155 | |||
| 2156 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2157 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
| 2158 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 2159 | " ORDER BY m.id ") |
||
| 2160 | cursor.execute(query, (id_,)) |
||
| 2161 | rows = cursor.fetchall() |
||
| 2162 | |||
| 2163 | meter_result = list() |
||
| 2164 | if rows is not None and len(rows) > 0: |
||
| 2165 | for row in rows: |
||
| 2166 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2167 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2168 | meter_result.append(result) |
||
| 2169 | meta_result['meters'] = meter_result |
||
| 2170 | cursor.execute(" SELECT name " |
||
| 2171 | " FROM tbl_shopfloors " |
||
| 2172 | " WHERE id = %s ", (id_,)) |
||
| 2173 | if cursor.fetchone() is None: |
||
| 2174 | cursor.close() |
||
| 2175 | cnx.close() |
||
| 2176 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2177 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2178 | |||
| 2179 | query = (" SELECT id, name, uuid " |
||
| 2180 | " FROM tbl_energy_categories ") |
||
| 2181 | cursor.execute(query) |
||
| 2182 | rows_energy_categories = cursor.fetchall() |
||
| 2183 | |||
| 2184 | energy_category_dict = dict() |
||
| 2185 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2186 | for row in rows_energy_categories: |
||
| 2187 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2188 | "name": row[1], |
||
| 2189 | "uuid": row[2]} |
||
| 2190 | |||
| 2191 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2192 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
| 2193 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 2194 | " ORDER BY m.id ") |
||
| 2195 | cursor.execute(query, (id_,)) |
||
| 2196 | rows = cursor.fetchall() |
||
| 2197 | |||
| 2198 | offlinemeter_result = list() |
||
| 2199 | if rows is not None and len(rows) > 0: |
||
| 2200 | for row in rows: |
||
| 2201 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2202 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2203 | offlinemeter_result.append(result) |
||
| 2204 | meta_result['offline_meters'] = offlinemeter_result |
||
| 2205 | query = (" SELECT id, name, uuid " |
||
| 2206 | " FROM tbl_energy_categories ") |
||
| 2207 | cursor.execute(query) |
||
| 2208 | rows_energy_categories = cursor.fetchall() |
||
| 2209 | |||
| 2210 | energy_category_dict = dict() |
||
| 2211 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2212 | for row in rows_energy_categories: |
||
| 2213 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2214 | "name": row[1], |
||
| 2215 | "uuid": row[2]} |
||
| 2216 | |||
| 2217 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2218 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
| 2219 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 2220 | " ORDER BY m.id ") |
||
| 2221 | cursor.execute(query, (id_,)) |
||
| 2222 | rows = cursor.fetchall() |
||
| 2223 | |||
| 2224 | virtualmeter_result = list() |
||
| 2225 | if rows is not None and len(rows) > 0: |
||
| 2226 | for row in rows: |
||
| 2227 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2228 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2229 | virtualmeter_result.append(result) |
||
| 2230 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 2231 | query = (" SELECT id, name, uuid " |
||
| 2232 | " FROM tbl_data_sources ") |
||
| 2233 | cursor.execute(query) |
||
| 2234 | rows_data_sources = cursor.fetchall() |
||
| 2235 | |||
| 2236 | data_source_dict = dict() |
||
| 2237 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 2238 | for row in rows_data_sources: |
||
| 2239 | data_source_dict[row[0]] = {"id": row[0], |
||
| 2240 | "name": row[1], |
||
| 2241 | "uuid": row[2]} |
||
| 2242 | |||
| 2243 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 2244 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
| 2245 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 2246 | " ORDER BY p.id ") |
||
| 2247 | cursor.execute(query, (id_,)) |
||
| 2248 | rows = cursor.fetchall() |
||
| 2249 | |||
| 2250 | point_result = list() |
||
| 2251 | if rows is not None and len(rows) > 0: |
||
| 2252 | for row in rows: |
||
| 2253 | result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
| 2254 | point_result.append(result) |
||
| 2255 | meta_result['points'] = point_result |
||
| 2256 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 2257 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
| 2258 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 2259 | " ORDER BY se.id ") |
||
| 2260 | cursor.execute(query, (id_,)) |
||
| 2261 | rows = cursor.fetchall() |
||
| 2262 | |||
| 2263 | sensor_result = list() |
||
| 2264 | if rows is not None and len(rows) > 0: |
||
| 2265 | for row in rows: |
||
| 2266 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2267 | sensor_result.append(result) |
||
| 2268 | meta_result['sensors'] = sensor_result |
||
| 2269 | query = (" SELECT wc.id, wc.name, wc.description " |
||
| 2270 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
| 2271 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 2272 | " ORDER BY wc.id ") |
||
| 2273 | cursor.execute(query, (id_,)) |
||
| 2274 | rows = cursor.fetchall() |
||
| 2275 | |||
| 2276 | workingcalendar_result = list() |
||
| 2277 | if rows is not None and len(rows) > 0: |
||
| 2278 | for row in rows: |
||
| 2279 | result = {"id": row[0], "name": row[1], "description": row[2]} |
||
| 2280 | workingcalendar_result.append(result) |
||
| 2281 | meta_result['working_calendars'] = workingcalendar_result |
||
| 2282 | |||
| 2283 | cursor.close() |
||
| 2284 | cnx.close() |
||
| 2285 | resp.text = json.dumps(meta_result) |
||
| 2286 | |||
| 2287 | |||
| 2288 | class ShopfloorImport: |
||
| 2289 | def __init__(self): |
||
| 2290 | pass |
||
| 2291 | |||
| 2292 | @staticmethod |
||
| 2293 | def on_options(req, resp): |
||
| 2294 | resp.status = falcon.HTTP_200 |
||
| 2295 | _ = req |
||
| 2296 | |||
| 2297 | @staticmethod |
||
| 2298 | @user_logger |
||
| 2299 | def on_post(req, resp): |
||
| 2300 | """Handles POST requests""" |
||
| 2301 | admin_control(req) |
||
| 2302 | try: |
||
| 2303 | raw_json = req.stream.read().decode('utf-8') |
||
| 2304 | except UnicodeDecodeError as ex: |
||
| 2305 | print("Failed to decode request") |
||
| 2306 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2307 | title='API.BAD_REQUEST', |
||
| 2308 | description='API.INVALID_ENCODING') |
||
| 2309 | except Exception as ex: |
||
| 2310 | print("Unexpected error reading request stream") |
||
| 2311 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 2312 | title='API.BAD_REQUEST', |
||
| 2313 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 2314 | |||
| 2315 | new_values = json.loads(raw_json) |
||
| 2316 | |||
| 2317 | if 'name' not in new_values.keys() or \ |
||
| 2318 | not isinstance(new_values['name'], str) or \ |
||
| 2319 | len(str.strip(new_values['name'])) == 0: |
||
| 2320 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2321 | description='API.INVALID_SHOPFLOOR_NAME') |
||
| 2322 | name = str.strip(new_values['name']) |
||
| 2323 | |||
| 2324 | if 'area' not in new_values.keys() or \ |
||
| 2325 | not (isinstance(new_values['area'], float) or |
||
| 2326 | isinstance(new_values['area'], int)) or \ |
||
| 2327 | new_values['area'] <= 0.0: |
||
| 2328 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2329 | description='API.INVALID_AREA_VALUE') |
||
| 2330 | area = new_values['area'] |
||
| 2331 | |||
| 2332 | if 'is_input_counted' not in new_values.keys() or \ |
||
| 2333 | not isinstance(new_values['is_input_counted'], bool): |
||
| 2334 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2335 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
||
| 2336 | is_input_counted = new_values['is_input_counted'] |
||
| 2337 | |||
| 2338 | if 'id' in new_values['contact'].keys(): |
||
| 2339 | if new_values['contact']['id'] <= 0: |
||
| 2340 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2341 | description='API.INVALID_CONTACT_ID') |
||
| 2342 | contact_id = new_values['contact']['id'] |
||
| 2343 | else: |
||
| 2344 | contact_id = None |
||
| 2345 | |||
| 2346 | if 'id' in new_values['cost_center'].keys(): |
||
| 2347 | if new_values['cost_center']['id'] <= 0: |
||
| 2348 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2349 | description='API.INVALID_COST_CENTER_ID') |
||
| 2350 | cost_center_id = new_values['cost_center']['id'] |
||
| 2351 | else: |
||
| 2352 | cost_center_id = None |
||
| 2353 | |||
| 2354 | if 'description' in new_values.keys() and \ |
||
| 2355 | new_values['description'] is not None and \ |
||
| 2356 | len(str(new_values['description'])) > 0: |
||
| 2357 | description = str.strip(new_values['description']) |
||
| 2358 | else: |
||
| 2359 | description = None |
||
| 2360 | |||
| 2361 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2362 | cursor = cnx.cursor() |
||
| 2363 | |||
| 2364 | cursor.execute(" SELECT name " |
||
| 2365 | " FROM tbl_shopfloors " |
||
| 2366 | " WHERE name = %s ", (name,)) |
||
| 2367 | if cursor.fetchone() is not None: |
||
| 2368 | cursor.close() |
||
| 2369 | cnx.close() |
||
| 2370 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2371 | description='API.SHOPFLOOR_NAME_IS_ALREADY_IN_USE') |
||
| 2372 | |||
| 2373 | View Code Duplication | if contact_id is not None: |
|
| 2374 | cursor.execute(" SELECT name " |
||
| 2375 | " FROM tbl_contacts " |
||
| 2376 | " WHERE id = %s ", |
||
| 2377 | (new_values['contact']['id'],)) |
||
| 2378 | row = cursor.fetchone() |
||
| 2379 | if row is None: |
||
| 2380 | cursor.close() |
||
| 2381 | cnx.close() |
||
| 2382 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2383 | description='API.CONTACT_NOT_FOUND') |
||
| 2384 | |||
| 2385 | if cost_center_id is not None: |
||
| 2386 | cursor.execute(" SELECT name " |
||
| 2387 | " FROM tbl_cost_centers " |
||
| 2388 | " WHERE id = %s ", |
||
| 2389 | (new_values['cost_center']['id'],)) |
||
| 2390 | row = cursor.fetchone() |
||
| 2391 | if row is None: |
||
| 2392 | cursor.close() |
||
| 2393 | cnx.close() |
||
| 2394 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2395 | description='API.COST_CENTER_NOT_FOUND') |
||
| 2396 | |||
| 2397 | add_values = (" INSERT INTO tbl_shopfloors " |
||
| 2398 | " (name, uuid, area, is_input_counted, " |
||
| 2399 | " contact_id, cost_center_id, description) " |
||
| 2400 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 2401 | cursor.execute(add_values, (name, |
||
| 2402 | str(uuid.uuid4()), |
||
| 2403 | area, |
||
| 2404 | is_input_counted, |
||
| 2405 | contact_id, |
||
| 2406 | cost_center_id, |
||
| 2407 | description)) |
||
| 2408 | new_id = cursor.lastrowid |
||
| 2409 | if new_values['equipments'] is not None and len(new_values['equipments']) > 0: |
||
| 2410 | for equipment in new_values['equipments']: |
||
| 2411 | cursor.execute(" SELECT name " |
||
| 2412 | " FROM tbl_equipments " |
||
| 2413 | " WHERE id = %s ", (equipment['id'],)) |
||
| 2414 | if cursor.fetchone() is None: |
||
| 2415 | cursor.close() |
||
| 2416 | cnx.close() |
||
| 2417 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2418 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 2419 | |||
| 2420 | query = (" SELECT id " |
||
| 2421 | " FROM tbl_shopfloors_equipments " |
||
| 2422 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
| 2423 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 2424 | if cursor.fetchone() is not None: |
||
| 2425 | cursor.close() |
||
| 2426 | cnx.close() |
||
| 2427 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2428 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
| 2429 | |||
| 2430 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
| 2431 | " VALUES (%s, %s) ") |
||
| 2432 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 2433 | if new_values['commands'] is not None and len(new_values['commands']) > 0: |
||
| 2434 | for command in new_values['commands']: |
||
| 2435 | cursor.execute(" SELECT name " |
||
| 2436 | " FROM tbl_commands " |
||
| 2437 | " WHERE id = %s ", (command['id'],)) |
||
| 2438 | if cursor.fetchone() is None: |
||
| 2439 | cursor.close() |
||
| 2440 | cnx.close() |
||
| 2441 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2442 | description='API.COMMAND_NOT_FOUND') |
||
| 2443 | |||
| 2444 | query = (" SELECT id " |
||
| 2445 | " FROM tbl_shopfloors_commands " |
||
| 2446 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
| 2447 | cursor.execute(query, (new_id, command['id'],)) |
||
| 2448 | if cursor.fetchone() is not None: |
||
| 2449 | cursor.close() |
||
| 2450 | cnx.close() |
||
| 2451 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2452 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
| 2453 | |||
| 2454 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
| 2455 | " VALUES (%s, %s) ") |
||
| 2456 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 2457 | if new_values['meters'] is not None and len(new_values['meters']) > 0: |
||
| 2458 | for meter in new_values['meters']: |
||
| 2459 | cursor.execute(" SELECT name " |
||
| 2460 | " FROM tbl_meters " |
||
| 2461 | " WHERE id = %s ", (meter['id'],)) |
||
| 2462 | if cursor.fetchone() is None: |
||
| 2463 | cursor.close() |
||
| 2464 | cnx.close() |
||
| 2465 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2466 | description='API.METER_NOT_FOUND') |
||
| 2467 | |||
| 2468 | query = (" SELECT id " |
||
| 2469 | " FROM tbl_shopfloors_meters " |
||
| 2470 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
| 2471 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 2472 | if cursor.fetchone() is not None: |
||
| 2473 | cursor.close() |
||
| 2474 | cnx.close() |
||
| 2475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2476 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
| 2477 | |||
| 2478 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
| 2479 | " VALUES (%s, %s) ") |
||
| 2480 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
| 2481 | if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
||
| 2482 | for offline_meter in new_values['offline_meters']: |
||
| 2483 | cursor.execute(" SELECT name " |
||
| 2484 | " FROM tbl_offline_meters " |
||
| 2485 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 2486 | if cursor.fetchone() is None: |
||
| 2487 | cursor.close() |
||
| 2488 | cnx.close() |
||
| 2489 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2490 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2491 | |||
| 2492 | query = (" SELECT id " |
||
| 2493 | " FROM tbl_shopfloors_offline_meters " |
||
| 2494 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
| 2495 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 2496 | if cursor.fetchone() is not None: |
||
| 2497 | cursor.close() |
||
| 2498 | cnx.close() |
||
| 2499 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2500 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
| 2501 | |||
| 2502 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
| 2503 | " VALUES (%s, %s) ") |
||
| 2504 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
| 2505 | if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
||
| 2506 | for virtual_meter in new_values['virtual_meters']: |
||
| 2507 | cursor.execute(" SELECT name " |
||
| 2508 | " FROM tbl_virtual_meters " |
||
| 2509 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 2510 | if cursor.fetchone() is None: |
||
| 2511 | cursor.close() |
||
| 2512 | cnx.close() |
||
| 2513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2514 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2515 | |||
| 2516 | query = (" SELECT id " |
||
| 2517 | " FROM tbl_shopfloors_virtual_meters " |
||
| 2518 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
| 2519 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 2520 | if cursor.fetchone() is not None: |
||
| 2521 | cursor.close() |
||
| 2522 | cnx.close() |
||
| 2523 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2524 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
| 2525 | |||
| 2526 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
| 2527 | " VALUES (%s, %s) ") |
||
| 2528 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
| 2529 | if new_values['points'] is not None and len(new_values['points']) > 0: |
||
| 2530 | for point in new_values['points']: |
||
| 2531 | cursor.execute(" SELECT name " |
||
| 2532 | " FROM tbl_points " |
||
| 2533 | " WHERE id = %s ", (point['id'],)) |
||
| 2534 | if cursor.fetchone() is None: |
||
| 2535 | cursor.close() |
||
| 2536 | cnx.close() |
||
| 2537 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2538 | description='API.POINT_NOT_FOUND') |
||
| 2539 | |||
| 2540 | query = (" SELECT id " |
||
| 2541 | " FROM tbl_shopfloors_points " |
||
| 2542 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
| 2543 | cursor.execute(query, (new_id, point['id'],)) |
||
| 2544 | if cursor.fetchone() is not None: |
||
| 2545 | cursor.close() |
||
| 2546 | cnx.close() |
||
| 2547 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2548 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
| 2549 | |||
| 2550 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
| 2551 | " VALUES (%s, %s) ") |
||
| 2552 | cursor.execute(add_row, (new_id, point['id'],)) |
||
| 2553 | if new_values['sensors'] is not None and len(new_values['sensors']) > 0: |
||
| 2554 | for sensor in new_values['sensors']: |
||
| 2555 | cursor.execute(" SELECT name " |
||
| 2556 | " FROM tbl_sensors " |
||
| 2557 | " WHERE id = %s ", (sensor['id'],)) |
||
| 2558 | if cursor.fetchone() is None: |
||
| 2559 | cursor.close() |
||
| 2560 | cnx.close() |
||
| 2561 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2562 | description='API.SENSOR_NOT_FOUND') |
||
| 2563 | |||
| 2564 | query = (" SELECT id " |
||
| 2565 | " FROM tbl_shopfloors_sensors " |
||
| 2566 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
| 2567 | cursor.execute(query, (new_id, sensor['id'],)) |
||
| 2568 | if cursor.fetchone() is not None: |
||
| 2569 | cursor.close() |
||
| 2570 | cnx.close() |
||
| 2571 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2572 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
| 2573 | |||
| 2574 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
| 2575 | " VALUES (%s, %s) ") |
||
| 2576 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
| 2577 | if new_values['working_calendars'] is not None and len(new_values['workingcalendars']) > 0: |
||
| 2578 | for working_calendar in new_values['working_calendars']: |
||
| 2579 | cursor.execute(" SELECT name " |
||
| 2580 | " FROM tbl_working_calendars " |
||
| 2581 | " WHERE id = %s ", (working_calendar['id'],)) |
||
| 2582 | if cursor.fetchone() is None: |
||
| 2583 | cursor.close() |
||
| 2584 | cnx.close() |
||
| 2585 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2586 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 2587 | |||
| 2588 | query = (" SELECT id " |
||
| 2589 | " FROM tbl_shopfloors_working_calendars " |
||
| 2590 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
| 2591 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
| 2592 | if cursor.fetchone() is not None: |
||
| 2593 | cursor.close() |
||
| 2594 | cnx.close() |
||
| 2595 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2596 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 2597 | |||
| 2598 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
| 2599 | " VALUES (%s, %s) ") |
||
| 2600 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
| 2601 | cnx.commit() |
||
| 2602 | cursor.close() |
||
| 2603 | cnx.close() |
||
| 2604 | |||
| 2605 | resp.status = falcon.HTTP_201 |
||
| 2606 | resp.location = '/shopfloors/' + str(new_id) |
||
| 2607 | |||
| 2608 | |||
| 2609 | class ShopfloorClone: |
||
| 2610 | def __init__(self): |
||
| 2611 | pass |
||
| 2612 | |||
| 2613 | @staticmethod |
||
| 2614 | def on_options(req, resp, id_): |
||
| 2615 | resp.status = falcon.HTTP_200 |
||
| 2616 | _ = req |
||
| 2617 | _ = id_ |
||
| 2618 | |||
| 2619 | @staticmethod |
||
| 2620 | def on_post(req, resp, id_): |
||
| 2621 | admin_control(req) |
||
| 2622 | if not id_.isdigit() or int(id_) <= 0: |
||
| 2623 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 2624 | description='API.INVALID_SHOPFLOOR_ID') |
||
| 2625 | |||
| 2626 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 2627 | cursor = cnx.cursor() |
||
| 2628 | |||
| 2629 | query = (" SELECT id, name, uuid " |
||
| 2630 | " FROM tbl_contacts ") |
||
| 2631 | cursor.execute(query) |
||
| 2632 | rows_contacts = cursor.fetchall() |
||
| 2633 | |||
| 2634 | contact_dict = dict() |
||
| 2635 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 2636 | for row in rows_contacts: |
||
| 2637 | contact_dict[row[0]] = {"id": row[0], |
||
| 2638 | "name": row[1], |
||
| 2639 | "uuid": row[2]} |
||
| 2640 | |||
| 2641 | query = (" SELECT id, name, uuid " |
||
| 2642 | " FROM tbl_cost_centers ") |
||
| 2643 | cursor.execute(query) |
||
| 2644 | rows_cost_centers = cursor.fetchall() |
||
| 2645 | |||
| 2646 | cost_center_dict = dict() |
||
| 2647 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 2648 | for row in rows_cost_centers: |
||
| 2649 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 2650 | "name": row[1], |
||
| 2651 | "uuid": row[2]} |
||
| 2652 | |||
| 2653 | query = (" SELECT id, name, uuid, " |
||
| 2654 | " area, is_input_counted, contact_id, cost_center_id, description " |
||
| 2655 | " FROM tbl_shopfloors " |
||
| 2656 | " WHERE id = %s ") |
||
| 2657 | cursor.execute(query, (id_,)) |
||
| 2658 | row = cursor.fetchone() |
||
| 2659 | |||
| 2660 | if row is None: |
||
| 2661 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2662 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2663 | else: |
||
| 2664 | meta_result = {"id": row[0], |
||
| 2665 | "name": row[1], |
||
| 2666 | "uuid": row[2], |
||
| 2667 | "area": row[3], |
||
| 2668 | "is_input_counted": bool(row[4]), |
||
| 2669 | "contact": contact_dict.get(row[5], None), |
||
| 2670 | "cost_center": cost_center_dict.get(row[6], None), |
||
| 2671 | "description": row[7], |
||
| 2672 | "equipments": None, |
||
| 2673 | "commands": None, |
||
| 2674 | "meters": None, |
||
| 2675 | "offline_meters": None, |
||
| 2676 | "virtual_meters": None, |
||
| 2677 | "points": None, |
||
| 2678 | "sensors": None, |
||
| 2679 | "working_calendars": None |
||
| 2680 | } |
||
| 2681 | query = (" SELECT e.id, e.name, e.uuid " |
||
| 2682 | " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e " |
||
| 2683 | " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s " |
||
| 2684 | " ORDER BY e.id ") |
||
| 2685 | cursor.execute(query, (id_,)) |
||
| 2686 | rows = cursor.fetchall() |
||
| 2687 | |||
| 2688 | equipment_result = list() |
||
| 2689 | if rows is not None and len(rows) > 0: |
||
| 2690 | for row in rows: |
||
| 2691 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2692 | equipment_result.append(result) |
||
| 2693 | meta_result['equipments'] = equipment_result |
||
| 2694 | query = (" SELECT c.id, c.name, c.uuid " |
||
| 2695 | " FROM tbl_shopfloors s, tbl_shopfloors_commands sc, tbl_commands c " |
||
| 2696 | " WHERE sc.shopfloor_id = s.id AND c.id = sc.command_id AND s.id = %s " |
||
| 2697 | " ORDER BY c.id ") |
||
| 2698 | cursor.execute(query, (id_,)) |
||
| 2699 | rows = cursor.fetchall() |
||
| 2700 | |||
| 2701 | command_result = list() |
||
| 2702 | if rows is not None and len(rows) > 0: |
||
| 2703 | for row in rows: |
||
| 2704 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2705 | command_result.append(result) |
||
| 2706 | meta_result['commands'] = command_result |
||
| 2707 | query = (" SELECT id, name, uuid " |
||
| 2708 | " FROM tbl_energy_categories ") |
||
| 2709 | cursor.execute(query) |
||
| 2710 | rows_energy_categories = cursor.fetchall() |
||
| 2711 | |||
| 2712 | energy_category_dict = dict() |
||
| 2713 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2714 | for row in rows_energy_categories: |
||
| 2715 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2716 | "name": row[1], |
||
| 2717 | "uuid": row[2]} |
||
| 2718 | |||
| 2719 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2720 | " FROM tbl_shopfloors s, tbl_shopfloors_meters sm, tbl_meters m " |
||
| 2721 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.meter_id AND s.id = %s " |
||
| 2722 | " ORDER BY m.id ") |
||
| 2723 | cursor.execute(query, (id_,)) |
||
| 2724 | rows = cursor.fetchall() |
||
| 2725 | |||
| 2726 | meter_result = list() |
||
| 2727 | if rows is not None and len(rows) > 0: |
||
| 2728 | for row in rows: |
||
| 2729 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2730 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2731 | meter_result.append(result) |
||
| 2732 | meta_result['meters'] = meter_result |
||
| 2733 | cursor.execute(" SELECT name " |
||
| 2734 | " FROM tbl_shopfloors " |
||
| 2735 | " WHERE id = %s ", (id_,)) |
||
| 2736 | if cursor.fetchone() is None: |
||
| 2737 | cursor.close() |
||
| 2738 | cnx.close() |
||
| 2739 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2740 | description='API.SHOPFLOOR_NOT_FOUND') |
||
| 2741 | |||
| 2742 | query = (" SELECT id, name, uuid " |
||
| 2743 | " FROM tbl_energy_categories ") |
||
| 2744 | cursor.execute(query) |
||
| 2745 | rows_energy_categories = cursor.fetchall() |
||
| 2746 | |||
| 2747 | energy_category_dict = dict() |
||
| 2748 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2749 | for row in rows_energy_categories: |
||
| 2750 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2751 | "name": row[1], |
||
| 2752 | "uuid": row[2]} |
||
| 2753 | |||
| 2754 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2755 | " FROM tbl_shopfloors s, tbl_shopfloors_offline_meters sm, tbl_offline_meters m " |
||
| 2756 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
||
| 2757 | " ORDER BY m.id ") |
||
| 2758 | cursor.execute(query, (id_,)) |
||
| 2759 | rows = cursor.fetchall() |
||
| 2760 | |||
| 2761 | offlinemeter_result = list() |
||
| 2762 | if rows is not None and len(rows) > 0: |
||
| 2763 | for row in rows: |
||
| 2764 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2765 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2766 | offlinemeter_result.append(result) |
||
| 2767 | meta_result['offline_meters'] = offlinemeter_result |
||
| 2768 | query = (" SELECT id, name, uuid " |
||
| 2769 | " FROM tbl_energy_categories ") |
||
| 2770 | cursor.execute(query) |
||
| 2771 | rows_energy_categories = cursor.fetchall() |
||
| 2772 | |||
| 2773 | energy_category_dict = dict() |
||
| 2774 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 2775 | for row in rows_energy_categories: |
||
| 2776 | energy_category_dict[row[0]] = {"id": row[0], |
||
| 2777 | "name": row[1], |
||
| 2778 | "uuid": row[2]} |
||
| 2779 | |||
| 2780 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
||
| 2781 | " FROM tbl_shopfloors s, tbl_shopfloors_virtual_meters sm, tbl_virtual_meters m " |
||
| 2782 | " WHERE sm.shopfloor_id = s.id AND m.id = sm.virtual_meter_id AND s.id = %s " |
||
| 2783 | " ORDER BY m.id ") |
||
| 2784 | cursor.execute(query, (id_,)) |
||
| 2785 | rows = cursor.fetchall() |
||
| 2786 | |||
| 2787 | virtualmeter_result = list() |
||
| 2788 | if rows is not None and len(rows) > 0: |
||
| 2789 | for row in rows: |
||
| 2790 | result = {"id": row[0], "name": row[1], "uuid": row[2], |
||
| 2791 | "energy_category": energy_category_dict.get(row[3], None)} |
||
| 2792 | virtualmeter_result.append(result) |
||
| 2793 | meta_result['virtual_meters'] = virtualmeter_result |
||
| 2794 | query = (" SELECT id, name, uuid " |
||
| 2795 | " FROM tbl_data_sources ") |
||
| 2796 | cursor.execute(query) |
||
| 2797 | rows_data_sources = cursor.fetchall() |
||
| 2798 | |||
| 2799 | data_source_dict = dict() |
||
| 2800 | if rows_data_sources is not None and len(rows_data_sources) > 0: |
||
| 2801 | for row in rows_data_sources: |
||
| 2802 | data_source_dict[row[0]] = {"id": row[0], |
||
| 2803 | "name": row[1], |
||
| 2804 | "uuid": row[2]} |
||
| 2805 | |||
| 2806 | query = (" SELECT p.id, p.name, p.data_source_id " |
||
| 2807 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
||
| 2808 | " WHERE sp.shopfloor_id = s.id AND p.id = sp.point_id AND s.id = %s " |
||
| 2809 | " ORDER BY p.id ") |
||
| 2810 | cursor.execute(query, (id_,)) |
||
| 2811 | rows = cursor.fetchall() |
||
| 2812 | |||
| 2813 | point_result = list() |
||
| 2814 | if rows is not None and len(rows) > 0: |
||
| 2815 | for row in rows: |
||
| 2816 | result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
||
| 2817 | point_result.append(result) |
||
| 2818 | meta_result['points'] = point_result |
||
| 2819 | query = (" SELECT se.id, se.name, se.uuid " |
||
| 2820 | " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se " |
||
| 2821 | " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s " |
||
| 2822 | " ORDER BY se.id ") |
||
| 2823 | cursor.execute(query, (id_,)) |
||
| 2824 | rows = cursor.fetchall() |
||
| 2825 | |||
| 2826 | sensor_result = list() |
||
| 2827 | if rows is not None and len(rows) > 0: |
||
| 2828 | for row in rows: |
||
| 2829 | result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
| 2830 | sensor_result.append(result) |
||
| 2831 | meta_result['sensors'] = sensor_result |
||
| 2832 | query = (" SELECT wc.id, wc.name, wc.description " |
||
| 2833 | " FROM tbl_shopfloors s, tbl_shopfloors_working_calendars swc, tbl_working_calendars wc " |
||
| 2834 | " WHERE swc.shopfloor_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
||
| 2835 | " ORDER BY wc.id ") |
||
| 2836 | cursor.execute(query, (id_,)) |
||
| 2837 | rows = cursor.fetchall() |
||
| 2838 | |||
| 2839 | workingcalendar_result = list() |
||
| 2840 | if rows is not None and len(rows) > 0: |
||
| 2841 | for row in rows: |
||
| 2842 | result = {"id": row[0], "name": row[1], "description": row[2]} |
||
| 2843 | workingcalendar_result.append(result) |
||
| 2844 | meta_result['working_calendars'] = workingcalendar_result |
||
| 2845 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 2846 | if config.utc_offset[0] == '-': |
||
| 2847 | timezone_offset = -timezone_offset |
||
| 2848 | new_name = (str.strip(meta_result['name']) + |
||
| 2849 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 2850 | add_values = (" INSERT INTO tbl_shopfloors " |
||
| 2851 | " (name, uuid, area, is_input_counted, " |
||
| 2852 | " contact_id, cost_center_id, description) " |
||
| 2853 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
||
| 2854 | cursor.execute(add_values, (new_name, |
||
| 2855 | str(uuid.uuid4()), |
||
| 2856 | meta_result['area'], |
||
| 2857 | meta_result['is_input_counted'], |
||
| 2858 | meta_result['contact']['id'], |
||
| 2859 | meta_result['cost_center']['id'], |
||
| 2860 | meta_result['description'])) |
||
| 2861 | new_id = cursor.lastrowid |
||
| 2862 | if meta_result['equipments'] is not None and len(meta_result['equipments']) > 0: |
||
| 2863 | for equipment in meta_result['equipments']: |
||
| 2864 | cursor.execute(" SELECT name " |
||
| 2865 | " FROM tbl_equipments " |
||
| 2866 | " WHERE id = %s ", (equipment['id'],)) |
||
| 2867 | if cursor.fetchone() is None: |
||
| 2868 | cursor.close() |
||
| 2869 | cnx.close() |
||
| 2870 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2871 | description='API.EQUIPMENT_NOT_FOUND') |
||
| 2872 | |||
| 2873 | query = (" SELECT id " |
||
| 2874 | " FROM tbl_shopfloors_equipments " |
||
| 2875 | " WHERE shopfloor_id = %s AND equipment_id = %s") |
||
| 2876 | cursor.execute(query, (new_id, equipment['id'],)) |
||
| 2877 | if cursor.fetchone() is not None: |
||
| 2878 | cursor.close() |
||
| 2879 | cnx.close() |
||
| 2880 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2881 | description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS') |
||
| 2882 | |||
| 2883 | add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) " |
||
| 2884 | " VALUES (%s, %s) ") |
||
| 2885 | cursor.execute(add_row, (new_id, equipment['id'],)) |
||
| 2886 | if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
||
| 2887 | for command in meta_result['commands']: |
||
| 2888 | cursor.execute(" SELECT name " |
||
| 2889 | " FROM tbl_commands " |
||
| 2890 | " WHERE id = %s ", (command['id'],)) |
||
| 2891 | if cursor.fetchone() is None: |
||
| 2892 | cursor.close() |
||
| 2893 | cnx.close() |
||
| 2894 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2895 | description='API.COMMAND_NOT_FOUND') |
||
| 2896 | |||
| 2897 | query = (" SELECT id " |
||
| 2898 | " FROM tbl_shopfloors_commands " |
||
| 2899 | " WHERE shopfloor_id = %s AND command_id = %s") |
||
| 2900 | cursor.execute(query, (new_id, command['id'],)) |
||
| 2901 | if cursor.fetchone() is not None: |
||
| 2902 | cursor.close() |
||
| 2903 | cnx.close() |
||
| 2904 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2905 | description='API.SHOPFLOOR_COMMAND_RELATION_EXISTS') |
||
| 2906 | |||
| 2907 | add_row = (" INSERT INTO tbl_shopfloors_commands (shopfloor_id, command_id) " |
||
| 2908 | " VALUES (%s, %s) ") |
||
| 2909 | cursor.execute(add_row, (new_id, command['id'],)) |
||
| 2910 | if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
||
| 2911 | for meter in meta_result['meters']: |
||
| 2912 | cursor.execute(" SELECT name " |
||
| 2913 | " FROM tbl_meters " |
||
| 2914 | " WHERE id = %s ", (meter['id'],)) |
||
| 2915 | if cursor.fetchone() is None: |
||
| 2916 | cursor.close() |
||
| 2917 | cnx.close() |
||
| 2918 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2919 | description='API.METER_NOT_FOUND') |
||
| 2920 | |||
| 2921 | query = (" SELECT id " |
||
| 2922 | " FROM tbl_shopfloors_meters " |
||
| 2923 | " WHERE shopfloor_id = %s AND meter_id = %s") |
||
| 2924 | cursor.execute(query, (new_id, meter['id'],)) |
||
| 2925 | if cursor.fetchone() is not None: |
||
| 2926 | cursor.close() |
||
| 2927 | cnx.close() |
||
| 2928 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2929 | description='API.SHOPFLOOR_METER_RELATION_EXISTS') |
||
| 2930 | |||
| 2931 | add_row = (" INSERT INTO tbl_shopfloors_meters (shopfloor_id, meter_id) " |
||
| 2932 | " VALUES (%s, %s) ") |
||
| 2933 | cursor.execute(add_row, (new_id, meter['id'],)) |
||
| 2934 | if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
||
| 2935 | for offline_meter in meta_result['offline_meters']: |
||
| 2936 | cursor.execute(" SELECT name " |
||
| 2937 | " FROM tbl_offline_meters " |
||
| 2938 | " WHERE id = %s ", (offline_meter['id'],)) |
||
| 2939 | if cursor.fetchone() is None: |
||
| 2940 | cursor.close() |
||
| 2941 | cnx.close() |
||
| 2942 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2943 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 2944 | |||
| 2945 | query = (" SELECT id " |
||
| 2946 | " FROM tbl_shopfloors_offline_meters " |
||
| 2947 | " WHERE shopfloor_id = %s AND offline_meter_id = %s") |
||
| 2948 | cursor.execute(query, (new_id, offline_meter['id'],)) |
||
| 2949 | if cursor.fetchone() is not None: |
||
| 2950 | cursor.close() |
||
| 2951 | cnx.close() |
||
| 2952 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2953 | description='API.SHOPFLOOR_OFFLINE_METER_RELATION_EXISTS') |
||
| 2954 | |||
| 2955 | add_row = (" INSERT INTO tbl_shopfloors_offline_meters (shopfloor_id, offline_meter_id) " |
||
| 2956 | " VALUES (%s, %s) ") |
||
| 2957 | cursor.execute(add_row, (new_id, offline_meter['id'],)) |
||
| 2958 | if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
||
| 2959 | for virtual_meter in meta_result['virtual_meters']: |
||
| 2960 | cursor.execute(" SELECT name " |
||
| 2961 | " FROM tbl_virtual_meters " |
||
| 2962 | " WHERE id = %s ", (virtual_meter['id'],)) |
||
| 2963 | if cursor.fetchone() is None: |
||
| 2964 | cursor.close() |
||
| 2965 | cnx.close() |
||
| 2966 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2967 | description='API.VIRTUAL_METER_NOT_FOUND') |
||
| 2968 | |||
| 2969 | query = (" SELECT id " |
||
| 2970 | " FROM tbl_shopfloors_virtual_meters " |
||
| 2971 | " WHERE shopfloor_id = %s AND virtual_meter_id = %s") |
||
| 2972 | cursor.execute(query, (new_id, virtual_meter['id'],)) |
||
| 2973 | if cursor.fetchone() is not None: |
||
| 2974 | cursor.close() |
||
| 2975 | cnx.close() |
||
| 2976 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 2977 | description='API.SHOPFLOOR_VIRTUAL_METER_RELATION_EXISTS') |
||
| 2978 | |||
| 2979 | add_row = (" INSERT INTO tbl_shopfloors_virtual_meters (shopfloor_id, virtual_meter_id) " |
||
| 2980 | " VALUES (%s, %s) ") |
||
| 2981 | cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
||
| 2982 | if meta_result['points'] is not None and len(meta_result['points']) > 0: |
||
| 2983 | for point in meta_result['points']: |
||
| 2984 | cursor.execute(" SELECT name " |
||
| 2985 | " FROM tbl_points " |
||
| 2986 | " WHERE id = %s ", (point['id'],)) |
||
| 2987 | if cursor.fetchone() is None: |
||
| 2988 | cursor.close() |
||
| 2989 | cnx.close() |
||
| 2990 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 2991 | description='API.POINT_NOT_FOUND') |
||
| 2992 | |||
| 2993 | query = (" SELECT id " |
||
| 2994 | " FROM tbl_shopfloors_points " |
||
| 2995 | " WHERE shopfloor_id = %s AND point_id = %s") |
||
| 2996 | cursor.execute(query, (new_id, point['id'],)) |
||
| 2997 | if cursor.fetchone() is not None: |
||
| 2998 | cursor.close() |
||
| 2999 | cnx.close() |
||
| 3000 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3001 | description='API.SHOPFLOOR_POINT_RELATION_EXISTS') |
||
| 3002 | |||
| 3003 | add_row = (" INSERT INTO tbl_shopfloors_points (shopfloor_id, point_id) " |
||
| 3004 | " VALUES (%s, %s) ") |
||
| 3005 | cursor.execute(add_row, (new_id, point['id'],)) |
||
| 3006 | if meta_result['sensors'] is not None and len(meta_result['sensors']) > 0: |
||
| 3007 | for sensor in meta_result['sensors']: |
||
| 3008 | cursor.execute(" SELECT name " |
||
| 3009 | " FROM tbl_sensors " |
||
| 3010 | " WHERE id = %s ", (sensor['id'],)) |
||
| 3011 | if cursor.fetchone() is None: |
||
| 3012 | cursor.close() |
||
| 3013 | cnx.close() |
||
| 3014 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3015 | description='API.SENSOR_NOT_FOUND') |
||
| 3016 | |||
| 3017 | query = (" SELECT id " |
||
| 3018 | " FROM tbl_shopfloors_sensors " |
||
| 3019 | " WHERE shopfloor_id = %s AND sensor_id = %s") |
||
| 3020 | cursor.execute(query, (new_id, sensor['id'],)) |
||
| 3021 | if cursor.fetchone() is not None: |
||
| 3022 | cursor.close() |
||
| 3023 | cnx.close() |
||
| 3024 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3025 | description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS') |
||
| 3026 | |||
| 3027 | add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) " |
||
| 3028 | " VALUES (%s, %s) ") |
||
| 3029 | cursor.execute(add_row, (new_id, sensor['id'],)) |
||
| 3030 | if meta_result['working_calendars'] is not None and len(meta_result['workingcalendars']) > 0: |
||
| 3031 | for working_calendar in meta_result['working_calendars']: |
||
| 3032 | cursor.execute(" SELECT name " |
||
| 3033 | " FROM tbl_working_calendars " |
||
| 3034 | " WHERE id = %s ", (working_calendar['id'],)) |
||
| 3035 | if cursor.fetchone() is None: |
||
| 3036 | cursor.close() |
||
| 3037 | cnx.close() |
||
| 3038 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 3039 | description='API.WORKING_CALENDAR_NOT_FOUND') |
||
| 3040 | |||
| 3041 | query = (" SELECT id " |
||
| 3042 | " FROM tbl_shopfloors_working_calendars " |
||
| 3043 | " WHERE shopfloor_id = %s AND working_calendar_id = %s") |
||
| 3044 | cursor.execute(query, (new_id, working_calendar['id'],)) |
||
| 3045 | if cursor.fetchone() is not None: |
||
| 3046 | cursor.close() |
||
| 3047 | cnx.close() |
||
| 3048 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 3049 | description='API.SHOPFLOOR_WORKING_CALENDAR_RELATION_EXISTS') |
||
| 3050 | |||
| 3051 | add_row = (" INSERT INTO tbl_shopfloors_working_calendars (shopfloor_id, working_calendar_id) " |
||
| 3052 | " VALUES (%s, %s) ") |
||
| 3053 | cursor.execute(add_row, (new_id, working_calendar['id'],)) |
||
| 3054 | cnx.commit() |
||
| 3055 | cursor.close() |
||
| 3056 | cnx.close() |
||
| 3057 | |||
| 3058 | resp.status = falcon.HTTP_201 |
||
| 3059 | resp.location = '/shopfloors/' + str(new_id) |
||
| 3060 |