Skip to content

Commit

Permalink
Improve error handling (#11)
Browse files Browse the repository at this point in the history
* Improve error handling

* Throw

* wip
  • Loading branch information
NecatiMeral authored Jul 12, 2024
1 parent 40ee0d1 commit 45b2c19
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
14 changes: 7 additions & 7 deletions dotnet/src/NecatiMeral.Logic.Meltem/MeltemNodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ protected virtual string GetDiagnosticsMessage(Exception ex)

protected void ExecuteWithConnection(Action<ModbusClient> action)
{
_client.Connect(IPAddress.Value, Port.Value);
_client.UnitIdentifier = (byte)UnitId.Value;
if (!_client.Connected)
{
return;
}

try
{
_client.Connect(IPAddress.Value, Port.Value);
_client.UnitIdentifier = (byte)UnitId.Value;
if (!_client.Connected)
{
throw new InvalidOperationException("Connection failed");
}

action(_client);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ public void Should_Get_Unbalanced_Ventilation()
// Assert
Sut.VentilationPercentage.Value.ShouldBe(50);
}

[Fact]
public void Should_Handle_Errors()
{
// Arrange
var node = CreateNode();

ConfigureMeltemNode(node);
node.Port.Value = 99999;
node.Action.Value = GetDeviceAction.GetVentilationPercent;
node.Trigger.Value = true;

// Act
node.Execute();

// Assert
node.VentilationPercentage.Value.ShouldBe(0);
node.Diagnostics.Value.ShouldContain("Exception");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public SetVentilationNodeTests(ITestOutputHelper testOutputHelper)
}

[Fact]
public void Should_Set_Balanced_Ventilation()
public void Should_Set_Balanced_Ventilation_Percent()
{
// Arrange
Sut.Action.Value = SetDeviceAction.SetBalancedVentilationPercent;
Expand All @@ -21,14 +21,16 @@ public void Should_Set_Balanced_Ventilation()
Sut.Execute();

// Assert
Sut.Output.Value.ShouldBe(100);

var registers = Server.GetHoldingRegisters((byte)UnitIdentifier);
registers[MeltemRegisters.InitSetVentilation].ShouldBe((short)768);
registers[MeltemRegisters.SetVentilation1].ShouldBe((short)-14336);
registers[MeltemRegisters.ApplyVentilation].ShouldBe((short)0);
}

[Fact]
public void Should_Set_Unbalanced_Ventilation()
public void Should_Set_Unbalanced_Ventilation_Percent()
{
// Arrange
Sut.Action.Value = SetDeviceAction.SetUnbalancedVentilationPercent;
Expand All @@ -39,10 +41,31 @@ public void Should_Set_Unbalanced_Ventilation()
Sut.Execute();

// Assert
Sut.Output.Value.ShouldBe(150);

var registers = Server.GetHoldingRegisters((byte)UnitIdentifier);
registers[MeltemRegisters.InitSetVentilation].ShouldBe((short)1024);
registers[MeltemRegisters.SetVentilation1].ShouldBe((short)25600);
registers[MeltemRegisters.SetVentilation2].ShouldBe((short)-14336);
registers[MeltemRegisters.ApplyVentilation].ShouldBe((short)0);
}

[Fact]
public void Should_Set_Balanced_Ventilation_Numeric()
{
// Arrange
Sut.Action.Value = SetDeviceAction.SetBalancedVentilationNumeric;
Sut.BalancedVentilationLevelNumeric.Value = 5;

// Act
Sut.Execute();

// Assert
Sut.Output.Value.ShouldBe(5);

var registers = Server.GetHoldingRegisters((byte)UnitIdentifier);
registers[MeltemRegisters.InitSetVentilation].ShouldBe((short)768);
registers[MeltemRegisters.SetVentilation1].ShouldBe((short)-14336);
registers[MeltemRegisters.ApplyVentilation].ShouldBe((short)0);
}
}

0 comments on commit 45b2c19

Please sign in to comment.