-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
59 lines (45 loc) · 1.75 KB
/
Copy pathserver.py
File metadata and controls
59 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import logging
import sqlite3
import time
import os
from flask import Flask, jsonify, render_template, make_response, request
app = Flask(__name__)
def get_json():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "tempData.db")
connect = sqlite3.connect(db_path)
temps = []
for row in connect.execute("SELECT * FROM temp"):
temps.append({"date": row[0], "temperature": row[1], "humidity": row[2]})
return temps
@app.route('/', methods=['GET'])
def index():
labels = map(lambda x: x['date'], get_json())
data_temp = map(lambda x: x['temperature'], get_json())
data_hum = map(lambda x: x['humidity'], get_json())
return render_template("index.html", labels=map(json.dumps, labels), temp_data=map(json.dumps, data_temp),
hum_data=map(json.dumps, data_hum))
@app.route('/contents', methods=['GET'])
def get_temperature():
return jsonify(get_json())
@app.route('/contents', methods=['POST'])
def create_temperature():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "tempData.db")
connect = sqlite3.connect(db_path)
currentDate = time.strftime('%d.%m.%Y, %H:%m:%s')
temp = request.json['temperature']
hum = request.json['humidity']
cursor = connect.cursor()
cursor.execute("INSERT INTO temp VALUES(?,?,?)", (currentDate, temp, hum))
connect.commit()
connect.close()
return 'finished', 201
@app.errorhandler(404)
def not_found(exception):
app.logger.error(exception)
return make_response(jsonify({'error': request.full_path}), 404)
if __name__ == '__main__':
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
app.run(host="0.0.0.0", debug=False)