Search in sources :

Example 26 with ModbusSlaveEndpoint

use of org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint in project openhab-core by openhab.

the class SmokeTest method testUnregisterPollingExplicit.

@Test
public void testUnregisterPollingExplicit() throws Exception {
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger unexpectedCount = new AtomicInteger();
    AtomicInteger errorCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(3);
    AtomicInteger expectedReceived = new AtomicInteger();
    long start = System.currentTimeMillis();
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        PollTask task = comms.registerRegularPoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 1, 15, 1), 200, 0, result -> {
            Optional<ModbusRegisterArray> registersOptional = result.getRegisters();
            if (registersOptional.isPresent()) {
                expectedReceived.incrementAndGet();
            } else {
                // bits
                unexpectedCount.incrementAndGet();
            }
            callbackCalled.countDown();
        }, failure -> {
            if (spi.getDigitalInCount() > 0) {
                // No errors expected after server filled with data
                unexpectedCount.incrementAndGet();
            } else {
                expectedReceived.incrementAndGet();
                errorCount.incrementAndGet();
                generateData();
            }
        });
        assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
        long end = System.currentTimeMillis();
        assertPollDetails(unexpectedCount, expectedReceived, start, end, 190, 600);
        // Explicitly unregister the regular poll
        comms.unregisterRegularPoll(task);
        // wait some more and ensure nothing comes back
        Thread.sleep(500);
        assertThat(unexpectedCount.get(), is(equalTo(0)));
    }
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 27 with ModbusSlaveEndpoint

use of org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint in project openhab-core by openhab.

the class SmokeTest method testSlaveReadErrorResponse.

/**
 * Test handling of slave error responses. In this case, error code = 2, illegal data address, since no data.
 *
 * @throws Exception
 */
@Test
public void testSlaveReadErrorResponse() throws Exception {
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger okCount = new AtomicInteger();
    AtomicInteger errorCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(1);
    AtomicReference<Exception> lastError = new AtomicReference<>();
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
            assert result.getRegisters().isPresent();
            okCount.incrementAndGet();
            callbackCalled.countDown();
        }, failure -> {
            errorCount.incrementAndGet();
            lastError.set(failure.getCause());
            callbackCalled.countDown();
        });
        assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
        assertThat(okCount.get(), is(equalTo(0)));
        assertThat(errorCount.get(), is(equalTo(1)));
        assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
    }
}
Also used : ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ModbusConnectionException(org.openhab.core.io.transport.modbus.exception.ModbusConnectionException) IOException(java.io.IOException) ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) Test(org.junit.jupiter.api.Test)

Example 28 with ModbusSlaveEndpoint

use of org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint in project openhab-core by openhab.

the class SmokeTest method testIOError.

/**
 * Have super slow connection response, eventually resulting as timeout (due to default timeout of 3 s in
 * net.wimpi.modbus.Modbus.DEFAULT_TIMEOUT)
 *
 * @throws Exception
 */
@Test
public void testIOError() throws Exception {
    artificialServerWait = 60000;
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger okCount = new AtomicInteger();
    AtomicInteger errorCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(1);
    AtomicReference<Exception> lastError = new AtomicReference<>();
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
            assert result.getRegisters().isPresent();
            okCount.incrementAndGet();
            callbackCalled.countDown();
        }, failure -> {
            errorCount.incrementAndGet();
            lastError.set(failure.getCause());
            callbackCalled.countDown();
        });
        assertTrue(callbackCalled.await(15, TimeUnit.SECONDS));
        assertThat(okCount.get(), is(equalTo(0)));
        assertThat(lastError.toString(), errorCount.get(), is(equalTo(1)));
        assertTrue(lastError.get() instanceof ModbusSlaveIOException, lastError.toString());
    }
}
Also used : ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) CountDownLatch(java.util.concurrent.CountDownLatch) ModbusConnectionException(org.openhab.core.io.transport.modbus.exception.ModbusConnectionException) IOException(java.io.IOException) ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) Test(org.junit.jupiter.api.Test)

Example 29 with ModbusSlaveEndpoint

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

the class ModbusDataHandlerTest method testInitGeneric.

/**
 * @param pollerFunctionCode poller function code. Use null if you want to have data thing direct child of endpoint
 *            thing
 * @param pollerStart start index of poller
 * @param config thing config
 * @param statusConsumer assertion method for data thingstatus
 */
private void testInitGeneric(ModbusReadFunctionCode pollerFunctionCode, int pollerStart, Configuration config, Consumer<ThingStatusInfo> statusConsumer) {
    int pollLength = 3;
    Bridge parent;
    if (pollerFunctionCode == null) {
        parent = createTcpMock();
        addThing(parent);
    } else {
        ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("thisishost", 502, false);
        // Minimally mocked request
        ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
        doReturn(pollerStart).when(request).getReference();
        doReturn(pollLength).when(request).getDataLength();
        doReturn(pollerFunctionCode).when(request).getFunctionCode();
        PollTask task = Mockito.mock(PollTask.class);
        doReturn(endpoint).when(task).getEndpoint();
        doReturn(request).when(task).getRequest();
        parent = createPollerMock("poller1", task);
    }
    String thingId = "read1";
    ModbusDataThingHandler dataHandler = createDataHandler(thingId, parent, builder -> builder.withConfiguration(config), bundleContext);
    statusConsumer.accept(dataHandler.getThing().getStatusInfo());
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) 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)

Example 30 with ModbusSlaveEndpoint

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

the class ModbusDataHandlerTest method testValueTypeGeneric.

private void testValueTypeGeneric(ModbusReadFunctionCode functionCode, ValueType valueType, ThingStatus expectedStatus) {
    ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("thisishost", 502, false);
    // Minimally mocked request
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    doReturn(3).when(request).getDataLength();
    doReturn(functionCode).when(request).getFunctionCode();
    PollTask task = Mockito.mock(PollTask.class);
    doReturn(endpoint).when(task).getEndpoint();
    doReturn(request).when(task).getRequest();
    Bridge poller = createPollerMock("poller1", task);
    Configuration dataConfig = new Configuration();
    dataConfig.put("readStart", "1");
    dataConfig.put("readTransform", "default");
    dataConfig.put("readValueType", valueType.getConfigValue());
    ModbusDataThingHandler dataHandler = createDataHandler("data1", poller, builder -> builder.withConfiguration(dataConfig));
    assertThat(dataHandler.getThing().getStatus(), is(equalTo(expectedStatus)));
}
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) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) Bridge(org.openhab.core.thing.Bridge)

Aggregations

ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)32 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)23 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)20 Test (org.junit.jupiter.api.Test)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)14 PollTask (org.openhab.core.io.transport.modbus.PollTask)13 ModbusConnectionException (org.openhab.core.io.transport.modbus.exception.ModbusConnectionException)12 IOException (java.io.IOException)11 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)10 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)8 Configuration (org.openhab.core.config.core.Configuration)8 BitArray (org.openhab.core.io.transport.modbus.BitArray)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ModbusSlaveErrorResponseException (org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException)7 ModbusSlaveIOException (org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException)7 Bridge (org.openhab.core.thing.Bridge)7 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)6