55"""
66
77import gzip
8+ import hashlib
89import json
910import logging
1011import struct
@@ -171,10 +172,12 @@ def _serialize_dataclass(obj: Any) -> dict[str, Any]:
171172# Eight character endpoint, matching the width of the map response header field.
172173MAP_ENDPOINT = "fakeendp"
173174
174- # Side length in cells of the simulated map image.
175+ # Minimum side length in cells of the simulated map image.
175176MAP_IMAGE_SIZE = 32
176177
177178_MAP_FILE_HEADER_LEN = 20
179+ _MAP_CELL_SIZE_MM = 50
180+ _MAP_IMAGE_PADDING = 2
178181_IMAGE_BLOCK_TYPE = 2
179182_PATH_BLOCK_TYPE = 3
180183_POSE_BLOCK_TYPE = 8
@@ -247,6 +250,7 @@ def __init__(
247250 "reset_consumable" : self ._handle_reset_consumable ,
248251 "app_get_init_status" : self ._handle_app_get_init_status ,
249252 "get_network_info" : self ._handle_get_network_info ,
253+ "get_map_v1" : lambda params : self .build_map_bytes (),
250254 }
251255
252256 # The simulated robot's own view of its map. Tests extend the path
@@ -393,23 +397,28 @@ def _path_blocks(self, start: int, count: int) -> bytes:
393397
394398 def build_map_bytes (self ) -> bytes :
395399 """Build a decompressed map file reflecting the current robot state."""
400+ # Full-map image offsets are cells; path and robot coordinates are millimetres.
401+ points = [self .robot_position [:2 ], * struct .iter_unpack ("<HH" , self .path_payload (0 , self .path_length ))]
402+ xs = [x // _MAP_CELL_SIZE_MM for x , _ in points ]
403+ ys = [y // _MAP_CELL_SIZE_MM for _ , y in points ]
404+ left = max (0 , min (xs ) - _MAP_IMAGE_PADDING )
405+ top = max (0 , min (ys ) - _MAP_IMAGE_PADDING )
406+ width = max (MAP_IMAGE_SIZE , max (xs ) - left + _MAP_IMAGE_PADDING + 1 )
407+ height = max (MAP_IMAGE_SIZE , max (ys ) - top + _MAP_IMAGE_PADDING + 1 )
396408 image = _block (
397409 _IMAGE_BLOCK_TYPE ,
398- bytes ([_MAP_INSIDE ]) * (MAP_IMAGE_SIZE * MAP_IMAGE_SIZE ),
399- struct .pack ("<IIII" , 0 , 0 , MAP_IMAGE_SIZE , MAP_IMAGE_SIZE ),
400- )
401- header = b"rr" + struct .pack ("<H" , _MAP_FILE_HEADER_LEN ) + bytes (_MAP_FILE_HEADER_LEN - 4 )
402- return (
403- header
404- + image
405- + self ._path_blocks (0 , self .path_length )
406- + self ._pose_block ()
407- + _block (_DIGEST_BLOCK_TYPE , bytes (20 ))
410+ bytes ([_MAP_INSIDE ]) * (width * height ),
411+ struct .pack ("<Iiiii" , 0 , top , left , height , width ),
408412 )
413+ body = image + self ._path_blocks (0 , self .path_length ) + self ._pose_block ()
414+ digest_header = struct .pack ("<HHI" , _DIGEST_BLOCK_TYPE , 8 , 20 )
415+ header = struct .pack ("<2sHIHHII" , b"rr" , _MAP_FILE_HEADER_LEN , len (body ) + len (digest_header ) + 20 , 1 , 0 , 0 , 0 )
416+ data = header + body + digest_header
417+ return data + hashlib .sha1 (data , usedforsecurity = False ).digest ()
409418
410- def _map_response_message (self , request_id : int ) -> RoborockMessage :
419+ def _map_response_message (self , request_id : int , map_bytes : bytes ) -> RoborockMessage :
411420 """Frame the map as the encrypted binary response the device sends."""
412- body = Utils .encrypt_cbc (gzip .compress (self . build_map_bytes () ), self .security_data .nonce )
421+ body = Utils .encrypt_cbc (gzip .compress (map_bytes ), self .security_data .nonce )
413422 header = struct .pack ("<8s8sH6s" , self .security_data .endpoint .encode (), bytes (8 ), request_id & 0xFFFF , bytes (6 ))
414423 return RoborockMessage (protocol = RoborockMessageProtocol .MAP_RESPONSE , payload = header + body )
415424
@@ -430,11 +439,6 @@ async def _handle_publish(self, message: RoborockMessage, channel: FakeChannel)
430439 _LOGGER .debug ("Failed to parse plaintext JSON RPC payload: %s" , e , exc_info = True )
431440 return
432441
433- if method == "get_map_v1" :
434- # The map arrives as an encrypted binary frame rather than a JSON result.
435- channel .notify_subscribers (self ._map_response_message (msg_id ))
436- return
437-
438442 result = None
439443 error = None
440444
@@ -449,6 +453,11 @@ async def _handle_publish(self, message: RoborockMessage, channel: FakeChannel)
449453 else :
450454 result = "ok"
451455
456+ if method == "get_map_v1" and error is None and isinstance (result , bytes ):
457+ # Custom handlers return decompressed map bytes, just like the default.
458+ channel .notify_subscribers (self ._map_response_message (msg_id , result ))
459+ return
460+
452461 response_data = {
453462 "dps" : {"102" : json .dumps ({"id" : msg_id , "result" : result , "error" : error })},
454463 "t" : int (time .time ()),
0 commit comments