From dcbeae371f149db313f90df8d1031ace379bdd0c Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Thu, 20 Jul 2023 21:40:04 -0500 Subject: add post with query string for automation --- server.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'server.py') diff --git a/server.py b/server.py index d6aeb9a..f2a9173 100644 --- a/server.py +++ b/server.py @@ -98,8 +98,11 @@ class TrackerHTTPRequestHandler(BaseHTTPRequestHandler): def do_POST(self): u = urlparse(self.path) - length = int(self.headers['Content-Length']) - post_data = json.loads(self.rfile.read(length).decode('utf-8')) + try: + length = int(self.headers['Content-Length']) + post_data = json.loads(self.rfile.read(length).decode('utf-8')) + except: + post_data = {} print("POST", u.path) if u.path == "/submit_payload": timestamp = datetime.fromtimestamp(int(post_data["timestamp"]/1000)) @@ -155,6 +158,17 @@ class TrackerHTTPRequestHandler(BaseHTTPRequestHandler): self.send_response(204) self.end_headers() self.wfile.write(b"") + elif u.path == "/automation": + created = datetime.now() + q = parse_qs(u.query) + if "lat" in q and "lon" in q: + with conn.cursor() as cur: + cur.execute("INSERT INTO datapoint (datatype, key, value, created) VALUES (%s, %s, %s, %s)", ("geoposition", "latitude", q["lat"], created)) + cur.execute("INSERT INTO datapoint (datatype, key, value, created) VALUES (%s, %s, %s, %s)", ("geoposition", "longitude", q["lon"], created)) + conn.commit() + self.send_response(204) + self.end_headers() + self.wfile.write(b"") else: self.send_response(404) self.end_headers() -- cgit v1.2.3