Search in sources :

Example 1 with NumberItem

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

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

the class ProtocolGenericBindingProvider method parseBindingConfig.

/**
     * Parses the configuration string and update the provided config
     *
     * @param config - the Configuration that needs to be updated with the parsing results
     * @param item - the Item that this configuration is intented for
     * @param bindingConfig - the configuration string that will be parsed
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(ProtocolBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String direction = null;
    Direction directionType = null;
    String commandAsString = null;
    String host = null;
    String port = null;
    String protocolCommand = null;
    if (bindingConfig != null) {
        Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
        Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);
        if ((!actionMatcher.matches() && !statusMatcher.matches())) {
            throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of four [config=" + statusMatcher + "] or five parts [config=" + actionMatcher + "]");
        } else {
            if (actionMatcher.matches()) {
                direction = actionMatcher.group(1);
                commandAsString = actionMatcher.group(2);
                host = actionMatcher.group(3);
                port = actionMatcher.group(4);
                protocolCommand = actionMatcher.group(5) != null ? actionMatcher.group(5) : actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                direction = statusMatcher.group(1);
                commandAsString = null;
                host = statusMatcher.group(2);
                port = statusMatcher.group(3);
                protocolCommand = statusMatcher.group(4) != null ? statusMatcher.group(4) : statusMatcher.group(5);
            }
            if (direction.equals(">")) {
                directionType = Direction.OUT;
            } else if (direction.equals("<")) {
                directionType = Direction.IN;
            }
            ProtocolBindingConfigElement newElement = new ProtocolBindingConfigElement(host, port, directionType, protocolCommand, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                // for those configuration strings that are not really linked to a openHAB command we
                // create a dummy Command to be able to store the configuration information
                // I have choosen to do that with NumberItems
                NumberItem dummy = new NumberItem(Integer.toString(counter));
                command = createCommandFromString(dummy, Integer.toString(counter));
                counter++;
            } else {
                command = createCommandFromString(item, commandAsString);
            }
            config.put(command, newElement);
        }
    } else {
        return;
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Direction(org.openhab.binding.tcp.Direction)

Example 3 with NumberItem

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

the class SysteminfoGenericBindingProviderTest method testProcessBindingConfig_FileCount03.

@Test
public /* Verify a relative path file count configuration (Windows): "DirFiles:60000:..\resources\" */
void testProcessBindingConfig_FileCount03() throws BindingConfigParseException {
    String simpleConfig = "DirFiles:60000:..\\resources\\";
    NumberItem testItem = new NumberItem("DirFiles03");
    provider.processBindingConfiguration("systeminfo", testItem, simpleConfig);
    Assert.assertEquals("DirFiles", provider.getCommandType("DirFiles03").toString());
    Assert.assertNull(provider.getItemType("DirFiles03"));
    Assert.assertEquals(60000, provider.getRefreshInterval("DirFiles03"));
    Assert.assertEquals("..\\resources\\", provider.getTarget("DirFiles03"));
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Test(org.junit.Test)

Example 4 with NumberItem

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

the class SysteminfoGenericBindingProviderTest method testProcessBindingConfig_FileCount04.

/* Verify an absolute path file count configuration (*nix): "DirFiles:60000:/usr/bin/" */
public void testProcessBindingConfig_FileCount04() throws BindingConfigParseException {
    String simpleConfig = "DirFiles:60000:/usr/bin/";
    NumberItem testItem = new NumberItem("DirFiles04");
    provider.processBindingConfiguration("systeminfo", testItem, simpleConfig);
    Assert.assertEquals("DirFiles", provider.getCommandType("DirFiles04").toString());
    Assert.assertNull(provider.getItemType("DirFiles04"));
    Assert.assertEquals(60000, provider.getRefreshInterval("DirFiles04"));
    Assert.assertEquals("/usr/bin/", provider.getTarget("DirFiles04"));
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem)

Example 5 with NumberItem

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

the class SysteminfoGenericBindingProviderTest method testProcessBindingConfig_FileCount05.

@Test
public /* Verify an absolute path file count configuration (Windows): "DirFiles:60000:c:\windows\" */
void testProcessBindingConfig_FileCount05() throws BindingConfigParseException {
    String simpleConfig = "DirFiles:60000:c:\\temp\\";
    NumberItem testItem = new NumberItem("DirFiles05");
    provider.processBindingConfiguration("systeminfo", testItem, simpleConfig);
    Assert.assertEquals("DirFiles", provider.getCommandType("DirFiles05").toString());
    Assert.assertNull(provider.getItemType("DirFiles05"));
    Assert.assertEquals(60000, provider.getRefreshInterval("DirFiles05"));
    Assert.assertEquals("c:\\temp\\", provider.getTarget("DirFiles05"));
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Test(org.junit.Test)

Aggregations

NumberItem (org.openhab.core.library.items.NumberItem)55 DecimalType (org.openhab.core.library.types.DecimalType)27 SwitchItem (org.openhab.core.library.items.SwitchItem)24 ContactItem (org.openhab.core.library.items.ContactItem)17 StringType (org.openhab.core.library.types.StringType)17 Test (org.junit.Test)16 DimmerItem (org.openhab.core.library.items.DimmerItem)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 StringItem (org.openhab.core.library.items.StringItem)15 Item (org.openhab.core.items.Item)11 PercentType (org.openhab.core.library.types.PercentType)11 DateTimeType (org.openhab.core.library.types.DateTimeType)10 Calendar (java.util.Calendar)9 DateTimeItem (org.openhab.core.library.items.DateTimeItem)9 ColorItem (org.openhab.core.library.items.ColorItem)8 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 BigDecimal (java.math.BigDecimal)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5