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