FirmwareCheckVersionProcedure.cs 6.4 KB

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