Search in sources :

Example 1 with DDWRTBindingProvider

use of org.openhab.binding.ddwrt.DDWRTBindingProvider in project openhab1-addons by openhab.

the class DDWRTBinding method execute.

@Override
protected void execute() {
    logger.trace("execute");
    if (password == null) {
        return;
    } else if (StringUtils.isBlank(password)) {
        logger.error("Password mustn't be empty!");
        return;
    }
    try {
        TelnetClient client = null;
        for (DDWRTBindingProvider provider : providers) {
            for (String item : provider.getItemNames()) {
                String query = null;
                String type = provider.getType(item);
                if (queryMap.containsKey(type)) {
                    if (type.startsWith(DDWRTBindingProvider.TYPE_ROUTER_TYPE)) {
                        query = queryMap.get(type);
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_24) && !interface_24.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_24 + " | grep UP";
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_50) && !interface_50.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_50 + " | grep UP";
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_GUEST) && !interface_guest.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_guest + " | grep UP";
                    }
                } else {
                    continue;
                }
                if (query == null) {
                    continue;
                }
                logger.trace("execute query ({})  ({}) ({})", query, ip, username);
                if (client == null) {
                    client = new TelnetClient();
                    client.connect(ip);
                    if (username != null) {
                        receive(client);
                        send(client, username);
                    }
                    receive(client);
                    send(client, password);
                    receive(client);
                }
                send(client, query);
                String answer = receive(client);
                String[] lines = answer.split("\r\n");
                if (lines.length >= 2) {
                    answer = lines[1].trim();
                }
                Class<? extends Item> itemType = provider.getItemType(item);
                State state = null;
                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (lines.length > 2) {
                        if (lines[1].contains("UP")) {
                            state = OnOffType.ON;
                        } else {
                            state = OnOffType.OFF;
                        }
                    } else {
                        state = OnOffType.OFF;
                    }
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    state = new DecimalType(answer);
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(answer);
                }
                if (state != null) {
                    eventPublisher.postUpdate(item, state);
                }
            }
        }
        if (client != null) {
            client.disconnect();
        }
    } catch (Exception e) {
        logger.warn("Could not get item state ", e);
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) TelnetClient(org.apache.commons.net.telnet.TelnetClient) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) DDWRTBindingProvider(org.openhab.binding.ddwrt.DDWRTBindingProvider) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 2 with DDWRTBindingProvider

use of org.openhab.binding.ddwrt.DDWRTBindingProvider in project openhab1-addons by openhab.

the class DDWRTBinding method internalReceiveCommand.

@Override
public void internalReceiveCommand(String itemName, Command command) {
    logger.trace("internalReceiveCommand");
    if (StringUtils.isNotBlank(password)) {
        String type = null;
        for (DDWRTBindingProvider provider : providers) {
            type = provider.getType(itemName);
            if (type != null) {
                break;
            }
        }
        logger.trace("DD-WRT type: {}", type);
        if (type == null) {
            return;
        }
        TelnetCommandThread thread = new TelnetCommandThread(type, command);
        thread.start();
    }
}
Also used : DDWRTBindingProvider(org.openhab.binding.ddwrt.DDWRTBindingProvider)

Aggregations

DDWRTBindingProvider (org.openhab.binding.ddwrt.DDWRTBindingProvider)2 IOException (java.io.IOException)1 TelnetClient (org.apache.commons.net.telnet.TelnetClient)1 NumberItem (org.openhab.core.library.items.NumberItem)1 DecimalType (org.openhab.core.library.types.DecimalType)1 StringType (org.openhab.core.library.types.StringType)1 State (org.openhab.core.types.State)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1