123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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
- {
- private List<string> excludeHeaderList = new List<string>() {
- "FactoryConfiguration",
- "AuxPower5V",
- "AuxPower12V",
- "AuxPower24V",
- "AuxPower48V",
- "CsuHwRev",
- "FirmwareUpdate",
- "FanModuleHwRev",
- "RelayModuleHwRev" };
- public VersionLogProcedure() : base()
- {
- Name = "Version Logger";
- Content = "Report version back to MES";
- }
- internal override async Task<bool> Run()
- {
- var versionPair = await GetVersion();
- if (versionPair != null)
- {
- foreach (var infoPair in versionPair)
- {
- if (!excludeHeaderList.Contains(infoPair.Key))
- {
- LogPair.Add(infoPair.Key, infoPair.Value);
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- internal async Task<Dictionary<string, string>> 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<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)
- {
- 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;
- }
- }
- }
- }
|