VersionLogProcedure.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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
  11. {
  12. public class VersionLogProcedure : ProcedureBase
  13. {
  14. private List<string> excludeHeaderList = new List<string>() {
  15. "FactoryConfiguration",
  16. "AuxPower5V",
  17. "AuxPower12V",
  18. "AuxPower24V",
  19. "AuxPower48V",
  20. "CsuHwRev",
  21. "FirmwareUpdate",
  22. "FanModuleHwRev",
  23. "RelayModuleHwRev" };
  24. public VersionLogProcedure() : base()
  25. {
  26. Name = "Version Logger";
  27. Content = "Report version back to MES";
  28. }
  29. internal override async Task<bool> Run()
  30. {
  31. var versionPair = await GetVersion();
  32. if (versionPair != null)
  33. {
  34. foreach (var infoPair in versionPair)
  35. {
  36. if (!excludeHeaderList.Contains(infoPair.Key))
  37. {
  38. LogPair.Add(infoPair.Key, infoPair.Value);
  39. }
  40. }
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. internal async Task<Dictionary<string, string>> GetVersion()
  49. {
  50. try
  51. {
  52. using (WebClient webClient = new WebClient())
  53. {
  54. NameValueCollection parameters = new NameValueCollection();
  55. parameters.Add("opt", "1");
  56. webClient.QueryString = parameters;
  57. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  58. // 使用 StreamReader 讀取 stream 內的字元
  59. using (StreamReader reader = new StreamReader(stream))
  60. {
  61. // 將 StreamReader 所讀到的字元轉為 string
  62. string request = reader.ReadToEnd();
  63. InfoLog += $"get version response:{request}\n";
  64. var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
  65. var toReturn = new Dictionary<string, string>();
  66. foreach (var pair in values)
  67. {
  68. if (pair.Value is string v)
  69. {
  70. toReturn.Add(pair.Key, v);
  71. }
  72. else if (pair.Value is Newtonsoft.Json.Linq.JArray a)
  73. {
  74. try
  75. {
  76. var versionList = JsonConvert.DeserializeObject<List<string>>(a.ToString());
  77. for (int index = 0; index < versionList.Count; index++)
  78. {
  79. toReturn.Add(string.Format("{0}{1}", pair.Key, index), versionList[index]);
  80. }
  81. }
  82. catch
  83. {
  84. }
  85. }
  86. }
  87. return toReturn;
  88. }
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. Logger.Print("Get Version Failed", isError: true);
  94. Logger.Print(e.Message + "", isError: true);
  95. InfoLog += "Get Version Failed\n";
  96. InfoLog += e.Message;
  97. InfoLog += "\n";
  98. return null;
  99. }
  100. }
  101. }
  102. }