Example #1
0
  /** {@inheritDoc} */
  @Override
  public boolean invokeMiosAction(
      String itemName, String actionName, List<Entry<String, Object>> params) {
    try {
      logger.debug(
          "invokeMiosAction item {}, action {}, params {}",
          new Object[] {
            itemName, actionName, Integer.valueOf((params == null) ? 0 : params.size())
          });

      boolean sent = false;

      // Lookup the MiOS Unit name and property for this item
      String unitName = getMiosUnitName(itemName);

      MiosUnitConnector connector = getMiosConnector(unitName);
      if (connector == null) {
        logger.warn(
            "invokeMiosAction: Action call for item '{}' but no connector found for MiOS Unit '{}', ignoring",
            itemName,
            unitName);
        return false;
      }

      if (!connector.isConnected()) {
        logger.warn(
            "invokeMiosAction: Action call for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring",
            itemName,
            unitName);
        return false;
      }

      for (BindingProvider provider : providers) {
        if (provider instanceof MiosBindingProvider) {
          MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
          MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);

          if ((config != null) && (config instanceof DeviceBindingConfig)) {
            connector.invokeAction((DeviceBindingConfig) config, actionName, params);
            sent = true;
          } else {
            logger.error(
                "invokeMiosAction: Missing BindingConfig for item '{}', or not bound to a MiOS Device.",
                itemName);
          }
        }
      }

      return sent;
    } catch (Exception e) {
      logger.error("invokeMiosScene: Error handling command", e);
      return false;
    }
  }
Example #2
0
  /** {@inheritDoc} */
  @Override
  protected void internalReceiveCommand(String itemName, Command command) {
    try {
      logger.debug("internalReceiveCommand: itemName '{}', command '{}'", itemName, command);

      // Lookup the MiOS Unit name and property for this item
      String unitName = getMiosUnitName(itemName);

      MiosUnitConnector connector = getMiosConnector(unitName);
      if (connector == null) {
        logger.warn(
            "Received command ({}) for item '{}' but no connector found for MiOS Unit '{}', ignoring",
            new Object[] {command.toString(), itemName, unitName});
        return;
      }

      if (!connector.isConnected()) {
        logger.warn(
            "Received command ({}) for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring",
            new Object[] {command.toString(), itemName, unitName});
        return;
      }

      for (BindingProvider provider : providers) {
        if (provider instanceof MiosBindingProvider) {
          MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
          MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);

          if (config != null) {
            ItemRegistry reg = miosProvider.getItemRegistry();

            if (reg != null) {
              Item item = reg.getItem(config.getItemName());
              State state = item.getState();
              connector.invokeCommand(config, command, state);
            } else {
              logger.warn(
                  "internalReceiveCommand: Missing ItemRegistry for item '{}' command '{}'",
                  itemName,
                  command);
            }
          } else {
            logger.trace(
                "internalReceiveCommand: Missing BindingConfig for item '{}' command '{}'",
                itemName,
                command);
          }
        }
      }

    } catch (Exception e) {
      logger.error("Error handling command", e);
    }
  }
Example #3
0
  /**
   * Invoked by the OSGi Framework, once per instance, during the Binding deactivation process.
   *
   * <p>Internally this is used to close out any resources used by the MiOS Binding.
   *
   * <p>OSGi is configured to do this in OSGI-INF/activebinding.xml
   */
  @Override
  public void deactivate() {
    logger.debug(getName() + " deactivate()");

    // close any open connections
    for (MiosUnitConnector connector : connectors.values()) {
      if (connector.isConnected()) {
        connector.close();
      }
    }
  }
Example #4
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;
  }
Example #5
0
  /** {@inheritDoc} */
  @Override
  public boolean invokeMiosScene(String itemName) {
    try {
      logger.debug("invokeMiosScene item {}", itemName);

      boolean sent = false;

      // Lookup the MiOS Unit name and property for this item
      String unitName = getMiosUnitName(itemName);

      MiosUnitConnector connector = getMiosConnector(unitName);
      if (connector == null) {
        logger.warn(
            "invokeMiosScene: Scene call for item '{}' but no connector found for MiOS Unit '{}', ignoring",
            itemName,
            unitName);
        return false;
      }

      if (!connector.isConnected()) {
        logger.warn(
            "invokeMiosScene: Scene call for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring",
            itemName,
            unitName);
        return false;
      }

      for (BindingProvider provider : providers) {
        if (provider instanceof MiosBindingProvider) {
          MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider;
          MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName);

          if ((config != null) && (config instanceof SceneBindingConfig)) {
            connector.invokeScene((SceneBindingConfig) config);
            sent = true;
          } else {
            logger.error(
                "invokeMiosScene: Missing BindingConfig for item '{}', or not bound to a MiOS Scene.",
                itemName);
          }
        }
      }

      return sent;
    } catch (Exception e) {
      logger.error("invokeMiosScene: Error handling command", e);
      return false;
    }
  }