Use JSON input for Swisscom

This commit is contained in:
Nico Schottelius 2016-10-09 21:16:21 +02:00
parent a226a09be5
commit 697ff85958

View file

@ -3,10 +3,11 @@
import urllib import urllib
import psycopg2 import psycopg2
import websocket import websocket
import xml.etree.ElementTree as ET # import xml.etree.ElementTree as ET
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
import re import re
import json
import pprint
# HTTPRequestHandler class # HTTPRequestHandler class
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
@ -31,6 +32,7 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
# Print on stdout # Print on stdout
print(post_data) print(post_data)
print(self.dataToString(post_data))
# And insert into the db # And insert into the db
# self.insert_xml(post_data) # self.insert_xml(post_data)
@ -65,6 +67,20 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
print("Cannot split: %s" % (text)) 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): def devEUI(self, data):
root = ET.fromstring(data) root = ET.fromstring(data)
return root[1].text return root[1].text