".encode()
yield "device is required.".encode()
return
try:
latitude = float(args["lat"][0].replace(",", "."))
longitude = float(args["lon"][0].replace(",", "."))
except Exception:
re("400 Bad Request", [])
yield "
400 Bad Request
".encode()
yield "lat and lon are required.".encode()
return
try:
altitude = float(args["alt"][0].replace(",", "."))
except:
altitude = None
try:
timestamp = datetime.datetime.fromtimestamp(float(args["t"][0]) / 1000)
except:
timestamp = datetime.datetime.now()
timestr = timestamp.strftime('%Y-%m-%d %H:%M:%S')
conn, cur = getDatabase()
sql = "INSERT INTO tracker(ts, device, lat, lon, alt) VALUES (%s, %s, %s, %s, %s);"
cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None))
conn.commit()
cur.close()
conn.close()
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 "
400 Bad Request
".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()
cur.close()
conn.close()
re("200 OK", [["Content-Type", "text/html"]])
yield ("""
Current Location
My location at %s
(last known position where I had a GPS signal, a network connection, and some battery power)
""" % (row["ts"], row["lat"], row["lon"], row["lat"], row["lon"])).encode()
return
if env["PATH_INFO"] in ("/access", "/access.php", "/export", "/export.php"):
try:
device = args["device"][0]
except:
re("400 Bad Request", [])
yield "
400 Bad Request
".encode()
yield "device is required.".encode()
return
on = args["on"][0] if "on" in args else None
since = args["since"][0] if "since" in args else None
frm = since if since else on if on else args["from"][0] if "from" in args else None
to = None if since else on if on else args["to"][0] if "to" in args else None
if not "format" in args or args["format"][0] in ("json", "gjson", "geojson", "gj") or not args["format"]:
builder = buildGJSON
elif args["format"][0] == "gpx":
builder = buildGPX
elif args["format"][0] == "kml":
builder = buildKML
else:
re("400 Bad Request", [])
yield "
400 Bad Request
".encode()
yield "Unknown format: %s" % args["format"]
return
frm = frm or "2000-01-01"
to = to or datetime.datetime.now().strftime('%Y-%m-%d')
for time in frm, to:
try:
datetime.datetime.strptime(time, "%Y-%m-%d")
except:
re("400 Bad Request", [])
yield "
400 Bad Request
".encode()
yield "Dates must be in YYYY-MM-DD format.".encode()
return
conn, cur = getDatabase()
sql = "SELECT * FROM tracker WHERE device=%s AND DATE(ts)>=%s and DATE(ts)<=%s ORDER BY ts ASC;";
cur.execute(sql, (device, frm, to))
data = cur.fetchall()
headers, output = builder(data)
re("200 OK", headers)
cur.close()
conn.close()
yield output.encode()
return