Implement current location getter, make compatible with kumitterer/gpstools, not implementing authentication at this point

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-12 14:44:38 +02:00
parent 444dc50f59
commit 663665399d

54
main.py
View file

@ -14,7 +14,7 @@ def getDatabase(path = "config.cfg"):
name = config.get("Database", "name")
conn = pymysql.connect(host, user, pwd, name)
cur = conn.cursor()
cur = conn.cursor(pymysql.cursors.DictCursor)
return conn, cur
@ -27,18 +27,19 @@ def application(env, re):
re("405 Method Not Allowed", [])
return
if env["PATH_INFO"] == "/endpoint":
if env["PATH_INFO"] in ("/endpoint", "/endpoint.php"):
try:
device = args["device"][0]
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "device is required.".encode()
return
try:
latitude = float(args["lat"][0].replace(",", "."))
longitude = float(args["lon"][0].replace(",", "."))
except Exception as e:
except Exception:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "lat and lon are required.".encode()
@ -61,4 +62,49 @@ def application(env, re):
cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None))
conn.commit()
re("200 OK", [])
yield "OK".encode()
return
if env["PATH_INFO"] in ("/location", "/location.php"):
try:
device = args["device"][0]
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "device is required.".encode()
return
conn, cur = getDatabase()
cur.execute("SELECT * FROM tracker WHERE device = %s ORDER BY ts DESC LIMIT 1;", device)
row = cur.fetchone()
re("200 OK", [["Content-Type", "text/html"]])
yield ("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>Current Location</title>
<link rel="stylesheet" href="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.css" />
<script src="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js"></script>
</head>
<body>
<h1>My location at %s</h1>
<p>(last known position where I had a GPS signal, a network connection, and some battery power)</p>
<div id="map" style="height:500px;"></div>
<script>
var mymap = L.map("map").setView([%s, %s], 12);
L.tileLayer("https://b.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(mymap);
var marker = L.marker([%s, %s]).addTo(mymap);
</script>
</body>
</html>""" % (row["ts"], row["lat"], row["lon"], row["lat"], row["lon"])).encode()
return