Search in sources :

Example 36 with StringType

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

the class EcobeeBinding method createState.

/**
     * Creates an openHAB {@link State} in accordance to the class of the given {@code propertyValue}. Currently
     * {@link Date}, {@link BigDecimal}, {@link Temperature} and {@link Boolean} are handled explicitly. All other
     * {@code dataTypes} are mapped to {@link StringType}.
     * <p>
     * If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be returned.
     *
     * Copied/adapted from the Koubachi binding.
     *
     * @param propertyValue
     *
     * @return the new {@link State} in accordance with {@code dataType}. Will never be {@code null}.
     */
private State createState(Object propertyValue) {
    if (propertyValue == null) {
        return UnDefType.NULL;
    }
    Class<?> dataType = propertyValue.getClass();
    if (Date.class.isAssignableFrom(dataType)) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) propertyValue);
        return new DateTimeType(calendar);
    } else if (Integer.class.isAssignableFrom(dataType)) {
        return new DecimalType((Integer) propertyValue);
    } else if (BigDecimal.class.isAssignableFrom(dataType)) {
        return new DecimalType((BigDecimal) propertyValue);
    } else if (Boolean.class.isAssignableFrom(dataType)) {
        if ((Boolean) propertyValue) {
            return OnOffType.ON;
        } else {
            return OnOffType.OFF;
        }
    } else if (Temperature.class.isAssignableFrom(dataType)) {
        return new DecimalType(((Temperature) propertyValue).toLocalTemperature());
    } else if (State.class.isAssignableFrom(dataType)) {
        return (State) propertyValue;
    } else {
        return new StringType(propertyValue.toString());
    }
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) Calendar(java.util.Calendar) DecimalType(org.openhab.core.library.types.DecimalType)

Example 37 with StringType

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

the class LightwaveRfBindingFunctionalTest method testWifiLinkStatusReceived.

@Test
public void testWifiLinkStatusReceived() throws Exception {
    String message = "*!{\"trans\":452,\"mac\":\"ab:cd:ef\",\"time\":1447712274,\"type\":\"hub\",\"prod\":\"wfl\",\"fw\":\"U2.91Y\"," + "\"uptime\":1386309,\"timeZone\":0,\"lat\":52.48,\"long\":-87.89,\"duskTime\":1447690400," + "\"dawnTime\":1447659083,\"tmrs\":0,\"evns\":1,\"run\":0,\"macs\":8,\"ip\":\"192.168.0.1\",\"devs\":0}";
    List<ItemConfigAndExpectedState> itemConfigAndExpectedStates = new LinkedList<ItemConfigAndExpectedState>();
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_IP"), "serial=wifilink,type=WIFILINK_IP", new StringType("192.168.0.1")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_FIRMWARE"), "serial=wifilink,type=WIFILINK_FIRMWARE", new StringType("U2.91Y")));
    Calendar duskCal = Calendar.getInstance();
    duskCal.setTime(new Date(1447690400000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("WIFILINK_DUSK_TIME"), "serial=wifilink,type=WIFILINK_DUSK_TIME", new DateTimeType(duskCal)));
    Calendar dawnCal = Calendar.getInstance();
    dawnCal.setTime(new Date(1447659083000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("WIFILINK_DAWN_TIME"), "serial=wifilink,type=WIFILINK_DAWN_TIME", new DateTimeType(dawnCal)));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("WIFILINK_UPTIME"), "serial=wifilink,type=WIFILINK_UPTIME", new DecimalType("1386309")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_LONGITUDE"), "serial=wifilink,type=WIFILINK_LONGITUDE", new StringType("-87.89")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_LATITUDE"), "serial=wifilink,type=WIFILINK_LATITUDE", new StringType("52.48")));
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1447712274000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("TIME"), "serial=wifilink,type=UPDATETIME", new DateTimeType(cal)));
    testReceivingACommandAndVerify(itemConfigAndExpectedStates, message);
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) 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) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 38 with StringType

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

the class VarVisualize method visualizationVarStatus.

/**
     * Visualization for {@link StringType} and {@link DecimalType}.
     * {@inheritDoc}
     */
@Override
public boolean visualizationVarStatus(ModStatusVar pchkInput, Command cmd, Item item, EventPublisher eventPublisher) {
    if (pchkInput.getLogicalSourceAddr().equals(this.addr) && pchkInput.getVar() == this.var) {
        if (item.getAcceptedDataTypes().contains(StringType.class)) {
            String value = pchkInput.getValue().toVarUnitString(this.unit, LcnDefs.Var.isLockableRegulatorSource(this.var), LcnDefs.Var.useLcnSpecialValues(this.var));
            eventPublisher.postUpdate(item.getName(), new StringType(value));
            return true;
        } else if (item.getAcceptedDataTypes().contains(DecimalType.class)) {
            eventPublisher.postUpdate(item.getName(), new DecimalType(pchkInput.getValue().toVarUnit(this.unit, LcnDefs.Var.isLockableRegulatorSource(this.var))));
            return true;
        }
    }
    return false;
}
Also used : StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType)

Example 39 with StringType

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

the class LcnGenericBindingProvider method processBindingConfiguration.

/**
     * Item processing for the LCN bindings.
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    Matcher matcher = PATTERN_BINDING_GENERAL.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException(bindingConfig + "' contains no valid binding!");
    }
    matcher.reset();
    LcnBindingConfig bc = new LcnBindingConfig(item);
    while (matcher.find()) {
        String binding = matcher.group(1);
        if (binding != null && !binding.trim().isEmpty()) {
            String openHabCmd = null;
            String connId, lcnTarget;
            Matcher openHabMatcher = PATTERN_BINDING_WITH_OPENHAB.matcher(binding);
            Matcher pureMatcher = PATTERN_BINDING_PURE.matcher(binding);
            if (openHabMatcher.matches()) {
                openHabCmd = openHabMatcher.group(1);
                connId = openHabMatcher.group(2);
                lcnTarget = openHabMatcher.group(3);
            } else if (pureMatcher.matches()) {
                connId = pureMatcher.group(1);
                lcnTarget = pureMatcher.group(2);
            } else {
                throw new BindingConfigParseException("Invalid binding configuration for " + binding + "!");
            }
            String lcnShort = resolveMappings(lcnTarget, openHabCmd);
            if (lcnShort == null || lcnShort.equals(openHabCmd)) {
                lcnShort = lcnTarget;
            }
            Command cmd = openHabCmd == null ? TypeParser.parseCommand(new StringItem("").getAcceptedCommandTypes(), "") : openHabCmd.equals("%i") ? new StringType("%i") : TypeParser.parseCommand(item.getAcceptedCommandTypes(), openHabCmd);
            bc.add(new LcnBindingConfig.Mapping(cmd, connId, lcnShort));
        }
    }
    // Finished
    this.addBindingConfig(item, bc);
    for (LcnAddrMod addr : bc.getRelatedModules()) {
        HashSet<String> l = this.itemNamesByModulCache.get(addr);
        if (l == null) {
            l = new HashSet<String>();
            this.itemNamesByModulCache.put(addr, l);
        }
        l.add(item.getName());
    }
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) StringItem(org.openhab.core.library.items.StringItem) LcnAddrMod(org.openhab.binding.lcn.common.LcnAddrMod)

Example 40 with StringType

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

the class LightwaveRfHeatingInfoResponse method getState.

@Override
public State getState(LightwaveRfType type) {
    switch(type) {
        case HEATING_BATTERY:
            return new DecimalType(getBatteryLevel());
        case SIGNAL:
            return new DecimalType(getSignal());
        case HEATING_CURRENT_TEMP:
            return new DecimalType(getCurrentTemperature());
        case HEATING_SET_TEMP:
            return new DecimalType(getCurrentTargetTemperature());
        case HEATING_MODE:
            return new StringType(getState());
        case HEATING_OUTPUT:
            return new PercentType(getOutput());
        case UPDATETIME:
            Calendar cal = Calendar.getInstance();
            cal.setTime(getTime());
            return new DateTimeType(cal);
        case VERSION:
            return new StringType(getVersion());
        default:
            return null;
    }
}
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) PercentType(org.openhab.core.library.types.PercentType)

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