eDosStationFull/eDosStation_Backup_2025.01.09_11.33.03/EpdExit.xaml.cs

304 lines
9.8 KiB
C#
Raw Normal View History

2026-08-18 12:15:26 +02:00
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 ep;
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 startEPDRead()
{
EpdReading = true; bCancel.IsEnabled = false;
Dispatcher.InvokeAsync(new Action(() => { ep.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 || ep.HasStepError())
{
var s = ep.GetSteps();
DecodeResults.logsteps(module, ep.StepReturnValues, thisStation.splunkLog);
}
if (ep==null || !ep.EPDIssued)
{
Dispatcher.InvokeAsync(() =>
{
//rv = ep.SetDeIssued();
endWithMessage("This EPD is not issued");
});
return;
}
if (ep.EpdID == 0)
{
endWithMessage("This EPD is not found");
return;
}
int station = -1;
if (int.TryParse(ep.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 (ep.alarmStatus != 0)
{
DosePanel.Background = AlarmBrush;
Alarm.IsSelected = true;
//NoTimeout = true;
}
else if ((ep.otherAlarms & 0x7f) != 0)
{
DosePanel.Background = AlarmBrush;
OtherAlarm.IsSelected = true;
//NoTimeout = true;
}
else
{
DosePanel.Background = GreenBrush;
ok.IsSelected = true;
}
rv = ep.GetAllDeissue(true); //reconnect
if (rv != 0 || thisStation.LogAll)
{
//MessageBox.Show($"GetDoses {rv} rv:{ep.ErrorRV} step:{ep.ErrorStep} er:{ep.GetDosesError}");
ep.GetSteps();
if (rv!=0) MessageBox.Show(DecodeResults.decodesteps(ep.StepReturnValues));
DecodeResults.logsteps(module, ep.StepReturnValues, thisStation.splunkLog);
}
Wearer.Text = $"{ep.WearerName}";
Wearer.Name = WearerFullName;
EpdVisit.Text = $"{ep.WearerID}";
HpDose07.Text = $"{ep.Hp07Name}: {ep.Hp07:0.00} µSv ";
HpDose10.Text = $"{ep.Hp10Name}: {ep.Hp10:0.00} µSv";
if (EpdVisitID>0)
{
bool isBG = ep.isBG;
#if SimulateEPD
ep.CountsClock = 789;
#endif
var l = new SqlLog();
try
{
dataInterface.VisitExit(EpdVisitID, ep);
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 (ep == null)
try
{
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;
}
catch (System.Exception ex)
{
thisStation.splunkLog.WriteException(null, "StartEpd", ex);
MessageBox.Show(ex.Message);
ep = null;
}
if (ep != 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 (ep != null) { ep.WaitEnd(); ep.Dispose(); ep = null; }
}
catch (System.Exception ex) {
thisStation.splunkLog.WriteException("EpdExit", "Closing", ex);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
try
{
if (ep != null)
{
ep.WaitEnd();
ep.Dispose();
ep = 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();
}
}
}