master
ademant 2019-06-03 21:16:31 +02:00
parent 5fc6e3ca6c
commit 7b007bf150
2 changed files with 21 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import SocketServer as socketserver
import json
import time
import mysql.connector
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
@ -21,19 +22,33 @@ class MyTCPHandler(socketserver.BaseRequestHandler):
print(test)
if "payload" in test:
print(test['payload'])
datatime=time.time()
datatime=int(1000*time.time())
if "time" in test:
datatime=test['time']
datatime=int(test['time'])
print(datatime)
datasource=self.client_address[0]
if "device" in test:
datasource=test['device']
multi=1
if "mult" in test:
multi=test['mult']
mycursor=mydb.cursor()
payload=test['payload']
print(payload)
for x,y in payload.items():
mycursor.execute(sqlinsert.format(datatime,datasource,x,int(mult*y)))
mydb.commit()
# just send back the same data, but upper-cased
self.request.sendall("Done")
if __name__ == "__main__":
HOST, PORT = "localhost", 24048
HOST, PORT = "", 24048
pwf = open("my.cnf","r")
pwj = json.loads(pwf.read())
print(pwj)
mydb=mysql.connector.connect(host="localhost",user=pwj['user'],passwd=pwj['password'],database=pwj['database'])
sqlinsert="insert into datain (time,device,var,value) values ({0:d},'{1:s}','{2:s}',{3:d})"
# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

View File

@ -1,5 +1,5 @@
{
"Time": 2e+6,
"time": 200000000,
"device": "rasolar",
"payload":
{