Provide counter values for the dashboard
This commit is contained in:
@@ -327,8 +327,10 @@ class AsyncStreamServer(AsyncStream):
|
||||
logger.info(f'[{self.node_id}:{self.conn_no}] '
|
||||
f'Accept connection from {self.r_addr}')
|
||||
Infos.inc_counter('Inverter_Cnt')
|
||||
Infos.inc_counter('ServerMode_Cnt')
|
||||
await self.publish_outstanding_mqtt()
|
||||
await self.loop()
|
||||
Infos.dec_counter('ServerMode_Cnt')
|
||||
Infos.dec_counter('Inverter_Cnt')
|
||||
await self.publish_outstanding_mqtt()
|
||||
logger.info(f'[{self.node_id}:{self.conn_no}] Server loop stopped for'
|
||||
@@ -359,9 +361,11 @@ class AsyncStreamServer(AsyncStream):
|
||||
|
||||
class AsyncStreamClient(AsyncStream):
|
||||
def __init__(self, reader: StreamReader, writer: StreamWriter,
|
||||
rstream: "StreamPtr", close_cb) -> None:
|
||||
rstream: "StreamPtr", close_cb,
|
||||
use_emu: bool = False) -> None:
|
||||
AsyncStream.__init__(self, reader, writer, rstream)
|
||||
self.close_cb = close_cb
|
||||
self.emu_mode = use_emu
|
||||
|
||||
async def disc(self) -> None:
|
||||
logging.debug('AsyncStreamClient.disc()')
|
||||
@@ -376,8 +380,16 @@ class AsyncStreamClient(AsyncStream):
|
||||
async def client_loop(self, _: str) -> None:
|
||||
'''Loop for receiving messages from the TSUN cloud (client-side)'''
|
||||
Infos.inc_counter('Cloud_Conn_Cnt')
|
||||
if self.emu_mode:
|
||||
Infos.inc_counter('EmuMode_Cnt')
|
||||
else:
|
||||
Infos.inc_counter('ProxyMode_Cnt')
|
||||
await self.publish_outstanding_mqtt()
|
||||
await self.loop()
|
||||
if self.emu_mode:
|
||||
Infos.dec_counter('EmuMode_Cnt')
|
||||
else:
|
||||
Infos.dec_counter('ProxyMode_Cnt')
|
||||
Infos.dec_counter('Cloud_Conn_Cnt')
|
||||
await self.publish_outstanding_mqtt()
|
||||
logger.info(f'[{self.node_id}:{self.conn_no}] '
|
||||
|
||||
@@ -838,7 +838,10 @@ class Infos:
|
||||
def inc_counter(cls, counter: str) -> None:
|
||||
'''inc proxy statistic counter'''
|
||||
db_dict = cls.stat['proxy']
|
||||
db_dict[counter] += 1
|
||||
try:
|
||||
db_dict[counter] += 1
|
||||
except Exception:
|
||||
db_dict[counter] = 1
|
||||
cls.new_stat_data['proxy'] = True
|
||||
|
||||
@classmethod
|
||||
@@ -848,6 +851,15 @@ class Infos:
|
||||
db_dict[counter] -= 1
|
||||
cls.new_stat_data['proxy'] = True
|
||||
|
||||
@classmethod
|
||||
def get_counter(cls, counter: str) -> int:
|
||||
'''get proxy statistic counter'''
|
||||
try:
|
||||
db_dict = cls.stat['proxy']
|
||||
return db_dict[counter]
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
def ha_proxy_confs(self, ha_prfx: str, node_id: str, snr: str) \
|
||||
-> Generator[tuple[str, str, str, str], None, None]:
|
||||
'''Generator function yields json register struct for home-assistant
|
||||
|
||||
@@ -31,8 +31,10 @@ class InverterBase(InverterIfc, Proxy):
|
||||
self.config_id = config_id
|
||||
if remote_prot_class:
|
||||
self.prot_class = remote_prot_class
|
||||
self.use_emulation = True
|
||||
else:
|
||||
self.prot_class = prot_class
|
||||
self.use_emulation = False
|
||||
self.__ha_restarts = -1
|
||||
self.remote = StreamPtr(None)
|
||||
ifc = AsyncStreamServer(reader, writer,
|
||||
@@ -117,7 +119,8 @@ class InverterBase(InverterIfc, Proxy):
|
||||
Config.act_config[self.config_id]['enabled'] = False
|
||||
|
||||
ifc = AsyncStreamClient(
|
||||
reader, writer, self.local, self.__del_remote)
|
||||
reader, writer, self.local,
|
||||
self.__del_remote, self.use_emulation)
|
||||
|
||||
self.remote.ifc = ifc
|
||||
if hasattr(stream, 'id_str'):
|
||||
|
||||
@@ -28,10 +28,12 @@ class ModbusConn():
|
||||
logging.info(f'[{stream.node_id}:{stream.conn_no}] '
|
||||
f'Connected to {self.addr}')
|
||||
Infos.inc_counter('Inverter_Cnt')
|
||||
Infos.inc_counter('ClientMode_Cnt')
|
||||
await self.inverter.local.ifc.publish_outstanding_mqtt()
|
||||
return self.inverter
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
Infos.dec_counter('ClientMode_Cnt')
|
||||
Infos.dec_counter('Inverter_Cnt')
|
||||
await self.inverter.local.ifc.publish_outstanding_mqtt()
|
||||
self.inverter.__exit__(exc_type, exc, tb)
|
||||
|
||||
@@ -2,6 +2,7 @@ from quart import Blueprint
|
||||
from quart import render_template, url_for
|
||||
from quart import send_from_directory
|
||||
from quart_babel import format_datetime
|
||||
from infos import Infos
|
||||
import os
|
||||
|
||||
web_routes = Blueprint('web_routes', __name__)
|
||||
@@ -14,25 +15,6 @@ async def get_icon(file: str, mime: str = 'image/png'):
|
||||
mimetype=mime)
|
||||
|
||||
|
||||
def get_inv_count():
|
||||
return 1234
|
||||
|
||||
|
||||
TsunCnt = 0
|
||||
|
||||
|
||||
def get_tsun_count():
|
||||
global TsunCnt
|
||||
TsunCnt += 1
|
||||
return TsunCnt
|
||||
|
||||
|
||||
@web_routes.context_processor
|
||||
def utility_processor():
|
||||
return dict(inv_count=get_inv_count(),
|
||||
tsun_count=get_tsun_count())
|
||||
|
||||
|
||||
@web_routes.route('/')
|
||||
async def index():
|
||||
return await render_template(
|
||||
@@ -47,11 +29,12 @@ async def empty():
|
||||
|
||||
@web_routes.route('/data-fetch')
|
||||
async def data_fetch():
|
||||
global TsunCnt
|
||||
TsunCnt += 1
|
||||
return {
|
||||
"proxy-cnt": f"<h3>{TsunCnt}</h3>",
|
||||
"update-time": format_datetime(format="medium"),
|
||||
"server-cnt": f"<h3>{Infos.get_counter('ServerMode_Cnt')}</h3>",
|
||||
"client-cnt": f"<h3>{Infos.get_counter('ClientMode_Cnt')}</h3>",
|
||||
"proxy-cnt": f"<h3>{Infos.get_counter('ProxyMode_Cnt')}</h3>",
|
||||
"emulation-cnt": f"<h3>{Infos.get_counter('EmuMode_Cnt')}</h3>",
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user