64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
import base64
|
|
|
|
# import eventlet
|
|
# eventlet.monkey_patch()
|
|
|
|
from flask import Flask, jsonify, send_file, Response
|
|
from flask_socketio import SocketIO
|
|
|
|
from control.camera import Camera
|
|
from control.walle import WallE
|
|
|
|
DATA_URL_PREFIX = 'data:image/jpeg;base64,'
|
|
# DEBUG = True
|
|
|
|
boundary = 'lkajflkasdjlkfaj'
|
|
|
|
app = Flask(__name__, static_url_path='', static_folder='client/dist')
|
|
app.config.from_object(__name__)
|
|
sio = SocketIO(app)
|
|
|
|
# camera = Camera()
|
|
walle = WallE()
|
|
|
|
|
|
def camera_thread():
|
|
while True:
|
|
for image in camera.generate_images():
|
|
if not image:
|
|
break
|
|
image_url = DATA_URL_PREFIX + base64.b64encode(image).decode('ascii')
|
|
sio.emit('camera_image', {
|
|
'image': image_url,
|
|
}, broadcast=True)
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return send_file('client/dist/index.html')
|
|
|
|
|
|
@app.route('/imagestream.mjpg')
|
|
def image_stream():
|
|
# cam = Camera()
|
|
# return Response(cam.mjpeg_stream(boundary.encode()),
|
|
# mimetype='multipart/x-mixed-replace; boundary=lkajflkasdjlkfaj')
|
|
return ""
|
|
|
|
|
|
@sio.on("camera")
|
|
def camera_message(directions):
|
|
walle.set_eye_velocity(directions['angle'], directions['force'])
|
|
print(f"Moving camera in direction {directions['angle']:f} with velocity {directions['force']}")
|
|
|
|
|
|
@sio.on("move")
|
|
def move_message(directions):
|
|
walle.set_movement(directions['angle'], directions['force'])
|
|
print(f"Moving in direction {directions['angle']:f} with velocity {directions['force']}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# eventlet.spawn(camera_thread)
|
|
sio.run(app, host='0.0.0.0')
|