Skip to content

Commit

Permalink
Modbus.read_group() calls get_all()
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jan 15, 2024
1 parent 3accc6b commit dad8ebd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 🚀 New

* Added a cache to the registers with default timeout 1 second.
* `Modbus.read_group()` calls `Modbus.get_all()` instead of reading individual registers sequentially. Since during a `status` all groups are read in quick succession, and with caching, this results in much faster status outputs.

### ✨ Improved

Expand Down
2 changes: 1 addition & 1 deletion python/lvmecp/hvac.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HVACController(PLCModule):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.status: dict[str, float | bool] = {}
self.status: dict[str, float | bool | None] = {}

async def _update_internal(self):
"""Update status."""
Expand Down
25 changes: 14 additions & 11 deletions python/lvmecp/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ async def get_all(self):
results = await asyncio.gather(*tasks, return_exceptions=True)

if any([isinstance(result, Exception) for result in results]):
for result in results:
for ii, result in enumerate(results):
if isinstance(result, Exception):
log.warning(f"Failed retrieving value for {names[ii]!r}")
results[ii] = None

registers = cast(
dict[str, int | float | None],
{names[ii]: results[ii] for ii in range(len(names))},
Expand All @@ -351,15 +354,15 @@ async def get_all(self):
async def read_group(self, group: str):
"""Returns a dictionary of all read registers that match a ``group``."""

names = []
tasks = []
async with self:
for name in self:
register = self[name]
if register.group is not None and register.group == group:
names.append(name)
tasks.append(register.get(open_connection=False))
registers = await self.get_all()

group_registers = {}
for name in self:
register = self[name]
if register.group is not None and register.group == group:
if name not in registers:
continue

results = await asyncio.gather(*tasks)
group_registers[name] = registers[name]

return dict(zip(names, results))
return group_registers

0 comments on commit dad8ebd

Please sign in to comment.