forked from openhab/openhab-core
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set a default presentation pattern for String/Number/Datetime items
Default pattern for string item: %s Default pattern for datetime item: %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS Default pattern for number item: %.àf Default pattern for number with dimension item: %.àf %unit% Closes openhab#4071 Closes openhab#3835 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
- Loading branch information
Showing
4 changed files
with
157 additions
and
17 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
112 changes: 112 additions & 0 deletions
112
...rc/main/java/org/openhab/core/internal/items/DefaultStateDescriptionFragmentProvider.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,112 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.core.internal.items; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.library.CoreItemFactory; | ||
import org.openhab.core.types.StateDescriptionFragment; | ||
import org.openhab.core.types.StateDescriptionFragmentBuilder; | ||
import org.openhab.core.types.StateDescriptionFragmentProvider; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A {@link StateDescriptionFragment} provider providing a default state pattern for items of type String, | ||
* DateTime and Number (with or without dimension). | ||
* | ||
* @author Laurent Garnier - initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
@Component(service = { StateDescriptionFragmentProvider.class, | ||
DefaultStateDescriptionFragmentProvider.class }, immediate = true, property = { "service.ranking:Integer=-2" }) | ||
public class DefaultStateDescriptionFragmentProvider implements StateDescriptionFragmentProvider { | ||
|
||
private static final StateDescriptionFragment DEFAULT_STRING = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%s").build(); | ||
private static final StateDescriptionFragment DEFAULT_DATETIME = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS").build(); | ||
private static final StateDescriptionFragment DEFAULT_NUMBER = StateDescriptionFragmentBuilder.create() | ||
.withPattern("%.0f").build(); | ||
private static final StateDescriptionFragment DEFAULT_NUMBER_WITH_DIMENSION = StateDescriptionFragmentBuilder | ||
.create().withPattern("%.0f %unit%").build(); | ||
|
||
private final Logger logger = LoggerFactory.getLogger(DefaultStateDescriptionFragmentProvider.class); | ||
|
||
private final Map<String, StateDescriptionFragment> stateDescriptionFragments = new ConcurrentHashMap<>(); | ||
|
||
private Integer rank = -2; // takes less precedence over all other providers | ||
|
||
@Activate | ||
public DefaultStateDescriptionFragmentProvider(Map<String, Object> properties) { | ||
Object serviceRanking = properties.get(Constants.SERVICE_RANKING); | ||
if (serviceRanking instanceof Integer) { | ||
rank = (Integer) serviceRanking; | ||
} | ||
} | ||
|
||
@Deactivate | ||
protected void deactivate() { | ||
removeAllItems(); | ||
} | ||
|
||
public void addItem(String itemName, String itemType) { | ||
logger.debug("DefaultStateDescriptionFragmentProvider addItem {} {}", itemName, itemType); | ||
if (itemType.startsWith(CoreItemFactory.NUMBER + ":")) { | ||
stateDescriptionFragments.put(itemName, DEFAULT_NUMBER_WITH_DIMENSION); | ||
} else { | ||
switch (itemType) { | ||
case CoreItemFactory.STRING: | ||
stateDescriptionFragments.put(itemName, DEFAULT_STRING); | ||
break; | ||
case CoreItemFactory.DATETIME: | ||
stateDescriptionFragments.put(itemName, DEFAULT_DATETIME); | ||
break; | ||
case CoreItemFactory.NUMBER: | ||
stateDescriptionFragments.put(itemName, DEFAULT_NUMBER); | ||
break; | ||
default: | ||
stateDescriptionFragments.remove(itemName); | ||
} | ||
} | ||
} | ||
|
||
public void removeItem(String itemName) { | ||
logger.debug("DefaultStateDescriptionFragmentProvider removeItem {}", itemName); | ||
stateDescriptionFragments.remove(itemName); | ||
} | ||
|
||
public void removeAllItems() { | ||
logger.debug("DefaultStateDescriptionFragmentProvider removeAllItems"); | ||
stateDescriptionFragments.clear(); | ||
} | ||
|
||
@Override | ||
public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) { | ||
return stateDescriptionFragments.get(itemName); | ||
} | ||
|
||
@Override | ||
public Integer getRank() { | ||
return rank; | ||
} | ||
} |
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