Search in sources :

Example 1 with AsyncModbusFailure

use of org.openhab.core.io.transport.modbus.AsyncModbusFailure 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 2 with AsyncModbusFailure

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

the class ModbusPollerThingHandlerTest method testRefreshWithPreviousData2.

/**
 * Testing again caching, such that most recently received data is propagated to children
 *
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws InterruptedException
 */
@Test
public void testRefreshWithPreviousData2() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InterruptedException {
    Configuration pollerConfig = new Configuration();
    pollerConfig.put("refresh", 0L);
    pollerConfig.put("start", 5);
    pollerConfig.put("length", 13);
    pollerConfig.put("type", "coil");
    pollerConfig.put("cacheMillis", 10000L);
    poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
    addThing(poller);
    verifyEndpointBasicInitInteraction();
    ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
    assertNotNull(thingHandler);
    ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
    thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
    assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    verify(comms, never()).submitOneTimePoll(any(), any(), any());
    // data is received
    ModbusReadCallback pollerReadCallback = getPollerCallback(thingHandler);
    ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback = getPollerFailureCallback(thingHandler);
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    ModbusReadRequestBlueprint request2 = Mockito.mock(ModbusReadRequestBlueprint.class);
    ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
    Exception error = Mockito.mock(Exception.class);
    AsyncModbusReadResult registersResult = new AsyncModbusReadResult(request, registers);
    AsyncModbusFailure<ModbusReadRequestBlueprint> errorResult = new AsyncModbusFailure<ModbusReadRequestBlueprint>(request2, error);
    pollerReadCallback.handle(registersResult);
    // data child should receive the data
    verify(child1).onReadResult(registersResult);
    verifyNoMoreInteractions(child1);
    reset(child1);
    // Sleep to have time between the data
    Thread.sleep(5L);
    // error is received
    failureCallback.handle(errorResult);
    // data child should receive the error
    verify(child1).handleReadError(errorResult);
    verifyNoMoreInteractions(child1);
    reset(child1);
    // call refresh, should return latest data (that is, error)
    // cache is still valid, we should not have real data poll this time
    thingHandler.refresh();
    verify(comms, never()).submitOneTimePoll(any(), any(), any());
    // data child receives the cached error
    verify(child1).handleReadError(errorResult);
    verifyNoMoreInteractions(child1);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) Configuration(org.openhab.core.config.core.Configuration) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusReadCallback(org.openhab.core.io.transport.modbus.ModbusReadCallback) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncModbusFailure

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

the class ModbusPollerThingHandlerTest method testErrorPassedToChildDataThings.

@Test
public void testErrorPassedToChildDataThings() 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();
    assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    final ArgumentCaptor<ModbusFailureCallback<ModbusReadRequestBlueprint>> callbackCapturer = ArgumentCaptor.forClass((Class) ModbusFailureCallback.class);
    verify(comms).registerRegularPoll(any(), eq(150l), eq(0L), notNull(), callbackCapturer.capture());
    ModbusFailureCallback<ModbusReadRequestBlueprint> readCallback = callbackCapturer.getValue();
    assertNotNull(readCallback);
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    Exception error = Mockito.mock(Exception.class);
    ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
    assertNotNull(thingHandler);
    ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
    ModbusDataThingHandler child2 = Mockito.mock(ModbusDataThingHandler.class);
    AsyncModbusFailure<ModbusReadRequestBlueprint> result = new AsyncModbusFailure<ModbusReadRequestBlueprint>(request, error);
    // has one data child
    thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child1).handleReadError(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
    reset(child1);
    // two children (one child initialized)
    thingHandler.childHandlerInitialized(child2, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child1).handleReadError(result);
    verify(child2).handleReadError(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
    reset(child1);
    reset(child2);
    // one child disposed
    thingHandler.childHandlerDisposed(child1, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child2).handleReadError(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
}
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) ModbusFailureCallback(org.openhab.core.io.transport.modbus.ModbusFailureCallback) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Aggregations

ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)3 Configuration (org.openhab.core.config.core.Configuration)3 AsyncModbusFailure (org.openhab.core.io.transport.modbus.AsyncModbusFailure)3 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)3 Test (org.junit.jupiter.api.Test)2 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)2 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)2 PollTask (org.openhab.core.io.transport.modbus.PollTask)2 Thing (org.openhab.core.thing.Thing)2 ModbusFailureCallback (org.openhab.core.io.transport.modbus.ModbusFailureCallback)1 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)1 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)1 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)1 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)1 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)1 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)1 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)1 Bridge (org.openhab.core.thing.Bridge)1