Code: Alles auswählen
PLOTS = {
'luftfeuchtigkeit': {
'title': 'Luftfeuchtigkeit',
'ylabel': 'Luftfeuchtigkeit in %',
'plots': [('Luftfeuchtigkeit innen'), ('Luftfeuchtigkeit ausen')]
},
'temperatur': {
'title': 'Temperatur',
'ylabel': 'Temperatur in °C',
'plots': [("Temperatur innen"), ("Temperatur ausen")]
},
'erdfeuchtigkeit': {
'title': 'Erdfeuchtigkeit',
'ylabel': 'Erdfeuchtigkeit in %',
'plots': [('Erdfeuchtigkeit')]
},
'lux': {
'title': 'Lichtitensität',
'ylabel': 'Lichtitensität in Lux',
'plots': [('Lichtitensität(lux)')]
},
}
@app.route('/image/<filename>', methods=['GET', 'POST'])
def data(filename):
data_one = filename.split('.')[0]
conn = psycopg2.connect()
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("SELECT TIMESTAMP, LUFTFEUCHTIGKEITDRIN, TEMPERATURDRIN, LUFTFEUCHTIGKEITAUSEN, TEMPERATURAUSEN, ERDFEUCHTIGKEIT, LUX from MESSDATEN")
rows = cur.fetchall()
time = []
wertone = []
werttwo = []
rowone = 1
rowtwo = 2
plot = PLOTS[data_one]
if data_one == 'luftfeuchtigkeit':
rowone = 1
rowtwo = 3
elif data_one == 'temperatur' :
rowone = 2
rowtwo = 4
elif data_one == 'erdfeuchtigkeit' :
rowone = 5
elif data_one == 'lux' :
rowone = 6
else :
print ("error")
for row in rows:
time.append(row[0])
wertone.append(row[rowone])
if data_one == 'luftfeuchtigkeit' or data_one == 'temperatur':
werttwo.append(row[rowtwo])
fig, ax = plt.subplots()
if data_one == 'luftfeuchtigkeit' or data_one == 'temperatur':
for label in plot['plots']:
ax.plot(time, wertone, werttwo, label=label)
else:
for label in plot['plots']:
ax.plot(time, wertone, label=label )
ax.grid(True)
fig.legend()
ax.set_title(plot['title'])
ax.set_ylabel(plot['ylabel'])
fig.autofmt_xdate()
buffer = io.BytesIO()
plt.savefig(buffer, format = 'png')
plot_data = buffer.getvalue()
response = make_response(plot_data)
response.headers['Content-type'] = 'image/png'
return response