@@ 2579-2846 (lines=268) @@ | ||
2576 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
|
2577 | ||
2578 | ||
2579 | class EnergyStorageContainerGridItem: |
|
2580 | def __init__(self): |
|
2581 | """Initializes Class""" |
|
2582 | pass |
|
2583 | ||
2584 | @staticmethod |
|
2585 | def on_options(req, resp, id_, gid): |
|
2586 | _=req |
|
2587 | resp.status = falcon.HTTP_200 |
|
2588 | _=id_ |
|
2589 | @staticmethod |
|
2590 | def on_get(req, resp, id_, gid): |
|
2591 | access_control(req) |
|
2592 | if not id_.isdigit() or int(id_) <= 0: |
|
2593 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2594 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
2595 | if not gid.isdigit() or int(gid) <= 0: |
|
2596 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2597 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
2598 | ||
2599 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2600 | cursor = cnx.cursor() |
|
2601 | ||
2602 | cursor.execute(" SELECT name " |
|
2603 | " FROM tbl_energy_storage_containers " |
|
2604 | " WHERE id = %s ", (id_,)) |
|
2605 | if cursor.fetchone() is None: |
|
2606 | cursor.close() |
|
2607 | cnx.close() |
|
2608 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2609 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
2610 | ||
2611 | query = (" SELECT id, name, uuid " |
|
2612 | " FROM tbl_energy_storage_containers ") |
|
2613 | cursor.execute(query) |
|
2614 | rows_energystoragecontainers = cursor.fetchall() |
|
2615 | ||
2616 | energy_storage_container_dict = dict() |
|
2617 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
2618 | for row in rows_energystoragecontainers: |
|
2619 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
2620 | "name": row[1], |
|
2621 | "uuid": row[2]} |
|
2622 | # query meter dict |
|
2623 | query = (" SELECT id, name, uuid " |
|
2624 | " FROM tbl_meters ") |
|
2625 | cursor.execute(query) |
|
2626 | rows_meters = cursor.fetchall() |
|
2627 | ||
2628 | meter_dict = dict() |
|
2629 | if rows_meters is not None and len(rows_meters) > 0: |
|
2630 | for row in rows_meters: |
|
2631 | meter_dict[row[0]] = {"id": row[0], |
|
2632 | "name": row[1], |
|
2633 | "uuid": row[2]} |
|
2634 | # query point dict |
|
2635 | query = (" SELECT id, name " |
|
2636 | " FROM tbl_points ") |
|
2637 | cursor.execute(query) |
|
2638 | rows_points = cursor.fetchall() |
|
2639 | ||
2640 | point_dict = dict() |
|
2641 | if rows_points is not None and len(rows_points) > 0: |
|
2642 | for row in rows_points: |
|
2643 | point_dict[row[0]] = {"id": row[0], |
|
2644 | "name": row[1]} |
|
2645 | ||
2646 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
|
2647 | " buy_meter_id, sell_meter_id, capacity " |
|
2648 | " FROM tbl_energy_storage_containers_grids " |
|
2649 | " WHERE id = %s ") |
|
2650 | cursor.execute(query, (gid,)) |
|
2651 | row = cursor.fetchone() |
|
2652 | cursor.close() |
|
2653 | cnx.close() |
|
2654 | ||
2655 | if row is None: |
|
2656 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2657 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
2658 | else: |
|
2659 | meta_result = {"id": row[0], |
|
2660 | "name": row[1], |
|
2661 | "uuid": row[2], |
|
2662 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
2663 | "power_point": point_dict.get(row[4]), |
|
2664 | "buy_meter": meter_dict.get(row[5]), |
|
2665 | "sell_meter": meter_dict.get(row[6]), |
|
2666 | "capacity": row[7] |
|
2667 | } |
|
2668 | ||
2669 | resp.text = json.dumps(meta_result) |
|
2670 | ||
2671 | @staticmethod |
|
2672 | @user_logger |
|
2673 | def on_delete(req, resp, id_, gid): |
|
2674 | admin_control(req) |
|
2675 | if not id_.isdigit() or int(id_) <= 0: |
|
2676 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2677 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
2678 | if not gid.isdigit() or int(gid) <= 0: |
|
2679 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2680 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
2681 | ||
2682 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2683 | cursor = cnx.cursor() |
|
2684 | ||
2685 | cursor.execute(" SELECT name " |
|
2686 | " FROM tbl_energy_storage_containers " |
|
2687 | " WHERE id = %s ", (id_,)) |
|
2688 | if cursor.fetchone() is None: |
|
2689 | cursor.close() |
|
2690 | cnx.close() |
|
2691 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2692 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
2693 | ||
2694 | cursor.execute(" SELECT name " |
|
2695 | " FROM tbl_energy_storage_containers_grids " |
|
2696 | " WHERE id = %s ", (gid,)) |
|
2697 | if cursor.fetchone() is None: |
|
2698 | cursor.close() |
|
2699 | cnx.close() |
|
2700 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2701 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
2702 | ||
2703 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
|
2704 | " WHERE id = %s ", (gid,)) |
|
2705 | cnx.commit() |
|
2706 | ||
2707 | cursor.close() |
|
2708 | cnx.close() |
|
2709 | ||
2710 | resp.status = falcon.HTTP_204 |
|
2711 | ||
2712 | @staticmethod |
|
2713 | @user_logger |
|
2714 | def on_put(req, resp, id_, gid): |
|
2715 | """Handles PUT requests""" |
|
2716 | admin_control(req) |
|
2717 | try: |
|
2718 | raw_json = req.stream.read().decode('utf-8') |
|
2719 | except Exception as ex: |
|
2720 | print(str(ex)) |
|
2721 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2722 | title='API.BAD_REQUEST', |
|
2723 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2724 | if not id_.isdigit() or int(id_) <= 0: |
|
2725 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2726 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
2727 | ||
2728 | if not gid.isdigit() or int(gid) <= 0: |
|
2729 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2730 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
2731 | ||
2732 | new_values = json.loads(raw_json) |
|
2733 | ||
2734 | if 'name' not in new_values['data'].keys() or \ |
|
2735 | not isinstance(new_values['data']['name'], str) or \ |
|
2736 | len(str.strip(new_values['data']['name'])) == 0: |
|
2737 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2738 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
|
2739 | name = str.strip(new_values['data']['name']) |
|
2740 | ||
2741 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
2742 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
2743 | new_values['data']['power_point_id'] <= 0: |
|
2744 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2745 | description='API.INVALID_POWER_POINT_ID') |
|
2746 | power_point_id = new_values['data']['power_point_id'] |
|
2747 | ||
2748 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
2749 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
2750 | new_values['data']['buy_meter_id'] <= 0: |
|
2751 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2752 | description='API.INVALID_BUY_METER_ID') |
|
2753 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
2754 | ||
2755 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
2756 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
2757 | new_values['data']['sell_meter_id'] <= 0: |
|
2758 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2759 | description='API.INVALID_METER_ID') |
|
2760 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
2761 | ||
2762 | if 'capacity' not in new_values['data'].keys() or \ |
|
2763 | not (isinstance(new_values['data']['capacity'], float) or |
|
2764 | isinstance(new_values['data']['capacity'], int)): |
|
2765 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2766 | description='API.INVALID_CAPACITY') |
|
2767 | capacity = Decimal(new_values['data']['capacity']) |
|
2768 | ||
2769 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2770 | cursor = cnx.cursor() |
|
2771 | ||
2772 | cursor.execute(" SELECT name " |
|
2773 | " FROM tbl_energy_storage_containers " |
|
2774 | " WHERE id = %s ", (id_,)) |
|
2775 | if cursor.fetchone() is None: |
|
2776 | cursor.close() |
|
2777 | cnx.close() |
|
2778 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2779 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
2780 | ||
2781 | cursor.execute(" SELECT name " |
|
2782 | " FROM tbl_energy_storage_containers_grids " |
|
2783 | " WHERE id = %s ", (gid,)) |
|
2784 | if cursor.fetchone() is None: |
|
2785 | cursor.close() |
|
2786 | cnx.close() |
|
2787 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2788 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
2789 | ||
2790 | cursor.execute(" SELECT name " |
|
2791 | " FROM tbl_energy_storage_containers_grids " |
|
2792 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
2793 | (id_, name, gid)) |
|
2794 | if cursor.fetchone() is not None: |
|
2795 | cursor.close() |
|
2796 | cnx.close() |
|
2797 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2798 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
|
2799 | ||
2800 | cursor.execute(" SELECT name " |
|
2801 | " FROM tbl_points " |
|
2802 | " WHERE id = %s ", |
|
2803 | (power_point_id,)) |
|
2804 | if cursor.fetchone() is None: |
|
2805 | cursor.close() |
|
2806 | cnx.close() |
|
2807 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2808 | description='API.POWER_POINT_NOT_FOUND') |
|
2809 | ||
2810 | cursor.execute(" SELECT name " |
|
2811 | " FROM tbl_meters " |
|
2812 | " WHERE id = %s ", |
|
2813 | (buy_meter_id,)) |
|
2814 | if cursor.fetchone() is None: |
|
2815 | cursor.close() |
|
2816 | cnx.close() |
|
2817 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2818 | description='API.BUY_METER_NOT_FOUND') |
|
2819 | ||
2820 | cursor.execute(" SELECT name " |
|
2821 | " FROM tbl_meters " |
|
2822 | " WHERE id = %s ", |
|
2823 | (sell_meter_id,)) |
|
2824 | if cursor.fetchone() is None: |
|
2825 | cursor.close() |
|
2826 | cnx.close() |
|
2827 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2828 | description='API.SELL_METER_NOT_FOUND') |
|
2829 | ||
2830 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
|
2831 | " SET name = %s, energy_storage_container_id = %s, " |
|
2832 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
2833 | " WHERE id = %s ") |
|
2834 | cursor.execute(update_row, (name, |
|
2835 | id_, |
|
2836 | power_point_id, |
|
2837 | buy_meter_id, |
|
2838 | sell_meter_id, |
|
2839 | capacity, |
|
2840 | gid)) |
|
2841 | cnx.commit() |
|
2842 | ||
2843 | cursor.close() |
|
2844 | cnx.close() |
|
2845 | ||
2846 | resp.status = falcon.HTTP_200 |
|
2847 | ||
2848 | ||
2849 | class EnergyStorageContainerGridPointCollection: |
@@ 2358-2622 (lines=265) @@ | ||
2355 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(new_id) |
|
2356 | ||
2357 | ||
2358 | class MicrogridGridItem: |
|
2359 | def __init__(self): |
|
2360 | """Initializes MicrogridGridItem""" |
|
2361 | pass |
|
2362 | ||
2363 | @staticmethod |
|
2364 | def on_options(req, resp, id_, gid): |
|
2365 | resp.status = falcon.HTTP_200 |
|
2366 | ||
2367 | @staticmethod |
|
2368 | def on_get(req, resp, id_, gid): |
|
2369 | access_control(req) |
|
2370 | if not id_.isdigit() or int(id_) <= 0: |
|
2371 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2372 | description='API.INVALID_MICROGRID_ID') |
|
2373 | if not gid.isdigit() or int(gid) <= 0: |
|
2374 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2375 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2376 | ||
2377 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2378 | cursor = cnx.cursor() |
|
2379 | ||
2380 | cursor.execute(" SELECT name " |
|
2381 | " FROM tbl_microgrids " |
|
2382 | " WHERE id = %s ", (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.MICROGRID_NOT_FOUND') |
|
2388 | ||
2389 | # query microgrid dict |
|
2390 | query = (" SELECT id, name, uuid " |
|
2391 | " FROM tbl_microgrids ") |
|
2392 | cursor.execute(query) |
|
2393 | rows_microgrids = cursor.fetchall() |
|
2394 | ||
2395 | microgrid_dict = dict() |
|
2396 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
2397 | for row in rows_microgrids: |
|
2398 | microgrid_dict[row[0]] = {"id": row[0], |
|
2399 | "name": row[1], |
|
2400 | "uuid": row[2]} |
|
2401 | # query meter dict |
|
2402 | query = (" SELECT id, name, uuid " |
|
2403 | " FROM tbl_meters ") |
|
2404 | cursor.execute(query) |
|
2405 | rows_meters = cursor.fetchall() |
|
2406 | ||
2407 | meter_dict = dict() |
|
2408 | if rows_meters is not None and len(rows_meters) > 0: |
|
2409 | for row in rows_meters: |
|
2410 | meter_dict[row[0]] = {"id": row[0], |
|
2411 | "name": row[1], |
|
2412 | "uuid": row[2]} |
|
2413 | # query point dict |
|
2414 | query = (" SELECT id, name " |
|
2415 | " FROM tbl_points ") |
|
2416 | cursor.execute(query) |
|
2417 | rows_points = cursor.fetchall() |
|
2418 | ||
2419 | point_dict = dict() |
|
2420 | if rows_points is not None and len(rows_points) > 0: |
|
2421 | for row in rows_points: |
|
2422 | point_dict[row[0]] = {"id": row[0], |
|
2423 | "name": row[1]} |
|
2424 | ||
2425 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity " |
|
2426 | " FROM tbl_microgrids_grids " |
|
2427 | " WHERE id = %s ") |
|
2428 | cursor.execute(query, (gid,)) |
|
2429 | row = cursor.fetchone() |
|
2430 | cursor.close() |
|
2431 | cnx.close() |
|
2432 | ||
2433 | if row is None: |
|
2434 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2435 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2436 | else: |
|
2437 | meta_result = {"id": row[0], |
|
2438 | "name": row[1], |
|
2439 | "uuid": row[2], |
|
2440 | "microgrid": microgrid_dict.get(row[3]), |
|
2441 | "power_point": point_dict.get(row[4]), |
|
2442 | "buy_meter": meter_dict.get(row[5]), |
|
2443 | "sell_meter": meter_dict.get(row[6]), |
|
2444 | "capacity": row[7]} |
|
2445 | ||
2446 | resp.text = json.dumps(meta_result) |
|
2447 | ||
2448 | @staticmethod |
|
2449 | @user_logger |
|
2450 | def on_delete(req, resp, id_, gid): |
|
2451 | admin_control(req) |
|
2452 | if not id_.isdigit() or int(id_) <= 0: |
|
2453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2454 | description='API.INVALID_MICROGRID_ID') |
|
2455 | if not gid.isdigit() or int(gid) <= 0: |
|
2456 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2457 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2458 | ||
2459 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2460 | cursor = cnx.cursor() |
|
2461 | ||
2462 | cursor.execute(" SELECT name " |
|
2463 | " FROM tbl_microgrids " |
|
2464 | " WHERE id = %s ", (id_,)) |
|
2465 | if cursor.fetchone() is None: |
|
2466 | cursor.close() |
|
2467 | cnx.close() |
|
2468 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2469 | description='API.MICROGRID_NOT_FOUND') |
|
2470 | ||
2471 | cursor.execute(" SELECT name " |
|
2472 | " FROM tbl_microgrids_grids " |
|
2473 | " WHERE id = %s ", (gid,)) |
|
2474 | if cursor.fetchone() is None: |
|
2475 | cursor.close() |
|
2476 | cnx.close() |
|
2477 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2478 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2479 | ||
2480 | cursor.execute(" DELETE FROM tbl_microgrids_grids " |
|
2481 | " WHERE id = %s ", (gid,)) |
|
2482 | cnx.commit() |
|
2483 | ||
2484 | cursor.close() |
|
2485 | cnx.close() |
|
2486 | ||
2487 | resp.status = falcon.HTTP_204 |
|
2488 | ||
2489 | @staticmethod |
|
2490 | @user_logger |
|
2491 | def on_put(req, resp, id_, gid): |
|
2492 | """Handles PUT requests""" |
|
2493 | admin_control(req) |
|
2494 | try: |
|
2495 | raw_json = req.stream.read().decode('utf-8') |
|
2496 | except Exception as ex: |
|
2497 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2498 | title='API.BAD_REQUEST', |
|
2499 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2500 | if not id_.isdigit() or int(id_) <= 0: |
|
2501 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2502 | description='API.INVALID_MICROGRID_ID') |
|
2503 | ||
2504 | if not gid.isdigit() or int(gid) <= 0: |
|
2505 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2506 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2507 | ||
2508 | new_values = json.loads(raw_json) |
|
2509 | ||
2510 | if 'name' not in new_values['data'].keys() or \ |
|
2511 | not isinstance(new_values['data']['name'], str) or \ |
|
2512 | len(str.strip(new_values['data']['name'])) == 0: |
|
2513 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2514 | description='API.INVALID_MICROGRID_GRID_NAME') |
|
2515 | name = str.strip(new_values['data']['name']) |
|
2516 | ||
2517 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
2518 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
2519 | new_values['data']['power_point_id'] <= 0: |
|
2520 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2521 | description='API.INVALID_POWER_POINT_ID') |
|
2522 | power_point_id = new_values['data']['power_point_id'] |
|
2523 | ||
2524 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
2525 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
2526 | new_values['data']['buy_meter_id'] <= 0: |
|
2527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2528 | description='API.INVALID_BUY_METER_ID') |
|
2529 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
2530 | ||
2531 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
2532 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
2533 | new_values['data']['sell_meter_id'] <= 0: |
|
2534 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2535 | description='API.INVALID_METER_ID') |
|
2536 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
2537 | ||
2538 | if 'capacity' not in new_values['data'].keys() or \ |
|
2539 | not (isinstance(new_values['data']['capacity'], float) or |
|
2540 | isinstance(new_values['data']['capacity'], int)): |
|
2541 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2542 | description='API.INVALID_CAPACITY') |
|
2543 | capacity = float(new_values['data']['capacity']) |
|
2544 | ||
2545 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2546 | cursor = cnx.cursor() |
|
2547 | ||
2548 | cursor.execute(" SELECT name " |
|
2549 | " FROM tbl_microgrids " |
|
2550 | " WHERE id = %s ", (id_,)) |
|
2551 | if cursor.fetchone() is None: |
|
2552 | cursor.close() |
|
2553 | cnx.close() |
|
2554 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2555 | description='API.MICROGRID_NOT_FOUND') |
|
2556 | ||
2557 | cursor.execute(" SELECT name " |
|
2558 | " FROM tbl_microgrids_grids " |
|
2559 | " WHERE id = %s ", (gid,)) |
|
2560 | if cursor.fetchone() is None: |
|
2561 | cursor.close() |
|
2562 | cnx.close() |
|
2563 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2564 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2565 | ||
2566 | cursor.execute(" SELECT name " |
|
2567 | " FROM tbl_microgrids_grids " |
|
2568 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
2569 | (id_, name, gid)) |
|
2570 | if cursor.fetchone() is not None: |
|
2571 | cursor.close() |
|
2572 | cnx.close() |
|
2573 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2574 | description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
|
2575 | ||
2576 | cursor.execute(" SELECT name " |
|
2577 | " FROM tbl_points " |
|
2578 | " WHERE id = %s ", |
|
2579 | (power_point_id,)) |
|
2580 | if cursor.fetchone() is None: |
|
2581 | cursor.close() |
|
2582 | cnx.close() |
|
2583 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2584 | description='API.POWER_POINT_NOT_FOUND') |
|
2585 | ||
2586 | cursor.execute(" SELECT name " |
|
2587 | " FROM tbl_meters " |
|
2588 | " WHERE id = %s ", |
|
2589 | (buy_meter_id,)) |
|
2590 | if cursor.fetchone() is None: |
|
2591 | cursor.close() |
|
2592 | cnx.close() |
|
2593 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2594 | description='API.BUY_METER_NOT_FOUND') |
|
2595 | ||
2596 | cursor.execute(" SELECT name " |
|
2597 | " FROM tbl_meters " |
|
2598 | " WHERE id = %s ", |
|
2599 | (sell_meter_id,)) |
|
2600 | if cursor.fetchone() is None: |
|
2601 | cursor.close() |
|
2602 | cnx.close() |
|
2603 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2604 | description='API.SELL_METER_NOT_FOUND') |
|
2605 | ||
2606 | update_row = (" UPDATE tbl_microgrids_grids " |
|
2607 | " SET name = %s, microgrid_id = %s, " |
|
2608 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
2609 | " WHERE id = %s ") |
|
2610 | cursor.execute(update_row, (name, |
|
2611 | id_, |
|
2612 | power_point_id, |
|
2613 | buy_meter_id, |
|
2614 | sell_meter_id, |
|
2615 | capacity, |
|
2616 | gid)) |
|
2617 | cnx.commit() |
|
2618 | ||
2619 | cursor.close() |
|
2620 | cnx.close() |
|
2621 | ||
2622 | resp.status = falcon.HTTP_200 |
|
2623 | ||
2624 | ||
2625 | class MicrogridHeatpumpCollection: |