Files
pamhagen-sysctrl/PamhagenSysCtrl/Windows/MainWindow.xaml.cs
T

88 lines
3.6 KiB
C#

using PamhagenSysCtrl.Helpers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace PamhagenSysCtrl {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
for (int i = 1; i <= 60; i++) {
var b = new Button() {
Content = $"V{i}",
Margin = new(10 + ((i - 1) / 10) * 50, 10 + ((i - 1) % 10) * 40, 10, 10),
BorderThickness = new(2),
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Height = 30,
Width = 40,
FontSize = 14,
};
var borderStyle = new Style(typeof(Border));
borderStyle.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(5)));
b.Resources.Add(typeof(Border), borderStyle);
b.Click += (sender, evt) => {
if (App.Plant == null || sender is not Button b) return;
var i = int.Parse($"{b.Content}"[1..]);
if (App.Plant.WantValveOpen(i)) {
App.Plant.CloseV(i);
} else {
App.Plant.OpenV(i);
}
App.Plant.CommitChanges();
};
ButtonPanel.Children.Add(b);
}
}
public async void ConnectButton_Click(object? sender, RoutedEventArgs evt) {
try {
App.Plant = new(PortInput.Text);
App.Plant.Update += OnUpdate;
await App.Plant.StartThread();
} catch (Exception exc) {
App.Plant?.Dispose();
App.Plant = null;
var str = "Beim Aufbauen einer Verbindung zur SPS ist ein Fehler aufgetreten:\n\n" + exc.Message;
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
MessageBox.Show(str, "SPS-Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void OnUpdate(object? sender, EventArgs evt) {
if (sender is not PamhagenPlant plant) return;
for (int i = 1; i <= 60; i++) {
var target = plant.WantValveClosed(i);
var actual = plant.IsValveClosed(i);
if (ButtonPanel.Children[i - 1] is not Button b)
continue;
b.BorderBrush = target ? Brushes.DarkRed : Brushes.SlateGray;
b.Foreground = target ? Brushes.DarkRed : Brushes.Black;
b.Background = actual ? Brushes.MistyRose : Brushes.WhiteSmoke;
}
Status.Text = $"""
Gradation °Oe: {plant.GradationOe:N2}
Presse Querschnecke: {plant.PresseQuerSchnecke}
Füllstände:
Wanne A1: {plant.FillLevelA1} (ausgewählt: {plant.WantA1Selected}, Status: {plant.IsA1Selected})
Wanne A2: {plant.FillLevelA2} (ausgewählt: {plant.WantA2Selected}, Status: {plant.IsA2Selected})
Presse 1: {plant.FillLevelPress1}
Presse 2: {plant.FillLevelPress2}
Tank 1: {plant.FillLevelT1}
Tank 2: {plant.FillLevelT2}
Tank 3: {plant.FillLevelT3}
Tank 4: {plant.FillLevelT4}
Tank 5: {plant.FillLevelT5}
Tank 6: {plant.FillLevelT6}
Tank 7: {plant.FillLevelT7}
Tank 8: {plant.FillLevelT8}
Tank 9: {plant.FillLevelT9}
""";
}
}
}