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

Timeout improvements (#464)

- Only start to count the timer inside the lock.
- Improve precision of the timeout option.
- Use a context manager for the connection.
- Remove SO_REUSEADDR option.

Amend: Revert retry_intvl (#506)
This commit is contained in:
Felipe Martins Diel 2021-01-09 22:18:56 -03:00
parent d7cbe304e0
commit c3bb598e27
3 changed files with 34 additions and 24 deletions

View File

@ -70,13 +70,14 @@ def scan(
packet[0x20] = checksum & 0xFF packet[0x20] = checksum & 0xFF
packet[0x21] = checksum >> 8 packet[0x21] = checksum >> 8
starttime = time.time() start_time = time.time()
discovered = [] discovered = []
try: try:
while (time.time() - starttime) < timeout: while (time.time() - start_time) < timeout:
time_left = timeout - (time.time() - start_time)
conn.settimeout(min(1, time_left))
conn.sendto(packet, (discover_ip_address, discover_ip_port)) conn.sendto(packet, (discover_ip_address, discover_ip_port))
conn.settimeout(1)
while True: while True:
try: try:
@ -307,22 +308,22 @@ class device:
packet[0x20] = checksum & 0xFF packet[0x20] = checksum & 0xFF
packet[0x21] = checksum >> 8 packet[0x21] = checksum >> 8
start_time = time.time()
with self.lock: with self.lock:
conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as conn:
conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) timeout = self.timeout
start_time = time.time()
while True: while True:
try: time_left = timeout - (time.time() - start_time)
conn.settimeout(min(1, time_left))
conn.sendto(packet, self.host) conn.sendto(packet, self.host)
conn.settimeout(1)
resp, _ = conn.recvfrom(2048) try:
resp = conn.recvfrom(2048)[0]
break break
except socket.timeout: except socket.timeout:
if (time.time() - start_time) > self.timeout: if (time.time() - start_time) > timeout:
conn.close()
raise exception(-4000) # Network timeout. raise exception(-4000) # Network timeout.
conn.close()
if len(resp) < 0x30: if len(resp) < 0x30:
raise exception(-4007) # Length error. raise exception(-4007) # Length error.

View File

@ -58,7 +58,9 @@ class rm(device):
def check_sensors(self) -> dict: def check_sensors(self) -> dict:
"""Return the state of the sensors.""" """Return the state of the sensors."""
resp = self._send(0x1) resp = self._send(0x1)
return {"temperature": resp[0x0] + resp[0x1] / 10.0} temperature = struct.unpack("<bb", resp[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 10.0
return {"temperature": temperature}
class rm4(rm): class rm4(rm):
@ -90,7 +92,7 @@ class rm4(rm):
def check_sensors(self) -> dict: def check_sensors(self) -> dict:
"""Return the state of the sensors.""" """Return the state of the sensors."""
resp = self._send(0x24) resp = self._send(0x24)
return { temperature = struct.unpack("<bb", resp[:0x2])
"temperature": resp[0x0] + resp[0x1] / 100.0, temperature = temperature[0x0] + temperature[0x1] / 100.0
"humidity": resp[0x2] + resp[0x3] / 100.0, humidity = resp[0x2] + resp[0x3] / 100.0
} return {"temperature": temperature, "humidity": humidity}

View File

@ -1,4 +1,6 @@
"""Support for sensors.""" """Support for sensors."""
import struct
from .device import device from .device import device
from .exceptions import check_error from .exceptions import check_error
@ -33,10 +35,15 @@ class a1(device):
response = self.send_packet(0x6A, packet) response = self.send_packet(0x6A, packet)
check_error(response[0x22:0x24]) check_error(response[0x22:0x24])
payload = self.decrypt(response[0x38:]) payload = self.decrypt(response[0x38:])
data = bytearray(payload[0x4:]) data = payload[0x4:]
temperature = struct.unpack("<bb", data[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 10.0
humidity = data[0x2] + data[0x3] / 10.0
return { return {
"temperature": data[0x0] + data[0x1] / 10.0, "temperature": temperature,
"humidity": data[0x2] + data[0x3] / 10.0, "humidity": humidity,
"light": data[0x4], "light": data[0x4],
"air_quality": data[0x6], "air_quality": data[0x6],
"noise": data[0x8], "noise": data[0x8],