[harmonyhub] Use AbstractStorageBasedTypeProvider (#14507)

* [harmonyhub] Use AbstractDynamicTypeProvider

---------

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-05-13 18:07:48 +02:00 committed by GitHub
parent 717addee9a
commit 36024a8c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 87 deletions

View File

@ -0,0 +1,46 @@
/**
* 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.binding.harmonyhub.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.storage.StorageService;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.AbstractStorageBasedTypeProvider;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* The {@link HarmonyHubDynamicTypeProvider} is an instance of a {@link AbstractStorageBasedTypeProvider} for the
* HarmonyHub
* binding
*
* @author Jan N. Klug - Initial contribution
*/
@Component(service = { HarmonyHubDynamicTypeProvider.class, ChannelTypeProvider.class })
@NonNullByDefault
public class HarmonyHubDynamicTypeProvider extends AbstractStorageBasedTypeProvider {
@Activate
public HarmonyHubDynamicTypeProvider(@Reference StorageService storageService) {
super(storageService);
}
public void removeChannelTypesForThing(ThingUID uid) {
String thingUid = uid.getAsString() + ":";
getChannelTypes(null).stream().map(ChannelType::getUID).filter(c -> c.getAsString().startsWith(thingUid))
.forEach(this::removeChannelType);
}
}

View File

@ -12,15 +12,10 @@
*/
package org.openhab.binding.harmonyhub.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -39,12 +34,6 @@ import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.type.ChannelGroupType;
import org.openhab.core.thing.type.ChannelGroupTypeProvider;
import org.openhab.core.thing.type.ChannelGroupTypeUID;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
@ -58,10 +47,8 @@ import org.osgi.service.component.annotations.Reference;
* @author Wouter Born - Add null annotations
*/
@NonNullByDefault
@Component(service = { ThingHandlerFactory.class, ChannelTypeProvider.class,
ChannelGroupTypeProvider.class }, configurationPid = "binding.harmonyhub")
public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
implements ChannelTypeProvider, ChannelGroupTypeProvider {
@Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.harmonyhub")
public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
.concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
@ -71,12 +58,13 @@ public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final HttpClient httpClient;
private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
private final List<ChannelGroupType> channelGroupTypes = new CopyOnWriteArrayList<>();
private final HarmonyHubDynamicTypeProvider dynamicTypeProvider;
@Activate
public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory,
@Reference HarmonyHubDynamicTypeProvider dynamicTypeProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.dynamicTypeProvider = dynamicTypeProvider;
}
@Override
@ -89,13 +77,14 @@ public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_HUB_THING_TYPE)) {
HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, this);
HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, dynamicTypeProvider,
httpClient);
registerHarmonyDeviceDiscoveryService(harmonyHubHandler);
return harmonyHubHandler;
}
if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_DEVICE_THING_TYPE)) {
return new HarmonyDeviceHandler(thing, this);
return new HarmonyDeviceHandler(thing, dynamicTypeProvider);
}
return null;
@ -121,57 +110,4 @@ public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(),
bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
}
@Override
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
return channelTypes;
}
@Override
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
for (ChannelType channelType : channelTypes) {
if (channelType.getUID().equals(channelTypeUID)) {
return channelType;
}
}
return null;
}
@Override
public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
@Nullable Locale locale) {
for (ChannelGroupType channelGroupType : channelGroupTypes) {
if (channelGroupType.getUID().equals(channelGroupTypeUID)) {
return channelGroupType;
}
}
return null;
}
@Override
public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
return channelGroupTypes;
}
public HttpClient getHttpClient() {
return this.httpClient;
}
public void addChannelType(ChannelType type) {
channelTypes.add(type);
}
public void removeChannelType(ChannelType type) {
channelTypes.remove(type);
}
public void removeChannelTypesForThing(ThingUID uid) {
List<ChannelType> removes = new ArrayList<>();
for (ChannelType c : channelTypes) {
if (c.getUID().getAsString().startsWith(uid.getAsString())) {
removes.add(c);
}
}
channelTypes.removeAll(removes);
}
}

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.harmonyhub.internal.HarmonyHubHandlerFactory;
import org.openhab.binding.harmonyhub.internal.HarmonyHubDynamicTypeProvider;
import org.openhab.binding.harmonyhub.internal.config.HarmonyDeviceConfig;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.Bridge;
@ -67,13 +67,13 @@ public class HarmonyDeviceHandler extends BaseThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(HARMONY_DEVICE_THING_TYPE);
private HarmonyHubHandlerFactory factory;
private final HarmonyHubDynamicTypeProvider typeProvider;
private @NonNullByDefault({}) HarmonyDeviceConfig config;
public HarmonyDeviceHandler(Thing thing, HarmonyHubHandlerFactory factory) {
public HarmonyDeviceHandler(Thing thing, HarmonyHubDynamicTypeProvider typeProvider) {
super(thing);
this.factory = factory;
this.typeProvider = typeProvider;
}
protected @Nullable HarmonyHubHandler getHarmonyHubHandler() {
@ -142,8 +142,9 @@ public class HarmonyDeviceHandler extends BaseThingHandler {
}
@Override
public void dispose() {
factory.removeChannelTypesForThing(getThing().getUID());
public void handleRemoval() {
typeProvider.removeChannelTypesForThing(getThing().getUID());
super.handleRemoval();
}
/**
@ -190,7 +191,7 @@ public class HarmonyDeviceHandler extends BaseThingHandler {
.withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withOptions(states).build())
.build();
factory.addChannelType(channelType);
typeProvider.putChannelType(channelType);
Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_BUTTON_PRESS), "String")
.withType(channelTypeUID).build();

View File

@ -28,7 +28,8 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.harmonyhub.internal.HarmonyHubHandlerFactory;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.harmonyhub.internal.HarmonyHubDynamicTypeProvider;
import org.openhab.binding.harmonyhub.internal.config.HarmonyHubConfig;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.DecimalType;
@ -85,7 +86,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
// Websocket will timeout after 60 seconds, pick a sensible max under this,
private static final int HEARTBEAT_INTERVAL_MAX = 50;
private Set<HubStatusListener> listeners = ConcurrentHashMap.newKeySet();
private final HarmonyHubHandlerFactory factory;
private final HarmonyHubDynamicTypeProvider typeProvider;
private @NonNullByDefault({}) HarmonyHubConfig config;
private final HarmonyClient client;
private @Nullable ScheduledFuture<?> retryJob;
@ -94,10 +95,10 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
private int heartBeatInterval;
public HarmonyHubHandler(Bridge bridge, HarmonyHubHandlerFactory factory) {
public HarmonyHubHandler(Bridge bridge, HarmonyHubDynamicTypeProvider typeProvider, HttpClient httpClient) {
super(bridge);
this.factory = factory;
client = new HarmonyClient(factory.getHttpClient());
this.typeProvider = typeProvider;
client = new HarmonyClient(httpClient);
client.addListener(this);
}
@ -193,7 +194,12 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
listeners.clear();
cancelRetry();
disconnectFromHub();
factory.removeChannelTypesForThing(getThing().getUID());
}
@Override
public void handleRemoval() {
typeProvider.removeChannelTypesForThing(getThing().getUID());
super.handleRemoval();
}
@Override
@ -400,7 +406,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien
.withReadOnly(false).withOptions(states).build())
.build();
factory.addChannelType(channelType);
typeProvider.putChannelType(channelType);
Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_CURRENT_ACTIVITY), "String")
.withType(channelTypeUID).build();