Search in sources :

Example 1 with ColorItem

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

the class DmxGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    if (dmxService == null) {
        logger.error("DMX Service unavailable. Cannot process item configuration for {}", item.getName());
        return;
    }
    super.processBindingConfiguration(context, item, bindingConfig);
    String config = (bindingConfig == null) ? "" : bindingConfig.replaceAll(" ", "").toUpperCase();
    logger.trace("Binding item: {} with configuration {}", item.getName(), config);
    DmxItem itemBinding = null;
    if (item instanceof ColorItem) {
        itemBinding = new DmxColorItem(item.getName(), config, this);
    } else if (item instanceof DimmerItem) {
        itemBinding = new DmxDimmerItem(item.getName(), config, this);
    } else if (item instanceof SwitchItem) {
        itemBinding = new DmxSwitchItem(item.getName(), config, this);
    } else {
        throw new BindingConfigParseException("Unsupported item type " + item.getClass().getSimpleName());
    }
    if (itemBinding.isStatusListener()) {
        final DmxStatusUpdateListener dmxStatusListener = itemBinding;
        final String itemName = item.getName();
        logger.trace("Registering status listener for item {} ", item.getName());
        dmxService.registerStatusListener(dmxStatusListener);
        // add binding change listener to clean up status listeners on item
        // removal
        addBindingChangeListener(new BindingChangeListener() {

            @Override
            public void bindingChanged(BindingProvider provider, String changedItemName) {
                if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                    logger.trace("Removing status listener for item {}", itemName);
                    dmxService.unregisterStatusListener(dmxStatusListener);
                }
            }

            @Override
            public void allBindingsChanged(BindingProvider provider) {
                if (!provider.providesBindingFor(itemName)) {
                    logger.trace("Removing status listener for item {}", itemName);
                    dmxService.unregisterStatusListener(dmxStatusListener);
                }
            }
        });
    }
    addBindingConfig(item, itemBinding);
}
Also used : DmxStatusUpdateListener(org.openhab.binding.dmx.DmxStatusUpdateListener) DmxItem(org.openhab.binding.dmx.internal.config.DmxItem) DmxSwitchItem(org.openhab.binding.dmx.internal.config.DmxSwitchItem) DmxColorItem(org.openhab.binding.dmx.internal.config.DmxColorItem) ColorItem(org.openhab.core.library.items.ColorItem) BindingChangeListener(org.openhab.core.binding.BindingChangeListener) DmxBindingProvider(org.openhab.binding.dmx.DmxBindingProvider) AbstractGenericBindingProvider(org.openhab.model.item.binding.AbstractGenericBindingProvider) BindingProvider(org.openhab.core.binding.BindingProvider) DmxDimmerItem(org.openhab.binding.dmx.internal.config.DmxDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) DmxDimmerItem(org.openhab.binding.dmx.internal.config.DmxDimmerItem) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DmxColorItem(org.openhab.binding.dmx.internal.config.DmxColorItem) DmxSwitchItem(org.openhab.binding.dmx.internal.config.DmxSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 2 with ColorItem

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

the class JpaHistoricItem method fromPersistedItem.

/**
     * Converts the string value of the persisted item to the state of a HistoricItem.
     * 
     * @param pItem the persisted JpaPersistentItem
     * @param item the source reference Item
     * @return historic item
     */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof CallItem) {
        state = new CallType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 3 with ColorItem

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

the class MysqlPersistenceService method store.

/**
     * @{inheritDoc
     */
@Override
public void store(Item item, String alias) {
    // Don't log undefined/uninitialised data
    if (item.getState() instanceof UnDefType) {
        return;
    }
    // If we've not initialised the bundle, then return
    if (initialized == false) {
        return;
    }
    // Connect to mySQL server if we're not already connected
    if (!isConnected()) {
        connectToDatabase();
    }
    // If we still didn't manage to connect, then return!
    if (!isConnected()) {
        logger.warn("mySQL: No connection to database. Can not persist item '{}'! " + "Will retry connecting to database when error count:{} equals errReconnectThreshold:{}", item, errCnt, errReconnectThreshold);
        return;
    }
    // Get the table name for this item
    String tableName = getTable(item);
    if (tableName == null) {
        logger.error("Unable to store item '{}'.", item.getName());
        return;
    }
    // Do some type conversion to ensure we know the data type.
    // This is necessary for items that have multiple types and may return their
    // state in a format that's not preferred or compatible with the MySQL type.
    // eg. DimmerItem can return OnOffType (ON, OFF), or PercentType (0-100).
    // We need to make sure we cover the best type for serialisation.
    String value;
    if (item instanceof ColorItem) {
        value = item.getStateAs(HSBType.class).toString();
    } else if (item instanceof RollershutterItem) {
        value = item.getStateAs(PercentType.class).toString();
    } else {
        /*
             * !!ATTENTION!!
             * 
             * 1.
             * DimmerItem.getStateAs(PercentType.class).toString() always returns 0
             * RollershutterItem.getStateAs(PercentType.class).toString() works as expected
             * 
             * 2.
             * (item instanceof ColorItem) == (item instanceof DimmerItem) = true
             * Therefore for instance tests ColorItem always has to be tested before DimmerItem
             * 
             * !!ATTENTION!!
             */
        // All other items should return the best format by default
        value = item.getState().toString();
    }
    // Get current timestamp
    long timeNow = Calendar.getInstance().getTimeInMillis();
    Timestamp timestamp = new Timestamp(timeNow);
    String sqlCmd = null;
    PreparedStatement statement = null;
    try {
        if (localtime) {
            sqlCmd = new String("INSERT INTO " + tableName + " (TIME, VALUE) VALUES(?,?) ON DUPLICATE KEY UPDATE VALUE=?;");
            statement = connection.prepareStatement(sqlCmd);
            statement.setTimestamp(1, timestamp);
            statement.setString(2, value);
            statement.setString(3, value);
        } else {
            sqlCmd = new String("INSERT INTO " + tableName + " (TIME, VALUE) VALUES(NOW(),?) ON DUPLICATE KEY UPDATE VALUE=?;");
            statement = connection.prepareStatement(sqlCmd);
            statement.setString(1, value);
            statement.setString(2, value);
        }
        statement.executeUpdate();
        logger.debug("mySQL: Stored item '{}' as '{}'[{}] in SQL database at {}.", item.getName(), item.getState().toString(), value, timestamp.toString());
        logger.debug("mySQL: query: {}", sqlCmd);
        // Success
        errCnt = 0;
    } catch (Exception e) {
        errCnt++;
        logger.error("mySQL: Could not store item '{}' in database with " + "statement '{}': {}", item.getName(), sqlCmd, e.getMessage());
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception hidden) {
            }
        }
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ColorItem(org.openhab.core.library.items.ColorItem) PreparedStatement(java.sql.PreparedStatement) PercentType(org.openhab.core.library.types.PercentType) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 4 with ColorItem

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

the class InfluxDBPersistenceService method store.

/**
   * {@inheritDoc}
   */
@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        return;
    }
    if (!isProperlyConfigured) {
        logger.warn("Configuration for influxdb08 not yet loaded or broken.");
        return;
    }
    if (!isConnected()) {
        logger.warn("InfluxDB is not yet connected");
        return;
    }
    String realName = item.getName();
    String name = (alias != null) ? alias : realName;
    State state = null;
    if (item instanceof DimmerItem || item instanceof RollershutterItem) {
        state = item.getStateAs(PercentType.class);
    } else if (item instanceof ColorItem) {
        state = item.getStateAs(HSBType.class);
    } else {
        // All other items should return the best format by default
        state = item.getState();
    }
    Object value = stateToObject(state);
    logger.trace("storing {} in influxdb08 {}", name, value);
    // For now time is calculated by influxdb08, may be this should be configurable?
    Serie serie = new Serie.Builder(name).columns(VALUE_COLUMN_NAME).values(value).build();
    try {
        influxDB.write(dbName, TimeUnit.MILLISECONDS, serie);
    } catch (RuntimeException e) {
        logger.error("storing failed with exception for item: {}", name);
        handleDatabaseException(e);
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) Serie(org.influxdb.dto.Serie)

Example 5 with ColorItem

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

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Aggregations

ColorItem (org.openhab.core.library.items.ColorItem)18 DimmerItem (org.openhab.core.library.items.DimmerItem)13 RollershutterItem (org.openhab.core.library.items.RollershutterItem)11 SwitchItem (org.openhab.core.library.items.SwitchItem)11 PercentType (org.openhab.core.library.types.PercentType)11 NumberItem (org.openhab.core.library.items.NumberItem)8 ContactItem (org.openhab.core.library.items.ContactItem)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 DecimalType (org.openhab.core.library.types.DecimalType)7 HSBType (org.openhab.core.library.types.HSBType)7 Calendar (java.util.Calendar)6 DateTimeType (org.openhab.core.library.types.DateTimeType)6 State (org.openhab.core.types.State)6 StringType (org.openhab.core.library.types.StringType)5 BigDecimal (java.math.BigDecimal)4 Test (org.junit.Test)4 Item (org.openhab.core.items.Item)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)4 Date (java.util.Date)3 LocationItem (org.openhab.core.library.items.LocationItem)3