123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
-
- 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<FirmwareCheckVersionProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- };
- public FirmwareCheckVersionProcedure() : base()
- {
- Name = "Firmware Version Check";
- Content = "Wait restart and check firmware versions";
- LogWriter = new ProcedureLog.LogWriter<FirmwareCheckVersionProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> 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<string,string> 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<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();
- LogWriter.Log($"get version response:{request}", isDebugLog: true);
- //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)
- {
- LogWriter.Log("Get Version Failed");
- LogWriter.Log(e.Message, isDebugLog: true);
- }
- return null;
- }
- }
- }
- }
|