Skip to content

Commit

Permalink
fix proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus committed Aug 20, 2022
1 parent d07ba47 commit e7f5d08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
3 changes: 3 additions & 0 deletions bumper/mqtt/helper_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ async def send_command(
finally:
self._commands.pop(request_id, None)

def publish(self, topic: str, data: bytes) -> None:
self._client.publish(topic, data)

async def disconnect(self) -> None:
"""Disconnect client."""
if self.is_connected:
Expand Down
23 changes: 14 additions & 9 deletions bumper/mqtt/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from amqtt.mqtt.protocol.handler import ProtocolHandlerException
from cachetools import TTLCache
from websockets.exceptions import InvalidHandshake, InvalidURI
from websockets.legacy.client import connect
from websockets.typing import Subprotocol

import bumper

from ..util import get_logger

Expand Down Expand Up @@ -74,22 +74,27 @@ async def _handle_messages(self) -> None:
data = str(message.data.decode("utf-8"))

_LOGGER.info(
f"MQTT Proxy Client - Message Received From Ecovacs - Topic: {message.topic} - Message: {data}"
f"Message Received From Ecovacs - Topic: {message.topic} - Message: {data}"
)
topic = message.topic
ttopic = topic.split("/")
if ttopic[1] == "p2p":
if ttopic[3] == "proxyhelper":
_LOGGER.error(
f'"proxyhelper" was sender - INVALID!! Topic: {topic}'
)
continue

self.request_mapper[ttopic[10]] = ttopic[3]
ttopic[3] = "proxyhelper"
topic = "/".join(ttopic)
_LOGGER.info(
f"MQTT Proxy Client - Converted Topic From {message.topic} TO {topic}"
)
_LOGGER.info(f"Converted Topic From {message.topic} TO {topic}")

_LOGGER.info(
f"MQTT Proxy Client - Proxy Forward Message to Robot - Topic: {topic} - Message: {data}"
f"Proxy Forward Message to Robot - Topic: {topic} - Message: {data}"
)
await self._client.publish(topic, data.encode(), QOS_0)

bumper.mqtt_helperbot.publish(topic, message.data)
except Exception: # pylint: disable=broad-except
_LOGGER.error(
"An error occurred during handling a message", exc_info=True
Expand All @@ -105,7 +110,7 @@ async def publish(self, topic: str, message: bytes, qos: int | None = None) -> N
await self._client.publish(topic, message, qos)


class _NoCertVerifyClient(MQTTClient):
class _NoCertVerifyClient(MQTTClient): # type:ignore[misc]
"""
Mqtt client, which is not verify the certificate.
Expand Down
24 changes: 11 additions & 13 deletions bumper/mqtt/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ async def authenticate(self, session: Session, **kwargs: dict[str, Any]) -> bool
client_id = session.client_id

try:
if client_id == HELPER_BOT_CLIENT_ID:
mqttserverlog.info("Bumper Authentication Success - Helperbot")
return True

if "@" in client_id:
didsplit = str(client_id).split("@")
if not ( # if ecouser or bumper aren't in details it is a bot
"ecouser" in didsplit[1] or "bumper" in didsplit[1]
):
if "ecouser" not in didsplit[1]:
# if ecouser aren't in details it is a bot
tmpbotdetail = str(didsplit[1]).split("/")
bot_add(
username,
Expand Down Expand Up @@ -201,11 +204,6 @@ async def authenticate(self, session: Session, **kwargs: dict[str, Any]) -> bool
realm = tmpclientdetail[0]
resource = tmpclientdetail[1]

if userid == "helperbot":
mqttserverlog.info(
"Bumper Authentication Success - Helperbot: %s", client_id
)
return True
if check_authcode(didsplit[0], password) or not bumper.use_auth:
client_add(userid, realm, resource)
mqttserverlog.info(
Expand Down Expand Up @@ -342,35 +340,35 @@ async def on_broker_message_received( # pylint: disable=no-self-use
)
if ttopic[6] == "":
proxymodelog.warning(
"MQTT Proxy Client - Request mapper is missing entry, "
"Request mapper is missing entry, "
f"probably request took to long... Client_id: {client_id}"
f" - Request_id: {ttopic[10]}"
)
return

ttopic_join = "/".join(ttopic)
proxymodelog.info(
f"MQTT Proxy Client - Bot Message Converted Topic From {message.topic} TO {ttopic_join} "
f"Bot Message Converted Topic From {message.topic} TO {ttopic_join} "
f"with message: {data_decoded}"
)
else:
ttopic_join = message.topic
proxymodelog.info(
f"MQTT Proxy Client - Bot Message From {ttopic_join} with message: {data_decoded}"
f"Bot Message From {ttopic_join} with message: {data_decoded}"
)

try:
# Send back to ecovacs
proxymodelog.info(
"MQTT Proxy Client - Proxy Forward Message to Ecovacs - Topic:"
"Proxy Forward Message to Ecovacs - Topic:"
f" {ttopic_join} - Message: {data_decoded}"
)
await self._proxy_clients[client_id].publish(
ttopic_join, data_decoded.encode(), message.qos
)
except Exception: # pylint: disable=broad-except
proxymodelog.error(
"MQTT Proxy Client - Forwarding to Ecovacs - Exception",
"Forwarding to Ecovacs - Exception",
exc_info=True,
)

Expand Down

0 comments on commit e7f5d08

Please sign in to comment.