Search in sources :

Example 1 with CalDavQuery

use of org.openhab.io.caldav.CalDavQuery in project openhab1-addons by openhab.

the class CalDavBinding method bindingChanged.

@Override
public void bindingChanged(BindingProvider provider, String itemName) {
    if (provider.getItemNames().contains(itemName)) {
        final CalDavNextEventConfig config = ((CalDavBindingProvider) provider).getConfig(itemName);
        List<CalDavEvent> events = calDavLoader.getEvents(new CalDavQuery(this.readCalendars, DateTime.now()));
        this.updateItemState(config, events);
    }
}
Also used : CalDavEvent(org.openhab.io.caldav.CalDavEvent) CalDavBindingProvider(org.openhab.binding.caldav_command.CalDavBindingProvider) CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Example 2 with CalDavQuery

use of org.openhab.io.caldav.CalDavQuery in project openhab1-addons by openhab.

the class CalDavBinding method getQueryForConfig.

private CalDavQuery getQueryForConfig(CalDavConfig config) {
    CalDavQuery query = new CalDavQuery(config.getCalendar(), DateTime.now(), Sort.ASCENDING);
    query.setFilterName(config.getFilterName());
    query.setFilterCategory(config.getFilterCategory());
    query.setFilterCategoryMatchesAny(config.getCategoriesFiltersAny());
    return query;
}
Also used : CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Example 3 with CalDavQuery

use of org.openhab.io.caldav.CalDavQuery in project openhab1-addons by openhab.

the class CalDavBinding method doActionInitial.

private void doActionInitial() {
    List<CalDavEvent> events = calDavLoader.getEvents(new CalDavQuery(this.readCalendars, DateTime.now(), Sort.ASCENDING));
    Map<String, EventUtils.EventContent> map = new HashMap<String, EventUtils.EventContent>();
    for (CalDavEvent calDavEvent : events) {
        final List<EventUtils.EventContent> parseContent = EventUtils.parseContent(calDavEvent, this.itemRegistry, null);
        for (EventUtils.EventContent eventContent : parseContent) {
            if (disabledItems.contains(eventContent.getItem().getName())) {
                // changing this is item is disabled, do not change it
                continue;
            }
            EventUtils.EventContent currentEventContent = map.get(eventContent.getItem().getName());
            if (eventContent.getTime().isBefore(DateTime.now()) && (currentEventContent == null || eventContent.getTime().isAfter(currentEventContent.getTime()))) {
                map.put(eventContent.getItem().getName(), eventContent);
            }
        }
    }
    for (EventUtils.EventContent currentEventContent : map.values()) {
        eventPublisher.sendCommand(currentEventContent.getItem().getName(), currentEventContent.getCommand());
        logger.debug("setting initial value for {} to {}", currentEventContent.getItem().getName(), currentEventContent.getCommand());
    }
}
Also used : HashMap(java.util.HashMap) CalDavEvent(org.openhab.io.caldav.CalDavEvent) EventUtils(org.openhab.io.caldav.EventUtils) CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Example 4 with CalDavQuery

use of org.openhab.io.caldav.CalDavQuery in project openhab1-addons by openhab.

the class CaldavPersistenceService method query.

@Override
public Iterable<HistoricItem> query(final FilterCriteria filter) {
    List<CalDavEvent> events = calDavLoader.getEvents(new CalDavQuery(calendarId));
    List<HistoricItem> outList = new ArrayList<HistoricItem>();
    for (CalDavEvent calDavEvent : events) {
        if (filter.getBeginDate() != null && calDavEvent.getEnd().toDate().before(filter.getBeginDate())) {
            continue;
        }
        if (filter.getEndDate() != null && calDavEvent.getStart().toDate().after(filter.getEndDate())) {
            continue;
        }
        Item item = null;
        try {
            item = this.itemRegistry.getItem(filter.getItemName());
        } catch (ItemNotFoundException e) {
            logger.error("item {} could not be found", filter.getItemName());
            continue;
        }
        final List<EventUtils.EventContent> parseContent = EventUtils.parseContent(calDavEvent, item);
        for (EventUtils.EventContent eventContent : parseContent) {
            if (filter.getBeginDate() != null && eventContent.getTime().toDate().before(filter.getBeginDate())) {
                continue;
            }
            if (filter.getEndDate() != null && eventContent.getTime().toDate().after(filter.getEndDate())) {
                continue;
            }
            final State eventState = eventContent.getState();
            if (filter.getState() != null && filter.getOperator() != null) {
                switch(filter.getOperator()) {
                    case EQ:
                        {
                            if (!filter.getState().equals(eventState)) {
                                continue;
                            }
                            break;
                        }
                    case NEQ:
                        {
                            if (filter.getState().equals(eventState)) {
                                continue;
                            }
                            break;
                        }
                    case LTE:
                        {
                            if (eventState instanceof DecimalType && filter.getState() instanceof DecimalType) {
                                if (((DecimalType) eventState).longValue() > ((DecimalType) filter.getState()).longValue()) {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            break;
                        }
                    case GTE:
                        {
                            if (eventState instanceof DecimalType && filter.getState() instanceof DecimalType) {
                                if (((DecimalType) eventState).longValue() < ((DecimalType) filter.getState()).longValue()) {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            break;
                        }
                    case LT:
                        {
                            if (eventState instanceof DecimalType && filter.getState() instanceof DecimalType) {
                                if (((DecimalType) eventState).longValue() >= ((DecimalType) filter.getState()).longValue()) {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            break;
                        }
                    case GT:
                        {
                            if (eventState instanceof DecimalType && filter.getState() instanceof DecimalType) {
                                if (((DecimalType) eventState).longValue() <= ((DecimalType) filter.getState()).longValue()) {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            break;
                        }
                }
            }
            // just filtered events are here...
            final CaldavItem caldavItem = new CaldavItem(filter.getItemName(), eventState, eventContent.getTime().toDate());
            caldavItem.setEvent(calDavEvent);
            outList.add(caldavItem);
        }
    }
    Collections.sort(outList, new Comparator<HistoricItem>() {

        @Override
        public int compare(HistoricItem arg0, HistoricItem arg1) {
            if (filter.getOrdering().equals(FilterCriteria.Ordering.ASCENDING)) {
                return (int) (arg0.getTimestamp().getTime() - arg1.getTimestamp().getTime());
            } else {
                return (int) (arg1.getTimestamp().getTime() - arg0.getTimestamp().getTime());
            }
        }
    });
    if (outList.size() < filter.getPageNumber() * filter.getPageSize()) {
        return Collections.emptyList();
    }
    outList = outList.subList(filter.getPageNumber() * filter.getPageSize(), Math.min((filter.getPageNumber() * filter.getPageSize()) + filter.getPageSize(), outList.size()));
    logger.trace("result size for query: {}", outList.size());
    return outList;
}
Also used : EventUtils(org.openhab.io.caldav.EventUtils) ArrayList(java.util.ArrayList) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) CalDavEvent(org.openhab.io.caldav.CalDavEvent) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) CalDavQuery(org.openhab.io.caldav.CalDavQuery) HistoricItem(org.openhab.core.persistence.HistoricItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 5 with CalDavQuery

use of org.openhab.io.caldav.CalDavQuery in project openhab1-addons by openhab.

the class CalDavBinding method handleForEventPreview.

private synchronized void handleForEventPreview() {
    CalDavBindingProvider provider = getCalDavBindingProvider();
    if (provider == null) {
        logger.error("cannot find any provider");
        return;
    }
    List<CalDavEvent> events = calDavLoader.getEvents(new CalDavQuery(this.readCalendars, DateTime.now()));
    for (String configItemName : provider.getItemNames()) {
        final CalDavNextEventConfig config = provider.getConfig(configItemName);
        this.updateItemState(config, events);
    }
}
Also used : CalDavEvent(org.openhab.io.caldav.CalDavEvent) CalDavBindingProvider(org.openhab.binding.caldav_command.CalDavBindingProvider) CalDavQuery(org.openhab.io.caldav.CalDavQuery)

Aggregations

CalDavQuery (org.openhab.io.caldav.CalDavQuery)6 CalDavEvent (org.openhab.io.caldav.CalDavEvent)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CalDavBindingProvider (org.openhab.binding.caldav_command.CalDavBindingProvider)2 EventUtils (org.openhab.io.caldav.EventUtils)2 List (java.util.List)1 CalDavBindingProvider (org.openhab.binding.caldav_personal.CalDavBindingProvider)1 Item (org.openhab.core.items.Item)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 DecimalType (org.openhab.core.library.types.DecimalType)1 HistoricItem (org.openhab.core.persistence.HistoricItem)1 State (org.openhab.core.types.State)1