Move stuff into library, finish adeunisrf

This commit is contained in:
Nico Schottelius 2016-11-12 17:44:06 +01:00
commit fe535323d3
4 changed files with 113 additions and 45 deletions

View file

@ -5,44 +5,14 @@
# 2016-11-02
# GPLv3+
import select
import psycopg2
import psycopg2.extensions
import websocket
import adeunisrf
import lorautil
channels = [ "loriot", "swisscom", "ttn" ]
def to_nodered(provider, data):
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
ws.send("%s" % data)
ws.close()
def wait_for_pkg(conns):
readable, writable, exceptional = select.select(conns,[],[])
for conn in readable:
conn.poll()
while conn.notifies:
notify = conn.notifies.pop(0)
print("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
to_nodered(notify.channel, notify.payload)
log = logging.getLogger("notify")
log.setLevel(logging.DEBUG)
if __name__ == '__main__':
conns = []
for channel in channels:
conn = psycopg2.connect("dbname=lorawan")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute("LISTEN {};".format(channel))
conns.append(conn)
print("Waiting for notifications on channel {}".format(channel))
conns = lorautil.pg_conn_notify()
while True:
wait_for_pkg(conns)
pg_wait_for_pkg(conns, lorautil.nodered_send)