@@ 4554-4768 (lines=215) @@ | ||
4551 | resp.location = '/hybridpowerstation/' + str(id_) + '/pvs/' + str(new_id) |
|
4552 | ||
4553 | ||
4554 | class HybridPowerStationPVItem: |
|
4555 | def __init__(self): |
|
4556 | """Initializes Class""" |
|
4557 | pass |
|
4558 | ||
4559 | @staticmethod |
|
4560 | def on_options(req, resp, id_, pvid): |
|
4561 | resp.status = falcon.HTTP_200 |
|
4562 | ||
4563 | @staticmethod |
|
4564 | def on_get(req, resp, id_, pvid): |
|
4565 | access_control(req) |
|
4566 | if not id_.isdigit() or int(id_) <= 0: |
|
4567 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4568 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
4569 | if not pvid.isdigit() or int(pvid) <= 0: |
|
4570 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4571 | description='API.INVALID_HYBRID_POWER_STATION_PV_ID') |
|
4572 | ||
4573 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4574 | cursor = cnx.cursor() |
|
4575 | ||
4576 | cursor.execute(" SELECT name " |
|
4577 | " FROM tbl_hybrid_power_stations " |
|
4578 | " WHERE id = %s ", (id_,)) |
|
4579 | if cursor.fetchone() is None: |
|
4580 | cursor.close() |
|
4581 | cnx.close() |
|
4582 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4583 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
4584 | ||
4585 | query = (" SELECT id, name, uuid " |
|
4586 | " FROM tbl_hybrid_power_stations ") |
|
4587 | cursor.execute(query) |
|
4588 | rows_hybridpowerstations = cursor.fetchall() |
|
4589 | ||
4590 | hybrid_power_station_dict = dict() |
|
4591 | if rows_hybridpowerstations is not None and len(rows_hybridpowerstations) > 0: |
|
4592 | for row in rows_hybridpowerstations: |
|
4593 | hybrid_power_station_dict[row[0]] = {"id": row[0], |
|
4594 | "name": row[1], |
|
4595 | "uuid": row[2]} |
|
4596 | query = (" SELECT id, name, uuid " |
|
4597 | " FROM tbl_meters ") |
|
4598 | cursor.execute(query) |
|
4599 | rows_meters = cursor.fetchall() |
|
4600 | ||
4601 | meter_dict = dict() |
|
4602 | if rows_meters is not None and len(rows_meters) > 0: |
|
4603 | for row in rows_meters: |
|
4604 | meter_dict[row[0]] = {"id": row[0], |
|
4605 | "name": row[1], |
|
4606 | "uuid": row[2]} |
|
4607 | # query point dict |
|
4608 | query = (" SELECT id, name " |
|
4609 | " FROM tbl_points ") |
|
4610 | cursor.execute(query) |
|
4611 | rows_points = cursor.fetchall() |
|
4612 | ||
4613 | point_dict = dict() |
|
4614 | if rows_points is not None and len(rows_points) > 0: |
|
4615 | for row in rows_points: |
|
4616 | point_dict[row[0]] = {"id": row[0], |
|
4617 | "name": row[1]} |
|
4618 | ||
4619 | query = (" SELECT id, name, uuid, hybrid_power_station_id, operating_status_point_id, meter_id " |
|
4620 | " FROM tbl_hybrid_power_stations_pvs " |
|
4621 | " WHERE id = %s ") |
|
4622 | cursor.execute(query, (pvid,)) |
|
4623 | row = cursor.fetchone() |
|
4624 | cursor.close() |
|
4625 | cnx.close() |
|
4626 | ||
4627 | if row is None: |
|
4628 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4629 | description='API.HYBRID_POWER_STATION_PV_NOT_FOUND') |
|
4630 | else: |
|
4631 | meta_result = {"id": row[0], |
|
4632 | "name": row[1], |
|
4633 | "uuid": row[2], |
|
4634 | "hybrid_power_station": hybrid_power_station_dict.get(row[3]), |
|
4635 | "operating_status_point": point_dict.get(row[4]), |
|
4636 | "meter": meter_dict.get(row[5]) |
|
4637 | } |
|
4638 | ||
4639 | resp.text = json.dumps(meta_result) |
|
4640 | ||
4641 | @staticmethod |
|
4642 | @user_logger |
|
4643 | def on_delete(req, resp, id_, pvid): |
|
4644 | admin_control(req) |
|
4645 | if not id_.isdigit() or int(id_) <= 0: |
|
4646 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4647 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
4648 | if not pvid.isdigit() or int(pvid) <= 0: |
|
4649 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4650 | description='API.INVALID_HYBRID_POWER_STATION_PV_ID') |
|
4651 | ||
4652 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4653 | cursor = cnx.cursor() |
|
4654 | ||
4655 | cursor.execute(" SELECT name " |
|
4656 | " FROM tbl_hybrid_power_stations " |
|
4657 | " WHERE id = %s ", (id_,)) |
|
4658 | if cursor.fetchone() is None: |
|
4659 | cursor.close() |
|
4660 | cnx.close() |
|
4661 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4662 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
4663 | ||
4664 | cursor.execute(" SELECT name " |
|
4665 | " FROM tbl_hybrid_power_stations_pvs " |
|
4666 | " WHERE id = %s ", (pvid,)) |
|
4667 | if cursor.fetchone() is None: |
|
4668 | cursor.close() |
|
4669 | cnx.close() |
|
4670 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4671 | description='API.HYBRID_POWER_STATION_PV_NOT_FOUND') |
|
4672 | ||
4673 | cursor.execute(" DELETE FROM tbl_hybrid_power_stations_pvs " |
|
4674 | " WHERE id = %s ", (pvid,)) |
|
4675 | cnx.commit() |
|
4676 | ||
4677 | cursor.close() |
|
4678 | cnx.close() |
|
4679 | ||
4680 | resp.status = falcon.HTTP_204 |
|
4681 | ||
4682 | @staticmethod |
|
4683 | @user_logger |
|
4684 | def on_put(req, resp, id_, pvid): |
|
4685 | """Handles PUT requests""" |
|
4686 | admin_control(req) |
|
4687 | try: |
|
4688 | raw_json = req.stream.read().decode('utf-8') |
|
4689 | except Exception as ex: |
|
4690 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
4691 | title='API.BAD_REQUEST', |
|
4692 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
4693 | if not id_.isdigit() or int(id_) <= 0: |
|
4694 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4695 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
4696 | if not pvid.isdigit() or int(pvid) <= 0: |
|
4697 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4698 | description='API.INVALID_HYBRID_POWER_STATION_PV_ID') |
|
4699 | ||
4700 | new_values = json.loads(raw_json) |
|
4701 | ||
4702 | if 'name' not in new_values['data'].keys() or \ |
|
4703 | not isinstance(new_values['data']['name'], str) or \ |
|
4704 | len(str.strip(new_values['data']['name'])) == 0: |
|
4705 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4706 | description='API.INVALID_HYBRID_POWER_STATION_PV_NAME') |
|
4707 | name = str.strip(new_values['data']['name']) |
|
4708 | ||
4709 | if 'operating_status_point_id' not in new_values['data'].keys() or \ |
|
4710 | not isinstance(new_values['data']['operating_status_point_id'], int) or \ |
|
4711 | new_values['data']['operating_status_point_id'] <= 0: |
|
4712 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4713 | description='API.INVALID_OPERATING_STATUS_POINT_ID') |
|
4714 | operating_status_point_id = new_values['data']['operating_status_point_id'] |
|
4715 | ||
4716 | if 'meter_id' not in new_values['data'].keys() or \ |
|
4717 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
4718 | new_values['data']['meter_id'] <= 0: |
|
4719 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4720 | description='API.INVALID_METER_ID') |
|
4721 | meter_id = new_values['data']['meter_id'] |
|
4722 | ||
4723 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4724 | cursor = cnx.cursor() |
|
4725 | ||
4726 | cursor.execute(" SELECT name " |
|
4727 | " FROM tbl_hybrid_power_stations " |
|
4728 | " WHERE id = %s ", (id_,)) |
|
4729 | if cursor.fetchone() is None: |
|
4730 | cursor.close() |
|
4731 | cnx.close() |
|
4732 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4733 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
4734 | ||
4735 | cursor.execute(" SELECT name " |
|
4736 | " FROM tbl_hybrid_power_stations_pvs " |
|
4737 | " WHERE id = %s ", (pvid,)) |
|
4738 | if cursor.fetchone() is None: |
|
4739 | cursor.close() |
|
4740 | cnx.close() |
|
4741 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4742 | description='API.HYBRID_POWER_STATION_PV_NOT_FOUND') |
|
4743 | ||
4744 | cursor.execute(" SELECT name " |
|
4745 | " FROM tbl_hybrid_power_stations_pvs " |
|
4746 | " WHERE hybrid_power_station_id = %s AND name = %s AND id != %s ", |
|
4747 | (id_, name, pvid)) |
|
4748 | if cursor.fetchone() is not None: |
|
4749 | cursor.close() |
|
4750 | cnx.close() |
|
4751 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4752 | description='API.HYBRID_POWER_STATION_PV_NAME_IS_ALREADY_IN_USE') |
|
4753 | ||
4754 | update_row = (" UPDATE tbl_hybrid_power_stations_pvs " |
|
4755 | " SET name = %s, hybrid_power_station_id = %s, operating_status_point_id = %s, " |
|
4756 | " meter_id = %s " |
|
4757 | " WHERE id = %s ") |
|
4758 | cursor.execute(update_row, (name, |
|
4759 | id_, |
|
4760 | operating_status_point_id, |
|
4761 | meter_id, |
|
4762 | pvid)) |
|
4763 | cnx.commit() |
|
4764 | ||
4765 | cursor.close() |
|
4766 | cnx.close() |
|
4767 | ||
4768 | resp.status = falcon.HTTP_200 |
|
4769 | ||
4770 | ||
4771 | class HybridPowerStationPVPointCollection: |
|
@@ 1662-1876 (lines=215) @@ | ||
1659 | resp.location = '/hybridpowerstation/' + str(id_) + '/cms/' + str(new_id) |
|
1660 | ||
1661 | ||
1662 | class HybridPowerStationCMItem: |
|
1663 | def __init__(self): |
|
1664 | """Initializes Class""" |
|
1665 | pass |
|
1666 | ||
1667 | @staticmethod |
|
1668 | def on_options(req, resp, id_, cid): |
|
1669 | resp.status = falcon.HTTP_200 |
|
1670 | ||
1671 | @staticmethod |
|
1672 | def on_get(req, resp, id_, cid): |
|
1673 | access_control(req) |
|
1674 | if not id_.isdigit() or int(id_) <= 0: |
|
1675 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1676 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
1677 | if not cid.isdigit() or int(cid) <= 0: |
|
1678 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1679 | description='API.INVALID_HYBRID_POWER_STATION_CM_ID') |
|
1680 | ||
1681 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1682 | cursor = cnx.cursor() |
|
1683 | ||
1684 | cursor.execute(" SELECT name " |
|
1685 | " FROM tbl_hybrid_power_stations " |
|
1686 | " WHERE id = %s ", (id_,)) |
|
1687 | if cursor.fetchone() is None: |
|
1688 | cursor.close() |
|
1689 | cnx.close() |
|
1690 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1691 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
1692 | ||
1693 | query = (" SELECT id, name, uuid " |
|
1694 | " FROM tbl_hybrid_power_stations ") |
|
1695 | cursor.execute(query) |
|
1696 | rows_hybridpowerstations = cursor.fetchall() |
|
1697 | ||
1698 | hybrid_power_station_dict = dict() |
|
1699 | if rows_hybridpowerstations is not None and len(rows_hybridpowerstations) > 0: |
|
1700 | for row in rows_hybridpowerstations: |
|
1701 | hybrid_power_station_dict[row[0]] = {"id": row[0], |
|
1702 | "name": row[1], |
|
1703 | "uuid": row[2]} |
|
1704 | query = (" SELECT id, name, uuid " |
|
1705 | " FROM tbl_meters ") |
|
1706 | cursor.execute(query) |
|
1707 | rows_meters = cursor.fetchall() |
|
1708 | ||
1709 | meter_dict = dict() |
|
1710 | if rows_meters is not None and len(rows_meters) > 0: |
|
1711 | for row in rows_meters: |
|
1712 | meter_dict[row[0]] = {"id": row[0], |
|
1713 | "name": row[1], |
|
1714 | "uuid": row[2]} |
|
1715 | # query point dict |
|
1716 | query = (" SELECT id, name " |
|
1717 | " FROM tbl_points ") |
|
1718 | cursor.execute(query) |
|
1719 | rows_points = cursor.fetchall() |
|
1720 | ||
1721 | point_dict = dict() |
|
1722 | if rows_points is not None and len(rows_points) > 0: |
|
1723 | for row in rows_points: |
|
1724 | point_dict[row[0]] = {"id": row[0], |
|
1725 | "name": row[1]} |
|
1726 | ||
1727 | query = (" SELECT id, name, uuid, hybrid_power_station_id, operating_status_point_id, meter_id " |
|
1728 | " FROM tbl_hybrid_power_stations_cms " |
|
1729 | " WHERE id = %s ") |
|
1730 | cursor.execute(query, (cid,)) |
|
1731 | row = cursor.fetchone() |
|
1732 | cursor.close() |
|
1733 | cnx.close() |
|
1734 | ||
1735 | if row is None: |
|
1736 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1737 | description='API.HYBRID_POWER_STATION_CM_NOT_FOUND') |
|
1738 | else: |
|
1739 | meta_result = {"id": row[0], |
|
1740 | "name": row[1], |
|
1741 | "uuid": row[2], |
|
1742 | "hybrid_power_station": hybrid_power_station_dict.get(row[3]), |
|
1743 | "operating_status_point": point_dict.get(row[4]), |
|
1744 | "meter": meter_dict.get(row[5]) |
|
1745 | } |
|
1746 | ||
1747 | resp.text = json.dumps(meta_result) |
|
1748 | ||
1749 | @staticmethod |
|
1750 | @user_logger |
|
1751 | def on_delete(req, resp, id_, cid): |
|
1752 | admin_control(req) |
|
1753 | if not id_.isdigit() or int(id_) <= 0: |
|
1754 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1755 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
1756 | if not cid.isdigit() or int(cid) <= 0: |
|
1757 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1758 | description='API.INVALID_HYBRID_POWER_STATION_CM_ID') |
|
1759 | ||
1760 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1761 | cursor = cnx.cursor() |
|
1762 | ||
1763 | cursor.execute(" SELECT name " |
|
1764 | " FROM tbl_hybrid_power_stations " |
|
1765 | " WHERE id = %s ", (id_,)) |
|
1766 | if cursor.fetchone() is None: |
|
1767 | cursor.close() |
|
1768 | cnx.close() |
|
1769 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1770 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
1771 | ||
1772 | cursor.execute(" SELECT name " |
|
1773 | " FROM tbl_hybrid_power_stations_cms " |
|
1774 | " WHERE id = %s ", (cid,)) |
|
1775 | if cursor.fetchone() is None: |
|
1776 | cursor.close() |
|
1777 | cnx.close() |
|
1778 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1779 | description='API.HYBRID_POWER_STATION_CM_NOT_FOUND') |
|
1780 | ||
1781 | cursor.execute(" DELETE FROM tbl_hybrid_power_stations_cms " |
|
1782 | " WHERE id = %s ", (cid,)) |
|
1783 | cnx.commit() |
|
1784 | ||
1785 | cursor.close() |
|
1786 | cnx.close() |
|
1787 | ||
1788 | resp.status = falcon.HTTP_204 |
|
1789 | ||
1790 | @staticmethod |
|
1791 | @user_logger |
|
1792 | def on_put(req, resp, id_, cid): |
|
1793 | """Handles PUT requests""" |
|
1794 | admin_control(req) |
|
1795 | try: |
|
1796 | raw_json = req.stream.read().decode('utf-8') |
|
1797 | except Exception as ex: |
|
1798 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1799 | title='API.BAD_REQUEST', |
|
1800 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1801 | if not id_.isdigit() or int(id_) <= 0: |
|
1802 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1803 | description='API.INVALID_HYBRID_POWER_STATION_ID') |
|
1804 | if not cid.isdigit() or int(cid) <= 0: |
|
1805 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1806 | description='API.INVALID_HYBRID_POWER_STATION_CM_ID') |
|
1807 | ||
1808 | new_values = json.loads(raw_json) |
|
1809 | ||
1810 | if 'name' not in new_values['data'].keys() or \ |
|
1811 | not isinstance(new_values['data']['name'], str) or \ |
|
1812 | len(str.strip(new_values['data']['name'])) == 0: |
|
1813 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1814 | description='API.INVALID_HYBRID_POWER_STATION_CM_NAME') |
|
1815 | name = str.strip(new_values['data']['name']) |
|
1816 | ||
1817 | if 'operating_status_point_id' not in new_values['data'].keys() or \ |
|
1818 | not isinstance(new_values['data']['operating_status_point_id'], int) or \ |
|
1819 | new_values['data']['operating_status_point_id'] <= 0: |
|
1820 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1821 | description='API.INVALID_OPERATING_STATUS_POINT_ID') |
|
1822 | operating_status_point_id = new_values['data']['operating_status_point_id'] |
|
1823 | ||
1824 | if 'meter_id' not in new_values['data'].keys() or \ |
|
1825 | not isinstance(new_values['data']['meter_id'], int) or \ |
|
1826 | new_values['data']['meter_id'] <= 0: |
|
1827 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1828 | description='API.INVALID_METER_ID') |
|
1829 | meter_id = new_values['data']['meter_id'] |
|
1830 | ||
1831 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1832 | cursor = cnx.cursor() |
|
1833 | ||
1834 | cursor.execute(" SELECT name " |
|
1835 | " FROM tbl_hybrid_power_stations " |
|
1836 | " WHERE id = %s ", (id_,)) |
|
1837 | if cursor.fetchone() is None: |
|
1838 | cursor.close() |
|
1839 | cnx.close() |
|
1840 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1841 | description='API.HYBRID_POWER_STATION_NOT_FOUND') |
|
1842 | ||
1843 | cursor.execute(" SELECT name " |
|
1844 | " FROM tbl_hybrid_power_stations_cms " |
|
1845 | " WHERE id = %s ", (cid,)) |
|
1846 | if cursor.fetchone() is None: |
|
1847 | cursor.close() |
|
1848 | cnx.close() |
|
1849 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1850 | description='API.HYBRID_POWER_STATION_CM_NOT_FOUND') |
|
1851 | ||
1852 | cursor.execute(" SELECT name " |
|
1853 | " FROM tbl_hybrid_power_stations_cms " |
|
1854 | " WHERE hybrid_power_station_id = %s AND name = %s AND id != %s ", |
|
1855 | (id_, name, cid)) |
|
1856 | if cursor.fetchone() is not None: |
|
1857 | cursor.close() |
|
1858 | cnx.close() |
|
1859 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1860 | description='API.HYBRID_POWER_STATION_CM_NAME_IS_ALREADY_IN_USE') |
|
1861 | ||
1862 | update_row = (" UPDATE tbl_hybrid_power_stations_cms " |
|
1863 | " SET name = %s, hybrid_power_station_id = %s, operating_status_point_id = %s, " |
|
1864 | " meter_id = %s " |
|
1865 | " WHERE id = %s ") |
|
1866 | cursor.execute(update_row, (name, |
|
1867 | id_, |
|
1868 | operating_status_point_id, |
|
1869 | meter_id, |
|
1870 | cid)) |
|
1871 | cnx.commit() |
|
1872 | ||
1873 | cursor.close() |
|
1874 | cnx.close() |
|
1875 | ||
1876 | resp.status = falcon.HTTP_200 |
|
1877 | ||
1878 | ||
1879 | class HybridPowerStationCMPointCollection: |