Esempio n. 1
0
        /// <summary>
        /// Will parse the entire string returned from the +CMGL command into an array of
        /// summaries.
        /// </summary>
        /// <param name="smsSummaryDump">The raw string from the Serial Port</param>
        /// <param name="smsOut">The array to received all parsed results</param>
        /// <returns></returns>
        public static bool TryParseList(string smsSummaryDump, out SmsSummary[] smsOut)
        {
            smsOut = null;
            var workingSmsOut = new List<SmsSummary>();

            if (string.IsNullOrWhiteSpace(smsSummaryDump))
                return false;

            try
            {
                //distill string in unix style newline
                var smsChars = new System.Text.StringBuilder();
                foreach (var c in smsSummaryDump.ToCharArray().Where(c => c != (char)0x0D))
                {
                    smsChars.Append(c);
                }

                //split on unix newline
                var smsLines = smsChars.ToString().Split((char)0x0A);

                //find index of each summary line
                var indexSummaryLines = new List<int>();
                for (var i = 0; i < smsLines.Length; i++)
                {
                    var smsLine = smsLines[i];

                    if (Regex.IsMatch(smsLine, SMS_SUMMARY_PATTERN_LN1))
                        indexSummaryLines.Add(i);
                }

                //go through each index only for lines that matched summary regex
                foreach (var j in indexSummaryLines)
                {
                    //expect message body to be next line past msg summary
                    if (smsLines.Length <= j + 1)
                        continue;

                    //put the summary and message body together and try to parse it
                    SmsSummary smsSummary = null;
                    if (TryParse(string.Format("{0} {1}", smsLines[j], smsLines[j + 1]), out smsSummary))
                        workingSmsOut.Add(smsSummary);
                }
                if (workingSmsOut.Count > 0)
                {
                    smsOut = workingSmsOut.ToArray();
                    return true;
                }

                return false;
            }
            catch
            {
                smsOut = null;
                return false;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parse a single line having the message concatenated to the end of the summary string.
        /// Its intention is mostly to serve <see cref="TryParseList"/> since the actual contents
        /// from the MT is not in the form.
        /// </summary>
        /// <param name="smsSummaryText"></param>
        /// <param name="smsOut"></param>
        /// <returns>
        /// True if the string matched to <see cref="SMS_SUMMARY_PATTERN_LN1"/> after 
        /// after having the AT command striped <see cref="Utility.StripOffAtCommand"/>.
        /// </returns>
        public static bool TryParse(string smsSummaryText, out SmsSummary smsOut)
        {
            smsOut = null;
            if (string.IsNullOrWhiteSpace(smsSummaryText))
                return false;

            try
            {
                smsSummaryText = Utility.StripOffAtCommand(smsSummaryText);

                var smsMatch = Regex.Match(smsSummaryText, SMS_SUMMARY_PATTERN_LN1);

                if(smsMatch.Success)
                {
                    smsOut = new SmsSummary();
                    var index = 0;
                    int.TryParse(smsMatch.Groups[1].Value, out index);
                    smsOut.Index = index;
                    smsOut.Status = smsMatch.Groups[2].Value;
                    smsOut.Number = smsMatch.Groups[3].Value;

                    var text = smsSummaryText.Replace(smsMatch.Groups[0].Value, "");
                    smsOut.Message = text;
                    return true;
                }
            }
            catch
            {
                smsOut = null;
                return false;
            }

            return false;
        }