Skip to content

Commit

Permalink
Commands send to the EVL that timeout will now trigger a disconnect u…
Browse files Browse the repository at this point in the history
…nder the assumption that the EVL has become unresponsive (as it is prone to do).
  • Loading branch information
ufodone committed Jan 15, 2023
1 parent 8c8b54f commit ba3d043
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ async def read_loop(self):

# Lost connection so reattempt connection in a bit
if not self._shutdown:
reconnect_time = 30
# TODO: implement exponential backoff
reconnect_time = 10
_LOGGER.error("Reconnection attempt in %ds", reconnect_time)
await asyncio.sleep(reconnect_time)

Expand Down Expand Up @@ -171,13 +172,23 @@ async def connect(self):
async def disconnect(self):
"""Internal method for forcing connection closure if hung."""
_LOGGER.debug('Cleaning up from disconnection with server.')

self._loggedin = False

# Fail all outstanding commands
for op in self._commandQueue:
op.state = self.Operation.State.FAILED

# Tear down the connection
if self._writer:
self._writer.close()
await self._writer.wait_closed()
self._writer = None
if self._reader:
self._reader = None
try:
self._writer.close()
await self._writer.wait_closed()
except Exception as ex:
_LOGGER.error("Exception while closing connection: %s", ex)

self._writer = None
self._reader = None

async def send_data(self, data):
"""Raw data send- just make sure it's encoded properly and logged."""
Expand Down Expand Up @@ -368,6 +379,7 @@ async def queue_command(self, cmd, data, code = None):
self._commandQueue.append(op)
self._commandEvent.set()
await op.responseEvent.wait()
return op.state == op.State.SUCCEEDED

async def process_command_queue(self):
"""Manage processing of commands to be issued to the EVL. Commands are serialized to the EVL to avoid
Expand All @@ -393,9 +405,12 @@ async def process_command_queue(self):
if op.state == self.Operation.State.SENT:
# Still waiting on a response from the EVL so break out of loop and wait for the response
if now >= op.expiryTime:
# Timeout waiting for response from the EVL so fail the command
# Timeout waiting for response from the EVL so fail the command,
# This is likely due to the EVL becoming unresponsive so tear down the
# connection to start a recovery.
_LOGGER.error(f"Command '{op.cmd}' failed due to timeout waiting for response from EVL")
op.state = self.Operation.State.FAILED
await self.disconnect()
break
elif op.state == self.Operation.State.QUEUED:
# Send command to the EVL
Expand All @@ -404,7 +419,7 @@ async def process_command_queue(self):
try:
await self.send_command(op.cmd, op.data)
except Exception as ex:
_LOGGER.error(f"Unexpected exception trying to sent command: {ex}")
_LOGGER.error(f"Unexpected exception trying to send command: {ex}")
op.state = self.Operation.State.FAILED
elif op.state == self.Operation.State.SUCCEEDED:
# Remove completed command from head of the queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async def dump_zone_timers(self):
async def keypresses_to_partition(self, partitionNumber, keypresses):
"""Send keypresses to a particular partition."""
for char in keypresses:
await self.queue_command(evl_Commands['PartitionKeypress'], str.format("{0},{1}", partitionNumber, char))
result = await self.queue_command(evl_Commands['PartitionKeypress'], str.format("{0},{1}", partitionNumber, char))
if not result:
break

async def arm_stay_partition(self, code, partitionNumber):
"""Public method to arm/stay a partition."""
Expand Down Expand Up @@ -83,11 +85,15 @@ def parseHandler(self, rawInput):
else:
rawInput = re.sub("[\r\n]", "", rawInput)

# Nothing left to process after stripping the line breaks
if not rawInput:
return (None, None)

# Look for a sentinel
m = re.match("[%\^]", rawInput)
if m is None:
# No sentinels so ignore the data
_LOGGER.error("Unrecognized data recieved from the envisalink. Ignoring: '%s'", rawInput)
_LOGGER.error("Unrecognized data received from the envisalink. Ignoring: '%s'", rawInput)
return (None, None)

start_idx = m.start(0)
Expand Down

0 comments on commit ba3d043

Please sign in to comment.