Files
signalk-plugin/plugin/telegram/commands/live.js
2026-03-11 15:25:03 +01:00

85 lines
2.6 KiB
JavaScript

const dataHub = require('../../tools/dataHub');
/**
* Formatta tutti i dati (sensori + meteo) per il live tracker.
* @returns {string} Testo formattato Markdown
*/
function formatLiveData() {
const sensors = dataHub.getSensorData();
const { forecast, sea } = dataHub.getWeatherData();
let text = '*LIVE - Dati Completi*\n';
text += `_${new Date().toLocaleTimeString('it-IT')}_\n\n`;
// Sezione sensori
if (sensors) {
text += '*Sensori:*\n';
for (const [key, value] of Object.entries(sensors)) {
if (key.startsWith('_')) continue;
let label = key.replace(/_/g, ' ');
label = label.charAt(0).toUpperCase() + label.slice(1);
const val = (value !== null && value !== undefined)
? (typeof value === 'number' ? value.toFixed(2) : String(value))
: 'N/A';
text += ` ${label}: ${val}\n`;
}
} else {
text += '_Nessun dato sensore disponibile_\n';
}
// Sezione meteo (compatta)
if (forecast) {
text += '\n*Meteo:*\n';
const parts = [];
if (forecast.temperature !== null && forecast.temperature !== undefined) {
parts.push(`Temp: ${forecast.temperature}C`);
}
if (forecast.humidity !== null && forecast.humidity !== undefined) {
parts.push(`Um: ${forecast.humidity}%`);
}
if (forecast.wind?.speed !== null && forecast.wind?.speed !== undefined) {
parts.push(`Vento: ${forecast.wind.speed}km/h`);
}
text += ` ${parts.join(' | ')}\n`;
}
if (sea?.waves) {
const seaParts = [];
if (sea.waves.height !== null && sea.waves.height !== undefined) {
seaParts.push(`Onde: ${sea.waves.height}m`);
}
if (sea.waves.period !== null && sea.waves.period !== undefined) {
seaParts.push(`Per: ${sea.waves.period}s`);
}
if (seaParts.length > 0) {
text += ` ${seaParts.join(' | ')}\n`;
}
}
return text;
}
module.exports = {
command: 'live',
description: 'Dati live (meteo + sensori) con aggiornamento automatico',
pattern: /\/live/,
execute: async (bot, msg) => {
const chatId = msg.chat.id;
const text = formatLiveData();
await bot.sendMessage(chatId, text, {
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: [
[{ text: 'Avvia Live (2s)', callback_data: 'live-start' }],
[{ text: 'Aggiorna', callback_data: 'live-refresh' }]
]
}
});
},
formatLiveData
};