first draft on new dashboard

This commit is contained in:
matt committed 2026-10-14 01:05:36 -07:00
1 parent ac2b142b7a
commit 5eb8bdd677
2 files changed
+116 -468

No files matched your search

+44 -138
View File
@@ -1,153 +1,59 @@
// server.js
const http = require('http');
// server.js – minimal Express + Socket.io + Redis subscriber
const http = require('http');
const express = require('express');
const { createClient } = require('redis');
const { Server } = require('socket.io');
const fetch = require('node-fetch'); // npm i node-fetch@2
const fs = require('fs');
const yaml = require('js-yaml'); // npm i js-yaml
const path = require('path');
const { createClient } = require('redis');
const app = express();
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const io = new Server(server);
/* --------------------------------------------------------------------- */
/* ---------- 1. Load the YAML configuration file ---------------------- */
/* --------------------------------------------------------------------- */
let config = {};
try {
const filePath = '/app/cosmostat_settings.yaml';
const file = fs.readFileSync(filePath, 'utf8');
config = yaml.load(file);
} catch (e) {
console.error('Failed to load config.yaml:', e);
process.exit(1);
// ---------- 1. Settings ----------
const REDIS_URL = 'redis://0.0.0.0:6379'; // adjust if needed
const REDIS_CHANNEL = 'temp_hum'; // channel you subscribed to
// ---------- 2. Serve static files ----------
app.use(express.static('public')); // public contains index.php, src/… etc.
// ---------- 3. Redis subscriber ----------
const redisClient = createClient({ url: REDIS_URL });
redisClient.on('error', err => console.error('Redis error:', err));
async function startRedis() {
await redisClient.connect();
const sub = redisClient.duplicate();
await sub.connect();
// Helper: forward a Redis message to all Socket.io clients
const forward = (msg) => {
try {
const payload = JSON.parse(msg);
io.emit('temp_hum', payload); // event name matches the JS listener
} catch (e) {
console.error('Failed to parse Redis message:', e);
}
};
await sub.subscribe(REDIS_CHANNEL, forward);
console.log(`Subscribed to Redis channel "${REDIS_CHANNEL}"`);
}
const API_PORT = config.custom_api_port || 5000; // fallback to 5000
const API_HOST = config.api_bind_ip || '192.168.37.1'; // fallback IP
const API_BASE = `http://${API_HOST}:${API_PORT}`;
console.log('API URL:', API_BASE);
/* --------------------------------------------------------------------- */
/* ---------- 2. Socket.io -------------------------------------------- */
/* --------------------------------------------------------------------- */
io.on('connection', async socket => {
console.log('client connected:', socket.id);
startRedis().catch(console.error);
/* ------------- send cached client_summary ------------- */
if (clientSummaryCache.last) {
socket.emit('client_summary', clientSummaryCache.last);
console.log('sent cached client_summary to', socket.id);
}
// ---------- 4. Socket.io ----------
io.on('connection', socket => {
console.log('Socket.io client connected:', socket.id);
/* ----------------------------------------------------------------- */
/* Call the external API every time a client connects */
/* ----------------------------------------------------------------- */
try {
const resp = await fetch(`${API_BASE}/start_timer`, { method: 'GET' });
const data = await resp.json();
console.log('API responded to connect:', data);
} catch (err) {
console.error('Failed to hit start_timer endpoint:', err);
}
// Optionally send the most recent snapshot
// (If you keep a cache, emit it here)
/* ----------------------------------------------------------------- */
/* Listen for tableRendered event from the client */
/* ----------------------------------------------------------------- */
socket.on('tableRendered', async () => {
console.log('Client reported table rendered - starting timer');
try {
const resp = await fetch(`${API_BASE}/start_timer`, { method: 'GET' });
const text = await resp.text();
console.log('Timer endpoint responded:', text);
} catch (err) {
console.error('Failed to hit start_timer:', err);
}
});
socket.on('disconnect', () => {
console.log('Socket.io client disconnected:', socket.id);
});
});
/* --------------------------------------------------------------------- */
/* ---------- 3. Serve static files ----------------------------------- */
/* --------------------------------------------------------------------- */
app.use(express.static('public'));
/* --------------------------------------------------------------------- */
/* ---------- 4. Redis subscriber ------------------------------------- */
/* --------------------------------------------------------------------- */
const redisClient = createClient({
url: 'redis://0.0.0.0:6379',
socket: { keepAlive: 60000, // 60 s TCP keep-alive
reconnectStrategy: attempts => Math.min(attempts * 100, 3000) } // back-off
});
redisClient.on('error', err => console.error('Redis error', err));
/* --- local cache for client_summary -------------------------------- */
const clientSummaryCache = {}; // { last: <payload> }
/* --------------------------------------------------------------------- */
(async () => {
await redisClient.connect();
const sub = redisClient.duplicate();
await sub.connect();
/* --------------------------------------------------------------------- */
/* Helper that re-subscribes to a channel (and re-sends the handler) */
/* --------------------------------------------------------------------- */
async function safeSubscribe(channel, handler) {
try {
await sub.subscribe(channel, handler);
console.log(`Subscribed to ${channel}`);
} catch (e) {
console.error(`Failed to subscribe to ${channel}`, e);
}
}
/* --------------------------------------------------------------------- */
/* Subscribe to all required channels */
/* --------------------------------------------------------------------- */
await safeSubscribe('host_metrics', (msg) => forward('host_metrics', msg));
await safeSubscribe('client_summary', (msg) => forward('client_summary', msg));
/* --------------------------------------------------------------------- */
/* Forward messages to Socket.io */
/* --------------------------------------------------------------------- */
function forward(channel, message) {
try {
const payload = JSON.parse(message);
/* ----- update cache on client_summary ----- */
if (channel === 'client_summary') {
clientSummaryCache.last = payload;
}
io.emit(channel, payload);
} catch (e) {
console.error(`Failed to parse message from ${channel}`, e);
}
}
/* --------------------------------------------------------------------- */
/* Re-subscribe automatically when the Redis connection reconnects */
/* --------------------------------------------------------------------- */
sub.on('reconnecting', () => console.log('Redis reconnecting…'));
sub.on('ready', async () => {
console.log('Redis ready - re-subscribing to all channels');
await safeSubscribe('host_metrics', (msg) => forward('host_metrics', msg));
await safeSubscribe('client_summary', (msg) => forward('client_summary', msg));
});
/* Optional: if the connection ends for any reason, close the process */
sub.on('end', () => {
console.error('Redis connection closed - exiting');
process.exit(1);
});
})();
/* --------------------------------------------------------------------- */
/* ---------- 5. Start the HTTP server --------------------------------- */
/* --------------------------------------------------------------------- */
// ---------- 5. Start HTTP server ----------
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
console.log(`Dashboard server listening on http://localhost:${PORT}`);
});