/// <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; }
/// <summary> /// Connects to the gateway /// </summary> /// <param name="gwConfig">The gateway config.</param> private void ConnectGateway(GatewayConfig gwConfig) { MobileGatewayConfiguration config = MobileGatewayConfiguration.NewInstance(); config.PortName = gwConfig.ComPort; config.BaudRate = (PortBaudRate)Enum.Parse(typeof(PortBaudRate), gwConfig.BaudRate); config.DataBits = (PortDataBits)Enum.Parse(typeof(PortDataBits), gwConfig.DataBits); if (!string.IsNullOrEmpty(gwConfig.Parity)) { PortParity portParity; if (EnumMatcher.Parity.TryGetValue(gwConfig.Parity, out portParity)) { config.Parity = portParity; } } if (!string.IsNullOrEmpty(gwConfig.StopBits)) { PortStopBits portStopBits; if (EnumMatcher.StopBits.TryGetValue(gwConfig.StopBits, out portStopBits)) { config.StopBits = portStopBits; } } if (!string.IsNullOrEmpty(gwConfig.Handshake)) { PortHandshake handshake; if (EnumMatcher.Handshake.TryGetValue(gwConfig.Handshake, out handshake)) { config.Handshake = handshake; } } if (!string.IsNullOrEmpty(gwConfig.LogSettings)) { LogLevel logLevel; if (EnumMatcher.LoggingLevel.TryGetValue(gwConfig.LogSettings, out logLevel)) { config.LogLevel = logLevel; } } if (!string.IsNullOrEmpty(gwConfig.Pin)) { config.Pin = gwConfig.Pin; } // Set the license key AppConfig licenseKey = AppConfig.SingleOrDefault(ac => ac.Name == ConfigParameter.LicenseKey); if (licenseKey != null) config.LicenseKey = licenseKey.Value; MessageGateway<IMobileGateway, MobileGatewayConfiguration> messageGateway = MessageGateway<IMobileGateway, MobileGatewayConfiguration>.NewInstance(); try { IMobileGateway mobileGateway; mobileGateway = messageGateway.Find(config); if (mobileGateway == null) { log.Error(string.Format("Error connecting to gateway. Check the log file", config.PortName)); } if (!messageGatewayService.Add(mobileGateway)) { throw messageGatewayService.LastError; } if (!string.IsNullOrEmpty(gwConfig.MessageMemory)) { MessageStorage messageStorage = EnumMatcher.MessageMemory[gwConfig.MessageMemory]; mobileGateway.MessageStorage = messageStorage; } mobileGateway.Id = gwConfig.Id; mobileGateway.EnableNewMessageNotification(MessageNotification.StatusReport); mobileGateway.PollNewMessages = true; mobileGateway.Configuration.DeleteReceivedMessage = gwConfig.DeleteAfterRetrieve.Value; mobileGateway.MessageReceived += new MessageReceivedEventHandler(mobileGateway_MessageReceived); mobileGateway.MessageSendingFailed += new MessageErrorEventHandler(mobileGateway_MessageSendingFailed); mobileGateway.MessageSent += new MessageEventHandler(mobileGateway_MessageSent); mobileGateway.Attributes = GatewayAttribute.DeliveryReport | GatewayAttribute.FlashSms | GatewayAttribute.LongMessage | GatewayAttribute.SmartSms | GatewayAttribute.WapPush; GatewayFunction functions = (GatewayFunction)Enum.Parse(typeof(GatewayFunction), gwConfig.Functions.ToString()); if ((functions & GatewayFunction.SendMessage) == GatewayFunction.SendMessage) mobileGateway.Attributes = mobileGateway.Attributes | GatewayAttribute.Send; if ((functions & GatewayFunction.ReceiveMessage) == GatewayFunction.ReceiveMessage) mobileGateway.Attributes = mobileGateway.Attributes | GatewayAttribute.ReceiveByPolling | GatewayAttribute.ReceiveByTrigger; log.Info(string.Format("Successfully connected to gateway. Model is {0}. Port is {1}", mobileGateway.DeviceInformation.Model, config.PortName)); if (gwConfig.Initialize.HasValue && !gwConfig.Initialize.Value) { log.Info("Initializing gateway for the first time"); List<MessageInformation> messages = mobileGateway.GetMessages(MessageStatusType.ReceivedReadMessages); bool isSuccessfulSave1 = SaveReceivedMessages(gwConfig, messages); messages = mobileGateway.GetMessages(MessageStatusType.ReceivedUnreadMessages); bool isSuccessfulSave2 = SaveReceivedMessages(gwConfig, messages); if (isSuccessfulSave1 && isSuccessfulSave2) { gwConfig.Initialize = true; gwConfig.Save(); log.Info("Gateway initialized successfully"); } } } catch (Exception ex) { log.Error(string.Format("Failed to connect to gateway using {0}", config.PortName)); log.Error(ex.Message, ex); } }