pymammotion 0.5.34__py3-none-any.whl → 0.5.44__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.
Potentially problematic release.
This version of pymammotion might be problematic. Click here for more details.
- pymammotion/__init__.py +3 -3
- pymammotion/aliyun/cloud_gateway.py +106 -18
- pymammotion/aliyun/model/dev_by_account_response.py +198 -20
- pymammotion/const.py +3 -0
- pymammotion/data/model/device.py +1 -0
- pymammotion/data/model/device_config.py +1 -1
- pymammotion/data/model/enums.py +5 -3
- pymammotion/data/model/generate_route_information.py +2 -2
- pymammotion/data/model/hash_list.py +113 -33
- pymammotion/data/model/region_data.py +4 -4
- pymammotion/data/{state_manager.py → mower_state_manager.py} +17 -7
- pymammotion/data/mqtt/event.py +47 -22
- pymammotion/data/mqtt/mammotion_properties.py +257 -0
- pymammotion/data/mqtt/properties.py +32 -29
- pymammotion/data/mqtt/status.py +17 -16
- pymammotion/homeassistant/__init__.py +3 -0
- pymammotion/homeassistant/mower_api.py +446 -0
- pymammotion/homeassistant/rtk_api.py +54 -0
- pymammotion/http/http.py +387 -13
- pymammotion/http/model/http.py +82 -2
- pymammotion/http/model/response_factory.py +10 -4
- pymammotion/mammotion/commands/mammotion_command.py +6 -0
- pymammotion/mammotion/commands/messages/navigation.py +10 -6
- pymammotion/mammotion/devices/__init__.py +27 -3
- pymammotion/mammotion/devices/base.py +16 -138
- pymammotion/mammotion/devices/mammotion.py +364 -204
- pymammotion/mammotion/devices/mammotion_bluetooth.py +7 -5
- pymammotion/mammotion/devices/mammotion_cloud.py +42 -83
- pymammotion/mammotion/devices/mammotion_mower_ble.py +49 -0
- pymammotion/mammotion/devices/mammotion_mower_cloud.py +39 -0
- pymammotion/mammotion/devices/managers/managers.py +81 -0
- pymammotion/mammotion/devices/mower_device.py +121 -0
- pymammotion/mammotion/devices/mower_manager.py +107 -0
- pymammotion/mammotion/devices/rtk_ble.py +89 -0
- pymammotion/mammotion/devices/rtk_cloud.py +113 -0
- pymammotion/mammotion/devices/rtk_device.py +50 -0
- pymammotion/mammotion/devices/rtk_manager.py +122 -0
- pymammotion/mqtt/__init__.py +2 -1
- pymammotion/mqtt/aliyun_mqtt.py +232 -0
- pymammotion/mqtt/mammotion_mqtt.py +174 -192
- pymammotion/mqtt/mqtt_models.py +66 -0
- pymammotion/proto/__init__.py +1 -1
- pymammotion/proto/mctrl_nav.proto +1 -1
- pymammotion/proto/mctrl_nav_pb2.py +1 -1
- pymammotion/proto/mctrl_nav_pb2.pyi +4 -4
- pymammotion/proto/mctrl_sys.proto +1 -1
- pymammotion/utility/datatype_converter.py +13 -12
- pymammotion/utility/device_type.py +88 -3
- pymammotion/utility/mur_mur_hash.py +132 -87
- {pymammotion-0.5.34.dist-info → pymammotion-0.5.44.dist-info}/METADATA +25 -31
- {pymammotion-0.5.34.dist-info → pymammotion-0.5.44.dist-info}/RECORD +59 -45
- {pymammotion-0.5.34.dist-info → pymammotion-0.5.44.dist-info}/WHEEL +1 -1
- pymammotion/http/_init_.py +0 -0
- {pymammotion-0.5.34.dist-info → pymammotion-0.5.44.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from bleak import BLEDevice
|
|
4
|
+
|
|
5
|
+
from pymammotion import CloudIOTGateway
|
|
6
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
7
|
+
from pymammotion.data.model.device import MowingDevice
|
|
8
|
+
from pymammotion.data.model.enums import ConnectionPreference
|
|
9
|
+
from pymammotion.data.mower_state_manager import MowerStateManager
|
|
10
|
+
from pymammotion.mammotion.devices.mammotion_cloud import MammotionCloud
|
|
11
|
+
from pymammotion.mammotion.devices.mammotion_mower_ble import MammotionMowerBLEDevice
|
|
12
|
+
from pymammotion.mammotion.devices.mammotion_mower_cloud import MammotionMowerCloudDevice
|
|
13
|
+
from pymammotion.mammotion.devices.managers.managers import AbstractDeviceManager
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MammotionMowerDeviceManager(AbstractDeviceManager):
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
name: str,
|
|
20
|
+
iot_id: str,
|
|
21
|
+
cloud_client: CloudIOTGateway,
|
|
22
|
+
cloud_device: Device,
|
|
23
|
+
ble_device: BLEDevice | None = None,
|
|
24
|
+
mqtt: MammotionCloud | None = None,
|
|
25
|
+
preference: ConnectionPreference = ConnectionPreference.BLUETOOTH,
|
|
26
|
+
) -> None:
|
|
27
|
+
super().__init__(name, iot_id, cloud_client, cloud_device, preference)
|
|
28
|
+
self._ble_device: MammotionMowerBLEDevice | None = None
|
|
29
|
+
self._cloud_device: MammotionMowerCloudDevice | None = None
|
|
30
|
+
|
|
31
|
+
self._state_manager = MowerStateManager(MowingDevice())
|
|
32
|
+
self._state_manager.get_device().name = name
|
|
33
|
+
self.add_ble(ble_device) if ble_device else None
|
|
34
|
+
self.add_cloud(mqtt) if mqtt else None
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def state_manager(self) -> MowerStateManager:
|
|
38
|
+
"""Return the state manager."""
|
|
39
|
+
return self._state_manager
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def state(self) -> MowingDevice:
|
|
43
|
+
"""Return the state of the device."""
|
|
44
|
+
return self._state_manager.get_device()
|
|
45
|
+
|
|
46
|
+
@state.setter
|
|
47
|
+
def state(self, value: MowingDevice) -> None:
|
|
48
|
+
self._state_manager.set_device(value)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def ble(self) -> MammotionMowerBLEDevice | None:
|
|
52
|
+
return self._ble_device
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def cloud(self) -> MammotionMowerCloudDevice | None:
|
|
56
|
+
return self._cloud_device
|
|
57
|
+
|
|
58
|
+
def has_queued_commands(self) -> bool:
|
|
59
|
+
if self.cloud and self.preference == ConnectionPreference.WIFI:
|
|
60
|
+
return not self.cloud.mqtt.command_queue.empty()
|
|
61
|
+
elif self.ble:
|
|
62
|
+
return not self.ble.command_queue.empty()
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
def add_ble(self, ble_device: BLEDevice) -> MammotionMowerBLEDevice:
|
|
66
|
+
self._ble_device = MammotionMowerBLEDevice(
|
|
67
|
+
state_manager=self._state_manager, cloud_device=self._device, device=ble_device
|
|
68
|
+
)
|
|
69
|
+
return self._ble_device
|
|
70
|
+
|
|
71
|
+
def add_cloud(self, mqtt: MammotionCloud) -> MammotionMowerCloudDevice:
|
|
72
|
+
self._cloud_device = MammotionMowerCloudDevice(
|
|
73
|
+
mqtt, cloud_device=self._device, state_manager=self._state_manager
|
|
74
|
+
)
|
|
75
|
+
return self._cloud_device
|
|
76
|
+
|
|
77
|
+
def replace_cloud(self, cloud_device: MammotionMowerCloudDevice) -> None:
|
|
78
|
+
self._cloud_device = cloud_device
|
|
79
|
+
|
|
80
|
+
def remove_cloud(self) -> None:
|
|
81
|
+
self._state_manager.cloud_get_commondata_ack_callback = None
|
|
82
|
+
self._state_manager.cloud_get_hashlist_ack_callback = None
|
|
83
|
+
self._state_manager.cloud_get_plan_callback = None
|
|
84
|
+
self._state_manager.cloud_on_notification_callback = None
|
|
85
|
+
self._state_manager.cloud_gethash_ack_callback = None
|
|
86
|
+
self._cloud_device = None
|
|
87
|
+
|
|
88
|
+
def replace_ble(self, ble_device: MammotionMowerBLEDevice) -> None:
|
|
89
|
+
self._ble_device = ble_device
|
|
90
|
+
|
|
91
|
+
def remove_ble(self) -> None:
|
|
92
|
+
self._state_manager.ble_get_commondata_ack_callback = None
|
|
93
|
+
self._state_manager.ble_get_hashlist_ack_callback = None
|
|
94
|
+
self._state_manager.ble_get_plan_callback = None
|
|
95
|
+
self._state_manager.ble_on_notification_callback = None
|
|
96
|
+
self._state_manager.ble_gethash_ack_callback = None
|
|
97
|
+
self._ble_device = None
|
|
98
|
+
|
|
99
|
+
def replace_mqtt(self, mqtt: MammotionCloud) -> None:
|
|
100
|
+
device = self._cloud_device.device
|
|
101
|
+
self._cloud_device = MammotionMowerCloudDevice(mqtt, cloud_device=device, state_manager=self._state_manager)
|
|
102
|
+
|
|
103
|
+
def has_cloud(self) -> bool:
|
|
104
|
+
return self._cloud_device is not None
|
|
105
|
+
|
|
106
|
+
def has_ble(self) -> bool:
|
|
107
|
+
return self._ble_device is not None
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""RTK device with Bluetooth LE connectivity."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Any
|
|
6
|
+
from uuid import UUID
|
|
7
|
+
|
|
8
|
+
from bleak import BleakGATTCharacteristic, BLEDevice
|
|
9
|
+
from bleak_retry_connector import BleakClientWithServiceCache
|
|
10
|
+
|
|
11
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
12
|
+
from pymammotion.bluetooth import BleMessage
|
|
13
|
+
from pymammotion.data.model.device import RTKDevice
|
|
14
|
+
from pymammotion.mammotion.commands.mammotion_command import MammotionCommand
|
|
15
|
+
from pymammotion.mammotion.devices.rtk_device import MammotionRTKDevice
|
|
16
|
+
|
|
17
|
+
_LOGGER = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MammotionRTKBLEDevice(MammotionRTKDevice):
|
|
21
|
+
"""RTK device with BLE connectivity - simpler than mowers, no map sync."""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self, cloud_device: Device, rtk_state: RTKDevice, device: BLEDevice, interface: int = 0, **kwargs: Any
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Initialize MammotionRTKBLEDevice."""
|
|
27
|
+
super().__init__(cloud_device, rtk_state)
|
|
28
|
+
self.command_sent_time = 0
|
|
29
|
+
self._disconnect_strategy = True
|
|
30
|
+
self._interface = f"hci{interface}"
|
|
31
|
+
self.ble_device = device
|
|
32
|
+
self._client: BleakClientWithServiceCache | None = None
|
|
33
|
+
self._read_char: BleakGATTCharacteristic | int | str | UUID = 0
|
|
34
|
+
self._write_char: BleakGATTCharacteristic | int | str | UUID = 0
|
|
35
|
+
self._disconnect_timer: asyncio.TimerHandle | None = None
|
|
36
|
+
self._message: BleMessage | None = None
|
|
37
|
+
self._commands: MammotionCommand = MammotionCommand(device.name or "", 1)
|
|
38
|
+
self.command_queue = asyncio.Queue()
|
|
39
|
+
self._expected_disconnect = False
|
|
40
|
+
self._connect_lock = asyncio.Lock()
|
|
41
|
+
self._operation_lock = asyncio.Lock()
|
|
42
|
+
self._key: str | None = None
|
|
43
|
+
loop = asyncio.get_event_loop()
|
|
44
|
+
loop.create_task(self.process_queue())
|
|
45
|
+
|
|
46
|
+
def __del__(self) -> None:
|
|
47
|
+
"""Cleanup."""
|
|
48
|
+
if self._disconnect_timer:
|
|
49
|
+
self._disconnect_timer.cancel()
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def client(self) -> BleakClientWithServiceCache:
|
|
53
|
+
"""Return the BLE client."""
|
|
54
|
+
return self._client
|
|
55
|
+
|
|
56
|
+
def set_disconnect_strategy(self, *, disconnect: bool) -> None:
|
|
57
|
+
"""Set disconnect strategy."""
|
|
58
|
+
self._disconnect_strategy = disconnect
|
|
59
|
+
|
|
60
|
+
async def process_queue(self) -> None:
|
|
61
|
+
"""Process queued commands - simplified for RTK."""
|
|
62
|
+
while True:
|
|
63
|
+
key, kwargs = await self.command_queue.get()
|
|
64
|
+
try:
|
|
65
|
+
_LOGGER.debug("Processing RTK BLE command: %s", key)
|
|
66
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
|
67
|
+
# Send command via BLE (implementation depends on BLE infrastructure)
|
|
68
|
+
# For now, this is a placeholder
|
|
69
|
+
_LOGGER.debug("RTK BLE command sent: %s", key)
|
|
70
|
+
except Exception as ex:
|
|
71
|
+
_LOGGER.exception("Error processing RTK BLE command: %s", ex)
|
|
72
|
+
finally:
|
|
73
|
+
self.command_queue.task_done()
|
|
74
|
+
|
|
75
|
+
async def queue_command(self, key: str, **kwargs: Any) -> None:
|
|
76
|
+
"""Queue a command to the RTK device."""
|
|
77
|
+
await self.command_queue.put((key, kwargs))
|
|
78
|
+
|
|
79
|
+
async def command(self, key: str, **kwargs):
|
|
80
|
+
"""Send a command to the RTK device."""
|
|
81
|
+
return await self.queue_command(key, **kwargs)
|
|
82
|
+
|
|
83
|
+
async def _ble_sync(self) -> None:
|
|
84
|
+
"""RTK devices don't use BLE sync in the same way as mowers."""
|
|
85
|
+
|
|
86
|
+
async def stop(self) -> None:
|
|
87
|
+
"""Stop everything ready for destroying."""
|
|
88
|
+
if self._client is not None and self._client.is_connected:
|
|
89
|
+
await self._client.disconnect()
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""RTK device with cloud MQTT connectivity."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from collections.abc import Awaitable, Callable
|
|
5
|
+
import logging
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
9
|
+
from pymammotion.data.model.device import RTKDevice
|
|
10
|
+
from pymammotion.data.mqtt.properties import ThingPropertiesMessage
|
|
11
|
+
from pymammotion.data.mqtt.status import ThingStatusMessage
|
|
12
|
+
from pymammotion.mammotion.commands.mammotion_command import MammotionCommand
|
|
13
|
+
from pymammotion.mammotion.devices.mammotion_cloud import MammotionCloud
|
|
14
|
+
from pymammotion.mammotion.devices.rtk_device import MammotionRTKDevice
|
|
15
|
+
|
|
16
|
+
_LOGGER = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MammotionRTKCloudDevice(MammotionRTKDevice):
|
|
20
|
+
"""RTK device with cloud connectivity - simpler than mowers, no map sync."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, mqtt: MammotionCloud, cloud_device: Device, rtk_state: RTKDevice) -> None:
|
|
23
|
+
"""Initialize MammotionRTKCloudDevice."""
|
|
24
|
+
super().__init__(cloud_device, rtk_state)
|
|
25
|
+
self.stopped = False
|
|
26
|
+
self.on_ready_callback: Callable[[], Awaitable[None]] | None = None
|
|
27
|
+
self.loop = asyncio.get_event_loop()
|
|
28
|
+
self._mqtt = mqtt
|
|
29
|
+
self.iot_id = cloud_device.iot_id
|
|
30
|
+
self.device = cloud_device
|
|
31
|
+
self._commands: MammotionCommand = MammotionCommand(
|
|
32
|
+
cloud_device.device_name,
|
|
33
|
+
int(mqtt.cloud_client.mammotion_http.response.data.userInformation.userAccount),
|
|
34
|
+
)
|
|
35
|
+
# Subscribe to MQTT events for this device
|
|
36
|
+
self._mqtt.mqtt_properties_event.add_subscribers(self._parse_message_properties_for_device)
|
|
37
|
+
self._mqtt.mqtt_status_event.add_subscribers(self._parse_message_status_for_device)
|
|
38
|
+
self._mqtt.on_ready_event.add_subscribers(self.on_ready)
|
|
39
|
+
self._mqtt.on_disconnected_event.add_subscribers(self.on_disconnect)
|
|
40
|
+
self._mqtt.on_connected_event.add_subscribers(self.on_connect)
|
|
41
|
+
|
|
42
|
+
def __del__(self) -> None:
|
|
43
|
+
"""Cleanup subscriptions."""
|
|
44
|
+
if hasattr(self, "_mqtt"):
|
|
45
|
+
self._mqtt.on_ready_event.remove_subscribers(self.on_ready)
|
|
46
|
+
self._mqtt.on_disconnected_event.remove_subscribers(self.on_disconnect)
|
|
47
|
+
self._mqtt.on_connected_event.remove_subscribers(self.on_connect)
|
|
48
|
+
self._mqtt.mqtt_properties_event.remove_subscribers(self._parse_message_properties_for_device)
|
|
49
|
+
self._mqtt.mqtt_status_event.remove_subscribers(self._parse_message_status_for_device)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def command_sent_time(self) -> float:
|
|
53
|
+
return self._mqtt.command_sent_time
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def mqtt(self):
|
|
57
|
+
return self._mqtt
|
|
58
|
+
|
|
59
|
+
async def on_ready(self) -> None:
|
|
60
|
+
"""Callback for when MQTT is subscribed to events."""
|
|
61
|
+
if self.stopped:
|
|
62
|
+
return
|
|
63
|
+
if self.on_ready_callback:
|
|
64
|
+
await self.on_ready_callback()
|
|
65
|
+
|
|
66
|
+
async def on_disconnect(self) -> None:
|
|
67
|
+
"""Callback for when MQTT disconnects."""
|
|
68
|
+
self._mqtt.disconnect()
|
|
69
|
+
|
|
70
|
+
async def on_connect(self) -> None:
|
|
71
|
+
"""Callback for when MQTT connects."""
|
|
72
|
+
|
|
73
|
+
async def stop(self) -> None:
|
|
74
|
+
"""Stop all tasks and disconnect."""
|
|
75
|
+
self.stopped = True
|
|
76
|
+
|
|
77
|
+
async def start(self) -> None:
|
|
78
|
+
"""Start the device connection."""
|
|
79
|
+
self.stopped = False
|
|
80
|
+
if not self.mqtt.is_connected():
|
|
81
|
+
loop = asyncio.get_running_loop()
|
|
82
|
+
await loop.run_in_executor(None, self.mqtt.connect_async)
|
|
83
|
+
|
|
84
|
+
async def queue_command(self, key: str, **kwargs: Any) -> None:
|
|
85
|
+
"""Queue a command to the RTK device."""
|
|
86
|
+
_LOGGER.debug("Queueing command: %s", key)
|
|
87
|
+
future = asyncio.Future()
|
|
88
|
+
command_bytes = getattr(self._commands, key)(**kwargs)
|
|
89
|
+
await self._mqtt.command_queue.put((self.iot_id, key, command_bytes, future))
|
|
90
|
+
try:
|
|
91
|
+
return await future
|
|
92
|
+
except asyncio.CancelledError:
|
|
93
|
+
"""Try again once."""
|
|
94
|
+
future = asyncio.Future()
|
|
95
|
+
await self._mqtt.command_queue.put((self.iot_id, key, command_bytes, future))
|
|
96
|
+
|
|
97
|
+
async def _parse_message_properties_for_device(self, event: ThingPropertiesMessage) -> None:
|
|
98
|
+
"""Parse property messages for this RTK device."""
|
|
99
|
+
if event.params.iot_id != self.iot_id:
|
|
100
|
+
return
|
|
101
|
+
# RTK devices have simpler properties - update as needed
|
|
102
|
+
_LOGGER.debug("RTK properties update: %s", event)
|
|
103
|
+
|
|
104
|
+
async def _parse_message_status_for_device(self, status: ThingStatusMessage) -> None:
|
|
105
|
+
"""Parse status messages for this RTK device."""
|
|
106
|
+
if status.params.iot_id != self.iot_id:
|
|
107
|
+
return
|
|
108
|
+
# Update online status
|
|
109
|
+
self._rtk_device.online = True
|
|
110
|
+
_LOGGER.debug("RTK status update: %s", status)
|
|
111
|
+
|
|
112
|
+
async def _ble_sync(self) -> None:
|
|
113
|
+
"""RTK devices don't use BLE sync in the same way as mowers."""
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""RTK device class without map synchronization callbacks."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
8
|
+
from pymammotion.data.model.device import RTKDevice
|
|
9
|
+
from pymammotion.data.model.raw_data import RawMowerData
|
|
10
|
+
|
|
11
|
+
_LOGGER = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MammotionRTKDevice:
|
|
15
|
+
"""RTK device without map synchronization - simpler than mowers."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, cloud_device: Device, rtk_state: RTKDevice) -> None:
|
|
18
|
+
"""Initialize MammotionRTKDevice."""
|
|
19
|
+
self.loop = asyncio.get_event_loop()
|
|
20
|
+
self._rtk_device = rtk_state
|
|
21
|
+
self._raw_data = dict()
|
|
22
|
+
self._raw_mower_data: RawMowerData = RawMowerData()
|
|
23
|
+
self._notify_future: asyncio.Future[bytes] | None = None
|
|
24
|
+
self._cloud_device = cloud_device
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def rtk(self) -> RTKDevice:
|
|
28
|
+
"""Get the RTK device state."""
|
|
29
|
+
return self._rtk_device
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def raw_data(self) -> dict[str, Any]:
|
|
33
|
+
"""Get the raw data of the device."""
|
|
34
|
+
return self._raw_data
|
|
35
|
+
|
|
36
|
+
async def command(self, key: str, **kwargs: Any) -> bytes | None:
|
|
37
|
+
"""Send a command to the device."""
|
|
38
|
+
return await self.queue_command(key, **kwargs)
|
|
39
|
+
|
|
40
|
+
async def queue_command(self, key: str, **kwargs: Any) -> bytes | None:
|
|
41
|
+
"""Queue commands to RTK device - to be implemented by connection-specific subclasses."""
|
|
42
|
+
raise NotImplementedError("Subclasses must implement queue_command")
|
|
43
|
+
|
|
44
|
+
async def _ble_sync(self) -> None:
|
|
45
|
+
"""Send ble sync command - to be implemented by connection-specific subclasses."""
|
|
46
|
+
raise NotImplementedError("Subclasses must implement _ble_sync")
|
|
47
|
+
|
|
48
|
+
def stop(self) -> None:
|
|
49
|
+
"""Stop everything ready for destroying - to be implemented by connection-specific subclasses."""
|
|
50
|
+
raise NotImplementedError("Subclasses must implement stop")
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""RTK Device Manager - manages RTK devices with cloud and BLE connectivity."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
from bleak import BLEDevice
|
|
6
|
+
|
|
7
|
+
from pymammotion.aliyun.cloud_gateway import CloudIOTGateway
|
|
8
|
+
from pymammotion.aliyun.model.dev_by_account_response import Device
|
|
9
|
+
from pymammotion.data.model.device import RTKDevice
|
|
10
|
+
from pymammotion.data.model.enums import ConnectionPreference
|
|
11
|
+
from pymammotion.mammotion.devices.mammotion_cloud import MammotionCloud
|
|
12
|
+
from pymammotion.mammotion.devices.managers.managers import AbstractDeviceManager
|
|
13
|
+
from pymammotion.mammotion.devices.rtk_ble import MammotionRTKBLEDevice
|
|
14
|
+
from pymammotion.mammotion.devices.rtk_cloud import MammotionRTKCloudDevice
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class MammotionRTKDeviceManager(AbstractDeviceManager):
|
|
18
|
+
"""Manages an RTK device with both cloud and BLE connectivity options."""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
name: str,
|
|
23
|
+
iot_id: str,
|
|
24
|
+
cloud_client: CloudIOTGateway,
|
|
25
|
+
cloud_device: Device,
|
|
26
|
+
ble_device: BLEDevice | None = None,
|
|
27
|
+
mqtt: MammotionCloud | None = None,
|
|
28
|
+
preference: ConnectionPreference = ConnectionPreference.WIFI,
|
|
29
|
+
) -> None:
|
|
30
|
+
"""Initialize RTK device manager."""
|
|
31
|
+
super().__init__(name, iot_id, cloud_client, cloud_device, preference)
|
|
32
|
+
self._ble_device: MammotionRTKBLEDevice | None = None
|
|
33
|
+
self._cloud_device: MammotionRTKCloudDevice | None = None
|
|
34
|
+
self.name = name
|
|
35
|
+
self.iot_id = iot_id
|
|
36
|
+
self.cloud_client = cloud_client
|
|
37
|
+
self._device: Device = cloud_device
|
|
38
|
+
self.mammotion_http = cloud_client.mammotion_http
|
|
39
|
+
self.preference = preference
|
|
40
|
+
|
|
41
|
+
# Initialize RTK state
|
|
42
|
+
self._rtk_state = RTKDevice(
|
|
43
|
+
name=name,
|
|
44
|
+
iot_id=iot_id,
|
|
45
|
+
product_key=cloud_device.product_key,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# Add connection types if provided
|
|
49
|
+
if ble_device:
|
|
50
|
+
self.add_ble(ble_device)
|
|
51
|
+
if mqtt:
|
|
52
|
+
self.add_cloud(mqtt)
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def state(self) -> RTKDevice:
|
|
56
|
+
"""Return the RTK device state."""
|
|
57
|
+
return self._rtk_state
|
|
58
|
+
|
|
59
|
+
@state.setter
|
|
60
|
+
def state(self, value: RTKDevice) -> None:
|
|
61
|
+
"""Set the RTK device state."""
|
|
62
|
+
self._rtk_state = value
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def ble(self) -> MammotionRTKBLEDevice | None:
|
|
66
|
+
"""Return BLE device interface."""
|
|
67
|
+
return self._ble_device
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def cloud(self) -> MammotionRTKCloudDevice | None:
|
|
71
|
+
"""Return cloud device interface."""
|
|
72
|
+
return self._cloud_device
|
|
73
|
+
|
|
74
|
+
def has_queued_commands(self) -> bool:
|
|
75
|
+
"""Check if there are queued commands."""
|
|
76
|
+
if self.cloud and self.preference == ConnectionPreference.WIFI:
|
|
77
|
+
return not self.cloud.mqtt.command_queue.empty()
|
|
78
|
+
elif self.ble:
|
|
79
|
+
return not self.ble.command_queue.empty()
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
def add_ble(self, ble_device: BLEDevice) -> MammotionRTKBLEDevice:
|
|
83
|
+
"""Add BLE device."""
|
|
84
|
+
self._ble_device = MammotionRTKBLEDevice(
|
|
85
|
+
cloud_device=self._device, rtk_state=self._rtk_state, device=ble_device
|
|
86
|
+
)
|
|
87
|
+
return self._ble_device
|
|
88
|
+
|
|
89
|
+
@override
|
|
90
|
+
def add_cloud(self, mqtt: MammotionCloud) -> MammotionRTKCloudDevice:
|
|
91
|
+
"""Add cloud device."""
|
|
92
|
+
self._cloud_device = MammotionRTKCloudDevice(mqtt, cloud_device=self._device, rtk_state=self._rtk_state)
|
|
93
|
+
return self._cloud_device
|
|
94
|
+
|
|
95
|
+
def replace_cloud(self, cloud_device: MammotionRTKCloudDevice) -> None:
|
|
96
|
+
"""Replace cloud device."""
|
|
97
|
+
self._cloud_device = cloud_device
|
|
98
|
+
|
|
99
|
+
def remove_cloud(self) -> None:
|
|
100
|
+
"""Remove cloud device."""
|
|
101
|
+
self._cloud_device = None
|
|
102
|
+
|
|
103
|
+
def replace_ble(self, ble_device: MammotionRTKBLEDevice) -> None:
|
|
104
|
+
"""Replace BLE device."""
|
|
105
|
+
self._ble_device = ble_device
|
|
106
|
+
|
|
107
|
+
def remove_ble(self) -> None:
|
|
108
|
+
"""Remove BLE device."""
|
|
109
|
+
self._ble_device = None
|
|
110
|
+
|
|
111
|
+
def replace_mqtt(self, mqtt: MammotionCloud) -> None:
|
|
112
|
+
"""Replace MQTT connection."""
|
|
113
|
+
device = self._cloud_device.device
|
|
114
|
+
self._cloud_device = MammotionRTKCloudDevice(mqtt, cloud_device=device, rtk_state=self._rtk_state)
|
|
115
|
+
|
|
116
|
+
def has_cloud(self) -> bool:
|
|
117
|
+
"""Check if cloud connection is available."""
|
|
118
|
+
return self._cloud_device is not None
|
|
119
|
+
|
|
120
|
+
def has_ble(self) -> bool:
|
|
121
|
+
"""Check if BLE connection is available."""
|
|
122
|
+
return self._ble_device is not None
|