'use strict'; var request = require('request'); var cheerio = require('cheerio'); var log4js = require('log4js'); var logger = log4js.getLogger(); var STRING = require('string'); var util = require('util'); var Elapsed = require('elapsed'); var clone = require('clone'); require('sugar-date'); var calendarInterface = function() { this.jsonBlock = []; this.cachedStatus = {}; this.setJson = function(j) { this.jsonBlock = j; }; this.getJson = function() { return this.jsonBlock; }; }; function processICAL(ical) { var lines, l, counter; var subcounter, subBlock; logger.info('+ processICAL'); var workingBlock = []; var segments = { meetingStartID: 'DTSTART;TZID=Europe/London:', meetingEndID: 'DTEND;TZID=Europe/London:', meetingDescID: 'DESCRIPTION:', summaryID: 'SUMMARY:', begin: 'BEGIN:VEVENT', end: 'END:VEVENT', beginAlarm: 'BEGIN:VALARM', uid: 'UID:' }; function processBlock(block) { var alarmFlag, ws, blockStep; var workBlock = { summary: '', dtstart: null, dtend: null, description: '', timeStart: null, timeEnd: null, actualEnd: null, duration: 0, combined: '', uid: '' }; alarmFlag = false; for (var step = 0; step < block.length; step++) { blockStep = block[step]; if (blockStep.indexOf(segments.summaryID) >= 0) { workBlock.summary = STRING(block[step].split(segments.summaryID)[1]).collapseWhitespace().s; } if (blockStep.indexOf(segments.meetingStartID) >= 0) { ws = STRING(block[step].split(segments.meetingStartID)[1]).collapseWhitespace().s; workBlock.dtstart = Date.create(ws); } if (blockStep.indexOf(segments.meetingEndID) >= 0) { ws = STRING(block[step].split(segments.meetingEndID)[1]).collapseWhitespace().s; workBlock.dtend = Date.create(ws); workBlock.actualEnd = workBlock.dtend; } if (blockStep.indexOf(segments.meetingStartAlt) >= 0) { ws = STRING(block[step].split(segments.meetingStartAlt)[1]).collapseWhitespace().s; workBlock.dtstart = Date.create(ws); } if (blockStep.indexOf(segments.meetingEndAlt) >= 0) { ws = STRING(block[step].split(segments.meetingEndAlt)[1]).collapseWhitespace().s; workBlock.dtend = Date.create(ws); } if (blockStep.indexOf(segments.meetingDescID) >= 0 && !alarmFlag) { workBlock.description = STRING(block[step].split(segments.meetingDescID)[1]).collapseWhitespace().s; } if (blockStep.indexOf(segments.uid) >= 0 && !alarmFlag) { workBlock.uid = STRING(block[step].split(segments.uid)[1]).collapseWhitespace().s; } if (blockStep.indexOf(segments.beginAlarm) >= 0) { alarmFlag = true; } } if (workBlock.dtstart !== null) { workBlock.timeStart = workBlock.dtstart.format('{24hr}:{mm}:{ss} {tt}'); workBlock.cronStart = workBlock.dtstart.format('{m} {H} * * *'); workBlock.combined = workBlock.timeStart + ' - '; } workBlock.combined = workBlock.combined + workBlock.summary; if (workBlock.dtend !== null) { var fiveMins; workBlock.timeEnd = workBlock.actualEnd.format('{24hr}:{mm}:{ss} {tt}'); workBlock.cronStop = workBlock.actualEnd.format('{m} {H} * * *'); fiveMins = Date.create(workBlock.actualEnd).addMinutes('-5'); workBlock.cronAlert = fiveMins.format('{m} {H} * * *'); } if (workBlock.dtstart !== null && workBlock.dtend !== null) { var elapsedTime = new Elapsed(workBlock.dtstart, workBlock.dtend); workBlock.duration = elapsedTime.optimal; workBlock.combined = workBlock.combined + ', ' + elapsedTime.optimal; } return workBlock; } lines = ical.split('\r\n'); l = lines.length; counter = 0; while (counter < l) { if (lines[counter].indexOf(segments.begin) < 0) { counter++; } else { subcounter = 0; subBlock = []; while (subcounter < 75) { if (lines[counter + subcounter].indexOf(segments.end) < 0) { subBlock.push(lines[counter + subcounter]); subcounter++; } else { break; } } counter = counter + subcounter; workingBlock.push(processBlock(subBlock)); } } logger.info(workingBlock.length); logger.info('- processICAL'); return workingBlock; } calendarInterface.prototype.getTodaysMeetings = function() { logger.info('+ getTodaysMeetings'); var today = { previous: [], upcoming: [], current: {} }; var now = new Date(); var jBlock = this.getJson(); logger.debug(jBlock.length); for (var t = 0; t < jBlock.length; t++) { if (jBlock[t].dtstart.isToday()) { if (now.isBetween(jBlock[t].dtstart, jBlock[t].dtend)) { today.current = jBlock[t]; } if (jBlock[t].dtstart.isAfter(now)) { today.upcoming.push(jBlock[t]); } else { if (Object.keys(today.current).length > 0) { if (today.current.uid !== jBlock[t].uid) { today.previous.push(jBlock[t]); } } else { today.previous.push(jBlock[t]); } } } } logger.info('- getTodaysMeetings'); return today; }; calendarInterface.prototype.getCachedStatus = function() { return this.cachedStatus; }; calendarInterface.prototype.getRoomStatusV2 = function(cb) { var self = this; request('http://crmplace.com/censis/ical_server.php?type=ics&key=largemeetingroom&email=largemeetingroom@censis.org.uk', function(err, res, body) { if (err) { logger.error('Get remote Calendar Request failed'); return; } self.setJson(processICAL(body)); var processedList = self.getTodaysMeetings(); self.cachedStatus = clone(processedList); if (typeof cb === 'function') { cb(processedList); } }); }; module.exports.calendarInterface = calendarInterface;