@@ 2398-2665 (lines=268) @@ | ||
2395 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(new_id) |
|
2396 | ||
2397 | ||
2398 | class MicrogridGridItem: |
|
2399 | def __init__(self): |
|
2400 | """Initializes MicrogridGridItem""" |
|
2401 | pass |
|
2402 | ||
2403 | @staticmethod |
|
2404 | def on_options(req, resp, id_, gid): |
|
2405 | _ = req |
|
2406 | resp.status = falcon.HTTP_200 |
|
2407 | _ = id_ |
|
2408 | ||
2409 | @staticmethod |
|
2410 | def on_get(req, resp, id_, gid): |
|
2411 | access_control(req) |
|
2412 | if not id_.isdigit() or int(id_) <= 0: |
|
2413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2414 | description='API.INVALID_MICROGRID_ID') |
|
2415 | if not gid.isdigit() or int(gid) <= 0: |
|
2416 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2417 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2418 | ||
2419 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2420 | cursor = cnx.cursor() |
|
2421 | ||
2422 | cursor.execute(" SELECT name " |
|
2423 | " FROM tbl_microgrids " |
|
2424 | " WHERE id = %s ", (id_,)) |
|
2425 | if cursor.fetchone() is None: |
|
2426 | cursor.close() |
|
2427 | cnx.close() |
|
2428 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2429 | description='API.MICROGRID_NOT_FOUND') |
|
2430 | ||
2431 | # query microgrid dict |
|
2432 | query = (" SELECT id, name, uuid " |
|
2433 | " FROM tbl_microgrids ") |
|
2434 | cursor.execute(query) |
|
2435 | rows_microgrids = cursor.fetchall() |
|
2436 | ||
2437 | microgrid_dict = dict() |
|
2438 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
2439 | for row in rows_microgrids: |
|
2440 | microgrid_dict[row[0]] = {"id": row[0], |
|
2441 | "name": row[1], |
|
2442 | "uuid": row[2]} |
|
2443 | # query meter dict |
|
2444 | query = (" SELECT id, name, uuid " |
|
2445 | " FROM tbl_meters ") |
|
2446 | cursor.execute(query) |
|
2447 | rows_meters = cursor.fetchall() |
|
2448 | ||
2449 | meter_dict = dict() |
|
2450 | if rows_meters is not None and len(rows_meters) > 0: |
|
2451 | for row in rows_meters: |
|
2452 | meter_dict[row[0]] = {"id": row[0], |
|
2453 | "name": row[1], |
|
2454 | "uuid": row[2]} |
|
2455 | # query point dict |
|
2456 | query = (" SELECT id, name " |
|
2457 | " FROM tbl_points ") |
|
2458 | cursor.execute(query) |
|
2459 | rows_points = cursor.fetchall() |
|
2460 | ||
2461 | point_dict = dict() |
|
2462 | if rows_points is not None and len(rows_points) > 0: |
|
2463 | for row in rows_points: |
|
2464 | point_dict[row[0]] = {"id": row[0], |
|
2465 | "name": row[1]} |
|
2466 | ||
2467 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity " |
|
2468 | " FROM tbl_microgrids_grids " |
|
2469 | " WHERE id = %s ") |
|
2470 | cursor.execute(query, (gid,)) |
|
2471 | row = cursor.fetchone() |
|
2472 | cursor.close() |
|
2473 | cnx.close() |
|
2474 | ||
2475 | if row is None: |
|
2476 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2477 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2478 | else: |
|
2479 | meta_result = {"id": row[0], |
|
2480 | "name": row[1], |
|
2481 | "uuid": row[2], |
|
2482 | "microgrid": microgrid_dict.get(row[3]), |
|
2483 | "power_point": point_dict.get(row[4]), |
|
2484 | "buy_meter": meter_dict.get(row[5]), |
|
2485 | "sell_meter": meter_dict.get(row[6]), |
|
2486 | "capacity": row[7]} |
|
2487 | ||
2488 | resp.text = json.dumps(meta_result) |
|
2489 | ||
2490 | @staticmethod |
|
2491 | @user_logger |
|
2492 | def on_delete(req, resp, id_, gid): |
|
2493 | admin_control(req) |
|
2494 | if not id_.isdigit() or int(id_) <= 0: |
|
2495 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2496 | description='API.INVALID_MICROGRID_ID') |
|
2497 | if not gid.isdigit() or int(gid) <= 0: |
|
2498 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2499 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2500 | ||
2501 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2502 | cursor = cnx.cursor() |
|
2503 | ||
2504 | cursor.execute(" SELECT name " |
|
2505 | " FROM tbl_microgrids " |
|
2506 | " WHERE id = %s ", (id_,)) |
|
2507 | if cursor.fetchone() is None: |
|
2508 | cursor.close() |
|
2509 | cnx.close() |
|
2510 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2511 | description='API.MICROGRID_NOT_FOUND') |
|
2512 | ||
2513 | cursor.execute(" SELECT name " |
|
2514 | " FROM tbl_microgrids_grids " |
|
2515 | " WHERE id = %s ", (gid,)) |
|
2516 | if cursor.fetchone() is None: |
|
2517 | cursor.close() |
|
2518 | cnx.close() |
|
2519 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2520 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2521 | ||
2522 | cursor.execute(" DELETE FROM tbl_microgrids_grids " |
|
2523 | " WHERE id = %s ", (gid,)) |
|
2524 | cnx.commit() |
|
2525 | ||
2526 | cursor.close() |
|
2527 | cnx.close() |
|
2528 | ||
2529 | resp.status = falcon.HTTP_204 |
|
2530 | ||
2531 | @staticmethod |
|
2532 | @user_logger |
|
2533 | def on_put(req, resp, id_, gid): |
|
2534 | """Handles PUT requests""" |
|
2535 | admin_control(req) |
|
2536 | try: |
|
2537 | raw_json = req.stream.read().decode('utf-8') |
|
2538 | except Exception as ex: |
|
2539 | print(str(ex)) |
|
2540 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2541 | title='API.BAD_REQUEST', |
|
2542 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2543 | if not id_.isdigit() or int(id_) <= 0: |
|
2544 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2545 | description='API.INVALID_MICROGRID_ID') |
|
2546 | ||
2547 | if not gid.isdigit() or int(gid) <= 0: |
|
2548 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2549 | description='API.INVALID_MICROGRID_GRID_ID') |
|
2550 | ||
2551 | new_values = json.loads(raw_json) |
|
2552 | ||
2553 | if 'name' not in new_values['data'].keys() or \ |
|
2554 | not isinstance(new_values['data']['name'], str) or \ |
|
2555 | len(str.strip(new_values['data']['name'])) == 0: |
|
2556 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2557 | description='API.INVALID_MICROGRID_GRID_NAME') |
|
2558 | name = str.strip(new_values['data']['name']) |
|
2559 | ||
2560 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
2561 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
2562 | new_values['data']['power_point_id'] <= 0: |
|
2563 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2564 | description='API.INVALID_POWER_POINT_ID') |
|
2565 | power_point_id = new_values['data']['power_point_id'] |
|
2566 | ||
2567 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
2568 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
2569 | new_values['data']['buy_meter_id'] <= 0: |
|
2570 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2571 | description='API.INVALID_BUY_METER_ID') |
|
2572 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
2573 | ||
2574 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
2575 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
2576 | new_values['data']['sell_meter_id'] <= 0: |
|
2577 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2578 | description='API.INVALID_METER_ID') |
|
2579 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
2580 | ||
2581 | if 'capacity' not in new_values['data'].keys() or \ |
|
2582 | not (isinstance(new_values['data']['capacity'], float) or |
|
2583 | isinstance(new_values['data']['capacity'], int)): |
|
2584 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2585 | description='API.INVALID_CAPACITY') |
|
2586 | capacity = float(new_values['data']['capacity']) |
|
2587 | ||
2588 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2589 | cursor = cnx.cursor() |
|
2590 | ||
2591 | cursor.execute(" SELECT name " |
|
2592 | " FROM tbl_microgrids " |
|
2593 | " WHERE id = %s ", (id_,)) |
|
2594 | if cursor.fetchone() is None: |
|
2595 | cursor.close() |
|
2596 | cnx.close() |
|
2597 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2598 | description='API.MICROGRID_NOT_FOUND') |
|
2599 | ||
2600 | cursor.execute(" SELECT name " |
|
2601 | " FROM tbl_microgrids_grids " |
|
2602 | " WHERE id = %s ", (gid,)) |
|
2603 | if cursor.fetchone() is None: |
|
2604 | cursor.close() |
|
2605 | cnx.close() |
|
2606 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2607 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
2608 | ||
2609 | cursor.execute(" SELECT name " |
|
2610 | " FROM tbl_microgrids_grids " |
|
2611 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
2612 | (id_, name, gid)) |
|
2613 | if cursor.fetchone() is not None: |
|
2614 | cursor.close() |
|
2615 | cnx.close() |
|
2616 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2617 | description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
|
2618 | ||
2619 | cursor.execute(" SELECT name " |
|
2620 | " FROM tbl_points " |
|
2621 | " WHERE id = %s ", |
|
2622 | (power_point_id,)) |
|
2623 | if cursor.fetchone() is None: |
|
2624 | cursor.close() |
|
2625 | cnx.close() |
|
2626 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2627 | description='API.POWER_POINT_NOT_FOUND') |
|
2628 | ||
2629 | cursor.execute(" SELECT name " |
|
2630 | " FROM tbl_meters " |
|
2631 | " WHERE id = %s ", |
|
2632 | (buy_meter_id,)) |
|
2633 | if cursor.fetchone() is None: |
|
2634 | cursor.close() |
|
2635 | cnx.close() |
|
2636 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2637 | description='API.BUY_METER_NOT_FOUND') |
|
2638 | ||
2639 | cursor.execute(" SELECT name " |
|
2640 | " FROM tbl_meters " |
|
2641 | " WHERE id = %s ", |
|
2642 | (sell_meter_id,)) |
|
2643 | if cursor.fetchone() is None: |
|
2644 | cursor.close() |
|
2645 | cnx.close() |
|
2646 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2647 | description='API.SELL_METER_NOT_FOUND') |
|
2648 | ||
2649 | update_row = (" UPDATE tbl_microgrids_grids " |
|
2650 | " SET name = %s, microgrid_id = %s, " |
|
2651 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
2652 | " WHERE id = %s ") |
|
2653 | cursor.execute(update_row, (name, |
|
2654 | id_, |
|
2655 | power_point_id, |
|
2656 | buy_meter_id, |
|
2657 | sell_meter_id, |
|
2658 | capacity, |
|
2659 | gid)) |
|
2660 | cnx.commit() |
|
2661 | ||
2662 | cursor.close() |
|
2663 | cnx.close() |
|
2664 | ||
2665 | resp.status = falcon.HTTP_200 |
|
2666 | ||
2667 | ||
2668 | class MicrogridHeatpumpCollection: |
@@ 2881-3149 (lines=269) @@ | ||
2878 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
|
2879 | ||
2880 | ||
2881 | class EnergyStorageContainerGridItem: |
|
2882 | def __init__(self): |
|
2883 | """Initializes Class""" |
|
2884 | pass |
|
2885 | ||
2886 | @staticmethod |
|
2887 | def on_options(req, resp, id_, gid): |
|
2888 | _ = req |
|
2889 | resp.status = falcon.HTTP_200 |
|
2890 | _ = id_ |
|
2891 | ||
2892 | @staticmethod |
|
2893 | def on_get(req, resp, id_, gid): |
|
2894 | access_control(req) |
|
2895 | if not id_.isdigit() or int(id_) <= 0: |
|
2896 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2897 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
2898 | if not gid.isdigit() or int(gid) <= 0: |
|
2899 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2900 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
2901 | ||
2902 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2903 | cursor = cnx.cursor() |
|
2904 | ||
2905 | cursor.execute(" SELECT name " |
|
2906 | " FROM tbl_energy_storage_containers " |
|
2907 | " WHERE id = %s ", (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.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
2913 | ||
2914 | query = (" SELECT id, name, uuid " |
|
2915 | " FROM tbl_energy_storage_containers ") |
|
2916 | cursor.execute(query) |
|
2917 | rows_energystoragecontainers = cursor.fetchall() |
|
2918 | ||
2919 | energy_storage_container_dict = dict() |
|
2920 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
2921 | for row in rows_energystoragecontainers: |
|
2922 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
2923 | "name": row[1], |
|
2924 | "uuid": row[2]} |
|
2925 | # query meter dict |
|
2926 | query = (" SELECT id, name, uuid " |
|
2927 | " FROM tbl_meters ") |
|
2928 | cursor.execute(query) |
|
2929 | rows_meters = cursor.fetchall() |
|
2930 | ||
2931 | meter_dict = dict() |
|
2932 | if rows_meters is not None and len(rows_meters) > 0: |
|
2933 | for row in rows_meters: |
|
2934 | meter_dict[row[0]] = {"id": row[0], |
|
2935 | "name": row[1], |
|
2936 | "uuid": row[2]} |
|
2937 | # query point dict |
|
2938 | query = (" SELECT id, name " |
|
2939 | " FROM tbl_points ") |
|
2940 | cursor.execute(query) |
|
2941 | rows_points = cursor.fetchall() |
|
2942 | ||
2943 | point_dict = dict() |
|
2944 | if rows_points is not None and len(rows_points) > 0: |
|
2945 | for row in rows_points: |
|
2946 | point_dict[row[0]] = {"id": row[0], |
|
2947 | "name": row[1]} |
|
2948 | ||
2949 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
|
2950 | " buy_meter_id, sell_meter_id, capacity " |
|
2951 | " FROM tbl_energy_storage_containers_grids " |
|
2952 | " WHERE id = %s ") |
|
2953 | cursor.execute(query, (gid,)) |
|
2954 | row = cursor.fetchone() |
|
2955 | cursor.close() |
|
2956 | cnx.close() |
|
2957 | ||
2958 | if row is None: |
|
2959 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2960 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
2961 | else: |
|
2962 | meta_result = {"id": row[0], |
|
2963 | "name": row[1], |
|
2964 | "uuid": row[2], |
|
2965 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
2966 | "power_point": point_dict.get(row[4]), |
|
2967 | "buy_meter": meter_dict.get(row[5]), |
|
2968 | "sell_meter": meter_dict.get(row[6]), |
|
2969 | "capacity": row[7] |
|
2970 | } |
|
2971 | ||
2972 | resp.text = json.dumps(meta_result) |
|
2973 | ||
2974 | @staticmethod |
|
2975 | @user_logger |
|
2976 | def on_delete(req, resp, id_, gid): |
|
2977 | admin_control(req) |
|
2978 | if not id_.isdigit() or int(id_) <= 0: |
|
2979 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2980 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
2981 | if not gid.isdigit() or int(gid) <= 0: |
|
2982 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2983 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
2984 | ||
2985 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2986 | cursor = cnx.cursor() |
|
2987 | ||
2988 | cursor.execute(" SELECT name " |
|
2989 | " FROM tbl_energy_storage_containers " |
|
2990 | " WHERE id = %s ", (id_,)) |
|
2991 | if cursor.fetchone() is None: |
|
2992 | cursor.close() |
|
2993 | cnx.close() |
|
2994 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2995 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
2996 | ||
2997 | cursor.execute(" SELECT name " |
|
2998 | " FROM tbl_energy_storage_containers_grids " |
|
2999 | " WHERE id = %s ", (gid,)) |
|
3000 | if cursor.fetchone() is None: |
|
3001 | cursor.close() |
|
3002 | cnx.close() |
|
3003 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3004 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
3005 | ||
3006 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
|
3007 | " WHERE id = %s ", (gid,)) |
|
3008 | cnx.commit() |
|
3009 | ||
3010 | cursor.close() |
|
3011 | cnx.close() |
|
3012 | ||
3013 | resp.status = falcon.HTTP_204 |
|
3014 | ||
3015 | @staticmethod |
|
3016 | @user_logger |
|
3017 | def on_put(req, resp, id_, gid): |
|
3018 | """Handles PUT requests""" |
|
3019 | admin_control(req) |
|
3020 | try: |
|
3021 | raw_json = req.stream.read().decode('utf-8') |
|
3022 | except Exception as ex: |
|
3023 | print(str(ex)) |
|
3024 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
3025 | title='API.BAD_REQUEST', |
|
3026 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
3027 | if not id_.isdigit() or int(id_) <= 0: |
|
3028 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3029 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
3030 | ||
3031 | if not gid.isdigit() or int(gid) <= 0: |
|
3032 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3033 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
3034 | ||
3035 | new_values = json.loads(raw_json) |
|
3036 | ||
3037 | if 'name' not in new_values['data'].keys() or \ |
|
3038 | not isinstance(new_values['data']['name'], str) or \ |
|
3039 | len(str.strip(new_values['data']['name'])) == 0: |
|
3040 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3041 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
|
3042 | name = str.strip(new_values['data']['name']) |
|
3043 | ||
3044 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
3045 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
3046 | new_values['data']['power_point_id'] <= 0: |
|
3047 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3048 | description='API.INVALID_POWER_POINT_ID') |
|
3049 | power_point_id = new_values['data']['power_point_id'] |
|
3050 | ||
3051 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
3052 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
3053 | new_values['data']['buy_meter_id'] <= 0: |
|
3054 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3055 | description='API.INVALID_BUY_METER_ID') |
|
3056 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
3057 | ||
3058 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
3059 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
3060 | new_values['data']['sell_meter_id'] <= 0: |
|
3061 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3062 | description='API.INVALID_METER_ID') |
|
3063 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
3064 | ||
3065 | if 'capacity' not in new_values['data'].keys() or \ |
|
3066 | not (isinstance(new_values['data']['capacity'], float) or |
|
3067 | isinstance(new_values['data']['capacity'], int)): |
|
3068 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3069 | description='API.INVALID_CAPACITY') |
|
3070 | capacity = Decimal(new_values['data']['capacity']) |
|
3071 | ||
3072 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
3073 | cursor = cnx.cursor() |
|
3074 | ||
3075 | cursor.execute(" SELECT name " |
|
3076 | " FROM tbl_energy_storage_containers " |
|
3077 | " WHERE id = %s ", (id_,)) |
|
3078 | if cursor.fetchone() is None: |
|
3079 | cursor.close() |
|
3080 | cnx.close() |
|
3081 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3082 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
3083 | ||
3084 | cursor.execute(" SELECT name " |
|
3085 | " FROM tbl_energy_storage_containers_grids " |
|
3086 | " WHERE id = %s ", (gid,)) |
|
3087 | if cursor.fetchone() is None: |
|
3088 | cursor.close() |
|
3089 | cnx.close() |
|
3090 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3091 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
3092 | ||
3093 | cursor.execute(" SELECT name " |
|
3094 | " FROM tbl_energy_storage_containers_grids " |
|
3095 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
3096 | (id_, name, gid)) |
|
3097 | if cursor.fetchone() is not None: |
|
3098 | cursor.close() |
|
3099 | cnx.close() |
|
3100 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
3101 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
|
3102 | ||
3103 | cursor.execute(" SELECT name " |
|
3104 | " FROM tbl_points " |
|
3105 | " WHERE id = %s ", |
|
3106 | (power_point_id,)) |
|
3107 | if cursor.fetchone() is None: |
|
3108 | cursor.close() |
|
3109 | cnx.close() |
|
3110 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3111 | description='API.POWER_POINT_NOT_FOUND') |
|
3112 | ||
3113 | cursor.execute(" SELECT name " |
|
3114 | " FROM tbl_meters " |
|
3115 | " WHERE id = %s ", |
|
3116 | (buy_meter_id,)) |
|
3117 | if cursor.fetchone() is None: |
|
3118 | cursor.close() |
|
3119 | cnx.close() |
|
3120 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3121 | description='API.BUY_METER_NOT_FOUND') |
|
3122 | ||
3123 | cursor.execute(" SELECT name " |
|
3124 | " FROM tbl_meters " |
|
3125 | " WHERE id = %s ", |
|
3126 | (sell_meter_id,)) |
|
3127 | if cursor.fetchone() is None: |
|
3128 | cursor.close() |
|
3129 | cnx.close() |
|
3130 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
3131 | description='API.SELL_METER_NOT_FOUND') |
|
3132 | ||
3133 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
|
3134 | " SET name = %s, energy_storage_container_id = %s, " |
|
3135 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
3136 | " WHERE id = %s ") |
|
3137 | cursor.execute(update_row, (name, |
|
3138 | id_, |
|
3139 | power_point_id, |
|
3140 | buy_meter_id, |
|
3141 | sell_meter_id, |
|
3142 | capacity, |
|
3143 | gid)) |
|
3144 | cnx.commit() |
|
3145 | ||
3146 | cursor.close() |
|
3147 | cnx.close() |
|
3148 | ||
3149 | resp.status = falcon.HTTP_200 |
|
3150 | ||
3151 | ||
3152 | class EnergyStorageContainerGridPointCollection: |