--- /dev/null
+/**
+ * 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);
+ }
+}
*/
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;
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;
* @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(),
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
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;
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);
- }
}
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;
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() {
}
@Override
- public void dispose() {
- factory.removeChannelTypesForThing(getThing().getUID());
+ public void handleRemoval() {
+ typeProvider.removeChannelTypesForThing(getThing().getUID());
+ super.handleRemoval();
}
/**
.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();
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;
// 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;
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);
}
listeners.clear();
cancelRetry();
disconnectFromHub();
- factory.removeChannelTypesForThing(getThing().getUID());
+ }
+
+ @Override
+ public void handleRemoval() {
+ typeProvider.removeChannelTypesForThing(getThing().getUID());
+ super.handleRemoval();
}
@Override
.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();