using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using Comal.Classes; using Comal.Stores; using InABox.Clients; using InABox.Configuration; using InABox.Core; using Syncfusion.UI.Xaml.Maps; namespace PRSDesktop { public class MapMarker { public Guid ID { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string Code { get; set; } public string Description { get; set; } public DateTime Updated { get; set; } public string UpdatedBy { get; set; } public string DeviceID { get; set; } public Brush Background { get; internal set; } } public class ViewModel { private readonly List _markers = new(); private MapMarker _selected; public ViewModel() { Markers = new ObservableCollection(); } public ObservableCollection Markers { get; } public MapMarker Selected { get => _selected; set { _selected = value; Refresh(); } } public void Add(MapMarker marker, bool refresh = false) { _markers.Add(marker); if (refresh) Refresh(); } public void Clear() { _selected = null; _markers.Clear(); Refresh(); } public void Refresh() { Markers.Clear(); foreach (var marker in _markers) { marker.Background = new SolidColorBrush(marker == _selected ? Colors.Yellow : Colors.Orange); if (marker != _selected) Markers.Add(marker); } if (_selected != null) Markers.Add(_selected); } } public class ImageryLayerExt : ImageryLayer { protected override string GetUri(int X, int Y, int Scale) { var link = "http://mt1.google.com/vt/lyrs=y&x=" + X + "&y=" + Y + "&z=" + Scale; return link; } } public class GPSHistory { public DateTime Date { get; set; } public string Description { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } } /// /// Interaction logic for MapsPanel.xaml /// public partial class MapsPanel : UserControl, IPanel { private LiveMapsSettings _settings; private bool bFirst = true; private readonly ViewModel viewmodel = new(); private readonly int ZOOMEDIN = 16; public MapsPanel() { InitializeComponent(); DataContext = viewmodel; //ausgov.Uri = "c:\\development\\maps\\MB_2011_WA.shp"; } public bool IsReady { get; set; } public void CreateToolbarButtons(IPanelHost host) { } public string SectionName => "Maps"; public DataModel DataModel(Selection selection) { return new MapsDataModel(); } public void Heartbeat(TimeSpan time) { } public void Refresh() { var type = ViewType.SelectedItem as string; if (!string.IsNullOrWhiteSpace(type)) { var grpid = ((KeyValuePair)ViewGroup.SelectedItem).Key; if (type.Equals("Equipment")) { var filter = new Filter(x => x.TrackerLink.Location.Timestamp).IsNotEqualTo(DateTime.MinValue); if (!Security.IsAllowed()) filter = filter.And(x => x.Private).IsEqualTo(false); if (grpid != Guid.Empty) filter = filter.And(x => x.GroupLink.ID).IsEqualTo(grpid); LoadMarkers( filter, x => x.ID, x => x.Code, x => x.Description, x => x.TrackerLink.DeviceID, x => x.TrackerLink.Location.Latitude, x => x.TrackerLink.Location.Longitude, x => x.TrackerLink.Description, x => x.TrackerLink.Location.Timestamp ); } else if (type.Equals("Jobs")) { var filter = new Filter(x => x.SiteAddress.Location.Timestamp).IsNotEqualTo(DateTime.MinValue); if (grpid != Guid.Empty) filter = filter.And(x => x.JobStatus.ID).IsEqualTo(grpid); LoadMarkers( filter, x => x.ID, x => x.JobNumber, x => x.Name, null, x => x.SiteAddress.Location.Latitude, x => x.SiteAddress.Location.Longitude, x => x.LastUpdateBy, x => x.SiteAddress.Location.Timestamp ); } else if (type.Equals("TimeSheets")) { if (grpid == Guid.Empty) // Starts LoadMarkers( new Filter(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.StartLocation.Timestamp) .IsNotEqualTo(new TimeSpan(0)), x => x.EmployeeLink.ID, x => x.EmployeeLink.Code, x => x.EmployeeLink.Name, null, x => x.StartLocation.Latitude, x => x.StartLocation.Longitude, x => x.SoftwareVersion, x => x.StartLocation.Timestamp ); else if (grpid == CoreUtils.FullGuid) // Finishes LoadMarkers( new Filter(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.Finish).IsNotEqualTo(new TimeSpan(0)) .And(x => x.FinishLocation.Timestamp).IsNotEqualTo(new TimeSpan(0)), x => x.EmployeeLink.ID, x => x.EmployeeLink.Code, x => x.EmployeeLink.Name, null, x => x.FinishLocation.Latitude, x => x.FinishLocation.Longitude, x => x.SoftwareVersion, x => x.FinishLocation.Timestamp ); else // Open LoadMarkers( new Filter(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.Finish).IsEqualTo(new TimeSpan(0)) .And(x => x.StartLocation.Timestamp).IsNotEqualTo(new TimeSpan(0)), x => x.EmployeeLink.ID, x => x.EmployeeLink.Code, x => x.EmployeeLink.Name, null, x => x.StartLocation.Latitude, x => x.StartLocation.Longitude, x => x.SoftwareVersion, x => x.StartLocation.Timestamp ); } else if (type.Equals("Trackers")) { var filter = new Filter(x => x.Location.Timestamp).IsNotEqualTo(DateTime.MinValue); if (grpid != Guid.Empty) filter = filter.And(x => x.Type.ID).IsEqualTo(grpid); LoadMarkers( filter, x => x.ID, x => x.DeviceID, x => x.Description, x => x.DeviceID, x => x.Location.Latitude, x => x.Location.Longitude, x => x.LastUpdateBy, x => x.Location.Timestamp ); } } } public Dictionary Selected() { return new Dictionary(); } public void Setup() { _settings = new UserConfiguration().Load(); if (ClientFactory.IsSupported()) ViewType.Items.Add("Equipment"); if (ClientFactory.IsSupported()) ViewType.Items.Add("Jobs"); if (ClientFactory.IsSupported()) ViewType.Items.Add("TimeSheets"); if (ClientFactory.IsSupported()) ViewType.Items.Add("Trackers"); ViewType.SelectedIndex = ViewType.Items.Count > _settings.ViewType ? _settings.ViewType : -1; ViewType.SelectionChanged += ViewTypeSelectionChanged; var groups = ViewType.SelectedIndex > -1 ? LoadGroups(ViewType.SelectedItem as string) : LoadGroups(""); ViewGroup.ItemsSource = groups; ViewGroup.SelectedIndex = ViewGroup.Items.Count > _settings.ViewGroup ? _settings.ViewGroup : -1; ViewGroup.SelectionChanged += ViewGroupSelectionChanged; } public void Shutdown() { } public event DataModelUpdateEvent OnUpdateDataModel; public Dictionary DataEnvironment() { return new Dictionary(); } private void LoadGroups(string all, Dictionary results, Expression> description) where TGroup : Entity, IRemotable, IPersistent, new() { results.Clear(); results.Add(Guid.Empty, all); var groups = new Client().Query( LookupFactory.DefineFilter(), LookupFactory.DefineColumns(), LookupFactory.DefineSort() ); foreach (var row in groups.Rows) results[row.Get(x => x.ID)] = row.Get(description); } private void ViewTypeSelectionChanged(object sender, SelectionChangedEventArgs e) { var sel = e.AddedItems.Count > 0 ? e.AddedItems[0] as string : null; if (string.IsNullOrWhiteSpace(sel)) return; var groups = LoadGroups(sel); ViewGroup.SelectionChanged -= ViewGroupSelectionChanged; ViewGroup.ItemsSource = groups; ViewGroup.SelectionChanged += ViewGroupSelectionChanged; ViewGroup.SelectedIndex = groups.Any() ? 0 : -1; } private Dictionary LoadGroups(string sel) { var result = new Dictionary(); if (sel.Equals("Equipment")) { LoadGroups("All Equipment", result, x => x.Description); } else if (sel.Equals("Jobs")) { LoadGroups("All Jobs", result, x => x.Description); } else if (sel.Equals("TimeSheets")) { result[Guid.NewGuid()] = "Open TimeSheets"; result[Guid.Empty] = "TimeSheet Starts"; result[CoreUtils.FullGuid] = "TimeSheet Finishes"; } else if (sel.Equals("Trackers")) { LoadGroups("All Trackers", result, x => x.Description); } return result; } private void ViewGroupSelectionChanged(object sender, SelectionChangedEventArgs e) { _settings.ViewType = ViewType.SelectedIndex; _settings.ViewGroup = ViewGroup.SelectedIndex; new UserConfiguration().Save(_settings); Refresh(); } private void LoadDayMarkers() { viewmodel.Markers.Clear(); } private void LoadMarkers(Filter filter, Expression> guid, Expression> code, Expression> description, Expression> deviceid, Expression> latitude, Expression> longitude, Expression> updatedby, Expression> timestamp, bool first = true) where T : Entity, IRemotable, IPersistent, new() { viewmodel.Clear(); var columns = new Columns(guid, code, description, latitude, longitude, timestamp, updatedby); if (deviceid != null && !columns.ColumnNames().Contains(CoreUtils.GetFullPropertyName(deviceid, "."))) columns.Add(deviceid); new Client().Query( filter, columns, null, (table, error) => { Dispatcher.Invoke(() => { viewmodel.Markers.Clear(); if (error != null) { MessageBox.Show(error.Message); } else { var values = new Dictionary(); foreach (var row in table.Rows) { var id = (Guid)row.Get(guid); var time = (DateTime)row.Get(timestamp); if (!values.ContainsKey(id) || (first ? values[id].TimeStamp < time : values[id].TimeStamp > time)) values[id] = new DbMarker( code != null ? (string)row.Get(code) : "", (string)row.Get(description), time, (double)row.Get(latitude), (double)row.Get(longitude), (string)row.Get(updatedby), deviceid != null ? (string)row.Get(deviceid) : "" ); } foreach (var key in values.Keys) viewmodel.Add( new MapMarker { ID = key, Code = values[key].Code, Latitude = string.Format("{0:F6}", values[key].Latitude), Longitude = string.Format("{0:F6}", values[key].Longitude), Description = values[key].Description, Updated = values[key].TimeStamp, UpdatedBy = values[key].UpdatedBy, DeviceID = values[key].DeviceID } ); viewmodel.Refresh(); } //if (bFirst) ResetZoom(); bFirst = false; LoadMarkerList(); }); } ); } private void ResetZoom() { var nwLon = double.MaxValue; var nwLat = double.MinValue; var seLon = double.MinValue; var seLat = double.MaxValue; foreach (var marker in viewmodel.Markers) { var lat = double.Parse(marker.Latitude); var lon = double.Parse(marker.Longitude); if (lat != 0.0F && lon != 0.00) { nwLat = lat > nwLat ? lat : nwLat; seLat = lat < seLat ? lat : seLat; nwLon = lon < nwLon ? lon : nwLon; seLon = lon > seLon ? lon : seLon; } } var layer = Map.Layers[0] as ImageryLayer; var cLat = (nwLat + seLat) / 2.0F; var cLon = (nwLon + seLon) / 2.0F; layer.Center = new Point(cLat, cLon); layer.DistanceType = DistanceType.KiloMeter; var c = new Location { Latitude = cLat, Longitude = cLon }; var nw = new Location { Latitude = nwLat, Longitude = nwLon }; layer.Radius = c.DistanceTo(nw, UnitOfLength.Kilometers) / 2.0F; } private void ZoomIn_Click(object sender, RoutedEventArgs e) { CenterMap(); if (Map.ZoomLevel < 20) { Map.ZoomLevel++; LoadMarkerList(); } } private void ZoomOut_Click(object sender, RoutedEventArgs e) { CenterMap(); if (Map.ZoomLevel > 1) { Map.ZoomLevel--; LoadMarkerList(); } } private void Map_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { //CenterMap(); if (e.Delta > 0) { if (Zoom.Value < 20) Zoom.Value++; e.Handled = true; } else if (e.Delta < 0) { if (Zoom.Value > 1) Zoom.Value--; e.Handled = true; } } private void CenterMap() { var layer = Map.Layers[0] as ImageryLayer; var center = layer.GetLatLonFromPoint(new Point(Map.ActualWidth / 2.0F, Map.ActualHeight / 2.0F)); layer.Center = new Point(center.Y, center.X); } private void Map_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { var layer = Map.Layers[0] as ImageryLayer; var center = layer.GetLatLonFromPoint(e.GetPosition(Map)); layer.Center = new Point(center.Y, center.X); if (Map.ZoomLevel < 20) { Map.ZoomLevel++; LoadMarkerList(); } e.Handled = true; } private void Image_PreviewMouseDown(object sender, MouseButtonEventArgs e) { var border = sender as Border; //Point pBorder = border.TransformToAncestor(Map).Transform(new Point(0, 0)); //var left = pBorder.X - 2; //var top = pBorder.Y - 2; //var right = left + border.ActualWidth + 4; //var bottom = top + border.ActualHeight + 4; //ImageryLayer layer = Map.Layers[0] as ImageryLayer; //var nw = layer.GetLatLonFromPoint(new Point(left, top)); //var se = layer.GetLatLonFromPoint(new Point(right, bottom)); var found = border.DataContext as MapMarker; Markers.SelectedItem = found; if (e.ClickCount > 1 && e.ButtonState == MouseButtonState.Pressed && e.ChangedButton == MouseButton.Left) ZoomToMarker(found); //foreach (var marker in viewmodel.Markers) //{ // if (IsMarkerVisible(marker, nw, se)) // { // found = marker; // break; // } //} //if (found != null) //{ // Description.Text = found.Description; // LastUpdate.Text = String.Format("{0:dd MMM yy hh:mm:ss}", found.Updated); // Device.Text = found.UpdatedBy; // History.Visibility = String.IsNullOrWhiteSpace(found.DeviceID) ? Visibility.Collapsed : Visibility.Visible; // LoadDeviceHistory(found.DeviceID); // viewmodel.Selected = found; //} //else //{ // Description.Text = ""; // LastUpdate.Text = ""; // Device.Text = ""; // History.Visibility = Visibility.Collapsed; //} } private void Date_DateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var marker = Markers.SelectedItem as MapMarker; if (marker == null) return; LoadDeviceHistory(marker.DeviceID); } private void LoadDeviceHistory(string deviceID) { History.ItemsSource = null; var movements = new ObservableCollection(); History.ItemsSource = movements; new Client().Query( new Filter(x => x.DeviceID).IsEqualTo(deviceID) .And(x => x.Location.Timestamp).IsGreaterThanOrEqualTo(Date.Date.Date) .And(x => x.Location.Timestamp).IsLessThan(Date.Date.Date.AddDays(1)), null, new SortOrder(x => x.Location.Timestamp, SortDirection.Descending), (table, error) => { if (table != null) { var updates = new List(); var last = new Location { Latitude = 0.0F, Longitude = 0.0F }; var q = new List(); for (var i = 0; i < Math.Min(3, table.Rows.Count); i++) q.Add(table.Rows[i]); if (q.Count > 0) AddHistory(movements, updates, q[0]); for (var i = 3; i < table.Rows.Count; i++) { if (Moved(q, 0, 1) || Moved(q, 1, 2)) AddHistory(movements, updates, q[1]); // Slide the window up q.RemoveAt(0); q.Add(table.Rows[i]); } // If there are only two records in the table, // make sure we add the second if (q.Count == 2) AddHistory(movements, updates, q[1]); // if there are three or more records, // make sure we add the last if (Moved(q, 2, 3)) AddHistory(movements, updates, q[2]); //foreach (var row in table.Rows) //{ // InABox.Core.Location cur = new InABox.Core.Location() { Latitude = row.Get(x => x.Location.Latitude), Longitude = row.Get(x => x.Location.Longitude) }; // if ((cur.Latitude != 0.0F) && (cur.Longitude != 0.0F)) // && (cur.DistanceTo(last, UnitOfLength.Kilometers) * 1000F > 500F)) // { // AddHistory(movements, updates, row); // last = cur; // } //} if (updates.Any()) new Client().Save(updates, "", (o, e) => { }); if (!movements.Any()) { var history = new GPSHistory { Date = DateTime.Today, Description = "No Activity Recorded", Latitude = 0.0F, Longitude = 0.0F }; Dispatcher.Invoke(() => { movements.Add(history); }); } } } ); } private bool Moved(List rows, int row1, int row2) { if (rows.Count <= row1) return false; if (rows.Count <= row2) return false; //InABox.Core.Location c0 = new InABox.Core.Location() { Latitude = rows[row1].Get(x => x.Location.Latitude), Longitude = rows[row1].Get(x => x.Location.Longitude) }; //InABox.Core.Location c1 = new InABox.Core.Location() { Latitude = rows[row2].Get(x => x.Location.Latitude), Longitude = rows[row2].Get(x => x.Location.Longitude) }; //return c1.DistanceTo(c0, UnitOfLength.Kilometers) * 1000F > 25F; var bDate = DateTime.Equals( rows[row1].Get(x => x.Created).Date, rows[row2].Get(x => x.Created).Date ); var bAddress = string.Equals( rows[row1].Get(x => x.Location.Address), rows[row2].Get(x => x.Location.Address) ); return /* !bDate || */ !bAddress; } private void AddHistory(ObservableCollection movements, List updates, CoreRow row) { var cur = new Location { Latitude = row.Get(x => x.Location.Latitude), Longitude = row.Get(x => x.Location.Longitude) }; var address = row.Get(x => x.Location.Address); if (string.IsNullOrWhiteSpace(address)) { address = StoreUtils.ReverseGeocode(cur.Latitude, cur.Longitude); var update = row.ToObject(); update.Location.Address = address; updates.Add(update); } var history = new GPSHistory { Date = row.Get(x => x.Location.Timestamp), Description = address, Latitude = cur.Latitude, Longitude = cur.Longitude }; Dispatcher.Invoke(() => { movements.Add(history); }); } private static bool IsMarkerVisible(MapMarker marker, Point northwest, Point southeast) { if (northwest.X == 0 && northwest.Y == 0) return true; var lat = double.Parse(marker.Latitude); var lon = double.Parse(marker.Longitude); var lat1 = northwest.Y < 0 ? lat <= northwest.Y : lat > northwest.Y; var lat2 = southeast.Y < 0 ? lat >= southeast.Y : lat <= southeast.Y; var lon1 = northwest.X < 0 ? lon <= northwest.X : lon > northwest.X; var lon2 = southeast.X < 0 ? lon >= southeast.X : lon <= southeast.X; return lat1 && lat2 && lon1 && lon2; } private void LoadMarkerList() { //var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1) }; //timer.Tick += ((t,e) => //{ //timer.IsEnabled = false; viewmodel.Refresh(); var layer = Map.Layers[0] as ImageryLayer; var nw = layer.GetLatLonFromPoint(new Point(0F, 0F)); var se = layer.GetLatLonFromPoint(new Point(Map.ActualWidth, Map.ActualHeight)); var markers = viewmodel.Markers.Where(marker => ShowAll.IsChecked == true ? true : IsMarkerVisible(marker, nw, se)) .OrderBy(x => x.Description); Markers.ItemsSource = markers.ToArray(); //}); //timer.IsEnabled = true; } private void Map_PreviewMouseUp(object sender, MouseButtonEventArgs e) { LoadMarkerList(); } private void Markers_MouseDoubleClick(object sender, MouseButtonEventArgs e) { var marker = Markers.SelectedItem as MapMarker; ZoomToMarker(marker); } private void ZoomToMarker(MapMarker marker) { if (double.Parse(marker.Latitude) != 0.0F && double.Parse(marker.Longitude) != 0.0F) { var layer = Map.Layers[0] as ImageryLayer; layer.Center = new Point(double.Parse(marker.Latitude), double.Parse(marker.Longitude)); Map.ZoomLevel = ZOOMEDIN; } } private void Markers_SelectionChanged(object sender, SelectionChangedEventArgs e) { var marker = Markers.SelectedItem as MapMarker; viewmodel.Selected = Markers.SelectedItem as MapMarker; if (marker != null) { Description.Text = marker.Description; LastUpdate.Text = string.Format("{0:dd MMM yy hh:mm:ss}", marker.Updated); Device.Text = marker.UpdatedBy; History.Visibility = string.IsNullOrWhiteSpace(marker.DeviceID) ? Visibility.Collapsed : Visibility.Visible; LoadDeviceHistory(marker.DeviceID); } else { Description.Text = ""; LastUpdate.Text = ""; Device.Text = ""; History.Visibility = Visibility.Collapsed; } } private void Viewstyle_SelectionChanged(object sender, SelectionChangedEventArgs e) { var layer = typeof(ImageryLayer); var type = LayerType.OSM; var style = BingMapStyle.Road; if (Viewstyle.SelectedItem == BingStandardViewStyle) { layer = typeof(ImageryLayer); type = LayerType.Bing; style = BingMapStyle.Road; } else if (Viewstyle.SelectedItem == BingTerrainViewStyle) { layer = typeof(ImageryLayer); type = LayerType.Bing; style = BingMapStyle.Aerial; } else if (Viewstyle.SelectedItem == BingTerrainLabelsViewStyle) { layer = typeof(ImageryLayer); type = LayerType.Bing; style = BingMapStyle.AerialWithLabels; } else if (Viewstyle.SelectedItem == GoogleMapsViewStyle) { layer = typeof(ImageryLayerExt); } else { layer = typeof(ImageryLayer); type = LayerType.OSM; } var cur = Map.Layers[0] as ImageryLayer; if (!cur.GetType().Equals(layer)) { var center = cur.Center; var radius = cur.Radius; var template = cur.MarkerTemplate; Map.Layers.Clear(); if (layer == typeof(ImageryLayer)) Map.Layers.Add(new ImageryLayer()); else if (layer == typeof(ImageryLayerExt)) Map.Layers.Add(new ImageryLayerExt()); cur = Map.Layers[0] as ImageryLayer; cur.Center = center; cur.Radius = radius; cur.MarkerTemplate = template; cur.Markers = viewmodel.Markers; cur.BingMapKey = "5kn7uPPBBGKdmWYiwHJL~LjUY7IIgFzGdXpkpWT7Fsw~AmlksNuYOBHNQ3cOa61Nz2ghvK5EbrBZ3aQqvTS4OjcPxTBGNGsj2hKc058CgtgJ"; } if (cur.LayerType != type) cur.LayerType = type; if (cur.BingMapStyle != style) cur.BingMapStyle = style; } private void IncludeVisibleMarkers_Checked(object sender, RoutedEventArgs e) { LoadMarkerList(); } private void ResetView_Click(object sender, RoutedEventArgs e) { ResetZoom(); } private void History_MouseDoubleClick(object sender, MouseButtonEventArgs e) { var history = History.SelectedItem as GPSHistory; if (history == null) return; if (history.Latitude != 0.0F && history.Longitude != 0.0F) { var layer = Map.Layers[0] as ImageryLayer; layer.Center = new Point(history.Latitude, history.Longitude); Map.ZoomLevel = ZOOMEDIN; } } private void Zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (Map == null) return; Map.ZoomLevel = (int)e.NewValue; LoadMarkerList(); } private class DbMarker { public DbMarker(string code, string description, DateTime timestamp, double latitude, double longitude, string updatedby, string deviceid) { Code = code; Description = description; TimeStamp = timestamp; Latitude = latitude; Longitude = longitude; UpdatedBy = updatedby; DeviceID = deviceid; } public string Code { get; } public string Description { get; } public DateTime TimeStamp { get; } public double Latitude { get; } public double Longitude { get; } public string UpdatedBy { get; } public string DeviceID { get; } } } }