| @@ 2917-3185 (lines=269) @@ | ||
| 2914 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
|
| 2915 | ||
| 2916 | ||
| 2917 | class EnergyStorageContainerGridItem: |
|
| 2918 | def __init__(self): |
|
| 2919 | pass |
|
| 2920 | ||
| 2921 | @staticmethod |
|
| 2922 | def on_options(req, resp, id_, gid): |
|
| 2923 | _ = req |
|
| 2924 | resp.status = falcon.HTTP_200 |
|
| 2925 | _ = id_ |
|
| 2926 | ||
| 2927 | @staticmethod |
|
| 2928 | def on_get(req, resp, id_, gid): |
|
| 2929 | access_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 | query = (" SELECT id, name, uuid " |
|
| 2950 | " FROM tbl_energy_storage_containers ") |
|
| 2951 | cursor.execute(query) |
|
| 2952 | rows_energystoragecontainers = cursor.fetchall() |
|
| 2953 | ||
| 2954 | energy_storage_container_dict = dict() |
|
| 2955 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 2956 | for row in rows_energystoragecontainers: |
|
| 2957 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 2958 | "name": row[1], |
|
| 2959 | "uuid": row[2]} |
|
| 2960 | # query meter dict |
|
| 2961 | query = (" SELECT id, name, uuid " |
|
| 2962 | " FROM tbl_meters ") |
|
| 2963 | cursor.execute(query) |
|
| 2964 | rows_meters = cursor.fetchall() |
|
| 2965 | ||
| 2966 | meter_dict = dict() |
|
| 2967 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 2968 | for row in rows_meters: |
|
| 2969 | meter_dict[row[0]] = {"id": row[0], |
|
| 2970 | "name": row[1], |
|
| 2971 | "uuid": row[2]} |
|
| 2972 | # query point dict |
|
| 2973 | query = (" SELECT id, name " |
|
| 2974 | " FROM tbl_points ") |
|
| 2975 | cursor.execute(query) |
|
| 2976 | rows_points = cursor.fetchall() |
|
| 2977 | ||
| 2978 | point_dict = dict() |
|
| 2979 | if rows_points is not None and len(rows_points) > 0: |
|
| 2980 | for row in rows_points: |
|
| 2981 | point_dict[row[0]] = {"id": row[0], |
|
| 2982 | "name": row[1]} |
|
| 2983 | ||
| 2984 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
|
| 2985 | " buy_meter_id, sell_meter_id, capacity " |
|
| 2986 | " FROM tbl_energy_storage_containers_grids " |
|
| 2987 | " WHERE id = %s ") |
|
| 2988 | cursor.execute(query, (gid,)) |
|
| 2989 | row = cursor.fetchone() |
|
| 2990 | cursor.close() |
|
| 2991 | cnx.close() |
|
| 2992 | ||
| 2993 | if row is None: |
|
| 2994 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2995 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2996 | else: |
|
| 2997 | meta_result = {"id": row[0], |
|
| 2998 | "name": row[1], |
|
| 2999 | "uuid": row[2], |
|
| 3000 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
| 3001 | "power_point": point_dict.get(row[4]), |
|
| 3002 | "buy_meter": meter_dict.get(row[5]), |
|
| 3003 | "sell_meter": meter_dict.get(row[6]), |
|
| 3004 | "capacity": row[7] |
|
| 3005 | } |
|
| 3006 | ||
| 3007 | resp.text = json.dumps(meta_result) |
|
| 3008 | ||
| 3009 | @staticmethod |
|
| 3010 | @user_logger |
|
| 3011 | def on_delete(req, resp, id_, gid): |
|
| 3012 | admin_control(req) |
|
| 3013 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3014 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3015 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3016 | if not gid.isdigit() or int(gid) <= 0: |
|
| 3017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3018 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 3019 | ||
| 3020 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3021 | cursor = cnx.cursor() |
|
| 3022 | ||
| 3023 | cursor.execute(" SELECT name " |
|
| 3024 | " FROM tbl_energy_storage_containers " |
|
| 3025 | " WHERE id = %s ", (id_,)) |
|
| 3026 | if cursor.fetchone() is None: |
|
| 3027 | cursor.close() |
|
| 3028 | cnx.close() |
|
| 3029 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3030 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 3031 | ||
| 3032 | cursor.execute(" SELECT name " |
|
| 3033 | " FROM tbl_energy_storage_containers_grids " |
|
| 3034 | " WHERE id = %s ", (gid,)) |
|
| 3035 | if cursor.fetchone() is None: |
|
| 3036 | cursor.close() |
|
| 3037 | cnx.close() |
|
| 3038 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3039 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 3040 | ||
| 3041 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
|
| 3042 | " WHERE id = %s ", (gid,)) |
|
| 3043 | cnx.commit() |
|
| 3044 | ||
| 3045 | cursor.close() |
|
| 3046 | cnx.close() |
|
| 3047 | ||
| 3048 | resp.status = falcon.HTTP_204 |
|
| 3049 | ||
| 3050 | @staticmethod |
|
| 3051 | @user_logger |
|
| 3052 | def on_put(req, resp, id_, gid): |
|
| 3053 | """Handles PUT requests""" |
|
| 3054 | admin_control(req) |
|
| 3055 | try: |
|
| 3056 | raw_json = req.stream.read().decode('utf-8') |
|
| 3057 | except Exception as ex: |
|
| 3058 | print(str(ex)) |
|
| 3059 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 3060 | title='API.BAD_REQUEST', |
|
| 3061 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 3062 | if not id_.isdigit() or int(id_) <= 0: |
|
| 3063 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3064 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 3065 | ||
| 3066 | if not gid.isdigit() or int(gid) <= 0: |
|
| 3067 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3068 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 3069 | ||
| 3070 | new_values = json.loads(raw_json) |
|
| 3071 | ||
| 3072 | if 'name' not in new_values['data'].keys() or \ |
|
| 3073 | not isinstance(new_values['data']['name'], str) or \ |
|
| 3074 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 3075 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3076 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
|
| 3077 | name = str.strip(new_values['data']['name']) |
|
| 3078 | ||
| 3079 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 3080 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 3081 | new_values['data']['power_point_id'] <= 0: |
|
| 3082 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3083 | description='API.INVALID_POWER_POINT_ID') |
|
| 3084 | power_point_id = new_values['data']['power_point_id'] |
|
| 3085 | ||
| 3086 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
| 3087 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
| 3088 | new_values['data']['buy_meter_id'] <= 0: |
|
| 3089 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3090 | description='API.INVALID_BUY_METER_ID') |
|
| 3091 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
| 3092 | ||
| 3093 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
| 3094 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
| 3095 | new_values['data']['sell_meter_id'] <= 0: |
|
| 3096 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3097 | description='API.INVALID_METER_ID') |
|
| 3098 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
| 3099 | ||
| 3100 | if 'capacity' not in new_values['data'].keys() or \ |
|
| 3101 | not (isinstance(new_values['data']['capacity'], float) or |
|
| 3102 | isinstance(new_values['data']['capacity'], int)): |
|
| 3103 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3104 | description='API.INVALID_CAPACITY') |
|
| 3105 | capacity = Decimal(new_values['data']['capacity']) |
|
| 3106 | ||
| 3107 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 3108 | cursor = cnx.cursor() |
|
| 3109 | ||
| 3110 | cursor.execute(" SELECT name " |
|
| 3111 | " FROM tbl_energy_storage_containers " |
|
| 3112 | " WHERE id = %s ", (id_,)) |
|
| 3113 | if cursor.fetchone() is None: |
|
| 3114 | cursor.close() |
|
| 3115 | cnx.close() |
|
| 3116 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3117 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 3118 | ||
| 3119 | cursor.execute(" SELECT name " |
|
| 3120 | " FROM tbl_energy_storage_containers_grids " |
|
| 3121 | " WHERE id = %s ", (gid,)) |
|
| 3122 | if cursor.fetchone() is None: |
|
| 3123 | cursor.close() |
|
| 3124 | cnx.close() |
|
| 3125 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3126 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 3127 | ||
| 3128 | cursor.execute(" SELECT name " |
|
| 3129 | " FROM tbl_energy_storage_containers_grids " |
|
| 3130 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
| 3131 | (id_, name, gid)) |
|
| 3132 | if cursor.fetchone() is not None: |
|
| 3133 | cursor.close() |
|
| 3134 | cnx.close() |
|
| 3135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 3136 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
|
| 3137 | ||
| 3138 | cursor.execute(" SELECT name " |
|
| 3139 | " FROM tbl_points " |
|
| 3140 | " WHERE id = %s ", |
|
| 3141 | (power_point_id,)) |
|
| 3142 | if cursor.fetchone() is None: |
|
| 3143 | cursor.close() |
|
| 3144 | cnx.close() |
|
| 3145 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3146 | description='API.POWER_POINT_NOT_FOUND') |
|
| 3147 | ||
| 3148 | cursor.execute(" SELECT name " |
|
| 3149 | " FROM tbl_meters " |
|
| 3150 | " WHERE id = %s ", |
|
| 3151 | (buy_meter_id,)) |
|
| 3152 | if cursor.fetchone() is None: |
|
| 3153 | cursor.close() |
|
| 3154 | cnx.close() |
|
| 3155 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3156 | description='API.BUY_METER_NOT_FOUND') |
|
| 3157 | ||
| 3158 | cursor.execute(" SELECT name " |
|
| 3159 | " FROM tbl_meters " |
|
| 3160 | " WHERE id = %s ", |
|
| 3161 | (sell_meter_id,)) |
|
| 3162 | if cursor.fetchone() is None: |
|
| 3163 | cursor.close() |
|
| 3164 | cnx.close() |
|
| 3165 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 3166 | description='API.SELL_METER_NOT_FOUND') |
|
| 3167 | ||
| 3168 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
|
| 3169 | " SET name = %s, energy_storage_container_id = %s, " |
|
| 3170 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
| 3171 | " WHERE id = %s ") |
|
| 3172 | cursor.execute(update_row, (name, |
|
| 3173 | id_, |
|
| 3174 | power_point_id, |
|
| 3175 | buy_meter_id, |
|
| 3176 | sell_meter_id, |
|
| 3177 | capacity, |
|
| 3178 | gid)) |
|
| 3179 | cnx.commit() |
|
| 3180 | ||
| 3181 | cursor.close() |
|
| 3182 | cnx.close() |
|
| 3183 | ||
| 3184 | resp.status = falcon.HTTP_200 |
|
| 3185 | ||
| 3186 | ||
| 3187 | class EnergyStorageContainerGridPointCollection: |
|
| 3188 | def __init__(self): |
|
| @@ 2436-2703 (lines=268) @@ | ||
| 2433 | resp.location = '/microgrids/' + str(id_) + '/grids/' + str(new_id) |
|
| 2434 | ||
| 2435 | ||
| 2436 | class MicrogridGridItem: |
|
| 2437 | def __init__(self): |
|
| 2438 | pass |
|
| 2439 | ||
| 2440 | @staticmethod |
|
| 2441 | def on_options(req, resp, id_, gid): |
|
| 2442 | _ = req |
|
| 2443 | resp.status = falcon.HTTP_200 |
|
| 2444 | _ = id_ |
|
| 2445 | ||
| 2446 | @staticmethod |
|
| 2447 | def on_get(req, resp, id_, gid): |
|
| 2448 | access_control(req) |
|
| 2449 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2450 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2451 | description='API.INVALID_MICROGRID_ID') |
|
| 2452 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2454 | description='API.INVALID_MICROGRID_GRID_ID') |
|
| 2455 | ||
| 2456 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2457 | cursor = cnx.cursor() |
|
| 2458 | ||
| 2459 | cursor.execute(" SELECT name " |
|
| 2460 | " FROM tbl_microgrids " |
|
| 2461 | " WHERE id = %s ", (id_,)) |
|
| 2462 | if cursor.fetchone() is None: |
|
| 2463 | cursor.close() |
|
| 2464 | cnx.close() |
|
| 2465 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2466 | description='API.MICROGRID_NOT_FOUND') |
|
| 2467 | ||
| 2468 | # query microgrid dict |
|
| 2469 | query = (" SELECT id, name, uuid " |
|
| 2470 | " FROM tbl_microgrids ") |
|
| 2471 | cursor.execute(query) |
|
| 2472 | rows_microgrids = cursor.fetchall() |
|
| 2473 | ||
| 2474 | microgrid_dict = dict() |
|
| 2475 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
|
| 2476 | for row in rows_microgrids: |
|
| 2477 | microgrid_dict[row[0]] = {"id": row[0], |
|
| 2478 | "name": row[1], |
|
| 2479 | "uuid": row[2]} |
|
| 2480 | # query meter dict |
|
| 2481 | query = (" SELECT id, name, uuid " |
|
| 2482 | " FROM tbl_meters ") |
|
| 2483 | cursor.execute(query) |
|
| 2484 | rows_meters = cursor.fetchall() |
|
| 2485 | ||
| 2486 | meter_dict = dict() |
|
| 2487 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 2488 | for row in rows_meters: |
|
| 2489 | meter_dict[row[0]] = {"id": row[0], |
|
| 2490 | "name": row[1], |
|
| 2491 | "uuid": row[2]} |
|
| 2492 | # query point dict |
|
| 2493 | query = (" SELECT id, name " |
|
| 2494 | " FROM tbl_points ") |
|
| 2495 | cursor.execute(query) |
|
| 2496 | rows_points = cursor.fetchall() |
|
| 2497 | ||
| 2498 | point_dict = dict() |
|
| 2499 | if rows_points is not None and len(rows_points) > 0: |
|
| 2500 | for row in rows_points: |
|
| 2501 | point_dict[row[0]] = {"id": row[0], |
|
| 2502 | "name": row[1]} |
|
| 2503 | ||
| 2504 | query = (" SELECT id, name, uuid, microgrid_id, power_point_id, buy_meter_id, sell_meter_id, capacity " |
|
| 2505 | " FROM tbl_microgrids_grids " |
|
| 2506 | " WHERE id = %s ") |
|
| 2507 | cursor.execute(query, (gid,)) |
|
| 2508 | row = cursor.fetchone() |
|
| 2509 | cursor.close() |
|
| 2510 | cnx.close() |
|
| 2511 | ||
| 2512 | if row is None: |
|
| 2513 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2514 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
| 2515 | else: |
|
| 2516 | meta_result = {"id": row[0], |
|
| 2517 | "name": row[1], |
|
| 2518 | "uuid": row[2], |
|
| 2519 | "microgrid": microgrid_dict.get(row[3]), |
|
| 2520 | "power_point": point_dict.get(row[4]), |
|
| 2521 | "buy_meter": meter_dict.get(row[5]), |
|
| 2522 | "sell_meter": meter_dict.get(row[6]), |
|
| 2523 | "capacity": row[7]} |
|
| 2524 | ||
| 2525 | resp.text = json.dumps(meta_result) |
|
| 2526 | ||
| 2527 | @staticmethod |
|
| 2528 | @user_logger |
|
| 2529 | def on_delete(req, resp, id_, gid): |
|
| 2530 | admin_control(req) |
|
| 2531 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2532 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2533 | description='API.INVALID_MICROGRID_ID') |
|
| 2534 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2535 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2536 | description='API.INVALID_MICROGRID_GRID_ID') |
|
| 2537 | ||
| 2538 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2539 | cursor = cnx.cursor() |
|
| 2540 | ||
| 2541 | cursor.execute(" SELECT name " |
|
| 2542 | " FROM tbl_microgrids " |
|
| 2543 | " WHERE id = %s ", (id_,)) |
|
| 2544 | if cursor.fetchone() is None: |
|
| 2545 | cursor.close() |
|
| 2546 | cnx.close() |
|
| 2547 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2548 | description='API.MICROGRID_NOT_FOUND') |
|
| 2549 | ||
| 2550 | cursor.execute(" SELECT name " |
|
| 2551 | " FROM tbl_microgrids_grids " |
|
| 2552 | " WHERE id = %s ", (gid,)) |
|
| 2553 | if cursor.fetchone() is None: |
|
| 2554 | cursor.close() |
|
| 2555 | cnx.close() |
|
| 2556 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2557 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
| 2558 | ||
| 2559 | cursor.execute(" DELETE FROM tbl_microgrids_grids " |
|
| 2560 | " WHERE id = %s ", (gid,)) |
|
| 2561 | cnx.commit() |
|
| 2562 | ||
| 2563 | cursor.close() |
|
| 2564 | cnx.close() |
|
| 2565 | ||
| 2566 | resp.status = falcon.HTTP_204 |
|
| 2567 | ||
| 2568 | @staticmethod |
|
| 2569 | @user_logger |
|
| 2570 | def on_put(req, resp, id_, gid): |
|
| 2571 | """Handles PUT requests""" |
|
| 2572 | admin_control(req) |
|
| 2573 | try: |
|
| 2574 | raw_json = req.stream.read().decode('utf-8') |
|
| 2575 | except Exception as ex: |
|
| 2576 | print(str(ex)) |
|
| 2577 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2578 | title='API.BAD_REQUEST', |
|
| 2579 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2580 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2581 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2582 | description='API.INVALID_MICROGRID_ID') |
|
| 2583 | ||
| 2584 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2585 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2586 | description='API.INVALID_MICROGRID_GRID_ID') |
|
| 2587 | ||
| 2588 | new_values = json.loads(raw_json) |
|
| 2589 | ||
| 2590 | if 'name' not in new_values['data'].keys() or \ |
|
| 2591 | not isinstance(new_values['data']['name'], str) or \ |
|
| 2592 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 2593 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2594 | description='API.INVALID_MICROGRID_GRID_NAME') |
|
| 2595 | name = str.strip(new_values['data']['name']) |
|
| 2596 | ||
| 2597 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 2598 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 2599 | new_values['data']['power_point_id'] <= 0: |
|
| 2600 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2601 | description='API.INVALID_POWER_POINT_ID') |
|
| 2602 | power_point_id = new_values['data']['power_point_id'] |
|
| 2603 | ||
| 2604 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
| 2605 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
| 2606 | new_values['data']['buy_meter_id'] <= 0: |
|
| 2607 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2608 | description='API.INVALID_BUY_METER_ID') |
|
| 2609 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
| 2610 | ||
| 2611 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
| 2612 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
| 2613 | new_values['data']['sell_meter_id'] <= 0: |
|
| 2614 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2615 | description='API.INVALID_METER_ID') |
|
| 2616 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
| 2617 | ||
| 2618 | if 'capacity' not in new_values['data'].keys() or \ |
|
| 2619 | not (isinstance(new_values['data']['capacity'], float) or |
|
| 2620 | isinstance(new_values['data']['capacity'], int)): |
|
| 2621 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2622 | description='API.INVALID_CAPACITY') |
|
| 2623 | capacity = float(new_values['data']['capacity']) |
|
| 2624 | ||
| 2625 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2626 | cursor = cnx.cursor() |
|
| 2627 | ||
| 2628 | cursor.execute(" SELECT name " |
|
| 2629 | " FROM tbl_microgrids " |
|
| 2630 | " WHERE id = %s ", (id_,)) |
|
| 2631 | if cursor.fetchone() is None: |
|
| 2632 | cursor.close() |
|
| 2633 | cnx.close() |
|
| 2634 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2635 | description='API.MICROGRID_NOT_FOUND') |
|
| 2636 | ||
| 2637 | cursor.execute(" SELECT name " |
|
| 2638 | " FROM tbl_microgrids_grids " |
|
| 2639 | " WHERE id = %s ", (gid,)) |
|
| 2640 | if cursor.fetchone() is None: |
|
| 2641 | cursor.close() |
|
| 2642 | cnx.close() |
|
| 2643 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2644 | description='API.MICROGRID_GRID_NOT_FOUND') |
|
| 2645 | ||
| 2646 | cursor.execute(" SELECT name " |
|
| 2647 | " FROM tbl_microgrids_grids " |
|
| 2648 | " WHERE microgrid_id = %s AND name = %s AND id != %s ", |
|
| 2649 | (id_, name, gid)) |
|
| 2650 | if cursor.fetchone() is not None: |
|
| 2651 | cursor.close() |
|
| 2652 | cnx.close() |
|
| 2653 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2654 | description='API.MICROGRID_GRID_NAME_IS_ALREADY_IN_USE') |
|
| 2655 | ||
| 2656 | cursor.execute(" SELECT name " |
|
| 2657 | " FROM tbl_points " |
|
| 2658 | " WHERE id = %s ", |
|
| 2659 | (power_point_id,)) |
|
| 2660 | if cursor.fetchone() is None: |
|
| 2661 | cursor.close() |
|
| 2662 | cnx.close() |
|
| 2663 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2664 | description='API.POWER_POINT_NOT_FOUND') |
|
| 2665 | ||
| 2666 | cursor.execute(" SELECT name " |
|
| 2667 | " FROM tbl_meters " |
|
| 2668 | " WHERE id = %s ", |
|
| 2669 | (buy_meter_id,)) |
|
| 2670 | if cursor.fetchone() is None: |
|
| 2671 | cursor.close() |
|
| 2672 | cnx.close() |
|
| 2673 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2674 | description='API.BUY_METER_NOT_FOUND') |
|
| 2675 | ||
| 2676 | cursor.execute(" SELECT name " |
|
| 2677 | " FROM tbl_meters " |
|
| 2678 | " WHERE id = %s ", |
|
| 2679 | (sell_meter_id,)) |
|
| 2680 | if cursor.fetchone() is None: |
|
| 2681 | cursor.close() |
|
| 2682 | cnx.close() |
|
| 2683 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2684 | description='API.SELL_METER_NOT_FOUND') |
|
| 2685 | ||
| 2686 | update_row = (" UPDATE tbl_microgrids_grids " |
|
| 2687 | " SET name = %s, microgrid_id = %s, " |
|
| 2688 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
| 2689 | " WHERE id = %s ") |
|
| 2690 | cursor.execute(update_row, (name, |
|
| 2691 | id_, |
|
| 2692 | power_point_id, |
|
| 2693 | buy_meter_id, |
|
| 2694 | sell_meter_id, |
|
| 2695 | capacity, |
|
| 2696 | gid)) |
|
| 2697 | cnx.commit() |
|
| 2698 | ||
| 2699 | cursor.close() |
|
| 2700 | cnx.close() |
|
| 2701 | ||
| 2702 | resp.status = falcon.HTTP_200 |
|
| 2703 | ||
| 2704 | ||
| 2705 | class MicrogridHeatpumpCollection: |
|
| 2706 | def __init__(self): |
|