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 { public class VersionLogProcedure : ProcedureBase { public VersionLogProcedure() : base() { Name = "Version Logger"; Content = "Report version back to MES"; } internal override async Task Run() { var versionPair = await GetVersion(); if (versionPair != null) { foreach (var infoPair in versionPair) { LogPair.Add(infoPair.Key, infoPair.Value); } return true; } else { return false; } } 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(); 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) { 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; } } } }