using AwInitilizer.Assist; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AwInitilizer.Procedure.FirmwareCheckVersion { public enum ErrorType { None, RestartTimeout, VersionCheckFail, } public enum LogEvent { } public class FirmwareCheckVersionProcedure : ProcedureBase { public ErrorType Error { get; set; } = ErrorType.None; private ProcedureLog.LogWriter LogWriter; private readonly static Dictionary ReportDict = new Dictionary() { }; private readonly static Dictionary LogDict = new Dictionary() { }; public FirmwareCheckVersionProcedure() : base() { Name = "Firmware Version Check"; Content = "Wait restart and check firmware versions"; LogWriter = new ProcedureLog.LogWriter(this) { ReportPair = ReportDict, LogPair = LogDict }; } internal override async Task Run() { //wait restart bool response = false; int pollingCnt = 0; await Task.Delay(TimeSpan.FromMinutes(2)); for (pollingCnt = 0; pollingCnt < 56; pollingCnt++) { await Task.Delay(TimeSpan.FromSeconds(15)); response = await ChekCsuBootCompelete(); if (response) break; } LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2)); //timeout if (pollingCnt >= 56) { Error = ErrorType.RestartTimeout; return false; } //get version var versionPair = await GetVersion(); var updatedList = UpdateData.FirmwareUpdateModels; //check all version foreach (var model in updatedList) { var checkResult = CheckModelbyList(model, versionPair); if (!checkResult) { Error = ErrorType.VersionCheckFail; return false; } } return true; } private bool CheckModelbyList(Model.FirmwareUpdateModel model, Dictionary versionPair) { if (string.IsNullOrEmpty(model.Module)) { return true; } var logPairName = $"{model.Module}VersionCheck"; if (versionPair.Keys.Contains(model.Module)) { LogWriter.Log(string.Format("Read {0} version : {1} , Expect:{2}", model.Module, versionPair[model.Module], model.Version)); bool isVersionMatched = false; if (model.Module == "CsuRootFsFwRev") { isVersionMatched = versionPair[model.Module].StartsWith(model.Version); } else { isVersionMatched = versionPair[model.Module] == model.Version; } if (isVersionMatched) { LogWriter.Report(logPairName,"success"); return true; } else { LogWriter.Report(logPairName, "fail", isError: true); return false; } } else if (model.Module.ToLower() == "dtb") { //pass return true; } else { //model name not found LogWriter.Report(logPairName, "fail", isError: true); LogWriter.Log($"Model {model.Module} version not found"); return false; } } internal async Task> GetVersion(bool isConnectTest = false) { try { using (WebClientTimeout webClient = new WebClientTimeout()) { NameValueCollection parameters = new NameValueCollection(); parameters.Add("opt", "1"); webClient.QueryString = parameters; using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php")) // 使用 StreamReader 讀取 stream 內的字元 using (StreamReader reader = new StreamReader(stream)) { // 將 StreamReader 所讀到的字元轉為 string string request = reader.ReadToEnd(); LogWriter.Log($"get version response:{request}", isDebugLog: true); //InfoLog += $"get version response:{request}\n"; var values = JsonConvert.DeserializeObject>(request); var toReturn = new Dictionary(); foreach (var pair in values) { if (pair.Value is string v) { toReturn.Add(pair.Key, v); } else if (pair.Value is Newtonsoft.Json.Linq.JArray a) { try { var versionList = JsonConvert.DeserializeObject>(a.ToString()); for (int index = 0; index < versionList.Count; index++) { toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]); } } catch { } } } return toReturn; } } } catch (Exception e) { if (!isConnectTest) { LogWriter.Log("Get Version Failed"); LogWriter.Log(e.Message, isDebugLog: true); } return null; } } } }