|
6 | 6 | from typing import Any, Dict |
7 | 7 |
|
8 | 8 | import click |
| 9 | +from pyshark import FileCapture # type: ignore |
| 10 | +from pyshark.capture.live_capture import LiveCapture, UnknownInterfaceException # type: ignore |
| 11 | +from pyshark.packet.packet import Packet # type: ignore |
9 | 12 |
|
10 | 13 | from roborock import RoborockException |
11 | 14 | from roborock.api import RoborockApiClient |
12 | 15 | from roborock.cloud_api import RoborockMqttClient |
13 | 16 | from roborock.containers import DeviceData, LoginData |
| 17 | +from roborock.protocol import MessageParser |
14 | 18 | from roborock.util import run_sync |
15 | 19 |
|
16 | 20 | _LOGGER = logging.getLogger(__name__) |
@@ -135,10 +139,60 @@ async def command(ctx, cmd, device_id, params): |
135 | 139 | mqtt_client.__del__() |
136 | 140 |
|
137 | 141 |
|
| 142 | +@click.command() |
| 143 | +@click.option("--local_key", required=True) |
| 144 | +@click.option("--device_ip", required=True) |
| 145 | +@click.option("--file", required=False) |
| 146 | +@click.pass_context |
| 147 | +@run_sync() |
| 148 | +async def parser(_, local_key, device_ip, file): |
| 149 | + file_provided = file is not None |
| 150 | + if file_provided: |
| 151 | + capture = FileCapture(file) |
| 152 | + else: |
| 153 | + _LOGGER.info("Listen for interface rvi0 since no file was provided") |
| 154 | + capture = LiveCapture(interface="rvi0") |
| 155 | + buffer = {"data": bytes()} |
| 156 | + |
| 157 | + def on_package(packet: Packet): |
| 158 | + if hasattr(packet, "ip"): |
| 159 | + if packet.transport_layer == "TCP" and (packet.ip.dst == device_ip or packet.ip.src == device_ip): |
| 160 | + if hasattr(packet, "DATA"): |
| 161 | + if hasattr(packet.DATA, "data"): |
| 162 | + if packet.ip.dst == device_ip: |
| 163 | + try: |
| 164 | + f, buffer["data"] = MessageParser.parse( |
| 165 | + buffer["data"] + bytes.fromhex(packet.DATA.data), |
| 166 | + local_key, |
| 167 | + ) |
| 168 | + print(f"Received request: {f}") |
| 169 | + except BaseException as e: |
| 170 | + print(e) |
| 171 | + pass |
| 172 | + elif packet.ip.src == device_ip: |
| 173 | + try: |
| 174 | + f, buffer["data"] = MessageParser.parse( |
| 175 | + buffer["data"] + bytes.fromhex(packet.DATA.data), |
| 176 | + local_key, |
| 177 | + ) |
| 178 | + print(f"Received response: {f}") |
| 179 | + except BaseException as e: |
| 180 | + print(e) |
| 181 | + pass |
| 182 | + |
| 183 | + try: |
| 184 | + await capture.packets_from_tshark(on_package, close_tshark=not file_provided) |
| 185 | + except UnknownInterfaceException: |
| 186 | + raise RoborockException( |
| 187 | + "You need to run 'rvictl -s XXXXXXXX-XXXXXXXXXXXXXXXX' first, with an iPhone connected to usb port" |
| 188 | + ) |
| 189 | + |
| 190 | + |
138 | 191 | cli.add_command(login) |
139 | 192 | cli.add_command(discover) |
140 | 193 | cli.add_command(list_devices) |
141 | 194 | cli.add_command(command) |
| 195 | +cli.add_command(parser) |
142 | 196 |
|
143 | 197 |
|
144 | 198 | def main(): |
|
0 commit comments