Skip to content

Commit 2cf973c

Browse files
committed
Moved reconnect to main
1 parent e0fab6d commit 2cf973c

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

lib/device.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
PH803W_PING_INTERVAL = 4
99
RECONNECT_DELAY = 10
1010
# RESPONSE_TIMEOUT = 5000
11-
ABORT_AFTER_CONSECUTIVE_EMPTY = 10
11+
ABORT_AFTER_CONSECUTIVE_EMPTY = 30
1212

1313
_LOGGER = logging.getLogger(__name__)
1414

@@ -22,7 +22,6 @@ def __init__(self, host):
2222
self.host = host
2323
self.passcode = ""
2424
self._measurements = []
25-
self._measurements_counter = 0
2625
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2726
self._loop = True
2827
self._empty_counter = 0
@@ -36,20 +35,9 @@ def run(self, once: bool = True) -> bool:
3635
self._connect()
3736
return self._run(once)
3837
else:
39-
measurements = -1
40-
while self._loop:
41-
if measurements == self._measurements_counter:
42-
_LOGGER.error("Aborting reconnects, no new measurements")
43-
break
44-
measurements = self._measurements_counter
45-
self._connect()
46-
try:
47-
self._run(once)
48-
except:
49-
_LOGGER.warning("Exception in run loop")
50-
self.close()
51-
sleep(RECONNECT_DELAY)
52-
return not self._loop and self._measurements_counter > 0
38+
self._connect()
39+
self._run(once)
40+
return not self._loop
5341

5442
def _connect(self) -> bool:
5543
self._loop = True
@@ -99,8 +87,9 @@ def _run(self, once: bool = True) -> bool:
9987
_LOGGER.error("Too many empty consecutive packages")
10088
raise DeviceError("Too many empty consecutive packages")
10189
if len(response) == 0:
102-
_LOGGER.debug(self._empty_bar() + "Empty message received")
10390
self._empty_counter += 1
91+
if self._empty_counter % 10 == 0:
92+
_LOGGER.warning("%s %s empty messages received" % (self._empty_bar(), self._empty_counter))
10493
continue
10594
self._empty_counter = 0
10695

@@ -165,10 +154,9 @@ def _handle_data_response(self, data):
165154
if len(data) == 18:
166155
meas = Measurement(data)
167156
self._measurements.append(meas)
168-
self._measurements_counter += 1
169157
else:
170158
pass
171-
_LOGGER.debug(self._empty_bar() + str(meas))
159+
_LOGGER.debug(meas)
172160

173161
def _handle_data_extended_response(self, data):
174162
pass
@@ -180,17 +168,17 @@ def _handle_ping_pong_response(self):
180168
# pass
181169
# else:
182170
# _LOGGER.debug("Pong thread alredy running")
183-
_LOGGER.debug(self._empty_bar() + "Pong message received")
171+
_LOGGER.debug("Pong message received")
184172

185173
def _send_ping(self):
186174
pong_data = bytes.fromhex("0000000303000015")
187175
self._socket.sendall(pong_data)
188-
_LOGGER.debug(self._empty_bar() + "Ping sent")
176+
_LOGGER.debug("Ping sent")
189177

190178
def _ping_loop(self):
191179
while self._loop:
192-
sleep(PH803W_PING_INTERVAL)
193180
self._send_ping()
181+
sleep(PH803W_PING_INTERVAL)
194182

195183
# async def _async_queue_ping(self):
196184
# await asyncio.sleep(PH803W_PING_INTERVAL)
@@ -200,6 +188,7 @@ def abort(self):
200188
self._loop = False
201189

202190
def close(self):
191+
self._loop = False
203192
try:
204193
self._socket.close()
205194
except:

lib/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
except:
1818
result = discovery.DeviceDiscovery('192.168.1.89', None)
1919

20-
dev = device.Device(result.ip)
21-
# listener = loop.create_task(dev.run_async())
22-
# asyncio.wait([listener])
23-
loop.run_until_complete(dev.run_async(once=False))
24-
#print(dev.get_result())
20+
while True:
21+
try:
22+
with device.Device(result.ip) as dev:
23+
loop.run_until_complete(dev.run_async(once=False))
24+
except:
25+
_LOGGER.error("Exception in run loop, restarting...")
2526

2627
loop.close()

0 commit comments

Comments
 (0)