VersionLogProcedure.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace AwInitilizer.Procedure.VersionLog
  11. {
  12. public class VersionLogProcedure : ProcedureBase
  13. {
  14. private static readonly List<string> excludeHeaderList = new List<string>()
  15. {
  16. "FactoryConfiguration",
  17. "AuxPower5V",
  18. "AuxPower12V",
  19. "AuxPower24V",
  20. "AuxPower48V",
  21. "CsuHwRev",
  22. "FirmwareUpdate",
  23. "FanModuleHwRev",
  24. "RelayModuleHwRev"
  25. };
  26. private static readonly Dictionary<string, string> MesReportNamePair = new Dictionary<string, string>
  27. {
  28. { "CsuBootLoadFwRev","CSU Bootloader" },
  29. { "CsuKernelFwRev","CSU Kernel" },
  30. { "CsuRootFsFwRev","CSU Rootfs" },
  31. { "CsuPrimFwRev","Primary MCU" },
  32. { "FanModuleFwRev","FAN Module" },
  33. { "LedModuleFwRev","LED Module" },
  34. { "LcmFwRev","LCM UI" },
  35. { "RelayModuleFwRev","Relay Module" },
  36. { "PsuPrimFwRev","PSU Primary" },
  37. { "PsuSecFwRev","PSU Secondary" },
  38. { "TelcomModemFwRev","Telecom Module" },
  39. { "AuxPwrFwRev","Aux Power" },
  40. { "FourthGenModuleVersion","Fourth Gen Module" },
  41. { "Connector1FwRev","Connector 1" },
  42. { "Connector2FwRev","Connector 2" },
  43. };
  44. public enum LogEvent
  45. {
  46. }
  47. private ProcedureLog.LogWriter<VersionLogProcedure, LogEvent> LogWriter;
  48. public VersionLogProcedure() : base()
  49. {
  50. Name = "Version Logger";
  51. Content = "Report version back to MES";
  52. LogWriter = new ProcedureLog.LogWriter<VersionLogProcedure, LogEvent>(this);
  53. }
  54. internal override async Task<bool> Run()
  55. {
  56. var versionPair = await GetVersion();
  57. if (versionPair != null)
  58. {
  59. foreach (var infoPair in versionPair)
  60. {
  61. if (!excludeHeaderList.Contains(infoPair.Key))
  62. {
  63. var mesKey = GetMesReportKey(infoPair.Key);
  64. LogWriter.Report(mesKey, infoPair.Value);
  65. //LogPair.Add(infoPair.Key, infoPair.Value);
  66. }
  67. }
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. private string GetMesReportKey(string readVersion)
  76. {
  77. if(MesReportNamePair.ContainsKey(readVersion))
  78. {
  79. return MesReportNamePair[readVersion];
  80. }
  81. return readVersion;
  82. }
  83. internal async Task<Dictionary<string, string>> GetVersion()
  84. {
  85. try
  86. {
  87. using (WebClient webClient = new WebClient())
  88. {
  89. NameValueCollection parameters = new NameValueCollection();
  90. parameters.Add("opt", "1");
  91. webClient.QueryString = parameters;
  92. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  93. // 使用 StreamReader 讀取 stream 內的字元
  94. using (StreamReader reader = new StreamReader(stream))
  95. {
  96. // 將 StreamReader 所讀到的字元轉為 string
  97. string request = reader.ReadToEnd();
  98. LogWriter.Log($"get version response:{request}\n", isDebugLog: true);
  99. //InfoLog += $"get version response:{request}\n";
  100. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  101. var toReturn = new Dictionary<string, string>();
  102. foreach (var pair in values)
  103. {
  104. if (pair.Value is string v)
  105. {
  106. toReturn.Add(pair.Key, v);
  107. }
  108. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  109. {
  110. try
  111. {
  112. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  113. for (int index = 0; index < versionList.Count; index++)
  114. {
  115. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  116. }
  117. }
  118. catch
  119. {
  120. }
  121. }
  122. }
  123. return toReturn;
  124. }
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. LogWriter.Log("Get Version Failed", isError: true);
  130. LogWriter.Log(e.Message, isDebugLog: true);
  131. //Logger.Print("Get Version Failed", isError: true);
  132. //Logger.Print(e.Message + "", isError: true);
  133. //InfoLog += "Get Version Failed\n";
  134. //InfoLog += e.Message;
  135. //InfoLog += "\n";
  136. return null;
  137. }
  138. }
  139. }
  140. }