From 03471f59045bba8b61d1e3056d75178fe297b57a Mon Sep 17 00:00:00 2001 From: Edi Feschiyan Date: Mon, 8 Jun 2020 13:43:13 +0300 Subject: Completed audiomixer api --- .gitignore | 1 + aglbaseservice.py | 5 +-- audiomixer.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gps.py | 2 +- weather.py | 2 +- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 audiomixer.py diff --git a/.gitignore b/.gitignore index 4876ac5..67ad1bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* scratch __pycache__ +downloads/ diff --git a/aglbaseservice.py b/aglbaseservice.py index bc4b1d2..2146491 100644 --- a/aglbaseservice.py +++ b/aglbaseservice.py @@ -94,6 +94,7 @@ class AFBResponse: f'[Info: {self.info if hasattr(self,"info") else None}]' \ f'[Data: {self.data if hasattr(self, "data") else None}]' + class AGLBaseService: api: str url: str @@ -148,7 +149,7 @@ class AGLBaseService: exit(1) URL = f'ws://{self.ip}:{self.port}/api?token={self.token}&uuid={self.uuid}' - self._conn = connect(close_timeout=0, uri=URL, subprotocols=['x-afb-ws-json1'], ping_timeout=None) + self._conn = connect(close_timeout=0, uri=URL, subprotocols=['x-afb-ws-json1'], ping_timeout=None, compression=None) self.websocket = await self._conn.__aenter__() return self @@ -248,7 +249,7 @@ class AGLBaseService: return msgid async def subscribe(self, event): - return await self.request('subscribe', {'value': f'{event}'}) + return await self.request('subscribe', {'value': f'{event}'}) # some services may use 'event' instead 'value' async def unsubscribe(self, event): return await self.request('unsubscribe', {'value': f'{event}'}) diff --git a/audiomixer.py b/audiomixer.py new file mode 100644 index 0000000..3d61c10 --- /dev/null +++ b/audiomixer.py @@ -0,0 +1,94 @@ +from aglbaseservice import AGLBaseService, AFBResponse +import asyncio +import os + +verbs = ['subscribe', 'unsubscribe', 'list_controls', 'volume', 'mute'] +events = ['volume_changed', 'mute_changed', 'controls_changed'] + + +class AudioMixerService(AGLBaseService): + service = 'agl-service-audiomixer' + parser = AGLBaseService.getparser() + parser.add_argument('--list_controls', default=True, help='Request list of controls', action='store_true') + parser.add_argument('--getmute', help='Get mute state', action='store_true') + parser.add_argument('--setmute', help='Set mute state', type=int, choices=[0, 1]) + parser.add_argument('--setvolume', help='Set volume level', type=float) + parser.add_argument('--getvolume', help='Get volume level', action='store_true') + + + def __init__(self, ip, port=None, service='agl-service-audiomixer'): + super().__init__(api='audiomixer', ip=ip, port=port, service=service) + + async def subscribe(self, event='volume_changed'): # audio mixer uses 'event' instead 'value', + return await self.request('subscribe', {'event': event}) + + async def unsubscribe(self, event='volume_changed'): + return await self.request('unsubscribe', {'event': event}) + + async def list_controls(self): + return await self.request('list_controls') + + async def volume(self, value=None): + if value is not None: + return await self.request('volume', {'control': 'Master', 'value': value}) + else: + return await self.request('volume', {'control': 'Master'}) + + async def mute(self, value=None): + return await self.request('mute', {'control': 'Master', 'value': value}) + + +async def main(): + args = AudioMixerService.parser.parse_args() + ams = await AudioMixerService(ip=args.ipaddr, port=args.port) + + if args.list_controls: + resp = await ams.list_controls() + print(f'Requesting list_controls with id {resp}') + r = AFBResponse(await ams.response()) + print(r) + + if args.setvolume is not None: + resp = await ams.volume(args.setvolume) + print(f'Setting volume to {args.setvolume} with id {resp}') + r = AFBResponse(await ams.response()) + print(r) + + if args.getvolume: + resp = await ams.volume() + print(f'Requesting volume with id {resp}') + r = AFBResponse(await ams.response()) + print(r) + + if args.setmute is not None: + resp = await ams.mute(args.setmute) + print(f'Setting mute to {args.setmute} with id {resp}') + r = AFBResponse(await ams.response()) + print(r) + + if args.getmute: + resp = await ams.mute() + r = AFBResponse(await ams.response()) + print(r) + + if args.subscribe: + for event in args.subscribe: + id = await ams.subscribe(event) + print(f'Subscribing to {event} with id {id}') + r = AFBResponse(await ams.response()) + print(r) + + if args.unsubscribe: + for event in args.unsubscribe: + id = await ams.unsubscribe(event) + print(f'Unsubscribing from {event} with id {id}') + r = AFBResponse(await ams.response()) + print(r) + + if args.listener: + async for response in ams.listener(): + print(response) + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/gps.py b/gps.py index c11b2a8..1905e0f 100644 --- a/gps.py +++ b/gps.py @@ -27,7 +27,7 @@ class GPSService(AGLBaseService): async def main(loop): args = GPSService.parser.parse_args() - gpss = await GPSService(args.ipaddr) + gpss = await GPSService(ip=args.ipaddr, port=args.port) if args.loglevel: gpss.logger.setLevel(args.loglevel) diff --git a/weather.py b/weather.py index de0223f..f3c02d0 100644 --- a/weather.py +++ b/weather.py @@ -21,7 +21,7 @@ class WeatherService(AGLBaseService): async def main(): args = WeatherService.parser.parse_args() - aws = await WeatherService(ip=args.ipaddr) + aws = await WeatherService(ip=args.ipaddr, port=args.port) if args.current: id = await aws.current_weather() resp = AFBResponse(await aws.response()) -- cgit 1.2.3-korg