use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testRegistersPassedToChildDataThings.
@Test
public void testRegistersPassedToChildDataThings() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
PollTask pollTask = Mockito.mock(PollTask.class);
doReturn(pollTask).when(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), notNull(), notNull());
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 150L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
ArgumentCaptor<ModbusReadCallback> callbackCapturer = ArgumentCaptor.forClass(ModbusReadCallback.class);
verify(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), callbackCapturer.capture(), notNull());
ModbusReadCallback readCallback = callbackCapturer.getValue();
assertNotNull(readCallback);
ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
assertNotNull(thingHandler);
ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
ModbusDataThingHandler child2 = Mockito.mock(ModbusDataThingHandler.class);
AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
// has one data child
thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
reset(child1);
// two children (one child initialized)
thingHandler.childHandlerInitialized(child2, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child1).onReadResult(result);
verify(child2).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
reset(child1);
reset(child2);
// one child disposed
thingHandler.childHandlerDisposed(child1, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child2).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
}
use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testPollUnregistrationOnDispose.
@Test
public void testPollUnregistrationOnDispose() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
PollTask pollTask = Mockito.mock(PollTask.class);
doReturn(pollTask).when(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), notNull(), notNull());
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 150L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
// verify registration
final AtomicReference<ModbusReadCallback> callbackRef = new AtomicReference<>();
verify(mockedModbusManager).newModbusCommunicationInterface(argThat(new TypeSafeMatcher<ModbusSlaveEndpoint>() {
@Override
public void describeTo(Description description) {
description.appendText("correct endpoint");
}
@Override
protected boolean matchesSafely(ModbusSlaveEndpoint endpoint) {
return checkEndpoint(endpoint);
}
}), any());
verify(comms).registerRegularPoll(argThat(new TypeSafeMatcher<ModbusReadRequestBlueprint>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(ModbusReadRequestBlueprint request) {
return checkRequest(request, ModbusReadFunctionCode.READ_COILS);
}
}), eq(150l), eq(0L), argThat(new TypeSafeMatcher<ModbusReadCallback>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(ModbusReadCallback callback) {
callbackRef.set(callback);
return true;
}
}), notNull());
verifyNoMoreInteractions(mockedModbusManager);
// reset call counts for easy assertions
reset(mockedModbusManager);
// remove the thing
disposeThing(poller);
// 1) should first unregister poll task
verify(comms).unregisterRegularPoll(eq(pollTask));
verifyNoMoreInteractions(mockedModbusManager);
}
use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testRefreshWithPreviousData.
/**
* When there's no recently received data, refresh() will re-use that instead
*
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Test
public void testRefreshWithPreviousData() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 0L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
pollerConfig.put("cacheMillis", 10000L);
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
assertNotNull(thingHandler);
thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
verify(comms, never()).submitOneTimePoll(any(), any(), any());
// data is received
ModbusReadCallback pollerReadCallback = getPollerCallback(thingHandler);
ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
pollerReadCallback.handle(result);
// data child receives the data
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
reset(child1);
// call refresh
// cache is still valid, we should not have real data poll this time
thingHandler.refresh();
verify(comms, never()).submitOneTimePoll(any(), any(), any());
// data child receives the cached data
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
}
use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testRefreshWithOldPreviousData.
@Test
public void testRefreshWithOldPreviousData() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InterruptedException {
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 0L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
pollerConfig.put("cacheMillis", 10L);
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
assertNotNull(thingHandler);
ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
verify(comms, never()).submitOneTimePoll(any(), any(), any());
// data is received
ModbusReadCallback pollerReadCallback = getPollerCallback(thingHandler);
ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
pollerReadCallback.handle(result);
// data child should receive the data
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
reset(child1);
// Sleep to ensure cache expiry
Thread.sleep(15L);
// call refresh. Since cache expired, will poll for more
verify(comms, never()).submitOneTimePoll(any(), any(), any());
thingHandler.refresh();
verify(comms).submitOneTimePoll(any(), any(), any());
}
use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method getPollerCallback.
public ModbusReadCallback getPollerCallback(ModbusPollerThingHandler handler) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field callbackField = ModbusPollerThingHandler.class.getDeclaredField("callbackDelegator");
callbackField.setAccessible(true);
return (ModbusReadCallback) callbackField.get(handler);
}
Aggregations