⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itemsmscreator.cs

📁 Wince 上操作短信的实例
💻 CS
📖 第 1 页 / 共 2 页
字号:
        }

        

        #region ActionType enum conversion (string <-> ActionType)


        /// <summary>
        /// Helper function that converts an ActionType to a string
        /// </summary>
        /// <param name="actionType"> An ActionType enum member</param>
        /// <returns> The equivalent string</returns>
        private static string GetActionString(ActionType actionType)
        {
            string action = "";

            switch (actionType)
            {
                case ActionType.Accept:
                    action = acceptActionString;
                    break;
                case ActionType.Decline:
                    action = declineActionString;
                    break;
                case ActionType.ProposalSubject:
                    action = proposalSubjectString;
                    break;
                case ActionType.ProposalBody:
                    action = proposalBodyString;
                    break;
                case ActionType.Tentative:
                    action = tentativeActionString;
                    break;
                default: break;
            }

            return action;
        }

        #endregion
    }


    /// <summary>
    /// The SmsParser object parses SMS messages and stores the information
    /// necessary until two SMS messages that contain all the information
    /// pertinent to an appointment are received.
    /// </summary>
    public static class SmsParser
    {
        #region Private Variables        
        private const string separator = SmsCreator.separator;

        //This stores SMS that are waiting for another part
        //for completion.
        private static ProposalTracker proposalArray = new ProposalTracker();
        #endregion

        #region Parse SMS

        /// <summary>
        /// This method parses an SMS that is intercepted using regular expressions.
        /// </summary>
        /// <param name="msg"> The intercepted SMS</param>
        /// <returns>The appointment parsed, if it is null no appointment was parsed.</returns>
        public static Appointment ParseMessage(string msg, string address, ref ActionType appointmentAction)
        {
            string temp = "";
            string pattern = SmsCreator.identifierProtocol + @"(?<type>\w+)" + separator;            
            Regex msgRegex = new Regex(pattern);
            Match m = msgRegex.Match(msg);

            if (m.Success)
            {
                temp = m.Groups["type"].Value;
            }

            //If the message type is an appointment
            if (temp != null && temp[0] == 'A')
            {
                pattern = SmsCreator.identifierProtocol + @"(?<type>\w*)" +
                          separator + @"(?<action>\w*)" +
                          separator + @"(?<identifier>[^" +
                          separator + "]*)" + 
                          separator + @"(?<location>[^" + 
                          separator + "]*)" + 
                          separator + @"(?<subject>.[^" + 
                          separator + "]*)" +
                          separator + @"(?<startTime>[^" + 
                          separator + "]*)" + 
                          separator + @"(?<endTime>[^" + 
                          separator + "]*)" + 
                          separator;

                msgRegex = new Regex(pattern);
                m = msgRegex.Match(msg);

                if (!m.Success)
                {
                    pattern = SmsCreator.identifierProtocol + @"(?<type>\w*)" +
                              separator + @"(?<action>\w*)" +
                              separator + @"(?<identifier>[^" +
                              separator + "]*)" +
                              separator + @"(?<body>[^" +
                              separator + "]*)" +
                              separator + @"(?<body>.[^" +
                              separator + "]*)" +
                              separator;

                    msgRegex = new Regex(pattern);
                    m = msgRegex.Match(msg);
                }

                if (m.Success)
                {
                    appointmentAction = GetEnumActionType(m.Groups["action"].Value);
                    int identifier = Int32.Parse(m.Groups["identifier"].Value);
                    Appointment appointment = new Appointment();

                    //Is it an appointment request body
                    if (appointmentAction == ActionType.ProposalBody)
                    {
                        appointment.Body = m.Groups["subject"].Value;
                        //See if the first part has already arrived
                        Appointment fullAppointment = proposalArray.Add(appointment, identifier);
                        return fullAppointment;
                    }
                    else
                    {
                        appointment.Subject = m.Groups["subject"].Value;
                        appointment.Location = m.Groups["location"].Value;
                        appointment.Start = DateTime.FromFileTimeUtc(long.Parse(m.Groups["startTime"].Value));
                        appointment.End = DateTime.FromFileTimeUtc(long.Parse(m.Groups["endTime"].Value));

                        if (appointmentAction == ActionType.ProposalSubject)
                        {
                            //Check if the second part has already arrived
                            Appointment fullAppointment = proposalArray.Add(appointment, identifier);
                            return fullAppointment;
                        }
                        else
                        {
                            return appointment;
                        }
                    }
                }
                else
                {
                    return null;
                }
            }
            return null;
        }

        #endregion

        /// <summary>
        /// Helper function that converts a string to an ActionType Enum
        /// </summary>
        /// <param name="actionType"> The string containing an action</param>
        /// <returns> The parsed ActionType</returns>
        private static ActionType GetEnumActionType(string actionType)
        {
            ActionType toReturn;

            if (actionType.Equals(SmsCreator.acceptActionString))
            {
                toReturn = ActionType.Accept;
            }
            else if (actionType.Equals(SmsCreator.proposalSubjectString))
            {
                toReturn = ActionType.ProposalSubject;
            }
            else if (actionType.Equals(SmsCreator.proposalBodyString))
            {
                toReturn = ActionType.ProposalBody;
            }
            else if (actionType.Equals(SmsCreator.declineActionString))
            {
                toReturn = ActionType.Decline;
            }
            else
            {
                toReturn = ActionType.Tentative;
            }

            return toReturn;
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -