From 697ff859586c74511f4f63e8265170f4c919cc21 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 9 Oct 2016 21:16:21 +0200 Subject: [PATCH] Use JSON input for Swisscom --- swisscom-receive-packet.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/swisscom-receive-packet.py b/swisscom-receive-packet.py index 7071aa6..4090ccb 100644 --- a/swisscom-receive-packet.py +++ b/swisscom-receive-packet.py @@ -3,10 +3,11 @@ import urllib import psycopg2 import websocket -import xml.etree.ElementTree as ET +# import xml.etree.ElementTree as ET from http.server import BaseHTTPRequestHandler, HTTPServer import re - +import json +import pprint # HTTPRequestHandler class class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): @@ -31,6 +32,7 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): # Print on stdout print(post_data) + print(self.dataToString(post_data)) # And insert into the db # self.insert_xml(post_data) @@ -65,6 +67,20 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): print("Cannot split: %s" % (text)) + def jsonToDict(self, data): + return json.loads(data) + + def dictToPayload(self, thedict): + return thedict['DevEUI_uplink']['payload_hex'] + + def hexToString(self, myhex): + return bytes.fromhex(myhex).decode('utf-8') + + def dataToString(self, data): + mydict = self.jsonToDict(data) + payload = self.dictToPayload(mydict) + return self.hexToString(payload) + def devEUI(self, data): root = ET.fromstring(data) return root[1].text