Search in sources :

Example 1 with ModbusTcpThingHandler

use of org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler 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 ModbusTcpThingHandler

use of org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler in project openhab-addons by openhab.

the class ModbusDataHandlerTest method createTcpMock.

private Bridge createTcpMock() {
    Bridge tcpBridge = ModbusPollerThingHandlerTest.createTcpThingBuilder("tcp1").build();
    ModbusTcpThingHandler tcpThingHandler = Mockito.mock(ModbusTcpThingHandler.class);
    tcpBridge.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
    tcpBridge.setHandler(tcpThingHandler);
    doReturn(comms).when(tcpThingHandler).getCommunicationInterface();
    try {
        doReturn(0).when(tcpThingHandler).getSlaveId();
    } catch (EndpointNotInitializedException e) {
        // not raised -- we are mocking return value only, not actually calling the method
        throw new IllegalStateException();
    }
    tcpThingHandler.initialize();
    assertThat(tcpBridge.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    return tcpBridge;
}
Also used : ModbusTcpThingHandler(org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) Bridge(org.openhab.core.thing.Bridge)

Example 3 with ModbusTcpThingHandler

use of org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler in project openhab-addons by openhab.

the class ModbusHandlerFactory method createHandler.

@Override
@Nullable
protected ThingHandler createHandler(Thing thing) {
    ThingTypeUID thingTypeUID = thing.getThingTypeUID();
    if (thingTypeUID.equals(THING_TYPE_MODBUS_TCP)) {
        logger.debug("createHandler Modbus tcp");
        return new ModbusTcpThingHandler((Bridge) thing, manager);
    } else if (thingTypeUID.equals(THING_TYPE_MODBUS_SERIAL)) {
        logger.debug("createHandler Modbus serial");
        return new ModbusSerialThingHandler((Bridge) thing, manager);
    } else if (thingTypeUID.equals(THING_TYPE_MODBUS_POLLER)) {
        logger.debug("createHandler Modbus poller");
        return new ModbusPollerThingHandler((Bridge) thing);
    } else if (thingTypeUID.equals(THING_TYPE_MODBUS_DATA)) {
        logger.debug("createHandler data");
        return new ModbusDataThingHandler(thing);
    }
    logger.error("createHandler for unknown thing type uid {}. Thing label was: {}", thing.getThingTypeUID(), thing.getLabel());
    return null;
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) ModbusTcpThingHandler(org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) ModbusSerialThingHandler(org.openhab.binding.modbus.internal.handler.ModbusSerialThingHandler) Bridge(org.openhab.core.thing.Bridge) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ModbusTcpThingHandler (org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler)3 Bridge (org.openhab.core.thing.Bridge)3 Nullable (org.eclipse.jdt.annotation.Nullable)1 Test (org.junit.jupiter.api.Test)1 InOrder (org.mockito.InOrder)1 EndpointNotInitializedException (org.openhab.binding.modbus.handler.EndpointNotInitializedException)1 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)1 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)1 ModbusSerialThingHandler (org.openhab.binding.modbus.internal.handler.ModbusSerialThingHandler)1 Configuration (org.openhab.core.config.core.Configuration)1 EndpointPoolConfiguration (org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration)1 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)1 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)1 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)1 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)1