From 75361759d739d3e4f607c777773f701f27c33fa7 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 10 Oct 2016 15:17:26 +0200 Subject: [PATCH] Commit payload and deveui --- sql | 2 +- swisscom-receive-packet.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sql b/sql index 3c6b165..4cd1761 100644 --- a/sql +++ b/sql @@ -1 +1 @@ -create table packets ( id SERIAL, received_dt timestamp default now(), provider varchar(128), packet json ); +create table packets ( id SERIAL, received_dt timestamp default now(), provider varchar(128), packet json, payload varchar(128), deveui varchar(128) ); diff --git a/swisscom-receive-packet.py b/swisscom-receive-packet.py index 4ebdb3f..c2798b3 100644 --- a/swisscom-receive-packet.py +++ b/swisscom-receive-packet.py @@ -32,7 +32,10 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): # Print on stdout print(post_data) - print(self.dataToString(post_data)) + + payload = self.dataToString(post_data) + deveui = self.dataToDevEUI(post_data) + print("{}:{}".format(deveui, payload)) # And insert into the db self.insert_json("swisscom", post_data) @@ -81,6 +84,11 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): payload = self.dictToPayload(mydict) return self.hexToString(payload) + def dataToDevEUI(self, data): + mydict = self.jsonToDict(data) + eui = mydict['DevEUI_uplink']['DevEUI'] + return eui + def devEUI(self, data): root = ET.fromstring(data) return root[1].text @@ -93,11 +101,11 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): myhex = self.payload_hex(data) return bytes.fromhex(myhex).decode('utf-8') - def insert_json(self, provider, data): + 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)", (provider, data, )) + 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: