Search in sources :

Example 31 with NumberItem

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

the class MCP3424Binding method handleGpioPinAnalogValueChangeEvent.

/**
     * Function will be called by Pi4J, if new conversion result is available.
     * On this time we can notify openhab, that something was changed.
     **/
@Override
public void handleGpioPinAnalogValueChangeEvent(GpioPinAnalogValueChangeEvent event) {
    GpioPin pin = event.getPin();
    MCP3424GpioProvider provider = (MCP3424GpioProvider) pin.getProvider();
    if (pin.getTag() instanceof NumberItem) {
        double value = provider.getAnalogValue(pin.getPin());
        try {
            this.eventPublisher.postUpdate(pin.getName(), new DecimalType(value));
        } catch (NumberFormatException exception) {
            logger.warn("Unable to convert '{}' for item {} to number", String.valueOf(value), pin.getName());
        }
        logger.debug("GPIO channel change: {} = {}", pin, value);
    } else if (pin.getTag() instanceof DimmerItem) {
        double value = provider.getPercentValue(pin.getPin());
        try {
            this.eventPublisher.postUpdate(pin.getName(), new PercentType((int) value));
        } catch (NumberFormatException e) {
            logger.warn("Unable to convert '{}' for item {} to number", String.valueOf(value), pin.getName());
        }
        logger.debug("GPIO channel change: {} = {}", pin, value);
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) GpioPin(com.pi4j.io.gpio.GpioPin) MCP3424GpioProvider(com.pi4j.gpio.extension.mcp.MCP3424GpioProvider) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType)

Example 32 with NumberItem

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

the class TestCaseSupport method configureNumberItemBinding.

protected void configureNumberItemBinding(int items, String slaveName, int itemOffset, String itemPrefix, State initialState) throws BindingConfigParseException {
    Assert.assertEquals(REFRESH_INTERVAL, binding.getRefreshInterval());
    final ModbusGenericBindingProvider provider = new ModbusGenericBindingProvider();
    for (int itemIndex = itemOffset; itemIndex < items + itemOffset; itemIndex++) {
        NumberItem item = new NumberItem(String.format("%sItem%d", itemPrefix, itemIndex + 1));
        if (initialState != null) {
            item.setState(initialState);
        }
        provider.processBindingConfiguration("test.items", item, String.format("%s:%d", slaveName, itemIndex));
    }
    binding.setEventPublisher(eventPublisher);
    binding.addBindingProvider(provider);
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem)

Example 33 with NumberItem

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

the class AudioZone method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int num = 0;
    String str = "";
    int source = new Integer(properties.getSource());
    switch(config.getObjectType()) {
        case AUDIOZONE_MUTE:
            num = properties.isMute() ? 1 : 0;
            break;
        case AUDIOZONE_POWER:
            num = properties.isOn() ? 1 : 0;
            break;
        case AUDIOZONE_SOURCE:
            num = properties.getSource();
            break;
        case AUDIOZONE_VOLUME:
            num = properties.getVolume();
            break;
        case AUDIOZONE_TEXT:
            if (sourceValid(source)) {
                str = audioSources.get(source).formatAudioText();
            }
            break;
        case AUDIOZONE_TEXT_FIELD1:
            if (sourceValid(source)) {
                str = audioSources.get(source).getAudioText(0);
            }
            break;
        case AUDIOZONE_TEXT_FIELD2:
            if (sourceValid(source)) {
                str = audioSources.get(source).getAudioText(1);
            }
            break;
        case AUDIOZONE_TEXT_FIELD3:
            if (sourceValid(source)) {
                str = audioSources.get(source).getAudioText(2);
            }
            break;
        case AUDIOZONE_KEY:
            num = -1;
            break;
        default:
            return;
    }
    if (item instanceof DimmerItem) {
        logger.debug("updating percent type {}", num);
        publisher.postUpdate(item.getName(), new PercentType(num));
    } else if (item instanceof NumberItem) {
        logger.debug("updating number type {}", num);
        publisher.postUpdate(item.getName(), new DecimalType(num));
    } else if (item instanceof SwitchItem) {
        logger.debug("updating switch type {}", num > 0 ? OnOffType.ON : OnOffType.OFF);
        publisher.postUpdate(item.getName(), num > 0 ? OnOffType.ON : OnOffType.OFF);
    } else if (item instanceof StringItem) {
        logger.debug("updating string type {}", str);
        publisher.postUpdate(item.getName(), new StringType(str));
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 34 with NumberItem

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

the class Auxiliary method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int setting = 0;
    switch(config.getObjectType()) {
        case AUX_CURRENT:
            setting = celsius ? MessageUtils.omniToC(properties.getCurrent()) : MessageUtils.omniToF(properties.getCurrent());
            break;
        case AUX_HIGH:
            setting = celsius ? MessageUtils.omniToC(properties.getHighSetpoint()) : MessageUtils.omniToF(properties.getCurrent());
            break;
        case AUX_LOW:
            setting = celsius ? MessageUtils.omniToC(properties.getLowSetpoint()) : MessageUtils.omniToF(properties.getCurrent());
            break;
        case AUX_STATUS:
            setting = properties.getStatus();
            break;
        default:
            return;
    }
    logger.debug("updating item {} for type {} to {}", item.getName(), config.getObjectType(), setting);
    if (item instanceof NumberItem) {
        publisher.postUpdate(item.getName(), new DecimalType(setting));
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DecimalType(org.openhab.core.library.types.DecimalType)

Example 35 with NumberItem

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

the class PlugwiseGenericBindingProvider method parseBindingConfig.

/**
     * Parses the configuration string and update the provided config
     *
     * @param config
     * @param item
     * @param bindingConfig
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(PlugwiseBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String plugwiseID = null;
    String plugwiseCommand = null;
    int interval = 60;
    if (bindingConfig == null) {
        logger.warn("bindingConfig for item '{}' is null", item.getName());
        return;
    }
    Matcher actionWithJobMatcher = ACTION_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithJobMatcher = STATUS_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher actionWithoutJobMatcher = ACTION_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithoutJobMatcher = STATUS_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    if (!actionWithJobMatcher.matches() && !statusWithJobMatcher.matches() && !actionWithoutJobMatcher.matches() && !statusWithoutJobMatcher.matches()) {
        throw new //
        BindingConfigParseException(//
        "Plugwise binding configuration must consist of either:\n" + "* 2 parts: [config=" + statusWithoutJobMatcher + //
        "]\n" + "* 3 parts: [config=" + statusWithJobMatcher + //
        "]\n" + "           [config=" + actionWithoutJobMatcher + //
        "]\n" + "* 4 parts: [config=" + actionWithJobMatcher + "]");
    }
    if (actionWithJobMatcher.matches()) {
        commandAsString = actionWithJobMatcher.group(1);
        plugwiseID = actionWithJobMatcher.group(2);
        plugwiseCommand = actionWithJobMatcher.group(3);
        interval = Integer.valueOf(actionWithJobMatcher.group(4));
    } else if (statusWithJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithJobMatcher.group(1);
        plugwiseCommand = statusWithJobMatcher.group(2);
        interval = Integer.valueOf(statusWithJobMatcher.group(3));
    } else if (actionWithoutJobMatcher.matches()) {
        commandAsString = actionWithoutJobMatcher.group(1);
        plugwiseID = actionWithoutJobMatcher.group(2);
        plugwiseCommand = actionWithoutJobMatcher.group(3);
        interval = -1;
    } else if (statusWithoutJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithoutJobMatcher.group(1);
        plugwiseCommand = statusWithoutJobMatcher.group(2);
        interval = -1;
    }
    PlugwiseCommandType type = PlugwiseCommandType.getCommandType(plugwiseCommand);
    if (PlugwiseCommandType.validateBinding(type, item)) {
        PlugwiseBindingConfigElement newElement = new PlugwiseBindingConfigElement(plugwiseID, type, interval);
        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++;
            config.put(command, newElement);
        } else {
            command = createCommandFromString(item, commandAsString);
            config.put(command, newElement);
        }
    } else {
        String validItemType = PlugwiseCommandType.getValidItemTypes(plugwiseCommand);
        if (StringUtils.isEmpty(validItemType)) {
            throw new BindingConfigParseException("'" + bindingConfig + "' is no valid binding type");
        } else {
            throw new BindingConfigParseException("'" + bindingConfig + "' is not bound to a valid item type. Valid item type(s): " + validItemType);
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

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