pymammotion 0.2.29__py3-none-any.whl → 0.2.31__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymammotion/__init__.py +11 -8
- pymammotion/aliyun/cloud_gateway.py +26 -24
- pymammotion/aliyun/cloud_service.py +3 -3
- pymammotion/aliyun/dataclass/dev_by_account_response.py +1 -1
- pymammotion/bluetooth/ble.py +5 -5
- pymammotion/bluetooth/ble_message.py +30 -16
- pymammotion/bluetooth/data/convert.py +1 -1
- pymammotion/bluetooth/data/framectrldata.py +1 -1
- pymammotion/bluetooth/data/notifydata.py +6 -6
- pymammotion/const.py +1 -0
- pymammotion/data/model/__init__.py +2 -0
- pymammotion/data/model/device.py +22 -20
- pymammotion/data/model/device_config.py +1 -1
- pymammotion/data/model/enums.py +4 -4
- pymammotion/data/model/excute_boarder_params.py +5 -5
- pymammotion/data/model/execute_boarder.py +4 -4
- pymammotion/data/model/hash_list.py +1 -1
- pymammotion/data/model/location.py +2 -2
- pymammotion/data/model/plan.py +4 -4
- pymammotion/data/model/region_data.py +4 -4
- pymammotion/data/model/report_info.py +1 -1
- pymammotion/data/mqtt/event.py +1 -1
- pymammotion/data/state_manager.py +9 -9
- pymammotion/event/event.py +14 -14
- pymammotion/http/http.py +29 -51
- pymammotion/http/model/http.py +75 -0
- pymammotion/mammotion/commands/messages/driver.py +20 -23
- pymammotion/mammotion/commands/messages/navigation.py +47 -48
- pymammotion/mammotion/commands/messages/network.py +17 -35
- pymammotion/mammotion/commands/messages/system.py +6 -7
- pymammotion/mammotion/control/joystick.py +10 -10
- pymammotion/mammotion/devices/__init__.py +2 -2
- pymammotion/mammotion/devices/base.py +248 -0
- pymammotion/mammotion/devices/mammotion.py +23 -1005
- pymammotion/mammotion/devices/mammotion_bluetooth.py +447 -0
- pymammotion/mammotion/devices/mammotion_cloud.py +246 -0
- pymammotion/mqtt/mammotion_future.py +2 -2
- pymammotion/mqtt/mammotion_mqtt.py +17 -14
- pymammotion/proto/__init__.py +6 -0
- pymammotion/utility/constant/__init__.py +3 -1
- pymammotion/utility/datatype_converter.py +9 -9
- pymammotion/utility/device_type.py +13 -13
- pymammotion/utility/map.py +2 -2
- pymammotion/utility/periodic.py +5 -5
- pymammotion/utility/rocker_util.py +1 -1
- {pymammotion-0.2.29.dist-info → pymammotion-0.2.31.dist-info}/METADATA +3 -1
- {pymammotion-0.2.29.dist-info → pymammotion-0.2.31.dist-info}/RECORD +49 -45
- {pymammotion-0.2.29.dist-info → pymammotion-0.2.31.dist-info}/LICENSE +0 -0
- {pymammotion-0.2.29.dist-info → pymammotion-0.2.31.dist-info}/WHEEL +0 -0
@@ -0,0 +1,447 @@
|
|
1
|
+
import asyncio
|
2
|
+
import logging
|
3
|
+
from typing import Any, cast
|
4
|
+
from uuid import UUID
|
5
|
+
|
6
|
+
import betterproto
|
7
|
+
from bleak import BleakGATTCharacteristic, BleakGATTServiceCollection, BLEDevice
|
8
|
+
from bleak.exc import BleakDBusError
|
9
|
+
from bleak_retry_connector import (
|
10
|
+
BLEAK_RETRY_EXCEPTIONS,
|
11
|
+
BleakClientWithServiceCache,
|
12
|
+
BleakNotFoundError,
|
13
|
+
establish_connection,
|
14
|
+
)
|
15
|
+
|
16
|
+
from pymammotion.bluetooth import BleMessage
|
17
|
+
from pymammotion.data.model.device import MowingDevice
|
18
|
+
from pymammotion.mammotion.commands.mammotion_command import MammotionCommand
|
19
|
+
from pymammotion.mammotion.devices.base import MammotionBaseDevice
|
20
|
+
from pymammotion.proto import has_field
|
21
|
+
from pymammotion.proto.luba_msg import LubaMsg
|
22
|
+
|
23
|
+
DBUS_ERROR_BACKOFF_TIME = 0.25
|
24
|
+
|
25
|
+
DISCONNECT_DELAY = 10
|
26
|
+
|
27
|
+
|
28
|
+
_LOGGER = logging.getLogger(__name__)
|
29
|
+
|
30
|
+
|
31
|
+
class CharacteristicMissingError(Exception):
|
32
|
+
"""Raised when a characteristic is missing."""
|
33
|
+
|
34
|
+
|
35
|
+
def _sb_uuid(comms_type: str = "service") -> UUID | str:
|
36
|
+
"""Return Mammotion UUID.
|
37
|
+
|
38
|
+
Args:
|
39
|
+
comms_type (str): The type of communication (tx, rx, or service).
|
40
|
+
|
41
|
+
Returns:
|
42
|
+
UUID | str: The UUID for the specified communication type or an error message.
|
43
|
+
|
44
|
+
"""
|
45
|
+
_uuid = {"tx": "ff01", "rx": "ff02", "service": "2A05"}
|
46
|
+
|
47
|
+
if comms_type in _uuid:
|
48
|
+
return UUID(f"0000{_uuid[comms_type]}-0000-1000-8000-00805f9b34fb")
|
49
|
+
|
50
|
+
return "Incorrect type, choose between: tx, rx or service"
|
51
|
+
|
52
|
+
|
53
|
+
READ_CHAR_UUID = _sb_uuid(comms_type="rx")
|
54
|
+
WRITE_CHAR_UUID = _sb_uuid(comms_type="tx")
|
55
|
+
|
56
|
+
|
57
|
+
def _handle_timeout(fut: asyncio.Future[None]) -> None:
|
58
|
+
"""Handle a timeout."""
|
59
|
+
if not fut.done():
|
60
|
+
fut.set_exception(asyncio.TimeoutError)
|
61
|
+
|
62
|
+
|
63
|
+
async def _handle_retry(fut: asyncio.Future[None], func, command: bytes) -> None:
|
64
|
+
"""Handle a retry."""
|
65
|
+
if not fut.done():
|
66
|
+
await func(command)
|
67
|
+
|
68
|
+
|
69
|
+
class MammotionBaseBLEDevice(MammotionBaseDevice):
|
70
|
+
"""Base class for Mammotion BLE devices."""
|
71
|
+
|
72
|
+
def __init__(self, mowing_state: MowingDevice, device: BLEDevice, interface: int = 0, **kwargs: Any) -> None:
|
73
|
+
"""Initialize MammotionBaseBLEDevice."""
|
74
|
+
super().__init__(mowing_state)
|
75
|
+
self._disconnect_strategy = True
|
76
|
+
self._ble_sync_task = None
|
77
|
+
self._prev_notification = None
|
78
|
+
self._interface = f"hci{interface}"
|
79
|
+
self._device = device
|
80
|
+
self._mower = mowing_state
|
81
|
+
self._client: BleakClientWithServiceCache | None = None
|
82
|
+
self._read_char: BleakGATTCharacteristic | int | str | UUID = 0
|
83
|
+
self._write_char: BleakGATTCharacteristic | int | str | UUID = 0
|
84
|
+
self._disconnect_timer: asyncio.TimerHandle | None = None
|
85
|
+
self._message: BleMessage | None = None
|
86
|
+
self._commands: MammotionCommand = MammotionCommand(device.name)
|
87
|
+
self._expected_disconnect = False
|
88
|
+
self._connect_lock = asyncio.Lock()
|
89
|
+
self._operation_lock = asyncio.Lock()
|
90
|
+
self._key: str | None = None
|
91
|
+
|
92
|
+
def update_device(self, device: BLEDevice) -> None:
|
93
|
+
"""Update the BLE device."""
|
94
|
+
self._device = device
|
95
|
+
|
96
|
+
async def _ble_sync(self) -> None:
|
97
|
+
command_bytes = self._commands.send_todev_ble_sync(2)
|
98
|
+
await self._message.post_custom_data_bytes(command_bytes)
|
99
|
+
|
100
|
+
async def run_periodic_sync_task(self) -> None:
|
101
|
+
"""Send ble sync to robot."""
|
102
|
+
try:
|
103
|
+
await self._ble_sync()
|
104
|
+
finally:
|
105
|
+
self.schedule_ble_sync()
|
106
|
+
|
107
|
+
def schedule_ble_sync(self) -> None:
|
108
|
+
"""Periodically sync to keep connection alive."""
|
109
|
+
if self._client is not None and self._client.is_connected:
|
110
|
+
self._ble_sync_task = self.loop.call_later(
|
111
|
+
130, lambda: asyncio.ensure_future(self.run_periodic_sync_task())
|
112
|
+
)
|
113
|
+
|
114
|
+
async def queue_command(self, key: str, **kwargs: Any) -> bytes | None:
|
115
|
+
return await self._send_command_with_args(key, **kwargs)
|
116
|
+
|
117
|
+
async def _send_command_with_args(self, key: str, **kwargs) -> bytes | None:
|
118
|
+
"""Send command to device and read response."""
|
119
|
+
if self._operation_lock.locked():
|
120
|
+
_LOGGER.debug(
|
121
|
+
"%s: Operation already in progress, waiting for it to complete; RSSI: %s",
|
122
|
+
self.name,
|
123
|
+
self.rssi,
|
124
|
+
)
|
125
|
+
async with self._operation_lock:
|
126
|
+
try:
|
127
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
128
|
+
return await self._send_command_locked(key, command_bytes)
|
129
|
+
except BleakNotFoundError:
|
130
|
+
_LOGGER.exception(
|
131
|
+
"%s: device not found, no longer in range, or poor RSSI: %s",
|
132
|
+
self.name,
|
133
|
+
self.rssi,
|
134
|
+
)
|
135
|
+
raise
|
136
|
+
except CharacteristicMissingError as ex:
|
137
|
+
_LOGGER.debug(
|
138
|
+
"%s: characteristic missing: %s; RSSI: %s",
|
139
|
+
self.name,
|
140
|
+
ex,
|
141
|
+
self.rssi,
|
142
|
+
exc_info=True,
|
143
|
+
)
|
144
|
+
except BLEAK_RETRY_EXCEPTIONS:
|
145
|
+
_LOGGER.debug("%s: communication failed with:", self.name, exc_info=True)
|
146
|
+
return
|
147
|
+
|
148
|
+
async def _send_command(self, key: str, retry: int | None = None) -> bytes | None:
|
149
|
+
"""Send command to device and read response."""
|
150
|
+
if self._operation_lock.locked():
|
151
|
+
_LOGGER.debug(
|
152
|
+
"%s: Operation already in progress, waiting for it to complete; RSSI: %s",
|
153
|
+
self.name,
|
154
|
+
self.rssi,
|
155
|
+
)
|
156
|
+
async with self._operation_lock:
|
157
|
+
try:
|
158
|
+
command_bytes = getattr(self._commands, key)()
|
159
|
+
return await self._send_command_locked(key, command_bytes)
|
160
|
+
except BleakNotFoundError:
|
161
|
+
_LOGGER.exception(
|
162
|
+
"%s: device not found, no longer in range, or poor RSSI: %s",
|
163
|
+
self.name,
|
164
|
+
self.rssi,
|
165
|
+
)
|
166
|
+
raise
|
167
|
+
except CharacteristicMissingError as ex:
|
168
|
+
_LOGGER.debug(
|
169
|
+
"%s: characteristic missing: %s; RSSI: %s",
|
170
|
+
self.name,
|
171
|
+
ex,
|
172
|
+
self.rssi,
|
173
|
+
exc_info=True,
|
174
|
+
)
|
175
|
+
except BLEAK_RETRY_EXCEPTIONS:
|
176
|
+
_LOGGER.debug("%s: communication failed with:", self.name, exc_info=True)
|
177
|
+
return
|
178
|
+
|
179
|
+
@property
|
180
|
+
def name(self) -> str:
|
181
|
+
"""Return device name."""
|
182
|
+
return f"{self._device.name} ({self._device.address})"
|
183
|
+
|
184
|
+
@property
|
185
|
+
def rssi(self) -> int:
|
186
|
+
"""Return RSSI of device."""
|
187
|
+
try:
|
188
|
+
return cast(self._mower.sys.toapp_report_data.connect.ble_rssi, int)
|
189
|
+
finally:
|
190
|
+
return 0
|
191
|
+
|
192
|
+
async def _ensure_connected(self) -> None:
|
193
|
+
"""Ensure connection to device is established."""
|
194
|
+
if self._connect_lock.locked():
|
195
|
+
_LOGGER.debug(
|
196
|
+
"%s: Connection already in progress, waiting for it to complete; RSSI: %s",
|
197
|
+
self.name,
|
198
|
+
self.rssi,
|
199
|
+
)
|
200
|
+
if self._client and self._client.is_connected:
|
201
|
+
_LOGGER.debug(
|
202
|
+
"%s: Already connected before obtaining lock, resetting timer; RSSI: %s",
|
203
|
+
self.name,
|
204
|
+
self.rssi,
|
205
|
+
)
|
206
|
+
self._reset_disconnect_timer()
|
207
|
+
return
|
208
|
+
async with self._connect_lock:
|
209
|
+
# Check again while holding the lock
|
210
|
+
if self._client and self._client.is_connected:
|
211
|
+
_LOGGER.debug(
|
212
|
+
"%s: Already connected after obtaining lock, resetting timer; RSSI: %s",
|
213
|
+
self.name,
|
214
|
+
self.rssi,
|
215
|
+
)
|
216
|
+
self._reset_disconnect_timer()
|
217
|
+
return
|
218
|
+
_LOGGER.debug("%s: Connecting; RSSI: %s", self.name, self.rssi)
|
219
|
+
client: BleakClientWithServiceCache = await establish_connection(
|
220
|
+
BleakClientWithServiceCache,
|
221
|
+
self._device,
|
222
|
+
self.name,
|
223
|
+
self._disconnected,
|
224
|
+
max_attempts=10,
|
225
|
+
ble_device_callback=lambda: self._device,
|
226
|
+
)
|
227
|
+
_LOGGER.debug("%s: Connected; RSSI: %s", self.name, self.rssi)
|
228
|
+
self._client = client
|
229
|
+
self._message = BleMessage(client)
|
230
|
+
|
231
|
+
try:
|
232
|
+
self._resolve_characteristics(client.services)
|
233
|
+
except CharacteristicMissingError as ex:
|
234
|
+
_LOGGER.debug(
|
235
|
+
"%s: characteristic missing, clearing cache: %s; RSSI: %s",
|
236
|
+
self.name,
|
237
|
+
ex,
|
238
|
+
self.rssi,
|
239
|
+
exc_info=True,
|
240
|
+
)
|
241
|
+
await client.clear_cache()
|
242
|
+
self._cancel_disconnect_timer()
|
243
|
+
await self._execute_disconnect_with_lock()
|
244
|
+
raise
|
245
|
+
|
246
|
+
_LOGGER.debug(
|
247
|
+
"%s: Starting notify and disconnect timer; RSSI: %s",
|
248
|
+
self.name,
|
249
|
+
self.rssi,
|
250
|
+
)
|
251
|
+
self._reset_disconnect_timer()
|
252
|
+
await self._start_notify()
|
253
|
+
command_bytes = self._commands.send_todev_ble_sync(2)
|
254
|
+
await self._message.post_custom_data_bytes(command_bytes)
|
255
|
+
self.schedule_ble_sync()
|
256
|
+
|
257
|
+
async def _send_command_locked(self, key: str, command: bytes) -> bytes:
|
258
|
+
"""Send command to device and read response."""
|
259
|
+
await self._ensure_connected()
|
260
|
+
try:
|
261
|
+
return await self._execute_command_locked(key, command)
|
262
|
+
except BleakDBusError as ex:
|
263
|
+
# Disconnect so we can reset state and try again
|
264
|
+
await asyncio.sleep(DBUS_ERROR_BACKOFF_TIME)
|
265
|
+
_LOGGER.debug(
|
266
|
+
"%s: RSSI: %s; Backing off %ss; Disconnecting due to error: %s",
|
267
|
+
self.name,
|
268
|
+
self.rssi,
|
269
|
+
DBUS_ERROR_BACKOFF_TIME,
|
270
|
+
ex,
|
271
|
+
)
|
272
|
+
await self._execute_forced_disconnect()
|
273
|
+
raise
|
274
|
+
except BLEAK_RETRY_EXCEPTIONS as ex:
|
275
|
+
# Disconnect so we can reset state and try again
|
276
|
+
_LOGGER.debug("%s: RSSI: %s; Disconnecting due to error: %s", self.name, self.rssi, ex)
|
277
|
+
await self._execute_forced_disconnect()
|
278
|
+
raise
|
279
|
+
|
280
|
+
async def _notification_handler(self, _sender: BleakGATTCharacteristic, data: bytearray) -> None:
|
281
|
+
"""Handle notification responses."""
|
282
|
+
if self._message is None:
|
283
|
+
return
|
284
|
+
result = self._message.parseNotification(data)
|
285
|
+
if result == 0:
|
286
|
+
data = await self._message.parseBlufiNotifyData(True)
|
287
|
+
self._update_raw_data(data)
|
288
|
+
self._message.clearNotification()
|
289
|
+
_LOGGER.debug("%s: Received notification: %s", self.name, data)
|
290
|
+
else:
|
291
|
+
return
|
292
|
+
new_msg = LubaMsg().parse(data)
|
293
|
+
if betterproto.serialized_on_wire(new_msg.net):
|
294
|
+
if new_msg.net.todev_ble_sync != 0 or has_field(new_msg.net.toapp_wifi_iot_status):
|
295
|
+
if has_field(new_msg.net.toapp_wifi_iot_status) and self._commands.get_device_product_key() == "":
|
296
|
+
self._commands.set_device_product_key(new_msg.net.toapp_wifi_iot_status.productkey)
|
297
|
+
|
298
|
+
return
|
299
|
+
|
300
|
+
# may or may not be correct, some work could be done here to correctly match responses
|
301
|
+
if self._notify_future and not self._notify_future.done():
|
302
|
+
self._notify_future.set_result(data)
|
303
|
+
|
304
|
+
self._reset_disconnect_timer()
|
305
|
+
await self._state_manager.notification(new_msg)
|
306
|
+
|
307
|
+
async def _start_notify(self) -> None:
|
308
|
+
"""Start notification."""
|
309
|
+
_LOGGER.debug("%s: Subscribe to notifications; RSSI: %s", self.name, self.rssi)
|
310
|
+
await self._client.start_notify(self._read_char, self._notification_handler)
|
311
|
+
|
312
|
+
async def _execute_command_locked(self, key: str, command: bytes) -> bytes:
|
313
|
+
"""Execute command and read response."""
|
314
|
+
assert self._client is not None
|
315
|
+
self._notify_future = self.loop.create_future()
|
316
|
+
self._key = key
|
317
|
+
_LOGGER.debug("%s: Sending command: %s", self.name, key)
|
318
|
+
await self._message.post_custom_data_bytes(command)
|
319
|
+
|
320
|
+
timeout = 2
|
321
|
+
timeout_handle = self.loop.call_at(self.loop.time() + timeout, _handle_timeout, self._notify_future)
|
322
|
+
timeout_expired = False
|
323
|
+
try:
|
324
|
+
notify_msg = await self._notify_future
|
325
|
+
except asyncio.TimeoutError:
|
326
|
+
timeout_expired = True
|
327
|
+
notify_msg = b""
|
328
|
+
finally:
|
329
|
+
if not timeout_expired:
|
330
|
+
timeout_handle.cancel()
|
331
|
+
self._notify_future = None
|
332
|
+
|
333
|
+
_LOGGER.debug("%s: Notification received: %s", self.name, notify_msg.hex())
|
334
|
+
return notify_msg
|
335
|
+
|
336
|
+
def get_address(self) -> str:
|
337
|
+
"""Return address of device."""
|
338
|
+
return self._device.address
|
339
|
+
|
340
|
+
def _resolve_characteristics(self, services: BleakGATTServiceCollection) -> None:
|
341
|
+
"""Resolve characteristics."""
|
342
|
+
self._read_char = services.get_characteristic(READ_CHAR_UUID)
|
343
|
+
if not self._read_char:
|
344
|
+
self._read_char = READ_CHAR_UUID
|
345
|
+
_LOGGER.error(CharacteristicMissingError(READ_CHAR_UUID))
|
346
|
+
self._write_char = services.get_characteristic(WRITE_CHAR_UUID)
|
347
|
+
if not self._write_char:
|
348
|
+
self._write_char = WRITE_CHAR_UUID
|
349
|
+
_LOGGER.error(CharacteristicMissingError(WRITE_CHAR_UUID))
|
350
|
+
|
351
|
+
def _reset_disconnect_timer(self) -> None:
|
352
|
+
"""Reset disconnect timer."""
|
353
|
+
self._cancel_disconnect_timer()
|
354
|
+
self._expected_disconnect = False
|
355
|
+
self._disconnect_timer = self.loop.call_later(DISCONNECT_DELAY, self._disconnect_from_timer)
|
356
|
+
|
357
|
+
def _disconnected(self, client: BleakClientWithServiceCache) -> None:
|
358
|
+
"""Disconnected callback."""
|
359
|
+
if self._expected_disconnect:
|
360
|
+
_LOGGER.debug("%s: Disconnected from device; RSSI: %s", self.name, self.rssi)
|
361
|
+
return
|
362
|
+
_LOGGER.warning(
|
363
|
+
"%s: Device unexpectedly disconnected; RSSI: %s",
|
364
|
+
self.name,
|
365
|
+
self.rssi,
|
366
|
+
)
|
367
|
+
self._cancel_disconnect_timer()
|
368
|
+
|
369
|
+
def _disconnect_from_timer(self) -> None:
|
370
|
+
"""Disconnect from device."""
|
371
|
+
if self._operation_lock.locked() and self._client.is_connected:
|
372
|
+
_LOGGER.debug(
|
373
|
+
"%s: Operation in progress, resetting disconnect timer; RSSI: %s",
|
374
|
+
self.name,
|
375
|
+
self.rssi,
|
376
|
+
)
|
377
|
+
self._reset_disconnect_timer()
|
378
|
+
return
|
379
|
+
self._cancel_disconnect_timer()
|
380
|
+
self._timed_disconnect_task = asyncio.create_task(self._execute_timed_disconnect())
|
381
|
+
|
382
|
+
def _cancel_disconnect_timer(self) -> None:
|
383
|
+
"""Cancel disconnect timer."""
|
384
|
+
if self._disconnect_timer:
|
385
|
+
self._disconnect_timer.cancel()
|
386
|
+
self._disconnect_timer = None
|
387
|
+
|
388
|
+
async def _execute_forced_disconnect(self) -> None:
|
389
|
+
"""Execute forced disconnection."""
|
390
|
+
self._cancel_disconnect_timer()
|
391
|
+
_LOGGER.debug(
|
392
|
+
"%s: Executing forced disconnect",
|
393
|
+
self.name,
|
394
|
+
)
|
395
|
+
await self._execute_disconnect()
|
396
|
+
|
397
|
+
async def _execute_timed_disconnect(self) -> None:
|
398
|
+
"""Execute timed disconnection."""
|
399
|
+
if not self._disconnect_strategy:
|
400
|
+
return
|
401
|
+
_LOGGER.debug(
|
402
|
+
"%s: Executing timed disconnect after timeout of %s",
|
403
|
+
self.name,
|
404
|
+
DISCONNECT_DELAY,
|
405
|
+
)
|
406
|
+
await self._execute_disconnect()
|
407
|
+
|
408
|
+
async def _execute_disconnect(self) -> None:
|
409
|
+
"""Execute disconnection."""
|
410
|
+
_LOGGER.debug("%s: Executing disconnect", self.name)
|
411
|
+
async with self._connect_lock:
|
412
|
+
await self._execute_disconnect_with_lock()
|
413
|
+
|
414
|
+
async def _execute_disconnect_with_lock(self) -> None:
|
415
|
+
"""Execute disconnection while holding the lock."""
|
416
|
+
assert self._connect_lock.locked(), "Lock not held"
|
417
|
+
_LOGGER.debug("%s: Executing disconnect with lock", self.name)
|
418
|
+
if self._disconnect_timer: # If the timer was reset, don't disconnect
|
419
|
+
_LOGGER.debug("%s: Skipping disconnect as timer reset", self.name)
|
420
|
+
return
|
421
|
+
client = self._client
|
422
|
+
self._expected_disconnect = True
|
423
|
+
|
424
|
+
if not client:
|
425
|
+
_LOGGER.debug("%s: Already disconnected", self.name)
|
426
|
+
return
|
427
|
+
_LOGGER.debug("%s: Disconnecting", self.name)
|
428
|
+
try:
|
429
|
+
"""We reset what command the robot last heard before disconnecting."""
|
430
|
+
if client is not None and client.is_connected:
|
431
|
+
command_bytes = self._commands.send_todev_ble_sync(2)
|
432
|
+
await self._message.post_custom_data_bytes(command_bytes)
|
433
|
+
await client.stop_notify(self._read_char)
|
434
|
+
await client.disconnect()
|
435
|
+
except BLEAK_RETRY_EXCEPTIONS as ex:
|
436
|
+
_LOGGER.warning(
|
437
|
+
"%s: Error disconnecting: %s; RSSI: %s",
|
438
|
+
self.name,
|
439
|
+
ex,
|
440
|
+
self.rssi,
|
441
|
+
)
|
442
|
+
else:
|
443
|
+
_LOGGER.debug("%s: Disconnect completed successfully", self.name)
|
444
|
+
self._client = None
|
445
|
+
|
446
|
+
def set_disconnect_strategy(self, disconnect: bool) -> None:
|
447
|
+
self._disconnect_strategy = disconnect
|
@@ -0,0 +1,246 @@
|
|
1
|
+
import asyncio
|
2
|
+
import base64
|
3
|
+
import json
|
4
|
+
import logging
|
5
|
+
from collections import deque
|
6
|
+
from typing import Any, Awaitable, Callable, Optional, cast
|
7
|
+
|
8
|
+
import betterproto
|
9
|
+
|
10
|
+
from pymammotion import MammotionMQTT
|
11
|
+
from pymammotion.aliyun.dataclass.dev_by_account_response import Device
|
12
|
+
from pymammotion.data.model.device import MowingDevice
|
13
|
+
from pymammotion.data.mqtt.event import ThingEventMessage
|
14
|
+
from pymammotion.event.event import DataEvent
|
15
|
+
from pymammotion.mammotion.commands.mammotion_command import MammotionCommand
|
16
|
+
from pymammotion.mammotion.devices.base import MammotionBaseDevice
|
17
|
+
from pymammotion.mqtt.mammotion_future import MammotionFuture
|
18
|
+
from pymammotion.proto import has_field
|
19
|
+
from pymammotion.proto.luba_msg import LubaMsg
|
20
|
+
|
21
|
+
_LOGGER = logging.getLogger(__name__)
|
22
|
+
|
23
|
+
|
24
|
+
class MammotionCloud:
|
25
|
+
"""Per account MQTT cloud."""
|
26
|
+
|
27
|
+
def __init__(self, mqtt_client: MammotionMQTT) -> None:
|
28
|
+
self.loop = asyncio.get_event_loop()
|
29
|
+
self._ble_sync_task = None
|
30
|
+
self.is_ready = False
|
31
|
+
self.command_queue = asyncio.Queue()
|
32
|
+
self._waiting_queue = deque()
|
33
|
+
self.mqtt_message_event = DataEvent()
|
34
|
+
self.on_ready_event = DataEvent()
|
35
|
+
self._operation_lock = asyncio.Lock()
|
36
|
+
self._mqtt_client = mqtt_client
|
37
|
+
self._mqtt_client.on_connected = self.on_connected
|
38
|
+
self._mqtt_client.on_disconnected = self.on_disconnected
|
39
|
+
self._mqtt_client.on_message = self._on_mqtt_message
|
40
|
+
self._mqtt_client.on_ready = self.on_ready
|
41
|
+
|
42
|
+
# temporary for testing only
|
43
|
+
# self._start_sync_task = self.loop.call_later(30, lambda: asyncio.ensure_future(self.start_sync(0)))
|
44
|
+
|
45
|
+
def on_ready(self) -> None:
|
46
|
+
self.on_ready_event.data_event(None)
|
47
|
+
|
48
|
+
def is_connected(self) -> bool:
|
49
|
+
return self._mqtt_client.is_connected
|
50
|
+
|
51
|
+
def disconnect(self) -> None:
|
52
|
+
self._mqtt_client.disconnect()
|
53
|
+
|
54
|
+
def connect_async(self) -> None:
|
55
|
+
self._mqtt_client.connect_async()
|
56
|
+
|
57
|
+
def send_command(self, iot_id: str, command: bytes) -> None:
|
58
|
+
self._mqtt_client.get_cloud_client().send_cloud_command(iot_id, command)
|
59
|
+
|
60
|
+
async def on_connected(self) -> None:
|
61
|
+
"""Callback for when MQTT connects."""
|
62
|
+
|
63
|
+
async def on_disconnected(self) -> None:
|
64
|
+
"""Callback for when MQTT disconnects."""
|
65
|
+
|
66
|
+
async def _process_queue(self) -> None:
|
67
|
+
while True:
|
68
|
+
# Get the next item from the queue
|
69
|
+
iot_id, key, command, future = await self.command_queue.get()
|
70
|
+
try:
|
71
|
+
# Process the command using _execute_command_locked
|
72
|
+
result = await self._execute_command_locked(iot_id, key, command)
|
73
|
+
# Set the result on the future
|
74
|
+
future.set_result(result)
|
75
|
+
except Exception as ex:
|
76
|
+
# Set the exception on the future if something goes wrong
|
77
|
+
future.set_exception(ex)
|
78
|
+
finally:
|
79
|
+
# Mark the task as done
|
80
|
+
self.command_queue.task_done()
|
81
|
+
|
82
|
+
async def _execute_command_locked(self, iot_id: str, key: str, command: bytes) -> bytes:
|
83
|
+
"""Execute command and read response."""
|
84
|
+
assert self._mqtt_client is not None
|
85
|
+
self._key = key
|
86
|
+
_LOGGER.debug("Sending command: %s", key)
|
87
|
+
|
88
|
+
await self.loop.run_in_executor(None, self._mqtt_client.get_cloud_client().send_cloud_command, iot_id, command)
|
89
|
+
future = MammotionFuture(iot_id)
|
90
|
+
self._waiting_queue.append(future)
|
91
|
+
timeout = 5
|
92
|
+
try:
|
93
|
+
notify_msg = await future.async_get(timeout)
|
94
|
+
except asyncio.TimeoutError:
|
95
|
+
notify_msg = b""
|
96
|
+
|
97
|
+
_LOGGER.debug("%s: Message received", iot_id)
|
98
|
+
|
99
|
+
return notify_msg
|
100
|
+
|
101
|
+
async def _on_mqtt_message(self, topic: str, payload: str, iot_id: str) -> None:
|
102
|
+
"""Handle incoming MQTT messages."""
|
103
|
+
_LOGGER.debug("MQTT message received on topic %s: %s, iot_id: %s", topic, payload, iot_id)
|
104
|
+
|
105
|
+
json_str = json.dumps(payload)
|
106
|
+
payload = json.loads(json_str)
|
107
|
+
|
108
|
+
await self._handle_mqtt_message(topic, payload)
|
109
|
+
|
110
|
+
async def _parse_mqtt_response(self, topic: str, payload: dict) -> None:
|
111
|
+
"""Parse the MQTT response."""
|
112
|
+
if topic.endswith("/app/down/thing/events"):
|
113
|
+
_LOGGER.debug("Thing event received")
|
114
|
+
event = ThingEventMessage.from_dicts(payload)
|
115
|
+
params = event.params
|
116
|
+
if params.get("identifier", None) is None:
|
117
|
+
return
|
118
|
+
if params.identifier == "device_protobuf_msg_event" and event.method == "thing.events":
|
119
|
+
_LOGGER.debug("Protobuf event")
|
120
|
+
# Call the callbacks for each cloudDevice
|
121
|
+
await self.mqtt_message_event.data_event(event)
|
122
|
+
if event.method == "thing.properties":
|
123
|
+
_LOGGER.debug(event)
|
124
|
+
|
125
|
+
async def _handle_mqtt_message(self, topic: str, payload: dict) -> None:
|
126
|
+
"""Async handler for incoming MQTT messages."""
|
127
|
+
await self._parse_mqtt_response(topic=topic, payload=payload)
|
128
|
+
|
129
|
+
def _disconnect(self) -> None:
|
130
|
+
"""Disconnect the MQTT client."""
|
131
|
+
self._mqtt_client.disconnect()
|
132
|
+
|
133
|
+
@property
|
134
|
+
def waiting_queue(self):
|
135
|
+
return self._waiting_queue
|
136
|
+
|
137
|
+
|
138
|
+
class MammotionBaseCloudDevice(MammotionBaseDevice):
|
139
|
+
"""Base class for Mammotion Cloud devices."""
|
140
|
+
|
141
|
+
def __init__(self, mqtt: MammotionCloud, cloud_device: Device, mowing_state: MowingDevice) -> None:
|
142
|
+
"""Initialize MammotionBaseCloudDevice."""
|
143
|
+
super().__init__(mowing_state, cloud_device)
|
144
|
+
self.on_ready_callback: Optional[Callable[[], Awaitable[None]]] = None
|
145
|
+
self.loop = asyncio.get_event_loop()
|
146
|
+
self._mqtt = mqtt
|
147
|
+
self.iot_id = cloud_device.iotId
|
148
|
+
self.device = cloud_device
|
149
|
+
self._mower = mowing_state
|
150
|
+
self._command_futures = {}
|
151
|
+
self._commands: MammotionCommand = MammotionCommand(cloud_device.deviceName)
|
152
|
+
self.currentID = ""
|
153
|
+
self._mqtt.mqtt_message_event.add_subscribers(self._parse_message_for_device)
|
154
|
+
self._mqtt.on_ready_event.add_subscribers(self.on_ready)
|
155
|
+
|
156
|
+
if self._mqtt.is_ready:
|
157
|
+
self.run_periodic_sync_task()
|
158
|
+
|
159
|
+
async def on_ready(self) -> None:
|
160
|
+
"""Callback for when MQTT is subscribed to events."""
|
161
|
+
loop = asyncio.get_event_loop()
|
162
|
+
|
163
|
+
await self._ble_sync()
|
164
|
+
await self.run_periodic_sync_task()
|
165
|
+
loop.create_task(self._process_queue())
|
166
|
+
if self.on_ready_callback:
|
167
|
+
await self.on_ready_callback()
|
168
|
+
|
169
|
+
async def _ble_sync(self) -> None:
|
170
|
+
command_bytes = self._commands.send_todev_ble_sync(3)
|
171
|
+
loop = asyncio.get_running_loop()
|
172
|
+
await loop.run_in_executor(None, self._mqtt.send_command, self.iot_id, command_bytes)
|
173
|
+
|
174
|
+
async def run_periodic_sync_task(self) -> None:
|
175
|
+
"""Send ble sync to robot."""
|
176
|
+
try:
|
177
|
+
if not self._mqtt._operation_lock.locked():
|
178
|
+
await self._ble_sync()
|
179
|
+
finally:
|
180
|
+
self.schedule_ble_sync()
|
181
|
+
|
182
|
+
def schedule_ble_sync(self) -> None:
|
183
|
+
"""Periodically sync to keep connection alive."""
|
184
|
+
if self._mqtt is not None and self._mqtt.is_connected:
|
185
|
+
self._ble_sync_task = self.loop.call_later(
|
186
|
+
160, lambda: asyncio.ensure_future(self.run_periodic_sync_task())
|
187
|
+
)
|
188
|
+
|
189
|
+
async def queue_command(self, key: str, **kwargs: Any) -> bytes:
|
190
|
+
# Create a future to hold the result
|
191
|
+
_LOGGER.debug("Queueing command: %s", key)
|
192
|
+
future = asyncio.Future()
|
193
|
+
# Put the command in the queue as a tuple (key, command, future)
|
194
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
195
|
+
await self._mqtt.command_queue.put((self.iot_id, key, command_bytes, future))
|
196
|
+
# Wait for the future to be resolved
|
197
|
+
return await future
|
198
|
+
|
199
|
+
def _extract_message_id(self, payload: dict) -> str:
|
200
|
+
"""Extract the message ID from the payload."""
|
201
|
+
return payload.get("id", "")
|
202
|
+
|
203
|
+
def _extract_encoded_message(self, payload: dict) -> str:
|
204
|
+
"""Extract the encoded message from the payload."""
|
205
|
+
try:
|
206
|
+
content = payload.get("data", {}).get("data", {}).get("params", {}).get("content", "")
|
207
|
+
return str(content)
|
208
|
+
except AttributeError:
|
209
|
+
_LOGGER.error("Error extracting encoded message. Payload: %s", payload)
|
210
|
+
return ""
|
211
|
+
|
212
|
+
@staticmethod
|
213
|
+
def dequeue_by_iot_id(queue, iot_id):
|
214
|
+
for item in queue:
|
215
|
+
if item.iot_id == iot_id:
|
216
|
+
queue.remove(item)
|
217
|
+
return item
|
218
|
+
return None
|
219
|
+
|
220
|
+
async def _parse_message_for_device(self, event: ThingEventMessage) -> None:
|
221
|
+
params = event.params
|
222
|
+
if event.params.iotId != self.iot_id:
|
223
|
+
return
|
224
|
+
binary_data = base64.b64decode(params.value.content.proto)
|
225
|
+
self._update_raw_data(cast(bytes, binary_data))
|
226
|
+
new_msg = LubaMsg().parse(cast(bytes, binary_data))
|
227
|
+
|
228
|
+
if (
|
229
|
+
self._commands.get_device_product_key() == ""
|
230
|
+
and self._commands.get_device_name() == event.params.deviceName
|
231
|
+
):
|
232
|
+
self._commands.set_device_product_key(event.params.productKey)
|
233
|
+
|
234
|
+
if betterproto.serialized_on_wire(new_msg.net):
|
235
|
+
if new_msg.net.todev_ble_sync != 0 or has_field(new_msg.net.toapp_wifi_iot_status):
|
236
|
+
return
|
237
|
+
|
238
|
+
if len(self._mqtt.waiting_queue) > 0:
|
239
|
+
fut: MammotionFuture = self.dequeue_by_iot_id(self._mqtt.waiting_queue, self.iot_id)
|
240
|
+
if fut is None:
|
241
|
+
return
|
242
|
+
while fut.fut.cancelled() and len(self._mqtt.waiting_queue) > 0:
|
243
|
+
fut: MammotionFuture = self.dequeue_by_iot_id(self._mqtt.waiting_queue, self.iot_id)
|
244
|
+
if not fut.fut.cancelled():
|
245
|
+
fut.resolve(cast(bytes, binary_data))
|
246
|
+
await self._state_manager.notification(new_msg)
|