Begin to separate code
This commit is contained in:
parent
98213ec000
commit
5e122197da
4 changed files with 58 additions and 97 deletions
36
notify.py
36
notify.py
|
@ -1,21 +1,39 @@
|
|||
import select
|
||||
import psycopg2
|
||||
import psycopg2.extensions
|
||||
import websocket
|
||||
|
||||
channel = "lora"
|
||||
channels = [ "loriot", "swisscom", "ttn" ]
|
||||
|
||||
conn = psycopg2.connect("dbname=lorawan")
|
||||
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
def to_nodered(self, data):
|
||||
dev = self.devEUI(data)
|
||||
text = self.payload(data)
|
||||
|
||||
curs = conn.cursor()
|
||||
curs.execute("LISTEN {};".format(channel))
|
||||
ws = websocket.create_connection("ws://localhost:1880/")
|
||||
ws.send("%s" % data)
|
||||
ws.close()
|
||||
|
||||
print("Waiting for notifications on channel {}".format(channel))
|
||||
while True:
|
||||
if select.select([conn],[],[]) == ([],[],[]):
|
||||
print("Timeout")
|
||||
def wait_for_pkg(conns):
|
||||
if select.select(conns,[],[]) == ([],[],[]):
|
||||
print("Select error")
|
||||
else:
|
||||
conn.poll()
|
||||
while conn.notifies:
|
||||
notify = conn.notifies.pop(0)
|
||||
print("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
|
||||
|
||||
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:
|
||||
wait_for_pkg(conns):
|
||||
|
|
28
receiver/lorautil.py
Normal file
28
receiver/lorautil.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Helper functions for various Lora receivers
|
||||
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
|
||||
# 2016-11-02
|
||||
# GPLv3+
|
||||
|
||||
dbname="lorawan"
|
||||
|
||||
def db_notify(provider, payload='', deveui=''):
|
||||
notify="{}:{}".format(deveui, payload)
|
||||
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:
|
||||
print("DB Notify failed: %s" % e)
|
||||
|
||||
def db_insert_json(provider, data, payload='', deveui=''):
|
||||
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:
|
||||
print("DB Insert failed: %s" % e)
|
|
@ -7,24 +7,10 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
|
|||
import re
|
||||
import json
|
||||
import pprint
|
||||
import lorautil
|
||||
|
||||
# HTTPRequestHandler class
|
||||
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
||||
# not used, just a sample
|
||||
def do_GET(self):
|
||||
# Send response status code
|
||||
self.send_response(200)
|
||||
|
||||
# Send headers
|
||||
self.send_header('Content-type','text/html')
|
||||
self.end_headers()
|
||||
|
||||
# Send message back to client
|
||||
message = "Hello world!"
|
||||
# Write content as utf-8 data
|
||||
self.wfile.write(bytes(message, "utf8"))
|
||||
return
|
||||
|
||||
def do_POST(self):
|
||||
length = int(self.headers['Content-Length'])
|
||||
post_data = self.rfile.read(length).decode('utf-8')
|
||||
|
@ -42,35 +28,8 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
|||
print("deveui/payload: {}:{}".format(deveui, payload))
|
||||
|
||||
# And insert into the db
|
||||
self.insert_json("swisscom", post_data, payload, deveui)
|
||||
|
||||
# Sendo to dashboard
|
||||
# self.to_dashboard(post_data)
|
||||
|
||||
def to_dashboard(self, data):
|
||||
dev = self.devEUI(data)
|
||||
text = self.payload(data)
|
||||
|
||||
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/data")
|
||||
ws.send("%s" % data)
|
||||
ws.close()
|
||||
|
||||
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/rawmessage")
|
||||
ws.send("%s:%s" % (dev, text))
|
||||
ws.close()
|
||||
|
||||
# Working lora node
|
||||
if dev == "9CD90BB52B6A1D01":
|
||||
try:
|
||||
key, value = text.split("=")
|
||||
print("Trying to send: %s:%s" % (key, value))
|
||||
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/%s" % (key))
|
||||
ws.send("%s" % (value))
|
||||
ws.close()
|
||||
|
||||
except ValueError:
|
||||
print("Cannot split: %s" % (text))
|
||||
|
||||
lorautil.db_insert_json("swisscom", post_data, payload, deveui)
|
||||
lorautil.db_notify("swisscom", payload, deveui)
|
||||
|
||||
def jsonToDict(self, data):
|
||||
return json.loads(data)
|
||||
|
@ -103,27 +62,6 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
|||
myhex = self.payload_hex(data)
|
||||
return bytes.fromhex(myhex).decode('utf-8')
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def insert_xml(self, data):
|
||||
try:
|
||||
conn = psycopg2.connect("dbname=hackzurich")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("insert into data (packet) values (%s)", (data, ))
|
||||
cursor.connection.commit()
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print("DB Insert failed: %s" % e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
server_address = ('0.0.0.0', 8000)
|
||||
|
|
|
@ -35,29 +35,6 @@ def on_log(client,userdata,level,buf):
|
|||
print("message:" + msg)
|
||||
print("userdata:" + str(userdata))
|
||||
|
||||
def insert_json(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)
|
||||
|
||||
notify(payload, deveui)
|
||||
|
||||
def notify(payload='', deveui=''):
|
||||
notify="{}:{}".format(deveui, payload)
|
||||
try:
|
||||
conn = psycopg2.connect("dbname=lorawan")
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("select pg_notify ('lora', %s)", (notify, ))
|
||||
cursor.connection.commit()
|
||||
except Exception as e:
|
||||
print("DB Notify failed: %s" % e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue