MainWindow.xaml.cs 40 KB

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