FirmwareCheckVersionProcedure.cs 5.0 KB

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