FirmwareCheckVersionProcedure.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //Logger.Print("Waiting 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. //ReportLog.Add(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2));
  58. //timeout
  59. if (pollingCnt >= 56)
  60. {
  61. Error = ErrorType.RestartTimeout;
  62. //Logger.Print("Wait restart timeout", isError: true);
  63. //InfoLog += "Wait restart timeout\n";
  64. return false;
  65. }
  66. //get version
  67. var versionPair = await GetVersion();
  68. var updatedList = UpdateData.FirmwareUpdateModels;
  69. //check all version
  70. foreach (var model in updatedList)
  71. {
  72. var checkResult = CheckModelbyList(model, versionPair);
  73. if (!checkResult)
  74. Error = ErrorType.VersionCheckFail;
  75. return false;
  76. }
  77. return true;
  78. }
  79. private bool CheckModelbyList(Model.FirmwareUpdateModel model, Dictionary<string,string> versionPair)
  80. {
  81. if (string.IsNullOrEmpty(model.Module))
  82. {
  83. return true;
  84. }
  85. var logPairName = $"{model.Module}VersionCheck";
  86. if (versionPair.Keys.Contains(model.Module))
  87. {
  88. LogWriter.Log(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  89. //ReportLog.Add(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version));
  90. bool isVersionMatched = false;
  91. if (model.Module == "CsuRootFsFwRev")
  92. {
  93. isVersionMatched = versionPair[model.Module].StartsWith(model.Version);
  94. }
  95. else
  96. {
  97. isVersionMatched = versionPair[model.Module] == model.Version;
  98. }
  99. if (isVersionMatched)
  100. {
  101. LogWriter.Report(logPairName,"success");
  102. //Logger.Print($"Model {model.Module} updated", isError: false);
  103. //InfoLog += $"{Name}:updated success\n";
  104. //if (!LogPair.Keys.Contains(logPairName))
  105. // LogPair.Add(logPairName, "1");
  106. return true;
  107. }
  108. else
  109. {
  110. LogWriter.Report(logPairName, "fail");
  111. //Logger.Print($"Model {model.Module} version mismatch", isError: true);
  112. //InfoLog += $"{model.Module}:Updated Version mismatched\n";
  113. //if (!LogPair.Keys.Contains(logPairName))
  114. // LogPair.Add(logPairName, "0");
  115. return false;
  116. }
  117. }
  118. else if (model.Module.ToLower() == "dtb")
  119. {
  120. //pass
  121. return true;
  122. }
  123. else
  124. {
  125. //model name not found
  126. LogWriter.Report(logPairName, "fail");
  127. LogWriter.Log($"Model {model.Module} version not found");
  128. //Logger.Print($"Model {model.Module} version not found", isError: true);
  129. //InfoLog += $"Model {model.Module} version not found\n";
  130. //if (!LogPair.Keys.Contains(logPairName))
  131. // LogPair.Add(logPairName, "0");
  132. return false;
  133. }
  134. }
  135. internal async Task<Dictionary<string, string>> GetVersion(bool isConnectTest = false)
  136. {
  137. try
  138. {
  139. using (WebClientTimeout webClient = new WebClientTimeout())
  140. {
  141. NameValueCollection parameters = new NameValueCollection();
  142. parameters.Add("opt", "1");
  143. webClient.QueryString = parameters;
  144. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  145. // 使用 StreamReader 讀取 stream 內的字元
  146. using (StreamReader reader = new StreamReader(stream))
  147. {
  148. // 將 StreamReader 所讀到的字元轉為 string
  149. string request = reader.ReadToEnd();
  150. LogWriter.Log($"get version response:{request}", isDebugLog: true);
  151. //InfoLog += $"get version response:{request}\n";
  152. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  153. var toReturn = new Dictionary<string, string>();
  154. foreach (var pair in values)
  155. {
  156. if (pair.Value is string v)
  157. {
  158. toReturn.Add(pair.Key, v);
  159. }
  160. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  161. {
  162. try
  163. {
  164. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  165. for (int index = 0; index < versionList.Count; index++)
  166. {
  167. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  168. }
  169. }
  170. catch
  171. {
  172. }
  173. }
  174. }
  175. return toReturn;
  176. }
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. if (!isConnectTest)
  182. {
  183. LogWriter.Log("Get Version Failed");
  184. LogWriter.Log(e.Message, isDebugLog: true);
  185. //Logger.Print("Get Version Failed", isError: true);
  186. //Logger.Print(e.Message + "", isError: true);
  187. //InfoLog += "Get Version Failed\n";
  188. //InfoLog += e.Message;
  189. //InfoLog += "\n";
  190. }
  191. return null;
  192. }
  193. }
  194. }
  195. }