finished first version of mqtt2sql
parent
4742822b54
commit
a53024bf18
109
mqtt2sql.py
109
mqtt2sql.py
|
@ -3,13 +3,27 @@ import json,time
|
||||||
import pymysql,dateutil
|
import pymysql,dateutil
|
||||||
import datetime
|
import datetime
|
||||||
from dateutil.parser import parse as date_parse
|
from dateutil.parser import parse as date_parse
|
||||||
|
import threading
|
||||||
|
|
||||||
sqlinsert="insert into datain (time,sensortime,topic,value) values ({0:d},{1:d},'{2}',{3:d})"
|
sqlinsert="insert into datain (time,sensortime,topic,value) values ({0:d},{1:d},'{2}',{3:d})"
|
||||||
|
lastvalue={}
|
||||||
|
|
||||||
|
# check values and insert into database
|
||||||
|
def data2sql(acttime,msgtime,topic,value,mycursor):
|
||||||
|
bsend=True
|
||||||
|
if topic in lastvalue:
|
||||||
|
if value == lastvalue[topic]:
|
||||||
|
bsend=False
|
||||||
|
if bsend:
|
||||||
|
lastvalue[topic]=value
|
||||||
|
try:
|
||||||
|
sqlcheck=mycursor.execute(sqlinsert.format(acttime,msgtime,topic,value))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
# The callback for when the client receives a CONNACK response from the server.
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
print("Connected with result code "+str(rc))
|
print("Connected with result code "+str(rc))
|
||||||
|
|
||||||
# Subscribing in on_connect() means that if we lose the connection and
|
# Subscribing in on_connect() means that if we lose the connection and
|
||||||
# reconnect then subscriptions will be renewed.
|
# reconnect then subscriptions will be renewed.
|
||||||
client.subscribe("tele/+/STATE")
|
client.subscribe("tele/+/STATE")
|
||||||
|
@ -28,34 +42,109 @@ def on_message(client, userdata, msg):
|
||||||
# print(ts+" "+str(jpl))
|
# print(ts+" "+str(jpl))
|
||||||
str_to_dt=0
|
str_to_dt=0
|
||||||
if 'Time' in jpl:
|
if 'Time' in jpl:
|
||||||
str_to_dt = int(datetime.datetime.strptime(jpl['Time'], '%Y-%m-%dT%H:%M:%S').timestamp())
|
str_to_dt = int(1000*datetime.datetime.strptime(jpl['Time'], '%Y-%m-%dT%H:%M:%S').timestamp())
|
||||||
print(acttime)
|
|
||||||
print(str_to_dt)
|
|
||||||
print(jpl)
|
|
||||||
if 'LoadAvg' in jpl:
|
if 'LoadAvg' in jpl:
|
||||||
sqlcheck=mycursor.execute(sqlinsert.format(acttime,str_to_dt,ts+'/LoadAvg',jpl['LoadAvg']))
|
try:
|
||||||
print(sqlinsert.format(acttime,str_to_dt,ts+'/LoadAvg',jpl['LoadAvg']))
|
data2sql(acttime,str_to_dt,ts+'/LoadAvg',jpl['LoadAvg'],mycursor)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
if 'POWER' in jpl:
|
if 'POWER' in jpl:
|
||||||
power=0
|
power=0
|
||||||
if jpl['POWER']=='OFF':
|
if jpl['POWER']=='OFF':
|
||||||
power=1
|
power=1
|
||||||
sqlcheck=mycursor.execute(sqlinsert.format(acttime,str_to_dt,ts+'/Power',power))
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/Power',power,mycursor)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
if 'Wifi' in jpl:
|
if 'Wifi' in jpl:
|
||||||
jplw=jpl['Wifi']
|
jplw=jpl['Wifi']
|
||||||
if 'RSSI' in jplw:
|
if 'RSSI' in jplw:
|
||||||
sqlcheck=mycursor.execute(sqlinsert.format(acttime,str_to_dt,ts+'/RSSI',jplw['RSSI']))
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/RSSI',jplw['RSSI'],mycursor)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
mycursor.close()
|
mycursor.close()
|
||||||
mydb.commit()
|
mydb.commit()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
|
||||||
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
|
def on_connect_sens(client, userdata, flags, rc):
|
||||||
|
print("Connected with result code "+str(rc))
|
||||||
|
# Subscribing in on_connect() means that if we lose the connection and
|
||||||
|
# reconnect then subscriptions will be renewed.
|
||||||
|
client.subscribe("tele/+/SENSOR")
|
||||||
|
|
||||||
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
|
def on_message_sens(client, userdata, msg):
|
||||||
|
try:
|
||||||
|
mydb_sens=pymysql.connect(read_default_file="~/.my.cnf",database="Sensor")
|
||||||
|
except:
|
||||||
|
print("Could not connect to sql server! Quitting")
|
||||||
|
else:
|
||||||
|
mycursor_sens=mydb_sens.cursor(pymysql.cursors.DictCursor)
|
||||||
|
acttime=int(1000*time.time())
|
||||||
|
jpl=json.loads(msg.payload)
|
||||||
|
ts="sp/"+msg.topic.split("/")[1]
|
||||||
|
# print(ts+" "+str(jpl))
|
||||||
|
str_to_dt=0
|
||||||
|
if 'Time' in jpl:
|
||||||
|
str_to_dt = int(1000*datetime.datetime.strptime(jpl['Time'], '%Y-%m-%dT%H:%M:%S').timestamp())
|
||||||
|
if 'ENERGY' in jpl:
|
||||||
|
jplw=jpl['ENERGY']
|
||||||
|
if 'Power' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/Power',int(1000*jplw['Power']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if 'ApparentPower' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/ApparentPower',int(1000*jplw['ApparentPower']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if 'ReactivePower' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/ReactivePower',int(1000*jplw['ReactivePower']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if 'Factor' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/PowerFactor',int(1000*jplw['Factor']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if 'Voltags' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/Voltage',int(1000*jplw['Voltage']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if 'Current' in jplw:
|
||||||
|
try:
|
||||||
|
data2sql(acttime,str_to_dt,ts+'/Current',int(1000*jplw['Current']),mycursor_sens)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
mycursor_sens.close()
|
||||||
|
mydb_sens.commit()
|
||||||
|
mydb_sens.close()
|
||||||
|
|
||||||
client = mqtt.Client()
|
client = mqtt.Client()
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
|
|
||||||
client.connect("172.24.41.2", 1883, 60)
|
client.connect("172.24.41.2", 1883, 60)
|
||||||
|
|
||||||
|
client_sens = mqtt.Client()
|
||||||
|
client_sens.on_connect = on_connect_sens
|
||||||
|
client_sens.on_message = on_message_sens
|
||||||
|
|
||||||
|
client.connect("172.24.41.2", 1883, 60)
|
||||||
|
client_sens.connect("172.24.41.2", 1883, 60)
|
||||||
# Blocking call that processes network traffic, dispatches callbacks and
|
# Blocking call that processes network traffic, dispatches callbacks and
|
||||||
# handles reconnecting.
|
# handles reconnecting.
|
||||||
# Other loop*() functions are available that give a threaded interface and a
|
# Other loop*() functions are available that give a threaded interface and a
|
||||||
# manual interface.
|
# manual interface.
|
||||||
client.loop_forever()
|
mq_state=threading.Thread(target=client.loop_forever)
|
||||||
|
mq_sens=threading.Thread(target=client_sens.loop_forever)
|
||||||
|
#client.loop_forever()
|
||||||
|
mq_state.start()
|
||||||
|
mq_sens.start()
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
|
242
pysqltq.py
242
pysqltq.py
|
@ -1,6 +1,6 @@
|
||||||
import socket,threading,socketserver,json,time
|
import socket,threading,SocketServer,json,time
|
||||||
import pymysql
|
import pymysql
|
||||||
from queue import Queue
|
from Queue import Queue
|
||||||
|
|
||||||
def sql_get_dict(mycursor,mytable,myvar):
|
def sql_get_dict(mycursor,mytable,myvar):
|
||||||
mycursor.execute("select * from "+str(mytable))
|
mycursor.execute("select * from "+str(mytable))
|
||||||
|
@ -11,130 +11,128 @@ def sql_get_dict(mycursor,mytable,myvar):
|
||||||
return(tout)
|
return(tout)
|
||||||
|
|
||||||
def sql_insert(q):
|
def sql_insert(q):
|
||||||
try:
|
try:
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
except:
|
except:
|
||||||
print("Could not connect to sql server! Quitting")
|
print("Could not connect to sql server! Quitting")
|
||||||
else:
|
else:
|
||||||
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
||||||
sqlinsert="insert into data_storage (time,device_id,var_id,sensor_id,i2c,value) values ({0:d},{1:d},{2:d},{3:d},{4:d},{5:d})"
|
sqlinsert="insert into data_storage (time,device_id,var_id,sensor_id,i2c,value) values ({0:d},{1:d},{2:d},{3:d},{4:d},{5:d})"
|
||||||
# get variable id out of sql
|
# get variable id out of sql
|
||||||
svar=sql_get_dict(mycursor,"var_id","var")
|
svar=sql_get_dict(mycursor,"var_id","var")
|
||||||
sdev=sql_get_dict(mycursor,"device_id","device")
|
sdev=sql_get_dict(mycursor,"device_id","device")
|
||||||
ssens=sql_get_dict(mycursor,"sensor_id","sensor")
|
ssens=sql_get_dict(mycursor,"sensor_id","sensor")
|
||||||
sqldata=[]
|
sqldata=[]
|
||||||
while True:
|
while True:
|
||||||
if q.empty():
|
if q.empty():
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
try:
|
|
||||||
indata=q.get()
|
|
||||||
if indata is not None:
|
|
||||||
q.task_done()
|
|
||||||
except Exception as e:
|
|
||||||
print("Error during queuing")
|
|
||||||
print(e)
|
|
||||||
else:
|
|
||||||
if indata is not None:
|
|
||||||
if indata['device'] not in sdev:
|
|
||||||
mycursor.execute("insert into device_id (device) values('"+str(indata['device'])+"')")
|
|
||||||
mydb.commit()
|
|
||||||
sdev=sql_get_dict(mycursor,"device_id","device")
|
|
||||||
if indata['sensor'] not in ssens:
|
|
||||||
mycursor.execute("insert into sensor_id (sensor) values('"+str(indata['sensor'])+"')")
|
|
||||||
mydb.commit()
|
|
||||||
ssens=sql_get_dict(mycursor,"sensor_id","sensor")
|
|
||||||
if indata['varname'] not in svar:
|
|
||||||
mycursor.execute("insert into var_id (var) values('"+str(indata['varname'])+"')")
|
|
||||||
mydb.commit()
|
|
||||||
svar=sql_get_dict(mycursor,"var_id","var")
|
|
||||||
try:
|
|
||||||
mycursor.execute(sqlinsert.format(indata['time'],sdev[indata['device']],svar[indata['varname']],ssens[indata['sensor']],indata['i2c'],indata['value']))
|
|
||||||
except:
|
|
||||||
print("Eror in execute sql insert")
|
|
||||||
print(sqlinsert.format(indata['time'],sdev[indata['device']],svar[indata['varname']],ssens[indata['sensor']],indata['i2c'],indata['value']),file="sql_missed.txt")
|
|
||||||
else:
|
|
||||||
mydb.commit()
|
|
||||||
|
|
||||||
|
|
||||||
class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
|
|
||||||
def __init__(self, request, client_address, server):
|
|
||||||
self.queue = server.queue
|
|
||||||
socketserver.StreamRequestHandler.__init__(self, request, client_address, server)
|
|
||||||
def handle(self):
|
|
||||||
indata = str(self.request.recv(1024), 'ascii')
|
|
||||||
cur_thread = threading.current_thread()
|
|
||||||
#indata=self.data
|
|
||||||
bjson=True
|
|
||||||
# try if indata is in json format.
|
|
||||||
# only process indata, if in json
|
|
||||||
try:
|
try:
|
||||||
test=json.loads(indata)
|
indata=q.get()
|
||||||
except:
|
if indata is not None:
|
||||||
bjson=False
|
q.task_done()
|
||||||
|
except Exception as e:
|
||||||
|
print("Error during queuing")
|
||||||
|
print(e)
|
||||||
else:
|
else:
|
||||||
# indata must have a payload entry
|
if indata is not None:
|
||||||
if "payload" in test:
|
if indata['device'] not in sdev:
|
||||||
# get credential for sql server and open connection
|
mycursor.execute("insert into device_id (device) values('"+str(indata['device'])+"')")
|
||||||
# only process if sql connection could be open
|
mydb.commit()
|
||||||
|
sdev=sql_get_dict(mycursor,"device_id","device")
|
||||||
|
if indata['sensor'] not in ssens:
|
||||||
|
mycursor.execute("insert into sensor_id (sensor) values('"+str(indata['sensor'])+"')")
|
||||||
|
mydb.commit()
|
||||||
|
ssens=sql_get_dict(mycursor,"sensor_id","sensor")
|
||||||
|
if indata['varname'] not in svar:
|
||||||
|
mycursor.execute("insert into var_id (var) values('"+str(indata['varname'])+"')")
|
||||||
|
mydb.commit()
|
||||||
|
svar=sql_get_dict(mycursor,"var_id","var")
|
||||||
|
|
||||||
datasource=self.client_address[0]
|
txt_sql=sqlinsert.format(indata['time'],sdev[indata['device']],svar[indata['varname']],ssens[indata['sensor']],indata['i2c'],indata['value'])
|
||||||
if "device" in test:
|
try:
|
||||||
datasource=test['device'].translate(' ./:;*|')
|
mycursor.execute(txt_sql)
|
||||||
datasource=datasource[:64]
|
except:
|
||||||
multi=1
|
print("Eror in execute sql insert")
|
||||||
if "mult" in test:
|
errlog=open("sql_missed.txt","a")
|
||||||
multi=int(test['mult'])
|
errlog.write(txt_sql)
|
||||||
payload=test['payload']
|
errlog.close()
|
||||||
for x,y in payload.items():
|
else:
|
||||||
# remove unwanted characters from variable name
|
mydb.commit()
|
||||||
varx=x.translate(' ./:;*!')
|
|
||||||
varx=varx[:64]
|
|
||||||
value=0
|
|
||||||
if "value" in y:
|
|
||||||
value=int(y['value'])
|
|
||||||
sensor=""
|
|
||||||
if "time" in y:
|
|
||||||
datatime=int(y['time'])
|
|
||||||
else:
|
|
||||||
datatime=int(1000*time.time())
|
|
||||||
if "sensor" in y:
|
|
||||||
sensor=y['sensor']
|
|
||||||
sensor=sensor.translate(' ./:;*!')
|
|
||||||
sensor=sensor[:32]
|
|
||||||
i2c=0
|
|
||||||
if "i2c" in y:
|
|
||||||
i2c=int(y['i2c'])
|
|
||||||
q.put({"time":datatime,"device":datasource,"varname":varx,"sensor":sensor,"i2c":i2c,"value":int(multi*value)},block=False)
|
|
||||||
|
|
||||||
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
|
||||||
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True,queue=None):
|
class ThreadedTCPRequestHandler(SocketServer.StreamRequestHandler):
|
||||||
self.queue = queue
|
def __init__(self, request, client_address, server):
|
||||||
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass,
|
self.queue = server.queue
|
||||||
|
socketserver.StreamRequestHandler.__init__(self, request, client_address, server)
|
||||||
|
def handle(self):
|
||||||
|
indata = str(self.request.recv(1024), 'ascii')
|
||||||
|
cur_thread = threading.current_thread()
|
||||||
|
#indata=self.data
|
||||||
|
bjson=True
|
||||||
|
# try if indata is in json format.
|
||||||
|
# only process indata, if in json
|
||||||
|
try:
|
||||||
|
test=json.loads(indata)
|
||||||
|
except:
|
||||||
|
bjson=False
|
||||||
|
else:
|
||||||
|
# indata must have a payload entry
|
||||||
|
if "payload" in test:
|
||||||
|
# get credential for sql server and open connection
|
||||||
|
# only process if sql connection could be open
|
||||||
|
datasource=self.client_address[0]
|
||||||
|
if "device" in test:
|
||||||
|
datasource=test['device'].translate(' ./:;*|')
|
||||||
|
datasource=datasource[:64]
|
||||||
|
multi=1
|
||||||
|
if "mult" in test:
|
||||||
|
multi=int(test['mult'])
|
||||||
|
payload=test['payload']
|
||||||
|
for x,y in payload.items():
|
||||||
|
# remove unwanted characters from variable name
|
||||||
|
varx=x.translate(' ./:;*!')
|
||||||
|
varx=varx[:64]
|
||||||
|
value=0
|
||||||
|
if "value" in y:
|
||||||
|
value=int(y['value'])
|
||||||
|
sensor=""
|
||||||
|
if "time" in y:
|
||||||
|
datatime=int(y['time'])
|
||||||
|
else:
|
||||||
|
datatime=int(1000*time.time())
|
||||||
|
if "sensor" in y:
|
||||||
|
sensor=y['sensor']
|
||||||
|
sensor=sensor.translate(' ./:;*!')
|
||||||
|
sensor=sensor[:32]
|
||||||
|
i2c=0
|
||||||
|
if "i2c" in y:
|
||||||
|
i2c=int(y['i2c'])
|
||||||
|
q.put({"time":datatime,"device":datasource,"varname":varx,"sensor":sensor,"i2c":i2c,"value":int(multi*value)},block=False)
|
||||||
|
|
||||||
|
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
||||||
|
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True,queue=None):
|
||||||
|
self.queue = queue
|
||||||
|
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass,
|
||||||
bind_and_activate=bind_and_activate)
|
bind_and_activate=bind_and_activate)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
q=Queue(maxsize=0)
|
||||||
q=Queue(maxsize=0)
|
sql_worker=threading.Thread(target=sql_insert,args=(q,))
|
||||||
|
sql_worker.setDaemon(True)
|
||||||
sql_worker=threading.Thread(target=sql_insert,args=(q,))
|
sql_worker.start()
|
||||||
sql_worker.setDaemon(True)
|
# Port 0 means to select an arbitrary unused port
|
||||||
sql_worker.start()
|
HOST, PORT = "", 24048
|
||||||
|
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler,queue=q)
|
||||||
# Port 0 means to select an arbitrary unused port
|
server.timeout=None
|
||||||
|
# Start a thread with the server -- that thread will then start one
|
||||||
HOST, PORT = "", 24048
|
# more thread for each request
|
||||||
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler,queue=q)
|
server_thread = threading.Thread(target=server.serve_forever,args=(q,))
|
||||||
|
# Exit the server thread when the main thread terminates
|
||||||
# Start a thread with the server -- that thread will then start one
|
server_thread.daemon = True
|
||||||
# more thread for each request
|
server_thread.timeout=None
|
||||||
server_thread = threading.Thread(target=server.serve_forever,args=(q,))
|
server_thread.start()
|
||||||
# Exit the server thread when the main thread terminates
|
print("Server loop running in thread:", server_thread.name)
|
||||||
server_thread.daemon = True
|
server.serve_forever()
|
||||||
server_thread.start()
|
server.shutdown()
|
||||||
print("Server loop running in thread:", server_thread.name)
|
server.server_close()
|
||||||
|
|
||||||
server.serve_forever()
|
|
||||||
|
|
||||||
server.shutdown()
|
|
||||||
server.server_close()
|
|
||||||
|
|
549
pyweb.py
549
pyweb.py
|
@ -10,9 +10,9 @@ pathname = os.path.dirname(sys.argv[0])
|
||||||
abspath=os.path.abspath(pathname)
|
abspath=os.path.abspath(pathname)
|
||||||
configfile=abspath+"/config.json"
|
configfile=abspath+"/config.json"
|
||||||
try:
|
try:
|
||||||
cf=open(configfile,"r")
|
cf=open(configfile,"r")
|
||||||
except:
|
except:
|
||||||
cf=open(configfile+".template","r")
|
cf=open(configfile+".template","r")
|
||||||
|
|
||||||
log_conf=json.load(cf)
|
log_conf=json.load(cf)
|
||||||
cf.close()
|
cf.close()
|
||||||
|
@ -24,28 +24,28 @@ for n in parameter:
|
||||||
if "sqlserver" in log_conf:
|
if "sqlserver" in log_conf:
|
||||||
hostname="banana"
|
hostname="banana"
|
||||||
if "host" in log_conf['sqlserver']:
|
if "host" in log_conf['sqlserver']:
|
||||||
hostname=log_conf['sqlserver']['host']
|
hostname=log_conf['sqlserver']['host']
|
||||||
port=24049
|
port=24049
|
||||||
if "port" in log_conf['sqlserver']:
|
if "port" in log_conf['sqlserver']:
|
||||||
port=int(log_conf['sqlserver']['port'])
|
port=int(log_conf['sqlserver']['port'])
|
||||||
|
|
||||||
clientlist=parameter['allowed_ip']
|
clientlist=parameter['allowed_ip']
|
||||||
try:
|
try:
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
except:
|
except:
|
||||||
clientlist=parameter['allowed_ip']
|
clientlist=parameter['allowed_ip']
|
||||||
else:
|
else:
|
||||||
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
||||||
mycursor.execute("select ip,keyid from clients;")
|
mycursor.execute("select ip,keyid from clients;")
|
||||||
myclientlist=mycursor.fetchall()
|
myclientlist=mycursor.fetchall()
|
||||||
if len(myclientlist)>0:
|
if len(myclientlist)>0:
|
||||||
tcl={}
|
tcl={}
|
||||||
for i in myclientlist:
|
for i in myclientlist:
|
||||||
if len(gpg.list_keys(i['keyid']))>0:
|
if len(gpg.list_keys(i['keyid']))>0:
|
||||||
tcl[i['ip']]=i['keyid']
|
tcl[i['ip']]=i['keyid']
|
||||||
if len(tcl)>0:
|
if len(tcl)>0:
|
||||||
clientlist=tcl
|
clientlist=tcl
|
||||||
|
|
||||||
#sqlinsert="insert into measures (time,id,value) values ({0:d},{1:d},{2:d})"
|
#sqlinsert="insert into measures (time,id,value) values ({0:d},{1:d},{2:d})"
|
||||||
sqlinsert="insert into measures (time,id,value) values "
|
sqlinsert="insert into measures (time,id,value) values "
|
||||||
hsqlinsert="insert into hourly_measures (time,id,value) values "
|
hsqlinsert="insert into hourly_measures (time,id,value) values "
|
||||||
|
@ -67,105 +67,106 @@ _JSONSIGNEDDATA="signed_data"
|
||||||
_JSONENCRYPTDATA="encrypted_data"
|
_JSONENCRYPTDATA="encrypted_data"
|
||||||
|
|
||||||
def analyse_jsonin(json_in,hash_id):
|
def analyse_jsonin(json_in,hash_id):
|
||||||
measdata={}
|
measdata={}
|
||||||
if _JSONDATA in json_in:
|
if _JSONDATA in json_in:
|
||||||
measdata=json_in[_JSONDATA]
|
measdata=json_in[_JSONDATA]
|
||||||
if _JSONSIGNEDDATA in json_in:
|
if _JSONSIGNEDDATA in json_in:
|
||||||
vpgp=gpg.verify(json_in[_JSONSIGNEDDATA])
|
vpgp=gpg.verify(json_in[_JSONSIGNEDDATA])
|
||||||
if hash_id != vpgp.key_id:
|
if hash_id != vpgp.key_id:
|
||||||
print("signature does not fit hash id")
|
print("signature does not fit hash id")
|
||||||
else:
|
else:
|
||||||
signed_in=json_in[_JSONSIGNEDDATA].split("\n")
|
signed_in=json_in[_JSONSIGNEDDATA].split("\n")
|
||||||
signed_in[signed_in.index(_BEGINSIGNATURE):]=""
|
signed_in[signed_in.index(_BEGINSIGNATURE):]=""
|
||||||
del signed_in[signed_in.index(_BEGINMESSAGE)]
|
del signed_in[signed_in.index(_BEGINMESSAGE)]
|
||||||
del signed_in[signed_in.index("")]
|
del signed_in[signed_in.index("")]
|
||||||
for h in signed_in:
|
for h in signed_in:
|
||||||
if _BEGINHASH in h:
|
if _BEGINHASH in h:
|
||||||
del signed_in[signed_in.index(h)]
|
del signed_in[signed_in.index(h)]
|
||||||
if len(signed_in)>0:
|
if len(signed_in)>0:
|
||||||
measdata=json.loads(signed_in[0])
|
measdata=json.loads(signed_in[0])
|
||||||
if _JSONENCRYPTDATA in json_in:
|
if _JSONENCRYPTDATA in json_in:
|
||||||
dpgp=gpg.decrypt(json_in[_JSONENCRYPTDATA])
|
dpgp=gpg.decrypt(json_in[_JSONENCRYPTDATA])
|
||||||
if hash_id != dpgp.key_id:
|
if hash_id != dpgp.key_id:
|
||||||
print("signature of encrypted data does not fit hash id")
|
print("signature of encrypted data does not fit hash id")
|
||||||
else:
|
else:
|
||||||
measdata=json.loads(dpgp.data)
|
measdata=json.loads(dpgp.data)
|
||||||
if len(measdata)==0:
|
if len(measdata)==0:
|
||||||
print("no data available")
|
print("no data available")
|
||||||
else:
|
else:
|
||||||
_thread.start_new_thread(insert_sql,(measdata,))
|
_thread.start_new_thread(insert_sql,(measdata,))
|
||||||
|
|
||||||
def insert_sql(measdata):
|
def insert_sql(measdata):
|
||||||
# tsi=sqlinsert
|
# tsi=sqlinsert
|
||||||
sqlu=[]
|
sqlu=[]
|
||||||
tsi=""
|
tsi=""
|
||||||
tsi_count=0
|
tsi_count=0
|
||||||
atsi=""
|
atsi=""
|
||||||
for h in measdata: # iterate over each variable stored in json
|
for h in measdata: # iterate over each variable stored in json
|
||||||
md=measdata[h]
|
md=measdata[h]
|
||||||
sqlu.append("delete from last_measures where id = '"+str(h)+"';")
|
sqlu.append("delete from last_measures where id = '"+str(h)+"';")
|
||||||
mmax=max(md['measures'])
|
mmax=max(md['measures'])
|
||||||
atsi=atsi+'('+str(mmax)+','+h+','+str(md['measures'][mmax])+'),'
|
atsi=atsi+'('+str(mmax)+','+h+','+str(md['measures'][mmax])+'),'
|
||||||
for m in md['measures']: # iterate over each measurement for given variable
|
for m in md['measures']: # iterate over each measurement for given variable
|
||||||
try:
|
try:
|
||||||
stime=int(m)
|
stime=int(m)
|
||||||
except:
|
except:
|
||||||
print("wrong entry")
|
print("wrong entry")
|
||||||
else:
|
else:
|
||||||
ttsi='('+str(m)+','+h+','+str(md['measures'][m])+'),'
|
ttsi='('+str(m)+','+h+','+str(md['measures'][m])+'),'
|
||||||
tsi=tsi+ttsi
|
tsi=tsi+ttsi
|
||||||
tsi_count=tsi_count+1
|
tsi_count=tsi_count+1
|
||||||
tsi=tsi[:-1]+';'
|
tsi=tsi[:-1]+';'
|
||||||
atsi=atsi[:-1]+';'
|
atsi=atsi[:-1]+';'
|
||||||
try:
|
try:
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
except:
|
except:
|
||||||
print("could not connect to mysql")
|
print("could not connect to mysql")
|
||||||
with open("missed.sql","a") as sqlstore:
|
with open("missed.sql","a") as sqlstore:
|
||||||
sqlstore.write(tsi)
|
sqlstore.write(tsi)
|
||||||
else:
|
else:
|
||||||
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
||||||
for h in measdata:
|
for h in measdata:
|
||||||
mycursor.execute('select id from ids where id='+h+';')
|
mycursor.execute('select id from ids where id='+h+';')
|
||||||
t=mycursor.fetchall()
|
t=mycursor.fetchall()
|
||||||
if len(t)==0:
|
if len(t)==0:
|
||||||
hsql='insert into ids (id,device,varname,sensor,sensorsub,i2c) values ('+h+','
|
hsql='insert into ids (id,device,varname,sensor,sensorsub,i2c) values ('+h+','
|
||||||
hm=measdata[h]
|
hm=measdata[h]
|
||||||
ht={}
|
ht={}
|
||||||
for i in ['device','varname','sensor','sensorsub']:
|
for i in ['device','varname','sensor','sensorsub']:
|
||||||
if i in hm:
|
if i in hm:
|
||||||
hsql=hsql+"'"+hm[i]+"'"+','
|
hsql=hsql+"'"+hm[i]+"'"+','
|
||||||
else:
|
else:
|
||||||
hsql=hsql+"\'\',"
|
hsql=hsql+"\'\',"
|
||||||
if 'i2c' in hm:
|
if 'i2c' in hm:
|
||||||
try:
|
try:
|
||||||
hsql=hsql+"'"+str(hm['i2c'])+"'"
|
hsql=hsql+"'"+str(hm['i2c'])+"'"
|
||||||
except:
|
except:
|
||||||
hsql=hsql+"'0'"
|
hsql=hsql+"'0'"
|
||||||
else:
|
else:
|
||||||
hsql=hsql+0
|
hsql=hsql+0
|
||||||
hsql=hsql+');'
|
hsql=hsql+');'
|
||||||
try:
|
try:
|
||||||
mycursor.execute(hsql)
|
mycursor.execute(hsql)
|
||||||
except:
|
except:
|
||||||
print("could not insert new var_id")
|
print("could not insert new var_id")
|
||||||
print(hsql)
|
print(hsql)
|
||||||
for dsql in sqlu:
|
for dsql in sqlu:
|
||||||
mycursor.execute(dsql)
|
mycursor.execute(dsql)
|
||||||
mycursor.execute(sqlinsert+tsi)
|
mycursor.execute(sqlinsert+tsi)
|
||||||
mycursor.execute(hsqlinsert+tsi)
|
print(sqlinsert+tsi)
|
||||||
mycursor.execute(dsqlinsert+tsi)
|
mycursor.execute(hsqlinsert+tsi)
|
||||||
mycursor.execute(asqlinsert+atsi)
|
mycursor.execute(dsqlinsert+tsi)
|
||||||
delhtime=int((time.time()-3600)*1000)
|
mycursor.execute(asqlinsert+atsi)
|
||||||
deldtime=int((time.time()-86400)*1000)
|
delhtime=int((time.time()-3600)*1000)
|
||||||
mycursor.execute("delete from hourly_measures where time < "+str(delhtime))
|
deldtime=int((time.time()-86400)*1000)
|
||||||
mycursor.execute("delete from daily_measures where time < "+str(deldtime))
|
mycursor.execute("delete from hourly_measures where time < "+str(delhtime))
|
||||||
print(str(tsi_count)+" new measures inserted")
|
mycursor.execute("delete from daily_measures where time < "+str(deldtime))
|
||||||
mycursor.close()
|
print(str(tsi_count)+" new measures inserted")
|
||||||
mydb.commit()
|
mycursor.close()
|
||||||
mydb.close()
|
mydb.commit()
|
||||||
# print(tsi)
|
mydb.close()
|
||||||
# print(measdata)
|
# print(tsi)
|
||||||
|
# print(measdata)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,198 +176,198 @@ app=Bottle()
|
||||||
|
|
||||||
@app.get('/')
|
@app.get('/')
|
||||||
def approot():
|
def approot():
|
||||||
return '''
|
return '''
|
||||||
<a href="/ids">Uebersicht</a></br>
|
<a href="/ids">Uebersicht</a></br>
|
||||||
<a href="/solar">Solarübersicht</a></br>
|
<a href="/solar">Solarübersicht</a></br>
|
||||||
<a href="/cpu">CPU-Temperaturen</a></br>
|
<a href="/cpu">CPU-Temperaturen</a></br>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
@app.get('/ids')
|
@app.get('/ids')
|
||||||
def show_ids():
|
def show_ids():
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
mycursor=mydb.cursor(pymysql.cursors.DictCursor)
|
||||||
starttime=time.time()
|
starttime=time.time()
|
||||||
mycursor.execute('select gm.*,last_measures.value,ids.varname,ids.device,ids.sensor,ids.i2c,ids.sensorsub from (select id, max(time) as time,count(time) as count from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id;')
|
mycursor.execute('select gm.*,last_measures.value,ids.varname,ids.device,ids.sensor,ids.i2c,ids.sensorsub from (select id, max(time) as time,count(time) as count from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id;')
|
||||||
print("ids sql duration:"+str(time.time()-starttime))
|
print("ids sql duration:"+str(time.time()-starttime))
|
||||||
row=mycursor.fetchall()
|
row=mycursor.fetchall()
|
||||||
mycursor.close()
|
mycursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
return template('ids.tpl',measdata=row)
|
return template('ids.tpl',measdata=row)
|
||||||
|
|
||||||
@app.get('/solar')
|
@app.get('/solar')
|
||||||
def show_solar():
|
def show_solar():
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
myc=mydb.cursor()
|
myc=mydb.cursor()
|
||||||
myc.execute("select gm.time,last_measures.value/1000,ids.varname from (select id, max(time) as time from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id where ids.sensor='tristar';")
|
myc.execute("select gm.time,last_measures.value/1000,ids.varname from (select id, max(time) as time from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id where ids.sensor='tristar';")
|
||||||
ts=myc.fetchall()
|
ts=myc.fetchall()
|
||||||
myc.close()
|
myc.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
tt={"kwh_tot":0,"volt_bat":0,"volt_bat_sens":0,"volt_sweep_mc":0,"volt_sweep_oc":0,"amp_bat":0,"temp_heatsink":0,"temp_bat":0}
|
tt={"kwh_tot":0,"volt_bat":0,"volt_bat_sens":0,"volt_sweep_mc":0,"volt_sweep_oc":0,"amp_bat":0,"temp_heatsink":0,"temp_bat":0}
|
||||||
zs=0
|
zs=0
|
||||||
for tims,value,varname in ts:
|
for tims,value,varname in ts:
|
||||||
tt[varname]=value
|
tt[varname]=value
|
||||||
if int(tims)>zs:
|
if int(tims)>zs:
|
||||||
zs=int(tims)
|
zs=int(tims)
|
||||||
return template('solar.tpl',solardata=tt,zeitstempel=time.strftime('%H:%M:%S %Y-%m-%d', time.localtime(zs/1000)))
|
return template('solar.tpl',solardata=tt,zeitstempel=time.strftime('%H:%M:%S %Y-%m-%d', time.localtime(zs/1000)))
|
||||||
|
|
||||||
@app.get('/temp')
|
@app.get('/temp')
|
||||||
def show_temperature():
|
def show_temperature():
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
myc=mydb.cursor()
|
myc=mydb.cursor()
|
||||||
myc.execute("select gm.time,last_measures.value/1000,ids.varname,ids.device,short_names.short from (select id, max(time) as time from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id join short_names on short_names.id=gm.id;")
|
myc.execute("select gm.time,last_measures.value/1000,ids.varname,ids.device,short_names.short from (select id, max(time) as time from last_measures group by id) gm join last_measures on last_measures.id=gm.id and last_measures.time=gm.time join ids on ids.id=gm.id join short_names on short_names.id=gm.id;")
|
||||||
ts=myc.fetchall()
|
ts=myc.fetchall()
|
||||||
myc.close()
|
myc.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
tt={"temp_garten":0,"temp_dach":0,"temp_carport":0,"hum_garten":0,"hum_dach":0,"hum_carport":0,"press_garten":0,"press_dach":0,"press_carport":0,"lux_dach":0,"uv_dach":0,"temp_solar":0,"temp_bat":0,"temp_wohnen":0,"hum_wohnen":0,"press_wohnen":0}
|
tt={"temp_garten":0,"temp_dach":0,"temp_carport":0,"hum_garten":0,"hum_dach":0,"hum_carport":0,"press_garten":0,"press_dach":0,"press_carport":0,"lux_dach":0,"uv_dach":0,"temp_solar":0,"temp_bat":0,"temp_wohnen":0,"hum_wohnen":0,"press_wohnen":0}
|
||||||
zs=0
|
zs=0
|
||||||
thp={"rasolar":{"temperature":0,"humidity":0,"pressure":0,"time":0},"ragps":{"temperature":0,"humidity":0,"pressure":0,"time":0},"ragarden":{"temperature":0,"humidity":0,"pressure":0,"time":0}}
|
thp={"rasolar":{"temperature":0,"humidity":0,"pressure":0,"time":0},"ragps":{"temperature":0,"humidity":0,"pressure":0,"time":0},"ragarden":{"temperature":0,"humidity":0,"pressure":0,"time":0}}
|
||||||
|
|
||||||
for tims,value,varname,device,short in ts:
|
for tims,value,varname,device,short in ts:
|
||||||
tt[short]=value
|
tt[short]=value
|
||||||
try:
|
try:
|
||||||
thp[device][varname]=value
|
thp[device][varname]=value
|
||||||
except:
|
except:
|
||||||
thp[device]={varname:value}
|
thp[device]={varname:value}
|
||||||
try:
|
try:
|
||||||
if thp[device]['time']<tims:
|
if thp[device]['time']<tims:
|
||||||
thp[device]['time']=tims
|
thp[device]['time']=tims
|
||||||
except:
|
except:
|
||||||
thp[device]['time']=tims
|
thp[device]['time']=tims
|
||||||
if int(tims)>zs:
|
if int(tims)>zs:
|
||||||
zs=int(tims)
|
zs=int(tims)
|
||||||
return template('temperature.tpl',tempdata=tt,zeitstempel=time.strftime('%H:%M:%S %Y-%m-%d', time.localtime(zs/1000)))
|
return template('temperature.tpl',tempdata=tt,zeitstempel=time.strftime('%H:%M:%S %Y-%m-%d', time.localtime(zs/1000)))
|
||||||
|
|
||||||
@app.get('/clients')
|
@app.get('/clients')
|
||||||
def show_clients():
|
def show_clients():
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
myc=mydb.cursor()
|
myc=mydb.cursor()
|
||||||
no_clients=myc.execute('select distinct ids.device from ids join hourly_measures on hourly_measures.id=ids.id;')
|
no_clients=myc.execute('select distinct ids.device from ids join hourly_measures on hourly_measures.id=ids.id;')
|
||||||
if no_clients > 0:
|
if no_clients > 0:
|
||||||
t_cl=myc.fetchall()
|
t_cl=myc.fetchall()
|
||||||
client_list=[]
|
client_list=[]
|
||||||
for cl in t_cl:
|
for cl in t_cl:
|
||||||
client_list.append(cl[0])
|
client_list.append(cl[0])
|
||||||
return template('clients.tpl',clientdata=client_list)
|
return template('clients.tpl',clientdata=client_list)
|
||||||
else:
|
else:
|
||||||
return '''
|
return '''
|
||||||
<!DOCTYPE html><html><head>
|
<!DOCTYPE html><html><head>
|
||||||
<title>Übersicht Tristar</title>
|
<title>Übersicht Tristar</title>
|
||||||
</head><body>
|
</head><body>
|
||||||
<p>Aktuell keine Rechner verbunden</p>
|
<p>Aktuell keine Rechner verbunden</p>
|
||||||
</body></html>
|
</body></html>
|
||||||
'''
|
'''
|
||||||
@app.get('/client/<client>')
|
@app.get('/client/<client>')
|
||||||
def forward_client(client):
|
def forward_client(client):
|
||||||
response=requests.get('http://'+client+':8080/')
|
response=requests.get('http://'+client+':8080/')
|
||||||
print(requests.get('http://'+client+':8080/'))
|
print(requests.get('http://'+client+':8080/'))
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return response.content
|
return response.content
|
||||||
else:
|
else:
|
||||||
return '''
|
return '''
|
||||||
<!DOCTYPE html><html><head>
|
<!DOCTYPE html><html><head>
|
||||||
<title>Keine Verbindung</title>
|
<title>Keine Verbindung</title>
|
||||||
</head><body>
|
</head><body>
|
||||||
<p>Kein Anschluss unter dieser Himbeere</p>
|
<p>Kein Anschluss unter dieser Himbeere</p>
|
||||||
</body></html>
|
</body></html>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@app.get('/cpu')
|
@app.get('/cpu')
|
||||||
def show_ids():
|
def show_ids():
|
||||||
starttime=time.time()
|
starttime=time.time()
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
myc=mydb.cursor(pymysql.cursors.DictCursor)
|
myc=mydb.cursor(pymysql.cursors.DictCursor)
|
||||||
myc.execute('select * from ids where sensor="CPU";')
|
myc.execute('select * from ids where sensor="CPU";')
|
||||||
channels=myc.fetchall()
|
channels=myc.fetchall()
|
||||||
myc.close()
|
myc.close()
|
||||||
cpu_col=cm.rainbow(numpy.linspace(0,1,len(channels)))
|
cpu_col=cm.rainbow(numpy.linspace(0,1,len(channels)))
|
||||||
plt.figure(figsize=[6,8])
|
plt.figure(figsize=[6,8])
|
||||||
mycursor=mydb.cursor()
|
mycursor=mydb.cursor()
|
||||||
for j in range(len(channels)):
|
for j in range(len(channels)):
|
||||||
mycursor.execute('select ((select max(time) from hourly_measures)-hourly_measures.time)/1000 as time,hourly_measures.value/1000 as value from hourly_measures join ids on ids.id=hourly_measures.id where ids.sensor="CPU" and ids.id='+str(channels[j]['id'])+';')
|
mycursor.execute('select ((select max(time) from hourly_measures)-hourly_measures.time)/1000 as time,hourly_measures.value/1000 as value from hourly_measures join ids on ids.id=hourly_measures.id where ids.sensor="CPU" and ids.id='+str(channels[j]['id'])+';')
|
||||||
row=numpy.array(mycursor.fetchall())
|
row=numpy.array(mycursor.fetchall())
|
||||||
plt.plot(row[:,0],row[:,1],color=cpu_col[j],label=channels[j]['varname']+'; '+channels[j]['device'])
|
plt.plot(row[:,0],row[:,1],color=cpu_col[j],label=channels[j]['varname']+'; '+channels[j]['device'])
|
||||||
plt.legend()
|
plt.legend()
|
||||||
print(str(time.time()-starttime)+"s for fetching and display")
|
print(str(time.time()-starttime)+"s for fetching and display")
|
||||||
plt.title('CPU Verlauf')
|
plt.title('CPU Verlauf')
|
||||||
plt.savefig("svg/cpu.svg")
|
plt.savefig("svg/cpu.svg")
|
||||||
mycursor.close()
|
mycursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
if len(row)>0:
|
if len(row)>0:
|
||||||
return template('cputemp.tpl',measdata=row)
|
return template('cputemp.tpl',measdata=row)
|
||||||
|
|
||||||
@app.get('/svg/<svgfile>')
|
@app.get('/svg/<svgfile>')
|
||||||
def show_svg(svgfile):
|
def show_svg(svgfile):
|
||||||
return static_file("svg/"+svgfile,root=abspath)
|
return static_file("svg/"+svgfile,root=abspath)
|
||||||
|
|
||||||
@app.get('/graph/<mid>/<kind>')
|
@app.get('/graph/<mid>/<kind>')
|
||||||
def show_graph(mid,kind):
|
def show_graph(mid,kind):
|
||||||
sqltable="hourly_measures"
|
sqltable="hourly_measures"
|
||||||
if(kind=="daily"):
|
if(kind=="daily"):
|
||||||
sqltable="daily_measures"
|
sqltable="daily_measures"
|
||||||
starttime=time.time()
|
starttime=time.time()
|
||||||
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
mydb=pymysql.connect(read_default_file="~/.my.cnf",database="rasolar")
|
||||||
mycursor=mydb.cursor()
|
mycursor=mydb.cursor()
|
||||||
print(str(time.time()-starttime)+' sql open')
|
print(str(time.time()-starttime)+' sql open')
|
||||||
nrow=mycursor.execute('select ((select max(time) from '+sqltable+')-'+sqltable+'.time)/3600000 as time,'+sqltable+'.value/1000 as value from '+sqltable+' where id=%s order by time',str(mid))
|
nrow=mycursor.execute('select ((select max(time) from '+sqltable+')-'+sqltable+'.time)/3600000 as time,'+sqltable+'.value/1000 as value from '+sqltable+' where id=%s order by time',str(mid))
|
||||||
if (nrow>0):
|
if (nrow>0):
|
||||||
row=numpy.array(mycursor.fetchall())
|
row=numpy.array(mycursor.fetchall())
|
||||||
print(str(time.time()-starttime)+' sql fetched')
|
print(str(time.time()-starttime)+' sql fetched')
|
||||||
nirow=mycursor.execute('select id,varname,device,sensor,i2c from ids where id='+str(mid))
|
nirow=mycursor.execute('select id,varname,device,sensor,i2c from ids where id='+str(mid))
|
||||||
gdata=mycursor.fetchall()
|
gdata=mycursor.fetchall()
|
||||||
mycursor.close()
|
mycursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
kanal_info={"varname":"","device":"","sensor":"","i2c":0,"count":nrow}
|
kanal_info={"varname":"","device":"","sensor":"","i2c":0,"count":nrow}
|
||||||
for ids,varname,device,sensor,i2c in gdata:
|
for ids,varname,device,sensor,i2c in gdata:
|
||||||
kanal_info["varname"]=varname
|
kanal_info["varname"]=varname
|
||||||
kanal_info["device"]=device
|
kanal_info["device"]=device
|
||||||
kanal_info["sensor"]=sensor
|
kanal_info["sensor"]=sensor
|
||||||
kanal_info["i2c"]=int(i2c)
|
kanal_info["i2c"]=int(i2c)
|
||||||
print(str(time.time()-starttime)+' sql closed')
|
print(str(time.time()-starttime)+' sql closed')
|
||||||
plt.figure(figsize=[6,8])
|
plt.figure(figsize=[6,8])
|
||||||
plt.plot(row[:,0],row[:,1])
|
plt.plot(row[:,0],row[:,1])
|
||||||
print(str(time.time()-starttime)+' picture')
|
print(str(time.time()-starttime)+' picture')
|
||||||
plt.savefig("svg/"+mid+".svg")
|
plt.savefig("svg/"+mid+".svg")
|
||||||
print(str(time.time()-starttime)+' saved')
|
print(str(time.time()-starttime)+' saved')
|
||||||
if len(row)>0:
|
if len(row)>0:
|
||||||
return template('verlauf.tpl',measdata=row,mid=mid,kanal_info=kanal_info)
|
return template('verlauf.tpl',measdata=row,mid=mid,kanal_info=kanal_info)
|
||||||
else:
|
else:
|
||||||
return template('''
|
return template('''
|
||||||
<!DOCTYPE html><html><head>
|
<!DOCTYPE html><html><head>
|
||||||
<title>Keine Daten</title>
|
<title>Keine Daten</title>
|
||||||
</head><body>
|
</head><body>
|
||||||
<p>Die Daten verstecken sich.</p>
|
<p>Die Daten verstecken sich.</p>
|
||||||
<a href="/graph/{{mid}}/hourly">Letzte Stunde</a></br>
|
<a href="/graph/{{mid}}/hourly">Letzte Stunde</a></br>
|
||||||
<a href="/graph/{{mid}}/daily">Letzter Tag</a></br>
|
<a href="/graph/{{mid}}/daily">Letzter Tag</a></br>
|
||||||
</body></html>
|
</body></html>
|
||||||
''',mid=mid)
|
''',mid=mid)
|
||||||
|
|
||||||
@app.get('/graph/<mid>')
|
@app.get('/graph/<mid>')
|
||||||
def show_graph_short(mid):
|
def show_graph_short(mid):
|
||||||
show_graph(mid,'hourly')
|
show_graph(mid,'hourly')
|
||||||
|
|
||||||
@app.post('/data/<hash_id>')
|
@app.post('/data/<hash_id>')
|
||||||
def dataimport(hash_id):
|
def dataimport(hash_id):
|
||||||
# print(hash_id)
|
# print(hash_id)
|
||||||
timestart=time.time()
|
timestart=time.time()
|
||||||
# check if request comes from allowed ip
|
# check if request comes from allowed ip
|
||||||
if (request.remote_addr in clientlist):
|
if (request.remote_addr in clientlist):
|
||||||
# hash must be the used gpg key id
|
# hash must be the used gpg key id
|
||||||
if (hash_id in clientlist[request.remote_addr]):
|
if (hash_id in clientlist[request.remote_addr]):
|
||||||
# print("correct id")
|
# print("correct id")
|
||||||
# check, if json is transmitted
|
# check, if json is transmitted
|
||||||
try:
|
try:
|
||||||
json_in=json.loads(request.json)
|
json_in=json.loads(request.json)
|
||||||
# print(json_in)
|
# print(json_in)
|
||||||
except:
|
except:
|
||||||
print("no json")
|
print("no json")
|
||||||
else:
|
else:
|
||||||
print(time.time()-timestart)
|
print(time.time()-timestart)
|
||||||
_thread.start_new_thread(analyse_jsonin,(json_in,hash_id,))
|
_thread.start_new_thread(analyse_jsonin,(json_in,hash_id,))
|
||||||
else:
|
else:
|
||||||
print("wrong id")
|
print("wrong id")
|
||||||
else:
|
else:
|
||||||
print("not allowed client address")
|
print("not allowed client address")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue