[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:
parent
f652ae678f
commit
6b693ff73b
@ -15,17 +15,25 @@ package org.openhab.transform.scale.internal;
|
|||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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.library.types.QuantityType;
|
||||||
import org.openhab.core.transform.AbstractFileTransformationService;
|
import org.openhab.core.transform.AbstractFileTransformationService;
|
||||||
import org.openhab.core.transform.TransformationException;
|
import org.openhab.core.transform.TransformationException;
|
||||||
@ -41,11 +49,17 @@ import org.slf4j.LoggerFactory;
|
|||||||
* @author Gaël L'hopital
|
* @author Gaël L'hopital
|
||||||
* @author Markus Rathgeb - drop usage of Guava
|
* @author Markus Rathgeb - drop usage of Guava
|
||||||
*/
|
*/
|
||||||
@Component(service = TransformationService.class, property = { "openhab.transform=SCALE" })
|
@Component(service = { TransformationService.class, ConfigOptionProvider.class }, property = {
|
||||||
public class ScaleTransformationService extends AbstractFileTransformationService<Map<Range, String>> {
|
"openhab.transform=SCALE" })
|
||||||
|
public class ScaleTransformationService extends AbstractFileTransformationService<Map<Range, String>>
|
||||||
|
implements ConfigOptionProvider {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(ScaleTransformationService.class);
|
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 */
|
/** RegEx to extract a scale definition */
|
||||||
private static final Pattern LIMITS_PATTERN = Pattern.compile("(\\[|\\])(.*)\\.\\.(.*)(\\[|\\])");
|
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 properties the list of properties defining all the available ranges
|
||||||
* @param source the input to transform
|
* @param source the input to transform
|
||||||
*
|
* @return the transformed result or null if the transformation couldn't be completed for any reason.
|
||||||
*/
|
*/
|
||||||
@Override
|
@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 {
|
try {
|
||||||
final BigDecimal value = new BigDecimal(source);
|
final BigDecimal value = new BigDecimal(source);
|
||||||
|
|
||||||
return formatResult(data, source, value);
|
return formatResult(data, source, value);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// Scale can only be used with numeric inputs, so lets try to see if ever its a valid quantity type
|
// 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);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,8 +30,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
/**
|
/**
|
||||||
* Profile to offer the ScaleTransformationservice on a ItemChannelLink
|
* Profile to offer the ScaleTransformationservice on a ItemChannelLink
|
||||||
*
|
*
|
||||||
* @author Stefan Triller - initial contribution
|
* @author Stefan Triller - Initial contribution
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class ScaleTransformationProfile implements StateProfile {
|
public class ScaleTransformationProfile implements StateProfile {
|
||||||
|
|||||||
@ -12,8 +12,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.transform.scale.internal.profiles;
|
package org.openhab.transform.scale.internal.profiles;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
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.ProfileTypeProvider;
|
||||||
import org.openhab.core.thing.profiles.ProfileTypeUID;
|
import org.openhab.core.thing.profiles.ProfileTypeUID;
|
||||||
import org.openhab.core.transform.TransformationService;
|
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.Component;
|
||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Profilefactory that creates the transformation profile for the scale transformation service
|
* {@link ProfileFactory} that creates the transformation profile for the {@link ScaleTransformationService}
|
||||||
*
|
|
||||||
* @author Stefan Triller - initial contribution
|
|
||||||
*
|
*
|
||||||
|
* @author Stefan Triller - Initial contribution
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
|
@Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
|
||||||
@ -45,7 +45,7 @@ public class ScaleTransformationProfileFactory implements ProfileFactory, Profil
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
|
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());
|
ScaleTransformationProfile.PROFILE_TYPE_UID.getId()).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ public class ScaleTransformationProfileFactory implements ProfileFactory, Profil
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
|
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
|
||||||
return Arrays.asList(ScaleTransformationProfile.PROFILE_TYPE_UID);
|
return List.of(ScaleTransformationProfile.PROFILE_TYPE_UID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Reference(target = "(openhab.transform=SCALE)")
|
@Reference(target = "(openhab.transform=SCALE)")
|
||||||
|
|||||||
@ -8,8 +8,9 @@
|
|||||||
<parameter name="function" type="text" required="true">
|
<parameter name="function" type="text" required="true">
|
||||||
<label>Filename</label>
|
<label>Filename</label>
|
||||||
<description>Filename containing the scale mappings.</description>
|
<description>Filename containing the scale mappings.</description>
|
||||||
|
<limitToOptions>false</limitToOptions>
|
||||||
</parameter>
|
</parameter>
|
||||||
<parameter name="sourceFormat" type="text" required="false">
|
<parameter name="sourceFormat" type="text">
|
||||||
<label>State Formatter</label>
|
<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>
|
<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>
|
<advanced>true</advanced>
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
Bundle resources go in here!
|
|
||||||
Loading…
x
Reference in New Issue
Block a user