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