Search in sources :

Example 11 with AsyncModbusReadResult

use of org.openhab.core.io.transport.modbus.AsyncModbusReadResult 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 12 with AsyncModbusReadResult

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

the class ModbusDataHandlerTest method testUpdateFromHandlerThenCommandFromItem.

@ParameterizedTest
@MethodSource("provideArgsForUpdateThenCommandFromItem")
public void testUpdateFromHandlerThenCommandFromItem(short stateUpdateFromHandler, String bitIndex, short expectedWriteDataToSlave, Command commandFromItem, String channel) {
    int expectedWriteDataToSlaveUnsigned = expectedWriteDataToSlave & 0xFFFF;
    captureModbusWrites();
    Configuration pollerConfig = new Configuration();
    // 0 -> non polling
    pollerConfig.put("refresh", 0L);
    pollerConfig.put("start", 2);
    pollerConfig.put("length", 3);
    pollerConfig.put("type", ModbusBindingConstantsInternal.READ_TYPE_HOLDING_REGISTER);
    ThingUID pollerUID = new ThingUID(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_POLLER, "realPoller");
    Bridge poller = BridgeBuilder.create(ModbusBindingConstantsInternal.THING_TYPE_MODBUS_POLLER, pollerUID).withLabel("label for realPoller").withConfiguration(pollerConfig).withBridge(realEndpointWithMockedComms.getUID()).build();
    addThing(poller);
    assertEquals(ThingStatus.ONLINE, poller.getStatus(), poller.getStatusInfo().getDescription());
    Configuration dataConfig = new Configuration();
    dataConfig.put("writeStart", "3." + bitIndex);
    dataConfig.put("writeValueType", "bit");
    dataConfig.put("writeType", "holding");
    String thingId = "read1";
    ModbusDataThingHandler dataHandler = createDataHandler(thingId, poller, builder -> builder.withConfiguration(dataConfig), bundleContext);
    assertEquals(ThingStatus.ONLINE, dataHandler.getThing().getStatus());
    assertEquals(pollerUID, dataHandler.getThing().getBridgeUID());
    AsyncModbusReadResult result = new AsyncModbusReadResult(Mockito.mock(ModbusReadRequestBlueprint.class), new ModbusRegisterArray(/* register 2, dummy data */
    0, /* register 3 */
    stateUpdateFromHandler, /* register 4, dummy data */
    9));
    // poller receives some data (and therefore data as well)
    getPollerCallback(((ModbusPollerThingHandler) poller.getHandler())).handle(result);
    dataHandler.handleCommand(new ChannelUID(dataHandler.getThing().getUID(), channel), commandFromItem);
    // Assert data written
    {
        assertEquals(1, writeRequests.size());
        ModbusWriteRequestBlueprint writeRequest = writeRequests.get(0);
        assertEquals(writeRequest.getFunctionCode(), ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER);
        assertEquals(writeRequest.getReference(), 3);
        assertEquals(((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().size(), 1);
        assertEquals(expectedWriteDataToSlaveUnsigned, ((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().getRegister(0));
    }
}
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) ModbusWriteRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ChannelUID(org.openhab.core.thing.ChannelUID) ThingUID(org.openhab.core.thing.ThingUID) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusWriteRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) Bridge(org.openhab.core.thing.Bridge) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with AsyncModbusReadResult

use of org.openhab.core.io.transport.modbus.AsyncModbusReadResult 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

AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)13 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)13 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)10 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)8 Configuration (org.openhab.core.config.core.Configuration)8 Thing (org.openhab.core.thing.Thing)7 Test (org.junit.jupiter.api.Test)6 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)6 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)6 AsyncModbusFailure (org.openhab.core.io.transport.modbus.AsyncModbusFailure)3 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)3 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)3 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)3 PollTask (org.openhab.core.io.transport.modbus.PollTask)3 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)3 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)3 Nullable (org.eclipse.jdt.annotation.Nullable)2 BitArray (org.openhab.core.io.transport.modbus.BitArray)2 Bridge (org.openhab.core.thing.Bridge)2 ChannelUID (org.openhab.core.thing.ChannelUID)2