lorawan/python/notify.py

49 lines
1.2 KiB
Python
Raw Normal View History

2016-11-02 16:22:43 +00:00
#!/usr/bin/env python3
2016-11-02 16:15:56 +00:00
# Send lora packet to node-red when being triggered by postgresql
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02
# GPLv3+
import select
import psycopg2
import psycopg2.extensions
2016-11-02 16:10:07 +00:00
import websocket
import adeunisrf
2016-11-02 16:10:07 +00:00
channels = [ "loriot", "swisscom", "ttn" ]
2016-11-02 16:19:17 +00:00
def to_nodered(provider, data):
2016-11-02 16:15:56 +00:00
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
2016-11-02 16:10:07 +00:00
ws.send("%s" % data)
ws.close()
2016-11-02 16:10:07 +00:00
def wait_for_pkg(conns):
2016-11-02 16:55:52 +00:00
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))
2016-11-02 16:15:56 +00:00
to_nodered(notify.channel, notify.payload)
2016-11-02 16:10:07 +00:00
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))
while True:
2016-11-02 16:15:56 +00:00
wait_for_pkg(conns)