pymammotion 0.5.69__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 +53 -0
- pymammotion/agora/__init__.py +0 -0
- pymammotion/agora/agora_api.py +755 -0
- pymammotion/agora/agora_rtc_capabilities.py +748 -0
- pymammotion/agora/agora_websockets.py +1175 -0
- pymammotion/aliyun/__init__.py +1 -0
- pymammotion/aliyun/client.py +235 -0
- pymammotion/aliyun/cloud_gateway.py +982 -0
- pymammotion/aliyun/model/aep_response.py +21 -0
- pymammotion/aliyun/model/connect_response.py +51 -0
- pymammotion/aliyun/model/dev_by_account_response.py +195 -0
- pymammotion/aliyun/model/login_by_oauth_response.py +64 -0
- pymammotion/aliyun/model/regions_response.py +29 -0
- pymammotion/aliyun/model/session_by_authcode_response.py +19 -0
- pymammotion/aliyun/model/thing_response.py +12 -0
- pymammotion/aliyun/regions.py +62 -0
- pymammotion/aliyun/tea/core.py +297 -0
- pymammotion/aliyun/tmp_constant.py +171 -0
- pymammotion/bluetooth/__init__.py +1 -0
- pymammotion/bluetooth/ble.py +62 -0
- pymammotion/bluetooth/ble_message.py +676 -0
- pymammotion/bluetooth/const.py +27 -0
- pymammotion/bluetooth/data/__init__.py +0 -0
- pymammotion/bluetooth/data/convert.py +25 -0
- pymammotion/bluetooth/data/framectrldata.py +40 -0
- pymammotion/bluetooth/data/notifydata.py +62 -0
- pymammotion/bluetooth/model/__init__.py +0 -0
- pymammotion/bluetooth/model/atomic_integer.py +54 -0
- pymammotion/const.py +13 -0
- pymammotion/data/__init__.py +0 -0
- pymammotion/data/model/__init__.py +8 -0
- pymammotion/data/model/account.py +8 -0
- pymammotion/data/model/device.py +192 -0
- pymammotion/data/model/device_config.py +72 -0
- pymammotion/data/model/device_info.py +60 -0
- pymammotion/data/model/device_limits.py +49 -0
- pymammotion/data/model/enums.py +77 -0
- pymammotion/data/model/errors.py +12 -0
- pymammotion/data/model/events.py +14 -0
- pymammotion/data/model/generate_geojson.py +565 -0
- pymammotion/data/model/generate_route_information.py +26 -0
- pymammotion/data/model/hash_list.py +475 -0
- pymammotion/data/model/location.py +36 -0
- pymammotion/data/model/mowing_modes.py +77 -0
- pymammotion/data/model/rapid_state.py +45 -0
- pymammotion/data/model/raw_data.py +215 -0
- pymammotion/data/model/region_data.py +102 -0
- pymammotion/data/model/report_info.py +182 -0
- pymammotion/data/model/work.py +27 -0
- pymammotion/data/mower_state_manager.py +369 -0
- pymammotion/data/mqtt/__init__.py +1 -0
- pymammotion/data/mqtt/event.py +227 -0
- pymammotion/data/mqtt/mammotion_properties.py +276 -0
- pymammotion/data/mqtt/properties.py +203 -0
- pymammotion/data/mqtt/status.py +57 -0
- pymammotion/event/__init__.py +6 -0
- pymammotion/event/event.py +96 -0
- pymammotion/homeassistant/__init__.py +3 -0
- pymammotion/homeassistant/mower_api.py +514 -0
- pymammotion/homeassistant/rtk_api.py +54 -0
- pymammotion/http/__init__.py +0 -0
- pymammotion/http/encryption.py +220 -0
- pymammotion/http/http.py +673 -0
- pymammotion/http/model/__init__.py +0 -0
- pymammotion/http/model/camera_stream.py +31 -0
- pymammotion/http/model/http.py +249 -0
- pymammotion/http/model/response_factory.py +61 -0
- pymammotion/http/model/rtk.py +16 -0
- pymammotion/mammotion/__init__.py +0 -0
- pymammotion/mammotion/commands/__init__.py +0 -0
- pymammotion/mammotion/commands/abstract_message.py +24 -0
- pymammotion/mammotion/commands/mammotion_command.py +81 -0
- pymammotion/mammotion/commands/messages/__init__.py +0 -0
- pymammotion/mammotion/commands/messages/basestation.py +43 -0
- pymammotion/mammotion/commands/messages/driver.py +122 -0
- pymammotion/mammotion/commands/messages/media.py +87 -0
- pymammotion/mammotion/commands/messages/navigation.py +564 -0
- pymammotion/mammotion/commands/messages/network.py +205 -0
- pymammotion/mammotion/commands/messages/ota.py +38 -0
- pymammotion/mammotion/commands/messages/system.py +330 -0
- pymammotion/mammotion/commands/messages/video.py +33 -0
- pymammotion/mammotion/control/__init__.py +0 -0
- pymammotion/mammotion/control/joystick.py +145 -0
- pymammotion/mammotion/devices/__init__.py +29 -0
- pymammotion/mammotion/devices/base.py +163 -0
- pymammotion/mammotion/devices/mammotion.py +571 -0
- pymammotion/mammotion/devices/mammotion_bluetooth.py +496 -0
- pymammotion/mammotion/devices/mammotion_cloud.py +355 -0
- pymammotion/mammotion/devices/mammotion_mower_ble.py +48 -0
- pymammotion/mammotion/devices/mammotion_mower_cloud.py +39 -0
- pymammotion/mammotion/devices/managers/managers.py +81 -0
- pymammotion/mammotion/devices/mower_device.py +120 -0
- pymammotion/mammotion/devices/mower_manager.py +107 -0
- pymammotion/mammotion/devices/rtk_ble.py +89 -0
- pymammotion/mammotion/devices/rtk_cloud.py +115 -0
- pymammotion/mammotion/devices/rtk_device.py +50 -0
- pymammotion/mammotion/devices/rtk_manager.py +125 -0
- pymammotion/mqtt/__init__.py +6 -0
- pymammotion/mqtt/aliyun_mqtt.py +237 -0
- pymammotion/mqtt/linkkit/__init__.py +5 -0
- pymammotion/mqtt/linkkit/h2client.py +585 -0
- pymammotion/mqtt/linkkit/linkkit.py +3025 -0
- pymammotion/mqtt/mammotion_future.py +26 -0
- pymammotion/mqtt/mammotion_mqtt.py +214 -0
- pymammotion/mqtt/mqtt_models.py +66 -0
- pymammotion/proto/__init__.py +4841 -0
- pymammotion/proto/basestation.proto +51 -0
- pymammotion/proto/basestation_pb2.py +35 -0
- pymammotion/proto/basestation_pb2.pyi +89 -0
- pymammotion/proto/common.proto +7 -0
- pymammotion/proto/common_pb2.py +25 -0
- pymammotion/proto/common_pb2.pyi +13 -0
- pymammotion/proto/dev_net.proto +321 -0
- pymammotion/proto/dev_net_pb2.py +111 -0
- pymammotion/proto/dev_net_pb2.pyi +515 -0
- pymammotion/proto/luba_msg.proto +76 -0
- pymammotion/proto/luba_msg_pb2.py +41 -0
- pymammotion/proto/luba_msg_pb2.pyi +97 -0
- pymammotion/proto/luba_mul.proto +129 -0
- pymammotion/proto/luba_mul_pb2.py +61 -0
- pymammotion/proto/luba_mul_pb2.pyi +178 -0
- pymammotion/proto/mctrl_driver.proto +107 -0
- pymammotion/proto/mctrl_driver_pb2.py +57 -0
- pymammotion/proto/mctrl_driver_pb2.pyi +167 -0
- pymammotion/proto/mctrl_nav.proto +591 -0
- pymammotion/proto/mctrl_nav_pb2.py +136 -0
- pymammotion/proto/mctrl_nav_pb2.pyi +1067 -0
- pymammotion/proto/mctrl_ota.proto +80 -0
- pymammotion/proto/mctrl_ota_pb2.py +45 -0
- pymammotion/proto/mctrl_ota_pb2.pyi +128 -0
- pymammotion/proto/mctrl_pept.proto +34 -0
- pymammotion/proto/mctrl_pept_pb2.py +33 -0
- pymammotion/proto/mctrl_pept_pb2.pyi +58 -0
- pymammotion/proto/mctrl_sys.proto +741 -0
- pymammotion/proto/mctrl_sys_pb2.py +206 -0
- pymammotion/proto/mctrl_sys_pb2.pyi +1213 -0
- pymammotion/proto/message_pool.py +3 -0
- pymammotion/proto/py.typed +0 -0
- pymammotion/py.typed +0 -0
- pymammotion/utility/constant/__init__.py +3 -0
- pymammotion/utility/constant/device_constant.py +315 -0
- pymammotion/utility/conversions.py +5 -0
- pymammotion/utility/datatype_converter.py +124 -0
- pymammotion/utility/device_config.py +755 -0
- pymammotion/utility/device_type.py +489 -0
- pymammotion/utility/map.py +259 -0
- pymammotion/utility/movement.py +18 -0
- pymammotion/utility/mur_mur_hash.py +159 -0
- pymammotion/utility/periodic.py +106 -0
- pymammotion/utility/rocker_util.py +194 -0
- pymammotion-0.5.69.dist-info/METADATA +93 -0
- pymammotion-0.5.69.dist-info/RECORD +154 -0
- pymammotion-0.5.69.dist-info/WHEEL +4 -0
- pymammotion-0.5.69.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from asyncio import TimerHandle
|
|
3
|
+
from collections.abc import Awaitable, Callable
|
|
4
|
+
import logging
|
|
5
|
+
import time
|
|
6
|
+
from typing import Any, cast
|
|
7
|
+
from uuid import UUID
|
|
8
|
+
|
|
9
|
+
import betterproto2
|
|
10
|
+
from bleak import BleakGATTCharacteristic, BleakGATTServiceCollection, BLEDevice
|
|
11
|
+
from bleak.exc import BleakDBusError
|
|
12
|
+
from bleak_retry_connector import (
|
|
13
|
+
BLEAK_RETRY_EXCEPTIONS,
|
|
14
|
+
BleakClientWithServiceCache,
|
|
15
|
+
BleakNotFoundError,
|
|
16
|
+
establish_connection,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
20
|
+
from pymammotion.bluetooth import BleMessage
|
|
21
|
+
from pymammotion.data.mower_state_manager import MowerStateManager
|
|
22
|
+
from pymammotion.mammotion.commands.mammotion_command import MammotionCommand
|
|
23
|
+
from pymammotion.mammotion.devices.base import MammotionBaseDevice
|
|
24
|
+
from pymammotion.proto import DevNet, LubaMsg
|
|
25
|
+
|
|
26
|
+
DBUS_ERROR_BACKOFF_TIME = 0.25
|
|
27
|
+
|
|
28
|
+
DISCONNECT_DELAY = 10
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_LOGGER = logging.getLogger(__name__)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CharacteristicMissingError(Exception):
|
|
35
|
+
"""Raised when a characteristic is missing."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _sb_uuid(comms_type: str = "service") -> UUID | str:
|
|
39
|
+
"""Return Mammotion UUID.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
comms_type (str): The type of communication (tx, rx, or service).
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
UUID | str: The UUID for the specified communication type or an error message.
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
_uuid = {"tx": "ff01", "rx": "ff02", "service": "2A05"}
|
|
49
|
+
|
|
50
|
+
if comms_type in _uuid:
|
|
51
|
+
return UUID(f"0000{_uuid[comms_type]}-0000-1000-8000-00805f9b34fb")
|
|
52
|
+
|
|
53
|
+
return "Incorrect type, choose between: tx, rx or service"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
READ_CHAR_UUID = _sb_uuid(comms_type="rx")
|
|
57
|
+
WRITE_CHAR_UUID = _sb_uuid(comms_type="tx")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _handle_timeout(fut: asyncio.Future[None]) -> None:
|
|
61
|
+
"""Handle a timeout."""
|
|
62
|
+
if not fut.done():
|
|
63
|
+
fut.set_exception(asyncio.TimeoutError)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
async def _handle_retry(fut: asyncio.Future[None], func, command: bytes) -> None:
|
|
67
|
+
"""Handle a retry."""
|
|
68
|
+
if not fut.done():
|
|
69
|
+
await func(command)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class MammotionBaseBLEDevice(MammotionBaseDevice):
|
|
73
|
+
"""Base class for Mammotion BLE devices."""
|
|
74
|
+
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
state_manager: MowerStateManager,
|
|
78
|
+
cloud_device: Device,
|
|
79
|
+
device: BLEDevice,
|
|
80
|
+
interface: int = 0,
|
|
81
|
+
**kwargs: Any,
|
|
82
|
+
) -> None:
|
|
83
|
+
"""Initialize MammotionBaseBLEDevice."""
|
|
84
|
+
super().__init__(state_manager, cloud_device)
|
|
85
|
+
self.command_sent_time: float = 0.0
|
|
86
|
+
self._disconnect_strategy = True
|
|
87
|
+
self._ble_sync_task: TimerHandle | None = None
|
|
88
|
+
self._prev_notification = None
|
|
89
|
+
self._interface = f"hci{interface}"
|
|
90
|
+
self.ble_device = device
|
|
91
|
+
self._client: BleakClientWithServiceCache | None = None
|
|
92
|
+
self._read_char: BleakGATTCharacteristic | int | str | UUID = 0
|
|
93
|
+
self._write_char: BleakGATTCharacteristic | int | str | UUID = 0
|
|
94
|
+
self._disconnect_timer: asyncio.TimerHandle | None = None
|
|
95
|
+
self._message: BleMessage | None = None
|
|
96
|
+
self._commands: MammotionCommand = MammotionCommand(device.name or "", 1)
|
|
97
|
+
self.command_queue = asyncio.Queue()
|
|
98
|
+
self._expected_disconnect = False
|
|
99
|
+
self._connect_lock = asyncio.Lock()
|
|
100
|
+
self._operation_lock = asyncio.Lock()
|
|
101
|
+
self._key: str | None = None
|
|
102
|
+
self._cloud_device = cloud_device
|
|
103
|
+
self.set_queue_callback(self.queue_command)
|
|
104
|
+
loop = asyncio.get_event_loop()
|
|
105
|
+
loop.create_task(self.process_queue())
|
|
106
|
+
|
|
107
|
+
def __del__(self) -> None:
|
|
108
|
+
"""Cleanup."""
|
|
109
|
+
if self._disconnect_timer:
|
|
110
|
+
self._disconnect_timer.cancel()
|
|
111
|
+
if self._ble_sync_task:
|
|
112
|
+
self._ble_sync_task.cancel()
|
|
113
|
+
|
|
114
|
+
self._state_manager.ble_queue_command_callback.remove_subscribers(self.queue_command)
|
|
115
|
+
|
|
116
|
+
def set_notification_callback(self, func: Callable[[tuple[str, Any | None]], Awaitable[None]]) -> None:
|
|
117
|
+
self._state_manager.ble_on_notification_callback.add_subscribers(func)
|
|
118
|
+
|
|
119
|
+
def set_queue_callback(self, func: Callable[[str, dict[str, Any]], Awaitable[None]]) -> None:
|
|
120
|
+
self._state_manager.ble_queue_command_callback.add_subscribers(func)
|
|
121
|
+
|
|
122
|
+
def update_device(self, device: BLEDevice) -> None:
|
|
123
|
+
"""Update the BLE device."""
|
|
124
|
+
self.ble_device = device
|
|
125
|
+
|
|
126
|
+
async def _ble_sync(self) -> None:
|
|
127
|
+
if self._client is not None and self._client.is_connected:
|
|
128
|
+
command_bytes = self._commands.send_todev_ble_sync(2)
|
|
129
|
+
await self._message.post_custom_data_bytes(command_bytes)
|
|
130
|
+
|
|
131
|
+
async def run_periodic_sync_task(self) -> None:
|
|
132
|
+
"""Send ble sync to robot."""
|
|
133
|
+
try:
|
|
134
|
+
await self._ble_sync()
|
|
135
|
+
finally:
|
|
136
|
+
self.schedule_ble_sync()
|
|
137
|
+
|
|
138
|
+
def schedule_ble_sync(self) -> None:
|
|
139
|
+
"""Periodically sync to keep connection alive."""
|
|
140
|
+
if self._client is not None and self._client.is_connected:
|
|
141
|
+
self._ble_sync_task = self.loop.call_later(
|
|
142
|
+
130, lambda: asyncio.ensure_future(self.run_periodic_sync_task())
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
async def stop(self) -> None:
|
|
146
|
+
"""Stop all tasks and disconnect."""
|
|
147
|
+
if self._ble_sync_task:
|
|
148
|
+
self._ble_sync_task.cancel()
|
|
149
|
+
if self._client is not None and self._client.is_connected:
|
|
150
|
+
await self._client.disconnect()
|
|
151
|
+
|
|
152
|
+
async def queue_command(self, key: str, **kwargs: Any) -> None:
|
|
153
|
+
# Create a future to hold the result
|
|
154
|
+
_LOGGER.debug("Queueing command: %s", key)
|
|
155
|
+
future = asyncio.Future()
|
|
156
|
+
# Put the command in the queue as a tuple (key, command, future)
|
|
157
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
|
158
|
+
await self.command_queue.put((key, command_bytes, future))
|
|
159
|
+
# Wait for the future to be resolved
|
|
160
|
+
await future
|
|
161
|
+
# return await self._send_command_with_args(key, **kwargs)
|
|
162
|
+
|
|
163
|
+
async def process_queue(self) -> None:
|
|
164
|
+
while True:
|
|
165
|
+
# Get the next item from the queue
|
|
166
|
+
key, command, future = await self.command_queue.get()
|
|
167
|
+
try:
|
|
168
|
+
# Process the command using _execute_command_locked
|
|
169
|
+
await self._send_command_locked(key, command)
|
|
170
|
+
# Set the result on the future
|
|
171
|
+
future.set_result(None)
|
|
172
|
+
except Exception as ex:
|
|
173
|
+
# Set the exception on the future if something goes wrong
|
|
174
|
+
future.set_exception(ex)
|
|
175
|
+
finally:
|
|
176
|
+
# Mark the task as done
|
|
177
|
+
self.command_queue.task_done()
|
|
178
|
+
|
|
179
|
+
async def _send_command_with_args(self, key: str, **kwargs) -> None:
|
|
180
|
+
"""Send command to device and read response."""
|
|
181
|
+
if self._operation_lock.locked():
|
|
182
|
+
_LOGGER.debug(
|
|
183
|
+
"%s: Operation already in progress, waiting for it to complete; RSSI: %s",
|
|
184
|
+
self.name,
|
|
185
|
+
self.rssi,
|
|
186
|
+
)
|
|
187
|
+
async with self._operation_lock:
|
|
188
|
+
try:
|
|
189
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
|
190
|
+
return await self._send_command_locked(key, command_bytes)
|
|
191
|
+
except BleakNotFoundError:
|
|
192
|
+
_LOGGER.exception(
|
|
193
|
+
"%s: device not found, no longer in range, or poor RSSI: %s",
|
|
194
|
+
self.name,
|
|
195
|
+
self.rssi,
|
|
196
|
+
)
|
|
197
|
+
raise
|
|
198
|
+
except CharacteristicMissingError as ex:
|
|
199
|
+
_LOGGER.debug(
|
|
200
|
+
"%s: characteristic missing: %s; RSSI: %s",
|
|
201
|
+
self.name,
|
|
202
|
+
ex,
|
|
203
|
+
self.rssi,
|
|
204
|
+
exc_info=True,
|
|
205
|
+
)
|
|
206
|
+
except BLEAK_RETRY_EXCEPTIONS:
|
|
207
|
+
_LOGGER.debug("%s: communication failed with:", self.name, exc_info=True)
|
|
208
|
+
return
|
|
209
|
+
|
|
210
|
+
async def _send_command(self, key: str, retry: int | None = None) -> None:
|
|
211
|
+
"""Send command to device and read response."""
|
|
212
|
+
if self._operation_lock.locked():
|
|
213
|
+
_LOGGER.debug(
|
|
214
|
+
"%s: Operation already in progress, waiting for it to complete; RSSI: %s",
|
|
215
|
+
self.name,
|
|
216
|
+
self.rssi,
|
|
217
|
+
)
|
|
218
|
+
async with self._operation_lock:
|
|
219
|
+
try:
|
|
220
|
+
command_bytes = getattr(self._commands, key)()
|
|
221
|
+
return await self._send_command_locked(key, command_bytes)
|
|
222
|
+
except BleakNotFoundError:
|
|
223
|
+
_LOGGER.exception(
|
|
224
|
+
"%s: device not found, no longer in range, or poor RSSI: %s",
|
|
225
|
+
self.name,
|
|
226
|
+
self.rssi,
|
|
227
|
+
)
|
|
228
|
+
raise
|
|
229
|
+
except CharacteristicMissingError as ex:
|
|
230
|
+
_LOGGER.debug(
|
|
231
|
+
"%s: characteristic missing: %s; RSSI: %s",
|
|
232
|
+
self.name,
|
|
233
|
+
ex,
|
|
234
|
+
self.rssi,
|
|
235
|
+
exc_info=True,
|
|
236
|
+
)
|
|
237
|
+
except BLEAK_RETRY_EXCEPTIONS:
|
|
238
|
+
_LOGGER.debug("%s: communication failed with:", self.name, exc_info=True)
|
|
239
|
+
return
|
|
240
|
+
|
|
241
|
+
@property
|
|
242
|
+
def name(self) -> str:
|
|
243
|
+
"""Return device name."""
|
|
244
|
+
return f"{self.ble_device.name} ({self.ble_device.address})"
|
|
245
|
+
|
|
246
|
+
@property
|
|
247
|
+
def rssi(self) -> int:
|
|
248
|
+
"""Return RSSI of device."""
|
|
249
|
+
return self.mower.report_data.connect.ble_rssi
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def client(self) -> BleakClientWithServiceCache | None:
|
|
253
|
+
"""Return client."""
|
|
254
|
+
return self._client
|
|
255
|
+
|
|
256
|
+
async def _ensure_connected(self) -> None:
|
|
257
|
+
"""Ensure connection to device is established."""
|
|
258
|
+
if self._connect_lock.locked():
|
|
259
|
+
_LOGGER.debug(
|
|
260
|
+
"%s: Connection already in progress, waiting for it to complete; RSSI: %s",
|
|
261
|
+
self.name,
|
|
262
|
+
self.rssi,
|
|
263
|
+
)
|
|
264
|
+
if self._client and self._client.is_connected:
|
|
265
|
+
_LOGGER.debug(
|
|
266
|
+
"%s: Already connected before obtaining lock, resetting timer; RSSI: %s",
|
|
267
|
+
self.name,
|
|
268
|
+
self.rssi,
|
|
269
|
+
)
|
|
270
|
+
self._reset_disconnect_timer()
|
|
271
|
+
return
|
|
272
|
+
async with self._connect_lock:
|
|
273
|
+
# Check again while holding the lock
|
|
274
|
+
if self._client and self._client.is_connected:
|
|
275
|
+
_LOGGER.debug(
|
|
276
|
+
"%s: Already connected after obtaining lock, resetting timer; RSSI: %s",
|
|
277
|
+
self.name,
|
|
278
|
+
self.rssi,
|
|
279
|
+
)
|
|
280
|
+
self._reset_disconnect_timer()
|
|
281
|
+
return
|
|
282
|
+
_LOGGER.debug("%s: Connecting; RSSI: %s", self.name, self.rssi)
|
|
283
|
+
client: BleakClientWithServiceCache = await establish_connection(
|
|
284
|
+
BleakClientWithServiceCache,
|
|
285
|
+
self.ble_device,
|
|
286
|
+
self.name,
|
|
287
|
+
self._disconnected,
|
|
288
|
+
max_attempts=10,
|
|
289
|
+
ble_device_callback=lambda: self.ble_device,
|
|
290
|
+
)
|
|
291
|
+
_LOGGER.debug("%s: Connected; RSSI: %s", self.name, self.rssi)
|
|
292
|
+
self._client = client
|
|
293
|
+
self._message = BleMessage(client)
|
|
294
|
+
|
|
295
|
+
try:
|
|
296
|
+
self._resolve_characteristics(client.services)
|
|
297
|
+
except CharacteristicMissingError as ex:
|
|
298
|
+
_LOGGER.debug(
|
|
299
|
+
"%s: characteristic missing, clearing cache: %s; RSSI: %s",
|
|
300
|
+
self.name,
|
|
301
|
+
ex,
|
|
302
|
+
self.rssi,
|
|
303
|
+
exc_info=True,
|
|
304
|
+
)
|
|
305
|
+
await client.clear_cache()
|
|
306
|
+
self._cancel_disconnect_timer()
|
|
307
|
+
await self._execute_disconnect_with_lock()
|
|
308
|
+
raise
|
|
309
|
+
|
|
310
|
+
_LOGGER.debug(
|
|
311
|
+
"%s: Starting notify and disconnect timer; RSSI: %s",
|
|
312
|
+
self.name,
|
|
313
|
+
self.rssi,
|
|
314
|
+
)
|
|
315
|
+
self._reset_disconnect_timer()
|
|
316
|
+
await self._start_notify()
|
|
317
|
+
await self._ble_sync()
|
|
318
|
+
self.schedule_ble_sync()
|
|
319
|
+
|
|
320
|
+
async def _send_command_locked(self, key: str, command: bytes) -> None:
|
|
321
|
+
"""Send command to device and read response."""
|
|
322
|
+
await self._ensure_connected()
|
|
323
|
+
try:
|
|
324
|
+
return await self._execute_command_locked(key, command)
|
|
325
|
+
except BleakDBusError as ex:
|
|
326
|
+
# Disconnect so we can reset state and try again
|
|
327
|
+
await asyncio.sleep(DBUS_ERROR_BACKOFF_TIME)
|
|
328
|
+
_LOGGER.debug(
|
|
329
|
+
"%s: RSSI: %s; Backing off %ss; Disconnecting due to error: %s",
|
|
330
|
+
self.name,
|
|
331
|
+
self.rssi,
|
|
332
|
+
DBUS_ERROR_BACKOFF_TIME,
|
|
333
|
+
ex,
|
|
334
|
+
)
|
|
335
|
+
await self._execute_forced_disconnect()
|
|
336
|
+
raise
|
|
337
|
+
except BLEAK_RETRY_EXCEPTIONS as ex:
|
|
338
|
+
# Disconnect so we can reset state and try again
|
|
339
|
+
_LOGGER.debug("%s: RSSI: %s; Disconnecting due to error: %s", self.name, self.rssi, ex)
|
|
340
|
+
await self._execute_forced_disconnect()
|
|
341
|
+
raise
|
|
342
|
+
|
|
343
|
+
async def _notification_handler(self, _sender: BleakGATTCharacteristic, data: bytes) -> None:
|
|
344
|
+
"""Handle notification responses."""
|
|
345
|
+
|
|
346
|
+
if self._message is None:
|
|
347
|
+
return
|
|
348
|
+
result = self._message.parseNotification(data)
|
|
349
|
+
if result == 0:
|
|
350
|
+
data = await self._message.parseBlufiNotifyData(True)
|
|
351
|
+
self._message.clear_notification()
|
|
352
|
+
try:
|
|
353
|
+
self._update_raw_data(data)
|
|
354
|
+
except (KeyError, ValueError, IndexError, UnicodeDecodeError):
|
|
355
|
+
_LOGGER.exception("Error parsing message %s", data)
|
|
356
|
+
data = b""
|
|
357
|
+
|
|
358
|
+
_LOGGER.debug("%s: Received notification: %s", self.name, data)
|
|
359
|
+
else:
|
|
360
|
+
return
|
|
361
|
+
|
|
362
|
+
new_msg = LubaMsg().parse(data)
|
|
363
|
+
res = betterproto2.which_one_of(new_msg, "LubaSubMsg")
|
|
364
|
+
if res[0] == "net":
|
|
365
|
+
dev_net: DevNet = cast(DevNet, res[1])
|
|
366
|
+
if dev_net.todev_ble_sync != 0 or dev_net.toapp_wifi_iot_status is not None:
|
|
367
|
+
if dev_net.toapp_wifi_iot_status is not None and self._commands.get_device_product_key() == "":
|
|
368
|
+
self._commands.set_device_product_key(dev_net.toapp_wifi_iot_status.productkey)
|
|
369
|
+
|
|
370
|
+
await self._state_manager.notification(new_msg)
|
|
371
|
+
|
|
372
|
+
self._reset_disconnect_timer()
|
|
373
|
+
|
|
374
|
+
async def _start_notify(self) -> None:
|
|
375
|
+
"""Start notification."""
|
|
376
|
+
_LOGGER.debug("%s: Subscribe to notifications; RSSI: %s", self.name, self.rssi)
|
|
377
|
+
await self._client.start_notify(self._read_char, self._notification_handler)
|
|
378
|
+
|
|
379
|
+
async def _execute_command_locked(self, key: str, command: bytes) -> None:
|
|
380
|
+
"""Execute command and read response."""
|
|
381
|
+
assert self._client is not None
|
|
382
|
+
_LOGGER.debug("%s: Sending command: %s", self.name, key)
|
|
383
|
+
await self._message.post_custom_data_bytes(command)
|
|
384
|
+
self.command_sent_time = time.time()
|
|
385
|
+
|
|
386
|
+
def get_address(self) -> str:
|
|
387
|
+
"""Return address of device."""
|
|
388
|
+
return self.ble_device.address
|
|
389
|
+
|
|
390
|
+
def _resolve_characteristics(self, services: BleakGATTServiceCollection) -> None:
|
|
391
|
+
"""Resolve characteristics."""
|
|
392
|
+
self._read_char = services.get_characteristic(READ_CHAR_UUID)
|
|
393
|
+
if not self._read_char:
|
|
394
|
+
_LOGGER.error(CharacteristicMissingError(READ_CHAR_UUID))
|
|
395
|
+
self._write_char = services.get_characteristic(WRITE_CHAR_UUID)
|
|
396
|
+
if not self._write_char:
|
|
397
|
+
_LOGGER.error(CharacteristicMissingError(WRITE_CHAR_UUID))
|
|
398
|
+
|
|
399
|
+
def _reset_disconnect_timer(self) -> None:
|
|
400
|
+
"""Reset disconnect timer."""
|
|
401
|
+
self._cancel_disconnect_timer()
|
|
402
|
+
self._expected_disconnect = False
|
|
403
|
+
self._disconnect_timer = self.loop.call_later(DISCONNECT_DELAY, self._disconnect_from_timer)
|
|
404
|
+
|
|
405
|
+
def _disconnected(self, client: BleakClientWithServiceCache) -> None:
|
|
406
|
+
"""Disconnected callback."""
|
|
407
|
+
if self._expected_disconnect:
|
|
408
|
+
_LOGGER.debug("%s: Disconnected from device; RSSI: %s", self.name, self.rssi)
|
|
409
|
+
return
|
|
410
|
+
_LOGGER.warning(
|
|
411
|
+
"%s: Device unexpectedly disconnected; RSSI: %s",
|
|
412
|
+
self.name,
|
|
413
|
+
self.rssi,
|
|
414
|
+
)
|
|
415
|
+
self._cancel_disconnect_timer()
|
|
416
|
+
self._client = None
|
|
417
|
+
|
|
418
|
+
def _disconnect_from_timer(self) -> None:
|
|
419
|
+
"""Disconnect from device."""
|
|
420
|
+
if self._operation_lock.locked() and self._client.is_connected:
|
|
421
|
+
_LOGGER.debug(
|
|
422
|
+
"%s: Operation in progress, resetting disconnect timer; RSSI: %s",
|
|
423
|
+
self.name,
|
|
424
|
+
self.rssi,
|
|
425
|
+
)
|
|
426
|
+
self._reset_disconnect_timer()
|
|
427
|
+
return
|
|
428
|
+
self._cancel_disconnect_timer()
|
|
429
|
+
self._timed_disconnect_task = asyncio.create_task(self._execute_timed_disconnect())
|
|
430
|
+
|
|
431
|
+
def _cancel_disconnect_timer(self) -> None:
|
|
432
|
+
"""Cancel disconnect timer."""
|
|
433
|
+
if self._disconnect_timer:
|
|
434
|
+
self._disconnect_timer.cancel()
|
|
435
|
+
self._disconnect_timer = None
|
|
436
|
+
|
|
437
|
+
async def _execute_forced_disconnect(self) -> None:
|
|
438
|
+
"""Execute forced disconnection."""
|
|
439
|
+
self._cancel_disconnect_timer()
|
|
440
|
+
_LOGGER.debug(
|
|
441
|
+
"%s: Executing forced disconnect",
|
|
442
|
+
self.name,
|
|
443
|
+
)
|
|
444
|
+
await self._execute_disconnect()
|
|
445
|
+
|
|
446
|
+
async def _execute_timed_disconnect(self) -> None:
|
|
447
|
+
"""Execute timed disconnection."""
|
|
448
|
+
if not self._disconnect_strategy:
|
|
449
|
+
return
|
|
450
|
+
_LOGGER.debug(
|
|
451
|
+
"%s: Executing timed disconnect after timeout of %s",
|
|
452
|
+
self.name,
|
|
453
|
+
DISCONNECT_DELAY,
|
|
454
|
+
)
|
|
455
|
+
await self._execute_disconnect()
|
|
456
|
+
|
|
457
|
+
async def _execute_disconnect(self) -> None:
|
|
458
|
+
"""Execute disconnection."""
|
|
459
|
+
_LOGGER.debug("%s: Executing disconnect", self.name)
|
|
460
|
+
async with self._connect_lock:
|
|
461
|
+
await self._execute_disconnect_with_lock()
|
|
462
|
+
|
|
463
|
+
async def _execute_disconnect_with_lock(self) -> None:
|
|
464
|
+
"""Execute disconnection while holding the lock."""
|
|
465
|
+
assert self._connect_lock.locked(), "Lock not held"
|
|
466
|
+
_LOGGER.debug("%s: Executing disconnect with lock", self.name)
|
|
467
|
+
if self._disconnect_timer: # If the timer was reset, don't disconnect
|
|
468
|
+
_LOGGER.debug("%s: Skipping disconnect as timer reset", self.name)
|
|
469
|
+
return
|
|
470
|
+
client = self._client
|
|
471
|
+
self._expected_disconnect = True
|
|
472
|
+
|
|
473
|
+
if not client:
|
|
474
|
+
_LOGGER.debug("%s: Already disconnected", self.name)
|
|
475
|
+
return
|
|
476
|
+
_LOGGER.debug("%s: Disconnecting", self.name)
|
|
477
|
+
try:
|
|
478
|
+
"""We reset what command the robot last heard before disconnecting."""
|
|
479
|
+
if client is not None and client.is_connected:
|
|
480
|
+
command_bytes = self._commands.send_todev_ble_sync(2)
|
|
481
|
+
await self._message.post_custom_data_bytes(command_bytes)
|
|
482
|
+
await client.stop_notify(self._read_char)
|
|
483
|
+
await client.disconnect()
|
|
484
|
+
except BLEAK_RETRY_EXCEPTIONS as ex:
|
|
485
|
+
_LOGGER.warning(
|
|
486
|
+
"%s: Error disconnecting: %s; RSSI: %s",
|
|
487
|
+
self.name,
|
|
488
|
+
ex,
|
|
489
|
+
self.rssi,
|
|
490
|
+
)
|
|
491
|
+
else:
|
|
492
|
+
_LOGGER.debug("%s: Disconnect completed successfully", self.name)
|
|
493
|
+
self._client = None
|
|
494
|
+
|
|
495
|
+
def set_disconnect_strategy(self, *, disconnect: bool) -> None:
|
|
496
|
+
self._disconnect_strategy = disconnect
|