From f79ee0adc27a710ab45cff33018de8bee3a60b85 Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Mon, 4 Apr 2011 18:22:03 +0300 Subject: [PATCH] refactor LocationService --- .../GpsEmulator/MainWindow.xaml.cs | 2 +- .../GpsEmulatorClient/GeoCoordinateWatcher.cs | 2 +- main/MyFriendsAround.WP7/App.xaml.cs | 10 ++ main/MyFriendsAround.WP7/Model/Location.cs | 29 +++++ .../MyFriendsAround.WP7.csproj | 9 +- .../Service/ILocationService.cs | 19 +++ .../Service/LocationChangedEventArgs.cs | 18 +++ .../Service/LocationService.cs | 70 +++++++++++ .../Service/LocationStatusEventArgs.cs | 17 +++ .../Service/ServiceAgent.cs | 4 +- .../ViewModel/MainViewModel.cs | 114 +++++++++--------- .../ViewModel/PushPinModel.cs | 1 + main/MyFriendsAround.WP7/Views/MainPage.xaml | 12 +- main/MyFriendsAround.Web/Web.config | 2 +- main/MyFriendsAround.sln | 3 +- .../ServiceDefinition.csdef | 2 +- 16 files changed, 236 insertions(+), 78 deletions(-) create mode 100644 main/MyFriendsAround.WP7/Model/Location.cs create mode 100644 main/MyFriendsAround.WP7/Service/ILocationService.cs create mode 100644 main/MyFriendsAround.WP7/Service/LocationChangedEventArgs.cs create mode 100644 main/MyFriendsAround.WP7/Service/LocationService.cs create mode 100644 main/MyFriendsAround.WP7/Service/LocationStatusEventArgs.cs diff --git a/main/Libs/GpsEmulator/GpsEmulator/MainWindow.xaml.cs b/main/Libs/GpsEmulator/GpsEmulator/MainWindow.xaml.cs index a79e023..457501a 100644 --- a/main/Libs/GpsEmulator/GpsEmulator/MainWindow.xaml.cs +++ b/main/Libs/GpsEmulator/GpsEmulator/MainWindow.xaml.cs @@ -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(); } diff --git a/main/Libs/GpsEmulator/GpsEmulatorClient/GeoCoordinateWatcher.cs b/main/Libs/GpsEmulator/GpsEmulatorClient/GeoCoordinateWatcher.cs index f70ce02..e07ff33 100644 --- a/main/Libs/GpsEmulator/GpsEmulatorClient/GeoCoordinateWatcher.cs +++ b/main/Libs/GpsEmulator/GpsEmulatorClient/GeoCoordinateWatcher.cs @@ -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(client_OpenCompleted); client.GetCurrentPositionCompleted +=new EventHandler(client_GetCurrentPositionCompleted); ICommunicationObject commObject = client as ICommunicationObject; diff --git a/main/MyFriendsAround.WP7/App.xaml.cs b/main/MyFriendsAround.WP7/App.xaml.cs index 5805350..20581f8 100644 --- a/main/MyFriendsAround.WP7/App.xaml.cs +++ b/main/MyFriendsAround.WP7/App.xaml.cs @@ -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( new LocationService(), "LocationService"); + } + + + public static ILocationService LocationService + { + get { + return Container.Instance.Resolve("LocationService"); + } } // Code to execute when the application is launching (eg, from Start) diff --git a/main/MyFriendsAround.WP7/Model/Location.cs b/main/MyFriendsAround.WP7/Model/Location.cs new file mode 100644 index 0000000..fa74bf5 --- /dev/null +++ b/main/MyFriendsAround.WP7/Model/Location.cs @@ -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; + } + } +} diff --git a/main/MyFriendsAround.WP7/MyFriendsAround.WP7.csproj b/main/MyFriendsAround.WP7/MyFriendsAround.WP7.csproj index 237d814..e6849fd 100644 --- a/main/MyFriendsAround.WP7/MyFriendsAround.WP7.csproj +++ b/main/MyFriendsAround.WP7/MyFriendsAround.WP7.csproj @@ -159,6 +159,11 @@ + + + + + @@ -258,9 +263,7 @@ - - - + diff --git a/main/MyFriendsAround.WP7/Service/ILocationService.cs b/main/MyFriendsAround.WP7/Service/ILocationService.cs new file mode 100644 index 0000000..6da53da --- /dev/null +++ b/main/MyFriendsAround.WP7/Service/ILocationService.cs @@ -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 LocationChanged; + event EventHandler StatusChanged; + Location CurrentLocation { get; } + Location GetCurrentLocation(); + void Start(); + void Stop(); + } +} diff --git a/main/MyFriendsAround.WP7/Service/LocationChangedEventArgs.cs b/main/MyFriendsAround.WP7/Service/LocationChangedEventArgs.cs new file mode 100644 index 0000000..6d3c655 --- /dev/null +++ b/main/MyFriendsAround.WP7/Service/LocationChangedEventArgs.cs @@ -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; + } + } +} diff --git a/main/MyFriendsAround.WP7/Service/LocationService.cs b/main/MyFriendsAround.WP7/Service/LocationService.cs new file mode 100644 index 0000000..449cac6 --- /dev/null +++ b/main/MyFriendsAround.WP7/Service/LocationService.cs @@ -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 _gpsWatcher; + public event EventHandler LocationChanged; + public event EventHandler 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>(watcher_PositionChanged); + _gpsWatcher.StatusChanged += new EventHandler(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 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(); + } + + } +} diff --git a/main/MyFriendsAround.WP7/Service/LocationStatusEventArgs.cs b/main/MyFriendsAround.WP7/Service/LocationStatusEventArgs.cs new file mode 100644 index 0000000..43c02cf --- /dev/null +++ b/main/MyFriendsAround.WP7/Service/LocationStatusEventArgs.cs @@ -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; + } + } +} diff --git a/main/MyFriendsAround.WP7/Service/ServiceAgent.cs b/main/MyFriendsAround.WP7/Service/ServiceAgent.cs index 53c6ae8..491654c 100644 --- a/main/MyFriendsAround.WP7/Service/ServiceAgent.cs +++ b/main/MyFriendsAround.WP7/Service/ServiceAgent.cs @@ -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 } diff --git a/main/MyFriendsAround.WP7/ViewModel/MainViewModel.cs b/main/MyFriendsAround.WP7/ViewModel/MainViewModel.cs index 2e9dbfb..cbd943d 100644 --- a/main/MyFriendsAround.WP7/ViewModel/MainViewModel.cs +++ b/main/MyFriendsAround.WP7/ViewModel/MainViewModel.cs @@ -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 /// 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>(watcher_PositionChanged); - _gpsWatcher.StatusChanged += new EventHandler(watcher_StatusChanged); - - // - _gpsWatcher.Start(); + App.LocationService.LocationChanged += new EventHandler(LocationService_LocationChanged); + App.LocationService.StatusChanged += new EventHandler(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 _mynewlocation = new ObservableCollection(); + _mynewlocation.Add(new PushPinModel() + { + Location = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude), + PinUserName = "Me" + }); + MyLocationPushPins = _mynewlocation; + } + else + { + MyLocationPushPins = new ObservableCollection(); + } + } + + System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second); + } + void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs 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 _mynewlocation = new ObservableCollection(); _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 _gpsWatcher; - public string ApplicationTitle { @@ -213,12 +233,12 @@ namespace MyFriendsAround.WP7.ViewModel /// public const string GpsLocationPropertyName = "GpsLocation"; - private GeoCoordinate _gpsLocation = GeoCoordinate.Unknown; + private Location _gpsLocation = Location.Unknown; /// /// Gets the GpsLocation property. /// - public GeoCoordinate GpsLocation + public Location GpsLocation { get { @@ -239,37 +259,7 @@ namespace MyFriendsAround.WP7.ViewModel } } - - /// - /// The property's name. - /// - public const string GpsTimestampPropertyName = "GpsTimestamp"; - - private DateTimeOffset _gpsTimestamp = DateTimeOffset.MinValue; - - /// - /// Gets the GpsTimestamp property. - /// - public DateTimeOffset GpsTimestamp - { - get - { - return _gpsTimestamp; - } - - set - { - if (_gpsTimestamp == value) - { - return; - } - - _gpsTimestamp = value; - - // Update bindings, no broadcast - RaisePropertyChanged(GpsTimestampPropertyName); - } - } + /// /// The 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(); } @@ -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 diff --git a/main/MyFriendsAround.WP7/ViewModel/PushPinModel.cs b/main/MyFriendsAround.WP7/ViewModel/PushPinModel.cs index 64891db..171da7e 100644 --- a/main/MyFriendsAround.WP7/ViewModel/PushPinModel.cs +++ b/main/MyFriendsAround.WP7/ViewModel/PushPinModel.cs @@ -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 { diff --git a/main/MyFriendsAround.WP7/Views/MainPage.xaml b/main/MyFriendsAround.WP7/Views/MainPage.xaml index ef9ba31..0292932 100644 --- a/main/MyFriendsAround.WP7/Views/MainPage.xaml +++ b/main/MyFriendsAround.WP7/Views/MainPage.xaml @@ -28,10 +28,10 @@ - - + @@ -88,10 +88,6 @@ Template="{StaticResource PushpinControlTemplate2}"> - - diff --git a/main/MyFriendsAround.Web/Web.config b/main/MyFriendsAround.Web/Web.config index 5818f40..83b81f2 100644 --- a/main/MyFriendsAround.Web/Web.config +++ b/main/MyFriendsAround.Web/Web.config @@ -9,7 +9,7 @@ + connectionString="metadata=res://*/MyFriendsModel.csdl|res://*/MyFriendsModel.ssdl|res://*/MyFriendsModel.msl;provider=System.Data.SqlClient;provider connection string="Server=ewzculop8c.database.windows.net;Database=myfriendsaround;User ID=claudiu@ewzculop8c;Password=Endava2011;Trusted_Connection=False;Encrypt=True"" providerName="System.Data.EntityClient" /> diff --git a/main/MyFriendsAround.sln b/main/MyFriendsAround.sln index c6575a6..0727136 100644 --- a/main/MyFriendsAround.sln +++ b/main/MyFriendsAround.sln @@ -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 diff --git a/main/MyFriendsAroundWindowsAzure/ServiceDefinition.csdef b/main/MyFriendsAroundWindowsAzure/ServiceDefinition.csdef index 262c7a8..ee367b5 100644 --- a/main/MyFriendsAroundWindowsAzure/ServiceDefinition.csdef +++ b/main/MyFriendsAroundWindowsAzure/ServiceDefinition.csdef @@ -1,6 +1,6 @@  - +