Search in sources :

Example 1 with EndpointNotInitializedException

use of org.openhab.binding.modbus.handler.EndpointNotInitializedException 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 2 with EndpointNotInitializedException

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

the class ModbusDataHandlerTest method createTcpMock.

private Bridge createTcpMock() {
    Bridge tcpBridge = ModbusPollerThingHandlerTest.createTcpThingBuilder("tcp1").build();
    ModbusTcpThingHandler tcpThingHandler = Mockito.mock(ModbusTcpThingHandler.class);
    tcpBridge.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
    tcpBridge.setHandler(tcpThingHandler);
    doReturn(comms).when(tcpThingHandler).getCommunicationInterface();
    try {
        doReturn(0).when(tcpThingHandler).getSlaveId();
    } catch (EndpointNotInitializedException e) {
        // not raised -- we are mocking return value only, not actually calling the method
        throw new IllegalStateException();
    }
    tcpThingHandler.initialize();
    assertThat(tcpBridge.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    return tcpBridge;
}
Also used : ModbusTcpThingHandler(org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) Bridge(org.openhab.core.thing.Bridge)

Example 3 with EndpointNotInitializedException

use of org.openhab.binding.modbus.handler.EndpointNotInitializedException 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 4 with EndpointNotInitializedException

use of org.openhab.binding.modbus.handler.EndpointNotInitializedException 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 5 with EndpointNotInitializedException

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

the class StiebelEltronHandler method startUp.

/*
     * This method starts the operation of this handler Connect to the slave bridge
     * Start the periodic polling1
     */
private void startUp() {
    if (comms != null) {
        return;
    }
    ModbusEndpointThingHandler slaveEndpointThingHandler = getEndpointThingHandler();
    if (slaveEndpointThingHandler == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "Bridge is offline");
        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));
        return;
    }
    if (config == null) {
        logger.debug("Invalid comms/config/manager ref for stiebel eltron handler");
        return;
    }
    if (systemInformationPoller == null) {
        AbstractBasePoller poller = new AbstractBasePoller() {

            @Override
            protected void handlePolledData(ModbusRegisterArray registers) {
                handlePolledSystemInformationData(registers);
            }
        };
        poller.registerPollTask(500, 36, ModbusReadFunctionCode.READ_INPUT_REGISTERS);
        systemInformationPoller = poller;
    }
    if (energyPoller == null) {
        AbstractBasePoller poller = new AbstractBasePoller() {

            @Override
            protected void handlePolledData(ModbusRegisterArray registers) {
                handlePolledEnergyData(registers);
            }
        };
        poller.registerPollTask(3500, 16, ModbusReadFunctionCode.READ_INPUT_REGISTERS);
        energyPoller = poller;
    }
    if (systemStatePoller == null) {
        AbstractBasePoller poller = new AbstractBasePoller() {

            @Override
            protected void handlePolledData(ModbusRegisterArray registers) {
                handlePolledSystemStateData(registers);
            }
        };
        poller.registerPollTask(2500, 2, ModbusReadFunctionCode.READ_INPUT_REGISTERS);
        systemStatePoller = poller;
    }
    if (systemParameterPoller == null) {
        AbstractBasePoller poller = new AbstractBasePoller() {

            @Override
            protected void handlePolledData(ModbusRegisterArray registers) {
                handlePolledSystemParameterData(registers);
            }
        };
        poller.registerPollTask(1500, 11, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS);
        systemParameterPoller = poller;
    }
    updateStatus(ThingStatus.UNKNOWN);
}
Also used : ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) OpenClosedType(org.openhab.core.library.types.OpenClosedType) Unit(javax.measure.Unit) AsyncModbusFailure(org.openhab.core.io.transport.modbus.AsyncModbusFailure) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ModbusWriteRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint) LoggerFactory(org.slf4j.LoggerFactory) SystemInformationBlock(org.openhab.binding.modbus.stiebeleltron.internal.dto.SystemInformationBlock) StiebelEltronBindingConstants(org.openhab.binding.modbus.stiebeleltron.internal.StiebelEltronBindingConstants) CELSIUS(org.openhab.core.library.unit.SIUnits.CELSIUS) SystemStateBlockParser(org.openhab.binding.modbus.stiebeleltron.internal.parser.SystemStateBlockParser) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint) 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) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) SystemParameterBlockParser(org.openhab.binding.modbus.stiebeleltron.internal.parser.SystemParameterBlockParser) EnergyBlock(org.openhab.binding.modbus.stiebeleltron.internal.dto.EnergyBlock) DecimalType(org.openhab.core.library.types.DecimalType) Units(org.openhab.core.library.unit.Units) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) StiebelEltronConfiguration(org.openhab.binding.modbus.stiebeleltron.internal.StiebelEltronConfiguration) SystemParameterBlock(org.openhab.binding.modbus.stiebeleltron.internal.dto.SystemParameterBlock) 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) RefreshType(org.openhab.core.types.RefreshType) 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) SystemInfromationBlockParser(org.openhab.binding.modbus.stiebeleltron.internal.parser.SystemInfromationBlockParser) ModbusEndpointThingHandler(org.openhab.binding.modbus.handler.ModbusEndpointThingHandler) EnergyBlockParser(org.openhab.binding.modbus.stiebeleltron.internal.parser.EnergyBlockParser) Optional(java.util.Optional) SystemStateBlock(org.openhab.binding.modbus.stiebeleltron.internal.dto.SystemStateBlock) Bridge(org.openhab.core.thing.Bridge) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException)

Aggregations

EndpointNotInitializedException (org.openhab.binding.modbus.handler.EndpointNotInitializedException)5 Bridge (org.openhab.core.thing.Bridge)5 ModbusEndpointThingHandler (org.openhab.binding.modbus.handler.ModbusEndpointThingHandler)4 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)4 Optional (java.util.Optional)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 AsyncModbusFailure (org.openhab.core.io.transport.modbus.AsyncModbusFailure)3 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)3 ModbusReadFunctionCode (org.openhab.core.io.transport.modbus.ModbusReadFunctionCode)3 PollTask (org.openhab.core.io.transport.modbus.PollTask)3 ChannelUID (org.openhab.core.thing.ChannelUID)3 Thing (org.openhab.core.thing.Thing)3 ThingStatus (org.openhab.core.thing.ThingStatus)3 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)3 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)3 ThingHandler (org.openhab.core.thing.binding.ThingHandler)3 Command (org.openhab.core.types.Command)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3