Search in sources :

Example 31 with DimmerItem

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

the class MysqlPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    if (!initialized) {
        logger.debug("Query aborted on item {} - mySQL not initialised!", filter.getItemName());
        return Collections.emptyList();
    }
    if (!isConnected()) {
        connectToDatabase();
    }
    if (!isConnected()) {
        logger.debug("Query aborted on item {} - mySQL not connected!", filter.getItemName());
        return Collections.emptyList();
    }
    SimpleDateFormat mysqlDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Get the item name from the filter
    // Also get the Item object so we can determine the type
    Item item = null;
    String itemName = filter.getItemName();
    logger.debug("mySQL query: item is {}", itemName);
    try {
        if (itemRegistry != null) {
            item = itemRegistry.getItem(itemName);
        }
    } catch (ItemNotFoundException e1) {
        logger.error("Unable to get item type for {}", itemName);
        // Set type to null - data will be returned as StringType
        item = null;
    }
    if (item instanceof GroupItem) {
        // For Group Items is BaseItem needed to get correct Type of Value.
        item = GroupItem.class.cast(item).getBaseItem();
    }
    String table = sqlTables.get(itemName);
    if (table == null) {
        logger.error("mySQL: Unable to find table for query '{}'.", itemName);
        return Collections.emptyList();
    }
    String filterString = new String();
    if (filter.getBeginDate() != null) {
        if (filterString.isEmpty()) {
            filterString += " WHERE";
        } else {
            filterString += " AND";
        }
        filterString += " TIME>'" + mysqlDateFormat.format(filter.getBeginDate()) + "'";
    }
    if (filter.getEndDate() != null) {
        if (filterString.isEmpty()) {
            filterString += " WHERE";
        } else {
            filterString += " AND";
        }
        filterString += " TIME<'" + mysqlDateFormat.format(filter.getEndDate().getTime()) + "'";
    }
    if (filter.getOrdering() == Ordering.ASCENDING) {
        filterString += " ORDER BY Time ASC";
    } else {
        filterString += " ORDER BY Time DESC";
    }
    if (filter.getPageSize() != 0x7fffffff) {
        filterString += " LIMIT " + filter.getPageNumber() * filter.getPageSize() + "," + filter.getPageSize();
    }
    try {
        long timerStart = System.currentTimeMillis();
        // Retrieve the table array
        Statement st = connection.createStatement();
        String queryString = new String();
        queryString = "SELECT Time, Value FROM " + table;
        if (!filterString.isEmpty()) {
            queryString += filterString;
        }
        logger.debug("mySQL: query:" + queryString);
        // Turn use of the cursor on.
        st.setFetchSize(50);
        ResultSet rs = st.executeQuery(queryString);
        long count = 0;
        List<HistoricItem> items = new ArrayList<HistoricItem>();
        State state;
        while (rs.next()) {
            count++;
            if (item instanceof NumberItem) {
                state = new DecimalType(rs.getDouble(2));
            } else if (item instanceof ColorItem) {
                state = new HSBType(rs.getString(2));
            } else if (item instanceof DimmerItem) {
                state = new PercentType(rs.getInt(2));
            } else if (item instanceof SwitchItem) {
                state = OnOffType.valueOf(rs.getString(2));
            } else if (item instanceof ContactItem) {
                state = OpenClosedType.valueOf(rs.getString(2));
            } else if (item instanceof RollershutterItem) {
                state = new PercentType(rs.getInt(2));
            } else if (item instanceof DateTimeItem) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(rs.getTimestamp(2).getTime());
                state = new DateTimeType(calendar);
            } else {
                state = new StringType(rs.getString(2));
            }
            MysqlItem mysqlItem = new MysqlItem(itemName, state, rs.getTimestamp(1));
            items.add(mysqlItem);
        }
        rs.close();
        st.close();
        long timerStop = System.currentTimeMillis();
        logger.debug("mySQL: query returned {} rows in {}ms", count, timerStop - timerStart);
        // Success
        errCnt = 0;
        return items;
    } catch (SQLException e) {
        errCnt++;
        logger.error("mySQL: Error running querying : ", e.getMessage());
    }
    return null;
}
Also used : StringType(org.openhab.core.library.types.StringType) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) 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) GroupItem(org.openhab.core.items.GroupItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) ResultSet(java.sql.ResultSet) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) GroupItem(org.openhab.core.items.GroupItem) HistoricItem(org.openhab.core.persistence.HistoricItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) 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) DecimalType(org.openhab.core.library.types.DecimalType) SimpleDateFormat(java.text.SimpleDateFormat) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 32 with DimmerItem

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

the class MapDBPersistenceService method store.

@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        return;
    }
    if (alias == null) {
        alias = item.getName();
    }
    logger.debug("store called for {}", alias);
    State state = item.getState();
    if (item instanceof ColorItem) {
        state = item.getStateAs(HSBType.class);
    } else if (item instanceof DimmerItem || item instanceof RollershutterItem) {
        state = item.getStateAs(PercentType.class);
    }
    MapDBItem mItem = new MapDBItem();
    mItem.setName(alias);
    mItem.setState(state);
    mItem.setTimestamp(new Date());
    MapDBItem oldItem = map.put(alias, mItem);
    if (!commitSameState) {
        if (oldItem != null) {
            if (!oldItem.getState().toString().equals(state.toString())) {
                needsCommit = true;
            }
        }
    }
    logger.debug("Stored '{}' with state '{}' in mapdb database", alias, state.toString());
}
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) HSBType(org.openhab.core.library.types.HSBType) Date(java.util.Date)

Example 33 with DimmerItem

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

the class MilightGenericBindingProvider method processBindingConfiguration.

@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    try {
        if (bindingConfig != null) {
            String[] configParts = bindingConfig.split(";");
            if (configParts.length > 4) {
                throw new BindingConfigParseException("milight binding configuration must not have more than four parts");
            }
            if (item instanceof ColorItem) {
                BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], BindingType.rgb.name(), null);
                addBindingConfig(item, milightBindingConfig);
            } else if (item instanceof DimmerItem || item instanceof SwitchItem) {
                BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], configParts.length < 3 ? null : configParts[2], configParts.length < 4 ? null : configParts[3]);
                addBindingConfig(item, milightBindingConfig);
            }
        } else {
            logger.warn("bindingConfig is NULL (item=" + item + ") -> processing bindingConfig aborted!");
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        logger.warn("bindingConfig is invalid (item=" + item + ") -> processing bindingConfig aborted!");
    }
}
Also used : BindingConfig(org.openhab.core.binding.BindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DimmerItem(org.openhab.core.library.items.DimmerItem) ColorItem(org.openhab.core.library.items.ColorItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 34 with DimmerItem

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

Aggregations

DimmerItem (org.openhab.core.library.items.DimmerItem)34 SwitchItem (org.openhab.core.library.items.SwitchItem)20 PercentType (org.openhab.core.library.types.PercentType)18 NumberItem (org.openhab.core.library.items.NumberItem)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ColorItem (org.openhab.core.library.items.ColorItem)13 ContactItem (org.openhab.core.library.items.ContactItem)11 DecimalType (org.openhab.core.library.types.DecimalType)11 Test (org.junit.Test)9 State (org.openhab.core.types.State)9 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 StringType (org.openhab.core.library.types.StringType)7 Calendar (java.util.Calendar)6 Item (org.openhab.core.items.Item)6 DateTimeType (org.openhab.core.library.types.DateTimeType)6 HSBType (org.openhab.core.library.types.HSBType)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 StringItem (org.openhab.core.library.items.StringItem)5 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)4 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)4