summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEvgeny Sorokin <me@ch1p.io>2024-01-17 03:35:59 +0300
committerEvgeny Sorokin <me@ch1p.io>2024-01-17 03:35:59 +0300
commita9a241ad19449c29b68cd4a5b539bcbec816e341 (patch)
tree2ebfc73b206e4cec30fd94613bd6bf42ef5b269d /bin
parent8a89dd77be03ca8eb9cdc378ba8e912292494fa9 (diff)
lws: pump page rewritten to python
Diffstat (limited to 'bin')
-rw-r--r--bin/web_kbn.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/bin/web_kbn.py b/bin/web_kbn.py
index d9d0035..09fa9c6 100644
--- a/bin/web_kbn.py
+++ b/bin/web_kbn.py
@@ -14,6 +14,7 @@ from homekit.config import config, AppConfigUnit
from homekit.util import homekit_path, filesize_fmt, seconds_to_human_readable_string
from homekit.modem import E3372, ModemsConfig, MacroNetWorkType
from homekit.inverter.config import InverterdConfig
+from homekit.relay.sunxi_h3_client import RelayClient
from homekit import http
@@ -24,7 +25,8 @@ class WebKbnConfig(AppConfigUnit):
def schema(cls) -> Optional[dict]:
return {
'listen_addr': cls._addr_schema(required=True),
- 'assets_public_path': {'type': 'string'}
+ 'assets_public_path': {'type': 'string'},
+ 'pump_addr': cls._addr_schema(required=True),
}
@@ -91,6 +93,13 @@ def get_modem_data(modem_cfg: dict, get_raw=False) -> Union[dict, tuple]:
}
+def get_pump_client() -> RelayClient:
+ addr = config.app_config['pump_addr']
+ cl = RelayClient(host=addr.host, port=addr.port)
+ cl.connect()
+ return cl
+
+
def get_inverter_client() -> inverterd.Client:
cl = inverterd.Client(host=InverterdConfig()['remote_addr'].host)
cl.connect()
@@ -180,6 +189,7 @@ class WebSite(http.HTTPServer):
self.get('/inverter.cgi', self.inverter)
self.get('/inverter.ajx', self.inverter_ajx)
+ self.get('/pump.cgi', self.pump)
async def render_page(self,
req: http.Request,
@@ -263,6 +273,23 @@ class WebSite(http.HTTPServer):
status, rated, html = await asyncio.get_event_loop().run_in_executor(None, get_inverter_data)
return self.ok({'html': html})
+ async def pump(self, req: http.Request):
+ # TODO
+ # these are blocking calls
+ # should be rewritten using aio
+
+ cl = get_pump_client()
+
+ action = req.query.get('set', None)
+ if action in ('on', 'off'):
+ getattr(cl, action)()
+ raise HTTPFound('/pump.cgi')
+
+ status = cl.status()
+ return await self.render_page(req, 'pump',
+ title='Насос',
+ context=dict(status=status))
+
if __name__ == '__main__':
config.load_app(WebKbnConfig)