kiwi-longtime-waterfall/record_wf.py

83 lines
2.2 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import websocket
import time
import numpy as np
import datetime
import os
last_date = None
logfile = None
os.makedirs("logs", exist_ok=True)
def write_log(dt, line):
global last_date, logfile
date = dt.date()
# reopen log file if day has changed
if last_date != date:
if logfile:
logfile.close()
logname = os.path.join("logs", date.strftime("%Y-%m-%d.csv"))
print(f"(Re-)opening logfile: {logname}")
logfile = open(logname, "a")
last_date = date
if line[-1] != '\n':
line += '\n'
logfile.write(line)
last_keepalive = datetime.datetime.now()
def on_open(wsapp):
wsapp.send("SET auth t=kiwi p=#")
wsapp.send("SERVER DE CLIENT openwebrx.js W/F")
wsapp.send("SET send_dB=1")
wsapp.send("SET zoom=0 start=0")
wsapp.send("SET maxdb=0 mindb=-100")
wsapp.send("SET interp=13")
wsapp.send("SET wf_comp=0")
wsapp.send("SET wf_speed=1")
#wsapp.send("SET MARKER min=0.000 max=30000.000 zoom=0 width=1904")
#wsapp.send("SET aper=1 algo=3 param=0.00")
def on_message(wsapp, message):
global last_keepalive
msgtype = message[:3].decode('ascii')
msgdata = message[4:]
now = datetime.datetime.now()
if msgtype == 'MSG':
msgstr = msgdata.decode('utf-8')
print(f"Status message: {msgstr}")
elif msgtype == 'W/F':
#print(msgdata)
print(f"Received waterfall data: {len(msgdata)} bytes.")
#print(f"Header: {msgdata[:12]}")
specdata = msgdata[12:]
spectrum = np.frombuffer(specdata, dtype=np.uint8).astype(int)
spectrum = spectrum - 255 # converts to dBm
logline = f"{now.timestamp():.3f};" + ";".join([f"{dBm:d}" for dBm in spectrum]) + "\n"
write_log(now, logline)
if now - last_keepalive > datetime.timedelta(seconds=10):
wsapp.send("SET keepalive")
last_keepalive = now
else:
print(f"Unknown type: {msgtype} data: {msgdata}")
url = f"ws://192.168.75.14:8073/kiwi/{int(time.time())}/W/F"
wsapp = websocket.WebSocketApp(url,
on_message=on_message,
on_open=on_open)
wsapp.run_forever()