-
Notifications
You must be signed in to change notification settings - Fork 78
chore: Create module for v1 request encoding #413
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Roborock V1 Protocol Encoder.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import math | ||
| import time | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from roborock.roborock_message import MessageRetry, RoborockMessage, RoborockMessageProtocol | ||
| from roborock.roborock_typing import RoborockCommand | ||
| from roborock.util import get_next_int | ||
|
|
||
| # All mqtt commands are sent securely. Only local commands in this list are secured. | ||
| COMMANDS_SECURED = { | ||
| RoborockCommand.GET_MAP_V1, | ||
| RoborockCommand.GET_MULTI_MAP, | ||
| } | ||
|
|
||
| CommandType = RoborockCommand | str | ||
| ParamsType = list | dict | int | None | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class SecurityData: | ||
| """Security data included in the request for some V1 commands.""" | ||
|
|
||
| endpoint: str | ||
| nonce: bytes | ||
|
|
||
| def to_dict(self) -> dict[str, Any]: | ||
| """Convert security data to a dictionary for sending in the payload.""" | ||
| return {"security": {"endpoint": self.endpoint, "nonce": self.nonce.hex().lower()}} | ||
|
|
||
|
|
||
| @dataclass | ||
| class RequestMessage: | ||
| """Data structure for v1 RoborockMessage payloads.""" | ||
|
|
||
| method: RoborockCommand | str | ||
| params: ParamsType | ||
| timestamp: int = field(default_factory=lambda: math.floor(time.time())) | ||
| request_id: int = field(default_factory=lambda: get_next_int(10000, 32767)) | ||
|
|
||
| def as_payload(self, security_data: SecurityData | None) -> bytes: | ||
| """Convert the request arguments to a dictionary.""" | ||
| inner = { | ||
| "id": self.request_id, | ||
| "method": self.method, | ||
| "params": self.params or [], | ||
| **(security_data.to_dict() if security_data else {}), | ||
| } | ||
| return bytes( | ||
| json.dumps( | ||
| { | ||
| "dps": {"101": json.dumps(inner, separators=(",", ":"))}, | ||
| "t": self.timestamp, | ||
| }, | ||
| separators=(",", ":"), | ||
| ).encode() | ||
| ) | ||
|
|
||
|
|
||
| def create_mqtt_payload_encoder(security_data: SecurityData) -> Callable[[CommandType, ParamsType], RoborockMessage]: | ||
| """Create a payload encoder for V1 commands over MQTT.""" | ||
|
|
||
| def _get_payload(method: CommandType, params: ParamsType) -> RoborockMessage: | ||
| """Build the payload for a V1 command.""" | ||
| request = RequestMessage(method=method, params=params) | ||
| payload = request.as_payload(security_data) # always secure | ||
| return RoborockMessage( | ||
| timestamp=request.timestamp, | ||
| protocol=RoborockMessageProtocol.RPC_REQUEST, | ||
| payload=payload, | ||
| ) | ||
|
|
||
| return _get_payload | ||
|
|
||
|
|
||
| def create_local_payload_encoder(security_data: SecurityData) -> Callable[[CommandType, ParamsType], RoborockMessage]: | ||
| """Create a payload encoder for V1 commands over local connection.""" | ||
|
|
||
| def _get_payload(method: CommandType, params: ParamsType) -> RoborockMessage: | ||
| """Build the payload for a V1 command.""" | ||
| request = RequestMessage(method=method, params=params) | ||
| is_secured = request.method in COMMANDS_SECURED | ||
| payload = request.as_payload(security_data if is_secured else None) | ||
|
|
||
| message_retry: MessageRetry | None = None | ||
| if method == RoborockCommand.RETRY_REQUEST and isinstance(params, dict): | ||
| message_retry = MessageRetry(method=method, retry_id=params["retry_id"]) | ||
|
|
||
| return RoborockMessage( | ||
| timestamp=request.timestamp, | ||
| protocol=RoborockMessageProtocol.GENERAL_REQUEST, | ||
| payload=payload, | ||
| message_retry=message_retry, | ||
| ) | ||
|
|
||
| return _get_payload | ||
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
|
allenporter marked this conversation as resolved.
Outdated
|
Empty file.
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
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.
Uh oh!
There was an error while loading. Please reload this page.