Search in sources :

Example 86 with StringType

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

the class AnelDataParser method addCommand.

private static <T> void addCommand(T[] cache, int index, T newValue, String commandType, Map<AnelCommandType, State> result) {
    if (newValue != null) {
        if (!newValue.equals(cache[index])) {
            final AnelCommandType cmd = AnelCommandType.getCommandType(commandType);
            final State state;
            if (newValue instanceof String) {
                state = new StringType((String) newValue);
            } else if (newValue instanceof Boolean) {
                state = (Boolean) newValue ? OnOffType.ON : OnOffType.OFF;
            } else {
                throw new UnsupportedOperationException("TODO: implement value to state conversion for: " + newValue.getClass().getCanonicalName());
            }
            result.put(cmd, state);
            cache[index] = newValue;
        }
    } else if (cache[index] != null) {
        result.put(AnelCommandType.getCommandType(commandType), UnDefType.UNDEF);
        cache[index] = null;
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State)

Example 87 with StringType

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

the class PlanetPublisher method publishValue.

/**
     * Publishes the item with the value, if the item is a OnOffType and the
     * value is a calendar, a job is scheduled.
     */
private void publishValue(Item item, Object value, AstroBindingConfig bindingConfig) {
    if (value == null) {
        context.getEventPublisher().postUpdate(item.getName(), UnDefType.UNDEF);
    } else if (value instanceof Calendar) {
        Calendar calendar = (Calendar) value;
        if (bindingConfig.getOffset() != 0) {
            calendar = (Calendar) calendar.clone();
            calendar.add(Calendar.MINUTE, bindingConfig.getOffset());
        }
        if (item.getAcceptedDataTypes().contains(DateTimeType.class)) {
            context.getEventPublisher().postUpdate(item.getName(), new DateTimeType(calendar));
        } else if (item.getAcceptedCommandTypes().contains(OnOffType.class)) {
            context.getJobScheduler().scheduleItem(calendar, item.getName());
        } else {
            logger.warn("Unsupported type for item {}, only DateTimeType and OnOffType supported!", item.getName());
        }
    } else if (value instanceof Number) {
        if (item.getAcceptedDataTypes().contains(DecimalType.class)) {
            BigDecimal decimalValue = new BigDecimal(value.toString()).setScale(2, RoundingMode.HALF_UP);
            context.getEventPublisher().postUpdate(item.getName(), new DecimalType(decimalValue));
        } else if (value instanceof Long && item.getAcceptedDataTypes().contains(StringType.class) && "duration".equals(bindingConfig.getProperty())) {
            // special case, transforming duration to minute:second string
            context.getEventPublisher().postUpdate(item.getName(), new StringType(durationToString((Long) value)));
        } else {
            logger.warn("Unsupported type for item {}, only DecimalType supported!", item.getName());
        }
    } else if (value instanceof String || value instanceof Enum) {
        if (item.getAcceptedDataTypes().contains(StringType.class)) {
            if (value instanceof Enum) {
                String enumValue = WordUtils.capitalizeFully(StringUtils.replace(value.toString(), "_", " "));
                context.getEventPublisher().postUpdate(item.getName(), new StringType(enumValue));
            } else {
                context.getEventPublisher().postUpdate(item.getName(), new StringType(value.toString()));
            }
        } else {
            logger.warn("Unsupported type for item {}, only String supported!", item.getName());
        }
    } else {
        logger.warn("Unsupported value type {}", value.getClass().getSimpleName());
    }
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) DecimalType(org.openhab.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Example 88 with StringType

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

the class AlarmDecoderBinding method setUnupdatedItemsToDefault.

/**
     * Since there is no way to poll, all items of unknown status are
     * simply assumed to be in the default (neutral) state. This method
     * is called when there are reasons to assume that there are no faults,
     * for instance because the alarm panel is in state READY.
     */
private void setUnupdatedItemsToDefault() {
    logger.trace("setting {} unupdated items to default", m_unupdatedItems.size());
    synchronized (m_unupdatedItems) {
        while (!m_unupdatedItems.isEmpty()) {
            // cannot use the config in the hash map, since it is null
            String itemName = m_unupdatedItems.keySet().iterator().next();
            ArrayList<AlarmDecoderBindingConfig> al = getItems(itemName);
            for (AlarmDecoderBindingConfig bc : al) {
                switch(bc.getMsgType()) {
                    case RFX:
                        if (bc.hasFeature("data")) {
                            updateItem(bc, new DecimalType(0));
                        } else if (bc.hasFeature("contact")) {
                            updateItem(bc, OpenClosedType.CLOSED);
                        }
                        break;
                    case EXP:
                    case REL:
                        updateItem(bc, OpenClosedType.CLOSED);
                        break;
                    case LRR:
                        updateItem(bc, new StringType(""));
                        break;
                    case INVALID:
                    default:
                        m_unupdatedItems.remove(itemName);
                        break;
                }
            }
        }
        m_unupdatedItems.clear();
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType)

Example 89 with StringType

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

the class FreeswitchBinding method endCallItemUpdate.

/**
     * update items on call end
     *
     * @param config
     */
private void endCallItemUpdate(FreeswitchBindingConfig config) {
    OnOffType activeState = OnOffType.OFF;
    ;
    CallType callType = (CallType) CallType.EMPTY;
    StringType callerId = StringType.EMPTY;
    /*
         * A channel has ended that has this item associated with it
         * We still need to check if this item is associated with other
         * channels.
         * We are going to iterate backwards to get the last added channel;
         */
    ListIterator<String> it = new ArrayList<String>(itemMap.keySet()).listIterator(itemMap.size());
    // if we get a match we will stop processing
    boolean match = false;
    while (it.hasPrevious()) {
        String uuid = it.previous();
        for (FreeswitchBindingConfig c : itemMap.get(uuid)) {
            if (c.getItemName().equals(config.getItemName())) {
                Channel channel = eventCache.get(uuid);
                activeState = OnOffType.ON;
                callType = channel.getCall();
                callerId = new StringType(String.format("%s : %s", channel.getEventHeader(CID_NAME), channel.getEventHeader(CID_NUMBER)));
                match = true;
                break;
            }
        }
        if (match) {
            break;
        }
    }
    if (config.getItemType().isAssignableFrom(SwitchItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), activeState);
    } else if (config.getItemType().isAssignableFrom(CallItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), callType);
    } else if (config.getItemType().isAssignableFrom(StringItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), callerId);
    } else {
        logger.warn("handleHangupCall - postUpdate for itemType '{}' is undefined", config.getItemName());
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) CallItem(org.openhab.library.tel.items.CallItem)

Example 90 with StringType

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

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