Skip to content

Commit

Permalink
Merge branch 'main' into AddPutIntoNoEndposition
Browse files Browse the repository at this point in the history
  • Loading branch information
rs22 committed Jan 14, 2024
2 parents eaac2d8 + 94a1713 commit 6cf5c22
Show file tree
Hide file tree
Showing 11 changed files with 4,710 additions and 19,233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public async Task Test_Initialization()
// Assert
mockConnection.Verify(v => v.ReceiveAsync(It.IsAny<CancellationToken>()), Times.Exactly(2));
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(5));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1,1, new byte[] { 0x00 }).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1, 1, new byte[] { 0x00 }).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointStartInitialisationMessage("99W1________________", "INTERLOCKING________").ToByteArray(), receivedMessages[1]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInALeftHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), receivedMessages[2]);
Assert.Equal(new PointAbilityToMovePointMessage("99W1________________", "INTERLOCKING________", PointAbilityToMovePointMessageReportedAbilityToMovePointStatus.PointIsAbleToMove).ToByteArray(), receivedMessages[3]);
Assert.Equal(new PointInitialisationCompletedMessage("99W1________________", "INTERLOCKING________").ToByteArray(), receivedMessages[4]);
}

[Fact]
public async Task Test_Initialization_Timeout(){
// Arrange
Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task Test_Send_Position()
// Act
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
foreach (var position in new List<GenericPointPosition>() { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
foreach (var position in new [] { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
{
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: position, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace FieldElementSubsystems.Test.Connection.Baseline4R2;
public class PointToInterlockingConnectionTest
{

private static Mock<IConnection> CreateDefaultMockConnection()
{
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
return mockConnection;
}

private static readonly IDictionary<string, string?> TestSettings = new Dictionary<string, string?> {
{"PointSettings:LocalId", "99W1" },
{"PointSettings:LocalRastaId", "100" },
Expand Down Expand Up @@ -48,10 +57,7 @@ public void Test_Connect()
public async Task Test_Initialization()
{
// Arrange
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
var mockConnection = CreateDefaultMockConnection();
var receivedMessages = new List<byte[]>();
mockConnection.Setup(x => x.SendAsync(Capture.In(receivedMessages)))
.Returns(Task.FromResult(0));
Expand All @@ -62,9 +68,10 @@ public async Task Test_Initialization()
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);

// Assert
mockConnection.Verify(v => v.ReceiveAsync(It.IsAny<CancellationToken>()), Times.Exactly(2));
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(5));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1,1, new byte[] { 0x00 }).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1, 1, new byte[] { 0x00 }).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointStartInitialisationMessage("99W1________________", "INTERLOCKING________").ToByteArray(), receivedMessages[1]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInALeftHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), receivedMessages[2]);
Assert.Equal(new PointAbilityToMovePointMessage("99W1________________", "INTERLOCKING________", PointAbilityToMovePointMessageReportedAbilityToMovePointStatus.PointIsAbleToMove).ToByteArray(), receivedMessages[3]);
Expand Down Expand Up @@ -124,10 +131,7 @@ public async Task Test_Initialization_Version_Mismatch(){
public async Task Test_Send_Position()
{
// Arrange
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
var mockConnection = CreateDefaultMockConnection();
var args = new List<byte[]>();
mockConnection.Setup(x => x.SendAsync(Capture.In(args)))
.Returns(Task.FromResult(0));
Expand All @@ -137,15 +141,15 @@ public async Task Test_Send_Position()
// Act
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Right, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.UnintendedPosition, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.NoEndPosition, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
foreach (var position in new [] { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
{
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: position, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
}

// Assert
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(9));
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInARightHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[5]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInALeftHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[6]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInALeftHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[5]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInARightHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[6]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInUnintendedPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[7]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInNoEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), args[8]);
}
Expand All @@ -154,7 +158,7 @@ public async Task Test_Send_Position()
public async Task Test_Receive_Position()
{
// Arrange
var mockConnection = new Mock<IConnection>();
var mockConnection = CreateDefaultMockConnection();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray())
Expand All @@ -179,10 +183,7 @@ public async Task Test_Receive_Position()
public async Task Test_TimeoutMessage()
{
// Arrange
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
var mockConnection = CreateDefaultMockConnection();
var args = new List<byte[]>();
mockConnection.Setup(x => x.SendAsync(Capture.In(args)))
.Returns(Task.FromResult(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using EulynxLive.FieldElementSubsystems.Configuration;
using Grpc.Core;
using EulynxLive.Messages.Baseline4R1;
using System.Threading.Channels;
using EulynxLive.FieldElementSubsystems.Extensions;
Expand All @@ -15,7 +14,7 @@ public class PointToInterlockingConnectionBuilder : IPointToInterlockingConnecti
private readonly ILogger<PointToInterlockingConnection> _logger;
private readonly CancellationToken _stoppingToken;
private readonly IConfiguration _configuration;

public PointToInterlockingConnectionBuilder(
ILogger<PointToInterlockingConnection> logger,
IConfiguration configuration,
Expand Down Expand Up @@ -68,7 +67,7 @@ public PointToInterlockingConnection(
public async Task<bool> InitializeConnection(GenericPointState state, bool observeAbilityToMove, bool simulateTimeout, CancellationToken cancellationToken)
{
var versionCheckReceived = await ReceiveMessage<PointPdiVersionCheckCommand>(cancellationToken);

if (versionCheckReceived == null)
{
_logger.LogError("Unexpected message.");
Expand All @@ -88,21 +87,21 @@ public async Task<bool> InitializeConnection(GenericPointState state, bool obser
}

var versionCheckResponse = new PointPdiVersionCheckMessage(_localId, _remoteId, PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, _pdiVersion, (byte)_checksum.Length, _checksum);

await SendMessage(versionCheckResponse);

if (simulateTimeout)
{
// Never send the missing initialization messages
return true;
}

if (await ReceiveMessage<PointInitialisationRequestCommand>(cancellationToken) == null)
{
_logger.LogError("Unexpected message.");
return false;
}

var startInitialization = new PointStartInitialisationMessage(_localId, _remoteId);
await SendMessage(startInitialization);

Expand All @@ -123,7 +122,7 @@ public async Task<bool> InitializeConnection(GenericPointState state, bool obser
}

/// <summary>
/// Checks whether the given version matches the expected version
/// Checks whether the given version matches the expected version
/// </summary>
/// <param name="versionCheckResponse"></param>
/// <returns></returns> <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using EulynxLive.FieldElementSubsystems.Configuration;
using Grpc.Core;
using EulynxLive.Messages.Baseline4R2;
using System.Threading.Channels;
using EulynxLive.FieldElementSubsystems.Extensions;


namespace EulynxLive.FieldElementSubsystems.Connections.EulynxBaseline4R2;


public class PointToInterlockingConnectionBuilder : IPointToInterlockingConnectionBuilder
{
private readonly ILogger<PointToInterlockingConnection> _logger;
private readonly CancellationToken _stoppingToken;
private readonly IConfiguration _configuration;

public PointToInterlockingConnectionBuilder(
ILogger<PointToInterlockingConnection> logger,
IConfiguration configuration,
Expand Down Expand Up @@ -68,7 +67,7 @@ public PointToInterlockingConnection(
public async Task<bool> InitializeConnection(GenericPointState state, bool observeAbilityToMove, bool simulateTimeout, CancellationToken cancellationToken)
{
var versionCheckReceived = await ReceiveMessage<PointPdiVersionCheckCommand>(cancellationToken);

if (versionCheckReceived == null)
{
_logger.LogError("Unexpected message.");
Expand All @@ -88,19 +87,21 @@ public async Task<bool> InitializeConnection(GenericPointState state, bool obser
}

var versionCheckResponse = new PointPdiVersionCheckMessage(_localId, _remoteId, PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, _pdiVersion, (byte)_checksum.Length, _checksum);

await SendMessage(versionCheckResponse);

if (simulateTimeout)
{
// Never send the missing initialization messages
return true;
}

if (await ReceiveMessage<PointInitialisationRequestCommand>(cancellationToken) == null)
{
_logger.LogError("Unexpected message.");
return false;
}

var startInitialization = new PointStartInitialisationMessage(_localId, _remoteId);
await SendMessage(startInitialization);

Expand All @@ -119,15 +120,14 @@ public async Task<bool> InitializeConnection(GenericPointState state, bool obser
await SendMessage(completeInitialization);
return true;
}

/// <summary>
/// Checks whether the given version matches the expected version
/// Checks whether the given version matches the expected version
/// </summary>
/// <param name="versionCheckResponse"></param>
/// <returns></returns> <summary>
private bool CheckPDIVersionReceived(byte version) => version == _pdiVersion;


public async Task SendPointPosition(GenericPointState state)
{
var pointState = new PointState(state);
Expand Down
16 changes: 10 additions & 6 deletions src/Point/Point.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="8.0.0" />
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="..\**\proto\rasta.proto" />
<Protobuf Include="..\**\proto\point.proto" />
<Protobuf Include="..\ProtobufInterfaces\proto\rasta.proto" />
<Protobuf Include="..\ProtobufInterfaces\proto\point.proto" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Messages\Messages.csproj" />
<ProjectReference Include="..\ProtobufInterfaces\ProtobufInterfaces.csproj" />
<ProjectReference Include="..\FieldElementSubsystems\FieldElementSubsystems.csproj" />
</ItemGroup>

Expand Down Expand Up @@ -75,4 +74,9 @@
</ItemGroup>
</Target>

<!-- Provide the point.proto for download -->
<Target Name="CopyCustomContentOnPublish" AfterTargets="PublishRunWebpack">
<Copy SourceFiles="..\ProtobufInterfaces\proto\point.proto" DestinationFolder="$(PublishDir)$(SpaRoot)build" />
</Target>

</Project>
5 changes: 0 additions & 5 deletions src/Point/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace EulynxLive.Point
{
public class Program
Expand Down
Loading

0 comments on commit 6cf5c22

Please sign in to comment.