Files
appRobotDriver/server/InfoServer.js
2026-06-11 22:05:45 +02:00

87 lines
3.6 KiB
JavaScript

// server/InfoServer.js
'use strict';
const express = require('express');
const https = require('https');
const path = require('path');
const robotConfigService = require('./RobotConfigService');
const PUBLIC_DIR = path.join(__dirname, '..', 'public');
function createInfoServer(httpsOptions, sharedState, robot, GCode, senders, options = {}) {
const app = express();
// ── Statische Dateien ────────────────────────────────────────────────────
const staticFile = (file) => (req, res) =>
res.sendFile(path.join(PUBLIC_DIR, file), err => {
if (err) res.status(404).end('Not found');
});
app.get('/', staticFile('index.html'));
app.get('/app.js', staticFile('app.js'));
app.get('/style.css', staticFile('style.css'));
app.get('/allApps.css', staticFile('allApps.css'));
// ── API ──────────────────────────────────────────────────────────────────
app.get('/api/status', (req, res) => {
const sendersStatus = senders.map(({ name, instance }) => {
const status = instance?.getStatus ? instance.getStatus() : {
state: instance?.isTestMode ? 'connected' : instance?.tSocket ? 'connected' : 'disconnected',
url: instance?.url || null,
error: instance?.error || null,
isTestMode: !!instance?.isTestMode,
reconnectAttempt: instance?.reconnectAttempt || 0,
reconnectTimer: !!instance?.reconnectTimer
};
const state = status.state || (instance?.tSocket ? 'connected' : 'disconnected');
const health = state === 'connected' ? 'ok'
: state === 'reconnecting' ? 'warning'
: 'disconnected';
const reason = state === 'disconnected'
? status.error || 'no active socket connection'
: undefined;
return {
name,
state,
url: status.url || null,
isTestMode: !!status.isTestMode,
error: status.error || null,
reconnectAttempt: status.reconnectAttempt || 0,
reconnectTimer: !!status.reconnectTimer,
health,
reason
};
});
const connectedSenders = sendersStatus.filter(s => s.health === 'ok').length;
res.json({
generatedAt: new Date().toISOString(),
health: {
ok: sendersStatus.length > 0 && sendersStatus.every(s => s.health === 'ok'),
connectedSenders,
totalSenders: sendersStatus.length
},
clients: sharedState.connectedClients,
senders: sendersStatus,
lastCommands: sharedState.lastCommands,
lastPings: sharedState.lastPings
});
});
app.get('/api/position', (req, res) => {
res.json(JSON.parse(GCode.getM114(robot)));
});
// ── Robot-Config-Service ─────────────────────────────────────────────────
robotConfigService.register(app, { apiKey: options.apiKey });
// ── 404 ──────────────────────────────────────────────────────────────────
app.use((req, res) => res.status(404).end('Not found'));
return https.createServer(httpsOptions, app);
}
module.exports = createInfoServer;