Search in sources :

Example 6 with ModbusReadCallback

use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.

the class ModbusPollerThingHandlerTest method testRefreshWithPreviousData2.

/**
 * Testing again caching, such that most recently received data is propagated to children
 *
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws InterruptedException
 */
@Test
public void testRefreshWithPreviousData2() 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", 10000L);
    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);
    ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback = getPollerFailureCallback(thingHandler);
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    ModbusReadRequestBlueprint request2 = Mockito.mock(ModbusReadRequestBlueprint.class);
    ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
    Exception error = Mockito.mock(Exception.class);
    AsyncModbusReadResult registersResult = new AsyncModbusReadResult(request, registers);
    AsyncModbusFailure<ModbusReadRequestBlueprint> errorResult = new AsyncModbusFailure<ModbusReadRequestBlueprint>(request2, error);
    pollerReadCallback.handle(registersResult);
    // data child should receive the data
    verify(child1).onReadResult(registersResult);
    verifyNoMoreInteractions(child1);
    reset(child1);
    // Sleep to have time between the data
    Thread.sleep(5L);
    // error is received
    failureCallback.handle(errorResult);
    // data child should receive the error
    verify(child1).handleReadError(errorResult);
    verifyNoMoreInteractions(child1);
    reset(child1);
    // call refresh, should return latest data (that is, error)
    // 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 error
    verify(child1).handleReadError(errorResult);
    verifyNoMoreInteractions(child1);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) Configuration(org.openhab.core.config.core.Configuration) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Example 7 with ModbusReadCallback

use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.

the class ModbusPollerThingHandlerTest method testRefreshWithPreviousDataCacheDisabled.

/**
 * When there's no recently received data, refresh() will re-use that instead
 *
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws NoSuchFieldException
 * @throws SecurityException
 */
@Test
public void testRefreshWithPreviousDataCacheDisabled() 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", 0L);
    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 receives the data
    verify(child1).onReadResult(result);
    verifyNoMoreInteractions(child1);
    reset(child1);
    // call refresh
    // caching disabled, should poll from manager
    thingHandler.refresh();
    verify(comms).submitOneTimePoll(any(), any(), any());
    verifyNoMoreInteractions(mockedModbusManager);
    // data child receives the cached data
    verifyNoMoreInteractions(child1);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) Configuration(org.openhab.core.config.core.Configuration) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Example 8 with ModbusReadCallback

use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.

the class ModbusDataHandlerTest method getPollerCallback.

public ModbusReadCallback getPollerCallback(ModbusPollerThingHandler handler) {
    Field callbackField;
    try {
        callbackField = ModbusPollerThingHandler.class.getDeclaredField("callbackDelegator");
        callbackField.setAccessible(true);
        return (ModbusReadCallback) callbackField.get(handler);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        fail(e);
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler)

Example 9 with ModbusReadCallback

use of org.openhab.core.io.transport.modbus.ModbusReadCallback in project openhab-addons by openhab.

the class ModbusPollerThingHandlerTest method testBitsPassedToChildDataThings.

@Test
public void testBitsPassedToChildDataThings() 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(any(), eq(150l), eq(0L), callbackCapturer.capture(), notNull());
    ModbusReadCallback readCallback = callbackCapturer.getValue();
    assertNotNull(readCallback);
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    BitArray bits = Mockito.mock(BitArray.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, bits);
    // 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);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) PollTask(org.openhab.core.io.transport.modbus.PollTask) Configuration(org.openhab.core.config.core.Configuration) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) BitArray(org.openhab.core.io.transport.modbus.BitArray) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Aggregations

ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)9 Test (org.junit.jupiter.api.Test)7 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)7 Configuration (org.openhab.core.config.core.Configuration)7 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)7 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)6 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)6 Thing (org.openhab.core.thing.Thing)6 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)5 PollTask (org.openhab.core.io.transport.modbus.PollTask)3 Field (java.lang.reflect.Field)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Description (org.hamcrest.Description)1 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)1 AsyncModbusFailure (org.openhab.core.io.transport.modbus.AsyncModbusFailure)1 BitArray (org.openhab.core.io.transport.modbus.BitArray)1 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)1