Search in sources :

Example 6 with ModbusEndpointThingHandler

use of org.openhab.binding.modbus.handler.ModbusEndpointThingHandler in project openhab-addons by openhab.

the class ModbusEndpointDiscoveryService method startScan.

@Override
public boolean startScan(ModbusDiscoveryService service) {
    ModbusEndpointThingHandler handler = this.handler;
    if (handler == null || !handler.isDiscoveryEnabled()) {
        return false;
    }
    logger.trace("Starting discovery on endpoint {}", handler.getUID().getAsString());
    participants.addAll(service.getDiscoveryParticipants());
    startNextParticipant(handler, service);
    return true;
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler)

Example 7 with ModbusEndpointThingHandler

use of org.openhab.binding.modbus.handler.ModbusEndpointThingHandler in project openhab-addons by openhab.

the class ModbusDataThingHandler method initialize.

@Override
public synchronized void initialize() {
    // Long running initialization should be done asynchronously in background.
    try {
        logger.trace("initialize() of thing {} '{}' starting", thing.getUID(), thing.getLabel());
        ModbusDataConfiguration localConfig = config = getConfigAs(ModbusDataConfiguration.class);
        updateUnchangedValuesEveryMillis = localConfig.getUpdateUnchangedValuesEveryMillis();
        Bridge bridge = getBridge();
        if (bridge == null || !bridge.getStatus().equals(ThingStatus.ONLINE)) {
            logger.debug("Thing {} '{}' has no bridge or it is not online", getThing().getUID(), getThing().getLabel());
            updateStatusIfChanged(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "No online bridge");
            return;
        }
        BridgeHandler bridgeHandler = bridge.getHandler();
        if (bridgeHandler == null) {
            logger.warn("Bridge {} '{}' has no handler.", bridge.getUID(), bridge.getLabel());
            String errmsg = String.format("Bridge %s '%s' configuration incomplete or with errors", bridge.getUID(), bridge.getLabel());
            throw new ModbusConfigurationException(errmsg);
        }
        if (bridgeHandler instanceof ModbusEndpointThingHandler) {
            // Write-only thing, parent is endpoint
            ModbusEndpointThingHandler endpointHandler = (ModbusEndpointThingHandler) bridgeHandler;
            slaveId = endpointHandler.getSlaveId();
            comms = endpointHandler.getCommunicationInterface();
            childOfEndpoint = true;
            functionCode = null;
            readRequest = null;
        } else {
            ModbusPollerThingHandler localPollerHandler = (ModbusPollerThingHandler) bridgeHandler;
            pollerHandler = localPollerHandler;
            ModbusReadRequestBlueprint localReadRequest = localPollerHandler.getRequest();
            if (localReadRequest == null) {
                logger.debug("Poller {} '{}' has no read request -- configuration is changing or bridge having invalid configuration?", bridge.getUID(), bridge.getLabel());
                updateStatusIfChanged(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Poller %s '%s' has no poll task", bridge.getUID(), bridge.getLabel()));
                return;
            }
            readRequest = localReadRequest;
            slaveId = localReadRequest.getUnitID();
            functionCode = localReadRequest.getFunctionCode();
            comms = localPollerHandler.getCommunicationInterface();
            pollStart = localReadRequest.getReference();
            childOfEndpoint = false;
        }
        validateAndParseReadParameters(localConfig);
        validateAndParseWriteParameters(localConfig);
        validateMustReadOrWrite();
        updateStatusIfChanged(ThingStatus.ONLINE);
    } catch (ModbusConfigurationException | EndpointNotInitializedException e) {
        logger.debug("Thing {} '{}' initialization error: {}", getThing().getUID(), getThing().getLabel(), e.getMessage());
        updateStatusIfChanged(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
    } finally {
        logger.trace("initialize() of thing {} '{}' finished", thing.getUID(), thing.getLabel());
    }
}
Also used : ModbusDataConfiguration(org.openhab.binding.modbus.internal.config.ModbusDataConfiguration) ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) BridgeHandler(org.openhab.core.thing.binding.BridgeHandler) ModbusConfigurationException(org.openhab.binding.modbus.internal.ModbusConfigurationException) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusPollerThingHandler(org.openhab.binding.modbus.handler.ModbusPollerThingHandler) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) Bridge(org.openhab.core.thing.Bridge)

Example 8 with ModbusEndpointThingHandler

use of org.openhab.binding.modbus.handler.ModbusEndpointThingHandler in project openhab-addons by openhab.

the class AbstractSunSpecHandler method publishUniqueAddress.

/**
 * Publish the unique address property if it has not been set before
 */
private void publishUniqueAddress(ModelBlock block) {
    Map<String, String> properties = getThing().getProperties();
    if (properties.containsKey(PROPERTY_UNIQUE_ADDRESS) && !properties.get(PROPERTY_UNIQUE_ADDRESS).isEmpty()) {
        logger.debug("Current unique address is: {}", properties.get(PROPERTY_UNIQUE_ADDRESS));
        return;
    }
    ModbusEndpointThingHandler handler = getEndpointThingHandler();
    if (handler == null) {
        return;
    }
    getThing().setProperty(PROPERTY_UNIQUE_ADDRESS, handler.getUID().getAsString() + ":" + block.address);
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler)

Example 9 with ModbusEndpointThingHandler

use of org.openhab.binding.modbus.handler.ModbusEndpointThingHandler in project openhab-addons by openhab.

the class AbstractSunSpecHandler method connectEndpoint.

/**
 * Get a reference to the modbus endpoint
 */
private void connectEndpoint() {
    if (comms != null) {
        return;
    }
    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));
        logger.debug("No bridge handler available -- aborting init for {}", label);
        return;
    }
    try {
        slaveId = slaveEndpointThingHandler.getSlaveId();
        comms = slaveEndpointThingHandler.getCommunicationInterface();
    } catch (EndpointNotInitializedException e) {
    // this will be handled below as endpoint remains 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));
        logger.debug("Bridge not initialized fully (no endpoint) -- aborting init for {}", this);
        return;
    }
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) 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) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException)

Example 10 with ModbusEndpointThingHandler

use of org.openhab.binding.modbus.handler.ModbusEndpointThingHandler in project openhab-addons by openhab.

the class AbstractSunSpecHandler method getEndpointThingHandler.

/**
 * Get the endpoint handler from the bridge this handler is connected to
 * Checks that we're connected to the right type of bridge
 *
 * @return the endpoint handler or null if the bridge does not exist
 */
@Nullable
private ModbusEndpointThingHandler getEndpointThingHandler() {
    Bridge bridge = getBridge();
    if (bridge == null) {
        logger.debug("Bridge is null");
        return null;
    }
    if (bridge.getStatus() != ThingStatus.ONLINE) {
        logger.debug("Bridge is not online");
        return null;
    }
    ThingHandler handler = bridge.getHandler();
    if (handler == null) {
        logger.debug("Bridge handler is null");
        return null;
    }
    if (handler instanceof ModbusEndpointThingHandler) {
        ModbusEndpointThingHandler slaveEndpoint = (ModbusEndpointThingHandler) handler;
        return slaveEndpoint;
    } else {
        logger.debug("Unexpected bridge handler: {}", handler);
        return null;
    }
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) Bridge(org.openhab.core.thing.Bridge) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ModbusEndpointThingHandler (org.openhab.binding.modbus.handler.ModbusEndpointThingHandler)13 Bridge (org.openhab.core.thing.Bridge)11 Nullable (org.eclipse.jdt.annotation.Nullable)10 ThingHandler (org.openhab.core.thing.binding.ThingHandler)10 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)8 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)6 Optional (java.util.Optional)5 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)5 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)5 ModbusReadFunctionCode (org.openhab.core.io.transport.modbus.ModbusReadFunctionCode)5 ChannelUID (org.openhab.core.thing.ChannelUID)5 Thing (org.openhab.core.thing.Thing)5 ThingStatus (org.openhab.core.thing.ThingStatus)5 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)5 Command (org.openhab.core.types.Command)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 EndpointNotInitializedException (org.openhab.binding.modbus.handler.EndpointNotInitializedException)4 ArrayList (java.util.ArrayList)3 Unit (javax.measure.Unit)3