using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace AwInitilizer.Procedure.VersionLog { public class VersionLogProcedure : ProcedureBase { private static readonly string SoftwareVersionHeader = "FullsetTestingVersion"; private static readonly List excludeHeaderList = new List() { "FactoryConfiguration", "AuxPower5V", "AuxPower12V", "AuxPower24V", "AuxPower48V", "CsuHwRev", "FirmwareUpdate", "FanModuleHwRev", "RelayModuleHwRev" }; private static readonly Dictionary MesReportNamePair = new Dictionary { { "CsuBootLoadFwRev","CSU Bootloader" }, { "CsuKernelFwRev","CSU Kernel" }, { "CsuRootFsFwRev","CSU Rootfs" }, { "CsuPrimFwRev","Primary MCU" }, { "FanModuleFwRev","FAN Module" }, { "LedModuleFwRev","LED Module" }, { "LcmFwRev","LCM UI" }, { "RelayModuleFwRev","Relay Module" }, { "PsuPrimFwRev","PSU Primary" }, { "PsuSecFwRev","PSU Secondary" }, { "TelcomModemFwRev","Telecom Module" }, { "AuxPwrFwRev","Aux Power" }, { "FourthGenModuleVersion","Fourth Gen Module" }, { "Connector1FwRev","Connector 1" }, { "Connector2FwRev","Connector 2" }, }; public enum ErrorType { None, ReadVersionFail, } public enum LogEvent { } public ErrorType Error { get; set; } = ErrorType.None; private ProcedureLog.LogWriter LogWriter; public VersionLogProcedure() : base() { Name = "Version Logger"; Content = "Report version back to MES"; LogWriter = new ProcedureLog.LogWriter(this); } internal override async Task Run() { var versionPair = await GetVersion(); if (versionPair != null) { foreach (var infoPair in versionPair) { if (!excludeHeaderList.Contains(infoPair.Key)) { var mesKey = GetMesReportKey(infoPair.Key); LogWriter.Report(mesKey, infoPair.Value); //LogPair.Add(infoPair.Key, infoPair.Value); } } } else { Error = ErrorType.ReadVersionFail; return false; } var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var version = assembly.GetName().Version.ToString(); var gitVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion; var logVersion = string.Format("{0}-{1}", version, gitVersion); LogWriter.Report(SoftwareVersionHeader, logVersion); return true; } private string GetMesReportKey(string readVersion) { if(MesReportNamePair.ContainsKey(readVersion)) { return MesReportNamePair[readVersion]; } return readVersion; } internal async Task> GetVersion() { try { using (WebClient webClient = new WebClient()) { 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}\n", 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) { LogWriter.Log("Get Version Failed", isError: true); LogWriter.Log(e.Message, isDebugLog: true); //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; } } } }