Files
signalk-plugin/plugin/cores/realtime/auth.js
Giuseppe Raffa c2c1598226 feat: Implement rulesets and layout management for kiosk plugin
- Added rulesets manager to handle various data types and updates via HTTP and WebSocket.
- Introduced layout store for managing kiosk layouts with caching and server synchronization.
- Enhanced dashboard and data routes to support new layout and ruleset features.
- Updated kiosk HTML and JavaScript to utilize new layout rendering and data binding.
- Removed obsolete map route and integrated map functionality into the new tile renderer.
- Improved Telegram commands to reflect changes in data structure and logging.
- Refactored weather fetching intervals to prevent multiple instances.
- Added SSE stream for real-time layout updates in the kiosk.
2026-05-12 10:17:54 +02:00

51 lines
1.7 KiB
JavaScript

const configManager = require('../../config/configManager.js');
const REALTIME_URL = process.env.REALTIME_URL;
/**
* Autentica il sensore per connetterlo al server.
* Il token viene inviato al server, che restituisce un token temporaneo per la connessione websocket.
* @returns {Promise<{socketToken: string, sensorId: string, expiresIn: number}|null>}
*/
async function authenticate() {
const SENSOR_CODE = configManager.getSensorCode();
const SENSOR_ID = configManager.getSensorId();
if (!REALTIME_URL || !SENSOR_CODE || !SENSOR_ID) {
console.error('[REALTIME|AUTH] REALTIME_URL, SENSOR_CODE o SENSOR_ID non configurati');
return null;
}
try {
const response = await fetch(`${REALTIME_URL}/connect`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: SENSOR_ID,
code: SENSOR_CODE
})
});
if (!response.ok) {
const err = await response.json().catch(() => ({}));
console.error(`[REALTIME|AUTH] auth error (${response.status}):`, err.error || 'unknown');
return null;
}
const data = await response.json();
// Server risponde { s: 'ok', t: token }
if (data.s !== 'ok' || !data.t) {
console.error('[REALTIME|AUTH] Risposta inattesa:', data);
return null;
}
console.log(`[REALTIME|AUTH] Autenticato: ${SENSOR_ID}`);
return { socketToken: data.t, sensorId: SENSOR_ID };
} catch (error) {
console.error(`[REALTIME|AUTH] error: ${error.message}`);
return null;
}
}
module.exports = { authenticate };