Search in sources :

Example 26 with StringItem

use of org.openhab.core.library.items.StringItem in project openhab1-addons by openhab.

the class BaseIntegrationTest method initService.

@BeforeClass
public static void initService() throws InterruptedException {
    items.put("dimmer", new DimmerItem("dimmer"));
    items.put("number", new NumberItem("number"));
    items.put("string", new StringItem("string"));
    items.put("switch", new SwitchItem("switch"));
    items.put("contact", new ContactItem("contact"));
    items.put("color", new ColorItem("color"));
    items.put("rollershutter", new RollershutterItem("rollershutter"));
    items.put("datetime", new DateTimeItem("datetime"));
    items.put("call", new CallItem("call"));
    items.put("location", new LocationItem("location"));
    service = new DynamoDBPersistenceService();
    service.setItemRegistry(new ItemRegistry() {

        @Override
        public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }

        @Override
        public boolean isValidItemName(String itemName) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems(String pattern) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems() {
            throw new NotImplementedException();
        }

        @Override
        public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
            throw new NotImplementedException();
        }

        @Override
        public Item getItem(String name) throws ItemNotFoundException {
            Item item = items.get(name);
            if (item == null) {
                throw new ItemNotFoundException(name);
            }
            return item;
        }

        @Override
        public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }
    });
    HashMap<String, Object> config = new HashMap<>();
    config.put("region", System.getProperty("DYNAMODBTEST_REGION"));
    config.put("accessKey", System.getProperty("DYNAMODBTEST_ACCESS"));
    config.put("secretKey", System.getProperty("DYNAMODBTEST_SECRET"));
    config.put("tablePrefix", "dynamodb-integration-tests-");
    for (Entry<String, Object> entry : config.entrySet()) {
        if (entry.getValue() == null) {
            logger.warn(String.format("Expecting %s to have value for integration tests. Integration tests will be skipped", entry.getKey()));
            service = null;
            return;
        }
    }
    service.activate(null, config);
    // Clear data
    for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string" }) {
        try {
            service.getDb().getDynamoClient().deleteTable(table);
            service.getDb().getDynamoDB().getTable(table).waitForDelete();
        } catch (ResourceNotFoundException e) {
        }
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang.NotImplementedException) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) ItemRegistry(org.openhab.core.items.ItemRegistry) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) CallItem(org.openhab.library.tel.items.CallItem) Item(org.openhab.core.items.Item) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) LocationItem(org.openhab.core.library.items.LocationItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ItemRegistryChangeListener(org.openhab.core.items.ItemRegistryChangeListener) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) Collection(java.util.Collection) CallItem(org.openhab.library.tel.items.CallItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) BeforeClass(org.junit.BeforeClass)

Example 27 with StringItem

use of org.openhab.core.library.items.StringItem 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)

Aggregations

StringItem (org.openhab.core.library.items.StringItem)27 NumberItem (org.openhab.core.library.items.NumberItem)16 StringType (org.openhab.core.library.types.StringType)16 DecimalType (org.openhab.core.library.types.DecimalType)14 SwitchItem (org.openhab.core.library.items.SwitchItem)12 DimmerItem (org.openhab.core.library.items.DimmerItem)8 DateTimeType (org.openhab.core.library.types.DateTimeType)8 Test (org.junit.Test)7 ContactItem (org.openhab.core.library.items.ContactItem)7 Calendar (java.util.Calendar)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)5 RollershutterItem (org.openhab.core.library.items.RollershutterItem)5 PercentType (org.openhab.core.library.types.PercentType)5 BigDecimal (java.math.BigDecimal)4 ColorItem (org.openhab.core.library.items.ColorItem)4 Date (java.util.Date)3 Item (org.openhab.core.items.Item)3 State (org.openhab.core.types.State)3 LinkedList (java.util.LinkedList)2 JSONException (org.json.JSONException)2