-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (88 loc) · 3.35 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from fileinput import filename
from flask import *
from vehicle_access import *
import teslapy
from tesla_email import *
import time
app = Flask(__name__, static_folder='static')
@app.route('/', methods=["POST", "GET"])
@app.route('/index', methods=["POST", "GET"])
def index():
'''
Homepage
'''
messages = []
print("index() route being called:")
if request.method == "POST":
print("POST REQUEST RECEIVED")
email = request.form.get("email")
token = request.form.get("token")
tesla = teslapy.Tesla(email)
if email and token:
tesla = fetch_login_object(email, token)
print("Branch 1")
print(tesla.vehicle_list())
elif email and not get_login_url(email):
print("Branch 2")
return redirect(url_for('dashboard', email=email))
else:
print("Branch 3")
msg = get_login_url(email)
if msg == "None":
messages.append("Account Already validated.")
else:
messages.append(msg)
return render_template('index.html',messages = messages)
@app.route('/dashboard/<email>/', methods=["POST", "GET"])
def dashboard(email):
messages = []
tesla = teslapy.Tesla(email)
vehicles = email_data(tesla)
out_str = []
if request.method == "POST":
print("\trequest data", request.form)
if len(dict(request.form)) == 1:
target = list(dict(request.form).keys())[0]
action = list(dict(request.form).values())[0]
target = target[target.find("_")+1::]
target = [target]
else:
target = list(dict(request.form).keys())
email_vehicle_list = []
for vehicle in target:
name = vehicle[vehicle.find("_")+1::]
if name != "sendemail":
email_vehicle_list.append(name)
else:
action = name
target = email_vehicle_list
print("\tThis is the email list of the vehicles:",target)
print("\tthe target is:", target)
for vehicle in tesla.vehicle_list():
if vehicle["display_name"] in target:
if action == "lock":
print("locking", vehicle["display_name"])
vehicle.command("LOCK")
elif action == "unlock":
print("unlocking:",vehicle["display_name"])
vehicle.command("UNLOCK")
elif action == "remotestart":
print("remotely starting:",vehicle["display_name"])
vehicle.command("REMOTE_START")
elif action == "sendemail":
print("\tWe can now send an email to:", vehicle["display_name"])
email_information = email_data(tesla, names=target)
send_email_with_data(email_information)
else:
continue
model_names = vehicle_names(tesla)
for name,stats in vehicles.items():
out_str.append( (str(name), stats) )
return render_template('dashboard.html', data=out_str, messages=messages, email=email, model_names = model_names)
@app.route('/logout/<email>/')
def logout(email):
tesla_logout(email)
return render_template(url_for('index'))
def main():
app.run(debug=True)
main()