Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Scretch9 committed Nov 24, 2023
1 parent 86f02f1 commit 1b17126
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
16 changes: 6 additions & 10 deletions src/Point/Point.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System.Net.WebSockets;
using System.Text.Json;
using Google.Protobuf;
using Grpc.Core;
using Sci;
using DegradedPointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.DegradedPointPosition;
using EulynxLive.Messages.IPointToInterlockingConnection;
using EulynxLive.Point.Proto;
using Grpc.Core;
using PointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.PointPosition;
using DegradedPointPosition = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.DegradedPointPosition;
using PointState = EulynxLive.Messages.IPointToInterlockingConnection.IPointToInterlockingConnection.PointState;
using static Sci.Rasta;
using Sci;
using System.Net.WebSockets;
using System.Text;
using Grpc.Net.Client;
using EulynxLive.Point.Components;
using EulynxLive.Point.Proto;
using System.Text.Json;

namespace EulynxLive.Point
{
Expand Down
63 changes: 35 additions & 28 deletions src/Point/PointToInterlockingConnectionB4R1Impl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.WebSockets;
using EulynxLive.Messages.Baseline4R1;
using EulynxLive.Point;
using Google.Protobuf;
Expand Down Expand Up @@ -48,74 +47,81 @@ public void Connect()
public async Task<bool> InitializeConnection(PointState state)
{
_logger.LogTrace("Connected. Waiting for request...");
if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token)
|| Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()) is not PointPdiVersionCheckCommand)
if (await ReceiveMessage<PointPdiVersionCheckCommand>() == null)
{
_logger.LogError("Unexpected message.");
return false;
}

var versionCheckResponse = new PointPdiVersionCheckMessage(_localId, _remoteId, PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, /* TODO */ 0, 0, new byte[] { });
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(versionCheckResponse.ToByteArray()) });
await SendMessage(versionCheckResponse);

if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token)
|| Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray()) is not PointInitialisationRequestCommand)
if (await ReceiveMessage<PointInitialisationRequestCommand>() == null)
{
_logger.LogError("Unexpected message.");
return false;
}

var startInitialization = new PointStartInitialisationMessage(_localId, _remoteId);
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(startInitialization.ToByteArray()) });
await SendMessage(startInitialization);

var pointState = new B4R1PointStateImpl(state);
var initialPosition = new PointPointPositionMessage(_localId, _remoteId, pointState.PointPosition, pointState.DegradedPointPosition);
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(initialPosition.ToByteArray()) });
await SendMessage(initialPosition);

var completeInitialization = new PointInitialisationCompletedMessage(_localId, _remoteId);
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(completeInitialization.ToByteArray()) });
await SendMessage(completeInitialization);
return true;
}

public async Task SendPointPosition(PointState state)
{
var pointState = new B4R1PointStateImpl(state);
var response = new PointPointPositionMessage(_localId, _remoteId, pointState.PointPosition, pointState.DegradedPointPosition);
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(response.ToByteArray()) });
await SendMessage(response);
}

async public Task SendTimeoutMessage()
{
var response = new PointTimeoutMessage(_localId, _remoteId);
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(response.ToByteArray()) });
await SendMessage(response);
}

public async Task<PointPosition?> ReceivePointPosition()
{
var message = await ReceiveMessage<PointMovePointCommand>();

if (!await _currentConnection.ResponseStream.MoveNext())
return (message != null)? message.CommandedPointPosition switch
{
return null;
}
var message = Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray());

if (message is PointMovePointCommand movePointCommand)
{
return movePointCommand.CommandedPointPosition switch
{
PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsARightHandPointMoving => PointPosition.RIGHT,
PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsALeftHandPointMoving => PointPosition.LEFT,
};
}

_logger.LogInformation("Received unknown message {}", message.GetType());
return null;
PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsARightHandPointMoving => PointPosition.RIGHT,
PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsALeftHandPointMoving => PointPosition.LEFT,
} : null;
}

public void Dispose()
{
_currentConnection?.Dispose();
}

private async Task SendMessage(Message message)
{
if (_currentConnection == null) throw new NullReferenceException("Connection is null. Did you call Connect()?");
await _currentConnection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(message.ToByteArray()) });
}

private async Task<T?> ReceiveMessage<T>() where T : Message
{
if (_currentConnection == null) throw new NullReferenceException("Connection is null. Did you call Connect()?");
if (!await _currentConnection.ResponseStream.MoveNext(_timeout.Token)) return null;

var message = Message.FromBytes(_currentConnection.ResponseStream.Current.Message.ToByteArray());
if (message is not T)
{
_logger.LogError("Unexpected message: {}", message);
return null;
}
return message as T;
}
}

public class B4R1PointStateImpl
Expand All @@ -124,7 +130,8 @@ public class B4R1PointStateImpl
public PointPointPositionMessageReportedPointPosition PointPosition { get => map(_state.PointPosition); }
public PointPointPositionMessageReportedDegradedPointPosition DegradedPointPosition { get => map(_state.DegradedPointPosition); }

public B4R1PointStateImpl(PointState state){
public B4R1PointStateImpl(PointState state)
{
_state = state;
}

Expand Down

0 comments on commit 1b17126

Please sign in to comment.