-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat(messages): add OnMapTrace for mower firmware (LZMA-wrapped variant) #1567
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
Open
Beennnn
wants to merge
5
commits into
DeebotUniverse:dev
Choose a base branch
from
Beennnn:feat/parse-mower-getmaptrace-fw-1.15
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd16a78
feat(messages): add OnMapTrace for mower firmware (LZMA-wrapped variant)
Beennnn d88f690
review: use static pre-encoded test data, fix CodeQL logging
Beennnn 5db2ba5
feat(map): add MowerMapTrace renderer for trace points
Beennnn 6fed632
fix: ruff lint — use orjson, rename loop var, restore except parens
Beennnn 3afd611
fix: wrap multi-exception except with fmt:off to dodge ruff 0.15.11 bug
Beennnn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Mower trajectory accumulator and SVG renderer. | ||
|
|
||
| Mowers (e.g. Ecovacs GOAT family) do not expose the regular ``map`` | ||
| capability used by vacuums, but their firmware pushes trajectory points | ||
| through :class:`~deebot_client.events.map.MapTraceEvent`. This module | ||
| keeps the parsing, accumulation and rendering of those points in one | ||
| place so consumers (e.g. the Home Assistant integration) only have to | ||
| forward the event payload and read back an SVG. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| class MowerMapTrace: | ||
| """Accumulator and SVG renderer for mower trajectory traces.""" | ||
|
|
||
| MAX_POINTS = 5000 | ||
|
|
||
| _STROKE_COLOR = "#1976d2" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._points: list[tuple[int, int]] = [] | ||
|
|
||
| @property | ||
| def has_points(self) -> bool: | ||
| """Return whether any trace points have been accumulated.""" | ||
| return bool(self._points) | ||
|
|
||
| def clear(self) -> None: | ||
| """Drop all accumulated trace points.""" | ||
| self._points.clear() | ||
|
|
||
| def add_data(self, raw: str) -> int: | ||
| """Parse a ``MapTraceEvent.data`` string and accumulate points. | ||
|
|
||
| Tokens are ``"x,y"`` separated by ``";"``. Malformed tokens are | ||
| skipped silently. The accumulator keeps at most :attr:`MAX_POINTS` | ||
| points (FIFO drop). Returns the number of points actually added. | ||
| """ | ||
| new_points: list[tuple[int, int]] = [] | ||
| for raw_token in raw.split(";"): | ||
| token = raw_token.strip() | ||
| if not token: | ||
| continue | ||
| try: | ||
| x_str, y_str = token.split(",") | ||
| new_points.append((int(x_str), int(y_str))) | ||
| except ValueError: | ||
| continue | ||
|
|
||
| if not new_points: | ||
| return 0 | ||
|
|
||
| self._points.extend(new_points) | ||
| if len(self._points) > self.MAX_POINTS: | ||
| self._points = self._points[-self.MAX_POINTS :] | ||
| return len(new_points) | ||
|
|
||
| def to_svg(self) -> str | None: | ||
| """Render the accumulated trace as an SVG polyline. | ||
|
|
||
| Returns ``None`` when no points have been accumulated yet. | ||
| """ | ||
| if not self._points: | ||
| return None | ||
|
|
||
| xs = [p[0] for p in self._points] | ||
| ys = [p[1] for p in self._points] | ||
| min_x, max_x = min(xs), max(xs) | ||
| min_y, max_y = min(ys), max(ys) | ||
|
|
||
| # 5% padding on the larger of the two dimensions, with a 50-unit floor | ||
| # so a near-zero-area trace still has visible margin. | ||
| padding = max(50, max(max_x - min_x, max_y - min_y) // 20) | ||
| min_x -= padding | ||
| max_x += padding | ||
| min_y -= padding | ||
| max_y += padding | ||
|
|
||
| width = max_x - min_x | ||
| height = max_y - min_y | ||
|
|
||
| # Mower coordinates use bottom-up Y; flip for SVG top-down rendering. | ||
| flipped = " ".join(f"{x},{max_y + min_y - y}" for x, y in self._points) | ||
|
|
||
| # Stroke scales with width so the line stays visible on tiny lawns | ||
| # and isn't a hairline on huge ones. | ||
| stroke_width = max(20, width // 200) | ||
|
|
||
| return ( | ||
| f'<svg xmlns="http://www.w3.org/2000/svg" ' | ||
| f'viewBox="{min_x} {min_y} {width} {height}" ' | ||
| f'preserveAspectRatio="xMidYMid meet">' | ||
| f'<polyline points="{flipped}" fill="none" ' | ||
| f'stroke="{self._STROKE_COLOR}" stroke-width="{stroke_width}" ' | ||
| f'stroke-linejoin="round" stroke-linecap="round"/>' | ||
| f"</svg>" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """Tests for OnMapTrace — mower variant with LZMA-compressed info field.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from deebot_client.events import FirmwareEvent | ||
| from deebot_client.events.map import MapTraceEvent | ||
| from deebot_client.message import HandlingState | ||
| from deebot_client.messages.json.map import OnMapTrace | ||
| from tests.messages.json import assert_message | ||
|
|
||
| # Static pre-encoded payloads (LZMA1 with trimmed header, base64). | ||
| # Generated from real GOAT A1600 RTK firmware 1.15.x encoding format. | ||
|
|
||
| # '[["7","0;100,200;150,250;"]]' (28 bytes uncompressed) | ||
| _SINGLE_GROUP = "XQAABAAcAAAAAC3ghG4jMKGNRtkww/d7MLX33z8usOwaHU2B7///wFIAAA==" | ||
|
|
||
| # '[["5","0;-11850,-28849;-11800,-28899;","0;-12850,-23699;-12800,-23750;"],["6","0;-7899,-39700;-7950,-39649;"]]' (110 bytes) | ||
| _MULTI_GROUP = "XQAABABuAAAAAC3ghGojMKGNRtiHVVsAoT/QJyYK0+w8iNNfkK1fJciMh2LrturUwS3TNs8H+7FN7dV1zViWeRKpxrkDUNNQG/4bayp4L33u+jJYgA==" | ||
|
|
||
| # '[]' (2 bytes) | ||
| _EMPTY_GROUPS = "XQAABAACAAAAAC2XXP/////wAAAA" | ||
|
|
||
| # '[["1","0;1,2;3,4;"]]' (20 bytes) | ||
| _SERIAL_TEST = "XQAABAAUAAAAAC3ghGIjMKGNR5N7rKaBA7QSJbYEb82/P//2SwAA" | ||
|
|
||
| # b'not json content' compressed — decompresses to non-JSON | ||
| _NON_JSON = "XQAABAAQAAAAADcbyuolm7SQrGkrEp4K2Iwi8deA//3qYAA=" | ||
|
|
||
|
|
||
| def _envelope(info_b64: str, info_size: int, fw: str = "1.15.13") -> dict: | ||
| return { | ||
| "header": { | ||
| "pri": 1, | ||
| "tzm": 120, | ||
| "ts": "1777450352860644879", | ||
| "fwVer": fw, | ||
| "hwVer": "0.0.0", | ||
| }, | ||
| "body": { | ||
| "data": { | ||
| "mid": "123456789", | ||
| "batid": "hmfald", | ||
| "serial": "1", | ||
| "index": "0", | ||
| "type": "4", | ||
| "info": info_b64, | ||
| "infoSize": info_size, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def test_OnMapTrace_decompresses_and_flattens_groups() -> None: | ||
| assert_message( | ||
| OnMapTrace, | ||
| _envelope(_SINGLE_GROUP, 28), | ||
| ( | ||
| FirmwareEvent("1.15.13"), | ||
| MapTraceEvent(start=1, total=1, data="100,200;150,250"), | ||
| ), | ||
| device_class="xmp9ds", | ||
| ) | ||
|
|
||
|
|
||
| def test_OnMapTrace_concatenates_multiple_groups_and_segments() -> None: | ||
| expected_trace = "-11850,-28849;-11800,-28899;-12850,-23699;-12800,-23750;-7899,-39700;-7950,-39649" | ||
| assert_message( | ||
| OnMapTrace, | ||
| _envelope(_MULTI_GROUP, 110), | ||
| ( | ||
| FirmwareEvent("1.15.13"), | ||
| MapTraceEvent(start=1, total=1, data=expected_trace), | ||
| ), | ||
| device_class="xmp9ds", | ||
| ) | ||
|
|
||
|
|
||
| def test_OnMapTrace_no_info_field_returns_analyse() -> None: | ||
| envelope = _envelope("", 0) | ||
| envelope["body"]["data"].pop("info") | ||
| envelope["body"]["data"].pop("infoSize") | ||
| assert_message( | ||
| OnMapTrace, | ||
| envelope, | ||
| FirmwareEvent("1.15.13"), | ||
| device_class="xmp9ds", | ||
| expected_state=HandlingState.ANALYSE_LOGGED, | ||
| ) | ||
|
|
||
|
|
||
| def test_OnMapTrace_empty_groups_returns_analyse() -> None: | ||
| assert_message( | ||
| OnMapTrace, | ||
| _envelope(_EMPTY_GROUPS, 2), | ||
| FirmwareEvent("1.15.13"), | ||
| device_class="xmp9ds", | ||
| expected_state=HandlingState.ANALYSE_LOGGED, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "broken_info", | ||
| [ | ||
| "@@@not-base64@@@", | ||
| "AQIDBA==", # too short for LZMA header | ||
| _NON_JSON, | ||
| ], | ||
| ) | ||
| def test_OnMapTrace_corrupt_info_returns_analyse(broken_info: str) -> None: | ||
| assert_message( | ||
| OnMapTrace, | ||
| _envelope(broken_info, 0), | ||
| FirmwareEvent("1.15.13"), | ||
| device_class="xmp9ds", | ||
| expected_state=HandlingState.ANALYSE_LOGGED, | ||
| ) | ||
|
|
||
|
|
||
| def test_OnMapTrace_uses_serial_as_event_start() -> None: | ||
| envelope = _envelope(_SERIAL_TEST, 20) | ||
| envelope["body"]["data"]["serial"] = "42" | ||
|
|
||
| assert_message( | ||
| OnMapTrace, | ||
| envelope, | ||
| (FirmwareEvent("1.15.13"), MapTraceEvent(start=42, total=42, data="1,2;3,4")), | ||
| device_class="xmp9ds", | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the map capability. MapTraceEvent is also used by the vacuum bots. I also know that mower have a full map, so we should implement that one instead creating a new workaround just for the traces