eDosStation/eDosStation/EntryBadge.xaml.cs
2020-02-05 14:21:53 +01:00

517 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace eDosStation
{
/// <summary>
/// Interaction logic for EntryBadge.xaml
/// </summary>
public partial class EntryBadge : Window
{
bool initialising = true;
Station thisStation;
EpdBaseClr.Epd2 ep;
bool EpdFound = false;
DispatcherTimer dispatcherTimer;
int Search;
Boolean Inst1 = false, Inst2 = false;
public static OkReadBadge.Badging a;
System.Threading.Thread t;
bool nextCSN = false;
bool nextSite = false;
bool nextCode = false;
bool nextFinish = false;
string csn, site, code;
static User _User;
DataSet1.GetTaskInfoRow CurTaskRow;
private bool StopReadBadge=true;
public EntryBadge(EpdBaseClr.Epd2 curEP, Station curStation)
{
ep=curEP;
thisStation = curStation;
InitializeComponent();
_User = null;
}
private bool LoadDoses()
{
bool rv = true;
System.Diagnostics.Debug.WriteLine("LoadDoses: EPD GEt Status");
int rva= ep.GetStatus();
if (rva != 1)
{
MessageBox.Show($"Entry GetStatus error {rva} {ep.GetStatusError}");
}
System.Diagnostics.Debug.WriteLine($"LoadDoses: EPD {ep.EpdID} Type:{ep.epdType}");
if (ep.Issued)
{
MessageBox.Show("EPD is currently issued\nPlease take an other", "EDP", MessageBoxButton.OK, MessageBoxImage.Hand, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
this.Close();
return false;
}
bool isBG = (ep.epdType == 0);
if (isBG != CurTaskRow.EPDRad.Equals("BG"))
{
MessageBox.Show("Wrong EPD Type", "EDP", MessageBoxButton.OK, MessageBoxImage.Hand, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
this.Close();
return false;
}
statusEPD.Text = $"EPD: {ep.EpdID}";
rva = ep.ReadTresholds();
if (rva != 1)
{
MessageBox.Show($"ReadTresholds error {ep.ReadTresholdsError}");
}
return rv;
//ep.Hp07THDose =
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
EpdFound = false;
dispatcherTimer.Stop();
ep.Close();
System.Threading.Thread.Sleep(200);
int rv= ep.Connect();
System.Diagnostics.Debug.WriteLine($"Connect {rv} error {ep.ConnectError}");
if (ep.DeviceCount >0 && ep.b.CurrentDeviceID>0)
{
System.Diagnostics.Debug.WriteLine($"Devices:{ep.DeviceCount} id:{ep.b.CurrentDeviceID} {ep.ConnectError} ");
EpdFound = true;
DoEPD();
}
if (!EpdFound)
{
Search--;
if (Search > 0)
{
statusEPD.Text = $"Search {Search} ({ep.ConnectError})";
dispatcherTimer.Start();
}
else
{
statusEPD.Text = $"Search Timeout";
}
}
}
private void DoEPD()
{
if (LoadDoses())
{
bool isBG = (ep.epdType != 3); //3 = zwarte 0=grijs
int? VisitID = null;
using (var co = new System.Data.SqlClient.SqlConnection(Properties.Settings.Default.RemoteSQL))
{
using (var cmd1 = new SqlCommand("VisitInit", co))
{
cmd1.CommandType = CommandType.StoredProcedure;
var pa = new SqlParameter("PersID", SqlDbType.Int, 4); pa.Value = _User.PersID; cmd1.Parameters.Add(pa);
pa = new SqlParameter("ID_Task", SqlDbType.Int, 4); pa.Value = CurTaskRow.ID_Task; cmd1.Parameters.Add(pa);
pa = new SqlParameter("Inst_CODE", SqlDbType.Int, 4); pa.Value = CurTaskRow.Inst_CODE; cmd1.Parameters.Add(pa);
pa = new SqlParameter("EPD_InternalID", SqlDbType.Int, 4); pa.Value = (int)ep.EpdID; cmd1.Parameters.Add(pa);
pa = new SqlParameter("isBG", SqlDbType.Bit, 4); pa.Value = isBG; cmd1.Parameters.Add(pa);
pa = new SqlParameter("ID_Visit", SqlDbType.Int, 4); pa.Direction = ParameterDirection.Output; cmd1.Parameters.Add(pa);
co.Open();
cmd1.ExecuteNonQuery();
VisitID = (int?)cmd1.Parameters["ID_Visit"].Value;
co.Close();
}
}
int rv= ep.PrepareForIssue();
if (rv != 1)
{
MessageBox.Show($"PrepareForIssue error {ep.PrepareForIssueError}");
}
ep.WearerID = VisitID.ToString();
ep.WearerName = _User.PersID.ToString();
ep.Hp07RateOn = CurTaskRow.HP07RateON;
ep.Hp07RateOff = CurTaskRow.HP07RateOFF;
ep.Hp10Rate1On = CurTaskRow.HP10Rate1ON;
ep.Hp10Rate1Off = CurTaskRow.HP10Rate1ON;
ep.Hp10Rate2On = CurTaskRow.HP10Rate2ON;
ep.Hp10Rate2Off = CurTaskRow.HP10Rate2ON;
ep.Hp10THDose1 = CurTaskRow.Hp10Dose1;
ep.Hp10THDose2 = CurTaskRow.Hp10Dose2;
ep.Hp07THDose = CurTaskRow.Hp07Dose;
int? kx = null;
if (!CurTaskRow.IsFNGain_ANGainValueNull()) kx = (int)CurTaskRow.FNGain_ANGainValue;
if (kx != null && (ep.K3 != kx || ep.K4 != kx))
{
ep.K3 = ep.K4 = kx.Value;
//ep.K1 = 903;
//ep.K2 = 613;
//ep.K3 = 90;
//ep.K4 = 90;
//ep.K5 = 13100;
//ep.K6 = 1310;
//ep.K1 = 857;
//ep.K2 = 595;
//ep.K3 = 100;
//ep.K4 = 100;
//ep.K5 = 13100;
//ep.K6 = 1310;
if (!isBG)
{
if (ep.K6 != 1310)
{
MessageBox.Show("Alarm");
}
else
{
ep.WriteCalibration();
}
}
}
rv = ep.SetIssued();
if (rv != 1)
{
MessageBox.Show($"SetIssued error {ep.SetIssuedError}");
}
bEpdDone.Visibility = Visibility.Visible;
}
}
private void GetEPD()
{
if (_User == null || CurTaskRow == null) return;
int ok = ep.Connect();
if (ok != 1) System.Diagnostics.Debug.WriteLine($"GetEpd {ok} ConnectError: {ep.ConnectError}");
if (ep.DeviceCount == 1 && ep.b.CurrentDeviceID>0)
{
EpdFound = true;
DoEPD();
}
if (!EpdFound)
{
statusEPD.Text = $"EPD: Wait ";
Search = 100;
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
dispatcherTimer.Start();
return;
}
}
private void SetFilter(bool isAlara)
{
Inst1 = (cbInst1.IsChecked ==true);
Inst2 = (cbInst2.IsChecked ==true);
StringBuilder fi = new StringBuilder();
if (!Inst1 && !Inst2)
{
fi.Append(" 1=2 ");
}
else
{
if (Inst1 == true) fi.Append($"{(Inst2 ? "(" : "") } Inst_CODE = {thisStation.Inst_CODE1} ");
if (Inst2 == true) fi.Append($" {(Inst1 ? "or" : "") } Inst_CODE={thisStation.Inst_CODE2} {(Inst1 ? ")" : "")} ");
fi.Append($" {(Inst1 || Inst2 ? "AND" : "") } TaskType_CODE { (isAlara ? ">=" : "<")} '1000' ");
}
System.Windows.Data.CollectionViewSource getTaskListViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("getTaskListViewSource")));
var dv = getTaskListViewSource.View.SourceCollection as DataView;
//if (isAlara) dv.RowFilter = "ID_Task>=1000"; else dv.RowFilter = "ID_Task<1000";
try
{
dv.RowFilter = fi.ToString();
} catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
getTaskListViewSource.View.MoveCurrentToFirst();
}
void AskTask()
{
if (_User == null || _User.VisitIDLocked == true || _User.AccessAllow == false)
{
tbNotAllowed.IsSelected = true;
if (_User.VisitIDLocked == true || _User.AccessAllow == false) tReasonNoAccess.Text = _User.ReasonNoAccess;
else tReasonNoAccess.Text = "No identification";
}
else
{
if (!string.IsNullOrEmpty(_User.UserComment))
{
MessageBox.Show(_User.UserComment, "Comment", MessageBoxButton.OK, MessageBoxImage.Asterisk);
}
eDosStation.DataSet1 dataSet1 = ((eDosStation.DataSet1)(this.FindResource("dataSet1")));
using (var dataSet1GetTaskInfoTableAdapter = new eDosStation.DataSet1TableAdapters.GetTaskInfoTableAdapter())
{
dataSet1GetTaskInfoTableAdapter.Fill(dataSet1.GetTaskInfo, null, _User.Margin);
}
tbTask.IsSelected = true;
bool isAlara = (cbAlara.IsChecked == true);
SetFilter(isAlara);
}
}
void CheckBadge()
{
_User = new User(CSN.Text, Code.Text);
if (_User.Error == 0)
{
Naam.Text = string.Concat(_User.FirstName, " ", _User.LastName);
AskTask();
}
else
{
MessageBox.Show(_User.ErrorMessage);
Close();
}
}
public void showBadge()
{
CSN.Text = csn;
Site.Text = site;
Code.Text = code;
Dispatcher.Invoke(CheckBadge);
}
bool Msg(int err)
{
//decodeert verschillende USB messages. (callback )
if (StopReadBadge )
{
System.Diagnostics.Debug.WriteLine("msg dropped:");
if (err!=0) System.Diagnostics.Debug.WriteLine($"msg result={a.clr.Result}");
return false;
}
if (err == 0)
{
System.Diagnostics.Debug.WriteLine($"msg result={a.clr.Result}");
if (a.clr.Result.StartsWith("csn:", StringComparison.InvariantCultureIgnoreCase) && a.clr.Result.EndsWith(";"))
{ //GEn 2
csn = string.Empty;
foreach (string ar in a.clr.Result.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
if (ar.StartsWith("csn:")) csn = ar.Substring(4);
if (ar.StartsWith("site:")) site = ar.Substring(5);
if (ar.StartsWith("code:")) code = ar.Substring(5);
}
this.Dispatcher.Invoke(new Action(() => showBadge()));
StopReadBadge = true;
}
else
{
if (a.clr.Result.Equals(";"))
{
if (nextSite) site = string.Empty;
if (nextCode) code = string.Empty;
if (nextFinish || nextCode)
{
this.Dispatcher.Invoke(new Action(() => showBadge()));
StopReadBadge = true;
}
nextCSN = nextSite = nextCode = nextFinish = false;
}
else if (nextCSN) { csn = a.clr.Result; nextCSN = nextFinish = false; }
else if (nextSite) { site = a.clr.Result; nextSite = false; }
else if (nextCode) { code = a.clr.Result; nextCode = false; nextFinish = true; }
else if (a.clr.Result.Equals("csn:")) { nextCSN = true; nextCode = nextSite = false; }
else if (a.clr.Result.Equals("site:")) { nextSite = true; nextCSN = nextCode = false; }
else if (a.clr.Result.Equals("code:")) { nextCode = true; nextCSN = nextSite = false; }
else System.Diagnostics.Debug.WriteLine(a.clr.Result);
}
}
else
System.Diagnostics.Debug.WriteLine($"Error {err}");
return false;
}
private void bClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void BClear_Click(object sender, RoutedEventArgs e)
{
CSN.Text = string.Empty;
Site.Text = string.Empty;
Code.Text = string.Empty;
}
private void TaskOK_Click(object sender, RoutedEventArgs e)
{
bool isOk = false;
switch (TaskOK.Tag)
{
case "A": //No access
break;
case "M": //Margin
break;
case "B": // Badge
break;
case "L": //Locked
break;
case "O": // OK
isOk = true;
break;
}
if (isOk)
{
tbEPD.IsSelected = true;
System.Windows.Data.CollectionViewSource getTaskListViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("getTaskListViewSource")));
var rv = getTaskListViewSource.View.CurrentItem as DataRowView;
CurTaskRow = rv.Row as DataSet1.GetTaskInfoRow;
if (CurTaskRow.MinWorkerDosisMargin > _User.Margin)
{
MessageBox.Show("Margin exeeded");
}
else
{
GetEPD();
}
}
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lb = sender as ListBox;
DataRowView o = null;
if (e.AddedItems.Count > 0)
{
o = e.AddedItems[0] as DataRowView;
}
bool exceeded = false;
if (o != null)
{
exceeded = (((DataSet1.GetTaskInfoRow)o.Row).MarginExceeded);
}
if (_User == null) { TaskOK.Content = "Badge"; TaskOK.Tag = "B"; }
else if (_User.AccessAllow != true) { TaskOK.Content = "No Access"; TaskOK.Tag = "A"; }
else if (_User.VisitIDLocked == true) { TaskOK.Content = "Locked"; TaskOK.Tag = "L"; }
else if (exceeded == true) { TaskOK.Content = "No Margin"; TaskOK.Tag = "M"; }
else
{
TaskOK.Content = "OK"; TaskOK.Tag = "O";
}
}
private void CbInst1_Click(object sender, RoutedEventArgs e)
{
Inst1 = (cbInst1.IsChecked == true);
cbInst2.IsChecked = !Inst1;
bool isAlara = (cbAlara.IsChecked == true);
SetFilter(isAlara);
}
private void CbAlara_Click(object sender, RoutedEventArgs e)
{
if (!initialising)
{
bool isAlara = true;
cbAlara.IsChecked = true;
cbNormal.IsChecked = !isAlara;
SetFilter(isAlara);
}
}
private void CbNormal_Click(object sender, RoutedEventArgs e)
{
if (!initialising)
{
bool isAlara = false;
cbAlara.IsChecked = isAlara;
cbNormal.IsChecked = !isAlara;
SetFilter(isAlara);
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
t = null;
a = null;
//t.Abort();
}
private void CbInst2_Click(object sender, RoutedEventArgs e)
{
Inst2 = (cbInst2.IsChecked == true);
cbInst1.IsChecked = !Inst2;
bool isAlara = (cbAlara.IsChecked == true);
SetFilter(isAlara);
}
private void Starter()
{
try
{
if (a == null)
{
a = new OkReadBadge.Badging(Msg);
StopReadBadge = false;
a.Start();
}
} catch (System.Exception ex)
{
MessageBox.Show($"Badgereader error {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
EpdFound = false;
initialising = true;
tbEntry.IsSelected = true;
bEpdDone.Visibility = Visibility.Hidden;
cbAlara.IsChecked = false;
cbInst1.Content = thisStation.Inst_Name1;
Inst1 = true;
cbInst1.IsChecked = Inst1;
if (thisStation.Inst_CODE2 > 0)
{
cbInst2.Content = thisStation.Inst_Name2;
cbInst2.Visibility = Visibility.Visible;
Inst2 = false;
cbInst2.IsChecked = Inst2;
}
string TestCSN = thisStation.TestCSN;
initialising = false;
if (string.IsNullOrWhiteSpace(TestCSN))
{
t = new System.Threading.Thread(() => Starter());
t.Start();
}
else
{
////Debug
_User = new User(TestCSN, string.Empty);
if (_User.Error > 0)
{
MessageBox.Show(_User.ErrorMessage, "DB Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
} else {
Dispatcher.InvokeAsync(AskTask);
}
}
}
}
}