Konfig in robot.json
This commit is contained in:
@@ -1,102 +1,86 @@
|
||||
// server/InfoServer.js
|
||||
const fs = require('fs');
|
||||
const https = require('https');
|
||||
'use strict';
|
||||
|
||||
function createInfoServer(httpsOptions, sharedState, robot, GCode, senders) {
|
||||
return https.createServer(httpsOptions, (req, res) => {
|
||||
const express = require('express');
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
const robotConfigService = require('./RobotConfigService');
|
||||
|
||||
if (req.url === '/') {
|
||||
return serveFile(res, './public/index.html', 'text/html');
|
||||
}
|
||||
const PUBLIC_DIR = path.join(__dirname, '..', 'public');
|
||||
|
||||
if (req.url === '/app.js') {
|
||||
return serveFile(res, './public/app.js', 'application/javascript');
|
||||
}
|
||||
function createInfoServer(httpsOptions, sharedState, robot, GCode, senders, options = {}) {
|
||||
const app = express();
|
||||
|
||||
if (req.url === '/style.css') {
|
||||
return serveFile(res, './public/style.css', 'text/css');
|
||||
}
|
||||
// ── Statische Dateien ────────────────────────────────────────────────────
|
||||
const staticFile = (file) => (req, res) =>
|
||||
res.sendFile(path.join(PUBLIC_DIR, file), err => {
|
||||
if (err) res.status(404).end('Not found');
|
||||
});
|
||||
|
||||
if (req.url === '/allApps.css') {
|
||||
return serveFile(res, './public/allApps.css', 'text/css');
|
||||
}
|
||||
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 ---------- */
|
||||
if (req.url === '/api/status') {
|
||||
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;
|
||||
const health = {
|
||||
ok: sendersStatus.length > 0 && sendersStatus.every(s => s.health === 'ok'),
|
||||
connectedSenders,
|
||||
totalSenders: sendersStatus.length
|
||||
// ── 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 status = {
|
||||
generatedAt: new Date().toISOString(),
|
||||
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,
|
||||
clients: sharedState.connectedClients,
|
||||
senders: sendersStatus,
|
||||
lastCommands: sharedState.lastCommands,
|
||||
lastPings: sharedState.lastPings
|
||||
reason
|
||||
};
|
||||
});
|
||||
|
||||
res.writeHead(200, {'Content-Type': 'application/json'});
|
||||
return res.end(JSON.stringify(status));
|
||||
}
|
||||
|
||||
if (req.url === '/api/position') {
|
||||
res.writeHead(200, {'Content-Type': 'application/json'});
|
||||
return res.end(GCode.getM114(robot));
|
||||
}
|
||||
|
||||
res.writeHead(404);
|
||||
res.end('Not found');
|
||||
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);
|
||||
}
|
||||
|
||||
/* ---------- Helper ---------- */
|
||||
|
||||
function serveFile(res, path, type) {
|
||||
fs.readFile(path, (err, data) => {
|
||||
if (err) {
|
||||
res.writeHead(404);
|
||||
return res.end('Not found');
|
||||
}
|
||||
res.writeHead(200, {'Content-Type': type});
|
||||
res.end(data);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = createInfoServer;
|
||||
module.exports = createInfoServer;
|
||||
|
||||
Reference in New Issue
Block a user