MainWindow.xaml.cs 39 KB

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