Adjust sorting logic

pull/3/head
Mason10198 3 years ago
parent ddc30bcb81
commit 14d4797156

Binary file not shown.

Binary file not shown.

@ -292,7 +292,8 @@ def getAlerts(countyCodes):
logger.debug("Checking for alerts in {}".format(countyCodes)) logger.debug("Checking for alerts in {}".format(countyCodes))
for countyCode in countyCodes: for countyCode in countyCodes:
logger.debug("Checking for alerts in {}".format(countyCode)) logger.debug("Checking for alerts in {}".format(countyCode))
url = "https://api.weather.gov/alerts/active?zone={}".format(countyCode) # url = "https://api.weather.gov/alerts/active?zone={}".format(countyCode)
url = "https://api.weather.gov/alerts/active?area=AR" # THIS RETURNS ALL ACTIVE ALERTS IN THE US
logger.debug("Requesting {}".format(url)) logger.debug("Requesting {}".format(url))
response = requests.get(url) response = requests.get(url)
logger.debug("Response: {}\n\n".format(response.text)) logger.debug("Response: {}\n\n".format(response.text))
@ -324,7 +325,6 @@ def getAlerts(countyCodes):
else: else:
severity = severity_mapping_api.get(severity, 0) severity = severity_mapping_api.get(severity, 0)
alerts.append((event, severity)) # Add event to list as a tuple alerts.append((event, severity)) # Add event to list as a tuple
logger.debug("{}: {} with severity {}".format(countyCode, event, severity))
else: else:
logger.error( logger.error(
"Failed to retrieve alerts for {}, HTTP status code {}, response: {}".format( "Failed to retrieve alerts for {}, HTTP status code {}, response: {}".format(
@ -332,14 +332,28 @@ def getAlerts(countyCodes):
) )
) )
# Sort alerts by severity (highest to lowest) and only keep the events # Convert list to set to eliminate duplicates, then convert back to list
alerts.sort(key=lambda x: x[1], reverse=True) alerts = list(set(alerts))
# Sort by both API-provided severity and 'words' severity
alerts.sort(
key=lambda x: (
x[1], # API-provided severity
severity_mapping_words.get(x[0].split()[-1], 0) # 'words' severity
),
reverse=True
)
logger.debug("Sorted alerts: (alert), (severity)")
for alert in alerts:
logger.debug(alert)
# Only keep the events (not the severities)
alerts = [alert[0] for alert in alerts[:max_alerts]] # Only keep the first 'max_alerts' alerts alerts = [alert[0] for alert in alerts[:max_alerts]] # Only keep the first 'max_alerts' alerts
return alerts return alerts
def sayAlert(alerts): def sayAlert(alerts):
""" """
Generate and broadcast severe weather alert sounds on Asterisk. Generate and broadcast severe weather alert sounds on Asterisk.
@ -358,8 +372,8 @@ def sayAlert(alerts):
alert_count = 0 # Counter for alerts added to combined_sound alert_count = 0 # Counter for alerts added to combined_sound
for alert in alerts: for alert in alerts:
# Check if alert is in the SayAlertBlockedEvents list # Check if alert matches any pattern in the SayAlertBlockedEvents list
if alert in sayalert_blocked_events: if any(fnmatch.fnmatch(alert, blocked_event) for blocked_event in sayalert_blocked_events):
logger.debug("SayAlert blocking {} as per configuration".format(alert)) logger.debug("SayAlert blocking {} as per configuration".format(alert))
continue continue
@ -439,8 +453,8 @@ def buildTailmessage(alerts):
os.path.join(sounds_path, "ALERTS", "SWP95.wav") os.path.join(sounds_path, "ALERTS", "SWP95.wav")
) )
for alert in alerts: for alert in alerts:
# Check if alert is in the TailmessageBlockedEvents list # Check if alert matches any pattern in the TailmessageBlockedEvents list
if alert in tailmessage_blocked_events: if any(fnmatch.fnmatch(alert, blocked_event) for blocked_event in tailmessage_blocked_events):
logger.debug("Alert blocked by TailmessageBlockedEvents: {}".format(alert)) logger.debug("Alert blocked by TailmessageBlockedEvents: {}".format(alert))
continue continue

@ -36,7 +36,7 @@ SayAllClear = True
; [Tornado Warning, Severe Thunderstorm Warning, Flood Watch, Special Weather Statement], ; [Tornado Warning, Severe Thunderstorm Warning, Flood Watch, Special Weather Statement],
; and MaxAlerts = 2, then SkywarnPlus will only process the Tornado Warning and Severe Thunderstorm Warning. ; and MaxAlerts = 2, then SkywarnPlus will only process the Tornado Warning and Severe Thunderstorm Warning.
; Example: MaxAlerts = 3 ; Example: MaxAlerts = 3
MaxAlerts = 2 MaxAlerts = 5
; Optional alternate path to the directory where sound files are stored ; Optional alternate path to the directory where sound files are stored
; Default is SkywarnPlus/SOUNDS ; Default is SkywarnPlus/SOUNDS
@ -47,16 +47,21 @@ MaxAlerts = 2
[Blocking] [Blocking]
; GLOBAL BLOCKING - These alerts will be completely ignored and filtered out of the entire SkywarnPlus workflow ; GLOBAL BLOCKING - These alerts will be completely ignored and filtered out of the entire SkywarnPlus workflow
; CASE SENSITIVE list of events to ignore, comma separated. Wildcards can be used, e.g. *Statement, *Advisory ; CASE SENSITIVE list of events to ignore, comma separated. Wildcards can be used, e.g. *Statement, *Advisory
; Example: GlobalBlockedEvents = *Statement, *Advisory
GlobalBlockedEvents = GlobalBlockedEvents =
; SayAlert Blocking ; SayAlert Blocking
; These alerts will be blocked from being spoken when they are received ; These alerts will be blocked from being spoken when they are received
; These alerts will still be added to the tailmessage ; These alerts will still be added to the tailmessage
; CASE SENSITIVE list of events to ignore, comma separated.
; Example: SayAlertBlockedEvents = *Statement, *Advisory
SayAlertBlockedEvents = SayAlertBlockedEvents =
; Tailmessage Blocking ; Tailmessage Blocking
; These alerts will be blocked from being added to the tailmessage ; These alerts will be blocked from being added to the tailmessage
; These alerts will still be spoken when they are received ; These alerts will still be spoken when they are received
; CASE SENSITIVE list of events to ignore, comma separated.
; Example: TailmessageBlockedEvents = *Statement, *Advisory
TailmessageBlockedEvents = TailmessageBlockedEvents =
; Tail message settings ; Tail message settings
@ -109,10 +114,13 @@ RptLinkCT = CT-LINK.ulaw
CTAlerts = Hurricane Force Wind Warning, CTAlerts = Hurricane Force Wind Warning,
Severe Thunderstorm Warning, Severe Thunderstorm Warning,
Tropical Storm Warning, Tropical Storm Warning,
Coastal Flood Warning,
Winter Storm Warning, Winter Storm Warning,
Thunderstorm Warning, Thunderstorm Warning,
Extreme Wind Warning, Extreme Wind Warning,
Storm Surge Warning, Storm Surge Warning,
Dust Storm Warning,
Avalanche Warning,
Ice Storm Warning, Ice Storm Warning,
Hurricane Warning, Hurricane Warning,
Blizzard Warning, Blizzard Warning,
@ -141,7 +149,7 @@ Debug = False
[Logging] [Logging]
; Enable more verbose logging ; Enable more verbose logging
; Either True or False ; Either True or False
Debug = True Debug = False
; Optional alternate log file path ; Optional alternate log file path
; Default is /tmp/Skywarnplus/SkywarnPlus.log ; Default is /tmp/Skywarnplus/SkywarnPlus.log
@ -152,7 +160,7 @@ Debug = True
[DEV] [DEV]
; Delete cached data on startup ; Delete cached data on startup
; Either True or False ; Either True or False
CLEANSLATE = True CLEANSLATE = False
; Optional alternate TMP directory ; Optional alternate TMP directory
; Default is /tmp/SkywarnPlus ; Default is /tmp/SkywarnPlus
@ -160,12 +168,9 @@ CLEANSLATE = True
;TmpDir = /tmp/SkywarnPlus ;TmpDir = /tmp/SkywarnPlus
; Enable to inject the below list of test alerts instead of calling the NWS API ; Enable to inject the below list of test alerts instead of calling the NWS API
INJECT = True INJECT = False
; CASE SENSITIVE, comma & newline separated list of alerts to inject ; CASE SENSITIVE, comma & newline separated list of alerts to inject
INJECTALERTS = Wind Advisory, INJECTALERTS = Tornado Warning,
Special Weather Statement,
Tornado Watch, Tornado Watch,
Tornado Warning, Severe Thunderstorm Warning
Severe Thunderstorm Warning,
Flood Watch

Loading…
Cancel
Save

Powered by TurnKey Linux.