1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024 |
- using AwInitilizer.Model;
- using AwInitilizer.Procedure;
- using Microsoft.Win32;
- using Newtonsoft.Json;
- 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
- {
- Idel,
- Updating,
- Sucess,
- Fail
- }
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window, Interface.IIogger
- {
- private string barCode = string.Empty;
- private DispatcherTimer CleanInputTimer;
- private DispatcherTimer LogoutTimer;
- private bool IsInitilizing = false;
- private SystemID inputSystemID;
- private string csuFilePath;
- private string mcuFilePath;
- private MainViewModel ViewModel => DataContext as MainViewModel;
- private UpdateStatus _UpdateStatus = UpdateStatus.Idel;
- private UpdateStatus UpdateStatus
- {
- get => _UpdateStatus;
- set => SetUpdateStatus(value);
- }
- public MainWindow()
- {
- InitializeComponent();
- ServicePointManager.ServerCertificateValidationCallback +=
- (sender, cert, chain, sslPolicyErrors) => true;
- System.Net.ServicePointManager.Expect100Continue = false;
- CleanInputTimer = new DispatcherTimer();
- CleanInputTimer.Interval = TimeSpan.FromSeconds(1);
- CleanInputTimer.Tick += CleanInputTimer_Tick;
- LogoutTimer = new DispatcherTimer();
- LogoutTimer.Interval = TimeSpan.FromMinutes(1);
- LogoutTimer.Tick += LogoutTimer_Tick;
- Loaded += MainWindow_Loaded;
- PreviewKeyDown += MainWindow_PreviewKeyDown;
- PreviewKeyUp += MainWindow_PreviewKeyUp;
- //this.DataContext = new MainViewModel();
- 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<FirmwareUpdateModel>(),
- };
- this.DataContextChanged += MainWindow_DataContextChanged;
- LogoutTimer.Start();
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- base.OnClosing(e);
- try
- {
- CleanInputTimer.Tick -= CleanInputTimer_Tick;
- CleanInputTimer.Stop();
- }
- catch
- {
- }
- try
- {
- LogoutTimer.Tick -= LogoutTimer_Tick;
- LogoutTimer.Stop();
- }
- catch
- {
- }
- Environment.Exit(0);
- }
- private void MainWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- }
- private void CleanInputTimer_Tick(object sender, EventArgs e)
- {
- CleanInputTimer.Stop();
- Console.WriteLine(barCode);
- if (SystemID.TryParse(barCode, out SystemID systemID))
- {
- SystemIDScanReseived(systemID);
- }
- barCode = string.Empty;
- }
- bool shiftPressed = false;
- private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- ResetLogoutTimer();
- if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
- {
- shiftPressed = true;
- return;
- }
- else if(e.Key == Key.Capital)
- {
- shiftPressed = !shiftPressed;
- return;
- }
- else if (e.Key == Key.Enter)
- {
- e.Handled = true;
- return;
- }
- else
- {
- if (e.Key >= Key.D0 && e.Key <= Key.D9)
- {
- // Number keys pressed so need to so special processing
- // also check if shift pressed
- var input = e.Key.ToString();
- input = input.Remove(0, 1);
- barCode += input;
- }
- else if (e.Key == Key.OemMinus)
- {
- barCode += "-";
- }
- else if (e.Key == Key.Return)
- {
- return;
- }
- else
- {
- var input = e.Key.ToString();
- if (shiftPressed)
- input = input.ToUpper();
- barCode += input;
- }
- }
- CleanInputTimer.Stop();
- CleanInputTimer.Start();
- }
- private void SystemIDScanReseived(SystemID systemID)
- {
- ViewModel.IsInputCheckpassed = false;
- inputSystemID = systemID;
- var serialNumber = systemID.ToString();
- if (!DLL.SajetConnect.SajetTransSnCheck(ref serialNumber))
- {
- HintDialog.ShowMessage((string)Application.Current.FindResource("SnWoMisMatch"));
- return;
- }
- else
- {
- if (LoadConfigBySystemID(systemID))
- {
- UpdateStatus = UpdateStatus.Idel;
- }
- }
- }
- private void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
- {
- shiftPressed = false;
- }
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- Loaded -= MainWindow_Loaded;
- UpdateStatus = UpdateStatus.Idel;
- //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 RestarttoIdelProcedure());
- //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 (!DLL.SajetConnect.SajetTransSnCheck(ref serialNumber))
- {
- HintDialog.ShowMessage((string)Application.Current.FindResource("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 += (string)Application.Current.FindResource("ModelNameEmptyAlert") + "\n";
- isAvaliable = false;
- }
- else if (ViewModel.SettingModelName != updateData.ModelName)
- {
- //alertMsg += "Model Name setting is Mismathed\n";
- alertMsg += (string)Application.Current.FindResource("ModelNameMismatchAlert") + "\n";
- isAvaliable = false;
- }
- if (string.IsNullOrEmpty(updateData.SerialNumber))
- {
- //alertMsg += "Serial Number is Required\n";
- alertMsg += (string)Application.Current.FindResource("SerialNumberEmptyAlert") + "\n";
- isAvaliable = false;
- }
- var systemIDString = updateData.ModelName + updateData.SerialNumber;
- if (!string.IsNullOrEmpty(systemIDString))
- {
- if (SystemID.TryParse(systemIDString, out SystemID systemID))
- {
- updateData.SystemID = systemID;
- }
- else
- {
- //alertMsg += "Model Name format Error\n";
- alertMsg += (string)Application.Current.FindResource("ModelNameErrorAlert") + "\n";
- isAvaliable = false;
- }
- }
- if (updateData.SystemID != null &&
- updateData.SystemID.ModelName.Network.ToString().Contains("4G"))
- {
- if (string.IsNullOrEmpty(updateData.FourGenModuleVersion))
- {
- //alertMsg += "4G Module Version is Required\n";
- alertMsg += (string)Application.Current.FindResource("FourGenVersionEmptyAlert") + "\n";
- isAvaliable = false;
- }
- if (updateData.IsSimInsert)
- {
- if (string.IsNullOrEmpty(updateData.ICCID))
- {
- //alertMsg += "ICCID is Required when sim installed\n";
- alertMsg += (string)Application.Current.FindResource("IccidEmptyAlert") + "\n";
- isAvaliable = false;
- }
- if (string.IsNullOrEmpty(updateData.IMSI))
- {
- //alertMsg += "IMSI is Required when sim installed\n";
- alertMsg += (string)Application.Current.FindResource("ImsiEmptyAlert") + "\n";
- isAvaliable = false;
- }
- }
- }
- if (updateData.FirmwareUpdateModels == null)
- {
- //alertMsg += "FirmwareUpdateModels should be decalred\n";
- alertMsg += (string)Application.Current.FindResource("FirmwareListNullAlert") + "\n";
- isAvaliable = false;
- }
- else
- {
- foreach (var model in updateData.FirmwareUpdateModels)
- {
- if (string.IsNullOrEmpty(model.Module))
- {
- //alertMsg += "Firmware module name is Required\n";
- alertMsg += (string)Application.Current.FindResource("FirmwareNameEmptyAlert") + "\n";
- isAvaliable = false;
- }
- if (string.IsNullOrEmpty(model.Version))
- {
- //alertMsg += "Firmware module name is Required\n";
- alertMsg += (string)Application.Current.FindResource("FirmwareVersionEmptyAlert") + "\n";
- isAvaliable = false;
- }
- if (string.IsNullOrEmpty(model.FirmwareFileName))
- {
- //alertMsg += "Firmware file is Required\n";
- alertMsg += (string)Application.Current.FindResource("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;
- Dictionary<string, string> logPairs = new Dictionary<string, string>();
- //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].LogPair.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((string)Application.Current.FindResource("StatusBarUpdating"), procedureList[procedureIndex].Name);// $"Processing {procedureList[procedureIndex].Name}";
- var result = await procedureList[procedureIndex].DoWork();
- foreach(var procedureLog in procedureList[procedureIndex].LogPair)
- {
- if (logPairs.ContainsKey(procedureLog.Key))
- {
- logPairs[procedureLog.Key] = procedureLog.Value;
- }
- else
- {
- logPairs.Add(procedureLog.Key, procedureLog.Value);
- }
- }
- if (!result)
- break;
- }
- //report MES result
- ReportMESLog(logPairs);
- if (procedureIndex == procedureList.Count)
- {
- uxProgress.Value = 100;
- uxProgressRate.Content = "100%";
- UpdateStatus = UpdateStatus.Sucess;
- //report Success
- DLL.SajetConnect.SajetTranFinish(true);
- }
- else
- {
- UpdateStatus = UpdateStatus.Fail;
- //uxStatusBar.Content = $"Process {procedureList[procedureIndex].Name} Failed";
- uxStatusBar.Content = string.Format((string)Application.Current.FindResource("StatusBarFailed"), procedureList[procedureIndex].Name);
- }
- stopTime = DateTime.Now;
- ViewModel.UpdateElpased = stopTime - startTime;
- CreateLogFile();
- CreateUploadCustomterLogFile();
- ViewModel.IsUdatIng = false;
- ViewModel.IsInputCheckpassed = false;
- }
- private void ReportMESLog(Dictionary<string, string> logPairs)
- {
- //build header
- Dictionary<string, int> valuePairs = new Dictionary<string, int>();
- foreach (var pair in logPairs)
- {
- if(int.TryParse(pair.Value,out int val))
- {
- valuePairs.Add(pair.Key,val);
- }
- else if(pair.Value.ToLower().Contains("fail"))
- {
- valuePairs.Add(pair.Key, 0);
- }
- else if (pair.Value.ToLower().Contains("success"))
- {
- valuePairs.Add(pair.Key, 1);
- }
- else
- {
- valuePairs.Add(string.Format("{0}:{1}", pair.Key, pair.Value),1);
- }
- }
- //register Header
- var codePair = new Dictionary<string, string>();
- var model = ViewModel.SystemID.ModelName.ToString();
- foreach(var key in valuePairs.Keys)
- {
- var code = DLL.SajetConnect.SajetTransRegisterHeader(model,key);
- if (string.IsNullOrEmpty(code))
- continue;
- codePair.Add(key, code);
- }
- //report value
- var reportResult = DLL.SajetConnect.SajetTransReport(valuePairs, codePair);
- }
- 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)
- {
- DLL.SajetConnect.SajetTransLog(msg, isError);
- 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 = Properties.Settings.Default.FirmwareRoot;
- if(string.IsNullOrEmpty(defaultPath))
- {
- defaultPath = ".\\";
- }
- settingRootFolder = Path.GetFullPath(defaultPath);
- }
- catch
- {
- //HintDialog.ShowMessage("Firmware root path ERROR");
- HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigRootFolderNotfoundAlert"));
- return false;
- }
- if (!Directory.Exists(settingRootFolder))
- {
- //HintDialog.ShowMessage("Firmware root path not exist");
- HintDialog.ShowMessage((string)Application.Current.FindResource("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((string)Application.Current.FindResource("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((string)Application.Current.FindResource("LoadConfigModelInitNotfoundAlert"));
- return false;
- }
- //check format
- var settingString = File.ReadAllText(modelSeettingFilePath);
- SettingConfig setting;
- try
- {
- setting = JsonConvert.DeserializeObject<SettingConfig>(settingString);
- }
- catch
- {
- //HintDialog.ShowMessage("Setting file ERROR");
- HintDialog.ShowMessage((string)Application.Current.FindResource("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.FirmwareUpdateModels = setting.FirmwareUpdateList
- .Where(x => !string.IsNullOrEmpty(x.Module) && !string.IsNullOrEmpty(x.FirmwareFileName)
- ).ToList();
- UpdateProcedure();
- ViewModel.IsInputCheckpassed = CheckInputData();
- }
- return true;
- }
- private void UpdateProcedure()
- {
- List<ProcedureBase> procedures = new List<ProcedureBase>();
- //init intilize procedure list
- procedures.Add(new BasicInfoUpdateProcedure());
- procedures.Add(new FourGenModuleCheckProcedure());
- //for (int firemwareIndex = 0; firemwareIndex < ViewModel.FirmwareUpdateModels.Count ; firemwareIndex++)
- //{
- // //procedures.Add(new FirmwareUpdateProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
- // procedures.Add(new FirmwareUploadProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
- //}
- procedures.Add(new FirmwareBundleUploadProcedure());
- procedures.Add(new FirmwareCheckVersionProcedure());
- procedures.Add(new ButtonStatusCheckPorcedure());
- procedures.Add(new RestarttoIdelProcedure());
- procedures.Add(new VersionLogProcedure());
- 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((string)Application.Current.FindResource("InitModelNameEmptyAlert"));
- isCheckPassed = false;
- }
- else if(!ModelName.TryParse(setting.ModelName,out _))
- {
- //HintDialog.ShowMessage("ModelName format Error");
- HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameErrorAlert"));
- isCheckPassed = false;
- }
- if (setting.IsSimInsert)
- {
- if (string.IsNullOrEmpty(setting.ICCID))
- {
- //HintDialog.ShowMessage("ICCID should not empty while IsSimInsert is set");
- HintDialog.ShowMessage((string)Application.Current.FindResource("InitIccidEmptyAlert"));
- isCheckPassed = false;
- }
- if (string.IsNullOrEmpty(setting.IMSI))
- {
- //HintDialog.ShowMessage("IMSI should not empty while IsSimInsert is set");
- HintDialog.ShowMessage((string)Application.Current.FindResource("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((string)Application.Current.FindResource("InitFirmwareNameEmptyAlert"));
- isCheckPassed = false;
- }
- if (isVersionVaild || isFileNameVaild)
- {
- if (!isVersionVaild)
- {
- //HintDialog.ShowMessage($"Version should not empty while {model.Module} firmware is set");
- HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareVersionEmptyAlert"),model.Module));
- isCheckPassed = false;
- }
- if (!isFileNameVaild)
- {
- //HintDialog.ShowMessage($"File name should not empty while {model.Module} version is set");
- HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("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((string)Application.Current.FindResource("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.TryParse(systemIDBytes, out var modelName))
- {
- if (modelName.ToString() != setting.ModelName)
- {
- //HintDialog.ShowMessage($"{model.Module} Firemware and ModelName is Mismatched");
- HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderMismatchAlert"), model.Module));
- isCheckPassed = false;
- }
- }
- else
- {
- // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
- HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
- isCheckPassed = false;
- }
- }
- else
- {
- //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
- HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("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((string)Application.Current.FindResource("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.TryParse("DSWU601U0UD2PHD2111A001A0", out var systemID);
- //SystemIDScanReseived(systemID);
- if (signinDialog.DialogResult != true)
- {
- 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.Idel)
- {
- uxStatusContainer.Background = new SolidColorBrush(Colors.White);
- uxStatus.Foreground = new SolidColorBrush(Colors.Black);
- uxStatus.Content = (string)Application.Current.FindResource("StatusIdel");//"Idel";
- uxStatusBar.Content = "";
- }
- else if (status == UpdateStatus.Updating)
- {
- uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
- uxStatus.Foreground = new SolidColorBrush(Colors.Black);
- uxStatus.Content = (string)Application.Current.FindResource("StatusUpdating");//"Updating";
- }
- else if (status == UpdateStatus.Sucess)
- {
- uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
- uxStatus.Foreground = new SolidColorBrush(Colors.White);
- uxStatus.Content = (string)Application.Current.FindResource("StatusSuccess");//"Success";
- uxStatusBar.Content = "";
- }
- else if (status == UpdateStatus.Fail)
- {
- uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
- uxStatus.Foreground = new SolidColorBrush(Colors.White);
- uxStatus.Content = (string)Application.Current.FindResource("StatusFail");//"Fail";
- }
- }
- }
- }
|