MainWindow.xaml.cs 37 KB

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