Пример #1
0
        // Updates work task id on newly digitized routenetwork-elements
        // that do not yet have a WorkTaskMrid
        private async Task UpdateWorkTaskIdOnNewlyDigitized(Guid workTaskMrId)
        {
            foreach (var command in _eventStore.Get())
            {
                foreach (var routeNetworkEvent in command.RouteNetworkEvents)
                {
                    switch (routeNetworkEvent)
                    {
                    case RouteNodeAdded routeNodeAdded:
                        var routeNodeSt = await _geoDatabase.GetRouteNodeShadowTable(routeNodeAdded.NodeId);

                        // If the id is empty, we update it to the current work task id.
                        if (routeNodeSt.WorkTaskMrid == Guid.Empty)
                        {
                            routeNodeSt.WorkTaskMrid = workTaskMrId;
                            await _geoDatabase.UpdateRouteNode(routeNodeSt);
                        }
                        break;

                    case RouteSegmentAdded routeSegmentAdded:
                        var routeSegmentSt = await _geoDatabase.GetRouteSegmentShadowTable(routeSegmentAdded.SegmentId);

                        // If the id is empty, we update it to the current work task id.
                        if (routeSegmentSt is not null && routeSegmentSt.WorkTaskMrid == Guid.Empty)
                        {
                            routeSegmentSt.WorkTaskMrid = workTaskMrId;
                            await _geoDatabase.UpdateRouteSegment(routeSegmentSt);
                        }
                        break;
                    }
                }
            }
        }
Пример #2
0
        public async Task Handle(RouteSegmentConnectivityChanged request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(RouteSegmentConnectivityChangedHandler)}");

            var startNode = (await _geoDatabase.GetIntersectingStartRouteNodes(request.After)).FirstOrDefault();
            var endNode   = (await _geoDatabase.GetIntersectingEndRouteNodes(request.After)).FirstOrDefault();

            if ((!(startNode is null) && !(endNode is null)) && startNode.Mrid == endNode.Mrid)
            {
                _logger.LogWarning($"Reverting RouteSegment with mrid '{request.After.Mrid}', because of both ends intersecting with the same RouteNode with mrid '{startNode.Mrid}'");
                await _geoDatabase.UpdateRouteSegment(request.Before);

                return;
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            if (startNode is null)
            {
                startNode = _routeNodeFactory.Create(request.After.FindStartPoint());
                var insertRouteNodeEvent = await InsertRouteNode(startNode);

                routeNetworkEvents.Add(insertRouteNodeEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = request.After.GetLineString();
                lineString.Coordinates[0] = new Coordinate(startNode.GetPoint().Coordinate);
                request.After.Coord       = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(request.After);
            }

            if (endNode is null)
            {
                endNode = _routeNodeFactory.Create(request.After.FindEndPoint());
                var insertRouteNodeEvent = await InsertRouteNode(endNode);

                routeNetworkEvents.Add(insertRouteNodeEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = request.After.GetLineString();
                lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(endNode.GetPoint().Coordinate);
                request.After.Coord = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(request.After);
            }

            var(routeSegmentClone, routeSegmentAddedEvent) = await InsertRouteSegmentClone(request.After);

            routeNetworkEvents.Add(routeSegmentAddedEvent);

            var routeSegmentMarkedForDeletionEvent = await MarkRouteSegmentForDeletion(request.Before);

            routeNetworkEvents.Add(routeSegmentMarkedForDeletionEvent);

            var beforeStartNode             = (await _geoDatabase.GetIntersectingStartRouteNodes(request.Before)).FirstOrDefault();
            var beforeEndNode               = (await _geoDatabase.GetIntersectingEndRouteNodes(request.Before)).FirstOrDefault();
            var isBeforeStartNodeDeleteable = await IsRouteNodeDeleteable(beforeStartNode);

            var isBeforeEndNodeDeletable = await IsRouteNodeDeleteable(beforeEndNode);

            if (isBeforeStartNodeDeleteable)
            {
                var routeNodeMarkedForDeletionEvent = await MarkDeleteRouteNode(beforeStartNode);

                routeNetworkEvents.Add(routeNodeMarkedForDeletionEvent);
            }

            if (isBeforeEndNodeDeletable)
            {
                var routeNodeMarkedForDeletionEvent = await MarkDeleteRouteNode(beforeEndNode);

                routeNetworkEvents.Add(routeNodeMarkedForDeletionEvent);
            }

            var cmdId = Guid.NewGuid();
            var routeSegmentConnectivityChangedEvent = new RouteNetworkCommand(nameof(RouteSegmentConnectivityChanged), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(routeSegmentConnectivityChangedEvent);
        }
        public async Task Handle(RouteNodeLocationChanged request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(RouteNodeLocationChangedHandler)}");

            var intersectingSegmentsBeforeChange = (await _geoDatabase.GetIntersectingRouteSegments(request.RouteNodeBefore.Coord)).ToList();
            var routeSegmentsToBeUpdated         = new List <RouteSegment>();

            if (intersectingSegmentsBeforeChange.Count > 0)
            {
                var anySegmentIntersectRouteNode = false;
                foreach (var intersectingSegment in intersectingSegmentsBeforeChange)
                {
                    var startNode = (await _geoDatabase.GetIntersectingStartRouteNodes(intersectingSegment)).FirstOrDefault();

                    if (startNode is null)
                    {
                        var lineString = intersectingSegment.GetLineString();
                        lineString.Coordinates[0] = new Coordinate(request.RouteNodeAfter.GetPoint().Coordinate);
                        intersectingSegment.Coord = lineString.AsBinary();
                    }
                    else
                    {
                        var lineString = intersectingSegment.GetLineString();
                        lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(request.RouteNodeAfter.GetPoint().Coordinate);
                        intersectingSegment.Coord = lineString.AsBinary();
                    }

                    anySegmentIntersectRouteNode = (await _geoDatabase.GetAllIntersectingRouteNodesNotIncludingEdges(intersectingSegment.Coord, intersectingSegment.Mrid)).Any();
                    if (anySegmentIntersectRouteNode)
                    {
                        break;
                    }

                    routeSegmentsToBeUpdated.Add(intersectingSegment);
                }

                // Rollback in case of segment intersecting with any route nodes
                if (anySegmentIntersectRouteNode)
                {
                    throw new Exception("Route segments intersects with any route nodes");
                }
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            var routeNodeGeometryModifiedEvent = _routeNodeEventFactory.CreateGeometryModified(request.RouteNodeAfter);

            routeNetworkEvents.Add(routeNodeGeometryModifiedEvent);

            for (var i = 0; i < routeSegmentsToBeUpdated.Count; i++)
            {
                var routeSegmentToBeUpdated = routeSegmentsToBeUpdated[i];
                await _geoDatabase.UpdateRouteSegment(routeSegmentToBeUpdated);

                var routeSegmentGeometryModifiedEvent = _routeSegmentEventFactory.CreateGeometryModified(routeSegmentToBeUpdated, true);
                routeNetworkEvents.Add(routeSegmentGeometryModifiedEvent);
            }

            var cmdId = Guid.NewGuid();
            var routeNodeLocationChangedCommand = new RouteNetworkCommand(nameof(RouteNodeLocationChanged), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(routeNodeLocationChangedCommand);
        }
 public async Task Handle(RollbackInvalidRouteSegment request, CancellationToken token)
 {
     _logger.LogWarning($"Rollbacks invalid {nameof(RouteSegment)} with id: '{request.RollbackToSegment.Mrid}'. {request.Message}");
     await _geoDatabase.UpdateRouteSegment(request.RollbackToSegment);
 }
        public async Task Handle(NewRouteSegmentDigitized request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(NewRouteSegmentDigitizedHandler)}");

            if (request.RouteSegment is null)
            {
                throw new ArgumentNullException($"{nameof(RouteSegment)} cannot be null.");
            }

            var routeSegment = request.RouteSegment;
            var startNode    = (await _geoDatabase.GetIntersectingStartRouteNodes(routeSegment)).FirstOrDefault();
            var endNode      = (await _geoDatabase.GetIntersectingEndRouteNodes(routeSegment)).FirstOrDefault();

            if ((!(startNode is null) && !(endNode is null)) && startNode.Mrid == endNode.Mrid)
            {
                _logger.LogWarning($"Deleting RouteSegment with mrid '{routeSegment.Mrid}', because of both ends intersecting with the same RouteNode with mrid '{startNode.Mrid}'");
                await _geoDatabase.DeleteRouteSegment(routeSegment.Mrid);

                return;
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            if (startNode is null)
            {
                var startPoint = routeSegment.FindStartPoint();
                startNode              = _routeNodeFactory.Create(startPoint);
                startNode.Username     = routeSegment.Username;
                startNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(startNode);

                var startRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(startNode);
                routeNetworkEvents.Add(startRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[0] = new Coordinate(startNode.GetPoint().Coordinate);
                routeSegment.Coord        = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            if (endNode is null)
            {
                var endPoint = routeSegment.FindEndPoint();
                endNode              = _routeNodeFactory.Create(endPoint);
                endNode.Username     = routeSegment.Username;
                endNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(endNode);

                var endRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(endNode);
                routeNetworkEvents.Add(endRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(endNode.GetPoint().Coordinate);
                routeSegment.Coord = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            var routeSegmentAddedEvent = _routeSegmentEventFactory.CreateAdded(routeSegment, startNode, endNode);

            routeNetworkEvents.Add(routeSegmentAddedEvent);

            var cmdId = Guid.NewGuid();
            var newRouteSegmentDigitizedCommand = new RouteNetworkCommand(nameof(NewRouteSegmentDigitized), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(newRouteSegmentDigitizedCommand);
        }