| @@ 2469-2734 (lines=266) @@ | ||
| 2466 | resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(new_id) |
|
| 2467 | ||
| 2468 | ||
| 2469 | class EnergyStorageContainerGridItem: |
|
| 2470 | def __init__(self): |
|
| 2471 | """Initializes Class""" |
|
| 2472 | pass |
|
| 2473 | ||
| 2474 | @staticmethod |
|
| 2475 | def on_options(req, resp, id_, gid): |
|
| 2476 | resp.status = falcon.HTTP_200 |
|
| 2477 | ||
| 2478 | @staticmethod |
|
| 2479 | def on_get(req, resp, id_, gid): |
|
| 2480 | access_control(req) |
|
| 2481 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2482 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2483 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2484 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2485 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2486 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2487 | ||
| 2488 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2489 | cursor = cnx.cursor() |
|
| 2490 | ||
| 2491 | cursor.execute(" SELECT name " |
|
| 2492 | " FROM tbl_energy_storage_containers " |
|
| 2493 | " WHERE id = %s ", (id_,)) |
|
| 2494 | if cursor.fetchone() is None: |
|
| 2495 | cursor.close() |
|
| 2496 | cnx.close() |
|
| 2497 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2498 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 2499 | ||
| 2500 | query = (" SELECT id, name, uuid " |
|
| 2501 | " FROM tbl_energy_storage_containers ") |
|
| 2502 | cursor.execute(query) |
|
| 2503 | rows_energystoragecontainers = cursor.fetchall() |
|
| 2504 | ||
| 2505 | energy_storage_container_dict = dict() |
|
| 2506 | if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
|
| 2507 | for row in rows_energystoragecontainers: |
|
| 2508 | energy_storage_container_dict[row[0]] = {"id": row[0], |
|
| 2509 | "name": row[1], |
|
| 2510 | "uuid": row[2]} |
|
| 2511 | # query meter dict |
|
| 2512 | query = (" SELECT id, name, uuid " |
|
| 2513 | " FROM tbl_meters ") |
|
| 2514 | cursor.execute(query) |
|
| 2515 | rows_meters = cursor.fetchall() |
|
| 2516 | ||
| 2517 | meter_dict = dict() |
|
| 2518 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 2519 | for row in rows_meters: |
|
| 2520 | meter_dict[row[0]] = {"id": row[0], |
|
| 2521 | "name": row[1], |
|
| 2522 | "uuid": row[2]} |
|
| 2523 | # query point dict |
|
| 2524 | query = (" SELECT id, name " |
|
| 2525 | " FROM tbl_points ") |
|
| 2526 | cursor.execute(query) |
|
| 2527 | rows_points = cursor.fetchall() |
|
| 2528 | ||
| 2529 | point_dict = dict() |
|
| 2530 | if rows_points is not None and len(rows_points) > 0: |
|
| 2531 | for row in rows_points: |
|
| 2532 | point_dict[row[0]] = {"id": row[0], |
|
| 2533 | "name": row[1]} |
|
| 2534 | ||
| 2535 | query = (" SELECT id, name, uuid, energy_storage_container_id, power_point_id, " |
|
| 2536 | " buy_meter_id, sell_meter_id, capacity " |
|
| 2537 | " FROM tbl_energy_storage_containers_grids " |
|
| 2538 | " WHERE id = %s ") |
|
| 2539 | cursor.execute(query, (gid,)) |
|
| 2540 | row = cursor.fetchone() |
|
| 2541 | cursor.close() |
|
| 2542 | cnx.close() |
|
| 2543 | ||
| 2544 | if row is None: |
|
| 2545 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2546 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2547 | else: |
|
| 2548 | meta_result = {"id": row[0], |
|
| 2549 | "name": row[1], |
|
| 2550 | "uuid": row[2], |
|
| 2551 | "energy_storage_container": energy_storage_container_dict.get(row[3]), |
|
| 2552 | "power_point": point_dict.get(row[4]), |
|
| 2553 | "buy_meter": meter_dict.get(row[5]), |
|
| 2554 | "sell_meter": meter_dict.get(row[6]), |
|
| 2555 | "capacity": row[7] |
|
| 2556 | } |
|
| 2557 | ||
| 2558 | resp.text = json.dumps(meta_result) |
|
| 2559 | ||
| 2560 | @staticmethod |
|
| 2561 | @user_logger |
|
| 2562 | def on_delete(req, resp, id_, gid): |
|
| 2563 | admin_control(req) |
|
| 2564 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2565 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2566 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2567 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2568 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2569 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2570 | ||
| 2571 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2572 | cursor = cnx.cursor() |
|
| 2573 | ||
| 2574 | cursor.execute(" SELECT name " |
|
| 2575 | " FROM tbl_energy_storage_containers " |
|
| 2576 | " WHERE id = %s ", (id_,)) |
|
| 2577 | if cursor.fetchone() is None: |
|
| 2578 | cursor.close() |
|
| 2579 | cnx.close() |
|
| 2580 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2581 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 2582 | ||
| 2583 | cursor.execute(" SELECT name " |
|
| 2584 | " FROM tbl_energy_storage_containers_grids " |
|
| 2585 | " WHERE id = %s ", (gid,)) |
|
| 2586 | if cursor.fetchone() is None: |
|
| 2587 | cursor.close() |
|
| 2588 | cnx.close() |
|
| 2589 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2590 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2591 | ||
| 2592 | cursor.execute(" DELETE FROM tbl_energy_storage_containers_grids " |
|
| 2593 | " WHERE id = %s ", (gid,)) |
|
| 2594 | cnx.commit() |
|
| 2595 | ||
| 2596 | cursor.close() |
|
| 2597 | cnx.close() |
|
| 2598 | ||
| 2599 | resp.status = falcon.HTTP_204 |
|
| 2600 | ||
| 2601 | @staticmethod |
|
| 2602 | @user_logger |
|
| 2603 | def on_put(req, resp, id_, gid): |
|
| 2604 | """Handles PUT requests""" |
|
| 2605 | admin_control(req) |
|
| 2606 | try: |
|
| 2607 | raw_json = req.stream.read().decode('utf-8') |
|
| 2608 | except Exception as ex: |
|
| 2609 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 2610 | title='API.BAD_REQUEST', |
|
| 2611 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 2612 | if not id_.isdigit() or int(id_) <= 0: |
|
| 2613 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2614 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
|
| 2615 | ||
| 2616 | if not gid.isdigit() or int(gid) <= 0: |
|
| 2617 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2618 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_ID') |
|
| 2619 | ||
| 2620 | new_values = json.loads(raw_json) |
|
| 2621 | ||
| 2622 | if 'name' not in new_values['data'].keys() or \ |
|
| 2623 | not isinstance(new_values['data']['name'], str) or \ |
|
| 2624 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 2625 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2626 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_GRID_NAME') |
|
| 2627 | name = str.strip(new_values['data']['name']) |
|
| 2628 | ||
| 2629 | if 'power_point_id' not in new_values['data'].keys() or \ |
|
| 2630 | not isinstance(new_values['data']['power_point_id'], int) or \ |
|
| 2631 | new_values['data']['power_point_id'] <= 0: |
|
| 2632 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2633 | description='API.INVALID_POWER_POINT_ID') |
|
| 2634 | power_point_id = new_values['data']['power_point_id'] |
|
| 2635 | ||
| 2636 | if 'buy_meter_id' not in new_values['data'].keys() or \ |
|
| 2637 | not isinstance(new_values['data']['buy_meter_id'], int) or \ |
|
| 2638 | new_values['data']['buy_meter_id'] <= 0: |
|
| 2639 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2640 | description='API.INVALID_BUY_METER_ID') |
|
| 2641 | buy_meter_id = new_values['data']['buy_meter_id'] |
|
| 2642 | ||
| 2643 | if 'sell_meter_id' not in new_values['data'].keys() or \ |
|
| 2644 | not isinstance(new_values['data']['sell_meter_id'], int) or \ |
|
| 2645 | new_values['data']['sell_meter_id'] <= 0: |
|
| 2646 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2647 | description='API.INVALID_METER_ID') |
|
| 2648 | sell_meter_id = new_values['data']['sell_meter_id'] |
|
| 2649 | ||
| 2650 | if 'capacity' not in new_values['data'].keys() or \ |
|
| 2651 | not (isinstance(new_values['data']['capacity'], float) or |
|
| 2652 | isinstance(new_values['data']['capacity'], int)): |
|
| 2653 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2654 | description='API.INVALID_CAPACITY') |
|
| 2655 | capacity = Decimal(new_values['data']['capacity']) |
|
| 2656 | ||
| 2657 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 2658 | cursor = cnx.cursor() |
|
| 2659 | ||
| 2660 | cursor.execute(" SELECT name " |
|
| 2661 | " FROM tbl_energy_storage_containers " |
|
| 2662 | " WHERE id = %s ", (id_,)) |
|
| 2663 | if cursor.fetchone() is None: |
|
| 2664 | cursor.close() |
|
| 2665 | cnx.close() |
|
| 2666 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2667 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
|
| 2668 | ||
| 2669 | cursor.execute(" SELECT name " |
|
| 2670 | " FROM tbl_energy_storage_containers_grids " |
|
| 2671 | " WHERE id = %s ", (gid,)) |
|
| 2672 | if cursor.fetchone() is None: |
|
| 2673 | cursor.close() |
|
| 2674 | cnx.close() |
|
| 2675 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2676 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND') |
|
| 2677 | ||
| 2678 | cursor.execute(" SELECT name " |
|
| 2679 | " FROM tbl_energy_storage_containers_grids " |
|
| 2680 | " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
|
| 2681 | (id_, name, gid)) |
|
| 2682 | if cursor.fetchone() is not None: |
|
| 2683 | cursor.close() |
|
| 2684 | cnx.close() |
|
| 2685 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 2686 | description='API.ENERGY_STORAGE_CONTAINER_GRID_NAME_IS_ALREADY_IN_USE') |
|
| 2687 | ||
| 2688 | cursor.execute(" SELECT name " |
|
| 2689 | " FROM tbl_points " |
|
| 2690 | " WHERE id = %s ", |
|
| 2691 | (power_point_id,)) |
|
| 2692 | if cursor.fetchone() is None: |
|
| 2693 | cursor.close() |
|
| 2694 | cnx.close() |
|
| 2695 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2696 | description='API.POWER_POINT_NOT_FOUND') |
|
| 2697 | ||
| 2698 | cursor.execute(" SELECT name " |
|
| 2699 | " FROM tbl_meters " |
|
| 2700 | " WHERE id = %s ", |
|
| 2701 | (buy_meter_id,)) |
|
| 2702 | if cursor.fetchone() is None: |
|
| 2703 | cursor.close() |
|
| 2704 | cnx.close() |
|
| 2705 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2706 | description='API.BUY_METER_NOT_FOUND') |
|
| 2707 | ||
| 2708 | cursor.execute(" SELECT name " |
|
| 2709 | " FROM tbl_meters " |
|
| 2710 | " WHERE id = %s ", |
|
| 2711 | (sell_meter_id,)) |
|
| 2712 | if cursor.fetchone() is None: |
|
| 2713 | cursor.close() |
|
| 2714 | cnx.close() |
|
| 2715 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 2716 | description='API.SELL_METER_NOT_FOUND') |
|
| 2717 | ||
| 2718 | update_row = (" UPDATE tbl_energy_storage_containers_grids " |
|
| 2719 | " SET name = %s, energy_storage_container_id = %s, " |
|
| 2720 | " power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s " |
|
| 2721 | " WHERE id = %s ") |
|
| 2722 | cursor.execute(update_row, (name, |
|
| 2723 | id_, |
|
| 2724 | power_point_id, |
|
| 2725 | buy_meter_id, |
|
| 2726 | sell_meter_id, |
|
| 2727 | capacity, |
|
| 2728 | gid)) |
|
| 2729 | cnx.commit() |
|
| 2730 | ||
| 2731 | cursor.close() |
|
| 2732 | cnx.close() |
|
| 2733 | ||
| 2734 | resp.status = falcon.HTTP_200 |
|
| 2735 | ||
| 2736 | ||
| 2737 | class EnergyStorageContainerHVACCollection: |
|
| @@ 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: |
|