329 lines
12 KiB
JavaScript
329 lines
12 KiB
JavaScript
// Test script to verify discovery functionality
|
|
// This simulates the browser environment and tests the discovery system
|
|
|
|
// Mock browser environment
|
|
let modalDisplay = 'none';
|
|
const mockDocument = {
|
|
getElementById: function(id) {
|
|
console.log(`getElementById called with: ${id}`);
|
|
|
|
// Mock the modal
|
|
if (id === 'mapping-modal') {
|
|
return {
|
|
style: {
|
|
display: modalDisplay,
|
|
set display(value) {
|
|
modalDisplay = value;
|
|
console.log(`Modal display set to: ${value}`);
|
|
},
|
|
get display() {
|
|
return modalDisplay;
|
|
}
|
|
},
|
|
innerHTML: 'Mock modal content'
|
|
};
|
|
}
|
|
|
|
// Mock form fields
|
|
const mockFields = {
|
|
'mapping_id': { value: '' },
|
|
'protocol_type': { value: '', dispatchEvent: () => console.log('protocol_type change event') },
|
|
'protocol_address': { value: '' },
|
|
'station_id': {
|
|
value: '',
|
|
options: [{ value: '', textContent: 'Select Station' }, { value: 'station_main', textContent: 'Main Pump Station' }],
|
|
dispatchEvent: () => console.log('station_id change event')
|
|
},
|
|
'equipment_id': {
|
|
value: '',
|
|
options: [{ value: '', textContent: 'Select Equipment' }, { value: 'pump_primary', textContent: 'Primary Pump' }]
|
|
},
|
|
'data_type_id': {
|
|
value: '',
|
|
options: [{ value: '', textContent: 'Select Data Type' }, { value: 'speed_pump', textContent: 'Pump Speed' }]
|
|
},
|
|
'db_source': { value: '' }
|
|
};
|
|
|
|
return mockFields[id] || null;
|
|
},
|
|
querySelector: function(selector) {
|
|
console.log(`querySelector called with: ${selector}`);
|
|
return null;
|
|
},
|
|
querySelectorAll: function(selector) {
|
|
console.log(`querySelectorAll called with: ${selector}`);
|
|
return [];
|
|
}
|
|
};
|
|
|
|
// Mock global document
|
|
const document = mockDocument;
|
|
|
|
// Mock showAddMappingModal function
|
|
const showAddMappingModal = function() {
|
|
console.log('showAddMappingModal called');
|
|
const modal = document.getElementById('mapping-modal');
|
|
if (modal) {
|
|
modal.style.display = 'block';
|
|
console.log('Modal opened successfully');
|
|
}
|
|
};
|
|
|
|
// Import the discovery class (simplified version for testing)
|
|
class ProtocolDiscovery {
|
|
constructor() {
|
|
this.currentScanId = 'test-scan-123';
|
|
this.isScanning = false;
|
|
}
|
|
|
|
// Test the populateProtocolForm method
|
|
populateProtocolForm(endpoint) {
|
|
console.log('\n=== Testing populateProtocolForm ===');
|
|
|
|
// Create a new protocol mapping ID
|
|
const mappingId = `${endpoint.device_id}_${endpoint.protocol_type}`;
|
|
|
|
// Get default metadata IDs
|
|
const defaultStationId = this.getDefaultStationId();
|
|
const defaultEquipmentId = this.getDefaultEquipmentId(defaultStationId);
|
|
const defaultDataTypeId = this.getDefaultDataTypeId();
|
|
|
|
// Set form values
|
|
const formData = {
|
|
mapping_id: mappingId,
|
|
protocol_type: endpoint.protocol_type === 'opc_ua' ? 'opcua' : endpoint.protocol_type,
|
|
protocol_address: this.getDefaultProtocolAddress(endpoint),
|
|
device_name: endpoint.device_name || endpoint.device_id,
|
|
device_address: endpoint.address,
|
|
device_port: endpoint.port || '',
|
|
station_id: defaultStationId,
|
|
equipment_id: defaultEquipmentId,
|
|
data_type_id: defaultDataTypeId
|
|
};
|
|
|
|
console.log('Form data created:', formData);
|
|
|
|
// Auto-populate the protocol mapping form
|
|
this.autoPopulateProtocolForm(formData);
|
|
}
|
|
|
|
autoPopulateProtocolForm(formData) {
|
|
console.log('\n=== Testing autoPopulateProtocolForm ===');
|
|
console.log('Auto-populating protocol form with:', formData);
|
|
|
|
// First, open the "Add New Mapping" modal
|
|
this.openAddMappingModal();
|
|
|
|
// Wait for modal to be fully loaded and visible
|
|
const waitForModal = setInterval(() => {
|
|
const modal = document.getElementById('mapping-modal');
|
|
const isModalVisible = modal && modal.style.display !== 'none';
|
|
|
|
if (isModalVisible) {
|
|
clearInterval(waitForModal);
|
|
this.populateModalFields(formData);
|
|
}
|
|
}, 50);
|
|
|
|
// Timeout after 2 seconds
|
|
setTimeout(() => {
|
|
clearInterval(waitForModal);
|
|
const modal = document.getElementById('mapping-modal');
|
|
if (modal && modal.style.display !== 'none') {
|
|
this.populateModalFields(formData);
|
|
} else {
|
|
console.error('Modal did not open within timeout period');
|
|
}
|
|
}, 2000);
|
|
}
|
|
|
|
populateModalFields(formData) {
|
|
console.log('\n=== Testing populateModalFields ===');
|
|
console.log('Populating modal fields with:', formData);
|
|
|
|
// Find and populate form fields in the modal
|
|
const mappingIdField = document.getElementById('mapping_id');
|
|
const protocolTypeField = document.getElementById('protocol_type');
|
|
const protocolAddressField = document.getElementById('protocol_address');
|
|
const stationIdField = document.getElementById('station_id');
|
|
const equipmentIdField = document.getElementById('equipment_id');
|
|
const dataTypeIdField = document.getElementById('data_type_id');
|
|
const dbSourceField = document.getElementById('db_source');
|
|
|
|
console.log('Found fields:', {
|
|
mappingIdField: !!mappingIdField,
|
|
protocolTypeField: !!protocolTypeField,
|
|
protocolAddressField: !!protocolAddressField,
|
|
stationIdField: !!stationIdField,
|
|
equipmentIdField: !!equipmentIdField,
|
|
dataTypeIdField: !!dataTypeIdField,
|
|
dbSourceField: !!dbSourceField
|
|
});
|
|
|
|
// Populate mapping ID
|
|
if (mappingIdField) {
|
|
mappingIdField.value = formData.mapping_id;
|
|
console.log('✓ Set mapping_id to:', formData.mapping_id);
|
|
}
|
|
|
|
// Populate protocol type
|
|
if (protocolTypeField) {
|
|
protocolTypeField.value = formData.protocol_type;
|
|
console.log('✓ Set protocol_type to:', formData.protocol_type);
|
|
// Trigger protocol field updates
|
|
protocolTypeField.dispatchEvent(new Event('change'));
|
|
}
|
|
|
|
// Populate protocol address
|
|
if (protocolAddressField) {
|
|
protocolAddressField.value = formData.protocol_address;
|
|
console.log('✓ Set protocol_address to:', formData.protocol_address);
|
|
}
|
|
|
|
// Set station, equipment, and data type
|
|
if (stationIdField) {
|
|
this.waitForStationsLoaded(() => {
|
|
if (this.isValidStationId(formData.station_id)) {
|
|
stationIdField.value = formData.station_id;
|
|
console.log('✓ Set station_id to:', formData.station_id);
|
|
// Trigger equipment dropdown update
|
|
stationIdField.dispatchEvent(new Event('change'));
|
|
|
|
// Wait for equipment to be loaded
|
|
setTimeout(() => {
|
|
if (equipmentIdField && this.isValidEquipmentId(formData.equipment_id)) {
|
|
equipmentIdField.value = formData.equipment_id;
|
|
console.log('✓ Set equipment_id to:', formData.equipment_id);
|
|
}
|
|
|
|
if (dataTypeIdField && this.isValidDataTypeId(formData.data_type_id)) {
|
|
dataTypeIdField.value = formData.data_type_id;
|
|
console.log('✓ Set data_type_id to:', formData.data_type_id);
|
|
}
|
|
|
|
// Set default database source
|
|
if (dbSourceField && !dbSourceField.value) {
|
|
dbSourceField.value = 'measurements.' + formData.device_name.toLowerCase().replace(/[^a-z0-9]/g, '_');
|
|
console.log('✓ Set db_source to:', dbSourceField.value);
|
|
}
|
|
|
|
console.log('\n✅ Protocol form successfully populated!');
|
|
console.log('All fields should now be filled with discovery data.');
|
|
}, 100);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
openAddMappingModal() {
|
|
console.log('\n=== Testing openAddMappingModal ===');
|
|
console.log('Attempting to open Add New Mapping modal...');
|
|
|
|
// First try to use the global function
|
|
if (typeof showAddMappingModal === 'function') {
|
|
console.log('✓ Using showAddMappingModal function');
|
|
showAddMappingModal();
|
|
return;
|
|
}
|
|
|
|
console.log('❌ Could not find any way to open the protocol mapping modal');
|
|
}
|
|
|
|
getDefaultProtocolAddress(endpoint) {
|
|
const protocolType = endpoint.protocol_type;
|
|
switch (protocolType) {
|
|
case 'modbus_tcp':
|
|
return '40001';
|
|
case 'opc_ua':
|
|
return 'ns=2;s=MyVariable';
|
|
case 'modbus_rtu':
|
|
return '40001';
|
|
case 'rest_api':
|
|
return '/api/v1/data/endpoint';
|
|
default:
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
getDefaultStationId() {
|
|
const stationSelect = document.getElementById('station_id');
|
|
if (stationSelect && stationSelect.options.length > 1) {
|
|
return stationSelect.options[1].value;
|
|
}
|
|
return 'station_main';
|
|
}
|
|
|
|
getDefaultEquipmentId(stationId) {
|
|
const equipmentSelect = document.getElementById('equipment_id');
|
|
if (equipmentSelect && equipmentSelect.options.length > 1) {
|
|
return equipmentSelect.options[1].value;
|
|
}
|
|
if (stationId === 'station_main') return 'pump_primary';
|
|
if (stationId === 'station_backup') return 'pump_backup';
|
|
if (stationId === 'station_control') return 'controller_plc';
|
|
return 'pump_primary';
|
|
}
|
|
|
|
getDefaultDataTypeId() {
|
|
const dataTypeSelect = document.getElementById('data_type_id');
|
|
if (dataTypeSelect && dataTypeSelect.options.length > 1) {
|
|
return dataTypeSelect.options[1].value;
|
|
}
|
|
return 'speed_pump';
|
|
}
|
|
|
|
isValidStationId(stationId) {
|
|
const stationSelect = document.getElementById('station_id');
|
|
if (!stationSelect) return false;
|
|
return Array.from(stationSelect.options).some(option => option.value === stationId);
|
|
}
|
|
|
|
isValidEquipmentId(equipmentId) {
|
|
const equipmentSelect = document.getElementById('equipment_id');
|
|
if (!equipmentSelect) return false;
|
|
return Array.from(equipmentSelect.options).some(option => option.value === equipmentId);
|
|
}
|
|
|
|
isValidDataTypeId(dataTypeId) {
|
|
const dataTypeSelect = document.getElementById('data_type_id');
|
|
if (!dataTypeSelect) return false;
|
|
return Array.from(dataTypeSelect.options).some(option => option.value === dataTypeId);
|
|
}
|
|
|
|
waitForStationsLoaded(callback, maxWait = 3000) {
|
|
const stationSelect = document.getElementById('station_id');
|
|
if (!stationSelect) {
|
|
console.error('Station select element not found');
|
|
callback();
|
|
return;
|
|
}
|
|
|
|
// Check if stations are already loaded
|
|
if (stationSelect.options.length > 1) {
|
|
console.log('✓ Stations already loaded:', stationSelect.options.length);
|
|
callback();
|
|
return;
|
|
}
|
|
|
|
console.log('Waiting for stations to load...');
|
|
callback(); // In test, just call immediately
|
|
}
|
|
}
|
|
|
|
// Run the test
|
|
console.log('🚀 Starting Protocol Discovery Test\n');
|
|
|
|
const discovery = new ProtocolDiscovery();
|
|
|
|
// Test with a sample discovered endpoint
|
|
const sampleEndpoint = {
|
|
device_id: 'device_001',
|
|
protocol_type: 'modbus_tcp',
|
|
device_name: 'Water Pump Controller',
|
|
address: '192.168.1.100',
|
|
port: 502
|
|
};
|
|
|
|
console.log('Testing with sample endpoint:', sampleEndpoint);
|
|
discovery.populateProtocolForm(sampleEndpoint); |