123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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 List<string> excludeHeaderList = new List<string>()
- {
- "FactoryConfiguration",
- "AuxPower5V",
- "AuxPower12V",
- "AuxPower24V",
- "AuxPower48V",
- "CsuHwRev",
- "FirmwareUpdate",
- "FanModuleHwRev",
- "RelayModuleHwRev"
- };
- private static readonly Dictionary<string, string> MesReportNamePair = new Dictionary<string, string>
- {
- { "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 LogEvent
- {
- }
- private ProcedureLog.LogWriter<VersionLogProcedure, LogEvent> LogWriter;
- public VersionLogProcedure() : base()
- {
- Name = "Version Logger";
- Content = "Report version back to MES";
- LogWriter = new ProcedureLog.LogWriter<VersionLogProcedure, LogEvent>(this);
- }
- internal override async Task<bool> 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);
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- private string GetMesReportKey(string readVersion)
- {
- if(MesReportNamePair.ContainsKey(readVersion))
- {
- return MesReportNamePair[readVersion];
- }
- return readVersion;
- }
- 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();
- LogWriter.Log($"get version response:{request}\n", 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)
- {
- 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;
- }
- }
- }
- }
|