2016-10-09 11:40:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import urllib
|
|
|
|
import psycopg2
|
|
|
|
import websocket
|
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
import re
|
2016-10-09 19:16:21 +00:00
|
|
|
import json
|
|
|
|
import pprint
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
# HTTPRequestHandler class
|
|
|
|
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
|
|
|
# not used, just a sample
|
|
|
|
def do_GET(self):
|
|
|
|
# Send response status code
|
|
|
|
self.send_response(200)
|
|
|
|
|
|
|
|
# Send headers
|
|
|
|
self.send_header('Content-type','text/html')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
# Send message back to client
|
|
|
|
message = "Hello world!"
|
|
|
|
# Write content as utf-8 data
|
|
|
|
self.wfile.write(bytes(message, "utf8"))
|
|
|
|
return
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
length = int(self.headers['Content-Length'])
|
|
|
|
post_data = self.rfile.read(length).decode('utf-8')
|
|
|
|
|
2016-11-02 09:02:15 +00:00
|
|
|
try:
|
|
|
|
payload = self.dataToString(post_data)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
payload = ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
deveui = self.dataToDevEUI(post_data)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
deveui = ""
|
|
|
|
|
|
|
|
print("deveui/payload: {}:{}".format(deveui, payload))
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
# And insert into the db
|
2016-10-20 09:00:11 +00:00
|
|
|
self.insert_json("swisscom", post_data, payload, deveui)
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
# Sendo to dashboard
|
|
|
|
# self.to_dashboard(post_data)
|
|
|
|
|
|
|
|
def to_dashboard(self, data):
|
|
|
|
dev = self.devEUI(data)
|
|
|
|
text = self.payload(data)
|
|
|
|
|
|
|
|
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/data")
|
|
|
|
ws.send("%s" % data)
|
|
|
|
ws.close()
|
|
|
|
|
|
|
|
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/rawmessage")
|
|
|
|
ws.send("%s:%s" % (dev, text))
|
|
|
|
ws.close()
|
|
|
|
|
|
|
|
# Working lora node
|
|
|
|
if dev == "9CD90BB52B6A1D01":
|
|
|
|
try:
|
|
|
|
key, value = text.split("=")
|
|
|
|
print("Trying to send: %s:%s" % (key, value))
|
|
|
|
ws = websocket.create_connection("wss://home-safety-visual.eu-gb.mybluemix.net/%s" % (key))
|
|
|
|
ws.send("%s" % (value))
|
|
|
|
ws.close()
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
print("Cannot split: %s" % (text))
|
|
|
|
|
|
|
|
|
2016-10-09 19:16:21 +00:00
|
|
|
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)
|
|
|
|
|
2016-10-10 13:17:26 +00:00
|
|
|
def dataToDevEUI(self, data):
|
|
|
|
mydict = self.jsonToDict(data)
|
|
|
|
eui = mydict['DevEUI_uplink']['DevEUI']
|
|
|
|
return eui
|
|
|
|
|
2016-10-09 11:40:08 +00:00
|
|
|
def devEUI(self, data):
|
|
|
|
root = ET.fromstring(data)
|
|
|
|
return root[1].text
|
|
|
|
|
|
|
|
def payload_hex(self, data):
|
|
|
|
root = ET.fromstring(data)
|
|
|
|
return root[7].text
|
|
|
|
|
|
|
|
def payload(self, data):
|
|
|
|
myhex = self.payload_hex(data)
|
|
|
|
return bytes.fromhex(myhex).decode('utf-8')
|
|
|
|
|
2016-10-10 13:17:26 +00:00
|
|
|
def insert_json(self, provider, data, payload='', deveui=''):
|
2016-10-10 11:00:08 +00:00
|
|
|
try:
|
|
|
|
conn = psycopg2.connect("dbname=lorawan")
|
|
|
|
cursor = conn.cursor()
|
2016-10-10 13:17:26 +00:00
|
|
|
cursor.execute("insert into packets values (DEFAULT, DEFAULT, %s, %s, %s, %s)", (provider, data, payload, deveui))
|
2016-10-10 11:00:08 +00:00
|
|
|
cursor.connection.commit()
|
|
|
|
conn.close()
|
|
|
|
except Exception as e:
|
|
|
|
print("DB Insert failed: %s" % e)
|
|
|
|
|
|
|
|
|
2016-10-09 11:40:08 +00:00
|
|
|
def insert_xml(self, data):
|
|
|
|
try:
|
|
|
|
conn = psycopg2.connect("dbname=hackzurich")
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("insert into data (packet) values (%s)", (data, ))
|
|
|
|
cursor.connection.commit()
|
|
|
|
conn.close()
|
|
|
|
except Exception as e:
|
|
|
|
print("DB Insert failed: %s" % e)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
server_address = ('0.0.0.0', 8000)
|
|
|
|
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
|
|
|
|
print('running server...')
|
|
|
|
httpd.serve_forever()
|