mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 14:43:30 +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:
parent
d7cbe304e0
commit
c3bb598e27
@ -70,13 +70,14 @@ def scan(
|
||||
packet[0x20] = checksum & 0xFF
|
||||
packet[0x21] = checksum >> 8
|
||||
|
||||
starttime = time.time()
|
||||
start_time = time.time()
|
||||
discovered = []
|
||||
|
||||
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.settimeout(1)
|
||||
|
||||
while True:
|
||||
try:
|
||||
@ -307,22 +308,22 @@ class device:
|
||||
packet[0x20] = checksum & 0xFF
|
||||
packet[0x21] = checksum >> 8
|
||||
|
||||
start_time = time.time()
|
||||
with self.lock:
|
||||
conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as conn:
|
||||
timeout = self.timeout
|
||||
start_time = time.time()
|
||||
|
||||
while True:
|
||||
try:
|
||||
while True:
|
||||
time_left = timeout - (time.time() - start_time)
|
||||
conn.settimeout(min(1, time_left))
|
||||
conn.sendto(packet, self.host)
|
||||
conn.settimeout(1)
|
||||
resp, _ = conn.recvfrom(2048)
|
||||
break
|
||||
except socket.timeout:
|
||||
if (time.time() - start_time) > self.timeout:
|
||||
conn.close()
|
||||
raise exception(-4000) # Network timeout.
|
||||
conn.close()
|
||||
|
||||
try:
|
||||
resp = conn.recvfrom(2048)[0]
|
||||
break
|
||||
except socket.timeout:
|
||||
if (time.time() - start_time) > timeout:
|
||||
raise exception(-4000) # Network timeout.
|
||||
|
||||
if len(resp) < 0x30:
|
||||
raise exception(-4007) # Length error.
|
||||
|
@ -58,7 +58,9 @@ class rm(device):
|
||||
def check_sensors(self) -> dict:
|
||||
"""Return the state of the sensors."""
|
||||
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):
|
||||
@ -90,7 +92,7 @@ class rm4(rm):
|
||||
def check_sensors(self) -> dict:
|
||||
"""Return the state of the sensors."""
|
||||
resp = self._send(0x24)
|
||||
return {
|
||||
"temperature": resp[0x0] + resp[0x1] / 100.0,
|
||||
"humidity": resp[0x2] + resp[0x3] / 100.0,
|
||||
}
|
||||
temperature = struct.unpack("<bb", resp[:0x2])
|
||||
temperature = temperature[0x0] + temperature[0x1] / 100.0
|
||||
humidity = resp[0x2] + resp[0x3] / 100.0
|
||||
return {"temperature": temperature, "humidity": humidity}
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Support for sensors."""
|
||||
import struct
|
||||
|
||||
from .device import device
|
||||
from .exceptions import check_error
|
||||
|
||||
@ -33,10 +35,15 @@ class a1(device):
|
||||
response = self.send_packet(0x6A, packet)
|
||||
check_error(response[0x22:0x24])
|
||||
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 {
|
||||
"temperature": data[0x0] + data[0x1] / 10.0,
|
||||
"humidity": data[0x2] + data[0x3] / 10.0,
|
||||
"temperature": temperature,
|
||||
"humidity": humidity,
|
||||
"light": data[0x4],
|
||||
"air_quality": data[0x6],
|
||||
"noise": data[0x8],
|
||||
|
Loading…
Reference in New Issue
Block a user