using System; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; using Color = System.Drawing.Color; namespace ProductionUiCordonelLegacy.UserControls.FinalTest { /// /// Interaction logic for UcRadioCheck.xaml /// public partial class UcRadioCheck { public class UIContainerElement { public Int32? Value; public Color Color; public UIContainerElement(Int32? value) { Value = value; if (!value.HasValue) { Color = Color.LightYellow; } else { Color = Color.Transparent; } } public String GetText() { return Value.HasValue ? Value.Value.ToString() : "-"; } } public class UIContainer { public UIContainerElement CordMin; public UIContainerElement SirtMin; public UIContainerElement CordRssi; public UIContainerElement SirtRssi; public UIContainerElement CordMax; public UIContainerElement SirtMax; public UIContainer(Int32? cordMin = null, Int32? sirtMin = null, Int32? cordRssi = null, Int32? sirtRssi = null, Int32? cordMax = null, Int32? sirtMax = null) { CordMin = new UIContainerElement(cordMin); SirtMin = new UIContainerElement(sirtMin); CordRssi = new UIContainerElement(cordRssi); SirtRssi = new UIContainerElement(sirtRssi); CordMax = new UIContainerElement(cordMax); SirtMax = new UIContainerElement(sirtMax); //only when results from semi are prensent if (CordRssi.Value.HasValue && SirtRssi.Value.HasValue) { Boolean success = true; if (!CordMin.Value.HasValue || CordRssi.Value.Value < CordMin.Value.Value) { success = false; CordMin.Color = Color.Tomato; CordRssi.Color = Color.White; } if (!SirtMin.Value.HasValue || SirtRssi.Value.Value < SirtMin.Value.Value) { success = false; SirtMin.Color = Color.Tomato; SirtRssi.Color = Color.White; } if (!CordMax.Value.HasValue || CordRssi.Value.Value > CordMax.Value.Value) { success = false; CordMax.Color = Color.Tomato; CordRssi.Color = Color.White; } if (!SirtMax.Value.HasValue || SirtRssi.Value.Value > SirtMax.Value.Value) { success = false; SirtMax.Color = Color.Tomato; SirtRssi.Color = Color.White; } if (success) { CordRssi.Color = Color.LightGreen; SirtRssi.Color = Color.LightGreen; } } } } public void SetUi(UIContainer uiContainer) { Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => { setLbl(lblCordMin, uiContainer.CordMin); setLbl(lblSirtMin, uiContainer.SirtMin); setLbl(lblCordRssi, uiContainer.CordRssi); setLbl(lblSirtRssi, uiContainer.SirtRssi); setLbl(lblCordMax, uiContainer.CordMax); setLbl(lblSirtMax, uiContainer.SirtMax); } )); } private void setLbl(Label lbl, UIContainerElement element) { lbl.Content = element.GetText(); lbl.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(element.Color.A, element.Color.R, element.Color.G, element.Color.B)); } internal event EventHandler OnRetryRequested; internal event EventHandler OnResetRequested; internal event EventHandler OnDoneRequested; internal event EventHandler OnForceAPam; public UcRadioCheck() { InitializeComponent(); } private void btnReset_Click(Object sender, RoutedEventArgs e) { Task.Run(() => { OnResetRequested?.Invoke(sender, e); }); } private void Button_Click(Object sender, RoutedEventArgs e) { Task.Run(() => { OnRetryRequested?.Invoke(sender, e); }); } internal void ViewResult(Int32 WaitSeconds = 10) { BtnDone.Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => { BtnDone.IsEnabled = true; } )); Task.Run(() => { var waitMS = WaitSeconds * 1000; while (waitMS > 0) { var ms = waitMS; BtnDone.Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => { BtnDone.Content = $"Done({ms / 1000}s)"; } )); waitMS = waitMS - 1000; Thread.Sleep(1000); } BtnDone.Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => { BtnDone.Content = "Done"; } )); OnDoneRequested?.Invoke(this, EventArgs.Empty); }); } private void BtnDone_Click(Object sender, RoutedEventArgs e) { Task.Run(() => { OnDoneRequested?.Invoke(sender, e); }); BtnDone.IsEnabled = false; } private void BtnDone_Copy_Click(Object sender, RoutedEventArgs e) { Task.Run(() => { OnDoneRequested?.Invoke(sender, e); }); } private void BtnfrocePam_Click(Object sender, RoutedEventArgs e) { Task.Run(() => { OnForceAPam?.Invoke(sender, e); }); } } }