Skip to main content
Sign in
Snippets Groups Projects
Commit 5abb09a0 authored by Tobias's avatar Tobias
Browse files

Added ClearOutput to IControlComponent

parent c4ffca22
Branches
No related tags found
No related merge requests found
......@@ -228,6 +228,10 @@ namespace ControlComponents.Core
return orderOutputs[role].ChangeComponent(id);
}
public void ClearOutput(string role)
{
orderOutputs[role].ClearComponent();
}
// TODO In the core ControlComponent targetRole is ignored. This is counterintuitive
public virtual TReturn ReadProperty<TReturn>(string targetRole, string propertyName)
......
......
......@@ -2,17 +2,17 @@ using System;
namespace ControlComponents.Core
{
public class FrameControlComponent : ControlComponent
public class FrameControlComponent<T> : ControlComponent where T : IControlComponent
{
// This component includes the external deployed operation mode
private readonly IControlComponent externalControlComponent;
protected readonly T externalControlComponent;
// TODO default name should not be used -> Component names must be unique
public FrameControlComponent(IControlComponent cc, IControlComponentProvider provider, string name = "FrameControlComponent") : base(name)
public FrameControlComponent(T cc, IControlComponentProvider provider, string name = "FrameControlComponent") : base(name)
{
this.externalControlComponent = cc;
// (TODO) create ExternalOpmodeOutput to inject it into opmode
// TODO create ExternalOpmodeOutput to inject it into opmode
var output = new OrderOutput("ExternalOperationMode", ComponentName, provider, cc);
AddOrderOutput(output);
......
......
......@@ -28,5 +28,6 @@ namespace ControlComponents.Core
Task DeselectOperationMode();
bool ChangeOutput(string role, string id);
void ClearOutput(string role);
}
}
\ No newline at end of file
......@@ -11,5 +11,6 @@ namespace ControlComponents.Core
bool IsSet { get; }
bool ChangeComponent(string id);
void ClearComponent();
}
}
\ No newline at end of file
......@@ -245,5 +245,10 @@ namespace ControlComponents.Core
{
return (Role + Id).GetHashCode();
}
public void ClearOutput(string role)
{
controlComponent.CallMethod<string>(Role, nameof(ClearOutput), role);
}
}
}
......@@ -41,7 +41,7 @@ namespace ControlComponents.Protocols
public string ComponentName => throw new NotImplementedException();
public ExecutionState EXST { get; } = ExecutionState.STOPPED;
public ExecutionState EXST { get; private set; } = ExecutionState.STOPPED;
public ExecutionMode EXMODE => throw new NotImplementedException();
......@@ -91,6 +91,7 @@ namespace ControlComponents.Protocols
ControlComponentInfo info = System.Text.Json.JsonSerializer.Deserialize<ControlComponentInfo>(json);
if (info.EXST != EXST)
{
EXST = info.EXST;
ExecutionStateChanged?.Invoke(this, new ExecutionStateEventArgs(EXST));
}
if (info.OCCUPIER != OCCUPIER)
......@@ -250,5 +251,10 @@ namespace ControlComponents.Protocols
{
throw new NotImplementedException();
}
public void ClearOutput(string role)
{
throw new NotImplementedException();
}
}
}
......@@ -21,7 +21,7 @@ namespace ControlComponents.Core.Tests
string FailingOpModeExecute = "FAILING-Execute";
ControlComponent externalCC;
FrameControlComponent sut;
FrameControlComponent<IControlComponent> sut;
Task running;
Mock<IControlComponentProvider> provider;
......@@ -49,7 +49,7 @@ namespace ControlComponents.Core.Tests
externalCC.AddOperationMode(new FailingOperationModeExecute(FailingOpModeExecute));
externalCC.AddOperationMode(new OperationModeAsync(NormalOpMode));
sut = new FrameControlComponent(externalCC, provider.Object);
sut = new FrameControlComponent<IControlComponent>(externalCC, provider.Object);
}
[TearDown]
......
......
......@@ -91,7 +91,7 @@ namespace ControlComponents.Protocols.Tests
.Returns(Task.FromResult(new WebSocketReceiveResult(bytes.Length, WebSocketMessageType.Text, true)));
// Wait for a message of the websocket
await Task.Delay(1000, t.Token).ContinueWith(t => Task.Delay(1));
await Task.Delay(5000, t.Token).ContinueWith(t => Task.Delay(1));
i.Should().Be(1);
}
......@@ -99,44 +99,45 @@ namespace ControlComponents.Protocols.Tests
public async Task When_Created_Then_SubscribedToOccupierChanged()
{
Mock<IWebSocket> socket = new Mock<IWebSocket>();
socket.Setup(s => s.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()))
.Returns(Task.Delay(10).ContinueWith(t => new WebSocketReceiveResult(10, WebSocketMessageType.Text, true)));
var info = new ControlComponentInfo(ExecutionState.STOPPED, SENDER);
var bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<ControlComponentInfo>(info);
socket.Setup(s => s.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new WebSocketReceiveResult(0, WebSocketMessageType.Text, true)));
IWebSocketFactory webSocketFactory = new WebSocketFactory(socket.Object);
ControlComponentHttp sut = new ControlComponentHttp(CC, baseUrl, client, webSocketFactory);
int i = 0;
sut.OccupierChanged += (object sender, OccupationEventArgs e) => i++;
sut.ExecutionStateChanged += (object sender, ExecutionStateEventArgs e) => i++;
await Task.Delay(20);
i.Should().Be(1);
}
[Test]
public void Test1()
{
Mock<IWebSocket> socket = new Mock<IWebSocket>();
IWebSocketFactory webSocketFactory = new WebSocketFactory(socket.Object);
CancellationTokenSource t = new CancellationTokenSource();
sut.OccupierChanged += (object sender, OccupationEventArgs e) => { i++; t.Cancel(); };
sut.ExecutionStateChanged += (object sender, ExecutionStateEventArgs e) => { i++; t.Cancel(); };
var response = new HttpResponseMessage()
// activate the websocket after subscribing to events
socket.Setup(s => s.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()))
.Callback((ArraySegment<byte> buffer, CancellationToken token) =>
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("", Encoding.UTF8, "text/plain")
};
string path = $"/controlcomponent/{CC}/SENDER/OPERATIONS/RESET";
var request = new HttpRequestMessage(HttpMethod.Get, baseUrl + path);
ConfigureHttpClient(response, request);
ControlComponentHttp cc = new ControlComponentHttp(CC, baseUrl, client, webSocketFactory);
cc.Reset(SENDER);
bytes.CopyTo(buffer.Array, 0);
})
.Returns(Task.FromResult(new WebSocketReceiveResult(bytes.Length, WebSocketMessageType.Text, true)));
cc.EXST.Should().Be(ExecutionState.RESETTING);
// Wait for a message of the websocket
await Task.Delay(1000, t.Token).ContinueWith(t => Task.Delay(1));
i.Should().Be(1);
}
[Test]
public void Test2()
public void When_Reset_Then_Resetting()
{
Mock<IWebSocket> socket = new Mock<IWebSocket>();
var info = new ControlComponentInfo(ExecutionState.RESETTING, SENDER);
var bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<ControlComponentInfo>(info);
// activate the websocket after subscribing to events
socket.Setup(s => s.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()))
.Callback((ArraySegment<byte> buffer, CancellationToken token) =>
{
bytes.CopyTo(buffer.Array, 0);
})
.Returns(Task.FromResult(new WebSocketReceiveResult(bytes.Length, WebSocketMessageType.Text, true)));
IWebSocketFactory webSocketFactory = new WebSocketFactory(socket.Object);
var response = new HttpResponseMessage()
......@@ -145,7 +146,7 @@ namespace ControlComponents.Protocols.Tests
Content = new StringContent("", Encoding.UTF8, "text/plain")
};
string path = $"/controlcomponent/{CC}/SENDER/OPERATIONS/RESET";
string path = $"/controlcomponent/{CC}/{SENDER}/OPERATIONS/RESET";
var request = new HttpRequestMessage(HttpMethod.Get, baseUrl + path);
ConfigureHttpClient(response, request);
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment