using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using J2i.Net.WiFiPositioning;
using J2i.Net.LocationSettings;
namespace SimpleClient
{
public partial class SimpleClientForm : Form
{
delegate void DisplayLocationDelegate(WiFiLocation loc);
delegate void VoidDelegate();
Wps _wps = null;
RegistrySettings _registry = new RegistrySettings();
VoidDelegate _enableMenus;
string _userName;
string _realm;
bool _stopPeriodic = false;
ResultCode _lastResultCode;
DisplayLocationDelegate _displayLocation;
public SimpleClientForm()
{
ResultCode rc;
InitializeComponent();
_displayLocation = new DisplayLocationDelegate(this.UpdatePosition);
_wps = new Wps(_registry.SkyhookUser, _registry.SkyhookRealm);
_wps.WiFiLocationCallback = new WiFiLocationCallback(UpdatePosition);
_enableMenus = new VoidDelegate(EnableMenuItems);
rc = _wps.BeginTiling("\\temp\\", 500000, 4000000);
}
Continuation UpdatePosition(ResultCode rc, WiFiLocation loc)
{
UpdatePosition(loc);
if (_stopPeriodic)
{
_stopPeriodic = false;
EnableMenuItems();
return Continuation.Stop;
}
return Continuation.Continue;
}
void AppendListViewItem(string elementName, string elementValue)
{
ListViewItem lvi = new ListViewItem(new string[]{elementName,elementValue});
lvPosition.Items.Add(lvi);
}
void UpdatePosition()
{
WiFiLocation loc;
_lastResultCode = _wps.GetWiFiLocation(StreetAddressLookupType.NoStreet,out loc);
this.UpdatePosition(loc);
}
void EnableMenuItems()
{
if (this.InvokeRequired)
Invoke(_enableMenus);
else
{
miFullAddress.Enabled = true;
miNow.Enabled = true;
miClose.Enabled = true;
}
}
void UpdatePosition(WiFiLocation loc)
{
if (this.InvokeRequired)
{
this.Invoke(_displayLocation, new object[] { loc });
}
else
{
statusBar.Text = _lastResultCode.ToString();
lvPosition.Items.Clear();
AppendListViewItem("TimeStamp", DateTime.Now.ToLongTimeString());
if (loc == null)
{
AppendListViewItem("Latitude", "Unknown");
AppendListViewItem("Longitude", "Unknown");
}
else
{
AppendListViewItem("Latitude", loc.Latitude.ToString("0.000000"));
AppendListViewItem("Longitude", loc.Longitude.ToString("0.000000"));
AppendListViewItem("H.P.E.", loc.HorizontalPositioningError.ToString());
AppendListViewItem("Speed", String.Format("{0} km/h", loc.Speed));
AppendListViewItem("Bearing", String.Format("{0} degrees", loc.Bearing));
AppendListViewItem("Access Point Count", loc.AccessPointCount.ToString());
if (loc.StreetAddress != null)
{
AppendListViewItem("Street Number", loc.StreetAddress.StreetNumber);
AppendListViewItem("Address Line", loc.StreetAddress.AddressLine);
AppendListViewItem("County", loc.StreetAddress.County);
AppendListViewItem("State", loc.StreetAddress.State.Name);
AppendListViewItem("City", loc.StreetAddress.City);
AppendListViewItem("Postal Code", loc.StreetAddress.PostalCode);
AppendListViewItem("Country", loc.StreetAddress.Country.Name);
AppendListViewItem("Province", loc.StreetAddress.Province);
AppendListViewItem("Region", loc.StreetAddress.Region);
}
}
}
}
private void miUpdateLocation_Click(object sender, EventArgs e)
{
}
private void miNow_Click(object sender, EventArgs e)
{
this.UpdatePosition();
}
void StartPeriodicLocation()
{
ResultCode rc = _wps.BeginPeriodicLocation(StreetAddressLookupType.NoStreet, 5000, 0);
}
void PeriodicLocationEnded(IAsyncResult ar)
{
}
private void miPeriodically_Click(object sender, EventArgs e)
{
if (this.miPeriodically.Checked)
{
miPeriodically.Checked = false;
_stopPeriodic = true;
}
else
{
this.miPeriodically.Checked = true;
miNow.Enabled = false;
miFullAddress.Enabled = false;
miClose.Enabled = false;
VoidDelegate v = new VoidDelegate(StartPeriodicLocation);
ThreadStart ts = new ThreadStart(StartPeriodicLocation);
Thread t = new Thread(ts);
t.IsBackground = true;
t.Start();
}
}
private void SimpleClientForm_Load(object sender, EventArgs e)
{
//_wps = new Wps(_userName, _realm);
}
private void miClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void miFullAddress_Click(object sender, EventArgs e)
{
WiFiLocation loc;
_lastResultCode = _wps.GetWiFiLocation(StreetAddressLookupType.FullStreet, out loc);
this.UpdatePosition(loc);
}
}
}