Search in sources :

Example 1 with SwitchItem

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

the class MqttItemConfigTest method canParseMultipleOutboundConfigs.

@Test
public void canParseMultipleOutboundConfigs() throws BindingConfigParseException {
    MqttItemConfig c = new MqttItemConfig(new SwitchItem("myItem"), ">[mybroker:/mytopic:command:ON:1],>[mybroker:/mytopic:command:OFF:0]");
    assertEquals(2, c.getMessagePublishers().size());
    assertEquals(0, c.getMessageSubscribers().size());
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 2 with SwitchItem

use of org.openhab.core.library.items.SwitchItem 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 3 with SwitchItem

use of org.openhab.core.library.items.SwitchItem 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 4 with SwitchItem

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

the class MqttItemConfigTest method canParseMultipleInboundConfigs.

@Test
public void canParseMultipleInboundConfigs() throws BindingConfigParseException {
    MqttItemConfig c = new MqttItemConfig(new SwitchItem("myItem"), "<[mybroker:/myHome/doorbell:state:XSLT(doorbell.xslt)], <[mybroker:/myHome/doorbell:command:ON], <[mybroker:/myHome/doorbell:state:XSLT(doorbell.xslt)]");
    assertEquals(0, c.getMessagePublishers().size());
    assertEquals(3, c.getMessageSubscribers().size());
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 5 with SwitchItem

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

Aggregations

SwitchItem (org.openhab.core.library.items.SwitchItem)55 NumberItem (org.openhab.core.library.items.NumberItem)25 DimmerItem (org.openhab.core.library.items.DimmerItem)21 Test (org.junit.Test)18 ContactItem (org.openhab.core.library.items.ContactItem)17 Item (org.openhab.core.items.Item)15 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 DecimalType (org.openhab.core.library.types.DecimalType)15 ColorItem (org.openhab.core.library.items.ColorItem)12 StringItem (org.openhab.core.library.items.StringItem)12 PercentType (org.openhab.core.library.types.PercentType)11 StringType (org.openhab.core.library.types.StringType)10 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 DateTimeType (org.openhab.core.library.types.DateTimeType)7 Calendar (java.util.Calendar)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)4