use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.
the class CalDavLoaderImpl method addEventToMap.
public synchronized void addEventToMap(EventContainer eventContainer, boolean createTimer) {
CalendarRuntime calendarRuntime = EventStorage.getInstance().getEventCache().get(eventContainer.getCalendarId());
ConcurrentHashMap<String, EventContainer> eventContainerMap = calendarRuntime.getEventMap();
if (eventContainerMap.containsKey(eventContainer.getEventId())) {
EventContainer eventContainerOld = eventContainerMap.get(eventContainer.getEventId());
// event is already in map
if (eventContainer.getLastChanged().isAfter(eventContainerOld.getLastChanged())) {
log.debug("event is already in event map and newer -> delete the old one, reschedule timer");
// cancel old jobs
for (String timerKey : eventContainerOld.getTimerMap()) {
try {
this.scheduler.deleteJob(JobKey.jobKey(timerKey));
} catch (SchedulerException e) {
log.error("cannot cancel event with job-id: " + timerKey, e);
}
}
eventContainerOld.getTimerMap().clear();
// override event
eventContainerMap.put(eventContainer.getEventId(), eventContainer);
for (EventNotifier notifier : eventListenerList) {
for (CalDavEvent event : eventContainerOld.getEventList()) {
log.trace("notify listener... {}", notifier);
try {
notifier.eventRemoved(event);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
}
for (EventNotifier notifier : eventListenerList) {
for (CalDavEvent event : eventContainer.getEventList()) {
log.trace("notify listener... {}", notifier);
try {
notifier.eventLoaded(event);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
}
if (createTimer) {
int index = 0;
for (CalDavEvent event : eventContainer.getEventList()) {
if (event.getEnd().isAfterNow()) {
try {
createJob(eventContainer, event, index);
} catch (SchedulerException e) {
log.error("cannot create jobs for event '{}': ", event.getShortName(), e.getMessage());
}
}
index++;
}
}
} else {
// event is already in map and not updated (eventContainerold and eventContainer have the same
// "lastchanged" date) : we need to reschedule possible new events from recurrent events
log.debug("event is already in map and not updated, we NEED to update eventlist and schedule new events jobs");
ArrayList<CalDavEvent> eventsToAdd = new ArrayList<>();
for (CalDavEvent event : eventContainer.getEventList()) {
boolean eventAlreadyKnown = false;
for (CalDavEvent oldevent : eventContainerOld.getEventList()) {
if (event.equals(oldevent)) {
eventAlreadyKnown = true;
log.trace("events match");
} else {
log.trace("eventsarenotequal : {} - {} - {} - {} - {}", event.getId(), event.getName(), event.getStart(), event.getEnd(), event.getLastChanged());
log.trace("eventsarenotequal : {} - {} - {} - {} - {}", oldevent.getId(), oldevent.getName(), oldevent.getStart(), oldevent.getEnd(), oldevent.getLastChanged());
}
}
if (!eventAlreadyKnown) {
eventsToAdd.add(event);
}
}
// add only new events
for (CalDavEvent event : eventsToAdd) {
if (event.getEnd().isAfterNow()) {
eventContainerOld.getEventList().add(event);
for (EventNotifier notifier : eventListenerList) {
log.trace("notify listener... {}", notifier);
try {
notifier.eventLoaded(event);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
if (createTimer) {
try {
log.trace("creating job for event {} ", event.getShortName());
createJob(eventContainerOld, event, eventContainerOld.getEventList().size() - 1);
// -1 because we already added the event to eventContainerOld
} catch (SchedulerException e) {
log.error("cannot create jobs for event '{}': ", event.getShortName(), e.getMessage());
}
}
}
}
// update eventcontainer's calculateduntil
eventContainerOld.setCalculatedUntil(eventContainer.getCalculatedUntil());
}
} else {
// event is new
eventContainerMap.put(eventContainer.getEventId(), eventContainer);
log.trace("listeners for events: {}", eventListenerList.size());
for (EventNotifier notifier : eventListenerList) {
for (CalDavEvent event : eventContainer.getEventList()) {
log.trace("notify listener... {}", notifier);
try {
notifier.eventLoaded(event);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
}
if (createTimer) {
int index = 0;
for (CalDavEvent event : eventContainer.getEventList()) {
if (event.getEnd().isAfterNow()) {
try {
createJob(eventContainer, event, index);
} catch (SchedulerException e) {
log.error("cannot create jobs for event: " + event.getShortName());
}
}
index++;
}
}
}
}
use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.
the class EventReloaderJob method removeDeletedEvents.
private synchronized void removeDeletedEvents(String calendarKey, List<String> oldMap) {
final CalendarRuntime eventRuntime = EventStorage.getInstance().getEventCache().get(calendarKey);
for (String filename : oldMap) {
EventContainer eventContainer = eventRuntime.getEventContainerByFilename(filename);
if (eventContainer == null) {
log.error("cannot find event container for filename: {}", filename);
continue;
}
// cancel old jobs
for (String jobId : eventContainer.getTimerMap()) {
try {
String group;
if (jobId.startsWith(CalDavLoaderImpl.JOB_NAME_EVENT_START)) {
group = CalDavLoaderImpl.JOB_NAME_EVENT_START;
} else if (jobId.startsWith(CalDavLoaderImpl.JOB_NAME_EVENT_END)) {
group = CalDavLoaderImpl.JOB_NAME_EVENT_END;
} else {
throw new SchedulerException("unknown job id: " + jobId);
}
boolean deleteJob = CalDavLoaderImpl.instance.getScheduler().deleteJob(JobKey.jobKey(jobId, group));
log.debug("old job ({}) deleted? {}", jobId, deleteJob);
} catch (SchedulerException e) {
log.error("cannot delete job '{}'", jobId);
}
}
eventContainer.getTimerMap().clear();
for (EventNotifier notifier : CalDavLoaderImpl.instance.getEventListenerList()) {
for (CalDavEvent event : eventContainer.getEventList()) {
try {
notifier.eventRemoved(event);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
}
ConcurrentHashMap<String, EventContainer> eventContainerMap = eventRuntime.getEventMap();
if (eventContainer != null) {
this.removeFromDisk(eventContainer);
log.debug("remove deleted event: {}", eventContainer.getEventId());
eventContainerMap.remove(eventContainer.getEventId());
}
}
}
use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.
the class EventReloaderJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
final String config = context.getJobDetail().getJobDataMap().getString(KEY_CONFIG);
CalendarRuntime eventRuntime = EventStorage.getInstance().getEventCache().get(config);
log.debug("running EventReloaderJob for config : {}", config);
// reload cached events (if necessary)
if (!cachedEventsLoaded.containsKey(config)) {
try {
log.debug("reload cached events for config: {}", eventRuntime.getConfig().getKey());
for (File fileCalendarKeys : new File(CalDavLoaderImpl.CACHE_PATH).listFiles()) {
if (!eventRuntime.getConfig().getKey().equals(Util.getFilename(fileCalendarKeys.getName()))) {
log.trace("not our config : {}", Util.getFilename(fileCalendarKeys.getName()));
continue;
}
log.trace("found our config : {}", Util.getFilename(fileCalendarKeys.getName()));
final Collection<File> icsFiles = FileUtils.listFiles(fileCalendarKeys, new String[] { "ics" }, false);
for (File icsFile : icsFiles) {
try {
FileInputStream fis = new FileInputStream(icsFile);
log.debug("loading events from file : {}", icsFile);
loadEvents(Util.getFilename(icsFile.getAbsolutePath()), new org.joda.time.DateTime(icsFile.lastModified()), fis, eventRuntime.getConfig(), new ArrayList<String>(), true);
} catch (IOException e) {
log.error("cannot load events for file: " + icsFile, e);
} catch (ParserException e) {
log.error("cannot load events for file: " + icsFile, e);
}
}
break;
}
} catch (Throwable e) {
log.error("cannot load events", e);
} finally {
cachedEventsLoaded.put(config, true);
}
}
try {
log.debug("loading events for config: " + config);
List<String> oldEventIds = new ArrayList<String>();
for (EventContainer eventContainer : eventRuntime.getEventMap().values()) {
oldEventIds.add(eventContainer.getFilename());
log.debug("old eventcontainer -- id : {} -- filename : {} -- calcuntil : {} -- lastchanged : {} -- ishistoric : {}", eventContainer.getEventId(), eventContainer.getFilename(), eventContainer.getCalculatedUntil(), eventContainer.getLastChanged(), eventContainer.isHistoricEvent());
if (log.isDebugEnabled()) {
for (int i = 0; i < eventContainer.getEventList().size(); i++) {
CalDavEvent elem = eventContainer.getEventList().get(i);
log.debug("old eventlist contient l'evenement : {} -- deb : {} -- fin : {} -- lastchang {}", elem.getName(), elem.getStart(), elem.getEnd(), elem.getLastChanged());
}
}
}
loadEvents(eventRuntime, oldEventIds);
// stop all events in oldMap
removeDeletedEvents(config, oldEventIds);
for (EventNotifier notifier : CalDavLoaderImpl.instance.getEventListenerList()) {
try {
notifier.calendarReloaded(config);
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
// print All scheduled jobs :
if (log.isDebugEnabled()) {
log.debug("jobs scheduled : ");
Scheduler scheduler = CalDavLoaderImpl.instance.getScheduler();
for (String groupName : CalDavLoaderImpl.instance.getScheduler().getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
String jobName = jobKey.getName();
String jobGroup = jobKey.getGroup();
// get job's trigger
List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
Date nextFireTime = triggers.get(0).getNextFireTime();
log.debug("[job] : {} - [groupName] : {} - {}", jobName, jobGroup, nextFireTime);
}
}
}
} catch (SardineException e) {
log.error("error while loading calendar entries: {} ({} - {} )", e.getMessage(), e.getStatusCode(), e.getResponsePhrase(), e);
throw new JobExecutionException("error while loading calendar entries", e, false);
} catch (Exception e) {
log.error("error while loading calendar entries: {}", e.getMessage(), e);
throw new JobExecutionException("error while loading calendar entries", e, false);
}
}
use of org.openhab.io.caldav.CalDavEvent in project openhab1-addons by openhab.
the class CalDavBinding method updateItemState.
private void updateItemState(CalDavNextEventConfig config, List<CalDavEvent> events) {
String itemName = config.getItemNameToListenTo();
String itemNamePreview = config.getItemName();
logger.trace("update item state for item: {}", itemName);
Command state = null;
DateTime time = null;
if (calDavLoader == null) {
logger.warn("caldav loader is not set");
return;
}
for (CalDavEvent calDavEvent : events) {
try {
final Item item = this.itemRegistry.getItem(itemName);
final List<EventUtils.EventContent> parseContent = EventUtils.parseContent(calDavEvent, item);
for (EventUtils.EventContent eventContent : parseContent) {
if (!eventContent.getTime().isBefore(DateTime.now()) && (time == null || time.isAfter(eventContent.getTime()))) {
time = eventContent.getTime();
state = eventContent.getCommand();
}
}
} catch (ItemNotFoundException e) {
logger.error("item {} could not be found", itemName);
}
}
if (time == null && config.getType() != CalDavType.DISABLE) {
// no item found
eventPublisher.postUpdate(itemNamePreview, org.openhab.core.types.UnDefType.UNDEF);
return;
}
CalDavType type = config.getType();
logger.trace("handling event of type: {}", type);
if (type == CalDavType.VALUE) {
logger.debug("setting value for '{}' to: {}", itemNamePreview, state);
eventPublisher.sendCommand(itemNamePreview, state);
} else if (type == CalDavType.DATE) {
Command c = new DateTimeType(FORMATTER.print(time));
logger.debug("setting value for '{}' to: {}", itemNamePreview, c);
eventPublisher.sendCommand(itemNamePreview, c);
} else if (type == CalDavType.DISABLE) {
// nothing to do
return;
} else {
logger.warn("unhandled type: {}", type);
}
}
use of org.openhab.io.caldav.CalDavEvent 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);
}
}
Aggregations