MainWindow.xaml.cs 36 KB

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