using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using Project2.hogwarts1;
namespace Project2
{
///
/// Summary description for Form1.
///
public class BPS : System.Windows.Forms.Form
{
private System.Windows.Forms.Button updateButton;
private System.Windows.Forms.TextBox usernameTextbox;
private System.Windows.Forms.TextBox longTextBox;
private System.Windows.Forms.TextBox latTextBox;
private System.Windows.Forms.Label bigLabel;
private System.Windows.Forms.MenuItem settingsMenu;
private System.Windows.Forms.MenuItem alertsMenuItem;
private System.Windows.Forms.MenuItem broadcastMenuItem;
private System.Windows.Forms.MenuItem aboutMenu;
private System.Windows.Forms.Label latLabel;
private System.Windows.Forms.Label longLabel;
private System.Windows.Forms.MainMenu mainMenu1;
public BPS()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.updateButton = new System.Windows.Forms.Button();
this.usernameTextbox = new System.Windows.Forms.TextBox();
this.longTextBox = new System.Windows.Forms.TextBox();
this.latTextBox = new System.Windows.Forms.TextBox();
this.bigLabel = new System.Windows.Forms.Label();
this.settingsMenu = new System.Windows.Forms.MenuItem();
this.alertsMenuItem = new System.Windows.Forms.MenuItem();
this.broadcastMenuItem = new System.Windows.Forms.MenuItem();
this.aboutMenu = new System.Windows.Forms.MenuItem();
this.latLabel = new System.Windows.Forms.Label();
this.longLabel = new System.Windows.Forms.Label();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.settingsMenu);
this.mainMenu1.MenuItems.Add(this.aboutMenu);
//
// updateButton
//
this.updateButton.Location = new System.Drawing.Point(120, 232);
this.updateButton.Size = new System.Drawing.Size(96, 24);
this.updateButton.Text = "Update";
this.updateButton.Click += new System.EventHandler(this.updateButton_Click);
//
// usernameTextbox
//
this.usernameTextbox.Location = new System.Drawing.Point(16, 232);
this.usernameTextbox.Size = new System.Drawing.Size(96, 22);
this.usernameTextbox.Text = "cameron";
//
// longTextBox
//
this.longTextBox.Location = new System.Drawing.Point(120, 200);
this.longTextBox.Size = new System.Drawing.Size(96, 22);
this.longTextBox.Text = "-82.893390";
//
// latTextBox
//
this.latTextBox.Location = new System.Drawing.Point(16, 200);
this.latTextBox.Size = new System.Drawing.Size(96, 22);
this.latTextBox.Text = "34.667238";
//
// bigLabel
//
this.bigLabel.Location = new System.Drawing.Point(24, 16);
this.bigLabel.Size = new System.Drawing.Size(200, 152);
this.bigLabel.Text = "Click Update to begin";
//
// settingsMenu
//
this.settingsMenu.MenuItems.Add(this.broadcastMenuItem);
this.settingsMenu.MenuItems.Add(this.alertsMenuItem);
this.settingsMenu.Text = "Settings";
//
// alertsMenuItem
//
this.alertsMenuItem.Text = "Alerts";
this.alertsMenuItem.Click += new System.EventHandler(this.alertsMenuItem_Click);
//
// broadcastMenuItem
//
this.broadcastMenuItem.Checked = true;
this.broadcastMenuItem.Text = "Broadcast";
this.broadcastMenuItem.Click += new System.EventHandler(this.broadcastMenuItem_Click);
//
// aboutMenu
//
this.aboutMenu.Text = "About";
this.aboutMenu.Click += new System.EventHandler(this.aboutMenu_Click);
//
// latLabel
//
this.latLabel.Location = new System.Drawing.Point(16, 184);
this.latLabel.Size = new System.Drawing.Size(96, 16);
this.latLabel.Text = "Latitude";
//
// longLabel
//
this.longLabel.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular);
this.longLabel.Location = new System.Drawing.Point(120, 184);
this.longLabel.Size = new System.Drawing.Size(96, 16);
this.longLabel.Text = "Longitude";
//
// BPS
//
this.Controls.Add(this.longLabel);
this.Controls.Add(this.latLabel);
this.Controls.Add(this.bigLabel);
this.Controls.Add(this.latTextBox);
this.Controls.Add(this.longTextBox);
this.Controls.Add(this.usernameTextbox);
this.Controls.Add(this.updateButton);
this.Menu = this.mainMenu1;
this.Text = "B.P.S.";
}
#endregion
///
/// The main entry point for the application.
///
static void Main()
{
Application.Run(new BPS());
}
private void aboutMenu_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Created by Cameron Boyle and Matthew Coull");
}
private void broadcastMenuItem_Click(object sender, System.EventArgs e)
{
broadcastMenuItem.Checked = !broadcastMenuItem.Checked;
}
private void alertsMenuItem_Click(object sender, System.EventArgs e)
{
alertsMenuItem.Checked = !alertsMenuItem.Checked;
}
private void updateXML()
{
//This code reads the xml file into an XmlDocument Object so that I can loop through the nodes
XmlTextReader reader = new XmlTextReader("\\Program Files\\Project2\\contacts.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
XmlNode listnode = xmlDoc.DocumentElement;
XmlNode cursornode;
string username;
for(int x = 0; x < listnode.ChildNodes.Count; x++)
{//This iterates through all of the contacts one by one
//Set cursornode to the current contact
cursornode = listnode.ChildNodes[x];
username = cursornode.ChildNodes[0].InnerText;
if(username.ToLower() == usernameTextbox.Text.ToLower())
{//update the elements in the xmlDoc
//Get info out of the node
if(broadcastMenuItem.Checked == true)
{//save brouadcast status
cursornode.ChildNodes[1].InnerText = "on";
}
else
{
cursornode.ChildNodes[1].InnerText = "off";
}
//save coordinates
cursornode.ChildNodes[2].InnerText = latTextBox.Text;
cursornode.ChildNodes[3].InnerText = longTextBox.Text;
//save update time
cursornode.ChildNodes[4].InnerText = System.DateTime.Now.ToShortTimeString();
}
}
//Have to close the reader filestream before we can write back to the file
reader.Close();
//This code writed back the modified XML to the same file and automatically formats the spacing
XmlTextWriter writer = new XmlTextWriter("\\Program Files\\Project2\\contacts.xml",null);
writer.Formatting = Formatting.Indented;
xmlDoc.Save(writer);
writer.Close();
}
private void doWebUpdateXML()
{
string newXML = "";
hogwarts1.Service1 hw = new hogwarts1.Service1();
newXML += "";
newXML += "" + usernameTextbox.Text.ToLower() + "";
if(broadcastMenuItem.Checked == true)
{//save brouadcast status
newXML += "on";
}
else
{
newXML += "off";
}
newXML += "" + latTextBox.Text + "";
newXML += "" + longTextBox.Text + "";
newXML += "";
newXML += "";
hw.UpdateXML(newXML);
}
private void doWebGetXML()
{
string newXML = "";
hogwarts1.Service1 hw = new hogwarts1.Service1();
newXML = hw.GetXML(usernameTextbox.Text.ToLower());
//MessageBox.Show(newXML);
//This code reads the xml into an XmlDocument Object so that I can loop through the nodes
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = newXML;
//This code writed back the modified XML to the same file and automatically formats the spacing
XmlTextWriter writer = new XmlTextWriter("\\Program Files\\Project2\\contacts.xml",null);
writer.Formatting = Formatting.Indented;
xmlDoc.Save(writer);
writer.Close();
}
private void displayList()
{
//This code reads the xml file into an XmlDocument Object so that I can loop through the nodes
XmlTextReader reader = new XmlTextReader("\\Program Files\\Project2\\contacts.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();
XmlNode listnode = xmlDoc.DocumentElement;
XmlNode cursornode;
bigLabel.Text = "";
string location = "";
string username, status,latitude,longitude,time;
double dist;
for(int x = 0; x < listnode.ChildNodes.Count; x++)
{//This iterates through all of the contacts one by one
//Set cursornode to the current contact
cursornode = listnode.ChildNodes[x];
//Get info out of the node
username = cursornode.ChildNodes[0].InnerText;
status = cursornode.ChildNodes[1].InnerText;
latitude = cursornode.ChildNodes[2].InnerText;
longitude = cursornode.ChildNodes[3].InnerText;
time = cursornode.ChildNodes[4].InnerText;
if(status != "off")
{//Only display the contact if they are active
//Now I begin concatenating the text that will be displayed for this contact
bigLabel.Text += username;
//Show this contact's distance from the current user
dist = getDist(latTextBox.Text, longTextBox.Text, latitude, longitude);
if(dist > 0.09)
{
bigLabel.Text += " => ";
bigLabel.Text += dist;
bigLabel.Text += " Miles ";
bigLabel.Text += getDir(latTextBox.Text, longTextBox.Text, latitude, longitude);
bigLabel.Text += "\n";
}
location = getLocation(latitude,longitude);
if(location != "")
{//Show the location name if the contact is within the radius of a hangout
bigLabel.Text += " (";
bigLabel.Text += location;
}
else
{//If the contact is not in a hangout, display coordinates
bigLabel.Text += "\n";
bigLabel.Text += " (";
bigLabel.Text += Math.Round(Convert.ToDouble(latitude),4);
if(latitude.StartsWith("-"))
{
bigLabel.Text += "S";
}
else
{
bigLabel.Text += "N";
}
bigLabel.Text += ", " + Math.Round(Convert.ToDouble(longitude),4);
if(longitude.StartsWith("-"))
{
bigLabel.Text += "W";
}
else
{
bigLabel.Text += "E";
}
}
bigLabel.Text += ")\n\n";
}
}
}
private void doAlerts(double radius)
{
//This code reads the xml file into an XmlDocument Object so that I can loop through the nodes
XmlTextReader reader = new XmlTextReader("\\Program Files\\Project2\\contacts.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();
XmlNode listnode = xmlDoc.DocumentElement;
XmlNode cursornode;
string username,status,latitude,longitude;
double dist;
for(int x = 0; x < listnode.ChildNodes.Count; x++)
{//This iterates through all of the contacts one by one
//Set cursornode to the current contact
cursornode = listnode.ChildNodes[x];
//Get info out of the node
username = cursornode.ChildNodes[0].InnerText;
status = cursornode.ChildNodes[1].InnerText;
latitude = cursornode.ChildNodes[2].InnerText;
longitude = cursornode.ChildNodes[3].InnerText;
if(status != "off" && username.ToLower() != usernameTextbox.Text.ToLower())
{//Only alert on active contacts
dist = getDist(latTextBox.Text, longTextBox.Text, latitude, longitude);
if(dist <= radius)
{
MessageBox.Show("You are within " + radius + " miles of " + username + "!");
}
}
}
}
private string getLocation(string mylat, string mylong)
{
//This code reads the xml file into an XmlDocument Object so that I can loop through the nodes
XmlTextReader reader = new XmlTextReader("\\Program Files\\Project2\\locations.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();
XmlNode listnode = xmlDoc.DocumentElement;
XmlNode cursornode;
string mylocation, locationname,radius,latitude,longitude;
double dist, prevdist;
prevdist = 1000000.0;
mylocation = "";
for(int x = 0; x < listnode.ChildNodes.Count; x++)
{//This iterates through all of the locations one by one
//Set cursornode to the current location
cursornode = listnode.ChildNodes[x];
//Get info out of the node
locationname = cursornode.ChildNodes[0].InnerText;
radius = cursornode.ChildNodes[1].InnerText;
latitude = cursornode.ChildNodes[2].InnerText;
longitude = cursornode.ChildNodes[3].InnerText;
dist = getDist(mylat, mylong, latitude, longitude);
if(dist <= Convert.ToDouble(radius) && dist <= prevdist)
{
mylocation = locationname;
}
}
return mylocation;
}
private double getDist(string strLat1, string strLong1, string strLat2, string strLong2)
{//this method calculates the distance in miles between 2 sets of decimal lat&long coordinates
double dist = 0;
//convert string type degrees into double type radians
double lat1 = System.Convert.ToDouble(strLat1)*Math.PI/180.0;
double long1 = System.Convert.ToDouble(strLong1)*Math.PI/180.0;
double lat2 = System.Convert.ToDouble(strLat2)*Math.PI/180.0;
double long2 = System.Convert.ToDouble(strLong2)*Math.PI/180.0;
//this gynormous formula gives the great circle distance between to coordinate sets
//meaning that the distance calculated even takes into account the curvature of the earth
dist = Math.Cos(lat1)*Math.Cos(long1)*Math.Cos(lat2)*Math.Cos(long2);
dist += Math.Cos(lat1)*Math.Sin(long1)*Math.Cos(lat2)*Math.Sin(long2);
dist += Math.Sin(lat1)*Math.Sin(lat2);
dist = Math.Acos(dist);
dist *= 3963.1; //multiply by the radius of earth in miles
return Math.Round(dist,2);
}
private string getDir(string strLat1, string strLong1, string strLat2, string strLong2)
{//this method calculates the compass direction from the first set of coordinates to the second
string direction = "";
//convert string type degrees into double type degrees for comparisons
double lat1 = System.Convert.ToDouble(strLat1);
double long1 = System.Convert.ToDouble(strLong1);
double lat2 = System.Convert.ToDouble(strLat2);
double long2 = System.Convert.ToDouble(strLong2);
if(lat1 <= lat2)
{
direction += "N";
}
else
{
direction += "S";
}
if(long1 <= long2)
{
direction += "E";
}
else
{
direction += "W";
}
return direction;
}
private void updateButton_Click(object sender, System.EventArgs e)
{
if(broadcastMenuItem.Checked)
{//we are broadcasting, so update
// updateXML(); local version
doWebUpdateXML(); // web service version
}
doWebGetXML(); //WORKING!!!!
//refresh the contact list based on the local XML
displayList();
if(alertsMenuItem.Checked)
{//handle proximity alerts
doAlerts(2.0);
}
}
}
}