main.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. from aiocmd.aiocmd import PromptToolkitCmd
  3. from openfreebuds import IOpenFreebuds
  4. from openfreebuds_cmd.utils import to_fixed
  5. class OpenFreebudsCmd(PromptToolkitCmd):
  6. manager: IOpenFreebuds = None
  7. def __init__(self):
  8. super().__init__(ignore_sigint=False)
  9. async def do_connect(self, device_name, device_addr):
  10. await self.manager.start(device_name, device_addr)
  11. async def do_listen(self):
  12. print("Listening for incoming events, use Ctrl-C to break...")
  13. sub_id = await self.manager.subscribe()
  14. try:
  15. while True:
  16. event = await self.manager.wait_for_event(sub_id)
  17. print(event)
  18. except KeyboardInterrupt:
  19. pass
  20. finally:
  21. await self.manager.unsubscribe(sub_id)
  22. async def do_set(self, group, prop, value):
  23. await self.manager.set_property(group, prop, value)
  24. async def do_core_health(self):
  25. report = await self.manager.get_health_report()
  26. print(json.dumps(report, indent=4, ensure_ascii=False))
  27. async def do_status(self):
  28. state = await self.manager.get_state()
  29. print("State:", state)
  30. if state != IOpenFreebuds.STATE_CONNECTED:
  31. return
  32. store = await self.manager.get_property(None, None)
  33. for group in store:
  34. print(group)
  35. for prop in store[group]:
  36. if prop.endswith("_options") or prop == "supported_languages":
  37. print(" ", prop)
  38. for opt in store[group][prop].split(","):
  39. print(" - ", opt)
  40. elif group == "dual_connect" and prop not in ["enabled", "preferred_device"]:
  41. print(" ", prop)
  42. for key, value in json.loads(store[group][prop]).items():
  43. print(" - ", to_fixed(key, 15), value)
  44. else:
  45. print(" ", to_fixed(prop, 30),
  46. store[group][prop])