MainWindow.xaml.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. this.Focus();
  807. //SystemID.TryParse("DWWU301J0UD1PHD3000A001A0", out var systemID);
  808. //SystemIDScanReseived(systemID);
  809. if (signinDialog.DialogResult != true)
  810. {
  811. App.Current.Shutdown();
  812. }
  813. else
  814. {
  815. ViewModel.UserID = signinDialog.UserId;
  816. ViewModel.WorkOrder = signinDialog.WorkOrder;
  817. LogoutTimer?.Start();
  818. }
  819. }
  820. private int logoutCheckCnt = 0;
  821. private void LogoutTimer_Tick(object sender, EventArgs e)
  822. {
  823. LogoutTimer.Stop();
  824. if (ViewModel.IsUdatIng)
  825. {
  826. logoutCheckCnt = 0;
  827. LogoutTimer.Start();
  828. Console.WriteLine("LogoutTimer_Tick reset");
  829. return;
  830. }
  831. else if (++logoutCheckCnt > 10)
  832. {
  833. logoutCheckCnt = 0;
  834. DisplayLogin();
  835. Console.WriteLine("LogoutTimer_Tick DisplayLogin");
  836. }
  837. else
  838. {
  839. LogoutTimer.Start();
  840. Console.WriteLine("LogoutTimer_Tick continue");
  841. }
  842. }
  843. private void Logout_Click(object sender, RoutedEventArgs e)
  844. {
  845. DisplayLogin();
  846. }
  847. private void WorkOrder_TextChanged(object sender, TextChangedEventArgs e)
  848. {
  849. //ViewModel.IsInputCheckpassed = false;
  850. }
  851. private void WorkOrder_KeyDown(object sender, KeyEventArgs e)
  852. {
  853. //ViewModel.IsInputCheckpassed = true;
  854. if (e.Key == Key.Enter && ViewModel.SystemID != null)
  855. {
  856. SystemIDScanReseived(ViewModel.SystemID);
  857. }
  858. }
  859. private void SetUpdateStatus(UpdateStatus status)
  860. {
  861. if(status == UpdateStatus.Idel)
  862. {
  863. uxStatusContainer.Background = new SolidColorBrush(Colors.White);
  864. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  865. uxStatus.Content = (string)Application.Current.FindResource("StatusIdel");//"Idel";
  866. uxStatusBar.Content = "";
  867. }
  868. else if (status == UpdateStatus.Updating)
  869. {
  870. uxStatusContainer.Background = new SolidColorBrush(Colors.SkyBlue);
  871. uxStatus.Foreground = new SolidColorBrush(Colors.Black);
  872. uxStatus.Content = (string)Application.Current.FindResource("StatusUpdating");//"Updating";
  873. }
  874. else if (status == UpdateStatus.Sucess)
  875. {
  876. uxStatusContainer.Background = new SolidColorBrush(Colors.Green);
  877. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  878. uxStatus.Content = (string)Application.Current.FindResource("StatusSuccess");//"Success";
  879. uxStatusBar.Content = "";
  880. }
  881. else if (status == UpdateStatus.Fail)
  882. {
  883. uxStatusContainer.Background = new SolidColorBrush(Colors.Red);
  884. uxStatus.Foreground = new SolidColorBrush(Colors.White);
  885. uxStatus.Content = (string)Application.Current.FindResource("StatusFail");//"Fail";
  886. }
  887. }
  888. }
  889. }