Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
49 lines (39 loc) · 1.41 KB
/
Copy pathclient.py
File metadata and controls
49 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Print live readings from a running `mapleocr serve`.
pip install websockets
python client.py # everything
python client.py hp mp exp # only these
"""
import asyncio
import json
import sys
import websockets
URL = "ws://127.0.0.1:8765/v1/stream"
def describe(v: dict) -> str:
t = v["type"]
if t == "ratio":
return f"{v['current']}/{v['max']}"
if t == "exp":
return f"{v['value']} ({v['percent']:.2f}%)"
if t == "map":
return f"{v['region']} / {v['name']}"
if t == "number":
return str(v["value"])
if t == "text":
return v["text"]
return json.dumps(v)[:120]
async def main(rois: list[str]) -> None:
async with websockets.connect(URL) as ws:
hello = json.loads(await ws.recv())
print("connected, protocol", hello["protocol"], "ROIs:", ", ".join(hello["rois"]))
if rois:
await ws.send(json.dumps({"op": "subscribe", "rois": rois}))
async for raw in ws:
msg = json.loads(raw)
if msg["type"] == "reading":
r = msg["reading"]
tag = "now " if msg["snapshot"] else "new "
print(f"{tag}{r['roi']:<16} {describe(r['value'])} [{r['engine']}, {r['confidence']:.2f}]")
elif msg["type"] == "error":
print("error:", msg["message"])
if __name__ == "__main__":
asyncio.run(main(sys.argv[1:]))