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