-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[http] enable UoM for number channels (#9601)
* add unit Signed-off-by: Jan N. Klug <[email protected]> * documentation an XML Signed-off-by: Jan N. Klug <[email protected]> * address review comments Signed-off-by: Jan N. Klug <[email protected]> * improvements Signed-off-by: Jan N. Klug <[email protected]> * improvements Signed-off-by: Jan N. Klug <[email protected]>
- Loading branch information
Showing
7 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...g.http/src/main/java/org/openhab/binding/http/internal/converter/NumberItemConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB 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.openhab.binding.http.internal.converter; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.http.internal.config.HttpChannelConfig; | ||
import org.openhab.binding.http.internal.transform.ValueTransformation; | ||
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; | ||
|
||
/** | ||
* 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<State> updateState, Consumer<Command> postCommand, | ||
@Nullable Consumer<String> sendHttpValue, ValueTransformation stateTransformations, | ||
ValueTransformation commandTransformations, HttpChannelConfig channelConfig) { | ||
super(updateState, postCommand, sendHttpValue, stateTransformations, commandTransformations, channelConfig); | ||
} | ||
|
||
@Override | ||
protected @Nullable Command toCommand(String value) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected State toState(String value) { | ||
String trimmedValue = value.trim(); | ||
if (!trimmedValue.isEmpty()) { | ||
try { | ||
if (channelConfig.unit != null) { | ||
// we have a given unit - use that | ||
return new QuantityType<>(trimmedValue + " " + channelConfig.unit); | ||
} else { | ||
try { | ||
// try if we have a simple number | ||
return new DecimalType(trimmedValue); | ||
} catch (IllegalArgumentException e1) { | ||
// not a plain number, maybe with unit? | ||
return new QuantityType<>(trimmedValue); | ||
} | ||
} | ||
} catch (IllegalArgumentException e) { | ||
// finally failed | ||
} | ||
} | ||
return UnDefType.UNDEF; | ||
} | ||
|
||
@Override | ||
protected String toString(Command command) { | ||
return command.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters