MainWindow.xaml.cs 37 KB

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