Search in sources :

Example 1 with AsyncModbusReadResult

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

the class E3DCHandlerStateTest method getDataResult.

private AsyncModbusReadResult getDataResult() {
    byte[] dataBlockBytes = new byte[] { 0, -14, 0, 0, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
    ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
    return new AsyncModbusReadResult(readRequest, new ModbusRegisterArray(dataBlockBytes));
}
Also used : ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult)

Example 2 with AsyncModbusReadResult

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

the class ALD1Handler method readSuccessful.

private void readSuccessful(AsyncModbusReadResult result) {
    result.getRegisters().ifPresent(registers -> {
        if (getThing().getStatus() != ThingStatus.ONLINE) {
            updateStatus(ThingStatus.ONLINE);
        }
        for (ALD1Registers channel : ALD1Registers.values()) {
            int index = channel.getRegisterNumber() - FIRST_READ_REGISTER;
            ModbusBitUtilities.extractStateFromRegisters(registers, index, channel.getType()).map(d -> d.toBigDecimal().multiply(channel.getMultiplier())).map(bigDecimal -> new QuantityType<>(bigDecimal, channel.getUnit())).ifPresent(v -> updateState(createChannelUid(channel), v));
        }
    });
}
Also used : ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) QuantityType(org.openhab.core.library.types.QuantityType) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) BaseModbusThingHandler(org.openhab.binding.modbus.handler.BaseModbusThingHandler) RefreshType(org.openhab.core.types.RefreshType) ModbusReadFunctionCode(org.openhab.core.io.transport.modbus.ModbusReadFunctionCode) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ModbusBitUtilities(org.openhab.core.io.transport.modbus.ModbusBitUtilities) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) ChannelUID(org.openhab.core.thing.ChannelUID) QuantityType(org.openhab.core.library.types.QuantityType) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)

Example 3 with AsyncModbusReadResult

use of org.openhab.core.io.transport.modbus.AsyncModbusReadResult 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);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) PollTask(org.openhab.core.io.transport.modbus.PollTask) 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 4 with AsyncModbusReadResult

use of org.openhab.core.io.transport.modbus.AsyncModbusReadResult 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);
}
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 5 with AsyncModbusReadResult

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

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