Add db insert w/ json

This commit is contained in:
Nico Schottelius 2016-10-10 13:00:08 +02:00
parent 697ff85958
commit 07d9559100
2 changed files with 14 additions and 1 deletions

1
sql Normal file
View File

@ -0,0 +1 @@
create table packets ( id SERIAL, received_dt timestamp default now(), provider varchar(128), packet json );

View File

@ -35,7 +35,7 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
print(self.dataToString(post_data))
# And insert into the db
# self.insert_xml(post_data)
self.insert_json("swisscom", post_data)
# Send to Martin / port 8001
@ -93,6 +93,18 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
myhex = self.payload_hex(data)
return bytes.fromhex(myhex).decode('utf-8')
def insert_json(self, provider, data):
try:
conn = psycopg2.connect("dbname=lorawan")
cursor = conn.cursor()
cursor.execute("insert into packets values (DEFAULT, DEFAULT, '%s', '%s')", (provider, data, ))
cursor.connection.commit()
conn.close()
except Exception as e:
print("DB Insert failed: %s" % e)
def insert_xml(self, data):
try:
conn = psycopg2.connect("dbname=hackzurich")