Example #1
0
function caldavLoad() {
  var totalParse = 0;
  dav.createAccount({ server: davicalServer, xhr: xhr })
  .then(function(account) {
    global.debugger('[CALENDARINITIALISE] - CALDAV server connection successful')
    account.calendars.forEach(function(calendar, calendarIndex, allcals) {
      if (calendar.displayName.substr(calendar.displayName.length - 5) != 'Inbox') {
        request(calendar.url, calParse)
        .auth('roombookings', 'roombookings', false)
        .on('response', function(response) {
          global.debugger('[CALENDARDOWNLOAD] - Calendar: ' + calendar.displayName + ', HTTP GET - Status: ' + response.statusCode + ', Content: ' + response.headers['content-type'])
        })
        .on('error', function(err) {
          global.debugger('[CALENDARDOWNLOAD] - Get error: ' + calendar.displayName + ' ' + err)
        })
        function calParse(err, resp, body) {
          if (resp && resp.statusCode == 200) {
            var parseCount = 0,
                eventarray = [],
                vcalendar = new ICAL.Component(icaljs.parse(body)),
                vevents = vcalendar.getAllSubcomponents(),
                colour = colourSelector();

            vevents.forEach(function(vevent){
              var event = new ICAL.Event(vevent);
              if (event.summary) {
                eventarray.push(parseEvent(calendar, event, vevent, colour));
                parseCount++
                totalParse++
              }
            })
            global.debugger('[CALENDARPARSER] - Calendar: ' + calendar.displayName + ', JSON PARSE - Events: ' + parseCount)
            calID++
            resourceData.push({id: calID, title: calendar.displayName, color: colour, events: eventarray, method: 'caldav'});
            calendarNames.push(calendar.displayName)
            calendarHashes.push(calendar.ctag)
            if (resourceData.length == account.calendars.filter(function(element, index, array){if (element.displayName.substr(element.displayName.length - 5) != 'Inbox') { return true } else { return false }}).length + iPadCalendars.length) {
              global.debugger('[CALENDARDOWNLOAD] - Calendars downloaded: ' + calID )
              global.debugger('[CALENDARPARSER] - Events parsed: ' + totalParse )
              global.debugger('[CALENDARINITIALISE] - Update broadcast to clients')
              io.emit('resourceUpdate', resourceData)
              // Kick off polling schedule
              setTimeout(function(){
                schedule.scheduleJob(calPollSchedule, function(){
                  caldavPoll();
                });
              }, 120000);
            }
          }
          else {
            global.debugger('[CALENDARINITIALISE] - CALDAV server HTTP error')
          }
        }
      }
    });
  })
};
Example #2
0
function caldavPoll() {
  var polledCalendars = [];
  dav.createAccount({ server: davicalServer, xhr: xhr })
  .then(function(account) {
    global.debugger('[CALENDARPOLL] - CALDAV server connection successful')
    setTimeout(function(){
      account.calendars.forEach(function(calendar, calendarIndex, allcals) {
        if (calendar.displayName.substr(calendar.displayName.length - 5) != 'Inbox') {
          polledCalendars.push(calendar)
        }
        if (polledCalendars.length > 0 && polledCalendars.length == account.calendars.filter(function(element, index, array){if (element.displayName.substr(element.displayName.length - 5) != 'Inbox') { return true } else { return false }}).length) {
          global.debugger('[CALENDARPOLL] - Polling finished')
          setTimeout(function(){
            checkCalendars(polledCalendars)
          }, 2500);
        }
      });
    }, 2500);
  })
}