diff --git a/index.js b/index.js index 4988088..b4eb062 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,23 @@ async function sendAlert(msg) { return } +function formatTime(seconds) { + if (seconds < 60) { + return `${seconds} seconde${seconds > 1 ? 's' : ''}`; + } else if (seconds < 3600) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + return remainingSeconds === 0 + ? `environ ${minutes} minute${minutes > 1 ? 's' : ''}` + : `${minutes} minute${minutes > 1 ? 's' : ''} et ${remainingSeconds} seconde${remainingSeconds > 1 ? 's' : ''}`; + } else { + const hours = Math.floor(seconds / 3600); + const remainingMinutes = Math.floor((seconds % 3600) / 60); + return remainingMinutes === 0 + ? `environ ${hours} heure${hours > 1 ? 's' : ''}` + : `environ ${hours} heure${hours > 1 ? 's' : ''} et ${remainingMinutes} minute${remainingMinutes > 1 ? 's' : ''}`; + } +} const data = await getData(); const now = new Date(); @@ -65,10 +82,12 @@ const currentPower = await getCurrentPower(data); const lastUpdateTime = await getLastUpdateTime(data); console.log(`[${lastUpdateTime}] La salle Greyzollon Duluth produissait ${currentPower} watt${currentPower>0?'s':''} !`) const secondDifference = Math.ceil(Math.abs(now.getTime() - new Date(lastUpdateTime).getTime()) / 1000); +const formatedDifference = formatTime(secondDifference) if (secondDifference > (6 * 60)) { - console.log(`❌ L'installation n'a pas répondu depuis '${lastUpdateTime}' ça fait ${secondDifference} secondes !`); - await sendAlert(`❌ L'installation n'a pas répondu depuis '${lastUpdateTime}' ça fait ${secondDifference} secondes !`); + const msg = `❌ L'installation n'a pas répondu depuis '${lastUpdateTime}' ça fait ${formatedDifference} (${secondDifference}) !` + console.log(msg); + await sendAlert(msg); } else { - console.log(`✅ L'installation répond correctement dernière mise à jour il y a ${secondDifference} secondes.`); + console.log(`✅ L'installation répond correctement dernière mise à jour il y a ${formatedDifference} (${secondDifference}).`); }