diff --git a/bundles/org.openhab.binding.modbus/DEVELOPERS.md b/bundles/org.openhab.binding.modbus/DEVELOPERS.md index ad7a256a0afc8..fcde24e1fdfae 100644 --- a/bundles/org.openhab.binding.modbus/DEVELOPERS.md +++ b/bundles/org.openhab.binding.modbus/DEVELOPERS.md @@ -115,9 +115,7 @@ public class MyHandler extends BaseModbusThingHandler { } @Override - public void initialize() { - super.initialize(); - + public void modbusInitialize() { // do other Thing initialization ModbusReadRequestBlueprint blueprint = new ModbusReadRequestBlueprint(getSlaveId(), diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/BaseModbusThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/BaseModbusThingHandler.java index 727261e5d41f3..aca117e657200 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/BaseModbusThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/BaseModbusThingHandler.java @@ -42,31 +42,38 @@ public abstract class BaseModbusThingHandler extends BaseThingHandler { private List periodicPollers = Collections.synchronizedList(new ArrayList<>()); private List> oneTimePollers = Collections.synchronizedList(new ArrayList<>()); - private volatile boolean initialized; public BaseModbusThingHandler(Thing thing) { super(thing); } /** - * This method must be invoked in the base class' initialize() method before any other initialization is done. - * It will throw an unchecked exception if the {@link ModbusCommunicationInterface} is not accessible (fail-fast). - * This prevents any further initialization of the Thing. The framework will set the ThingStatus to - * HANDLER_INITIALIZING_ERROR and display the exception's message. + * This method is called when the Thing is being initialized, but only if the Modbus Bridge is configured correctly. + * The code that normally goes into `BaseThingHandler.initialize()` like configuration reading and validation goes + * here. */ - @Override - public void initialize() { - getModbus(); + public abstract void modbusInitialize(); + @Override + final public void initialize() { try { + // check if the Bridge is configured correctly (fail-fast) + getModbus(); getBridgeHandler().getSlaveId(); - } catch (EndpointNotInitializedException e) { - throw new IllegalStateException(e); - } - initialized = true; + modbusInitialize(); + } catch (EndpointNotInitializedException | IllegalStateException e) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, + "Modbus initialization failed: " + e.getMessage()); + } } + /** + * Get Slave ID, also called as unit id, represented by the thing + * + * @return slave id represented by this thing handler + * @throws EndpointNotInitializedException in case the initialization is not complete + */ public int getSlaveId() { try { return getBridgeHandler().getSlaveId(); @@ -75,6 +82,11 @@ public int getSlaveId() { } } + /** + * Return true if auto discovery is enabled for this endpoint + * + * @return boolean true if the discovery is enabled + */ public boolean isDiscoveryEnabled() { return getBridgeHandler().isDiscoveryEnabled(); } @@ -96,8 +108,6 @@ public boolean isDiscoveryEnabled() { public PollTask registerRegularPoll(ModbusReadRequestBlueprint request, long pollPeriodMillis, long initialDelayMillis, ModbusReadCallback resultCallback, ModbusFailureCallback failureCallback) { - checkInitialized(); - PollTask task = getModbus().registerRegularPoll(request, pollPeriodMillis, initialDelayMillis, resultCallback, failureCallback); periodicPollers.add(task); @@ -131,8 +141,6 @@ public boolean unregisterRegularPoll(PollTask task) { */ public Future submitOneTimePoll(ModbusReadRequestBlueprint request, ModbusReadCallback resultCallback, ModbusFailureCallback failureCallback) { - checkInitialized(); - Future future = getModbus().submitOneTimePoll(request, resultCallback, failureCallback); oneTimePollers.add(future); oneTimePollers.removeIf(Future::isDone); @@ -152,8 +160,6 @@ public Future submitOneTimePoll(ModbusReadRequestBlueprint request, ModbusRea */ public Future submitOneTimeWrite(ModbusWriteRequestBlueprint request, ModbusWriteCallback resultCallback, ModbusFailureCallback failureCallback) { - checkInitialized(); - Future future = getModbus().submitOneTimeWrite(request, resultCallback, failureCallback); oneTimePollers.add(future); oneTimePollers.removeIf(Future::isDone); @@ -202,13 +208,6 @@ private ModbusEndpointThingHandler getBridgeHandler() { } } - private void checkInitialized() { - if (!initialized) { - throw new IllegalStateException( - getClass().getSimpleName() + " not initialized. Please call super.initialize()."); - } - } - @Override public void dispose() { oneTimePollers.forEach(p -> p.cancel(true));