From 8a38420655319946103b29414210e7114db155f2 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Fri, 29 Dec 2023 10:04:10 +0100 Subject: [PATCH] Remove commons classes that are available in core (#552) Signed-off-by: Jan N. Klug (cherry picked from commit cd6f4fc4886fdfdb379c400994d00a18db2aae7a) --- .../internal/ClientThingHandlerTest.java | 13 -- .../itemvalueconverter/ChannelMode.java | 28 --- .../itemvalueconverter/ContentWrapper.java | 56 ----- .../ItemValueConverter.java | 42 ---- .../ItemValueConverterChannelConfig.java | 139 ------------ .../AbstractTransformingItemConverter.java | 116 ---------- .../converter/ColorItemConverter.java | 146 ------------- .../converter/DimmerItemConverter.java | 104 --------- .../FixedValueMappingItemConverter.java | 65 ------ .../converter/GenericItemConverter.java | 61 ------ .../converter/ImageItemConverter.java | 56 ----- .../converter/NumberItemConverter.java | 79 ------- .../converter/PlayerItemConverter.java | 86 -------- .../converter/RollershutterItemConverter.java | 102 --------- .../CascadedValueTransformation.java | 64 ------ .../transform/NoOpValueTransformation.java | 42 ---- .../transform/SingleValueTransformation.java | 90 -------- .../transform/ValueTransformation.java | 35 ---- .../ValueTransformationProvider.java | 34 --- .../ValueTransformationProviderImpl.java | 54 ----- ...AbstractTransformingItemConverterTest.java | 172 --------------- .../converter/ConverterTest.java | 198 ------------------ .../CascadedValueTransformationTest.java | 133 ------------ .../org.smarthomej.transform.chain/pom.xml | 9 - .../src/main/feature/feature.xml | 1 - 25 files changed, 1925 deletions(-) delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ChannelMode.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ContentWrapper.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverterChannelConfig.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ColorItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/DimmerItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/FixedValueMappingItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/GenericItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ImageItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/NumberItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/PlayerItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/RollershutterItemConverter.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/CascadedValueTransformation.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/NoOpValueTransformation.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/SingleValueTransformation.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformation.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformationProvider.java delete mode 100644 bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/internal/ValueTransformationProviderImpl.java delete mode 100644 bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverterTest.java delete mode 100644 bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/ConverterTest.java delete mode 100644 bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/transform/CascadedValueTransformationTest.java diff --git a/bundles/org.smarthomej.binding.tcpudp/src/test/java/org/smarthomej/binding/tcpudp/internal/ClientThingHandlerTest.java b/bundles/org.smarthomej.binding.tcpudp/src/test/java/org/smarthomej/binding/tcpudp/internal/ClientThingHandlerTest.java index 59e65da24b..685bca2d02 100644 --- a/bundles/org.smarthomej.binding.tcpudp/src/test/java/org/smarthomej/binding/tcpudp/internal/ClientThingHandlerTest.java +++ b/bundles/org.smarthomej.binding.tcpudp/src/test/java/org/smarthomej/binding/tcpudp/internal/ClientThingHandlerTest.java @@ -26,11 +26,9 @@ import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; @@ -53,8 +51,6 @@ import org.smarthomej.binding.tcpudp.internal.test.EchoServer; import org.smarthomej.binding.tcpudp.internal.test.TestUtil; import org.smarthomej.commons.SimpleDynamicStateDescriptionProvider; -import org.smarthomej.commons.transform.NoOpValueTransformation; -import org.smarthomej.commons.transform.ValueTransformationProvider; /** * The {@link ClientThingHandlerTest} is a test class for {@link ClientThingHandler} @@ -73,18 +69,9 @@ public class ClientThingHandlerTest extends JavaTest { @Mock private @NonNullByDefault({}) ThingHandlerCallback thingHandlerCallback; - @Mock - private @NonNullByDefault({}) ValueTransformationProvider valueTransformationProvider; - @Mock private @NonNullByDefault({}) SimpleDynamicStateDescriptionProvider simpleDynamicStateDescriptionProvider; - @BeforeEach - public void startUp() { - Mockito.doReturn(NoOpValueTransformation.getInstance()).when(valueTransformationProvider) - .getValueTransformation(any()); - } - @Test public void tcpRequestTest() { requestTest(ClientConfiguration.Protocol.TCP); diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ChannelMode.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ChannelMode.java deleted file mode 100644 index 5143ad344d..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ChannelMode.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * The {@link ChannelMode} enum defines control modes for channels - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public enum ChannelMode { - READONLY, - READWRITE, - WRITEONLY -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ContentWrapper.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ContentWrapper.java deleted file mode 100644 index 54ccc54e18..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ContentWrapper.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter; - -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; - -/** - * The {@link ContentWrapper} defines the pre-processed response - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class ContentWrapper { - private final byte[] rawContent; - private final Charset encoding; - private final @Nullable String mediaType; - - public ContentWrapper(byte[] rawContent, String encoding, @Nullable String mediaType) { - this.rawContent = rawContent; - this.mediaType = mediaType; - - Charset finalEncoding = StandardCharsets.UTF_8; - try { - finalEncoding = Charset.forName(encoding); - } catch (IllegalArgumentException e) { - } - this.encoding = finalEncoding; - } - - public byte[] getRawContent() { - return rawContent; - } - - public String getAsString() { - return new String(rawContent, encoding); - } - - public @Nullable String getMediaType() { - return mediaType; - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverter.java deleted file mode 100644 index cae81b633e..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverter.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.types.Command; - -/** - * The {@link ItemValueConverter} defines the interface for converting received content to item state and converting - * comannds to sending value - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public interface ItemValueConverter { - - /** - * called to process a given content for this channel - * - * @param content content of the HTTP request (null results in UNDEF) - */ - void process(@Nullable ContentWrapper content); - - /** - * called to send a command to this channel - * - * @param command - */ - void send(Command command); -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverterChannelConfig.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverterChannelConfig.java deleted file mode 100644 index 764daffbb9..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/ItemValueConverterChannelConfig.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter; - -import java.math.BigDecimal; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.IncreaseDecreaseType; -import org.openhab.core.library.types.NextPreviousType; -import org.openhab.core.library.types.OnOffType; -import org.openhab.core.library.types.OpenClosedType; -import org.openhab.core.library.types.PlayPauseType; -import org.openhab.core.library.types.RewindFastforwardType; -import org.openhab.core.library.types.StopMoveType; -import org.openhab.core.library.types.UpDownType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.smarthomej.commons.itemvalueconverter.converter.ColorItemConverter; - -/** - * The {@link ItemValueConverterChannelConfig} is an abstract class base class for the channel configuration of things - * using the item value converters - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class ItemValueConverterChannelConfig { - private final Map stringStateMap = new HashMap<>(); - private final Map commandStringMap = new HashMap<>(); - - public ChannelMode mode = ChannelMode.READWRITE; - - // number - public @Nullable String unit; - - // switch, dimmer, color - public @Nullable String onValue; - public @Nullable String offValue; - - // dimmer, color - public BigDecimal step = BigDecimal.ONE; - public @Nullable String increaseValue; - public @Nullable String decreaseValue; - - // color - public ColorItemConverter.ColorMode colorMode = ColorItemConverter.ColorMode.RGB; - - // contact - public @Nullable String openValue; - public @Nullable String closedValue; - - // rollershutter - public @Nullable String upValue; - public @Nullable String downValue; - public @Nullable String stopValue; - public @Nullable String moveValue; - - // player - public @Nullable String playValue; - public @Nullable String pauseValue; - public @Nullable String nextValue; - public @Nullable String previousValue; - public @Nullable String rewindValue; - public @Nullable String fastforwardValue; - - private boolean initialized = false; - - /** - * maps a command to a user-defined string - * - * @param command the command to map - * @return a string or null if no mapping found - */ - public @Nullable String commandToFixedValue(Command command) { - if (!initialized) { - createMaps(); - } - - return commandStringMap.get(command); - } - - /** - * maps a user-defined string to a state - * - * @param string the string to map - * @return the state or null if no mapping found - */ - public @Nullable State fixedValueToState(String string) { - if (!initialized) { - createMaps(); - } - - return stringStateMap.get(string); - } - - private void createMaps() { - addToMaps(this.onValue, OnOffType.ON); - addToMaps(this.offValue, OnOffType.OFF); - addToMaps(this.openValue, OpenClosedType.OPEN); - addToMaps(this.closedValue, OpenClosedType.CLOSED); - addToMaps(this.upValue, UpDownType.UP); - addToMaps(this.downValue, UpDownType.DOWN); - - commandStringMap.put(IncreaseDecreaseType.INCREASE, increaseValue); - commandStringMap.put(IncreaseDecreaseType.DECREASE, decreaseValue); - commandStringMap.put(StopMoveType.STOP, stopValue); - commandStringMap.put(StopMoveType.MOVE, moveValue); - commandStringMap.put(PlayPauseType.PLAY, playValue); - commandStringMap.put(PlayPauseType.PAUSE, pauseValue); - commandStringMap.put(NextPreviousType.NEXT, nextValue); - commandStringMap.put(NextPreviousType.PREVIOUS, previousValue); - commandStringMap.put(RewindFastforwardType.REWIND, rewindValue); - commandStringMap.put(RewindFastforwardType.FASTFORWARD, fastforwardValue); - - initialized = true; - } - - private void addToMaps(@Nullable String value, State state) { - if (value != null) { - commandStringMap.put((Command) state, value); - stringStateMap.put(value, state); - } - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverter.java deleted file mode 100644 index c0df15fff3..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverter.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ChannelMode; -import org.smarthomej.commons.itemvalueconverter.ContentWrapper; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverter; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link AbstractTransformingItemConverter} is a base class for an item converter with transformations - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public abstract class AbstractTransformingItemConverter implements ItemValueConverter { - private final Consumer updateState; - private final Consumer postCommand; - private final @Nullable Consumer sendValue; - private final ValueTransformation stateTransformations; - private final ValueTransformation commandTransformations; - - protected final ItemValueConverterChannelConfig channelConfig; - - public AbstractTransformingItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - this.updateState = updateState; - this.postCommand = postCommand; - this.sendValue = sendValue; - this.stateTransformations = stateTransformations; - this.commandTransformations = commandTransformations; - this.channelConfig = channelConfig; - } - - @Override - public void process(@Nullable ContentWrapper content) { - if (content == null) { - updateState.accept(UnDefType.UNDEF); - return; - } - if (channelConfig.mode != ChannelMode.WRITEONLY) { - stateTransformations.apply(content.getAsString()).ifPresent(transformedValue -> { - Command command = toCommand(transformedValue); - if (command != null) { - postCommand.accept(command); - } else { - toState(transformedValue).ifPresent(updateState); - } - }); - } else { - throw new IllegalStateException("Write-only channel"); - } - } - - @Override - public void send(Command command) { - Consumer sendHttpValue = this.sendValue; - if (sendHttpValue != null && channelConfig.mode != ChannelMode.READONLY) { - commandTransformations.apply(toString(command)).ifPresent(sendHttpValue); - } else { - throw new IllegalStateException("Read-only channel"); - } - } - - /** - * check if this converter received a value that needs to be sent as command - * - * @param value the value - * @return the command or null - */ - protected abstract @Nullable Command toCommand(String value); - - /** - * convert the received value to a state - * - * @param value the value - * @return the state that represents the value of UNDEF if conversion failed - */ - protected abstract Optional toState(String value); - - /** - * convert a command to a string - * - * @param command the command - * @return the string representation of the command - */ - protected abstract String toString(Command command); - - @FunctionalInterface - public interface Factory { - ItemValueConverter create(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendHttpValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ColorItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ColorItemConverter.java deleted file mode 100644 index 283990a1ba..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ColorItemConverter.java +++ /dev/null @@ -1,146 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.math.BigDecimal; -import java.util.Optional; -import java.util.function.Consumer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.HSBType; -import org.openhab.core.library.types.PercentType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link ColorItemConverter} implements {@link org.openhab.core.library.items.ColorItem} conversions - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class ColorItemConverter extends AbstractTransformingItemConverter { - private static final BigDecimal BYTE_FACTOR = BigDecimal.valueOf(2.55); - private static final BigDecimal HUNDRED = BigDecimal.valueOf(100); - private static final Pattern TRIPLE_MATCHER = Pattern.compile("(?\\d+),(?\\d+),(?\\d+)"); - - private State state = UnDefType.UNDEF; - - public ColorItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - protected @Nullable Command toCommand(String value) { - return null; - } - - @Override - public String toString(Command command) { - String string = channelConfig.commandToFixedValue(command); - if (string != null) { - return string; - } - - if (command instanceof HSBType) { - HSBType newState = (HSBType) command; - state = newState; - return hsbToString(newState); - } else if (command instanceof PercentType && state instanceof HSBType) { - HSBType newState = new HSBType(((HSBType) state).getBrightness(), ((HSBType) state).getSaturation(), - (PercentType) command); - state = newState; - return hsbToString(newState); - } - - throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported"); - } - - @Override - public Optional toState(String string) { - State newState = UnDefType.UNDEF; - if (string.equals(channelConfig.onValue)) { - if (state instanceof HSBType) { - newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), - PercentType.HUNDRED); - } else { - newState = HSBType.WHITE; - } - } else if (string.equals(channelConfig.offValue)) { - if (state instanceof HSBType) { - newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), PercentType.ZERO); - } else { - newState = HSBType.BLACK; - } - } else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType) { - BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().add(channelConfig.step); - if (HUNDRED.compareTo(newBrightness) < 0) { - newBrightness = HUNDRED; - } - newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), - new PercentType(newBrightness)); - } else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType) { - BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().subtract(channelConfig.step); - if (BigDecimal.ZERO.compareTo(newBrightness) > 0) { - newBrightness = BigDecimal.ZERO; - } - newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), - new PercentType(newBrightness)); - } else { - Matcher matcher = TRIPLE_MATCHER.matcher(string); - if (matcher.matches()) { - switch (channelConfig.colorMode) { - case RGB: - int r = Integer.parseInt(matcher.group("r")); - int g = Integer.parseInt(matcher.group("g")); - int b = Integer.parseInt(matcher.group("b")); - newState = HSBType.fromRGB(r, g, b); - break; - case HSB: - newState = new HSBType(string); - break; - } - } - } - - state = newState; - return Optional.of(newState); - } - - private String hsbToString(HSBType state) { - switch (channelConfig.colorMode) { - case RGB: - PercentType[] rgb = state.toRGB(); - return String.format("%1$d,%2$d,%3$d", rgb[0].toBigDecimal().multiply(BYTE_FACTOR).intValue(), - rgb[1].toBigDecimal().multiply(BYTE_FACTOR).intValue(), - rgb[2].toBigDecimal().multiply(BYTE_FACTOR).intValue()); - case HSB: - return state.toString(); - } - throw new IllegalStateException("Invalid colorMode setting"); - } - - public enum ColorMode { - RGB, - HSB - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/DimmerItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/DimmerItemConverter.java deleted file mode 100644 index e8bdcaa6f8..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/DimmerItemConverter.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.math.BigDecimal; -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.PercentType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link DimmerItemConverter} implements {@link org.openhab.core.library.items.DimmerItem} conversions - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class DimmerItemConverter extends AbstractTransformingItemConverter { - private static final BigDecimal HUNDRED = BigDecimal.valueOf(100); - - private State state = UnDefType.UNDEF; - - public DimmerItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - protected @Nullable Command toCommand(String value) { - return null; - } - - @Override - public String toString(Command command) { - String string = channelConfig.commandToFixedValue(command); - if (string != null) { - return string; - } - - if (command instanceof PercentType) { - return ((PercentType) command).toString(); - } - - throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported"); - } - - @Override - public Optional toState(String string) { - State newState = UnDefType.UNDEF; - - if (string.equals(channelConfig.onValue)) { - newState = PercentType.HUNDRED; - } else if (string.equals(channelConfig.offValue)) { - newState = PercentType.ZERO; - } else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType) { - BigDecimal newBrightness = ((PercentType) state).toBigDecimal().add(channelConfig.step); - if (HUNDRED.compareTo(newBrightness) < 0) { - newBrightness = HUNDRED; - } - newState = new PercentType(newBrightness); - } else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType) { - BigDecimal newBrightness = ((PercentType) state).toBigDecimal().subtract(channelConfig.step); - if (BigDecimal.ZERO.compareTo(newBrightness) > 0) { - newBrightness = BigDecimal.ZERO; - } - newState = new PercentType(newBrightness); - } else { - try { - BigDecimal value = new BigDecimal(string); - if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) { - value = PercentType.HUNDRED.toBigDecimal(); - } - if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) { - value = PercentType.ZERO.toBigDecimal(); - } - newState = new PercentType(value); - } catch (NumberFormatException e) { - // ignore - } - } - - state = newState; - return Optional.of(newState); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/FixedValueMappingItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/FixedValueMappingItemConverter.java deleted file mode 100644 index c01e754eb1..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/FixedValueMappingItemConverter.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.Objects; -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link FixedValueMappingItemConverter} implements mapping conversions for different item-types - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class FixedValueMappingItemConverter extends AbstractTransformingItemConverter { - - public FixedValueMappingItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - protected @Nullable Command toCommand(String value) { - return null; - } - - @Override - public String toString(Command command) { - String value = channelConfig.commandToFixedValue(command); - if (value != null) { - return value; - } - - throw new IllegalArgumentException( - "Command type '" + command.toString() + "' not supported or mapping not defined."); - } - - @Override - public Optional toState(String string) { - State state = channelConfig.fixedValueToState(string); - - return Optional.of(Objects.requireNonNullElse(state, UnDefType.UNDEF)); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/GenericItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/GenericItemConverter.java deleted file mode 100644 index 3a8cdc1956..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/GenericItemConverter.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.Optional; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link GenericItemConverter} implements simple conversions for different item types - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class GenericItemConverter extends AbstractTransformingItemConverter { - private final Function toState; - - public GenericItemConverter(Function toState, Consumer updateState, - Consumer postCommand, @Nullable Consumer sendValue, - ValueTransformation stateTransformations, ValueTransformation commandTransformations, - ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - this.toState = toState; - } - - protected Optional toState(String value) { - try { - return Optional.of(toState.apply(value)); - } catch (IllegalArgumentException e) { - return Optional.of(UnDefType.UNDEF); - } - } - - @Override - protected @Nullable Command toCommand(String value) { - return null; - } - - protected String toString(Command command) { - return command.toString(); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ImageItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ImageItemConverter.java deleted file mode 100644 index 583d4428b8..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/ImageItemConverter.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.RawType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ContentWrapper; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverter; - -/** - * The {@link ImageItemConverter} implements {@link org.openhab.core.library.items.ImageItem} conversions - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class ImageItemConverter implements ItemValueConverter { - private final Consumer updateState; - - public ImageItemConverter(Consumer updateState) { - this.updateState = updateState; - } - - @Override - public void process(@Nullable ContentWrapper content) { - if (content == null) { - updateState.accept(UnDefType.UNDEF); - return; - } - String mediaType = content.getMediaType(); - updateState.accept( - new RawType(content.getRawContent(), mediaType != null ? mediaType : RawType.DEFAULT_MIME_TYPE)); - } - - @Override - public void send(Command command) { - throw new IllegalStateException("Read-only channel"); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/NumberItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/NumberItemConverter.java deleted file mode 100644 index 94d7ac564f..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/NumberItemConverter.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.Optional; -import java.util.function.Consumer; - -import javax.measure.format.MeasurementParseException; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.DecimalType; -import org.openhab.core.library.types.QuantityType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link NumberItemConverter} implements {@link org.openhab.core.library.items.NumberItem} conversions - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class NumberItemConverter extends AbstractTransformingItemConverter { - - public NumberItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - protected @Nullable Command toCommand(String value) { - return null; - } - - @Override - protected Optional toState(String value) { - String trimmedValue = value.trim(); - State newState = UnDefType.UNDEF; - if (!trimmedValue.isEmpty()) { - try { - if (channelConfig.unit != null) { - // we have a given unit - use that - newState = new QuantityType<>(trimmedValue + " " + channelConfig.unit); - } else { - try { - // try if we have a simple number - newState = new DecimalType(trimmedValue); - } catch (IllegalArgumentException e1) { - // not a plain number, maybe with unit? - newState = new QuantityType<>(trimmedValue); - } - } - } catch (IllegalArgumentException | MeasurementParseException e) { - // finally failed - } - } - return Optional.of(newState); - } - - @Override - protected String toString(Command command) { - return command.toString(); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/PlayerItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/PlayerItemConverter.java deleted file mode 100644 index 9d6a245ded..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/PlayerItemConverter.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.NextPreviousType; -import org.openhab.core.library.types.PlayPauseType; -import org.openhab.core.library.types.RewindFastforwardType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link PlayerItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem} - * conversions - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class PlayerItemConverter extends AbstractTransformingItemConverter { - private @Nullable String lastCommand; // store last command to prevent duplicate commands - - public PlayerItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - public String toString(Command command) { - String string = channelConfig.commandToFixedValue(command); - if (string != null) { - return string; - } - - throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported"); - } - - @Override - protected @Nullable Command toCommand(String string) { - if (string.equals(lastCommand)) { - // only send commands once - return null; - } - lastCommand = string; - - if (string.equals(channelConfig.playValue)) { - return PlayPauseType.PLAY; - } else if (string.equals(channelConfig.pauseValue)) { - return PlayPauseType.PAUSE; - } else if (string.equals(channelConfig.nextValue)) { - return NextPreviousType.NEXT; - } else if (string.equals(channelConfig.previousValue)) { - return NextPreviousType.PREVIOUS; - } else if (string.equals(channelConfig.rewindValue)) { - return RewindFastforwardType.REWIND; - } else if (string.equals(channelConfig.fastforwardValue)) { - return RewindFastforwardType.FASTFORWARD; - } - - return null; - } - - @Override - public Optional toState(String string) { - // no value - we ignore state updates - return Optional.empty(); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/RollershutterItemConverter.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/RollershutterItemConverter.java deleted file mode 100644 index 22ed5bafa4..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/itemvalueconverter/converter/RollershutterItemConverter.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.math.BigDecimal; -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.library.types.PercentType; -import org.openhab.core.library.types.StopMoveType; -import org.openhab.core.library.types.UpDownType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link RollershutterItemConverter} implements {@link org.openhab.core.library.items.RollershutterItem} - * conversions - * - * @author Jan N. Klug - Initial contribution - */ - -@NonNullByDefault -public class RollershutterItemConverter extends AbstractTransformingItemConverter { - - public RollershutterItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateTransformations, - ValueTransformation commandTransformations, ItemValueConverterChannelConfig channelConfig) { - super(updateState, postCommand, sendValue, stateTransformations, commandTransformations, channelConfig); - } - - @Override - public String toString(Command command) { - String string = channelConfig.commandToFixedValue(command); - if (string != null) { - return string; - } - - if (command instanceof PercentType) { - final String downValue = channelConfig.downValue; - final String upValue = channelConfig.upValue; - if (command.equals(PercentType.HUNDRED) && downValue != null) { - return downValue; - } else if (command.equals(PercentType.ZERO) && upValue != null) { - return upValue; - } else { - return ((PercentType) command).toString(); - } - } - - throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported"); - } - - @Override - protected @Nullable Command toCommand(String string) { - if (string.equals(channelConfig.upValue)) { - return UpDownType.UP; - } else if (string.equals(channelConfig.downValue)) { - return UpDownType.DOWN; - } else if (string.equals(channelConfig.moveValue)) { - return StopMoveType.MOVE; - } else if (string.equals(channelConfig.stopValue)) { - return StopMoveType.STOP; - } - - return null; - } - - @Override - public Optional toState(String string) { - State newState = UnDefType.UNDEF; - try { - BigDecimal value = new BigDecimal(string); - if (value.compareTo(PercentType.HUNDRED.toBigDecimal()) > 0) { - value = PercentType.HUNDRED.toBigDecimal(); - } - if (value.compareTo(PercentType.ZERO.toBigDecimal()) < 0) { - value = PercentType.ZERO.toBigDecimal(); - } - newState = new PercentType(value); - } catch (NumberFormatException e) { - // ignore - } - - return Optional.of(newState); - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/CascadedValueTransformation.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/CascadedValueTransformation.java deleted file mode 100644 index 77e360cd53..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/CascadedValueTransformation.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import java.util.stream.Collectors; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.transform.TransformationService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The {@link CascadedValueTransformation} implements {@link ValueTransformation for a cascaded set of - * transformations} - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class CascadedValueTransformation implements ValueTransformation { - private final Logger logger = LoggerFactory.getLogger(CascadedValueTransformation.class); - private final List transformations; - - public CascadedValueTransformation(String transformationString, - Function transformationServiceSupplier) { - List transformations; - try { - transformations = Arrays.stream(transformationString.split("∩")).filter(s -> !s.isEmpty()) - .map(transformation -> new SingleValueTransformation(transformation, transformationServiceSupplier)) - .collect(Collectors.toList()); - } catch (IllegalArgumentException e) { - transformations = List.of(NoOpValueTransformation.getInstance()); - logger.warn("Transformation ignored, failed to parse {}: {}", transformationString, e.getMessage()); - } - this.transformations = transformations; - } - - @Override - public Optional apply(String value) { - Optional valueOptional = Optional.of(value); - - // process all transformations - for (ValueTransformation transformation : transformations) { - valueOptional = valueOptional.flatMap(transformation::apply); - } - - return valueOptional; - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/NoOpValueTransformation.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/NoOpValueTransformation.java deleted file mode 100644 index cf9643f1ce..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/NoOpValueTransformation.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import java.util.Optional; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * The {@link NoOpValueTransformation} implements a no-op (identity) transformation - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class NoOpValueTransformation implements ValueTransformation { - private static final NoOpValueTransformation NO_OP_VALUE_TRANSFORMATION = new NoOpValueTransformation(); - - @Override - public Optional apply(String value) { - return Optional.of(value); - } - - /** - * get the static value transformation for identity - * - * @return - */ - public static ValueTransformation getInstance() { - return NO_OP_VALUE_TRANSFORMATION; - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/SingleValueTransformation.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/SingleValueTransformation.java deleted file mode 100644 index ab7a73616e..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/SingleValueTransformation.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import java.lang.ref.WeakReference; -import java.util.Optional; -import java.util.function.Function; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.transform.TransformationException; -import org.openhab.core.transform.TransformationService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * A transformation for a value used in {@HttpChannel}. - * - * @author David Graeff - Initial contribution - * @author Jan N. Klug - adapted from MQTT binding to HTTP binding - */ -@NonNullByDefault -public class SingleValueTransformation implements ValueTransformation { - private final Logger logger = LoggerFactory.getLogger(SingleValueTransformation.class); - private final Function transformationServiceSupplier; - private WeakReference<@Nullable TransformationService> transformationService = new WeakReference<>(null); - private final String pattern; - private final String serviceName; - - /** - * Creates a new channel state transformer. - * - * @param pattern A transformation pattern, starting with the transformation service - * name, followed by a colon and the transformation itself. - * @param transformationServiceSupplier a {@link Function} providing transformations - */ - public SingleValueTransformation(String pattern, - Function transformationServiceSupplier) { - this.transformationServiceSupplier = transformationServiceSupplier; - int index = pattern.indexOf(':'); - if (index == -1) { - throw new IllegalArgumentException( - "The transformation pattern must consist of the type and the pattern separated by a colon"); - } - this.serviceName = pattern.substring(0, index).toUpperCase().trim(); - this.pattern = pattern.substring(index + 1).trim(); - } - - @Override - public Optional apply(String value) { - TransformationService transformationService = this.transformationService.get(); - if (transformationService == null) { - transformationService = transformationServiceSupplier.apply(serviceName); - if (transformationService == null) { - logger.warn("Transformation service {} for pattern {} not found!", serviceName, pattern); - return Optional.empty(); - } - this.transformationService = new WeakReference<>(transformationService); - } - - try { - String result = transformationService.transform(pattern, value); - if (result == null) { - logger.debug("Transformation {} returned empty result when applied to {}.", this, value); - return Optional.empty(); - } - return Optional.of(result); - } catch (TransformationException e) { - logger.warn("Executing transformation {} failed: {}", this, e.getMessage()); - } - - return Optional.empty(); - } - - @Override - public String toString() { - return "ChannelStateTransformation{pattern='" + pattern + "', serviceName='" + serviceName + "'}"; - } -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformation.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformation.java deleted file mode 100644 index 219f5186ac..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformation.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import java.util.Optional; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * The {@link ValueTransformation} applies a set of transformations to a value - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public interface ValueTransformation { - - /** - * applies the value transformation to a value - * - * @param value The value - * @return Optional of string representing the transformed value (empty if transformation not present or failed) - */ - Optional apply(String value); -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformationProvider.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformationProvider.java deleted file mode 100644 index 223fea68f0..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/ValueTransformationProvider.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; - -/** - * The {@link ValueTransformationProvider} allows to retrieve a transformation service by name - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public interface ValueTransformationProvider { - - /** - * - * @param pattern A transformation pattern, starting with the transformation service - * name, followed by a colon and the transformation itself. - * @return - */ - ValueTransformation getValueTransformation(@Nullable String pattern); -} diff --git a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/internal/ValueTransformationProviderImpl.java b/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/internal/ValueTransformationProviderImpl.java deleted file mode 100644 index 815955ad8e..0000000000 --- a/bundles/org.smarthomej.commons/src/main/java/org/smarthomej/commons/transform/internal/ValueTransformationProviderImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform.internal; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.transform.TransformationHelper; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; -import org.smarthomej.commons.transform.CascadedValueTransformation; -import org.smarthomej.commons.transform.NoOpValueTransformation; -import org.smarthomej.commons.transform.ValueTransformation; -import org.smarthomej.commons.transform.ValueTransformationProvider; - -/** - * The {@link ValueTransformationProviderImpl} implements - * {@link org.smarthomej.commons.transform.ValueTransformationProvider} - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -@Component(service = ValueTransformationProvider.class) -public class ValueTransformationProviderImpl implements ValueTransformationProvider { - - private final BundleContext bundleContext; - - @Activate - @SuppressWarnings("unused") - public ValueTransformationProviderImpl(ComponentContext componentContext) { - this.bundleContext = componentContext.getBundleContext(); - } - - @Override - public ValueTransformation getValueTransformation(@Nullable String pattern) { - if (pattern == null || pattern.isEmpty()) { - return NoOpValueTransformation.getInstance(); - } - - return new CascadedValueTransformation(pattern, - name -> TransformationHelper.getTransformationService(bundleContext, name)); - } -} diff --git a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverterTest.java b/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverterTest.java deleted file mode 100644 index 1aaf8bfa35..0000000000 --- a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/AbstractTransformingItemConverterTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.only; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import java.util.function.Consumer; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.openhab.core.library.types.StringType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ContentWrapper; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.NoOpValueTransformation; -import org.smarthomej.commons.transform.ValueTransformation; - -/** - * The {@link AbstractTransformingItemConverterTest} is a test class for the {@link AbstractTransformingItemConverter} - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class AbstractTransformingItemConverterTest { - - @Mock - private @NonNullByDefault({}) Consumer sendHttpValue; - - @Mock - private @NonNullByDefault({}) Consumer updateState; - - @Mock - private @NonNullByDefault({}) Consumer postCommand; - - private @NonNullByDefault({}) AutoCloseable closeable; - - @Spy - private ValueTransformation stateValueTransformation = NoOpValueTransformation.getInstance(); - - @Spy - private ValueTransformation commandValueTransformation = NoOpValueTransformation.getInstance(); - - @BeforeEach - public void init() { - closeable = MockitoAnnotations.openMocks(this); - } - - @AfterEach - public void close() throws Exception { - closeable.close(); - } - - @Test - public void undefOnNullContentTest() { - TestItemConverter realConverter = new TestItemConverter(updateState, postCommand, sendHttpValue, - stateValueTransformation, commandValueTransformation, false); - TestItemConverter converter = spy(realConverter); - - converter.process(null); - // make sure UNDEF is send as state update - verify(updateState, only()).accept(UnDefType.UNDEF); - verify(postCommand, never()).accept(any()); - verify(sendHttpValue, never()).accept(any()); - - // make sure no other processing applies - verify(converter, never()).toState(any()); - verify(converter, never()).toCommand(any()); - verify(converter, never()).toString(any()); - } - - @Test - public void commandIsPostedAsCommand() { - TestItemConverter converter = new TestItemConverter(updateState, postCommand, sendHttpValue, - stateValueTransformation, commandValueTransformation, true); - - converter.process(new ContentWrapper("TEST".getBytes(StandardCharsets.UTF_8), "", null)); - - // check state transformation is applied - verify(stateValueTransformation).apply(any()); - verify(commandValueTransformation, never()).apply(any()); - - // check only postCommand is applied - verify(updateState, never()).accept(any()); - verify(postCommand, only()).accept(new StringType("TEST")); - verify(sendHttpValue, never()).accept(any()); - } - - @Test - public void updateIsPostedAsUpdate() { - TestItemConverter converter = new TestItemConverter(updateState, postCommand, sendHttpValue, - stateValueTransformation, commandValueTransformation, false); - - converter.process(new ContentWrapper("TEST".getBytes(StandardCharsets.UTF_8), "", null)); - - // check state transformation is applied - verify(stateValueTransformation).apply(any()); - verify(commandValueTransformation, never()).apply(any()); - - // check only updateState is called - verify(updateState, only()).accept(new StringType("TEST")); - verify(postCommand, never()).accept(any()); - verify(sendHttpValue, never()).accept(any()); - } - - @Test - public void sendCommandSendsCommand() { - TestItemConverter converter = new TestItemConverter(updateState, postCommand, sendHttpValue, - stateValueTransformation, commandValueTransformation, false); - - converter.send(new StringType("TEST")); - - // check command transformation is applied - verify(stateValueTransformation, never()).apply(any()); - verify(commandValueTransformation).apply(any()); - - // check only sendHttpValue is applied - verify(updateState, never()).accept(any()); - verify(postCommand, never()).accept(any()); - verify(sendHttpValue, only()).accept("TEST"); - } - - private static class TestItemConverter extends AbstractTransformingItemConverter { - private boolean hasCommand; - - public TestItemConverter(Consumer updateState, Consumer postCommand, - @Nullable Consumer sendValue, ValueTransformation stateValueTransformation, - ValueTransformation commandValueTransformation, boolean hasCommand) { - super(updateState, postCommand, sendValue, stateValueTransformation, commandValueTransformation, - new ItemValueConverterChannelConfig()); - this.hasCommand = hasCommand; - } - - @Override - protected @Nullable Command toCommand(String value) { - return hasCommand ? new StringType(value) : null; - } - - @Override - protected Optional toState(String value) { - return Optional.of(new StringType(value)); - } - - @Override - protected String toString(Command command) { - return command.toString(); - } - } -} diff --git a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/ConverterTest.java b/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/ConverterTest.java deleted file mode 100644 index 80d284ee9d..0000000000 --- a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/itemvalueconverter/converter/ConverterTest.java +++ /dev/null @@ -1,198 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.itemvalueconverter.converter; - -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openhab.core.library.types.DecimalType; -import org.openhab.core.library.types.HSBType; -import org.openhab.core.library.types.PercentType; -import org.openhab.core.library.types.PlayPauseType; -import org.openhab.core.library.types.PointType; -import org.openhab.core.library.types.QuantityType; -import org.openhab.core.library.types.StringType; -import org.openhab.core.library.unit.SIUnits; -import org.openhab.core.library.unit.Units; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.UnDefType; -import org.smarthomej.commons.itemvalueconverter.ContentWrapper; -import org.smarthomej.commons.itemvalueconverter.ItemValueConverterChannelConfig; -import org.smarthomej.commons.transform.NoOpValueTransformation; - -/** - * The {@link ConverterTest} is a test class for state converters - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class ConverterTest { - - @Mock - private @NonNullByDefault({}) Consumer sendHttpValue; - - @Mock - private @NonNullByDefault({}) Consumer updateState; - - @Mock - private @NonNullByDefault({}) Consumer postCommand; - - private @NonNullByDefault({}) AutoCloseable closeable; - - @BeforeEach - public void init() { - closeable = MockitoAnnotations.openMocks(this); - } - - @AfterEach - public void close() throws Exception { - closeable.close(); - } - - @Test - public void numberItemConverter() { - NumberItemConverter converter = new NumberItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), - new ItemValueConverterChannelConfig()); - - // without unit - Assertions.assertEquals(Optional.of(new DecimalType(1234)), converter.toState("1234")); - - // unit in transformation result - Assertions.assertEquals(Optional.of(new QuantityType<>(100, SIUnits.CELSIUS)), converter.toState("100°C")); - - // no valid value - Assertions.assertEquals(Optional.of(UnDefType.UNDEF), converter.toState("W")); - Assertions.assertEquals(Optional.of(UnDefType.UNDEF), converter.toState("")); - } - - @Test - public void numberItemConverterWithUnit() { - ItemValueConverterChannelConfig channelConfig = new ItemValueConverterChannelConfig(); - channelConfig.unit = "W"; - NumberItemConverter converter = new NumberItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), channelConfig); - - // without unit - Assertions.assertEquals(Optional.of(new QuantityType<>(500, Units.WATT)), converter.toState("500")); - - // no valid value - Assertions.assertEquals(Optional.of(UnDefType.UNDEF), converter.toState("foo")); - Assertions.assertEquals(Optional.of(UnDefType.UNDEF), converter.toState("")); - } - - @Test - public void stringTypeConverter() { - GenericItemConverter converter = createConverter(StringType::new); - Assertions.assertEquals(Optional.of(new StringType("Test")), converter.toState("Test")); - } - - @Test - public void decimalTypeConverter() { - GenericItemConverter converter = createConverter(DecimalType::new); - Assertions.assertEquals(Optional.of(new DecimalType(15.6)), converter.toState("15.6")); - } - - @Test - public void pointTypeConverter() { - GenericItemConverter converter = createConverter(PointType::new); - Assertions.assertEquals( - Optional.of(new PointType(new DecimalType(51.1), new DecimalType(7.2), new DecimalType(100))), - converter.toState("51.1, 7.2, 100")); - } - - @Test - public void playerItemTypeConverter() { - ItemValueConverterChannelConfig cfg = new ItemValueConverterChannelConfig(); - cfg.playValue = "PLAY"; - ContentWrapper content = new ContentWrapper("PLAY".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - PlayerItemConverter converter = new PlayerItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), cfg); - converter.process(content); - converter.process(content); - - Mockito.verify(postCommand).accept(PlayPauseType.PLAY); - Mockito.verify(updateState, Mockito.never()).accept(ArgumentMatchers.any()); - } - - @Test - public void colorItemTypeRGBConverter() { - ItemValueConverterChannelConfig cfg = new ItemValueConverterChannelConfig(); - cfg.colorMode = ColorItemConverter.ColorMode.RGB; - ContentWrapper content = new ContentWrapper("123,34,47".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - ColorItemConverter converter = new ColorItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), cfg); - - converter.process(content); - Mockito.verify(updateState).accept(HSBType.fromRGB(123, 34, 47)); - } - - @Test - public void colorItemTypeHSBConverter() { - ItemValueConverterChannelConfig cfg = new ItemValueConverterChannelConfig(); - cfg.colorMode = ColorItemConverter.ColorMode.HSB; - ContentWrapper content = new ContentWrapper("123,34,47".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - ColorItemConverter converter = new ColorItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), cfg); - - converter.process(content); - Mockito.verify(updateState).accept(new HSBType("123,34,47")); - } - - @Test - public void rollerSHutterConverter() { - ItemValueConverterChannelConfig cfg = new ItemValueConverterChannelConfig(); - RollershutterItemConverter converter = new RollershutterItemConverter(updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), cfg); - - // test 0 and 100 - ContentWrapper content = new ContentWrapper("0".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - converter.process(content); - Mockito.verify(updateState).accept(PercentType.ZERO); - content = new ContentWrapper("100".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - converter.process(content); - Mockito.verify(updateState).accept(PercentType.HUNDRED); - - // test under/over-range (expect two times total for zero/100 - content = new ContentWrapper("-1".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - converter.process(content); - Mockito.verify(updateState, Mockito.times(2)).accept(PercentType.ZERO); - content = new ContentWrapper("105".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - converter.process(content); - Mockito.verify(updateState, Mockito.times(2)).accept(PercentType.HUNDRED); - - // test value - content = new ContentWrapper("67".getBytes(StandardCharsets.UTF_8), "UTF-8", null); - converter.process(content); - Mockito.verify(updateState).accept(new PercentType(67)); - } - - public GenericItemConverter createConverter(Function fcn) { - return new GenericItemConverter(fcn, updateState, postCommand, sendHttpValue, - NoOpValueTransformation.getInstance(), NoOpValueTransformation.getInstance(), - new ItemValueConverterChannelConfig()); - } -} diff --git a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/transform/CascadedValueTransformationTest.java b/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/transform/CascadedValueTransformationTest.java deleted file mode 100644 index cad7fc2359..0000000000 --- a/bundles/org.smarthomej.commons/src/test/java/org/smarthomej/commons/transform/CascadedValueTransformationTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.commons.transform; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.eq; - -import java.util.Map; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openhab.core.transform.TransformationException; -import org.openhab.core.transform.TransformationService; - -/** - * The {@link CascadedValueTransformationTest} contains tests for the {@link - * org.smarthomej.commons.transform.CascadedValueTransformation} - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class CascadedValueTransformationTest { - private static final String T1_NAME = "TRANSFORM1"; - private static final String T1_PATTERN = "T1Pattern"; - private static final String T1_INPUT = "T1Input"; - private static final String T1_RESULT = "T1Result"; - - private static final String T2_NAME = "TRANSFORM2"; - private static final String T2_PATTERN = "T2Pattern"; - private static final String T2_INPUT = T1_RESULT; - private static final String T2_RESULT = "T2Result"; - - @Mock - private @NonNullByDefault({}) TransformationService transformationService1; - - @Mock - private @NonNullByDefault({}) TransformationService transformationService2; - - private @NonNullByDefault({}) AutoCloseable closeable; - - private @NonNullByDefault({}) Map serviceProvider; - - @BeforeEach - public void init() throws TransformationException { - closeable = MockitoAnnotations.openMocks(this); - Mockito.when(transformationService1.transform(eq(T1_PATTERN), eq(T1_INPUT))).thenAnswer(answer -> T1_RESULT); - Mockito.when(transformationService2.transform(eq(T2_PATTERN), eq(T1_INPUT))).thenAnswer(answer -> T2_RESULT); - Mockito.when(transformationService2.transform(eq(T2_PATTERN), eq(T2_INPUT))).thenAnswer(answer -> T2_RESULT); - - serviceProvider = Map.of("TRANSFORM1", transformationService1, "TRANSFORM2", transformationService2); - } - - @AfterEach - public void close() throws Exception { - closeable.close(); - } - - @Test - public void testMissingTransformation() { - String pattern = "TRANSFORM:pattern"; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertNull(result); - } - - @Test - public void testSingleTransformation() { - String pattern = T1_NAME + ":" + T1_PATTERN; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertEquals(T1_RESULT, result); - } - - @Test - public void testInvalidFirstTransformation() { - String pattern = T1_NAME + "X:" + T1_PATTERN + "∩" + T2_NAME + ":" + T2_PATTERN; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertNull(result); - } - - @Test - public void testInvalidSecondTransformation() { - String pattern = T1_NAME + ":" + T1_PATTERN + "∩" + T2_NAME + "X:" + T2_PATTERN; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertNull(result); - } - - @Test - public void testDoubleTransformationWithoutSpaces() { - String pattern = T1_NAME + ":" + T1_PATTERN + "∩" + T2_NAME + ":" + T2_PATTERN; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertEquals(T2_RESULT, result); - } - - @Test - public void testDoubleTransformationWithSpaces() { - String pattern = " " + T1_NAME + " : " + T1_PATTERN + " ∩ " + T2_NAME + " : " + T2_PATTERN + " "; - - CascadedValueTransformation transformation = new CascadedValueTransformation(pattern, serviceProvider::get); - String result = transformation.apply(T1_INPUT).orElse(null); - - assertEquals(T2_RESULT, result); - } -} diff --git a/bundles/org.smarthomej.transform.chain/pom.xml b/bundles/org.smarthomej.transform.chain/pom.xml index 91e6d27ade..50d11fae92 100644 --- a/bundles/org.smarthomej.transform.chain/pom.xml +++ b/bundles/org.smarthomej.transform.chain/pom.xml @@ -14,13 +14,4 @@ SmartHome/J Add-ons :: Bundles :: Transformation Service :: Chain - - - org.smarthomej.addons.bundles - org.smarthomej.commons - ${project.version} - provided - - - diff --git a/bundles/org.smarthomej.transform.chain/src/main/feature/feature.xml b/bundles/org.smarthomej.transform.chain/src/main/feature/feature.xml index 39e0963664..0a5fdb764c 100644 --- a/bundles/org.smarthomej.transform.chain/src/main/feature/feature.xml +++ b/bundles/org.smarthomej.transform.chain/src/main/feature/feature.xml @@ -2,7 +2,6 @@ openhab-runtime-base - mvn:org.smarthomej.addons.bundles/org.smarthomej.commons/${project.version} mvn:org.smarthomej.addons.bundles/org.smarthomej.transform.chain/${project.version}