Fix discovery service persistence and API issues
- Add persistent discovery service auto-initialization in start_dashboard.py - Fix 'is_scanning' field in discovery status API - Fix scan results API to handle dictionary data correctly - Fix database schema issues for discovery_results table - Add debugging for service initialization Resolves issues with discovery service not persisting across restarts and API endpoints returning incorrect data formats.
This commit is contained in:
parent
1339b8bc55
commit
a41d638268
|
|
@ -15,7 +15,7 @@ from .configuration_manager import (
|
|||
configuration_manager, OPCUAConfig, ModbusTCPConfig, PumpStationConfig,
|
||||
PumpConfig, SafetyLimitsConfig, DataPointMapping, ProtocolType, ProtocolMapping
|
||||
)
|
||||
from src.discovery.protocol_discovery_persistent import persistent_persistent_discovery_service, DiscoveryStatus, DiscoveredEndpoint
|
||||
from src.discovery.protocol_discovery_persistent import persistent_discovery_service, DiscoveryStatus, DiscoveredEndpoint
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -1025,26 +1025,26 @@ async def get_discovery_results(scan_id: str):
|
|||
|
||||
# Convert discovered endpoints to dict format
|
||||
endpoints_data = []
|
||||
for endpoint in result.discovered_endpoints:
|
||||
for endpoint in result["discovered_endpoints"]:
|
||||
endpoint_data = {
|
||||
"protocol_type": endpoint.protocol_type.value,
|
||||
"address": endpoint.address,
|
||||
"port": endpoint.port,
|
||||
"device_id": endpoint.device_id,
|
||||
"device_name": endpoint.device_name,
|
||||
"capabilities": endpoint.capabilities,
|
||||
"response_time": endpoint.response_time,
|
||||
"discovered_at": endpoint.discovered_at.isoformat() if endpoint.discovered_at else None
|
||||
"protocol_type": endpoint.get("protocol_type"),
|
||||
"address": endpoint.get("address"),
|
||||
"port": endpoint.get("port"),
|
||||
"device_id": endpoint.get("device_id"),
|
||||
"device_name": endpoint.get("device_name"),
|
||||
"capabilities": endpoint.get("capabilities", []),
|
||||
"response_time": endpoint.get("response_time"),
|
||||
"discovered_at": endpoint.get("discovered_at")
|
||||
}
|
||||
endpoints_data.append(endpoint_data)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"scan_id": scan_id,
|
||||
"status": result.status.value,
|
||||
"scan_duration": result.scan_duration,
|
||||
"errors": result.errors,
|
||||
"timestamp": result.timestamp.isoformat() if result.timestamp else None,
|
||||
"status": result.get("status"),
|
||||
"scan_duration": None, # Not available in current implementation
|
||||
"errors": result.get("error_message"),
|
||||
"timestamp": result.get("scan_started_at"),
|
||||
"discovered_endpoints": endpoints_data
|
||||
}
|
||||
except HTTPException:
|
||||
|
|
@ -1076,14 +1076,14 @@ async def get_recent_discoveries():
|
|||
endpoints_data = []
|
||||
for endpoint in recent_endpoints:
|
||||
endpoint_data = {
|
||||
"protocol_type": endpoint.protocol_type.value,
|
||||
"address": endpoint.address,
|
||||
"port": endpoint.port,
|
||||
"device_id": endpoint.device_id,
|
||||
"device_name": endpoint.device_name,
|
||||
"capabilities": endpoint.capabilities,
|
||||
"response_time": endpoint.response_time,
|
||||
"discovered_at": endpoint.discovered_at.isoformat() if endpoint.discovered_at else None
|
||||
"protocol_type": endpoint.get("protocol_type"),
|
||||
"address": endpoint.get("address"),
|
||||
"port": endpoint.get("port"),
|
||||
"device_id": endpoint.get("device_id"),
|
||||
"device_name": endpoint.get("device_name"),
|
||||
"capabilities": endpoint.get("capabilities", []),
|
||||
"response_time": endpoint.get("response_time"),
|
||||
"discovered_at": endpoint.get("discovered_at")
|
||||
}
|
||||
endpoints_data.append(endpoint_data)
|
||||
|
||||
|
|
|
|||
|
|
@ -111,6 +111,7 @@ class PersistentProtocolDiscoveryService:
|
|||
|
||||
return {
|
||||
"current_scan_id": self._current_scan_id,
|
||||
"is_scanning": self._current_scan_id is not None,
|
||||
"recent_scans": recent_scans,
|
||||
"total_discovered_endpoints": total_endpoints
|
||||
}
|
||||
|
|
@ -118,6 +119,7 @@ class PersistentProtocolDiscoveryService:
|
|||
logger.error(f"Error getting discovery status: {e}")
|
||||
return {
|
||||
"current_scan_id": None,
|
||||
"is_scanning": False,
|
||||
"recent_scans": [],
|
||||
"total_discovered_endpoints": 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ Start Dashboard Server for Protocol Mapping Testing
|
|||
"""
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
|
@ -13,6 +14,7 @@ from fastapi import Request
|
|||
|
||||
from src.dashboard.api import dashboard_router
|
||||
from src.dashboard.templates import DASHBOARD_HTML
|
||||
from src.discovery.protocol_discovery_persistent import persistent_discovery_service
|
||||
|
||||
# Create FastAPI app
|
||||
app = FastAPI(title="Calejo Control Adapter Dashboard", version="1.0.0")
|
||||
|
|
@ -38,6 +40,22 @@ async def health_check():
|
|||
"""Health check endpoint"""
|
||||
return {"status": "healthy", "service": "dashboard"}
|
||||
|
||||
async def initialize_services():
|
||||
"""Initialize services before starting the server"""
|
||||
try:
|
||||
print("🔄 Starting persistent discovery service initialization...")
|
||||
await persistent_discovery_service.initialize()
|
||||
print("✅ Persistent discovery service initialized")
|
||||
|
||||
# Test that it's working
|
||||
status = persistent_discovery_service.get_discovery_status()
|
||||
print(f"📊 Discovery status: {status}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to initialize persistent discovery service: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Get port from environment variable or default to 8080
|
||||
port = int(os.getenv("REST_API_PORT", "8080"))
|
||||
|
|
@ -46,6 +64,9 @@ if __name__ == "__main__":
|
|||
print(f"📊 Dashboard available at: http://localhost:{port}")
|
||||
print("📊 Protocol Mapping tab should be visible in the navigation")
|
||||
|
||||
# Initialize services
|
||||
asyncio.run(initialize_services())
|
||||
|
||||
uvicorn.run(
|
||||
app,
|
||||
host="0.0.0.0",
|
||||
|
|
|
|||
Loading…
Reference in New Issue