lorawan/python/lorautil.py

41 lines
1.1 KiB
Python
Raw Normal View History

2016-11-02 16:10:07 +00:00
# Helper functions for various Lora receivers
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02
# GPLv3+
2016-11-02 16:13:04 +00:00
import psycopg2
2016-11-02 16:22:43 +00:00
import json
2016-11-02 16:42:39 +00:00
import logging
log = logging.getLogger("lorautil")
log.setLevel(logging.DEBUG)
2016-11-02 16:13:04 +00:00
2016-11-02 16:10:07 +00:00
dbname="lorawan"
def db_notify(provider, payload='', deveui=''):
notify="{}:{}".format(deveui, payload)
2016-11-02 16:42:39 +00:00
log.debug("Notify: {} {}".format(provider, notify))
2016-11-02 16:10:07 +00:00
try:
conn = psycopg2.connect("dbname={}".format(dbname))
cursor = conn.cursor()
cursor.execute("select pg_notify (%s, %s)", (provider, notify))
cursor.connection.commit()
except Exception as e:
2016-11-02 16:42:39 +00:00
log.error("DB Notify failed: %s" % e)
2016-11-02 16:10:07 +00:00
def db_insert_json(provider, data, payload='', deveui=''):
2016-11-02 16:42:39 +00:00
2016-11-02 16:10:07 +00:00
try:
conn = psycopg2.connect("dbname={}".format(dbname))
cursor = conn.cursor()
cursor.execute("insert into packets values (DEFAULT, DEFAULT, %s, %s, %s, %s)", (provider, data, payload, deveui))
cursor.connection.commit()
conn.close()
except Exception as e:
2016-11-02 16:42:39 +00:00
log.error("DB Insert failed: %s" % e)
2016-11-02 16:22:43 +00:00
2016-11-02 16:44:01 +00:00
def jsonToDict(data):
2016-11-02 16:22:43 +00:00
return json.loads(data)