Files
DE70/.svn/pristine/1a/1a952fdc7e6ef952bd6f7ffe7ab99a296a959270.svn-base
2024-09-02 10:03:23 +07:00

1297 lines
47 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Channels;
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.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Windows.Threading;
using System.Diagnostics;
using System.Collections.ObjectModel;
using SafeBagDE70.Model;
using HandyControl.Tools;
using System.Drawing.Printing;
using System.Drawing;
using static HandyControl.Tools.Interop.InteropValues;
using SafeBagDE70.API;
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using DE70_CMDv2;
using System.Configuration;
using Serilog;
using System.ComponentModel;
using SafeBagDE70.StateMachine;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Runtime.Intrinsics.Arm;
using SafeBagDE70.Services;
using System.Windows.Forms;
using Button = System.Windows.Controls.Button;
using UserControl = System.Windows.Controls.UserControl;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control;
namespace SafeBagDE70
{ //Summary
public partial class DepositSMUCv2 : UserControl
{
SerialPort serialPortDE70 = new SerialPort();
SerialPort serialPortSensor= new SerialPort();
List<CountSession> sessions = new List<CountSession>();
public ObservableCollection<LvCountItem> lvCountItems = new ObservableCollection<LvCountItem>();
private DispatcherTimer sensorTimer;
DispatcherTimer? warningTimer = null;
DispatcherTimer? endsessionTimer = null;
DispatcherTimer? backToWelcomeTimer = null;
//DispatcherTimer?
private volatile bool bIsCounted = false;
int countTimer = 0;
CCmdRsp cCmdRsp = null;
bool bInitialized = false;
bool bOpenPort = true;
bool bIsMaintain = false;
MainWindow mainWindow = null;
Thread thread = null;
Task taskCounting = null;
public DepositSMUCv2()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
mainWindow=((MainWindow)Application.Current.MainWindow);
InitTransaction();
StopTimers();
}
public void InitTransaction()
{
try
{
lblName.Content= "Vietinbank " + Environment.MachineName;
if (AppData.IsAuthenticated)
{
lblUser.Content = $"Xin chào khách hàng: {AppData.CurrentCustomerAccount.Name} (Gian hàng {AppData.CurrentCustomerAccount.CustomerId})";
}
cCmdRsp = new CCmdRsp(serialPortDE70);
AppData.cCmdRsp = cCmdRsp;
// LoginUsingQRCode();
InitSerialPorts();
//Subscrible to the DataReveied event\
bInitialized = InitSession();
InitTimers();
AppData.IsInTransaction = true;
ResetButtons();
btnStart.IsEnabled = true;
ResetTimers();
var state=GetCDMState();
if(state.SR2==0x1C)
{
StopTimers();
ShowError(state.CDMStatus);
}
}
catch (Exception ex)
{
ShowError("LỖI CỔNG KẾT NỐI, VUI LÒNG KIỂM TRA LẠI CỔNG KẾT NỐI");
}
}
private void InitSerialPorts()
{
InitSerialPortDE70();
InitSerialPortSensor();
}
private void InitSerialPortSensor()
{
if (serialPortSensor.IsOpen) serialPortSensor.Close();
serialPortSensor.PortName = AppData.SensorPort;
serialPortSensor.BaudRate = 9600;
serialPortSensor.Parity = Parity.None;
serialPortSensor.DataBits = 8;
serialPortSensor.StopBits = StopBits.One;
serialPortSensor.ReadTimeout = 1000;
serialPortSensor.WriteTimeout = 1000;
if(bOpenPort)
{
if (!serialPortSensor.IsOpen)
serialPortSensor.Open();
}
}
private void InitSerialPortDE70()
{
if (serialPortDE70.IsOpen) serialPortDE70.Close();
serialPortDE70.ReadBufferSize = 4096;
serialPortDE70.WriteBufferSize = 4096;
serialPortDE70.PortName = AppData.CDMPort;
serialPortDE70.BaudRate = 9600;
serialPortDE70.Parity = Parity.Even;
serialPortDE70.DataBits = 8;
serialPortDE70.StopBits = StopBits.One;
serialPortDE70.ReadTimeout = 10000;
serialPortDE70.WriteTimeout = 10000;
if(bOpenPort)
{
if (!serialPortDE70.IsOpen)
serialPortDE70.Open();
}
}
private void InitTimers()
{
warningTimer = new DispatcherTimer(); // 30 seconds
warningTimer.Interval = new TimeSpan(0, 0, 15);
warningTimer.Tick += WarningTimer_Tick; ;
warningTimer.Stop();
endsessionTimer = new DispatcherTimer(); // 60 seconds
endsessionTimer.Interval = new TimeSpan(0, 0, 30);
endsessionTimer.Tick += EndSessionTimer_Tick;
endsessionTimer.Stop();
backToWelcomeTimer = new DispatcherTimer();
backToWelcomeTimer.Interval = new TimeSpan(0, 0, 15);
backToWelcomeTimer.Tick += BackToWelcomeTimer_Tick;
backToWelcomeTimer.Stop();
sensorTimer = new DispatcherTimer();
sensorTimer.Interval = new TimeSpan(0, 0, 1); //1 seconds
sensorTimer.Tick += SensorTimer_Tick;
sensorTimer.Start();
}
public void StopWelcomeTimer()
{
if(backToWelcomeTimer != null)
backToWelcomeTimer.Stop();
}
public void StartWelcomeTimer()
{
if (backToWelcomeTimer != null)
backToWelcomeTimer.Start();
}
private void BackToWelcomeTimer_Tick(object? sender, EventArgs e)
{
mainWindow.ShowWelcomeScreen();
}
private void EndSessionTimer_Tick(object? sender, EventArgs e)
{
Log.Information("7. Timout starting");
mainWindow.ShowEndSessionScreen();
StopTimers();
DoStoreStart();
bIsCounted = false;
CountStart(false);
Task.Run(() =>
{
taskCounting.Wait();
DoStoreStart();
Dispatcher.Invoke(() =>
{
EndSession();
});
Log.Information("7. Timout completed");
});
}
private void BackToWelcomeScreen()
{
StopTimers();
var cdmState = GetCDMState();
var state = GetCDMState();
if (state.IsNotesInEscrow > 0)
{
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN TRONG NGĂN PHỤ HOẶC ẤN LƯU TRỮ ĐỂ TIẾP TỤC", false, true);
}
else if (state.IsNotesInRejectPocket > 0 || state.IsRejectPocketFull > 0)
{
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN KHÔNG HỢP LỆ ĐỂ TIẾP TỤC", false, true);
}
else if (state.IsNotesInHopper > 0)
{
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN NGĂN NỘP ĐỂ TIẾP TỤC", false, true);
}
else if (state.IsCassetteFull != 0)
{
mainWindow.ShowCollectionScreen();
}
else
{
mainWindow.ShowWelcomeScreen();
}
}
private void WarningTimer_Tick(object? sender, EventArgs e)
{
mainWindow.ShowTimeoutScreen();
}
public void EndTransaction()
{
try
{
AppData.IsInTransaction = false;
if (sensorTimer != null)
{
sensorTimer.Tick -= SensorTimer_Tick;
sensorTimer.Stop();
}
if (warningTimer != null)
{
warningTimer.Tick -=WarningTimer_Tick;
warningTimer.Stop();
}
if (endsessionTimer != null)
{
endsessionTimer.Tick -=EndSessionTimer_Tick;
endsessionTimer.Stop();
}
}
catch (Exception ex)
{
}
}
public void InitLvCountItems()
{
if (lvCount.Items.Count == 11) {
foreach(var item in lvCountItems)
{
item.Quantity = "0";
}
}
else
{
lvCountItems = new ObservableCollection<LvCountItem>()
{
new LvCountItem{Name = "500",Quantity = "0"},
new LvCountItem{Name = "1000",Quantity = "0"},
new LvCountItem{Name = "2000",Quantity = "0"},
new LvCountItem{Name = "5000",Quantity = "0"},
new LvCountItem{Name = "10000",Quantity = "0"},
new LvCountItem{Name = "20000",Quantity = "0"},
new LvCountItem{Name = "50000",Quantity = "0"},
new LvCountItem{Name = "100000",Quantity = "0"},
new LvCountItem{Name = "200000",Quantity = "0"},
new LvCountItem{Name = "500000",Quantity = "0"},
new LvCountItem{Name = "Tổng",Quantity = "0",Value="0"},
};
}
lvCount.ItemsSource = lvCountItems;
//lvCountItems[0].Quantity = "10";
}
public long maxQuantitySheet = 10000;
public int quantityStored = 0;
public int totalAllDenominations = 0;
public int posDenomi;
public bool printerFlag = false;
public static string? recvDataString;
// public int[] TempQuantity = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// public int[] TempNote = new int[5] { 0, 0, 0, 0, 0 };
// public int[] Notes = new int[5] { 0, 0, 0, 0, 0 };
public int countSensorTimer = 0;
//PrinterManager printerManager = new PrinterManager();
private void SensorTimer_Tick(object? sender, EventArgs e)
{
try
{
countSensorTimer++;
if (countSensorTimer % 10 == 0)
{
APIModels.SendHealthCheck();
}
sensorTimer.IsEnabled = false;
GetSensorValue();
sensorTimer.IsEnabled = true;
}
catch (Exception ex)
{
Log.Fatal(ex, "Sensor_Timer");
}
}
Stopwatch watch = new Stopwatch();
private void GetSensorValue()
{
try
{
int state = 0;
if (serialPortSensor.IsOpen == true)
{
//serialPortSensor.DiscardInBuffer();
//serialPortSensor.DiscardOutBuffer();
serialPortSensor.Write(new byte[] { (byte)'S', 13, 10 }, 0, 3);
var buffer = new byte[16];
int n = serialPortSensor.Read(buffer, 0, buffer.Length);
for (int i = 1; i < n - 1; i++)
{
if ((buffer[i] == 13) && (buffer[i + 1] == 10))
{
state = buffer[i - 1] - 48;
if (bInitialized)
{
if (state != 0)
gridMain.IsEnabled = false;
else
gridMain.IsEnabled = true;
}
switch (state)
{
case 0:
lblStatus.Content = "NGĂN KÉT: ĐÓNG, NGĂN TÚI: ĐÓNG";
break;
case 1:
lblStatus.Content = "NGĂN KÉT: ĐÓNG, NGĂN TÚI: MỞ";
break;
case 2:
lblStatus.Content = "NGĂN KÉT: MỞ, NGĂN TÚI: ĐÓNG";
break;
case 3:
lblStatus.Content = "NGĂN KÉT: MỞ, NGĂN TÚI: MỞ";
break;
}
if (state > 0)
{
if (!bIsMaintain)
{
bIsMaintain = true;
DoMaintaince();
}
// TransitionToState("Maintainance");
}
else
{
if(bIsMaintain)
{
bIsMaintain = false;
mainWindow.ShowWelcomeScreen();
}
}
break;
}
}
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Debug.WriteLine(elapsedMs);
watch.Start();
}
}
catch (Exception ex)
{
Log.Fatal(ex,"GetSensorValue");
if(serialPortSensor.IsOpen)
serialPortSensor.Close();
serialPortSensor.Open();
serialPortSensor.DiscardInBuffer();
serialPortSensor.DiscardOutBuffer();
}
}
private void DoMaintaince()
{
StopTimers();
((MainWindow)Application.Current.MainWindow).ShowMaintainanceScreen();
}
public int UpdateTotalQuantity()
{
int total = 0;
foreach (var item in lvCountItems)
{
if (item.Name != "Tổng")
{
int quantity = 0;
if (int.TryParse(item.Quantity, out quantity))
{
total += quantity;
}
}
}
return total;
}
public int AutoSerialGet()
{
try
{
int ret;
ushort seri_num = 0;
ushort get_num = 0;
bool endflg = false;
var countSession = new CountSession();
countSession.IsStored = false;
sessions.Add(countSession);
while (true)
{
AppData.WDTCount = 0;
ret = cCmdRsp.SendSerialSense();
if (ret == Constants.E_OK)
{
ret = cCmdRsp.GetSerialNum(out seri_num);
if (ret == Constants.E_OK)
{
// Serial number acquisition
if (cCmdRsp.ChkCntEnd())
{
endflg = true;
//Debug.WriteLine("END FLAG: "+cCmdRsp.recv_dat.dat[1]);
}
//Debug.WriteLine("START");
//Debug.WriteLine($"Num Serial: {seri_num}");
//Log.Information($"Num Serial: {seri_num}");
while (get_num < seri_num)
{
AppData.WDTCount = 0;
get_num++;
Log.Information($"Serial Idx: {get_num}/{seri_num}");
ret = cCmdRsp.SendGetSerial(get_num);
Log.Information($"Serial Status: {cCmdRsp.recv_dat.dat[18]}");
switch (cCmdRsp.recv_dat.dat[18])
{
case 0x00: countSession.TempNotes[0]++;break;
case 0x02:
//TempNote[1] ++;
Log.Information($"Serial Value: {cCmdRsp.recv_dat.dat[17]}");
switch (cCmdRsp.recv_dat.dat[17])
{
case 0x00: countSession.QuantityDenomi[0]++; break;
case 0x01: countSession.QuantityDenomi[1]++; break;
case 0x02: countSession.QuantityDenomi[2]++; break;
case 0x03: countSession.QuantityDenomi[3]++; break;
case 0x04: countSession.QuantityDenomi[4]++; break;
case 0x05: countSession.QuantityDenomi[5]++; break;
case 0x06: countSession.QuantityDenomi[6]++; break;
case 0x07: countSession.QuantityDenomi[7]++; break;
case 0x08: countSession.QuantityDenomi[8]++; break;
case 0x09: countSession.QuantityDenomi[9]++; break;
}
break;
case 0x03: countSession.TempNotes[2]++; break;
case 0x0b: countSession.TempNotes[3]++; break;
case 0x0c: countSession.TempNotes[4]++; break;
}
UpdateListViewCountItems();
}
//Debug.WriteLine("Ellapsed Time: " + timeSpan.TotalSeconds);
if (endflg)
{
break;
}
}
else
{
break;
}
}
else
{
break;
}
}
if (countSession.QuantityDenomi.Sum() == 0 && countSession.TempNotes.Sum() == 0)
{
sessions.Remove(countSession);
}
}catch(Exception ex)
{
Log.Fatal(ex, "AutoSerialGet");
}
//Log.Information(JsonConvert.SerializeObject(sessions));
return 0;
}
private string GetShortInforFromCountItems()
{
return JsonConvert.SerializeObject(lvCountItems);
}
private bool CountStart(bool bInitState=true)
{
bool bSuccess = true;
printerFlag = false;
string currentMethod = MethodBase.GetCurrentMethod().Name;
try
{
if(bOpenPort)
{
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_CNTSTART);
if (cCmdRsp.recv_dat != null && cCmdRsp.recv_dat.dat[2] != 43) { }
do
{
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
cCmdRsp.SendXXCmd((byte)Command.CMD_SENSE);
}
while (cCmdRsp.recv_dat != null && cCmdRsp.recv_dat.dat[1] == 0x09); //SR2 = 0x09 : Counting Status
}
//SR3 = 0x43 : Stacker full
//thread = new Thread(new ThreadStart(() =>
//{
// Log.Information("Before AutoCount: " + GetShortInforFromCountItems());
// bIsCounted = false;
// AutoSerialGet();
// bIsCounted = true;
// Log.Information("After AutoCount: " + GetShortInforFromCountItems());
// if (bInitState)
// {
// Dispatcher.Invoke(() =>
// {
// btnCountStart.IsEnabled = true;
// btnCheck.IsEnabled = true;
// btnStoreStart.IsEnabled = true;
// ResetTimers();
// Log.Information("2. Count completed");
// });
// }
//}));
//thread.Start();
taskCounting = Task.Run(() =>
{
Log.Information("Before AutoCount: " + GetShortInforFromCountItems());
bIsCounted = false;
AutoSerialGet();
bIsCounted = true;
Log.Information("After AutoCount: " + GetShortInforFromCountItems());
if (bInitState)
{
Dispatcher.Invoke(() =>
{
btnCountStart.IsEnabled = true;
btnCheck.IsEnabled = true;
btnStoreStart.IsEnabled = true;
ResetTimers();
Log.Information("2. Count completed");
});
}
});
}
catch (Exception ex)
{
bSuccess = false;
Log.Fatal(ex,currentMethod);
}
return bSuccess;
}
private void UpdateListViewCountItems()
{
Dispatcher.Invoke(DispatcherPriority.Normal, () =>
{
double sum = 0;
if (sessions != null && sessions.Count > 0)
{
for (int i = 0; i < 10; i++)
{
int quantity = 0;
for (int j = 0; j < sessions.Count; j++)
{
quantity += sessions[j].QuantityDenomi[9 - i];
}
lvCountItems[i].Quantity = quantity.ToString();
sum += lvCountItems[i].getValue();
}
//Update amount of money
lvCountItems[10].Quantity = UpdateTotalQuantity().ToString();
lvCountItems[10].Value = sum.ToString();
}
else
{
for (int i = 0; i < 11; i++)
{
lvCountItems[i].Quantity = "0";
lvCountItems[i].Value = "0";
}
}
});
}
private bool DoCheck()
{
bool bSuccess = true;
string currentMethod = MethodBase.GetCurrentMethod().Name;
try
{
Log.Information("Before Check: "+GetShortInforFromCountItems());
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_CANCEL);
Log.Information(JsonConvert.SerializeObject(sessions));
sessions.RemoveAll(item=>!item.IsStored);
Log.Information(JsonConvert.SerializeObject(sessions));
UpdateListViewCountItems();
Log.Information("After Check: "+GetShortInforFromCountItems());
}
catch (Exception ex)
{
bSuccess = false;
Log.Fatal(ex, currentMethod);
}
return bSuccess;
}
private bool WaitUntilCountReady(int nSecondTimeout)
{
CDMSTATE state = null;
int nTimes = 0;
bool bSuccess = false;
while (true)
{
state = GetCDMState();
if (state.SR2 == 0x07)
{
bSuccess = true;
break;
}
nTimes++;
Thread.Sleep(1000);
if ((nSecondTimeout > 0) && nTimes >= nSecondTimeout)
break;
}
return bSuccess;
}
private bool WaitUntilEnscrowEmpty(int nSecondTimeout)
{
CDMSTATE state = null;
int nTimes = 0;
bool bSuccess = false;
while(true)
{
state = GetCDMState();
if (state.IsNotesInEscrow == 0)
{
bSuccess = true;
break;
}
nTimes++;
Thread.Sleep(1000);
if ((nSecondTimeout > 0) && nTimes >= nSecondTimeout)
break;
}
return bSuccess;
}
private bool DoStoreStart()
{
bool bSuccess = true;
printerFlag = true;
string currentMethod = MethodBase.GetCurrentMethod().Name;
try
{
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
//sessions.Add(new CountSession());
// sessions[sessions.Count - 1].QuantityDenomi = (int[])TempQuantity.Clone();
//Debug.WriteLine(quantityStored);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_STORESTART);
bSuccess=WaitUntilCountReady(10);
if (bSuccess)
{
foreach (var item in sessions)
{
item.IsStored = true;
}
quantityStored = UpdateTotalQuantity();
Log.Information("Stored " + GetShortInforFromCountItems());
}
else
{
Log.Error("Store FAIL " + GetShortInforFromCountItems());
}
Thread.Sleep(2000);
}
catch (Exception ex)
{
bSuccess = false;
Log.Fatal(ex, currentMethod);
}
return bSuccess;
}
private bool DoStart()
{
bool bSuccess = true;
InitSession();
string currentMethod = MethodBase.GetCurrentMethod().Name;
AppData.IsInTransaction = true;
try
{
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_SENSE);
var recvData = cCmdRsp.recv_dat;
if (recvData.dat[14] == recvData.dat[16])
{
cCmdRsp.SendLock();
}
else
{
cCmdRsp.SendEventFix(recvData.dat[16]);
cCmdRsp.SendLock();
}
if (cCmdRsp.recv_dat.dat[0]!=0x40)
{
bSuccess = false;
}
}
catch (Exception ex)
{
bSuccess = false;
Log.Fatal(ex, currentMethod);
}
return bSuccess;
}
private bool DoEnd()
{
bool bSuccess = false;
var state = GetCDMState();
StopTimers();
Log.Information("CDM State: " + JsonConvert.SerializeObject(state));
var lstEnabledControls = new List<Control> { btnEnd, btnCountStart };
if (state.IsNotesInEscrow > 0)
{
btnEnd.IsEnabled = true;
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN TRONG NGĂN PHỤ HOẶC ẤN LƯU TRỮ ĐỂ TIẾP TỤC", true,true);
}
else if (state.IsNotesInRejectPocket > 0 || state.IsRejectPocketFull > 0)
{
btnEnd.IsEnabled = true;
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN KHÔNG HỢP LỆ ĐỂ TIẾP TỤC", true, true);
}
else if(state.IsNotesInHopper>0)
{
btnEnd.IsEnabled = true;
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN NGĂN NỘP ĐỂ TIẾP TỤC", true, true);
}
else
{
bSuccess = true;
AppData.WDTCount = 0;
EndSession();
btnStart.IsEnabled = true;
btnLogout.IsEnabled = true;
ResetTimers();
}
return bSuccess;
}
private bool DoLogout()
{
bool bSuccess = false;
var state = GetCDMState();
Log.Information("CDM State: " + JsonConvert.SerializeObject(state));
InitLvCountItems();
if (state.IsNotesInEscrow > 0)
{
mainWindow.ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN TRONG NGĂN PHỤ HOẶC ẤN LƯU TRỮ ĐỂ TIẾP TỤC", true);
}
else if (state.IsNotesInRejectPocket > 0 || state.IsRejectPocketFull > 0)
{
mainWindow.ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN KHÔNG HỢP LỆ ĐỂ TIẾP TỤC", true);
}
else //if (double.Parse(lvCountItems[10].Value)== 0)
{
bSuccess = true;
EndSession();
BackToWelcomeScreen();
}
return bSuccess;
}
private void EndSession()
{
string currentMethod = MethodBase.GetCurrentMethod().Name;
//Remove sessions with store flag is false
sessions.RemoveAll(item => !item.IsStored);
UpdateListViewCountItems();
var totalQuantity = lvCountItems[10].Quantity;
var totalValue = lvCountItems[10].Value;
try
{
Log.Information(currentMethod);
serialPortDE70.DiscardInBuffer();
serialPortDE70.DiscardOutBuffer();
if (double.Parse(totalValue) > 0)
{
var data2Send = new CountingData2Send
{
TransactionCode = AppData.CurrentCustomerAccount.TransactionCode,
Sessions = sessions,
MachineId = Environment.MachineName,
UserId = AppData.CurrentCustomerAccount.CustomerId,
MachineTime = DateTime.Now.ToString("yyyyMMddHHmmss"),
//RejectNotes = Notes[0],
GenuineNotes = quantityStored,
//UnfitNotes = Notes[2],
//CounterfeitNotes = Notes[3],
// SuspendNotes = Notes[4]
};
SyncData(data2Send);
//Reset all and ready to next session
if (printerFlag == true)
{
if (double.Parse(totalValue) > 0)
PrintReceipt();
printerFlag = false;
}
String strNotify = $"BẠN VỪA THỰC HIỆN TIỀN VÀO MÁY CDM:\r\n"
+ $"SỐ LƯỢNG TỜ: {totalQuantity}\r\n"
+ $"SỐ TIỀN: {totalValue} VND\r\n";
mainWindow.ShowInformationScreen(strNotify, false, false);
ResetSessions();
StartWelcomeTimer();
}
else
{
mainWindow.ShowWelcomeScreen();
}
//SendData2Server
}
catch (Exception ex)
{
Log.Fatal(ex, currentMethod);
}
}
private async Task SyncData(CountingData2Send data2Send)
{
Log.Information("Saving data to DB: " + JsonConvert.SerializeObject(data2Send));
var history = Save2DB(data2Send);
Log.Information("Sending data to server: "+JsonConvert.SerializeObject(data2Send));
bool bSuccess = await APIModels.SendCountingData(data2Send);
if (bSuccess)
{
history.IsSent = true;
Update2DB(history);
Log.Information("Sending data to server successfully");
}
else
{
Log.Information("Sending data to server failed");
}
}
public History Save2DB(CountingData2Send data2Send)
{
var data = JsonConvert.SerializeObject(data2Send);
var history = new History
{
Data = data,
DepositTime = DateTime.Now,
IsSent = false,
UserCode = AppData.CurrentCustomerAccount.CustomerId
};
try
{
using (var db = new CDMDbContext())
{
db.Histories.Add(history);
db.SaveChanges();
}
}
catch (Exception ex)
{
Log.Fatal(ex,"Save2DB Exception");
}
return history;
}
public History Update2DB(History history)
{
try
{
using (var db = new CDMDbContext())
{
db.Histories.Update(history);
db.SaveChanges();
}
}
catch (Exception ex)
{
Log.Fatal(ex,"Update2DB Exception");
}
return history;
}
private void ShowError(string strError)
{
//gridMain.IsEnabled = false;
//btnCollect2.Visibility=Visibility.Collapsed;
//panelError.Visibility = Visibility.Visible;
//lblError.Text = strError;
//((MainWindow)Application.Current.MainWindow).ShowWelcomeScreen();
((MainWindow)Application.Current.MainWindow).ShowErrorScreen(strError);
}
private CDMSTATE GetCDMState()
{
cCmdRsp.SendDeSense();
string strSend = cCmdRsp.GetSendDat(false);
string strRecv=cCmdRsp.GetRecvDat();
Log.Information($"[SEND] {strSend}");
Log.Information($"[RECV] {strRecv}");
var data = cCmdRsp.recv_dat.dat;
AppData.CDMSTATE.IsCassetteFull = data[17] & 0x08;
AppData.CDMSTATE.IsNotesInEscrow = data[4] & 0x01;
AppData.CDMSTATE.IsRejectPocketFull = data[3] & 0x40;
AppData.CDMSTATE.IsNotesInRejectPocket = data[3] & 0x10;
AppData.CDMSTATE.IsNotesInHopper = data[3] & 0x01;
AppData.CDMSTATE.SR1 = data[0];
AppData.CDMSTATE.SR2 = data[1];
AppData.CDMSTATE.SR3 = data[2];
return AppData.CDMSTATE;
}
private bool InitSession()
{
try
{
//Check status
var cdmState = GetCDMState();
if (cdmState.IsCassetteFull != 0)
{
((MainWindow)Application.Current.MainWindow).ShowCollectionScreen();
return false;
}
else if (cdmState.IsNotesInEscrow != 0)
{
((MainWindow)Application.Current.MainWindow).ShowInformationScreen("ĐỀ NGHỊ BỎ TIỀN TRONG NGĂN PHỤ ĐỂ TIẾP TỤC");
}
else
{
ResetSessions();
return true;
}
}
catch(Exception ex)
{
Log.Fatal(ex,"InitSession");
}
return false;
}
private void ResetSessions()
{
InitLvCountItems();
sessions.Clear();
quantityStored = 0;
maxQuantitySheet = 0;
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_UNLOCK);
}
private void PrintReceipt()
{
Log.Information("PrintReceipt");
var shopName = AppData.CurrentCustomerAccount.Name;
var shopCode = AppData.CurrentCustomerAccount.CustomerId;
var tradingCode = AppData.CurrentCustomerAccount.TransactionCode;// "TRA04052024CDM";
var storeAccountNumber = 686868686688;
var currentTime = DateTime.Now;
var printDocument = new PrintDocument();
System.Drawing.Image LogoAeonMall = System.Drawing.Image.FromFile("Logo\\logoAeonmall.png");
System.Drawing.Image LogoVietin = System.Drawing.Image.FromFile("Logo\\logoVietin.png");
printDocument.PrinterSettings.PrinterName = "CUSTOM K80";
printDocument.PrintPage += (sender, e) =>
{
int fontSize = 8;
int lineSpace = 20;
float marginLeft = 25;
float marginTop = 5;
float width = 275;
float height = 280;
float LogoAeonWidth = 90; // Chiều rộng của LogoAeon
float LogoAeonHeight = 30; // Chiều cao của LogoAeon
float LogoAeonX = marginLeft + 25; // Đặt LogoAeon ở giữa theo chiều ngang
float LogoAeonY = marginTop + 10; // Khoảng cách từ mép trên của viền đến LogoAeon
float LogoVietinWidth = 90; // Chiều rộng của LogoVietin
float LogoVietinHeight = 30; // Chiều cao của LogoVietin
float LogoVietinX = marginLeft + 165; // Đặt LogoVietin ở giữa theo chiều ngang
float LogoVietinY = marginTop + 10; // Khoảng cách từ mép trên của viền đến LogoVietin
e.Graphics.DrawImage(LogoVietin, LogoVietinX, LogoVietinY, LogoVietinWidth, LogoVietinHeight);
e.Graphics.DrawImage(LogoAeonMall, LogoAeonX, LogoAeonY, LogoAeonWidth, LogoAeonHeight);
e.Graphics.DrawImage(LogoVietin, LogoVietinX, LogoVietinY, LogoVietinWidth, LogoVietinHeight);
e.Graphics.DrawImage(LogoAeonMall, LogoAeonX, LogoAeonY, LogoAeonWidth, LogoAeonHeight);
e.Graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Black, 1), marginLeft, marginTop, width, height);
e.Graphics.DrawString("Biên nhận nộp tiền", new Font("Arial", 12, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, new PointF(80, 50));
e.Graphics.DrawString("Mã máy CDM: "+Environment.MachineName, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 1 + 65));
e.Graphics.DrawString("Thời gian giao dịch: " + currentTime.ToString("dd/MM/yyyy HH:mm:ss"), new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 2 + 65));
e.Graphics.DrawString("Mã giao dịch: " + tradingCode, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 3 + 65));
e.Graphics.DrawString("Mã Gian hàng: " + shopCode, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 4 + 65));
e.Graphics.DrawString("Tên Gian hàng: " + shopName, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 5 + 65));
e.Graphics.DrawString("Số tài khoản gian hàng: " + storeAccountNumber, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 6 + 65));
e.Graphics.DrawString("Nội dung giao dịch: Nop doanh so tien mat_", new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 7 + 65));
e.Graphics.DrawString(shopCode, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 8 + 65));
e.Graphics.DrawString("Số tiền nộp: " + lvCountItems[10].Value, new Font("Arial", fontSize), System.Drawing.Brushes.Black, new PointF(50, lineSpace * 9 + 65));
e.Graphics.DrawString("*****Xin cám ơn*****", new Font("Arial", 7), System.Drawing.Brushes.Black, new PointF(120, lineSpace * 10 + 65));
// To send raw ESC/POS commands, you'd typically use e.Graphics.DrawString() with the command string,
// or for more complex scenarios, directly manipulate the byte stream sent to the printer.
};
printDocument.Print();
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
EndTransaction();
}
public void ResetTimers()
{
StopTimers();
StartTimers();
}
public void StartTimers()
{
if(warningTimer!=null && endsessionTimer != null)
{
warningTimer.Start();
endsessionTimer.Start();
}
}
public void StopTimers()
{
if(warningTimer!=null && endsessionTimer !=null)
{
warningTimer.Stop();
endsessionTimer.Stop();
}
}
private void ResetButtons()
{
btnStart.IsEnabled = false;
btnCountStart.IsEnabled = false;
btnCheck.IsEnabled = false;
btnStoreStart.IsEnabled = false;
btnEnd.IsEnabled = false;
btnLogout.IsEnabled = false;
}
private Button _previousClickedButton = null;
private void btnCommon_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if (_previousClickedButton != null)
{
_previousClickedButton.BorderBrush = System.Windows.Media.Brushes.Transparent;
_previousClickedButton.BorderThickness = new Thickness(1);
}
// Set the border of the currently clicked button
btn.BorderBrush = System.Windows.Media.Brushes.DarkRed;
btn.BorderThickness = new Thickness(3);
// Update the previously clicked button reference
_previousClickedButton = btn;
SoundManager.StartButtonPressSound();
// Console.Beep(800, 200);
StopTimers();
bool bResult = false;
if (btn==btnStart)
{
Log.Information("Deposit starting");
Log.Information("User: " + AppData.CurrentCustomerAccount.CustomerId);
Log.Information("1. Start starting");
ResetButtons();
bResult = DoStart();
if (bResult)
{
btnCountStart.IsEnabled = true;
ResetTimers();
}
else
{
btnStart.IsEnabled = true;
btnLogout.IsEnabled = true;
}
if (bResult)
Log.Information("1. Start completed");
else
Log.Information("1. Start failed");
}
else if(btn==btnCountStart)
{
Log.Information("2. Count starting");
ResetButtons();
bResult = CountStart();
// if (taskCounting != null) taskCounting.Wait();
//while (!bIsCounted) // chua thoat duoc loop
//{
// Thread.Sleep(100);
//}
//Do in counting thread
}
else if(btn==btnCheck)
{
Log.Information($"3. Check starting");
ResetButtons();
bResult = DoCheck();
btnCountStart.IsEnabled= true;
ResetTimers();
if (bResult)
Log.Information($"3. Check completed");
else
Log.Information($"3. Check failed");
}
else if (btn == btnStoreStart)
{
//TransitionToState("Store");
Log.Information("4. Store starting");
ResetButtons();
bResult = DoStoreStart();
btnEnd.IsEnabled= true;
btnCountStart.IsEnabled = true;
ResetTimers();
if(bResult)
Log.Information("4. Store completed");
else
Log.Information("4. Store failed");
}
else if (btn == btnEnd)
{
// TransitionToState("End");
Log.Information("5. End starting");
ResetButtons();
bResult = DoEnd();
//btnStart.IsEnabled = true;
//btnLogout.IsEnabled = true;
//ResetTimers();
if (bResult)
Log.Information("5. End Completed");
else
Log.Information("5. End failed");
}
else if (btn == btnLogout)
{
Log.Information("6. Exit starting");
bResult = DoLogout();
if (bResult)
Log.Information("6. Exit Completed");
else
Log.Information("6. Exit failed");
}
}
}
}