refactor LocationService

This commit is contained in:
2011-04-04 18:22:03 +03:00
parent 2de2c2485e
commit f79ee0adc2
16 changed files with 236 additions and 78 deletions
@@ -43,7 +43,7 @@ namespace GpsEmulator
try
{
// setup the GPS host service
host = new ServiceHost(this, new Uri("http://localhost:8192/"));
host = new ServiceHost(this, new Uri("http://localhost:9192/"));
host.AddServiceEndpoint(typeof(IGpsEmulatorService), new BasicHttpBinding(BasicHttpSecurityMode.None), "GpsEmulator");
host.Open();
}
@@ -167,7 +167,7 @@ namespace GpsEmulatorClient
status = GeoPositionStatus.Initializing;
client = new GpsEmulatorServiceClient(
new BasicHttpBinding(BasicHttpSecurityMode.None),
new EndpointAddress("http://localhost:8192/GpsEmulator")); // change end point to real IP when testing on a real device
new EndpointAddress("http://localhost:9192/GpsEmulator")); // change end point to real IP when testing on a real device
client.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_OpenCompleted);
client.GetCurrentPositionCompleted +=new EventHandler<GetCurrentPositionCompletedEventArgs>(client_GetCurrentPositionCompleted);
ICommunicationObject commObject = client as ICommunicationObject;
+10
View File
@@ -21,6 +21,7 @@ using MyFriendsAround.WP7.Utils;
using GalaSoft.MvvmLight.Threading;
using MyFriendsAround.WP7.Views;
using NetworkDetection;
using MyFriendsAround.WP7.Service;
namespace MyFriendsAround.WP7
@@ -49,6 +50,15 @@ namespace MyFriendsAround.WP7
//register ViewModelLocator
Container.Instance.RegisterInstance(typeof(ViewModelLocator), "ViewModelLocator");
Container.Instance.RegisterInstance<ILocationService>( new LocationService(), "LocationService");
}
public static ILocationService LocationService
{
get {
return Container.Instance.Resolve<ILocationService>("LocationService");
}
}
// Code to execute when the application is launching (eg, from Start)
@@ -0,0 +1,29 @@
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyFriendsAround.WP7.Model
{
public class Location
{
public static readonly Location Unknown;
public DateTimeOffset Timestamp { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Location(double latitude, double longitude, DateTimeOffset timestamp)
{
Latitude = latitude;
Longitude = longitude;
Timestamp = timestamp;
}
}
}
@@ -159,6 +159,11 @@
</Compile>
<Compile Include="Helpers\Navigation\IPageNavigation.cs" />
<Compile Include="Helpers\Navigation\PageNavigation.cs" />
<Compile Include="Service\ILocationService.cs" />
<Compile Include="Model\Location.cs" />
<Compile Include="Service\LocationChangedEventArgs.cs" />
<Compile Include="Service\LocationService.cs" />
<Compile Include="Service\LocationStatusEventArgs.cs" />
<Compile Include="Utils\ApplicationExtensions.cs" />
<Compile Include="Utils\IsolatedStorageHelper.cs" />
<Compile Include="Utils\LittleWatson.cs">
@@ -258,9 +263,7 @@
<Content Include="Toolkit.Content\ApplicationBar.Cancel.png" />
<Content Include="Toolkit.Content\ApplicationBar.Check.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Device.Location;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.Service
{
public interface ILocationService
{
event EventHandler<LocationChangedEventArgs> LocationChanged;
event EventHandler<LocationStatusEventArgs> StatusChanged;
Location CurrentLocation { get; }
Location GetCurrentLocation();
void Start();
void Stop();
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.Service
{
public class LocationChangedEventArgs: EventArgs
{
public Location Location { get; private set; }
public LocationChangedEventArgs(Location location)
{
Location = location;
}
}
}
@@ -0,0 +1,70 @@
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyFriendsAround.WP7.Model;
using System.Device.Location;
namespace MyFriendsAround.WP7.Service
{
public class LocationService : ILocationService
{
private IGeoPositionWatcher<GeoCoordinate> _gpsWatcher;
public event EventHandler<LocationChangedEventArgs> LocationChanged;
public event EventHandler<LocationStatusEventArgs> StatusChanged;
public Location CurrentLocation { get; private set; }
public LocationService()
{
#if GPS_EMULATOR
_gpsWatcher = new GpsEmulatorClient.GeoCoordinateWatcher();
#else
_gpsWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 10
};
#endif
_gpsWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
_gpsWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs args)
{
//
if (StatusChanged != null)
StatusChanged(sender, new LocationStatusEventArgs(args.Status));
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> args)
{
if (LocationChanged != null)
LocationChanged(sender, new LocationChangedEventArgs(
new Location(args.Position.Location.Latitude, args.Position.Location.Longitude, args.Position.Timestamp)
));
}
public Location GetCurrentLocation()
{
return CurrentLocation;
}
public void Start()
{
_gpsWatcher.Start();
}
public void Stop()
{
_gpsWatcher.Stop();
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Device.Location;
namespace MyFriendsAround.WP7.Service
{
public class LocationStatusEventArgs : EventArgs
{
public GeoPositionStatus Status { get; private set; }
public LocationStatusEventArgs(GeoPositionStatus status)
{
Status = status;
}
}
}
@@ -16,8 +16,8 @@ namespace MyFriendsAround.WP7.Service
static ServiceAgent()
{
baseUrl = "http://myfriendsaround.cloudapp.net/myfriends";//live azure
//baseUrl = "http://127.0.0.1:8086/myfriends";//running in local azure emulator
//baseUrl = "http://myfriendsaround.cloudapp.net/myfriends";//live azure
baseUrl = "http://127.0.0.1:82/myfriends";//running in local azure emulator
//baseUrl = "http://localhost.:55672/myfriends";//for local asp.net mvc use
}
@@ -32,6 +32,7 @@ using MyFriendsAround.WP7.Views;
using NetworkDetection;
using Newtonsoft.Json;
using Microsoft.Phone.Tasks;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.ViewModel
{
@@ -55,6 +56,7 @@ namespace MyFriendsAround.WP7.ViewModel
/// </summary>
public MainViewModel()
{
GpsLocation = Location.Unknown;
//
MainLoadCommand = new RelayCommand(() => MainLoad());
PublishLocationCommand = new RelayCommand(() => PublishLocationAction());
@@ -112,39 +114,59 @@ namespace MyFriendsAround.WP7.ViewModel
private void InitGps()
{
#if GPS_EMULATOR
_gpsWatcher = new GpsEmulatorClient.GeoCoordinateWatcher();
#else
_gpsWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 10
};
#endif
_gpsWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
_gpsWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
//
_gpsWatcher.Start();
App.LocationService.LocationChanged += new EventHandler<LocationChangedEventArgs>(LocationService_LocationChanged);
App.LocationService.StatusChanged += new EventHandler<LocationStatusEventArgs>(LocationService_StatusChanged);
App.LocationService.Start();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
void LocationService_StatusChanged(object sender, LocationStatusEventArgs e)
{
GpsStatus = e.Status;
}
void LocationService_LocationChanged(object sender, LocationChangedEventArgs e)
{
if (e.Location != Location.Unknown)
{
GpsLocation = e.Location;
if (LastBoundRect!= null && LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
.5,
.5)))
{
ObservableCollection<PushPinModel> _mynewlocation = new ObservableCollection<PushPinModel>();
_mynewlocation.Add(new PushPinModel()
{
Location = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
PinUserName = "Me"
});
MyLocationPushPins = _mynewlocation;
}
else
{
MyLocationPushPins = new ObservableCollection<PushPinModel>();
}
}
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location != GeoCoordinate.Unknown)
{
GpsTimestamp = e.Position.Timestamp;
GpsLocation = e.Position.Location;
GpsLocation = new Location(
e.Position.Location.Latitude,
e.Position.Location.Longitude,
e.Position.Timestamp);
if (LastBoundRect.Intersects(new LocationRect(GpsLocation, .5, .5)))
if (LastBoundRect!=null && LastBoundRect.Intersects(new LocationRect(e.Position.Location, .5, .5)))
{
ObservableCollection<PushPinModel> _mynewlocation = new ObservableCollection<PushPinModel>();
_mynewlocation.Add(new PushPinModel()
{
Location = GpsLocation,
Location = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
PinUserName = "Me"
});
MyLocationPushPins = _mynewlocation;
@@ -166,8 +188,6 @@ namespace MyFriendsAround.WP7.ViewModel
#region Properties & Fields
private PhotoChooserTask photoChooserTask;
private IGeoPositionWatcher<GeoCoordinate> _gpsWatcher;
public string ApplicationTitle
{
@@ -213,12 +233,12 @@ namespace MyFriendsAround.WP7.ViewModel
/// </summary>
public const string GpsLocationPropertyName = "GpsLocation";
private GeoCoordinate _gpsLocation = GeoCoordinate.Unknown;
private Location _gpsLocation = Location.Unknown;
/// <summary>
/// Gets the GpsLocation property.
/// </summary>
public GeoCoordinate GpsLocation
public Location GpsLocation
{
get
{
@@ -239,37 +259,7 @@ namespace MyFriendsAround.WP7.ViewModel
}
}
/// <summary>
/// The <see cref="GpsTimestamp" /> property's name.
/// </summary>
public const string GpsTimestampPropertyName = "GpsTimestamp";
private DateTimeOffset _gpsTimestamp = DateTimeOffset.MinValue;
/// <summary>
/// Gets the GpsTimestamp property.
/// </summary>
public DateTimeOffset GpsTimestamp
{
get
{
return _gpsTimestamp;
}
set
{
if (_gpsTimestamp == value)
{
return;
}
_gpsTimestamp = value;
// Update bindings, no broadcast
RaisePropertyChanged(GpsTimestampPropertyName);
}
}
/// <summary>
/// The <see cref="GpsStatus" /> property's name.
@@ -616,11 +606,11 @@ namespace MyFriendsAround.WP7.ViewModel
private void ShowMyLocation()
{
//
if (GpsLocation != GeoCoordinate.Unknown &&
if (GpsLocation != Location.Unknown &&
GpsStatus == GeoPositionStatus.Ready
)
{
MapCenter = GpsLocation;
MapCenter = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude);
}
}
@@ -633,14 +623,20 @@ namespace MyFriendsAround.WP7.ViewModel
//filter visible pushpins
foreach (PushPinModel pushPin in PushPins)
{
if (LastBoundRect.Intersects(new LocationRect(pushPin.Location, .5, .5)))
if (LastBoundRect!=null && LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(pushPin.Location.Latitude, pushPin.Location.Longitude),
.5, .5)))
{
_newVisiblePushPins.Add(pushPin);
}
}
VisiblePushPins = _newVisiblePushPins;
//
if (!LastBoundRect.Intersects(new LocationRect(GpsLocation, .5, .5)))
if (GpsLocation == Location.Unknown ||
LastBoundRect == null ||
!LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
.5, .5)))
{
MyLocationPushPins = new ObservableCollection<PushPinModel>();
}
@@ -809,7 +805,7 @@ namespace MyFriendsAround.WP7.ViewModel
private void PublishLocationAction()
{
if (GpsLocation != GeoCoordinate.Unknown)
if (GpsLocation != Location.Unknown)
{
Friend myInfo = new Friend();
myInfo.Id = Identification.GetDeviceId();
@@ -947,7 +943,7 @@ namespace MyFriendsAround.WP7.ViewModel
// Clean up if needed
base.Cleanup();
_gpsWatcher.Stop();
App.LocationService.Stop();
}
#endregion
@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Device.Location;
using System.Linq;
using System.Text;
using MyFriendsAround.WP7.Model;
namespace MyFriendsAround.WP7.ViewModel
{
+4 -8
View File
@@ -28,10 +28,10 @@
<phone:PhoneApplicationPage.Resources>
<ControlTemplate x:Key="PushpinControlTemplate1"
TargetType="my:Pushpin">
<Grid Height="32"
Width="32"
Margin="-16,-16,0,0">
<Ellipse Fill="#FFA3A3BE"
<Grid Height="24"
Width="24"
Margin="-12,-12,0,0">
<Ellipse Fill="#FF12A5C7"
Stroke="Black"
Margin="0" />
<Image Source="{Binding PinSource}" />
@@ -88,10 +88,6 @@
Template="{StaticResource PushpinControlTemplate2}">
</my:Pushpin>
</DataTemplate>
<my:MapItemsControl x:Name="GroupAPins"
ItemTemplate="{StaticResource LogoTemplate}"
ItemsSource="{Binding PushPins}">
</my:MapItemsControl>
</Grid.Resources>
+1 -1
View File
@@ -9,7 +9,7 @@
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|myfriendsaround.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<!--<add name="myfriendsaroundConnectionString" connectionString="Data Source=ewzculop8c.database.windows.net;Initial Catalog=myfriendsaround;User ID=user;Password=password" providerName="System.Data.SqlClient" />-->
<add name="MyFriendsModelContainer"
connectionString="metadata=res://*/MyFriendsModel.csdl|res://*/MyFriendsModel.ssdl|res://*/MyFriendsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=ewzculop8c.database.windows.net;Database=myfriendsaround;User ID=[USER]@ewzculop8c;Password=[PASSWORD];Trusted_Connection=False;Encrypt=True&quot;" providerName="System.Data.EntityClient" />
connectionString="metadata=res://*/MyFriendsModel.csdl|res://*/MyFriendsModel.ssdl|res://*/MyFriendsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=ewzculop8c.database.windows.net;Database=myfriendsaround;User ID=claudiu@ewzculop8c;Password=Endava2011;Trusted_Connection=False;Encrypt=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
+1 -2
View File
@@ -13,6 +13,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFriendsAround.BLL", "MyFr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFriendsAround.WP7", "MyFriendsAround.WP7\MyFriendsAround.WP7.csproj", "{B690843F-9163-4292-9450-8855AAA3FD5B}"
ProjectSection(ProjectDependencies) = postProject
{566AEE14-134C-4EE4-93B5-1FFB021DE678} = {566AEE14-134C-4EE4-93B5-1FFB021DE678}
{41FDB0B4-0F93-4D1C-99C1-57F4A7E7EF3D} = {41FDB0B4-0F93-4D1C-99C1-57F4A7E7EF3D}
EndProjectSection
EndProject
@@ -117,7 +118,6 @@ Global
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Any CPU.Deploy.0 = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Mixed Platforms.ActiveCfg = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Mixed Platforms.Build.0 = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Mixed Platforms.Deploy.0 = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|x86.ActiveCfg = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Release|Any CPU.Build.0 = Release|Any CPU
@@ -232,7 +232,6 @@ Global
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Any CPU.Deploy.0 = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Mixed Platforms.ActiveCfg = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Mixed Platforms.Build.0 = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Mixed Platforms.Deploy.0 = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|x86.ActiveCfg = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Release|Any CPU.Build.0 = Release|Any CPU
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyFriendsAroundWindowsAzure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="MyFriendsAround.Web">
<WebRole name="MyFriendsAround.Web" vmsize="ExtraSmall">
<Sites>
<Site name="Web">
<Bindings>