本文整理汇总了Java中org.openhab.core.items.Item.getStateAs方法的典型用法代码示例。如果您正苦于以下问题:Java Item.getStateAs方法的具体用法?Java Item.getStateAs怎么用?Java Item.getStateAs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openhab.core.items.Item
的用法示例。
在下文中一共展示了Item.getStateAs方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculate
import org.openhab.core.items.Item; //导入方法依赖的package包/类
/**
* @{inheritDoc
*/
public State calculate(List<Item> items) {
BigDecimal sum = BigDecimal.ZERO;
int count = 0;
if(items!=null) {
for(Item item : items) {
DecimalType itemState = (DecimalType) item.getStateAs(DecimalType.class);
if(itemState!=null) {
sum = sum.add(itemState.toBigDecimal());
count++;
}
}
}
if(count>0) {
return new DecimalType(sum.divide(new BigDecimal(count), RoundingMode.HALF_UP));
} else {
return UnDefType.UNDEF;
}
}
示例2: store
import org.openhab.core.items.Item; //导入方法依赖的package包/类
@Override
public void store(Item item, String alias) {
if (item.getState() instanceof UnDefType)
return;
if (alias == null)
alias = item.getName();
logger.debug("store called for {}", alias);
State state = item.getState();
if (item instanceof DimmerItem || item instanceof RollershutterItem) {
state = item.getStateAs(PercentType.class);
} else if (item instanceof ColorItem) {
state = item.getStateAs(HSBType.class);
}
MapDBItem mItem = new MapDBItem();
mItem.setName(alias);
mItem.setState(state);
mItem.setTimestamp(new Date());
MapDBItem oldItem = map.put(alias, mItem);
if (!commitSameState) {
if (oldItem != null) {
if (!oldItem.getState().toString().equals(state.toString())) {
needsCommit = true;
}
}
}
logger.debug("Stored '{}' with state '{}' in mapdb database", alias,
state.toString());
}
示例3: store
import org.openhab.core.items.Item; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void store(Item item, String alias) {
if (item.getState() instanceof UnDefType) {
return;
}
if (!isProperlyConfigured) {
logger.warn("Configuration for influxdb not yet loaded or broken.");
return;
}
if (!isConnected()) {
logger.warn("InfluxDB is not yet connected");
return;
}
String realName = item.getName();
String name = (alias != null) ? alias : realName;
State state = null;
if (item instanceof DimmerItem || item instanceof RollershutterItem) {
state = item.getStateAs(PercentType.class);
} else if (item instanceof ColorItem) {
state = item.getStateAs(HSBType.class);
} else {
// All other items should return the best format by default
state = item.getState();
}
Object value = stateToObject(state);
logger.trace("storing {} in influxdb {}", name, value);
// For now time is calculated by influxdb, may be this should be configurable?
Serie serie = new Serie.Builder(name)
.columns(VALUE_COLUMN_NAME)
.values(value)
.build();
// serie.setColumns(new String[] {"time", VALUE_COLUMN_NAME});
// Object[] point = new Object[] {System.currentTimeMillis(), value};
try {
influxDB.write(dbName, TimeUnit.MILLISECONDS, serie);
} catch (RuntimeException e) {
logger.error("storing failed with exception for item: {}", name);
handleDatabaseException(e);
}
}
示例4: formatLabel
import org.openhab.core.items.Item; //导入方法依赖的package包/类
private String formatLabel(Item item, String itemName, String label) {
// now insert the value, if the state is a string or decimal value and
// there is some formatting pattern defined in the label
// (i.e. it contains at least a %)
if (itemName != null && label.contains("[")) {
int indexOpenBracket = label.indexOf("[");
int indexCloseBracket = label.indexOf("]");
State state = null;
String formatPattern = label.substring(indexOpenBracket + 1, indexCloseBracket);
// TODO: TEE: we should find a more generic solution here! When
// using indexes in formatString this 'contains' will fail again
// and will cause an
// 'java.util.IllegalFormatConversionException:
// d != java.lang.String' later on when trying to format a
// String
// as %d (number).
if (item != null) {
if (label.contains("%d")) {
// a number is requested
state = item.getState();
if (!(state instanceof DecimalType)) {
state = item.getStateAs(DecimalType.class);
}
} else {
state = item.getState();
}
}
if (state == null || state instanceof UnDefType) {
formatPattern = formatUndefined(formatPattern);
} else if (state instanceof Type) {
// The following exception handling has been added to work
// around a Java bug with formatting
// numbers. See http://bugs.sun.com/view_bug.do?bug_id=6476425
// Without this catch, the whole sitemap, or page can not be
// displayed!
// This also handles IllegalFormatConverionException, which is a
// subclass of IllegalArgument.
try {
formatPattern = ((Type) state).format(formatPattern);
} catch (IllegalArgumentException e) {
logger.warn("Exception while formatting value '{}' of item {} with format '{}': {}", state,
itemName, formatPattern, e);
formatPattern = new String("Err");
}
}
label = label.substring(0, indexOpenBracket + 1) + formatPattern + label.substring(indexCloseBracket);
}
label = transform(label);
return label;
}