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-11-02 16:10:07 +00:00
|
|
|
import lorautil
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
# HTTPRequestHandler class
|
|
|
|
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
|
|
|
|
def do_POST(self):
|
|
|
|
length = int(self.headers['Content-Length'])
|
|
|
|
post_data = self.rfile.read(length).decode('utf-8')
|
|
|
|
|
2016-11-12 15:09:33 +00:00
|
|
|
payload = self.payload_hex(post_data)
|
|
|
|
deveui = self.get_deveui(post_data)
|
2016-11-02 09:02:15 +00:00
|
|
|
|
2016-11-12 15:09:33 +00:00
|
|
|
# Try to decode to unicode
|
2016-11-02 09:02:15 +00:00
|
|
|
try:
|
2016-11-12 15:09:33 +00:00
|
|
|
payload = self.data_to_unicode(payload)
|
2016-11-02 09:02:15 +00:00
|
|
|
except UnicodeDecodeError:
|
2016-11-12 15:09:33 +00:00
|
|
|
pass
|
2016-11-02 09:02:15 +00:00
|
|
|
|
2016-11-12 16:44:06 +00:00
|
|
|
f print("deveui/payload: {}:{}".format(deveui, payload))
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
# And insert into the db
|
2016-11-02 16:10:07 +00:00
|
|
|
lorautil.db_insert_json("swisscom", post_data, payload, deveui)
|
|
|
|
lorautil.db_notify("swisscom", payload, deveui)
|
2016-10-09 11:40:08 +00:00
|
|
|
|
2016-11-12 15:09:33 +00:00
|
|
|
def payload_hex(self, data):
|
|
|
|
mydict = lorautil.jsonToDict(data)
|
|
|
|
return mydict['DevEUI_uplink']['payload_hex']
|
2016-10-09 19:16:21 +00:00
|
|
|
|
2016-11-12 15:09:33 +00:00
|
|
|
def data_to_unicode(self, myhex):
|
2016-10-09 19:16:21 +00:00
|
|
|
return bytes.fromhex(myhex).decode('utf-8')
|
|
|
|
|
2016-11-12 15:09:33 +00:00
|
|
|
def get_deveui(self, data):
|
2016-11-02 16:42:39 +00:00
|
|
|
mydict = lorautil.jsonToDict(data)
|
2016-10-10 13:17:26 +00:00
|
|
|
eui = mydict['DevEUI_uplink']['DevEUI']
|
|
|
|
return eui
|
|
|
|
|
2016-10-09 11:40:08 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
server_address = ('0.0.0.0', 8000)
|
|
|
|
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
|
|
|
|
print('running server...')
|
|
|
|
httpd.serve_forever()
|