Add valve open/close logic

This commit is contained in:
2026-07-14 16:55:03 +02:00
parent 44a605598e
commit 77bcfcc3b8
3 changed files with 68 additions and 38 deletions
+48 -1
View File
@@ -4,6 +4,9 @@ using System.Windows.Media;
namespace PamhagenSysCtrl.Helpers {
public class PamhagenGraph : Graph {
private readonly List<Path> _selectedPaths = [];
public IEnumerable<Path> SelectedPaths => _selectedPaths;
public readonly Trough MW1;
public readonly Trough MW2;
@@ -347,7 +350,7 @@ namespace PamhagenSysCtrl.Helpers {
}
protected static Valve CreateValve(int n, double x, double y) {
return new($"V{n}", x, y, () => App.Plant?.IsValveOpen(4) ?? false, () => App.Plant?.WantValveOpen(4) ?? false);
return new(n, x, y, () => App.Plant?.IsValveOpen(4) ?? false, () => App.Plant?.WantValveOpen(4) ?? false);
}
protected static Tank CreateTank(int n, double x, double y, int capacity) {
@@ -377,5 +380,49 @@ namespace PamhagenSysCtrl.Helpers {
TraverseSubgraph(o, visited, edgeAction, valveAction);
}
}
public void AddPath(Path path) {
_selectedPaths.Add(path);
UpdateValvesAdjacientToPath(path, true);
if (App.Plant is PamhagenPlant plant) {
foreach (var (edge, _) in path.Hops) {
if (edge is Valve v) {
plant.OpenV(v.Nr);
}
}
plant.CommitChanges();
}
}
public void RemovePath(Path path) {
_selectedPaths.Remove(path);
UpdateValvesAdjacientToPath(path, false);
foreach (var p in _selectedPaths) UpdateValvesAdjacientToPath(p, true);
if (App.Plant is PamhagenPlant plant) {
foreach (var (edge, _) in path.Hops) {
if (edge is Valve v) {
plant.CloseV(v.Nr);
}
}
plant.CommitChanges();
}
}
private void UpdateValvesAdjacientToPath(Path path, bool locked) {
foreach (var (edge, node) in path.Hops) {
TraverseSubgraph(node, valveAction: (v) => v.IsLocked = locked);
}
}
public void ColorPipesAdjacientToPath(Path path, Brush? color1, Brush? color2) {
var color = color1;
HashSet<IEdge> visited = [];
foreach (var (edge, node) in path.Hops) {
edge.Highlight = color1;
visited.Add(edge);
if (node is Pump) color = color2;
TraverseSubgraph(node, visited, edgeAction: (e) => e.Highlight = color);
}
}
}
}