Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions in client._task_network_consumer #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions GivTCP/givenergy_modbus_async/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,47 +328,53 @@ async def one_shot_command(

async def _task_network_consumer(self):
"""Task for orchestrating incoming data."""
while hasattr(self, "reader") and self.reader and not self.reader.at_eof():
frame = await self.reader.read(300)
# await self.debug_frames['all'].put(frame)
for message in self.framer.decode(frame):
_logger.debug("Processing %s", message)
if isinstance(message, ExceptionBase):
_logger.warning(
"Expected response never arrived but resulted in exception: %s",
message,
)
continue
if isinstance(message, HeartbeatRequest):
_logger.debug("Responding to HeartbeatRequest")
await self.tx_queue.put(
(message.expected_response().encode(), None)
)
continue
if not isinstance(message, TransparentResponse):
_logger.warning(
"Received unexpected message type for a client: %s", message
)
continue
if isinstance(message, WriteHoldingRegisterResponse):
if message.error:
_logger.warning("%s", message)
else:
_logger.info("%s", message)

future = self.expected_responses.get(message.shape_hash())

if future and not future.done():
future.set_result(message)
# try:
self.plant.update(message)
# except RegisterCacheUpdateFailed as e:
# # await self.debug_frames['error'].put(frame)
# _logger.debug(f'Ignoring {message}: {e}')
_logger.debug(
"network_consumer reader at EOF, cannot continue, closing connection"
)
await self.close()
try:
while hasattr(self, "reader") and self.reader and not self.reader.at_eof():
frame = await self.reader.read(300)
# await self.debug_frames['all'].put(frame)
for message in self.framer.decode(frame):
_logger.debug("Processing %s", message)
if isinstance(message, ExceptionBase):
_logger.warning(
"Expected response never arrived but resulted in exception: %s",
message,
)
continue
if isinstance(message, HeartbeatRequest):
_logger.debug("Responding to HeartbeatRequest")
await self.tx_queue.put(
(message.expected_response().encode(), None)
)
continue
if not isinstance(message, TransparentResponse):
_logger.warning(
"Received unexpected message type for a client: %s", message
)
continue
if isinstance(message, WriteHoldingRegisterResponse):
if message.error:
_logger.warning("%s", message)
else:
_logger.info("%s", message)

future = self.expected_responses.get(message.shape_hash())

if future and not future.done():
future.set_result(message)
# try:
self.plant.update(message)
# except RegisterCacheUpdateFailed as e:
# # await self.debug_frames['error'].put(frame)
# _logger.debug(f'Ignoring {message}: {e}')
_logger.debug(
"network_consumer reader at EOF, cannot continue, closing connection"
)
except Exception as e:
_logger.error(
"network_consumer reader exception {}", e
)
finally:
await self.close()

async def _task_network_producer(self, tx_message_wait: float = 0.25):
"""Producer loop to transmit queued frames with an appropriate delay."""
Expand Down