MainWindow.xaml.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. }
  413. private void ReportMESLog(Dictionary<string, string> logPairs)
  414. {
  415. //build header
  416. Dictionary<string, int> valuePairs = new Dictionary<string, int>();
  417. foreach (var pair in logPairs)
  418. {
  419. if(int.TryParse(pair.Value,out int val))
  420. {
  421. valuePairs.Add(pair.Key,val);
  422. }
  423. else if(pair.Value.ToLower().Contains("fail"))
  424. {
  425. valuePairs.Add(pair.Key, 0);
  426. }
  427. else if (pair.Value.ToLower().Contains("success"))
  428. {
  429. valuePairs.Add(pair.Key, 1);
  430. }
  431. else
  432. {
  433. valuePairs.Add(string.Format("{0}:{1}", pair.Key, pair.Value),1);
  434. }
  435. }
  436. //register Header
  437. var codePair = new Dictionary<string, string>();
  438. var model = ViewModel.SystemID.ModelName.ToString();
  439. foreach(var key in valuePairs.Keys)
  440. {
  441. var code = DLL.SajetConnect.SajetTransRegisterHeader(model,key);
  442. if (string.IsNullOrEmpty(code))
  443. continue;
  444. codePair.Add(key, code);
  445. }
  446. //report value
  447. var reportResult = DLL.SajetConnect.SajetTransReport(valuePairs, codePair);
  448. }
  449. private void CreateLogFile()
  450. {
  451. var fileName = ViewModel.SystemID + ViewModel.SerialNumber + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
  452. var folderName = "Log";
  453. var filePath = Path.Combine(folderName, fileName);
  454. string content = "";
  455. if (File.Exists(fileName))
  456. {
  457. File.Delete(fileName);
  458. }
  459. if (!Directory.Exists("Log"))
  460. {
  461. Directory.CreateDirectory("Log");
  462. }
  463. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  464. StreamWriter fileWriter = new StreamWriter(fileStream);
  465. fileWriter.WriteLine("Barcode");
  466. fileWriter.WriteLine($"Model name:{ ViewModel.ModelName } , Serial number: { ViewModel.SerialNumber }");
  467. fileWriter.WriteLine("==========================");
  468. fileWriter.WriteLine("Setting file");
  469. fileWriter.WriteLine($"Model name:{ ViewModel.SettingModelName}");
  470. fileWriter.WriteLine($"4G Module Version:{ ViewModel.FourGenModuleVersion}");
  471. fileWriter.WriteLine($"Is sim insert:{ ViewModel.IsSimInsert}");
  472. fileWriter.WriteLine($"sim ICCID:{ ViewModel.ICCID}");
  473. fileWriter.WriteLine($"sim IMSI:{ ViewModel.IMSI}");
  474. foreach (var model in ViewModel.FirmwareUpdateModels)
  475. {
  476. fileWriter.WriteLine($"{model.Module} version:{ model.Version}");
  477. }
  478. var procedureList = ViewModel.UpdateProcedure.ToList();
  479. for (int procedureIndex = 0; procedureIndex < procedureList.Count; procedureIndex++)
  480. {
  481. var procedure = procedureList[procedureIndex];
  482. fileWriter.WriteLine("==========================");
  483. fileWriter.WriteLine(procedure.Name);
  484. fileWriter.WriteLine("Is Activated:" + (procedure.IsActivated ? "Yes" : "No"));
  485. fileWriter.WriteLine("Status:" + procedure.Status.ToString());
  486. fileWriter.WriteLine(procedure.InfoLog);
  487. }
  488. fileWriter.WriteLine("==========================");
  489. fileWriter.Close();
  490. fileStream.Close();
  491. }
  492. private void CreateUploadCustomterLogFile()
  493. {
  494. var fileName = ViewModel.SystemID + ViewModel.SerialNumber +"_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
  495. var folderName = "CustomerLog";
  496. var filePath = Path.Combine(folderName, fileName);
  497. if (File.Exists(fileName))
  498. {
  499. File.Delete(fileName);
  500. }
  501. if (!Directory.Exists("CustomerLog"))
  502. {
  503. Directory.CreateDirectory("CustomerLog");
  504. }
  505. CreateCustomerLog(filePath);
  506. }
  507. private void CreateCustomerLog(string filePath)
  508. {
  509. FileStream fileStream = new FileStream(filePath, FileMode.Create);
  510. StreamWriter fileWriter = new StreamWriter(fileStream);
  511. string borderString = "================================================================================";
  512. //fileWriter.WriteLine(" Switch Mode Power Supply");
  513. fileWriter.WriteLine(" Button Test and Firmware update");
  514. fileWriter.WriteLine(" test report");
  515. fileWriter.WriteLine("");
  516. fileWriter.WriteLine("Test Program Name : {0,-30} ", "FirmwareUpdate");
  517. fileWriter.WriteLine("Serial No : {0,-30}System Time : {1}", ViewModel.SystemID.ToString(),DateTime.Now.ToString());
  518. fileWriter.WriteLine("Model Name : {0,-30}Elapsed Time : {1}", ViewModel.ModelName, ViewModel.UpdateElpased.ToString(@"hh\:mm\:ss"));
  519. fileWriter.WriteLine("LOT Number : Environment : ");
  520. fileWriter.WriteLine("Order Number : Inspector : {0}", ViewModel.UserID);
  521. fileWriter.WriteLine("Customer : Test Result : {0}", ViewModel.UpdateProcedure.Last().Status == ProcedureStatus.PASS? "PASS":"FAIL");
  522. fileWriter.WriteLine("");
  523. fileWriter.WriteLine(borderString);
  524. for (int procedureIndex = 0; procedureIndex < ViewModel.UpdateProcedure.Count; procedureIndex++)
  525. {
  526. var procedure = ViewModel.UpdateProcedure[procedureIndex];
  527. fileWriter.WriteLine("STEP.{0} : {1,-63}{2,6}", procedureIndex+1, procedure.Name, procedure.Status.ToString());
  528. foreach(var reportLog in procedure.ReportLog)
  529. {
  530. fileWriter.WriteLine(reportLog);
  531. }
  532. fileWriter.WriteLine(borderString);
  533. }
  534. fileWriter.Close();
  535. fileStream.Close();
  536. }
  537. public void Print(string msg, bool isError = false)
  538. {
  539. DLL.SajetConnect.SajetTransLog(msg, isError);
  540. Dispatcher.Invoke(() => {
  541. Span line = new Span();
  542. line.Inlines.Add(msg + "\n");
  543. Span.SetForeground(line, isError ? Brushes.Red : Brushes.Green);
  544. //uxTerminal.Inlines.Add(line);
  545. //uxTerminalScroller.ScrollToEnd();
  546. });
  547. Console.WriteLine(msg);
  548. }
  549. #endregion
  550. private bool LoadConfigBySystemID(SystemID systemID)
  551. {
  552. string settingRootFolder;
  553. try
  554. {
  555. var defaultPath = Properties.Settings.Default.FirmwareRoot;
  556. if(string.IsNullOrEmpty(defaultPath))
  557. {
  558. defaultPath = ".\\";
  559. }
  560. settingRootFolder = Path.GetFullPath(defaultPath);
  561. }
  562. catch
  563. {
  564. //HintDialog.ShowMessage("Firmware root path ERROR");
  565. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigRootFolderNotfoundAlert"));
  566. return false;
  567. }
  568. if (!Directory.Exists(settingRootFolder))
  569. {
  570. //HintDialog.ShowMessage("Firmware root path not exist");
  571. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigFolderNotfoundAlert"));
  572. return false;
  573. }
  574. string modelDirectoy = Path.Combine(settingRootFolder, systemID.ModelName.ToString());
  575. if (!Directory.Exists(modelDirectoy))
  576. {
  577. //HintDialog.ShowMessage("Model firmware root path not exist");
  578. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelFolderNotfoundAlert"));
  579. return false;
  580. }
  581. string modelSeettingFilePath = Path.Combine(modelDirectoy, systemID.ModelName.ToString() + ".ini");
  582. if (!File.Exists(modelSeettingFilePath))
  583. {
  584. //HintDialog.ShowMessage("Model firmware setting not exist");
  585. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitNotfoundAlert"));
  586. return false;
  587. }
  588. //check format
  589. var settingString = File.ReadAllText(modelSeettingFilePath);
  590. SettingConfig setting;
  591. try
  592. {
  593. setting = JsonConvert.DeserializeObject<SettingConfig>(settingString);
  594. }
  595. catch
  596. {
  597. //HintDialog.ShowMessage("Setting file ERROR");
  598. HintDialog.ShowMessage((string)Application.Current.FindResource("LoadConfigModelInitFormatErrorAlert"));
  599. return false;
  600. }
  601. if (CheckSettingConfig(System.IO.Path.GetDirectoryName(modelSeettingFilePath), ref setting))
  602. {
  603. ViewModel.SystemID = systemID;
  604. ViewModel.ModelName = systemID.ModelName.ToString();
  605. ViewModel.SerialNumber = systemID.SerialNumber;
  606. ViewModel.SettingModelName = setting.ModelName;
  607. ViewModel.SettingFileName = Path.GetFileName(modelSeettingFilePath);
  608. ViewModel.FourGenModuleVersion = setting.FourGenModuleVersion;
  609. ViewModel.IsSimInsert = setting.IsSimInsert;
  610. ViewModel.ICCID = setting.ICCID;
  611. ViewModel.IMSI = setting.IMSI;
  612. ViewModel.FirmwareUpdateModels = setting.FirmwareUpdateList
  613. .Where(x => !string.IsNullOrEmpty(x.Module) && !string.IsNullOrEmpty(x.Version) && !string.IsNullOrEmpty(x.FirmwareFileName)
  614. ).ToList();
  615. UpdateProcedure();
  616. ViewModel.IsInputCheckpassed = CheckInputData();
  617. }
  618. return true;
  619. }
  620. private void UpdateProcedure()
  621. {
  622. List<ProcedureBase> procedures = new List<ProcedureBase>();
  623. //init intilize procedure list
  624. procedures.Add(new ButtonStatusCheckPorcedure());
  625. procedures.Add(new BasicInfoUpdateProcedure());
  626. procedures.Add(new FourGenModuleCheckProcedure());
  627. //for (int firemwareIndex = 0; firemwareIndex < ViewModel.FirmwareUpdateModels.Count ; firemwareIndex++)
  628. //{
  629. // //procedures.Add(new FirmwareUpdateProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  630. // procedures.Add(new FirmwareUploadProcedure(ViewModel.FirmwareUpdateModels[firemwareIndex]));
  631. //}
  632. procedures.Add(new FirmwareBundleUploadProcedure());
  633. procedures.Add(new FirmwareCheckVersionProcedure());
  634. procedures.Add(new RestarttoIdelProcedure());
  635. procedures.Add(new VersionLogProcedure());
  636. ViewModel.UpdateProcedure.Clear();
  637. foreach(var p in procedures)
  638. {
  639. ViewModel.UpdateProcedure.Add(p);
  640. }
  641. //uxProcedureDataGrid.ItemsSource = procedures;
  642. }
  643. private bool CheckSettingConfig(string folderPath, ref SettingConfig setting)
  644. {
  645. if (setting == null)
  646. return false;
  647. bool isCheckPassed = true;
  648. if (string.IsNullOrEmpty(setting.ModelName))
  649. {
  650. //HintDialog.ShowMessage("ModelName is requred");
  651. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameEmptyAlert"));
  652. isCheckPassed = false;
  653. }
  654. else if(!ModelName.TryParse(setting.ModelName,out _))
  655. {
  656. //HintDialog.ShowMessage("ModelName format Error");
  657. HintDialog.ShowMessage((string)Application.Current.FindResource("InitModelNameErrorAlert"));
  658. isCheckPassed = false;
  659. }
  660. if (setting.IsSimInsert)
  661. {
  662. if (string.IsNullOrEmpty(setting.ICCID))
  663. {
  664. //HintDialog.ShowMessage("ICCID should not empty while IsSimInsert is set");
  665. HintDialog.ShowMessage((string)Application.Current.FindResource("InitIccidEmptyAlert"));
  666. isCheckPassed = false;
  667. }
  668. if (string.IsNullOrEmpty(setting.IMSI))
  669. {
  670. //HintDialog.ShowMessage("IMSI should not empty while IsSimInsert is set");
  671. HintDialog.ShowMessage((string)Application.Current.FindResource("InitImsiEmptyAlert"));
  672. isCheckPassed = false;
  673. }
  674. }
  675. if(setting.FirmwareUpdateList!=null)
  676. {
  677. for (int firmwareIndex = 0; firmwareIndex < setting.FirmwareUpdateList.Count; firmwareIndex++)
  678. {
  679. var model = setting.FirmwareUpdateList[firmwareIndex];
  680. bool isVersionVaild = !string.IsNullOrEmpty(model.Version);
  681. bool isFileNameVaild = !string.IsNullOrEmpty(model.FirmwareFileName);
  682. bool isNoduleNameVaild = !string.IsNullOrEmpty(model.Module);
  683. if(!isNoduleNameVaild)
  684. {
  685. //HintDialog.ShowMessage("Firmware module name should not empty");
  686. HintDialog.ShowMessage((string)Application.Current.FindResource("InitFirmwareNameEmptyAlert"));
  687. isCheckPassed = false;
  688. }
  689. if (isVersionVaild || isFileNameVaild)
  690. {
  691. if (!isVersionVaild)
  692. {
  693. //HintDialog.ShowMessage($"Version should not empty while {model.Module} firmware is set");
  694. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareVersionEmptyAlert"),model.Module));
  695. isCheckPassed = false;
  696. }
  697. if (!isFileNameVaild)
  698. {
  699. //HintDialog.ShowMessage($"File name should not empty while {model.Module} version is set");
  700. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileEmptyAlert"), model.Module));
  701. isCheckPassed = false;
  702. }
  703. if (isVersionVaild && isFileNameVaild)
  704. {
  705. var filePath = System.IO.Path.Combine(folderPath, model.FirmwareFileName);
  706. //check file exist
  707. if (!File.Exists(filePath))
  708. {
  709. //HintDialog.ShowMessage($"{model.Module} Firemware file is missing");
  710. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileMissingAlert"), model.Module));
  711. isCheckPassed = false;
  712. }
  713. else
  714. {
  715. try
  716. {
  717. using (var fs = File.OpenRead(filePath))
  718. {
  719. byte[] systemIDBytes = new byte[16];
  720. if (fs.Read(systemIDBytes, 0, 16) == 16)
  721. {
  722. if (systemIDBytes.ToList().Contains(0x00))
  723. {
  724. int endIndex = Array.FindIndex(systemIDBytes, (x) => { return x == 0x00; });
  725. //int endIndex = parameter.FindIndex((x) => { return x == 0x00; });
  726. if (endIndex != default && endIndex != -1)
  727. {
  728. systemIDBytes = systemIDBytes.Take(endIndex).ToArray();
  729. }
  730. }
  731. if (ModelName.TryParse(systemIDBytes, out var modelName))
  732. {
  733. if (modelName.ToString() != setting.ModelName)
  734. {
  735. //HintDialog.ShowMessage($"{model.Module} Firemware and ModelName is Mismatched");
  736. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderMismatchAlert"), model.Module));
  737. isCheckPassed = false;
  738. }
  739. }
  740. else
  741. {
  742. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  743. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  744. isCheckPassed = false;
  745. }
  746. }
  747. else
  748. {
  749. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  750. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  751. isCheckPassed = false;
  752. }
  753. //byte[] imgType = new byte[4];
  754. //if (fs.Read(imgType, 0, 4) == 4)
  755. //{
  756. // if (!imgType.SequenceEqual(new byte[] { 0x10, 0x00, 0x00, 0x04, }))
  757. // {
  758. // HintDialog.ShowMessage($"{model.Module} Firemware type ERROR");
  759. // isCheckPassed = false;
  760. // }
  761. //}
  762. //else
  763. //{
  764. // HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  765. // isCheckPassed = false;
  766. //}
  767. }
  768. }
  769. catch
  770. {
  771. //HintDialog.ShowMessage($"{model.Module} Firemware header ERROR");
  772. HintDialog.ShowMessage(string.Format((string)Application.Current.FindResource("InitFirmwareFileHeaderFormatAlert"), model.Module));
  773. isCheckPassed = false;
  774. }
  775. model.FirmwareFileName = filePath;
  776. }
  777. }
  778. }
  779. }
  780. }
  781. return isCheckPassed;
  782. }
  783. private void ResetLogoutTimer()
  784. {
  785. if(LogoutTimer!=null)
  786. {
  787. logoutCheckCnt = 0;
  788. LogoutTimer.Stop();
  789. LogoutTimer.Start();
  790. }
  791. }
  792. private void DisplayLogin()
  793. {
  794. LogoutTimer?.Stop();
  795. ViewModel.UserID = "";
  796. ViewModel.WorkOrder = "";
  797. var signinDialog = new SigninDialog();
  798. try
  799. {
  800. signinDialog.Owner = this;
  801. }
  802. catch
  803. {
  804. }
  805. signinDialog.ShowDialog();
  806. //SystemID.TryParse("DWWU301J0UD1PHD3000A001A0", out var systemID);
  807. //SystemIDScanReseived(systemID);
  808. if (signinDialog.DialogResult != true)
  809. {
  810. App.Current.Shutdown();
  811. }
  812. else
  813. {
  814. ViewModel.UserID = signinDialog.UserId;
  815. ViewModel.WorkOrder = signinDialog.WorkOrder;
  816. LogoutTimer?.Start();
  817. }
  818. }
  819. private int logoutCheckCnt = 0;
  820. private void LogoutTimer_Tick(object sender, EventArgs e)
  821. {
  822. LogoutTimer.Stop();
  823. if (ViewModel.IsUdatIng)
  824. {
  825. logoutCheckCnt = 0;
  826. LogoutTimer.Start();
  827. Console.WriteLine("LogoutTimer_Tick reset");
  828. return;
  829. }
  830. else if (++logoutCheckCnt > 10)
  831. {
  832. logoutCheckCnt = 0;
  833. DisplayLogin();
  834. Console.WriteLine("LogoutTimer_Tick DisplayLogin");
  835. }
  836. else
  837. {
  838. LogoutTimer.Start();
  839. Console.WriteLine("LogoutTimer_Tick continue");
  840. }
  841. }
  842. private void Logout_Click(object sender, RoutedEventArgs e)
  843. {
  844. DisplayLogin();
  845. }
  846. private void WorkOrder_TextChanged(object sender, TextChangedEventArgs e)
  847. {
  848. //ViewModel.IsInputCheckpassed = false;
  849. }
  850. private void WorkOrder_KeyDown(object sender, KeyEventArgs e)
  851. {
  852. //ViewModel.IsInputCheckpassed = true;
  853. if (e.Key == Key.Enter && ViewModel.SystemID != null)
  854. {
  855. SystemIDScanReseived(ViewModel.SystemID);
  856. }
  857. }
  858. private void SetUpdateStatus(UpdateStatus status)
  859. {
  860. if(status == UpdateStatus.Idel)
  861. {
  862. uxStatusContainer.Background = new SolidColorBrush(Colors.White);
  863. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  864. uxStatus.Content = (string)Application.Current.FindResource("StatusIdel");//"Idel";
  865. uxStatusBar.Content = "";
  866. }
  867. else if (status == UpdateStatus.Updating)
  868. {
  869. uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
  870. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  871. uxStatus.Content = (string)Application.Current.FindResource("StatusUpdating");//"Updating";
  872. }
  873. else if (status == UpdateStatus.Sucess)
  874. {
  875. uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
  876. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  877. uxStatus.Content = (string)Application.Current.FindResource("StatusSuccess");//"Success";
  878. uxStatusBar.Content = "";
  879. }
  880. else if (status == UpdateStatus.Fail)
  881. {
  882. uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
  883. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  884. uxStatus.Content = (string)Application.Current.FindResource("StatusFail");//"Fail";
  885. }
  886. }
  887. }
  888. }