Search in sources :

Example 26 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class MongoDBPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    if (!initialized) {
        return Collections.emptyList();
    }
    if (!isConnected()) {
        connectToDatabase();
    }
    if (!isConnected()) {
        return Collections.emptyList();
    }
    String name = filter.getItemName();
    Item item = getItem(name);
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    DBObject query = new BasicDBObject();
    if (filter.getItemName() != null) {
        query.put(FIELD_ITEM, filter.getItemName());
    }
    if (filter.getState() != null && filter.getOperator() != null) {
        String op = convertOperator(filter.getOperator());
        Object value = convertValue(filter.getState());
        query.put(FIELD_VALUE, new BasicDBObject(op, value));
    }
    if (filter.getBeginDate() != null) {
        query.put(FIELD_TIMESTAMP, new BasicDBObject("$gte", filter.getBeginDate()));
    }
    if (filter.getEndDate() != null) {
        query.put(FIELD_TIMESTAMP, new BasicDBObject("$lte", filter.getEndDate()));
    }
    Integer sortDir = (filter.getOrdering() == Ordering.ASCENDING) ? 1 : -1;
    DBCursor cursor = this.mongoCollection.find(query).sort(new BasicDBObject(FIELD_TIMESTAMP, sortDir)).skip(filter.getPageNumber() * filter.getPageSize()).limit(filter.getPageSize());
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        final State state;
        if (item instanceof NumberItem) {
            state = new DecimalType(obj.getDouble(FIELD_VALUE));
        } else if (item instanceof DimmerItem) {
            state = new PercentType(obj.getInt(FIELD_VALUE));
        } else if (item instanceof SwitchItem) {
            state = OnOffType.valueOf(obj.getString(FIELD_VALUE));
        } else if (item instanceof ContactItem) {
            state = OpenClosedType.valueOf(obj.getString(FIELD_VALUE));
        } else if (item instanceof RollershutterItem) {
            state = new PercentType(obj.getInt(FIELD_VALUE));
        } else if (item instanceof ColorItem) {
            state = new HSBType(obj.getString(FIELD_VALUE));
        } else if (item instanceof DateTimeItem) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(obj.getDate(FIELD_VALUE));
            state = new DateTimeType(cal);
        } else {
            state = new StringType(obj.getString(FIELD_VALUE));
        }
        items.add(new MongoDBItem(name, state, obj.getDate(FIELD_TIMESTAMP)));
    }
    return items;
}
Also used : StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HistoricItem(org.openhab.core.persistence.HistoricItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) 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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 27 with HSBType

use of org.openhab.core.library.types.HSBType 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 28 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DiyOnXBeeBinding method changeColorBrightness.

private String changeColorBrightness(final HSBType hsbType, IncreaseDecreaseType increaseDecrease) {
    final PercentType brightness = hsbType.getBrightness();
    final PercentType newBrightness = changeBrightness(increaseDecrease, brightness);
    final HSBType newHSB = new HSBType(hsbType.getHue(), hsbType.getSaturation(), newBrightness);
    return makeRGB(newHSB.toColor());
}
Also used : PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 29 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DmxColorItem method processCommand.

/**
     * {@inheritDoc}
     */
@Override
public void processCommand(DmxService service, Command command) {
    // process HSB command
    if (command instanceof HSBType) {
        HSBType hsbValue = (HSBType) command;
        setHSBValue(service, hsbValue);
        return;
    }
    // process increase/decrease
    if (command instanceof IncreaseDecreaseType && !isRedefinedByCustomCommand(command) && !service.hasChannelActions(channels[0])) {
        // rather than doing a linear fade on all channels, we fade only the
        // V part of HSV to maintain the color during the fade
        HSBType hsb = hsbState;
        int brightness = 0;
        IncreaseDecreaseType t = (IncreaseDecreaseType) command;
        if (IncreaseDecreaseType.INCREASE.equals(t)) {
            if (hsb == null) {
                hsb = new HSBType(Color.WHITE);
            }
            for (int ch : channels) {
                service.enableChannel(ch);
            }
            brightness = hsb.getBrightness().intValue();
            brightness += BRIGHTNESS_STEP_SIZE;
            if (brightness > 100) {
                brightness = 100;
            }
        } else {
            if (hsb == null) {
                hsb = new HSBType(Color.BLACK);
            }
            brightness = hsb.getBrightness().intValue();
            brightness -= BRIGHTNESS_STEP_SIZE;
            if (brightness <= 0) {
                brightness = 0;
            }
        }
        HSBType newHsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(brightness));
        setHSBValue(service, newHsb);
        return;
    }
    // process percent command
    if (command instanceof PercentType && !isRedefinedByCustomCommand(command) && !service.hasChannelActions(channels[0])) {
        PercentType t = (PercentType) command;
        HSBType hsb = hsbState;
        if (hsb == null) {
            hsb = new HSBType(Color.WHITE);
        }
        HSBType newHsb = new HSBType(hsb.getHue(), hsb.getSaturation(), t);
        setHSBValue(service, newHsb);
        return;
    }
    // process on/off, increase/decrease, percent type
    super.processCommand(service, command);
}
Also used : IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 30 with HSBType

use of org.openhab.core.library.types.HSBType in project openhab1-addons by openhab.

the class DmxColorItemTest method canBeSetWithHsbCommand.

@Test
public void canBeSetWithHsbCommand() throws BindingConfigParseException {
    DmxItem item = getValidInstance();
    DmxService service = Mockito.mock(DmxService.class);
    HSBType hsb = new HSBType(DecimalType.ZERO, PercentType.HUNDRED, PercentType.HUNDRED);
    item.processCommand(service, hsb);
    Mockito.verify(service).setChannelValue(3, 255);
    Mockito.verify(service).setChannelValue(4, 0);
    Mockito.verify(service).setChannelValue(5, 0);
}
Also used : DmxService(org.openhab.binding.dmx.DmxService) HSBType(org.openhab.core.library.types.HSBType) Test(org.junit.Test)

Aggregations

HSBType (org.openhab.core.library.types.HSBType)30 DecimalType (org.openhab.core.library.types.DecimalType)20 PercentType (org.openhab.core.library.types.PercentType)19 Color (java.awt.Color)8 StringType (org.openhab.core.library.types.StringType)8 DateTimeType (org.openhab.core.library.types.DateTimeType)7 Calendar (java.util.Calendar)6 ColorItem (org.openhab.core.library.items.ColorItem)6 ContactItem (org.openhab.core.library.items.ContactItem)5 DateTimeItem (org.openhab.core.library.items.DateTimeItem)5 DimmerItem (org.openhab.core.library.items.DimmerItem)5 NumberItem (org.openhab.core.library.items.NumberItem)5 RollershutterItem (org.openhab.core.library.items.RollershutterItem)5 SwitchItem (org.openhab.core.library.items.SwitchItem)5 OnOffType (org.openhab.core.library.types.OnOffType)5 Test (org.junit.Test)4 State (org.openhab.core.types.State)4 DmxService (org.openhab.binding.dmx.DmxService)3 Item (org.openhab.core.items.Item)3 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)3