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);
}
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;
}
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;
}
Aggregations