diff options
author | Edi Feschiyan <edi.feschiyan@konsulko.com> | 2020-06-24 20:00:09 +0300 |
---|---|---|
committer | Edi Feschiyan <edi.feschiyan@konsulko.com> | 2020-06-24 20:00:09 +0300 |
commit | 4f89ab88c04333d97dd2ee4a588303c07e55b7c5 (patch) | |
tree | 766c61c00893f1feab33e8a7c798861babe3014a | |
parent | 890fb73d88b2cf290df947a7438be87a73fb92b5 (diff) |
Adding bluetooth_map
-rw-r--r-- | pyagl/services/bluetooth_map.py | 63 | ||||
-rw-r--r-- | pyagl/tests/test_bluetooth_map.py | 89 | ||||
-rw-r--r-- | tests/test_weather.py | 0 |
3 files changed, 152 insertions, 0 deletions
diff --git a/pyagl/services/bluetooth_map.py b/pyagl/services/bluetooth_map.py new file mode 100644 index 0000000..24797db --- /dev/null +++ b/pyagl/services/bluetooth_map.py @@ -0,0 +1,63 @@ +from pyagl.services.base import AGLBaseService, AFBResponse +import asyncio +import os + +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) + +async def main(loop): + args = BTMAPService.parser.parse_args() + bmp = await BTMAPService(args.ipaddr) + + if args.compose: + if 'recipient' not in args or 'message' not in args: + BTMAPService.parser.error("You have to use both --recipipent and --message in order to compose") + msgid = await bmp.compose(recipient=args.recipient, message=args.message) + print(f'Sent compose request [recipient:{args.recipient}][message:{args.message}] with messageid {msgid}') + resp = await bmp.afbresponse() + print(resp) + + if args.list_messages: + msgid = await bmp.list_messages() + print(f'Sent list_messages request with messageid {msgid}') + resp = await bmp.afbresponse() + print(resp) + + if args.subscribe: + msgid = await bmp.subscribe(args.subscribe) + print(f'Subscribed for {args.subscribe} with messageid {msgid}') + resp = await bmp.afbresponse() + print(resp) + + if args.listener: + async for response in bmp.listener(): + print(response) + + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) diff --git a/pyagl/tests/test_bluetooth_map.py b/pyagl/tests/test_bluetooth_map.py new file mode 100644 index 0000000..9dec04c --- /dev/null +++ b/pyagl/tests/test_bluetooth_map.py @@ -0,0 +1,89 @@ +import asyncio +import os +import pytest +from random import randint +from pyagl.services.base import AFBResponse, AFBT +from pyagl.services.bluetooth_map import BTMAPService as BMP +import logging + +pytestmark = pytest.mark.asyncio + + +@pytest.fixture(scope='module') +def event_loop(): + loop = asyncio.get_event_loop() + yield loop + + +@pytest.fixture(scope='module') +async def service(): + address = os.environ.get('AGL_TGT_IP', 'localhost') + port = os.environ.get('AGL_TGT_PORT', None) + + ams = await BMP(ip=address, port=port) + yield ams + await ams.websocket.close() + +@pytest.fixture(scope='module') +def recipient(): + phonenumber = os.environ.get('AGL_BTMAP_RECIPIENT', None) + if phonenumber is None: + pytest.xfail('No phone number was set, please "export AGL_BTMAP_RECIPIENT=" with a phonenumber') + return phonenumber + +@pytest.fixture(scope='module') +def composetext(): + text = os.environ.get('AGL_BTMAP_TEXT', + f'#{randint(0,9999999)} - Hello from AGL. This message is generated by test_bluetooth_map.py') + return text + + + +@pytest.mark.xfail(reason='Expected to fail if no messages are found') +@pytest.fixture(scope='module') +async def messages(service: BMP): + msgid = await service.list_messages() + resp = await service.afbresponse() + assert 'messages' in resp.data + assert len(resp.data) > 0, "no messages found " + yield resp.data['messages'] + +@pytest.mark.dependency +async def test_list_messages(event_loop, service: BMP): + msgid = await service.list_messages() + resp = await service.afbresponse() + assert resp.msgid == msgid + +@pytest.mark.dependency(depends=['test_list_messages']) +@pytest.mark.xfail(reason='Expected to fail if there are no messages, need message id to test the verb') +async def test_message_verb(event_loop, service, messages): + assert len(messages), "No existing messages to test the verb with" + messageids = list(messages.keys()) + msgid = await service.message(messageids.pop()) + resp = await service.afbresponse() + assert resp.status == 'success' + +@pytest.mark.dependency +async def test_subscribe_notifications(event_loop, service: BMP): + msgid = await service.subscribe('notification') + resp = await service.afbresponse() + print(resp) + assert resp.status == 'success' + + +@pytest.mark.dependency +async def test_compose_message(event_loop, service: BMP, recipient, composetext): + msgid = await service.compose(recipient, composetext) + resp = await service.afbresponse() + assert resp.status == 'success' + + +@pytest.mark.dependency(depends=['test_compose_message', 'test_subscribe_notifications']) +async def test_message_to_self(event_loop, service: BMP, recipient, composetext): + resp = await service.afbresponse() + assert resp.api == f'{service.api}/notification' + assert resp.type == AFBT.EVENT + assert isinstance(resp.data, dict) + assert 'message' in resp.data + assert resp.data['message'] == composetext, 'Message mismatching' + diff --git a/tests/test_weather.py b/tests/test_weather.py deleted file mode 100644 index e69de29..0000000 --- a/tests/test_weather.py +++ /dev/null |