From 2e234cc8c81d8d03731b1593135ede18af404b87 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sat, 25 Mar 2023 13:53:24 -0500 Subject: add airgradient endpoint --- server.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server.py b/server.py index 5a46c17..d6aeb9a 100644 --- a/server.py +++ b/server.py @@ -143,6 +143,18 @@ class TrackerHTTPRequestHandler(BaseHTTPRequestHandler): self.send_response(204) self.end_headers() self.wfile.write(b"") + elif u.path == "/sensors/airgradient/measures": + chip_id = post_data.pop("chip_id") + co2 = post_data.pop("rco2") + pm25 = post_data.pop("pm02") + temp = post_data.pop("atmp") + rhum = post_data.pop("rhum") + with conn.cursor() as cur: + cur.execute("INSERT INTO airgradient_measure (created, chip_id, co2, pm25, temp, rhum) VALUES (NOW(), %s, %s, %s, %s, %s)", (chip_id, co2, pm25, temp, rhum)) + conn.commit() + self.send_response(204) + self.end_headers() + self.wfile.write(b"") else: self.send_response(404) self.end_headers() @@ -159,6 +171,7 @@ def setup_db(): cur.execute("CREATE TABLE IF NOT EXISTS book_datapoint (id SERIAL PRIMARY KEY, created TIMESTAMP, book_id SERIAL, pages TEXT, CONSTRAINT fk_book FOREIGN KEY(book_id) REFERENCES book(id));") cur.execute("CREATE TABLE IF NOT EXISTS email (id SERIAL PRIMARY KEY, created TIMESTAMP, username TEXT, domain TEXT);") cur.execute("CREATE TABLE IF NOT EXISTS form (id SERIAL PRIMARY KEY, type TEXT, prompt TEXT, prompt_id TEXT, extra JSON);") + cur.execute("CREATE TABLE IF NOT EXISTS airgradient_measure (id SERIAL PRIMARY KEY, created TIMESTAMP, chip_id INTEGER, co2 INTEGER, pm25 INTEGER, temp REAL, rhum INTEGER);") conn.commit() date_pattern = re.compile(r"(\d{4})-(\d{2})-(\d{2})") -- cgit v1.2.3