Search in sources :

Example 1 with FritzboxBindingProvider

use of org.openhab.binding.fritzbox.FritzboxBindingProvider in project openhab1-addons by openhab.

the class FritzboxBinding method internalReceiveCommand.

@Override
public void internalReceiveCommand(String itemName, Command command) {
    if (password != null && !password.isEmpty()) {
        String type = null;
        for (FritzboxBindingProvider provider : providers) {
            type = provider.getType(itemName);
            if (type != null) {
                break;
            }
        }
        logger.info("Fritzbox type: {}", type);
        if (type == null) {
            return;
        }
        TelnetCommandThread thread = new TelnetCommandThread(type, command);
        thread.start();
    }
}
Also used : FritzboxBindingProvider(org.openhab.binding.fritzbox.FritzboxBindingProvider)

Example 2 with FritzboxBindingProvider

use of org.openhab.binding.fritzbox.FritzboxBindingProvider in project openhab1-addons by openhab.

the class FritzboxBinding method execute.

@Override
protected void execute() {
    if (password == null) {
        return;
    } else if (password.trim().isEmpty()) {
        return;
    }
    try {
        TelnetClient client = null;
        for (FritzboxBindingProvider provider : providers) {
            for (String item : provider.getItemNames()) {
                String query = null;
                String type = provider.getType(item);
                if (queryMap.containsKey(type)) {
                    query = queryMap.get(type);
                } else if (type.startsWith("tam")) {
                    query = "ctlmgr_ctl r tam settings/" + type.toUpperCase() + "/Active";
                } else if (type.startsWith("query")) {
                    query = type.substring(type.indexOf(":") + 1).trim();
                } else {
                    continue;
                }
                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);
                org.openhab.core.types.State state = null;
                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (answer.equals("1")) {
                        state = OnOffType.ON;
                    } 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 : FritzboxBindingProvider(org.openhab.binding.fritzbox.FritzboxBindingProvider) NumberItem(org.openhab.core.library.items.NumberItem) TelnetClient(org.apache.commons.net.telnet.TelnetClient) StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) ConfigurationException(org.osgi.service.cm.ConfigurationException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException)

Aggregations

FritzboxBindingProvider (org.openhab.binding.fritzbox.FritzboxBindingProvider)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 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerException (org.quartz.SchedulerException)1