143 lines
5.3 KiB
Python
143 lines
5.3 KiB
Python
from flask import Flask, render_template, request, send_from_directory
|
|
from flask_socketio import SocketIO, emit, join_room
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = 'your-secret-key-here'
|
|
socketio = SocketIO(app, cors_allowed_origins="*")
|
|
|
|
# Store connected clients
|
|
connected_clients = set()
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Main page - redirects to client"""
|
|
return render_template('index.html')
|
|
|
|
@app.route('/client')
|
|
def client():
|
|
"""Client page that receives events"""
|
|
return render_template('client.html')
|
|
|
|
@app.route('/controller')
|
|
def controller():
|
|
"""Controller page that sends events"""
|
|
return render_template('controller.html')
|
|
|
|
@app.route('/mechanical-counter')
|
|
def mechanical_counter():
|
|
"""Mechanical counter client page"""
|
|
return render_template('mechanical_counter.html')
|
|
|
|
@app.route('/mechanical-counter-controller')
|
|
def mechanical_counter_controller():
|
|
"""Mechanical counter controller page"""
|
|
return render_template('mechanical_counter_controller.html')
|
|
|
|
@app.route('/static/<path:filename>')
|
|
def static_files(filename):
|
|
"""Serve static files"""
|
|
return send_from_directory('static', filename)
|
|
|
|
@socketio.on('connect')
|
|
def handle_connect():
|
|
"""Handle client connection"""
|
|
print(f'Client connected: {request.sid}')
|
|
connected_clients.add(request.sid)
|
|
emit('status', {'message': 'Connected to server'})
|
|
|
|
@socketio.on('disconnect')
|
|
def handle_disconnect():
|
|
"""Handle client disconnection"""
|
|
print(f'Client disconnected: {request.sid}')
|
|
connected_clients.discard(request.sid)
|
|
|
|
@socketio.on('send_event')
|
|
def handle_send_event(data):
|
|
"""Handle event from controller to broadcast to clients"""
|
|
print(f'Broadcasting event: {data}')
|
|
# Broadcast to all connected clients except sender
|
|
emit('receive_event', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('counter_update')
|
|
def handle_counter_update(data):
|
|
"""Handle counter updates from controller"""
|
|
print(f'Counter update: {data}')
|
|
# Broadcast counter update to all clients
|
|
emit('counter_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('position_update')
|
|
def handle_position_update(data):
|
|
"""Handle position updates from controller"""
|
|
print(f'Position update received: {data}')
|
|
print(f'Broadcasting to {len(connected_clients)} clients')
|
|
# Broadcast position update to all clients
|
|
emit('position_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('viewport_update')
|
|
def handle_viewport_update(data):
|
|
"""Handle viewport updates from client"""
|
|
print(f'Viewport update received: {data}')
|
|
print(f'Broadcasting viewport update to {len(connected_clients)} clients')
|
|
# Broadcast viewport update to all clients (including controller)
|
|
emit('viewport_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('request_viewport')
|
|
def handle_request_viewport(data):
|
|
"""Handle viewport request from controller"""
|
|
print(f'Viewport request received: {data}')
|
|
# Broadcast viewport request to all clients
|
|
emit('request_viewport', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('font_size_update')
|
|
def handle_font_size_update(data):
|
|
"""Handle font size updates from controller"""
|
|
print(f'Font size update received: {data}')
|
|
print(f'Broadcasting font size update to {len(connected_clients)} clients')
|
|
# Broadcast font size update to all clients
|
|
emit('font_size_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('wheel_size_update')
|
|
def handle_wheel_size_update(data):
|
|
"""Handle wheel size updates from controller"""
|
|
print(f'Wheel size update received: {data}')
|
|
print(f'Broadcasting wheel size update to {len(connected_clients)} clients')
|
|
# Broadcast wheel size update to all clients
|
|
emit('wheel_size_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('animation_speed_update')
|
|
def handle_animation_speed_update(data):
|
|
"""Handle animation speed updates from controller"""
|
|
print(f'Animation speed update received: {data}')
|
|
print(f'Broadcasting animation speed update to {len(connected_clients)} clients')
|
|
# Broadcast animation speed update to all clients
|
|
emit('animation_speed_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('brightness_update')
|
|
def handle_brightness_update(data):
|
|
"""Handle brightness updates from controller"""
|
|
print(f'Brightness update received: {data}')
|
|
print(f'Broadcasting brightness update to {len(connected_clients)} clients')
|
|
# Broadcast brightness update to all clients
|
|
emit('brightness_update', data, broadcast=True, include_self=False)
|
|
|
|
@socketio.on('join_room')
|
|
def handle_join_room(data):
|
|
"""Handle client joining a specific room"""
|
|
room = data.get('room', 'default')
|
|
join_room(room)
|
|
emit('status', {'message': f'Joined room: {room}'})
|
|
|
|
@socketio.on('send_to_room')
|
|
def handle_send_to_room(data):
|
|
"""Handle sending event to specific room"""
|
|
room = data.get('room', 'default')
|
|
event_data = data.get('event', {})
|
|
print(f'Sending to room {room}: {event_data}')
|
|
emit('receive_event', event_data, room=room, include_self=False)
|
|
|
|
if __name__ == '__main__':
|
|
print("Starting webserver on port 8079...")
|
|
print("Client page: http://localhost:8079/client")
|
|
print("Controller page: http://localhost:8079/controller")
|
|
socketio.run(app, host='0.0.0.0', port=8079, debug=True) |