From 6b693ff73b3e868f9c87cd72cfec9c1935bff993 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 3 Jan 2021 14:35:46 +0100 Subject: [PATCH] [scale] Added ConfigOptionProvider to provide file names ending with '.scale' in Profile configuration (#9640) Signed-off-by: Christoph Weitkamp --- .../internal/ScaleTransformationService.java | 37 ++++++++++++++++--- .../profiles/ScaleTransformationProfile.java | 3 +- .../ScaleTransformationProfileFactory.java | 12 +++--- .../resources/OH-INF/config/scaleProfile.xml | 3 +- .../src/main/resources/readme.txt | 1 - 5 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 bundles/org.openhab.transform.scale/src/main/resources/readme.txt diff --git a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java index 4907f66308..367096dc85 100644 --- a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java +++ b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java @@ -15,17 +15,25 @@ package org.openhab.transform.scale.internal; import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; +import java.net.URI; +import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.config.core.ConfigOptionProvider; +import org.openhab.core.config.core.ParameterOption; import org.openhab.core.library.types.QuantityType; import org.openhab.core.transform.AbstractFileTransformationService; import org.openhab.core.transform.TransformationException; @@ -41,11 +49,17 @@ import org.slf4j.LoggerFactory; * @author Gaël L'hopital * @author Markus Rathgeb - drop usage of Guava */ -@Component(service = TransformationService.class, property = { "openhab.transform=SCALE" }) -public class ScaleTransformationService extends AbstractFileTransformationService> { +@Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = { + "openhab.transform=SCALE" }) +public class ScaleTransformationService extends AbstractFileTransformationService> + implements ConfigOptionProvider { private final Logger logger = LoggerFactory.getLogger(ScaleTransformationService.class); + private static final String PROFILE_CONFIG_URI = "profile:transform:SCALE"; + private static final String CONFIG_PARAM_FUNCTION = "function"; + private static final String[] FILE_NAME_EXTENSIONS = { "scale" }; + /** RegEx to extract a scale definition */ private static final Pattern LIMITS_PATTERN = Pattern.compile("(\\[|\\])(.*)\\.\\.(.*)(\\[|\\])"); @@ -95,13 +109,13 @@ public class ScaleTransformationService extends AbstractFileTransformationServic * * @param properties the list of properties defining all the available ranges * @param source the input to transform - * + * @return the transformed result or null if the transformation couldn't be completed for any reason. */ @Override - protected String internalTransform(Map data, String source) throws TransformationException { + protected @Nullable String internalTransform(Map data, String source) + throws TransformationException { try { final BigDecimal value = new BigDecimal(source); - return formatResult(data, source, value); } catch (NumberFormatException e) { // Scale can only be used with numeric inputs, so lets try to see if ever its a valid quantity type @@ -179,4 +193,17 @@ public class ScaleTransformationService extends AbstractFileTransformationServic throw new TransformationException("An error occurred while opening file.", ex); } } + + @Override + public @Nullable Collection<@NonNull ParameterOption> getParameterOptions(URI uri, String param, + @Nullable String context, @Nullable Locale locale) { + if (PROFILE_CONFIG_URI.equals(uri.toString())) { + switch (param) { + case CONFIG_PARAM_FUNCTION: + return getFilenames(FILE_NAME_EXTENSIONS).stream().map(f -> new ParameterOption(f, f)) + .collect(Collectors.toList()); + } + } + return null; + } } diff --git a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java index 5fc43cabc5..925a27f782 100644 --- a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java +++ b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java @@ -30,8 +30,7 @@ import org.slf4j.LoggerFactory; /** * Profile to offer the ScaleTransformationservice on a ItemChannelLink * - * @author Stefan Triller - initial contribution - * + * @author Stefan Triller - Initial contribution */ @NonNullByDefault public class ScaleTransformationProfile implements StateProfile { diff --git a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfileFactory.java b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfileFactory.java index 132bbe27f9..9fd6d4a2f9 100644 --- a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfileFactory.java +++ b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfileFactory.java @@ -12,8 +12,8 @@ */ package org.openhab.transform.scale.internal.profiles; -import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Locale; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -27,14 +27,14 @@ import org.openhab.core.thing.profiles.ProfileTypeBuilder; import org.openhab.core.thing.profiles.ProfileTypeProvider; import org.openhab.core.thing.profiles.ProfileTypeUID; import org.openhab.core.transform.TransformationService; +import org.openhab.transform.scale.internal.ScaleTransformationService; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** - * Profilefactory that creates the transformation profile for the scale transformation service - * - * @author Stefan Triller - initial contribution + * {@link ProfileFactory} that creates the transformation profile for the {@link ScaleTransformationService} * + * @author Stefan Triller - Initial contribution */ @NonNullByDefault @Component(service = { ProfileFactory.class, ProfileTypeProvider.class }) @@ -45,7 +45,7 @@ public class ScaleTransformationProfileFactory implements ProfileFactory, Profil @Override public Collection getProfileTypes(@Nullable Locale locale) { - return Arrays.asList(ProfileTypeBuilder.newState(ScaleTransformationProfile.PROFILE_TYPE_UID, + return List.of(ProfileTypeBuilder.newState(ScaleTransformationProfile.PROFILE_TYPE_UID, ScaleTransformationProfile.PROFILE_TYPE_UID.getId()).build()); } @@ -57,7 +57,7 @@ public class ScaleTransformationProfileFactory implements ProfileFactory, Profil @Override public Collection getSupportedProfileTypeUIDs() { - return Arrays.asList(ScaleTransformationProfile.PROFILE_TYPE_UID); + return List.of(ScaleTransformationProfile.PROFILE_TYPE_UID); } @Reference(target = "(openhab.transform=SCALE)") diff --git a/bundles/org.openhab.transform.scale/src/main/resources/OH-INF/config/scaleProfile.xml b/bundles/org.openhab.transform.scale/src/main/resources/OH-INF/config/scaleProfile.xml index 04e3abb3ae..00b5f16923 100644 --- a/bundles/org.openhab.transform.scale/src/main/resources/OH-INF/config/scaleProfile.xml +++ b/bundles/org.openhab.transform.scale/src/main/resources/OH-INF/config/scaleProfile.xml @@ -8,8 +8,9 @@ Filename containing the scale mappings. + false - + How to format the state on the channel before transforming it, i.e. %s or %.1f °C (default is %s) true diff --git a/bundles/org.openhab.transform.scale/src/main/resources/readme.txt b/bundles/org.openhab.transform.scale/src/main/resources/readme.txt deleted file mode 100644 index a2ee892dc4..0000000000 --- a/bundles/org.openhab.transform.scale/src/main/resources/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Bundle resources go in here! -- 2.47.3