using System; using System.Windows; using System.Windows.Media; using System.Windows.Threading; namespace eDosStation { public partial class EpdExit : Window { DataInterface dataInterface; Station thisStation; EpdBaseClr.Epd2 epdDevice; DispatcherTimer dispatcherTimer; SolidColorBrush GreenBrush = new SolidColorBrush(Colors.LightGreen); SolidColorBrush AlarmBrush = new SolidColorBrush(Colors.LightCoral); private DateTime ResultTime; private bool EpdReading = false; private bool FinalTimeOut = false; #if SimulateEPD public SimulateData simulData = new SimulateData(); #endif public EpdExit(Station curStation) { InitializeComponent(); thisStation = curStation; dataInterface = curStation.dataInterface; } private void CountDown(object sender, EventArgs e) { double v = (ResultTime - DateTime.Now).TotalSeconds; if (v > Progress.Minimum) Progress.Value = v; else { SetTimedOutAndEnd(); } } 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}"); 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, int step, int result) { System.Diagnostics.Debug.WriteLine($"EPD Progress step:{step} result:{result}"); int index = -1; switch (step) { case 4: index = 0; break; case 5: index = 1; break; case 12: index = 2; break; } if (index >= 0 && index < stepEPD.Items.Count) { this.Dispatcher.Invoke(() => { stepEPD.SelectedIndex = index; // Optional: Force the progress step visual item to update status if needed if (stepEPD.ItemContainerGenerator.ContainerFromIndex(index) is FrameworkElement container) { container.UpdateLayout(); } }, System.Windows.Threading.DispatcherPriority.Normal); } } private void startEPDRead() { EpdReading = true; bCancel.IsEnabled = false; Dispatcher.InvokeAsync(new Action(() => { epdDevice.Wait(thisStation.EPDTimeOut, thisStation.EPDRetries); }), DispatcherPriority.Background); } private void SetTimedOutAndEnd() { if (!FinalTimeOut) { bCancel.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 LoadDoses() { const string module = "EpdExit LoadDoses"; int EpdVisitID; #if SimulateEPD ep.epdType = simulData.EdpType; ep.Issued = true; ep.alarmStatus = 0; ep.otherAlarms = 0; ep.WearerID = simulData.WearerID; ep.WearerName = simulData.WearerName; ep.Hp07 = 1; ep.Hp10 = 2; #endif int rv = 0; EpdVisitID = 0; if (thisStation.LogAll || epdDevice.HasStepError()) { var s = epdDevice.GetSteps(); DecodeResults.logsteps(module, epdDevice.StepReturnValues, thisStation.splunkLog); } if (epdDevice==null || !epdDevice.EPDIssued) { Dispatcher.InvokeAsync(() => { //rv = ep.SetDeIssued(); endWithMessage("This EPD is not issued"); }); return; } if (epdDevice.EpdID == 0) { endWithMessage("This EPD is not found"); return; } int station = -1; if (int.TryParse(epdDevice.WearerID, out EpdVisitID)) station = EpdVisitID % 100; if (station<=0) { endWithMessage($"This EPD was issued with an invalid id "); return; } if (station != thisStation.ID_Station) { endWithMessage($"This EPD was issued on another station (ID:{station})"); return; } //Check id EPD is manual exit string WearerFullName = string.Empty; int Exited = dataInterface.VisitInfo(EpdVisitID, out WearerFullName); if (Exited>0) { endWithMessage($"This EPD was already returned or had a manual exit (ID:{station})"); return; } //bool NoTimeout = false; if (epdDevice.alarmStatus != 0) { DosePanel.Background = AlarmBrush; Alarm.IsSelected = true; //NoTimeout = true; } else if ((epdDevice.otherAlarms & 0x7f) != 0) { DosePanel.Background = AlarmBrush; OtherAlarm.IsSelected = true; //NoTimeout = true; } else { DosePanel.Background = GreenBrush; ok.IsSelected = true; } rv = epdDevice.GetAllDeissue(true); //reconnect if (rv != 0 || thisStation.LogAll) { //MessageBox.Show($"GetDoses {rv} rv:{ep.ErrorRV} step:{ep.ErrorStep} er:{ep.GetDosesError}"); epdDevice.GetSteps(); if (rv!=0) MessageBox.Show(DecodeResults.decodesteps(epdDevice.StepReturnValues)); DecodeResults.logsteps(module, epdDevice.StepReturnValues, thisStation.splunkLog); } Wearer.Text = $"{epdDevice.WearerName}"; Wearer.Name = WearerFullName; EpdVisit.Text = $"{epdDevice.WearerID}"; HpDose07.Text = $"{epdDevice.Hp07Name}: {epdDevice.Hp07:0.00} µSv "; HpDose10.Text = $"{epdDevice.Hp10Name}: {epdDevice.Hp10:0.00} µSv"; if (EpdVisitID>0) { bool isBG = epdDevice.isBG; #if SimulateEPD ep.CountsClock = 789; #endif var l = new SqlLog(); try { dataInterface.VisitExit(EpdVisitID, epdDevice); l.LogCommand(DataInterface.VisitExitCommand); } catch (System.Exception ex) { l.LogCommand(ex.Message); MessageBox.Show(ex.Message); } l.Dispose(); } PersonName.Text = string.Empty; return; } private void Window_Loaded(object sender, RoutedEventArgs e) { Wait.IsSelected = true; dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background); dispatcherTimer.Interval = thisStation.ScreenProgressBarInterval; dispatcherTimer.Tick += CountDown; //StartEPD if (epdDevice == null) try { epdDevice = new EpdBaseClr.Epd2(thisStation.ComPort, thisStation.OnlyMK2); epdDevice.SetFeedbackSound(thisStation.useBeep, thisStation.useBeepMK3()); epdDevice.EpdRead += Ep_EpdRead; epdDevice.EpdError += Ep_EpdError; epdDevice.EpdTO += Ep_EpdTO; epdDevice.EpdProgress += Ep_EpdProgress; } catch (System.Exception ex) { thisStation.splunkLog.WriteException(null, "StartEpd", ex); MessageBox.Show(ex.Message); epdDevice = null; } if (epdDevice != null) { ResultTime = DateTime.Now.AddSeconds(thisStation.ScreenEPDTimeout); Progress.Value = Progress.Maximum = thisStation.ScreenEPDTimeout; dispatcherTimer.Start(); startEPDRead(); } } private void BCloseExit_Click(object sender, RoutedEventArgs e) { endPage(); } private void BConfirmAlarm_Click(object sender, RoutedEventArgs e) { endPage(); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { FinalTimeOut = false; if (dispatcherTimer?.IsEnabled == true) dispatcherTimer.IsEnabled = false; try { dispatcherTimer?.Stop(); dispatcherTimer = null; if (epdDevice != null) { epdDevice.WaitEnd(); epdDevice.Dispose(); epdDevice = null; } } catch (System.Exception ex) { thisStation.splunkLog.WriteException("EpdExit", "Closing", ex); System.Diagnostics.Debug.WriteLine(ex.Message); } try { if (epdDevice != null) { epdDevice.WaitEnd(); epdDevice.Dispose(); epdDevice = null; } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } public void endWithMessage(string message) { new InstMessage(message).ShowDialog(); endPage(); } public void endPage() { if (EpdReading) { //MessageBox.Show("Fout bij initialiseren EPD! "); } EpdReading = false; dispatcherTimer?.Stop(); dispatcherTimer = null; this.Close(); } } }