using AwInitilizer.Assist; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AwInitilizer.Procedure.WifRssiCheck { public enum ErrorType { None, WifiModeDataNotFound, WifiModeNotClient, WifiRssiDataNotFound, WifiRssiLow, } public enum LogEvent { WifiRssi } public class WifRssiCheckProcedure : ProcedureBase { public ErrorType Error { get; set; } = ErrorType.None; private ProcedureLog.LogWriter LogWriter; private readonly static Dictionary ReportDict = new Dictionary() { { LogEvent.WifiRssi, "WifiRssi" } }; private readonly static Dictionary LogDict = new Dictionary() { { LogEvent.WifiRssi, "Get Wifi Rssi : {0}" } }; public WifRssiCheckProcedure() : base() { Name = "WiFi Rssi Check"; Content = "Check Wifi Rssi Signal"; LogWriter = new ProcedureLog.LogWriter(this) { ReportPair = ReportDict, LogPair = LogDict }; } internal override async Task Run() { if (!UpdateData.SystemID.ModelName.Network.ToString().Contains("WiFi")) { LogWriter.Log("Wifi not supported, skip procedure"); return true; } var checkResult = await CheckWifiRssi(); return checkResult; } internal async Task CheckWifiRssi() { var rssi = await GetWifiRssi(); LogWriter.Report(LogEvent.WifiRssi, rssi.ToString()); if (rssi == 0) { return false; } else { var rssiCheckPass = rssi >= -80; if (rssiCheckPass) return true; else { Error = ErrorType.WifiRssiLow; return false; } } } internal async Task GetWifiRssi() { try { using (WebClientTimeout webClient = new WebClientTimeout()) { NameValueCollection parameters = new NameValueCollection(); parameters.Add("opt", "3"); 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($"GetResponse:{request}", isDebugLog: true); Regex rx_mode = new Regex("(WifiMode)\\\": ([0-9]*)"); var matches_mode = rx_mode.Matches(request); if (matches_mode.Count != 0) { var match = matches_mode[0]; if (match.Groups.Count != 3) { Error = ErrorType.WifiModeDataNotFound; return 0; } else { if (int.TryParse(match.Groups[2].Value, out var wifiMode)) { if (wifiMode != 1) { Error = ErrorType.WifiModeNotClient; return 0; } } } } else { Error = ErrorType.WifiModeDataNotFound; return 0; } Regex rx = new Regex("(WifiRssi)\\\": (-?[0-9]*)"); var matches = rx.Matches(request); if (matches.Count != 0) { var match = matches[0]; if (match.Groups.Count != 3) { Error = ErrorType.WifiRssiDataNotFound; return 0; } else { if (int.TryParse(match.Groups[2].Value, out var rssiSignal)) { return rssiSignal; } Error = ErrorType.WifiRssiDataNotFound; return 0; } } else { Error = ErrorType.WifiRssiDataNotFound; return 0; } } } } catch (Exception e) { Error = ErrorType.WifiRssiDataNotFound; LogWriter.Log("Get Wifi Rssi Failed"); LogWriter.Log(e.Message, isDebugLog: true); return 0; } } } }