Skip to content

Commit 837bc61

Browse files
Merge pull request #5 from seoyoungjin/master
additional discovery info
2 parents 52d7cda + bd0bb36 commit 837bc61

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

orvibo/s20.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Orbivo S20. """
22

33
import binascii
4+
import struct
45
import logging
56
import socket
67
import threading
@@ -58,23 +59,36 @@ def _setup():
5859
udp.start()
5960

6061

62+
def _device_time(tab):
63+
ts = struct.unpack('<L', tab)[0] - 2208988800
64+
return ts
65+
66+
6167
def discover(timeout=DISCOVERY_TIMEOUT):
6268
""" Discover devices on the local network.
6369
6470
:param timeout: Optional timeout in seconds.
6571
:returns: Set of discovered host addresses.
6672
"""
67-
hosts = set()
73+
hosts = {}
6874
payload = MAGIC + DISCOVERY
6975
for _ in range(RETRIES):
7076
_SOCKET.sendto(bytearray(payload), ('255.255.255.255', PORT))
7177
start = time.time()
7278
while time.time() < start + timeout:
7379
for host, data in _BUFFER.copy().items():
74-
if _is_discovery_response(data):
75-
if host not in hosts:
76-
_LOGGER.debug("Discovered device at %s", host)
77-
hosts.add(host)
80+
if not _is_discovery_response(data):
81+
continue
82+
if host not in hosts:
83+
_LOGGER.debug("Discovered device at %s", host)
84+
entry = {}
85+
entry['mac'] = data[7:13]
86+
entry['imac'] = data[19:25]
87+
entry['next'] = 0
88+
entry['st'] = int(data[-1])
89+
entry['time'] = _device_time(data[37:41])
90+
entry['serverTime'] = int(time.time())
91+
hosts[host] = entry
7892
return hosts
7993

8094

@@ -114,13 +128,22 @@ class S20(object):
114128
115129
Protocol documentation: http://pastebin.com/LfUhsbcS
116130
"""
117-
def __init__(self, host):
131+
def __init__(self, host, mac = None):
118132
""" Initialize S20 object.
119133
120134
:param host: IP or hostname of device.
121135
"""
122136
self.host = host
123-
(self._mac, self._mac_reversed) = self._discover_mac()
137+
if not mac:
138+
(self._mac, self._mac_reversed) = self._discover_mac()
139+
else:
140+
if type(mac) is str:
141+
self._mac = binascii.a2b_hex(''.join(mac.split(':')))
142+
else:
143+
self._mac = mac
144+
ba = bytearray(self._mac)
145+
ba.reverse()
146+
self._mac_reversed = bytes(ba)
124147
self._subscribe()
125148

126149
@property

0 commit comments

Comments
 (0)