167 lines
6.5 KiB
Python
167 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test the integration between discovery and simplified protocol mapping system
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
|
|
|
|
from src.dashboard.simplified_models import ProtocolSignalCreate, ProtocolType
|
|
from src.dashboard.simplified_configuration_manager import simplified_configuration_manager
|
|
|
|
def test_discovery_to_signal_workflow():
|
|
"""Test the complete workflow from discovery to signal creation"""
|
|
|
|
print("=" * 60)
|
|
print("Testing Discovery to Protocol Signal Integration")
|
|
print("=" * 60)
|
|
|
|
# Simulate discovery results
|
|
discovery_results = [
|
|
{
|
|
"device_name": "Boiler Temperature Sensor",
|
|
"protocol_type": "opcua",
|
|
"protocol_address": "ns=2;s=Temperature",
|
|
"data_point": "Temperature",
|
|
"device_address": "192.168.1.100"
|
|
},
|
|
{
|
|
"device_name": "Main Water Pump",
|
|
"protocol_type": "modbus_tcp",
|
|
"protocol_address": "40001",
|
|
"data_point": "Speed",
|
|
"device_address": "192.168.1.101"
|
|
},
|
|
{
|
|
"device_name": "System Pressure Sensor",
|
|
"protocol_type": "modbus_tcp",
|
|
"protocol_address": "40002",
|
|
"data_point": "Pressure",
|
|
"device_address": "192.168.1.102"
|
|
}
|
|
]
|
|
|
|
print("\n1. Discovery Results:")
|
|
for i, device in enumerate(discovery_results, 1):
|
|
print(f" {i}. {device['device_name']} - {device['protocol_type']} - {device['protocol_address']}")
|
|
|
|
# Convert discovery results to signal format
|
|
print("\n2. Converting Discovery to Signal Format:")
|
|
signals_created = []
|
|
|
|
for device in discovery_results:
|
|
# Generate signal name
|
|
signal_name = f"{device['device_name']} {device['data_point']}"
|
|
|
|
# Generate tags
|
|
tags = [
|
|
f"device:{device['device_name'].lower().replace(' ', '_')}",
|
|
f"protocol:{device['protocol_type']}",
|
|
f"data_point:{device['data_point'].lower().replace(' ', '_')}",
|
|
f"address:{device['device_address']}",
|
|
"discovered:true"
|
|
]
|
|
|
|
# Generate database source
|
|
db_source = f"measurements.{device['device_name'].lower().replace(' ', '_')}_{device['data_point'].lower().replace(' ', '_')}"
|
|
|
|
# Create signal
|
|
signal_create = ProtocolSignalCreate(
|
|
signal_name=signal_name,
|
|
tags=tags,
|
|
protocol_type=ProtocolType(device['protocol_type']),
|
|
protocol_address=device['protocol_address'],
|
|
db_source=db_source
|
|
)
|
|
|
|
# Add to configuration manager
|
|
success = simplified_configuration_manager.add_protocol_signal(signal_create)
|
|
|
|
if success:
|
|
signals_created.append(signal_create)
|
|
print(f" ✓ Created: {signal_name}")
|
|
print(f" Tags: {', '.join(tags)}")
|
|
print(f" Protocol: {device['protocol_type']} at {device['protocol_address']}")
|
|
print(f" DB Source: {db_source}")
|
|
else:
|
|
print(f" ✗ Failed to create: {signal_name}")
|
|
|
|
# Test filtering and retrieval
|
|
print("\n3. Testing Signal Management:")
|
|
|
|
# Get all signals
|
|
all_signals = simplified_configuration_manager.get_protocol_signals()
|
|
print(f" Total signals: {len(all_signals)}")
|
|
|
|
# Filter by protocol
|
|
modbus_signals = [s for s in all_signals if 'protocol:modbus_tcp' in s.tags]
|
|
print(f" Modbus TCP signals: {len(modbus_signals)}")
|
|
|
|
# Filter by device
|
|
boiler_signals = [s for s in all_signals if 'device:boiler_temperature_sensor' in s.tags]
|
|
print(f" Boiler signals: {len(boiler_signals)}")
|
|
|
|
# Get all tags
|
|
all_tags = simplified_configuration_manager.get_all_tags()
|
|
print(f" All tags: {len(all_tags)} unique tags")
|
|
|
|
# Test signal updates
|
|
print("\n4. Testing Signal Updates:")
|
|
if signals_created:
|
|
first_signal = signals_created[0]
|
|
signal_id = first_signal.generate_signal_id()
|
|
|
|
# Get the signal
|
|
signal = simplified_configuration_manager.get_protocol_signal(signal_id)
|
|
if signal:
|
|
print(f" Retrieved signal: {signal.signal_name}")
|
|
|
|
# Update the signal
|
|
updated_tags = signal.tags + ["unit:celsius", "alarm:high_temp"]
|
|
update_success = simplified_configuration_manager.update_protocol_signal(
|
|
signal_id,
|
|
tags=updated_tags,
|
|
preprocessing_enabled=True
|
|
)
|
|
|
|
if update_success:
|
|
print(f" ✓ Updated signal with new tags and preprocessing")
|
|
updated_signal = simplified_configuration_manager.get_protocol_signal(signal_id)
|
|
print(f" New tags: {', '.join(updated_signal.tags)}")
|
|
print(f" Preprocessing: {updated_signal.preprocessing_enabled}")
|
|
else:
|
|
print(f" ✗ Failed to update signal")
|
|
|
|
# Test signal deletion
|
|
print("\n5. Testing Signal Deletion:")
|
|
if signals_created:
|
|
last_signal = signals_created[-1]
|
|
signal_id = last_signal.generate_signal_id()
|
|
|
|
delete_success = simplified_configuration_manager.delete_protocol_signal(signal_id)
|
|
|
|
if delete_success:
|
|
print(f" ✓ Deleted signal: {last_signal.signal_name}")
|
|
remaining_signals = simplified_configuration_manager.get_protocol_signals()
|
|
print(f" Remaining signals: {len(remaining_signals)}")
|
|
else:
|
|
print(f" ✗ Failed to delete signal")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Integration Test Results:")
|
|
print(f" - Discovery devices processed: {len(discovery_results)}")
|
|
print(f" - Signals successfully created: {len(signals_created)}")
|
|
print(f" - Final signal count: {len(simplified_configuration_manager.get_protocol_signals())}")
|
|
print(f" - Unique tags available: {len(simplified_configuration_manager.get_all_tags())}")
|
|
|
|
if len(signals_created) == len(discovery_results):
|
|
print("\n✅ SUCCESS: All discovery devices successfully converted to protocol signals!")
|
|
print(" The simplified system is working correctly with discovery integration.")
|
|
else:
|
|
print("\n❌ FAILURE: Some discovery devices failed to convert to signals.")
|
|
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
test_discovery_to_signal_workflow() |