Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Raise minimum Python version to `3.11` (Debian Bookworm and newer).
- Switch to `pyproject.toml` with the standard `setuptools` build-backend.
- Switch to using [uv](https://docs.astral.sh/uv/) for managing the code.
- Add decoding for battery cell status and battery module statistics

### Documentation

Expand Down
44 changes: 42 additions & 2 deletions src/rctclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# SPDX-License-Identifier: GPL-3.0-only

import logging
import json
import select
import socket
import sys
Expand Down Expand Up @@ -99,6 +100,44 @@ def receive_frame(sock: socket.socket, timeout: int = 2) -> ReceiveFrame:
raise TimeoutError


def _format_cli_value(data_type: DataType, value: object) -> str:
'''
Formats decoded values for CLI output.
'''
if data_type == DataType.BATTERY_MODULE_STATUS:
# Compact JSON representation keyed by cell id.
return json.dumps(
{
str(cell_id): {
'temperature_c': cell.temperature_c,
'voltage_v': cell.voltage_v,
}
for cell_id, cell in sorted(value.cells.items())
},
separators=(',', ':'),
)
if data_type == DataType.BATTERY_MODULE_STATISTICS:
def _encode_extreme(extreme: object, unit: str) -> dict[str, object]:
timestamp = extreme.timestamp.isoformat() if extreme.timestamp is not None else None
return {
'cell': extreme.cell,
'timestamp': timestamp,
'value': extreme.value,
'unit': unit,
}

return json.dumps(
{
'u_min': _encode_extreme(value.u_min, 'V'),
'u_max': _encode_extreme(value.u_max, 'V'),
't_min': _encode_extreme(value.t_min, 'degC'),
't_max': _encode_extreme(value.t_max, 'degC'),
},
separators=(',', ':'),
)
return str(value)


@cli.command('read-value')
@click.pass_context
@click.option('-p', '--port', default=8899, type=click.INT, help='Port at which the device listens, default 8899',
Expand Down Expand Up @@ -216,10 +255,11 @@ def read_value(ctx, port: int, host: str, id: Optional[str], name: Optional[str]
if verbose:
description = oinfo.description if oinfo.description is not None else ''
unit = oinfo.unit if oinfo.unit is not None else ''
value_out = _format_cli_value(oinfo.response_data_type, value)
click.echo(f'#{oinfo.index:3} 0x{oinfo.object_id:8X} {oinfo.name:{R.name_max_length()}} '
f'{description:75} {value} {unit}')
f'{description:75} {value_out} {unit}')
else:
click.echo(f'{value}')
click.echo(_format_cli_value(oinfo.response_data_type, value))

try:
sock.close()
Expand Down
Loading