org.eclipse.smarthome.core.items.Item.getAcceptedDataTypes()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(79)

本文整理了Java中org.eclipse.smarthome.core.items.Item.getAcceptedDataTypes()方法的一些代码示例,展示了Item.getAcceptedDataTypes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Item.getAcceptedDataTypes()方法的具体详情如下:
包路径:org.eclipse.smarthome.core.items.Item
类名称:Item
方法名:getAcceptedDataTypes

Item.getAcceptedDataTypes介绍

[英]This method provides a list of all data types that can be used to update the item state

Imagine e.g. a dimmer device: It's status could be 0%, 10%, 50%, 100%, but also OFF or ON and maybe UNDEFINED. So the accepted data types would be in this case PercentType, OnOffType and UnDefType

The order of data types denotes the order of preference. So in case a state needs to be converted in order to be accepted, it will be attempted to convert it to a type from top to bottom. Therefore the type with the least information loss should be on top of the list - in the example above the PercentType carries more information than the OnOffType, hence it is listed first.
[中]此方法提供可用于更新项目状态的所有数据类型的列表
想象一下,例如一个调光器设备:它的状态可能是0%、10%、50%、100%,但也可能是关闭或打开,并且可能是未定义的。因此,在本例中,接受的数据类型将是PercentType、OnOffType和UndeType
数据类型的顺序表示优先顺序。因此,如果一个状态需要转换才能被接受,它将尝试从上到下将其转换为一种类型。因此,信息丢失最少的类型应该位于列表的顶部-在上面的示例中,PercentType比OnOffType承载更多的信息,因此它被列在第一位。

代码示例

代码示例来源:origin: eclipse/smarthome

private boolean isAccepted(Item item, State state) {
  return item.getAcceptedDataTypes().contains(state.getClass());
}

代码示例来源:origin: eclipse/smarthome

/**
 * The accepted data types of a group item is the same as of the underlying base item.
 * If none is defined, the intersection of all sets of accepted data types of all group
 * members is used instead.
 *
 * @return the accepted data types of this group item
 */
@Override
@SuppressWarnings("unchecked")
public List<Class<? extends State>> getAcceptedDataTypes() {
  if (baseItem != null) {
    return baseItem.getAcceptedDataTypes();
  } else {
    List<Class<? extends State>> acceptedDataTypes = null;
    for (Item item : members) {
      if (acceptedDataTypes == null) {
        acceptedDataTypes = new ArrayList<>(item.getAcceptedDataTypes());
      } else {
        acceptedDataTypes.retainAll(item.getAcceptedDataTypes());
      }
    }
    return acceptedDataTypes == null ? Collections.unmodifiableList(Collections.EMPTY_LIST)
        : Collections.unmodifiableList(acceptedDataTypes);
  }
}

代码示例来源:origin: eclipse/smarthome

private static List<State> parseStates(@Nullable Item baseItem, String @Nullable [] params) {
  List<State> states = new ArrayList<State>();
  if (params == null || baseItem == null) {
    return states;
  }
  for (String param : params) {
    State state = TypeParser.parseState(baseItem.getAcceptedDataTypes(), param);
    if (state == null) {
      LoggerFactory.getLogger(ItemDTOMapper.class).warn(
          "State '{}' is not valid for a group item with base type '{}'",
          new Object[] { param, baseItem.getType() });
      states.clear();
      break;
    } else {
      states.add(state);
    }
  }
  return states;
}

代码示例来源:origin: eclipse/smarthome

for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
  State convertedState = state.as(acceptedType);
  if (convertedState != null) {

代码示例来源:origin: openhab/openhab-core

private boolean isAccepted(Item item, State state) {
  return item.getAcceptedDataTypes().contains(state.getClass());
}

代码示例来源:origin: openhab/openhab-core

private static <T extends State> List<String> getAcceptedDataTypeNames(Item item) {
  return item.getAcceptedDataTypes().stream().map(t -> t.getSimpleName()).collect(Collectors.toList());
}

代码示例来源:origin: openhab/openhab-core

/**
 * The accepted data types of a group item is the same as of the underlying base item.
 * If none is defined, the intersection of all sets of accepted data types of all group
 * members is used instead.
 *
 * @return the accepted data types of this group item
 */
@Override
@SuppressWarnings("unchecked")
public List<Class<? extends State>> getAcceptedDataTypes() {
  if (baseItem != null) {
    return baseItem.getAcceptedDataTypes();
  } else {
    List<Class<? extends State>> acceptedDataTypes = null;
    for (Item item : members) {
      if (acceptedDataTypes == null) {
        acceptedDataTypes = new ArrayList<>(item.getAcceptedDataTypes());
      } else {
        acceptedDataTypes.retainAll(item.getAcceptedDataTypes());
      }
    }
    return acceptedDataTypes == null ? Collections.unmodifiableList(Collections.EMPTY_LIST)
        : Collections.unmodifiableList(acceptedDataTypes);
  }
}

代码示例来源:origin: openhab/openhab-core

private boolean isAcceptedState(State newState, Item item) {
  boolean isAccepted = false;
  if (item.getAcceptedDataTypes().contains(newState.getClass())) {
    isAccepted = true;
  } else {
    // Look for class hierarchy
    for (Class<?> state : item.getAcceptedDataTypes()) {
      try {
        if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newState.getClass())) {
          isAccepted = true;
          break;
        }
      } catch (InstantiationException e) {
        logger.warn("InstantiationException on {}", e.getMessage(), e); // Should never happen
      } catch (IllegalAccessException e) {
        logger.warn("IllegalAccessException on {}", e.getMessage(), e); // Should never happen
      }
    }
  }
  return isAccepted;
}

代码示例来源:origin: openhab/openhab-core

private static List<State> parseStates(@Nullable Item baseItem, String @Nullable [] params) {
  List<State> states = new ArrayList<State>();
  if (params == null || baseItem == null) {
    return states;
  }
  for (String param : params) {
    State state = TypeParser.parseState(baseItem.getAcceptedDataTypes(), param);
    if (state == null) {
      LoggerFactory.getLogger(ItemDTOMapper.class).warn(
          "State '{}' is not valid for a group item with base type '{}'",
          new Object[] { param, baseItem.getType() });
      states.clear();
      break;
    } else {
      states.add(state);
    }
  }
  return states;
}

代码示例来源:origin: openhab/openhab-core

private synchronized void calculateAcceptedTypes() {
  acceptedCommandTypeMap.clear();
  acceptedStateTypeMap.clear();
  for (ItemFactory itemFactory : itemFactories) {
    for (String itemTypeName : itemFactory.getSupportedItemTypes()) {
      Item item = itemFactory.createItem(itemTypeName, "tmp");
      if (item != null) {
        acceptedCommandTypeMap.put(itemTypeName, item.getAcceptedCommandTypes());
        acceptedStateTypeMap.put(itemTypeName, item.getAcceptedDataTypes());
      } else {
        logger.error("Item factory {} suggested it can create items of type {} but returned null",
            itemFactory, itemTypeName);
      }
    }
  }
}

代码示例来源:origin: org.eclipse.smarthome.automation/org.eclipse.smarthome.automation.module.script.defaultscope

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public Object postUpdate(String itemName, String stateString) {
  if (eventPublisher != null && itemRegistry != null) {
    try {
      Item item = itemRegistry.getItem(itemName);
      State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
      eventPublisher.post(ItemEventFactory.createStateEvent(itemName, state));
    } catch (ItemNotFoundException e) {
      LoggerFactory.getLogger(ScriptBusEvent.class).warn("Item '{}' does not exist.", itemName);
    }
  }
  return null;
}

代码示例来源:origin: openhab/openhab-core

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public Object postUpdate(String itemName, String stateString) {
  if (eventPublisher != null && itemRegistry != null) {
    try {
      Item item = itemRegistry.getItem(itemName);
      State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
      eventPublisher.post(ItemEventFactory.createStateEvent(itemName, state));
    } catch (ItemNotFoundException e) {
      LoggerFactory.getLogger(ScriptBusEvent.class).warn("Item '{}' does not exist.", itemName);
    }
  }
  return null;
}

代码示例来源:origin: openhab/openhab-core

State state = TypeParser.parseState(item.getAcceptedDataTypes(), value);

代码示例来源:origin: openhab/openhab-core

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
static public Object postUpdate(String itemName, String stateString) {
  ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
  EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
  if (publisher != null && registry != null) {
    try {
      Item item = registry.getItem(itemName);
      State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
      if (state != null) {
        publisher.post(ItemEventFactory.createStateEvent(itemName, state));
      } else {
        LoggerFactory.getLogger(BusEvent.class).warn(
            "Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName,
            getAcceptedDataTypeNames(item));
      }
    } catch (ItemNotFoundException e) {
      LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
    }
  }
  return null;
}

代码示例来源:origin: openhab/openhab-core

State state = TypeParser.parseState(item.getAcceptedDataTypes(), value);
if (state == null) {

代码示例来源:origin: org.eclipse.smarthome.ui/org.eclipse.smarthome.ui

if (i.getAcceptedDataTypes().contains(PercentType.class)) {
  returnState = i.getStateAs(PercentType.class);
} else {

代码示例来源:origin: openhab/openhab-core

for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
  State convertedState = state.as(acceptedType);
  if (convertedState != null) {

代码示例来源:origin: openhab/openhab-core

if (i.getAcceptedDataTypes().contains(PercentType.class)) {
  returnState = itemState.as(PercentType.class);
} else {

代码示例来源:origin: openhab/openhab-core

case UPDATE:
  if (newType instanceof State) {
    List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
    final State state = (State) newType;
    internalGetUpdateRules(item.getName(), false, acceptedDataTypes, state, result);
case CHANGE:
  if (newType instanceof State && oldType instanceof State) {
    List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
    final State newState = (State) newType;
    final State oldState = (State) oldType;

代码示例来源:origin: openhab/openhab-core

if (args.length > 1) {
  String stateName = args[1];
  State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateName);
  if (state != null) {
    eventPublisher.post(ItemEventFactory.createStateEvent(item.getName(), state));
    console.println("Error: State '" + stateName + "' is not valid for item '" + itemName + "'");
    console.print("Valid data types are: ( ");
    for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
      console.print(acceptedType.getSimpleName() + " ");

相关文章