feat: use gateway
This commit is contained in:
@@ -6,13 +6,16 @@ const WebSocket = require('ws');
|
||||
const path = require('path');
|
||||
|
||||
// Import services
|
||||
const UdpDiscovery = require('./udp-discovery');
|
||||
const UdpSender = require('./udp-discovery');
|
||||
const GatewayClient = require('./gateway-client');
|
||||
const PresetRegistry = require('../presets/preset-registry');
|
||||
|
||||
class LEDLabServer {
|
||||
constructor(options = {}) {
|
||||
this.port = options.port || 8080;
|
||||
this.udpPort = options.udpPort || 4210;
|
||||
this.gatewayUrl = options.gatewayUrl || 'http://localhost:3001';
|
||||
this.filterAppLabel = options.filterAppLabel || 'pixelstream';
|
||||
this.matrixWidth = options.matrixWidth || 16;
|
||||
this.matrixHeight = options.matrixHeight || 16;
|
||||
this.fps = options.fps || 20;
|
||||
@@ -21,7 +24,11 @@ class LEDLabServer {
|
||||
this.server = http.createServer(this.app);
|
||||
this.wss = new WebSocket.Server({ server: this.server });
|
||||
|
||||
this.udpDiscovery = new UdpDiscovery(this.udpPort);
|
||||
this.udpSender = new UdpSender(this.udpPort);
|
||||
this.gatewayClient = new GatewayClient({
|
||||
gatewayUrl: this.gatewayUrl,
|
||||
filterAppLabel: this.filterAppLabel || 'pixelstream'
|
||||
});
|
||||
this.presetRegistry = new PresetRegistry();
|
||||
|
||||
// Legacy single-stream support (kept for backwards compatibility)
|
||||
@@ -37,7 +44,7 @@ class LEDLabServer {
|
||||
|
||||
this.setupExpress();
|
||||
this.setupWebSocket();
|
||||
this.setupUdpDiscovery();
|
||||
this.setupGatewayClient();
|
||||
this.setupPresetManager();
|
||||
}
|
||||
|
||||
@@ -47,7 +54,7 @@ class LEDLabServer {
|
||||
|
||||
// API routes
|
||||
this.app.get('/api/nodes', (req, res) => {
|
||||
const nodes = this.udpDiscovery.getNodes();
|
||||
const nodes = this.gatewayClient.getNodes();
|
||||
res.json({ nodes });
|
||||
});
|
||||
|
||||
@@ -61,7 +68,7 @@ class LEDLabServer {
|
||||
streaming: this.currentPreset !== null,
|
||||
currentPreset: this.currentPresetName || null,
|
||||
matrixSize: { width: this.matrixWidth, height: this.matrixHeight },
|
||||
nodeCount: this.udpDiscovery.getNodeCount(),
|
||||
nodeCount: this.gatewayClient.getNodeCount(),
|
||||
currentTarget: this.currentTarget,
|
||||
fps: this.fps,
|
||||
});
|
||||
@@ -122,7 +129,7 @@ class LEDLabServer {
|
||||
streaming: this.currentPreset !== null,
|
||||
currentPreset: this.currentPresetName || null,
|
||||
matrixSize: { width: this.matrixWidth, height: this.matrixHeight },
|
||||
nodes: this.udpDiscovery.getNodes(),
|
||||
nodes: this.gatewayClient.getNodes(),
|
||||
presetParameters: this.currentPreset ? this.currentPreset.getParameters() : null,
|
||||
currentTarget: this.currentTarget,
|
||||
fps: this.fps,
|
||||
@@ -174,26 +181,14 @@ class LEDLabServer {
|
||||
}
|
||||
}
|
||||
|
||||
setupUdpDiscovery() {
|
||||
this.udpDiscovery.on('nodeDiscovered', (node) => {
|
||||
console.log('Node discovered:', node.ip);
|
||||
|
||||
this.broadcastToClients({
|
||||
type: 'nodeDiscovered',
|
||||
node
|
||||
});
|
||||
});
|
||||
|
||||
this.udpDiscovery.on('nodeLost', (node) => {
|
||||
console.log('Node lost:', node.ip);
|
||||
|
||||
this.broadcastToClients({
|
||||
type: 'nodeLost',
|
||||
node
|
||||
});
|
||||
});
|
||||
|
||||
this.udpDiscovery.start();
|
||||
setupGatewayClient() {
|
||||
// Start gateway client for node discovery
|
||||
this.gatewayClient.start();
|
||||
|
||||
// Start UDP sender for sending frames
|
||||
this.udpSender.start();
|
||||
|
||||
console.log('Using gateway for node discovery and UDP sender for frame streaming');
|
||||
}
|
||||
|
||||
setupPresetManager() {
|
||||
@@ -441,7 +436,9 @@ class LEDLabServer {
|
||||
if (frameData) {
|
||||
// Send to specific target
|
||||
if (this.currentTarget) {
|
||||
this.udpDiscovery.sendToNode(this.currentTarget, frameData);
|
||||
this.udpSender.sendToNode(this.currentTarget, frameData).catch(err => {
|
||||
console.error(`Error sending frame to ${this.currentTarget}:`, err);
|
||||
});
|
||||
}
|
||||
|
||||
// Send frame data to WebSocket clients for preview
|
||||
@@ -463,7 +460,9 @@ class LEDLabServer {
|
||||
if (frameData) {
|
||||
// Send to specific node
|
||||
// frameData format: "RAW:FF0000FF0000..." (RAW prefix + hex pixel data)
|
||||
this.udpDiscovery.sendToNode(nodeIp, frameData);
|
||||
this.udpSender.sendToNode(nodeIp, frameData).catch(err => {
|
||||
console.error(`Error sending frame to ${nodeIp}:`, err);
|
||||
});
|
||||
|
||||
// Send frame data to WebSocket clients for preview
|
||||
this.broadcastToClients({
|
||||
@@ -476,7 +475,7 @@ class LEDLabServer {
|
||||
}
|
||||
|
||||
sendToSpecificNode(nodeIp, message) {
|
||||
return this.udpDiscovery.sendToNode(nodeIp, message);
|
||||
return this.udpSender.sendToNode(nodeIp, message);
|
||||
}
|
||||
|
||||
broadcastCurrentState() {
|
||||
@@ -484,7 +483,7 @@ class LEDLabServer {
|
||||
streaming: this.currentPreset !== null,
|
||||
currentPreset: this.currentPresetName || null,
|
||||
matrixSize: { width: this.matrixWidth, height: this.matrixHeight },
|
||||
nodes: this.udpDiscovery.getNodes(),
|
||||
nodes: this.gatewayClient.getNodes(),
|
||||
presetParameters: this.currentPreset ? this.currentPreset.getParameters() : null,
|
||||
currentTarget: this.currentTarget,
|
||||
fps: this.fps,
|
||||
@@ -604,7 +603,8 @@ class LEDLabServer {
|
||||
startServer() {
|
||||
this.server.listen(this.port, () => {
|
||||
console.log(`LEDLab server running on port ${this.port}`);
|
||||
console.log(`UDP discovery on port ${this.udpPort}`);
|
||||
console.log(`Gateway client connecting to ${this.gatewayUrl}`);
|
||||
console.log(`UDP sender configured for port ${this.udpPort}`);
|
||||
console.log(`Matrix size: ${this.matrixWidth}x${this.matrixHeight}`);
|
||||
});
|
||||
}
|
||||
@@ -615,8 +615,9 @@ class LEDLabServer {
|
||||
// Stop streaming first
|
||||
this.stopStreaming();
|
||||
|
||||
// Stop UDP discovery
|
||||
this.udpDiscovery.stop();
|
||||
// Stop gateway client and UDP sender
|
||||
this.gatewayClient.stop();
|
||||
this.udpSender.stop();
|
||||
|
||||
// Close all WebSocket connections immediately
|
||||
this.wss.close();
|
||||
@@ -646,6 +647,8 @@ if (require.main === module) {
|
||||
const server = new LEDLabServer({
|
||||
port: process.env.PORT || 8080,
|
||||
udpPort: process.env.UDP_PORT || 4210,
|
||||
gatewayUrl: process.env.GATEWAY_URL || 'http://localhost:3001',
|
||||
filterAppLabel: process.env.FILTER_APP_LABEL || 'pixelstream',
|
||||
matrixWidth: parseInt(process.env.MATRIX_WIDTH) || 16,
|
||||
matrixHeight: parseInt(process.env.MATRIX_HEIGHT) || 16,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user