Esempio n. 1
0
        /// <summary>
        /// Get tracking device for route.
        /// </summary>
        /// <param name="devices">List with all tracking devices.</param>
        /// <param name="route">Route which device should be found.</param>
        /// <returns>Route tracking device or null if route has no device.</returns>
        private TrackingDevice _GetTrackingDevice(IEnumerable <TrackingDevice> devices, Route route)
        {
            // Check if route has associated device.
            MobileDevice device = TrackingHelper.GetDeviceByRoute(route);

            if (device == null)
            {
                return(null);
            }

            // Return device.
            return(_FindDevice(devices, device));
        }
Esempio n. 2
0
        /// <returns>'True' if any information was sent, 'false' otherwise.</returns>
        private bool _DeployRoutes(
            IEnumerable <TrackingDevice> devices,
            IEnumerable <Route> routes,
            DateTime plannedDate)
        {
            Debug.Assert(devices != null);
            Debug.Assert(routes != null);

            plannedDate = plannedDate.Date;

            var newStops     = new List <DM.Stop>();
            var updatedStops = new List <DM.Stop>();

            var deployedRoutes = new List <Route>();
            var deployedStops  = new List <Stop>();

            // Get all non deleted stops for current routes for planned date.
            var existingStops =
                _trackingService.GetNotDeletedStops(devices.Select(x => x.ServerId), plannedDate);

            foreach (var route in routes)
            {
                // check if route has associated device
                MobileDevice device = TrackingHelper.GetDeviceByRoute(route);
                if (device == null)
                {
                    continue;
                }

                // check if device belongs to devices with tracking id
                TrackingDevice td = _FindDevice(devices, device);
                if (td == null)
                {
                    continue;
                }

                // Get all stops for current route.
                var currentDateStops = existingStops.Where(stop => stop.DeviceID == td.ServerId);

                // Get id's of non deleted stops.
                var stopsToDelete = currentDateStops.
                                    Where(stop => stop.Deleted == DM.DeletionStatus.NotDeleted).
                                    ToDictionary(stop => stop.ObjectID);

                // Get version number for sended stops.
                var version = _GetNewVersion(currentDateStops);

                // Prepare stops to be deployed.
                var sortedStops   = CommonHelpers.GetSortedStops(route);
                var trackingStops = _CreateTrackingStops(
                    td.ServerId,
                    version,
                    plannedDate,
                    sortedStops);
                trackingStops = trackingStops.ToList();

                // Add stop to either new stops or updated stops collection.
                foreach (var item in trackingStops.ToIndexed())
                {
                    var trackingStop  = item.Value;
                    var existingStop  = stopsToDelete.Remove(trackingStop.ObjectID);
                    var stopsToDeploy = existingStop ? updatedStops : newStops;
                    stopsToDeploy.Add(trackingStop);

                    if (!existingStop)
                    {
                        deployedStops.Add(sortedStops[item.Index]);
                    }
                }

                // Deletes from tracking service stops which were deleted locally.
                // When stop is moved to other route we treat it as a deletion from an old route
                // and adding to the new one.
                foreach (var stopToDelete in stopsToDelete.Values)
                {
                    stopToDelete.Deleted = DM.DeletionStatus.Deleted;
                    updatedStops.Add(stopToDelete);
                }

                // We need a list of both new and updated stops in order to apply common properties
                // like arrival delays.
                var allStops = new List <DM.Stop>(trackingStops);

                TrackingHelper.ApplyArrivalDelayToStops(
                    _solver.SolverSettings.ArriveDepartDelay, allStops);

                deployedRoutes.Add(route);
            }

            // We must sent route settings, barriers only if we have stops to update.
            if (newStops.Count > 0 || updatedStops.Count > 0)
            {
                // Update route settings.
                var routeSettings = _SerializeRouteSettings();
                _trackingService.UpdateRouteSettings(plannedDate, routeSettings);

                _UpdateRouteTable(routes, devices, plannedDate);

                // Update barriers for planned date.
                _UpdateBarriers(plannedDate);

                _trackingService.UpdateStops(newStops, updatedStops);

                return(true);
            }
            else
            {
                return(false);
            }
        }