Beispiel #1
0
        private void ConnectModemCommandExecute(ModemConfigInfo modemInfo)
        {
            if (Project.SmsModule != null && Project.SmsModule.StartupCommands != null && !String.IsNullOrEmpty(Project.SmsModule.StartupCommands))
            {
                string[] commands = Project.SmsModule.StartupCommands.Split('\n');
                foreach (string command in commands)
                {
                    modemInfo.StartupCommands.Add(command);
                }
            }

            // save the most recent settings to the server's config for faster re-use later
            Properties.Settings.Default.LastUsedBaudRate = int.Parse(modemInfo.BaudRate);
            Properties.Settings.Default.LastUsedComPort  = modemInfo.ComPort;
            Properties.Settings.Default.Save();

            try
            {
                SmsController      = new SmsController(modemInfo, Project.SmsModule.PollRate);
                IsConnectedToModem = true;

                AddStatusMessage(String.Format("Connected to modem on {0}", modemInfo.ComPort), "Server");

                SmsController.SmsReceived += SmsController_SmsReceived;
            }
            catch (Exception ex)
            {
                ShowErrorBoxCommand.Execute(String.Format("Could not connect to the modem. Exception: {0}", ex.Message));
                IsConnectedToModem = false;
            }
        }
Beispiel #2
0
        private void DisconnectCommandExecute()
        {
            OutbreakName = "Not connected to a database";

            Project.Dispose();
            Project = null;

            SmsController.Dispose();
            SmsController = null;

            StatusMessages.Clear();
            DiagnosticCommandResults.Clear();

            IsConnectedToDatabase = false;
            IsConnectedToModem    = false;
        }
Beispiel #3
0
        private void IssueATCommandExecute(string command)
        {
            DiagnosticCommandResults.Add(command);

            try
            {
                string result            = SmsController.ExecCommand(command, 700, "VHF ERROR MESSAGE");
                string displayableResult = result.TrimStart('\r').TrimStart('\n').TrimEnd('\n').TrimEnd('\r');
                displayableResult = displayableResult.Replace("\r\n\r\nOK", "\nOK");
                DiagnosticCommandResults.Add(displayableResult);
                AddStatusMessage(String.Format("Diagnostic command issued: {0}", command), "Server");
            }
            catch (Exception ex)
            {
                DiagnosticCommandResults.Add(String.Format("Server generated an exception: {0}", ex.Message));

                // re-start the controller
                SmsController.SmsReceived -= SmsController_SmsReceived;
                SmsController.Dispose();

                SmsController              = new SmsController(SmsController.ModemConfiguration, Project.SmsModule.PollRate);
                SmsController.SmsReceived += SmsController_SmsReceived;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Handles the reception of an SMS message
        /// </summary>
        /// <param name="sender">The object that fired the event</param>
        /// <param name="e">The event arguments</param>
        private void SmsController_SmsReceived(object sender, Core.Events.SmsReceivedArgs e)
        {
            string[] parts = e.Message.Message.Split(' ');
            if (parts.Length == 1 && parts[0].Equals(Project.SmsModule.SelfRegistrationCode) && Project.SmsModule.AllowsSelfRegistration)
            {
                // self-register message received
                bool registered = Project.ExecuteSmsSelfRegister(e.Message.Sender);
                if (registered)
                {
                    SmsController.SendSMS(e.Message.Sender, "Your phone has been registered to send messages.");
                    AddStatusMessage("Self-registration successful", e.Message.Sender);
                }
                else
                {
                    SmsController.SendSMS(e.Message.Sender, "Registration failed. Your phone may already be registered, or self-registration may be disabled.");
                    AddStatusMessage("Self-registration failed", e.Message.Sender);
                }
            }
            else if (parts.Length == 3)
            {
                int  updateType;
                bool success = int.TryParse(parts[0], out updateType);

                if (success)
                {
                    if (Project.IsUserAuthorized(e.Message.Sender, updateType))
                    {
                        AddStatusMessage(e.Message.Message, e.Message.Sender);
                    }
                    else
                    {
                        #region Rejection
                        AddStatusMessage("Unauthorized message was received and discarded: " + e.Message.Message, e.Message.Sender);

                        if (Project.SmsModule.SendsReadReceipts == true)
                        {
                            try
                            {
                                SmsController.SendSMS(e.Message.Sender, "Message was rejected. Reason: Unauthorized");
                            }
                            catch (Exception ex)
                            {
                                AddStatusMessage("An exception was thrown while sending message rejection message to sender " + e.Message.Sender + ". Exception: " + ex.Message, "[SERVER]");

                                // just in case this is causing a problem when the port fails, close/dispose and re-open
                                SmsController.SmsReceived -= SmsController_SmsReceived;
                                SmsController.Dispose();

                                SmsController              = new SmsController(SmsController.ModemConfiguration, Project.SmsModule.PollRate);
                                SmsController.SmsReceived += SmsController_SmsReceived;
                            }
                        }

                        return;

                        #endregion // Rejection
                    }

                    bool operationWasSuccessful = true;

                    try
                    {
                        operationWasSuccessful = Project.ExecuteSmsUpdate(e.Message);
                    }
                    catch (Exception ex)
                    {
                        operationWasSuccessful = false;
                        AddStatusMessage("An exception was thrown while updating contact follow-up information. Exception: " + ex.Message, "[SERVER]");
                    }

                    if (operationWasSuccessful && Project.SmsModule.SendsReadReceipts == true)
                    {
                        // send confirmation message
                        try
                        {
                            SmsController.SendSMS(e.Message.Sender, "Message received");
                        }
                        catch (Exception ex)
                        {
                            AddStatusMessage("An exception was thrown while sending message received message to sender " + e.Message.Sender + ". Exception: " + ex.Message, "[SERVER]");

                            // just in case this is causing a problem when the port fails, close/dispose and re-open
                            SmsController.SmsReceived -= SmsController_SmsReceived;
                            SmsController.Dispose();

                            SmsController              = new SmsController(SmsController.ModemConfiguration, Project.SmsModule.PollRate);
                            SmsController.SmsReceived += SmsController_SmsReceived;
                        }
                    }
                    else if (!operationWasSuccessful && Project.SmsModule.SendsReadReceipts == true)
                    {
                        #region Rejection
                        AddStatusMessage("Malformed message was discarded: " + e.Message.Message, e.Message.Sender);
                        if (Project.SmsModule.SendsReadReceipts == true)
                        {
                            try
                            {
                                SmsController.SendSMS(e.Message.Sender, "Message was rejected. Reason: Incorrect format");
                            }
                            catch (Exception ex)
                            {
                                AddStatusMessage("An exception was thrown while sending message rejection message to sender " + e.Message.Sender + ". Exception: " + ex.Message, "[SERVER]");

                                // just in case this is causing a problem when the port fails, close/dispose and re-open
                                SmsController.SmsReceived -= SmsController_SmsReceived;
                                SmsController.Dispose();

                                SmsController              = new SmsController(SmsController.ModemConfiguration, Project.SmsModule.PollRate);
                                SmsController.SmsReceived += SmsController_SmsReceived;
                            }
                        }
                        #endregion // Rejection
                    }
                }
            }
            else
            {
                AddStatusMessage("Malformed message was discarded: " + e.Message.Message, e.Message.Sender);
                if (Project.SmsModule.SendsReadReceipts == true)
                {
                    try
                    {
                        SmsController.SendSMS(e.Message.Sender, "Message was rejected. Reason: Incorrect format");
                    }
                    catch (Exception ex)
                    {
                        AddStatusMessage("An exception was thrown while sending message rejection message to sender " + e.Message.Sender + ". Exception: " + ex.Message, "[SERVER]");

                        // just in case this is causing a problem when the port fails, close/dispose and re-open
                        SmsController.SmsReceived -= SmsController_SmsReceived;
                        SmsController.Dispose();

                        SmsController              = new SmsController(SmsController.ModemConfiguration, Project.SmsModule.PollRate);
                        SmsController.SmsReceived += SmsController_SmsReceived;
                    }
                }
            }
        }