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;
}
}
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;
}
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());
}
}
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;
}
}
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);
}
Aggregations