FirmwareCheckVersionProcedure.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ReportLog.Add(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2));
  35. //timeout
  36. if (pollingCnt >= 56)
  37. {
  38. Logger.Print("Wait restart timeout", isError: true);
  39. InfoLog += "Wait restart timeout\n";
  40. return false;
  41. }
  42. //get version
  43. var versionPair = await GetVersion();
  44. var updatedList = UpdateData.FirmwareUpdateModels;
  45. //check all version
  46. foreach (var model in updatedList)
  47. {
  48. if (string.IsNullOrEmpty(model.Module) ||
  49. string.IsNullOrEmpty(model.Version))
  50. {
  51. continue;
  52. }
  53. var logPairNmae = $"{model.Module}VersionCheck";
  54. if (versionPair.Keys.Contains(model.Module))
  55. {
  56. ReportLog.Add(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  57. bool isVersionMatched = false;
  58. if (model.Module == "CsuRootFsFwRev")
  59. {
  60. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  61. }
  62. else
  63. {
  64. isVersionMatched = versionPair[model.Module] == model.Version;
  65. }
  66. if (isVersionMatched)
  67. {
  68. Logger.Print($"Model {model.Module} updated", isError: false);
  69. InfoLog += $"{Name}:updated success\n";
  70. if (!LogPair.Keys.Contains(logPairNmae))
  71. LogPair.Add(logPairNmae, "1");
  72. }
  73. else
  74. {
  75. Logger.Print($"Model {model.Module} version mismatch", isError: true);
  76. InfoLog += $"{model.Module}:Updated Version mismatched\n";
  77. if (!LogPair.Keys.Contains(logPairNmae))
  78. LogPair.Add(logPairNmae, "0");
  79. return false;
  80. }
  81. }
  82. else
  83. {
  84. //model name not found
  85. Logger.Print($"Model {model.Module} version not found", isError: true);
  86. InfoLog += $"Model {model.Module} version not found\n";
  87. if (!LogPair.Keys.Contains(logPairNmae))
  88. LogPair.Add(logPairNmae, "0");
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  95. {
  96. try
  97. {
  98. using (WebClientTimeout webClient = new WebClientTimeout())
  99. {
  100. NameValueCollection parameters = new NameValueCollection();
  101. parameters.Add("opt", "1");
  102. webClient.QueryString = parameters;
  103. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  104. // 使用 StreamReader 讀取 stream 內的字元
  105. using (StreamReader reader = new StreamReader(stream))
  106. {
  107. // 將 StreamReader 所讀到的字元轉為 string
  108. string request = reader.ReadToEnd();
  109. InfoLog += $"get version response:{request}\n";
  110. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  111. var toReturn = new Dictionary<string, string>();
  112. foreach (var pair in values)
  113. {
  114. if (pair.Value is string v)
  115. {
  116. toReturn.Add(pair.Key, v);
  117. }
  118. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  119. {
  120. try
  121. {
  122. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  123. for (int index = 0; index < versionList.Count; index++)
  124. {
  125. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  126. }
  127. }
  128. catch
  129. {
  130. }
  131. }
  132. }
  133. return toReturn;
  134. }
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. if (!isConnectTest)
  140. {
  141. Logger.Print("Get Version Failed", isError: true);
  142. Logger.Print(e.Message + "", isError: true);
  143. InfoLog += "Get Version Failed\n";
  144. InfoLog += e.Message;
  145. InfoLog += "\n";
  146. }
  147. return null;
  148. }
  149. }
  150. }
  151. }