158 lines
6.7 KiB
Python
158 lines
6.7 KiB
Python
"""
|
|
Debug test to understand why setpoints are returning 0.0
|
|
"""
|
|
|
|
import asyncio
|
|
import pytest
|
|
import pytest_asyncio
|
|
from sqlalchemy import text
|
|
|
|
from src.database.flexible_client import FlexibleDatabaseClient
|
|
from src.core.auto_discovery import AutoDiscovery
|
|
from src.core.setpoint_manager import SetpointManager
|
|
from src.core.safety import SafetyLimitEnforcer
|
|
from src.core.emergency_stop import EmergencyStopManager
|
|
from src.monitoring.watchdog import DatabaseWatchdog
|
|
|
|
|
|
class TestDebugSetpoint:
|
|
"""Debug test for setpoint issues."""
|
|
|
|
@pytest_asyncio.fixture
|
|
async def debug_db_client(self):
|
|
"""Create database client for debugging."""
|
|
client = FlexibleDatabaseClient("sqlite:///:memory:")
|
|
await client.connect()
|
|
client.create_tables()
|
|
|
|
# Insert debug test data
|
|
client.execute(
|
|
"""INSERT INTO pump_stations (station_id, station_name, location) VALUES
|
|
('DEBUG_STATION_001', 'Debug Station 1', 'Test Area')"""
|
|
)
|
|
|
|
client.execute(
|
|
"""INSERT INTO pumps (station_id, pump_id, pump_name, control_type, default_setpoint_hz) VALUES
|
|
('DEBUG_STATION_001', 'DEBUG_PUMP_001', 'Debug Pump 1', 'DIRECT_SPEED', 35.0)"""
|
|
)
|
|
|
|
client.execute(
|
|
"""INSERT INTO pump_safety_limits (station_id, pump_id, hard_min_speed_hz, hard_max_speed_hz,
|
|
hard_min_level_m, hard_max_level_m, hard_max_power_kw, hard_max_flow_m3h,
|
|
emergency_stop_level_m, dry_run_protection_level_m, max_speed_change_hz_per_min) VALUES
|
|
('DEBUG_STATION_001', 'DEBUG_PUMP_001', 20.0, 70.0, 0.5, 5.0, 100.0, 500.0, 4.8, 0.6, 10.0)"""
|
|
)
|
|
|
|
client.execute(
|
|
"""INSERT INTO pump_plans (station_id, pump_id, interval_start, interval_end,
|
|
suggested_speed_hz, target_flow_m3h, target_power_kw, plan_version, optimization_run_id, plan_status) VALUES
|
|
('DEBUG_STATION_001', 'DEBUG_PUMP_001', datetime('now', '-1 hour'), datetime('now', '+1 hour'),
|
|
42.5, 320.0, 65.0, 1, 'DEBUG_OPT_001', 'ACTIVE')"""
|
|
)
|
|
|
|
return client
|
|
|
|
@pytest_asyncio.fixture
|
|
async def debug_components(self, debug_db_client):
|
|
"""Create components for debugging."""
|
|
discovery = AutoDiscovery(debug_db_client)
|
|
await discovery.discover()
|
|
|
|
safety_enforcer = SafetyLimitEnforcer(debug_db_client)
|
|
await safety_enforcer.load_safety_limits()
|
|
emergency_stop_manager = EmergencyStopManager(debug_db_client)
|
|
watchdog = DatabaseWatchdog(debug_db_client, alert_manager=None, timeout_seconds=60)
|
|
|
|
setpoint_manager = SetpointManager(
|
|
db_client=debug_db_client,
|
|
discovery=discovery,
|
|
safety_enforcer=safety_enforcer,
|
|
emergency_stop_manager=emergency_stop_manager,
|
|
watchdog=watchdog
|
|
)
|
|
await setpoint_manager.start()
|
|
|
|
return {
|
|
'db_client': debug_db_client,
|
|
'discovery': discovery,
|
|
'safety_enforcer': safety_enforcer,
|
|
'emergency_stop_manager': emergency_stop_manager,
|
|
'watchdog': watchdog,
|
|
'setpoint_manager': setpoint_manager
|
|
}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_debug_setpoint_reading(self, debug_components):
|
|
"""Debug why setpoints are returning 0.0."""
|
|
db_client = debug_components['db_client']
|
|
setpoint_manager = debug_components['setpoint_manager']
|
|
emergency_stop_manager = debug_components['emergency_stop_manager']
|
|
|
|
# Check if emergency stop is active
|
|
emergency_stop_active = emergency_stop_manager.is_emergency_stop_active('DEBUG_STATION_001', 'DEBUG_PUMP_001')
|
|
print(f"Emergency stop active: {emergency_stop_active}")
|
|
|
|
# Check what's in the database
|
|
with db_client.engine.connect() as conn:
|
|
plans = conn.execute(
|
|
text("SELECT * FROM pump_plans WHERE station_id = 'DEBUG_STATION_001' AND pump_id = 'DEBUG_PUMP_001'")
|
|
).fetchall()
|
|
print(f"Pump plans in database: {plans}")
|
|
|
|
# Check pumps
|
|
pumps = conn.execute(
|
|
text("SELECT * FROM pumps WHERE station_id = 'DEBUG_STATION_001' AND pump_id = 'DEBUG_PUMP_001'")
|
|
).fetchall()
|
|
print(f"Pump in database: {pumps}")
|
|
|
|
# Check if there are any optimization plans
|
|
optimization_plans = conn.execute(
|
|
text("SELECT COUNT(*) FROM pump_plans")
|
|
).fetchone()
|
|
print(f"Total optimization plans: {optimization_plans}")
|
|
|
|
# Check plan status and time intervals
|
|
plan_details = conn.execute(
|
|
text("SELECT plan_status, interval_start, interval_end, suggested_speed_hz FROM pump_plans")
|
|
).fetchall()
|
|
print(f"Plan details: {plan_details}")
|
|
|
|
# Check current time in SQLite
|
|
current_time = conn.execute(
|
|
text("SELECT datetime('now')")
|
|
).fetchone()
|
|
print(f"Current time in SQLite: {current_time}")
|
|
|
|
# Check safety limits in database
|
|
safety_limits_in_db = conn.execute(
|
|
text("SELECT * FROM pump_safety_limits WHERE station_id = 'DEBUG_STATION_001' AND pump_id = 'DEBUG_PUMP_001'")
|
|
).fetchall()
|
|
print(f"Safety limits in database: {safety_limits_in_db}")
|
|
|
|
# Check all safety limits
|
|
all_safety_limits = conn.execute(
|
|
text("SELECT COUNT(*) FROM pump_safety_limits")
|
|
).fetchone()
|
|
print(f"Total safety limits in database: {all_safety_limits}")
|
|
|
|
# Debug safety limits
|
|
safety_enforcer = debug_components['safety_enforcer']
|
|
safety_limits = safety_enforcer.get_safety_limits('DEBUG_STATION_001', 'DEBUG_PUMP_001')
|
|
print(f"Safety limits: {safety_limits}")
|
|
|
|
# Check safety limits cache by looking at the internal cache
|
|
print(f"Safety limits cache keys: {list(safety_enforcer.safety_limits_cache.keys())}")
|
|
|
|
# Get setpoint
|
|
setpoint = setpoint_manager.get_current_setpoint('DEBUG_STATION_001', 'DEBUG_PUMP_001')
|
|
print(f"Setpoint returned: {setpoint}")
|
|
|
|
# Check all setpoints
|
|
all_setpoints = setpoint_manager.get_all_current_setpoints()
|
|
print(f"All setpoints: {all_setpoints}")
|
|
|
|
# The setpoint should be 42.5 from the optimization plan
|
|
assert setpoint is not None, "Setpoint should not be None"
|
|
assert setpoint > 0, f"Setpoint should be positive, got {setpoint}"
|
|
|
|
print(f"Debug test completed: setpoint={setpoint}") |