Begin to implement notify via postgresql
This commit is contained in:
parent
a48c5f7f71
commit
3e325a5c6a
2 changed files with 26 additions and 0 deletions
19
notify.py
Normal file
19
notify.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
import select
|
||||
import psycopg2
|
||||
import psycopg2.extensions
|
||||
|
||||
conn = psycopg2.connect("dbname=lorawan")
|
||||
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
|
||||
curs = conn.cursor()
|
||||
curs.execute("LISTEN test;")
|
||||
|
||||
print("Waiting for notifications on channel test")
|
||||
while True:
|
||||
if select.select([conn],[],[]) == ([],[],[]):
|
||||
print("Timeout")
|
||||
else:
|
||||
conn.poll()
|
||||
while conn.notifies:
|
||||
notify = conn.notifies.pop(0)
|
||||
print("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
|
|
@ -4,6 +4,9 @@
|
|||
# www.schimmel-bisolutions.nl
|
||||
# first install paho.mqtt.client: pip install paho-mqtt
|
||||
#
|
||||
# Enhanced for lorawan in Digital Glarus by Nico Schottelius (nico.schottelius -at- ungleich.ch)
|
||||
#
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import psycopg2
|
||||
import json
|
||||
|
@ -38,6 +41,10 @@ def insert_json(provider, data, payload='', deveui=''):
|
|||
cursor = conn.cursor()
|
||||
cursor.execute("insert into packets values (DEFAULT, DEFAULT, %s, %s, %s, %s)", (provider, data, payload, deveui))
|
||||
cursor.connection.commit()
|
||||
|
||||
notify="{}/{}".format(deveui, payload)
|
||||
cursor.execute("select pg_notify ('lora', %s)", (notify))
|
||||
cursor.connection.commit()
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print("DB Insert failed: %s" % e)
|
||||
|
|
Loading…
Reference in a new issue