-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (91 loc) · 2.99 KB
/
app.py
File metadata and controls
116 lines (91 loc) · 2.99 KB
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
from flask import Flask, render_template, request, session
from flask_socketio import SocketIO, emit
from threading import Lock
import sys
import numpy as np
import json
#some dummy data
data = [{'foo': [1,2,3,4], 'fee': 'hello'}]
data_json = json.dumps(data)
app = Flask(__name__)
# Set this variable to "threading", "eventlet" or "gevent" to test the
# different async modes, or leave it set to None for the application to choose
# the best option based on installed packages.
async_mode = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()
#global variables to hold the params and camera
params = None
updateParams = False
camera = None
updateCamera = False
controls = None
updateControls = False
#number of seconds between updates
seconds = 0.01
#this will pass to the viewer every "seconds"
def background_thread():
"""Example of how to send server generated events to clients."""
global params, updateParams, camera, updateCamera, controls, updateControls
while True:
socketio.sleep(seconds)
if (updateParams):
print("========= params:",params)
socketio.emit('update_params', params, namespace='/test')
if (updateCamera):
socketio.emit('update_camera', camera, namespace='/test')
if (updateControls):
socketio.emit('update_controls', controls, namespace='/test')
updateParams = False
updateCamera = False
updateControls = False
#testing the connection
@socketio.on('connection_test', namespace='/test')
def connection_test(message):
session['receive_count'] = session.get('receive_count', 0) + 1
emit('connection_response',{'data': message['data'], 'count': session['receive_count']})
#sending data
@socketio.on('input_data_request', namespace='/test')
def input_data_request(message):
session['receive_count'] = session.get('receive_count', 0) + 1
emit('input_data_response',data_json)
#will receive data from gui (and print to console as a test within "from_gui")
@socketio.on('gui_input', namespace='/test')
def gui_input(message):
global params, updateParams
updateParams = True
params = message
#emit('from_gui',message)
#will receive data from camera
@socketio.on('camera_input', namespace='/test')
def camera_input(message):
global camera, updateCamera
updateCamera = True
camera = message
#will receive data from controls
@socketio.on('controls_input', namespace='/test')
def controls(message):
global controls, updateControls
updateControls = True
controls = message
#the background task sends data to the viewer
@socketio.on('connect', namespace='/test')
def from_gui():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=background_thread)
##############
#flask stuff
@app.route("/viewer")
def viewer():
return render_template("viewer.html")
@app.route("/gui")
def gui():
return render_template("gui.html")
if __name__ == "__main__":
socketio.run(app, debug=True, host='0.0.0.0', port=5000)
#app.run(host='0.0.0.0')