@@ 1740-1877 (lines=138) @@ | ||
1737 | resp.status = falcon.HTTP_204 |
|
1738 | ||
1739 | ||
1740 | class EquipmentVirtualMeterCollection: |
|
1741 | def __init__(self): |
|
1742 | """Initializes EquipmentVirtualMeterCollection""" |
|
1743 | pass |
|
1744 | ||
1745 | @staticmethod |
|
1746 | def on_options(req, resp, id_): |
|
1747 | _ = req |
|
1748 | resp.status = falcon.HTTP_200 |
|
1749 | _ = id_ |
|
1750 | ||
1751 | @staticmethod |
|
1752 | def on_get(req, resp, id_): |
|
1753 | if 'API-KEY' not in req.headers or \ |
|
1754 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1755 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1756 | access_control(req) |
|
1757 | else: |
|
1758 | api_key_control(req) |
|
1759 | if not id_.isdigit() or int(id_) <= 0: |
|
1760 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1761 | description='API.INVALID_EQUIPMENT_ID') |
|
1762 | ||
1763 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1764 | cursor = cnx.cursor() |
|
1765 | ||
1766 | cursor.execute(" SELECT name " |
|
1767 | " FROM tbl_equipments " |
|
1768 | " WHERE id = %s ", (id_,)) |
|
1769 | if cursor.fetchone() is None: |
|
1770 | cursor.close() |
|
1771 | cnx.close() |
|
1772 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1773 | description='API.EQUIPMENT_NOT_FOUND') |
|
1774 | ||
1775 | query = (" SELECT id, name, uuid " |
|
1776 | " FROM tbl_energy_categories ") |
|
1777 | cursor.execute(query) |
|
1778 | rows_energy_categories = cursor.fetchall() |
|
1779 | ||
1780 | energy_category_dict = dict() |
|
1781 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1782 | for row in rows_energy_categories: |
|
1783 | energy_category_dict[row[0]] = {"id": row[0], |
|
1784 | "name": row[1], |
|
1785 | "uuid": row[2]} |
|
1786 | ||
1787 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1788 | " FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
|
1789 | " WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
|
1790 | " ORDER BY m.id ") |
|
1791 | cursor.execute(query, (id_,)) |
|
1792 | rows = cursor.fetchall() |
|
1793 | ||
1794 | result = list() |
|
1795 | if rows is not None and len(rows) > 0: |
|
1796 | for row in rows: |
|
1797 | meta_result = {"id": row[0], |
|
1798 | "name": row[1], |
|
1799 | "uuid": row[2], |
|
1800 | "energy_category": energy_category_dict.get(row[3], None), |
|
1801 | "is_output": bool(row[4])} |
|
1802 | result.append(meta_result) |
|
1803 | ||
1804 | resp.text = json.dumps(result) |
|
1805 | ||
1806 | @staticmethod |
|
1807 | @user_logger |
|
1808 | def on_post(req, resp, id_): |
|
1809 | """Handles POST requests""" |
|
1810 | admin_control(req) |
|
1811 | try: |
|
1812 | raw_json = req.stream.read().decode('utf-8') |
|
1813 | except Exception as ex: |
|
1814 | print(str(ex)) |
|
1815 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1816 | title='API.BAD_REQUEST', |
|
1817 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1818 | ||
1819 | if not id_.isdigit() or int(id_) <= 0: |
|
1820 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1821 | description='API.INVALID_EQUIPMENT_ID') |
|
1822 | ||
1823 | new_values = json.loads(raw_json) |
|
1824 | ||
1825 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1826 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1827 | new_values['data']['virtual_meter_id'] <= 0: |
|
1828 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1829 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1830 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1831 | ||
1832 | if 'is_output' not in new_values['data'].keys() or \ |
|
1833 | not isinstance(new_values['data']['is_output'], bool): |
|
1834 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1835 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1836 | is_output = new_values['data']['is_output'] |
|
1837 | ||
1838 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1839 | cursor = cnx.cursor() |
|
1840 | ||
1841 | cursor.execute(" SELECT name " |
|
1842 | " from tbl_equipments " |
|
1843 | " WHERE id = %s ", (id_,)) |
|
1844 | if cursor.fetchone() is None: |
|
1845 | cursor.close() |
|
1846 | cnx.close() |
|
1847 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1848 | description='API.EQUIPMENT_NOT_FOUND') |
|
1849 | ||
1850 | cursor.execute(" SELECT name " |
|
1851 | " FROM tbl_virtual_meters " |
|
1852 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1853 | if cursor.fetchone() is None: |
|
1854 | cursor.close() |
|
1855 | cnx.close() |
|
1856 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1857 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1858 | ||
1859 | query = (" SELECT id " |
|
1860 | " FROM tbl_equipments_virtual_meters " |
|
1861 | " WHERE equipment_id = %s AND virtual_meter_id = %s") |
|
1862 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1863 | if cursor.fetchone() is not None: |
|
1864 | cursor.close() |
|
1865 | cnx.close() |
|
1866 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1867 | description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
|
1868 | ||
1869 | add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
|
1870 | " VALUES (%s, %s, %s) ") |
|
1871 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
|
1872 | cnx.commit() |
|
1873 | cursor.close() |
|
1874 | cnx.close() |
|
1875 | ||
1876 | resp.status = falcon.HTTP_201 |
|
1877 | resp.location = '/equipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1878 | ||
1879 | ||
1880 | class EquipmentVirtualMeterItem: |
|
@@ 1536-1673 (lines=138) @@ | ||
1533 | resp.status = falcon.HTTP_204 |
|
1534 | ||
1535 | ||
1536 | class EquipmentOfflineMeterCollection: |
|
1537 | def __init__(self): |
|
1538 | """Initializes EquipmentOfflineMeterCollection""" |
|
1539 | pass |
|
1540 | ||
1541 | @staticmethod |
|
1542 | def on_options(req, resp, id_): |
|
1543 | _ = req |
|
1544 | resp.status = falcon.HTTP_200 |
|
1545 | _ = id_ |
|
1546 | ||
1547 | @staticmethod |
|
1548 | def on_get(req, resp, id_): |
|
1549 | if 'API-KEY' not in req.headers or \ |
|
1550 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1551 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1552 | access_control(req) |
|
1553 | else: |
|
1554 | api_key_control(req) |
|
1555 | if not id_.isdigit() or int(id_) <= 0: |
|
1556 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1557 | description='API.INVALID_EQUIPMENT_ID') |
|
1558 | ||
1559 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1560 | cursor = cnx.cursor() |
|
1561 | ||
1562 | cursor.execute(" SELECT name " |
|
1563 | " FROM tbl_equipments " |
|
1564 | " WHERE id = %s ", (id_,)) |
|
1565 | if cursor.fetchone() is None: |
|
1566 | cursor.close() |
|
1567 | cnx.close() |
|
1568 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1569 | description='API.EQUIPMENT_NOT_FOUND') |
|
1570 | ||
1571 | query = (" SELECT id, name, uuid " |
|
1572 | " FROM tbl_energy_categories ") |
|
1573 | cursor.execute(query) |
|
1574 | rows_energy_categories = cursor.fetchall() |
|
1575 | ||
1576 | energy_category_dict = dict() |
|
1577 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1578 | for row in rows_energy_categories: |
|
1579 | energy_category_dict[row[0]] = {"id": row[0], |
|
1580 | "name": row[1], |
|
1581 | "uuid": row[2]} |
|
1582 | ||
1583 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1584 | " FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
|
1585 | " WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
|
1586 | " ORDER BY m.id ") |
|
1587 | cursor.execute(query, (id_,)) |
|
1588 | rows = cursor.fetchall() |
|
1589 | ||
1590 | result = list() |
|
1591 | if rows is not None and len(rows) > 0: |
|
1592 | for row in rows: |
|
1593 | meta_result = {"id": row[0], |
|
1594 | "name": row[1], |
|
1595 | "uuid": row[2], |
|
1596 | "energy_category": energy_category_dict.get(row[3], None), |
|
1597 | "is_output": bool(row[4])} |
|
1598 | result.append(meta_result) |
|
1599 | ||
1600 | resp.text = json.dumps(result) |
|
1601 | ||
1602 | @staticmethod |
|
1603 | @user_logger |
|
1604 | def on_post(req, resp, id_): |
|
1605 | """Handles POST requests""" |
|
1606 | admin_control(req) |
|
1607 | try: |
|
1608 | raw_json = req.stream.read().decode('utf-8') |
|
1609 | except Exception as ex: |
|
1610 | print(str(ex)) |
|
1611 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1612 | title='API.BAD_REQUEST', |
|
1613 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1614 | ||
1615 | if not id_.isdigit() or int(id_) <= 0: |
|
1616 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1617 | description='API.INVALID_EQUIPMENT_ID') |
|
1618 | ||
1619 | new_values = json.loads(raw_json) |
|
1620 | ||
1621 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
1622 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
1623 | new_values['data']['offline_meter_id'] <= 0: |
|
1624 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1625 | description='API.INVALID_OFFLINE_METER_ID') |
|
1626 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
1627 | ||
1628 | if 'is_output' not in new_values['data'].keys() or \ |
|
1629 | not isinstance(new_values['data']['is_output'], bool): |
|
1630 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1631 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1632 | is_output = new_values['data']['is_output'] |
|
1633 | ||
1634 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1635 | cursor = cnx.cursor() |
|
1636 | ||
1637 | cursor.execute(" SELECT name " |
|
1638 | " from tbl_equipments " |
|
1639 | " WHERE id = %s ", (id_,)) |
|
1640 | if cursor.fetchone() is None: |
|
1641 | cursor.close() |
|
1642 | cnx.close() |
|
1643 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1644 | description='API.EQUIPMENT_NOT_FOUND') |
|
1645 | ||
1646 | cursor.execute(" SELECT name " |
|
1647 | " FROM tbl_offline_meters " |
|
1648 | " WHERE id = %s ", (offline_meter_id,)) |
|
1649 | if cursor.fetchone() is None: |
|
1650 | cursor.close() |
|
1651 | cnx.close() |
|
1652 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1653 | description='API.OFFLINE_METER_NOT_FOUND') |
|
1654 | ||
1655 | query = (" SELECT id " |
|
1656 | " FROM tbl_equipments_offline_meters " |
|
1657 | " WHERE equipment_id = %s AND offline_meter_id = %s") |
|
1658 | cursor.execute(query, (id_, offline_meter_id,)) |
|
1659 | if cursor.fetchone() is not None: |
|
1660 | cursor.close() |
|
1661 | cnx.close() |
|
1662 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1663 | description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
|
1664 | ||
1665 | add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
|
1666 | " VALUES (%s, %s, %s) ") |
|
1667 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
|
1668 | cnx.commit() |
|
1669 | cursor.close() |
|
1670 | cnx.close() |
|
1671 | ||
1672 | resp.status = falcon.HTTP_201 |
|
1673 | resp.location = '/equipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1674 | ||
1675 | ||
1676 | class EquipmentOfflineMeterItem: |
|
@@ 1333-1470 (lines=138) @@ | ||
1330 | resp.status = falcon.HTTP_200 |
|
1331 | ||
1332 | ||
1333 | class EquipmentMeterCollection: |
|
1334 | def __init__(self): |
|
1335 | """Initializes EquipmentMeterCollection""" |
|
1336 | pass |
|
1337 | ||
1338 | @staticmethod |
|
1339 | def on_options(req, resp, id_): |
|
1340 | _ = req |
|
1341 | resp.status = falcon.HTTP_200 |
|
1342 | _ = id_ |
|
1343 | ||
1344 | @staticmethod |
|
1345 | def on_get(req, resp, id_): |
|
1346 | if 'API-KEY' not in req.headers or \ |
|
1347 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1348 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1349 | access_control(req) |
|
1350 | else: |
|
1351 | api_key_control(req) |
|
1352 | if not id_.isdigit() or int(id_) <= 0: |
|
1353 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1354 | description='API.INVALID_EQUIPMENT_ID') |
|
1355 | ||
1356 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1357 | cursor = cnx.cursor() |
|
1358 | ||
1359 | cursor.execute(" SELECT name " |
|
1360 | " FROM tbl_equipments " |
|
1361 | " WHERE id = %s ", (id_,)) |
|
1362 | if cursor.fetchone() is None: |
|
1363 | cursor.close() |
|
1364 | cnx.close() |
|
1365 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1366 | description='API.EQUIPMENT_NOT_FOUND') |
|
1367 | ||
1368 | query = (" SELECT id, name, uuid " |
|
1369 | " FROM tbl_energy_categories ") |
|
1370 | cursor.execute(query) |
|
1371 | rows_energy_categories = cursor.fetchall() |
|
1372 | ||
1373 | energy_category_dict = dict() |
|
1374 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1375 | for row in rows_energy_categories: |
|
1376 | energy_category_dict[row[0]] = {"id": row[0], |
|
1377 | "name": row[1], |
|
1378 | "uuid": row[2]} |
|
1379 | ||
1380 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1381 | " FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
|
1382 | " WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
|
1383 | " ORDER BY m.id ") |
|
1384 | cursor.execute(query, (id_,)) |
|
1385 | rows = cursor.fetchall() |
|
1386 | ||
1387 | result = list() |
|
1388 | if rows is not None and len(rows) > 0: |
|
1389 | for row in rows: |
|
1390 | meta_result = {"id": row[0], |
|
1391 | "name": row[1], |
|
1392 | "uuid": row[2], |
|
1393 | "energy_category": energy_category_dict.get(row[3], None), |
|
1394 | "is_output": bool(row[4])} |
|
1395 | result.append(meta_result) |
|
1396 | ||
1397 | resp.text = json.dumps(result) |
|
1398 | ||
1399 | @staticmethod |
|
1400 | @user_logger |
|
1401 | def on_post(req, resp, id_): |
|
1402 | """Handles POST requests""" |
|
1403 | admin_control(req) |
|
1404 | try: |
|
1405 | raw_json = req.stream.read().decode('utf-8') |
|
1406 | except Exception as ex: |
|
1407 | print(str(ex)) |
|
1408 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1409 | title='API.BAD_REQUEST', |
|
1410 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1411 | ||
1412 | if not id_.isdigit() or int(id_) <= 0: |
|
1413 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1414 | description='API.INVALID_EQUIPMENT_ID') |
|
1415 | ||
1416 | new_values = json.loads(raw_json) |
|
1417 | ||
1418 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1419 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1420 | new_values['data']['meter_id'] <= 0: |
|
1421 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1422 | description='API.INVALID_METER_ID') |
|
1423 | meter_id = new_values['data']['meter_id'] |
|
1424 | ||
1425 | if 'is_output' not in new_values['data'].keys() or \ |
|
1426 | not isinstance(new_values['data']['is_output'], bool): |
|
1427 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1428 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1429 | is_output = new_values['data']['is_output'] |
|
1430 | ||
1431 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1432 | cursor = cnx.cursor() |
|
1433 | ||
1434 | cursor.execute(" SELECT name " |
|
1435 | " from tbl_equipments " |
|
1436 | " WHERE id = %s ", (id_,)) |
|
1437 | if cursor.fetchone() is None: |
|
1438 | cursor.close() |
|
1439 | cnx.close() |
|
1440 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1441 | description='API.EQUIPMENT_NOT_FOUND') |
|
1442 | ||
1443 | cursor.execute(" SELECT name " |
|
1444 | " FROM tbl_meters " |
|
1445 | " WHERE id = %s ", (meter_id,)) |
|
1446 | if cursor.fetchone() is None: |
|
1447 | cursor.close() |
|
1448 | cnx.close() |
|
1449 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1450 | description='API.METER_NOT_FOUND') |
|
1451 | ||
1452 | query = (" SELECT id " |
|
1453 | " FROM tbl_equipments_meters " |
|
1454 | " WHERE equipment_id = %s AND meter_id = %s") |
|
1455 | cursor.execute(query, (id_, meter_id,)) |
|
1456 | if cursor.fetchone() is not None: |
|
1457 | cursor.close() |
|
1458 | cnx.close() |
|
1459 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1460 | description='API.EQUIPMENT_METER_RELATION_EXISTS') |
|
1461 | ||
1462 | add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
|
1463 | " VALUES (%s, %s, %s) ") |
|
1464 | cursor.execute(add_row, (id_, meter_id, is_output)) |
|
1465 | cnx.commit() |
|
1466 | cursor.close() |
|
1467 | cnx.close() |
|
1468 | ||
1469 | resp.status = falcon.HTTP_201 |
|
1470 | resp.location = '/equipments/' + str(id_) + '/meters/' + str(meter_id) |
|
1471 | ||
1472 | ||
1473 | class EquipmentMeterItem: |
@@ 1720-1858 (lines=139) @@ | ||
1717 | resp.status = falcon.HTTP_204 |
|
1718 | ||
1719 | ||
1720 | class CombinedEquipmentOfflineMeterCollection: |
|
1721 | def __init__(self): |
|
1722 | """"Initializes CombinedEquipmentOfflineMeterCollection""" |
|
1723 | pass |
|
1724 | ||
1725 | @staticmethod |
|
1726 | def on_options(req, resp, id_): |
|
1727 | _ = req |
|
1728 | resp.status = falcon.HTTP_200 |
|
1729 | _ = id_ |
|
1730 | ||
1731 | @staticmethod |
|
1732 | def on_get(req, resp, id_): |
|
1733 | if 'API-KEY' not in req.headers or \ |
|
1734 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1735 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1736 | access_control(req) |
|
1737 | else: |
|
1738 | api_key_control(req) |
|
1739 | if not id_.isdigit() or int(id_) <= 0: |
|
1740 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1741 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1742 | ||
1743 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1744 | cursor = cnx.cursor() |
|
1745 | ||
1746 | cursor.execute(" SELECT name " |
|
1747 | " FROM tbl_combined_equipments " |
|
1748 | " WHERE id = %s ", (id_,)) |
|
1749 | if cursor.fetchone() is None: |
|
1750 | cursor.close() |
|
1751 | cnx.close() |
|
1752 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1753 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1754 | ||
1755 | query = (" SELECT id, name, uuid " |
|
1756 | " FROM tbl_energy_categories ") |
|
1757 | cursor.execute(query) |
|
1758 | rows_energy_categories = cursor.fetchall() |
|
1759 | ||
1760 | energy_category_dict = dict() |
|
1761 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1762 | for row in rows_energy_categories: |
|
1763 | energy_category_dict[row[0]] = {"id": row[0], |
|
1764 | "name": row[1], |
|
1765 | "uuid": row[2]} |
|
1766 | ||
1767 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1768 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
|
1769 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
|
1770 | " ORDER BY m.id ") |
|
1771 | cursor.execute(query, (id_,)) |
|
1772 | rows = cursor.fetchall() |
|
1773 | ||
1774 | result = list() |
|
1775 | if rows is not None and len(rows) > 0: |
|
1776 | for row in rows: |
|
1777 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1778 | "energy_category": energy_category_dict.get(row[3], None), |
|
1779 | "is_output": bool(row[4])} |
|
1780 | result.append(meta_result) |
|
1781 | ||
1782 | cursor.close() |
|
1783 | cnx.close() |
|
1784 | ||
1785 | resp.text = json.dumps(result) |
|
1786 | ||
1787 | @staticmethod |
|
1788 | @user_logger |
|
1789 | def on_post(req, resp, id_): |
|
1790 | """Handles POST requests""" |
|
1791 | admin_control(req) |
|
1792 | try: |
|
1793 | raw_json = req.stream.read().decode('utf-8') |
|
1794 | except Exception as ex: |
|
1795 | print(ex) |
|
1796 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1797 | title='API.BAD_REQUEST', |
|
1798 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1799 | ||
1800 | if not id_.isdigit() or int(id_) <= 0: |
|
1801 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1802 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1803 | ||
1804 | new_values = json.loads(raw_json) |
|
1805 | ||
1806 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
1807 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
1808 | new_values['data']['offline_meter_id'] <= 0: |
|
1809 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1810 | description='API.INVALID_OFFLINE_METER_ID') |
|
1811 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
1812 | ||
1813 | if 'is_output' not in new_values['data'].keys() or \ |
|
1814 | not isinstance(new_values['data']['is_output'], bool): |
|
1815 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1816 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1817 | is_output = new_values['data']['is_output'] |
|
1818 | ||
1819 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1820 | cursor = cnx.cursor() |
|
1821 | ||
1822 | cursor.execute(" SELECT name " |
|
1823 | " from tbl_combined_equipments " |
|
1824 | " WHERE id = %s ", (id_,)) |
|
1825 | if cursor.fetchone() is None: |
|
1826 | cursor.close() |
|
1827 | cnx.close() |
|
1828 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1829 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1830 | ||
1831 | cursor.execute(" SELECT name " |
|
1832 | " FROM tbl_offline_meters " |
|
1833 | " WHERE id = %s ", (offline_meter_id,)) |
|
1834 | if cursor.fetchone() is None: |
|
1835 | cursor.close() |
|
1836 | cnx.close() |
|
1837 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1838 | description='API.OFFLINE_METER_NOT_FOUND') |
|
1839 | ||
1840 | query = (" SELECT id " |
|
1841 | " FROM tbl_combined_equipments_offline_meters " |
|
1842 | " WHERE combined_equipment_id = %s AND offline_meter_id = %s") |
|
1843 | cursor.execute(query, (id_, offline_meter_id,)) |
|
1844 | if cursor.fetchone() is not None: |
|
1845 | cursor.close() |
|
1846 | cnx.close() |
|
1847 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1848 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') |
|
1849 | ||
1850 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
|
1851 | " (combined_equipment_id, offline_meter_id, is_output ) " |
|
1852 | " VALUES (%s, %s, %s) ") |
|
1853 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
|
1854 | cnx.commit() |
|
1855 | cursor.close() |
|
1856 | cnx.close() |
|
1857 | ||
1858 | resp.status = falcon.HTTP_201 |
|
1859 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1860 | ||
1861 | ||
@@ 1925-2062 (lines=138) @@ | ||
1922 | resp.status = falcon.HTTP_204 |
|
1923 | ||
1924 | ||
1925 | class CombinedEquipmentVirtualMeterCollection: |
|
1926 | def __init__(self): |
|
1927 | """"Initializes CombinedEquipmentVirtualMeterCollection""" |
|
1928 | pass |
|
1929 | ||
1930 | @staticmethod |
|
1931 | def on_options(req, resp, id_): |
|
1932 | _ = req |
|
1933 | resp.status = falcon.HTTP_200 |
|
1934 | _ = id_ |
|
1935 | ||
1936 | @staticmethod |
|
1937 | def on_get(req, resp, id_): |
|
1938 | if 'API-KEY' not in req.headers or \ |
|
1939 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1940 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1941 | access_control(req) |
|
1942 | else: |
|
1943 | api_key_control(req) |
|
1944 | if not id_.isdigit() or int(id_) <= 0: |
|
1945 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1946 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1947 | ||
1948 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1949 | cursor = cnx.cursor() |
|
1950 | ||
1951 | cursor.execute(" SELECT name " |
|
1952 | " FROM tbl_combined_equipments " |
|
1953 | " WHERE id = %s ", (id_,)) |
|
1954 | if cursor.fetchone() is None: |
|
1955 | cursor.close() |
|
1956 | cnx.close() |
|
1957 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1958 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1959 | ||
1960 | query = (" SELECT id, name, uuid " |
|
1961 | " FROM tbl_energy_categories ") |
|
1962 | cursor.execute(query) |
|
1963 | rows_energy_categories = cursor.fetchall() |
|
1964 | ||
1965 | energy_category_dict = dict() |
|
1966 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1967 | for row in rows_energy_categories: |
|
1968 | energy_category_dict[row[0]] = {"id": row[0], |
|
1969 | "name": row[1], |
|
1970 | "uuid": row[2]} |
|
1971 | ||
1972 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1973 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
|
1974 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
|
1975 | " ORDER BY m.id ") |
|
1976 | cursor.execute(query, (id_,)) |
|
1977 | rows = cursor.fetchall() |
|
1978 | ||
1979 | result = list() |
|
1980 | if rows is not None and len(rows) > 0: |
|
1981 | for row in rows: |
|
1982 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1983 | "energy_category": energy_category_dict.get(row[3], None), |
|
1984 | "is_output": bool(row[4])} |
|
1985 | result.append(meta_result) |
|
1986 | ||
1987 | cursor.close() |
|
1988 | cnx.close() |
|
1989 | resp.text = json.dumps(result) |
|
1990 | ||
1991 | @staticmethod |
|
1992 | @user_logger |
|
1993 | def on_post(req, resp, id_): |
|
1994 | """Handles POST requests""" |
|
1995 | admin_control(req) |
|
1996 | try: |
|
1997 | raw_json = req.stream.read().decode('utf-8') |
|
1998 | except Exception as ex: |
|
1999 | print(ex) |
|
2000 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
2001 | title='API.BAD_REQUEST', |
|
2002 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
2003 | ||
2004 | if not id_.isdigit() or int(id_) <= 0: |
|
2005 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2006 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
2007 | ||
2008 | new_values = json.loads(raw_json) |
|
2009 | ||
2010 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
2011 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
2012 | new_values['data']['virtual_meter_id'] <= 0: |
|
2013 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2014 | description='API.INVALID_VIRTUAL_METER_ID') |
|
2015 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
2016 | ||
2017 | if 'is_output' not in new_values['data'].keys() or \ |
|
2018 | not isinstance(new_values['data']['is_output'], bool): |
|
2019 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
2020 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
2021 | is_output = new_values['data']['is_output'] |
|
2022 | ||
2023 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
2024 | cursor = cnx.cursor() |
|
2025 | ||
2026 | cursor.execute(" SELECT name " |
|
2027 | " from tbl_combined_equipments " |
|
2028 | " WHERE id = %s ", (id_,)) |
|
2029 | if cursor.fetchone() is None: |
|
2030 | cursor.close() |
|
2031 | cnx.close() |
|
2032 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2033 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
2034 | ||
2035 | cursor.execute(" SELECT name " |
|
2036 | " FROM tbl_virtual_meters " |
|
2037 | " WHERE id = %s ", (virtual_meter_id,)) |
|
2038 | if cursor.fetchone() is None: |
|
2039 | cursor.close() |
|
2040 | cnx.close() |
|
2041 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
2042 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
2043 | ||
2044 | query = (" SELECT id " |
|
2045 | " FROM tbl_combined_equipments_virtual_meters " |
|
2046 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
|
2047 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
2048 | if cursor.fetchone() is not None: |
|
2049 | cursor.close() |
|
2050 | cnx.close() |
|
2051 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
2052 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') |
|
2053 | ||
2054 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
|
2055 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
|
2056 | " VALUES (%s, %s, %s) ") |
|
2057 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
|
2058 | cnx.commit() |
|
2059 | cursor.close() |
|
2060 | cnx.close() |
|
2061 | ||
2062 | resp.status = falcon.HTTP_201 |
|
2063 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
2064 | ||
2065 | ||
@@ 1516-1653 (lines=138) @@ | ||
1513 | resp.status = falcon.HTTP_200 |
|
1514 | ||
1515 | ||
1516 | class CombinedEquipmentMeterCollection: |
|
1517 | def __init__(self): |
|
1518 | """"Initializes CombinedEquipmentMeterCollection""" |
|
1519 | pass |
|
1520 | ||
1521 | @staticmethod |
|
1522 | def on_options(req, resp, id_): |
|
1523 | _ = req |
|
1524 | resp.status = falcon.HTTP_200 |
|
1525 | _ = id_ |
|
1526 | ||
1527 | @staticmethod |
|
1528 | def on_get(req, resp, id_): |
|
1529 | if 'API-KEY' not in req.headers or \ |
|
1530 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1531 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1532 | access_control(req) |
|
1533 | else: |
|
1534 | api_key_control(req) |
|
1535 | if not id_.isdigit() or int(id_) <= 0: |
|
1536 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1537 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1538 | ||
1539 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1540 | cursor = cnx.cursor() |
|
1541 | ||
1542 | cursor.execute(" SELECT name " |
|
1543 | " FROM tbl_combined_equipments " |
|
1544 | " WHERE id = %s ", (id_,)) |
|
1545 | if cursor.fetchone() is None: |
|
1546 | cursor.close() |
|
1547 | cnx.close() |
|
1548 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1549 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1550 | ||
1551 | query = (" SELECT id, name, uuid " |
|
1552 | " FROM tbl_energy_categories ") |
|
1553 | cursor.execute(query) |
|
1554 | rows_energy_categories = cursor.fetchall() |
|
1555 | ||
1556 | energy_category_dict = dict() |
|
1557 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1558 | for row in rows_energy_categories: |
|
1559 | energy_category_dict[row[0]] = {"id": row[0], |
|
1560 | "name": row[1], |
|
1561 | "uuid": row[2]} |
|
1562 | ||
1563 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1564 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
|
1565 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
|
1566 | " ORDER BY m.id ") |
|
1567 | cursor.execute(query, (id_,)) |
|
1568 | rows = cursor.fetchall() |
|
1569 | ||
1570 | result = list() |
|
1571 | if rows is not None and len(rows) > 0: |
|
1572 | for row in rows: |
|
1573 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
1574 | "energy_category": energy_category_dict.get(row[3], None), |
|
1575 | "is_output": bool(row[4])} |
|
1576 | result.append(meta_result) |
|
1577 | ||
1578 | cursor.close() |
|
1579 | cnx.close() |
|
1580 | ||
1581 | resp.text = json.dumps(result) |
|
1582 | ||
1583 | @staticmethod |
|
1584 | @user_logger |
|
1585 | def on_post(req, resp, id_): |
|
1586 | """Handles POST requests""" |
|
1587 | admin_control(req) |
|
1588 | try: |
|
1589 | raw_json = req.stream.read().decode('utf-8') |
|
1590 | except Exception as ex: |
|
1591 | print(ex) |
|
1592 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1593 | title='API.BAD_REQUEST', |
|
1594 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1595 | ||
1596 | if not id_.isdigit() or int(id_) <= 0: |
|
1597 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1598 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1599 | ||
1600 | new_values = json.loads(raw_json) |
|
1601 | ||
1602 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1603 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1604 | new_values['data']['meter_id'] <= 0: |
|
1605 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1606 | description='API.INVALID_METER_ID') |
|
1607 | meter_id = new_values['data']['meter_id'] |
|
1608 | ||
1609 | if 'is_output' not in new_values['data'].keys() or \ |
|
1610 | not isinstance(new_values['data']['is_output'], bool): |
|
1611 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1612 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1613 | is_output = new_values['data']['is_output'] |
|
1614 | ||
1615 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1616 | cursor = cnx.cursor() |
|
1617 | ||
1618 | cursor.execute(" SELECT name " |
|
1619 | " from tbl_combined_equipments " |
|
1620 | " WHERE id = %s ", (id_,)) |
|
1621 | if cursor.fetchone() is None: |
|
1622 | cursor.close() |
|
1623 | cnx.close() |
|
1624 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1625 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1626 | ||
1627 | cursor.execute(" SELECT name " |
|
1628 | " FROM tbl_meters " |
|
1629 | " WHERE id = %s ", (meter_id,)) |
|
1630 | if cursor.fetchone() is None: |
|
1631 | cursor.close() |
|
1632 | cnx.close() |
|
1633 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1634 | description='API.METER_NOT_FOUND') |
|
1635 | ||
1636 | query = (" SELECT id " |
|
1637 | " FROM tbl_combined_equipments_meters " |
|
1638 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
|
1639 | cursor.execute(query, (id_, meter_id,)) |
|
1640 | if cursor.fetchone() is not None: |
|
1641 | cursor.close() |
|
1642 | cnx.close() |
|
1643 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
1644 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTS') |
|
1645 | ||
1646 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
|
1647 | " VALUES (%s, %s, %s) ") |
|
1648 | cursor.execute(add_row, (id_, meter_id, is_output)) |
|
1649 | cnx.commit() |
|
1650 | cursor.close() |
|
1651 | cnx.close() |
|
1652 | ||
1653 | resp.status = falcon.HTTP_201 |
|
1654 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
|
1655 | ||
1656 |