You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
from flask import Flask, render_template, request, abort
|
|
import os
|
|
from datetime import datetime
|
|
|
|
app = Flask(__name__)
|
|
|
|
TEPLOTA_FILE = "teplota.txt"
|
|
VLHKOST_FILE = "vlhkost.txt"
|
|
|
|
def appendToFile(file,value):
|
|
try:
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
with open(file,"a") as f:
|
|
f.write(f"{timestamp},{value}")
|
|
return True
|
|
except Exception as e:
|
|
print(f"Chyba při zápisu do souboru {file}:{e}")
|
|
return False
|
|
|
|
def readLastLine(filename):
|
|
try:
|
|
with open(filename,"r") as f:
|
|
lines = f.readlines()
|
|
for line in reversed(lines):
|
|
stripped_line = line.strip()
|
|
if stripped_line:
|
|
parts = stripped_line.split(",")
|
|
if len(parts) == 2:
|
|
return parts[1]
|
|
else:
|
|
return stripped_line
|
|
return "žádná data"
|
|
except FileNotFoundError:
|
|
return "Soubor nenalezen"
|
|
except Exception as e:
|
|
print(f"Chyba při čtení souboru {filename}:{e}")
|
|
return "Chyba čtení"
|
|
|
|
@app.route("/")
|
|
def index():
|
|
last_temp = readLastLine(TEPLOTA_FILE)
|
|
last_hum = readLastLine(VLHKOST_FILE)
|
|
return render_template("index.html",last_t = last_temp,last_h=last_hum)
|
|
|
|
@app.route("/api/teplota/<int:value>", methods=["GET","POST"])
|
|
def record_t(value):
|
|
if appendToFile(TEPLOTA_FILE,value):
|
|
return f"Teplota {value}°C uložena",200
|
|
else:
|
|
return "Chyba při ukládání teploty", 500
|
|
|
|
@app.route("/api/vlhkost/<int:value>", methods=["GET","POST"])
|
|
def record_h(value):
|
|
if appendToFile(VLHKOST_FILE,value):
|
|
return f"vlhkost {value}°C uložena",200
|
|
else:
|
|
return "Chyba při ukládání teploty", 500
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, host="0.0.0.0", port=5000)
|