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, control='Master', value=None): if value is not None: return await self.request('volume', {'control': control, 'value': value}) else: return await self.request('volume', {'control': control}) async def mute(self, value=None): return await self.request('mute', {'control': 'Master', 'value': value})
class BluetoothService(AGLBaseService): service = 'agl-service-bluetooth' parser = AGLBaseService.getparser() parser.add_argument('--default_adapter', help='Get default bluetooth adapter', action='store_true') parser.add_argument('--managed_objects', help='Get managed objects', action='store_true') parser.add_argument('--adapter', help='Select remote adapter', required=False, default='hci0') parser.add_argument('--adapter_state') parser.add_argument('--connect', help='Connect to device', metavar='dev_88_0F_10_96_D3_20') parser.add_argument('--disconnect', help='Disconnect from device', metavar='dev_88_0F_10_96_D3_20') parser.add_argument('--pair', help='Pair with a device', metavar='dev_88_0F_10_96_D3_20') parser.add_argument('--cancel_pairing', help='Cancel ongoing pairing') parser.add_argument('--confirm_pairing', metavar='pincode') parser.add_argument('--remove_device', metavar='dev_88_0F_10_96_D3_20', help='Remove paired device') 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'): await super().subscribe(event=event) async def unsubscribe(self, event='device_changes'): await super().unsubscribe(event=event) async def managed_objects(self): return await self.request('managed_objects') async def adapter_state(self, adapter=None, value=None): p = {} if adapter: p = {'adapter': adapter} if isinstance(value, dict): p = {**p, **value} return await self.request('adapter_state', p) async def default_adapter(self): return await self.request('default_adapter', "") async def connect(self, device: str = 'hci0'): return await self.request('connect', {'device': device}) async def disconnect(self, device: str = 'hci0'): return await self.request('disconnect', {'device': device}) async def pair(self, device): return await self.request('pair', {'device': device}) async def cancel_pairing(self): return await self.request('cancel_pairing') async def confirm_pairing(self, pincode): return await self.request('confirm_pairing', {'pincode': pincode}) async def remove_device(self, device): return await self.request('remove_device', {'device': device}) async def avrcp_controls(self): pass
class NFCService(AGLBaseService): service = 'agl-service-nfc' parser = AGLBaseService.getparser() def __init__(self, ip, port=None, api='nfc'): super().__init__(ip=ip, port=port, api=api, service='agl-service-nfc') async def subscribe(self, event='presence'): return await super().subscribe(event=event) async def unsubscribe(self, event='presence'): return await super().unsubscribe(event=event)
class WeatherService(AGLBaseService): service = 'agl-service-weather' parser = AGLBaseService.getparser() parser.add_argument('--current', default=True, help='Request current weather state', action='store_true') parser.add_argument('--apikey', default=False, help='Request weather API Key', action='store_true') def __init__(self, ip, port=None): super().__init__(api='weather', ip=ip, port=port, service='agl-service-weather') async def current_weather(self): return await self.request('current_weather', "") async def apikey(self): return await self.request('api_key', "")
class GPSService(AGLBaseService): service = 'agl-service-gps' parser = AGLBaseService.getparser() parser.add_argument('--record', help='Begin recording verb ') parser.add_argument('--location', help='Get current location', action='store_true') def __init__(self, ip, port=None): super().__init__(api='gps', ip=ip, port=port, service='agl-service-gps') async def location(self): return await self.request('location') async def record(self, state='on'): return await self.request('record', {'state': state}) async def subscribe(self, event='location'): return await super().subscribe(event=event) async def unsubscribe(self, event='location'): return await super().subscribe(event=event)
class GeoClueService(AGLBaseService): service = 'agl-service-geoclue' parser = AGLBaseService.getparser() parser.add_argument('--location', help='Get current location', action='store_true') def __init__(self, ip, port=None, api='geoclue'): super().__init__(ip=ip, port=port, api=api, service='agl-service-geoclue') async def location(self): return await self.request('location') async def subscribe(self, event='location'): return await super().subscribe(event=event) async def unsubscribe(self, event='location'): return await super().unsubscribe(event=event)
class BTMAPService(AGLBaseService): service = 'agl-service-bluetooth-map' parser = AGLBaseService.getparser() parser.add_argument('--compose', help='Compose text message to a recipient', action='store_true') parser.add_argument('--recipient', help='Recipient for composing a message') parser.add_argument('--message', help='Message to send to the recipient') parser.add_argument('--list_messages', help='List text messages over MAP', action='store_true') def __init__(self, ip, port=None, service='agl-service-bluetooth-map'): super().__init__(api='bluetooth-map', ip=ip, port=port, service=service) async def compose(self, recipient, message): return await self.request('compose', { 'recipient': recipient, 'message': message }) async def message(self, handle): return await self.request('message', {'handle': handle}) async def list_messages(self, folder='INBOX'): return await self.request('list_messages', {'folder': folder}) async def subscribe(self, event='notification'): return await super().subscribe(event) async def unsubscribe(self, event='notification'): return await super().subscribe(event)
from pyagl.services.base import AGLBaseService, AFBResponse import asyncio import os class {{cookiecutter.classname}}(AGLBaseService): service = '{{cookiecutter.aglsystemdservice}}' parser = AGLBaseService.getparser() def __init__(self, ip, port=None, service='{{cookiecutter.aglsystemdservice}}'): super().__init__(api='{{cookiecutter.api}}', ip=ip, port=port, service=service) # more init stuff specific to the new service async def main(loop): args = {{cookiecutter.classname}}.parser.parse_args() svc = {{cookiecutter.classname}}(args.ipaddr)
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): 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'): return await super().subscribe(event=event) async def unsubscribe(self, event='metadata'): 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, 'previous': None, 'next': None, 'seek': 'position', 'fast-forward': 'position', 'rewind': 'position', 'pick-track': 'index', 'volume': 'volume', 'loop': 'state', # 'avrcp_controls': 'value' } 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" 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[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" assert value < 100, "Tried to set the volume over 100%, use values betwen 0-100" msg = {'value': name, name: str(value)} 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" return await self.request('controls', msg)