From dc39874cc5a66e32bcabcdd969f7d9786a368f8e Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sun, 26 Feb 2023 12:01:26 +0100 Subject: [PATCH] [harmonyhub] Use AbstractDynamicTypeProvider Signed-off-by: Jan N. Klug --- .../HarmonyHubDynamicTypeProvider.java | 45 ++++++++++ .../internal/HarmonyHubHandlerFactory.java | 82 ++----------------- .../handler/HarmonyDeviceHandler.java | 15 ++-- .../internal/handler/HarmonyHubHandler.java | 20 +++-- 4 files changed, 75 insertions(+), 87 deletions(-) create mode 100644 bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubDynamicTypeProvider.java diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubDynamicTypeProvider.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubDynamicTypeProvider.java new file mode 100644 index 0000000000000..749a1b6095ac7 --- /dev/null +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubDynamicTypeProvider.java @@ -0,0 +1,45 @@ +/** + * 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.AbstractDynamicTypeProvider; +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 AbstractDynamicTypeProvider} for the HarmonyHub + * binding + * + * @author Jan N. Klug - Initial contribution + */ +@Component(service = { HarmonyHubDynamicTypeProvider.class, ChannelTypeProvider.class }) +@NonNullByDefault +public class HarmonyHubDynamicTypeProvider extends AbstractDynamicTypeProvider { + + @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); + } +} diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubHandlerFactory.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubHandlerFactory.java index 3ec1275996e91..20ccb0332cb78 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubHandlerFactory.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubHandlerFactory.java @@ -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.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 @@ * @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 SUPPORTED_THING_TYPES_UIDS = Stream .concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(), @@ -71,12 +58,13 @@ public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory private final Map> discoveryServiceRegs = new HashMap<>(); private final HttpClient httpClient; - private final List channelTypes = new CopyOnWriteArrayList<>(); - private final List 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 boolean supportsThingType(ThingTypeUID thingTypeUID) { 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 @@ private synchronized void registerHarmonyDeviceDiscoveryService(HarmonyHubHandle this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(), bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>())); } - - @Override - public Collection 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 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 removes = new ArrayList<>(); - for (ChannelType c : channelTypes) { - if (c.getUID().getAsString().startsWith(uid.getAsString())) { - removes.add(c); - } - } - channelTypes.removeAll(removes); - } } diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyDeviceHandler.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyDeviceHandler.java index 3ed08a25092cb..a13ce2afbc1ca 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyDeviceHandler.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyDeviceHandler.java @@ -22,7 +22,7 @@ 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 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 void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) { } @Override - public void dispose() { - factory.removeChannelTypesForThing(getThing().getUID()); + public void handleRemoval() { + typeProvider.removeChannelTypesForThing(getThing().getUID()); + super.handleRemoval(); } /** @@ -190,7 +191,7 @@ private void updateButtonPressChannel(@Nullable HarmonyConfig harmonyConfig) { .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(); diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java index adebda7958cda..e015f3854f7f5 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java @@ -28,7 +28,8 @@ 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 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 void dispose() { listeners.clear(); cancelRetry(); disconnectFromHub(); - factory.removeChannelTypesForThing(getThing().getUID()); + } + + @Override + public void handleRemoval() { + typeProvider.removeChannelTypesForThing(getThing().getUID()); + super.handleRemoval(); } @Override @@ -400,7 +406,7 @@ private void updateCurrentActivityChannel(@Nullable HarmonyConfig config) { .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();