summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/web_kbn.py64
1 files changed, 60 insertions, 4 deletions
diff --git a/bin/web_kbn.py b/bin/web_kbn.py
index 09fa9c6..c21269b 100644
--- a/bin/web_kbn.py
+++ b/bin/web_kbn.py
@@ -5,6 +5,7 @@ import aiohttp_jinja2
import json
import re
import inverterd
+import phonenumbers
import __py_include
from io import StringIO
@@ -27,6 +28,8 @@ class WebKbnConfig(AppConfigUnit):
'listen_addr': cls._addr_schema(required=True),
'assets_public_path': {'type': 'string'},
'pump_addr': cls._addr_schema(required=True),
+ 'inverter_grafana_url': {'type': 'string'},
+ 'sensors_grafana_url': {'type': 'string'},
}
@@ -69,8 +72,12 @@ def get_head_static() -> str:
return buf.getvalue()
+def get_modem_client(modem_cfg: dict) -> E3372:
+ return E3372(modem_cfg['ip'], legacy_token_auth=modem_cfg['legacy_auth'])
+
+
def get_modem_data(modem_cfg: dict, get_raw=False) -> Union[dict, tuple]:
- cl = E3372(modem_cfg['ip'], legacy_token_auth=modem_cfg['legacy_auth'])
+ cl = get_modem_client(modem_cfg)
signal = cl.device_signal
status = cl.monitoring_status
@@ -190,6 +197,8 @@ class WebSite(http.HTTPServer):
self.get('/inverter.cgi', self.inverter)
self.get('/inverter.ajx', self.inverter_ajx)
self.get('/pump.cgi', self.pump)
+ self.get('/sms.cgi', self.sms)
+ self.post('/sms.cgi', self.sms_post)
async def render_page(self,
req: http.Request,
@@ -208,8 +217,12 @@ class WebSite(http.HTTPServer):
return response
async def index(self, req: http.Request):
+ ctx = {}
+ for k in 'inverter', 'sensors':
+ ctx[f'{k}_grafana_url'] = config.app_config[f'{k}_grafana_url']
return await self.render_page(req, 'index',
- title="Home web site")
+ title="Home web site",
+ context=ctx)
async def modems(self, req: http.Request):
return await self.render_page(req, 'modems',
@@ -218,7 +231,7 @@ class WebSite(http.HTTPServer):
async def modems_ajx(self, req: http.Request):
modem = req.query.get('id', None)
- if modem not in self._modems_config.getkeys():
+ if modem not in self._modems_config.keys():
raise ValueError('invalid modem id')
modem_cfg = self._modems_config.get(modem)
@@ -234,7 +247,7 @@ class WebSite(http.HTTPServer):
async def modems_verbose(self, req: http.Request):
modem = req.query.get('id', None)
- if modem not in self._modems_config.getkeys():
+ if modem not in self._modems_config.keys():
raise ValueError('invalid modem id')
modem_cfg = self._modems_config.get(modem)
@@ -253,6 +266,49 @@ class WebSite(http.HTTPServer):
title=f'Подробная информация о модеме "{modem_name}"',
context=dict(data=data, modem_name=modem_name))
+ async def sms(self, req: http.Request):
+ modem = req.query.get('id', list(self._modems_config.keys())[0])
+ is_outbox = int(req.query.get('outbox', 0)) == 1
+ error = req.query.get('error', None)
+ sent = int(req.query.get('sent', 0)) == 1
+
+ cl = get_modem_client(self._modems_config[modem])
+ messages = cl.sms_list(1, 20, is_outbox)
+ return await self.render_page(req, 'sms',
+ title=f"SMS-сообщения ({'исходящие' if is_outbox else 'входящие'}, {modem})",
+ context=dict(
+ modems=self._modems_config,
+ selected_modem=modem,
+ is_outbox=is_outbox,
+ error=error,
+ is_sent=sent,
+ messages=messages
+ ))
+
+ async def sms_post(self, req: http.Request):
+ modem = req.query.get('id', list(self._modems_config.keys())[0])
+ is_outbox = int(req.query.get('outbox', 0)) == 1
+
+ fd = await req.post()
+ phone = fd.get('phone', None)
+ text = fd.get('text', None)
+
+ return_url = f'/sms.cgi?id={modem}&outbox={int(is_outbox)}'
+ phone = re.sub('\s+', '', phone)
+
+ if len(phone) > 4:
+ country = None
+ if not phone.startswith('+'):
+ country = 'RU'
+ number = phonenumbers.parse(phone, country)
+ if not phonenumbers.is_valid_number(number):
+ raise HTTPFound(f'{return_url}&error=Неверный+номер')
+ phone = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)
+
+ cl = get_modem_client(self._modems_config[modem])
+ cl.sms_send(phone, text)
+ raise HTTPFound(return_url)
+
async def inverter(self, req: http.Request):
action = req.query.get('do', None)
if action == 'set-osp':