1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-10 18:00:12 +01:00

Add packet retransmission and timeout

UDP doesn't guarantee delivery, so reattempt packet transmission if we don't
get a response and timeout if we still don't have anything after (by
default) 10 seconds.
This commit is contained in:
Matthew Garrett 2016-11-20 11:16:28 -08:00
parent 7ac243b838
commit d066513d02

View File

@ -124,9 +124,10 @@ def discover(timeout=None):
devices.append(dev) devices.append(dev)
class device: class device:
def __init__(self, host, mac): def __init__(self, host, mac, timeout=10):
self.host = host self.host = host
self.mac = mac self.mac = mac
self.timeout = timeout
self.count = random.randrange(0xffff) self.count = random.randrange(0xffff)
self.key = bytearray([0x09, 0x76, 0x28, 0x34, 0x3f, 0xe9, 0x9e, 0x23, 0x76, 0x5c, 0x15, 0x13, 0xac, 0xcf, 0x8b, 0x02]) self.key = bytearray([0x09, 0x76, 0x28, 0x34, 0x3f, 0xe9, 0x9e, 0x23, 0x76, 0x5c, 0x15, 0x13, 0xac, 0xcf, 0x8b, 0x02])
self.iv = bytearray([0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58]) self.iv = bytearray([0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58])
@ -221,8 +222,17 @@ class device:
packet[0x20] = checksum & 0xff packet[0x20] = checksum & 0xff
packet[0x21] = checksum >> 8 packet[0x21] = checksum >> 8
starttime = time.time()
while True:
try:
self.cs.sendto(packet, self.host) self.cs.sendto(packet, self.host)
self.cs.settimeout(1)
response = self.cs.recvfrom(1024) response = self.cs.recvfrom(1024)
break
except socket.timeout:
if (time.time() - starttime) < self.timeout:
pass
raise
return response[0] return response[0]