Search in sources :

Example 1 with AsyncModbusWriteResult

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

the class ModbusDataHandlerTest method testWriteHandlingGeneric.

@SuppressWarnings({ "null" })
private ModbusDataThingHandler testWriteHandlingGeneric(String start, String transform, ValueType valueType, String writeType, ModbusWriteFunctionCode successFC, String channel, Command command, Exception error, BundleContext context, boolean parentIsEndpoint) {
    ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("thisishost", 502, false);
    // Minimally mocked request
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    PollTask task = Mockito.mock(PollTask.class);
    doReturn(endpoint).when(task).getEndpoint();
    doReturn(request).when(task).getRequest();
    final Bridge parent;
    if (parentIsEndpoint) {
        parent = createTcpMock();
        addThing(parent);
    } else {
        parent = createPollerMock("poller1", task);
    }
    Configuration dataConfig = new Configuration();
    dataConfig.put("readStart", "");
    dataConfig.put("writeStart", start);
    dataConfig.put("writeTransform", transform);
    dataConfig.put("writeValueType", valueType.getConfigValue());
    dataConfig.put("writeType", writeType);
    String thingId = "write";
    ModbusDataThingHandler dataHandler = createDataHandler(thingId, parent, builder -> builder.withConfiguration(dataConfig), context);
    assertThat(dataHandler.getThing().getStatus(), is(equalTo(ThingStatus.ONLINE)));
    dataHandler.handleCommand(new ChannelUID(dataHandler.getThing().getUID(), channel), command);
    if (error != null) {
        dataHandler.handleReadError(new AsyncModbusFailure<ModbusReadRequestBlueprint>(request, error));
    } else {
        ModbusResponse resp = new ModbusResponse() {

            @Override
            public int getFunctionCode() {
                return successFC.getFunctionCode();
            }
        };
        dataHandler.onWriteResponse(new AsyncModbusWriteResult(Mockito.mock(ModbusWriteRequestBlueprint.class), resp));
    }
    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) ChannelUID(org.openhab.core.thing.ChannelUID) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) ModbusResponse(org.openhab.core.io.transport.modbus.ModbusResponse) AsyncModbusWriteResult(org.openhab.core.io.transport.modbus.AsyncModbusWriteResult) Bridge(org.openhab.core.thing.Bridge)

Example 2 with AsyncModbusWriteResult

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

the class ModbusManagerImpl method invokeCallbackWithResponse.

private void invokeCallbackWithResponse(ModbusWriteRequestBlueprint request, ModbusWriteCallback callback, org.openhab.core.io.transport.modbus.ModbusResponse response) {
    try {
        logger.trace("Calling write response callback {} for request {}. Response was {}", callback, request, response);
        callback.handle(new AsyncModbusWriteResult(request, response));
    } finally {
        logger.trace("Called write response callback {} for request {}. Response was {}", callback, request, response);
    }
}
Also used : AsyncModbusWriteResult(org.openhab.core.io.transport.modbus.AsyncModbusWriteResult)

Aggregations

AsyncModbusWriteResult (org.openhab.core.io.transport.modbus.AsyncModbusWriteResult)2 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)1 Configuration (org.openhab.core.config.core.Configuration)1 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)1 ModbusResponse (org.openhab.core.io.transport.modbus.ModbusResponse)1 PollTask (org.openhab.core.io.transport.modbus.PollTask)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 ChannelUID (org.openhab.core.thing.ChannelUID)1