Search in sources :

Example 51 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class Zone method handleEvent.

/**
     * {@inheritDoc}
     */
@Override
public void handleEvent(Item item, DSCAlarmBindingConfig config, EventPublisher publisher, DSCAlarmEvent event) {
    int apiCode = -1;
    APIMessage apiMessage = null;
    String str = "Status Unknown!";
    if (event != null) {
        apiMessage = event.getAPIMessage();
        apiCode = Integer.parseInt(apiMessage.getAPICode());
        str = apiMessage.getAPIName();
        logger.debug("handleEvent(): Zone Item Name: {}", item.getName());
        if (config != null) {
            if (config.getDSCAlarmItemType() != null) {
                switch(config.getDSCAlarmItemType()) {
                    case ZONE_ALARM_STATUS:
                        publisher.postUpdate(item.getName(), new StringType(str));
                        break;
                    case ZONE_TAMPER_STATUS:
                        publisher.postUpdate(item.getName(), new StringType(str));
                        break;
                    case ZONE_FAULT_STATUS:
                        publisher.postUpdate(item.getName(), new StringType(str));
                        break;
                    case ZONE_GENERAL_STATUS:
                        publisher.postUpdate(item.getName(), (apiCode == 609) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
                        break;
                    default:
                        logger.debug("handleEvent(): Zone item not updated.");
                        break;
                }
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) APIMessage(org.openhab.binding.dscalarm.internal.protocol.APIMessage)

Example 52 with StringType

use of org.openhab.core.library.types.StringType 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 53 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class DenonConnector method processUpdate.

/**
     * This method tries to parse information received over the telnet connection.
     * It's quite unreliable. Some chars go missing or turn into other chars. That's
     * why each command is validated using a regex.
     * 
     * @param commandString The received command (one line)
     */
private void processUpdate(String commandString) {
    if (COMMAND_PATTERN.matcher(commandString).matches()) {
        /*
             * This splits the commandString into the command and the parameter. SICD
             * for example has SI as the command and CD as the parameter.
             */
        String command = commandString.substring(0, 2);
        String value = commandString.substring(2, commandString.length()).trim();
        // Secondary zone commands with a parameter
        if (ZONE_SUBCOMMAND_PATTERN.matcher(commandString).matches()) {
            command = commandString.substring(0, 4);
            value = commandString.substring(4, commandString.length()).trim();
        }
        logger.debug("Command: {}, value: {}", command, value);
        if (value.equals("ON") || value.equals("OFF")) {
            sendUpdate(command, OnOffType.valueOf(value));
        } else if (value.equals("STANDBY")) {
            sendUpdate(command, OnOffType.OFF);
        } else if (StringUtils.isNumeric(value)) {
            PercentType percent = new PercentType(fromDenonValue(value));
            command = translateVolumeCommand(command);
            sendUpdate(command, percent);
        } else if (command.equals("SI")) {
            sendUpdate(DenonProperty.INPUT.getCode(), new StringType(value));
            sendUpdate(commandString, OnOffType.ON);
        } else if (command.equals("MS")) {
            sendUpdate(DenonProperty.SURROUND_MODE.getCode(), new StringType(value));
        } else if (command.equals("NS")) {
            processTitleCommand(command, value);
        }
    } else {
        logger.debug("Invalid command: " + commandString);
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) PercentType(org.openhab.core.library.types.PercentType)

Example 54 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class DenonConnector method updateMainZone.

private void updateMainZone() {
    String url = statusUrl + URL_ZONE_MAIN;
    logger.trace("Refreshing URL: {}", url);
    ZoneStatus mainZone = getDocument(url, ZoneStatus.class);
    if (mainZone != null) {
        stateCache.put(DenonProperty.INPUT.getCode(), new StringType(mainZone.getInputFuncSelect().getValue()));
        stateCache.put("SI" + mainZone.getInputFuncSelect().getValue(), OnOffType.ON);
        stateCache.put(DenonProperty.MASTER_VOLUME.getCode(), new PercentType(mainZone.getMasterVolume().getValue()));
        stateCache.put(DenonProperty.POWER_MAINZONE.getCode(), mainZone.getPower().getValue() ? OnOffType.ON : OnOffType.OFF);
        stateCache.put(DenonProperty.MUTE.getCode(), mainZone.getMute().getValue() ? OnOffType.ON : OnOffType.OFF);
        if (mainZone.getSurrMode() == null) {
            logger.debug("Unable to get the SURROUND_MODE. MainZone update may not be correct.");
        } else {
            stateCache.put(DenonProperty.SURROUND_MODE.getCode(), new StringType(mainZone.getSurrMode().getValue()));
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) ZoneStatus(org.openhab.binding.denon.internal.communication.entities.ZoneStatus) PercentType(org.openhab.core.library.types.PercentType)

Example 55 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class AnelDataParser method parseData.

/**
     * Parse data package from Anel NET-PwrCtrl device and update
     * {@link AnelState}. For all changes, {@link AnelCommandType}s are created
     * and returned. The expected format is as follows, separated with colons:
     * <ul>
     * <li>0. 'NET-PwrCtrl'
     * <li>1. &lt;name&gt; (may contain trailing spaces)
     * <li>2. &lt;ip&gt;
     * <li>3. &lt;netmask&gt;
     * <li>4. &lt;gateway&gt;
     * <li>5. &lt;mac addess&gt;
     * <li>6-13. &lt;name of switch n&gt;,&lt;state 0 or 1&gt;
     * <li>14. &lt;locked switches&gt;
     * <li>15. &lt;http port&gt;
     * <li>16-23. &lt;name of IO n&gt;,&lt;direction in=1 or out=0&gt;,&lt;state
     * 0 or 1&gt;
     * <li>24. &lt;temperature&gt;
     * <li>25. &lt;firmware version&gt; (may contain trailing line break)
     * </ul>
     * Source: <a
     * href="http://www.anel-elektronik.de/forum_new/viewtopic.php?f=16&t=207"
     * >Anel forum (German)</a>
     * <p>
     * It turned out that the HOME variant has a different format which contains
     * only the first 16 segments. If that is the case, the remaining fields of
     * {@link AnelState} are simply ignored (and remain unset).
     * </p>
     * Source: <a href="https://github.com/openhab/openhab/issues/2068">Issue
     * 2068</a>
     * 
     * @param data
     *            The data received from {@link AnelUDPConnector}.
     * @param state
     *            The internal (cached) state of the device.
     * @return A map of commands to the new openHAB {@link State}s.
     * @throws Exception
     *             If the data is invalid or corrupt.
     */
public static Map<AnelCommandType, State> parseData(byte[] data, AnelState state) throws Exception {
    final String string = new String(data);
    final String[] arr = string.split(":");
    if (arr.length != 28 && arr.length != 26 && arr.length != 16) {
        throw new IllegalArgumentException("Data with 16, 26, or 28 values expected but " + arr.length + " received: " + string);
    }
    if (!arr[0].equals("NET-PwrCtrl")) {
        throw new IllegalArgumentException("Data must start with 'NET-PwrCtrl' but it didn't: " + arr[0]);
    }
    if (!state.host.equals(arr[2]) && !state.host.equalsIgnoreCase(arr[1].trim())) {
        // this came from another device
        return Collections.emptyMap();
    }
    final Map<AnelCommandType, State> result = new LinkedHashMap<AnelCommandType, State>();
    // check for switch changes, update cached state, and prepare command if
    // needed
    final int locked = Integer.parseInt(arr[14]);
    for (int nr = 0; nr < 8; nr++) {
        final String[] swState = arr[6 + nr].split(",");
        if (swState.length == 2) {
            // expected format
            addCommand(state.switchName, nr, swState[0], "F" + (nr + 1) + "NAME", result);
            addCommand(state.switchState, nr, "1".equals(swState[1]), "F" + (nr + 1), result);
        } else {
            // unexpected format, set states to null
            addCommand(state.switchName, nr, null, "F" + (nr + 1) + "NAME", result);
            addCommand(state.switchState, nr, null, "F" + (nr + 1), result);
        }
        addCommand(state.switchLocked, nr, (locked & (1 << nr)) > 0, "F" + (nr + 1) + "LOCKED", result);
    }
    // IO and temperature is only available if array has length 24
    if (arr.length > 16) {
        // if needed
        for (int nr = 0; nr < 8; nr++) {
            final String[] ioState = arr[16 + nr].split(",");
            if (ioState.length == 3) {
                // expected format
                addCommand(state.ioName, nr, ioState[0], "IO" + (nr + 1) + "NAME", result);
                addCommand(state.ioIsInput, nr, "1".equals(ioState[1]), "IO" + (nr + 1) + "ISINPUT", result);
                addCommand(state.ioState, nr, "1".equals(ioState[2]), "IO" + (nr + 1), result);
            } else {
                // unexpected format, set states to null
                addCommand(state.ioName, nr, null, "IO" + (nr + 1) + "NAME", result);
                addCommand(state.ioIsInput, nr, null, "IO" + (nr + 1) + "ISINPUT", result);
                addCommand(state.ioState, nr, null, "IO" + (nr + 1), result);
            }
        }
        // example temperature string: '26.4°C'
        // '°' is caused by some different encoding, so cut last 2 chars
        final String temperature = arr[24].substring(0, arr[24].length() - 2);
        if (hasTemperaturChanged(state, temperature)) {
            result.put(AnelCommandType.TEMPERATURE, new DecimalType(temperature));
            state.temperature = temperature;
        }
    }
    // maybe the device's name changed?!
    final String name = arr[1];
    if (!name.equals(state.name)) {
        result.put(AnelCommandType.NAME, new StringType(name));
        state.name = name;
    }
    return result;
}
Also used : StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

StringType (org.openhab.core.library.types.StringType)90 DecimalType (org.openhab.core.library.types.DecimalType)69 State (org.openhab.core.types.State)30 DateTimeType (org.openhab.core.library.types.DateTimeType)28 PercentType (org.openhab.core.library.types.PercentType)27 NumberItem (org.openhab.core.library.items.NumberItem)25 Calendar (java.util.Calendar)23 StringItem (org.openhab.core.library.items.StringItem)18 OnOffType (org.openhab.core.library.types.OnOffType)15 SwitchItem (org.openhab.core.library.items.SwitchItem)12 ContactItem (org.openhab.core.library.items.ContactItem)10 DimmerItem (org.openhab.core.library.items.DimmerItem)10 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)8 HSBType (org.openhab.core.library.types.HSBType)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7