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);
}
}
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;
}
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());
}
}
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;
}
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);
}
}
Aggregations