FirmwareCheckVersionProcedure.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 
  2. using AwInitilizer.Assist;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace AwInitilizer.Procedure
  12. {
  13. public class FirmwareCheckVersionProcedure : ProcedureBase
  14. {
  15. public FirmwareCheckVersionProcedure() : base()
  16. {
  17. Name = "Firmware Version Check";
  18. Content = "Wait restart and check firmware versions";
  19. }
  20. internal override async Task<bool> Run()
  21. {
  22. //wait restart
  23. Logger.Print("Waiting restart..");
  24. bool response = false;
  25. int pollingCnt = 0;
  26. await Task.Delay(TimeSpan.FromMinutes(2));
  27. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  28. {
  29. await Task.Delay(TimeSpan.FromSeconds(15));
  30. response = await ChekCsuBootCompelete();
  31. if (response)
  32. break;
  33. }
  34. //timeout
  35. if (pollingCnt >= 56)
  36. {
  37. Logger.Print("Wait restart timeout", isError: true);
  38. InfoLog += "Wait restart timeout\n";
  39. return false;
  40. }
  41. //get version
  42. var versionPair = await GetVersion();
  43. var updatedList = UpdateData.FirmwareUpdateModels;
  44. //check all version
  45. foreach (var model in updatedList)
  46. {
  47. if (string.IsNullOrEmpty(model.Module) ||
  48. string.IsNullOrEmpty(model.Version))
  49. {
  50. continue;
  51. }
  52. if(versionPair.Keys.Contains(model.Module))
  53. {
  54. bool isVersionMatched = false;
  55. if (model.Module == "CsuRootFsFwRev")
  56. {
  57. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  58. }
  59. else
  60. {
  61. isVersionMatched = versionPair[model.Module] == model.Version;
  62. }
  63. if (isVersionMatched)
  64. {
  65. Logger.Print($"Model {model.Module} updated", isError: false);
  66. InfoLog += $"{Name}:updated success\n";
  67. LogPair.Add($"{model.Module}VersionCheck", "1");
  68. }
  69. else
  70. {
  71. Logger.Print($"Model {model.Module} version mismatch", isError: true);
  72. InfoLog += $"{model.Module}:Updated Version mismatched\n";
  73. LogPair.Add($"{model.Module}VersionCheck", "0");
  74. return false;
  75. }
  76. }
  77. else
  78. {
  79. //model name not found
  80. Logger.Print($"Model {model.Module} version not found", isError: true);
  81. InfoLog += $"Model {model.Module} version not found\n";
  82. LogPair.Add($"{model.Module}VersionCheck", "0");
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  89. {
  90. try
  91. {
  92. using (WebClientTimeout webClient = new WebClientTimeout())
  93. {
  94. NameValueCollection parameters = new NameValueCollection();
  95. parameters.Add("opt", "1");
  96. webClient.QueryString = parameters;
  97. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  98. // 使用 StreamReader 讀取 stream 內的字元
  99. using (StreamReader reader = new StreamReader(stream))
  100. {
  101. // 將 StreamReader 所讀到的字元轉為 string
  102. string request = reader.ReadToEnd();
  103. InfoLog += $"get version response:{request}\n";
  104. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  105. var toReturn = new Dictionary<string, string>();
  106. foreach (var pair in values)
  107. {
  108. if (pair.Value is string v)
  109. {
  110. toReturn.Add(pair.Key, v);
  111. }
  112. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  113. {
  114. try
  115. {
  116. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  117. for (int index = 0; index < versionList.Count; index++)
  118. {
  119. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  120. }
  121. }
  122. catch
  123. {
  124. }
  125. }
  126. }
  127. return toReturn;
  128. }
  129. }
  130. }
  131. catch (Exception e)
  132. {
  133. if (!isConnectTest)
  134. {
  135. Logger.Print("Get Version Failed", isError: true);
  136. Logger.Print(e.Message + "", isError: true);
  137. InfoLog += "Get Version Failed\n";
  138. InfoLog += e.Message;
  139. InfoLog += "\n";
  140. }
  141. return null;
  142. }
  143. }
  144. }
  145. }