diff --git a/static/discovery.js b/static/discovery.js index 8f1a3d6..a579952 100644 --- a/static/discovery.js +++ b/static/discovery.js @@ -284,14 +284,14 @@ class ProtocolDiscovery { return; } - // Get station and pump info from form or prompt - const stationId = document.getElementById('station-id')?.value || 'station_001'; - const pumpId = document.getElementById('pump-id')?.value || 'pump_001'; - const dataType = document.getElementById('data-type')?.value || 'pressure'; - const dbSource = document.getElementById('db-source')?.value || 'influxdb'; + // Get station, equipment, and data type from our metadata + const stationId = this.getDefaultStationId(); + const equipmentId = this.getDefaultEquipmentId(stationId); + const dataTypeId = this.getDefaultDataTypeId(); + const dbSource = 'influxdb'; // Default database source try { - const response = await fetch(`/api/v1/dashboard/discovery/apply/${this.currentScanId}?station_id=${stationId}&pump_id=${pumpId}&data_type=${dataType}&db_source=${dbSource}`, { + const response = await fetch(`/api/v1/dashboard/discovery/apply/${this.currentScanId}?station_id=${stationId}&equipment_id=${equipmentId}&data_type_id=${dataTypeId}&db_source=${dbSource}`, { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -305,8 +305,8 @@ class ProtocolDiscovery { this.showNotification(`Successfully created ${result.created_mappings.length} protocol mappings from discovery results`, 'success'); // Refresh protocol mappings grid - if (window.protocolMappingGrid) { - window.protocolMappingGrid.loadProtocolMappings(); + if (window.loadProtocolMappings) { + window.loadProtocolMappings(); } } else { this.showNotification('No protocol mappings were created. Check the discovery results for compatible endpoints.', 'warning'); @@ -366,6 +366,11 @@ class ProtocolDiscovery { // Create a new protocol mapping ID const mappingId = `${endpoint.device_id}_${endpoint.protocol_type}`; + // Get default metadata IDs from our sample metadata + const defaultStationId = this.getDefaultStationId(); + const defaultEquipmentId = this.getDefaultEquipmentId(defaultStationId); + const defaultDataTypeId = this.getDefaultDataTypeId(); + // Set form values (these would be used when creating a new mapping) const formData = { mapping_id: mappingId, @@ -374,9 +379,9 @@ class ProtocolDiscovery { device_name: endpoint.device_name || endpoint.device_id, device_address: endpoint.address, device_port: endpoint.port || '', - station_id: 'station_001', // Default station ID - equipment_id: 'equipment_001', // Default equipment ID - data_type_id: 'datatype_001' // Default data type ID + station_id: defaultStationId, + equipment_id: defaultEquipmentId, + data_type_id: defaultDataTypeId }; // Store form data for later use @@ -399,25 +404,29 @@ class ProtocolDiscovery { // Wait a moment for the modal to open, then populate fields setTimeout(() => { // 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 deviceNameField = document.getElementById('device-name'); - const deviceAddressField = document.getElementById('device-address'); - const devicePortField = document.getElementById('device-port'); - const stationIdField = document.getElementById('station-id'); - const equipmentIdField = document.getElementById('equipment-id'); - const dataTypeIdField = document.getElementById('data-type-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'); - if (mappingIdField) mappingIdField.value = formData.mapping_id; if (protocolTypeField) protocolTypeField.value = formData.protocol_type; if (protocolAddressField) protocolAddressField.value = formData.protocol_address; - if (deviceNameField) deviceNameField.value = formData.device_name; - if (deviceAddressField) deviceAddressField.value = formData.device_address; - if (devicePortField) devicePortField.value = formData.device_port; - if (stationIdField) stationIdField.value = formData.station_id; - if (equipmentIdField) equipmentIdField.value = formData.equipment_id; - if (dataTypeIdField) dataTypeIdField.value = formData.data_type_id; + + // Set station, equipment, and data type if they exist in our metadata + if (stationIdField && this.isValidStationId(formData.station_id)) { + stationIdField.value = formData.station_id; + // Trigger equipment dropdown update + stationIdField.dispatchEvent(new Event('change')); + } + + if (equipmentIdField && this.isValidEquipmentId(formData.equipment_id)) { + equipmentIdField.value = formData.equipment_id; + } + + if (dataTypeIdField && this.isValidDataTypeId(formData.data_type_id)) { + dataTypeIdField.value = formData.data_type_id; + } // Show success message this.showNotification(`Protocol form populated with ${formData.device_name}. Please review and complete any missing information.`, 'success'); @@ -542,6 +551,75 @@ class ProtocolDiscovery { } }, 5000); } + + /** + * Get default station ID from available metadata + */ + getDefaultStationId() { + // Try to get the first station from our metadata + const stationSelect = document.getElementById('station_id'); + if (stationSelect && stationSelect.options.length > 1) { + return stationSelect.options[1].value; // First actual station (skip "Select Station") + } + // Fallback to our sample metadata IDs + return 'station_main'; + } + + /** + * Get default equipment ID for a station + */ + getDefaultEquipmentId(stationId) { + // Try to get the first equipment for the station + const equipmentSelect = document.getElementById('equipment_id'); + if (equipmentSelect && equipmentSelect.options.length > 1) { + return equipmentSelect.options[1].value; // First actual equipment + } + // Fallback based on station + if (stationId === 'station_main') return 'pump_primary'; + if (stationId === 'station_backup') return 'pump_backup'; + if (stationId === 'station_control') return 'controller_plc'; + return 'pump_primary'; + } + + /** + * Get default data type ID + */ + getDefaultDataTypeId() { + // Try to get the first data type from our metadata + const dataTypeSelect = document.getElementById('data_type_id'); + if (dataTypeSelect && dataTypeSelect.options.length > 1) { + return dataTypeSelect.options[1].value; // First actual data type + } + // Fallback to our sample metadata IDs + return 'speed_pump'; + } + + /** + * Check if station ID exists in our metadata + */ + isValidStationId(stationId) { + const stationSelect = document.getElementById('station_id'); + if (!stationSelect) return false; + return Array.from(stationSelect.options).some(option => option.value === stationId); + } + + /** + * Check if equipment ID exists in our metadata + */ + isValidEquipmentId(equipmentId) { + const equipmentSelect = document.getElementById('equipment_id'); + if (!equipmentSelect) return false; + return Array.from(equipmentSelect.options).some(option => option.value === equipmentId); + } + + /** + * Check if data type ID exists in our metadata + */ + isValidDataTypeId(dataTypeId) { + const dataTypeSelect = document.getElementById('data_type_id'); + if (!dataTypeSelect) return false; + return Array.from(dataTypeSelect.options).some(option => option.value === dataTypeId); + } } // Initialize discovery when DOM is loaded