"""Connect to wsserver and process messages from it."""
async for websocket in connect(self.conf['url'],
ping_interval=1, ping_timeout=5):
- print(f"WSClient '{self.name}': Connection opened"
- f" to {self.conf['url']}.")
- await self.bus.send(Message(self.name,
- {'event':
- 'connection opened'}))
try:
+ # Open connection:
+ print(f"WSClient '{self.name}': Connection opened"
+ f" to {self.conf['url']}.")
+ await self.bus.send(Message(self.name,
+ {'event':
+ 'connection opened'}))
+ # Configure name, MAC, and filter:
conf_command: Dict[str, Any] = \
- {'command': 'configure websocket', 'target': ''}
+ {'command': 'configure websocket', 'target': ''}
if 'client' in self.conf:
conf_command['name'] = self._client
else:
conf_command['down filter'] = self._down_filter
json_command = json.dumps(conf_command)
await websocket.send(json_command)
+ print(f"WSClient '{self.name}': Connection configured"
+ f" to {self.conf['url']} as '{conf_command['name']}'.")
await self.bus.send(Message(self.name,
{'event':
'connection configured'}))
+ # Read incoming messages in loop:
self._websocket = websocket
- try:
- async for json_message in websocket:
- assert isinstance(json_message, str)
- await self._send(json_message)
- except ConnectionClosed:
- self._websocket = None
- continue
- await self.bus.send(Message(self.name,
- {'event':
- 'connection closed'}))
+ async for json_message in websocket:
+ assert isinstance(json_message, str)
+ await self._send(json_message)
except Exception as e:
print(f"WSClient '{self.name}': Exception in connection: {e}")
+ self._websocket = None
+ print(f"WSClient '{self.name}': Connection closed"
+ f" to {self.conf['url']}.")
+ await self.bus.send(Message(self.name,
+ {'event':
+ 'connection closed'}))
await asyncio.sleep(1)
async def _send(self, json_message: str) -> None: