|
@@ 1785-1957 (lines=173) @@
|
| 1782 |
|
resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id) |
| 1783 |
|
|
| 1784 |
|
|
| 1785 |
|
class EnergyStorageContainerDCDCItem: |
| 1786 |
|
def __init__(self): |
| 1787 |
|
"""Initializes Class""" |
| 1788 |
|
pass |
| 1789 |
|
|
| 1790 |
|
@staticmethod |
| 1791 |
|
def on_options(req, resp, id_, did): |
| 1792 |
|
resp.status = falcon.HTTP_200 |
| 1793 |
|
|
| 1794 |
|
@staticmethod |
| 1795 |
|
def on_get(req, resp, id_, did): |
| 1796 |
|
access_control(req) |
| 1797 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 1798 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1799 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 1800 |
|
if not did.isdigit() or int(did) <= 0: |
| 1801 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1802 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
| 1803 |
|
|
| 1804 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 1805 |
|
cursor = cnx.cursor() |
| 1806 |
|
|
| 1807 |
|
cursor.execute(" SELECT name " |
| 1808 |
|
" FROM tbl_energy_storage_containers " |
| 1809 |
|
" WHERE id = %s ", (id_,)) |
| 1810 |
|
if cursor.fetchone() is None: |
| 1811 |
|
cursor.close() |
| 1812 |
|
cnx.close() |
| 1813 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1814 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 1815 |
|
|
| 1816 |
|
query = (" SELECT id, name, uuid " |
| 1817 |
|
" FROM tbl_energy_storage_containers ") |
| 1818 |
|
cursor.execute(query) |
| 1819 |
|
rows_energystoragecontainers = cursor.fetchall() |
| 1820 |
|
|
| 1821 |
|
energy_storage_container_dict = dict() |
| 1822 |
|
if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
| 1823 |
|
for row in rows_energystoragecontainers: |
| 1824 |
|
energy_storage_container_dict[row[0]] = {"id": row[0], |
| 1825 |
|
"name": row[1], |
| 1826 |
|
"uuid": row[2]} |
| 1827 |
|
|
| 1828 |
|
query = (" SELECT id, name, uuid " |
| 1829 |
|
" FROM tbl_energy_storage_containers_dcdcs " |
| 1830 |
|
" WHERE id = %s ") |
| 1831 |
|
cursor.execute(query, (did,)) |
| 1832 |
|
row = cursor.fetchone() |
| 1833 |
|
cursor.close() |
| 1834 |
|
cnx.close() |
| 1835 |
|
|
| 1836 |
|
if row is None: |
| 1837 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1838 |
|
description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
| 1839 |
|
else: |
| 1840 |
|
meta_result = {"id": row[0], |
| 1841 |
|
"name": row[1], |
| 1842 |
|
"uuid": row[2] |
| 1843 |
|
} |
| 1844 |
|
|
| 1845 |
|
resp.text = json.dumps(meta_result) |
| 1846 |
|
|
| 1847 |
|
@staticmethod |
| 1848 |
|
@user_logger |
| 1849 |
|
def on_delete(req, resp, id_, did): |
| 1850 |
|
admin_control(req) |
| 1851 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 1852 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1853 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 1854 |
|
if not did.isdigit() or int(did) <= 0: |
| 1855 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1856 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
| 1857 |
|
|
| 1858 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 1859 |
|
cursor = cnx.cursor() |
| 1860 |
|
|
| 1861 |
|
cursor.execute(" SELECT name " |
| 1862 |
|
" FROM tbl_energy_storage_containers " |
| 1863 |
|
" WHERE id = %s ", (id_,)) |
| 1864 |
|
if cursor.fetchone() is None: |
| 1865 |
|
cursor.close() |
| 1866 |
|
cnx.close() |
| 1867 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1868 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 1869 |
|
|
| 1870 |
|
cursor.execute(" SELECT name " |
| 1871 |
|
" FROM tbl_energy_storage_containers_dcdcs " |
| 1872 |
|
" WHERE id = %s ", (did,)) |
| 1873 |
|
if cursor.fetchone() is None: |
| 1874 |
|
cursor.close() |
| 1875 |
|
cnx.close() |
| 1876 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1877 |
|
description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
| 1878 |
|
|
| 1879 |
|
cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs " |
| 1880 |
|
" WHERE id = %s ", (did,)) |
| 1881 |
|
cnx.commit() |
| 1882 |
|
|
| 1883 |
|
cursor.close() |
| 1884 |
|
cnx.close() |
| 1885 |
|
|
| 1886 |
|
resp.status = falcon.HTTP_204 |
| 1887 |
|
|
| 1888 |
|
@staticmethod |
| 1889 |
|
@user_logger |
| 1890 |
|
def on_put(req, resp, id_, did): |
| 1891 |
|
"""Handles PUT requests""" |
| 1892 |
|
admin_control(req) |
| 1893 |
|
try: |
| 1894 |
|
raw_json = req.stream.read().decode('utf-8') |
| 1895 |
|
except Exception as ex: |
| 1896 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 1897 |
|
title='API.BAD_REQUEST', |
| 1898 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 1899 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 1900 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1901 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 1902 |
|
|
| 1903 |
|
if not did.isdigit() or int(did) <= 0: |
| 1904 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1905 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID') |
| 1906 |
|
|
| 1907 |
|
new_values = json.loads(raw_json) |
| 1908 |
|
|
| 1909 |
|
if 'name' not in new_values['data'].keys() or \ |
| 1910 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 1911 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 1912 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1913 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME') |
| 1914 |
|
name = str.strip(new_values['data']['name']) |
| 1915 |
|
|
| 1916 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 1917 |
|
cursor = cnx.cursor() |
| 1918 |
|
|
| 1919 |
|
cursor.execute(" SELECT name " |
| 1920 |
|
" FROM tbl_energy_storage_containers " |
| 1921 |
|
" WHERE id = %s ", (id_,)) |
| 1922 |
|
if cursor.fetchone() is None: |
| 1923 |
|
cursor.close() |
| 1924 |
|
cnx.close() |
| 1925 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1926 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 1927 |
|
|
| 1928 |
|
cursor.execute(" SELECT name " |
| 1929 |
|
" FROM tbl_energy_storage_containers_dcdcs " |
| 1930 |
|
" WHERE id = %s ", (did,)) |
| 1931 |
|
if cursor.fetchone() is None: |
| 1932 |
|
cursor.close() |
| 1933 |
|
cnx.close() |
| 1934 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 1935 |
|
description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND') |
| 1936 |
|
|
| 1937 |
|
cursor.execute(" SELECT name " |
| 1938 |
|
" FROM tbl_energy_storage_containers_dcdcs " |
| 1939 |
|
" WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
| 1940 |
|
(id_, name, did)) |
| 1941 |
|
if cursor.fetchone() is not None: |
| 1942 |
|
cursor.close() |
| 1943 |
|
cnx.close() |
| 1944 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 1945 |
|
description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE') |
| 1946 |
|
|
| 1947 |
|
update_row = (" UPDATE tbl_energy_storage_containers_dcdcs " |
| 1948 |
|
" SET name = %s " |
| 1949 |
|
" WHERE id = %s ") |
| 1950 |
|
cursor.execute(update_row, (name, |
| 1951 |
|
did)) |
| 1952 |
|
cnx.commit() |
| 1953 |
|
|
| 1954 |
|
cursor.close() |
| 1955 |
|
cnx.close() |
| 1956 |
|
|
| 1957 |
|
resp.status = falcon.HTTP_200 |
| 1958 |
|
|
| 1959 |
|
|
| 1960 |
|
class EnergyStorageContainerDCDCPointCollection: |
|
@@ 3407-3579 (lines=173) @@
|
| 3404 |
|
resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id) |
| 3405 |
|
|
| 3406 |
|
|
| 3407 |
|
class EnergyStorageContainerHVACItem: |
| 3408 |
|
def __init__(self): |
| 3409 |
|
"""Initializes Class""" |
| 3410 |
|
pass |
| 3411 |
|
|
| 3412 |
|
@staticmethod |
| 3413 |
|
def on_options(req, resp, id_, hid): |
| 3414 |
|
resp.status = falcon.HTTP_200 |
| 3415 |
|
|
| 3416 |
|
@staticmethod |
| 3417 |
|
def on_get(req, resp, id_, hid): |
| 3418 |
|
access_control(req) |
| 3419 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3420 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3421 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 3422 |
|
if not hid.isdigit() or int(hid) <= 0: |
| 3423 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3424 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
| 3425 |
|
|
| 3426 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3427 |
|
cursor = cnx.cursor() |
| 3428 |
|
|
| 3429 |
|
cursor.execute(" SELECT name " |
| 3430 |
|
" FROM tbl_energy_storage_containers " |
| 3431 |
|
" WHERE id = %s ", (id_,)) |
| 3432 |
|
if cursor.fetchone() is None: |
| 3433 |
|
cursor.close() |
| 3434 |
|
cnx.close() |
| 3435 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3436 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 3437 |
|
|
| 3438 |
|
query = (" SELECT id, name, uuid " |
| 3439 |
|
" FROM tbl_energy_storage_containers ") |
| 3440 |
|
cursor.execute(query) |
| 3441 |
|
rows_energystoragecontainers = cursor.fetchall() |
| 3442 |
|
|
| 3443 |
|
energy_storage_container_dict = dict() |
| 3444 |
|
if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
| 3445 |
|
for row in rows_energystoragecontainers: |
| 3446 |
|
energy_storage_container_dict[row[0]] = {"id": row[0], |
| 3447 |
|
"name": row[1], |
| 3448 |
|
"uuid": row[2]} |
| 3449 |
|
|
| 3450 |
|
query = (" SELECT id, name, uuid " |
| 3451 |
|
" FROM tbl_energy_storage_containers_hvacs " |
| 3452 |
|
" WHERE id = %s ") |
| 3453 |
|
cursor.execute(query, (hid,)) |
| 3454 |
|
row = cursor.fetchone() |
| 3455 |
|
cursor.close() |
| 3456 |
|
cnx.close() |
| 3457 |
|
|
| 3458 |
|
if row is None: |
| 3459 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3460 |
|
description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
| 3461 |
|
else: |
| 3462 |
|
meta_result = {"id": row[0], |
| 3463 |
|
"name": row[1], |
| 3464 |
|
"uuid": row[2] |
| 3465 |
|
} |
| 3466 |
|
|
| 3467 |
|
resp.text = json.dumps(meta_result) |
| 3468 |
|
|
| 3469 |
|
@staticmethod |
| 3470 |
|
@user_logger |
| 3471 |
|
def on_delete(req, resp, id_, hid): |
| 3472 |
|
admin_control(req) |
| 3473 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3474 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3475 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 3476 |
|
if not hid.isdigit() or int(hid) <= 0: |
| 3477 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3478 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
| 3479 |
|
|
| 3480 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3481 |
|
cursor = cnx.cursor() |
| 3482 |
|
|
| 3483 |
|
cursor.execute(" SELECT name " |
| 3484 |
|
" FROM tbl_energy_storage_containers " |
| 3485 |
|
" WHERE id = %s ", (id_,)) |
| 3486 |
|
if cursor.fetchone() is None: |
| 3487 |
|
cursor.close() |
| 3488 |
|
cnx.close() |
| 3489 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3490 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 3491 |
|
|
| 3492 |
|
cursor.execute(" SELECT name " |
| 3493 |
|
" FROM tbl_energy_storage_containers_hvacs " |
| 3494 |
|
" WHERE id = %s ", (hid,)) |
| 3495 |
|
if cursor.fetchone() is None: |
| 3496 |
|
cursor.close() |
| 3497 |
|
cnx.close() |
| 3498 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3499 |
|
description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
| 3500 |
|
|
| 3501 |
|
cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs " |
| 3502 |
|
" WHERE id = %s ", (hid,)) |
| 3503 |
|
cnx.commit() |
| 3504 |
|
|
| 3505 |
|
cursor.close() |
| 3506 |
|
cnx.close() |
| 3507 |
|
|
| 3508 |
|
resp.status = falcon.HTTP_204 |
| 3509 |
|
|
| 3510 |
|
@staticmethod |
| 3511 |
|
@user_logger |
| 3512 |
|
def on_put(req, resp, id_, hid): |
| 3513 |
|
"""Handles PUT requests""" |
| 3514 |
|
admin_control(req) |
| 3515 |
|
try: |
| 3516 |
|
raw_json = req.stream.read().decode('utf-8') |
| 3517 |
|
except Exception as ex: |
| 3518 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 3519 |
|
title='API.BAD_REQUEST', |
| 3520 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 3521 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3522 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3523 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 3524 |
|
|
| 3525 |
|
if not hid.isdigit() or int(hid) <= 0: |
| 3526 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3527 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID') |
| 3528 |
|
|
| 3529 |
|
new_values = json.loads(raw_json) |
| 3530 |
|
|
| 3531 |
|
if 'name' not in new_values['data'].keys() or \ |
| 3532 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 3533 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 3534 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3535 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME') |
| 3536 |
|
name = str.strip(new_values['data']['name']) |
| 3537 |
|
|
| 3538 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3539 |
|
cursor = cnx.cursor() |
| 3540 |
|
|
| 3541 |
|
cursor.execute(" SELECT name " |
| 3542 |
|
" FROM tbl_energy_storage_containers " |
| 3543 |
|
" WHERE id = %s ", (id_,)) |
| 3544 |
|
if cursor.fetchone() is None: |
| 3545 |
|
cursor.close() |
| 3546 |
|
cnx.close() |
| 3547 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3548 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 3549 |
|
|
| 3550 |
|
cursor.execute(" SELECT name " |
| 3551 |
|
" FROM tbl_energy_storage_containers_hvacs " |
| 3552 |
|
" WHERE id = %s ", (hid,)) |
| 3553 |
|
if cursor.fetchone() is None: |
| 3554 |
|
cursor.close() |
| 3555 |
|
cnx.close() |
| 3556 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3557 |
|
description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND') |
| 3558 |
|
|
| 3559 |
|
cursor.execute(" SELECT name " |
| 3560 |
|
" FROM tbl_energy_storage_containers_hvacs " |
| 3561 |
|
" WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
| 3562 |
|
(id_, name, hid)) |
| 3563 |
|
if cursor.fetchone() is not None: |
| 3564 |
|
cursor.close() |
| 3565 |
|
cnx.close() |
| 3566 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3567 |
|
description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE') |
| 3568 |
|
|
| 3569 |
|
update_row = (" UPDATE tbl_energy_storage_containers_hvacs " |
| 3570 |
|
" SET name = %s " |
| 3571 |
|
" WHERE id = %s ") |
| 3572 |
|
cursor.execute(update_row, (name, |
| 3573 |
|
hid)) |
| 3574 |
|
cnx.commit() |
| 3575 |
|
|
| 3576 |
|
cursor.close() |
| 3577 |
|
cnx.close() |
| 3578 |
|
|
| 3579 |
|
resp.status = falcon.HTTP_200 |
| 3580 |
|
|
| 3581 |
|
|
| 3582 |
|
class EnergyStorageContainerHVACPointCollection: |
|
@@ 2264-2436 (lines=173) @@
|
| 2261 |
|
resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id) |
| 2262 |
|
|
| 2263 |
|
|
| 2264 |
|
class EnergyStorageContainerFirecontrolItem: |
| 2265 |
|
def __init__(self): |
| 2266 |
|
"""Initializes Class""" |
| 2267 |
|
pass |
| 2268 |
|
|
| 2269 |
|
@staticmethod |
| 2270 |
|
def on_options(req, resp, id_, fid): |
| 2271 |
|
resp.status = falcon.HTTP_200 |
| 2272 |
|
|
| 2273 |
|
@staticmethod |
| 2274 |
|
def on_get(req, resp, id_, fid): |
| 2275 |
|
access_control(req) |
| 2276 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2277 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2278 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 2279 |
|
if not fid.isdigit() or int(fid) <= 0: |
| 2280 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2281 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
| 2282 |
|
|
| 2283 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2284 |
|
cursor = cnx.cursor() |
| 2285 |
|
|
| 2286 |
|
cursor.execute(" SELECT name " |
| 2287 |
|
" FROM tbl_energy_storage_containers " |
| 2288 |
|
" WHERE id = %s ", (id_,)) |
| 2289 |
|
if cursor.fetchone() is None: |
| 2290 |
|
cursor.close() |
| 2291 |
|
cnx.close() |
| 2292 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2293 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 2294 |
|
|
| 2295 |
|
query = (" SELECT id, name, uuid " |
| 2296 |
|
" FROM tbl_energy_storage_containers ") |
| 2297 |
|
cursor.execute(query) |
| 2298 |
|
rows_energystoragecontainers = cursor.fetchall() |
| 2299 |
|
|
| 2300 |
|
energy_storage_container_dict = dict() |
| 2301 |
|
if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0: |
| 2302 |
|
for row in rows_energystoragecontainers: |
| 2303 |
|
energy_storage_container_dict[row[0]] = {"id": row[0], |
| 2304 |
|
"name": row[1], |
| 2305 |
|
"uuid": row[2]} |
| 2306 |
|
|
| 2307 |
|
query = (" SELECT id, name, uuid " |
| 2308 |
|
" FROM tbl_energy_storage_containers_firecontrols " |
| 2309 |
|
" WHERE id = %s ") |
| 2310 |
|
cursor.execute(query, (fid,)) |
| 2311 |
|
row = cursor.fetchone() |
| 2312 |
|
cursor.close() |
| 2313 |
|
cnx.close() |
| 2314 |
|
|
| 2315 |
|
if row is None: |
| 2316 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2317 |
|
description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
| 2318 |
|
else: |
| 2319 |
|
meta_result = {"id": row[0], |
| 2320 |
|
"name": row[1], |
| 2321 |
|
"uuid": row[2] |
| 2322 |
|
} |
| 2323 |
|
|
| 2324 |
|
resp.text = json.dumps(meta_result) |
| 2325 |
|
|
| 2326 |
|
@staticmethod |
| 2327 |
|
@user_logger |
| 2328 |
|
def on_delete(req, resp, id_, fid): |
| 2329 |
|
admin_control(req) |
| 2330 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2331 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2332 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 2333 |
|
if not fid.isdigit() or int(fid) <= 0: |
| 2334 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2335 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
| 2336 |
|
|
| 2337 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2338 |
|
cursor = cnx.cursor() |
| 2339 |
|
|
| 2340 |
|
cursor.execute(" SELECT name " |
| 2341 |
|
" FROM tbl_energy_storage_containers " |
| 2342 |
|
" WHERE id = %s ", (id_,)) |
| 2343 |
|
if cursor.fetchone() is None: |
| 2344 |
|
cursor.close() |
| 2345 |
|
cnx.close() |
| 2346 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2347 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 2348 |
|
|
| 2349 |
|
cursor.execute(" SELECT name " |
| 2350 |
|
" FROM tbl_energy_storage_containers_firecontrols " |
| 2351 |
|
" WHERE id = %s ", (fid,)) |
| 2352 |
|
if cursor.fetchone() is None: |
| 2353 |
|
cursor.close() |
| 2354 |
|
cnx.close() |
| 2355 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2356 |
|
description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
| 2357 |
|
|
| 2358 |
|
cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols " |
| 2359 |
|
" WHERE id = %s ", (fid,)) |
| 2360 |
|
cnx.commit() |
| 2361 |
|
|
| 2362 |
|
cursor.close() |
| 2363 |
|
cnx.close() |
| 2364 |
|
|
| 2365 |
|
resp.status = falcon.HTTP_204 |
| 2366 |
|
|
| 2367 |
|
@staticmethod |
| 2368 |
|
@user_logger |
| 2369 |
|
def on_put(req, resp, id_, fid): |
| 2370 |
|
"""Handles PUT requests""" |
| 2371 |
|
admin_control(req) |
| 2372 |
|
try: |
| 2373 |
|
raw_json = req.stream.read().decode('utf-8') |
| 2374 |
|
except Exception as ex: |
| 2375 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 2376 |
|
title='API.BAD_REQUEST', |
| 2377 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 2378 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2379 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2380 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
| 2381 |
|
|
| 2382 |
|
if not fid.isdigit() or int(fid) <= 0: |
| 2383 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2384 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID') |
| 2385 |
|
|
| 2386 |
|
new_values = json.loads(raw_json) |
| 2387 |
|
|
| 2388 |
|
if 'name' not in new_values['data'].keys() or \ |
| 2389 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 2390 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 2391 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2392 |
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME') |
| 2393 |
|
name = str.strip(new_values['data']['name']) |
| 2394 |
|
|
| 2395 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2396 |
|
cursor = cnx.cursor() |
| 2397 |
|
|
| 2398 |
|
cursor.execute(" SELECT name " |
| 2399 |
|
" FROM tbl_energy_storage_containers " |
| 2400 |
|
" WHERE id = %s ", (id_,)) |
| 2401 |
|
if cursor.fetchone() is None: |
| 2402 |
|
cursor.close() |
| 2403 |
|
cnx.close() |
| 2404 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2405 |
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
| 2406 |
|
|
| 2407 |
|
cursor.execute(" SELECT name " |
| 2408 |
|
" FROM tbl_energy_storage_containers_firecontrols " |
| 2409 |
|
" WHERE id = %s ", (fid,)) |
| 2410 |
|
if cursor.fetchone() is None: |
| 2411 |
|
cursor.close() |
| 2412 |
|
cnx.close() |
| 2413 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2414 |
|
description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND') |
| 2415 |
|
|
| 2416 |
|
cursor.execute(" SELECT name " |
| 2417 |
|
" FROM tbl_energy_storage_containers_firecontrols " |
| 2418 |
|
" WHERE energy_storage_container_id = %s AND name = %s AND id != %s ", |
| 2419 |
|
(id_, name, fid)) |
| 2420 |
|
if cursor.fetchone() is not None: |
| 2421 |
|
cursor.close() |
| 2422 |
|
cnx.close() |
| 2423 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2424 |
|
description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE') |
| 2425 |
|
|
| 2426 |
|
update_row = (" UPDATE tbl_energy_storage_containers_firecontrols " |
| 2427 |
|
" SET name = %s " |
| 2428 |
|
" WHERE id = %s ") |
| 2429 |
|
cursor.execute(update_row, (name, |
| 2430 |
|
fid)) |
| 2431 |
|
cnx.commit() |
| 2432 |
|
|
| 2433 |
|
cursor.close() |
| 2434 |
|
cnx.close() |
| 2435 |
|
|
| 2436 |
|
resp.status = falcon.HTTP_200 |
| 2437 |
|
|
| 2438 |
|
|
| 2439 |
|
class EnergyStorageContainerFirecontrolPointCollection: |