Commit 47dcde15 authored by Nico Schottelius's avatar Nico Schottelius

Update map on connect

parent 5a98367e
......@@ -33,7 +33,6 @@ def db_notify(provider, payload='', deveui=''):
log.error("DB Notify failed: %s" % e)
def db_insert_json(provider, data, payload='', deveui=''):
try:
conn = psycopg2.connect("dbname={}".format(dbname))
cursor = conn.cursor()
......@@ -44,6 +43,7 @@ def db_insert_json(provider, data, payload='', deveui=''):
except Exception as e:
log.error("DB Insert failed: %s" % e)
def jsonToDict(data):
return json.loads(data)
......@@ -55,8 +55,8 @@ def nodered_from_stdin():
time.sleep(0.1)
def nodered_send(provider, data):
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
def nodered_send(path, data):
ws = websocket.create_connection("ws://localhost:1880/{}".format(path))
ws.send("%s" % data)
ws.close()
......@@ -86,3 +86,31 @@ def pg_wait_for_pkg(conns, callback):
log.debug("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
callback(notify.channel, notify.payload)
class DB(object):
def __init__(query):
self.query = query
@classmethod
def gps_query(cls, since="1 day"):
return csl("select payload from packets where payload like 'lat%' and received_dt > NOW() - '{}'::INTERVAL".format(since))
def __iter__(self):
try:
self.conn = psycopg2.connect("dbname={}".format(dbname))
self.cursor = conn.cursor()
self.cursor.execute(self.query)
except Exception as e:
log.error("DB query failed: %s" % e)
raise
return self
def __next__(self):
data = self.cursor.fetchone()
if not data:
self.conn.close()
raise StopIteration
return data
......@@ -13,20 +13,28 @@ import json
import lorautil
import logging
logging.basicConfig(format='%(levelname)s: %(message)s')
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = self.rfile.read(length).decode('utf-8')
print(post_data)
try:
if json.loads(post_data)['action'] == "connected":
self.send_24h_gps()
# And insert into the db
#lorautil.db_insert_json("swisscom", post_data, payload, deveui)
#lorautil.db_notify("swisscom", payload, deveui)
except Exception as e:
log.error("Got unknown request: {} {}".format(post_data, e))
def send_24h_gps(self):
db = lorautil.DB.gps_query()
for record in db:
lorautil.nodered_send("gps-plain", record)
if __name__ == '__main__':
log = logging.getLogger(__name__)
server_address = ('0.0.0.0', 1900)
httpd = HTTPServer(server_address, RequestHandler)
httpd.serve_forever()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment