Search in sources :

Example 46 with Item

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

the class SappBinding method queryAndSendActualState.

/**
     * reads state from device and updates item repository
     */
private void queryAndSendActualState(SappBindingProvider provider, String itemName) {
    logger.debug("querying and sending item {}", itemName);
    try {
        Item item = itemRegistry.getItem(itemName);
        if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
            SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
            SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
            if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
                updateOnOffItem(provider, statusAddress, itemName, item);
            }
        } else if (item instanceof ContactItem) {
            SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
            SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
            updateOpenClosedItem(provider, statusAddress, itemName, item);
        } else if (item instanceof NumberItem) {
            SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
            SappAddressDecimal statusAddress = sappBindingConfigNumberItem.getStatus();
            updateDecimalItem(provider, statusAddress, itemName, item);
        } else if (item instanceof RollershutterItem) {
            SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
            SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
            updateRollershutterItem(provider, statusAddress, itemName, item);
        } else if (item instanceof DimmerItem) {
            SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
            SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
            updateDimmerItem(provider, statusAddress, itemName, item);
        } else {
            logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
        }
    } catch (ItemNotFoundException e) {
        logger.error("Item {} not found", itemName);
    }
}
Also used : SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) SappAddressOpenClosedStatus(org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SappAddressOnOffStatus(org.openhab.binding.sapp.internal.model.SappAddressOnOffStatus) SappAddressDimmer(org.openhab.binding.sapp.internal.model.SappAddressDimmer) SappAddressDecimal(org.openhab.binding.sapp.internal.model.SappAddressDecimal) SappAddressRollershutterStatus(org.openhab.binding.sapp.internal.model.SappAddressRollershutterStatus) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 47 with Item

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

the class SatelBinding method incomingEvent.

/**
     * {@inheritDoc}
     */
@Override
public void incomingEvent(SatelEvent event) {
    logger.trace("Handling incoming event: {}", event);
    // refresh all states that have changed
    if (event instanceof NewStatesEvent) {
        List<SatelCommand> commands = getRefreshCommands((NewStatesEvent) event);
        for (SatelCommand command : commands) {
            this.satelModule.sendCommand(command);
        }
    }
    // if just connected, force refreshing
    if (event instanceof ConnectionStatusEvent) {
        ConnectionStatusEvent statusEvent = (ConnectionStatusEvent) event;
        if (statusEvent.isConnected()) {
            switchForceRefresh(true);
        }
    }
    // update items
    for (SatelBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            SatelBindingConfig itemConfig = provider.getItemConfig(itemName);
            Item item = provider.getItem(itemName);
            State newState = itemConfig.convertEventToState(item, event);
            if (newState != null) {
                logger.debug("Updating item state: item = {}, state = {}, event = {}", itemName, newState, event);
                eventPublisher.postUpdate(itemName, newState);
                itemConfig.setItemInitialized();
            }
        }
    }
}
Also used : Item(org.openhab.core.items.Item) SatelBindingProvider(org.openhab.binding.satel.SatelBindingProvider) ConnectionStatusEvent(org.openhab.binding.satel.internal.event.ConnectionStatusEvent) State(org.openhab.core.types.State) NewStatesEvent(org.openhab.binding.satel.internal.event.NewStatesEvent) SatelCommand(org.openhab.binding.satel.command.SatelCommand) SatelBindingConfig(org.openhab.binding.satel.SatelBindingConfig)

Example 48 with Item

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

the class ZWaveConverterHandler method handleEvent.

/**
     * Handles an incoming {@link ZWaveCommandClassValueEvent}. Implement this
     * message in derived classes to convert the value and post an update on the
     * openHAB bus.
     *
     * @param provider
     *            the {@link ZWaveBindingProvider} that provides the item
     * @param itemName
     *            the name of the item that will receive the event.
     * @param event
     *            the received {@link ZWaveCommandClassValueEvent}.
     */
public void handleEvent(ZWaveBindingProvider provider, String itemName, ZWaveCommandClassValueEvent event) {
    ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
    Item item = provider.getItem(itemName);
    String commandClassName = bindingConfiguration.getArguments().get("command");
    boolean respondToBasic = "true".equalsIgnoreCase(bindingConfiguration.getArguments().get("respond_to_basic"));
    logger.trace("Getting converter for item = {}, command class = {}, item command class = {}", itemName, event.getCommandClass().getLabel(), commandClassName);
    if (item == null) {
        return;
    }
    if (commandClassName != null && !commandClassName.equalsIgnoreCase(event.getCommandClass().getLabel().toLowerCase()) && !(respondToBasic && event.getCommandClass() == CommandClass.BASIC)) {
        return;
    }
    ZWaveCommandClassConverter<?> converter = this.getConverter(event.getCommandClass());
    if (converter == null) {
        logger.warn("No converter found for command class = {}, ignoring event.", event.getCommandClass().toString());
        return;
    }
    converter.handleEvent(event, item, bindingConfiguration.getArguments());
}
Also used : DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) ZWaveBindingConfig(org.openhab.binding.zwave.ZWaveBindingConfig)

Example 49 with Item

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

the class InfluxDBPersistenceService method objectToState.

/**
   * Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
   * needed for querying a {@link HistoricState}.
   * 
   * @param value to be converted to a {@link State}
   * @param itemName name of the {@link Item} to get the {@link State} for
   * @return the state of the item represented by the itemName parameter, 
   *         else the string value of the Object parameter
   */
private State objectToState(Object value, String itemName) {
    String valueStr = String.valueOf(value);
    if (itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof NumberItem) {
                return new DecimalType(valueStr);
            } else if (item instanceof ColorItem) {
                return new HSBType(valueStr);
            } else if (item instanceof DimmerItem) {
                return new PercentType(valueStr);
            } else if (item instanceof SwitchItem) {
                return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON;
            } else if (item instanceof ContactItem) {
                return (string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF)) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (item instanceof RollershutterItem) {
                return new PercentType(valueStr);
            } else if (item instanceof DateTimeItem) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(new BigDecimal(valueStr).longValue());
                return new DateTimeType(calendar);
            } else {
                return new StringType(valueStr);
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Could not find item '{}' in registry", itemName);
        }
    }
    // just return a StringType as a fallback
    return new StringType(valueStr);
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) 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) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 50 with Item

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

the class DynamoDBPersistenceService method query.

/**
     * {@inheritDoc}
     */
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    logger.debug("got a query");
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return Collections.emptyList();
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return Collections.emptyList();
    }
    String itemName = filter.getItemName();
    Item item = getItemFromRegistry(itemName);
    if (item == null) {
        logger.warn("Could not get item {} from registry!", itemName);
        return Collections.emptyList();
    }
    Class<DynamoDBItem<?>> dtoClass = AbstractDynamoDBItem.getDynamoItemClass(item.getClass());
    String tableName = tableNameResolver.fromClass(dtoClass);
    DynamoDBMapper mapper = getDBMapper(tableName);
    logger.debug("item {} (class {}) will be tried to query using dto class {} from table {}", itemName, item.getClass(), dtoClass, tableName);
    List<HistoricItem> historicItems = new ArrayList<HistoricItem>();
    DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = createQueryExpression(dtoClass, filter);
    @SuppressWarnings("rawtypes") final PaginatedQueryList<? extends DynamoDBItem> paginatedList;
    try {
        paginatedList = mapper.query(dtoClass, queryExpression);
    } catch (AmazonServiceException e) {
        logger.error("DynamoDB query raised unexpected exception: {}. Returning empty collection. " + "Status code 400 (resource not found) might occur if table was just created.", e.getMessage());
        return Collections.emptyList();
    }
    for (int itemIndexOnPage = 0; itemIndexOnPage < filter.getPageSize(); itemIndexOnPage++) {
        int itemIndex = filter.getPageNumber() * filter.getPageSize() + itemIndexOnPage;
        DynamoDBItem<?> dynamoItem;
        try {
            dynamoItem = paginatedList.get(itemIndex);
        } catch (IndexOutOfBoundsException e) {
            logger.debug("Index {} is out-of-bounds", itemIndex);
            break;
        }
        if (dynamoItem != null) {
            HistoricItem historicItem = dynamoItem.asHistoricItem(item);
            logger.trace("Dynamo item {} converted to historic item: {}", item, historicItem);
            historicItems.add(historicItem);
        }
    }
    return historicItems;
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) ArrayList(java.util.ArrayList) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) AmazonServiceException(com.amazonaws.AmazonServiceException) HistoricItem(org.openhab.core.persistence.HistoricItem)

Aggregations

Item (org.openhab.core.items.Item)59 SwitchItem (org.openhab.core.library.items.SwitchItem)23 NumberItem (org.openhab.core.library.items.NumberItem)20 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)16 State (org.openhab.core.types.State)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14 DecimalType (org.openhab.core.library.types.DecimalType)9 HistoricItem (org.openhab.core.persistence.HistoricItem)9 ArrayList (java.util.ArrayList)7 ColorItem (org.openhab.core.library.items.ColorItem)7 StringItem (org.openhab.core.library.items.StringItem)7 HomematicBindingConfig (org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6 Test (org.junit.Test)5 GenericItem (org.openhab.core.items.GenericItem)5 GroupItem (org.openhab.core.items.GroupItem)5 ItemRegistry (org.openhab.core.items.ItemRegistry)5 Calendar (java.util.Calendar)3