@@ 1737-1862 (lines=126) @@ | ||
1734 | resp.status = falcon.HTTP_204 |
|
1735 | ||
1736 | ||
1737 | class CombinedEquipmentVirtualMeterCollection: |
|
1738 | @staticmethod |
|
1739 | def __init__(): |
|
1740 | pass |
|
1741 | ||
1742 | @staticmethod |
|
1743 | def on_options(req, resp, id_): |
|
1744 | resp.status = falcon.HTTP_200 |
|
1745 | ||
1746 | @staticmethod |
|
1747 | def on_get(req, resp, id_): |
|
1748 | if not id_.isdigit() or int(id_) <= 0: |
|
1749 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1750 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1751 | ||
1752 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1753 | cursor = cnx.cursor(dictionary=True) |
|
1754 | ||
1755 | cursor.execute(" SELECT name " |
|
1756 | " FROM tbl_combined_equipments " |
|
1757 | " WHERE id = %s ", (id_,)) |
|
1758 | if cursor.fetchone() is None: |
|
1759 | cursor.close() |
|
1760 | cnx.disconnect() |
|
1761 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1762 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1763 | ||
1764 | query = (" SELECT id, name, uuid " |
|
1765 | " FROM tbl_energy_categories ") |
|
1766 | cursor.execute(query) |
|
1767 | rows_energy_categories = cursor.fetchall() |
|
1768 | ||
1769 | energy_category_dict = dict() |
|
1770 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1771 | for row in rows_energy_categories: |
|
1772 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1773 | "name": row['name'], |
|
1774 | "uuid": row['uuid']} |
|
1775 | ||
1776 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1777 | " FROM tbl_combined_equipments e, tbl_combined_equipments_virtual_meters em, tbl_virtual_meters m " |
|
1778 | " WHERE em.combined_equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
|
1779 | " ORDER BY m.id ") |
|
1780 | cursor.execute(query, (id_,)) |
|
1781 | rows = cursor.fetchall() |
|
1782 | ||
1783 | result = list() |
|
1784 | if rows is not None and len(rows) > 0: |
|
1785 | for row in rows: |
|
1786 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1787 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1788 | "energy_category": energy_category, |
|
1789 | "is_output": bool(row['is_output'])} |
|
1790 | result.append(meta_result) |
|
1791 | ||
1792 | resp.body = json.dumps(result) |
|
1793 | ||
1794 | @staticmethod |
|
1795 | def on_post(req, resp, id_): |
|
1796 | """Handles POST requests""" |
|
1797 | try: |
|
1798 | raw_json = req.stream.read().decode('utf-8') |
|
1799 | except Exception as ex: |
|
1800 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1801 | ||
1802 | if not id_.isdigit() or int(id_) <= 0: |
|
1803 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1804 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1805 | ||
1806 | new_values = json.loads(raw_json, encoding='utf-8') |
|
1807 | ||
1808 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1809 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1810 | new_values['data']['virtual_meter_id'] <= 0: |
|
1811 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1812 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1813 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1814 | ||
1815 | if 'is_output' not in new_values['data'].keys() or \ |
|
1816 | not isinstance(new_values['data']['is_output'], bool): |
|
1817 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1818 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1819 | is_output = new_values['data']['is_output'] |
|
1820 | ||
1821 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1822 | cursor = cnx.cursor() |
|
1823 | ||
1824 | cursor.execute(" SELECT name " |
|
1825 | " from tbl_combined_equipments " |
|
1826 | " WHERE id = %s ", (id_,)) |
|
1827 | if cursor.fetchone() is None: |
|
1828 | cursor.close() |
|
1829 | cnx.disconnect() |
|
1830 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1831 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1832 | ||
1833 | cursor.execute(" SELECT name " |
|
1834 | " FROM tbl_virtual_meters " |
|
1835 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1836 | if cursor.fetchone() is None: |
|
1837 | cursor.close() |
|
1838 | cnx.disconnect() |
|
1839 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1840 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1841 | ||
1842 | query = (" SELECT id " |
|
1843 | " FROM tbl_combined_equipments_virtual_meters " |
|
1844 | " WHERE combined_equipment_id = %s AND virtual_meter_id = %s") |
|
1845 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1846 | if cursor.fetchone() is not None: |
|
1847 | cursor.close() |
|
1848 | cnx.disconnect() |
|
1849 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1850 | description='API.COMBINED_EQUIPMENT_VIRTUAL_METER_RELATION_EXISTED') |
|
1851 | ||
1852 | add_row = (" INSERT INTO tbl_combined_equipments_virtual_meters " |
|
1853 | " (combined_equipment_id, virtual_meter_id, is_output ) " |
|
1854 | " VALUES (%s, %s, %s) ") |
|
1855 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
|
1856 | new_id = cursor.lastrowid |
|
1857 | cnx.commit() |
|
1858 | cursor.close() |
|
1859 | cnx.disconnect() |
|
1860 | ||
1861 | resp.status = falcon.HTTP_201 |
|
1862 | resp.location = '/combinedequipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1863 | ||
1864 | ||
1865 | class CombinedEquipmentVirtualMeterItem: |
|
@@ 1550-1675 (lines=126) @@ | ||
1547 | resp.status = falcon.HTTP_204 |
|
1548 | ||
1549 | ||
1550 | class CombinedEquipmentOfflineMeterCollection: |
|
1551 | @staticmethod |
|
1552 | def __init__(): |
|
1553 | pass |
|
1554 | ||
1555 | @staticmethod |
|
1556 | def on_options(req, resp, id_): |
|
1557 | resp.status = falcon.HTTP_200 |
|
1558 | ||
1559 | @staticmethod |
|
1560 | def on_get(req, resp, id_): |
|
1561 | if not id_.isdigit() or int(id_) <= 0: |
|
1562 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1563 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1564 | ||
1565 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1566 | cursor = cnx.cursor(dictionary=True) |
|
1567 | ||
1568 | cursor.execute(" SELECT name " |
|
1569 | " FROM tbl_combined_equipments " |
|
1570 | " WHERE id = %s ", (id_,)) |
|
1571 | if cursor.fetchone() is None: |
|
1572 | cursor.close() |
|
1573 | cnx.disconnect() |
|
1574 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1575 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1576 | ||
1577 | query = (" SELECT id, name, uuid " |
|
1578 | " FROM tbl_energy_categories ") |
|
1579 | cursor.execute(query) |
|
1580 | rows_energy_categories = cursor.fetchall() |
|
1581 | ||
1582 | energy_category_dict = dict() |
|
1583 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1584 | for row in rows_energy_categories: |
|
1585 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1586 | "name": row['name'], |
|
1587 | "uuid": row['uuid']} |
|
1588 | ||
1589 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1590 | " FROM tbl_combined_equipments e, tbl_combined_equipments_offline_meters em, tbl_offline_meters m " |
|
1591 | " WHERE em.combined_equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
|
1592 | " ORDER BY m.id ") |
|
1593 | cursor.execute(query, (id_,)) |
|
1594 | rows = cursor.fetchall() |
|
1595 | ||
1596 | result = list() |
|
1597 | if rows is not None and len(rows) > 0: |
|
1598 | for row in rows: |
|
1599 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1600 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1601 | "energy_category": energy_category, |
|
1602 | "is_output": bool(row['is_output'])} |
|
1603 | result.append(meta_result) |
|
1604 | ||
1605 | resp.body = json.dumps(result) |
|
1606 | ||
1607 | @staticmethod |
|
1608 | def on_post(req, resp, id_): |
|
1609 | """Handles POST requests""" |
|
1610 | try: |
|
1611 | raw_json = req.stream.read().decode('utf-8') |
|
1612 | except Exception as ex: |
|
1613 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1614 | ||
1615 | if not id_.isdigit() or int(id_) <= 0: |
|
1616 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1617 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1618 | ||
1619 | new_values = json.loads(raw_json, encoding='utf-8') |
|
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(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(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_combined_equipments " |
|
1639 | " WHERE id = %s ", (id_,)) |
|
1640 | if cursor.fetchone() is None: |
|
1641 | cursor.close() |
|
1642 | cnx.disconnect() |
|
1643 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1644 | description='API.COMBINED_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.disconnect() |
|
1652 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1653 | description='API.OFFLINE_METER_NOT_FOUND') |
|
1654 | ||
1655 | query = (" SELECT id " |
|
1656 | " FROM tbl_combined_equipments_offline_meters " |
|
1657 | " WHERE combined_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.disconnect() |
|
1662 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1663 | description='API.COMBINED_EQUIPMENT_OFFLINE_METER_RELATION_EXISTED') |
|
1664 | ||
1665 | add_row = (" INSERT INTO tbl_combined_equipments_offline_meters " |
|
1666 | " (combined_equipment_id, offline_meter_id, is_output ) " |
|
1667 | " VALUES (%s, %s, %s) ") |
|
1668 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
|
1669 | new_id = cursor.lastrowid |
|
1670 | cnx.commit() |
|
1671 | cursor.close() |
|
1672 | cnx.disconnect() |
|
1673 | ||
1674 | resp.status = falcon.HTTP_201 |
|
1675 | resp.location = '/combinedequipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1676 | ||
1677 | ||
1678 | class CombinedEquipmentOfflineMeterItem: |
|
@@ 1364-1488 (lines=125) @@ | ||
1361 | resp.status = falcon.HTTP_200 |
|
1362 | ||
1363 | ||
1364 | class CombinedEquipmentMeterCollection: |
|
1365 | @staticmethod |
|
1366 | def __init__(): |
|
1367 | pass |
|
1368 | ||
1369 | @staticmethod |
|
1370 | def on_options(req, resp, id_): |
|
1371 | resp.status = falcon.HTTP_200 |
|
1372 | ||
1373 | @staticmethod |
|
1374 | def on_get(req, resp, id_): |
|
1375 | if not id_.isdigit() or int(id_) <= 0: |
|
1376 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1377 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1378 | ||
1379 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1380 | cursor = cnx.cursor(dictionary=True) |
|
1381 | ||
1382 | cursor.execute(" SELECT name " |
|
1383 | " FROM tbl_combined_equipments " |
|
1384 | " WHERE id = %s ", (id_,)) |
|
1385 | if cursor.fetchone() is None: |
|
1386 | cursor.close() |
|
1387 | cnx.disconnect() |
|
1388 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1389 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1390 | ||
1391 | query = (" SELECT id, name, uuid " |
|
1392 | " FROM tbl_energy_categories ") |
|
1393 | cursor.execute(query) |
|
1394 | rows_energy_categories = cursor.fetchall() |
|
1395 | ||
1396 | energy_category_dict = dict() |
|
1397 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1398 | for row in rows_energy_categories: |
|
1399 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1400 | "name": row['name'], |
|
1401 | "uuid": row['uuid']} |
|
1402 | ||
1403 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1404 | " FROM tbl_combined_equipments e, tbl_combined_equipments_meters em, tbl_meters m " |
|
1405 | " WHERE em.combined_equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
|
1406 | " ORDER BY m.id ") |
|
1407 | cursor.execute(query, (id_,)) |
|
1408 | rows = cursor.fetchall() |
|
1409 | ||
1410 | result = list() |
|
1411 | if rows is not None and len(rows) > 0: |
|
1412 | for row in rows: |
|
1413 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1414 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1415 | "energy_category": energy_category, |
|
1416 | "is_output": bool(row['is_output'])} |
|
1417 | result.append(meta_result) |
|
1418 | ||
1419 | resp.body = json.dumps(result) |
|
1420 | ||
1421 | @staticmethod |
|
1422 | def on_post(req, resp, id_): |
|
1423 | """Handles POST requests""" |
|
1424 | try: |
|
1425 | raw_json = req.stream.read().decode('utf-8') |
|
1426 | except Exception as ex: |
|
1427 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1428 | ||
1429 | if not id_.isdigit() or int(id_) <= 0: |
|
1430 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1431 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1432 | ||
1433 | new_values = json.loads(raw_json, encoding='utf-8') |
|
1434 | ||
1435 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1436 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1437 | new_values['data']['meter_id'] <= 0: |
|
1438 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1439 | description='API.INVALID_METER_ID') |
|
1440 | meter_id = new_values['data']['meter_id'] |
|
1441 | ||
1442 | if 'is_output' not in new_values['data'].keys() or \ |
|
1443 | not isinstance(new_values['data']['is_output'], bool): |
|
1444 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1445 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1446 | is_output = new_values['data']['is_output'] |
|
1447 | ||
1448 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1449 | cursor = cnx.cursor() |
|
1450 | ||
1451 | cursor.execute(" SELECT name " |
|
1452 | " from tbl_combined_equipments " |
|
1453 | " WHERE id = %s ", (id_,)) |
|
1454 | if cursor.fetchone() is None: |
|
1455 | cursor.close() |
|
1456 | cnx.disconnect() |
|
1457 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1458 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1459 | ||
1460 | cursor.execute(" SELECT name " |
|
1461 | " FROM tbl_meters " |
|
1462 | " WHERE id = %s ", (meter_id,)) |
|
1463 | if cursor.fetchone() is None: |
|
1464 | cursor.close() |
|
1465 | cnx.disconnect() |
|
1466 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1467 | description='API.METER_NOT_FOUND') |
|
1468 | ||
1469 | query = (" SELECT id " |
|
1470 | " FROM tbl_combined_equipments_meters " |
|
1471 | " WHERE combined_equipment_id = %s AND meter_id = %s") |
|
1472 | cursor.execute(query, (id_, meter_id,)) |
|
1473 | if cursor.fetchone() is not None: |
|
1474 | cursor.close() |
|
1475 | cnx.disconnect() |
|
1476 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1477 | description='API.COMBINED_EQUIPMENT_METER_RELATION_EXISTED') |
|
1478 | ||
1479 | add_row = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output ) " |
|
1480 | " VALUES (%s, %s, %s) ") |
|
1481 | cursor.execute(add_row, (id_, meter_id, is_output)) |
|
1482 | new_id = cursor.lastrowid |
|
1483 | cnx.commit() |
|
1484 | cursor.close() |
|
1485 | cnx.disconnect() |
|
1486 | ||
1487 | resp.status = falcon.HTTP_201 |
|
1488 | resp.location = '/combinedequipments/' + str(id_) + '/meters/' + str(meter_id) |
|
1489 | ||
1490 | ||
1491 | class CombinedEquipmentMeterItem: |
@@ 1581-1705 (lines=125) @@ | ||
1578 | resp.status = falcon.HTTP_204 |
|
1579 | ||
1580 | ||
1581 | class EquipmentVirtualMeterCollection: |
|
1582 | @staticmethod |
|
1583 | def __init__(): |
|
1584 | pass |
|
1585 | ||
1586 | @staticmethod |
|
1587 | def on_options(req, resp, id_): |
|
1588 | resp.status = falcon.HTTP_200 |
|
1589 | ||
1590 | @staticmethod |
|
1591 | def on_get(req, resp, id_): |
|
1592 | if not id_.isdigit() or int(id_) <= 0: |
|
1593 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1594 | description='API.INVALID_EQUIPMENT_ID') |
|
1595 | ||
1596 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1597 | cursor = cnx.cursor(dictionary=True) |
|
1598 | ||
1599 | cursor.execute(" SELECT name " |
|
1600 | " FROM tbl_equipments " |
|
1601 | " WHERE id = %s ", (id_,)) |
|
1602 | if cursor.fetchone() is None: |
|
1603 | cursor.close() |
|
1604 | cnx.disconnect() |
|
1605 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1606 | description='API.EQUIPMENT_NOT_FOUND') |
|
1607 | ||
1608 | query = (" SELECT id, name, uuid " |
|
1609 | " FROM tbl_energy_categories ") |
|
1610 | cursor.execute(query) |
|
1611 | rows_energy_categories = cursor.fetchall() |
|
1612 | ||
1613 | energy_category_dict = dict() |
|
1614 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1615 | for row in rows_energy_categories: |
|
1616 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1617 | "name": row['name'], |
|
1618 | "uuid": row['uuid']} |
|
1619 | ||
1620 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1621 | " FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " |
|
1622 | " WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " |
|
1623 | " ORDER BY m.id ") |
|
1624 | cursor.execute(query, (id_,)) |
|
1625 | rows = cursor.fetchall() |
|
1626 | ||
1627 | result = list() |
|
1628 | if rows is not None and len(rows) > 0: |
|
1629 | for row in rows: |
|
1630 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1631 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1632 | "energy_category": energy_category, |
|
1633 | "is_output": bool(row['is_output'])} |
|
1634 | result.append(meta_result) |
|
1635 | ||
1636 | resp.body = json.dumps(result) |
|
1637 | ||
1638 | @staticmethod |
|
1639 | def on_post(req, resp, id_): |
|
1640 | """Handles POST requests""" |
|
1641 | try: |
|
1642 | raw_json = req.stream.read().decode('utf-8') |
|
1643 | except Exception as ex: |
|
1644 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1645 | ||
1646 | if not id_.isdigit() or int(id_) <= 0: |
|
1647 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1648 | description='API.INVALID_EQUIPMENT_ID') |
|
1649 | ||
1650 | new_values = json.loads(raw_json, encoding='utf-8') |
|
1651 | ||
1652 | if 'virtual_meter_id' not in new_values['data'].keys() or \ |
|
1653 | not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
|
1654 | new_values['data']['virtual_meter_id'] <= 0: |
|
1655 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1656 | description='API.INVALID_VIRTUAL_METER_ID') |
|
1657 | virtual_meter_id = new_values['data']['virtual_meter_id'] |
|
1658 | ||
1659 | if 'is_output' not in new_values['data'].keys() or \ |
|
1660 | not isinstance(new_values['data']['is_output'], bool): |
|
1661 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1662 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1663 | is_output = new_values['data']['is_output'] |
|
1664 | ||
1665 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1666 | cursor = cnx.cursor() |
|
1667 | ||
1668 | cursor.execute(" SELECT name " |
|
1669 | " from tbl_equipments " |
|
1670 | " WHERE id = %s ", (id_,)) |
|
1671 | if cursor.fetchone() is None: |
|
1672 | cursor.close() |
|
1673 | cnx.disconnect() |
|
1674 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1675 | description='API.EQUIPMENT_NOT_FOUND') |
|
1676 | ||
1677 | cursor.execute(" SELECT name " |
|
1678 | " FROM tbl_virtual_meters " |
|
1679 | " WHERE id = %s ", (virtual_meter_id,)) |
|
1680 | if cursor.fetchone() is None: |
|
1681 | cursor.close() |
|
1682 | cnx.disconnect() |
|
1683 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1684 | description='API.VIRTUAL_METER_NOT_FOUND') |
|
1685 | ||
1686 | query = (" SELECT id " |
|
1687 | " FROM tbl_equipments_virtual_meters " |
|
1688 | " WHERE equipment_id = %s AND virtual_meter_id = %s") |
|
1689 | cursor.execute(query, (id_, virtual_meter_id,)) |
|
1690 | if cursor.fetchone() is not None: |
|
1691 | cursor.close() |
|
1692 | cnx.disconnect() |
|
1693 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1694 | description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTED') |
|
1695 | ||
1696 | add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " |
|
1697 | " VALUES (%s, %s, %s) ") |
|
1698 | cursor.execute(add_row, (id_, virtual_meter_id, is_output)) |
|
1699 | new_id = cursor.lastrowid |
|
1700 | cnx.commit() |
|
1701 | cursor.close() |
|
1702 | cnx.disconnect() |
|
1703 | ||
1704 | resp.status = falcon.HTTP_201 |
|
1705 | resp.location = '/equipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
|
1706 | ||
1707 | ||
1708 | class EquipmentVirtualMeterItem: |
|
@@ 1395-1519 (lines=125) @@ | ||
1392 | resp.status = falcon.HTTP_204 |
|
1393 | ||
1394 | ||
1395 | class EquipmentOfflineMeterCollection: |
|
1396 | @staticmethod |
|
1397 | def __init__(): |
|
1398 | pass |
|
1399 | ||
1400 | @staticmethod |
|
1401 | def on_options(req, resp, id_): |
|
1402 | resp.status = falcon.HTTP_200 |
|
1403 | ||
1404 | @staticmethod |
|
1405 | def on_get(req, resp, id_): |
|
1406 | if not id_.isdigit() or int(id_) <= 0: |
|
1407 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1408 | description='API.INVALID_EQUIPMENT_ID') |
|
1409 | ||
1410 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1411 | cursor = cnx.cursor(dictionary=True) |
|
1412 | ||
1413 | cursor.execute(" SELECT name " |
|
1414 | " FROM tbl_equipments " |
|
1415 | " WHERE id = %s ", (id_,)) |
|
1416 | if cursor.fetchone() is None: |
|
1417 | cursor.close() |
|
1418 | cnx.disconnect() |
|
1419 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1420 | description='API.EQUIPMENT_NOT_FOUND') |
|
1421 | ||
1422 | query = (" SELECT id, name, uuid " |
|
1423 | " FROM tbl_energy_categories ") |
|
1424 | cursor.execute(query) |
|
1425 | rows_energy_categories = cursor.fetchall() |
|
1426 | ||
1427 | energy_category_dict = dict() |
|
1428 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1429 | for row in rows_energy_categories: |
|
1430 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1431 | "name": row['name'], |
|
1432 | "uuid": row['uuid']} |
|
1433 | ||
1434 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1435 | " FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " |
|
1436 | " WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " |
|
1437 | " ORDER BY m.id ") |
|
1438 | cursor.execute(query, (id_,)) |
|
1439 | rows = cursor.fetchall() |
|
1440 | ||
1441 | result = list() |
|
1442 | if rows is not None and len(rows) > 0: |
|
1443 | for row in rows: |
|
1444 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1445 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1446 | "energy_category": energy_category, |
|
1447 | "is_output": bool(row['is_output'])} |
|
1448 | result.append(meta_result) |
|
1449 | ||
1450 | resp.body = json.dumps(result) |
|
1451 | ||
1452 | @staticmethod |
|
1453 | def on_post(req, resp, id_): |
|
1454 | """Handles POST requests""" |
|
1455 | try: |
|
1456 | raw_json = req.stream.read().decode('utf-8') |
|
1457 | except Exception as ex: |
|
1458 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1459 | ||
1460 | if not id_.isdigit() or int(id_) <= 0: |
|
1461 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1462 | description='API.INVALID_EQUIPMENT_ID') |
|
1463 | ||
1464 | new_values = json.loads(raw_json, encoding='utf-8') |
|
1465 | ||
1466 | if 'offline_meter_id' not in new_values['data'].keys() or \ |
|
1467 | not isinstance(new_values['data']['offline_meter_id'], int) or \ |
|
1468 | new_values['data']['offline_meter_id'] <= 0: |
|
1469 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1470 | description='API.INVALID_OFFLINE_METER_ID') |
|
1471 | offline_meter_id = new_values['data']['offline_meter_id'] |
|
1472 | ||
1473 | if 'is_output' not in new_values['data'].keys() or \ |
|
1474 | not isinstance(new_values['data']['is_output'], bool): |
|
1475 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1476 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1477 | is_output = new_values['data']['is_output'] |
|
1478 | ||
1479 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1480 | cursor = cnx.cursor() |
|
1481 | ||
1482 | cursor.execute(" SELECT name " |
|
1483 | " from tbl_equipments " |
|
1484 | " WHERE id = %s ", (id_,)) |
|
1485 | if cursor.fetchone() is None: |
|
1486 | cursor.close() |
|
1487 | cnx.disconnect() |
|
1488 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1489 | description='API.EQUIPMENT_NOT_FOUND') |
|
1490 | ||
1491 | cursor.execute(" SELECT name " |
|
1492 | " FROM tbl_offline_meters " |
|
1493 | " WHERE id = %s ", (offline_meter_id,)) |
|
1494 | if cursor.fetchone() is None: |
|
1495 | cursor.close() |
|
1496 | cnx.disconnect() |
|
1497 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1498 | description='API.OFFLINE_METER_NOT_FOUND') |
|
1499 | ||
1500 | query = (" SELECT id " |
|
1501 | " FROM tbl_equipments_offline_meters " |
|
1502 | " WHERE equipment_id = %s AND offline_meter_id = %s") |
|
1503 | cursor.execute(query, (id_, offline_meter_id,)) |
|
1504 | if cursor.fetchone() is not None: |
|
1505 | cursor.close() |
|
1506 | cnx.disconnect() |
|
1507 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1508 | description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTED') |
|
1509 | ||
1510 | add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " |
|
1511 | " VALUES (%s, %s, %s) ") |
|
1512 | cursor.execute(add_row, (id_, offline_meter_id, is_output)) |
|
1513 | new_id = cursor.lastrowid |
|
1514 | cnx.commit() |
|
1515 | cursor.close() |
|
1516 | cnx.disconnect() |
|
1517 | ||
1518 | resp.status = falcon.HTTP_201 |
|
1519 | resp.location = '/equipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
|
1520 | ||
1521 | ||
1522 | class EquipmentOfflineMeterItem: |
|
@@ 1210-1334 (lines=125) @@ | ||
1207 | resp.status = falcon.HTTP_200 |
|
1208 | ||
1209 | ||
1210 | class EquipmentMeterCollection: |
|
1211 | @staticmethod |
|
1212 | def __init__(): |
|
1213 | pass |
|
1214 | ||
1215 | @staticmethod |
|
1216 | def on_options(req, resp, id_): |
|
1217 | resp.status = falcon.HTTP_200 |
|
1218 | ||
1219 | @staticmethod |
|
1220 | def on_get(req, resp, id_): |
|
1221 | if not id_.isdigit() or int(id_) <= 0: |
|
1222 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1223 | description='API.INVALID_EQUIPMENT_ID') |
|
1224 | ||
1225 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1226 | cursor = cnx.cursor(dictionary=True) |
|
1227 | ||
1228 | cursor.execute(" SELECT name " |
|
1229 | " FROM tbl_equipments " |
|
1230 | " WHERE id = %s ", (id_,)) |
|
1231 | if cursor.fetchone() is None: |
|
1232 | cursor.close() |
|
1233 | cnx.disconnect() |
|
1234 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1235 | description='API.EQUIPMENT_NOT_FOUND') |
|
1236 | ||
1237 | query = (" SELECT id, name, uuid " |
|
1238 | " FROM tbl_energy_categories ") |
|
1239 | cursor.execute(query) |
|
1240 | rows_energy_categories = cursor.fetchall() |
|
1241 | ||
1242 | energy_category_dict = dict() |
|
1243 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
1244 | for row in rows_energy_categories: |
|
1245 | energy_category_dict[row['id']] = {"id": row['id'], |
|
1246 | "name": row['name'], |
|
1247 | "uuid": row['uuid']} |
|
1248 | ||
1249 | query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " |
|
1250 | " FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " |
|
1251 | " WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " |
|
1252 | " ORDER BY m.id ") |
|
1253 | cursor.execute(query, (id_,)) |
|
1254 | rows = cursor.fetchall() |
|
1255 | ||
1256 | result = list() |
|
1257 | if rows is not None and len(rows) > 0: |
|
1258 | for row in rows: |
|
1259 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
|
1260 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
|
1261 | "energy_category": energy_category, |
|
1262 | "is_output": bool(row['is_output'])} |
|
1263 | result.append(meta_result) |
|
1264 | ||
1265 | resp.body = json.dumps(result) |
|
1266 | ||
1267 | @staticmethod |
|
1268 | def on_post(req, resp, id_): |
|
1269 | """Handles POST requests""" |
|
1270 | try: |
|
1271 | raw_json = req.stream.read().decode('utf-8') |
|
1272 | except Exception as ex: |
|
1273 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
1274 | ||
1275 | if not id_.isdigit() or int(id_) <= 0: |
|
1276 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1277 | description='API.INVALID_EQUIPMENT_ID') |
|
1278 | ||
1279 | new_values = json.loads(raw_json, encoding='utf-8') |
|
1280 | ||
1281 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1282 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1283 | new_values['data']['meter_id'] <= 0: |
|
1284 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1285 | description='API.INVALID_METER_ID') |
|
1286 | meter_id = new_values['data']['meter_id'] |
|
1287 | ||
1288 | if 'is_output' not in new_values['data'].keys() or \ |
|
1289 | not isinstance(new_values['data']['is_output'], bool): |
|
1290 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1291 | description='API.INVALID_IS_OUTPUT_VALUE') |
|
1292 | is_output = new_values['data']['is_output'] |
|
1293 | ||
1294 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1295 | cursor = cnx.cursor() |
|
1296 | ||
1297 | cursor.execute(" SELECT name " |
|
1298 | " from tbl_equipments " |
|
1299 | " WHERE id = %s ", (id_,)) |
|
1300 | if cursor.fetchone() is None: |
|
1301 | cursor.close() |
|
1302 | cnx.disconnect() |
|
1303 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1304 | description='API.EQUIPMENT_NOT_FOUND') |
|
1305 | ||
1306 | cursor.execute(" SELECT name " |
|
1307 | " FROM tbl_meters " |
|
1308 | " WHERE id = %s ", (meter_id,)) |
|
1309 | if cursor.fetchone() is None: |
|
1310 | cursor.close() |
|
1311 | cnx.disconnect() |
|
1312 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
1313 | description='API.METER_NOT_FOUND') |
|
1314 | ||
1315 | query = (" SELECT id " |
|
1316 | " FROM tbl_equipments_meters " |
|
1317 | " WHERE equipment_id = %s AND meter_id = %s") |
|
1318 | cursor.execute(query, (id_, meter_id,)) |
|
1319 | if cursor.fetchone() is not None: |
|
1320 | cursor.close() |
|
1321 | cnx.disconnect() |
|
1322 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
|
1323 | description='API.EQUIPMENT_METER_RELATION_EXISTED') |
|
1324 | ||
1325 | add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " |
|
1326 | " VALUES (%s, %s, %s) ") |
|
1327 | cursor.execute(add_row, (id_, meter_id, is_output)) |
|
1328 | new_id = cursor.lastrowid |
|
1329 | cnx.commit() |
|
1330 | cursor.close() |
|
1331 | cnx.disconnect() |
|
1332 | ||
1333 | resp.status = falcon.HTTP_201 |
|
1334 | resp.location = '/equipments/' + str(id_) + '/meters/' + str(meter_id) |
|
1335 | ||
1336 | ||
1337 | class EquipmentMeterItem: |