WifImeiRecordProcedure.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using AwInitilizer.Assist;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. namespace AwInitilizer.Procedure.WifImeiRecord
  11. {
  12. public enum ErrorType
  13. {
  14. None,
  15. WifiImeiNotFound,
  16. }
  17. public enum LogEvent
  18. {
  19. WifiImei
  20. }
  21. public class WifImeiRecordProcedure : ProcedureBase
  22. {
  23. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  24. {
  25. { LogEvent.WifiImei, "WifiRssi" }
  26. };
  27. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  28. {
  29. { LogEvent.WifiImei, "Get Wifi IMEI : {0}" }
  30. };
  31. public WifImeiRecordProcedure() : base()
  32. {
  33. Name = "WiFi IMEI record";
  34. Content = "read and upload WIFI IMEI";
  35. LogWriter = new ProcedureLog.LogWriter<WifImeiRecordProcedure, LogEvent>(this)
  36. {
  37. ReportPair = ReportDict,
  38. LogPair = LogDict
  39. };
  40. }
  41. public ErrorType Error { get; set; } = ErrorType.None;
  42. private ProcedureLog.LogWriter<WifImeiRecordProcedure, LogEvent> LogWriter;
  43. internal override async Task<bool> Run()
  44. {
  45. if (!UpdateData.SystemID.ModelName.Network.Description.Contains("WiFi"))
  46. {
  47. LogWriter.Log("Wifi not supported, skip procedure");
  48. return true;
  49. }
  50. var imei = await GetWifiImei();
  51. LogWriter.Report(LogEvent.WifiImei, imei);
  52. return string.IsNullOrEmpty(imei);
  53. }
  54. internal async Task<string> GetWifiImei()
  55. {
  56. try
  57. {
  58. using (WebClientTimeout webClient = new WebClientTimeout())
  59. {
  60. NameValueCollection parameters = new NameValueCollection();
  61. parameters.Add("opt", "3");
  62. webClient.QueryString = parameters;
  63. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  64. // 使用 StreamReader 讀取 stream 內的字元
  65. using (StreamReader reader = new StreamReader(stream))
  66. {
  67. // 將 StreamReader 所讀到的字元轉為 string
  68. string request = reader.ReadToEnd();
  69. LogWriter.Log($"GetResponse:{request}", isDebugLog: true);
  70. Regex rx = new Regex("(TelcomModemImei)\\\": \"([0-9]*)\"");
  71. var matches = rx.Matches(request);
  72. if (matches.Count != 0)
  73. {
  74. var match = matches[0];
  75. if (match.Groups.Count != 3)
  76. {
  77. Error = ErrorType.WifiImeiNotFound;
  78. return "";
  79. }
  80. else
  81. {
  82. if (match.Groups[2].Value is string imei)
  83. {
  84. return imei;
  85. }
  86. Error = ErrorType.WifiImeiNotFound;
  87. return "";
  88. }
  89. }
  90. else
  91. {
  92. Error = ErrorType.WifiImeiNotFound;
  93. return "";
  94. }
  95. }
  96. }
  97. }
  98. catch (Exception e)
  99. {
  100. Error = ErrorType.WifiImeiNotFound;
  101. LogWriter.Log("Get Wifi IMEI Failed");
  102. LogWriter.Log(e.Message, isDebugLog: true);
  103. return "";
  104. }
  105. }
  106. }
  107. }