Skip to content

Commit

Permalink
PreventEndPosition and SimulateTimeout is the same thing
Browse files Browse the repository at this point in the history
  • Loading branch information
rs22 committed Jan 14, 2024
1 parent bb106de commit f9fc471
Show file tree
Hide file tree
Showing 14 changed files with 1,251 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EulynxLive.Messages.Baseline4R2;
using EulynxLive.Point;
using EulynxLive.Point.Hubs;
using EulynxLive.Point.Proto;

using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -78,8 +79,16 @@ private static (EulynxLive.Point.Point point, Task pointTask, List<byte[]> recei
mockHubContext.Setup(x => x.Clients.All.SendCoreAsync(It.IsAny<string>(), It.IsAny<object[]>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
var point = new EulynxLive.Point.Point(Mock.Of<ILogger<EulynxLive.Point.Point>>(), configuration, builder, connectionProvider.Object, mockHubContext.Object);
if (simulateTimeouts) {
point.EnableTimeoutLeft(true);
point.EnableTimeoutRight(true);
point.PreventLeftEndPosition(new PreventedPositionMessage
{
Position = PreventedPosition.SetNoEndPosition,
DegradedPosition = false
});
point.PreventRightEndPosition(new PreventedPositionMessage
{
Position = PreventedPosition.SetNoEndPosition,
DegradedPosition = false
});
}

async Task SimulatePoint()
Expand Down
28 changes: 0 additions & 28 deletions src/FieldElementSubsystems.Test/Point/PointServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,6 @@ public void TestReset()
point.Verify(x => x.Reset(), Times.Once);
}

[Fact]
public void TestScheduleTimeoutRight()
{
// Arrange
var point = new Mock<IPoint>();
var pointService = new PointService(point.Object);

// Act
pointService.ScheduleTimeoutRight(new EnableMovementFailedMessage() { EnableMovementFailed = true }, Mock.Of<ServerCallContext>());

// Assert
point.Verify(x => x.EnableTimeoutRight(true), Times.Once);
}

[Fact]
public void TestScheduleTimeoutLeft()
{
// Arrange
var point = new Mock<IPoint>();
var pointService = new PointService(point.Object);

// Act
pointService.ScheduleTimeoutLeft(new EnableMovementFailedMessage() { EnableMovementFailed = true }, Mock.Of<ServerCallContext>());

// Assert
point.Verify(x => x.EnableTimeoutLeft(true), Times.Once);
}

[Fact]
public async Task TestSetAbilityToMove()
{
Expand Down
12 changes: 10 additions & 2 deletions src/FieldElementSubsystems.Test/Point/PointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public async Task Test_TimeoutLeft()
.SetupSequence(m => m.ReceiveMovePointCommand(It.IsAny<CancellationToken>()))
.Returns(() =>
{
point.EnableTimeoutLeft(true);
point.PreventLeftEndPosition(new PreventedPositionMessage
{
Position = PreventedPosition.SetNoEndPosition,
DegradedPosition = false
});
return Task.FromResult(GenericPointPosition.Left);
})
.Returns(() =>
Expand Down Expand Up @@ -158,7 +162,11 @@ public async Task Test_TimeoutRight()
.SetupSequence(m => m.ReceiveMovePointCommand(It.IsAny<CancellationToken>()))
.Returns(() =>
{
point.EnableTimeoutRight(true);
point.PreventRightEndPosition(new PreventedPositionMessage
{
Position = PreventedPosition.SetNoEndPosition,
DegradedPosition = false
});
return Task.FromResult(GenericPointPosition.Right);
})
.Returns(() =>
Expand Down
3 changes: 0 additions & 3 deletions src/Point/IPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ public interface IPoint {
ConnectionProtocol? ConnectionProtocol { get; }
IPointToInterlockingConnection? Connection { get; }
GenericPointState PointState { get; }

void EnableTimeoutLeft(bool enableMovementFailed);
void EnableTimeoutRight(bool enableMovementFailed);
void EnableInitializationTimeout(bool enableInitializationTimeout);
void PreventLeftEndPosition(PreventedPositionMessage request);
void PreventRightEndPosition(PreventedPositionMessage request);
Expand Down
63 changes: 19 additions & 44 deletions src/Point/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ public Point(ILogger<Point> logger, IConfiguration configuration, IPointToInterl
PreventedPositionRight: PreventedPosition.DoNotPrevent,
DegradedPositionLeft: false,
DegradedPositionRight: false,
SimulateTimeoutLeft: false,
SimulateTimeoutRight: false,
SimulateInitializationTimeout: false
);

Expand Down Expand Up @@ -116,30 +114,6 @@ public void EnableInitializationTimeout(bool enableInitializationTimeout)
};
}

/// <summary>
/// Sets the timeout flag for the next move left command.
/// </summary>
public void EnableTimeoutLeft(bool enableMovementFailed)
{
_logger.LogInformation("Timeout on next move left command enabled.");
_simulatedPointState = _simulatedPointState with
{
SimulateTimeoutLeft = enableMovementFailed
};
}

/// <summary>
/// Sets the timeout flag for the next move right command.
/// </summary>
public void EnableTimeoutRight(bool enableMovementFailed)
{
_logger.LogInformation("Timeout on next move right command enabled.");
_simulatedPointState = _simulatedPointState with
{
SimulateTimeoutRight = enableMovementFailed
};
}

/// <summary>
/// Sets the ability to move for the next command.
/// </summary>
Expand Down Expand Up @@ -389,33 +363,34 @@ private async Task HandleCommandedPointPosition(GenericPointPosition commandedPo
return;
}

if (ShouldSimulateTimeout(commandedPointPosition, simulatedState))
if (commandedPointPosition == GenericPointPosition.Left && simulatedState.PreventedPositionLeft != PreventedPosition.DoNotPrevent)
{
_logger.LogInformation("Timeout");
await Connection.SendTimeoutMessage();
}
else
{
var (newPointPosition, newDegradedPointPosition) = HandlePreventedPointPosition(commandedPointPosition, simulatedState);
SetPointState(newPointPosition, RespectAllPointMachinesCrucial(newDegradedPointPosition));

_logger.LogInformation("End position reached.");
var (pointPosition, degradedPointPosition) = HandlePreventedPointPosition(commandedPointPosition, simulatedState);
SetPointState(pointPosition, degradedPointPosition);

await Connection.SendPointPosition(PointState);
_logger.LogInformation("Prevented left end position: {} {}.", PointState.PointPosition, PointState.DegradedPointPosition);
}
}

private bool ShouldSimulateTimeout(GenericPointPosition commandedPointPosition, SimulatedPointState simulatedState)
{
if (commandedPointPosition == GenericPointPosition.Left)
else if (commandedPointPosition == GenericPointPosition.Right && simulatedState.PreventedPositionRight != PreventedPosition.DoNotPrevent)
{
return simulatedState.SimulateTimeoutLeft;
await Connection.SendTimeoutMessage();

var (pointPosition, degradedPointPosition) = HandlePreventedPointPosition(commandedPointPosition, simulatedState);
SetPointState(pointPosition, degradedPointPosition);

await Connection.SendPointPosition(PointState);
_logger.LogInformation("Prevented right end position: {} {}.", PointState.PointPosition, PointState.DegradedPointPosition);
}
else if (commandedPointPosition == GenericPointPosition.Right)
else
{
return simulatedState.SimulateTimeoutRight;
}
var notDegradedPosition = AllPointMachinesCrucial ? GenericDegradedPointPosition.NotApplicable : GenericDegradedPointPosition.NotDegraded;
SetPointState(commandedPointPosition, notDegradedPosition);

throw new ArgumentException("Invalid commanded position", nameof(commandedPointPosition));
_logger.LogInformation("End position reached.");
await Connection.SendPointPosition(PointState);
}
}

/// <summary>
Expand Down
12 changes: 0 additions & 12 deletions src/Point/Services/PointService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ public override Task<Empty> Reset(Empty request, ServerCallContext context)
return Task.FromResult(new Empty());
}

public override Task<Empty> ScheduleTimeoutRight(EnableMovementFailedMessage request, ServerCallContext context)
{
_point.EnableTimeoutRight(request.EnableMovementFailed);
return Task.FromResult(new Empty());
}

public override Task<Empty> ScheduleTimeoutLeft(EnableMovementFailedMessage request, ServerCallContext context)
{
_point.EnableTimeoutLeft(request.EnableMovementFailed);
return Task.FromResult(new Empty());
}

public override async Task<Empty> SetAbilityToMove(AbilityToMoveMessage request, ServerCallContext context)
{
await _point.SetAbilityToMove(request);
Expand Down
2 changes: 0 additions & 2 deletions src/Point/SimulatedPointState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public record SimulatedPointState(
PreventedPosition PreventedPositionRight,
bool DegradedPositionLeft,
bool DegradedPositionRight,
bool SimulateTimeoutLeft,
bool SimulateTimeoutRight,
bool SimulateInitializationTimeout
);
}
Loading

0 comments on commit f9fc471

Please sign in to comment.