| @@ 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: |
|
| @@ 2835-3100 (lines=266) @@ | ||
| 2832 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
|
| 2833 | ||
| 2834 | ||
| 2835 | class EnergyStorageContainerGridItem: |
|
| 2836 | def __init__(self): |
|
| 2837 | """Initializes Class""" |
|
| 2838 | pass |
|
| 2839 | ||
| 2840 | @staticmethod |
|
| 2841 | def on_options(req, resp, id_, gid): |
|
| 2842 | resp.status = falcon.HTTP_200 |
|
| 2843 | ||
| 2844 | @staticmethod |
|
| 2845 | def on_get(req, resp, id_, gid): |
|
| 2846 | access_control(req) |
|
| 2847 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2848 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2849 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2850 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2851 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2852 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2853 | ||
| 2854 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2855 | cursor = cnx.cursor() |
|
| 2856 | ||
| 2857 | cursor.execute(" SELECT name " |
|
| 2858 | " FROM tbl_energy_storage_containers " |
|
| 2859 | " WHERE id = %s ", (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.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 2865 | ||
| 2866 | query = (" SELECT id, name, uuid " |
|
| 2867 | " FROM tbl_energy_storage_containers ") |
|
| 2868 | cursor.execute(query) |
|
| 2869 | rows_energystoragecontainers = cursor.fetchall() |
|
| 2870 | ||
| 2871 | energy_storage_container_dict = dict() |
|
| 2872 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 2873 | for row in rows_energystoragecontainers: |
|
| 2874 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 2875 | "name": row[1], |
|
| 2876 | "uuid": row[2]} |
|
| 2877 | # query meter dict |
|
| 2878 | query = (" SELECT id, name, uuid " |
|
| 2879 | " FROM tbl_meters ") |
|
| 2880 | cursor.execute(query) |
|
| 2881 | rows_meters = cursor.fetchall() |
|
| 2882 | ||
| 2883 | meter_dict = dict() |
|
| 2884 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 2885 | for row in rows_meters: |
|
| 2886 | meter_dict[row[0]] = {"id": row[0], |
|
| 2887 | "name": row[1], |
|
| 2888 | "uuid": row[2]} |
|
| 2889 | # query point dict |
|
| 2890 | query = (" SELECT id, name " |
|
| 2891 | " FROM tbl_points ") |
|
| 2892 | cursor.execute(query) |
|
| 2893 | rows_points = cursor.fetchall() |
|
| 2894 | ||
| 2895 | point_dict = dict() |
|
| 2896 | if rows_points is not None and len(rows_points) > 0: |
|
| 2897 | for row in rows_points: |
|
| 2898 | point_dict[row[0]] = {"id": row[0], |
|
| 2899 | "name": row[1]} |
|
| 2900 | ||
| 2901 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
|
| 2902 | " buy_meter_id, sell_meter_id, capacity " |
|
| 2903 | " FROM tbl_energy_storage_containers_grids " |
|
| 2904 | " WHERE id = %s ") |
|
| 2905 | cursor.execute(query, (gid,)) |
|
| 2906 | row = cursor.fetchone() |
|
| 2907 | cursor.close() |
|
| 2908 | cnx.close() |
|
| 2909 | ||
| 2910 | if row is None: |
|
| 2911 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2912 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2913 | else: |
|
| 2914 | meta_result = {"id": row[0], |
|
| 2915 | "name": row[1], |
|
| 2916 | "uuid": row[2], |
|
| 2917 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
| 2918 | "power_point": point_dict.get(row[4]), |
|
| 2919 | "buy_meter": meter_dict.get(row[5]), |
|
| 2920 | "sell_meter": meter_dict.get(row[6]), |
|
| 2921 | "capacity": row[7] |
|
| 2922 | } |
|
| 2923 | ||
| 2924 | resp.text = json.dumps(meta_result) |
|
| 2925 | ||
| 2926 | @staticmethod |
|
| 2927 | @user_logger |
|
| 2928 | def on_delete(req, resp, id_, gid): |
|
| 2929 | admin_control(req) |
|
| 2930 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2931 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2932 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2933 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2934 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2935 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2936 | ||
| 2937 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2938 | cursor = cnx.cursor() |
|
| 2939 | ||
| 2940 | cursor.execute(" SELECT name " |
|
| 2941 | " FROM tbl_energy_storage_containers " |
|
| 2942 | " WHERE id = %s ", (id_,)) |
|
| 2943 | if cursor.fetchone() is None: |
|
| 2944 | cursor.close() |
|
| 2945 | cnx.close() |
|
| 2946 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2947 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 2948 | ||
| 2949 | cursor.execute(" SELECT name " |
|
| 2950 | " FROM tbl_energy_storage_containers_grids " |
|
| 2951 | " WHERE id = %s ", (gid,)) |
|
| 2952 | if cursor.fetchone() is None: |
|
| 2953 | cursor.close() |
|
| 2954 | cnx.close() |
|
| 2955 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2956 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2957 | ||
| 2958 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
|
| 2959 | " WHERE id = %s ", (gid,)) |
|
| 2960 | cnx.commit() |
|
| 2961 | ||
| 2962 | cursor.close() |
|
| 2963 | cnx.close() |
|
| 2964 | ||
| 2965 | resp.status = falcon.HTTP_204 |
|
| 2966 | ||
| 2967 | @staticmethod |
|
| 2968 | @user_logger |
|
| 2969 | def on_put(req, resp, id_, gid): |
|
| 2970 | """Handles PUT requests""" |
|
| 2971 | admin_control(req) |
|
| 2972 | try: |
|
| 2973 | raw_json = req.stream.read().decode('utf-8') |
|
| 2974 | except Exception as ex: |
|
| 2975 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2976 | title='API.BAD_REQUEST', |
|
| 2977 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 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 | ||
| 2982 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2983 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2984 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2985 | ||
| 2986 | new_values = json.loads(raw_json) |
|
| 2987 | ||
| 2988 | if 'name' not in new_values['data'].keys() or \ |
|
| 2989 | not isinstance(new_values['data']['name'], str) or \ |
|
| 2990 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 2991 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2992 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
|
| 2993 | name = str.strip(new_values['data']['name']) |
|
| 2994 | ||
| 2995 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 2996 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 2997 | new_values['data']['power_point_id'] <= 0: |
|
| 2998 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2999 | description='API.INVALID_POWER_POINT_ID') |
|
| 3000 | power_point_id = new_values['data']['power_point_id'] |
|
| 3001 | ||
| 3002 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
| 3003 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
| 3004 | new_values['data']['buy_meter_id'] <= 0: |
|
| 3005 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3006 | description='API.INVALID_BUY_METER_ID') |
|
| 3007 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
| 3008 | ||
| 3009 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
| 3010 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
| 3011 | new_values['data']['sell_meter_id'] <= 0: |
|
| 3012 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3013 | description='API.INVALID_METER_ID') |
|
| 3014 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
| 3015 | ||
| 3016 | if 'capacity' not in new_values['data'].keys() or \ |
|
| 3017 | not (isinstance(new_values['data']['capacity'], float) or |
|
| 3018 | isinstance(new_values['data']['capacity'], int)): |
|
| 3019 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3020 | description='API.INVALID_CAPACITY') |
|
| 3021 | capacity = Decimal(new_values['data']['capacity']) |
|
| 3022 | ||
| 3023 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3024 | cursor = cnx.cursor() |
|
| 3025 | ||
| 3026 | cursor.execute(" SELECT name " |
|
| 3027 | " FROM tbl_energy_storage_containers " |
|
| 3028 | " WHERE id = %s ", (id_,)) |
|
| 3029 | if cursor.fetchone() is None: |
|
| 3030 | cursor.close() |
|
| 3031 | cnx.close() |
|
| 3032 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3033 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 3034 | ||
| 3035 | cursor.execute(" SELECT name " |
|
| 3036 | " FROM tbl_energy_storage_containers_grids " |
|
| 3037 | " WHERE id = %s ", (gid,)) |
|
| 3038 | if cursor.fetchone() is None: |
|
| 3039 | cursor.close() |
|
| 3040 | cnx.close() |
|
| 3041 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3042 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 3043 | ||
| 3044 | cursor.execute(" SELECT name " |
|
| 3045 | " FROM tbl_energy_storage_containers_grids " |
|
| 3046 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
| 3047 | (id_, name, gid)) |
|
| 3048 | if cursor.fetchone() is not None: |
|
| 3049 | cursor.close() |
|
| 3050 | cnx.close() |
|
| 3051 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3052 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
|
| 3053 | ||
| 3054 | cursor.execute(" SELECT name " |
|
| 3055 | " FROM tbl_points " |
|
| 3056 | " WHERE id = %s ", |
|
| 3057 | (power_point_id,)) |
|
| 3058 | if cursor.fetchone() is None: |
|
| 3059 | cursor.close() |
|
| 3060 | cnx.close() |
|
| 3061 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3062 | description='API.POWER_POINT_NOT_FOUND') |
|
| 3063 | ||
| 3064 | cursor.execute(" SELECT name " |
|
| 3065 | " FROM tbl_meters " |
|
| 3066 | " WHERE id = %s ", |
|
| 3067 | (buy_meter_id,)) |
|
| 3068 | if cursor.fetchone() is None: |
|
| 3069 | cursor.close() |
|
| 3070 | cnx.close() |
|
| 3071 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3072 | description='API.BUY_METER_NOT_FOUND') |
|
| 3073 | ||
| 3074 | cursor.execute(" SELECT name " |
|
| 3075 | " FROM tbl_meters " |
|
| 3076 | " WHERE id = %s ", |
|
| 3077 | (sell_meter_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.SELL_METER_NOT_FOUND') |
|
| 3083 | ||
| 3084 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
|
| 3085 | " SET name = %s, energy_storage_container_id = %s, " |
|
| 3086 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
| 3087 | " WHERE id = %s ") |
|
| 3088 | cursor.execute(update_row, (name, |
|
| 3089 | id_, |
|
| 3090 | power_point_id, |
|
| 3091 | buy_meter_id, |
|
| 3092 | sell_meter_id, |
|
| 3093 | capacity, |
|
| 3094 | gid)) |
|
| 3095 | cnx.commit() |
|
| 3096 | ||
| 3097 | cursor.close() |
|
| 3098 | cnx.close() |
|
| 3099 | ||
| 3100 | resp.status = falcon.HTTP_200 |
|
| 3101 | ||
| 3102 | ||
| 3103 | class EnergyStorageContainerGridPointCollection: |
|