-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (80 loc) · 2.96 KB
/
app.py
File metadata and controls
106 lines (80 loc) · 2.96 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
"""
Flask application for CarPlay dongle detection and communication.
Uses WebSockets for real-time communication with the frontend.
"""
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
from flask_cors import CORS
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key-change-this'
# Enable CORS for development
CORS(app)
# Initialize SocketIO with CORS settings
socketio = SocketIO(app, cors_allowed_origins="*")
# Store connected clients and their dongle status
connected_clients = {}
@app.route('/')
def index():
"""Serve the main page"""
return render_template('index.html')
@socketio.on('connect')
def handle_connect():
"""Handle new WebSocket connections"""
print(f'Client connected: {request.sid}')
connected_clients[request.sid] = {'dongle_connected': False}
emit('connection_response', {'status': 'connected'})
@socketio.on('disconnect')
def handle_disconnect():
"""Handle client disconnections"""
print(f'Client disconnected: {request.sid}')
if request.sid in connected_clients:
del connected_clients[request.sid]
@socketio.on('dongle_connected')
def handle_dongle_connected(data):
"""Handle dongle connection event from frontend"""
print(f'Dongle connected: {data}')
connected_clients[request.sid]['dongle_connected'] = True
connected_clients[request.sid]['dongle_info'] = data
# Broadcast to all clients (or just respond to sender)
emit('dongle_status', {
'connected': True,
'device_info': data
})
@socketio.on('dongle_disconnected')
def handle_dongle_disconnected():
"""Handle dongle disconnection event from frontend"""
print('Dongle disconnected')
if request.sid in connected_clients:
connected_clients[request.sid]['dongle_connected'] = False
emit('dongle_status', {'connected': False})
@socketio.on('usb_data')
def handle_usb_data(data):
"""Handle USB data received from the dongle"""
print(f'Received USB data: {data}')
# Process the data here
# For now, just echo it back
emit('usb_response', {
'status': 'received',
'data': data
})
@socketio.on('send_command')
def handle_send_command(data):
"""Handle command to send to dongle"""
command = data.get('command')
print(f'Command to send to dongle: {command}')
# Echo back to frontend, which will actually send via WebUSB
emit('send_to_dongle', {
'command': command,
'timestamp': data.get('timestamp')
})
if __name__ == '__main__':
# Run with socketio
print('Starting Flask-SocketIO server...')
print('Open https://localhost:5000 in your browser')
print('Note: WebUSB requires HTTPS. See README for certificate setup.')
# For development with self-signed certificate:
socketio.run(app,
host='0.0.0.0',
port=5000,
debug=True,
ssl_context='adhoc') # Creates self-signed cert