fix: Remove outer try-catch block from signals endpoint
- Removed the outer try-catch block that was catching HTTPException - HTTPException now properly propagates to FastAPI error handler - This ensures clear error messages are returned when protocol servers are disabled
This commit is contained in:
parent
28bf3ab246
commit
ce08cf846d
|
|
@ -601,128 +601,123 @@ async def _generate_mock_signals(stations: Dict, pumps_by_station: Dict) -> List
|
|||
@dashboard_router.get("/signals")
|
||||
async def get_signals():
|
||||
"""Get overview of all active signals across protocols"""
|
||||
try:
|
||||
# Use default stations and pumps since we don't have db access in this context
|
||||
stations = {
|
||||
"STATION_001": {"name": "Main Pump Station", "location": "Downtown"},
|
||||
"STATION_002": {"name": "Secondary Pump Station", "location": "Industrial Area"}
|
||||
}
|
||||
|
||||
pumps_by_station = {
|
||||
"STATION_001": [
|
||||
{"pump_id": "PUMP_001", "name": "Primary Pump"},
|
||||
{"pump_id": "PUMP_002", "name": "Backup Pump"}
|
||||
],
|
||||
"STATION_002": [
|
||||
{"pump_id": "PUMP_003", "name": "Industrial Pump"}
|
||||
]
|
||||
}
|
||||
|
||||
signals = []
|
||||
|
||||
# Check if protocol servers are enabled before trying to connect
|
||||
if settings.opcua_enabled or settings.modbus_enabled:
|
||||
# Try to get data from protocol servers
|
||||
try:
|
||||
# Initialize protocol data collector
|
||||
from src.dashboard.protocol_clients import ProtocolDataCollector
|
||||
collector = ProtocolDataCollector()
|
||||
# Use default stations and pumps since we don't have db access in this context
|
||||
stations = {
|
||||
"STATION_001": {"name": "Main Pump Station", "location": "Downtown"},
|
||||
"STATION_002": {"name": "Secondary Pump Station", "location": "Industrial Area"}
|
||||
}
|
||||
|
||||
pumps_by_station = {
|
||||
"STATION_001": [
|
||||
{"pump_id": "PUMP_001", "name": "Primary Pump"},
|
||||
{"pump_id": "PUMP_002", "name": "Backup Pump"}
|
||||
],
|
||||
"STATION_002": [
|
||||
{"pump_id": "PUMP_003", "name": "Industrial Pump"}
|
||||
]
|
||||
}
|
||||
|
||||
signals = []
|
||||
|
||||
# Check if protocol servers are enabled before trying to connect
|
||||
if settings.opcua_enabled or settings.modbus_enabled:
|
||||
# Try to get data from protocol servers
|
||||
try:
|
||||
# Initialize protocol data collector
|
||||
from src.dashboard.protocol_clients import ProtocolDataCollector
|
||||
collector = ProtocolDataCollector()
|
||||
|
||||
# Collect data from all pumps across all protocols
|
||||
for station_id, station in stations.items():
|
||||
pumps = pumps_by_station.get(station_id, [])
|
||||
|
||||
# Collect data from all pumps across all protocols
|
||||
for station_id, station in stations.items():
|
||||
pumps = pumps_by_station.get(station_id, [])
|
||||
for pump in pumps:
|
||||
pump_id = pump['pump_id']
|
||||
|
||||
for pump in pumps:
|
||||
pump_id = pump['pump_id']
|
||||
|
||||
# Get signal data from protocol servers
|
||||
pump_signals = await collector.get_signal_data(station_id, pump_id)
|
||||
signals.extend(pump_signals)
|
||||
|
||||
# Clean up connections
|
||||
await collector.cleanup()
|
||||
|
||||
# If no signals were retrieved from protocol servers, return error
|
||||
if not signals:
|
||||
logger.error("no_signals_from_protocol_servers")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="No signals available from protocol servers. Please check server connectivity."
|
||||
)
|
||||
except HTTPException:
|
||||
# Re-raise HTTP exceptions directly
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("failed_to_get_protocol_data", error=str(e))
|
||||
# Get signal data from protocol servers
|
||||
pump_signals = await collector.get_signal_data(station_id, pump_id)
|
||||
signals.extend(pump_signals)
|
||||
|
||||
# Clean up connections
|
||||
await collector.cleanup()
|
||||
|
||||
# If no signals were retrieved from protocol servers, return error
|
||||
if not signals:
|
||||
logger.error("no_signals_from_protocol_servers")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=f"Unable to connect to protocol servers: {str(e)}"
|
||||
detail="No signals available from protocol servers. Please check server connectivity."
|
||||
)
|
||||
else:
|
||||
# Protocol servers are disabled, return clear error
|
||||
logger.error("protocol_servers_disabled_in_production")
|
||||
except HTTPException:
|
||||
# Re-raise HTTP exceptions directly
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("failed_to_get_protocol_data", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Protocol servers are disabled in production environment. No real-time data available."
|
||||
detail=f"Unable to connect to protocol servers: {str(e)}"
|
||||
)
|
||||
|
||||
# Add system status signals
|
||||
signals.extend([
|
||||
{
|
||||
"name": "System_Status",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/status",
|
||||
"data_type": "String",
|
||||
"current_value": "Running",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
},
|
||||
{
|
||||
"name": "Database_Connection",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/status",
|
||||
"data_type": "Boolean",
|
||||
"current_value": "Connected",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
},
|
||||
{
|
||||
"name": "Health_Status",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/health",
|
||||
"data_type": "String",
|
||||
"current_value": "Healthy",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
}
|
||||
])
|
||||
|
||||
# Calculate protocol statistics
|
||||
protocol_counts = {}
|
||||
for signal in signals:
|
||||
protocol = signal["protocol"]
|
||||
if protocol not in protocol_counts:
|
||||
protocol_counts[protocol] = 0
|
||||
protocol_counts[protocol] += 1
|
||||
|
||||
protocol_stats = {}
|
||||
for protocol, count in protocol_counts.items():
|
||||
protocol_stats[protocol] = {
|
||||
"active_signals": count,
|
||||
"total_signals": count,
|
||||
"error_rate": "0%"
|
||||
}
|
||||
|
||||
return {
|
||||
"signals": signals,
|
||||
"protocol_stats": protocol_stats,
|
||||
"total_signals": len(signals),
|
||||
"last_updated": datetime.now().isoformat()
|
||||
else:
|
||||
# Protocol servers are disabled, return clear error
|
||||
logger.error("protocol_servers_disabled_in_production")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Protocol servers are disabled in production environment. No real-time data available."
|
||||
)
|
||||
|
||||
# Add system status signals
|
||||
signals.extend([
|
||||
{
|
||||
"name": "System_Status",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/status",
|
||||
"data_type": "String",
|
||||
"current_value": "Running",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
},
|
||||
{
|
||||
"name": "Database_Connection",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/status",
|
||||
"data_type": "Boolean",
|
||||
"current_value": "Connected",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
},
|
||||
{
|
||||
"name": "Health_Status",
|
||||
"protocol": "rest",
|
||||
"address": "/api/v1/dashboard/health",
|
||||
"data_type": "String",
|
||||
"current_value": "Healthy",
|
||||
"quality": "Good",
|
||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting signals: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to get signals: {str(e)}")
|
||||
])
|
||||
|
||||
# Calculate protocol statistics
|
||||
protocol_counts = {}
|
||||
for signal in signals:
|
||||
protocol = signal["protocol"]
|
||||
if protocol not in protocol_counts:
|
||||
protocol_counts[protocol] = 0
|
||||
protocol_counts[protocol] += 1
|
||||
|
||||
protocol_stats = {}
|
||||
for protocol, count in protocol_counts.items():
|
||||
protocol_stats[protocol] = {
|
||||
"active_signals": count,
|
||||
"total_signals": count,
|
||||
"error_rate": "0%"
|
||||
}
|
||||
|
||||
return {
|
||||
"signals": signals,
|
||||
"protocol_stats": protocol_stats,
|
||||
"total_signals": len(signals),
|
||||
"last_updated": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
@dashboard_router.get("/signals/export")
|
||||
async def export_signals():
|
||||
|
|
|
|||
Loading…
Reference in New Issue