diff --git a/python/adeunisrf.py b/python/adeunisrf.py new file mode 100644 index 0000000..6be1611 --- /dev/null +++ b/python/adeunisrf.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +# Send lora packet to node-red when being triggered by postgresql +# Nico Schottelius +# 2016-11-02 +# GPLv3+ + +# Bit 6 = 1 : accelerometer was triggered +# +# Bit 5 = 0 : BTN1 was NOT triggered +# +# Bit 4 = 1 : GPS info is present +# +# Bit 3 = 1 : MAC Down Counter is present +# +# Bit 2 = 1 : MAC up Counter is present +# +# Bit 1 = 1 : Battery voltage information is present +# +# Bit 0 = 0 : RSSI + SNR information is NOT present +# +# * Byte 02 : 1E = 0001 1110 (Temperature in °C, signed in two’s complement) = 30 °C +# +# * Byte 03 : 47 = GPS Latitude, degrees +# +# * Byte 04 : 22 = GPS Latitude, minutes +# +# * Byte 05 : 49 = GPS Latitude, minutes +# +# * Byte 06 : 60 = GPS Latitude, minutes + Hemisphere (0 = North) +# +# * Byte 07 : 00 = GPS Longitude, minutes +# +# * Byte 08 : 83 = GPS Longitude, minutes +# +# * Byte 09 : 18 = GPS Longitude, minutes +# +# * Byte 10 : 70 = GPS Longitude, minutes + Hemisphere (0 = East) +# +# * Byte 11 : none (because Byte 12 is starting with a 0 and not 1, 2 or 3) +# +# * Byte 12 : 08 = Uplink frame counter +# +# * Byte 13 : 08 = Downlink frame counter +# +# * Byte 14 : 0E = MSB Battery voltage ) +# +# * Byte 15 : FD = LSB Battery voltage ) è 3.837 V +# +# * Byte 16 : none +# +# * Byte 17 : none + +# should be: 46.969943, 9.038999 + +import binascii + + +def decode_pkg(pkg): + return binascii.a2b_hex(s) + +def has_gps(pkg): + return pkg[0] & 0x10 + +def gpsdata(pkg): + if not has_gps(pkg): + return (0,0) + + +if __name__ == '__main__': + decode_pkg() diff --git a/receiver/lorautil.py b/python/lorautil.py similarity index 100% rename from receiver/lorautil.py rename to python/lorautil.py diff --git a/receiver/loriot-receiver.py b/python/loriot-receiver.py similarity index 100% rename from receiver/loriot-receiver.py rename to python/loriot-receiver.py diff --git a/notify.py b/python/notify.py similarity index 98% rename from notify.py rename to python/notify.py index 49a5f4f..280ea80 100644 --- a/notify.py +++ b/python/notify.py @@ -10,8 +10,11 @@ import psycopg2 import psycopg2.extensions import websocket +import adeunisrf + channels = [ "loriot", "swisscom", "ttn" ] + def to_nodered(provider, data): ws = websocket.create_connection("ws://localhost:1880/{}".format(provider)) ws.send("%s" % data) diff --git a/receiver/sql_schema_postgres b/python/sql_schema_postgres similarity index 100% rename from receiver/sql_schema_postgres rename to python/sql_schema_postgres diff --git a/receiver/swisscom-receiver.py b/python/swisscom-receiver.py similarity index 64% rename from receiver/swisscom-receiver.py rename to python/swisscom-receiver.py index f993d66..345ef1b 100644 --- a/receiver/swisscom-receiver.py +++ b/python/swisscom-receiver.py @@ -15,15 +15,14 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): length = int(self.headers['Content-Length']) post_data = self.rfile.read(length).decode('utf-8') - try: - payload = self.dataToString(post_data) - except UnicodeDecodeError: - payload = "" + payload = self.payload_hex(post_data) + deveui = self.get_deveui(post_data) + # Try to decode to unicode try: - deveui = self.dataToDevEUI(post_data) + payload = self.data_to_unicode(payload) except UnicodeDecodeError: - deveui = "" + pass print("deveui/payload: {}:{}".format(deveui, payload)) @@ -31,25 +30,18 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): lorautil.db_insert_json("swisscom", post_data, payload, deveui) lorautil.db_notify("swisscom", payload, deveui) - def dictToPayload(self, thedict): - return thedict['DevEUI_uplink']['payload_hex'] + def payload_hex(self, data): + mydict = lorautil.jsonToDict(data) + return mydict['DevEUI_uplink']['payload_hex'] - def hexToString(self, myhex): + def data_to_unicode(self, myhex): return bytes.fromhex(myhex).decode('utf-8') - def dataToString(self, data): - mydict = lorautil.jsonToDict(data) - payload = self.dictToPayload(mydict) - return self.hexToString(payload) - - def dataToDevEUI(self, data): + def get_deveui(self, data): mydict = lorautil.jsonToDict(data) eui = mydict['DevEUI_uplink']['DevEUI'] return eui - def payload(self, data): - myhex = self.payload_hex(data) - return bytes.fromhex(myhex).decode('utf-8') if __name__ == '__main__': server_address = ('0.0.0.0', 8000) diff --git a/receiver/ttn-receiver.py b/python/ttn-receiver.py similarity index 100% rename from receiver/ttn-receiver.py rename to python/ttn-receiver.py