protected virtual ISignalResponse ParseE2(string response) { response = Helpers.SanitizeXmlString(response); string snr = null; string db = null; string acg = null; string ber = null; using (XmlReader reader = XmlReader.Create(new StringReader(response))) { bool canRead = reader.Read(); while (canRead) { if (reader.IsStartElement()) { switch (reader.Name.ToLower()) { case "e2snrdb": reader.Read(); db = reader.Value; break; case "e2snr": reader.Read(); snr = reader.Value; break; case "e2ber": reader.Read(); ber = reader.Value; break; case "e2acg": reader.Read(); acg = reader.Value; break; } } canRead = reader.Read(); } } IE2Signal signal = InitializeSignal(snr, db, acg, ber); if (signal == null) { throw new ParsingException("Failed to parse Enigma2 signal!"); } return(_factory.SignalResponse(signal)); }
private IE2Signal InitializeSignal(string snr, string db, string acg, string ber) { IE2Signal signal = _factory.E2Signal(); string ds = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; string realSnr; string realDb; snr = snr.Replace("%", "").Trim(); db = db.ToLower().Replace("db", "").Trim().Replace(",", ds).Replace(".", ds); if (snr.Length == 0 || db.Length == 0) { signal.Db = -1; signal.Snr = -1; signal.Acg = -1; signal.Ber = -1; return(signal); } //dB is in percentage, someting is wrong if (db.IndexOf("%", StringComparison.Ordinal) > -1) { //if SNR is in dB we simply switch the values if (snr.IndexOf("db", StringComparison.Ordinal) > -1) { realSnr = db; realDb = snr; realDb = realDb.Replace("db", "").Trim().Replace(",", ds).Replace(".", ds); realSnr = realSnr.Replace("%", "").Trim(); } else { //both dB and SNR are in %, we'll have to calculate dB realSnr = snr; realDb = null; } } else { realSnr = snr; realDb = db; } //check if snr value is in db if (snr.ToLower().IndexOf("db", StringComparison.Ordinal) > -1) { realSnr = null; realDb = db; } if (realSnr == null && realDb == null) { throw new ParsingException("Failed to parse Enigma2 signal!"); } if (realSnr != null && realDb != null) { signal.Snr = int.Parse(realSnr); signal.Db = decimal.Parse(realDb); } else if (realDb != null) { signal.Db = decimal.Parse(realDb); signal.Snr = (int)(signal.Db * 6.5M); } else { signal.Snr = int.Parse(realSnr); signal.Db = Math.Round(signal.Snr / 6.5M, 2); } signal.Acg = int.Parse(acg.Replace("%", "").Trim()); signal.Ber = int.Parse(ber); return(signal); }