59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
const dataHub = require('../../tools/dataHub');
|
|
|
|
/**
|
|
* Formatta i dati sensore in un messaggio Telegram leggibile.
|
|
* @returns {string} Testo formattato Markdown
|
|
*/
|
|
function formatSensorData() {
|
|
const sensors = dataHub.getSensorData();
|
|
|
|
if (!sensors) {
|
|
return 'Nessun dato sensore disponibile.\nI sensori potrebbero non essere ancora attivi.';
|
|
}
|
|
|
|
let text = '*Dati Sensori*\n';
|
|
text += `_${new Date().toLocaleTimeString('it-IT')}_\n\n`;
|
|
|
|
let hasData = false;
|
|
|
|
for (const [key, value] of Object.entries(sensors)) {
|
|
if (key.startsWith('_')) continue; // Skip campi interni
|
|
|
|
hasData = true;
|
|
let label = key.replace(/_/g, ' ');
|
|
label = label.charAt(0).toUpperCase() + label.slice(1);
|
|
|
|
const formatted = (value !== null && value !== undefined)
|
|
? (typeof value === 'number' ? value.toFixed(2) : String(value))
|
|
: 'N/A';
|
|
|
|
text += `*${label}:* ${formatted}\n`;
|
|
}
|
|
|
|
if (!hasData) {
|
|
text += '_Nessun dato configurato. Controlla sensors.references.json_\n';
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
module.exports = {
|
|
command: 'data',
|
|
description: 'Mostra i dati sensori attuali',
|
|
pattern: /\/data/,
|
|
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: 'Aggiorna', callback_data: 'data-refresh' }]
|
|
]
|
|
}
|
|
});
|
|
},
|
|
formatSensorData
|
|
};
|