Example #1
0
        private bool UpdateCarPosition()
        {
            try {
                var routesIds = (yTreeViewDrivers.RepresentationModel.ItemsList as IList <Vodovoz.ViewModel.WorkingDriverVMNode>)
                                .SelectMany(x => x.RouteListsIds.Keys).ToArray();
                var driversWithAdditionalLoading = _routeListRepository.GetDriversWithAdditionalLoading(uow, routesIds)
                                                   .Select(x => x.Id).ToArray();
                var start      = DateTime.Now;
                var lastPoints = _trackRepository.GetLastPointForRouteLists(uow, routesIds);

                var movedDrivers = lastPoints.Where(x => x.Time > DateTime.Now.AddMinutes(-20)).Select(x => x.RouteListId).ToArray();
                var ere20Minuts  = _trackRepository.GetLastPointForRouteLists(uow, movedDrivers, DateTime.Now.AddMinutes(-20));
                logger.Debug("Время запроса точек: {0}", DateTime.Now - start);
                carsOverlay.Clear();
                _fastDeliveryOverlay.Clear();
                carMarkers = new Dictionary <int, CarMarker>();
                foreach (var pointsForDriver in lastPoints.GroupBy(x => x.DriverId))
                {
                    var lastPoint = pointsForDriver.OrderBy(x => x.Time).Last();
                    var driverRow = (yTreeViewDrivers.RepresentationModel.ItemsList as IList <WorkingDriverVMNode>)
                                    .First(x => x.Id == lastPoint.DriverId);

                    CarMarkerType iconType;
                    var           ere20 = ere20Minuts.Where(x => x.DriverId == pointsForDriver.Key).OrderBy(x => x.Time).LastOrDefault();
                    if (lastPoint.Time < DateTime.Now.AddMinutes(-20))
                    {
                        iconType = driverRow.IsVodovozAuto ? CarMarkerType.BlueCarVodovoz : CarMarkerType.BlueCar;
                    }
                    else if (ere20 != null)
                    {
                        var point1 = new PointLatLng(lastPoint.Latitude, lastPoint.Longitude);
                        var point2 = new PointLatLng(ere20.Latitude, ere20.Longitude);
                        var diff   = gmapWidget.MapProvider.Projection.GetDistance(point1, point2);
                        if (diff <= 0.1)
                        {
                            iconType = driverRow.IsVodovozAuto ? CarMarkerType.RedCarVodovoz : CarMarkerType.RedCar;
                        }
                        else
                        {
                            iconType = driverRow.IsVodovozAuto ? CarMarkerType.GreenCarVodovoz : CarMarkerType.GreenCar;
                        }
                    }
                    else
                    {
                        iconType = driverRow.IsVodovozAuto ? CarMarkerType.GreenCarVodovoz : CarMarkerType.GreenCar;
                    }

                    if (lastSelectedDrivers.ContainsKey(lastPoint.DriverId))
                    {
                        lastSelectedDrivers[lastPoint.DriverId] = iconType;
                        iconType = driverRow.IsVodovozAuto ? CarMarkerType.BlackCarVodovoz : CarMarkerType.BlackCar;
                    }

                    string text   = $"{driverRow.ShortName}({driverRow.CarNumber})";
                    var    marker = new CarMarker(new PointLatLng(lastPoint.Latitude, lastPoint.Longitude),
                                                  iconType);
                    if (lastPoint.Time < DateTime.Now.AddSeconds(-30))
                    {
                        text += lastPoint.Time.Date == DateTime.Today
                                                        ? $"\nБыл виден: {lastPoint.Time:t} "
                                                        : $"\nБыл виден: {lastPoint.Time:g} ";
                    }
                    marker.ToolTipText = text;
                    carsOverlay.Markers.Add(marker);

                    if (routelisttrackfilterview1.FilterViewModel.ShowFastDeliveryCircle && driversWithAdditionalLoading.Contains(pointsForDriver.Key))
                    {
                        CustomPolygons.CreateRoundPolygon(_fastDeliveryOverlay, lastPoint.Latitude, lastPoint.Longitude, radiusInKm: _fastDeliveryMaxDistanceKm,
                                                          segmentsPointsCount: 30, borderWidth: 1, color: System.Drawing.Color.OrangeRed, fillAlpha: 30);
                    }

                    carMarkers.Add(lastPoint.DriverId, marker);
                }
            } catch (Exception ex) {
                logger.Error("Ошибка при обновлении позиции автомобиля", ex);
                return(false);
            }
            return(true);
        }