|
@@ 3242-3404 (lines=163) @@
|
| 3239 |
|
resp.status = falcon.HTTP_204 |
| 3240 |
|
|
| 3241 |
|
|
| 3242 |
|
class HybridPowerStationMCUCollection: |
| 3243 |
|
def __init__(self): |
| 3244 |
|
"""Initializes Class""" |
| 3245 |
|
pass |
| 3246 |
|
|
| 3247 |
|
@staticmethod |
| 3248 |
|
def on_options(req, resp, id_): |
| 3249 |
|
resp.status = falcon.HTTP_200 |
| 3250 |
|
|
| 3251 |
|
@staticmethod |
| 3252 |
|
def on_get(req, resp, id_): |
| 3253 |
|
access_control(req) |
| 3254 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3255 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3256 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 3257 |
|
|
| 3258 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3259 |
|
cursor = cnx.cursor() |
| 3260 |
|
|
| 3261 |
|
cursor.execute(" SELECT name " |
| 3262 |
|
" FROM tbl_hybrid_power_stations " |
| 3263 |
|
" WHERE id = %s ", (id_,)) |
| 3264 |
|
if cursor.fetchone() is None: |
| 3265 |
|
cursor.close() |
| 3266 |
|
cnx.close() |
| 3267 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3268 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3269 |
|
|
| 3270 |
|
# query meter dict |
| 3271 |
|
query = (" SELECT id, name, uuid " |
| 3272 |
|
" FROM tbl_meters ") |
| 3273 |
|
cursor.execute(query) |
| 3274 |
|
rows_meters = cursor.fetchall() |
| 3275 |
|
|
| 3276 |
|
meter_dict = dict() |
| 3277 |
|
if rows_meters is not None and len(rows_meters) > 0: |
| 3278 |
|
for row in rows_meters: |
| 3279 |
|
meter_dict[row[0]] = {"id": row[0], |
| 3280 |
|
"name": row[1], |
| 3281 |
|
"uuid": row[2]} |
| 3282 |
|
# query point dict |
| 3283 |
|
query = (" SELECT id, name " |
| 3284 |
|
" FROM tbl_points ") |
| 3285 |
|
cursor.execute(query) |
| 3286 |
|
rows_points = cursor.fetchall() |
| 3287 |
|
|
| 3288 |
|
point_dict = dict() |
| 3289 |
|
if rows_points is not None and len(rows_points) > 0: |
| 3290 |
|
for row in rows_points: |
| 3291 |
|
point_dict[row[0]] = {"id": row[0], |
| 3292 |
|
"name": row[1]} |
| 3293 |
|
|
| 3294 |
|
query = (" SELECT id, name, uuid, " |
| 3295 |
|
" operating_status_point_id " |
| 3296 |
|
" FROM tbl_hybrid_power_stations_mcus " |
| 3297 |
|
" WHERE hybrid_power_station_id = %s " |
| 3298 |
|
" ORDER BY name ") |
| 3299 |
|
cursor.execute(query, (id_,)) |
| 3300 |
|
rows = cursor.fetchall() |
| 3301 |
|
|
| 3302 |
|
result = list() |
| 3303 |
|
if rows is not None and len(rows) > 0: |
| 3304 |
|
for row in rows: |
| 3305 |
|
meta_result = {"id": row[0], |
| 3306 |
|
"name": row[1], |
| 3307 |
|
"uuid": row[2], |
| 3308 |
|
"operating_status_point": point_dict.get(row[3]) |
| 3309 |
|
} |
| 3310 |
|
result.append(meta_result) |
| 3311 |
|
|
| 3312 |
|
resp.text = json.dumps(result) |
| 3313 |
|
|
| 3314 |
|
@staticmethod |
| 3315 |
|
@user_logger |
| 3316 |
|
def on_post(req, resp, id_): |
| 3317 |
|
"""Handles POST requests""" |
| 3318 |
|
admin_control(req) |
| 3319 |
|
try: |
| 3320 |
|
raw_json = req.stream.read().decode('utf-8') |
| 3321 |
|
except Exception as ex: |
| 3322 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 3323 |
|
title='API.BAD_REQUEST', |
| 3324 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 3325 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3326 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3327 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 3328 |
|
|
| 3329 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3330 |
|
cursor = cnx.cursor() |
| 3331 |
|
|
| 3332 |
|
cursor.execute(" SELECT name " |
| 3333 |
|
" FROM tbl_hybrid_power_stations " |
| 3334 |
|
" WHERE id = %s ", (id_,)) |
| 3335 |
|
if cursor.fetchone() is None: |
| 3336 |
|
cursor.close() |
| 3337 |
|
cnx.close() |
| 3338 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3339 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3340 |
|
|
| 3341 |
|
new_values = json.loads(raw_json) |
| 3342 |
|
|
| 3343 |
|
if 'name' not in new_values['data'].keys() or \ |
| 3344 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 3345 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 3346 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3347 |
|
description='API.INVALID_HYBRID_POWER_STATION_MCU_NAME') |
| 3348 |
|
name = str.strip(new_values['data']['name']) |
| 3349 |
|
|
| 3350 |
|
if 'operating_status_point_id' not in new_values['data'].keys() or \ |
| 3351 |
|
not isinstance(new_values['data']['operating_status_point_id'], int) or \ |
| 3352 |
|
new_values['data']['operating_status_point_id'] <= 0: |
| 3353 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3354 |
|
description='API.INVALID_OPERATING_STATUS_POINT_ID') |
| 3355 |
|
operating_status_point_id = new_values['data']['operating_status_point_id'] |
| 3356 |
|
|
| 3357 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3358 |
|
cursor = cnx.cursor() |
| 3359 |
|
|
| 3360 |
|
cursor.execute(" SELECT name " |
| 3361 |
|
" FROM tbl_hybrid_power_stations " |
| 3362 |
|
" WHERE id = %s ", |
| 3363 |
|
(id_,)) |
| 3364 |
|
if cursor.fetchone() is None: |
| 3365 |
|
cursor.close() |
| 3366 |
|
cnx.close() |
| 3367 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3368 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3369 |
|
|
| 3370 |
|
cursor.execute(" SELECT name " |
| 3371 |
|
" FROM tbl_hybrid_power_stations_mcus " |
| 3372 |
|
" WHERE hybrid_power_station_id = %s AND name = %s ", |
| 3373 |
|
(id_, name,)) |
| 3374 |
|
if cursor.fetchone() is not None: |
| 3375 |
|
cursor.close() |
| 3376 |
|
cnx.close() |
| 3377 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3378 |
|
description='API.HYBRID_POWER_STATION_MCU_NAME_IS_ALREADY_IN_USE') |
| 3379 |
|
|
| 3380 |
|
cursor.execute(" SELECT name " |
| 3381 |
|
" FROM tbl_points " |
| 3382 |
|
" WHERE id = %s ", |
| 3383 |
|
(operating_status_point_id,)) |
| 3384 |
|
if cursor.fetchone() is None: |
| 3385 |
|
cursor.close() |
| 3386 |
|
cnx.close() |
| 3387 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3388 |
|
description='API.OPERATING_STATUS_POINT_NOT_FOUND') |
| 3389 |
|
|
| 3390 |
|
add_values = (" INSERT INTO tbl_hybrid_power_stations_mcus " |
| 3391 |
|
" (name, uuid, hybrid_power_station_id, operating_status_point) " |
| 3392 |
|
" VALUES (%s, %s, %s, %s) ") |
| 3393 |
|
cursor.execute(add_values, (name, |
| 3394 |
|
str(uuid.uuid4()), |
| 3395 |
|
id_, |
| 3396 |
|
operating_status_point_id |
| 3397 |
|
)) |
| 3398 |
|
new_id = cursor.lastrowid |
| 3399 |
|
cnx.commit() |
| 3400 |
|
cursor.close() |
| 3401 |
|
cnx.close() |
| 3402 |
|
|
| 3403 |
|
resp.status = falcon.HTTP_201 |
| 3404 |
|
resp.location = '/hybridpowerstations/' + str(id_) + '/mcus/' + str(new_id) |
| 3405 |
|
|
| 3406 |
|
|
| 3407 |
|
class HybridPowerStationMCUItem: |
|
@@ 2646-2808 (lines=163) @@
|
| 2643 |
|
resp.status = falcon.HTTP_204 |
| 2644 |
|
|
| 2645 |
|
|
| 2646 |
|
class HybridPowerStationLoadCollection: |
| 2647 |
|
def __init__(self): |
| 2648 |
|
"""Initializes Class""" |
| 2649 |
|
pass |
| 2650 |
|
|
| 2651 |
|
@staticmethod |
| 2652 |
|
def on_options(req, resp, id_): |
| 2653 |
|
resp.status = falcon.HTTP_200 |
| 2654 |
|
|
| 2655 |
|
@staticmethod |
| 2656 |
|
def on_get(req, resp, id_): |
| 2657 |
|
access_control(req) |
| 2658 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2659 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2660 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 2661 |
|
|
| 2662 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2663 |
|
cursor = cnx.cursor() |
| 2664 |
|
|
| 2665 |
|
cursor.execute(" SELECT name " |
| 2666 |
|
" FROM tbl_hybrid_power_stations " |
| 2667 |
|
" WHERE id = %s ", (id_,)) |
| 2668 |
|
if cursor.fetchone() is None: |
| 2669 |
|
cursor.close() |
| 2670 |
|
cnx.close() |
| 2671 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2672 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2673 |
|
|
| 2674 |
|
# query meter dict |
| 2675 |
|
query = (" SELECT id, name, uuid " |
| 2676 |
|
" FROM tbl_meters ") |
| 2677 |
|
cursor.execute(query) |
| 2678 |
|
rows_meters = cursor.fetchall() |
| 2679 |
|
|
| 2680 |
|
meter_dict = dict() |
| 2681 |
|
if rows_meters is not None and len(rows_meters) > 0: |
| 2682 |
|
for row in rows_meters: |
| 2683 |
|
meter_dict[row[0]] = {"id": row[0], |
| 2684 |
|
"name": row[1], |
| 2685 |
|
"uuid": row[2]} |
| 2686 |
|
# query point dict |
| 2687 |
|
query = (" SELECT id, name " |
| 2688 |
|
" FROM tbl_points ") |
| 2689 |
|
cursor.execute(query) |
| 2690 |
|
rows_points = cursor.fetchall() |
| 2691 |
|
|
| 2692 |
|
point_dict = dict() |
| 2693 |
|
if rows_points is not None and len(rows_points) > 0: |
| 2694 |
|
for row in rows_points: |
| 2695 |
|
point_dict[row[0]] = {"id": row[0], |
| 2696 |
|
"name": row[1]} |
| 2697 |
|
|
| 2698 |
|
query = (" SELECT id, name, uuid, " |
| 2699 |
|
" meter_id " |
| 2700 |
|
" FROM tbl_hybrid_power_stations_loads " |
| 2701 |
|
" WHERE hybrid_power_station_id = %s " |
| 2702 |
|
" ORDER BY name ") |
| 2703 |
|
cursor.execute(query, (id_,)) |
| 2704 |
|
rows = cursor.fetchall() |
| 2705 |
|
|
| 2706 |
|
result = list() |
| 2707 |
|
if rows is not None and len(rows) > 0: |
| 2708 |
|
for row in rows: |
| 2709 |
|
meta_result = {"id": row[0], |
| 2710 |
|
"name": row[1], |
| 2711 |
|
"uuid": row[2], |
| 2712 |
|
"meter": meter_dict.get(row[3]) |
| 2713 |
|
} |
| 2714 |
|
result.append(meta_result) |
| 2715 |
|
|
| 2716 |
|
resp.text = json.dumps(result) |
| 2717 |
|
|
| 2718 |
|
@staticmethod |
| 2719 |
|
@user_logger |
| 2720 |
|
def on_post(req, resp, id_): |
| 2721 |
|
"""Handles POST requests""" |
| 2722 |
|
admin_control(req) |
| 2723 |
|
try: |
| 2724 |
|
raw_json = req.stream.read().decode('utf-8') |
| 2725 |
|
except Exception as ex: |
| 2726 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 2727 |
|
title='API.BAD_REQUEST', |
| 2728 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 2729 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2730 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2731 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 2732 |
|
|
| 2733 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2734 |
|
cursor = cnx.cursor() |
| 2735 |
|
|
| 2736 |
|
cursor.execute(" SELECT name " |
| 2737 |
|
" FROM tbl_hybrid_power_stations " |
| 2738 |
|
" WHERE id = %s ", (id_,)) |
| 2739 |
|
if cursor.fetchone() is None: |
| 2740 |
|
cursor.close() |
| 2741 |
|
cnx.close() |
| 2742 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2743 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2744 |
|
|
| 2745 |
|
new_values = json.loads(raw_json) |
| 2746 |
|
|
| 2747 |
|
if 'name' not in new_values['data'].keys() or \ |
| 2748 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 2749 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 2750 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2751 |
|
description='API.INVALID_HYBRID_POWER_STATION_LOAD_NAME') |
| 2752 |
|
name = str.strip(new_values['data']['name']) |
| 2753 |
|
|
| 2754 |
|
if 'meter_id' not in new_values['data'].keys() or \ |
| 2755 |
|
not isinstance(new_values['data']['meter_id'], int) or \ |
| 2756 |
|
new_values['data']['meter_id'] <= 0: |
| 2757 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2758 |
|
description='API.INVALID_METER_ID') |
| 2759 |
|
meter_id = new_values['data']['meter_id'] |
| 2760 |
|
|
| 2761 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2762 |
|
cursor = cnx.cursor() |
| 2763 |
|
|
| 2764 |
|
cursor.execute(" SELECT name " |
| 2765 |
|
" FROM tbl_hybrid_power_stations " |
| 2766 |
|
" WHERE id = %s ", |
| 2767 |
|
(id_,)) |
| 2768 |
|
if cursor.fetchone() is None: |
| 2769 |
|
cursor.close() |
| 2770 |
|
cnx.close() |
| 2771 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2772 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2773 |
|
|
| 2774 |
|
cursor.execute(" SELECT name " |
| 2775 |
|
" FROM tbl_hybrid_power_stations_loads " |
| 2776 |
|
" WHERE hybrid_power_station_id = %s AND name = %s ", |
| 2777 |
|
(id_, name,)) |
| 2778 |
|
if cursor.fetchone() is not None: |
| 2779 |
|
cursor.close() |
| 2780 |
|
cnx.close() |
| 2781 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2782 |
|
description='API.HYBRID_POWER_STATION_LOAD_NAME_IS_ALREADY_IN_USE') |
| 2783 |
|
|
| 2784 |
|
cursor.execute(" SELECT name " |
| 2785 |
|
" FROM tbl_meters " |
| 2786 |
|
" WHERE id = %s ", |
| 2787 |
|
(meter_id,)) |
| 2788 |
|
if cursor.fetchone() is None: |
| 2789 |
|
cursor.close() |
| 2790 |
|
cnx.close() |
| 2791 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2792 |
|
description='API.METER_NOT_FOUND') |
| 2793 |
|
|
| 2794 |
|
add_values = (" INSERT INTO tbl_hybrid_power_stations_loads " |
| 2795 |
|
" (name, uuid, hybrid_power_station_id, meter_id) " |
| 2796 |
|
" VALUES (%s, %s, %s, %s) ") |
| 2797 |
|
cursor.execute(add_values, (name, |
| 2798 |
|
str(uuid.uuid4()), |
| 2799 |
|
id_, |
| 2800 |
|
meter_id, |
| 2801 |
|
)) |
| 2802 |
|
new_id = cursor.lastrowid |
| 2803 |
|
cnx.commit() |
| 2804 |
|
cursor.close() |
| 2805 |
|
cnx.close() |
| 2806 |
|
|
| 2807 |
|
resp.status = falcon.HTTP_201 |
| 2808 |
|
resp.location = '/hybridpowerstations/' + str(id_) + '/loads/' + str(new_id) |
| 2809 |
|
|
| 2810 |
|
|
| 2811 |
|
class HybridPowerStationLoadItem: |