Skip to content

Commit

Permalink
Refactor #2
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
  • Loading branch information
fwolter committed Dec 2, 2020
1 parent 9e07451 commit ce1efe1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
4 changes: 1 addition & 3 deletions bundles/org.openhab.binding.modbus/DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,38 @@
public abstract class BaseModbusThingHandler extends BaseThingHandler {
private List<PollTask> periodicPollers = Collections.synchronizedList(new ArrayList<>());
private List<Future<?>> 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();
Expand All @@ -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();
}
Expand All @@ -96,8 +108,6 @@ public boolean isDiscoveryEnabled() {
public PollTask registerRegularPoll(ModbusReadRequestBlueprint request, long pollPeriodMillis,
long initialDelayMillis, ModbusReadCallback resultCallback,
ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback) {
checkInitialized();

PollTask task = getModbus().registerRegularPoll(request, pollPeriodMillis, initialDelayMillis, resultCallback,
failureCallback);
periodicPollers.add(task);
Expand Down Expand Up @@ -131,8 +141,6 @@ public boolean unregisterRegularPoll(PollTask task) {
*/
public Future<?> submitOneTimePoll(ModbusReadRequestBlueprint request, ModbusReadCallback resultCallback,
ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback) {
checkInitialized();

Future<?> future = getModbus().submitOneTimePoll(request, resultCallback, failureCallback);
oneTimePollers.add(future);
oneTimePollers.removeIf(Future::isDone);
Expand All @@ -152,8 +160,6 @@ public Future<?> submitOneTimePoll(ModbusReadRequestBlueprint request, ModbusRea
*/
public Future<?> submitOneTimeWrite(ModbusWriteRequestBlueprint request, ModbusWriteCallback resultCallback,
ModbusFailureCallback<ModbusWriteRequestBlueprint> failureCallback) {
checkInitialized();

Future<?> future = getModbus().submitOneTimeWrite(request, resultCallback, failureCallback);
oneTimePollers.add(future);
oneTimePollers.removeIf(Future::isDone);
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit ce1efe1

Please sign in to comment.