using AwInitilizer.Assist; 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.WifImeiRecord { public enum ErrorType { None, WifiImeiNotFound, } public enum LogEvent { WifiImei } public class WifImeiRecordProcedure : ProcedureBase { private readonly static Dictionary ReportDict = new Dictionary() { { LogEvent.WifiImei, "WifiRssi" } }; private readonly static Dictionary LogDict = new Dictionary() { { LogEvent.WifiImei, "Get Wifi IMEI : {0}" } }; public WifImeiRecordProcedure() : base() { Name = "WiFi IMEI record"; Content = "read and upload WIFI IMEI"; LogWriter = new ProcedureLog.LogWriter(this) { ReportPair = ReportDict, LogPair = LogDict }; } public ErrorType Error { get; set; } = ErrorType.None; private ProcedureLog.LogWriter LogWriter; internal override async Task Run() { if (!UpdateData.SystemID.ModelName.Network.Description.Contains("WiFi")) { LogWriter.Log("Wifi not supported, skip procedure"); return true; } var imei = await GetWifiImei(); LogWriter.Report(LogEvent.WifiImei, imei); return string.IsNullOrEmpty(imei); } internal async Task GetWifiImei() { 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 = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\""); var matches = rx.Matches(request); if (matches.Count != 0) { var match = matches[0]; if (match.Groups.Count != 3) { Error = ErrorType.WifiImeiNotFound; return ""; } else { if (match.Groups[2].Value is string imei) { return imei; } Error = ErrorType.WifiImeiNotFound; return ""; } } else { Error = ErrorType.WifiImeiNotFound; return ""; } } } } catch (Exception e) { Error = ErrorType.WifiImeiNotFound; LogWriter.Log("Get Wifi IMEI Failed"); LogWriter.Log(e.Message, isDebugLog: true); return ""; } } } }