Skip to content

Commit

Permalink
FIX disabled devices should not try to initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-domenichini committed Jan 10, 2024
1 parent 5ec1b8a commit b2a6014
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Core/SmartIOT.Connector.Core/Scheduler/TagSchedulerEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ private void OnTagWrite(TagScheduleEvent evt)

public bool IsRestartNeeded()
{
if (DeviceDriver.Device.DeviceStatus == DeviceStatus.DISABLED)
return false;

bool restart = _lastRestartInstant == null;
if (!restart
&& _timeService.IsTimeoutElapsed(_lastRestartInstant!.Value, _configuration.RestartDeviceInErrorTimeout))
Expand Down
35 changes: 35 additions & 0 deletions Tests/SmartIOT.Connector.Core.Tests/TagSchedulerEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ private static (TagSchedulerEngine engine, Model.Device device, Tag tag20, Tag?
return (engine, device, device.Tags.First(x => x.TagId == "DB20")!, device.Tags.FirstOrDefault(x => x.TagId == "DB21"), device.Tags.First(x => x.TagId == "DB22")!);
}

[Fact]
public void Test_should_not_restart_on_disabled_device()
{
var timeService = new FakeTimeService
{
Now = DateTime.Now
};

SchedulerConfiguration schedulerConfiguration = new SchedulerConfiguration();

(TagSchedulerEngine engine, Model.Device device, Tag tag20, Tag? tag21, Tag tag22) = SetupSystem(device => new MockDeviceDriver(device), timeService, schedulerConfiguration, true, false, 0);
device.SetEnabled(false); // disable device

var eventListener = new FakeConnector();

engine.TagReadEvent += eventListener.OnTagReadEvent;
engine.TagWriteEvent += eventListener.OnTagWriteEvent;
engine.DeviceStatusEvent += eventListener.OnDeviceStatusEvent;
engine.ExceptionHandler += eventListener.OnException;

// verifica stato iniziale
Assert.Equal(DeviceStatus.DISABLED, device.DeviceStatus);
Assert.False(tag20.IsInitialized);
Assert.Equal(0, tag20.ErrorCode);
Assert.False(tag21!.IsInitialized);
Assert.Equal(0, tag21.ErrorCode);
Assert.False(tag22.IsInitialized);
Assert.Equal(0, tag22.ErrorCode);

// check driver restart
Assert.False(engine.IsRestartNeeded());

Assert.Throws<TagSchedulerWaitException>(() => engine.GetNextTagSchedule());
}

[Fact]
public void Test_read_two_tags_cycle()
{
Expand Down

0 comments on commit b2a6014

Please sign in to comment.