TempestWX 1.1.0: live mode, status pill, stable layout
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HS8Wa3DD7qZpX514kktof7
This commit is contained in:
1 parent
50b3424599
commit
70f8907236
6 files changed
+391
-30
No files matched your search
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
"""TempestWX live helper: minimal stdlib WebSocket client for the WeatherFlow
|
||||
Tempest stream. Usage: live.py <device_id>. The token is read from
|
||||
~/.config/tempestwx/token, never from argv. Prints one compact JSON line per
|
||||
obs_st / rapid_wind message. Exit 0 on SIGTERM, 1 on failure."""
|
||||
import base64, json, os, re, signal, socket, ssl, struct, sys
|
||||
|
||||
HOST = "ws.weatherflow.com"
|
||||
|
||||
def die(code):
|
||||
sys.exit(code)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2 or not re.fullmatch(r"[0-9]{1,10}", sys.argv[1]):
|
||||
die(2)
|
||||
dev = int(sys.argv[1])
|
||||
try:
|
||||
with open(os.path.expanduser("~/.config/tempestwx/token")) as f:
|
||||
tok = f.readline().strip()
|
||||
except OSError:
|
||||
die(3)
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]{8,200}", tok):
|
||||
die(3)
|
||||
signal.signal(signal.SIGTERM, lambda *a: die(0))
|
||||
raw = socket.create_connection((HOST, 443), timeout=15)
|
||||
s = ssl.create_default_context().wrap_socket(raw, server_hostname=HOST)
|
||||
key = base64.b64encode(os.urandom(16)).decode()
|
||||
s.sendall(("GET /swd/data?token=%s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\nSec-WebSocket-Key: %s\r\nSec-WebSocket-Version: 13\r\n\r\n"
|
||||
% (tok, HOST, key)).encode())
|
||||
buf = b""
|
||||
while b"\r\n\r\n" not in buf:
|
||||
d = s.recv(4096)
|
||||
if not d:
|
||||
die(1)
|
||||
buf += d
|
||||
if len(buf) > 65536:
|
||||
die(1)
|
||||
head, buf = buf.split(b"\r\n\r\n", 1)
|
||||
if b" 101 " not in head.split(b"\r\n")[0] + b" ":
|
||||
die(1)
|
||||
|
||||
def send(op, payload):
|
||||
m = os.urandom(4)
|
||||
n = len(payload)
|
||||
h = bytes([0x80 | op])
|
||||
if n < 126: h += bytes([0x80 | n])
|
||||
elif n < 65536: h += bytes([0x80 | 126]) + struct.pack(">H", n)
|
||||
else: h += bytes([0x80 | 127]) + struct.pack(">Q", n)
|
||||
s.sendall(h + m + bytes(b ^ m[i % 4] for i, b in enumerate(payload)))
|
||||
|
||||
def msg(o):
|
||||
send(1, json.dumps(o).encode())
|
||||
msg({"type": "listen_start", "device_id": dev, "id": "twx-obs"})
|
||||
msg({"type": "listen_rapid_start", "device_id": dev, "id": "twx-rapid"})
|
||||
|
||||
s.settimeout(None)
|
||||
frag = b""
|
||||
last = 0
|
||||
while True:
|
||||
while len(buf) < 2:
|
||||
buf += recv_more(s)
|
||||
b1, b2 = buf[0], buf[1]
|
||||
n = b2 & 0x7F
|
||||
off = 2
|
||||
if n == 126:
|
||||
while len(buf) < 4: buf += recv_more(s)
|
||||
n = struct.unpack(">H", buf[2:4])[0]; off = 4
|
||||
elif n == 127:
|
||||
while len(buf) < 10: buf += recv_more(s)
|
||||
n = struct.unpack(">Q", buf[2:10])[0]; off = 10
|
||||
if n > 1 << 20:
|
||||
die(1)
|
||||
while len(buf) < off + n:
|
||||
buf += recv_more(s)
|
||||
payload, buf = buf[off:off + n], buf[off + n:]
|
||||
op = b1 & 0x0F
|
||||
if op == 8:
|
||||
die(1)
|
||||
if op == 9:
|
||||
send(10, payload); continue
|
||||
if op == 10:
|
||||
continue
|
||||
if op in (1, 0):
|
||||
frag += payload
|
||||
if not (b1 & 0x80):
|
||||
continue
|
||||
text, frag = frag, b""
|
||||
try:
|
||||
d = json.loads(text.decode())
|
||||
except ValueError:
|
||||
continue
|
||||
t = d.get("type")
|
||||
if t == "rapid_wind" and isinstance(d.get("ob"), list):
|
||||
out = {"t": "rapid", "ob": d["ob"]}
|
||||
elif t == "obs_st" and isinstance(d.get("obs"), list) and d["obs"]:
|
||||
out = {"t": "obs", "ob": d["obs"][0]}
|
||||
else:
|
||||
continue
|
||||
sys.stdout.write(json.dumps(out, separators=(",", ":")) + "\n")
|
||||
sys.stdout.flush()
|
||||
|
||||
def recv_more(s):
|
||||
d = s.recv(65536)
|
||||
if not d:
|
||||
die(1)
|
||||
return d
|
||||
|
||||
try:
|
||||
main()
|
||||
except SystemExit:
|
||||
raise
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
Reference in new issue
Block a user