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

index 95e101a417d57406647f9dc185a02ddac95310f5..6e6c466e1ecf096807d873055111ed51ce0c397b 100644 (file)
@@ -14,8 +14,16 @@ package org.openhab.transform.map.internal;
 
 import java.io.FileReader;
 import java.io.IOException;
+import java.net.URI;
+import java.util.Collection;
+import java.util.Locale;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.core.config.core.ConfigOptionProvider;
+import org.openhab.core.config.core.ParameterOption;
 import org.openhab.core.transform.AbstractFileTransformationService;
 import org.openhab.core.transform.TransformationException;
 import org.openhab.core.transform.TransformationService;
@@ -30,11 +38,18 @@ import org.slf4j.LoggerFactory;
  * @author Kai Kreuzer - Initial contribution and API
  * @author Gaël L'hopital - Make it localizable
  */
-@Component(service = TransformationService.class, property = { "openhab.transform=MAP" })
-public class MapTransformationService extends AbstractFileTransformationService<Properties> {
+@NonNullByDefault
+@Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = {
+        "openhab.transform=MAP" })
+public class MapTransformationService extends AbstractFileTransformationService<Properties>
+        implements ConfigOptionProvider {
 
     private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class);
 
+    private static final String PROFILE_CONFIG_URI = "profile:transform:MAP";
+    private static final String CONFIG_PARAM_FUNCTION = "function";
+    private static final String[] FILE_NAME_EXTENSIONS = { "map" };
+
     /**
      * <p>
      * Transforms the input <code>source</code> by mapping it to another string. It expects the mappings to be read from
@@ -43,9 +58,10 @@ public class MapTransformationService extends AbstractFileTransformationService<
      *
      * @param properties the list of properties which contains the key value pairs for the mapping.
      * @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(Properties properties, String source) throws TransformationException {
+    protected @Nullable String internalTransform(Properties properties, String source) throws TransformationException {
         String target = properties.getProperty(source);
 
         if (target == null) {
@@ -69,4 +85,17 @@ public class MapTransformationService extends AbstractFileTransformationService<
             throw new TransformationException("An error occurred while opening file.", e);
         }
     }
+
+    @Override
+    public @Nullable Collection<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 0792737c696756f6e702768aeddff142092de1ad..0ab69b4bccc7ceee076626a2fd4447e8d14c05ec 100644 (file)
@@ -30,8 +30,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Profile to offer the MapTransformationservice on a ItemChannelLink
  *
- * @author Stefan Triller - initial contribution
- *
+ * @author Stefan Triller - Initial contribution
  */
 @NonNullByDefault
 public class MapTransformationProfile implements StateProfile {
index 2a31e3d704722fc7f928d19d081d8fec339f13ba..6ee4394e38f22ef29b8570cdd8fab856f87f2212 100644 (file)
@@ -12,8 +12,8 @@
  */
 package org.openhab.transform.map.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.map.internal.MapTransformationService;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 
 /**
- * Profilefactory that creates the transformation profile for the map transformation service
- *
- * @author Stefan Triller - initial contribution
+ * {@link ProfileFactory} that creates the transformation profile for the {@link MapTransformationService}.
  *
+ * @author Stefan Triller - Initial contribution
  */
 @NonNullByDefault
 @Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
@@ -45,7 +45,7 @@ public class MapTransformationProfileFactory implements ProfileFactory, ProfileT
 
     @Override
     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
-        return Arrays.asList(ProfileTypeBuilder
+        return List.of(ProfileTypeBuilder
                 .newState(MapTransformationProfile.PROFILE_TYPE_UID, MapTransformationProfile.PROFILE_TYPE_UID.getId())
                 .build());
     }
@@ -58,7 +58,7 @@ public class MapTransformationProfileFactory implements ProfileFactory, ProfileT
 
     @Override
     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
-        return Arrays.asList(MapTransformationProfile.PROFILE_TYPE_UID);
+        return List.of(MapTransformationProfile.PROFILE_TYPE_UID);
     }
 
     @Reference(target = "(openhab.transform=MAP)")
index 577ecdc053bed6f1ec7fe0fe617f735029e137dc..b07c41a586baf020a553a7e8609d182b2cfce24f 100644 (file)
@@ -8,8 +8,9 @@
                <parameter name="function" type="text" required="true">
                        <label>Filename</label>
                        <description>Filename containing the mapping information.</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.map/src/main/resources/readme.txt b/bundles/org.openhab.transform.map/src/main/resources/readme.txt
deleted file mode 100644 (file)
index a2ee892..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Bundle resources go in here!