Rewrite general weather alerts filter

This commit is contained in:
2025-03-29 22:42:27 -04:00
parent d93d5bd1df
commit 31b543be65

View File

@ -1,58 +1,53 @@
const severeWarningEvents = ["Severe Thunderstorm Warning","Destructive Severe Thunderstorm Warning","Considerable Destructive Severe Thunderstorm Warning"] const severeWarningEvents = ["Severe Thunderstorm Warning", "Destructive Severe Thunderstorm Warning", "Considerable Destructive Severe Thunderstorm Warning"]
const tornadoWarningEvents = ["Tornado Warning","Radar Indicated Tornado Warning","Confirmed Tornado Warning","Tornado Emergency"] const tornadoWarningEvents = ["Tornado Warning", "Radar Indicated Tornado Warning", "Confirmed Tornado Warning", "Tornado Emergency"]
let alerts = msg.payload let alerts = msg.payload
// Filter for Severe Thunderstorm Warnings // Helper function to safely access nested properties
const getNestedProperty = (obj, path, defaultValue = undefined) => {
return path.reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : defaultValue), obj)
}
let ts = alerts.filter(function(alert) { // Filter function
if (alert.raw.properties && const filterAlerts = (alerts, condition) => {
severeWarningEvents.includes(alert.raw.properties.event)) { return alerts.filter(alert => {
return true try {
} return condition(alert)
} catch (e) {
node.warn(`Error processing alert: ${e.message}`)
return false
}
})
}
// Filtering logic
const ts = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
return severeWarningEvents.includes(event)
}) })
// Filter for Severe Thunderstorm Warnings with tornado detection const tstp = filterAlerts(alerts, alert => {
// that have a tornado possible parameter const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
const tornadoDetection = getNestedProperty(alert, ['raw', 'properties', 'parameters', 'tornadoDetection'], [])
let tstp = alerts.filter(function(alert) { return severeWarningEvents.includes(event) && tornadoDetection.includes("POSSIBLE")
if (alert.raw.properties &&
severeWarningEvents.includes(alert.raw.properties.event) &&
alert.raw.properties.parameters &&
alert.raw.properties.parameters.tornadoDetection &&
alert.raw.properties.parameters.tornadoDetection.length > 0) {
let tornadoPossible = alert.raw.properties.parameters.tornadoDetection[0]
return tornadoPossible === "POSSIBLE"
}
}) })
// Filter for considerable destructive severe thunderstorm warnings const cdst = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
let cdst = alerts.filter(function(alert) { return event === "Considerable Destructive Severe Thunderstorm Warning"
if (alert.raw.properties &&
alert.raw.properties.event === "Considerable Destructive Severe Thunderstorm Warning") {
return true
}
}) })
// Filter for Tornado Warnings const tornado = filterAlerts(alerts, alert => {
const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
let tornado = alerts.filter(function(alert) { return tornadoWarningEvents.includes(event)
if (alert.raw.properties &&
tornadoWarningEvents.includes(alert.raw.properties.event)) {
return true
}
}) })
let confirmed_tornado = alerts.filter(function(alert) { const confirmed_tornado = filterAlerts(alerts, alert => {
if (alert.raw.properties && const event = getNestedProperty(alert, ['raw', 'properties', 'event'])
alert.raw.properties.event === "Confirmed Tornado Warning") { return event === "Confirmed Tornado Warning"
return true
}
}) })
// If there are any alerts, return them // Output messages
let tstormMsg = { let tstormMsg = {
"payload": { "payload": {
"alerts": ts, "alerts": ts,
@ -70,5 +65,5 @@ let tornadoWarnMsg = {
} }
} }
node.send([tstormMsg,tornadoWarnMsg]) node.send([tstormMsg, tornadoWarnMsg])
node.status({fill:'green',shape:'dot',text:'Alerts Updated'}) node.status({ fill: 'green', shape: 'dot', text: 'Alerts Updated' })