MainWindow.xaml.cs 40 KB

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