808 lines
37 KiB
C#
808 lines
37 KiB
C#
using PamhagenSysCtrl.Helpers;
|
|
using PamhagenSysCtrl.Helpers.Pipeline;
|
|
using PIEHidNetCore;
|
|
using System.Diagnostics;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace PamhagenSysCtrl.Windows {
|
|
public partial class PlantSchemeWindow : Window, PIEDataHandler, PIEErrorHandler {
|
|
|
|
private const double _minMove = 50;
|
|
|
|
private PIEDevice[]? devices;
|
|
private PIEDevice? xbe24;
|
|
private byte[]? lastData;
|
|
private Color[] lastColor = new Color[24];
|
|
|
|
private readonly PamhagenGraph Graph;
|
|
|
|
private Point? _lastMousePos;
|
|
private bool _mouseDragging;
|
|
private ISource? _lastSource;
|
|
private List<INode> _path = [];
|
|
private ISet<INode>? _lastSelected;
|
|
private ISet<INode>? _hover;
|
|
|
|
public double Scale { get; set; } = 5;
|
|
public double RoundedScale => Math.Round(Scale * 2) / 2;
|
|
|
|
public Path? HoveringPath {
|
|
get {
|
|
foreach (var b in FastSelectPaths.Where(b => b.Key.IsMouseOver)) {
|
|
if (Graph.SelectedPaths.Any(p => p.Start == b.Value.Source && p.Hops.Last().Node == b.Value.Sink))
|
|
continue;
|
|
return Graph.GetFreePath([b.Value.Source, b.Value.Sink], true);
|
|
}
|
|
|
|
if (_lastSource == null || _hover == null || !(_hover.Count > 0 || _path.Count > 0))
|
|
return null;
|
|
List<INode> points = [_lastSource, .. _path];
|
|
if (_hover.Count > 0 && !points.Contains(_hover.First())) {
|
|
points.Add(_hover.First());
|
|
}
|
|
if (points.Count >= 2) {
|
|
return Graph.GetFreePath(points);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected Dictionary<Button, (int KeyIdx, ISource Source, Pump Pump, ISink Sink)> FastSelectPaths;
|
|
|
|
public PlantSchemeWindow() {
|
|
InitializeComponent();
|
|
Menu_Help_CheckForUpdates.IsEnabled = App.Config.UpdateUrl != null;
|
|
Graph = new();
|
|
Graph.Draw(SchemeCanvas, RoundedScale);
|
|
FastSelectPaths = new Dictionary<Button, (int, ISource, Pump, ISink)> {
|
|
[FastSelectButton_MW1_Press1] = (12, Graph.MW1, Graph.MP1, Graph.Press1),
|
|
[FastSelectButton_MW2_Press1] = (18, Graph.MW2, Graph.MP2, Graph.Press1),
|
|
[FastSelectButton_MW1_Press2] = (13, Graph.MW1, Graph.MP1, Graph.Press2),
|
|
[FastSelectButton_MW2_Press2] = (19, Graph.MW2, Graph.MP2, Graph.Press2),
|
|
[FastSelectButton_MW1_Tank1] = (14, Graph.MW1, Graph.MP1, Graph.Tank1),
|
|
[FastSelectButton_MW2_Tank1] = (20, Graph.MW2, Graph.MP2, Graph.Tank1),
|
|
[FastSelectButton_MW1_Tank2] = (15, Graph.MW1, Graph.MP1, Graph.Tank2),
|
|
[FastSelectButton_MW2_Tank2] = (21, Graph.MW2, Graph.MP2, Graph.Tank2),
|
|
[FastSelectButton_MW1_Tank3] = (16, Graph.MW1, Graph.MP1, Graph.Tank3),
|
|
[FastSelectButton_MW2_Tank3] = (22, Graph.MW2, Graph.MP2, Graph.Tank3),
|
|
[FastSelectButton_MW1_Tank4] = (17, Graph.MW1, Graph.MP1, Graph.Tank4),
|
|
[FastSelectButton_MW2_Tank4] = (23, Graph.MW2, Graph.MP2, Graph.Tank4),
|
|
[FastSelectButton_MW1_Tank5] = (-1, Graph.MW1, Graph.MP1, Graph.Tank5),
|
|
[FastSelectButton_MW2_Tank5] = (-1, Graph.MW2, Graph.MP2, Graph.Tank5),
|
|
};
|
|
App.Plant?.Update += OnUpdate;
|
|
InitializePaths();
|
|
|
|
devices = PIEDevice.EnumeratePIE();
|
|
|
|
if (devices.Length > 0) {
|
|
foreach (var device in devices) {
|
|
// XBE-24 uses the Consumer HID usage page.
|
|
if (device.HidUsagePage == 0x0C &&
|
|
device.WriteLength > 1) {
|
|
xbe24 = device;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (xbe24 == null) {
|
|
return;
|
|
}
|
|
|
|
// Open the HID interface.
|
|
xbe24.SetupInterface();
|
|
|
|
// Receive input reports.
|
|
xbe24.SetDataCallback(this);
|
|
|
|
// Receive device errors, e.g. unplugging.
|
|
xbe24.SetErrorCallback(this);
|
|
|
|
lastData = new byte[xbe24.ReadLength];
|
|
|
|
MessageBox.Show($"Connected: {xbe24.ProductString}");
|
|
}
|
|
}
|
|
|
|
private void OnClosed(object sender, EventArgs evt) {
|
|
xbe24?.CloseInterface();
|
|
xbe24 = null;
|
|
|
|
Application.Current.Shutdown();
|
|
}
|
|
|
|
public void HandlePIEHidData(byte[] data, PIEDevice sourceDevice, int error) {
|
|
if (xbe24 == null || sourceDevice != xbe24)
|
|
return;
|
|
Dispatcher.Invoke(() => {
|
|
ProcessXbe24Data(data);
|
|
});
|
|
}
|
|
|
|
public void HandlePIEHidError(PIEDevice sourceDevice, int error) {
|
|
if (xbe24 == null || sourceDevice != xbe24)
|
|
return;
|
|
Dispatcher.Invoke(() => {
|
|
MessageBox.Show($"XBE-24: {error}", "XBE-24", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
});
|
|
}
|
|
|
|
private void ProcessXbe24Data(byte[] data) {
|
|
if (xbe24 == null || lastData == null)
|
|
return;
|
|
|
|
// XBE-24 button data starts at byte 3.
|
|
//
|
|
// There are 4 button bytes:
|
|
//
|
|
// byte 3 -> buttons 0-5
|
|
// byte 4 -> buttons 6-11
|
|
// byte 5 -> buttons 12-17
|
|
// byte 6 -> buttons 18-23
|
|
|
|
for (int column = 0; column < 4; column++) {
|
|
for (int row = 0; row < 6; row++) {
|
|
int button = column * 6 + row;
|
|
byte mask = (byte)(1 << row);
|
|
|
|
bool nowPressed =
|
|
(data[3 + column] & mask) != 0;
|
|
|
|
bool previouslyPressed =
|
|
(lastData[3 + column] & mask) != 0;
|
|
|
|
// Button was just pressed.
|
|
if (nowPressed && !previouslyPressed) {
|
|
OnButtonPressed(button);
|
|
}
|
|
|
|
// Button was just released.
|
|
if (!nowPressed && previouslyPressed) {
|
|
OnButtonReleased(button);
|
|
}
|
|
}
|
|
}
|
|
|
|
Array.Copy(data, lastData, xbe24.ReadLength);
|
|
}
|
|
|
|
private void OnButtonPressed(int button) {
|
|
// Your application logic goes here.
|
|
// 0 6 12 18
|
|
// 1 7 13 19
|
|
// 2 8 14 20
|
|
// 3 9 15 21
|
|
// 4 10 16 22
|
|
// 5 11 17 23
|
|
|
|
switch (button) {
|
|
case 2: FastSelectButton_EntryTroughStop_Click(null, null); break;
|
|
case 8: FastSelectButton_EntryTroughStart_Click(null, null); break;
|
|
case 3: FastSelectButton_MW2_Click(null, null); break;
|
|
case 9: FastSelectButton_MW1_Click(null, null); break;
|
|
case 4:
|
|
if (FastSelectButton_MP2_Forward.IsEnabled) {
|
|
FastSelectButton_MP2_Forward_Click(null, null);
|
|
} else {
|
|
SetXbeLight(4, Color.FromRgb(255, 0, 0));
|
|
}
|
|
break;
|
|
case 5: if (FastSelectButton_MP2_Stop.IsEnabled) FastSelectButton_MP2_Stop_Click(null, null); break;
|
|
case 10:
|
|
if (FastSelectButton_MP1_Forward.IsEnabled) {
|
|
FastSelectButton_MP1_Forward_Click(null, null);
|
|
} else {
|
|
SetXbeLight(10, Color.FromRgb(255, 0, 0));
|
|
}
|
|
break;
|
|
case 11: if (FastSelectButton_MP1_Stop.IsEnabled) FastSelectButton_MP1_Stop_Click(null, null); break;
|
|
case 12: FastSelectButton_Click(FastSelectButton_MW1_Press1, null); break;
|
|
case 13: FastSelectButton_Click(FastSelectButton_MW1_Press2, null); break;
|
|
case 14: FastSelectButton_Click(FastSelectButton_MW1_Tank1, null); break;
|
|
case 15: FastSelectButton_Click(FastSelectButton_MW1_Tank2, null); break;
|
|
case 16: FastSelectButton_Click(FastSelectButton_MW1_Tank3, null); break;
|
|
case 17: FastSelectButton_Click(FastSelectButton_MW1_Tank4, null); break;
|
|
case 18: FastSelectButton_Click(FastSelectButton_MW2_Press1, null); break;
|
|
case 19: FastSelectButton_Click(FastSelectButton_MW2_Press2, null); break;
|
|
case 20: FastSelectButton_Click(FastSelectButton_MW2_Tank1, null); break;
|
|
case 21: FastSelectButton_Click(FastSelectButton_MW2_Tank2, null); break;
|
|
case 22: FastSelectButton_Click(FastSelectButton_MW2_Tank3, null); break;
|
|
case 23: FastSelectButton_Click(FastSelectButton_MW2_Tank4, null); break;
|
|
}
|
|
}
|
|
|
|
private void OnButtonReleased(int button) {
|
|
// Your application logic goes here.
|
|
}
|
|
|
|
private void SetXbeLight(byte button, Color col, bool flashing = false) {
|
|
if (xbe24 == null || lastColor[button] == col)
|
|
return;
|
|
|
|
byte[] data = new byte[xbe24.WriteLength];
|
|
for (byte i = 0; i < 2; i++) {
|
|
data[0] = 0;
|
|
data[1] = 0xA5; // Set RGB LED
|
|
data[2] = button;
|
|
data[3] = i; // upper + lower LED
|
|
data[4] = col.R; // Red
|
|
data[5] = col.G; // Green
|
|
data[6] = col.B; // Blue
|
|
data[7] = flashing ? (byte)1 : (byte)0;
|
|
|
|
int result;
|
|
do {
|
|
result = xbe24.WriteData(data);
|
|
} while (result == 404);
|
|
}
|
|
|
|
lastColor[button] = col;
|
|
}
|
|
|
|
private void Menu_Settings_ManualControl_Changed(object sender, RoutedEventArgs evt) {
|
|
App.Plant?.ManualControlMode = Menu_Settings_ManualControl.IsChecked;
|
|
if (Menu_Settings_ManualControl.IsChecked) {
|
|
foreach (var win in Application.Current.Windows) {
|
|
if (win is ManualControlWindow w) {
|
|
w.Focus();
|
|
return;
|
|
}
|
|
}
|
|
var ctrl = new ManualControlWindow();
|
|
ctrl.Show();
|
|
} else {
|
|
foreach (var win in Application.Current.Windows) {
|
|
if (win is ManualControlWindow w) {
|
|
w.Close();
|
|
}
|
|
}
|
|
Menu_Settings_OverridePathClearances.IsChecked = false;
|
|
Menu_Settings_OverrideDryRunClearances.IsChecked = false;
|
|
|
|
InitializePaths();
|
|
}
|
|
OnUpdate(null, null);
|
|
}
|
|
|
|
private void Menu_Settings_OverridePathClearances_Changed(object sender, RoutedEventArgs evt) {
|
|
App.Plant?.OverridePathClearances = Menu_Settings_OverridePathClearances.IsChecked;
|
|
OnUpdate(null, null);
|
|
}
|
|
|
|
private void Menu_Settings_OverrideDryRunClearances_Changed(object sender, RoutedEventArgs evt) {
|
|
App.Plant?.OverrideDryRunClearances = Menu_Settings_OverrideDryRunClearances.IsChecked;
|
|
OnUpdate(null, null);
|
|
}
|
|
|
|
private void Menu_Settings_Directory_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
Process.Start("explorer.exe", App.DataPath);
|
|
} catch { }
|
|
}
|
|
|
|
private void Menu_Settings_Config_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
Process.Start(new ProcessStartInfo {
|
|
FileName = "notepad.exe",
|
|
Arguments = App.ConfigPath,
|
|
Verb = "runas",
|
|
UseShellExecute = true,
|
|
});
|
|
} catch { }
|
|
}
|
|
|
|
private async void Menu_Help_CheckForUpdates_Click(object sender, RoutedEventArgs evt) {
|
|
await App.CheckForUpdates(true);
|
|
}
|
|
|
|
private void Menu_Help_About_Click(object sender, RoutedEventArgs evt) {
|
|
var window = new AboutWindow {
|
|
Owner = this,
|
|
};
|
|
window.Show();
|
|
}
|
|
|
|
private void FastSelectButton_EntryTroughStart_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartTroughAuger();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_EntryTroughStop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopTroughAuger();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MW1_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.SelectMW1();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MW2_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.SelectMW2();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP1_Forward_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartMP1Forward();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP1_Stop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopMP1();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP2_Forward_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StartMP2Forward();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_MP2_Stop_Click(object sender, RoutedEventArgs evt) {
|
|
try {
|
|
App.Plant?.StopMP2();
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void FastSelectButton_Enter(object sender, MouseEventArgs evt) {
|
|
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void FastSelectButton_Leave(object sender, MouseEventArgs evt) {
|
|
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void FastSelectButton_Click(object sender, RoutedEventArgs? evt) {
|
|
if (sender is not Button b || !FastSelectPaths.TryGetValue(b, out var val))
|
|
return;
|
|
|
|
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == val.Source, null) is Path p1) {
|
|
Graph.RemovePath(p1);
|
|
if (p1.Hops.Last().Node == val.Sink)
|
|
return;
|
|
}
|
|
if (Graph.GetFreePath([val.Source, val.Sink]) is Path p2) {
|
|
Graph.AddPath(p2);
|
|
_lastSource = null;
|
|
}
|
|
}
|
|
|
|
private void OnSizeChanged(object sender, SizeChangedEventArgs evt) {
|
|
var rx = evt.NewSize.Width / 1550.0;
|
|
var ry = evt.NewSize.Height / 900.0;
|
|
var r = Math.Min(rx, ry);
|
|
Scale = r * 5;
|
|
SchemeCanvas.Margin = new();
|
|
Graph.Scale(RoundedScale);
|
|
if (RoundedScale < 4) {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Hidden;
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Hidden;
|
|
} else if (RoundedScale <= 5) {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Visible;
|
|
FastSelectButtonsLeft.ColumnDefinitions[0].Width = new(120);
|
|
FastSelectButtonsLeft.ColumnDefinitions[3].Width = new(120);
|
|
FastSelectButtonsLeft.RowDefinitions[0].Height = new(45);
|
|
FastSelectButtonsLeft.RowDefinitions[2].Height = new(40);
|
|
FastSelectButtonsLeft.RowDefinitions[4].Height = new(55);
|
|
FastSelectButtonsLeft.RowDefinitions[5].Height = new(55);
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Visible;
|
|
foreach (var col in FastSelectButtonsRight.ColumnDefinitions) {
|
|
if (col.Width.Value <= 5) continue;
|
|
col.Width = new(120);
|
|
}
|
|
foreach (var row in FastSelectButtonsRight.RowDefinitions) {
|
|
if (row.Height.Value <= 5) continue;
|
|
row.Height = new(40);
|
|
}
|
|
foreach (var b in FastSelectPaths.Keys) {
|
|
b.FontSize = 14;
|
|
}
|
|
FastSelectButton_EntryTrough_Start.FontSize = 12;
|
|
FastSelectButton_EntryTrough_Start_Run.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Stop.FontSize = 12;
|
|
FastSelectButton_EntryTrough_Stop_Run.FontSize = 14;
|
|
FastSelectButton_MW1.FontSize = 14;
|
|
FastSelectButton_MW2.FontSize = 14;
|
|
FastSelectButton_MP1_Stop.FontSize = 14;
|
|
FastSelectButton_MP1_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MP1_Forward.FontSize = 14;
|
|
FastSelectButton_MP1_Forward_Run.FontSize = 18;
|
|
FastSelectButton_MP2_Stop.FontSize = 14;
|
|
FastSelectButton_MP2_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MP2_Forward.FontSize = 14;
|
|
FastSelectButton_MP2_Forward_Run.FontSize = 18;
|
|
} else {
|
|
(FastSelectButtonsLeft.Parent as Border)?.Visibility = Visibility.Visible;
|
|
FastSelectButtonsLeft.ColumnDefinitions[0].Width = new(160);
|
|
FastSelectButtonsLeft.ColumnDefinitions[3].Width = new(160);
|
|
FastSelectButtonsLeft.RowDefinitions[0].Height = new(60);
|
|
FastSelectButtonsLeft.RowDefinitions[2].Height = new(50);
|
|
FastSelectButtonsLeft.RowDefinitions[4].Height = new(80);
|
|
FastSelectButtonsLeft.RowDefinitions[5].Height = new(80);
|
|
(FastSelectButtonsRight.Parent as Border)?.Visibility = Visibility.Visible;
|
|
foreach (var col in FastSelectButtonsRight.ColumnDefinitions) {
|
|
if (col.Width.Value <= 5) continue;
|
|
col.Width = new(160);
|
|
}
|
|
foreach (var row in FastSelectButtonsRight.RowDefinitions) {
|
|
if (row.Height.Value <= 5) continue;
|
|
row.Height = new(50);
|
|
}
|
|
foreach (var b in FastSelectPaths.Keys) {
|
|
b.FontSize = 16;
|
|
}
|
|
FastSelectButton_EntryTrough_Start.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Start_Run.FontSize = 18;
|
|
FastSelectButton_EntryTrough_Stop.FontSize = 14;
|
|
FastSelectButton_EntryTrough_Stop_Run.FontSize = 18;
|
|
FastSelectButton_MW1.FontSize = 16;
|
|
FastSelectButton_MW2.FontSize = 16;
|
|
FastSelectButton_MP1_Stop.FontSize = 16;
|
|
FastSelectButton_MP1_Stop_Run.FontSize = 24;
|
|
FastSelectButton_MP1_Forward.FontSize = 16;
|
|
FastSelectButton_MP1_Forward_Run.FontSize = 24;
|
|
FastSelectButton_MP2_Stop.FontSize = 16;
|
|
FastSelectButton_MP2_Stop_Run.FontSize = 24;
|
|
FastSelectButton_MP2_Forward.FontSize = 16;
|
|
FastSelectButton_MP2_Forward_Run.FontSize = 24;
|
|
}
|
|
UpdateScheme(Mouse.GetPosition(SchemeCanvas), Mouse.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseWheel(object sender, MouseWheelEventArgs evt) {
|
|
Scale = Math.Max(1, Math.Min(20, Scale + evt.Delta / 500.0));
|
|
Graph.Scale(RoundedScale);
|
|
UpdateScheme(evt.GetPosition(SchemeCanvas), evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseMove(object sender, MouseEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
if (_lastMousePos is Point p) {
|
|
if (!_mouseDragging) {
|
|
var dx = p.X - pos.X;
|
|
var dy = p.Y - pos.Y;
|
|
_mouseDragging = Math.Sqrt(dx * dx + dy * dy) >= _minMove;
|
|
}
|
|
if (_mouseDragging) {
|
|
var dx = SchemeCanvas.Margin.Right - SchemeCanvas.Margin.Left + p.X - pos.X;
|
|
var dy = SchemeCanvas.Margin.Bottom - SchemeCanvas.Margin.Top + p.Y - pos.Y;
|
|
SchemeCanvas.Margin = new(dx > 0 ? 0 : -dx, dy > 0 ? 0 : -dy, dx < 0 ? 0 : dx, dy < 0 ? 0 : dy);
|
|
}
|
|
}
|
|
UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
_lastMousePos = pos;
|
|
_mouseDragging = false;
|
|
_lastSelected = UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SchemeCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs evt) {
|
|
var pos = evt.GetPosition(SchemeCanvas);
|
|
_lastMousePos = null;
|
|
_mouseDragging = false;
|
|
var curSelected = UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
var clicked = curSelected.Intersect(_lastSelected ?? new HashSet<INode>()).ToList();
|
|
if (clicked.Count == 0) {
|
|
_lastSource = null;
|
|
}
|
|
foreach (var c in clicked) {
|
|
if (App.Plant?.ManualControlMode ?? false) {
|
|
if (c is Valve v) {
|
|
if (v.WantOpen) {
|
|
App.Plant.CloseV(v.Nr);
|
|
} else {
|
|
App.Plant.OpenV(v.Nr);
|
|
}
|
|
}
|
|
} else {
|
|
if (_lastSource == null && Graph.SelectedPaths.Any(p => p.Start == c)) {
|
|
var path = Graph.SelectedPaths.First(p => p.Start == c);
|
|
if (!path.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false)) {
|
|
Graph.RemovePath(path);
|
|
}
|
|
} else if (_lastSource != null && c is ISink sink) {
|
|
if (Graph.GetFreePath([_lastSource, .. _path, sink]) is Path path) {
|
|
Graph.AddPath(path);
|
|
_lastSource = null;
|
|
}
|
|
} else if (_lastSource != null) {
|
|
var path = Graph.GetFreePath([_lastSource, .. _path, c]);
|
|
if (path != null) {
|
|
_path.Add(c);
|
|
}
|
|
} else if (c is ISource source) {
|
|
_lastSource = source;
|
|
_path = [];
|
|
} else {
|
|
_lastSource = null;
|
|
}
|
|
}
|
|
try {
|
|
if (c is EntryTrough trough) {
|
|
if (trough.IsActive) {
|
|
App.Plant?.StopTroughAuger();
|
|
} else {
|
|
App.Plant?.StartTroughAuger();
|
|
}
|
|
} else if (c is Rebler rebler) {
|
|
if (rebler.IsActive) {
|
|
App.Plant?.StopRebler();
|
|
} else {
|
|
App.Plant?.StartRebler();
|
|
}
|
|
} else if (c is Switcher switcher) {
|
|
if (App.Plant?.WantMW1Selected ?? false) {
|
|
App.Plant?.SelectMW2();
|
|
} else {
|
|
App.Plant?.SelectMW1();
|
|
}
|
|
} else if (c is EncasedAuger auger) {
|
|
if (auger == Graph.Auger1) {
|
|
if (auger.IsActive) {
|
|
App.Plant?.StopAuger1();
|
|
} else {
|
|
App.Plant?.StartAuger1();
|
|
}
|
|
} else if (auger == Graph.Auger3) {
|
|
if (auger.IsActive) {
|
|
App.Plant?.StopAuger3();
|
|
} else {
|
|
App.Plant?.StartAuger3();
|
|
}
|
|
}
|
|
} else if (c is ConveyorBelt belt) {
|
|
if (belt.IsActive) {
|
|
App.Plant?.StopAuger2();
|
|
} else {
|
|
App.Plant?.StartAuger2();
|
|
}
|
|
} else if (c is Pump pump) {
|
|
if (pump == Graph.MP1) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopMP1();
|
|
} else {
|
|
App.Plant?.StartMP1Forward();
|
|
}
|
|
} else if (pump == Graph.MP2) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopMP2();
|
|
} else {
|
|
App.Plant?.StartMP2Forward();
|
|
}
|
|
} else if (pump == Graph.P12) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopP12();
|
|
} else {
|
|
App.Plant?.StartP12();
|
|
}
|
|
} else if (pump == Graph.P3) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopP3();
|
|
} else {
|
|
App.Plant?.StartP3();
|
|
}
|
|
} else if (pump == Graph.P4) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopP4();
|
|
} else {
|
|
App.Plant?.StartP4();
|
|
}
|
|
} else if (pump == Graph.P5) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopP5();
|
|
} else {
|
|
App.Plant?.StartP5();
|
|
}
|
|
} else if (pump == Graph.P6) {
|
|
if (pump.IsActive) {
|
|
App.Plant?.StopP6();
|
|
} else {
|
|
App.Plant?.StartP6();
|
|
}
|
|
}
|
|
}
|
|
} catch (NoClearanceException exc) {
|
|
MessageBox.Show($"{exc.Message}", "Keine Freigabe", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
UpdateScheme(pos, evt.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
private void SetFastSelectButton(Button b, byte? keyIdx, int m, bool disabled = false) {
|
|
b.IsEnabled = !disabled;
|
|
if (m == 1) {
|
|
b.Background = Brushes.MintCream;
|
|
b.BorderBrush = Brushes.DarkGreen;
|
|
b.Foreground = Brushes.DarkGreen;
|
|
if (keyIdx is byte k) SetXbeLight(k, Color.FromRgb(0, 255, 0));
|
|
} else if (m == 2) {
|
|
b.Background = Brushes.MistyRose;
|
|
b.BorderBrush = Brushes.DarkRed;
|
|
b.Foreground = Brushes.DarkRed;
|
|
if (keyIdx is byte k) SetXbeLight(k, Color.FromRgb(255, 0, 0));
|
|
} else {
|
|
b.Background = Brushes.LightGray;
|
|
b.BorderBrush = Brushes.DimGray;
|
|
b.Foreground = Brushes.Black;
|
|
if (keyIdx is byte k) SetXbeLight(k, disabled ? Color.FromRgb(8, 8, 8) : Color.FromRgb(255, 64, 64));
|
|
}
|
|
}
|
|
|
|
private void UpdateFastSelectPathButton(IEnumerable<Path> paths, Path? hoverPathDeactivate, Button b, byte? keyIdx) {
|
|
var e = FastSelectPaths[b];
|
|
SetFastSelectButton(b, keyIdx,
|
|
paths.Any(p => p.Start == e.Source && p.Hops.Last().Node == e.Sink) ? (hoverPathDeactivate?.Start == e.Source && hoverPathDeactivate?.Hops.Last().Node == e.Sink) ? 2 : 1 : 0,
|
|
e.Pump.IsActive || Graph.GetFreePath([e.Source, e.Sink], overrideStart: true) == null);
|
|
|
|
}
|
|
|
|
private ISet<INode> UpdateScheme(Point pos, bool down) {
|
|
foreach (var e in Graph.Edges) {
|
|
e.Highlight = null;
|
|
e.Flow = null;
|
|
}
|
|
foreach (var n in Graph.Nodes) {
|
|
if (n is Pump p) {
|
|
p.Highlight = null;
|
|
} else if (n is PipeJoin j) {
|
|
j.Highlight = null;
|
|
}
|
|
}
|
|
|
|
SetFastSelectButton(FastSelectButton_EntryTrough_Start, 8, App.Plant?.IsTroughAugerActive ?? false ? 1 : 0, !(App.Plant?.TroughAugerHasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_EntryTrough_Stop, 2, App.Plant?.IsTroughAugerActive ?? false ? 0 : 2);
|
|
SetFastSelectButton(FastSelectButton_MW1, 9, App.Plant?.IsMW1Selected ?? false ? 1 : 0);
|
|
SetFastSelectButton(FastSelectButton_MW2, 3, App.Plant?.IsMW2Selected ?? false ? 1 : 0);
|
|
|
|
SetFastSelectButton(FastSelectButton_MP1_Forward, 10, App.Plant?.MP1 == MotorState.Forward ? 1 : 0, !(App.Plant?.MP1HasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_MP1_Stop, 11, App.Plant?.MP1 == MotorState.Halt ? 2 : 0);
|
|
|
|
SetFastSelectButton(FastSelectButton_MP2_Forward, 4, App.Plant?.MP2 == MotorState.Forward ? 1 : 0, !(App.Plant?.MP2HasClearance ?? false));
|
|
SetFastSelectButton(FastSelectButton_MP2_Stop, 5, App.Plant?.MP2 == MotorState.Halt ? 2 : 0);
|
|
|
|
_hover = Graph.GetHover(pos.X, pos.Y, RoundedScale);
|
|
var hoverButtons = FastSelectPaths.Where(b => b.Key.IsMouseOver).ToDictionary();
|
|
Path? hoverPathDeactivate = null;
|
|
if (App.Plant?.ManualControlMode ?? false) {
|
|
foreach (var src in Graph.Nodes.Where(n => n is Pump)) {
|
|
Graph.ColorPipesByValves(src, PamhagenBrushes.Green, (src as Pump)?.State ?? default, PamhagenBrushes.DimOrange);
|
|
}
|
|
MP1_Target.Text = "Vor";
|
|
MP2_Target.Text = "Vor";
|
|
MP1_Target.FontSize = 16;
|
|
MP2_Target.FontSize = 16;
|
|
} else {
|
|
foreach (var p in Graph.SelectedPaths) {
|
|
Graph.ColorPipesAdjacientToPath(p, PamhagenBrushes.Green, p.Hops.Select(h => (h.Node as Pump)?.State ?? default).FirstOrDefault(n => n != MotorState.Halt), PamhagenBrushes.DimOrange);
|
|
}
|
|
if (_lastSource == null && _hover.Count > 0 && Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == _hover.First()) is Path path1) {
|
|
var pumpActive = path1.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false);
|
|
if (pumpActive) {
|
|
foreach (var pump in path1.Hops.Select(h => h.Node as Pump).Where(p => p != null)) {
|
|
pump?.Highlight = PamhagenBrushes.Red;
|
|
}
|
|
} else {
|
|
hoverPathDeactivate = path1;
|
|
Graph.ColorPipesAdjacientToPath(path1, PamhagenBrushes.Red, default, null);
|
|
}
|
|
} else if (hoverButtons.Count != 0 && Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == hoverButtons.First().Value.Source) is Path path2) {
|
|
var pumpActive = path2.Hops.Any(h => (h.Node as Pump)?.IsActive ?? false);
|
|
if (pumpActive) {
|
|
foreach (var pump in path2.Hops.Select(h => h.Node as Pump).Where(p => p != null)) {
|
|
pump?.Highlight = PamhagenBrushes.Red;
|
|
}
|
|
} else {
|
|
hoverPathDeactivate = path2;
|
|
Graph.ColorPipesAdjacientToPath(path2, PamhagenBrushes.Red, default, null);
|
|
}
|
|
}
|
|
if (_lastSource != null) {
|
|
foreach (var edge in _lastSource.Outputs) {
|
|
edge.Highlight = PamhagenBrushes.Green;
|
|
}
|
|
}
|
|
if (HoveringPath is Path hoveringPath) {
|
|
Graph.ColorPipesAdjacientToPath(hoveringPath, PamhagenBrushes.Green, default, PamhagenBrushes.DimOrange);
|
|
}
|
|
|
|
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == Graph.MW1) is Path pmw1) {
|
|
MP1_Target.Text = "\u2b9e " + string.Join(" \u2b9e ", pmw1.Hops
|
|
.Select(h => h.Node is Valve || h.Node is Pump || h.Node is CenterValve ? null : h.Node.Label)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l)));
|
|
} else {
|
|
MP1_Target.Text = "Vor";
|
|
}
|
|
if (Graph.SelectedPaths.Select(p => (Path?)p).FirstOrDefault(p => p?.Start == Graph.MW2) is Path pmw2) {
|
|
MP2_Target.Text = "\u2b9e " + string.Join(" \u2b9e ", pmw2.Hops
|
|
.Select(h => h.Node is Valve || h.Node is Pump || h.Node is CenterValve ? null : h.Node.Label)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l)));
|
|
} else {
|
|
MP2_Target.Text = "Vor";
|
|
}
|
|
var len1 = MP1_Target.Text.Length;
|
|
var len2 = MP2_Target.Text.Length;
|
|
MP1_Target.FontSize = len1 > 24 ? 8 : len1 > 20 ? 10 : len1 > 16 ? 12 : len1 > 12 ? 14 : 16;
|
|
MP2_Target.FontSize = len2 > 24 ? 8 : len2 > 20 ? 10 : len2 > 16 ? 12 : len2 > 12 ? 14 : 16;
|
|
}
|
|
|
|
foreach (var b in FastSelectPaths) {
|
|
//SetFastSelectButton(b.Key, b.Value.KeyIdx == -1 ? null : (byte)b.Value.KeyIdx, 0, false);
|
|
}
|
|
var paths = Graph.SelectedPaths;
|
|
if (HoveringPath is Path hp) paths = paths.Append(hp);
|
|
foreach (var b in FastSelectPaths) {
|
|
UpdateFastSelectPathButton(paths, hoverPathDeactivate, b.Key, b.Value.KeyIdx == -1 ? null : (byte)b.Value.KeyIdx);
|
|
}
|
|
|
|
Graph.Update(_hover, down);
|
|
return _hover;
|
|
}
|
|
|
|
public void OnUpdate(object? sender, PlantEventArgs? evt) {
|
|
if (App.Plant is PamhagenPlant p) {
|
|
Status_Connection.Text = $"Ja ({p.PlcPortName}" + (p.LastRttNs != null ? $", {p.LastRttNs / 1000000.0:N0} ms" : "") + ")";
|
|
Status_Plc.Text = $"{p.LastPlcStatus?.Mode} ({p.LastPlcStatus?.Flags:X3}) / {p.LastPlcError?.GetName()} ({(int?)p.LastPlcError:0000})";
|
|
Status_CompressedAir.Text = Enumerable.Range(1, 57).Any(p.IsValveClosed) ? "Ein" : Enumerable.Range(1, 57).Any(p.WantValveClosed) ? "Aus" : "?";
|
|
Status_ValvesOpen.Text = Enumerable.Range(1, 57).Count(p.IsValveOpen) + " / " + Enumerable.Range(1, 57).Count(p.WantValveOpen) + " / 57";
|
|
}
|
|
UpdateScheme(Mouse.GetPosition(SchemeCanvas), Mouse.LeftButton == MouseButtonState.Pressed);
|
|
}
|
|
|
|
public void InitializePaths() {
|
|
var add = ReconstructSelectedPaths().ToList();
|
|
var remove = Graph.SelectedPaths.ToList();
|
|
foreach (var p in remove) Graph.RemovePath(p);
|
|
for (int i = 1; i <= 57; i++) {
|
|
App.Plant?.CloseV(i);
|
|
}
|
|
foreach (var p in add) {
|
|
if (p.Hops.Any(h => h.Node is Valve v && v.IsLocked)) continue;
|
|
Graph.AddPath(p);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Path> ReconstructSelectedPaths() {
|
|
if (App.Plant is not PamhagenPlant p) yield break;
|
|
var sources = Graph.Nodes.Where(n => n is ISource).ToList();
|
|
var sinks = Graph.Nodes.Where(n => n is ISink).ToList();
|
|
foreach (var src in sources) {
|
|
var paths = new List<Path>();
|
|
foreach (var sink in sinks) {
|
|
paths.AddRange(Graph.GetPaths(src, sink, null, (n) => n is not ISink && (n is not Valve v || p.WantValveOpen(v.Nr))));
|
|
}
|
|
if (paths.Count == 1)
|
|
yield return paths.First();
|
|
}
|
|
}
|
|
}
|
|
}
|