lestoitsduval/scripts/production/index.js

72 lines
1.9 KiB
JavaScript

import Mailjet from 'node-mailjet'
const monitoringUrl = "https://monitoringapi.solaredge.com/site/2516130/overview?api_key=GVF1BZAAM74IRDZ2WRWT32A5V0CY9G4Z&format=json"
const detailUrl = "https://monitoringapi.solaredge.com/site/2516130/details?api_key=GVF1BZAAM74IRDZ2WRWT32A5V0CY9G4Z&format=json"
async function getCurrentPower() {
const response = await fetch(monitoringUrl);
const data = await response.json();
return data.overview.currentPower.power
}
async function sendAlert() {
console.log("sendAlert start")
const mailjet = Mailjet.apiConnect(
process.env.MJ_APIKEY_PUBLIC,
process.env.MJ_APIKEY_PRIVATE,
);
const request = mailjet
.post('send', { version: 'v3.1' })
.request({
Messages: [
{
From: {
Email: "admin@lestoitsduval.fr",
Name: "Les Toits du Val - Salle Greyzollon Duluth"
},
To: [
{
Email: "simon@lestoitsduval.fr",
Name: "Conseil de gestion"
}
],
Subject: "😱 Alert sur la production",
TextPart: "La production est de 0 watt !",
}
]
})
request
.then((result) => {
console.log("result")
console.log(result.body)
})
.catch((err) => {
console.log("err")
console.log(err)
})
console.log("sendAlert end")
return
}
function workingHours() {
const now = new Date();
const currentHour = now.getHours();
// Définition des heures de début et fin
const startHour = 4;
const endHour = 20;
// Vérification si l'heure actuelle est entre 9h et 17h
return currentHour >= startHour && currentHour <= endHour
}
const currentPower = await getCurrentPower();
console.log(`La salle Greyzollon Duluth produit ${currentPower} watt${currentPower>0?'s':''} !`)
if (currentPower == 0 && workingHours()) {
await sendAlert();
}