WifRssiCheckProcedure.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using AwInitilizer.Assist;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. namespace AwInitilizer.Procedure.WifRssiCheck
  12. {
  13. public enum ErrorType
  14. {
  15. None,
  16. WifiModeDataNotFound,
  17. WifiModeNotClient,
  18. WifiRssiDataNotFound,
  19. WifiRssiLow,
  20. }
  21. public enum LogEvent
  22. {
  23. WifiRssi
  24. }
  25. public class WifRssiCheckProcedure : ProcedureBase
  26. {
  27. public ErrorType Error { get; set; } = ErrorType.None;
  28. private ProcedureLog.LogWriter<WifRssiCheckProcedure, LogEvent> LogWriter;
  29. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  30. {
  31. { LogEvent.WifiRssi, "WifiRssi" }
  32. };
  33. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  34. {
  35. { LogEvent.WifiRssi, "Get Wifi Rssi : {0}" }
  36. };
  37. public WifRssiCheckProcedure() : base()
  38. {
  39. Name = "WiFi Rssi Check";
  40. Content = "Check Wifi Rssi Signal";
  41. LogWriter = new ProcedureLog.LogWriter<WifRssiCheckProcedure, LogEvent>(this)
  42. {
  43. ReportPair = ReportDict,
  44. LogPair = LogDict
  45. };
  46. }
  47. internal override async Task<bool> Run()
  48. {
  49. if (!UpdateData.SystemID.ModelName.Network.ToString().Contains("WiFi"))
  50. {
  51. LogWriter.Log("Wifi not supported, skip procedure");
  52. return true;
  53. }
  54. var checkResult = await CheckWifiRssi();
  55. return checkResult;
  56. }
  57. internal async Task<bool> CheckWifiRssi()
  58. {
  59. var rssi = await GetWifiRssi();
  60. LogWriter.Report(LogEvent.WifiRssi, rssi.ToString());
  61. if (rssi == 0)
  62. {
  63. return false;
  64. }
  65. else
  66. {
  67. var rssiCheckPass = rssi >= -80;
  68. if (rssiCheckPass)
  69. return true;
  70. else
  71. {
  72. Error = ErrorType.WifiRssiLow;
  73. return false;
  74. }
  75. }
  76. }
  77. internal async Task<int> GetWifiRssi()
  78. {
  79. try
  80. {
  81. using (WebClientTimeout webClient = new WebClientTimeout())
  82. {
  83. NameValueCollection parameters = new NameValueCollection();
  84. parameters.Add("opt", "3");
  85. webClient.QueryString = parameters;
  86. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  87. // 使用 StreamReader 讀取 stream 內的字元
  88. using (StreamReader reader = new StreamReader(stream))
  89. {
  90. // 將 StreamReader 所讀到的字元轉為 string
  91. string request = reader.ReadToEnd();
  92. LogWriter.Log($"GetResponse:{request}", isDebugLog: true);
  93. Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)");
  94. var matches_mode = rx_mode.Matches(request);
  95. if (matches_mode.Count != 0)
  96. {
  97. var match = matches_mode[0];
  98. if (match.Groups.Count != 3)
  99. {
  100. Error = ErrorType.WifiModeDataNotFound;
  101. return 0;
  102. }
  103. else
  104. {
  105. if (int.TryParse(match.Groups[2].Value, out var wifiMode))
  106. {
  107. if (wifiMode != 1)
  108. {
  109. Error = ErrorType.WifiModeNotClient;
  110. return 0;
  111. }
  112. }
  113. }
  114. }
  115. else
  116. {
  117. Error = ErrorType.WifiModeDataNotFound;
  118. return 0;
  119. }
  120. Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)");
  121. var matches = rx.Matches(request);
  122. if (matches.Count != 0)
  123. {
  124. var match = matches[0];
  125. if (match.Groups.Count != 3)
  126. {
  127. Error = ErrorType.WifiRssiDataNotFound;
  128. return 0;
  129. }
  130. else
  131. {
  132. if (int.TryParse(match.Groups[2].Value, out var rssiSignal))
  133. {
  134. return rssiSignal;
  135. }
  136. Error = ErrorType.WifiRssiDataNotFound;
  137. return 0;
  138. }
  139. }
  140. else
  141. {
  142. Error = ErrorType.WifiRssiDataNotFound;
  143. return 0;
  144. }
  145. }
  146. }
  147. }
  148. catch (Exception e)
  149. {
  150. Error = ErrorType.WifiRssiDataNotFound;
  151. LogWriter.Log("Get Wifi Rssi Failed");
  152. LogWriter.Log(e.Message, isDebugLog: true);
  153. return 0;
  154. }
  155. }
  156. }
  157. }