/// <summary>
        /// Get the last ADCP Serial port options from the database.
        /// This is the last used settings in Pulse.
        /// </summary>
        /// <returns>Last used ADCP serial port options.</returns>
        public SerialOptions GetLastPulseAdcpSerialOptions()
        {
            // Get the pulse options from the database
            PulseOptions options = GetPulseOptions();

            return(options.AdcpSerialOptions);            // Return ADCP serial options
        }
        /// <summary>
        /// Set the pulse options.
        /// This will serialize the Pulse options into a JSON object.
        /// Then write the JSON object to the database.
        /// </summary>
        /// <param name="options">Pule Options.</param>
        public void SetPulseOptions(PulseOptions options)
        {
            string json    = Newtonsoft.Json.JsonConvert.SerializeObject(options);                                                                      // Serialize object to JSON
            string jsonCmd = String.Format("{0} = '{1}'", Pulse.DbCommon.COL_PULSE_OPTIONS, json);
            string query   = String.Format("UPDATE {0} SET {1} WHERE ID=1;", Pulse.DbCommon.TBL_PULSE_SETTINGS, jsonCmd);                               // Create query string.  ID should always equal 1, only the first row will be used

            Pulse.DbCommon.RunQueryOnPulseDb(_dbMainConnection, query);
        }
        /// <summary>
        /// Get the last NMEA 2 Serial port options from the database.
        /// This is the last used settings in Pulse.
        /// </summary>
        /// <param name="isEnabled">Get flag if the port was enabled.</param>
        /// <returns>Last used NMEA 2 serial port options.</returns>
        public SerialOptions GetLastPulseNmea2SerialOptions(out bool isEnabled)
        {
            // Get the pulse options from the database
            PulseOptions options = GetPulseOptions();


            isEnabled = options.IsNmea2SerialEnabled;     // Set the IsNmea2SerialEnabled
            return(options.Nmea2SerialOptions);           // Return NMEA 2 serial options
        }
        /// <summary>
        /// Get the last GPS 2 Serial port options from the database.
        /// This is the last used settings in Pulse.
        /// </summary>
        /// <param name="isEnabled">Get flag if the port was enabled.</param>
        /// <returns>Last used GPS 2 serial port options.</returns>
        public SerialOptions GetLastPulseGps2SerialOptions(out bool isEnabled)
        {
            // Get the pulse options from the database
            PulseOptions options = GetPulseOptions();


            isEnabled = options.IsGps2SerialEnabled;     // Set the IsGpsEnabled
            return(options.Gps2SerialOptions);           // Return GPS serial options
        }
        /// <summary>
        /// Create a string of all the parameters and values for serial options.  Then set them in the database.
        /// </summary>
        /// <param name="adcpSerialOptions">Serial port options.</param>
        public void SetLastPulseAdcpSerialOptions(SerialOptions adcpSerialOptions)
        {
            // Get the current options from the database
            // Then replace the ADCP serial options with the new options.
            PulseOptions options = GetPulseOptions();

            if (adcpSerialOptions != null)
            {
                options.AdcpSerialOptions = adcpSerialOptions;
            }

            // Set the pulse options
            SetPulseOptions(options);
        }
        /// <summary>
        /// Create a string of all the parameters and values for NMEA 2 serial options.  Then set them in the database.
        /// </summary>
        /// <param name="enableNmeaSerial">Flag is NMEA 2 serial port is enabled.</param>
        /// <param name="nmeaSerialOptions">NMEA 2 serial port options.</param>
        public void SetLastPulseNmea2SerialOptions(bool enableNmeaSerial, SerialOptions nmeaSerialOptions)
        {
            // Get the current options from the database
            // Then replace the NMEA serial options with the new options.
            PulseOptions options = GetPulseOptions();

            if (nmeaSerialOptions != null)
            {
                options.IsNmea2SerialEnabled = enableNmeaSerial;
                options.Nmea2SerialOptions   = nmeaSerialOptions;
            }

            // Set the pulse options
            SetPulseOptions(options);
        }
        /// <summary>
        /// Create a string of all the parameters and values for GPS 2 serial options.  Then set them in the database.
        /// </summary>
        /// <param name="enableGpsSerial">Flag is GPS 2 serial port is enabled.</param>
        /// <param name="gpsSerialOptions">GPS 2 serial port options.</param>
        public void SetLastPulseGps2SerialOptions(bool enableGpsSerial, SerialOptions gpsSerialOptions)
        {
            // Get the current options from the database
            // Then replace the GPS serial options with the new options.
            PulseOptions options = GetPulseOptions();

            if (gpsSerialOptions != null)
            {
                options.IsGps2SerialEnabled = enableGpsSerial;
                options.Gps2SerialOptions   = gpsSerialOptions;
            }

            // Set the pulse options
            SetPulseOptions(options);
        }
        /// <summary>
        /// Get the Pulse options from the database.
        /// This will read in the pulse options from the database.
        /// The options are stored as a JSON object.  The JSON object
        /// is then deserialized into a PulseOption object.
        ///
        /// There should be only 1 row in the Pulse Options table, so read
        /// in all the rows and decode the first row if it contains any options.
        ///
        /// If there are no options set yet in the database, then default options will
        /// be returned.
        /// </summary>
        /// <returns>Pulse Options from the database or default options.</returns>
        public PulseOptions GetPulseOptions()
        {
            PulseOptions options = new PulseOptions();

            string query = String.Format("SELECT * FROM {0};", Pulse.DbCommon.TBL_PULSE_SETTINGS);

            try
            {
                // Query the database for the ADCP settings
                DataTable dt = Pulse.DbCommon.GetDataTableFromPulseDb(_dbMainConnection, query);

                // Go through the result settings the settings
                // If more than 1 result is found, return the first one found
                foreach (DataRow r in dt.Rows)
                {
                    // Check if there is data
                    if (r[Pulse.DbCommon.COL_PULSE_OPTIONS] == DBNull.Value)
                    {
                        break;
                    }

                    // This will call the default constructor or pass to the constructor parameter a null
                    // The constructor parameter must be set after creating the object.
                    string json = Convert.ToString(r[Pulse.DbCommon.COL_PULSE_OPTIONS]);
                    if (!String.IsNullOrEmpty(json))
                    {
                        options = Newtonsoft.Json.JsonConvert.DeserializeObject <PulseOptions>(json);
                    }


                    // Only read the first row
                    break;
                }
            }
            catch (SQLiteException e)
            {
                log.Error("SQL Error getting Pulse Options.", e);
            }
            catch (Exception ex)
            {
                log.Error("Error getting Pulse Options.", ex);
            }

            return(options);
        }