@@ 369-512 (lines=144) @@ | ||
366 | resp.status = falcon.HTTP_200 |
|
367 | ||
368 | # Clone a Combined Equipment |
|
369 | @staticmethod |
|
370 | def on_post(req, resp, id_): |
|
371 | """Handles PUT requests""" |
|
372 | if not id_.isdigit() or int(id_) <= 0: |
|
373 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
374 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
375 | try: |
|
376 | raw_json = req.stream.read().decode('utf-8') |
|
377 | except Exception as ex: |
|
378 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
379 | ||
380 | new_values = json.loads(raw_json, encoding='utf-8') |
|
381 | ||
382 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
383 | cursor = cnx.cursor(dictionary=True) |
|
384 | cursor.execute(" SELECT name " |
|
385 | " FROM tbl_combined_equipments " |
|
386 | " WHERE id = %s ", (id_,)) |
|
387 | if cursor.fetchone() is None: |
|
388 | cursor.close() |
|
389 | cnx.disconnect() |
|
390 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
391 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
392 | ||
393 | query = (" SELECT name, is_input_counted, is_output_counted, " |
|
394 | " cost_center_id, description " |
|
395 | " FROM tbl_combined_equipments " |
|
396 | " WHERE id = %s ") |
|
397 | cursor.execute(query, (id_,)) |
|
398 | row = cursor.fetchone() |
|
399 | ||
400 | if row is None: |
|
401 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
402 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
403 | else: |
|
404 | ||
405 | add_values = (" INSERT INTO tbl_combined_equipments " |
|
406 | " (name, uuid, is_input_counted, is_output_counted, " |
|
407 | " cost_center_id, description) " |
|
408 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
|
409 | cursor.execute(add_values, (row['name'] + ' Copy', |
|
410 | str(uuid.uuid4()), |
|
411 | row['is_input_counted'], |
|
412 | row['is_output_counted'], |
|
413 | row['cost_center_id'], |
|
414 | row['description'])) |
|
415 | new_id = cursor.lastrowid |
|
416 | cnx.commit() |
|
417 | ||
418 | # clone relation with meter |
|
419 | cursor.execute(" SELECT meter_id, is_output " |
|
420 | " FROM tbl_combined_equipments_meters " |
|
421 | " WHERE combined_equipment_id = %s ", |
|
422 | (id_,)) |
|
423 | rows_meters = cursor.fetchall() |
|
424 | if rows_meters is not None and len(rows_meters) > 0: |
|
425 | add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) " |
|
426 | " VALUES ") |
|
427 | for row in rows_meters: |
|
428 | add_values += " (" + str(new_id) + "," |
|
429 | add_values += str(row['meter_id']) + "," |
|
430 | add_values += str(bool(row['is_output'])) + "), " |
|
431 | # trim ", " at the end of string and then execute |
|
432 | cursor.execute(add_values[:-2]) |
|
433 | cnx.commit() |
|
434 | ||
435 | # clone relation with offline meter |
|
436 | cursor.execute(" SELECT offline_meter_id, is_output " |
|
437 | " FROM tbl_combined_equipments_offline_meters " |
|
438 | " WHERE combined_equipment_id = %s ", |
|
439 | (id_,)) |
|
440 | rows_offline_meters = cursor.fetchall() |
|
441 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
442 | add_values = (" INSERT INTO tbl_combined_equipments_offline_meters " |
|
443 | " (combined_equipment_id, offline_meter_id, is_output) " |
|
444 | " VALUES ") |
|
445 | for row in rows_offline_meters: |
|
446 | add_values += " (" + str(new_id) + "," |
|
447 | add_values += "'" + str(row['offline_meter_id']) + "'," |
|
448 | add_values += str(bool(row['is_output'])) + "), " |
|
449 | # trim ", " at the end of string and then execute |
|
450 | cursor.execute(add_values[:-2]) |
|
451 | cnx.commit() |
|
452 | ||
453 | # clone relation with virtual meter |
|
454 | cursor.execute(" SELECT virtual_meter_id, is_output " |
|
455 | " FROM tbl_combined_equipments_virtual_meters " |
|
456 | " WHERE combined_equipment_id = %s ", |
|
457 | (id_,)) |
|
458 | rows_virtual_meters = cursor.fetchall() |
|
459 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
460 | add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
|
461 | " (combined_equipment_id, virtual_meter_id, is_output) " |
|
462 | " VALUES ") |
|
463 | for row in rows_virtual_meters: |
|
464 | add_values += " (" + str(new_id) + "," |
|
465 | add_values += str(row['virtual_meter_id']) + "," |
|
466 | add_values += str(bool(row['is_output'])) + "), " |
|
467 | # trim ", " at the end of string and then execute |
|
468 | cursor.execute(add_values[:-2]) |
|
469 | cnx.commit() |
|
470 | ||
471 | # clone parameters |
|
472 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
473 | " FROM tbl_combined_equipments_parameters " |
|
474 | " WHERE combined_equipment_id = %s ", |
|
475 | (id_,)) |
|
476 | rows_parameters = cursor.fetchall() |
|
477 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
478 | add_values = (" INSERT INTO tbl_combined_equipments_parameters" |
|
479 | " (combined_equipment_id, name, parameter_type, constant, point_id, " |
|
480 | " numerator_meter_uuid, denominator_meter_uuid) " |
|
481 | " VALUES ") |
|
482 | for row in rows_parameters: |
|
483 | add_values += " (" + str(new_id) + "," |
|
484 | add_values += "'" + str(row['name']) + "'," |
|
485 | add_values += "'" + str(row['parameter_type']) + "'," |
|
486 | if row['constant'] is not None: |
|
487 | add_values += "'" + str(row['constant']) + "'," |
|
488 | else: |
|
489 | add_values += "null, " |
|
490 | ||
491 | if row['point_id'] is not None: |
|
492 | add_values += str(row['point_id']) + "," |
|
493 | else: |
|
494 | add_values += "null, " |
|
495 | ||
496 | if row['numerator_meter_uuid'] is not None: |
|
497 | add_values += "'" + row['numerator_meter_uuid'] + "'," |
|
498 | else: |
|
499 | add_values += "null, " |
|
500 | if row['denominator_meter_uuid'] is not None: |
|
501 | add_values += "'" + row['denominator_meter_uuid'] + "'), " |
|
502 | else: |
|
503 | add_values += "null), " |
|
504 | ||
505 | # trim ", " at the end of string and then execute |
|
506 | cursor.execute(add_values[:-2]) |
|
507 | cnx.commit() |
|
508 | ||
509 | cursor.close() |
|
510 | cnx.disconnect() |
|
511 | resp.status = falcon.HTTP_201 |
|
512 | resp.location = '/combinedequipments/' + str(new_id) |
|
513 | ||
514 | ||
515 | class CombinedEquipmentEquipmentCollection: |
@@ 382-523 (lines=142) @@ | ||
379 | resp.status = falcon.HTTP_200 |
|
380 | ||
381 | # Clone an Equipment |
|
382 | @staticmethod |
|
383 | def on_post(req, resp, id_): |
|
384 | """Handles PUT requests""" |
|
385 | if not id_.isdigit() or int(id_) <= 0: |
|
386 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
387 | description='API.INVALID_EQUIPMENT_ID') |
|
388 | try: |
|
389 | raw_json = req.stream.read().decode('utf-8') |
|
390 | except Exception as ex: |
|
391 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
392 | ||
393 | new_values = json.loads(raw_json, encoding='utf-8') |
|
394 | ||
395 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
396 | cursor = cnx.cursor(dictionary=True) |
|
397 | cursor.execute(" SELECT name " |
|
398 | " FROM tbl_equipments " |
|
399 | " WHERE id = %s ", (id_,)) |
|
400 | if cursor.fetchone() is None: |
|
401 | cursor.close() |
|
402 | cnx.disconnect() |
|
403 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
404 | description='API.EQUIPMENT_NOT_FOUND') |
|
405 | ||
406 | query = (" SELECT name, is_input_counted, is_output_counted, " |
|
407 | " cost_center_id, description " |
|
408 | " FROM tbl_equipments " |
|
409 | " WHERE id = %s ") |
|
410 | cursor.execute(query, (id_,)) |
|
411 | row = cursor.fetchone() |
|
412 | ||
413 | if row is None: |
|
414 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
415 | description='API.EQUIPMENT_NOT_FOUND') |
|
416 | else: |
|
417 | ||
418 | add_values = (" INSERT INTO tbl_equipments " |
|
419 | " (name, uuid, is_input_counted, is_output_counted, " |
|
420 | " cost_center_id, description) " |
|
421 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
|
422 | cursor.execute(add_values, (row['name'] + ' Copy', |
|
423 | str(uuid.uuid4()), |
|
424 | row['is_input_counted'], |
|
425 | row['is_output_counted'], |
|
426 | row['cost_center_id'], |
|
427 | row['description'])) |
|
428 | new_id = cursor.lastrowid |
|
429 | cnx.commit() |
|
430 | ||
431 | # clone relation with meter |
|
432 | cursor.execute(" SELECT meter_id, is_output " |
|
433 | " FROM tbl_equipments_meters " |
|
434 | " WHERE equipment_id = %s ", |
|
435 | (id_,)) |
|
436 | rows_meters = cursor.fetchall() |
|
437 | if rows_meters is not None and len(rows_meters) > 0: |
|
438 | add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) " |
|
439 | " VALUES ") |
|
440 | for row in rows_meters: |
|
441 | add_values += " (" + str(new_id) + "," |
|
442 | add_values += str(row['meter_id']) + "," |
|
443 | add_values += str(bool(row['is_output'])) + "), " |
|
444 | # trim ", " at the end of string and then execute |
|
445 | cursor.execute(add_values[:-2]) |
|
446 | cnx.commit() |
|
447 | ||
448 | # clone relation with offline meter |
|
449 | cursor.execute(" SELECT offline_meter_id, is_output " |
|
450 | " FROM tbl_equipments_offline_meters " |
|
451 | " WHERE equipment_id = %s ", |
|
452 | (id_,)) |
|
453 | rows_offline_meters = cursor.fetchall() |
|
454 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
455 | add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) " |
|
456 | " VALUES ") |
|
457 | for row in rows_offline_meters: |
|
458 | add_values += " (" + str(new_id) + "," |
|
459 | add_values += "'" + str(row['offline_meter_id']) + "'," |
|
460 | add_values += str(bool(row['is_output'])) + "), " |
|
461 | # trim ", " at the end of string and then execute |
|
462 | cursor.execute(add_values[:-2]) |
|
463 | cnx.commit() |
|
464 | ||
465 | # clone relation with virtual meter |
|
466 | cursor.execute(" SELECT virtual_meter_id, is_output " |
|
467 | " FROM tbl_equipments_virtual_meters " |
|
468 | " WHERE equipment_id = %s ", |
|
469 | (id_,)) |
|
470 | rows_virtual_meters = cursor.fetchall() |
|
471 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
472 | add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) " |
|
473 | " VALUES ") |
|
474 | for row in rows_virtual_meters: |
|
475 | add_values += " (" + str(new_id) + "," |
|
476 | add_values += str(row['virtual_meter_id']) + "," |
|
477 | add_values += str(bool(row['is_output'])) + "), " |
|
478 | # trim ", " at the end of string and then execute |
|
479 | cursor.execute(add_values[:-2]) |
|
480 | cnx.commit() |
|
481 | ||
482 | # clone parameters |
|
483 | cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
484 | " FROM tbl_equipments_parameters " |
|
485 | " WHERE equipment_id = %s ", |
|
486 | (id_,)) |
|
487 | rows_parameters = cursor.fetchall() |
|
488 | if rows_parameters is not None and len(rows_parameters) > 0: |
|
489 | add_values = (" INSERT INTO tbl_equipments_parameters" |
|
490 | " (equipment_id, name, parameter_type, constant, point_id, " |
|
491 | " numerator_meter_uuid, denominator_meter_uuid) " |
|
492 | " VALUES ") |
|
493 | for row in rows_parameters: |
|
494 | add_values += " (" + str(new_id) + "," |
|
495 | add_values += "'" + str(row['name']) + "'," |
|
496 | add_values += "'" + str(row['parameter_type']) + "'," |
|
497 | if row['constant'] is not None: |
|
498 | add_values += "'" + str(row['constant']) + "'," |
|
499 | else: |
|
500 | add_values += "null, " |
|
501 | ||
502 | if row['point_id'] is not None: |
|
503 | add_values += str(row['point_id']) + "," |
|
504 | else: |
|
505 | add_values += "null, " |
|
506 | ||
507 | if row['numerator_meter_uuid'] is not None: |
|
508 | add_values += "'" + row['numerator_meter_uuid'] + "'," |
|
509 | else: |
|
510 | add_values += "null, " |
|
511 | if row['denominator_meter_uuid'] is not None: |
|
512 | add_values += "'" + row['denominator_meter_uuid'] + "'), " |
|
513 | else: |
|
514 | add_values += "null), " |
|
515 | ||
516 | # trim ", " at the end of string and then execute |
|
517 | cursor.execute(add_values[:-2]) |
|
518 | cnx.commit() |
|
519 | ||
520 | cursor.close() |
|
521 | cnx.disconnect() |
|
522 | resp.status = falcon.HTTP_201 |
|
523 | resp.location = '/equipments/' + str(new_id) |
|
524 | ||
525 | ||
526 | class EquipmentParameterCollection: |