Adjust transformations to core changes (#13126)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K
2022-07-14 22:47:48 +02:00
committed by GitHub
parent 8e16c411f0
commit 9f8c1772d2
4 changed files with 81 additions and 85 deletions

View File

@@ -37,9 +37,9 @@ import org.openhab.core.common.registry.RegistryChangeListener;
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.TransformationConfiguration;
import org.openhab.core.transform.TransformationConfigurationRegistry;
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
import org.openhab.core.transform.TransformationService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
@@ -59,7 +59,7 @@ import org.slf4j.LoggerFactory;
"openhab.transform=SCALE" })
@NonNullByDefault
public class ScaleTransformationService
implements TransformationService, ConfigOptionProvider, RegistryChangeListener<TransformationConfiguration> {
implements TransformationService, ConfigOptionProvider, RegistryChangeListener<Transformation> {
private final Logger logger = LoggerFactory.getLogger(ScaleTransformationService.class);
@@ -77,34 +77,33 @@ public class ScaleTransformationService
/** Inaccessible range used to store presentation format ]0..0[ */
private static final Range FORMAT_RANGE = Range.range(BigDecimal.ZERO, false, BigDecimal.ZERO, false);
private final TransformationConfigurationRegistry transformationConfigurationRegistry;
private final TransformationRegistry transformationRegistry;
private final Map<String, Map<@Nullable Range, String>> cachedTransformations = new ConcurrentHashMap<>();
@Activate
public ScaleTransformationService(
@Reference TransformationConfigurationRegistry transformationConfigurationRegistry) {
this.transformationConfigurationRegistry = transformationConfigurationRegistry;
transformationConfigurationRegistry.addRegistryChangeListener(this);
public ScaleTransformationService(@Reference TransformationRegistry transformationRegistry) {
this.transformationRegistry = transformationRegistry;
transformationRegistry.addRegistryChangeListener(this);
}
@Deactivate
public void deactivate() {
transformationConfigurationRegistry.removeRegistryChangeListener(this);
transformationRegistry.removeRegistryChangeListener(this);
}
@Override
public void added(TransformationConfiguration element) {
public void added(Transformation element) {
// do nothing, configurations are added to cache if needed
}
@Override
public void removed(TransformationConfiguration element) {
public void removed(Transformation element) {
cachedTransformations.remove(element.getUID());
}
@Override
public void updated(TransformationConfiguration oldElement, TransformationConfiguration element) {
public void updated(Transformation oldElement, Transformation element) {
if (cachedTransformations.remove(oldElement.getUID()) != null) {
// import only if it was present before
importConfiguration(element);
@@ -144,12 +143,11 @@ public class ScaleTransformationService
@Override
public @Nullable String transform(String function, String source) throws TransformationException {
// always get a configuration from the registry to account for changed system locale
TransformationConfiguration transformationConfiguration = transformationConfigurationRegistry.get(function,
null);
Transformation transformation = transformationRegistry.get(function, null);
if (transformationConfiguration != null) {
if (!cachedTransformations.containsKey(transformationConfiguration.getUID())) {
importConfiguration(transformationConfiguration);
if (transformation != null) {
if (!cachedTransformations.containsKey(transformation.getUID())) {
importConfiguration(transformation);
}
Map<@Nullable Range, String> data = cachedTransformations.get(function);
@@ -196,13 +194,17 @@ public class ScaleTransformationService
.orElseThrow(() -> new TransformationException("No matching range for '" + source + "'"));
}
private void importConfiguration(@Nullable TransformationConfiguration configuration) {
private void importConfiguration(@Nullable Transformation configuration) {
if (configuration != null) {
try {
final Map<@Nullable Range, String> data = new LinkedHashMap<>();
data.put(FORMAT_RANGE, FORMAT_LABEL);
final OrderedProperties properties = new OrderedProperties();
properties.load(new StringReader(configuration.getContent()));
String function = configuration.getConfiguration().get(Transformation.FUNCTION);
if (function == null) {
return;
}
properties.load(new StringReader(function));
for (Object orderedKey : properties.orderedKeys()) {
final String entry = (String) orderedKey;
@@ -244,7 +246,7 @@ public class ScaleTransformationService
@Nullable Locale locale) {
if (PROFILE_CONFIG_URI.equals(uri.toString())) {
if (CONFIG_PARAM_FUNCTION.equals(param)) {
return transformationConfigurationRegistry.getConfigurations(SUPPORTED_CONFIGURATION_TYPES).stream()
return transformationRegistry.getTransformations(SUPPORTED_CONFIGURATION_TYPES).stream()
.map(c -> new ParameterOption(c.getUID(), c.getLabel())).collect(Collectors.toList());
}
}

View File

@@ -37,9 +37,9 @@ import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.transform.TransformationConfiguration;
import org.openhab.core.transform.TransformationConfigurationRegistry;
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
/**
* @author Gaël L'hopital - Initial contribution
@@ -51,8 +51,8 @@ public class ScaleTransformServiceTest {
private static final String SRC_FOLDER = "conf" + File.separator + "transform";
@Mock
private @NonNullByDefault({}) TransformationConfigurationRegistry transformationConfigurationRegistry;
private final Map<String, TransformationConfiguration> configurationMap = new HashMap<>();
private @NonNullByDefault({}) TransformationRegistry transformationConfigurationRegistry;
private final Map<String, Transformation> configurationMap = new HashMap<>();
private @NonNullByDefault({}) ScaleTransformationService processor;
@BeforeEach
@@ -62,15 +62,15 @@ public class ScaleTransformServiceTest {
try {
String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
String uid = Path.of(SRC_FOLDER).relativize(file).toString();
TransformationConfiguration transformationConfiguration = new TransformationConfiguration(uid, uid,
"scale", null, content);
Transformation transformationConfiguration = new Transformation(uid, uid, "scale",
Map.of(Transformation.FUNCTION, content));
configurationMap.put(uid, transformationConfiguration);
} catch (IOException ignored) {
}
});
Mockito.when(transformationConfigurationRegistry.get(anyString(), eq(null)))
.thenAnswer((Answer<TransformationConfiguration>) invocation -> {
.thenAnswer((Answer<Transformation>) invocation -> {
Object[] args = invocation.getArguments();
return configurationMap.get(args[0]);
});