summaryrefslogtreecommitdiffstats
path: root/bluetooth.py
diff options
context:
space:
mode:
authorEdi Feschiyan <edi.feschiyan@konsulko.com>2020-04-28 12:20:48 +0300
committerEdi Feschiyan <edi.feschiyan@konsulko.com>2020-04-28 12:21:31 +0300
commit9da1c8647b233e3a591cc313ebfdac6745806f6d (patch)
tree1a606d3d4883515b422d6bb9a9c94e8150344da4 /bluetooth.py
parent7519a78657685f14f93983f474d75ae0efb4a2ad (diff)
Refactored bluetooth
Diffstat (limited to 'bluetooth.py')
-rw-r--r--bluetooth.py82
1 files changed, 30 insertions, 52 deletions
diff --git a/bluetooth.py b/bluetooth.py
index 95aa51d..caef235 100644
--- a/bluetooth.py
+++ b/bluetooth.py
@@ -1,24 +1,22 @@
+from aglbaseservice import AGLBaseService
import asyncio
-import json
import os
-from random import randint
-from aglbaseservice import AGLBaseService
-import logging
Verbs = ['subscribe', 'unsubscribe', 'managed_objects', 'adapter_state', 'default_adapter', 'avrcp_controls',
'connect', 'disconnect', 'pair', 'cancel_pairing', 'confirm_pairing', 'remove_device']
AdapterStateParams = ['discovery', 'discoverable', 'powered', ]
BTEventType = ['adapter_changes', 'device_changes', 'media', 'agent']
+
class BluetoothService(AGLBaseService):
def __init__(self, ip, port=None, service='agl-service-bluetooth'):
super().__init__(api='Bluetooth-Manager', ip=ip, port=port, service=service)
- async def subscribe(self, event='device_changes'):
+ async def subscribe(self, event='device_changes', waitresponse=False):
await super().subscribe(event=event)
- async def unsubscribe(self, event='device_changes'):
+ async def unsubscribe(self, event='device_changes', waitresponse=False):
await super().unsubscribe(event=event)
async def managed_objects(self, waitresponse=False):
@@ -32,58 +30,38 @@ class BluetoothService(AGLBaseService):
p = {**p, **value}
return await self.request('adapter_state', p, waitresponse=waitresponse)
- if waitresponse:
- return await self.request('adapter_state', p)
- else:
- await self.request('adapter_state', p)
-
- async def default_adapter(self):
- verb = 'default_adapter'
- msgid = randint(0, 999999)
- msg = f'[2,"{msgid}","{self.api}/{verb}",""]'
- # print(msg)
- await self.send(msg)
- return await self.receive()
+
+ async def default_adapter(self, waitresponse=False):
+ return await self.request('default_adapter', "", waitresponse=waitresponse)
+
+ async def connect(self, device: str = 'hci0', waitresponse=False):
+ return await self.request('connect', {'device': device}, waitresponse=waitresponse)
+
+ async def disconnect(self, device: str = 'hci0', waitresponse=False):
+ return await self.request('disconnect', {'device': device}, waitresponse=waitresponse)
+
+ async def pair(self, device, waitresponse=False):
+ return await self.request('pair', {'device': device}, waitresponse=waitresponse)
+
+ async def cancel_pairing(self, waitresponse=False):
+ return await self.request('cancel_pairing', waitresponse=waitresponse)
+
+ async def confirm_pairing(self, pincode, waitresponse=False):
+ return await self.request('confirm_pairing', {'pincode': pincode}, waitresponse=waitresponse)
async def avrcp_controls(self):
pass
- async def connect(self, device):
- verb = 'connect'
- msgid = randint(0, 999999)
- d = {'device': device}
- msg = f'[2,"{msgid}","{self.api}/{verb}","{json.dumps(d)}"]'
-
- async def disconnect(self, device):
- verb = 'disconnect'
- msgid = randint(0, 999999)
- d = {'device': device}
- msg = f'[2,"{msgid}","{self.api}/{verb}","{json.dumps(d)}"]'
-
- async def pair(self, device):
- verb = 'pair'
- msgid = randint(0, 999999)
- d = {'device': device}
- msg = f'[2,"{msgid}","{self.api}/{verb}","{json.dumps(d)}"]'
-
- async def cancel_pairing(self):
- verb = 'cancel_pairing'
- msgid = randint(0, 999999)
- msg = f'[2,"{msgid}","{self.api}/{verb}",""]'
-
- async def confirm_pairing(self, pincode):
- verb = 'confirm_pairing'
- msgid = randint(0, 999999)
- d = {'pincode': pincode}
- msg = f'[2,"{msgid}","{self.api}/{verb}","{json.dumps(d)}"]'
-
async def main(loop):
addr = os.environ.get('AGL_TGT_IP', 'localhost')
- #port = os.environ.get('AGL_TGT_PORT', '30005')
-
- BTS = await BluetoothService(ip=addr, port=port)
- print(await BTS.adapter_state('hci1', {'uuids': ['0000110e-0000-1000-8000-00805f9b34fb']}))
-
+ port = os.environ.get('AGL_TGT_PORT', None)
+ BTS = await BluetoothService(ip=addr, service='agl-service-bluetooth', port=port)
+ listener = loop.create_task(BTS.listener())
+ await BTS.subscribe()
+ BTS.logger.debug(await BTS.adapter_state('hci0', {'uuids': ['0000110e-0000-1000-8000-00805f9b34fb']}))
+ await listener
+
+
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop)) \ No newline at end of file