Skip to content

Commit 07101f5

Browse files
author
happyleavesaoc
committed
2 parents f4b7188 + 837bc61 commit 07101f5

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
@@ -57,23 +58,36 @@ def _setup():
5758
udp.start()
5859

5960

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

7993

@@ -113,13 +127,22 @@ class S20(object):
113127
114128
Protocol documentation: http://pastebin.com/LfUhsbcS
115129
"""
116-
def __init__(self, host):
130+
def __init__(self, host, mac = None):
117131
""" Initialize S20 object.
118132
119133
:param host: IP or hostname of device.
120134
"""
121135
self.host = host
122-
(self._mac, self._mac_reversed) = self._discover_mac()
136+
if not mac:
137+
(self._mac, self._mac_reversed) = self._discover_mac()
138+
else:
139+
if type(mac) is str:
140+
self._mac = binascii.a2b_hex(''.join(mac.split(':')))
141+
else:
142+
self._mac = mac
143+
ba = bytearray(self._mac)
144+
ba.reverse()
145+
self._mac_reversed = bytes(ba)
123146
self._subscribe()
124147

125148
@property

0 commit comments

Comments
 (0)