const skFlow = require('../../config/skFlow'); const { startSession } = require('../utility/live'); const logsPaths = [ "navigation.position", "navigation.headingTrue", "navigation.speedOverGround", "propulsion.p1.temperature" ]; // Funzioni per generare il testo aggiornato per ogni tipo di dato const textGenerators = { logs: () => { const data = skFlow.getFrom(logsPaths); if (!data || Object.keys(data).length === 0) return 'Nessun log disponibile.'; let text = '*Telemetria di Bordo*\n\n'; for (const [path, value] of Object.entries(data)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } return text; }, weather: () => { const data = skFlow.getWithFilter('meb.forecast'); if (!data || Object.keys(data).length === 0) return 'Nessun dato meteo disponibile.'; let text = '*Dati Meteo*\n\n'; for (const [path, value] of Object.entries(data)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } return text; }, marine: () => { const data = skFlow.getWithFilter('meb.marine'); if (!data || Object.keys(data).length === 0) return 'Nessun dato sul mare disponibile.'; let text = '*Dati Meteo del mare*\n\n'; for (const [path, value] of Object.entries(data)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } return text; }, data: () => { let text = ''; const logs = skFlow.getFrom(logsPaths); text += '*Telemetria di Bordo*\n\n'; if (logs && Object.keys(logs).length > 0) { for (const [path, value] of Object.entries(logs)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } } else { text += 'Nessun dato disponibile.\n'; } const weather = skFlow.getWithFilter('meb.forecast'); text += '\n*Dati Meteo*\n\n'; if (weather && Object.keys(weather).length > 0) { for (const [path, value] of Object.entries(weather)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } } else { text += 'Nessun dato disponibile.\n'; } const marine = skFlow.getWithFilter('meb.marine'); text += '\n*Dati Meteo del mare*\n\n'; if (marine && Object.keys(marine).length > 0) { for (const [path, value] of Object.entries(marine)) { const displayValue = typeof value === 'object' ? JSON.stringify(value) : value; text += `*${path}*: ${displayValue}\n`; } } else { text += 'Nessun dato disponibile.\n'; } return text; } }; module.exports = { prefix: 'live:', handler: async (bot, query) => { const chatId = query.message.chat.id; const botMessageId = query.message.message_id; // callback_data = live:: const parts = query.data.split(':'); const dataType = parts[1]; const userMessageId = parts[2]; const getTextFn = textGenerators[dataType]; if (!getTextFn) { bot.answerCallbackQuery(query.id, { text: 'Tipo non supportato' }); return; } startSession(bot, chatId, botMessageId, userMessageId, getTextFn); bot.answerCallbackQuery(query.id, { text: 'Live avviato' }); }, textGenerators };