FirmwareCheckVersionProcedure.cs 7.2 KB

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