idasit/main.py

79 lines
2.3 KiB
Python
Raw Normal View History

2024-12-14 14:55:37 +01:00
import idasen as ida
from idasen.desk import IdasenDesk
from macparse import macaddress
import asyncio
import argparse
import sys
VERSION = "idasit 0.0.1"
def parse_args():
parser = argparse.ArgumentParser(description="CLI for controlling a desk.")
parser.add_argument('-m', '--mac', required=True, help="MAC address of the desk")
parser.add_argument('--monitor', action='store_true', help="Print height and speed while you move the desk")
parser.add_argument('--height', action='store_true', help="Print the desk height")
parser.add_argument('--move', type=float, help="Move the desk to a specific position")
parser.add_argument('--discover', action='store_true', help="Print the desk MAC")
parser.add_argument('--pair', action='store_true', help="Pair with the desk")
parser.add_argument('-v', '--version', action='version', version=VERSION)
args = parser.parse_args()
return vars(args)
# MAC = 'F8:9D:F4:10:90:DF'
# MAC = 'F8:9D:F4:10:90:D4'
SIT = 0.7793
STAND = 1.2116
async def pair_desk(MAC):
async with IdasenDesk(MAC, exit_on_fail=True) as desk:
await desk.pair()
print('paired')
async def discover_desk():
mac = await IdasenDesk.discover()
print(mac)
async def monitor(MAC) -> None:
try:
async with IdasenDesk(MAC, exit_on_fail=True) as desk:
async def printer(height: float, speed: float):
print(f"{height:.3f} meters - {speed:.3f} meters/second", flush=True)
await desk.monitor(printer)
while True:
await asyncio.sleep(1000000)
except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
pass
async def height(MAC):
async with IdasenDesk(MAC, exit_on_fail=True) as desk:
height = await desk.get_height()
print(f"{height:.3f} meters")
async def move_to(MAC, position: float) -> None:
async with IdasenDesk(MAC, exit_on_fail=True) as desk:
await desk.move_to_target(target=position)
def validate(mac):
try:
macaddress.MAC(mac)
return True
except:
return False
if __name__ == '__main__':
args = parse_args()
mac = args['mac']
if not validate(mac):
print(f'Not a valid MAC address. Use --discover to find the correct MAC address.')
sys.exit(2)
asyncio.run(discover_desk())