-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
68 lines (58 loc) · 2.03 KB
/
app.py
File metadata and controls
68 lines (58 loc) · 2.03 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
60
61
62
63
64
65
66
67
68
from flask import Flask, request, render_template, jsonify
import requests
import datetime
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
FIREBASE_URL = os.getenv("FIREBASE_URL")
FIREBASE_SECRET = os.getenv("FIREBASE_SECRET")
@app.route('/dados', methods=['GET'])
def receber_dados():
temp = request.args.get('temp')
if temp:
agora = datetime.datetime.now().isoformat()
dado = {
"temperatura": float(temp),
"hora": agora
}
r = requests.post(f"{FIREBASE_URL}/leituras.json?auth={FIREBASE_SECRET}", json=dado)
if r.status_code == 200:
return f"OK - Temperatura {temp} registrada."
else:
return f"Erro ao enviar ao Firebase: {r.text}", 500
return "Parâmetro 'temp' ausente", 400
@app.route('/api/dados')
def api_dados():
r = requests.get(f"{FIREBASE_URL}/leituras.json?auth={FIREBASE_SECRET}")
if r.status_code == 200:
return jsonify(r.json())
return jsonify({"erro": "Falha ao buscar dados"}), 500
@app.route('/')
def home():
return render_template("home.html")
@app.route('/api/ultima')
def ultima_temp():
r = requests.get(f"{FIREBASE_URL}/leituras.json?auth={FIREBASE_SECRET}")
if r.status_code == 200:
dados = r.json()
if dados:
ultima = list(dados.values())[-1]
temp = ultima.get("temperatura")
hora_iso = ultima.get("hora")
if temp and hora_iso:
hora_utc = datetime.datetime.fromisoformat(hora_iso)
hora_brt = hora_utc - datetime.timedelta(hours=3)
return {
"temperatura": temp,
"data": hora_brt.strftime("%d/%m/%Y"),
"hora": hora_brt.strftime("%H:%M:%S")
}
return {"erro": "Sem dados"}, 404
@app.route('/grafico')
def grafico():
return render_template("grafico.html")
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)