2020-10-03 02:45:45 +00:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
# Copyright (c) 2020, The Monero Project.
|
|
|
|
# Copyright (c) 2020, dsc@xmr.pm
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
|
|
|
|
from quart import websocket, jsonify
|
|
|
|
|
2021-04-05 17:49:02 +00:00
|
|
|
from wowlet_backend.factory import app
|
|
|
|
from wowlet_backend.wsparse import WebsocketParse
|
|
|
|
from wowlet_backend.utils import collect_websocket, feather_data
|
2020-10-03 02:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
async def root():
|
2020-12-22 18:03:48 +00:00
|
|
|
data = await feather_data()
|
|
|
|
return jsonify(data)
|
2020-10-03 02:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.websocket('/ws')
|
|
|
|
@collect_websocket
|
|
|
|
async def ws(queue):
|
2020-12-22 18:03:48 +00:00
|
|
|
data = await feather_data()
|
2020-10-03 02:45:45 +00:00
|
|
|
|
2020-12-22 18:03:48 +00:00
|
|
|
# blast available data on connect
|
|
|
|
for task_key, task_value in data.items():
|
|
|
|
if not task_value:
|
2020-10-03 02:45:45 +00:00
|
|
|
continue
|
2020-12-22 18:03:48 +00:00
|
|
|
await websocket.send(json.dumps({"cmd": task_key, "data": task_value}).encode())
|
2020-10-03 02:45:45 +00:00
|
|
|
|
|
|
|
async def rx():
|
|
|
|
while True:
|
2020-12-22 18:03:48 +00:00
|
|
|
buffer = await websocket.receive()
|
2020-10-03 02:45:45 +00:00
|
|
|
try:
|
2020-12-22 18:03:48 +00:00
|
|
|
blob = json.loads(buffer)
|
2020-10-03 02:45:45 +00:00
|
|
|
if "cmd" not in blob:
|
|
|
|
continue
|
|
|
|
cmd = blob.get('cmd')
|
|
|
|
_data = blob.get('data')
|
|
|
|
result = await WebsocketParse.parser(cmd, _data)
|
|
|
|
if result:
|
|
|
|
rtn = json.dumps({"cmd": cmd, "data": result}).encode()
|
|
|
|
await websocket.send(rtn)
|
|
|
|
except Exception as ex:
|
|
|
|
continue
|
|
|
|
|
|
|
|
async def tx():
|
|
|
|
while True:
|
|
|
|
data = await queue.get()
|
|
|
|
payload = json.dumps(data).encode()
|
|
|
|
await websocket.send(payload)
|
|
|
|
|
|
|
|
# bidirectional async rx and tx loops
|
|
|
|
consumer_task = asyncio.ensure_future(rx())
|
|
|
|
producer_task = asyncio.ensure_future(tx())
|
|
|
|
try:
|
|
|
|
await asyncio.gather(consumer_task, producer_task)
|
|
|
|
finally:
|
|
|
|
consumer_task.cancel()
|
|
|
|
producer_task.cancel()
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(403)
|
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.errorhandler(405)
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def page_not_found(e):
|
|
|
|
return ":)", 500
|