diff --git a/src/alerting.py b/src/alerting.py index c21fa0d..f5a95f7 100644 --- a/src/alerting.py +++ b/src/alerting.py @@ -134,7 +134,22 @@ class WaterLevelAlertSystem: """Load alert thresholds from config or database""" # Default thresholds for Northern Thailand stations return { - "P.1": {"warning": 5.0, "critical": 6.5, "emergency": 8.0}, + "P.1": { + # Zone-based thresholds for Nawarat Bridge (P.1) + "zone_1": 3.7, + "zone_2": 3.9, + "zone_3": 4.0, + "zone_4": 4.1, + "zone_5": 4.2, + "zone_6": 4.3, + "zone_7": 4.6, + "zone_8": 4.8, + "newedge": 4.8, # Same as zone 8 or adjust as needed + # Keep legacy thresholds for compatibility + "warning": 3.7, + "critical": 4.3, + "emergency": 4.8 + }, "P.4A": {"warning": 4.5, "critical": 6.0, "emergency": 7.5}, "P.20": {"warning": 3.0, "critical": 4.5, "emergency": 6.0}, "P.21": {"warning": 4.0, "critical": 5.5, "emergency": 7.0}, @@ -192,18 +207,42 @@ class WaterLevelAlertSystem: threshold_value = None alert_type = None - if water_level >= station_thresholds['emergency']: - alert_level = AlertLevel.EMERGENCY - threshold_value = station_thresholds['emergency'] - alert_type = "Emergency Water Level" - elif water_level >= station_thresholds['critical']: - alert_level = AlertLevel.CRITICAL - threshold_value = station_thresholds['critical'] - alert_type = "Critical Water Level" - elif water_level >= station_thresholds['warning']: - alert_level = AlertLevel.WARNING - threshold_value = station_thresholds['warning'] - alert_type = "High Water Level" + # Special handling for P.1 with zone-based thresholds + if station_code == "P.1" and 'zone_1' in station_thresholds: + # Check all zones in reverse order (highest to lowest) + zones = [ + ("zone_8", 4.8, AlertLevel.EMERGENCY, "Zone 8 - Emergency"), + ("newedge", 4.8, AlertLevel.EMERGENCY, "NewEdge Alert Level"), + ("zone_7", 4.6, AlertLevel.CRITICAL, "Zone 7 - Critical"), + ("zone_6", 4.3, AlertLevel.CRITICAL, "Zone 6 - Critical"), + ("zone_5", 4.2, AlertLevel.WARNING, "Zone 5 - Warning"), + ("zone_4", 4.1, AlertLevel.WARNING, "Zone 4 - Warning"), + ("zone_3", 4.0, AlertLevel.WARNING, "Zone 3 - Warning"), + ("zone_2", 3.9, AlertLevel.INFO, "Zone 2 - Info"), + ("zone_1", 3.7, AlertLevel.INFO, "Zone 1 - Info"), + ] + + for zone_name, zone_threshold, zone_alert_level, zone_description in zones: + if water_level >= zone_threshold: + alert_level = zone_alert_level + threshold_value = zone_threshold + alert_type = zone_description + break + + else: + # Standard threshold checking for other stations + if water_level >= station_thresholds.get('emergency', float('inf')): + alert_level = AlertLevel.EMERGENCY + threshold_value = station_thresholds['emergency'] + alert_type = "Emergency Water Level" + elif water_level >= station_thresholds.get('critical', float('inf')): + alert_level = AlertLevel.CRITICAL + threshold_value = station_thresholds['critical'] + alert_type = "Critical Water Level" + elif water_level >= station_thresholds.get('warning', float('inf')): + alert_level = AlertLevel.WARNING + threshold_value = station_thresholds['warning'] + alert_type = "High Water Level" if alert_level: alert = WaterAlert(