[systeminfo] Use AbstractStorageBasedTypeProvider (#14501)

* [systeminfo] Use AbstractDynamicTypeProvider

---------

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K
2023-05-12 13:59:01 +02:00
committed by GitHub
parent 227606b0a1
commit 16df575b38
3 changed files with 37 additions and 61 deletions

View File

@@ -15,23 +15,22 @@ package org.openhab.binding.systeminfo.internal;
import static org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants.*;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.storage.StorageService;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.AbstractStorageBasedTypeProvider;
import org.openhab.core.thing.binding.ThingTypeProvider;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.type.ChannelGroupDefinition;
@@ -51,7 +50,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Extended channels can be auto discovered and added to newly created groups in the {@link SystemInfoHandler}. The
* Extended channels can be auto discovered and added to newly created groups in the {@link SysteminfoHandler}. The
* thing needs to be updated to add the groups. The `SysteminfoThingTypeProvider` OSGi service gives access to the
* `ThingTypeRegistry` and serves the updated `ThingType`.
*
@@ -60,41 +59,25 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
@Component(service = { SysteminfoThingTypeProvider.class, ThingTypeProvider.class })
public class SysteminfoThingTypeProvider implements ThingTypeProvider {
public class SysteminfoThingTypeProvider extends AbstractStorageBasedTypeProvider {
private final Logger logger = LoggerFactory.getLogger(SysteminfoThingTypeProvider.class);
private final ThingTypeRegistry thingTypeRegistry;
private final ChannelGroupTypeRegistry channelGroupTypeRegistry;
private final ChannelTypeRegistry channelTypeRegistry;
private final Map<ThingTypeUID, ThingType> thingTypes = new HashMap<>();
private final Map<ThingUID, Map<String, Configuration>> thingChannelsConfig = new HashMap<>();
@Activate
public SysteminfoThingTypeProvider(@Reference ThingTypeRegistry thingTypeRegistry,
@Reference ChannelGroupTypeRegistry channelGroupTypeRegistry,
@Reference ChannelTypeRegistry channelTypeRegistry) {
super();
@Reference ChannelTypeRegistry channelTypeRegistry, @Reference StorageService storageService) {
super(storageService);
this.thingTypeRegistry = thingTypeRegistry;
this.channelGroupTypeRegistry = channelGroupTypeRegistry;
this.channelTypeRegistry = channelTypeRegistry;
}
@Override
public Collection<ThingType> getThingTypes(@Nullable Locale locale) {
return thingTypes.values();
}
@Override
public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
return thingTypes.get(thingTypeUID);
}
private void setThingType(ThingTypeUID uid, ThingType type) {
thingTypes.put(uid, type);
}
/**
* Create thing type with the provided typeUID and add it to the thing type registry.
*
@@ -114,17 +97,20 @@ public class SysteminfoThingTypeProvider implements ThingTypeProvider {
* @return false if `typeUID` or its base type UID `systeminfo:computer` cannot be found in the thingTypeRegistry
*/
public boolean updateThingType(ThingTypeUID typeUID, List<ChannelGroupDefinition> groupDefs) {
ThingTypeUID baseTypeUID = THING_TYPE_COMPUTER;
if (thingTypes.containsKey(typeUID)) {
baseTypeUID = typeUID;
ThingType baseType = thingTypeRegistry.getThingType(typeUID);
if (baseType == null) {
baseType = thingTypeRegistry.getThingType(THING_TYPE_COMPUTER);
if (baseType == null) {
logger.warn("Could not find base thing type in registry.");
return false;
}
}
ThingType baseType = thingTypeRegistry.getThingType(baseTypeUID);
ThingTypeBuilder builder = createThingTypeBuilder(typeUID, baseTypeUID);
if (baseType != null && builder != null) {
ThingTypeBuilder builder = createThingTypeBuilder(typeUID, baseType.getUID());
if (builder != null) {
logger.trace("Adding channel group definitions to thing type");
ThingType type = builder.withChannelGroupDefinitions(groupDefs).build();
setThingType(typeUID, type);
putThingType(type);
return true;
} else {
logger.debug("Error adding channel groups");
@@ -253,7 +239,7 @@ public class SysteminfoThingTypeProvider implements ThingTypeProvider {
/**
* Store the channel configurations for a thing, to be able to restore them later when the thing handler for the
* same thing gets recreated with a new thing type. This is necessary because the
* {@link BaseThingHandler#changeThingType()} method reverts channel configurations to their defaults.
* {@link BaseThingHandler##changeThingType()} method reverts channel configurations to their defaults.
*
* @param thing
*/

View File

@@ -159,6 +159,12 @@ public class SysteminfoHandler extends BaseThingHandler {
}
}
@Override
public void handleRemoval() {
thingTypeProvider.removeThingType(thing.getThingTypeUID());
super.handleRemoval();
}
private boolean instantiateSysteminfoLibrary() {
try {
systeminfo.initializeSysteminfo();