From f3fb41b37cd34c961750aa25701fbb35941ff75f Mon Sep 17 00:00:00 2001 From: Edi Feschiyan <edi.feschiyan@konsulko.com> Date: Thu, 14 May 2020 12:55:15 +0300 Subject: turned weather into oneliner --- .gitignore | 2 ++ weather.py | 49 ++++++++++++++----------------------------------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 1c2d52b..4876ac5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .idea/* +scratch +__pycache__ diff --git a/weather.py b/weather.py index 3cdca79..22c2646 100644 --- a/weather.py +++ b/weather.py @@ -1,50 +1,29 @@ -from random import randint -import sys import asyncio from random import randint -from websockets import connect import json +from aglbaseservice import AGLBaseService msgq = {} -IPADDR = '192.168.234.34' -PORT = '30031' -# PORT = '30031' -TOKEN = 'HELLO' -UUID = 'magic' -URL = f'ws://{IPADDR}:{PORT}/api?token={TOKEN}&uuid={UUID}' - -class WeatherService: - 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']) - self.websocket = await self._conn.__aenter__() - return self - async def close(self): - await self._conn.__aexit__(*sys.exc_info()) +class WeatherService(AGLBaseService): + service = 'agl-service-weather' + parser = AGLBaseService.getparser() + parser.add_argument('--apikey', default=False, help='Request weather API Key', action='store_true') - async def send(self, message): - await self.websocket.send(message) - - async def receive(self): - return await self.websocket.recv() + def __init__(self, ip, port=None): + super().__init__(api='weather', ip=ip, port=port, service='agl-service-weather') async def apikey(self): - msgid = randint(0, 999999) - msgq[msgid] = {'request': msgid, 'response': None} - await self.websocket.send(message=f'[2,"{msgid}","weather/api_key",""]'.format(str(msgid))) - return await self.receive() + return await self.request('api_key', "") async def main(): - MPS = await WeatherService() - try: - print(json.dumps(json.loads(await MPS.apikey()), indent=4, sort_keys=True)) - - finally: - await MPS.close() + args = WeatherService.parser.parse_args() + ws = await WeatherService(ip=args.ipaddr) + if args.apikey: + id = await ws.apikey() + resp = await ws.response() + print(resp[2]['response']['api_key']) if __name__ == '__main__': loop = asyncio.get_event_loop() -- cgit