lorawan/python/loriot-receiver.py

44 lines
1 KiB
Python
Raw Normal View History

2016-10-22 20:38:24 +00:00
#!/usr/bin/env python3
import ssl
import websocket
import json
import os
import psycopg2
2016-11-02 17:36:03 +00:00
import lorautil
2016-10-22 20:38:24 +00:00
class Loriot():
def __init__(self):
ws = websocket.WebSocket()
def connect(self):
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)
2016-11-02 16:22:43 +00:00
jdata = lorautil.jsonToDict(result)
eui = self.devEUI(jdata)
2016-11-02 17:27:49 +00:00
try:
payload = get_payload(jdata)
except Exception:
payload = ""
lorautil.db_insert_json("loriot", result, payload=payload, deveui=eui)
2016-11-02 17:37:54 +00:00
lorautil.db_notify("loriot", payload=payload, deveui=eui)
2016-10-22 20:38:24 +00:00
def devEUI(self, data):
return data['EUI']
def get_payload(self, data):
2016-11-02 17:27:49 +00:00
return bytes.fromhex(data['payload']).decode('utf-8')
2016-10-22 20:38:24 +00:00
if __name__ == '__main__':
l = Loriot()
l.connect()
l.listen()