当前位置: 首页>>代码示例>>Java>>正文


Java Item.getStateAs方法代码示例

本文整理汇总了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;
	}
}
 
开发者ID:Neulinet,项目名称:Zoo,代码行数:22,代码来源:ArithmeticGroupFunction.java

示例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());
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:34,代码来源:MapDBPersistenceService.java

示例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);
  }
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:50,代码来源:InfluxDBPersistenceService.java

示例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;
}
 
开发者ID:andrey-desman,项目名称:openhab-hdl,代码行数:57,代码来源:ItemUIRegistryImpl.java


注:本文中的org.openhab.core.items.Item.getStateAs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。