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 { public class FirmwareCheckVersionProcedure : ProcedureBase { public FirmwareCheckVersionProcedure() : base() { Name = "Firmware Version Check"; Content = "Wait restart and check firmware versions"; } internal override async Task Run() { //wait restart Logger.Print("Waiting 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; } ReportLog.Add(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2)); //timeout if (pollingCnt >= 56) { Logger.Print("Wait restart timeout", isError: true); InfoLog += "Wait restart timeout\n"; return false; } //get version var versionPair = await GetVersion(); var updatedList = UpdateData.FirmwareUpdateModels; //check all version foreach (var model in updatedList) { if (string.IsNullOrEmpty(model.Module) || string.IsNullOrEmpty(model.Version)) { continue; } var logPairNmae = $"{model.Module}VersionCheck"; if (versionPair.Keys.Contains(model.Module)) { ReportLog.Add(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) { Logger.Print($"Model {model.Module} updated", isError: false); InfoLog += $"{Name}:updated success\n"; if (!LogPair.Keys.Contains(logPairNmae)) LogPair.Add(logPairNmae, "1"); } else { Logger.Print($"Model {model.Module} version mismatch", isError: true); InfoLog += $"{model.Module}:Updated Version mismatched\n"; if (!LogPair.Keys.Contains(logPairNmae)) LogPair.Add(logPairNmae, "0"); return false; } } else { //model name not found Logger.Print($"Model {model.Module} version not found", isError: true); InfoLog += $"Model {model.Module} version not found\n"; if (!LogPair.Keys.Contains(logPairNmae)) LogPair.Add(logPairNmae, "0"); return false; } } return true; } 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(); 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) { Logger.Print("Get Version Failed", isError: true); Logger.Print(e.Message + "", isError: true); InfoLog += "Get Version Failed\n"; InfoLog += e.Message; InfoLog += "\n"; } return null; } } } }