Search in sources :

Example 1 with StringItem

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

the class MqttMessageSubscriberTest method canParseCommand.

@Test
public void canParseCommand() throws Exception {
    ColorItem colorItem = new ColorItem("ColorItem");
    DimmerItem dimmerItem = new DimmerItem("DimmerItem");
    LocationItem locationItem = new LocationItem("LocationItem");
    NumberItem numberItem = new NumberItem("NumberItem");
    RollershutterItem rollershutterItem = new RollershutterItem("SetpointItem");
    StringItem stringItem = new StringItem("StringItem");
    SwitchItem switchItem = new SwitchItem("SwitchItem");
    MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:command:default");
    assertEquals(StringType.valueOf("test"), subscriber.getCommand("test", stringItem.getAcceptedCommandTypes()));
    assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getCommand("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedCommandTypes()));
    assertEquals(StringType.valueOf(""), subscriber.getCommand("", stringItem.getAcceptedCommandTypes()));
    assertEquals(OnOffType.ON, subscriber.getCommand("ON", switchItem.getAcceptedCommandTypes()));
    assertEquals(HSBType.valueOf("5,6,5"), subscriber.getCommand("5,6,5", colorItem.getAcceptedCommandTypes()));
    assertEquals(DecimalType.ZERO, subscriber.getCommand(DecimalType.ZERO.toString(), numberItem.getAcceptedCommandTypes()));
    assertEquals(PercentType.HUNDRED, subscriber.getCommand(PercentType.HUNDRED.toString(), dimmerItem.getAcceptedCommandTypes()));
    assertEquals(PercentType.valueOf("80"), subscriber.getCommand(PercentType.valueOf("80").toString(), rollershutterItem.getAcceptedCommandTypes()));
    assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getCommand(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedCommandTypes()));
}
Also used : 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) ColorItem(org.openhab.core.library.items.ColorItem) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 2 with StringItem

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

the class MqttMessageSubscriberTest method canParseState.

@Test
public void canParseState() throws Exception {
    LocationItem locationItem = new LocationItem("LocationItem");
    StringItem stringItem = new StringItem("StringItem");
    SwitchItem switchItem = new SwitchItem("SwitchItem");
    MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:state:default");
    assertEquals(OnOffType.ON, subscriber.getState("ON", switchItem.getAcceptedDataTypes()));
    assertEquals(StringType.valueOf(""), subscriber.getState("", stringItem.getAcceptedDataTypes()));
    assertEquals(StringType.valueOf("test"), subscriber.getState("test", stringItem.getAcceptedDataTypes()));
    assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getState("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedDataTypes()));
    assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getState(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedDataTypes()));
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 3 with StringItem

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

the class WindowHandleTest method setUpDefaultDevice.

@Before
public void setUpDefaultDevice() {
    parameterAddress = new EnoceanParameterAddress(EnoceanId.fromString(EnoceanBindingProviderMock.DEVICE_ID));
    provider.setParameterAddress(parameterAddress);
    provider.setItem(new StringItem("dummie"));
    provider.setEep(EEPId.EEP_F6_10_00);
    binding.addBindingProvider(provider);
}
Also used : EnoceanParameterAddress(org.opencean.core.address.EnoceanParameterAddress) StringItem(org.openhab.core.library.items.StringItem) Before(org.junit.Before)

Example 4 with StringItem

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

the class KM200Comm method sendProvidersState.

/**
     * This function sets the state of a service on the device
     *
     */
public byte[] sendProvidersState(KM200BindingProvider provider, String item, Command command) {
    synchronized (device) {
        String type = null;
        String dataToSend = null;
        KM200CommObject object = null;
        Class<? extends Item> itemType = provider.getItemType(item);
        String service = checkParameterReplacement(provider, item);
        logger.debug("Prepare item for send: {} type: {} item: {}", service, type, itemType.getName());
        if (device.blacklistMap.contains(service)) {
            logger.debug("Service on blacklist: {}", service);
            return null;
        }
        if (device.serviceMap.containsKey(service)) {
            if (device.serviceMap.get(service).getWriteable() == 0) {
                logger.error("Service is listed as read-only: {}", service);
                return null;
            }
            object = device.serviceMap.get(service);
            type = object.getServiceType();
        } else {
            logger.error("Service is not in the determined device service list: {}", service);
            return null;
        }
        /* The service is availible, set now the values depeding on the item and binding type */
        logger.debug("state of: {} type: {}", command, type);
        /* Binding is a NumberItem */
        if (itemType.isAssignableFrom(NumberItem.class)) {
            BigDecimal bdVal = ((DecimalType) command).toBigDecimal();
            /* Check the capabilities of this service */
            if (object.getValueParameter() != null) {
                @SuppressWarnings("unchecked") List<BigDecimal> valParas = (List<BigDecimal>) object.getValueParameter();
                BigDecimal minVal = valParas.get(0);
                BigDecimal maxVal = valParas.get(1);
                if (bdVal.compareTo(minVal) < 0) {
                    bdVal = minVal;
                }
                if (bdVal.compareTo(maxVal) > 0) {
                    bdVal = maxVal;
                }
            }
            if (type.equals("floatValue")) {
                dataToSend = new JSONObject().put("value", bdVal).toString();
            } else if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", bdVal.toString()).toString();
            } else if (type.equals("switchProgram") && object.getVirtual() == 1) {
                /* A switchProgram as NumberItem is always virtual */
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else if (type.equals("errorList") && object.getVirtual() == 1) {
                /* A errorList as NumberItem is always virtual */
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else {
                logger.warn("Not supported type for numberItem: {}", type);
            }
        /* Binding is a StringItem */
        } else if (itemType.isAssignableFrom(StringItem.class)) {
            String val = ((StringType) command).toString();
            /* Check the capabilities of this service */
            if (object.getValueParameter() != null) {
                @SuppressWarnings("unchecked") List<String> valParas = (List<String>) object.getValueParameter();
                if (!valParas.contains(val)) {
                    logger.warn("Parameter is not in the service parameterlist: {}", val);
                    return null;
                }
            }
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else if (type.equals("floatValue")) {
                dataToSend = new JSONObject().put("value", Float.parseFloat(val)).toString();
            } else if (type.equals("switchProgram")) {
                if (object.getVirtual() == 1) {
                    dataToSend = sendVirtualState(object, itemType, service, command);
                } else {
                    /* The JSONArray of switch items can be sended directly */
                    try {
                        /* Check whether ths input string is a valid JSONArray */
                        JSONArray userArray = new JSONArray(val);
                        dataToSend = userArray.toString();
                    } catch (Exception e) {
                        logger.warn("The input for the switchProgram is not a valid JSONArray : {}", e.getMessage());
                        return null;
                    }
                }
            } else {
                logger.warn("Not supported type for stringItem: {}", type);
            }
        /* Binding is a DateTimeItem */
        } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
            String val = ((DateTimeType) command).toString();
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else if (type.equals("switchProgram")) {
                dataToSend = sendVirtualState(object, itemType, service, command);
            } else {
                logger.warn("Not supported type for dateTimeItem: {}", type);
            }
        /* Binding is a SwitchItem */
        } else if (itemType.isAssignableFrom(SwitchItem.class)) {
            String val = null;
            if (provider.getParameter(item).containsKey("on")) {
                if (command == OnOffType.OFF) {
                    val = provider.getParameter(item).get("off");
                } else if (command == OnOffType.ON) {
                    val = provider.getParameter(item).get("on");
                }
            } else {
                logger.warn("Switch-Item only on configured on/off string values {}", command);
                return null;
            }
            if (type.equals("stringValue")) {
                dataToSend = new JSONObject().put("value", val).toString();
            } else {
                logger.warn("Not supported type for SwitchItem:{}", type);
            }
        } else {
            logger.warn("Bindingtype not supported: {}", itemType.getClass());
            return null;
        }
        /* If some data is availible then we have to send it to device */
        if (dataToSend != null) {
            /* base64 + encoding */
            logger.debug("Encoding: {}", dataToSend);
            byte[] encData = encodeMessage(dataToSend);
            if (encData == null) {
                logger.error("Couldn't encrypt data");
                return null;
            }
            return encData;
        } else {
            return null;
        }
    }
}
Also used : JSONArray(org.json.JSONArray) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DateTimeType(org.openhab.core.library.types.DateTimeType) JSONObject(org.json.JSONObject) DecimalType(org.openhab.core.library.types.DecimalType) ArrayList(java.util.ArrayList) List(java.util.List) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 5 with StringItem

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

the class JdbcBaseDAO method getItemType.

public String getItemType(Item i) {
    Item item = i;
    String def = "STRINGITEM";
    if (i instanceof GroupItem) {
        item = ((GroupItem) i).getBaseItem();
        if (item == null) {
            // if GroupItem:<ItemType> is not defined in
            // *.items using StringType
            // logger.debug("JDBC: BaseItem GroupItem:<ItemType> is not
            // defined in *.items searching for first Member and try to use
            // as ItemType");
            logger.debug("JDBC::getItemType: Cannot detect ItemType for {} because the GroupItems' base type isn't set in *.items File.", i.getName());
            item = ((GroupItem) i).getMembers().get(0);
            if (item == null) {
                logger.debug("JDBC::getItemType: No ItemType found for first Child-Member of GroupItem {}, use ItemType for STRINGITEM as Fallback", i.getName());
                return def;
            }
        }
    }
    String itemType = item.getClass().getSimpleName().toUpperCase();
    logger.debug("JDBC::getItemType: Try to use ItemType {} for Item {}", itemType, i.getName());
    if (sqlTypes.get(itemType) == null) {
        logger.warn("JDBC::getItemType: No sqlType found for ItemType {}, use ItemType for STRINGITEM as Fallback for {}", itemType, i.getName());
        return def;
    }
    return itemType;
}
Also used : DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) JdbcItem(org.openhab.persistence.jdbc.model.JdbcItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) HistoricItem(org.openhab.core.persistence.HistoricItem) NumberItem(org.openhab.core.library.items.NumberItem) GroupItem(org.openhab.core.items.GroupItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) GroupItem(org.openhab.core.items.GroupItem)

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