aboutsummaryrefslogtreecommitdiffstats
path: root/aglbaseservice.py
diff options
context:
space:
mode:
authorEdi Feschiyan <edi.feschiyan@konsulko.com>2020-05-14 12:32:59 +0300
committerEdi Feschiyan <edi.feschiyan@konsulko.com>2020-06-09 09:29:49 +0300
commit32540833af3e633880aa365284c72950da27fece (patch)
tree7f319fad0d79ad2c7a10baccd012ed58876da7c1 /aglbaseservice.py
parent219e992aa8a8e5a7db48cc9d66bb0ddbbe7c21a3 (diff)
removing waitresponse, will be using *Service.response() to get a packet. trying to figure out async generator as a service listener. WIP
Diffstat (limited to 'aglbaseservice.py')
-rw-r--r--aglbaseservice.py54
1 files changed, 16 insertions, 38 deletions
diff --git a/aglbaseservice.py b/aglbaseservice.py
index ff35df4..6d68095 100644
--- a/aglbaseservice.py
+++ b/aglbaseservice.py
@@ -24,6 +24,7 @@ class AFBT(IntEnum):
EVENT = 5
msgq = {}
+AFBLEN = 3
def addrequest(msgid, msg):
msgq[msgid] = {'request': msg, 'response': None}
@@ -41,7 +42,7 @@ class AGLBaseService:
token: str
uuid: str
service = None
- logger = logging.getLogger(service)
+ logger = None
@staticmethod
def getparser():
@@ -150,45 +151,24 @@ class AGLBaseService:
return port
async def listener(self, stdout: bool = False):
- try:
- while True:
- msg = await self.receive()
- try:
- data = json.loads(msg)
- self.logger.debug('[AGL] -> ' + msg)
- if isinstance(data, list):
- if stdout:
- print(data)
- if data[0] == AFBT.RESPONSE and str.isnumeric(data[1]):
- msgid = int(data[1])
- if msgid in msgq:
- addresponse(msgid, data)
-
- except JSONDecodeError:
- self.logger.warning("Not decoding a non-json message")
+ while True:
+ data = await self.response()
+ if stdout: print(data)
+ yield data
- except KeyboardInterrupt:
- self.logger.debug("Received keyboard interrupt, exiting")
- except asyncio.CancelledError:
- self.logger.warning("Websocket listener coroutine stopped")
- except Exception as e:
- self.logger.error("Unhandled seal: " + str(e))
-
- async def response(self, stdout: bool = False):
+ async def response(self):
try:
msg = await self.websocket.recv()
try:
data = json.loads(msg)
- yield data
self.logger.debug('[AGL] -> ' + msg)
if isinstance(data, list):
- if stdout:
- print(data)
- if data[0] == AFBT.RESPONSE and str.isnumeric(data[1]):
+
+ if len(data) == AFBLEN and data[0] == AFBT.RESPONSE and str.isnumeric(data[1]):
msgid = int(data[1])
if msgid in msgq:
addresponse(msgid, data)
- yield data
+ return data
except JSONDecodeError:
self.logger.warning("Not decoding a non-json message")
@@ -199,16 +179,14 @@ class AGLBaseService:
except Exception as e:
self.logger.error("Unhandled seal: " + str(e))
- async def request(self, verb: str, values: Union[str, dict] = "", msgid: int = randint(0, 9999999),
- waitresponse: bool = False):
+ async def request(self, verb: str, values: Union[str, dict] = "", msgid: int = randint(0, 9999999)):
l = json.dumps([AFBT.REQUEST, str(msgid), f'{self.api}/{verb}', values])
self.logger.debug(f'[AGL] <- {l}')
await self.send(l)
- if waitresponse:
- return await self.receive()
+ return msgid
- async def subscribe(self, event, waitresponse=False):
- await self.request('subscribe', {'value': f'{event}'}, waitresponse=waitresponse)
+ async def subscribe(self, event):
+ return await self.request('subscribe', {'value': f'{event}'})
- async def unsubscribe(self, event, waitresponse=False):
- await self.request('unsubscribe', {'value': f'{event}'}, waitresponse=waitresponse)
+ async def unsubscribe(self, event):
+ return await self.request('unsubscribe', {'value': f'{event}'})