using PamhagenSysCtrl.Helpers; using PamhagenSysCtrl.Windows; 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), FontWeight = FontWeights.Bold, 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 (i > 57) { return; } else if (App.Plant.WantValveOpen(i)) { App.Plant.CloseV(i); } else { App.Plant.OpenV(i); } App.Plant.CommitChanges(); }; ValvePanel.Children.Add(b); } var w = new PlantSchemeWindow(); w.Show(); } 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.GetType().Name}: {exc.Message}"; if (exc.InnerException != null) str += $"\n\n{exc.InnerException.GetType().Name}: {exc.InnerException.Message}"; MessageBox.Show(str, "SPS-Fehler", MessageBoxButton.OK, MessageBoxImage.Error); return; } } public void OnUpdate(object? sender, PlantEventArgs 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 (ValvePanel.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; } ReblerButton.BorderBrush = plant.IsReblerActive ? Brushes.DarkGreen : Brushes.DarkRed; ReblerButton.Foreground = plant.IsReblerActive ? Brushes.DarkGreen : Brushes.DarkRed; ReblerButton.Background = plant.IsReblerActive ? Brushes.MintCream: Brushes.MistyRose; TroughAugerButton.BorderBrush = plant.IsTroughAugerActive ? Brushes.DarkGreen : Brushes.DarkRed; TroughAugerButton.Foreground = plant.IsTroughAugerActive ? Brushes.DarkGreen : Brushes.DarkRed; TroughAugerButton.Background = plant.IsTroughAugerActive ? Brushes.MintCream : Brushes.MistyRose; StirrerA1Button.BorderBrush = plant.IsStirrerA1Active ? Brushes.DarkGreen : Brushes.DarkRed; StirrerA1Button.Foreground = plant.IsStirrerA1Active ? Brushes.DarkGreen : Brushes.DarkRed; StirrerA1Button.Background = plant.IsStirrerA1Active ? Brushes.MintCream : Brushes.MistyRose; StirrerA2Button.BorderBrush = plant.IsStirrerA2Active ? Brushes.DarkGreen : Brushes.DarkRed; StirrerA2Button.Foreground = plant.IsStirrerA2Active ? Brushes.DarkGreen : Brushes.DarkRed; StirrerA2Button.Background = plant.IsStirrerA2Active ? Brushes.MintCream : Brushes.MistyRose; BeltButton.BorderBrush = plant.IsBeltActive ? Brushes.DarkGreen : Brushes.DarkRed; BeltButton.Foreground = plant.IsBeltActive ? Brushes.DarkGreen : Brushes.DarkRed; BeltButton.Background = plant.IsBeltActive ? Brushes.MintCream : Brushes.MistyRose; Auger1Button.BorderBrush = plant.IsAuger1Active ? Brushes.DarkGreen : Brushes.DarkRed; Auger1Button.Foreground = plant.IsAuger1Active ? Brushes.DarkGreen : Brushes.DarkRed; Auger1Button.Background = plant.IsAuger1Active ? Brushes.MintCream : Brushes.MistyRose; Auger2Button.BorderBrush = plant.IsAuger2Active ? Brushes.DarkGreen : Brushes.DarkRed; Auger2Button.Foreground = plant.IsAuger2Active ? Brushes.DarkGreen : Brushes.DarkRed; Auger2Button.Background = plant.IsAuger2Active ? Brushes.MintCream : Brushes.MistyRose; GradationPumpButton.BorderBrush = plant.IsGradationPumpActive ? Brushes.DarkGreen : Brushes.DarkRed; GradationPumpButton.Foreground = plant.IsGradationPumpActive ? Brushes.DarkGreen : Brushes.DarkRed; GradationPumpButton.Background = plant.IsGradationPumpActive ? Brushes.MintCream : Brushes.MistyRose; MP1BackwardButton.BorderBrush = plant.MP1 == MotorState.Backward ? Brushes.DarkGreen : Brushes.SlateGray; MP1BackwardButton.Foreground = plant.MP1 == MotorState.Backward ? Brushes.DarkGreen : Brushes.SlateGray; MP1BackwardButton.Background = plant.MP1 == MotorState.Backward ? Brushes.MintCream : Brushes.WhiteSmoke; MP1HaltButton.BorderBrush = plant.MP1 == MotorState.Halt ? Brushes.DarkRed : Brushes.SlateGray; MP1HaltButton.Foreground = plant.MP1 == MotorState.Halt ? Brushes.DarkRed : Brushes.SlateGray; MP1HaltButton.Background = plant.MP1 == MotorState.Halt ? Brushes.MistyRose : Brushes.WhiteSmoke; MP1ForwardButton.BorderBrush = plant.MP1 == MotorState.Forward ? Brushes.DarkGreen : Brushes.SlateGray; MP1ForwardButton.Foreground = plant.MP1 == MotorState.Forward ? Brushes.DarkGreen : Brushes.SlateGray; MP1ForwardButton.Background = plant.MP1 == MotorState.Forward ? Brushes.MintCream: Brushes.WhiteSmoke; MP2BackwardButton.BorderBrush = plant.MP2 == MotorState.Backward ? Brushes.DarkGreen : Brushes.SlateGray; MP2BackwardButton.Foreground = plant.MP2 == MotorState.Backward ? Brushes.DarkGreen : Brushes.SlateGray; MP2BackwardButton.Background = plant.MP2 == MotorState.Backward ? Brushes.MintCream : Brushes.WhiteSmoke; MP2HaltButton.BorderBrush = plant.MP2 == MotorState.Halt ? Brushes.DarkRed : Brushes.SlateGray; MP2HaltButton.Foreground = plant.MP2 == MotorState.Halt ? Brushes.DarkRed : Brushes.SlateGray; MP2HaltButton.Background = plant.MP2 == MotorState.Halt ? Brushes.MistyRose : Brushes.WhiteSmoke; MP2ForwardButton.BorderBrush = plant.MP2 == MotorState.Forward ? Brushes.DarkGreen : Brushes.SlateGray; MP2ForwardButton.Foreground = plant.MP2 == MotorState.Forward ? Brushes.DarkGreen : Brushes.SlateGray; MP2ForwardButton.Background = plant.MP2 == MotorState.Forward ? Brushes.MintCream : Brushes.WhiteSmoke; P12Button.BorderBrush = plant.P12 == MotorState.Forward ? Brushes.DarkGreen : Brushes.DarkRed; P12Button.Foreground = plant.P12 == MotorState.Forward ? Brushes.DarkGreen : Brushes.DarkRed; P12Button.Background = plant.P12 == MotorState.Forward ? Brushes.MintCream : Brushes.MistyRose; 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} """; } private void TroughAugerButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsTroughAugerActive) { App.Plant.StopTroughAuger(); } else { App.Plant.StartTroughAuger(); } App.Plant.CommitChanges(); } private void ReblerButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsReblerActive) { App.Plant.StopRebler(); } else { App.Plant.StartRebler(); } App.Plant.CommitChanges(); } private void StirrerA1Button_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsStirrerA1Active) { App.Plant.StopStirrerA1(); } else { App.Plant.StartStirrerA1(); } App.Plant.CommitChanges(); } private void StirrerA2Button_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsStirrerA2Active) { App.Plant.StopStirrerA2(); } else { App.Plant.StartStirrerA2(); } App.Plant.CommitChanges(); } private void BeltButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsBeltActive) { App.Plant.StopBelt(); } else { App.Plant.StartBelt(); } App.Plant.CommitChanges(); } private void Auger1Button_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsAuger1Active) { App.Plant.StopAuger1(); } else { App.Plant.StartAuger1(); } App.Plant.CommitChanges(); } private void Auger2Button_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsAuger2Active) { App.Plant.StopAuger2(); } else { App.Plant.StartAuger2(); } App.Plant.CommitChanges(); } private void GradationPumpButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.IsGradationPumpActive) { App.Plant.StopGradationPump(); } else { App.Plant.StartGradationPump(); } App.Plant.CommitChanges(); } private void MP1BackwardButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StartMP1Backward(); App.Plant.CommitChanges(); } private void MP1HaltButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StopMP1(); App.Plant.CommitChanges(); } private void MP1ForwardButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StartMP1Forward(); App.Plant.CommitChanges(); } private void MP2BackwardButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StartMP2Backward(); App.Plant.CommitChanges(); } private void MP2HaltButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StopMP2(); App.Plant.CommitChanges(); } private void MP2ForwardButton_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; App.Plant.StartMP2Forward(); App.Plant.CommitChanges(); } private void P12Button_Click(object? sender, RoutedEventArgs? evt) { if (App.Plant == null) return; if (App.Plant.P12 == MotorState.Forward) { App.Plant.StopP12(); } else { App.Plant.StartP12(); } App.Plant.CommitChanges(); } } }