123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
-
- 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<bool> 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<Dictionary<string, string>> 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<Dictionary<string, object>>(request);
- var toReturn = new Dictionary<string, string>();
- 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<List<string>>(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;
- }
- }
- }
- }
|