|
1 | 1 | """Roborock B01 Protocol encoding and decoding.""" |
2 | 2 |
|
| 3 | +import base64 |
3 | 4 | import json |
4 | 5 | import logging |
| 6 | +import struct |
5 | 7 | from base64 import b64encode |
| 8 | +from collections.abc import Sequence |
6 | 9 | from dataclasses import dataclass |
7 | 10 | from struct import pack |
8 | 11 | from typing import Any |
9 | 12 |
|
10 | | -from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP |
| 13 | +from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP, Q10RestrictionType |
11 | 14 | from roborock.data.b01_q10.b01_q10_containers import Q10RoborockPoint |
12 | 15 | from roborock.exceptions import RoborockException |
13 | 16 | from roborock.map.b01_q10_map_parser import ( |
|
19 | 22 | parse_map_packet, |
20 | 23 | parse_trace_packet, |
21 | 24 | ) |
| 25 | +from roborock.map.b01_q10_overlays import Q10RestrictedZone, Q10VirtualWall |
22 | 26 | from roborock.roborock_message import ( |
23 | 27 | RoborockMessage, |
24 | 28 | RoborockMessageProtocol, |
@@ -75,6 +79,118 @@ def encode_clean_params(params: CleanParams) -> str: |
75 | 79 | return b64encode(payload).decode() |
76 | 80 |
|
77 | 81 |
|
| 82 | +_Q10_ZONE_RECORD_SIZE = 38 |
| 83 | +_Q10_ROOM_NAME_FIELD_SIZE = 19 |
| 84 | + |
| 85 | + |
| 86 | +def _orientation(a: tuple[int, int], b: tuple[int, int], c: tuple[int, int]) -> int: |
| 87 | + return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) |
| 88 | + |
| 89 | + |
| 90 | +def _on_segment(a: tuple[int, int], b: tuple[int, int], point: tuple[int, int]) -> bool: |
| 91 | + return min(a[0], b[0]) <= point[0] <= max(a[0], b[0]) and min(a[1], b[1]) <= point[1] <= max(a[1], b[1]) |
| 92 | + |
| 93 | + |
| 94 | +def _segments_cross(a: tuple[int, int], b: tuple[int, int], c: tuple[int, int], d: tuple[int, int]) -> bool: |
| 95 | + orientations = ( |
| 96 | + _orientation(a, b, c), |
| 97 | + _orientation(a, b, d), |
| 98 | + _orientation(c, d, a), |
| 99 | + _orientation(c, d, b), |
| 100 | + ) |
| 101 | + if orientations[0] * orientations[1] < 0 and orientations[2] * orientations[3] < 0: |
| 102 | + return True |
| 103 | + return any( |
| 104 | + orientation == 0 and _on_segment(start, end, point) |
| 105 | + for orientation, start, end, point in ( |
| 106 | + (orientations[0], a, b, c), |
| 107 | + (orientations[1], a, b, d), |
| 108 | + (orientations[2], c, d, a), |
| 109 | + (orientations[3], c, d, b), |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +def _validate_quadrilateral(vertices: list[tuple[int, int]]) -> None: |
| 115 | + area_twice = sum( |
| 116 | + x1 * y2 - x2 * y1 for (x1, y1), (x2, y2) in zip(vertices, (*vertices[1:], vertices[0]), strict=True) |
| 117 | + ) |
| 118 | + if area_twice == 0: |
| 119 | + raise ValueError("restricted-zone vertices must enclose an area") |
| 120 | + if _segments_cross(vertices[0], vertices[1], vertices[2], vertices[3]) or _segments_cross( |
| 121 | + vertices[1], vertices[2], vertices[3], vertices[0] |
| 122 | + ): |
| 123 | + raise ValueError("restricted-zone edges must not cross") |
| 124 | + |
| 125 | + |
| 126 | +def encode_restricted_zones(zones: Sequence[Q10RestrictedZone]) -> str: |
| 127 | + """Encode the complete Q10 restricted-zone collection.""" |
| 128 | + if len(zones) > 255: |
| 129 | + raise ValueError("zones must be a sequence of at most 255 restrictions") |
| 130 | + if not zones: |
| 131 | + return base64.b64encode(b"\x01\x00").decode() |
| 132 | + |
| 133 | + payload = bytearray((1, len(zones))) |
| 134 | + for zone in zones: |
| 135 | + if zone.type is Q10RestrictionType.unknown: |
| 136 | + raise ValueError("unknown restricted-zone types cannot be written") |
| 137 | + if len(zone.vertices) != 4: |
| 138 | + raise ValueError("restricted zones must contain exactly four vertices") |
| 139 | + if len(set(zone.vertices)) != 4: |
| 140 | + raise ValueError("restricted-zone vertices must be distinct") |
| 141 | + record = bytearray((zone.type, len(zone.vertices))) |
| 142 | + vector_vertices = [point.to_vector() for point in zone.vertices] |
| 143 | + _validate_quadrilateral(vector_vertices) |
| 144 | + for x, y in vector_vertices: |
| 145 | + record.extend( |
| 146 | + struct.pack( |
| 147 | + ">hh", |
| 148 | + x, |
| 149 | + y, |
| 150 | + ) |
| 151 | + ) |
| 152 | + record.extend(bytes(_Q10_ZONE_RECORD_SIZE - len(record))) |
| 153 | + payload.extend(record) |
| 154 | + return base64.b64encode(payload).decode() |
| 155 | + |
| 156 | + |
| 157 | +def encode_virtual_walls(walls: Sequence[Q10VirtualWall]) -> str: |
| 158 | + """Encode the complete Q10 virtual-wall collection.""" |
| 159 | + if len(walls) > 255: |
| 160 | + raise ValueError("walls must be a sequence of at most 255 virtual walls") |
| 161 | + |
| 162 | + payload = bytearray((len(walls),)) |
| 163 | + for wall in walls: |
| 164 | + if wall.start == wall.end: |
| 165 | + raise ValueError("virtual-wall endpoints must be distinct") |
| 166 | + for point in (wall.start, wall.end): |
| 167 | + x, y = point.to_vector() |
| 168 | + payload.extend( |
| 169 | + struct.pack( |
| 170 | + ">hh", |
| 171 | + x, |
| 172 | + y, |
| 173 | + ) |
| 174 | + ) |
| 175 | + return base64.b64encode(payload).decode() |
| 176 | + |
| 177 | + |
| 178 | +def encode_room_name(room_id: int, name: str) -> str: |
| 179 | + """Encode a Q10 room-name update.""" |
| 180 | + if not 0 <= room_id <= 255: |
| 181 | + raise ValueError("room_id must be an integer between 0 and 255") |
| 182 | + if not name or "\x00" in name: |
| 183 | + raise ValueError("name must be a non-empty string without null bytes") |
| 184 | + encoded_name = name.encode("utf-8") |
| 185 | + if len(encoded_name) > _Q10_ROOM_NAME_FIELD_SIZE: |
| 186 | + raise ValueError("name must be at most 19 UTF-8 bytes") |
| 187 | + |
| 188 | + payload = bytearray((1, room_id, 0, len(encoded_name))) |
| 189 | + payload.extend(encoded_name) |
| 190 | + payload.extend(bytes(_Q10_ROOM_NAME_FIELD_SIZE - len(encoded_name))) |
| 191 | + return base64.b64encode(payload).decode() |
| 192 | + |
| 193 | + |
78 | 194 | def encode_mqtt_payload(command: B01_Q10_DP, params: ParamsType) -> RoborockMessage: |
79 | 195 | """Encode payload for B01 Q10 commands over MQTT. |
80 | 196 |
|
|
0 commit comments