Exemplo n.º 1
0
        /// <summary>
        /// Displays the gateway.
        /// </summary>
        private void DisplayGateway()
        {
            if (!string.IsNullOrEmpty(this.GatewayId))
            {
                // Retrieve and display the gateway configuration
                GatewayConfig gatewayConfig = GatewayConfig.SingleOrDefault(g => g.Id == this.GatewayId);
                if (gatewayConfig != null)
                {
                    // Display the gateway information
                    txtName.Text                = gatewayConfig.Id;
                    txtPhoneNo.Text             = gatewayConfig.OwnNumber;
                    chkConnectAtStartup.Checked = gatewayConfig.AutoConnect.Value;
                    GatewayFunction functions = (GatewayFunction)Enum.Parse(typeof(GatewayFunction), gatewayConfig.Functions.ToString());
                    if ((functions & GatewayFunction.SendMessage) == GatewayFunction.SendMessage)
                    {
                        chkSendMessage.Checked = true;
                    }
                    if ((functions & GatewayFunction.ReceiveMessage) == GatewayFunction.ReceiveMessage)
                    {
                        chkReceiveMessage.Checked = true;
                    }
                    cboLogType.Text = gatewayConfig.LogSettings;
                    chkClearLogOnConnect.Checked = gatewayConfig.ClearLogOnConnect.Value;
                    cboPort.Text                 = gatewayConfig.ComPort;
                    cboBaudRate.Text             = gatewayConfig.BaudRate;
                    cboHandshake.Text            = gatewayConfig.Handshake;
                    cboDataBits.Text             = gatewayConfig.DataBits;
                    cboParity.Text               = gatewayConfig.Parity;
                    cboStopBits.Text             = gatewayConfig.StopBits;
                    updCommandTimeout.Value      = gatewayConfig.CommandTimeout.Value;
                    updReadIntervalTimeout.Value = gatewayConfig.ReadTimeout.Value;
                    updCommandDelay.Value        = gatewayConfig.CommandDelay.Value;
                    txtSmsc.Text                 = gatewayConfig.Smsc;

                    chkUseSmscFromSim.Checked = gatewayConfig.UseSimSmsc.Value;
                    cboMessageMemory.Text     = gatewayConfig.MessageMemory;
                    cboMessageValidity.Text   = gatewayConfig.MessageValidity;
                    updSendRetry.Value        = gatewayConfig.SendRetry.Value;
                    updSendDelay.Value        = gatewayConfig.SendDelay.Value;
                    chkRequestDeliveryStatusReport.Checked = gatewayConfig.RequestStatusReport.Value;
                    chkDeleteAfterRetrieve.Checked         = gatewayConfig.DeleteAfterRetrieve.Value;

                    chkAutoArchiveMessageLog.Checked            = gatewayConfig.AutoArchiveLog.Value;
                    updArchiveMessageLogDay.Value               = gatewayConfig.AutoArchiveLogInterval.Value;
                    updArchiveMessageOlderThanDay.Value         = gatewayConfig.ArchiveOldMessageInterval.Value;
                    chkForwardArchivedMessageLogAsEmail.Checked = gatewayConfig.ForwardArchivedMessage.Value;
                    txtEmailAddress.Text = gatewayConfig.ForwardEmail;
                    chkDeleteArchivedMessageOlderThan.Checked  = gatewayConfig.DeleteArchivedMessage.Value;
                    updDeleteArchivedMessageOlderThanDay.Value = gatewayConfig.DeleteArchivedMessageInterval.Value;

                    updRefreshSignalInterval.Value = gatewayConfig.SignalRefreshInterval.Value;
                    txtPin.Text = gatewayConfig.Pin;
                    chkAutoResponseCall.Checked = gatewayConfig.AutoResponseCall.Value;
                    txtAutoResponseText.Text    = gatewayConfig.AutoResponseCallText;

                    txtName.Enabled = false;
                    this.IsUpdate   = true;
                }
            }
        }
        /// <summary>
        /// Gateways the form_ gateway added.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void gatewayForm_GatewayAdded(object sender, GatewayEventHandlerArgs e)
        {
            GatewayConfig newChannel = GatewayConfig.SingleOrDefault(g => g.Id == e.GatewayId);

            this.lvwChannels.AddObject(newChannel);
            if (GatewayAdded != null)
            {
                // Raise the event
                GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(e.GatewayId);
                this.GatewayAdded.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
            }
        }
        /// <summary>
        /// Handles the SelectedIndexChanged event of the cboChannel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cboChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedChannel = cboChannel.Text;
            string[] values = selectedChannel.Split(new string[] { GuiHelper.FieldSplitter }, StringSplitOptions.RemoveEmptyEntries);
            selectedChannel = string.Empty;
            for (int i = 0; i < values.Length - 1; i++)
            {
                selectedChannel += values[i];
            }

            GatewayConfig gw = GatewayConfig.SingleOrDefault(g => g.Id == selectedChannel);
            if (gw != null)
                txtFrom.Text = gw.OwnNumber;            
        }
        /// <summary>
        /// Invoked when a gateway is updated
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void gatewayForm_GatewayUpdated(object sender, GatewayEventHandlerArgs e)
        {
            GatewayConfig updatedChannel = GatewayConfig.SingleOrDefault(g => g.Id == e.GatewayId);

            // Refresh the list view
            RefreshView();

            if (GatewayUpdated != null)
            {
                // Raise the event
                GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(e.GatewayId);
                this.GatewayUpdated.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
            }
        }
        /// <summary>
        /// Does the work.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoWork(object sender, ElapsedEventArgs e)
        {
            if (messageGatewayService.Gateways.Count() == 0)
            {
                return;
            }

            if (log.IsDebugEnabled)
            {
                log.Debug("Checking outgoing message");
            }

            IOrderedQueryable <OutgoingMessage> messages = OutgoingMessage.All().Where(msg => msg.Status != StringEnum.GetStringValue(MessageStatus.Archived) &&
                                                                                       msg.Status != StringEnum.GetStringValue(MessageStatus.Failed) &&
                                                                                       msg.Status != StringEnum.GetStringValue(MessageStatus.Sending) &&
                                                                                       msg.Status != StringEnum.GetStringValue(MessageStatus.Sent) &&
                                                                                       (
                                                                                           msg.ScheduledDate == null ||
                                                                                           msg.ScheduledDate <= DateTime.Now
                                                                                       )).OrderBy(msg => msg.LastUpdate);


            // Send out messages
            foreach (OutgoingMessage message in messages)
            {
                List <IMessage>     outgoingMessages = new List <IMessage>(messages.Count());
                OutgoingMessageType messageType      = (OutgoingMessageType)StringEnum.Parse(typeof(OutgoingMessageType), message.MessageType);
                if (messageType == OutgoingMessageType.SMS)
                {
                    // Get the target routed gateway
                    Sms sms = Sms.NewInstance();
                    sms.Identifier         = message.Id;
                    sms.DestinationAddress = message.Recipient;
                    sms.Content            = message.Message;
                    MessageFormat format = (MessageFormat)StringEnum.Parse(typeof(MessageFormat), message.MessageFormat);
                    sms.DataCodingScheme = EnumMatcher.MessageCoding[format];
                    sms.ValidityPeriod   = MessageValidPeriod.OneDay;

                    MessageStatusReport statusReport = (MessageStatusReport)StringEnum.Parse(typeof(MessageStatusReport), message.StatusReport);
                    if (statusReport == MessageStatusReport.StatusReport)
                    {
                        sms.StatusReportRequest = MessageStatusReportRequest.SmsReportRequest;
                    }
                    else if (statusReport == MessageStatusReport.NoStatusReport)
                    {
                        sms.StatusReportRequest = MessageStatusReportRequest.NoSmsReportRequest;
                    }

                    sms.DcsMessageClass = EnumMatcher.MessageClass[message.MessageClass];
                    sms.QueuePriority   = EnumMatcher.MessagePriority[message.Priority];

                    IGateway      gateway       = messageGatewayService.Router.GetRoute(sms);
                    GatewayConfig gatewayConfig = GatewayConfig.SingleOrDefault(g => g.Id == gateway.Id);
                    if (gatewayConfig != null)
                    {
                        // Message validity period
                        if (!string.IsNullOrEmpty(gatewayConfig.MessageValidity))
                        {
                            sms.ValidityPeriod = EnumMatcher.ValidityPeriod[gatewayConfig.MessageValidity];
                        }

                        // Status report
                        if (statusReport == MessageStatusReport.FollowChannel)
                        {
                            if (gatewayConfig.RequestStatusReport.Value)
                            {
                                sms.StatusReportRequest = MessageStatusReportRequest.SmsReportRequest;
                            }
                            else
                            {
                                sms.StatusReportRequest = MessageStatusReportRequest.NoSmsReportRequest;
                            }
                        }

                        if (gatewayConfig.UseSimSmsc.HasValue && !gatewayConfig.UseSimSmsc.Value && !string.IsNullOrEmpty(gatewayConfig.Smsc))
                        {
                            sms.ServiceCenterNumber = gatewayConfig.Smsc;
                        }
                    }
                    outgoingMessages.Add(sms);

                    // Update status to "Sending"
                    message.Status     = StringEnum.GetStringValue(MessageStatus.Sending);
                    message.LastUpdate = DateTime.Now;
                    message.Update();
                }
                else if (messageType == OutgoingMessageType.WAPPush)
                {
                    Wappush wappush = Wappush.NewInstance(message.Recipient, message.WapUrl, message.Message);

                    ServiceIndicationAction signal;
                    if (EnumMatcher.ServiceIndication.TryGetValue(message.WapSignal, out signal))
                    {
                        wappush.Signal = signal;
                    }
                    wappush.CreateDate = message.WapCreateDate.Value;
                    wappush.ExpireDate = message.WapExpiryDate.Value;

                    MessageStatusReport statusReport = (MessageStatusReport)StringEnum.Parse(typeof(MessageStatusReport), message.StatusReport);
                    if (statusReport == MessageStatusReport.StatusReport)
                    {
                        wappush.StatusReportRequest = MessageStatusReportRequest.SmsReportRequest;
                    }
                    else if (statusReport == MessageStatusReport.NoStatusReport)
                    {
                        wappush.StatusReportRequest = MessageStatusReportRequest.NoSmsReportRequest;
                    }

                    wappush.DcsMessageClass = EnumMatcher.MessageClass[message.MessageClass];
                    wappush.QueuePriority   = EnumMatcher.MessagePriority[message.Priority];

                    IGateway      gateway       = messageGatewayService.Router.GetRoute(wappush);
                    GatewayConfig gatewayConfig = GatewayConfig.SingleOrDefault(g => g.Id == gateway.Id);
                    if (gatewayConfig != null)
                    {
                        // Message validity period
                        if (!string.IsNullOrEmpty(gatewayConfig.MessageValidity))
                        {
                            wappush.ValidityPeriod = EnumMatcher.ValidityPeriod[gatewayConfig.MessageValidity];
                        }

                        // Status report
                        if (statusReport == MessageStatusReport.FollowChannel)
                        {
                            if (gatewayConfig.RequestStatusReport.Value)
                            {
                                wappush.StatusReportRequest = MessageStatusReportRequest.SmsReportRequest;
                            }
                            else
                            {
                                wappush.StatusReportRequest = MessageStatusReportRequest.NoSmsReportRequest;
                            }
                        }
                    }

                    outgoingMessages.Add(wappush);

                    // Update status to "Sending"
                    message.Status     = StringEnum.GetStringValue(MessageStatus.Sending);
                    message.LastUpdate = DateTime.Now;
                    message.Update();
                }

                int count = messageGatewayService.SendMessages(outgoingMessages);
                log.Debug(string.Format("Messages are queued for sending. Count of number of messages is [{0}]", count));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns></returns>
        private bool SaveChanges()
        {
            // Validate the name is not empty
            if (!FormHelper.ValidateNotEmpty(txtName, Resources.MsgUniqueNameRequired))
            {
                return(false);
            }

            string gatewayId = txtName.Text.Trim();

            if (!this.IsUpdate)
            {
                // The gateway name must be unique
                if (GatewayConfig.Exists(g => g.Id.ToLower() == gatewayId.ToLower()))
                {
                    FormHelper.ShowError(txtName, string.Format(Resources.MsgGatewayNameAlreadyExists, gatewayId));
                    return(false);
                }

                // The port name should not be in use
                if (GatewayConfig.Exists(g => g.ComPort.ToLower() == cboPort.Text.ToLower()))
                {
                    DialogResult dialogResult = FormHelper.Confirm(string.Format(Resources.MsgDuplicateComPort, cboPort.Text));
                    if (dialogResult == DialogResult.No)
                    {
                        return(false);
                    }
                }
            }

            // If forwarded is checked, make sure email address is not empty
            if (chkForwardArchivedMessageLogAsEmail.Checked)
            {
                if (!FormHelper.ValidateNotEmpty(txtEmailAddress, Resources.MsgEmailAddressRequired))
                {
                    return(false);
                }
            }

            // If auto response is required, make sure the text is not empty
            if (chkAutoResponseCall.Checked)
            {
                if (!FormHelper.ValidateNotEmpty(txtAutoResponseText, Resources.MsgAutoResponseTextRequired))
                {
                    return(false);
                }
            }

            try
            {
                // Save the gateway configuration
                GatewayConfig gatewayConfig = new GatewayConfig();

                if (this.IsUpdate)
                {
                    gatewayConfig = GatewayConfig.SingleOrDefault(g => g.Id == gatewayId);
                }

                // General
                gatewayConfig.Id          = gatewayId;
                gatewayConfig.OwnNumber   = txtPhoneNo.Text;
                gatewayConfig.AutoConnect = chkConnectAtStartup.Checked;
                gatewayConfig.Functions   = 0;
                if (chkSendMessage.Checked)
                {
                    gatewayConfig.Functions += (int)GatewayFunction.SendMessage;
                }
                if (chkReceiveMessage.Checked)
                {
                    gatewayConfig.Functions += (int)GatewayFunction.ReceiveMessage;
                }
                gatewayConfig.LogSettings       = cboLogType.Text;
                gatewayConfig.ClearLogOnConnect = chkClearLogOnConnect.Checked;
                gatewayConfig.Initialize        = false;

                // Communication
                gatewayConfig.ComPort        = cboPort.Text;
                gatewayConfig.BaudRate       = cboBaudRate.Text;
                gatewayConfig.DataBits       = cboDataBits.Text;
                gatewayConfig.Parity         = cboParity.Text;
                gatewayConfig.StopBits       = cboStopBits.Text;
                gatewayConfig.Handshake      = cboHandshake.Text;
                gatewayConfig.CommandTimeout = Convert.ToInt32(updCommandTimeout.Value);
                gatewayConfig.CommandDelay   = Convert.ToInt32(updCommandDelay.Value);
                gatewayConfig.ReadTimeout    = Convert.ToInt32(updReadIntervalTimeout.Value);


                // Message Settings
                gatewayConfig.Smsc                = txtSmsc.Text;
                gatewayConfig.UseSimSmsc          = chkUseSmscFromSim.Checked;
                gatewayConfig.MessageMemory       = cboMessageMemory.Text;
                gatewayConfig.MessageValidity     = cboMessageValidity.Text;
                gatewayConfig.SendRetry           = Convert.ToInt32(updSendRetry.Value);
                gatewayConfig.SendDelay           = Convert.ToInt32(updSendDelay.Value);
                gatewayConfig.RequestStatusReport = chkRequestDeliveryStatusReport.Checked;
                gatewayConfig.DeleteAfterRetrieve = chkDeleteAfterRetrieve.Checked;


                // Message log
                gatewayConfig.AutoArchiveLog            = chkAutoArchiveMessageLog.Checked;
                gatewayConfig.AutoArchiveLogInterval    = Convert.ToInt32(updArchiveMessageLogDay.Value);
                gatewayConfig.ArchiveOldMessageInterval = Convert.ToInt32(updArchiveMessageOlderThanDay.Value);
                gatewayConfig.ForwardArchivedMessage    = chkForwardArchivedMessageLogAsEmail.Checked;
                gatewayConfig.ForwardEmail                  = txtEmailAddress.Text;
                gatewayConfig.DeleteArchivedMessage         = chkDeleteArchivedMessageOlderThan.Checked;
                gatewayConfig.DeleteArchivedMessageInterval = Convert.ToInt32(updDeleteArchivedMessageOlderThanDay.Value);

                // Other settings
                gatewayConfig.SignalRefreshInterval = Convert.ToInt32(updRefreshSignalInterval.Value);
                gatewayConfig.Pin = txtPin.Text;
                gatewayConfig.AutoResponseCall     = chkAutoResponseCall.Checked;
                gatewayConfig.AutoResponseCallText = txtAutoResponseText.Text;

                if (!this.IsUpdate)
                {
                    gatewayConfig.Save();
                }
                else
                {
                    gatewayConfig.Update();
                }

                if (GatewayAdded != null)
                {
                    // Raise the event
                    GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(gatewayId);
                    this.GatewayAdded.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
                }

                if (GatewayUpdated != null)
                {
                    // Raise the event
                    GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(gatewayId);
                    this.GatewayUpdated.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
                }
            }
            catch (Exception ex)
            {
                FormHelper.ShowError(ex.Message);
                return(false);
            }

            // Reset to false
            isFormChanged = false;

            // Show successful save message
            FormHelper.ShowInfo(Resources.MsgGatewayConfigSaved);

            // Return true as saving is successful
            return(true);
        }