Beispiel #1
0
  /**
   * Maps thing into thing data transfer object (DTO).
   *
   * @param thing the thing
   * @return the thing DTO object
   */
  public static ThingDTO map(Thing thing) {
    List<ChannelDTO> channelDTOs = new ArrayList<>();
    for (Channel channel : thing.getChannels()) {
      ChannelDTO channelDTO = ChannelDTOMapper.map(channel);
      channelDTOs.add(channelDTO);
    }

    String thingTypeUID = thing.getThingTypeUID().getAsString();
    String thingUID = thing.getUID().toString();
    String bridgeUID = thing.getBridgeUID() != null ? thing.getBridgeUID().toString() : null;

    return new ThingDTO(
        thingTypeUID,
        thingUID,
        thing.getLabel(),
        bridgeUID,
        channelDTOs,
        thing.getConfiguration(),
        thing.getProperties(),
        thing.getLocation());
  }
  private int getFreeId() {
    int id = 1;

    List<Number> takenIds = new ArrayList<Number>();

    // Which ids are taken in Thing list of OpenHAB
    Collection<Thing> thingList = thingRegistry.getAll();
    Iterator<Thing> iterator = thingList.iterator();

    while (iterator.hasNext()) {
      Thing thing = iterator.next();
      Configuration conf = thing.getConfiguration();
      if (conf != null) {
        Object nodeIdobj = conf.get("nodeId");
        if (nodeIdobj != null) {
          int nodeId = Integer.parseInt(nodeIdobj.toString());
          takenIds.add(nodeId);
        }
      }
    }

    // Which ids are already given by the binding, but not yet in the thing list?
    Iterator<Number> iteratorGiven = givenIds.iterator();
    while (iteratorGiven.hasNext()) {
      takenIds.add(iteratorGiven.next());
    }

    // generate new id
    boolean foundId = false;
    while (!foundId) {
      Random rand = new Random(System.currentTimeMillis());
      int newId = rand.nextInt((254 - 1) + 1) + 1;
      if (!takenIds.contains(newId)) {
        id = newId;
        foundId = true;
      }
    }

    return id;
  }