private string GetGMapsURLForLocation(GpsFix fix)
 {
     return string.Format("http://maps.google.com/maps?q={0},{1}&ll={0},{1}&z=11",fix.Lat.ToString(), fix.Long.ToString());
 }
        /// <summary>
        /// Send emails to all the people in the list outlining the emergency details
        /// </summary>
        /// <param name="EmergencyFrom">The name of the terminal that the emergency originated from</param>
        /// <param name="DateTime">Date and time of the emergency</param>
        /// <param name="Interface">The interface that the emergency was recieved on</param>
        private void SendEmergencyEmail(string EmergencyFrom, string DateTime, string Interface, GpsFix fix, bool ByPassConditions = false)
        {
            try
            {
                if (!ByPassConditions)
                {
                    if (!SatisfyNotificationConditions())
                        return;
                }

                Log("Sending emails");
                string subject = string.Format("Crosswire: Emergency recieved from {0}", EmergencyFrom);
                string body = string.Format("An emergency was recieved on {0} from {1} at {2}.", Interface, EmergencyFrom, DateTime);
                if (fix.IsValid)
                {
                    body += string.Format("  Their last known location was {0} at {1}", GetGMapsURLForLocation(fix), fix.Time.ToShortDateString() + " " + fix.Time.ToShortTimeString());
                }
                else
                {
                    body += "  Their location is unknown.";
                }
                if (Settings.Default.TestMode)
                {
                    body += Environment.NewLine + "**This service is currently in test mode and emergencies may not be real.";
                }
                foreach (var address in _emails)
                {
                    try
                    {
                        var fromAddress = new MailAddress(Settings.Default.EmailFrom, "Crosswire Emergency Monitor");
                        var toAddress = new MailAddress(address);

                        Log(string.Format("Sending email to {0}", toAddress));
                        var smtp = new SmtpClient
                        {
                            Host = Settings.Default.SmtpServer,
                            Port = Settings.Default.SmtpPort,
                            EnableSsl = Settings.Default.SmtpEnableSSL,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential(Settings.Default.SmtpServerUsername, Settings.Default.SmtpServerPassword)
                        };

                        using (var message = new MailMessage(fromAddress, toAddress)
                        {
                            Priority = MailPriority.High,
                            Subject = subject,
                            Body = body
                        })
                        {
                            smtp.Send(message);
                        }
                    }
                    catch(Exception ex)
                    {
                        Log(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
        }
 /// <summary>
 /// Get  the last known location for the given terminal ID.
 /// </summary>
 /// <param name="ID"></param>
 /// <returns></returns>
 private GpsFix GetLocationFromID(int ID)
 {
     GpsFix fix = new GpsFix();
     fix.IsValid = false;
     try
     {
         using (var conn = new MySqlConnection(string.Format("Host={0};Username={1};Password={2};Database={3}", Properties.Settings.Default.Server, Settings.Default.Username, Settings.Default.Password, Settings.Default.Database)))
         {
             conn.Open();
             using (var cmd = new MySqlCommand())
             {
                 cmd.Connection = conn;
                 cmd.CommandText = "SELECT ReceivedDT, Latitude, Longitude from locationhistory WHERE TerminalID= @id order by ReceivedDT desc";
                 MySqlParameter param = new MySqlParameter("id", DbType.Int32);
                 param.Value = ID;
                 cmd.Parameters.Add(param);
                 using (var reader = cmd.ExecuteReader())
                 {
                     if (reader.Read())
                     {
                         double lat = (float)reader["Latitude"];
                         lat = (int)(lat/100.0) + (lat / 100.0 - Math.Truncate(lat / 100))*100.0 / 60.0;
                         fix.Lat = (float)lat;
                         double lng = (float)reader["Longitude"];
                         lng = (int)(lng/100.0) + (lng / 100.0 - Math.Truncate(lng / 100))*100.0 / 60.0;
                         fix.Long = (float)lng;
                         fix.Time = (DateTime)reader["ReceivedDT"];
                         fix.IsValid = true;
                         return fix;
                     }
                 }
             }
         }
         return fix;
     }
     catch (Exception ex)
     {
         Log(ex.ToString());
         return fix;
     }
 }