示例#1
0
  private MiosUnitConnector getMiosConnector(String unitName) {
    logger.trace("getMiosConnector: start unitName '{}'", unitName);

    // sanity check
    if (unitName == null) {
      return null;
    }

    // check if the connector for this unit already exists
    MiosUnitConnector connector = connectors.get(unitName);
    if (connector != null) {
      return connector;
    }

    MiosUnit miosUnit;

    // NOTE: We deviate from the XBMC Binding, in that we only accept
    // "names" presented in the openHAB configuration files.

    // check if we have been initialized yet - can't process
    // named units until we have read the binding config.
    if (nameUnitMapper == null) {
      logger.trace(
          "Attempting to access the named MiOS Unit '{}' before the binding config has been loaded",
          unitName);
      return null;
    }

    miosUnit = nameUnitMapper.get(unitName);

    // Check this Unit name exists in our config
    if (miosUnit == null) {
      logger.error("Named MiOS Unit '{}' does not exist in the binding config", unitName);
      return null;
    }

    // create a new connection handler
    logger.debug("Creating new MiosConnector for '{}' on {}", unitName, miosUnit.getHostname());
    connector = new MiosUnitConnector(miosUnit, this);
    connectors.put(unitName, connector);

    // attempt to open the connection straight away
    try {
      connector.open();
    } catch (Exception e) {
      logger.error("Connection failed for '{}' on {}", unitName, miosUnit.getHostname());
    }

    return connector;
  }