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;
|
||||
|
||||
176
server/RobotConfigService.js
Normal file
176
server/RobotConfigService.js
Normal file
@@ -0,0 +1,176 @@
|
||||
// server/RobotConfigService.js
|
||||
//
|
||||
// Self-contained Express-Modul für die robot.json-Verwaltung.
|
||||
// Einbinden mit einer Zeile:
|
||||
//
|
||||
// const robotConfigService = require('./RobotConfigService');
|
||||
// robotConfigService.register(app, { apiKey: process.env.ROBOT_API_KEY });
|
||||
//
|
||||
// Routen:
|
||||
// GET /api/robot → aktuelle robot.json
|
||||
// PUT /api/robot → überschreibt robot.json, legt Snapshot an (Auth)
|
||||
// GET /api/robot/history → Liste aller Snapshots
|
||||
// GET /api/robot/history/:ts → einen bestimmten Snapshot abrufen
|
||||
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const fsp = require('fs/promises');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const DATA_DIR = path.join(__dirname, '..', 'data', 'robot');
|
||||
const ROBOT_JSON = path.join(DATA_DIR, 'robot.json');
|
||||
const KEY_FILE = path.join(DATA_DIR, '.apikey');
|
||||
|
||||
// ── Timestamp ────────────────────────────────────────────────────────────────
|
||||
|
||||
function makeTimestamp() {
|
||||
const now = new Date();
|
||||
const p = (n, w = 2) => String(n).padStart(w, '0');
|
||||
return `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())}_${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`;
|
||||
}
|
||||
|
||||
// ── API-Key-Auflösung ────────────────────────────────────────────────────────
|
||||
|
||||
function resolveApiKey(provided) {
|
||||
if (provided) return provided;
|
||||
try {
|
||||
return fs.readFileSync(KEY_FILE, 'utf8').trim();
|
||||
} catch {
|
||||
fs.mkdirSync(DATA_DIR, { recursive: true });
|
||||
const key = crypto.randomBytes(24).toString('hex');
|
||||
fs.writeFileSync(KEY_FILE, key, 'utf8');
|
||||
console.log(`[RobotConfigService] Kein ROBOT_API_KEY gesetzt — neuer Key generiert: ${key}`);
|
||||
console.log(`[RobotConfigService] Gespeichert in: ${KEY_FILE}`);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Datei-Operationen ────────────────────────────────────────────────────────
|
||||
|
||||
async function readRobotJson() {
|
||||
const raw = await fsp.readFile(ROBOT_JSON, 'utf8');
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
|
||||
async function writeRobotJson(data) {
|
||||
await fsp.mkdir(DATA_DIR, { recursive: true });
|
||||
const ts = makeTimestamp();
|
||||
const snapshotFile = `robot_${ts}.json`;
|
||||
|
||||
try {
|
||||
const current = await fsp.readFile(ROBOT_JSON, 'utf8');
|
||||
await fsp.writeFile(path.join(DATA_DIR, snapshotFile), current, 'utf8');
|
||||
} catch {
|
||||
// Noch kein robot.json vorhanden — kein Snapshot nötig
|
||||
}
|
||||
|
||||
await fsp.writeFile(ROBOT_JSON, JSON.stringify(data, null, 2), 'utf8');
|
||||
await pruneSnapshots();
|
||||
return { snapshotFile };
|
||||
}
|
||||
|
||||
async function listHistory() {
|
||||
try {
|
||||
const files = await fsp.readdir(DATA_DIR);
|
||||
return files
|
||||
.filter(f => /^robot_\d{8}_\d{6}\.json$/.test(f))
|
||||
.sort()
|
||||
.reverse()
|
||||
.map(f => ({ filename: f, day: f.slice(6, 14), timestamp: f.slice(15, 21) }));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function readSnapshot(ts) {
|
||||
const raw = await fsp.readFile(path.join(DATA_DIR, `robot_${ts}.json`), 'utf8');
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
|
||||
// ── Pruning ──────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// Regel: Pro Tag maximal 100 Snapshots.
|
||||
// Hat ein Tag mehr als 100, wird nur der neueste behalten.
|
||||
|
||||
async function pruneSnapshots() {
|
||||
let files;
|
||||
try { files = await fsp.readdir(DATA_DIR); } catch { return; }
|
||||
|
||||
const byDay = {};
|
||||
for (const f of files) {
|
||||
if (!/^robot_\d{8}_\d{6}\.json$/.test(f)) continue;
|
||||
const day = f.slice(6, 14);
|
||||
if (!byDay[day]) byDay[day] = [];
|
||||
byDay[day].push(f);
|
||||
}
|
||||
|
||||
for (const [day, dayFiles] of Object.entries(byDay)) {
|
||||
if (dayFiles.length <= 100) continue;
|
||||
dayFiles.sort(); // älteste zuerst
|
||||
const toDelete = dayFiles.slice(0, -1); // neueste behalten
|
||||
for (const f of toDelete) {
|
||||
try { await fsp.unlink(path.join(DATA_DIR, f)); } catch { /* ignorieren */ }
|
||||
}
|
||||
console.log(`[RobotConfigService] Tag ${day}: ${toDelete.length} Snapshots bereinigt, behalten: ${dayFiles[dayFiles.length - 1]}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Auth ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
function isAuthorized(req, key) {
|
||||
return (req.headers['authorization'] ?? '') === `Bearer ${key}`;
|
||||
}
|
||||
|
||||
// ── Registrierung ─────────────────────────────────────────────────────────────
|
||||
|
||||
function register(app, options = {}) {
|
||||
const apiKey = resolveApiKey(options.apiKey);
|
||||
const router = express.Router();
|
||||
router.use(express.json({ limit: '5mb' }));
|
||||
|
||||
router.get('/api/robot/history', async (req, res) => {
|
||||
try {
|
||||
return res.json({ history: await listHistory() });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/api/robot/history/:ts', async (req, res) => {
|
||||
try {
|
||||
return res.json(await readSnapshot(req.params.ts));
|
||||
} catch {
|
||||
return res.status(404).json({ error: 'Snapshot nicht gefunden' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/api/robot', async (req, res) => {
|
||||
try {
|
||||
return res.json(await readRobotJson());
|
||||
} catch {
|
||||
return res.status(404).json({ error: 'robot.json nicht gefunden' });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/api/robot', async (req, res) => {
|
||||
if (!isAuthorized(req, apiKey)) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
if (!req.body || typeof req.body !== 'object' || Array.isArray(req.body)) {
|
||||
return res.status(400).json({ error: 'Body muss ein JSON-Objekt sein' });
|
||||
}
|
||||
try {
|
||||
const result = await writeRobotJson(req.body);
|
||||
return res.json({ ok: true, ...result });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
}
|
||||
|
||||
module.exports = { register, readRobotJson, writeRobotJson, listHistory, readSnapshot };
|
||||
Reference in New Issue
Block a user