This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (59 loc) · 1.77 KB
/
app.py
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
69
70
71
72
73
74
import os
from dotenv import load_dotenv
from flask import (
Flask,
flash,
render_template,
redirect,
request,
url_for,
)
import pyparticle as pp
load_dotenv()
app = Flask(__name__)
app.secret_key = "ssssh don't tell anyone"
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
DEVICE_ID = os.getenv('DEVICE_ID')
particle = pp.Particle(access_token=AUTH_TOKEN)
devices = particle.list_devices()
device = devices[0]
def get_sent_messages():
# TODO: Make this return a collection of messages that were sent from the number
messages = []
return messages
def send_message(to, body):
# TODO: Send the text message
pass
@app.route("/", methods=["GET"])
def index():
try:
messages = get_sent_messages()
status = particle.get_variable(device['id'], 'test')
if status['result']:
flash(f"A lâmpada está ligada, meu cachorrão")
else:
flash(f"A lâmpada está desligada, meu cachorrinho")
return render_template("index.html", messages=messages)
except:
flash(f"Achamos um erro... Provavelmente a lâmpada está desconectada.")
return render_template("index.html", messages=messages)
@app.route("/on", methods=["POST"])
def on():
try:
particle.call_function(device['id'], 'pisca', 1)
flash('PISCOU :)')
return redirect(url_for('index'))
except:
flash(f"Um erro foi encontrado. O aparelho está desconectado.")
return redirect(url_for('index'))
@app.route("/off", methods=["POST"])
def off():
try:
particle.call_function(device['id'], 'pisca', 0)
flash('DESLIGOU :(')
return redirect(url_for('index'))
except:
flash(f"Um erro foi detectado. O aparelho está desconectado.")
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()