MainWindow.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. using AwInitilizer.Model;
  2. using AwInitilizer.Procedure;
  3. using Microsoft.Win32;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Threading;
  23. namespace AwInitilizer
  24. {
  25. enum UpdateStatus
  26. {
  27. Idel,
  28. Updating,
  29. Sucess,
  30. Fail
  31. }
  32. /// <summary>
  33. /// Interaction logic for MainWindow.xaml
  34. /// </summary>
  35. public partial class MainWindow : Window, Interface.IIogger
  36. {
  37. private DispatcherTimer LogoutTimer;
  38. private KeyinListener keyinListener;
  39. private bool IsInitilizing = false;
  40. private SystemID inputSystemID;
  41. private string csuFilePath;
  42. private string mcuFilePath;
  43. private MainViewModel ViewModel => DataContext as MainViewModel;
  44. private UpdateStatus _UpdateStatus = UpdateStatus.Idel;
  45. private UpdateStatus UpdateStatus
  46. {
  47. get => _UpdateStatus;
  48. set => SetUpdateStatus(value);
  49. }
  50. public MainWindow()
  51. {
  52. InitializeComponent();
  53. ServicePointManager.ServerCertificateValidationCallback +=
  54. (sender, cert, chain, sslPolicyErrors) => true;
  55. System.Net.ServicePointManager.Expect100Continue = false;
  56. LogoutTimer = new DispatcherTimer();
  57. LogoutTimer.Interval = TimeSpan.FromMinutes(1);
  58. LogoutTimer.Tick += LogoutTimer_Tick;
  59. keyinListener = new KeyinListener(this);
  60. keyinListener.OnSystemIDReseived += KeyinListener_OnSystemIDReseived;
  61. Loaded += MainWindow_Loaded;
  62. //this.DataContext = new MainViewModel();
  63. this.DataContext = new MainViewModel()
  64. {
  65. //SystemID = systemID,
  66. //ModelName = "AWLU770001W1P0",
  67. //SettingModelName = "AWLU770001W1P0",
  68. //SerialNumber = "D2045A001A0",
  69. ////FourGenModuleVersion = "EC25AFFAR07A08M4G",
  70. //IsSimInsert = false,
  71. ////ICCID = "12345678901234567890",
  72. ////IMSI = "123456789012345",
  73. ////CSUVersion = "V1.01.01.0601.00",
  74. ////MCUVersion = "D0.52.40.1770.P0",
  75. //IsInputCheckpassed = true,
  76. //FirmwareUpdateModels = new List<FirmwareUpdateModel>(),
  77. };
  78. LogoutTimer.Start();
  79. }
  80. private void KeyinListener_OnSystemIDReseived(object sender, SystemID systemID)
  81. {
  82. SystemIDScanReseived(systemID);
  83. }
  84. protected override void OnClosing(CancelEventArgs e)
  85. {
  86. base.OnClosing(e);
  87. try
  88. {
  89. LogoutTimer.Tick -= LogoutTimer_Tick;
  90. LogoutTimer.Stop();
  91. }
  92. catch
  93. {
  94. }
  95. Environment.Exit(0);
  96. }
  97. private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
  98. {
  99. ResetLogoutTimer();
  100. }
  101. private void SystemIDScanReseived(SystemID systemID)
  102. {
  103. ViewModel.IsInputCheckpassed = false;
  104. inputSystemID = systemID;
  105. var serialNumber = systemID.ToString();
  106. if (!DLL.SajetConnect.SajetTransSnCheck(ref serialNumber))
  107. {
  108. HintDialog.ShowMessage((string)Application.Current.FindResource("SnWoMisMatch"));
  109. return;
  110. }
  111. else
  112. {
  113. if (LoadConfigBySystemID(systemID))
  114. {
  115. UpdateStatus = UpdateStatus.Idel;
  116. }
  117. }
  118. }
  119. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  120. {
  121. Loaded -= MainWindow_Loaded;
  122. UpdateStatus = UpdateStatus.Idel;
  123. //init intilize procedure list
  124. //procedures.Add(new BasicInfoUpdateProcedure());
  125. //procedures.Add(new FourGenModuleCheckProcedure());
  126. //procedures.Add(new CsuFirmwareUpdateProcedure());
  127. //procedures.Add(new McuFirmwareUpdateProcedure());
  128. //procedures.Add(new ButtonStatusCheckPorcedure());
  129. //procedures.Add(new RestarttoIdelProcedure());
  130. //ViewModel.UpdateProcedure.Add(new VersionLogProcedure());
  131. //uxProcedureDataGrid.ItemsSource = procedures;\
  132. // var test = new HintDialog();
  133. // test.ImgPath = "pack://application:,,,/AwInitilizer;component/Image/Blue.png";
  134. // test.Message = "BLUE BOTTON"
  135. //; test.ShowDialog();
  136. DisplayLogin();
  137. var tester = new ProcedureBase();
  138. _ = tester.ChekCsuBootCompelete();
  139. }
  140. private void StartInit_Click(object sender, RoutedEventArgs e)
  141. {
  142. //check again
  143. ViewModel.IsInputCheckpassed = false;
  144. inputSystemID = ViewModel.SystemID;
  145. var serialNumber = inputSystemID.ToString();
  146. if (!DLL.SajetConnect.SajetTransSnCheck(ref serialNumber))
  147. {
  148. HintDialog.ShowMessage((string)Application.Current.FindResource("SnWoMisMatch"));
  149. return;
  150. }
  151. ViewModel.IsInputCheckpassed = CheckInputData();
  152. if (ViewModel.IsInputCheckpassed)
  153. {
  154. ViewModel.IsUdatIng = true;
  155. UpdateStatus = UpdateStatus.Updating;
  156. _ = UpdateTask();
  157. }
  158. }
  159. //private void CSUFileSelect_Click(object sender, RoutedEventArgs e)
  160. //{
  161. // OpenFileDialog openFileDialog = new OpenFileDialog();
  162. // if (openFileDialog.ShowDialog() == true){
  163. // ViewModel.CSUFileName = openFileDialog.SafeFileName;
  164. // ViewModel.CSUFilePath = openFileDialog.FileName;
  165. // }
  166. //}
  167. //private void MCUFileSelect_Click(object sender, RoutedEventArgs e)
  168. //{
  169. // OpenFileDialog openFileDialog = new OpenFileDialog();
  170. // if (openFileDialog.ShowDialog() == true)
  171. // {
  172. // ViewModel.MCUFileName = openFileDialog.SafeFileName;
  173. // ViewModel.MCUFilePath = openFileDialog.FileName;
  174. // }
  175. //}
  176. #region
  177. private bool CheckInputData()
  178. {
  179. bool isAvaliable = true;
  180. string alertMsg = string.Empty;
  181. UpdateData updateData;
  182. if (DataContext is MainViewModel viewModel)
  183. {
  184. updateData = viewModel;
  185. }
  186. else
  187. {
  188. throw new Exception("DataContext type error");
  189. }
  190. if (string.IsNullOrEmpty(updateData.ModelName))
  191. {
  192. //alertMsg += "Model Name is Required\n";
  193. alertMsg += (string)Application.Current.FindResource("ModelNameEmptyAlert") + "\n";
  194. isAvaliable = false;
  195. }
  196. else if (ViewModel.SettingModelName != updateData.ModelName)
  197. {
  198. //alertMsg += "Model Name setting is Mismathed\n";
  199. alertMsg += (string)Application.Current.FindResource("ModelNameMismatchAlert") + "\n";
  200. isAvaliable = false;
  201. }
  202. if (string.IsNullOrEmpty(updateData.SerialNumber))
  203. {
  204. //alertMsg += "Serial Number is Required\n";
  205. alertMsg += (string)Application.Current.FindResource("SerialNumberEmptyAlert") + "\n";
  206. isAvaliable = false;
  207. }
  208. var systemIDString = updateData.ModelName + updateData.SerialNumber;
  209. if (!string.IsNullOrEmpty(systemIDString))
  210. {
  211. if (SystemID.TryParse(systemIDString, out SystemID systemID))
  212. {
  213. updateData.SystemID = systemID;
  214. }
  215. else
  216. {
  217. //alertMsg += "Model Name format Error\n";
  218. alertMsg += (string)Application.Current.FindResource("ModelNameErrorAlert") + "\n";
  219. isAvaliable = false;
  220. }
  221. }
  222. if (updateData.SystemID != null &&
  223. updateData.SystemID.ModelName.Network.ToString().Contains("4G"))
  224. {
  225. if (string.IsNullOrEmpty(updateData.FourGenModuleVersion))
  226. {
  227. //alertMsg += "4G Module Version is Required\n";
  228. alertMsg += (string)Application.Current.FindResource("FourGenVersionEmptyAlert") + "\n";
  229. isAvaliable = false;
  230. }
  231. if (updateData.IsSimInsert)
  232. {
  233. if (string.IsNullOrEmpty(updateData.ICCID))
  234. {
  235. //alertMsg += "ICCID is Required when sim installed\n";
  236. alertMsg += (string)Application.Current.FindResource("IccidEmptyAlert") + "\n";
  237. isAvaliable = false;
  238. }
  239. if (string.IsNullOrEmpty(updateData.IMSI))
  240. {
  241. //alertMsg += "IMSI is Required when sim installed\n";
  242. alertMsg += (string)Application.Current.FindResource("ImsiEmptyAlert") + "\n";
  243. isAvaliable = false;
  244. }
  245. }
  246. }
  247. if (updateData.FirmwareUpdateModels == null)
  248. {
  249. //alertMsg += "FirmwareUpdateModels should be decalred\n";
  250. alertMsg += (string)Application.Current.FindResource("FirmwareListNullAlert") + "\n";
  251. isAvaliable = false;
  252. }
  253. else
  254. {
  255. foreach (var model in updateData.FirmwareUpdateModels)
  256. {
  257. if (string.IsNullOrEmpty(model.Module))
  258. {
  259. //alertMsg += "Firmware module name is Required\n";
  260. alertMsg += (string)Application.Current.FindResource("FirmwareNameEmptyAlert") + "\n";
  261. isAvaliable = false;
  262. }
  263. if (string.IsNullOrEmpty(model.Version))
  264. {
  265. //alertMsg += "Firmware module name is Required\n";
  266. alertMsg += (string)Application.Current.FindResource("FirmwareVersionEmptyAlert") + "\n";
  267. isAvaliable = false;
  268. }
  269. if (string.IsNullOrEmpty(model.FirmwareFileName))
  270. {
  271. //alertMsg += "Firmware file is Required\n";
  272. alertMsg += (string)Application.Current.FindResource("FirmwareFileEmptyAlert") + "\n";
  273. isAvaliable = false;
  274. }
  275. }
  276. }
  277. if (!isAvaliable)
  278. {
  279. HintDialog.ShowMessage(alertMsg);
  280. }
  281. return isAvaliable;
  282. }
  283. private async Task UpdateTask()
  284. {
  285. DateTime startTime, stopTime;
  286. startTime = DateTime.Now;
  287. ProcedureBase.UpdateData = ViewModel;
  288. ProcedureBase.Logger = this;
  289. MesErrorCode mesErrorCode = MesErrorCode.None;
  290. Dictionary<string, string> logPairs = new Dictionary<string, string>();
  291. //logPairs.Add("ModelName", ViewModel.SystemID.ModelName.ToString());
  292. //logPairs.Add("SerialNumber", ViewModel.SystemID.SerialNumber);
  293. ViewModel.IsUdatIng = true;
  294. var procedureList = ViewModel.UpdateProcedure.Where(x => x.IsActivated).ToList();
  295. int procedureIndex;
  296. for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  297. {
  298. procedureList[procedureIndex].LogPair.Clear();
  299. procedureList[procedureIndex].Reset();
  300. }
  301. for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  302. {
  303. uxProgress.Value = (procedureIndex * 100 / procedureList.Count);
  304. uxProgressRate.Content = ((int)(procedureIndex * 100 / procedureList.Count)) + "%";
  305. uxStatusBar.Content = string.Format((string)Application.Current.FindResource("StatusBarUpdating"), procedureList[procedureIndex].Name);// $"Processing {procedureList[procedureIndex].Name}";
  306. var result = await procedureList[procedureIndex].DoWork();
  307. foreach (var procedureLog in procedureList[procedureIndex].LogPair)
  308. {
  309. logPairs[procedureLog.Key] = procedureLog.Value;
  310. }
  311. if (!result)
  312. {
  313. mesErrorCode = Converter.MesErrorCodeMaper.GetMesErrorCode(procedureList[procedureIndex]);
  314. break;
  315. }
  316. }
  317. //report MES result
  318. ReportMESLog(logPairs);
  319. if (procedureIndex == procedureList.Count)
  320. {
  321. uxProgress.Value = 100;
  322. uxProgressRate.Content = "100%";
  323. UpdateStatus = UpdateStatus.Sucess;
  324. //report Success
  325. DLL.SajetConnect.SajetTranFinishSuccess();
  326. }
  327. else
  328. {
  329. UpdateStatus = UpdateStatus.Fail;
  330. //uxStatusBar.Content = $"Process {procedureList[procedureIndex].Name} Failed";
  331. uxStatusBar.Content = string.Format((string)Application.Current.FindResource("StatusBarFailed"), procedureList[procedureIndex].Name);
  332. //report Fail
  333. DLL.SajetConnect.SajetTranFinishFail(mesErrorCode);
  334. }
  335. stopTime = DateTime.Now;
  336. ViewModel.UpdateElpased = stopTime - startTime;
  337. CreateLogFile();
  338. CreateUploadCustomterLogFile();
  339. ViewModel.IsUdatIng = false;
  340. ViewModel.IsInputCheckpassed = false;
  341. }
  342. private void ReportMESLog(Dictionary<string, string> logPairs)
  343. {
  344. //report value
  345. var reportResult = DLL.SajetConnect.SajetTransReport(logPairs);
  346. }
  347. private void CreateLogFile()
  348. {
  349. var fileName = ViewModel.SystemID + ViewModel.SerialNumber + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
  350. var folderName = "Log";
  351. var filePath = Path.Combine(folderName, fileName);
  352. string content = "";
  353. if (File.Exists(fileName))
  354. {
  355. File.Delete(fileName);
  356. }
  357. if (!Directory.Exists("Log"))
  358. {
  359. Directory.CreateDirectory("Log");
  360. }
  361. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  362. StreamWriter fileWriter = new StreamWriter(fileStream);
  363. fileWriter.WriteLine("Barcode");
  364. fileWriter.WriteLine($"Model name:{ ViewModel.ModelName } , Serial number: { ViewModel.SerialNumber }");
  365. fileWriter.WriteLine("==========================");
  366. fileWriter.WriteLine("Setting file");
  367. fileWriter.WriteLine($"Model name:{ ViewModel.SettingModelName}");
  368. fileWriter.WriteLine($"4G Module Version:{ ViewModel.FourGenModuleVersion}");
  369. fileWriter.WriteLine($"Is sim insert:{ ViewModel.IsSimInsert}");
  370. fileWriter.WriteLine($"sim ICCID:{ ViewModel.ICCID}");
  371. fileWriter.WriteLine($"sim IMSI:{ ViewModel.IMSI}");
  372. foreach (var model in ViewModel.FirmwareUpdateModels)
  373. {
  374. fileWriter.WriteLine($"{model.Module} version:{ model.Version}");
  375. }
  376. var procedureList = ViewModel.UpdateProcedure.ToList();
  377. for (int procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  378. {
  379. var procedure = procedureList[procedureIndex];
  380. fileWriter.WriteLine("==========================");
  381. fileWriter.WriteLine(procedure.Name);
  382. fileWriter.WriteLine("Is Activated:" + (procedure.IsActivated ? "Yes" : "No"));
  383. fileWriter.WriteLine("Status:" + procedure.Status.ToString());
  384. fileWriter.WriteLine(procedure.InfoLog);
  385. }
  386. fileWriter.WriteLine("==========================");
  387. fileWriter.Close();
  388. fileStream.Close();
  389. }
  390. private void CreateUploadCustomterLogFile()
  391. {
  392. var fileName = ViewModel.SystemID + ViewModel.SerialNumber + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
  393. var folderName = "CustomerLog";
  394. var filePath = Path.Combine(folderName, fileName);
  395. if (File.Exists(fileName))
  396. {
  397. File.Delete(fileName);
  398. }
  399. if (!Directory.Exists("CustomerLog"))
  400. {
  401. Directory.CreateDirectory("CustomerLog");
  402. }
  403. CreateCustomerLog(filePath);
  404. }
  405. private void CreateCustomerLog(string filePath)
  406. {
  407. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  408. StreamWriter fileWriter = new StreamWriter(fileStream);
  409. string borderString = "================================================================================";
  410. //fileWriter.WriteLine(" Switch Mode Power Supply");
  411. fileWriter.WriteLine(" Button Test and Firmware update");
  412. fileWriter.WriteLine(" test report");
  413. fileWriter.WriteLine("");
  414. fileWriter.WriteLine("Test Program Name : {0,-30} ", "FirmwareUpdate");
  415. fileWriter.WriteLine("Serial No : {0,-30}System Time : {1}", ViewModel.SystemID.ToString(), DateTime.Now.ToString());
  416. fileWriter.WriteLine("Model Name : {0,-30}Elapsed Time : {1}", ViewModel.ModelName, ViewModel.UpdateElpased.ToString(@"hh\:mm\:ss"));
  417. fileWriter.WriteLine("LOT Number : Environment : ");
  418. fileWriter.WriteLine("Order Number : Inspector : {0}", ViewModel.UserID);
  419. fileWriter.WriteLine("Customer : Test Result : {0}", ViewModel.UpdateProcedure.Last().Status == ProcedureStatus.PASS ? "PASS" : "FAIL");
  420. fileWriter.WriteLine("");
  421. fileWriter.WriteLine(borderString);
  422. for (int procedureIndex = 0; procedureIndex < ViewModel.UpdateProcedure.Count; procedureIndex++)
  423. {
  424. var procedure = ViewModel.UpdateProcedure[procedureIndex];
  425. fileWriter.WriteLine("STEP.{0} : {1,-63}{2,6}", procedureIndex + 1, procedure.Name, procedure.Status.ToString());
  426. foreach (var reportLog in procedure.ReportLog)
  427. {
  428. fileWriter.WriteLine(reportLog);
  429. }
  430. fileWriter.WriteLine(borderString);
  431. }
  432. fileWriter.Close();
  433. fileStream.Close();
  434. }
  435. public void Print(string msg, bool isError = false)
  436. {
  437. DLL.SajetConnect.SajetTransLog(msg);
  438. Dispatcher.Invoke(() =>
  439. {
  440. Span line = new Span();
  441. line.Inlines.Add(msg + "\n");
  442. Span.SetForeground(line, isError ? Brushes.Red : Brushes.Green);
  443. //uxTerminal.Inlines.Add(line);
  444. //uxTerminalScroller.ScrollToEnd();
  445. });
  446. Console.WriteLine(msg);
  447. }
  448. #endregion
  449. private bool LoadConfigBySystemID(SystemID systemID)
  450. {
  451. string settingRootFolder;
  452. try
  453. {
  454. var defaultPath = Properties.Settings.Default.FirmwareRoot;
  455. if (string.IsNullOrEmpty(defaultPath))
  456. {
  457. defaultPath = ".\\";
  458. }
  459. settingRootFolder = Path.GetFullPath(defaultPath);
  460. }
  461. catch
  462. {
  463. //HintDialog.ShowMessage("Firmware root path ERROR");
  464. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigRootFolderNotfoundAlert"));
  465. return false;
  466. }
  467. if (!Directory.Exists(settingRootFolder))
  468. {
  469. //HintDialog.ShowMessage("Firmware root path not exist");
  470. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigFolderNotfoundAlert"));
  471. return false;
  472. }
  473. string modelDirectoy = Path.Combine(settingRootFolder, systemID.ModelName.ToString());
  474. if (!Directory.Exists(modelDirectoy))
  475. {
  476. //HintDialog.ShowMessage("Model firmware root path not exist");
  477. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelFolderNotfoundAlert"));
  478. return false;
  479. }
  480. string modelSeettingFilePath = Path.Combine(modelDirectoy, systemID.ModelName.ToString() + ".ini");
  481. if (!File.Exists(modelSeettingFilePath))
  482. {
  483. //HintDialog.ShowMessage("Model firmware setting not exist");
  484. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitNotfoundAlert"));
  485. return false;
  486. }
  487. //check format
  488. var settingString = File.ReadAllText(modelSeettingFilePath);
  489. SettingConfig setting;
  490. try
  491. {
  492. setting = JsonConvert.DeserializeObject<SettingConfig>(settingString);
  493. }
  494. catch
  495. {
  496. //HintDialog.ShowMessage("Setting file ERROR");
  497. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitFormatErrorAlert"));
  498. return false;
  499. }
  500. if (CheckSettingConfig(System.IO.Path.GetDirectoryName(modelSeettingFilePath), ref setting))
  501. {
  502. ViewModel.SystemID = systemID;
  503. ViewModel.ModelName = systemID.ModelName.ToString();
  504. ViewModel.SerialNumber = systemID.SerialNumber;
  505. ViewModel.SettingModelName = setting.ModelName;
  506. ViewModel.SettingFileName = Path.GetFileName(modelSeettingFilePath);
  507. ViewModel.FourGenModuleVersion = setting.FourGenModuleVersion;
  508. ViewModel.IsSimInsert = setting.IsSimInsert;
  509. ViewModel.ICCID = setting.ICCID;
  510. ViewModel.IMSI = setting.IMSI;
  511. ViewModel.FirmwareUpdateModels = setting.FirmwareUpdateList
  512. .Where(x => !string.IsNullOrEmpty(x.Module) && !string.IsNullOrEmpty(x.FirmwareFileName)
  513. ).ToList();
  514. UpdateProcedure();
  515. ViewModel.IsInputCheckpassed = CheckInputData();
  516. }
  517. return true;
  518. }
  519. private void UpdateProcedure()
  520. {
  521. List<ProcedureBase> procedures = new List<ProcedureBase>();
  522. //init intilize procedure list
  523. procedures.Add(new Procedure.BasicInfoUpdate.BasicInfoUpdateProcedure());
  524. procedures.Add(new Procedure.FourGenModuleCheck.FourGenModuleCheckProcedure());
  525. procedures.Add(new Procedure.WifRssiCheck.WifRssiCheckProcedure());
  526. //for (int firemwareIndex = 0; firemwareIndex < ViewModel.FirmwareUpdateModels.Count ; firemwareIndex++)
  527. //{
  528. // //procedures.Add(new FirmwareUpdateProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  529. // procedures.Add(new FirmwareUploadProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  530. //}
  531. procedures.Add(new Procedure.FirmwareBundleUpload.FirmwareBundleUploadProcedure());
  532. procedures.Add(new Procedure.FirmwareCheckVersion.FirmwareCheckVersionProcedure());
  533. procedures.Add(new Procedure.ButtonStatusCheck.ButtonStatusCheckPorcedure());
  534. procedures.Add(new Procedure.RestarttoIdel.RestarttoIdelProcedure());
  535. procedures.Add(new Procedure.VersionLog.VersionLogProcedure());
  536. ViewModel.UpdateProcedure.Clear();
  537. foreach (var p in procedures)
  538. {
  539. ViewModel.UpdateProcedure.Add(p);
  540. }
  541. //uxProcedureDataGrid.ItemsSource = procedures;
  542. }
  543. private bool CheckSettingConfig(string folderPath, ref SettingConfig setting)
  544. {
  545. if (setting == null)
  546. return false;
  547. bool isCheckPassed = true;
  548. if (string.IsNullOrEmpty(setting.ModelName))
  549. {
  550. //HintDialog.ShowMessage("ModelName is requred");
  551. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameEmptyAlert"));
  552. isCheckPassed = false;
  553. }
  554. else if (!ModelName.TryParse(setting.ModelName, out _))
  555. {
  556. //HintDialog.ShowMessage("ModelName format Error");
  557. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameErrorAlert"));
  558. isCheckPassed = false;
  559. }
  560. if (setting.IsSimInsert)
  561. {
  562. if (string.IsNullOrEmpty(setting.ICCID))
  563. {
  564. //HintDialog.ShowMessage("ICCID should not empty while IsSimInsert is set");
  565. HintDialog.ShowMessage((string)Application.Current.FindResource("InitIccidEmptyAlert"));
  566. isCheckPassed = false;
  567. }
  568. if (string.IsNullOrEmpty(setting.IMSI))
  569. {
  570. //HintDialog.ShowMessage("IMSI should not empty while IsSimInsert is set");
  571. HintDialog.ShowMessage((string)Application.Current.FindResource("InitImsiEmptyAlert"));
  572. isCheckPassed = false;
  573. }
  574. }
  575. if (setting.FirmwareUpdateList != null)
  576. {
  577. for (int firmwareIndex = 0; firmwareIndex < setting.FirmwareUpdateList.Count; firmwareIndex++)
  578. {
  579. var model = setting.FirmwareUpdateList[firmwareIndex];
  580. bool isVersionVaild = !string.IsNullOrEmpty(model.Version);
  581. bool isFileNameVaild = !string.IsNullOrEmpty(model.FirmwareFileName);
  582. bool isNoduleNameVaild = !string.IsNullOrEmpty(model.Module);
  583. if (!isNoduleNameVaild)
  584. {
  585. //HintDialog.ShowMessage("Firmware module name should not empty");
  586. HintDialog.ShowMessage((string)Application.Current.FindResource("InitFirmwareNameEmptyAlert"));
  587. isCheckPassed = false;
  588. }
  589. if (isVersionVaild || isFileNameVaild)
  590. {
  591. if (!isVersionVaild)
  592. {
  593. //HintDialog.ShowMessage($"Version should not empty while {model.Module} firmware is set");
  594. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareVersionEmptyAlert"), model.Module));
  595. isCheckPassed = false;
  596. }
  597. if (!isFileNameVaild)
  598. {
  599. //HintDialog.ShowMessage($"File name should not empty while {model.Module} version is set");
  600. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileEmptyAlert"), model.Module));
  601. isCheckPassed = false;
  602. }
  603. if (isVersionVaild && isFileNameVaild)
  604. {
  605. var filePath = System.IO.Path.Combine(folderPath, model.FirmwareFileName);
  606. //check file exist
  607. if (!File.Exists(filePath))
  608. {
  609. //HintDialog.ShowMessage($"{model.Module} Firemware file is missing");
  610. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileMissingAlert"), model.Module));
  611. isCheckPassed = false;
  612. }
  613. else
  614. {
  615. try
  616. {
  617. using (var fs = File.OpenRead(filePath))
  618. {
  619. byte[] systemIDBytes = new byte[16];
  620. if (fs.Read(systemIDBytes, 0, 16) == 16)
  621. {
  622. if (systemIDBytes.ToList().Contains(0x00))
  623. {
  624. int endIndex = Array.FindIndex(systemIDBytes, (x) => { return x == 0x00; });
  625. //int endIndex = parameter.FindIndex((x) => { return x == 0x00; });
  626. if (endIndex != default && endIndex != -1)
  627. {
  628. systemIDBytes = systemIDBytes.Take(endIndex).ToArray();
  629. }
  630. }
  631. if (ModelName.TryParse(systemIDBytes, out var modelName))
  632. {
  633. if (modelName.ToString() != setting.ModelName)
  634. {
  635. //HintDialog.ShowMessage($"{model.Module} Firemware and ModelName is Mismatched");
  636. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderMismatchAlert"), model.Module));
  637. isCheckPassed = false;
  638. }
  639. }
  640. else
  641. {
  642. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  643. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  644. isCheckPassed = false;
  645. }
  646. }
  647. else
  648. {
  649. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  650. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  651. isCheckPassed = false;
  652. }
  653. //byte[] imgType = new byte[4];
  654. //if (fs.Read(imgType, 0, 4) == 4)
  655. //{
  656. // if (!imgType.SequenceEqual(new byte[] { 0x10, 0x00, 0x00, 0x04, }))
  657. // {
  658. // HintDialog.ShowMessage($"{model.Module} Firemware type ERROR");
  659. // isCheckPassed = false;
  660. // }
  661. //}
  662. //else
  663. //{
  664. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  665. // isCheckPassed = false;
  666. //}
  667. }
  668. }
  669. catch
  670. {
  671. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  672. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  673. isCheckPassed = false;
  674. }
  675. model.FirmwareFileName = filePath;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. return isCheckPassed;
  682. }
  683. private void ResetLogoutTimer()
  684. {
  685. if (LogoutTimer != null)
  686. {
  687. logoutCheckCnt = 0;
  688. LogoutTimer.Stop();
  689. LogoutTimer.Start();
  690. }
  691. }
  692. private void DisplayLogin()
  693. {
  694. LogoutTimer?.Stop();
  695. ViewModel.UserID = "";
  696. ViewModel.WorkOrder = "";
  697. var signinDialog = new SigninDialog();
  698. try
  699. {
  700. signinDialog.Owner = this;
  701. }
  702. catch
  703. {
  704. }
  705. signinDialog.ShowDialog();
  706. this.Focus();
  707. //SystemID.TryParse("DSWU601U0UD2PHD2111A001A0", out var systemID);
  708. //SystemIDScanReseived(systemID);
  709. if (signinDialog.DialogResult != true)
  710. {
  711. App.Current.Shutdown();
  712. }
  713. else
  714. {
  715. ViewModel.UserID = signinDialog.UserId;
  716. ViewModel.WorkOrder = signinDialog.WorkOrder;
  717. LogoutTimer?.Start();
  718. }
  719. }
  720. private int logoutCheckCnt = 0;
  721. private void LogoutTimer_Tick(object sender, EventArgs e)
  722. {
  723. LogoutTimer.Stop();
  724. if (ViewModel.IsUdatIng)
  725. {
  726. logoutCheckCnt = 0;
  727. LogoutTimer.Start();
  728. Console.WriteLine("LogoutTimer_Tick reset");
  729. return;
  730. }
  731. else if (++logoutCheckCnt > 10)
  732. {
  733. logoutCheckCnt = 0;
  734. DisplayLogin();
  735. Console.WriteLine("LogoutTimer_Tick DisplayLogin");
  736. }
  737. else
  738. {
  739. LogoutTimer.Start();
  740. Console.WriteLine("LogoutTimer_Tick continue");
  741. }
  742. }
  743. private void Logout_Click(object sender, RoutedEventArgs e)
  744. {
  745. DisplayLogin();
  746. }
  747. private void WorkOrder_TextChanged(object sender, TextChangedEventArgs e)
  748. {
  749. //ViewModel.IsInputCheckpassed = false;
  750. }
  751. private void WorkOrder_KeyDown(object sender, KeyEventArgs e)
  752. {
  753. //ViewModel.IsInputCheckpassed = true;
  754. if (e.Key == Key.Enter && ViewModel.SystemID != null)
  755. {
  756. SystemIDScanReseived(ViewModel.SystemID);
  757. }
  758. }
  759. private void SetUpdateStatus(UpdateStatus status)
  760. {
  761. if (status == UpdateStatus.Idel)
  762. {
  763. uxStatusContainer.Background = new SolidColorBrush(Colors.White);
  764. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  765. uxStatus.Content = (string)Application.Current.FindResource("StatusIdel");//"Idel";
  766. uxStatusBar.Content = "";
  767. }
  768. else if (status == UpdateStatus.Updating)
  769. {
  770. uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
  771. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  772. uxStatus.Content = (string)Application.Current.FindResource("StatusUpdating");//"Updating";
  773. }
  774. else if (status == UpdateStatus.Sucess)
  775. {
  776. uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
  777. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  778. uxStatus.Content = (string)Application.Current.FindResource("StatusSuccess");//"Success";
  779. uxStatusBar.Content = "";
  780. }
  781. else if (status == UpdateStatus.Fail)
  782. {
  783. uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
  784. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  785. uxStatus.Content = (string)Application.Current.FindResource("StatusFail");//"Fail";
  786. }
  787. }
  788. }
  789. }