daily pastebin goal
33%
SHARE
TWEET

openremote calendar class

a guest Oct 12th, 2013 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.openremote.controller.protocol;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.Collection;
  6. import net.fortuna.ical4j.data.CalendarBuilder;
  7. import net.fortuna.ical4j.data.ParserException;
  8. import net.fortuna.ical4j.filter.Filter;
  9. import net.fortuna.ical4j.filter.PeriodRule;
  10. import net.fortuna.ical4j.filter.Rule;
  11. import net.fortuna.ical4j.model.Calendar;
  12. import net.fortuna.ical4j.model.Component;
  13. import net.fortuna.ical4j.model.DateTime;
  14. import net.fortuna.ical4j.model.Dur;
  15. import net.fortuna.ical4j.model.Period;
  16. import net.fortuna.ical4j.model.component.VEvent;
  17.  
  18. /**
  19.  * This is a sample class to launch a rule.
  20.  */
  21. public class CalendarProvider {
  22.        
  23.         public static Calendar calendar;
  24.         public static Object calendarLock = new Object();
  25.         //private static CalendarWatcher watcher;
  26.                
  27.         public static void loadCalendar(String calendarPath) throws IOException, ParserException {
  28.                 synchronized(calendarLock) {
  29.                         FileInputStream fin = new FileInputStream(calendarPath);
  30.                         CalendarBuilder builder = new CalendarBuilder();
  31.                         calendar = builder.build(fin);
  32.                 }
  33.         }
  34.        
  35.         public static float getCalendarTemperature(final String calendarPath) {
  36.                 try {
  37.                         if (calendar == null) {
  38.                                 loadCalendar(calendarPath);
  39.                         }                      
  40.                         java.util.Calendar today = java.util.Calendar.getInstance();
  41.  
  42.                         Period period = new Period(new DateTime(today.getTime()), new Dur(0, 0, 1, 0));
  43.                         Filter filter = new Filter(new Rule[] {new PeriodRule(period)}, Filter.MATCH_ALL);
  44.                        
  45.                         synchronized(calendarLock) {
  46.                                 @SuppressWarnings("rawtypes")
  47.                                 Collection eventsToday = filter.filter(calendar.getComponents(Component.VEVENT));
  48.                                
  49.                                 for (Object o : eventsToday) {
  50.                                         VEvent event = (VEvent) o;
  51.                                         try {
  52.                                                   float value = Float.parseFloat(event.getDescription().getValue());
  53.                                                   return value;
  54.                                           } catch (Exception e) { }
  55.                                 }
  56.                         }
  57.                 } catch (Exception e) {
  58.                         System.out.println(e.toString());
  59.                 }
  60.                
  61.                 return -1;
  62.         }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top