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;
* @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("(\\[|\\])(.*)\\.\\.(.*)(\\[|\\])");
*
* @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
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;
+ }
}
*/
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;
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 })
@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());
}
@Override
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
- return Arrays.asList(ScaleTransformationProfile.PROFILE_TYPE_UID);
+ return List.of(ScaleTransformationProfile.PROFILE_TYPE_UID);
}
@Reference(target = "(openhab.transform=SCALE)")