-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for ijai.vacuum.xxx map image
- Loading branch information
Alexander "Tarh
committed
Apr 13, 2024
1 parent
2475e84
commit 4d10e4d
Showing
13 changed files
with
606 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
Empty file.
66 changes: 66 additions & 0 deletions
66
custom_components/xiaomi_cloud_map_extractor/ijai/aes_decryptor.py
This file contains 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,66 @@ | ||
from Crypto.Cipher import AES | ||
from Crypto.Hash import MD5 | ||
from Crypto.Util.Padding import pad, unpad | ||
import base64 | ||
import logging | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
isEncryptKeyTypeHex = True | ||
|
||
def aesEncrypted(data, key: str): | ||
cipher = AES.new(key.encode("utf-8"), AES.MODE_ECB) | ||
|
||
encryptedData = cipher.encrypt( | ||
pad(data.encode("utf-8"), AES.block_size, 'pkcs7')) | ||
encryptedBase64Str = base64.b64encode(encryptedData).decode("utf-8") | ||
|
||
return encryptedBase64Str | ||
|
||
def aesDecrypted(data, key: str): | ||
parsedKey = key.encode("utf-8") | ||
if isEncryptKeyTypeHex: | ||
parsedKey = bytes.fromhex(key) | ||
|
||
cipher = AES.new(parsedKey, AES.MODE_ECB) | ||
|
||
decryptedBytes = cipher.decrypt(base64.b64decode(data)) | ||
|
||
decryptedData = unpad(decryptedBytes, AES.block_size, 'pkcs7') | ||
|
||
return bytes.fromhex(decryptedData.decode("utf-8")) | ||
|
||
|
||
def md5key(string: str, model: str, device_mac: str): | ||
pjstr = "".join(device_mac.lower().split(":")) | ||
|
||
tempModel = model.split('.')[-1] | ||
|
||
if len(tempModel) == 2: | ||
tempModel = "00" + tempModel | ||
elif len(tempModel) == 3: | ||
tempModel = "0" + tempModel | ||
|
||
tempKey = pjstr + tempModel | ||
aeskey = aesEncrypted(string, tempKey) | ||
#aeskey = string | ||
|
||
temp = MD5.new(aeskey.encode('utf-8')).hexdigest() | ||
if isEncryptKeyTypeHex: | ||
return temp | ||
else: | ||
return temp[8:-8].upper() | ||
|
||
|
||
def genMD5key(wifi_info_sn: str, owner_id: str, device_id: str, model: str, device_mac: str): | ||
arr = [wifi_info_sn, owner_id, device_id] | ||
tempString = '+'.join(arr) | ||
return md5key(tempString, model, device_mac) | ||
|
||
|
||
def unGzipCommon(data: str, wifi_info_sn: str, owner_id: str, device_id: str, model: str, device_mac: str) -> bytes: | ||
#base64map = base64.b64encode(data) | ||
# with open("0.encrypted.map", 'wb') as file: | ||
# file.write(data) | ||
return aesDecrypted(data, genMD5key(wifi_info_sn, owner_id, device_id, model, device_mac)) | ||
|
95 changes: 95 additions & 0 deletions
95
custom_components/xiaomi_cloud_map_extractor/ijai/image_handler.py
This file contains 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,95 @@ | ||
import logging | ||
from typing import Dict, Optional, Set, Tuple | ||
|
||
from PIL import Image | ||
from PIL.Image import Image as ImageType | ||
|
||
from custom_components.xiaomi_cloud_map_extractor.common.image_handler import ImageHandler | ||
from custom_components.xiaomi_cloud_map_extractor.const import * | ||
from custom_components.xiaomi_cloud_map_extractor.types import Colors, ImageConfig | ||
from custom_components.xiaomi_cloud_map_extractor.ijai.parsing_buffer import ParsingBuffer | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ImageHandlerIjai(ImageHandler): | ||
MAP_OUTSIDE = 0x00 | ||
MAP_WALL = 0xff | ||
MAP_SCAN = 0x01 | ||
MAP_NEW_DISCOVERED_AREA = 0x02 | ||
MAP_ROOM_MIN = 10 | ||
MAP_ROOM_MAX = 59 | ||
MAP_SELECTED_ROOM_MIN = 60 | ||
MAP_SELECTED_ROOM_MAX = 109 | ||
@staticmethod | ||
def parse(buf: ParsingBuffer, width: int, height: int, colors: Colors, image_config: ImageConfig, | ||
draw_cleaned_area: bool) \ | ||
-> Tuple[ImageType, Dict[int, Tuple[int, int, int, int]], Set[int], Optional[ImageType]]: | ||
palette = {\ | ||
ImageHandlerIjai.MAP_OUTSIDE: ImageHandler.__get_color__(COLOR_MAP_OUTSIDE, colors),\ | ||
ImageHandlerIjai.MAP_WALL: ImageHandler.__get_color__(COLOR_MAP_WALL_V2, colors),\ | ||
ImageHandlerIjai.MAP_SCAN: ImageHandler.__get_color__(COLOR_SCAN, colors),\ | ||
ImageHandlerIjai.MAP_NEW_DISCOVERED_AREA: ImageHandler.__get_color__(COLOR_NEW_DISCOVERED_AREA, colors)} | ||
rooms = {} | ||
cleaned_areas = set() | ||
scale = image_config[CONF_SCALE] | ||
trim_left = int(image_config[CONF_TRIM][CONF_LEFT] * width / 100) | ||
trim_right = int(image_config[CONF_TRIM][CONF_RIGHT] * width / 100) | ||
trim_top = int(image_config[CONF_TRIM][CONF_TOP] * height / 100) | ||
trim_bottom = int(image_config[CONF_TRIM][CONF_BOTTOM] * height / 100) | ||
trimmed_height = height - trim_top - trim_bottom | ||
trimmed_width = width - trim_left - trim_right | ||
if trimmed_width == 0 or trimmed_height == 0: | ||
return ImageHandler.create_empty_map_image(colors), rooms, cleaned_areas, None | ||
image = Image.new('RGBA', (trimmed_width, trimmed_height)) | ||
pixels = image.load() | ||
cleaned_areas_layer = None | ||
cleaned_areas_pixels = None | ||
if draw_cleaned_area: | ||
cleaned_areas_layer = Image.new('RGBA', (trimmed_width, trimmed_height)) | ||
cleaned_areas_pixels = cleaned_areas_layer.load() | ||
_LOGGER.debug(f"trim_bottom = {trim_bottom}, trim_top = {trim_top}, trim_left = {trim_left}, trim_right = {trim_right}") | ||
buf.skip('trim_bottom', trim_bottom * width) | ||
unknown_pixels = set() | ||
_LOGGER.debug(f"buffer: [{buf._offs:#x}] = {buf.peek_uint32("some_int32")}") | ||
for img_y in range(trimmed_height): | ||
buf.skip('trim_left', trim_left) | ||
for img_x in range(trimmed_width): | ||
pixel_type = buf.get_uint8('pixel') | ||
x = img_x | ||
y = trimmed_height - 1 - img_y | ||
if pixel_type in palette.keys(): | ||
pixels[x, y] = palette[pixel_type] | ||
elif ImageHandlerIjai.MAP_ROOM_MIN <= pixel_type <= ImageHandlerIjai.MAP_SELECTED_ROOM_MAX: | ||
room_x = img_x + trim_left | ||
room_y = img_y + trim_bottom | ||
if pixel_type < ImageHandlerIjai.MAP_SELECTED_ROOM_MIN: | ||
room_number = pixel_type | ||
else: | ||
room_number = pixel_type - ImageHandlerIjai.MAP_SELECTED_ROOM_MIN + ImageHandlerIjai.MAP_ROOM_MIN | ||
cleaned_areas.add(room_number) | ||
if draw_cleaned_area: | ||
cleaned_areas_pixels[x, y] = ImageHandler.__get_color__(COLOR_CLEANED_AREA, colors) | ||
if room_number not in rooms: | ||
rooms[room_number] = (room_x, room_y, room_x, room_y) | ||
else: | ||
rooms[room_number] = (min(rooms[room_number][0], room_x), | ||
min(rooms[room_number][1], room_y), | ||
max(rooms[room_number][2], room_x), | ||
max(rooms[room_number][3], room_y)) | ||
default = ImageHandler.ROOM_COLORS[room_number % len(ImageHandler.ROOM_COLORS)] | ||
pixels[x, y] = ImageHandler.__get_color__(f"{COLOR_ROOM_PREFIX}{room_number}", colors, default) | ||
else: | ||
pixels[x, y] = ImageHandler.__get_color__(COLOR_UNKNOWN, colors) | ||
unknown_pixels.add(pixel_type) | ||
_LOGGER.debug(f"unknown pixel [{x},{y}] = {pixel_type}") | ||
buf.skip('trim_right', trim_right) | ||
buf.skip('trim_top', trim_top * width) | ||
if image_config["scale"] != 1 and trimmed_width != 0 and trimmed_height != 0: | ||
image = image.resize((int(trimmed_width * scale), int(trimmed_height * scale)), resample=Image.NEAREST) | ||
if draw_cleaned_area: | ||
cleaned_areas_layer = cleaned_areas_layer.resize( | ||
(int(trimmed_width * scale), int(trimmed_height * scale)), resample=Image.NEAREST) | ||
if len(unknown_pixels) > 0: | ||
_LOGGER.warning('unknown pixel_types: %s', unknown_pixels) | ||
return image, rooms, cleaned_areas, cleaned_areas_layer |
Oops, something went wrong.