Skip to content

Commit

Permalink
Properly handle unknown broadcast messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Orhideous committed Feb 12, 2024
1 parent e5a2cba commit aa7102a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions roombapy/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,15 @@ def _get_response(self, ip=None):
self.log.debug(
"Received response: %s, address: %s", raw_response, addr
)
data = raw_response.decode()
if self._is_from_irobot(data):
return _decode_data(data)
response = _decode_data(raw_response)
if response is None:
continue
else:
return response
except socket.timeout:
self.log.info("Socket timeout")
return None

def _is_from_irobot(self, data):
if data == self.roomba_message:
return False

json_response = orjson.loads(data)
if (
"Roomba" in json_response["hostname"]
or "iRobot" in json_response["hostname"]
):
return True

return False

def _broadcast_message(self, amount):
for i in range(amount):
self.server_socket.sendto(
Expand All @@ -89,8 +78,29 @@ def _start_server(self):
self.log.debug("Socket server started, port %s", self.udp_port)


def _decode_data(data):
json_response = orjson.loads(data)
def _decode_data(raw_response):
print(raw_response)
try:
data = raw_response.decode()
except UnicodeDecodeError as e:
# Unknown ND response (routers, etc.)
return None

if data == RoombaDiscovery.roomba_message:
# Filter our own messages
return None

try:
json_response = orjson.loads(data)
except:
# Failed to parse JSON
return None

hostname = json_response.get("hostname", "")
if ("Roomba" not in hostname) and ("iRobot" not in hostname):
# Unknown payload
return None

return RoombaInfo(
hostname=json_response["hostname"],
robot_name=json_response["robotname"],
Expand Down

0 comments on commit aa7102a

Please sign in to comment.