From 46e840cb0a6e26641e78a170f92f9b11f8ac17d7 Mon Sep 17 00:00:00 2001 From: Kamil Lewandowski Date: Sun, 29 Oct 2023 13:47:26 +0100 Subject: [PATCH 1/2] Removed user_id from config There is no need to store user_id in the configuration file because we can read from controller and modify only one byte. Storing in a configuration file may also cause some issues when someone incorrectly set the byte responsible for the ability to move the desk. I also noticed that BASE_HEIGHT offset is different for my desk so I added a read from the controller to be fully synchronized. --- README.md | 1 - idasen_controller/config.py | 7 ------ idasen_controller/desk.py | 34 +++++++++++++++++---------- idasen_controller/example/config.yaml | 1 - idasen_controller/gatt.py | 7 +++++- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 7fc912a..db7467e 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,6 @@ Config options: | Option | Description | Default | | -------------------- | -------------------------------------------------------------------------------------------- | ---------------------------------------------------- | | `mac_address` | The MAC address (or UUID on MacOS) of the desk. This is required. | | -| `user_id` | The user ID (in hex) to use when connecting to the desk. This should not need to be changed. | `01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11` | | `base_height` | The lowest possible height (mm) of the desk top from the floor. | `620`. | | `movement_range` | How far above base height the desk can extend (mm). | `650`. | | `adapter_name` | The adapter name for the bluetooth adapter to use for the connection (Linux only). | `hci0` | diff --git a/idasen_controller/config.py b/idasen_controller/config.py index f2bb08f..10b995f 100755 --- a/idasen_controller/config.py +++ b/idasen_controller/config.py @@ -28,7 +28,6 @@ class Commands(str, Enum): class Config: # Config mac_address: Optional[str] = None - user_id: str = "01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11" base_height: int = DEFAULT_BASE_HEIGHT max_height: int = DEFAULT_BASE_HEIGHT + DEFAULT_MOVEMENT_RANGE movement_range: int = DEFAULT_MOVEMENT_RANGE @@ -70,12 +69,6 @@ def __init__(self): type=str, help="Mac address of the Linak desk", ) - parser.add_argument( - "--user-id", - dest="user_id", - type=str, - help="User ID to use when connecting to the desk", - ) parser.add_argument( "--base-height", dest="base_height", diff --git a/idasen_controller/desk.py b/idasen_controller/desk.py index aa395ff..d2e9f73 100755 --- a/idasen_controller/desk.py +++ b/idasen_controller/desk.py @@ -14,23 +14,34 @@ ) from .config import config from .util import bytes_to_hex, Height, Speed +import struct class Desk: @classmethod async def initialise(cls, client: BleakClient) -> None: - # Read the user id - + # Read capabilities capabilities = cls.decode_capabilities(await DPGService.dpg_command(client, DPGService.DPG.CMD_GET_CAPABILITIES)) print("Capabilities: {}".format(capabilities)) - - user_id = (await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID))[2:] - user_id_hex = bytes_to_hex(user_id) - print("User ID: {}".format(user_id_hex)) - if user_id_hex != config.user_id: - print("Setting user ID to".format(config.user_id)) - await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID, bytearray.fromhex(config.user_id)) - + + # Read the user id + user_id = (await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID)) + print("User ID: {}".format(bytes_to_hex(user_id))) + if user_id and user_id[0] != 1: + # Set user_id to more privileged + user_id[0] = 1 + print("Setting user ID to {}".format(bytes_to_hex(user_id))) + #await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID, user_id) + + # Check if base height should be taken from controller + if config.base_height == 0: + resp = (await DPGService.dpg_command(client, DPGService.DPG.CMD_BASE_OFFSET)) + if resp: + base_height = struct.unpack(" None: pass @classmethod - def decode_capabilities(cls, data: bytearray) -> dict: - caps = data[2:] + def decode_capabilities(cls, caps: bytearray) -> dict: if len(caps) < 2: return {} capByte = caps[0] diff --git a/idasen_controller/example/config.yaml b/idasen_controller/example/config.yaml index d0a8359..c3ba6dd 100644 --- a/idasen_controller/example/config.yaml +++ b/idasen_controller/example/config.yaml @@ -1,5 +1,4 @@ mac_address: AA:AA:AA:AA:AA:AA -user_id: "01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11" scan_timeout: 5 connection_timeout: 10 movement_timeout: 30 diff --git a/idasen_controller/gatt.py b/idasen_controller/gatt.py index 5988ad9..66adda2 100755 --- a/idasen_controller/gatt.py +++ b/idasen_controller/gatt.py @@ -159,6 +159,7 @@ class DPGDPGCharacteristic(Characteristic): uuid = "99fa0011-338a-1024-8a49-009c0215f78a" CMD_GET_CAPABILITIES = 128 + CMD_BASE_OFFSET = 129 CMD_USER_ID = 134 @classmethod @@ -202,4 +203,8 @@ async def dpg_command( await cls.DPG.read_command(client, command) async for sender, data in iter: # Return the first response from the callback - return data + if data[0] == 1: + return data[2:] + else: + return None + From 6f8781493bbfcb6cf1173ab25e8280b1e75e0b2b Mon Sep 17 00:00:00 2001 From: Kamil Lewandowski Date: Mon, 30 Oct 2023 21:06:26 +0100 Subject: [PATCH 2/2] Restore setting new user_id and fixed move_to Tested without server --- idasen_controller/desk.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/idasen_controller/desk.py b/idasen_controller/desk.py index d2e9f73..db45e96 100755 --- a/idasen_controller/desk.py +++ b/idasen_controller/desk.py @@ -31,7 +31,7 @@ async def initialise(cls, client: BleakClient) -> None: # Set user_id to more privileged user_id[0] = 1 print("Setting user ID to {}".format(bytes_to_hex(user_id))) - #await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID, user_id) + await DPGService.dpg_command(client, DPGService.DPG.CMD_USER_ID, user_id) # Check if base height should be taken from controller if config.base_height == 0: @@ -55,8 +55,8 @@ async def move_to(cls, client: BleakClient, target: Height) -> None: if initial_height.value == target.value: return - await ControlService.wakeup(client) - await ControlService.stop(client) + await cls.wakeup(client) + await cls.stop(client) data = ReferenceInputService.encode_height(target.value)