diff options
Diffstat (limited to 'mediaplayer.py')
-rw-r--r-- | mediaplayer.py | 133 |
1 files changed, 106 insertions, 27 deletions
diff --git a/mediaplayer.py b/mediaplayer.py index 3466d06..9291981 100644 --- a/mediaplayer.py +++ b/mediaplayer.py @@ -1,26 +1,52 @@ -from aglbaseservice import AGLBaseService +from aglbaseservice import AGLBaseService, AFBResponse +from typing import Union +import logging import asyncio import os +class AFBMediaPlayerResponse(AFBResponse): + status: str + info: str + data = None + + def __init__(self, data: AFBResponse): + if isinstance(data, list): + super().__init__(data) + self.msgid = data.msgid + self.type = data.type + self.data = data.data + class MediaPlayerService(AGLBaseService): + service = 'agl-service-mediaplayer' + parser = AGLBaseService.getparser() + parser.add_argument('--playlist', help='Get current playlist', action='store_true') + parser.add_argument('--control', help='Play/Pause/Previous/Next') + parser.add_argument('--seek', help='Seek time through audio track', metavar='msec', type=int) + parser.add_argument('--rewind', help='Rewind time', metavar='msec', type=int) + parser.add_argument('--fastforward', help='Fast forward time', metavar='msec', type=int) + parser.add_argument('--picktrack', help='Play specific track in the playlist', metavar='index', type=int) + parser.add_argument('--volume', help='Volume control - <1-100>', metavar='int') + parser.add_argument('--loop', help='Set loop state - <off/track/playlist>', metavar='string') + parser.add_argument('--avrcp', help='AVRCP Controls') def __await__(self): return super()._async_init().__await__() - def __init__(self, ip, port = None): + def __init__(self, ip, port=None): super().__init__(api='mediaplayer', ip=ip, port=port, service='agl-service-mediaplayer') async def playlist(self): return await self.request('playlist') async def subscribe(self, event='metadata'): - await super().subscribe(event=event) + return await super().subscribe(event=event) async def unsubscribe(self, event='metadata'): - await super().subscribe(event=event) + return await super().subscribe(event=event) async def control(self, name, value=None): loopstate = ['off', 'playlist', 'track'] + avrcp_controls = ['next', 'previous', 'play', 'pause'] controls = { 'play': None, 'pause': None, @@ -31,19 +57,20 @@ class MediaPlayerService(AGLBaseService): 'rewind': 'position', 'pick-track': 'index', 'volume': 'volume', - 'loop': 'state' + 'loop': 'state', + # 'avrcp_controls': 'value' } - assert name in controls.keys(), 'Tried to use non-existent {name} as control for {self.api}' - + assert name in controls.keys(), f'Tried to use non-existent {name} as control for {self.api}' + msg = None if name in ['play', 'pause', 'previous', 'next']: msg = {'value': name} elif name in ['seek', 'fast-forward', 'rewind']: - assert value > 0, "Tried to seek with negative integer" + #assert value > 0, "Tried to seek with negative integer" msg = {'value': name, controls[name]: str(value)} elif name == 'pick-track': assert type(value) is int, "Try picking a song with an integer" assert value > 0, "Tried to pick a song with a negative integer" - msg = {'value': name, controls[value]: str(value)} + msg = {'value': name, controls[name]: str(value)} elif name == 'volume': assert type(value) is int, "Try setting the volume with an integer" assert value > 0, "Tried to set the volume with a negative integer, use values betwen 0-100" @@ -52,28 +79,80 @@ class MediaPlayerService(AGLBaseService): elif name == 'loop': assert value in loopstate, f'Tried to set invalid loopstate - {value}, use "off", "playlist" or "track"' msg = {'value': name, controls[name]: str(value)} + # elif name == 'avrcp_controls': + # msg = {'value': name, } + assert msg is not None, "Congratulations, somehow you made an invalid control request" - await self.request('controls', msg) + return await self.request('controls', msg) async def main(loop): - addr = os.environ.get('AGL_TGT_IP', '192.168.234.202') - port = os.environ.get('AGL_TGT_PORT', None) - - MPS = await MediaPlayerService(ip=addr, port=port) - # listener = loop.create_task(MPS.listener()) - try: - await MPS.subscribe('metadata') - await MPS.playlist() - await MPS.control('next') - - # await listener - - except KeyboardInterrupt: - pass - - # listener.cancel() - await MPS.unsubscribe('playlist') + args = MediaPlayerService.parser.parse_args() + MPS = await MediaPlayerService(ip=args.ipaddr) + + if args.playlist: + id = await MPS.playlist() + r = AFBResponse(await MPS.response()) + for l in r.data['list']: print(l) + + if args.control: + id = await MPS.control(args.control) + print(f'Sent {args.control} request with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.seek: + id = await MPS.control('seek', args.seek) + print(f'Sent seek request to {args.seek} msec with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.fastforward: + id = await MPS.control('fast-forward', args.fastforward) + print(f'Sent fast-forward request for {args.fastforward} msec with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.rewind: + id = await MPS.control('rewind', -args.rewind) + print(f'Sent rewind request for {args.rewind} msec with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.picktrack: + id = await MPS.control('pick-track', args.picktrack) + print(f'Sent pick-track request with index {args.rewind} with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.volume: + id = await MPS.control('volume', int(args.volume)) + print(f'Sent volume request: {args.rewind} with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + if args.loop: + id = await MPS.control('loop', args.loop) + print(f'Sent loop-state request: {args.loop} with messageid {id}') + r = AFBResponse(await MPS.response()) + print(r) + + # if args.avrcp: + # id = await MPS.control('avrcp_controls', args.avrcp) + # print(f'Sent AVRCP control request: {args.loop} with messageid {id}') + # r = AFBResponse(await MPS.response()) + # print(r) + + if args.subscribe: + for event in args.subscribe: + id = await MPS.subscribe(event) + print(f"Subscribed for event {event} with messageid {id}") + r = await MPS.response() + print(r) + + if args.listener: + async for response in MPS.listener(): + print(response) if __name__ == '__main__': |