MainWindow.xaml.cs 40 KB

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