This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdesk_mover.py
54 lines (39 loc) · 1.58 KB
/
desk_mover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
from time import sleep
import linak_dpg_bt.constants as constants
from threading import Timer
from .desk_position import DeskPosition
from .height_speed import HeightSpeed
MOVE_TO_HANDLE = 0x003a
REFERENCE_OUTPUT_NOTIFY_HANDLE = 0x001e # Used for desk offset / speed notifications
_LOGGER = logging.getLogger(__name__)
class DeskMover:
def __init__(self, conn, target):
self._conn = conn
self._target = target
self._running = False
self._stopTimer = Timer(30, self._stop_move)
def start(self):
_LOGGER.debug("Start move to: %d", self._target)
self._running = True
self._stopTimer.start()
with self._conn as conn:
conn.subscribe_to_notification(REFERENCE_OUTPUT_NOTIFY_HANDLE, constants.REFERENCE_OUTPUT_HANDLE,
self._handle_notification)
for _ in range(150):
if self._running:
self._send_move_to()
sleep(0.2)
def _handle_notification(self, data):
hs = HeightSpeed.from_bytes(data)
_LOGGER.debug("Current relative height: %s, speed: %f", hs.height.human_cm, hs.speed.parsed)
if hs.speed.parsed < 0.001:
self._stop_move()
def _send_move_to(self):
_LOGGER.debug("Sending move to: %d", self._target)
self._conn.make_request(MOVE_TO_HANDLE, DeskPosition.bytes_from_raw(self._target))
def _stop_move(self):
_LOGGER.debug("Move stopped")
# send stop move
self._running = False
self._stopTimer.cancel()