diff options
-rw-r--r-- | aglbaseservice.py | 7 | ||||
-rw-r--r-- | bluetooth.py | 64 | ||||
-rw-r--r-- | gps.py | 64 | ||||
-rw-r--r-- | mediaplayer.py | 30 | ||||
-rw-r--r-- | mps.py | 0 |
5 files changed, 85 insertions, 80 deletions
diff --git a/aglbaseservice.py b/aglbaseservice.py index f97402b..972b6bb 100644 --- a/aglbaseservice.py +++ b/aglbaseservice.py @@ -6,8 +6,7 @@ import sys import asyncio from random import randint from websockets import connect -from os import environ -from argparse import ArgumentParser + # IPADDR = '127.0.0.1' @@ -93,10 +92,12 @@ class AGLBaseService: print("Received keyboard interrupt, exiting") except asyncio.CancelledError: print("Websocket listener coroutine stopped") + except Exception as e: + print("vote du phoque?!?!? : " + str(e)) async def subscribe(self, event): msgid = randint(0, 999999) - msg = f'["{AFBT.REQUEST}","{msgid}","{self.api}/subscribe",{{"value": "{event}"}}]' + msg = f'[{AFBT.REQUEST},"{msgid}","{self.api}/subscribe",{{"value": "{event}"}}]' await self.send(msg) async def unsubscribe(self, event): diff --git a/bluetooth.py b/bluetooth.py index 13ab0c9..c5394bb 100644 --- a/bluetooth.py +++ b/bluetooth.py @@ -1,10 +1,12 @@ import asyncio +import json +import os from random import randint from aglbaseservice import AGLBaseService 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): @@ -12,27 +14,30 @@ class BluetoothService(AGLBaseService): def __init__(self, ip, port): super().__init__(api='Bluetooth-Manager', ip=ip, port=port) - async def subscribe(self, event='location'): - super().subscribe(event=event) + async def subscribe(self, event='device_changes'): + await super().subscribe(event=event) - async def unsubscribe(self, event='location'): - super().unsubscribe(event=event) + async def unsubscribe(self, event='device_changes'): + await super().unsubscribe(event=event) async def managed_objects(self): verb = 'managed_objects' msgid = randint(0, 999999) msg = f'[2,"{msgid}","{self.api}/{verb}",""]' - print(msg) + # print(msg) await self.send(msg) return await self.receive() - async def adapter_state(self, param = None, value = None): + async def adapter_state(self, param=None, value=None): verb = 'adapter_state' msgid = randint(0, 999999) if param: - p = str({'adapter': param}) + p = {'adapter': param} + if isinstance(value, dict): + p = {**p, **value} + # msg = f'[2,"{msgid}","{self.api}/{verb}","{param}": {value if value is not None else ""}]' - msg = f'[2,"{msgid}","{self.api}/{verb}", {p}]' + msg = f'[2,"{msgid}","{self.api}/{verb}", {json.dumps(p)}]' else: msg = f'[2,"{msgid}","{self.api}/{verb}", ""]' @@ -44,21 +49,48 @@ class BluetoothService(AGLBaseService): verb = 'default_adapter' msgid = randint(0, 999999) msg = f'[2,"{msgid}","{self.api}/{verb}",""]' - print(msg) + # print(msg) await self.send(msg) return await self.receive() 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): - BTS = await BluetoothService(ip='192.168.234.202', port='30005') - #print(await BTS.managed_objects()) - print(await BTS.adapter_state('hci0', '{"discoverable": true}')) - await asyncio.sleep(delay=1) - print(await BTS.adapter_state('hci0')) - print(await BTS.adapter_state('hci1', '""')) + 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']})) if __name__ == '__main__': loop = asyncio.get_event_loop() @@ -4,62 +4,40 @@ import asyncio from random import randint from websockets import connect from websockets.exceptions import ConnectionClosedError +import os import json import concurrent +from aglbaseservice import AGLBaseService -IPADDR = '192.168.234.34' -PORT = '30011' -# PORT = '30031' -TOKEN = 'HELLO' -UUID = 'magic' -URL = f'ws://{IPADDR}:{PORT}/api?token={TOKEN}&uuid={UUID}' - -msgq = {} - - -class GPSService: - def __await__(self): - return self._async_init().__await__() - - async def _async_init(self): - self._conn = connect(close_timeout=0, uri=URL, subprotocols=['x-afb-ws-json1'], ping_interval=1) - self.websocket = await self._conn.__aenter__() - return self - - async def close(self): - await self._conn.__aexit__(*sys.exc_info()) - - async def send(self, message): - await self.websocket.send(message) - - async def receive(self): - return await self.websocket.recv() +class GPSService(AGLBaseService): + def __init__(self, ip, port): + super().__init__(api='gps', ip=ip, port=port) async def location(self): + verb = 'location' msgid = randint(0, 999999) - msgq[msgid] = {'request': msgid, 'response': None} - await self.websocket.send(f'[2,"{msgid}","gps/location",""]') + await self.send(f'[2,"{msgid}","{self.api}/{verb}",""]') return await self.receive() async def subscribe(self, event='location'): - msgid = randint(0, 999999) - msg = f'[2,"{msgid}","gps/subscribe",{{"value": "{event}"}}]' - await self.send(msg) + await super().subscribe(event=event) + + async def unsubscribe(self, event='location'): + await super().subscribe(event=event) + + +async def main(loop): + addr = os.environ.get('AGL_TGT_IP', 'localhost') + port = os.environ.get('AGL_TGT_PORT', '30011') + + gpss = await GPSService(ip=addr, port=port) -async def main(): - gpss = await GPSService() - try: - data = await gpss.receive() - print(data) - await gpss.subscribe() + await gpss.location() + # listener = loop.create_task(gpss.listener()) - except ConnectionClosedError as e: - print(e) - finally: - await gpss.close() if __name__ == '__main__': loop = asyncio.get_event_loop() - loop.run_until_complete(main())
\ No newline at end of file + loop.run_until_complete(main(loop))
\ No newline at end of file diff --git a/mediaplayer.py b/mediaplayer.py index 365eef2..ea8ea2e 100644 --- a/mediaplayer.py +++ b/mediaplayer.py @@ -1,6 +1,7 @@ import json from json import JSONDecodeError from random import randint +import os import sys import asyncio from random import randint @@ -9,13 +10,9 @@ import concurrent from enum import IntEnum from os import environ from argparse import ArgumentParser +from aglbaseservice import AGLBaseService -IPADDR = '192.168.234.202' -PORT = '30016' -# PORT = '30031' -TOKEN = 'HELLO' -UUID = 'magic' -URL = f'ws://{IPADDR}:{PORT}/api?token={TOKEN}&uuid={UUID}' +URL = f'ws://{os.environ["AGL_TGT_IP"]}:{os.environ.get("AGL_TGT_PORT","30016")}/api?token=HELLO&uuid=magic' # x-AFB-ws-json1 message Type class AFBT(IntEnum): @@ -34,15 +31,11 @@ def addresponse(msgid, msg): msgq[msgid]['response'] = msg -class MediaPlayerService: - api = None - url = None - ip = None - port = None - token = None - uuid = None +class MediaPlayerService(): + def __init__(self, ip, port): + super().__init__(api='mediaplayer', ip=ip, port=port) - def __init__(self, url=None, api='mediaplayer', ip='127.0.0.1', port='30000', token='HELLO', uuid='magic'): + def __init__(self, url=None, api='', ip='127.0.0.1', port='30000', token='HELLO', uuid='magic'): self.api = api self.url = url self.ip = ip @@ -93,7 +86,6 @@ class MediaPlayerService: if msgid in msgq: addresponse(msgid, data) - except JSONDecodeError: print("not decoding a non-json message") @@ -164,12 +156,14 @@ class MediaPlayerService: async def main(loop): - MPS = await MediaPlayerService() + addr = os.environ.get('AGL_TGT_IP', 'localhost') + port = os.environ.get('AGL_TGT_PORT', '30016') + + MPS = await MediaPlayerService(ip=addr, port=port) listener = loop.create_task(MPS.listener()) try: await MPS.subscribe() - await MPS.subscribe('playlist') - await MPS.control('pick-track', 6) + await listener except ConnectionClosedError as e: print("Connection timed out or closed abnormally. Trying to reconnect...") |