Skip to content

Commit

Permalink
[harmonyhub] Use AbstractStorageBasedTypeProvider (#14507)
Browse files Browse the repository at this point in the history
* [harmonyhub] Use AbstractDynamicTypeProvider

---------

Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored May 13, 2023
1 parent 717adde commit 36024a8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
.concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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<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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 36024a8

Please sign in to comment.