diff --git a/notify.py b/notify.py index 772454f..b4734a2 100644 --- a/notify.py +++ b/notify.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + # Send lora packet to node-red when being triggered by postgresql # Nico Schottelius # 2016-11-02 diff --git a/receiver/lorautil.py b/receiver/lorautil.py index 41101eb..e8db208 100644 --- a/receiver/lorautil.py +++ b/receiver/lorautil.py @@ -4,6 +4,7 @@ # GPLv3+ import psycopg2 +import json dbname="lorawan" @@ -28,3 +29,6 @@ def db_insert_json(provider, data, payload='', deveui=''): conn.close() except Exception as e: print("DB Insert failed: %s" % e) + +def jsonToDict(self, data): + return json.loads(data) diff --git a/receiver/loriot-receiver.py b/receiver/loriot-receiver.py index 0224a41..12b92d9 100644 --- a/receiver/loriot-receiver.py +++ b/receiver/loriot-receiver.py @@ -14,21 +14,14 @@ class Loriot(): tokenurl = os.environ['LORIOT_URL'] self.ws = websocket.create_connection(tokenurl, sslopt={"cert_reqs": ssl.CERT_NONE}) - def listen(self): while True: result = self.ws.recv() print(result) - jdata = self.jsonToDict(result) + jdata = lorautil.jsonToDict(result) eui = self.devEUI(jdata) - self.insert_json("loriot", result, deveui=eui) - - # payload = self.get_payload(jdata) - - - def jsonToDict(self, data): - return json.loads(data) + lorautil.insert_json("loriot", result, deveui=eui) def devEUI(self, data): return data['EUI'] @@ -37,17 +30,6 @@ class Loriot(): return "" # return data['EUI'] - def insert_json(self, provider, data, payload='', deveui=''): - try: - conn = psycopg2.connect("dbname=lorawan") - 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: - print("DB Insert failed: %s" % e) - - if __name__ == '__main__': l = Loriot() l.connect()