Search in sources :

Example 1 with ModbusSlaveEndpoint

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

the class ModbusTcpThingHandlerTest method testInitializeAndSlaveEndpoint.

@Test
public void testInitializeAndSlaveEndpoint() throws EndpointNotInitializedException {
    // Using mocked modbus manager
    Configuration thingConfig = new Configuration();
    thingConfig.put("host", "thisishost");
    thingConfig.put("port", 44);
    thingConfig.put("id", 9);
    thingConfig.put("timeBetweenTransactionsMillis", 1);
    thingConfig.put("timeBetweenReconnectMillis", 2);
    thingConfig.put("connectMaxTries", 3);
    thingConfig.put("reconnectAfterMillis", 4);
    thingConfig.put("connectTimeoutMillis", 5);
    EndpointPoolConfiguration expectedPoolConfiguration = new EndpointPoolConfiguration();
    expectedPoolConfiguration.setConnectMaxTries(3);
    expectedPoolConfiguration.setConnectTimeoutMillis(5);
    expectedPoolConfiguration.setInterConnectDelayMillis(2);
    expectedPoolConfiguration.setInterTransactionDelayMillis(1);
    expectedPoolConfiguration.setReconnectAfterMillis(4);
    Bridge thing = createTcpThingBuilder("tcpendpoint").withConfiguration(thingConfig).build();
    addThing(thing);
    assertThat(thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    ModbusTcpThingHandler thingHandler = (ModbusTcpThingHandler) thing.getHandler();
    assertNotNull(thingHandler);
    ModbusSlaveEndpoint slaveEndpoint = thingHandler.getEndpoint();
    assertThat(slaveEndpoint, is(equalTo(new ModbusTCPSlaveEndpoint("thisishost", 44, false))));
    assertThat(thingHandler.getSlaveId(), is(9));
    InOrder orderedVerify = Mockito.inOrder(mockedModbusManager);
    ModbusSlaveEndpoint endpoint = thingHandler.getEndpoint();
    Objects.requireNonNull(endpoint);
    orderedVerify.verify(mockedModbusManager).newModbusCommunicationInterface(endpoint, expectedPoolConfiguration);
}
Also used : InOrder(org.mockito.InOrder) EndpointPoolConfiguration(org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration) Configuration(org.openhab.core.config.core.Configuration) ModbusTcpThingHandler(org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler) EndpointPoolConfiguration(org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) Bridge(org.openhab.core.thing.Bridge) Test(org.junit.jupiter.api.Test)

Example 2 with ModbusSlaveEndpoint

use of org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint 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);
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Configuration(org.openhab.core.config.core.Configuration) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 3 with ModbusSlaveEndpoint

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

the class ModbusPollerThingHandlerTest method testPollingGeneric.

public void testPollingGeneric(String type, ModbusReadFunctionCode expectedFunctionCode) 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", type);
    poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
    addThing(poller);
    assertThat(poller.getStatusInfo().toString(), poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    verifyEndpointBasicInitInteraction();
    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) {
            description.appendText("correct request");
        }

        @Override
        protected boolean matchesSafely(ModbusReadRequestBlueprint request) {
            return checkRequest(request, expectedFunctionCode);
        }
    }), eq(150l), eq(0L), notNull(), notNull());
    verifyNoMoreInteractions(mockedModbusManager);
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Configuration(org.openhab.core.config.core.Configuration) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)

Example 4 with ModbusSlaveEndpoint

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

the class ModbusDataHandlerTest method testReadHandlingGeneric.

@SuppressWarnings({ "null" })
private ModbusDataThingHandler testReadHandlingGeneric(ModbusReadFunctionCode functionCode, String start, String transform, ValueType valueType, BitArray bits, ModbusRegisterArray registers, Exception error, BundleContext context, boolean autoCreateItemsAndLinkToChannels) {
    ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("thisishost", 502, false);
    int pollLength = 3;
    // Minimally mocked request
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    doReturn(pollLength).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", start);
    dataConfig.put("readTransform", transform);
    dataConfig.put("readValueType", valueType.getConfigValue());
    String thingId = "read1";
    ModbusDataThingHandler dataHandler = createDataHandler(thingId, poller, builder -> builder.withConfiguration(dataConfig), context, autoCreateItemsAndLinkToChannels);
    assertThat(dataHandler.getThing().getStatus(), is(equalTo(ThingStatus.ONLINE)));
    // call callbacks
    if (bits != null) {
        assertNull(registers);
        assertNull(error);
        AsyncModbusReadResult result = new AsyncModbusReadResult(request, bits);
        dataHandler.onReadResult(result);
    } else if (registers != null) {
        assertNull(bits);
        assertNull(error);
        AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
        dataHandler.onReadResult(result);
    } else {
        assertNull(bits);
        assertNull(registers);
        assertNotNull(error);
        AsyncModbusFailure<ModbusReadRequestBlueprint> result = new AsyncModbusFailure<ModbusReadRequestBlueprint>(request, error);
        dataHandler.handleReadError(result);
    }
    return dataHandler;
}
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) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) 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 5 with ModbusSlaveEndpoint

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

the class ModbusDataHandlerTest method testOutOfBoundsGeneric.

private void testOutOfBoundsGeneric(int pollStart, int pollLength, String start, ModbusReadFunctionCode functionCode, ValueType valueType, ThingStatus expectedStatus, BundleContext context) {
    ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("thisishost", 502, false);
    // Minimally mocked request
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    doReturn(pollStart).when(request).getReference();
    doReturn(pollLength).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 pollerThing = createPollerMock("poller1", task);
    Configuration dataConfig = new Configuration();
    dataConfig.put("readStart", start);
    dataConfig.put("readTransform", "default");
    dataConfig.put("readValueType", valueType.getConfigValue());
    ModbusDataThingHandler dataHandler = createDataHandler("data1", pollerThing, builder -> builder.withConfiguration(dataConfig), context);
    assertThat(dataHandler.getThing().getStatusInfo().getDescription(), 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