Search in sources :

Example 1 with GroupItem

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

the class MysqlPersistenceService method getItemType.

/**
     * 
     * @param i
     * @return
     */
private String getItemType(Item i) {
    Item item = i;
    if (i instanceof GroupItem) {
        item = ((GroupItem) i).getBaseItem();
        if (item == null) {
            // if GroupItem:<ItemType> is not defined in *.items using StringType
            logger.debug("mySQL: Cannot detect ItemType for {} because the GroupItems' base type isn't set in *.items File.", i.getName());
            item = ((GroupItem) i).getMembers().get(0);
            if (item == null) {
                logger.debug("mySQL: No ItemType found for first Child-Member of GroupItem {}, use ItemType STRINGITEM ({}) as Fallback", i.getName(), sqlTypes.get("STRINGITEM"));
                return sqlTypes.get("STRINGITEM");
            }
        }
    }
    String itemType = item.getClass().getSimpleName().toUpperCase();
    if (sqlTypes.get(itemType) == null) {
        logger.debug("mySQL: No sqlType found for ItemType {}, use ItemType STRINGITEM ({}) as Fallback for {}", itemType, sqlTypes.get("STRINGITEM"), i.getName());
        return sqlTypes.get("STRINGITEM");
    }
    logger.debug("mySQL: Use ItemType {} ({}) for Item {}", itemType, sqlTypes.get(itemType), itemType, i.getName());
    return sqlTypes.get(itemType);
}
Also used : 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) GroupItem(org.openhab.core.items.GroupItem)

Example 2 with GroupItem

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

the class JdbcBaseDAO method getItemType.

public String getItemType(Item i) {
    Item item = i;
    String def = "STRINGITEM";
    if (i instanceof GroupItem) {
        item = ((GroupItem) i).getBaseItem();
        if (item == null) {
            // if GroupItem:<ItemType> is not defined in
            // *.items using StringType
            // logger.debug("JDBC: BaseItem GroupItem:<ItemType> is not
            // defined in *.items searching for first Member and try to use
            // as ItemType");
            logger.debug("JDBC::getItemType: Cannot detect ItemType for {} because the GroupItems' base type isn't set in *.items File.", i.getName());
            item = ((GroupItem) i).getMembers().get(0);
            if (item == null) {
                logger.debug("JDBC::getItemType: No ItemType found for first Child-Member of GroupItem {}, use ItemType for STRINGITEM as Fallback", i.getName());
                return def;
            }
        }
    }
    String itemType = item.getClass().getSimpleName().toUpperCase();
    logger.debug("JDBC::getItemType: Try to use ItemType {} for Item {}", itemType, i.getName());
    if (sqlTypes.get(itemType) == null) {
        logger.warn("JDBC::getItemType: No sqlType found for ItemType {}, use ItemType for STRINGITEM as Fallback for {}", itemType, i.getName());
        return def;
    }
    return itemType;
}
Also used : DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) JdbcItem(org.openhab.persistence.jdbc.model.JdbcItem) 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) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) GroupItem(org.openhab.core.items.GroupItem)

Example 3 with GroupItem

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

the class JdbcPersistenceService method query.

/**
     * Queries the {@link PersistenceService} for data with a given filter
     * criteria
     *
     * @param filter
     *            the filter to apply to the query
     * @return a time series of items
     */
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    if (!checkDBAccessability()) {
        logger.warn("JDBC::query: database not connected, query aborted for item '{}'", filter.getItemName());
        return Collections.emptyList();
    }
    if (itemRegistry == null) {
        logger.error("JDBC::query: itemRegistry == null. Ignore and give up!");
        return Collections.emptyList();
    }
    // 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("JDBC::query: item is {}", itemName);
    try {
        item = itemRegistry.getItem(itemName);
    } catch (ItemNotFoundException e1) {
        logger.error("JDBC::query: unable to get item for itemName: '{}'. Ignore and give up!", itemName);
        return Collections.emptyList();
    }
    if (item instanceof GroupItem) {
        // For Group Item is BaseItem needed to get correct Type of Value.
        item = GroupItem.class.cast(item).getBaseItem();
        logger.debug("JDBC::query: item is instanceof GroupItem '{}'", itemName);
        if (item == null) {
            logger.debug("JDBC::query: BaseItem of GroupItem is null. Ignore and give up!");
            return Collections.emptyList();
        }
        if (item instanceof GroupItem) {
            logger.debug("JDBC::query: BaseItem of GroupItem is a GroupItem too. Ignore and give up!");
            return Collections.emptyList();
        }
    }
    String table = sqlTables.get(itemName);
    if (table == null) {
        logger.warn("JDBC::query: unable to find table for query, no data in database for item '{}'. Current number of tables in the database: {}", itemName, sqlTables.size());
        // if enabled, table will be created immediately
        if (item != null) {
            logger.warn("JDBC::query: try to generate the table for item '{}'", itemName);
            table = getTable(item);
        } else {
            logger.warn("JDBC::query: no way to generate the table for item '{}'", itemName);
            return Collections.emptyList();
        }
    }
    long timerStart = System.currentTimeMillis();
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    items = getHistItemFilterQuery(filter, conf.getNumberDecimalcount(), table, item);
    logger.debug("JDBC::query: query for {} returned {} rows in {} ms", item.getName(), items.size(), System.currentTimeMillis() - timerStart);
    // Success
    errCnt = 0;
    return items;
}
Also used : GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) HistoricItem(org.openhab.core.persistence.HistoricItem) ArrayList(java.util.ArrayList) GroupItem(org.openhab.core.items.GroupItem) HistoricItem(org.openhab.core.persistence.HistoricItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 4 with GroupItem

use of org.openhab.core.items.GroupItem 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 5 with GroupItem

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

the class RRD4jChartServlet method createChart.

@Override
public BufferedImage createChart(String service, String theme, Date startTime, Date endTime, int height, int width, String items, String groups) throws ItemNotFoundException {
    RrdGraphDef graphDef = new RrdGraphDef();
    long period = (startTime.getTime() - endTime.getTime()) / 1000;
    graphDef.setWidth(width);
    graphDef.setHeight(height);
    graphDef.setAntiAliasing(true);
    graphDef.setImageFormat("PNG");
    graphDef.setStartTime(period);
    graphDef.setTextAntiAliasing(true);
    graphDef.setLargeFont(new Font("SansSerif", Font.PLAIN, 15));
    graphDef.setSmallFont(new Font("SansSerif", Font.PLAIN, 11));
    int seriesCounter = 0;
    // Loop through all the items
    if (items != null) {
        String[] itemNames = items.split(",");
        for (String itemName : itemNames) {
            Item item = itemUIRegistry.getItem(itemName);
            addLine(graphDef, item, seriesCounter++);
        }
    }
    // Loop through all the groups and add each item from each group
    if (groups != null) {
        String[] groupNames = groups.split(",");
        for (String groupName : groupNames) {
            Item item = itemUIRegistry.getItem(groupName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    addLine(graphDef, member, seriesCounter++);
                }
            } else {
                throw new ItemNotFoundException("Item '" + item.getName() + "' defined in groups is not a group.");
            }
        }
    }
    // Write the chart as a PNG image
    RrdGraph graph;
    try {
        graph = new RrdGraph(graphDef);
        BufferedImage bi = new BufferedImage(graph.getRrdGraphInfo().getWidth(), graph.getRrdGraphInfo().getHeight(), BufferedImage.TYPE_INT_RGB);
        graph.render(bi.getGraphics());
        return bi;
    } catch (IOException e) {
        logger.error("Error generating graph: {}", e);
    }
    return null;
}
Also used : RrdGraphDef(org.rrd4j.graph.RrdGraphDef) NumberItem(org.openhab.core.library.items.NumberItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) RrdGraph(org.rrd4j.graph.RrdGraph) GroupItem(org.openhab.core.items.GroupItem) IOException(java.io.IOException) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

GroupItem (org.openhab.core.items.GroupItem)5 Item (org.openhab.core.items.Item)5 NumberItem (org.openhab.core.library.items.NumberItem)4 HistoricItem (org.openhab.core.persistence.HistoricItem)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)3 ColorItem (org.openhab.core.library.items.ColorItem)3 ContactItem (org.openhab.core.library.items.ContactItem)3 DateTimeItem (org.openhab.core.library.items.DateTimeItem)3 DimmerItem (org.openhab.core.library.items.DimmerItem)3 RollershutterItem (org.openhab.core.library.items.RollershutterItem)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 ArrayList (java.util.ArrayList)2 Font (java.awt.Font)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 SimpleDateFormat (java.text.SimpleDateFormat)1