public GprsProvider AsGprsProvider() { return(GprsProviderEx.ParseSimIMSI(Imsi)); /* * var res = GprsProviderEx.ParseSimIMSI(Imsi); * if (res == GprsProvider.Undefined) * res = GprsProviderEx.ParseSimRegisteredOperator(OperatorName); * return res; */ }
public ModemStatus GetModemStatus() { var result = FailedModemStatus.Clone(); var description = new List <string>(); var response = string.Empty; MatchCollection matches; using (var port = ConfigurePort()) { try { response = retry.Execute(() => { return(SendATCommand(port, "ATZ")); }); result.IsSuccess = Regex.Matches(response, @"OK", RegexOptions.IgnoreCase).Count > 0; description.Add(result.IsSuccess ? "Готов" : "Не готов"); response = retry.Execute(() => { return(SendATCommand(port, "AT")); }); result.IsSuccess = Regex.Matches(response, @"OK", RegexOptions.IgnoreCase).Count > 0; description.Add(result.IsSuccess ? "ЭХО" : "Нет ответа ЭХО"); response = retry.Execute(() => { return(SendATCommand(port, "AT+CIMI")); }); matches = Regex.Matches(response, @"\d{10,}", RegexOptions.IgnoreCase); if (matches.Count > 0) { result.Imsi = matches[0].Value; } result.IsSuccess = !string.IsNullOrEmpty(result.Imsi); description.Add(!string.IsNullOrEmpty(result.Imsi) ? "Есть IMSI" : "Нет IMSI"); //оператор var vOperator = GprsProviderEx.ParseSimIMSI(result.Imsi); //Преобразует оператор+IMSI в значение оператора result.OperatorName = vOperator.ProviderName(); result.IsSuccess &= vOperator != GprsProvider.Undefined; description.Add(vOperator != GprsProvider.Undefined ? "Есть оператор" : "Нет оператора"); //Model response = retry.Execute(() => { return(SendATCommand(port, "AT+GMM")); }); matches = Regex.Matches(response, @"[\S ]+", RegexOptions.Singleline); if (matches.Count >= 2 && "OK".Equals(matches[matches.Count - 1].Value, StringComparison.OrdinalIgnoreCase)) { result.ModelName = matches[matches.Count - 2].Value.Trim(); } //Manufacturer response = retry.Execute(() => { return(SendATCommand(port, "AT+GMI")); }); matches = Regex.Matches(response, @"[\S ]+", RegexOptions.Singleline); if (matches.Count >= 2 && "OK".Equals(matches[matches.Count - 1].Value, StringComparison.OrdinalIgnoreCase)) { result.Manufacturer = matches[matches.Count - 2].Value.Trim(); } var vModel = result.AsModel(); //преобразует производитель+модель в модель result.IsSuccess &= vModel != ModelModem.Unknown; //!string.IsNullOrEmpty(vModemStatus.ModelName); description.Add(vModel != ModelModem.Unknown ? "Detected" : "Unknown"); //состояние сети response = retry.Execute(() => { return(SendATCommand(port, "AT+CREG?")); }); var vNetStatus = -1; var vNetStatusDescr = "неизвестно"; var vAnswPos = response.IndexOf("+CREG:"); if (vAnswPos >= 0) { vAnswPos += "+CREG: 0,".Length; if (int.TryParse(response.Substring(vAnswPos, 1), out vNetStatus)) { vNetStatusDescr = vNetStatus.ToNetworkStatus(); } } result.IsSuccess = vNetStatus == 1; description.Add("Сеть: " + vNetStatusDescr); response = retry.Execute(() => { return(SendATCommand(port, "AT+CGSN")); }); matches = Regex.Matches(response, @"\d{10,}", RegexOptions.IgnoreCase); if (matches.Count > 0) { // Imei is serial number result.Imei = result.SerialNumber = matches[0].Value; } // AT+CGMR Request revision identification of software status response = retry.Execute(() => { return(SendATCommand(port, "AT+CGMR")); }); matches = Regex.Matches(response, @"[\S ]+", RegexOptions.Singleline); if (matches.Count >= 2 && "OK".Equals(matches[matches.Count - 1].Value, StringComparison.OrdinalIgnoreCase)) { result.RevisionId = matches[matches.Count - 2].Value; } var quality = GetSignalQuality(port); result.SignalQuality = quality; } catch (IOException ex) { result.State = string.Format("Ошибка: {0}", ex.Message); } catch (Exception ex) { result.State = string.Format("Ошибка: {0}", ex.Message); } finally { result.State = string.Join(", ", description); } } return(result); }