116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
import sqlite3, json, time, os
|
|
from flask import Flask, render_template, request, Response, redirect, url_for
|
|
|
|
DB_PATH = "/home/naue/rtk/rtk_rover.db"
|
|
CMD_FILE = "/home/naue/rtk/rtk_rover_cmd.json"
|
|
|
|
app = Flask(__name__)
|
|
|
|
def db():
|
|
con = sqlite3.connect(DB_PATH)
|
|
con.row_factory = sqlite3.Row
|
|
return con
|
|
|
|
def write_cmd(cmd):
|
|
tmp = CMD_FILE + ".tmp"
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
|
json.dump({"cmd": cmd, "note": "web", "ts": time.time()}, f)
|
|
os.replace(tmp, CMD_FILE)
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def index():
|
|
con = db()
|
|
sessions = con.execute("""
|
|
SELECT session_id,
|
|
datetime(start_ts,'unixepoch','localtime') AS start_time,
|
|
CASE WHEN end_ts IS NULL THEN '' ELSE datetime(end_ts,'unixepoch','localtime') END AS end_time,
|
|
note,
|
|
end_ts
|
|
FROM sessions
|
|
ORDER BY start_ts DESC
|
|
LIMIT 50
|
|
""").fetchall()
|
|
|
|
active_session = con.execute("""
|
|
SELECT session_id, datetime(start_ts,'unixepoch','localtime') AS start_time, note
|
|
FROM sessions
|
|
WHERE end_ts IS NULL
|
|
ORDER BY start_ts DESC
|
|
LIMIT 1
|
|
""").fetchone()
|
|
|
|
session_id = request.args.get("session_id") or (sessions[0]["session_id"] if sessions else "")
|
|
only_fix = request.args.get("only_fix", "0") == "1"
|
|
|
|
params = []
|
|
where = ""
|
|
if session_id:
|
|
where += " WHERE session_id=?"
|
|
params.append(session_id)
|
|
if only_fix:
|
|
where += " AND carr_soln=2" if where else " WHERE carr_soln=2"
|
|
|
|
points = con.execute(f"""
|
|
SELECT datetime(unix_ts,'unixepoch','localtime') AS t,
|
|
lat, lon, hmsl_corr_m, hacc_m, vacc_m, carr_soln, num_sats, source
|
|
FROM gnss_pvt
|
|
{where}
|
|
ORDER BY unix_ts DESC
|
|
LIMIT 200
|
|
""", params).fetchall()
|
|
|
|
count = con.execute(f"SELECT COUNT(*) AS n FROM gnss_pvt {where}", params).fetchone()["n"]
|
|
|
|
return render_template("index.html",
|
|
sessions=sessions,
|
|
session_id=session_id,
|
|
only_fix=only_fix,
|
|
points=points,
|
|
count=count,
|
|
active_session=active_session)
|
|
|
|
@app.route("/record/start", methods=["POST"])
|
|
def record_start():
|
|
write_cmd("start")
|
|
return redirect(url_for("index"))
|
|
|
|
@app.route("/record/stop", methods=["POST"])
|
|
def record_stop():
|
|
write_cmd("stop")
|
|
return redirect(url_for("index"))
|
|
|
|
@app.route("/export.csv")
|
|
def export_csv():
|
|
session_id = request.args.get("session_id")
|
|
only_fix = request.args.get("only_fix", "0") == "1"
|
|
|
|
params = []
|
|
where = ""
|
|
if session_id:
|
|
where += " WHERE session_id=?"
|
|
params.append(session_id)
|
|
if only_fix:
|
|
where += " AND carr_soln=2" if where else " WHERE carr_soln=2"
|
|
|
|
con = db()
|
|
rows = con.execute(f"""
|
|
SELECT datetime(unix_ts,'unixepoch','localtime') AS t,
|
|
lat, lon, hmsl_m, hmsl_corr_m, hacc_m, vacc_m,
|
|
fix_type, carr_soln, diff_soln, gnss_ok, num_sats, source
|
|
FROM gnss_pvt
|
|
{where}
|
|
ORDER BY unix_ts ASC
|
|
""", params).fetchall()
|
|
|
|
def generate():
|
|
yield "time,lat,lon,hmsl_m,hmsl_corr_m,hacc_m,vacc_m,fix_type,carr_soln,diff_soln,gnss_ok,num_sats,source\n"
|
|
for r in rows:
|
|
yield ",".join(str(r[k]) if r[k] is not None else "" for k in r.keys()) + "\n"
|
|
|
|
return Response(generate(), mimetype="text/csv",
|
|
headers={"Content-Disposition": "attachment; filename=rtk_export.csv"})
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=8080)
|