FirmwareCheckVersionProcedure.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.FirmwareCheckVersion
  12. {
  13. public enum ErrorType
  14. {
  15. None,
  16. RestartTimeout,
  17. VersionCheckFail,
  18. }
  19. public enum LogEvent
  20. {
  21. }
  22. public class FirmwareCheckVersionProcedure : ProcedureBase
  23. {
  24. public ErrorType Error { get; set; } = ErrorType.None;
  25. private ProcedureLog.LogWriter<FirmwareCheckVersionProcedure, LogEvent> LogWriter;
  26. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  27. {
  28. };
  29. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  30. {
  31. };
  32. public FirmwareCheckVersionProcedure() : base()
  33. {
  34. Name = "Firmware Version Check";
  35. Content = "Wait restart and check firmware versions";
  36. LogWriter = new ProcedureLog.LogWriter<FirmwareCheckVersionProcedure, LogEvent>(this)
  37. {
  38. ReportPair = ReportDict,
  39. LogPair = LogDict
  40. };
  41. }
  42. internal override async Task<bool> Run()
  43. {
  44. //wait restart
  45. bool response = false;
  46. int pollingCnt = 0;
  47. await Task.Delay(TimeSpan.FromMinutes(2));
  48. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  49. {
  50. await Task.Delay(TimeSpan.FromSeconds(15));
  51. response = await ChekCsuBootCompelete();
  52. if (response)
  53. break;
  54. }
  55. LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2));
  56. //timeout
  57. if (pollingCnt >= 56)
  58. {
  59. Error = ErrorType.RestartTimeout;
  60. return false;
  61. }
  62. //get version
  63. var versionPair = await GetVersion();
  64. var updatedList = UpdateData.FirmwareUpdateModels;
  65. //check all version
  66. foreach (var model in updatedList)
  67. {
  68. var checkResult = CheckModelbyList(model, versionPair);
  69. if (!checkResult)
  70. {
  71. Error = ErrorType.VersionCheckFail;
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. private bool CheckModelbyList(Model.FirmwareUpdateModel model, Dictionary<string,string> versionPair)
  78. {
  79. if (string.IsNullOrEmpty(model.Module))
  80. {
  81. return true;
  82. }
  83. var logPairName = $"{model.Module}VersionCheck";
  84. if (versionPair.Keys.Contains(model.Module))
  85. {
  86. LogWriter.Log(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  87. bool isVersionMatched = false;
  88. if (model.Module == "CsuRootFsFwRev")
  89. {
  90. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  91. }
  92. else
  93. {
  94. isVersionMatched = versionPair[model.Module] == model.Version;
  95. }
  96. if (isVersionMatched)
  97. {
  98. LogWriter.Report(logPairName,"success");
  99. return true;
  100. }
  101. else
  102. {
  103. LogWriter.Report(logPairName, "fail", isError: true);
  104. return false;
  105. }
  106. }
  107. else if (model.Module.ToLower() == "dtb")
  108. {
  109. //pass
  110. return true;
  111. }
  112. else
  113. {
  114. //model name not found
  115. LogWriter.Report(logPairName, "fail", isError: true);
  116. LogWriter.Log($"Model {model.Module} version not found");
  117. return false;
  118. }
  119. }
  120. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  121. {
  122. try
  123. {
  124. using (WebClientTimeout webClient = new WebClientTimeout())
  125. {
  126. NameValueCollection parameters = new NameValueCollection();
  127. parameters.Add("opt", "1");
  128. webClient.QueryString = parameters;
  129. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  130. // 使用 StreamReader 讀取 stream 內的字元
  131. using (StreamReader reader = new StreamReader(stream))
  132. {
  133. // 將 StreamReader 所讀到的字元轉為 string
  134. string request = reader.ReadToEnd();
  135. LogWriter.Log($"get version response:{request}", isDebugLog: true);
  136. //InfoLog += $"get version response:{request}\n";
  137. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  138. var toReturn = new Dictionary<string, string>();
  139. foreach (var pair in values)
  140. {
  141. if (pair.Value is string v)
  142. {
  143. toReturn.Add(pair.Key, v);
  144. }
  145. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  146. {
  147. try
  148. {
  149. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  150. for (int index = 0; index < versionList.Count; index++)
  151. {
  152. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  153. }
  154. }
  155. catch
  156. {
  157. }
  158. }
  159. }
  160. return toReturn;
  161. }
  162. }
  163. }
  164. catch (Exception e)
  165. {
  166. if (!isConnectTest)
  167. {
  168. LogWriter.Log("Get Version Failed");
  169. LogWriter.Log(e.Message, isDebugLog: true);
  170. }
  171. return null;
  172. }
  173. }
  174. }
  175. }