VersionLogProcedure.cs 6.2 KB

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