]> git.basschouten.com Git - openhab-addons.git/commitdiff
[scale] Added ConfigOptionProvider to provide file names ending with '.scale' in...
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 3 Jan 2021 13:35:46 +0000 (14:35 +0100)
committerGitHub <noreply@github.com>
Sun, 3 Jan 2021 13:35:46 +0000 (14:35 +0100)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java
bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java
bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfileFactory.java
bundles/org.openhab.transform.scale/src/main/resources/OH-INF/config/scaleProfile.xml
bundles/org.openhab.transform.scale/src/main/resources/readme.txt [deleted file]

index 4907f66308273bc9541999555bcfbed5a31acc3c..367096dc8559d1ccf375651cc47cdf71f9ceaf95 100644 (file)
@@ -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<Map<Range, String>> {
+@Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = {
+        "openhab.transform=SCALE" })
+public class ScaleTransformationService extends AbstractFileTransformationService<Map<Range, String>>
+        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<Range, String> data, String source) throws TransformationException {
+    protected @Nullable String internalTransform(Map<Range, String> 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;
+    }
 }
index 5fc43cabc5cc54e6589905fef335541df28ae961..925a27f782ff220dfba65a89cc43b2cde15e2e97 100644 (file)
@@ -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 {
index 132bbe27f9c828554bbcc1896be4350752ae874b..9fd6d4a2f9364f7e8ac5108872e989a62dae45d8 100644 (file)
@@ -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<ProfileType> 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<ProfileTypeUID> getSupportedProfileTypeUIDs() {
-        return Arrays.asList(ScaleTransformationProfile.PROFILE_TYPE_UID);
+        return List.of(ScaleTransformationProfile.PROFILE_TYPE_UID);
     }
 
     @Reference(target = "(openhab.transform=SCALE)")
index 04e3abb3ae9cc2917ce0992f404bd29fd52a6abe..00b5f16923517c36120ff87565959c0484352d44 100644 (file)
@@ -8,8 +8,9 @@
                <parameter name="function" type="text" required="true">
                        <label>Filename</label>
                        <description>Filename containing the scale mappings.</description>
+                       <limitToOptions>false</limitToOptions>
                </parameter>
-               <parameter name="sourceFormat" type="text" required="false">
+               <parameter name="sourceFormat" type="text">
                        <label>State Formatter</label>
                        <description>How to format the state on the channel before transforming it, i.e. %s or %.1f °C (default is %s)</description>
                        <advanced>true</advanced>
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 (file)
index a2ee892..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Bundle resources go in here!