using System; using System.Text; using System.Windows; using System.Windows.Threading; namespace eDosStation { /// /// Interaction logic for ShowEPDDose.xaml /// public partial class ShowEPDDose : Window { private enum eEntryState { none, getepd, epdtimeout, epdError, epdOK, final }; #pragma warning disable CS0414 // The field 'ShowEPDDose.EntryState' is assigned but its value is never used private eEntryState EntryState; #pragma warning restore CS0414 // The field 'ShowEPDDose.EntryState' is assigned but its value is never used Station thisStation; EpdBaseClr.Epd2 ep; DispatcherTimer dispatcherTimer; private DateTime ResultTime; private User thisUser; #pragma warning disable CS0414 // The field 'ShowEPDDose.EpdReading' is assigned but its value is never used private bool EpdReading; #pragma warning restore CS0414 // The field 'ShowEPDDose.EpdReading' is assigned but its value is never used private bool FinalTimeOut; public ShowEPDDose(Station curStation) { InitializeComponent(); thisStation = curStation; EpdReading = false; FinalTimeOut = false; EntryState = eEntryState.none; } private void CountDown(object sender, EventArgs e) { double v = (ResultTime - DateTime.Now).TotalSeconds; if (v > Progress.Minimum) { Progress.Value = v; ProgressText.Text = ((int)v).ToString(); } else { SetTimedOutAndEnd(); } } #region EPD Callback private void Ep_EpdTO(EpdBaseClr.Epd2 Sender, EpdBaseClr.EpdTOEventArgs e) { System.Diagnostics.Debug.WriteLine("TIME OUT"); Dispatcher.InvokeAsync(() => { SetTimedOutAndEnd(); }); } private void Ep_EpdError(EpdBaseClr.Epd2 Sender, EpdBaseClr.EpdErrorEventArgs e) { System.Diagnostics.Debug.WriteLine("ERROR "); Dispatcher.InvokeAsync(() => { MessageBox.Show($"EPD Error {e.returnValue} {e.ErrorRV} {e.ErrorStep}"); Dispatcher.InvokeAsync(() => { SetTimedOutAndEnd(); }); }); } private void Ep_EpdRead(EpdBaseClr.Epd2 Sender, EpdBaseClr.EpdEventArgs e) { EpdReading = false; dispatcherTimer?.Stop(); Dispatcher.InvokeAsync(() => LoadDoses()); } private void Ep_EpdProgress(EpdBaseClr.Epd2 Sender, EpdSteps step, EpdStepResults result) { System.Diagnostics.Debug.WriteLine($"EPD Progress step:{step} result:{result}"); int index = -1; switch (step) { case EpdSteps.StepDiscovering: index = 0; break; case EpdSteps.StepReadStatus: index = 1; break; case EpdSteps.StepDeIssue: index = 2; break; } if (index >= 0 && index < stepEPD.Items.Count) { this.Dispatcher.InvokeAsync(new Action(() => stepEPD.SelectedIndex = 1), DispatcherPriority.Render); } } #endregion private void LoadDoses() { int rv = 0; if (ep == null) return; rv = ep.GetAll(true); if (rv != 0) MessageBox.Show("Communication error"); isIssued.Text = ep.EPDIssued ? "Yes" : "no"; string sPersID = ep.WearerName.Trim(); Wearer.Text = sPersID; EpdVisit.Text = ep.WearerID.ToString(); HardVersion.Text = ep.HardVersion.ToString() + '.' + ep.HardVersionMinor.ToString(); // ep.SoftVersion.ToString(); if (ep.eDoshasAlarm) { hasAlarm.Text = "Yes"; } else hasAlarm.Text = "No"; epdid.Text = ep.EpdID.ToString(); var stype = new StringBuilder(); if (ep.isMK3) stype.Append("MK3 "); else stype.Append("MK2 "); if (ep.isBG) stype.Append(" BG"); else stype.Append(" NG"); epdtype.Text = stype.ToString(); Decoder.AlarmToTextBox(sAlarm, ep); HpDose07l.Text = ep.Hp07Name; HpDose10l.Text = ep.Hp10Name; HpDose07.Text = $"{ep.Hp07:0.00} µSv"; HpDose10.Text = $"{ep.Hp10:0.00} µSv"; HP10AlarmDose1l.Text = "Warning " + ep.Hp10Name; HP10AlarmDose2l.Text = "Alarm " + ep.Hp10Name; HP07AlarmDosel.Text = "Alarm " + ep.Hp07Name; HP10AlarmDose1.Text = $"{ep.Hp10THDose1:0.00} µSv"; HP10AlarmDose2.Text = $"{ep.Hp10THDose2:0.00} µSv"; HP07AlarmDose.Text = $"{ep.Hp07THDose:0.00} µSv"; HpPeak07l.Text = ep.Hp07Name + " Peak"; HpPeak07.Text = $"{ep.Hp07Peak:0.00} µSv {ep.Hp07PeakTime:yyyy-MM-dd HH:mm} "; HpPeak10l.Text = ep.Hp10Name + " Peak"; HpPeak10.Text = $"{ep.Hp10Peak:0.00} µSv {ep.Hp10PeakTime:yyyy-MM-dd HH:mm} "; if (ep.isNG) { lNGain.Visibility = NGain.Visibility = Visibility.Visible; NGain.Text = ep.NGgain.ToString("#.##"); } else { lNGain.Visibility = NGain.Visibility = Visibility.Hidden; } if (!string.IsNullOrEmpty(sPersID)) { try { thisUser = thisStation.dataInterface.GetUser(string.Empty, sPersID, false); epdUserMargin.Text = thisUser.Margin.ToString(); } catch { } } else epdUserMargin.Text = "--"; Progress.Visibility = Visibility.Collapsed; bEpdCancel.IsEnabled = true; } private void Window_Loaded(object sender, RoutedEventArgs e) { FinalTimeOut = false; EpdReading = false; ep = new EpdBaseClr.Epd2(thisStation.ComPort, thisStation.OnlyMK2); ep.SetFeedbackSound(thisStation.useBeep, thisStation.useBeepMK3()); ep.EpdRead += Ep_EpdRead; ep.EpdError += Ep_EpdError; ep.EpdTO += Ep_EpdTO; ep.EpdProgress += Ep_EpdProgress; dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background); dispatcherTimer.Interval = thisStation.ScreenProgressBarInterval; dispatcherTimer.Tick += CountDown; ResultTime = DateTime.Now.AddSeconds(thisStation.ScreenEPDTimeout); Progress.Value = Progress.Maximum = thisStation.ScreenEPDTimeout; dispatcherTimer.Start(); startEPDRead(); //dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal); //dispatcherTimer.Interval = TimeSpan.FromMilliseconds(thisStation.EPDTimeOut / 10); //Progress.Maximum = Progress.Value = dispatcherTimer.Interval.TotalSeconds * 10; //dispatcherTimer.Tick += CountDown; //Dispatcher.InvokeAsync(() => ep.Wait(thisStation.EPDTimeOut, 1),DispatcherPriority.Background); //dispatcherTimer.Start(); } private void startEPDRead() { EpdReading = true; bEpdCancel.IsEnabled = false; Dispatcher.InvokeAsync(new Action(() => { ep.Wait(thisStation.EPDTimeOut, thisStation.EPDRetries); }), DispatcherPriority.Background); } private void SetTimedOutAndEnd() { if (!FinalTimeOut) { bEpdCancel.IsEnabled = true; FinalTimeOut = true; dispatcherTimer?.Stop(); Progress.Value = 0; Progress.Background = System.Windows.Media.Brushes.Red; statusEPD.Content = "Time out"; tWaitFor.Text = "Lezer niet gevonden !"; dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background); dispatcherTimer.Tick += ShowAndEnd; dispatcherTimer.Interval = thisStation.ShowTimeoutTime; dispatcherTimer.Start(); } } private void ShowAndEnd(object sender, EventArgs e) { dispatcherTimer?.Stop(); dispatcherTimer = null; endPage(); } private void endPage() { this.Close(); } private void RadButton_Click(object sender, RoutedEventArgs e) { endPage(); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { dispatcherTimer?.Stop(); dispatcherTimer = null; EpdReading = true; try { if (ep != null) { ep.WaitEnd(); ep.Dispose(); ep = null; } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } private void DosimeterView_CloseClicked(object sender, RoutedEventArgs e) { this.Close(); } } }