[scale] Added ConfigOptionProvider to provide file names ending with '.scale' in Profile configuration (#9640)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-01-03 14:35:46 +01:00 committed by GitHub
parent f652ae678f
commit 6b693ff73b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 15 deletions

View 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;
}
}

View 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 {

View 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)")

View 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>

View File

@ -1 +0,0 @@
Bundle resources go in here!