-
Notifications
You must be signed in to change notification settings - Fork 44
/
simulator.py
executable file
·141 lines (115 loc) · 4.26 KB
/
simulator.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
#!/usr/bin/env python
# all the imports
from flask import Flask, request, session, redirect, url_for, \
render_template, make_response
from contextlib import closing
import state_manager
# configuration
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
def _make_status_response(status):
response = make_response()
response.status_code = status
return response
@app.route('/')
def vehicle_data():
global gState
return render_template('vehicle_controls.html', IP=gState.local_ip,
accelerator=gState.accelerator_pedal_position,
angle=gState.steering_wheel_angle,
received_messages=list(reversed(gState.received_messages()))[:25])
@app.route('/stop', methods=['POST'])
def stop():
#Stop the automatic updates
session['updates_paused'] = True
global gState
gState.pause()
return redirect(url_for('vehicle_data'))
@app.route('/single', methods=['POST'])
def single():
#make a global socket
global gState
gState.update_once()
return redirect(url_for('vehicle_data'))
@app.route('/start', methods=['POST'])
def start():
#make a global socket
session.pop('updates_paused', None)
global gState
gState.resume()
return redirect(url_for('vehicle_data'))
@app.route('/custom-json', methods=['POST'])
def send_custom_json():
custom_json_payload = request.form['custom_message_payload']
session['custom_message_payload'] = custom_json_payload
gState.send_json(custom_json_payload)
return redirect(url_for('vehicle_data'))
@app.route('/custom-message', methods=['POST'])
def send_custom_message():
name = request.form['custom_message_name']
value = request.form['custom_message_value']
event = request.form['custom_message_event']
session['custom_message_name'] = name
session['custom_message_value'] = value
session['custom_message_event'] = event
gState.send_callback(name, value, event)
return redirect(url_for('vehicle_data'))
@app.route('/_set_data', methods=['POST'])
def set_data():
global gState
name = request.form['name']
if name == "angle":
gState.steering_wheel_angle = float(request.form['value'])
elif name == "accelerator":
gState.accelerator_pedal_position = float(request.form['value'])
elif name == "brake":
gState.brake_pedal_position = float(request.form['value'])
elif name == "parking_brake_status":
gState.parking_brake_status = python_bool(request.form['value'])
elif name == "ignition_status":
gState.ignition_status = request.form['value']
elif name == "manual_trans_status":
gState.manual_trans_status = python_bool(request.form['value'])
elif name == "headlamp_status":
gState.headlamp_status = python_bool(request.form['value'])
elif name == "high_beam_status":
gState.high_beam_status = python_bool(request.form['value'])
elif name == "windshield_wiper_status":
gState.windshield_wiper_status = python_bool(request.form['value'])
elif name == "door_status":
gState.update_door(request.form['value'], python_bool(
request.form['event']))
elif name == "gear_lever_position":
gState.gear_lever_position = request.form['value']
elif name == "latitude":
gState.latitude = float(request.form['value'])
elif name == "longitude":
gState.longitude = float(request.form['value'])
elif name == "upshift":
gState.upshift()
elif name == "downshift":
gState.downshift()
else:
print("Unsupported data received from UI: " + str(request.form))
return _make_status_response(201)
def python_bool(value):
if value == "true":
return True
if value == "false":
return False
else:
return None
@app.route('/_get_data')
def get_data():
return gState.dynamics_data
if __name__ == '__main__':
global gState
gState = state_manager.StateManager()
flask_port = 50000
print('For the UI, navigate a browser to localhost:' + str(flask_port))
app.run(use_reloader=False, host='0.0.0.0', port=flask_port)