Skip to content

Commit

Permalink
further work
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed Dec 28, 2023
1 parent cd6f943 commit 7a3dd91
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.config.discovery;

import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;

/**
* The {@link AbstractThingHandlerDiscoveryService} extends the {@link AbstractDiscoveryService} for thing-based
* discovery services.
*
* It handles the injection of the {@link ThingHandler}
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public abstract class AbstractThingHandlerDiscoveryService<T extends ThingHandler> extends AbstractDiscoveryService
implements ThingHandlerService {

private final Class<T> thingClazz;
protected @Nullable T thingHandler;

public AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Set<ThingTypeUID> supportedThingTypes,
int timeout, boolean backgroundDiscoveryEnabledByDefault) throws IllegalArgumentException {
super(supportedThingTypes, timeout, backgroundDiscoveryEnabledByDefault);
this.thingClazz = thingClazz;
}

public AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Set<ThingTypeUID> supportedThingTypes,
int timeout) throws IllegalArgumentException {
super(supportedThingTypes, timeout);
this.thingClazz = thingClazz;
}

public AbstractThingHandlerDiscoveryService(Class<T> thingClazz, int timeout) throws IllegalArgumentException {
super(timeout);
this.thingClazz = thingClazz;
}

@Override
protected abstract void startScan();

@Override
@SuppressWarnings("unchecked")
public void setThingHandler(ThingHandler handler) {
if (thingClazz.isAssignableFrom(handler.getClass())) {
this.thingHandler = (T) handler;
} else {
throw new IllegalArgumentException(
"Expected class is " + thingClazz + " but the parameter has class " + handler.getClass());
}
}

@Override
public @Nullable ThingHandler getThingHandler() {
return thingHandler;
}

@Override
public void activate(@Nullable Map<String, Object> config) {
super.activate(config);
}

@Override
public void deactivate() {
super.deactivate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private <T extends ThingHandlerService> void registerThingHandlerService(ThingUI
}

String[] serviceNames = getAllInterfaces(c).stream()//
.map(clazz -> clazz.getCanonicalName())
.map(Class::getCanonicalName)
// we only add specific ThingHandlerServices, i.e. those that derive from the
// ThingHandlerService
// interface, NOT the ThingHandlerService itself. We do this to register them as specific OSGi
Expand Down Expand Up @@ -370,7 +370,7 @@ public void removeThing(ThingUID thingUID) {

private class RegisteredThingHandlerService<T extends ThingHandlerService> {

private T serviceInstance;
private final T serviceInstance;

private @Nullable ServiceObjects<T> serviceObjects;

Expand All @@ -397,11 +397,12 @@ public void initializeService(ThingHandler handler, String[] serviceNames) {
}

public void disposeService() {
this.serviceInstance.dispose();

ServiceRegistration<?> serviceReg = this.serviceRegistration;
if (serviceReg != null) {
serviceReg.unregister();
}
this.serviceInstance.dispose();

ServiceObjects<T> serviceObjs = this.serviceObjects;
if (serviceObjs != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,42 @@ public interface ThingHandlerService {
ThingHandler getThingHandler();

/**
* Method that will be called if this service will be activated
* This method is used by the framework during activation of the OSGi component.
* It is called BEFORE the thing handler is set.
*
* @deprecated please override initialize instead
* See {@link #initialize()}, {@link #deactivate()}
*/
@Deprecated
default void activate() {
}

/**
* Method that will be called if this service will be deactivated
* This method is used by the framework during de-activation of the OSGi component.
* It is NOT guaranteed that the thing handler is still valid.
*
* @deprecated please override dispose instead
* See {@link #dispose()}, {@link #activate()}
*/
@Deprecated
default void deactivate() {
}

/**
* Method that will be called if this service will be initialized
* This method is used by the framework during activation of the service.
* It is called AFTER the component is fully activated and thing handler has been set.
*
* Implementations should override this method to add additional initialization code.
*
* See {@link #activate(), #{@link #dispose()}
*/
default void initialize() {
activate();
}

/**
* Method that will be called if this service will be disposed
* This method is used by the framework during de-activation of the service.
* It is called while the component is still activated.
*
* Code depending on an activated service should go here.
*
* See {@link #deactivate()}, {@link #initialize()}
*/
default void dispose() {
deactivate();
}
}

0 comments on commit 7a3dd91

Please sign in to comment.