Move stuff into library, finish adeunisrf
This commit is contained in:
parent
acf5f05b81
commit
fe535323d3
4 changed files with 113 additions and 45 deletions
|
|
@ -55,17 +55,77 @@
|
||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
|
|
||||||
|
# L A T I T U D E L O N G I TU DE
|
||||||
|
# status temp deg min sec +hem deg min min min
|
||||||
|
# 9e 17 46 58 204 0 009 02 320 a5 9a 0e e7
|
||||||
|
# 1 2 3 4 5 6 ? 8 9 10 11 12 13 14
|
||||||
|
|
||||||
def decode_pkg(pkg):
|
|
||||||
return binascii.a2b_hex(s)
|
|
||||||
|
|
||||||
def has_gps(pkg):
|
# on display: 46 58 122N 09 02 207E
|
||||||
return pkg[0] & 0x10
|
# 46.58 204 09.02 320
|
||||||
|
# sodaq: lat=46.9699219 lon=9.0391764
|
||||||
|
# google: 46.969943, 9.038999
|
||||||
|
|
||||||
def gpsdata(pkg):
|
known_devices = [ "0018B20000000C58", "0018B20000000C37", "0018B20000000C59", "0018B20000000CD0" ]
|
||||||
if not has_gps(pkg):
|
|
||||||
return (0,0)
|
|
||||||
|
def get_gps(deveui, payload):
|
||||||
|
res = []
|
||||||
|
if not payload[0] & (2**7):
|
||||||
|
return res
|
||||||
|
|
||||||
|
lat_deg = float(payload[4:6])
|
||||||
|
lat_min = float(payload[6:8])
|
||||||
|
lat_sec = float(payload[8:10])
|
||||||
|
lat_frac_sec = float(payload[10:11])
|
||||||
|
lat = lat_deg + lat_min/60.0 + lat_sec/3600.0 + lat_frac_sec/36000.0
|
||||||
|
|
||||||
|
long_deg = float(payload[12:15])
|
||||||
|
long_min = float(payload[15:17])
|
||||||
|
long_sec = float(payload[17:19])
|
||||||
|
long_frac_sec = float(payload[19:20])
|
||||||
|
long = long_deg + long_min/60.0 + long_sec/3600 + long_frac_sec/36000.0
|
||||||
|
|
||||||
|
pos = ":lat={:.6f} long={:.6f}".format(lat, long)
|
||||||
|
|
||||||
|
res = [ deveui + pos ]
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
def get_temp(deveui, payload):
|
||||||
|
res = []
|
||||||
|
if payload[0] & (2**7):
|
||||||
|
res = [ deveui + ":temperature=" + str(binascii.a2b_hex(payload)[1])]
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
def decode_adeunis(pkg):
|
||||||
|
data = pkg.split(":")
|
||||||
|
|
||||||
|
deveui = data[0]
|
||||||
|
payload = data[1]
|
||||||
|
|
||||||
|
res = []
|
||||||
|
|
||||||
|
# Only handle known devices
|
||||||
|
if not deveui in known_devices:
|
||||||
|
return res
|
||||||
|
|
||||||
|
res += get_temp(deveui, payload)
|
||||||
|
res += get_gps(deveui, payload)
|
||||||
|
|
||||||
|
def nodered_adeunisrf(provider, data):
|
||||||
|
res = decode_adeunis(data)
|
||||||
|
if not res:
|
||||||
|
return
|
||||||
|
|
||||||
|
with websocket.create_connection("ws://localhost:1880/{}".format(provider)) as ws:
|
||||||
|
for d in res:
|
||||||
|
ws.send("%s" % d)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
decode_pkg()
|
conns = lorautil.pg_conn_notify()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pg_wait_for_pkg(conns, nodered_adeunisrf)
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,18 @@ import psycopg2
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import select
|
||||||
|
import psycopg2
|
||||||
|
import psycopg2.extensions
|
||||||
|
import websocket
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger("lorautil")
|
log = logging.getLogger("lorautil")
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
dbname="lorawan"
|
dbname="lorawan"
|
||||||
|
|
||||||
|
|
||||||
def db_notify(provider, payload='', deveui=''):
|
def db_notify(provider, payload='', deveui=''):
|
||||||
notify="{}:{}".format(deveui, payload)
|
notify="{}:{}".format(deveui, payload)
|
||||||
log.debug("Notify: {} {}".format(provider, notify))
|
log.debug("Notify: {} {}".format(provider, notify))
|
||||||
|
|
@ -38,3 +45,34 @@ def db_insert_json(provider, data, payload='', deveui=''):
|
||||||
|
|
||||||
def jsonToDict(data):
|
def jsonToDict(data):
|
||||||
return json.loads(data)
|
return json.loads(data)
|
||||||
|
|
||||||
|
|
||||||
|
def nodered_send(provider, data):
|
||||||
|
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
|
||||||
|
ws.send("%s" % data)
|
||||||
|
ws.close()
|
||||||
|
|
||||||
|
channels = [ "loriot", "swisscom", "ttn" ]
|
||||||
|
|
||||||
|
def pg_conn_notify():
|
||||||
|
conns = []
|
||||||
|
for channel in channels:
|
||||||
|
conn = psycopg2.connect("dbname={}".format(dbname))
|
||||||
|
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
|
|
||||||
|
curs = conn.cursor()
|
||||||
|
curs.execute("LISTEN {};".format(channel))
|
||||||
|
|
||||||
|
conns.append(conn)
|
||||||
|
log.debug("Waiting for notifications on channel {}".format(channel))
|
||||||
|
|
||||||
|
def pg_wait_for_pkg(conns, callback):
|
||||||
|
readable, writable, exceptional = select.select(conns,[],[])
|
||||||
|
|
||||||
|
for conn in readable:
|
||||||
|
conn.poll()
|
||||||
|
while conn.notifies:
|
||||||
|
notify = conn.notifies.pop(0)
|
||||||
|
log.debug("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
|
||||||
|
|
||||||
|
callback(notify.channel, notify.payload)
|
||||||
|
|
|
||||||
|
|
@ -5,44 +5,14 @@
|
||||||
# 2016-11-02
|
# 2016-11-02
|
||||||
# GPLv3+
|
# GPLv3+
|
||||||
|
|
||||||
import select
|
|
||||||
import psycopg2
|
|
||||||
import psycopg2.extensions
|
|
||||||
import websocket
|
|
||||||
|
|
||||||
import adeunisrf
|
import lorautil
|
||||||
|
|
||||||
channels = [ "loriot", "swisscom", "ttn" ]
|
log = logging.getLogger("notify")
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
def to_nodered(provider, data):
|
|
||||||
ws = websocket.create_connection("ws://localhost:1880/{}".format(provider))
|
|
||||||
ws.send("%s" % data)
|
|
||||||
ws.close()
|
|
||||||
|
|
||||||
def wait_for_pkg(conns):
|
|
||||||
|
|
||||||
readable, writable, exceptional = select.select(conns,[],[])
|
|
||||||
|
|
||||||
for conn in readable:
|
|
||||||
conn.poll()
|
|
||||||
while conn.notifies:
|
|
||||||
notify = conn.notifies.pop(0)
|
|
||||||
print("Got NOTIFY: {} {} {}".format(notify.pid, notify.channel, notify.payload))
|
|
||||||
to_nodered(notify.channel, notify.payload)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
conns = []
|
conns = lorautil.pg_conn_notify()
|
||||||
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:
|
while True:
|
||||||
wait_for_pkg(conns)
|
pg_wait_for_pkg(conns, lorautil.nodered_send)
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
print("deveui/payload: {}:{}".format(deveui, payload))
|
f print("deveui/payload: {}:{}".format(deveui, payload))
|
||||||
|
|
||||||
# And insert into the db
|
# And insert into the db
|
||||||
lorautil.db_insert_json("swisscom", post_data, payload, deveui)
|
lorautil.db_insert_json("swisscom", post_data, payload, deveui)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue