rename class Proxy to Inverter
This commit is contained in:
@@ -44,9 +44,6 @@ class AsyncStream(Message):
|
|||||||
|
|
||||||
self.unique_id = serial_no
|
self.unique_id = serial_no
|
||||||
|
|
||||||
ha = Config.get('ha')
|
|
||||||
self.entitiy_prfx = ha['entity_prefix'] + '/'
|
|
||||||
self.discovery_prfx = ha['discovery_prefix'] + '/'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,16 @@ logger = logging.getLogger('conn')
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Proxy(AsyncStream):
|
class Inverter(AsyncStream):
|
||||||
|
|
||||||
def __init__ (self, reader, writer, addr):
|
def __init__ (self, reader, writer, addr):
|
||||||
super().__init__(reader, writer, addr, None, True)
|
super().__init__(reader, writer, addr, None, True)
|
||||||
self.mqtt = Mqtt()
|
self.mqtt = Mqtt()
|
||||||
|
self.ha_restarts = 0
|
||||||
|
ha = Config.get('ha')
|
||||||
|
self.entitiy_prfx = ha['entity_prefix'] + '/'
|
||||||
|
self.discovery_prfx = ha['discovery_prefix'] + '/'
|
||||||
|
|
||||||
|
|
||||||
async def server_loop(self, addr):
|
async def server_loop(self, addr):
|
||||||
'''Loop for receiving messages from the inverter (server-side)'''
|
'''Loop for receiving messages from the inverter (server-side)'''
|
||||||
@@ -19,7 +24,7 @@ class Proxy(AsyncStream):
|
|||||||
await self.loop()
|
await self.loop()
|
||||||
logging.info(f'Server loop stopped for {addr}')
|
logging.info(f'Server loop stopped for {addr}')
|
||||||
|
|
||||||
# if the server connection closes, we also disconnect the connection to te TSUN cloud
|
# if the server connection closes, we also have to disconnect the connection to te TSUN cloud
|
||||||
if self.remoteStream:
|
if self.remoteStream:
|
||||||
logging.debug ("disconnect client connection")
|
logging.debug ("disconnect client connection")
|
||||||
self.remoteStream.disc()
|
self.remoteStream.disc()
|
||||||
@@ -52,7 +57,7 @@ class Proxy(AsyncStream):
|
|||||||
logging.info(f'{error}')
|
logging.info(f'{error}')
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Proxy: Exception for {addr}:\n"
|
f"Inverter: Exception for {addr}:\n"
|
||||||
f"{traceback.format_exc()}")
|
f"{traceback.format_exc()}")
|
||||||
|
|
||||||
|
|
||||||
@@ -60,9 +65,10 @@ class Proxy(AsyncStream):
|
|||||||
async def async_publ_mqtt(self) -> None:
|
async def async_publ_mqtt(self) -> None:
|
||||||
db = self.db.db
|
db = self.db.db
|
||||||
# check if new inverter or collector infos are available or when the home assistant has changed the status back to online
|
# check if new inverter or collector infos are available or when the home assistant has changed the status back to online
|
||||||
if (self.new_data.keys() & {'inverter', 'collector'}): #or self.mqtt.home_assistant_restarted:
|
if (self.new_data.keys() & {'inverter', 'collector'}) or self.mqtt.ha_restarts != self.ha_restarts:
|
||||||
await self.__register_home_assistant()
|
await self.__register_home_assistant()
|
||||||
#self.mqtt.home_assistant_restarted = False # clear flag
|
self.ha_restarts = self.mqtt.ha_restarts
|
||||||
|
|
||||||
for key in self.new_data:
|
for key in self.new_data:
|
||||||
if self.new_data[key] and key in db:
|
if self.new_data[key] and key in db:
|
||||||
data_json = json.dumps(db[key])
|
data_json = json.dumps(db[key])
|
||||||
@@ -70,14 +76,15 @@ class Proxy(AsyncStream):
|
|||||||
await self.mqtt.publish(f"{self.entitiy_prfx}{self.node_id}{key}", data_json)
|
await self.mqtt.publish(f"{self.entitiy_prfx}{self.node_id}{key}", data_json)
|
||||||
self.new_data[key] = False
|
self.new_data[key] = False
|
||||||
|
|
||||||
async def __register_home_assistant(self):
|
async def __register_home_assistant(self):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for data_json, component, id in self.db.ha_confs(self.entitiy_prfx + self.node_id, self.unique_id, self.sug_area):
|
for data_json, component, id in self.db.ha_confs(self.entitiy_prfx + self.node_id, self.unique_id, self.sug_area):
|
||||||
logger.debug(f'MQTT Register: {data_json}')
|
logger.debug(f'MQTT Register: {data_json}')
|
||||||
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{self.node_id}{id}/config", data_json)
|
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{self.node_id}{id}/config", data_json)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Proxy: Exception:\n"
|
f"Inverter: Exception:\n"
|
||||||
f"{traceback.format_exc()}")
|
f"{traceback.format_exc()}")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
@@ -85,6 +92,6 @@ class Proxy(AsyncStream):
|
|||||||
super().close() # call close handler in the parent class
|
super().close() # call close handler in the parent class
|
||||||
|
|
||||||
|
|
||||||
def __del__ (proxy):
|
def __del__ (self):
|
||||||
logging.debug ("Proxy __del__")
|
logging.debug ("Inverter __del__")
|
||||||
super().__del__()
|
super().__del__()
|
||||||
@@ -16,14 +16,22 @@ class Singleton(type):
|
|||||||
|
|
||||||
class Mqtt(metaclass=Singleton):
|
class Mqtt(metaclass=Singleton):
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
logger_mqtt.debug(f'MQTT: __init__')
|
logger_mqtt.debug(f'MQTT: __init__')
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
self.task = loop.create_task(self.__loop())
|
self.task = loop.create_task(self.__loop())
|
||||||
self.home_assistant_restarted = False
|
self.ha_restarts = 0
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ha_restarts(self):
|
||||||
|
return self._ha_restarts
|
||||||
|
|
||||||
|
@ha_restarts.setter
|
||||||
|
def ha_restarts(self, value):
|
||||||
|
self._ha_restarts = value
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
logger_mqtt.debug(f'MQTT: __del__')
|
logger_mqtt.debug(f'MQTT: __del__')
|
||||||
|
|
||||||
@@ -59,7 +67,7 @@ class Mqtt(metaclass=Singleton):
|
|||||||
status = message.payload.decode("UTF-8")
|
status = message.payload.decode("UTF-8")
|
||||||
logger_mqtt.info(f'Home-Assistant Status: {status}')
|
logger_mqtt.info(f'Home-Assistant Status: {status}')
|
||||||
if status == 'online':
|
if status == 'online':
|
||||||
self.home_assistant_restarted = True # set flag to force MQTT registering
|
self.restarts += 1
|
||||||
|
|
||||||
except aiomqtt.MqttError:
|
except aiomqtt.MqttError:
|
||||||
logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...")
|
logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...")
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import logging, asyncio, signal, functools, os
|
import logging, asyncio, signal, functools, os
|
||||||
#from logging.handlers import TimedRotatingFileHandler
|
|
||||||
from logging import config
|
from logging import config
|
||||||
from async_stream import AsyncStream
|
from async_stream import AsyncStream
|
||||||
from proxy import Proxy
|
from inverter import Inverter
|
||||||
from config import Config
|
from config import Config
|
||||||
from mqtt import Mqtt
|
from mqtt import Mqtt
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ async def handle_client(reader, writer):
|
|||||||
'''Handles a new incoming connection and starts an async loop'''
|
'''Handles a new incoming connection and starts an async loop'''
|
||||||
|
|
||||||
addr = writer.get_extra_info('peername')
|
addr = writer.get_extra_info('peername')
|
||||||
await Proxy(reader, writer, addr).server_loop(addr)
|
await Inverter(reader, writer, addr).server_loop(addr)
|
||||||
|
|
||||||
|
|
||||||
def handle_SIGTERM(loop):
|
def handle_SIGTERM(loop):
|
||||||
|
|||||||
Reference in New Issue
Block a user