# coding = UTF-8
import sqlite3
import datetime
from flask import Flask, render_template, request
DATABASE = 'data.db'
app = Flask(__name__)

# root
@app.route("/")
def hello():
    db = sqlite3.connect(DATABASE)
    cur = db.cursor()
    cur.execute("SELECT * FROM sensorlog WHERE sensorid=1")
    temp_data = cur.fetchall()
    cur.close()
    db.close()
    temp1 = temp_data[len(temp_data)-1]
    temp = temp1[2]
    return render_template('index.html', temp_data=temp_data, temp=temp)

# Adding data
@app.route("/input", methods=['POST', 'GET'])
def add_data():
    if request.method == 'POST':
        sensorid = int(request.form.get('id'))
        sensorvalue = float(request.form.get('val'))
    else:
        sensorid = int(request.args.get('id'))
        sensorvalue = float(request.args.get('val'))
    nowtime = datetime.datetime.now()
    nowtime = nowtime.strftime('%Y-%m-%d %H:%M:%S')
    db = sqlite3.connect(DATABASE)
    cur = db.cursor()
    cur.execute("INSERT INTO sensorlog (sensorid, sensorvalue, updatetime) VALUES (%d, %f, '%s')" % (sensorid, sensorvalue, nowtime))
    db.commit()
    cur.execute("SELECT * FROM sensorlist WHERE sensorid=%d" % sensorid)
    rv = cur.fetchall()
    cur.close()
    db.close()
    print(rv)
    maxrv = rv[0][2]
    minrv = rv[0][3]
    if sensorvalue < minrv:
        return '0'
    else:
        return '1'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080, debug=False)
