Skip to content

Commit

Permalink
Create helper function to decode discovery message
Browse files Browse the repository at this point in the history
Includes test for that decoder function
  • Loading branch information
shepherdjay committed Oct 5, 2018
1 parent a80bfb8 commit e0e469e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ venv.bak/
.mypy_cache/

# editor configs and cache
.vscode
.vscode

# ide stuff
/.idea
33 changes: 16 additions & 17 deletions senseme/senseme.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import socket
import time
from typing import Tuple

from .lib import MWT, BackgroundLoop
from .lib.xml import data_to_xml
Expand Down Expand Up @@ -65,7 +66,6 @@ def __init__(self, ip="", name="", model="", series="", mac="", **kwargs):
self.ip = ip
self.name = name
self.mac = mac
self.details = ""
self.model = model
self.series = series
self.monitor_frequency = kwargs.get("monitor_frequency", 45)
Expand Down Expand Up @@ -283,14 +283,7 @@ def discover_single_device(self):
if not m:
LOGGER.error("Didn't receive response.")
else:
self.details = m[0].decode("utf-8")
res = re.match("\((.*);DEVICE;ID;(.*);(.*),(.*)\)", self.details)
# TODO: Parse this properly rather than regex
self.name = res.group(1)
self.mac = res.group(2)
self.model = res.group(3)
self.series = res.group(4)
self.ip = m[1][0]
self.name, self.mac, self.model, self.series, self.ip = decode_discovery(m)
LOGGER.info(self.name, self.mac, self.model, self.series)
except OSError as e:
LOGGER.critical("No device was found.\n%s" % e)
Expand Down Expand Up @@ -584,14 +577,7 @@ def discover(devices_to_find=3, time_to_wait=5):
message = b""
if message:
LOGGER.info("Received a message")
message_decoded = message[0].decode("utf-8")
res = re.match("\((.*);DEVICE;ID;(.*);(.*),(.*)\)", message_decoded)
# TODO: Parse this properly rather than regex
name = res.group(1)
mac = res.group(2)
model = res.group(3)
series = res.group(4)
ip = message[1][0]
name, mac, model, series, ip = decode_discovery(message)
devices.append(
SenseMe(ip=ip, name=name, model=model, series=series, mac=mac)
)
Expand All @@ -609,3 +595,16 @@ def discover(devices_to_find=3, time_to_wait=5):
raise OSError("Couldn't get port 31415")
finally:
s.close()


def decode_discovery(message: Tuple[bytes, Tuple[str, str]]) -> Tuple:
""" Takes a message in the return format of socket.recvfrom and returns decoded SenseMe discovery information """
message_decoded = message[0].decode("utf-8")
res = re.match("\((.*);DEVICE;ID;(.*);(.*),(.*)\)", message_decoded)
# TODO: Parse this properly rather than regex
name = res.group(1)
mac = res.group(2)
model = res.group(3)
series = res.group(4)
ip = message[1][0]
return name, mac, model, series, ip
7 changes: 7 additions & 0 deletions tests/test_senseme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from senseme.senseme import decode_discovery


def test_discovery_message_decoding():
test_message = ("(NAME;DEVICE;ID;MAC;MODEL,SERIES)".encode("utf-8"), ("127.0.0.1", "3101"))
name, mac, model, series, ip = decode_discovery(test_message)
assert ("NAME", "MAC", "MODEL", "SERIES", "127.0.0.1") == (name, mac, model, series, ip)

0 comments on commit e0e469e

Please sign in to comment.