-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflasktest.py
203 lines (168 loc) · 5.76 KB
/
flasktest.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
from flask import Flask
from flask_socketio import SocketIO
from flask import jsonify
from flask import render_template
from flask import request
import threading
import logging
import time
import random
from trinamic_jobs import *
from threaded_jobs import *
from buttons import *
import atexit
button_event = threading.Event()
button_thread = threading.Thread(name="Button_Handler", target=read_button_task, args=(button_event,))
def create_app():
flapp = Flask(__name__)
def interrupt():
print ("Shutting down Button Thread")
global button_thread
button_thread.set()
def start_button():
global button_thread
button_thread.start()
start_button()
atexit.register(interrupt)
return flapp
app = create_app()
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
def do_config(formargs):
global config
global buttons
print ('formargs:{}'.format(formargs.items()))
for key, value in formargs.items():
if 'submit' in key:
if value == 'SYSTEM':
process_trinamic_config(formargs)
def job_queue(job, parameter=None, parameter2=None):
if job == 'test':
test_run(parameter)
elif job == 'tcycle':
time_run(parameter)
elif job == 'scycle':
speed_run(parameter,parameter2)
elif job == 'current':
set_current(parameter)
elif job == 'trajectory_to':
trajectory_to(parameter,parameter2)
elif job == 'cancel_trajectory':
cancel_trajectory(odrive_event)
elif job == 'move_to':
move_to(parameter)
elif job == 'set_state':
set_state(parameter)
elif job == 'cycle':
odrive_event.set()
cycle_thread = threading.Thread(name='Cycle',
target=do_cycle,
args=(odrive_event,parameter,parameter2))
cycle_thread.start()
elif job == 'set_dynamic':
set_dynamic(parameter,parameter2)
elif job == 'do_dynamic':
odrive_event.set()
dynamic_thread = threading.Thread(name='Dynamic',
target=dynamic_cycle,
args=(odrive_event,parameter,parameter2))
dynamic_thread.start()
@app.route('/')
def render_index():
all_threads = threading.enumerate()
print (str(all_threads))
return render_template('index.html', name='No Operation')
@app.route('/config')
def show_config():
do_config(request.args)
return render_template('config.html', name='No Operation')
@app.route('/json_data')
def json_data():
global thread_data
return jsonify(**thread_data)
@app.route('/json_config')
def json_config():
global config
return jsonify(**config)
@app.route('/button_config')
def button_config():
global buttons
return jsonify(**buttons)
@app.route('/queue/<job>')
@app.route('/queue/<job>/<parameter>')
@app.route('/queue/<job>/<parameter>/<parameter2>')
def index_do(job, parameter=None, parameter2=None):
job_queue(job,parameter,parameter2)
return render_template('index.html', name='Job Queued:' + job)
@app.route('/command')
@app.route('/command/<job>')
@app.route('/command/<job>/<parameter>')
@app.route('/command/<job>/<parameter>/<parameter2>')
def command_queue(job="NoJob", parameter=None, parameter2=None):
job_queue(job,parameter,parameter2)
return render_template('command.html', name='Job Queued:' + job)
@app.route('/docs')
def show_docs():
return render_template('docs.html', name='Show Docs')
@app.route('/login')
def show_login():
return render_template('login.html', name='Login')
@app.route('/dynamic')
def show_dynamic():
return render_template('dynamic.html', name='Dynamic')
@app.route('/contact')
def show_contact():
return render_template('contact.html', name='Show Contact Page')
@app.route('/action')
def show_action():
return render_template('action.html', name='SocketIO Action Page')
#
# SOCKETIO STUFF NEXT
#
@socketio.on('connect')
def handle_connect():
print ("==================== Accepted Connection")
socketio.emit('accept', {'name':'NeoPixel Driver'})
@socketio.on('login')
def handle_login(message):
print ("=== LOGIN ATTEMPT =================")
# There isn't a login yet, this is for Kev's template testing
socketio.emit('login_accepted', {'page':'/'})
@socketio.on('message')
def handle_message(message):
print('===================== Received message: ' + repr(message))
socketio.emit('message',{'status':'good'})
@socketio.on('read_pos')
def handle_read_position(message):
print('READ_POS===================== Received message: ' + repr(message))
position = read_position()
socketio.emit('Position',{'location': message['location'], 'position': position})
@socketio.on('get_data')
def handle_get_data(message):
global thread_data
get_motor_data()
#print('===================== Received message: ' + repr(message))
socketio.emit('motor_data',thread_data)
@socketio.on('do_job')
def handle_do_job(message):
print('===================== Received message: ' + repr(message))
message = dict(message)
for k,v in message.items():
job = k
params = v.split('/')
parameter = params[0]
if len(params) > 1:
parameter2 = params[1]
else:
parameter2 = None
job_queue(job, parameter, parameter2)
handle_get_data('update_clients')
@socketio.on('save_journey')
def handle_save_journey(message):
print('==================== SAVE JOURNEY: {}'.format(message))
save_journey(message)
@socketio.on('load_journey')
def handle_load_journey(message):
print('==================== LOAD JOURNEY: {}'.format(message))
socketio.emit('load_journey', load_journey())
socketio.run(app, host='0.0.0.0', debug=True)