Update map on connect

This commit is contained in:
Nico Schottelius 2016-11-15 16:39:12 +01:00
parent 5a98367e43
commit 47dcde1562
2 changed files with 43 additions and 7 deletions

View file

@ -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

View file

@ -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()