58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
const dataHub = require('../../tools/dataHub');
|
|
|
|
function formatSensorData() {
|
|
const sensorSnapshot = dataHub.getSensorData();
|
|
const data = { timestamp: new Date().toISOString(), ...(sensorSnapshot || {}) };
|
|
let output = `📊 *Dashboard Sensori*\n`;
|
|
output += `_Ultimo aggiornamento: ${new Date().toLocaleTimeString('it-IT')}_\n\n`;
|
|
|
|
let isDataEmpty = true;
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
if (key === 'timestamp') continue;
|
|
isDataEmpty = false;
|
|
|
|
let formattedKey = key.replace(/_/g, ' ');
|
|
// Prima lettera maiuscola
|
|
formattedKey = formattedKey.charAt(0).toUpperCase() + formattedKey.slice(1);
|
|
|
|
const formattedValue = (value !== null && value !== undefined)
|
|
? (typeof value === 'number' ? value.toFixed(2) : value)
|
|
: 'N/A';
|
|
|
|
output += `🔹 *${formattedKey}:* ${formattedValue}\n`;
|
|
}
|
|
|
|
if (isDataEmpty) {
|
|
output += `_Nessun dato configurato o letto. Controlla sensors.references.json_\n`;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
module.exports = {
|
|
command: 'dashboard',
|
|
description: 'Mostra i sensori live (dal file references)',
|
|
pattern: /\/dashboard/,
|
|
execute: async (bot, msg) => {
|
|
const chatId = msg.chat.id;
|
|
|
|
const text = formatSensorData();
|
|
|
|
await bot.sendMessage(chatId, text, {
|
|
parse_mode: 'Markdown',
|
|
reply_markup: {
|
|
inline_keyboard: [
|
|
[
|
|
{ text: "⚡️ Avvia Live Tracker (2s)", callback_data: 'dashboard-live-start' }
|
|
],
|
|
[
|
|
{ text: "🔄 Ricarica Dati", callback_data: 'dashboard-refresh' }
|
|
]
|
|
]
|
|
}
|
|
});
|
|
},
|
|
formatSensorData // Esportato per riuso nel refresh e nel live
|
|
};
|