using System; using System.Collections.Generic; using System.Text; using CtrEditor.ObjetosSim; namespace CtrEditor.HydraulicSimulator.TSNet.Components { /// /// Adaptador simplificado para osHydPipe - SOLO lectura de configuración y almacenamiento de resultados /// Los valores se capturan al INICIAR la simulación y no se modifican durante la ejecución /// public class TSNetPipeAdapter { private readonly osHydPipe pipe; private string pipeId; // Configuración capturada al inicio de simulación (INMUTABLE durante simulación) public PipeConfiguration Configuration { get; private set; } // Resultados calculados por TSNet (SOLO escritura desde TSNet) public TSNetPipeResults Results { get; private set; } public TSNetPipeAdapter(osHydPipe pipe) { this.pipe = pipe ?? throw new ArgumentNullException(nameof(pipe)); // Validación defensiva para Id if (pipe.Id == null) { throw new InvalidOperationException($"La tubería '{pipe.Nombre ?? "sin nombre"}' no tiene un Id válido"); } this.pipeId = $"PIPE_{pipe.Id.Value}"; this.Results = new TSNetPipeResults { PipeId = pipeId }; } /// /// ID único de la tubería para TSNet /// public string PipeId => pipeId; /// /// Captura la configuración actual de la tubería al INICIO de la simulación /// Esta configuración queda CONGELADA hasta que se detenga y reinicie la simulación /// public void CaptureConfigurationForSimulation() { Configuration = new PipeConfiguration { PipeId = pipeId, Length = pipe.Length, Diameter = pipe.Diameter, Roughness = pipe.Roughness, CapturedAt = DateTime.Now }; } /// /// Resetea los resultados (para nueva simulación) /// public void ResetCalculatedValues() { Results = new TSNetPipeResults { PipeId = pipeId }; } /// /// Validación de configuración capturada /// public List ValidateConfiguration() { var errors = new List(); if (Configuration == null) { errors.Add($"Tubería {PipeId}: Configuración no capturada"); return errors; } if (Configuration.Length <= 0) errors.Add($"Tubería {PipeId}: Length debe ser mayor que 0"); if (Configuration.Diameter <= 0) errors.Add($"Tubería {PipeId}: Diameter debe ser mayor que 0"); if (Configuration.Roughness < 0) errors.Add($"Tubería {PipeId}: Roughness no puede ser negativo"); return errors; } public override string ToString() { var configStatus = Configuration != null ? "Configured" : "Not Configured"; var resultStatus = Results?.FlowStatus ?? "No Results"; return $"TSNetPipeAdapter[{PipeId}] - Config: {configStatus}, Results: {resultStatus}"; } /// /// Aplica los resultados de TSNet a la tubería /// public void ApplyTSNetResults(Dictionary flows, Dictionary pressures) { if (Results == null) return; try { // Actualizar resultados desde los diccionarios de TSNet if (flows.ContainsKey(PipeId)) { Results.CalculatedFlowM3s = flows[PipeId]; Results.CalculatedFlowLMin = flows[PipeId] * 60000; // m³/s a L/min } Results.Timestamp = DateTime.Now; Results.FlowStatus = "Updated from TSNet"; } catch (Exception ex) { Results.FlowStatus = $"Error: {ex.Message}"; } } } /// /// Configuración inmutable de la tubería capturada al inicio de simulación /// public class PipeConfiguration { public string PipeId { get; set; } public DateTime CapturedAt { get; set; } // Propiedades físicas de la tubería public double Length { get; set; } public double Diameter { get; set; } public double Roughness { get; set; } } }