FirmwareCheckVersionProcedure.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. await Task.Delay(5_000);
  63. //get version
  64. var versionPair = await GetVersion();
  65. var updatedList = UpdateData.FirmwareUpdateModels;
  66. if (versionPair == null)
  67. {
  68. Error = ErrorType.VersionCheckFail;
  69. return false;
  70. }
  71. //check all version
  72. foreach (var model in updatedList)
  73. {
  74. var checkResult = CheckModelbyList(model, versionPair);
  75. if (!checkResult)
  76. {
  77. Error = ErrorType.VersionCheckFail;
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. private bool CheckModelbyList(Model.FirmwareUpdateModel model, Dictionary<string,string> versionPair)
  84. {
  85. if (string.IsNullOrEmpty(model.Module))
  86. {
  87. return true;
  88. }
  89. var logPairName = $"{model.Module}VersionCheck";
  90. if (versionPair.Keys.Contains(model.Module))
  91. {
  92. LogWriter.Log(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  93. bool isVersionMatched = false;
  94. if (model.Module == "CsuRootFsFwRev" ||
  95. model.Module.StartsWith("DDCsuRootFsFwRev"))
  96. {
  97. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  98. }
  99. else
  100. {
  101. isVersionMatched = versionPair[model.Module] == model.Version;
  102. }
  103. if (isVersionMatched)
  104. {
  105. LogWriter.Report(logPairName,"success");
  106. return true;
  107. }
  108. else
  109. {
  110. LogWriter.Report(logPairName, "fail", isError: true);
  111. return false;
  112. }
  113. }
  114. else if (model.Module.ToLower() == "dtb")
  115. {
  116. //pass
  117. return true;
  118. }
  119. else
  120. {
  121. //model name not found
  122. LogWriter.Report(logPairName, "fail", isError: true);
  123. LogWriter.Log($"Model {model.Module} version not found");
  124. return false;
  125. }
  126. }
  127. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  128. {
  129. try
  130. {
  131. var result = await EvApi.GetVersion();
  132. LogWriter.Log($"get version response:{result.Response}", isDebugLog: true);
  133. return result.Result;
  134. }
  135. catch (Exception e)
  136. {
  137. if (!isConnectTest)
  138. {
  139. LogWriter.Log("Get Version Failed");
  140. LogWriter.Log(e.Message, isDebugLog: true);
  141. }
  142. return null;
  143. }
  144. }
  145. }
  146. }