warningCheck.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Meteostanice from "./meteostanice"
  2. import { Eta } from "eta"
  3. const eta = new Eta({ views: "./templates" })
  4. export default async (langName, lang, station) => {
  5. const data = Meteostanice.getStationDataLast5Minutes(station.id)
  6. if (!data) return
  7. const thresholds = {
  8. indoorTemp: { low: 1800, high: 2800 }, // 18°C - 28°C
  9. indoorPressure: { low: 98000, high: 103000 }, // 980hPa - 1030hPa
  10. indoorHumidity: { low: 3000, high: 6000 }, // 30% - 60%
  11. outdoorTemp: { low: -1000, high: 3500 }, // -10°C - 35°C
  12. outdoorPressure: { low: 97000, high: 104000 }, // 970hPa - 1040hPa
  13. outdoorHumidity: { low: 2000, high: 9000 } // 20% - 90%
  14. }
  15. for (const entries of data) {
  16. const detectedWarnings = []
  17. for (const [prop, limit] of Object.entries(thresholds)) {
  18. if (entries.every(entry => Number(entry[prop]) > limit.high)) {
  19. detectedWarnings.push(`high${prop.charAt(0).toUpperCase() + prop.slice(1)}`)
  20. }
  21. if (entries.every(entry => Number(entry[prop]) < limit.low)) {
  22. detectedWarnings.push(`low${prop.charAt(0).toUpperCase() + prop.slice(1)}`)
  23. }
  24. }
  25. }
  26. const currentWarnings = station?.warnings ?? [];
  27. const addedWarnings = [
  28. ...detectedWarnings.filter(w => !currentWarnings.includes(w))
  29. ];
  30. const removedWarnings = [
  31. ...currentWarnings.filter(w => !detectedWarnings.includes(w))
  32. ]
  33. const stationEmails = Meteostanice.getEmails(station.id)
  34. const emailLink = `${process.env.BASE_URL}/${langName === "sk" ? `` : `${langName}/`}panel/stations/${station.id}`
  35. if (addedWarnings.length) {
  36. for (const email of stationEmails) {
  37. await Meteostanice.sendWarnings(email, lang.emails.stations.addedWarnings.subject(), lang.emails.stations.addedWarnings.text({ stationName: station.name, warnings: addedWarnings, stationLink: emailLink }), eta.render(`${langName}/email/stations/addedWarnings`, { stationName: station.name, warnings: addedWarnings, stationLink: emailLink }))
  38. }
  39. }
  40. if (removedWarnings.length) {
  41. for (const email of stationEmails) {
  42. await Meteostanice.sendWarnings(email, lang.emails.stations.removedWarnings.subject(), lang.emails.stations.removedWarnings.text({ stationName: station.name, warnings: addedWarnings, stationLink: emailLink }), eta.render(`${langName}/email/stations/removedWarnings`, { stationName: station.name, warnings: addedWarnings, stationLink: emailLink }))
  43. }
  44. }
  45. Meteostanice.editWarnings(station.id, detectedWarnings)
  46. }