MainWindow.xaml.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. Dictionary<string, string> logPairs = new Dictionary<string, string>();
  290. //logPairs.Add("ModelName", ViewModel.SystemID.ModelName.ToString());
  291. //logPairs.Add("SerialNumber", ViewModel.SystemID.SerialNumber);
  292. ViewModel.IsUdatIng = true;
  293. var procedureList = ViewModel.UpdateProcedure.Where(x => x.IsActivated).ToList();
  294. int procedureIndex;
  295. for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  296. {
  297. procedureList[procedureIndex].LogPair.Clear();
  298. procedureList[procedureIndex].Reset();
  299. }
  300. for (procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  301. {
  302. uxProgress.Value = (procedureIndex * 100 / procedureList.Count);
  303. uxProgressRate.Content = ((int)(procedureIndex * 100 / procedureList.Count)) + "%";
  304. uxStatusBar.Content = string.Format((string)Application.Current.FindResource("StatusBarUpdating"), procedureList[procedureIndex].Name);// $"Processing {procedureList[procedureIndex].Name}";
  305. var result = await procedureList[procedureIndex].DoWork();
  306. foreach (var procedureLog in procedureList[procedureIndex].LogPair)
  307. {
  308. if (logPairs.ContainsKey(procedureLog.Key))
  309. {
  310. logPairs[procedureLog.Key] = procedureLog.Value;
  311. }
  312. else
  313. {
  314. logPairs.Add(procedureLog.Key, procedureLog.Value);
  315. }
  316. }
  317. if (!result)
  318. break;
  319. }
  320. //report MES result
  321. ReportMESLog(logPairs);
  322. if (procedureIndex == procedureList.Count)
  323. {
  324. uxProgress.Value = 100;
  325. uxProgressRate.Content = "100%";
  326. UpdateStatus = UpdateStatus.Sucess;
  327. //report Success
  328. DLL.SajetConnect.SajetTranFinish(true);
  329. }
  330. else
  331. {
  332. UpdateStatus = UpdateStatus.Fail;
  333. //uxStatusBar.Content = $"Process {procedureList[procedureIndex].Name} Failed";
  334. uxStatusBar.Content = string.Format((string)Application.Current.FindResource("StatusBarFailed"), procedureList[procedureIndex].Name);
  335. }
  336. stopTime = DateTime.Now;
  337. ViewModel.UpdateElpased = stopTime - startTime;
  338. CreateLogFile();
  339. CreateUploadCustomterLogFile();
  340. ViewModel.IsUdatIng = false;
  341. ViewModel.IsInputCheckpassed = false;
  342. }
  343. private void ReportMESLog(Dictionary<string, string> logPairs)
  344. {
  345. //build header
  346. Dictionary<string, int> valuePairs = new Dictionary<string, int>();
  347. foreach (var pair in logPairs)
  348. {
  349. if (int.TryParse(pair.Value, out int val))
  350. {
  351. valuePairs.Add(pair.Key, val);
  352. }
  353. else if (pair.Value.ToLower().Contains("fail"))
  354. {
  355. valuePairs.Add(pair.Key, 0);
  356. }
  357. else if (pair.Value.ToLower().Contains("success"))
  358. {
  359. valuePairs.Add(pair.Key, 1);
  360. }
  361. else
  362. {
  363. valuePairs.Add(string.Format("{0}:{1}", pair.Key, pair.Value), 1);
  364. }
  365. }
  366. //register Header
  367. var codePair = new Dictionary<string, string>();
  368. var model = ViewModel.SystemID.ModelName.ToString();
  369. foreach (var key in valuePairs.Keys)
  370. {
  371. var code = DLL.SajetConnect.SajetTransRegisterHeader(model, key);
  372. if (string.IsNullOrEmpty(code))
  373. continue;
  374. codePair.Add(key, code);
  375. }
  376. //report value
  377. var reportResult = DLL.SajetConnect.SajetTransReport(valuePairs, codePair);
  378. }
  379. private void CreateLogFile()
  380. {
  381. var fileName = ViewModel.SystemID + ViewModel.SerialNumber + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
  382. var folderName = "Log";
  383. var filePath = Path.Combine(folderName, fileName);
  384. string content = "";
  385. if (File.Exists(fileName))
  386. {
  387. File.Delete(fileName);
  388. }
  389. if (!Directory.Exists("Log"))
  390. {
  391. Directory.CreateDirectory("Log");
  392. }
  393. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  394. StreamWriter fileWriter = new StreamWriter(fileStream);
  395. fileWriter.WriteLine("Barcode");
  396. fileWriter.WriteLine($"Model name:{ ViewModel.ModelName } , Serial number: { ViewModel.SerialNumber }");
  397. fileWriter.WriteLine("==========================");
  398. fileWriter.WriteLine("Setting file");
  399. fileWriter.WriteLine($"Model name:{ ViewModel.SettingModelName}");
  400. fileWriter.WriteLine($"4G Module Version:{ ViewModel.FourGenModuleVersion}");
  401. fileWriter.WriteLine($"Is sim insert:{ ViewModel.IsSimInsert}");
  402. fileWriter.WriteLine($"sim ICCID:{ ViewModel.ICCID}");
  403. fileWriter.WriteLine($"sim IMSI:{ ViewModel.IMSI}");
  404. foreach (var model in ViewModel.FirmwareUpdateModels)
  405. {
  406. fileWriter.WriteLine($"{model.Module} version:{ model.Version}");
  407. }
  408. var procedureList = ViewModel.UpdateProcedure.ToList();
  409. for (int procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  410. {
  411. var procedure = procedureList[procedureIndex];
  412. fileWriter.WriteLine("==========================");
  413. fileWriter.WriteLine(procedure.Name);
  414. fileWriter.WriteLine("Is Activated:" + (procedure.IsActivated ? "Yes" : "No"));
  415. fileWriter.WriteLine("Status:" + procedure.Status.ToString());
  416. fileWriter.WriteLine(procedure.InfoLog);
  417. }
  418. fileWriter.WriteLine("==========================");
  419. fileWriter.Close();
  420. fileStream.Close();
  421. }
  422. private void CreateUploadCustomterLogFile()
  423. {
  424. var fileName = ViewModel.SystemID + ViewModel.SerialNumber + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
  425. var folderName = "CustomerLog";
  426. var filePath = Path.Combine(folderName, fileName);
  427. if (File.Exists(fileName))
  428. {
  429. File.Delete(fileName);
  430. }
  431. if (!Directory.Exists("CustomerLog"))
  432. {
  433. Directory.CreateDirectory("CustomerLog");
  434. }
  435. CreateCustomerLog(filePath);
  436. }
  437. private void CreateCustomerLog(string filePath)
  438. {
  439. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  440. StreamWriter fileWriter = new StreamWriter(fileStream);
  441. string borderString = "================================================================================";
  442. //fileWriter.WriteLine(" Switch Mode Power Supply");
  443. fileWriter.WriteLine(" Button Test and Firmware update");
  444. fileWriter.WriteLine(" test report");
  445. fileWriter.WriteLine("");
  446. fileWriter.WriteLine("Test Program Name : {0,-30} ", "FirmwareUpdate");
  447. fileWriter.WriteLine("Serial No : {0,-30}System Time : {1}", ViewModel.SystemID.ToString(), DateTime.Now.ToString());
  448. fileWriter.WriteLine("Model Name : {0,-30}Elapsed Time : {1}", ViewModel.ModelName, ViewModel.UpdateElpased.ToString(@"hh\:mm\:ss"));
  449. fileWriter.WriteLine("LOT Number : Environment : ");
  450. fileWriter.WriteLine("Order Number : Inspector : {0}", ViewModel.UserID);
  451. fileWriter.WriteLine("Customer : Test Result : {0}", ViewModel.UpdateProcedure.Last().Status == ProcedureStatus.PASS ? "PASS" : "FAIL");
  452. fileWriter.WriteLine("");
  453. fileWriter.WriteLine(borderString);
  454. for (int procedureIndex = 0; procedureIndex < ViewModel.UpdateProcedure.Count; procedureIndex++)
  455. {
  456. var procedure = ViewModel.UpdateProcedure[procedureIndex];
  457. fileWriter.WriteLine("STEP.{0} : {1,-63}{2,6}", procedureIndex + 1, procedure.Name, procedure.Status.ToString());
  458. foreach (var reportLog in procedure.ReportLog)
  459. {
  460. fileWriter.WriteLine(reportLog);
  461. }
  462. fileWriter.WriteLine(borderString);
  463. }
  464. fileWriter.Close();
  465. fileStream.Close();
  466. }
  467. public void Print(string msg, bool isError = false)
  468. {
  469. DLL.SajetConnect.SajetTransLog(msg, isError);
  470. Dispatcher.Invoke(() =>
  471. {
  472. Span line = new Span();
  473. line.Inlines.Add(msg + "\n");
  474. Span.SetForeground(line, isError ? Brushes.Red : Brushes.Green);
  475. //uxTerminal.Inlines.Add(line);
  476. //uxTerminalScroller.ScrollToEnd();
  477. });
  478. Console.WriteLine(msg);
  479. }
  480. #endregion
  481. private bool LoadConfigBySystemID(SystemID systemID)
  482. {
  483. string settingRootFolder;
  484. try
  485. {
  486. var defaultPath = Properties.Settings.Default.FirmwareRoot;
  487. if (string.IsNullOrEmpty(defaultPath))
  488. {
  489. defaultPath = ".\\";
  490. }
  491. settingRootFolder = Path.GetFullPath(defaultPath);
  492. }
  493. catch
  494. {
  495. //HintDialog.ShowMessage("Firmware root path ERROR");
  496. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigRootFolderNotfoundAlert"));
  497. return false;
  498. }
  499. if (!Directory.Exists(settingRootFolder))
  500. {
  501. //HintDialog.ShowMessage("Firmware root path not exist");
  502. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigFolderNotfoundAlert"));
  503. return false;
  504. }
  505. string modelDirectoy = Path.Combine(settingRootFolder, systemID.ModelName.ToString());
  506. if (!Directory.Exists(modelDirectoy))
  507. {
  508. //HintDialog.ShowMessage("Model firmware root path not exist");
  509. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelFolderNotfoundAlert"));
  510. return false;
  511. }
  512. string modelSeettingFilePath = Path.Combine(modelDirectoy, systemID.ModelName.ToString() + ".ini");
  513. if (!File.Exists(modelSeettingFilePath))
  514. {
  515. //HintDialog.ShowMessage("Model firmware setting not exist");
  516. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitNotfoundAlert"));
  517. return false;
  518. }
  519. //check format
  520. var settingString = File.ReadAllText(modelSeettingFilePath);
  521. SettingConfig setting;
  522. try
  523. {
  524. setting = JsonConvert.DeserializeObject<SettingConfig>(settingString);
  525. }
  526. catch
  527. {
  528. //HintDialog.ShowMessage("Setting file ERROR");
  529. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitFormatErrorAlert"));
  530. return false;
  531. }
  532. if (CheckSettingConfig(System.IO.Path.GetDirectoryName(modelSeettingFilePath), ref setting))
  533. {
  534. ViewModel.SystemID = systemID;
  535. ViewModel.ModelName = systemID.ModelName.ToString();
  536. ViewModel.SerialNumber = systemID.SerialNumber;
  537. ViewModel.SettingModelName = setting.ModelName;
  538. ViewModel.SettingFileName = Path.GetFileName(modelSeettingFilePath);
  539. ViewModel.FourGenModuleVersion = setting.FourGenModuleVersion;
  540. ViewModel.IsSimInsert = setting.IsSimInsert;
  541. ViewModel.ICCID = setting.ICCID;
  542. ViewModel.IMSI = setting.IMSI;
  543. ViewModel.FirmwareUpdateModels = setting.FirmwareUpdateList
  544. .Where(x => !string.IsNullOrEmpty(x.Module) && !string.IsNullOrEmpty(x.FirmwareFileName)
  545. ).ToList();
  546. UpdateProcedure();
  547. ViewModel.IsInputCheckpassed = CheckInputData();
  548. }
  549. return true;
  550. }
  551. private void UpdateProcedure()
  552. {
  553. List<ProcedureBase> procedures = new List<ProcedureBase>();
  554. //init intilize procedure list
  555. procedures.Add(new BasicInfoUpdateProcedure());
  556. procedures.Add(new FourGenModuleCheckProcedure());
  557. //for (int firemwareIndex = 0; firemwareIndex < ViewModel.FirmwareUpdateModels.Count ; firemwareIndex++)
  558. //{
  559. // //procedures.Add(new FirmwareUpdateProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  560. // procedures.Add(new FirmwareUploadProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  561. //}
  562. procedures.Add(new FirmwareBundleUploadProcedure());
  563. procedures.Add(new FirmwareCheckVersionProcedure());
  564. procedures.Add(new ButtonStatusCheckPorcedure());
  565. procedures.Add(new RestarttoIdelProcedure());
  566. procedures.Add(new VersionLogProcedure());
  567. ViewModel.UpdateProcedure.Clear();
  568. foreach (var p in procedures)
  569. {
  570. ViewModel.UpdateProcedure.Add(p);
  571. }
  572. //uxProcedureDataGrid.ItemsSource = procedures;
  573. }
  574. private bool CheckSettingConfig(string folderPath, ref SettingConfig setting)
  575. {
  576. if (setting == null)
  577. return false;
  578. bool isCheckPassed = true;
  579. if (string.IsNullOrEmpty(setting.ModelName))
  580. {
  581. //HintDialog.ShowMessage("ModelName is requred");
  582. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameEmptyAlert"));
  583. isCheckPassed = false;
  584. }
  585. else if (!ModelName.TryParse(setting.ModelName, out _))
  586. {
  587. //HintDialog.ShowMessage("ModelName format Error");
  588. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameErrorAlert"));
  589. isCheckPassed = false;
  590. }
  591. if (setting.IsSimInsert)
  592. {
  593. if (string.IsNullOrEmpty(setting.ICCID))
  594. {
  595. //HintDialog.ShowMessage("ICCID should not empty while IsSimInsert is set");
  596. HintDialog.ShowMessage((string)Application.Current.FindResource("InitIccidEmptyAlert"));
  597. isCheckPassed = false;
  598. }
  599. if (string.IsNullOrEmpty(setting.IMSI))
  600. {
  601. //HintDialog.ShowMessage("IMSI should not empty while IsSimInsert is set");
  602. HintDialog.ShowMessage((string)Application.Current.FindResource("InitImsiEmptyAlert"));
  603. isCheckPassed = false;
  604. }
  605. }
  606. if (setting.FirmwareUpdateList != null)
  607. {
  608. for (int firmwareIndex = 0; firmwareIndex < setting.FirmwareUpdateList.Count; firmwareIndex++)
  609. {
  610. var model = setting.FirmwareUpdateList[firmwareIndex];
  611. bool isVersionVaild = !string.IsNullOrEmpty(model.Version);
  612. bool isFileNameVaild = !string.IsNullOrEmpty(model.FirmwareFileName);
  613. bool isNoduleNameVaild = !string.IsNullOrEmpty(model.Module);
  614. if (!isNoduleNameVaild)
  615. {
  616. //HintDialog.ShowMessage("Firmware module name should not empty");
  617. HintDialog.ShowMessage((string)Application.Current.FindResource("InitFirmwareNameEmptyAlert"));
  618. isCheckPassed = false;
  619. }
  620. if (isVersionVaild || isFileNameVaild)
  621. {
  622. if (!isVersionVaild)
  623. {
  624. //HintDialog.ShowMessage($"Version should not empty while {model.Module} firmware is set");
  625. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareVersionEmptyAlert"), model.Module));
  626. isCheckPassed = false;
  627. }
  628. if (!isFileNameVaild)
  629. {
  630. //HintDialog.ShowMessage($"File name should not empty while {model.Module} version is set");
  631. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileEmptyAlert"), model.Module));
  632. isCheckPassed = false;
  633. }
  634. if (isVersionVaild && isFileNameVaild)
  635. {
  636. var filePath = System.IO.Path.Combine(folderPath, model.FirmwareFileName);
  637. //check file exist
  638. if (!File.Exists(filePath))
  639. {
  640. //HintDialog.ShowMessage($"{model.Module} Firemware file is missing");
  641. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileMissingAlert"), model.Module));
  642. isCheckPassed = false;
  643. }
  644. else
  645. {
  646. try
  647. {
  648. using (var fs = File.OpenRead(filePath))
  649. {
  650. byte[] systemIDBytes = new byte[16];
  651. if (fs.Read(systemIDBytes, 0, 16) == 16)
  652. {
  653. if (systemIDBytes.ToList().Contains(0x00))
  654. {
  655. int endIndex = Array.FindIndex(systemIDBytes, (x) => { return x == 0x00; });
  656. //int endIndex = parameter.FindIndex((x) => { return x == 0x00; });
  657. if (endIndex != default && endIndex != -1)
  658. {
  659. systemIDBytes = systemIDBytes.Take(endIndex).ToArray();
  660. }
  661. }
  662. if (ModelName.TryParse(systemIDBytes, out var modelName))
  663. {
  664. if (modelName.ToString() != setting.ModelName)
  665. {
  666. //HintDialog.ShowMessage($"{model.Module} Firemware and ModelName is Mismatched");
  667. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderMismatchAlert"), model.Module));
  668. isCheckPassed = false;
  669. }
  670. }
  671. else
  672. {
  673. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  674. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  675. isCheckPassed = false;
  676. }
  677. }
  678. else
  679. {
  680. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  681. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  682. isCheckPassed = false;
  683. }
  684. //byte[] imgType = new byte[4];
  685. //if (fs.Read(imgType, 0, 4) == 4)
  686. //{
  687. // if (!imgType.SequenceEqual(new byte[] { 0x10, 0x00, 0x00, 0x04, }))
  688. // {
  689. // HintDialog.ShowMessage($"{model.Module} Firemware type ERROR");
  690. // isCheckPassed = false;
  691. // }
  692. //}
  693. //else
  694. //{
  695. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  696. // isCheckPassed = false;
  697. //}
  698. }
  699. }
  700. catch
  701. {
  702. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  703. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  704. isCheckPassed = false;
  705. }
  706. model.FirmwareFileName = filePath;
  707. }
  708. }
  709. }
  710. }
  711. }
  712. return isCheckPassed;
  713. }
  714. private void ResetLogoutTimer()
  715. {
  716. if (LogoutTimer != null)
  717. {
  718. logoutCheckCnt = 0;
  719. LogoutTimer.Stop();
  720. LogoutTimer.Start();
  721. }
  722. }
  723. private void DisplayLogin()
  724. {
  725. LogoutTimer?.Stop();
  726. ViewModel.UserID = "";
  727. ViewModel.WorkOrder = "";
  728. var signinDialog = new SigninDialog();
  729. try
  730. {
  731. signinDialog.Owner = this;
  732. }
  733. catch
  734. {
  735. }
  736. signinDialog.ShowDialog();
  737. this.Focus();
  738. //SystemID.TryParse("DSWU601U0UD2PHD2111A001A0", out var systemID);
  739. //SystemIDScanReseived(systemID);
  740. if (signinDialog.DialogResult != true)
  741. {
  742. App.Current.Shutdown();
  743. }
  744. else
  745. {
  746. ViewModel.UserID = signinDialog.UserId;
  747. ViewModel.WorkOrder = signinDialog.WorkOrder;
  748. LogoutTimer?.Start();
  749. }
  750. }
  751. private int logoutCheckCnt = 0;
  752. private void LogoutTimer_Tick(object sender, EventArgs e)
  753. {
  754. LogoutTimer.Stop();
  755. if (ViewModel.IsUdatIng)
  756. {
  757. logoutCheckCnt = 0;
  758. LogoutTimer.Start();
  759. Console.WriteLine("LogoutTimer_Tick reset");
  760. return;
  761. }
  762. else if (++logoutCheckCnt > 10)
  763. {
  764. logoutCheckCnt = 0;
  765. DisplayLogin();
  766. Console.WriteLine("LogoutTimer_Tick DisplayLogin");
  767. }
  768. else
  769. {
  770. LogoutTimer.Start();
  771. Console.WriteLine("LogoutTimer_Tick continue");
  772. }
  773. }
  774. private void Logout_Click(object sender, RoutedEventArgs e)
  775. {
  776. DisplayLogin();
  777. }
  778. private void WorkOrder_TextChanged(object sender, TextChangedEventArgs e)
  779. {
  780. //ViewModel.IsInputCheckpassed = false;
  781. }
  782. private void WorkOrder_KeyDown(object sender, KeyEventArgs e)
  783. {
  784. //ViewModel.IsInputCheckpassed = true;
  785. if (e.Key == Key.Enter && ViewModel.SystemID != null)
  786. {
  787. SystemIDScanReseived(ViewModel.SystemID);
  788. }
  789. }
  790. private void SetUpdateStatus(UpdateStatus status)
  791. {
  792. if (status == UpdateStatus.Idel)
  793. {
  794. uxStatusContainer.Background = new SolidColorBrush(Colors.White);
  795. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  796. uxStatus.Content = (string)Application.Current.FindResource("StatusIdel");//"Idel";
  797. uxStatusBar.Content = "";
  798. }
  799. else if (status == UpdateStatus.Updating)
  800. {
  801. uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
  802. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  803. uxStatus.Content = (string)Application.Current.FindResource("StatusUpdating");//"Updating";
  804. }
  805. else if (status == UpdateStatus.Sucess)
  806. {
  807. uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
  808. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  809. uxStatus.Content = (string)Application.Current.FindResource("StatusSuccess");//"Success";
  810. uxStatusBar.Content = "";
  811. }
  812. else if (status == UpdateStatus.Fail)
  813. {
  814. uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
  815. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  816. uxStatus.Content = (string)Application.Current.FindResource("StatusFail");//"Fail";
  817. }
  818. }
  819. }
  820. }