using AwInitilizer.Model;
using AwInitilizer.Procedure;
using MesAdaptor;
using Microsoft.Win32;
using Newtonsoft.Json;
using PhihongEv.Lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
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.Threading;
namespace AwInitilizer
{
enum UpdateStatus
{
Idle,
Updating,
Sucess,
Fail
}
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window, Interface.IIogger
{
private DispatcherTimer LogoutTimer;
private KeyinListener keyinListener;
private bool IsInitilizing = false;
private SystemID inputSystemID;
private string csuFilePath;
private string mcuFilePath;
private MainViewModel ViewModel => DataContext as MainViewModel;
private UpdateStatus _UpdateStatus = UpdateStatus.Idle;
private UpdateStatus UpdateStatus
{
get => _UpdateStatus;
set => SetUpdateStatus(value);
}
public MainWindow()
{
InitializeComponent();
ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
System.Net.ServicePointManager.Expect100Continue = false;
LogoutTimer = new DispatcherTimer();
LogoutTimer.Interval = TimeSpan.FromMinutes(1);
LogoutTimer.Tick += LogoutTimer_Tick;
keyinListener = new KeyinListener(this);
keyinListener.OnSystemIDReseived += KeyinListener_OnSystemIDReseived;
Loaded += MainWindow_Loaded;
this.DataContext = new MainViewModel();
//var test = SystemID.TryParse("AWLU770001W1P0D2045A001A0",out var systemID);
//this.DataContext = new MainViewModel()
//{
// SystemID = systemID,
// ModelName = "AWLU770001W1P0",
// SettingModelName = "AWLU770001W1P0",
// SerialNumber = "D2045A001A0",
// //FourGenModuleVersion = "EC25AFFAR07A08M4G",
// IsSimInsert = false,
// //ICCID = "12345678901234567890",
// //IMSI = "123456789012345",
// //CSUVersion = "V1.01.01.0601.00",
// //MCUVersion = "D0.52.40.1770.P0",
// IsInputCheckpassed = true,
// FirmwareUpdateModels = new List(),
//};
UpdateProcedure();
LogoutTimer.Start();
}
private void KeyinListener_OnSystemIDReseived(object sender, SystemID systemID)
{
SystemIDScanReseived(systemID);
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
try
{
LogoutTimer.Tick -= LogoutTimer_Tick;
LogoutTimer.Stop();
}
catch
{
}
//Environment.Exit(0);
}
private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
ResetLogoutTimer();
}
private void SystemIDScanReseived(SystemID systemID)
{
ViewModel.IsInputCheckpassed = false;
inputSystemID = systemID;
var serialNumber = systemID.ToString();
if (!SajetConnect.SajetTransSnCheck(ref serialNumber))
{
HintDialog.ShowMessage(Resx.AppResources.SnWoMisMatch);
return;
}
else
{
if (LoadConfigBySystemID(systemID))
{
UpdateStatus = UpdateStatus.Idle;
}
}
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Loaded -= MainWindow_Loaded;
UpdateStatus = UpdateStatus.Idle;
//init intilize procedure list
//procedures.Add(new BasicInfoUpdateProcedure());
//procedures.Add(new FourGenModuleCheckProcedure());
//procedures.Add(new CsuFirmwareUpdateProcedure());
//procedures.Add(new McuFirmwareUpdateProcedure());
//procedures.Add(new ButtonStatusCheckPorcedure());
//procedures.Add(new RestarttoIdleProcedure());
//ViewModel.UpdateProcedure.Add(new VersionLogProcedure());
//uxProcedureDataGrid.ItemsSource = procedures;\
// var test = new HintDialog();
// test.ImgPath = "pack://application:,,,/AwInitilizer;component/Image/Blue.png";
// test.Message = "BLUE BOTTON"
//; test.ShowDialog();
DisplayLogin();
var tester = new ProcedureBase();
_ = tester.ChekCsuBootCompelete();
}
private void StartInit_Click(object sender, RoutedEventArgs e)
{
//check again
ViewModel.IsInputCheckpassed = false;
inputSystemID = ViewModel.SystemID;
var serialNumber = inputSystemID.ToString();
if (!SajetConnect.SajetTransSnCheck(ref serialNumber))
{
HintDialog.ShowMessage(Resx.AppResources.SnWoMisMatch);
return;
}
ViewModel.IsInputCheckpassed = CheckInputData();
if (ViewModel.IsInputCheckpassed)
{
ViewModel.IsUdatIng = true;
UpdateStatus = UpdateStatus.Updating;
_ = UpdateTask();
}
}
//private void CSUFileSelect_Click(object sender, RoutedEventArgs e)
//{
// OpenFileDialog openFileDialog = new OpenFileDialog();
// if (openFileDialog.ShowDialog() == true){
// ViewModel.CSUFileName = openFileDialog.SafeFileName;
// ViewModel.CSUFilePath = openFileDialog.FileName;
// }
//}
//private void MCUFileSelect_Click(object sender, RoutedEventArgs e)
//{
// OpenFileDialog openFileDialog = new OpenFileDialog();
// if (openFileDialog.ShowDialog() == true)
// {
// ViewModel.MCUFileName = openFileDialog.SafeFileName;
// ViewModel.MCUFilePath = openFileDialog.FileName;
// }
//}
#region
private bool CheckInputData()
{
bool isAvaliable = true;
string alertMsg = string.Empty;
UpdateData updateData;
if (DataContext is MainViewModel viewModel)
{
updateData = viewModel;
}
else
{
throw new Exception("DataContext type error");
}
if (string.IsNullOrEmpty(updateData.ModelName))
{
//alertMsg += "Model Name is Required\n";
alertMsg += Resx.AppResources.ModelNameEmptyAlert + "\n";
isAvaliable = false;
}
else if (ViewModel.SettingModelName != updateData.ModelName)
{
//alertMsg += "Model Name setting is Mismathed\n";
alertMsg += Resx.AppResources.ModelNameMismatchAlert + "\n";
isAvaliable = false;
}
if (string.IsNullOrEmpty(updateData.SerialNumber))
{
//alertMsg += "Serial Number is Required\n";
alertMsg += Resx.AppResources.SerialNumberEmptyAlert + "\n";
isAvaliable = false;
}
var systemIDString = updateData.ModelName + updateData.SerialNumber;
if (!string.IsNullOrEmpty(systemIDString))
{
if (SystemID.TryLooseParse(systemIDString, out SystemID systemID))
{
updateData.SystemID = systemID;
}
else
{
//alertMsg += "Model Name format Error\n";
alertMsg += Resx.AppResources.ModelNameErrorAlert + "\n";
isAvaliable = false;
}
}
if (updateData.SystemID != null &&
updateData.SystemID.ModelName.Network.Description.Contains("4G"))
{
if (string.IsNullOrEmpty(updateData.FourGenModuleVersion))
{
//alertMsg += "4G Module Version is Required\n";
alertMsg += Resx.AppResources.FourGenVersionEmptyAlert + "\n";
isAvaliable = false;
}
if (updateData.IsSimInsert)
{
if (string.IsNullOrEmpty(updateData.ICCID))
{
//alertMsg += "ICCID is Required when sim installed\n";
alertMsg += Resx.AppResources.IccidEmptyAlert + "\n";
isAvaliable = false;
}
if (string.IsNullOrEmpty(updateData.IMSI))
{
//alertMsg += "IMSI is Required when sim installed\n";
alertMsg += Resx.AppResources.ImsiEmptyAlert + "\n";
isAvaliable = false;
}
}
}
if (updateData.FirmwareUpdateModels == null)
{
//alertMsg += "FirmwareUpdateModels should be decalred\n";
alertMsg += Resx.AppResources.FirmwareListNullAlert + "\n";
isAvaliable = false;
}
else
{
foreach (var model in updateData.FirmwareUpdateModels)
{
if (string.IsNullOrEmpty(model.Module))
{
//alertMsg += "Firmware module name is Required\n";
alertMsg += Resx.AppResources.FirmwareNameEmptyAlert + "\n";
isAvaliable = false;
}
if (string.IsNullOrEmpty(model.Version))
{
//alertMsg += "Firmware module name is Required\n";
alertMsg += Resx.AppResources.FirmwareVersionEmptyAlert + "\n";
isAvaliable = false;
}
if (string.IsNullOrEmpty(model.FirmwareFileName))
{
//alertMsg += "Firmware file is Required\n";
alertMsg += Resx.AppResources.FirmwareFileEmptyAlert + "\n";
isAvaliable = false;
}
}
}
if (!isAvaliable)
{
HintDialog.ShowMessage(alertMsg);
}
return isAvaliable;
}
private async Task UpdateTask()
{
DateTime startTime, stopTime;
startTime = DateTime.Now;
ProcedureBase.UpdateData = ViewModel;
ProcedureBase.Logger = this;
MesErrorCode mesErrorCode = MesErrorCode.None;
//Dictionary logPairs = new Dictionary();
ValueReportDatas reportDatas = new ValueReportDatas();
//logPairs.Add("ModelName", ViewModel.SystemID.ModelName.ToString());
//logPairs.Add("SerialNumber", ViewModel.SystemID.SerialNumber);
ViewModel.IsUdatIng = true;
var procedureList = ViewModel.UpdateProcedure.Where(x => x.IsActivated).ToList();
int procedureIndex;
for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
{
procedureList[procedureIndex].MesLogData.Clear();
procedureList[procedureIndex].Reset();
}
for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
{
uxProgress.Value = (procedureIndex * 100 / procedureList.Count);
uxProgressRate.Content = ((int)(procedureIndex * 100 / procedureList.Count)) + "%";
uxStatusBar.Content = string.Format(Resx.AppResources.StatusBarUpdating, procedureList[procedureIndex].Name);// $"Processing {procedureList[procedureIndex].Name}";
var result = await procedureList[procedureIndex].DoWork();
foreach (var procedureLog in procedureList[procedureIndex].MesLogData)
{
//logPairs[procedureLog.Key] = procedureLog.Val;
reportDatas.Add(procedureLog);
}
if (!result)
{
mesErrorCode = Converter.MesErrorCodeMaper.GetMesErrorCode(procedureList[procedureIndex]);
break;
}
}
//report MES result
ReportMESLog(reportDatas);
if (procedureIndex == procedureList.Count)
{
uxProgress.Value = 100;
uxProgressRate.Content = "100%";
UpdateStatus = UpdateStatus.Sucess;
//report Success
SajetConnect.SajetTranFinishSuccess();
CsvReport(true, reportDatas);
}
else
{
UpdateStatus = UpdateStatus.Fail;
//uxStatusBar.Content = $"Process {procedureList[procedureIndex].Name} Failed";
uxStatusBar.Content = string.Format(Resx.AppResources.StatusBarFailed, procedureList[procedureIndex].Name);
//report Fail
SajetConnect.SajetTranFinishFail(mesErrorCode);
CsvReport(false ,reportDatas);
}
stopTime = DateTime.Now;
ViewModel.UpdateElpased = stopTime - startTime;
CreateLogFile();
CreateUploadCustomterLogFile();
ViewModel.IsUdatIng = false;
ViewModel.IsInputCheckpassed = false;
}
private void ReportMESLog(ValueReportDatas logPairs)
{
//report value
var reportResult = SajetConnect.SajetTransReport(logPairs);
}
private void CsvReport(bool isTestPass, ValueReportDatas reportDatas)
{
var fileName = string.Format("{0}_{1}.csv", ViewModel.SystemID + ViewModel.SerialNumber, DateTime.Now.ToString("yyyyMMddHHmmssffff"));
var folderName = "CSV";
var filePath = Path.Combine(folderName, fileName);
if (File.Exists(fileName))
{
File.Delete(fileName);
}
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
FileStream fileStream = new FileStream(filePath, FileMode.Create);
StreamWriter fileWriter = new StreamWriter(fileStream);
fileWriter.WriteLine("key,value");
fileWriter.WriteLine(string.Format("{0},{1}", "IsTestPass", isTestPass.ToString()));
foreach (var data in reportDatas)
{
fileWriter.WriteLine(string.Format("{0},{1}", data.Key, data.Val));
}
fileWriter.Close();
fileStream.Close();
}
private void CreateLogFile()
{
var fileName = ViewModel.SystemID + ViewModel.SerialNumber + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
var folderName = "Log";
var filePath = Path.Combine(folderName, fileName);
string content = "";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
if (!Directory.Exists("Log"))
{
Directory.CreateDirectory("Log");
}
FileStream fileStream = new FileStream(filePath, FileMode.Create);
StreamWriter fileWriter = new StreamWriter(fileStream);
fileWriter.WriteLine("Barcode");
fileWriter.WriteLine($"Model name:{ ViewModel.ModelName } , Serial number: { ViewModel.SerialNumber }");
fileWriter.WriteLine("==========================");
fileWriter.WriteLine("Setting file");
fileWriter.WriteLine($"Model name:{ ViewModel.SettingModelName}");
fileWriter.WriteLine($"4G Module Version:{ ViewModel.FourGenModuleVersion}");
fileWriter.WriteLine($"Is sim insert:{ ViewModel.IsSimInsert}");
fileWriter.WriteLine($"sim ICCID:{ ViewModel.ICCID}");
fileWriter.WriteLine($"sim IMSI:{ ViewModel.IMSI}");
foreach (var model in ViewModel.FirmwareUpdateModels)
{
fileWriter.WriteLine($"{model.Module} version:{ model.Version}");
}
var procedureList = ViewModel.UpdateProcedure.ToList();
for (int procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
{
var procedure = procedureList[procedureIndex];
fileWriter.WriteLine("==========================");
fileWriter.WriteLine(procedure.Name);
fileWriter.WriteLine("Is Activated:" + (procedure.IsActivated ? "Yes" : "No"));
fileWriter.WriteLine("Status:" + procedure.Status.ToString());
fileWriter.WriteLine(procedure.InfoLog);
}
fileWriter.WriteLine("==========================");
fileWriter.Close();
fileStream.Close();
}
private void CreateUploadCustomterLogFile()
{
var fileName = ViewModel.SystemID + ViewModel.SerialNumber + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
var folderName = "CustomerLog";
var filePath = Path.Combine(folderName, fileName);
if (File.Exists(fileName))
{
File.Delete(fileName);
}
if (!Directory.Exists("CustomerLog"))
{
Directory.CreateDirectory("CustomerLog");
}
CreateCustomerLog(filePath);
}
private void CreateCustomerLog(string filePath)
{
FileStream fileStream = new FileStream(filePath, FileMode.Create);
StreamWriter fileWriter = new StreamWriter(fileStream);
string borderString = "================================================================================";
//fileWriter.WriteLine(" Switch Mode Power Supply");
fileWriter.WriteLine(" Button Test and Firmware update");
fileWriter.WriteLine(" test report");
fileWriter.WriteLine("");
fileWriter.WriteLine("Test Program Name : {0,-30} ", "FirmwareUpdate");
fileWriter.WriteLine("Serial No : {0,-30}System Time : {1}", ViewModel.SystemID.ToString(), DateTime.Now.ToString());
fileWriter.WriteLine("Model Name : {0,-30}Elapsed Time : {1}", ViewModel.ModelName, ViewModel.UpdateElpased.ToString(@"hh\:mm\:ss"));
fileWriter.WriteLine("LOT Number : Environment : ");
fileWriter.WriteLine("Order Number : Inspector : {0}", ViewModel.UserID);
fileWriter.WriteLine("Customer : Test Result : {0}", ViewModel.UpdateProcedure.Last().Status == ProcedureStatus.PASS ? "PASS" : "FAIL");
fileWriter.WriteLine("");
fileWriter.WriteLine(borderString);
for (int procedureIndex = 0; procedureIndex < ViewModel.UpdateProcedure.Count; procedureIndex++)
{
var procedure = ViewModel.UpdateProcedure[procedureIndex];
fileWriter.WriteLine("STEP.{0} : {1,-63}{2,6}", procedureIndex + 1, procedure.Name, procedure.Status.ToString());
foreach (var reportLog in procedure.ReportLog)
{
fileWriter.WriteLine(reportLog);
}
fileWriter.WriteLine(borderString);
}
fileWriter.Close();
fileStream.Close();
}
public void Print(string msg, bool isError = false)
{
SajetConnect.SajetTransLog(msg);
Dispatcher.Invoke(() =>
{
Span line = new Span();
line.Inlines.Add(msg + "\n");
Span.SetForeground(line, isError ? Brushes.Red : Brushes.Green);
//uxTerminal.Inlines.Add(line);
//uxTerminalScroller.ScrollToEnd();
});
Console.WriteLine(msg);
}
#endregion
private bool LoadConfigBySystemID(SystemID systemID)
{
string settingRootFolder;
try
{
var defaultPath = AppSettingConfig.FirmwareRoot;
if (string.IsNullOrEmpty(defaultPath))
{
defaultPath = ".\\";
}
settingRootFolder = Path.GetFullPath(defaultPath);
}
catch
{
//HintDialog.ShowMessage("Firmware root path ERROR");
HintDialog.ShowMessage(Resx.AppResources.LoadConfigRootFolderNotfoundAlert);
return false;
}
if (!Directory.Exists(settingRootFolder))
{
//HintDialog.ShowMessage("Firmware root path not exist");
HintDialog.ShowMessage(Resx.AppResources.LoadConfigFolderNotfoundAlert);
return false;
}
string modelDirectoy = Path.Combine(settingRootFolder, systemID.ModelName.ToString());
if (!Directory.Exists(modelDirectoy))
{
//HintDialog.ShowMessage("Model firmware root path not exist");
HintDialog.ShowMessage(Resx.AppResources.LoadConfigModelFolderNotfoundAlert);
return false;
}
string modelSeettingFilePath = Path.Combine(modelDirectoy, systemID.ModelName.ToString() + ".ini");
if (!File.Exists(modelSeettingFilePath))
{
//HintDialog.ShowMessage("Model firmware setting not exist");
HintDialog.ShowMessage(Resx.AppResources.LoadConfigModelInitNotfoundAlert);
return false;
}
//check format
var settingString = File.ReadAllText(modelSeettingFilePath);
SettingConfig setting;
try
{
setting = JsonConvert.DeserializeObject(settingString);
}
catch
{
//HintDialog.ShowMessage("Setting file ERROR");
HintDialog.ShowMessage(Resx.AppResources.LoadConfigModelInitFormatErrorAlert);
return false;
}
if (CheckSettingConfig(System.IO.Path.GetDirectoryName(modelSeettingFilePath), ref setting))
{
ViewModel.SystemID = systemID;
ViewModel.ModelName = systemID.ModelName.ToString();
ViewModel.SerialNumber = systemID.SerialNumber;
ViewModel.SettingModelName = setting.ModelName;
ViewModel.SettingFileName = Path.GetFileName(modelSeettingFilePath);
ViewModel.FourGenModuleVersion = setting.FourGenModuleVersion;
ViewModel.IsSimInsert = setting.IsSimInsert;
ViewModel.ICCID = setting.ICCID;
ViewModel.IMSI = setting.IMSI;
ViewModel.SkipEmergencyButton = setting.SkipEmergencyButton;
ViewModel.FirmwareUpdateModels = setting.FirmwareUpdateList
.Where(x => !string.IsNullOrEmpty(x.Module) && !string.IsNullOrEmpty(x.FirmwareFileName)
).ToList();
UpdateProcedure();
ViewModel.IsInputCheckpassed = CheckInputData();
}
return true;
}
private void UpdateProcedure()
{
List procedures = new List();
//init intilize procedure list
//procedures.Add(new Procedure.BasicInfoUpdate.BasicInfoUpdateProcedure());
//procedures.Add(new Procedure.FourGenModuleCheck.FourGenModuleCheckProcedure());
//procedures.Add(new Procedure.WifRssiCheck.WifRssiCheckProcedure());
//procedures.Add(new Procedure.TelcomModemImeiRecord.TelcomModemImeiRecordProcedure());
//procedures.Add(new Procedure.FirmwareBundleUpload.FirmwareFtpUploadProcedure());
//procedures.Add(new Procedure.FirmwareCheckVersion.FirmwareCheckVersionProcedure());
procedures.Add(new Procedure.ButtonStatusCheck.ButtonStatusCheckPorcedure());
//procedures.Add(new Procedure.RestarttoIdle.RestarttoIdleProcedure());
//procedures.Add(new Procedure.VersionLog.VersionLogProcedure());
//procedures.Add(new Procedure.BasicInfoUpdate.BasicInfoUpdateProcedure());
//procedures.Add(new Procedure.FourGenModuleCheck.FourGenModuleCheckProcedure());
//procedures.Add(new Procedure.WifRssiCheck.WifRssiCheckProcedure());
//procedures.Add(new Procedure.FirmwareBundleUpload.FirmwareBundleUploadProcedure());
//procedures.Add(new Procedure.FirmwareCheckVersion.FirmwareCheckVersionProcedure());
//procedures.Add(new Procedure.ButtonStatusCheck.ButtonStatusCheckPorcedure());
//procedures.Add(new Procedure.RestarttoIdel.RestarttoIdleProcedure());
//procedures.Add(new Procedure.VersionLog.VersionLogProcedure());
//for (int firemwareIndex = 0; firemwareIndex < ViewModel.FirmwareUpdateModels.Count ; firemwareIndex++)
//{
// //procedures.Add(new FirmwareUpdateProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
// procedures.Add(new FirmwareUploadProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
//}
ViewModel.UpdateProcedure.Clear();
foreach (var p in procedures)
{
ViewModel.UpdateProcedure.Add(p);
}
//uxProcedureDataGrid.ItemsSource = procedures;
}
private bool CheckSettingConfig(string folderPath, ref SettingConfig setting)
{
if (setting == null)
return false;
bool isCheckPassed = true;
if (string.IsNullOrEmpty(setting.ModelName))
{
//HintDialog.ShowMessage("ModelName is requred");
HintDialog.ShowMessage(Resx.AppResources.InitModelNameEmptyAlert);
isCheckPassed = false;
}
else if (!ModelName.TryLooseParse(setting.ModelName, out _))
{
//HintDialog.ShowMessage("ModelName format Error");
HintDialog.ShowMessage(Resx.AppResources.InitModelNameErrorAlert);
isCheckPassed = false;
}
if (setting.IsSimInsert)
{
if (string.IsNullOrEmpty(setting.ICCID))
{
//HintDialog.ShowMessage("ICCID should not empty while IsSimInsert is set");
HintDialog.ShowMessage(Resx.AppResources.InitIccidEmptyAlert);
isCheckPassed = false;
}
if (string.IsNullOrEmpty(setting.IMSI))
{
//HintDialog.ShowMessage("IMSI should not empty while IsSimInsert is set");
HintDialog.ShowMessage(Resx.AppResources.InitImsiEmptyAlert);
isCheckPassed = false;
}
}
if (setting.FirmwareUpdateList != null)
{
for (int firmwareIndex = 0; firmwareIndex < setting.FirmwareUpdateList.Count; firmwareIndex++)
{
var model = setting.FirmwareUpdateList[firmwareIndex];
bool isVersionVaild = !string.IsNullOrEmpty(model.Version);
bool isFileNameVaild = !string.IsNullOrEmpty(model.FirmwareFileName);
bool isNoduleNameVaild = !string.IsNullOrEmpty(model.Module);
if (!isNoduleNameVaild)
{
//HintDialog.ShowMessage("Firmware module name should not empty");
HintDialog.ShowMessage(Resx.AppResources.InitFirmwareNameEmptyAlert);
isCheckPassed = false;
}
if (isVersionVaild || isFileNameVaild)
{
if (!isVersionVaild)
{
//HintDialog.ShowMessage($"Version should not empty while {model.Module} firmware is set");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareVersionEmptyAlert, model.Module));
isCheckPassed = false;
}
if (!isFileNameVaild)
{
//HintDialog.ShowMessage($"File name should not empty while {model.Module} version is set");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileEmptyAlert, model.Module));
isCheckPassed = false;
}
if (isVersionVaild && isFileNameVaild)
{
var filePath = System.IO.Path.Combine(folderPath, model.FirmwareFileName);
//check file exist
if (!File.Exists(filePath))
{
//HintDialog.ShowMessage($"{model.Module} Firemware file is missing");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileMissingAlert, model.Module));
isCheckPassed = false;
}
else
{
try
{
using (var fs = File.OpenRead(filePath))
{
byte[] systemIDBytes = new byte[16];
if (fs.Read(systemIDBytes, 0, 16) == 16)
{
if (systemIDBytes.ToList().Contains(0x00))
{
int endIndex = Array.FindIndex(systemIDBytes, (x) => { return x == 0x00; });
//int endIndex = parameter.FindIndex((x) => { return x == 0x00; });
if (endIndex != default && endIndex != -1)
{
systemIDBytes = systemIDBytes.Take(endIndex).ToArray();
}
}
if (ModelName.TryLooseParse(systemIDBytes, out var modelName))
{
if (modelName.ToString() != setting.ModelName)
{
//HintDialog.ShowMessage($"{model.Module} Firemware and ModelName is Mismatched");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileHeaderMismatchAlert, model.Module));
isCheckPassed = false;
}
}
else
{
// HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileHeaderFormatAlert, model.Module));
isCheckPassed = false;
}
}
else
{
//HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileHeaderFormatAlert, model.Module));
isCheckPassed = false;
}
//byte[] imgType = new byte[4];
//if (fs.Read(imgType, 0, 4) == 4)
//{
// if (!imgType.SequenceEqual(new byte[] { 0x10, 0x00, 0x00, 0x04, }))
// {
// HintDialog.ShowMessage($"{model.Module} Firemware type ERROR");
// isCheckPassed = false;
// }
//}
//else
//{
// HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
// isCheckPassed = false;
//}
}
}
catch
{
//HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
HintDialog.ShowMessage(string.Format(Resx.AppResources.InitFirmwareFileHeaderFormatAlert, model.Module));
isCheckPassed = false;
}
model.FirmwareFileName = filePath;
}
}
}
}
}
return isCheckPassed;
}
private void ResetLogoutTimer()
{
if (LogoutTimer != null)
{
logoutCheckCnt = 0;
LogoutTimer.Stop();
LogoutTimer.Start();
}
}
private void DisplayLogin()
{
LogoutTimer?.Stop();
ViewModel.UserID = "";
ViewModel.WorkOrder = "";
var signinDialog = new SigninDialog();
try
{
signinDialog.Owner = this;
}
catch
{
}
signinDialog.ShowDialog();
this.Focus();
//SystemID.TryLooseParse("DSYE122E0ED5P7D2212A011A0", out var systemID);
//SystemIDScanReseived(systemID);
if (signinDialog.DialogResult != true)
{
SajetConnect.SajetTransClose();
App.Current.Shutdown();
}
else
{
ViewModel.UserID = signinDialog.UserId;
ViewModel.WorkOrder = signinDialog.WorkOrder;
LogoutTimer?.Start();
}
}
private int logoutCheckCnt = 0;
private void LogoutTimer_Tick(object sender, EventArgs e)
{
LogoutTimer.Stop();
if (ViewModel.IsUdatIng)
{
logoutCheckCnt = 0;
LogoutTimer.Start();
Console.WriteLine("LogoutTimer_Tick reset");
return;
}
else if (++logoutCheckCnt > 10)
{
logoutCheckCnt = 0;
DisplayLogin();
Console.WriteLine("LogoutTimer_Tick DisplayLogin");
}
else
{
LogoutTimer.Start();
Console.WriteLine("LogoutTimer_Tick continue");
}
}
private void Logout_Click(object sender, RoutedEventArgs e)
{
DisplayLogin();
}
private void WorkOrder_TextChanged(object sender, TextChangedEventArgs e)
{
//ViewModel.IsInputCheckpassed = false;
}
private void WorkOrder_KeyDown(object sender, KeyEventArgs e)
{
//ViewModel.IsInputCheckpassed = true;
if (e.Key == Key.Enter && ViewModel.SystemID != null)
{
SystemIDScanReseived(ViewModel.SystemID);
}
}
private void SetUpdateStatus(UpdateStatus status)
{
if (status == UpdateStatus.Idle)
{
uxStatusContainer.Background = new SolidColorBrush(Colors.White);
uxStatus.Foreground = new SolidColorBrush(Colors.Black);
uxStatus.Content = Resx.AppResources.StatusIdel;//"Idel";
uxStatusBar.Content = "";
}
else if (status == UpdateStatus.Updating)
{
uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
uxStatus.Foreground = new SolidColorBrush(Colors.Black);
uxStatus.Content = Resx.AppResources.StatusUpdating;//"Updating";
}
else if (status == UpdateStatus.Sucess)
{
uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
uxStatus.Foreground = new SolidColorBrush(Colors.White);
uxStatus.Content = Resx.AppResources.StatusSuccess;//"Success";
uxStatusBar.Content = "";
}
else if (status == UpdateStatus.Fail)
{
uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
uxStatus.Foreground = new SolidColorBrush(Colors.White);
uxStatus.Content = Resx.AppResources.StatusFail;//"Fail";
}
}
}
}