summaryrefslogtreecommitdiffstats
path: root/gps.py
diff options
context:
space:
mode:
authorEdi Feschiyan <edi.feschiyan@konsulko.com>2020-03-12 18:38:18 +0200
committerEdi Feschiyan <edi.feschiyan@konsulko.com>2020-03-12 18:38:18 +0200
commit1cd18d024c69369a6e502db4cd1d1acddf1e0958 (patch)
treed205e82ec918f080e05cba5b00a48ee01132cf01 /gps.py
parentba673345466a365adb107a32dc4143390d4032eb (diff)
fixed wrong argument in websocket - sending string instead int in json for aglbaseservice
Diffstat (limited to 'gps.py')
-rw-r--r--gps.py64
1 files changed, 21 insertions, 43 deletions
diff --git a/gps.py b/gps.py
index ff6aeae..37caa4c 100644
--- a/gps.py
+++ b/gps.py
@@ -4,62 +4,40 @@ import asyncio
from random import randint
from websockets import connect
from websockets.exceptions import ConnectionClosedError
+import os
import json
import concurrent
+from aglbaseservice import AGLBaseService
-IPADDR = '192.168.234.34'
-PORT = '30011'
-# PORT = '30031'
-TOKEN = 'HELLO'
-UUID = 'magic'
-URL = f'ws://{IPADDR}:{PORT}/api?token={TOKEN}&uuid={UUID}'
-
-msgq = {}
-
-
-class GPSService:
- 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'], ping_interval=1)
- self.websocket = await self._conn.__aenter__()
- return self
-
- async def close(self):
- await self._conn.__aexit__(*sys.exc_info())
-
- async def send(self, message):
- await self.websocket.send(message)
-
- async def receive(self):
- return await self.websocket.recv()
+class GPSService(AGLBaseService):
+ def __init__(self, ip, port):
+ super().__init__(api='gps', ip=ip, port=port)
async def location(self):
+ verb = 'location'
msgid = randint(0, 999999)
- msgq[msgid] = {'request': msgid, 'response': None}
- await self.websocket.send(f'[2,"{msgid}","gps/location",""]')
+ await self.send(f'[2,"{msgid}","{self.api}/{verb}",""]')
return await self.receive()
async def subscribe(self, event='location'):
- msgid = randint(0, 999999)
- msg = f'[2,"{msgid}","gps/subscribe",{{"value": "{event}"}}]'
- await self.send(msg)
+ await super().subscribe(event=event)
+
+ async def unsubscribe(self, event='location'):
+ await super().subscribe(event=event)
+
+
+async def main(loop):
+ addr = os.environ.get('AGL_TGT_IP', 'localhost')
+ port = os.environ.get('AGL_TGT_PORT', '30011')
+
+ gpss = await GPSService(ip=addr, port=port)
-async def main():
- gpss = await GPSService()
- try:
- data = await gpss.receive()
- print(data)
- await gpss.subscribe()
+ await gpss.location()
+ # listener = loop.create_task(gpss.listener())
- except ConnectionClosedError as e:
- print(e)
- finally:
- await gpss.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
- loop.run_until_complete(main()) \ No newline at end of file
+ loop.run_until_complete(main(loop)) \ No newline at end of file