FirmwareCheckVersionProcedure.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 
  2. using AwInitilizer.Assist;
  3. using CsuWebApiLib;
  4. using InitializerModel;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.Specialized;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace AwInitilizer.Procedure.FirmwareCheckVersion
  14. {
  15. public enum ErrorType
  16. {
  17. None,
  18. RestartTimeout,
  19. VersionCheckFail,
  20. }
  21. public enum LogEvent
  22. {
  23. }
  24. public class FirmwareCheckVersionProcedure : ProcedureBase
  25. {
  26. public ErrorType Error { get; set; } = ErrorType.None;
  27. private ProcedureLog.LogWriter<FirmwareCheckVersionProcedure, LogEvent> LogWriter;
  28. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  29. {
  30. };
  31. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  32. {
  33. };
  34. public FirmwareCheckVersionProcedure() : base()
  35. {
  36. Name = "Firmware Version Check";
  37. Content = "Wait restart and check firmware versions";
  38. LogWriter = new ProcedureLog.LogWriter<FirmwareCheckVersionProcedure, LogEvent>(this)
  39. {
  40. ReportPair = ReportDict,
  41. LogPair = LogDict
  42. };
  43. }
  44. internal override Task<bool> Run()
  45. {
  46. if (UpdateData.IsIdleCheckPass)
  47. {
  48. return RunWithNoBootCheck();
  49. }
  50. return RunWithBootCheck();
  51. }
  52. internal async Task<bool> RunWithNoBootCheck()
  53. {
  54. await Task.Delay(TimeSpan.FromMinutes(2));
  55. await Task.Delay(5_000);
  56. int pollingCnt = 0;
  57. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  58. {
  59. await Task.Delay(TimeSpan.FromSeconds(15));
  60. var checkVersionResult = await CheckVersion();
  61. if (checkVersionResult)
  62. {
  63. return true;
  64. }
  65. }
  66. Error = ErrorType.VersionCheckFail;
  67. return false;
  68. }
  69. internal async Task<bool> RunWithBootCheck()
  70. {
  71. //wait restart
  72. bool response = false;
  73. int pollingCnt = 0;
  74. await Task.Delay(TimeSpan.FromMinutes(2));
  75. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  76. {
  77. await Task.Delay(TimeSpan.FromSeconds(15));
  78. response = await ChekCsuBootCompelete();
  79. if (response)
  80. break;
  81. }
  82. LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2));
  83. //timeout
  84. if (pollingCnt >= 56)
  85. {
  86. Error = ErrorType.RestartTimeout;
  87. return false;
  88. }
  89. await Task.Delay(5_000);
  90. //get version
  91. var versionPair = await GetVersion();
  92. var updatedList = UpdateData.FirmwareUpdateModels;
  93. if (versionPair == null)
  94. {
  95. Error = ErrorType.VersionCheckFail;
  96. return false;
  97. }
  98. //check all version
  99. foreach (var model in updatedList)
  100. {
  101. var checkResult = CheckModelbyList(model, versionPair);
  102. if (!checkResult)
  103. {
  104. Error = ErrorType.VersionCheckFail;
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. private async Task<bool> CheckVersion()
  111. {
  112. //get version
  113. var versionPair = await GetVersion();
  114. var updatedList = UpdateData.FirmwareUpdateModels;
  115. if (versionPair == null)
  116. {
  117. //Error = ErrorType.VersionCheckFail;
  118. return false;
  119. }
  120. foreach (var model in updatedList)
  121. {
  122. var checkResult = CheckModelbyList(model, versionPair);
  123. if (!checkResult)
  124. {
  125. //Error = ErrorType.VersionCheckFail;
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. private bool CheckModelbyList(FirmwareUpdateModel model, Dictionary<string,string> versionPair)
  132. {
  133. if (string.IsNullOrEmpty(model.Module))
  134. {
  135. return true;
  136. }
  137. var logPairName = $"{model.Module}VersionCheck";
  138. if (versionPair.Keys.Contains(model.Module))
  139. {
  140. LogWriter.Log(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  141. bool isVersionMatched = false;
  142. if (model.Module == "CsuRootFsFwRev" ||
  143. model.Module.StartsWith("DDCsuRootFsFwRev"))
  144. {
  145. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  146. }
  147. else
  148. {
  149. isVersionMatched = versionPair[model.Module] == model.Version;
  150. }
  151. if (isVersionMatched)
  152. {
  153. LogWriter.Report(logPairName,"success");
  154. return true;
  155. }
  156. else
  157. {
  158. LogWriter.Report(logPairName, "fail", isError: true);
  159. return false;
  160. }
  161. }
  162. else if (model.Module.ToLower() == "dtb")
  163. {
  164. //pass
  165. return true;
  166. }
  167. else
  168. {
  169. //model name not found
  170. LogWriter.Report(logPairName, "fail", isError: true);
  171. LogWriter.Log($"Model {model.Module} version not found");
  172. return false;
  173. }
  174. }
  175. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  176. {
  177. try
  178. {
  179. var result = await EvApi.GetVersion();
  180. LogWriter.Log($"get version response:{result.Response}", isDebugLog: true);
  181. return result.Result;
  182. }
  183. catch (Exception e)
  184. {
  185. if (!isConnectTest)
  186. {
  187. LogWriter.Log("Get Version Failed");
  188. LogWriter.Log(e.Message, isDebugLog: true);
  189. }
  190. return null;
  191. }
  192. }
  193. }
  194. }