Search in sources :

Example 11 with PollTask

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

the class ModbusPollerThingHandler method unregisterPollTask.

/**
 * Unregister poll task.
 *
 * No-op in case no poll task is registered, or if the initialization is incomplete.
 */
public synchronized void unregisterPollTask() {
    logger.trace("unregisterPollTask()");
    if (config == null) {
        return;
    }
    PollTask localPollTask = this.pollTask;
    if (localPollTask != null) {
        logger.debug("Unregistering polling from ModbusManager");
        comms.unregisterRegularPoll(localPollTask);
    }
    this.pollTask = null;
    request = null;
    comms = null;
    updateStatus(ThingStatus.OFFLINE);
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask)

Example 12 with PollTask

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

the class AbstractSunSpecHandler method unregisterPollTask.

/**
 * Unregister poll task.
 *
 * No-op in case no poll task is registered, or if the initialization is incomplete.
 */
private synchronized void unregisterPollTask() {
    @Nullable PollTask task = pollTask;
    if (task == null) {
        return;
    }
    logger.debug("Unregistering polling from ModbusManager");
    @Nullable ModbusCommunicationInterface mycomms = comms;
    if (mycomms != null) {
        mycomms.unregisterRegularPoll(task);
    }
    pollTask = null;
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 13 with PollTask

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

the class BaseModbusThingHandler method registerRegularPoll.

/**
 * Register regularly polled task. The method returns immediately, and the execution of the poll task will happen in
 * the background.
 *
 * One can register only one regular poll task for triplet of (endpoint, request, callback).
 *
 * @param request request to send
 * @param pollPeriodMillis poll interval, in milliseconds
 * @param initialDelayMillis initial delay before starting polling, in milliseconds
 * @param callback callback to call with data
 * @param callback callback to call in case of failure
 * @return poll task representing the regular poll
 * @throws IllegalStateException when this communication has been closed already
 */
public PollTask registerRegularPoll(ModbusReadRequestBlueprint request, long pollPeriodMillis, long initialDelayMillis, ModbusReadCallback resultCallback, ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback) {
    PollTask task = getModbus().registerRegularPoll(request, pollPeriodMillis, initialDelayMillis, resultCallback, failureCallback);
    periodicPollers.add(task);
    return task;
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask)

Example 14 with PollTask

use of org.openhab.core.io.transport.modbus.PollTask 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());
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) PollTask(org.openhab.core.io.transport.modbus.PollTask) 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) 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 15 with PollTask

use of org.openhab.core.io.transport.modbus.PollTask 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)));
}
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) Bridge(org.openhab.core.thing.Bridge)

Aggregations

PollTask (org.openhab.core.io.transport.modbus.PollTask)21 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)16 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)10 Configuration (org.openhab.core.config.core.Configuration)10 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)10 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)8 Bridge (org.openhab.core.thing.Bridge)8 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)7 Test (org.junit.jupiter.api.Test)6 AsyncModbusFailure (org.openhab.core.io.transport.modbus.AsyncModbusFailure)5 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)5 Thing (org.openhab.core.thing.Thing)5 Nullable (org.eclipse.jdt.annotation.Nullable)4 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)4 ChannelUID (org.openhab.core.thing.ChannelUID)4 Optional (java.util.Optional)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)3 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)3 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)3