Use python three, use lorautil

This commit is contained in:
Nico Schottelius 2016-11-02 17:22:43 +01:00
parent be55ff2d99
commit ef99c3953c
3 changed files with 8 additions and 20 deletions

View file

@ -1,3 +1,5 @@
#!/usr/bin/env python3
# Send lora packet to node-red when being triggered by postgresql # Send lora packet to node-red when being triggered by postgresql
# Nico Schottelius <nico.schottelius -at- ungleich.ch> # Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-02 # 2016-11-02

View file

@ -4,6 +4,7 @@
# GPLv3+ # GPLv3+
import psycopg2 import psycopg2
import json
dbname="lorawan" dbname="lorawan"
@ -28,3 +29,6 @@ def db_insert_json(provider, data, payload='', deveui=''):
conn.close() conn.close()
except Exception as e: except Exception as e:
print("DB Insert failed: %s" % e) print("DB Insert failed: %s" % e)
def jsonToDict(self, data):
return json.loads(data)

View file

@ -14,21 +14,14 @@ class Loriot():
tokenurl = os.environ['LORIOT_URL'] tokenurl = os.environ['LORIOT_URL']
self.ws = websocket.create_connection(tokenurl, sslopt={"cert_reqs": ssl.CERT_NONE}) self.ws = websocket.create_connection(tokenurl, sslopt={"cert_reqs": ssl.CERT_NONE})
def listen(self): def listen(self):
while True: while True:
result = self.ws.recv() result = self.ws.recv()
print(result) print(result)
jdata = self.jsonToDict(result) jdata = lorautil.jsonToDict(result)
eui = self.devEUI(jdata) eui = self.devEUI(jdata)
self.insert_json("loriot", result, deveui=eui) lorautil.insert_json("loriot", result, deveui=eui)
# payload = self.get_payload(jdata)
def jsonToDict(self, data):
return json.loads(data)
def devEUI(self, data): def devEUI(self, data):
return data['EUI'] return data['EUI']
@ -37,17 +30,6 @@ class Loriot():
return "" return ""
# return data['EUI'] # 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__': if __name__ == '__main__':
l = Loriot() l = Loriot()
l.connect() l.connect()