Search in sources :

Example 1 with ModbusCommunicationInterface

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

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

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

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

the class E3DCThingHandler method connectEndpoint.

/**
 * Get a reference to the modbus endpoint
 */
@Nullable
private ModbusCommunicationInterface connectEndpoint() {
    if (comms != null) {
        return comms;
    }
    ModbusEndpointThingHandler slaveEndpointThingHandler = getEndpointThingHandler();
    if (slaveEndpointThingHandler == null) {
        @SuppressWarnings("null") String label = Optional.ofNullable(getBridge()).map(b -> b.getLabel()).orElse("<null>");
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Bridge '%s' is offline", label));
        return null;
    }
    try {
        slaveId = slaveEndpointThingHandler.getSlaveId();
        comms = slaveEndpointThingHandler.getCommunicationInterface();
    } catch (EndpointNotInitializedException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String.format("Slave Endpoint not initialized"));
        return null;
    }
    if (comms == null) {
        @SuppressWarnings("null") String label = Optional.ofNullable(getBridge()).map(b -> b.getLabel()).orElse("<null>");
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Bridge '%s' not completely initialized", label));
        return null;
    } else {
        return comms;
    }
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) StringBlock(org.openhab.binding.modbus.e3dc.internal.dto.StringBlock) DataType(org.openhab.binding.modbus.e3dc.internal.modbus.Data.DataType) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) E3DCModbusConstans(org.openhab.binding.modbus.e3dc.internal.modbus.E3DCModbusConstans) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) E3DCBindingConstants(org.openhab.binding.modbus.e3dc.internal.E3DCBindingConstants) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ChannelUID(org.openhab.core.thing.ChannelUID) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) 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) EmergencyBlock(org.openhab.binding.modbus.e3dc.internal.dto.EmergencyBlock) PollTask(org.openhab.core.io.transport.modbus.PollTask) ModbusReadFunctionCode(org.openhab.core.io.transport.modbus.ModbusReadFunctionCode) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) PowerBlock(org.openhab.binding.modbus.e3dc.internal.dto.PowerBlock) ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) Data(org.openhab.binding.modbus.e3dc.internal.modbus.Data) Parser(org.openhab.binding.modbus.e3dc.internal.modbus.Parser) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) Optional(java.util.Optional) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) InfoBlock(org.openhab.binding.modbus.e3dc.internal.dto.InfoBlock) E3DCConfiguration(org.openhab.binding.modbus.e3dc.internal.E3DCConfiguration) Bridge(org.openhab.core.thing.Bridge) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 5 with ModbusCommunicationInterface

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

the class StiebelEltronHandler method writeInt16.

/**
 * @param address address of the value to be written on the modbus
 * @param shortValue value to be written on the modbus
 */
protected void writeInt16(int address, short shortValue) {
    StiebelEltronConfiguration myconfig = StiebelEltronHandler.this.config;
    ModbusCommunicationInterface mycomms = StiebelEltronHandler.this.comms;
    if (myconfig == null || mycomms == null) {
        throw new IllegalStateException("registerPollTask called without proper configuration");
    }
    // big endian byte ordering
    byte hi = (byte) (shortValue >> 8);
    byte lo = (byte) shortValue;
    ModbusRegisterArray data = new ModbusRegisterArray(hi, lo);
    ModbusWriteRegisterRequestBlueprint request = new ModbusWriteRegisterRequestBlueprint(slaveId, address, data, false, myconfig.getMaxTries());
    mycomms.submitOneTimeWrite(request, result -> {
        if (hasConfigurationError()) {
            return;
        }
        logger.debug("Successful write, matching request {}", request);
        StiebelEltronHandler.this.updateStatus(ThingStatus.ONLINE);
    }, failure -> {
        StiebelEltronHandler.this.handleWriteError(failure);
    });
}
Also used : StiebelEltronConfiguration(org.openhab.binding.modbus.stiebeleltron.internal.StiebelEltronConfiguration) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)

Aggregations

ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)34 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)20 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 Test (org.junit.jupiter.api.Test)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 PollTask (org.openhab.core.io.transport.modbus.PollTask)10 IOException (java.io.IOException)8 BitArray (org.openhab.core.io.transport.modbus.BitArray)8 ModbusConnectionException (org.openhab.core.io.transport.modbus.exception.ModbusConnectionException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)7 ModbusSlaveErrorResponseException (org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException)7 ModbusSlaveIOException (org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException)7 Optional (java.util.Optional)6 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)6 Nullable (org.eclipse.jdt.annotation.Nullable)5 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)4