-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
247 lines (192 loc) · 8.26 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from flask import Flask, render_template, request, redirect, url_for
import requests
import os
from dotenv import load_dotenv
import sqlite3
import time
import shortuuid
from datetime import datetime
import flask_login
from geopy.distance import geodesic
from flask_migrate import Migrate
from models.database import db
import flask_sqlalchemy
from interfaces.backendfactory import BackendProviderFactory
# Import models for automated database migration
from models.share import Share
load_dotenv()
MAPBOX_TOKEN = os.getenv('MAPBOX_TOKEN')
BASE_URL = os.getenv('BASE_URL')
PORT = os.getenv('PORT', 5051)
DATA_DIR = os.path.abspath(os.getenv('DATA_DIR', '/data/'))
BACKEND_PROVIDER = os.getenv('BACKEND_PROVIDER', 'teslalogger')
BACKEND_PROVIDER_BASE_URL = os.getenv('BACKEND_PROVIDER_BASE_URL')
BACKEND_PROVIDER_CAR_ID = os.getenv('BACKEND_PROVIDER_CAR_ID', 1)
BACKEND_PROVIDER_MULTICAR = os.getenv('BACKEND_PROVIDER_MULTICAR', False)
# Backend provider instanciation
BackendProviderFactory(BACKEND_PROVIDER, BACKEND_PROVIDER_BASE_URL, BACKEND_PROVIDER_CAR_ID)
app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{DATA_DIR}/service.db"
# Use the global db object
db.init_app(app)
migrate = Migrate(app,db)
with app.app_context():
db.create_all()
# Fix static folder BASE_URL
app.view_functions["static"] = None
a_new_static_path = BASE_URL + '/static'
# Set the static_url_path property.
app.static_url_path = a_new_static_path
# Remove the old rule from Map._rules.
for rule in app.url_map.iter_rules('static'):
app.url_map._rules.remove(rule) # There is probably only one.
# Remove the old rule from Map._rules_by_endpoint. In this case we can just
# start fresh.
app.url_map._rules_by_endpoint['static'] = []
# Add the updated rule.
app.add_url_rule(f'{a_new_static_path}/<path:filename>',
endpoint='static',
view_func=app.send_static_file)
# Login Code
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
# User Mapping here
users = {'admin': {'password': os.getenv('ADMIN_PASSWORD', 'password')}}
# Initiate singleton
BackendProviderFactory(BACKEND_PROVIDER, BACKEND_PROVIDER_BASE_URL, BACKEND_PROVIDER_CAR_ID)
class User(flask_login.UserMixin):
pass
@login_manager.user_loader
def user_loader(email):
if email not in users:
return
user = User()
user.id = email
return user
@login_manager.request_loader
def request_loader(request):
email = request.form.get('email')
if email not in users:
return
user = User()
user.id = email
return user
@app.route(BASE_URL + '/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html.j2')
else:
email = request.form['email']
if email in users and request.form['password'] == users[email]['password']:
if 'remember-me' in request.form:
remember_me = True
else:
remember_me = False
user = User()
user.id = email
flask_login.login_user(user, remember=remember_me)
return redirect(url_for('map_admin'))
else:
return render_template('login.html.j2', success=False)
@app.route(BASE_URL + '/logout')
def logout():
flask_login.logout_user()
return render_template('login.html.j2', logout=True)
@login_manager.unauthorized_handler
def unauthorized_handler():
return redirect(url_for('login'))
@app.route(BASE_URL + '/')
def homepage():
return render_template('index.html.j2')
@app.route(BASE_URL + '/<shortuuid>')
def map(shortuuid):
result = db.session.query(Share).where(Share.shortuuid == shortuuid).first()
if result:
if result.expiry > time.time():
teslalogger = carstate(shortuuid)
return render_template('map.html.j2',
mbtoken=MAPBOX_TOKEN,
eta_data=teslalogger,
shortuuid=shortuuid,
BASE_URL=BASE_URL)
else:
return('Link Expired')
else:
return('Link Invalid')
@app.route(BASE_URL + '/carstate/<shortuuid>')
def carstate(shortuuid):
result = db.session.query(Share).where(Share.shortuuid == shortuuid).first()
if result:
if result.expiry > time.time():
# Update Attribute for car_id if in DB result else use ENV variable - only if MultiCar is enabled
if BACKEND_PROVIDER_MULTICAR == 'True':
if result.carid:
new_car_id = result.carid
else:
new_car_id = BACKEND_PROVIDER_CAR_ID
BackendProviderFactory.provider.car_id = new_car_id
provider = BackendProviderFactory.get_instance()
provider.refresh_data()
# Reset temp_carstate to make sure we don't carry over information from past trips or other cars
temp_carstate = None
temp_carstate = vars(provider)
# Check if ETA destination is similar and use Tesla provided destination if it's within 250m
# destination_db = (lat, lng)
if provider.active_route_destination:
if not result.lat or not result.lng:
# If the lat/lon for the destination was not set on the shared link stored in DB, use the destination set in Tesla
temp_carstate['eta_destination_lat'] = provider.active_route_latitude
temp_carstate['eta_destination_lng'] = provider.active_route_longitude
temp_carstate['eta_destination_tesla_seconds'] = provider.active_route_seconds_to_arrival
temp_carstate['eta_destination_tesla_battery_level'] = provider.active_route_energy_at_arrival
else:
temp_carstate['eta_destination_lat'] = result.lat
temp_carstate['eta_destination_lng'] = result.lng
temp_carstate['eta_waypoint_lat'] = provider.active_route_latitude
temp_carstate['eta_waypoint_lng'] = provider.active_route_longitude
else:
temp_carstate['eta_destination_lat'] = result.lat
temp_carstate['eta_destination_lng'] = result.lng
return temp_carstate
else:
return('Link Expired'), 410
else:
return('Link Invalid'), 404
@app.route(BASE_URL + '/admin', methods = ['POST', 'GET'])
@flask_login.login_required
def map_admin():
# Update Attribute for car_id if in DB result else use ENV variable
if BACKEND_PROVIDER_MULTICAR == 'True':
if request.args.get('carid'):
new_car_id = request.args.get('carid')
else:
new_car_id = BACKEND_PROVIDER_CAR_ID
BackendProviderFactory.provider.car_id = new_car_id
else:
new_car_id = BACKEND_PROVIDER_CAR_ID
if request.method == 'POST':
# GENERATE SHORTUUID:
uuid = shortuuid.uuid()
data = request.form
lat = data['lat']
lng = data['lng']
lat_to_insert = float(lat) if lat else None
lng_to_insert = float(lng) if lng else None
expiry_epoch = datetime.strptime(data['expiry'], '%Y-%m-%dT%H:%M').timestamp()
new_share = Share(shortuuid=uuid, lat=lat_to_insert , lng=lng_to_insert, expiry=expiry_epoch, carid=new_car_id)
db.session.add(new_share)
db.session.commit()
result = db.session.query(Share).where(Share.expiry > time.time()).all()
provider = BackendProviderFactory.get_instance()
provider.refresh_data()
if 'uuid' in locals():
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, uuid=uuid, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude], multicar=BACKEND_PROVIDER_MULTICAR, carid=new_car_id)
else:
return render_template('map_admin.html.j2', result=result, BASE_URL=BASE_URL, mbtoken=MAPBOX_TOKEN, car_location=[provider.longitude, provider.latitude], multicar=BACKEND_PROVIDER_MULTICAR, carid=new_car_id)
@app.template_filter('fromtimestamp')
def _jinja2_filter_datetime(date, fmt=None):
timestamp = datetime.fromtimestamp(date)
return timestamp
if __name__ == '__main__':
app.run(host='0.0.0.0', port=PORT)