Search in sources :

Example 81 with StringType

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

the class ConverterTest method testValueListByString.

@Test
public void testValueListByString() throws Exception {
    StringTypeConverter converter = new StringTypeConverter();
    Assert.assertEquals(new StringType("0"), converter.convertFromBinding(getValueListVariable("0", "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("10"), converter.convertFromBinding(getValueListVariable("1", "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("50"), converter.convertFromBinding(getValueListVariable("5", "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("6"), converter.convertFromBinding(getValueListVariable("6", "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("10"), converter.convertFromBinding(getValueListVariable(1, "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("6"), converter.convertFromBinding(getValueListVariable(6, "0;10;20;30;40;50")));
    Assert.assertEquals(new StringType("two"), converter.convertFromBinding(getValueListVariable(1, "one;two")));
    Assert.assertEquals(new StringType("one"), converter.convertFromBinding(getValueListVariable(false, "one;two")));
    Assert.assertEquals(new StringType("two"), converter.convertFromBinding(getValueListVariable(true, "one;two")));
    Assert.assertEquals("0", converter.convertToBinding(new StringType("0"), getValueListVariable("", "0;10;20;30;40;50")));
    Assert.assertEquals("1", converter.convertToBinding(new StringType("10"), getValueListVariable("", "0;10;20;30;40;50")));
    Assert.assertEquals("5", converter.convertToBinding(new StringType("50"), getValueListVariable("", "0;10;20;30;40;50")));
    Assert.assertEquals("2", converter.convertToBinding(new StringType("three"), getValueListVariable("", "one;two;three")));
    Assert.assertEquals("2", converter.convertToBinding(new StringType("three"), getValueListVariable("1", "one;two;three")));
    Assert.assertEquals(2, converter.convertToBinding(new StringType("three"), getValueListVariable(1, "one;two;three")));
    Assert.assertEquals(false, converter.convertToBinding(new StringType("one"), getValueListVariable(false, "one;two")));
    Assert.assertEquals(true, converter.convertToBinding(new StringType("two"), getValueListVariable(true, "one;two")));
}
Also used : StringType(org.openhab.core.library.types.StringType) StringTypeConverter(org.openhab.binding.homematic.internal.converter.state.StringTypeConverter) Test(org.junit.Test)

Example 82 with StringType

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

the class WindowHandleProfile method valueChanged.

@Override
public void valueChanged(ParameterAddress parameterAddress, Value valueObject) {
    Command command = null;
    if (valueObject.getValue().equals(Positions.DOWN.toString())) {
        command = new StringType("CLOSED");
    } else if (valueObject.getValue().equals(Positions.MIDDLE.toString())) {
        command = new StringType("OPEN");
    } else if (valueObject.getValue().equals(Positions.UP.toString())) {
        command = new StringType("AJAR");
    }
    postCommand(command);
}
Also used : Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType)

Example 83 with StringType

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

the class KM200Comm method getVirtualState.

/**
     * This function checks the virtual state of a service
     *
     */
public State getVirtualState(KM200CommObject object, Class<? extends Item> itemType, String service) {
    State state = null;
    String type = object.getServiceType();
    logger.debug("Check virtual state of: {} type: {} item: {}", service, type, itemType.getName());
    switch(type) {
        case "switchProgram":
            KM200SwitchProgramService sPService = ((KM200SwitchProgramService) device.serviceMap.get(object.getParent()).getValueParameter());
            String[] servicePath = service.split("/");
            String virtService = servicePath[servicePath.length - 1];
            if (virtService.equals("weekday")) {
                if (itemType.isAssignableFrom(StringItem.class)) {
                    String val = sPService.getActiveDay();
                    if (val == null) {
                        return null;
                    }
                    state = new StringType(val);
                } else {
                    logger.warn("Bindingtype not supported for day service: {}", itemType.getClass());
                    return null;
                }
            } else if (virtService.equals("nbrCycles")) {
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    Integer val = sPService.getNbrCycles();
                    if (val == null) {
                        return null;
                    }
                    state = new DecimalType(val);
                } else {
                    logger.warn("Bindingtype not supported for nbrCycles service: {}", itemType.getClass());
                    return null;
                }
            } else if (virtService.equals("cycle")) {
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    Integer val = sPService.getActiveCycle();
                    if (val == null) {
                        return null;
                    }
                    state = new DecimalType(val);
                } else {
                    logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
                    return null;
                }
            } else if (virtService.equals(sPService.getPositiveSwitch())) {
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    Integer val = sPService.getActivePositiveSwitch();
                    if (val == null) {
                        return null;
                    }
                    state = new DecimalType(val);
                } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
                    Integer val = sPService.getActivePositiveSwitch();
                    if (val == null) {
                        return null;
                    }
                    Calendar rightNow = Calendar.getInstance();
                    Integer hour = val % 60;
                    Integer minute = val - (hour * 60);
                    rightNow.set(Calendar.HOUR_OF_DAY, hour);
                    rightNow.set(Calendar.MINUTE, minute);
                    state = new DateTimeType(rightNow);
                } else {
                    logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
                    return null;
                }
            } else if (virtService.equals(sPService.getNegativeSwitch())) {
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    Integer val = sPService.getActiveNegativeSwitch();
                    if (val == null) {
                        return null;
                    }
                    state = new DecimalType(val);
                } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
                    Integer val = sPService.getActiveNegativeSwitch();
                    if (val == null) {
                        return null;
                    }
                    Calendar rightNow = Calendar.getInstance();
                    Integer hour = val % 60;
                    Integer minute = val - (hour * 60);
                    rightNow.set(Calendar.HOUR_OF_DAY, hour);
                    rightNow.set(Calendar.MINUTE, minute);
                    state = new DateTimeType(rightNow);
                } else {
                    logger.warn("Bindingtype not supported for cycle service: {}", itemType.getClass());
                    return null;
                }
            }
            break;
        case "errorList":
            KM200ErrorService eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
            String[] nServicePath = service.split("/");
            String nVirtService = nServicePath[nServicePath.length - 1];
            /* Go through the parameters and read the values */
            switch(nVirtService) {
                case "nbrErrors":
                    if (itemType.isAssignableFrom(NumberItem.class)) {
                        Integer val = eService.getNbrErrors();
                        if (val == null) {
                            return null;
                        }
                        state = new DecimalType(val);
                    } else {
                        logger.warn("Bindingtype not supported for error number service: {}", itemType.getClass());
                        return null;
                    }
                    break;
                case "error":
                    if (itemType.isAssignableFrom(NumberItem.class)) {
                        Integer val = eService.getActiveError();
                        if (val == null) {
                            return null;
                        }
                        state = new DecimalType(val);
                    } else {
                        logger.warn("Bindingtype not supported for error service: {}", itemType.getClass());
                        return null;
                    }
                    break;
                case "errorString":
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        String val = eService.getErrorString();
                        if (val == null) {
                            return null;
                        }
                        state = new StringType(val);
                    } else {
                        logger.warn("Bindingtype not supported for error string service: {}", itemType.getClass());
                        return null;
                    }
                    break;
            }
            break;
    }
    return state;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) 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) DateTimeItem(org.openhab.core.library.items.DateTimeItem)

Example 84 with StringType

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

the class KM200Comm method parseJSONData.

/**
     * This function parses the receviced JSON Data and return the right state
     *
     */
public State parseJSONData(String decodedData, String type, String item, KM200BindingProvider provider) {
    JSONObject nodeRoot = null;
    State state = null;
    Class<? extends Item> itemType = provider.getItemType(item);
    String service = checkParameterReplacement(provider, item);
    KM200CommObject object = device.serviceMap.get(service);
    logger.debug("parseJSONData service: {}, data: {}", service, decodedData);
    /* Now parsing of the JSON String depending on its type and the type of binding item */
    try {
        if (decodedData.length() > 0) {
            nodeRoot = new JSONObject(decodedData);
        } else {
            logger.warn("Get empty reply");
            return null;
        }
        switch(type) {
            case "stringValue":
                /* Check whether the type is a single value containing a string value */
                logger.debug("initDevice: type string value: {}", decodedData);
                String sVal = nodeRoot.getString("value");
                device.serviceMap.get(service).setValue(sVal);
                /* SwitchItem Binding */
                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (provider.getParameter(item).containsKey("on")) {
                        if (sVal.equals(provider.getParameter(item).get("off"))) {
                            state = OnOffType.OFF;
                        } else if (sVal.equals(provider.getParameter(item).get("on"))) {
                            state = OnOffType.ON;
                        }
                    } else {
                        logger.warn("Switch-Item only on configured on/off string values: {}", decodedData);
                        return null;
                    }
                /* NumberItem Binding */
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    try {
                        state = new DecimalType(Float.parseFloat(sVal));
                    } catch (NumberFormatException e) {
                        logger.error("Conversion of the string value to Decimal wasn't possible, data: {} error: {}", decodedData, e);
                        return null;
                    }
                /* DateTimeItem Binding */
                } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
                    try {
                        state = new DateTimeType(sVal);
                    } catch (IllegalArgumentException e) {
                        logger.error("Conversion of the string value to DateTime wasn't possible, data: {} error: {}", decodedData, e);
                        return null;
                    }
                /* StringItem Binding */
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(sVal);
                } else {
                    logger.warn("Bindingtype not supported for string values: {}", itemType.getClass());
                    return null;
                }
                return state;
            case "floatValue":
                /* Check whether the type is a single value containing a float value */
                logger.debug("state of type float value: {}", decodedData);
                BigDecimal bdVal = nodeRoot.getBigDecimal("value");
                device.serviceMap.get(service).setValue(bdVal);
                /* NumberItem Binding */
                if (itemType.isAssignableFrom(NumberItem.class)) {
                    state = new DecimalType(bdVal.floatValue());
                /* StringItem Binding */
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(bdVal.toString());
                } else {
                    logger.warn("Bindingtype not supported for float values: {}", itemType.getClass());
                    return null;
                }
                return state;
            case "switchProgram":
                /* Check whether the type is a switchProgram */
                KM200SwitchProgramService sPService = null;
                logger.debug("state of type switchProgram: {}", decodedData);
                /* Get the KM200SwitchProgramService class object with all specific parameters */
                if (object.getVirtual() == 0) {
                    sPService = ((KM200SwitchProgramService) object.getValueParameter());
                } else {
                    sPService = ((KM200SwitchProgramService) device.serviceMap.get(object.getParent()).getValueParameter());
                }
                /* Update the switches insode the KM200SwitchProgramService */
                sPService.updateSwitches(nodeRoot);
                /* the parsing of switch program-services have to be outside, using json in strings */
                if (object.getVirtual() == 1) {
                    return this.getVirtualState(object, itemType, service);
                } else {
                    /* if access to the parent non virtual service the return the switchPoints jsonarray */
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = new StringType(nodeRoot.getJSONArray("switchPoints").toString());
                    } else {
                        logger.warn("Bindingtype not supported for switchProgram, only json over strings supported: {}", itemType.getClass());
                        return null;
                    }
                    return state;
                }
            case "errorList":
                /* Check whether the type is a errorList */
                KM200ErrorService eService = null;
                logger.debug("state of type errorList: {}", decodedData);
                /* Get the KM200ErrorService class object with all specific parameters */
                if (object.getVirtual() == 0) {
                    eService = ((KM200ErrorService) object.getValueParameter());
                } else {
                    eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
                }
                /* Update the switches insode the KM200SwitchProgramService */
                eService.updateErrors(nodeRoot);
                /* the parsing of switch program-services have to be outside, using json in strings */
                if (object.getVirtual() == 1) {
                    return this.getVirtualState(object, itemType, service);
                } else {
                    /* if access to the parent non virtual service the return the switchPoints jsonarray */
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = new StringType(nodeRoot.getJSONArray("values").toString());
                    } else {
                        logger.warn("Bindingtype not supported for error list, only json over strings is supported: {}", itemType.getClass());
                        return null;
                    }
                    return state;
                }
            case "yRecording":
                /* Check whether the type is a yRecording */
                logger.info("state of: type yRecording is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
            case "systeminfo":
                /* Check whether the type is a systeminfo */
                logger.info("state of: type systeminfo is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
            case "arrayData":
                /* Check whether the type is a arrayData */
                logger.info("state of: type arrayData is not supported yet: {}", decodedData);
                /* have to be completed */
                break;
        }
    } catch (JSONException e) {
        logger.error("Parsingexception in JSON, data: {} error: {} ", decodedData, e.getMessage());
    }
    return null;
}
Also used : StringType(org.openhab.core.library.types.StringType) JSONException(org.json.JSONException) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) JSONObject(org.json.JSONObject) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType)

Example 85 with StringType

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

the class KoubachiBinding method createState.

/**
     * Creates an openHAB {@link State} in accordance to the class of the given
     * {@code propertyValue}. Currently {@link Date}, {@link BigDecimal} 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.
     *
     * @param propertyValue
     *
     * @return the new {@link State} in accordance to {@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 (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 {
        return new StringType(propertyValue.toString());
    }
}
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)

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