Search in sources :

Example 1 with JointSpaceBindingProvider

use of org.openhab.binding.jointspace.JointSpaceBindingProvider in project openhab1-addons by openhab.

the class JointSpaceBinding method internalReceiveCommand.

/**
     * @{inheritDoc Processes the commands and maps them to jointspace commands
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    if (itemName != null && !this.providers.isEmpty()) {
        JointSpaceBindingProvider provider = this.providers.iterator().next();
        if (provider == null) {
            logger.warn("Doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
            return;
        }
        logger.debug("Received command (item='{}', state='{}', class='{}')", new Object[] { itemName, command.toString(), command.getClass().toString() });
        String tvCommandString = null;
        // first check if we can translate the command directly
        tvCommandString = provider.getTVCommand(itemName, command.toString());
        // if not try some special notations
        if (tvCommandString == null) {
            if (command instanceof HSBType) {
                tvCommandString = provider.getTVCommand(itemName, "HSB");
            } else if (command instanceof DecimalType) {
                tvCommandString = provider.getTVCommand(itemName, "DEC");
            }
            if (tvCommandString == null) {
                tvCommandString = provider.getTVCommand(itemName, "*");
            }
        }
        if (tvCommandString == null) {
            logger.warn("Unrecognized command \"{}\"", command.toString());
            return;
        }
        if (tvCommandString.contains("key")) {
            logger.debug("Found a Key command: " + tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            if (commandlist.length != 2) {
                logger.warn("wrong number of arguments for key command \"{}\". Should be key.X", tvCommandString);
                return;
            }
            String key = commandlist[1];
            sendTVCommand(key, ip + ":" + port);
        } else if (tvCommandString.contains("ambilight")) {
            logger.debug("Found an ambilight command: {}", tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            String[] layer = command2LayerString(tvCommandString);
            if (commandlist.length < 2) {
                logger.warn("wrong number of arguments for ambilight command \"{}\". Should be at least ambilight.color, ambilight.mode.X, etc...", tvCommandString);
                return;
            }
            if (commandlist[1].contains("color")) {
                setAmbilightColor(ip + ":" + port, command, layer);
            } else if (commandlist[1].contains("mode")) {
                if (commandlist.length != 3) {
                    logger.warn("wrong number of arguments for ambilight.mode command \"{}\". Should be ambilight.mode.internal, ambilight.mode.manual, ambilight.mode.expert", tvCommandString);
                    return;
                }
                setAmbilightMode(commandlist[2], ip + ":" + port);
            }
        } else if (tvCommandString.contains("volume")) {
            logger.debug("Found a Volume command: {}", tvCommandString);
            sendVolume(ip + ":" + port, command);
        } else if (tvCommandString.contains("source")) {
            logger.debug("Found a Source command: {}", tvCommandString);
            String[] commandlist = tvCommandString.split("\\.");
            if (commandlist.length < 2) {
                logger.warn("wrong number of arguments for source command \"{}\". Should be at least mode.X...", tvCommandString);
                return;
            }
            sendSource(ip + ":" + port, commandlist[1]);
        } else {
            logger.warn("Unrecognized tv command \"{}\". Only key.X or ambilight[].X is supported", tvCommandString);
            return;
        }
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) JointSpaceBindingProvider(org.openhab.binding.jointspace.JointSpaceBindingProvider)

Example 2 with JointSpaceBindingProvider

use of org.openhab.binding.jointspace.JointSpaceBindingProvider in project openhab1-addons by openhab.

the class JointSpaceBinding method execute.

/**
     * @{inheritDoc
     *
     *              Calls @see updateItemState() for all items with a "POLL"
     *              command in the configuration
     */
@Override
protected void execute() {
    logger.debug("Checking if host is available");
    boolean success = false;
    int timeout = 5000;
    try {
        success = Ping.checkVitality(ip, 0, timeout);
        if (success) {
            logger.debug("established connection [host '{}' port '{}' timeout '{}']", new Object[] { ip, 0, timeout });
            // After connection is possible, do the actual polling
            for (JointSpaceBindingProvider provider : providers) {
                for (String itemName : provider.getItemNames()) {
                    String tvcommand = provider.getTVCommand(itemName, "POLL");
                    if (tvcommand != null) {
                        updateItemState(itemName, tvcommand);
                    }
                }
            }
        } else {
            logger.debug("couldn't establish network connection [host '{}' port '{}' timeout '{}']", new Object[] { ip, 0, timeout });
        }
    } catch (SocketTimeoutException se) {
        logger.debug("timed out while connecting to host '{}' port '{}' timeout '{}'", new Object[] { ip, 0, timeout });
    } catch (IOException ioe) {
        logger.debug("couldn't establish network connection [host '{}' port '{}' timeout '{}']", new Object[] { ip, 0, timeout });
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) IOException(java.io.IOException) JointSpaceBindingProvider(org.openhab.binding.jointspace.JointSpaceBindingProvider)

Aggregations

JointSpaceBindingProvider (org.openhab.binding.jointspace.JointSpaceBindingProvider)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 JSONObject (org.json.simple.JSONObject)1 DecimalType (org.openhab.core.library.types.DecimalType)1 HSBType (org.openhab.core.library.types.HSBType)1