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")
|
@dashboard_router.get("/signals")
|
||||||
async def get_signals():
|
async def get_signals():
|
||||||
"""Get overview of all active signals across protocols"""
|
"""Get overview of all active signals across protocols"""
|
||||||
try:
|
# Use default stations and pumps since we don't have db access in this context
|
||||||
# Use default stations and pumps since we don't have db access in this context
|
stations = {
|
||||||
stations = {
|
"STATION_001": {"name": "Main Pump Station", "location": "Downtown"},
|
||||||
"STATION_001": {"name": "Main Pump Station", "location": "Downtown"},
|
"STATION_002": {"name": "Secondary Pump Station", "location": "Industrial Area"}
|
||||||
"STATION_002": {"name": "Secondary Pump Station", "location": "Industrial Area"}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pumps_by_station = {
|
pumps_by_station = {
|
||||||
"STATION_001": [
|
"STATION_001": [
|
||||||
{"pump_id": "PUMP_001", "name": "Primary Pump"},
|
{"pump_id": "PUMP_001", "name": "Primary Pump"},
|
||||||
{"pump_id": "PUMP_002", "name": "Backup Pump"}
|
{"pump_id": "PUMP_002", "name": "Backup Pump"}
|
||||||
],
|
],
|
||||||
"STATION_002": [
|
"STATION_002": [
|
||||||
{"pump_id": "PUMP_003", "name": "Industrial Pump"}
|
{"pump_id": "PUMP_003", "name": "Industrial Pump"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
signals = []
|
signals = []
|
||||||
|
|
||||||
# Check if protocol servers are enabled before trying to connect
|
# Check if protocol servers are enabled before trying to connect
|
||||||
if settings.opcua_enabled or settings.modbus_enabled:
|
if settings.opcua_enabled or settings.modbus_enabled:
|
||||||
# Try to get data from protocol servers
|
# Try to get data from protocol servers
|
||||||
try:
|
try:
|
||||||
# Initialize protocol data collector
|
# Initialize protocol data collector
|
||||||
from src.dashboard.protocol_clients import ProtocolDataCollector
|
from src.dashboard.protocol_clients import ProtocolDataCollector
|
||||||
collector = ProtocolDataCollector()
|
collector = ProtocolDataCollector()
|
||||||
|
|
||||||
# Collect data from all pumps across all protocols
|
# Collect data from all pumps across all protocols
|
||||||
for station_id, station in stations.items():
|
for station_id, station in stations.items():
|
||||||
pumps = pumps_by_station.get(station_id, [])
|
pumps = pumps_by_station.get(station_id, [])
|
||||||
|
|
||||||
for pump in pumps:
|
for pump in pumps:
|
||||||
pump_id = pump['pump_id']
|
pump_id = pump['pump_id']
|
||||||
|
|
||||||
# Get signal data from protocol servers
|
# Get signal data from protocol servers
|
||||||
pump_signals = await collector.get_signal_data(station_id, pump_id)
|
pump_signals = await collector.get_signal_data(station_id, pump_id)
|
||||||
signals.extend(pump_signals)
|
signals.extend(pump_signals)
|
||||||
|
|
||||||
# Clean up connections
|
# Clean up connections
|
||||||
await collector.cleanup()
|
await collector.cleanup()
|
||||||
|
|
||||||
# If no signals were retrieved from protocol servers, return error
|
# If no signals were retrieved from protocol servers, return error
|
||||||
if not signals:
|
if not signals:
|
||||||
logger.error("no_signals_from_protocol_servers")
|
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))
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=503,
|
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:
|
except HTTPException:
|
||||||
# Protocol servers are disabled, return clear error
|
# Re-raise HTTP exceptions directly
|
||||||
logger.error("protocol_servers_disabled_in_production")
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("failed_to_get_protocol_data", error=str(e))
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=503,
|
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)}"
|
||||||
)
|
)
|
||||||
|
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
|
# Add system status signals
|
||||||
signals.extend([
|
signals.extend([
|
||||||
{
|
{
|
||||||
"name": "System_Status",
|
"name": "System_Status",
|
||||||
"protocol": "rest",
|
"protocol": "rest",
|
||||||
"address": "/api/v1/dashboard/status",
|
"address": "/api/v1/dashboard/status",
|
||||||
"data_type": "String",
|
"data_type": "String",
|
||||||
"current_value": "Running",
|
"current_value": "Running",
|
||||||
"quality": "Good",
|
"quality": "Good",
|
||||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Database_Connection",
|
"name": "Database_Connection",
|
||||||
"protocol": "rest",
|
"protocol": "rest",
|
||||||
"address": "/api/v1/dashboard/status",
|
"address": "/api/v1/dashboard/status",
|
||||||
"data_type": "Boolean",
|
"data_type": "Boolean",
|
||||||
"current_value": "Connected",
|
"current_value": "Connected",
|
||||||
"quality": "Good",
|
"quality": "Good",
|
||||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Health_Status",
|
"name": "Health_Status",
|
||||||
"protocol": "rest",
|
"protocol": "rest",
|
||||||
"address": "/api/v1/dashboard/health",
|
"address": "/api/v1/dashboard/health",
|
||||||
"data_type": "String",
|
"data_type": "String",
|
||||||
"current_value": "Healthy",
|
"current_value": "Healthy",
|
||||||
"quality": "Good",
|
"quality": "Good",
|
||||||
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
# Calculate protocol statistics
|
# Calculate protocol statistics
|
||||||
protocol_counts = {}
|
protocol_counts = {}
|
||||||
for signal in signals:
|
for signal in signals:
|
||||||
protocol = signal["protocol"]
|
protocol = signal["protocol"]
|
||||||
if protocol not in protocol_counts:
|
if protocol not in protocol_counts:
|
||||||
protocol_counts[protocol] = 0
|
protocol_counts[protocol] = 0
|
||||||
protocol_counts[protocol] += 1
|
protocol_counts[protocol] += 1
|
||||||
|
|
||||||
protocol_stats = {}
|
protocol_stats = {}
|
||||||
for protocol, count in protocol_counts.items():
|
for protocol, count in protocol_counts.items():
|
||||||
protocol_stats[protocol] = {
|
protocol_stats[protocol] = {
|
||||||
"active_signals": count,
|
"active_signals": count,
|
||||||
"total_signals": count,
|
"total_signals": count,
|
||||||
"error_rate": "0%"
|
"error_rate": "0%"
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
"signals": signals,
|
|
||||||
"protocol_stats": protocol_stats,
|
|
||||||
"total_signals": len(signals),
|
|
||||||
"last_updated": datetime.now().isoformat()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
return {
|
||||||
logger.error(f"Error getting signals: {str(e)}")
|
"signals": signals,
|
||||||
raise HTTPException(status_code=500, detail=f"Failed to get signals: {str(e)}")
|
"protocol_stats": protocol_stats,
|
||||||
|
"total_signals": len(signals),
|
||||||
|
"last_updated": datetime.now().isoformat()
|
||||||
|
}
|
||||||
|
|
||||||
@dashboard_router.get("/signals/export")
|
@dashboard_router.get("/signals/export")
|
||||||
async def export_signals():
|
async def export_signals():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue