remote update

This commit is contained in:
Nguyen Dinh Thuan
2024-09-19 06:55:27 +07:00
parent 56ba2b199b
commit a8cd442f91
5 changed files with 252 additions and 90 deletions
+66 -2
View File
@@ -256,6 +256,70 @@ namespace SafeBagDE70.API
return null; return null;
} }
} }
public static async Task<DeviceCommand[]?> GetCommands()
{
try
{
string deviceID = Environment.MachineName;
int tenantId = AppData.TenantId;
string url = AppData.BaseURL + $"GetListCommand?Device={deviceID}&TenantId={tenantId}";
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
using (HttpClient client = new HttpClient(clientHandler))
{
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<DeviceCommand[]>(json);
}
else
{
Log.Error("GetCommands: StatusCode=" + response.StatusCode);
return null;
}
}
}
catch (Exception ex)
{
Log.Fatal(ex, "GetCommands");
}
return null;
}
public static async Task<bool> UpdateDeviceCommand(int commandId, int executed)
{
bool bSuccess = false;
try
{
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
HttpClient httpClient = new HttpClient(clientHandler);
var data = new
{
id = commandId,
executed = executed
};
string strJsonObj = JsonConvert.SerializeObject(data);
var content = new StringContent(strJsonObj, Encoding.UTF8, "application/json");
using HttpResponseMessage response = await httpClient.PostAsync(AppData.BaseURL + $"PostUpdateCommand", content);
if (response.EnsureSuccessStatusCode().StatusCode == HttpStatusCode.OK)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
bSuccess = true;
}
}
catch (Exception ex)
{
}
return bSuccess;
}
private static string GetMACAddress() private static string GetMACAddress()
@@ -273,6 +337,7 @@ namespace SafeBagDE70.API
return MACAdress; return MACAdress;
} }
private static string? AddressBytesToString(byte[] addressBytes) private static string? AddressBytesToString(byte[] addressBytes)
{ {
if (addressBytes != null) if (addressBytes != null)
@@ -285,6 +350,7 @@ namespace SafeBagDE70.API
return null; return null;
} }
} }
public class AeonMallCustomer public class AeonMallCustomer
{ {
@@ -306,6 +372,4 @@ namespace SafeBagDE70.API
public string? Type { get; set; } public string? Type { get; set; }
public string? TimeTag { get; set; } public string? TimeTag { get; set; }
} }
} }
+33 -86
View File
@@ -30,57 +30,31 @@ namespace SafeBagDE70.Admin
private void btnResetCounter_Click(object sender, RoutedEventArgs e) private void btnResetCounter_Click(object sender, RoutedEventArgs e)
{ {
if (AppData.cCmdRsp != null) Log.Information("[ADMIN] reset cdm counter");
bool bResetCDMCounter = AppData.ResetCDMCounter();
if(!bResetCDMCounter)
{ {
Log.Information("[ADMIN] reset counter"); bResetCDMCounter = AppData.ResetCDMCounter();
CCmdRsp cCmdRsp = AppData.cCmdRsp;
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_UNLOCK);
Thread.Sleep(1000);
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
//Event fix
var recvData = cCmdRsp.recv_dat;
if (recvData.dat[14] == recvData.dat[16])
{
cCmdRsp.SendLock();
}
else
{
cCmdRsp.SendEventFix(recvData.dat[16]);
cCmdRsp.SendLock();
}
var currentMode = cCmdRsp.GetLockParam().mode;
cCmdRsp.GetLockParam().mode = Mode.MODE_COLLECTION;
cCmdRsp.SendLock();
Thread.Sleep(10000);
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
recvData = cCmdRsp.recv_dat;
if (recvData.dat[14] != recvData.dat[16])
{
Log.Information("[ADMIN] reset counter success");
lblCounterStatus.Text = "TRẠNG THÁI: KHỞI TẠO BỘ ĐẾM THÀNH CÔNG";
((MainWindow)Application.Current.MainWindow).ShowWelcomeScreen();
Task.Run(() =>
{
Thread.Sleep(5000);
Dispatcher.Invoke(() =>
{
lblCounterStatus.Text = "";
});
});
AppData.GetCassetteData();
}
else
{
Log.Information("[ADMIN] reset counter fail");
}
} }
if (bResetCDMCounter)
{
Log.Information("[ADMIN] reset counter successfully");
lblCounterStatus.Text = "TRẠNG THÁI: KHỞI TẠO BỘ ĐẾM THÀNH CÔNG";
((MainWindow)Application.Current.MainWindow).ShowWelcomeScreen();
Task.Run(() =>
{
Thread.Sleep(5000);
Dispatcher.Invoke(() =>
{
lblCounterStatus.Text = "";
});
});
}
else
{
Log.Information("[ADMIN] reset counter fail");
}
} }
private void btnOpenEscrow_Click(object sender, RoutedEventArgs e) private void btnOpenEscrow_Click(object sender, RoutedEventArgs e)
@@ -101,47 +75,20 @@ namespace SafeBagDE70.Admin
{ {
try try
{ {
var cCmdRsp = AppData.cCmdRsp; Log.Information("[ADMIN] Reset CDM Error");
bool bSuccess = true; if (AppData.ResetCDMError())
if (cCmdRsp != null)
{ {
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_SENSE); Log.Information("[ADMIN] Reset CDM Error successfully");
lblCounterStatus.Text = "Reset lỗi thành công";
var recvData = cCmdRsp.recv_dat; ((MainWindow)Application.Current.MainWindow).ShowWelcomeScreen();
Task.Run(() =>
if (recvData.dat[14] == recvData.dat[16])
{ {
cCmdRsp.SendLock(); Thread.Sleep(2000);
} Dispatcher.Invoke(() =>
else
{
cCmdRsp.SendEventFix(recvData.dat[16]);
}
//if (cCmdRsp.recv_dat.dat[0] != 0x40)
//{
// bSuccess = false;
//}
//if (bSuccess)
//{
//}
Thread.Sleep(1000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_RESET);
Thread.Sleep(2000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_SENSE);
if (cCmdRsp.recv_dat.dat[1] == 0x04 && cCmdRsp.recv_dat.dat[2] == 0x41)
{
lblCounterStatus.Text = "Reset lỗi thành công";
((MainWindow)Application.Current.MainWindow).ShowWelcomeScreen();
Task.Run(() =>
{ {
Thread.Sleep(2000); lblCounterStatus.Text = "";
Dispatcher.Invoke(() =>
{
lblCounterStatus.Text = "";
});
}); });
} });
} }
} }
catch (Exception ex) { catch (Exception ex) {
+128
View File
@@ -39,6 +39,7 @@ namespace SafeBagDE70
public static int TenantId = 0; public static int TenantId = 0;
public static bool IsInternetOk = false; public static bool IsInternetOk = false;
static Task? resendTask = null; static Task? resendTask = null;
static Task? commandTask = null;
public static DeviceStatus? _DeviceStatus = null; public static DeviceStatus? _DeviceStatus = null;
public static DeviceStatus? DeviceStatus public static DeviceStatus? DeviceStatus
{ {
@@ -315,6 +316,133 @@ namespace SafeBagDE70
Log.Error($"Resend could not start IsInTransaction: {AppData.IsInTransaction} IsInternetOk: {AppData.IsInternetOk}"); Log.Error($"Resend could not start IsInTransaction: {AppData.IsInTransaction} IsInternetOk: {AppData.IsInternetOk}");
} }
} }
public static void GetandExecuteCommands()
{
if (IsInternetOk && (commandTask == null || commandTask.IsCompleted))
{
commandTask = Task.Run(async () =>
{
try
{
var commands= await APIModels.GetCommands();
if(commands != null && commands.Count()>0)
{
foreach (var command in commands)
{
bool bResult = false;
switch(command.CommandCode)
{
case 0:
break;
case 1:
Log.Information("Remote Reset CDM Error");
//Restart CDM
bResult=ResetCDMError();
break;
case 2:
Log.Information("Remote Reset CDM Counter");
bResult = ResetCDMCounter();
if (!bResult)
{
bResult = ResetCDMCounter();
}
break;
}
//
await APIModels.UpdateDeviceCommand(command.Id, bResult?1:-1);
}
}
}
catch (Exception ex)
{
Log.Fatal(ex, "GetandExecuteCommands failed");
}
});
}
else
{
var str = "";
if (commandTask != null && !commandTask.IsCompleted)
str += "Previous command state are running; ";
if (!IsInternetOk)
str += "No Internet Connection";
Log.Error("GetandExecuteCommands error: "+str);
}
}
public static bool ResetCDMCounter()
{
bool bResetOk = false;
if (AppData.cCmdRsp != null)
{
Log.Information("[ADMIN] reset counter");
CCmdRsp cCmdRsp = AppData.cCmdRsp;
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_UNLOCK);
Thread.Sleep(1000);
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
//Event fix
var recvData = cCmdRsp.recv_dat;
if (recvData.dat[14] == recvData.dat[16])
{
cCmdRsp.SendLock();
}
else
{
cCmdRsp.SendEventFix(recvData.dat[16]);
cCmdRsp.SendLock();
}
var currentMode = cCmdRsp.GetLockParam().mode;
cCmdRsp.GetLockParam().mode = Mode.MODE_COLLECTION;
cCmdRsp.SendLock();
Thread.Sleep(10000);
cCmdRsp.SendDeSense();
Thread.Sleep(1000);
recvData = cCmdRsp.recv_dat;
if (recvData.dat[14] != recvData.dat[16])
{
AppData.GetCassetteData();
bResetOk = true;
}
}
return bResetOk;
}
public static bool ResetCDMError()
{
var cCmdRsp = AppData.cCmdRsp;
bool bSuccess = false;
if(cCmdRsp!=null && cCmdRsp.recv_dat!=null)
{
if(cCmdRsp.recv_dat.dat[1] == 0x1C && cCmdRsp.recv_dat.dat[2] == 0x4A)
{
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]);
}
Thread.Sleep(1000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_RESET);
Thread.Sleep(2000);
cCmdRsp.SendXXCmd((byte)Command.CMD_DE_SENSE);
if (cCmdRsp.recv_dat.dat[1] == 0x04 && cCmdRsp.recv_dat.dat[2] == 0x41)
{
bSuccess = true;
}
}
}
return bSuccess;
}
} }
public class CustomerAccount public class CustomerAccount
+5 -2
View File
@@ -42,6 +42,8 @@ using UserControl = System.Windows.Controls.UserControl;
using Application = System.Windows.Application; using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control; using Control = System.Windows.Controls.Control;
using static Azure.Core.HttpHeader; using static Azure.Core.HttpHeader;
using System.Net.Http;
using Azure;
namespace SafeBagDE70 namespace SafeBagDE70
@@ -72,13 +74,11 @@ namespace SafeBagDE70
//DispatcherTimer? //DispatcherTimer?
private volatile bool bIsCounted = false; private volatile bool bIsCounted = false;
int countTimer = 0;
CCmdRsp cCmdRsp = null; CCmdRsp cCmdRsp = null;
bool bInitialized = false; bool bInitialized = false;
bool bOpenPort = true; bool bOpenPort = true;
bool bIsMaintain = false; bool bIsMaintain = false;
MainWindow mainWindow = null; MainWindow mainWindow = null;
Thread thread = null;
Task taskCounting = null; Task taskCounting = null;
public DepositSMUCv2() public DepositSMUCv2()
{ {
@@ -396,6 +396,7 @@ namespace SafeBagDE70
{ {
GetPrinterStatus(); GetPrinterStatus();
GetCDMStatus(); GetCDMStatus();
AppData.GetandExecuteCommands();
AppData.DeviceStatus.Description = AppData.CDMSTATE.CDMStatus; AppData.DeviceStatus.Description = AppData.CDMSTATE.CDMStatus;
AppData.DeviceStatus.DeviceTime = DateTime.Now; AppData.DeviceStatus.DeviceTime = DateTime.Now;
@@ -484,6 +485,7 @@ namespace SafeBagDE70
} }
if (AppData.DeviceStatus.PrinterStatus != prevPrinterStatus) if (AppData.DeviceStatus.PrinterStatus != prevPrinterStatus)
{ {
Log.Information(strPrinterMessage);
AppData.DeviceStatus.Description = strPrinterMessage; AppData.DeviceStatus.Description = strPrinterMessage;
APIModels.SendEvent(AppData.DeviceStatus); APIModels.SendEvent(AppData.DeviceStatus);
prevPrinterStatus = AppData.DeviceStatus.PrinterStatus; prevPrinterStatus = AppData.DeviceStatus.PrinterStatus;
@@ -1288,6 +1290,7 @@ namespace SafeBagDE70
} }
} }
public History Save2DB(CountingData2Send data2Send) public History Save2DB(CountingData2Send data2Send)
{ {
var data = JsonConvert.SerializeObject(data2Send); var data = JsonConvert.SerializeObject(data2Send);
+20
View File
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SafeBagDE70.Model
{
public class DeviceCommand
{
public int Id { get; set; }
public int TenantId { get; set; }
public string Device { get; set; }
public string CreationTime { get; set; }
public bool IsDeleted { get; set; }
public int CommandCode { get; set; }
public int Executed { get; set; }
public string LastUpdate { get; set; }
}
}