Search in sources :

Example 1 with PollTask

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

the class StuderHandler method registerPollTask.

/**
 * Register poll task
 * This is where we set up our regular poller
 */
private synchronized void registerPollTask(int registerNumber) {
    if (pollTasks.size() >= registers.length) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
        throw new IllegalStateException("New pollTask invalid");
    }
    ModbusCommunicationInterface mycomms = comms;
    StuderConfiguration studerConfig = config;
    if (studerConfig == null || mycomms == null) {
        throw new IllegalStateException("registerPollTask called without proper configuration");
    }
    logger.debug("Setting up regular polling");
    ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(studerConfig.slaveAddress, ModbusReadFunctionCode.READ_INPUT_REGISTERS, registerNumber, 2, studerConfig.maxTries);
    long refreshMillis = studerConfig.refresh * 1000;
    PollTask pollTask = mycomms.registerRegularPoll(request, refreshMillis, 1000, result -> {
        if (result.getRegisters().isPresent()) {
            ModbusRegisterArray reg = result.getRegisters().get();
            handlePolledData(registerNumber, reg);
        } else {
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
            return;
        }
        if (getThing().getStatus() != ThingStatus.ONLINE) {
            updateStatus(ThingStatus.ONLINE);
        }
    }, this::handleError);
    pollTasks.add(pollTask);
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)

Example 2 with PollTask

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

the class StuderHandler method unregisterPollTasks.

private synchronized void unregisterPollTasks() {
    if (pollTasks.isEmpty()) {
        return;
    }
    logger.debug("Unregistering polling from ModbusManager");
    ModbusCommunicationInterface mycomms = comms;
    if (mycomms != null) {
        for (PollTask t : pollTasks) {
            mycomms.unregisterRegularPoll(t);
        }
        pollTasks.clear();
    }
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)

Example 3 with PollTask

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

the class E3DCThingHandler method dispose.

@Override
public void dispose() {
    ModbusCommunicationInterface localComms = comms;
    if (localComms != null) {
        PollTask localInfoPoller = infoPoller;
        if (localInfoPoller != null) {
            localComms.unregisterRegularPoll(localInfoPoller);
        }
        PollTask localDataPoller = dataPoller;
        if (localDataPoller != null) {
            localComms.unregisterRegularPoll(localDataPoller);
        }
    }
    // Comms will be close()'d by endpoint thing handler
    comms = null;
}
Also used : PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)

Example 4 with PollTask

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

the class AbstractSunSpecHandler method registerPollTask.

/**
 * Register poll task
 * This is where we set up our regular poller
 */
private synchronized void registerPollTask(ModelBlock mainBlock) {
    if (pollTask != null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
        throw new IllegalStateException("pollTask should be unregistered before registering a new one!");
    }
    @Nullable ModbusCommunicationInterface mycomms = comms;
    @Nullable SunSpecConfiguration myconfig = config;
    if (myconfig == null || mycomms == null) {
        throw new IllegalStateException("registerPollTask called without proper configuration");
    }
    logger.debug("Setting up regular polling");
    ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(getSlaveId(), ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, mainBlock.address, mainBlock.length, myconfig.maxTries);
    long refreshMillis = myconfig.getRefreshMillis();
    pollTask = mycomms.registerRegularPoll(request, refreshMillis, 1000, result -> {
        result.getRegisters().ifPresent(this::handlePolledData);
        if (getThing().getStatus() != ThingStatus.ONLINE) {
            updateStatus(ThingStatus.ONLINE);
        }
    }, this::handleError);
}
Also used : ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) Unit(javax.measure.Unit) ModelBlock(org.openhab.binding.modbus.sunspec.internal.dto.ModelBlock) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) LoggerFactory(org.slf4j.LoggerFactory) SunSpecConstants(org.openhab.binding.modbus.sunspec.internal.SunSpecConstants) BigDecimal(java.math.BigDecimal) Thing(org.openhab.core.thing.Thing) SunSpecConfiguration(org.openhab.binding.modbus.sunspec.internal.SunSpecConfiguration) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ChannelUID(org.openhab.core.thing.ChannelUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) QuantityType(org.openhab.core.library.types.QuantityType) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ThingHandler(org.openhab.core.thing.binding.ThingHandler) State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) PollTask(org.openhab.core.io.transport.modbus.PollTask) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ModbusReadFunctionCode(org.openhab.core.io.transport.modbus.ModbusReadFunctionCode) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) Optional(java.util.Optional) Bridge(org.openhab.core.thing.Bridge) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) SunSpecConfiguration(org.openhab.binding.modbus.sunspec.internal.SunSpecConfiguration) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 5 with PollTask

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

the class ModbusPollerThingHandlerTest method testRegistersPassedToChildDataThings.

@Test
public void testRegistersPassedToChildDataThings() 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)));
    ArgumentCaptor<ModbusReadCallback> callbackCapturer = ArgumentCaptor.forClass(ModbusReadCallback.class);
    verify(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), callbackCapturer.capture(), notNull());
    ModbusReadCallback readCallback = callbackCapturer.getValue();
    assertNotNull(readCallback);
    ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
    ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
    ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
    assertNotNull(thingHandler);
    ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
    ModbusDataThingHandler child2 = Mockito.mock(ModbusDataThingHandler.class);
    AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
    // has one data child
    thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child1).onReadResult(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
    reset(child1);
    // two children (one child initialized)
    thingHandler.childHandlerInitialized(child2, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child1).onReadResult(result);
    verify(child2).onReadResult(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
    reset(child1);
    reset(child2);
    // one child disposed
    thingHandler.childHandlerDisposed(child1, Mockito.mock(Thing.class));
    readCallback.handle(result);
    verify(child2).onReadResult(result);
    verifyNoMoreInteractions(child1);
    verifyNoMoreInteractions(child2);
}
Also used : ModbusDataThingHandler(org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler) PollTask(org.openhab.core.io.transport.modbus.PollTask) 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) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

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