SODashServer/lib/office/calendarInterface.js

332 lines
11 KiB
JavaScript

var request = require('request');
var cheerio = require('cheerio');
var t = require('./getTimeAndDate');
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 meetingStates = {
STARTING: 0,
INPROGRESS: 1,
NONE: 2,
FINISHED: 3
};
var calendarInterface = function() {
this.jsonBlock = [];
this.cachedStatus = {};
this.setJson = function(j) {
'use strict';
this.jsonBlock = j;
};
this.getJson = function() {
'use strict';
return this.jsonBlock;
};
//Console.log('Calendar synchronisation service Started.');
};
function processICAL(ical) {
'use strict';
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 workBlock = {
summary: '',
dtstart: null,
dtend: null,
description: '',
timeStart: null,
timeEnd: null,
actualEnd: null,
duration: 0,
combined: '',
uid: ''
};
var alarmFlag = false, ws, blockStep;
for (var step = 0; step < block.length; step++) {
// Logger.info(block[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) {
if (!alarmFlag) {
workBlock.description = STRING(block[step].split(segments.meetingDescID)[1]).collapseWhitespace().s;
}
}
if (blockStep.indexOf(segments.uid) >= 0) {
if (!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 = '<em>' + workBlock.timeStart + '</em> - ';
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 = workBlock.actualEnd.rewind({minutes: 5});
fiveMins = Date.create(new Date()).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;
}
var lines = ical.split('\r\n'), l = lines.length, counter = 0;
while (counter < l) {
if (lines[counter].indexOf(segments.begin) < 0) {
counter++;
} else {
var 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() {
'use strict';
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.debug(today);
logger.info('- getTodaysMeetings');
return today;
};
calendarInterface.prototype.getCachedStatus = function() {
'use strict';
return this.cachedStatus;
};
calendarInterface.prototype.getRoomStatusV2 = function(cb) {
'use strict';
var self = this;
// Var calJson = [];
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');
// Callback.call(null, new Error('Request failed'));
return;
}
//Self.jsonBlock = processICAL(body);
self.setJson(processICAL(body));
// Self.getTodaysMeetings();
var processedList = self.getTodaysMeetings();
self.cachedStatus = clone(processedList);
//Self.cachedStatus = processedList;
if (typeof cb === 'function') {
cb(processedList);
}
});
};
calendarInterface.prototype.getRoomStatus = function() {
var timeAndDate = new t.getTimeAndDate;
request('http://crmplace.com/censis/ical_server.php?type=ics&key=largemeetingroom&email=largemeetingroom@censis.org.uk', function(err, res, body) {
if (err) {
callback.call(null, new Error('Request failed'));
return;
}
var $ = cheerio.load(body);
var text = $('#element').text();
meetingInfo = $.html();
//Find today's date and time and convert to meeting info format
//getTimeNow.getTimeAndDate();
//console.log(TimeAndDate.time);
//console.log(TimeAndDate.date);
timeNow = timeAndDate.time;
dateToday = timeAndDate.date;
//Define meeting start and end identifying string
var meetingStartID = 'DTSTART;TZID=Europe/London:';
var meetingEndID = 'DTEND;TZID=Europe/London:';
var meetingDescID = 'DESCRIPTION:';
//Look for meetings taking place today
var meetingStart = new Array;
var meetingEnd = new Array;
var meetingDesc = new Array;
// Break up the file into lines.
var lines = meetingInfo.split('\n');
//Console.log(lines);
var meetingNum = 0;
for (i = 0; i < lines.length; i++) {
var n = lines[i].indexOf(meetingStartID);
if (n == -1) {
} else {
var meetingStartStr = lines[i];
var meetingDate = meetingStartStr.substring(meetingStartID.length, meetingStartID.length + 8);
if (meetingDate == dateToday.toString()) {
meetingStart[meetingNum] = lines[i];
meetingEnd[meetingNum] = lines[i + 1];
meetingDesc[meetingNum] = lines[i + 3];
meetingNum += 1;
}
}
}
if (meetingNum == 0) {
console.log('There are no meetings scheduled for today.')
} else {
console.log('Number of meetings today: ' + meetingNum);
//If timeNow is meetingStart, projectorStatus = ON
//if timeNow is within meeting, projectorStatus = ON
//if timeNow is meetingEnd, projectorStatus = OFF
var projectorStatus = 0;
//Is there a meeting on now
for (m = 0; m < meetingStart.length; m++) {
if (parseInt(timeNow) == parseInt(meetingStart[m].substring(36, 40))) {
var currentMeeting = m;
} else if ((parseInt(timeNow) <= parseInt(meetingEnd[m].substring(34, 38))) && (parseInt(timeNow) > parseInt(meetingStart[m].substring(36, 40)))) {
var currentMeeting = m;
} else {
var currentMeeting = 'None';
}
}
//If there is a meeting on now, define projector status
if (currentMeeting == 'None') {
console.log('Time now: ' + timeNow);
console.log('No meeting just now.');
} else {
console.log('Current meeting is: ' + meetingDesc[currentMeeting].substring(12, meetingDesc[currentMeeting].length));
if (parseInt(timeNow) == parseInt(meetingStart[currentMeeting].substring(36, 40))) {
return meetingStates.STARTING;
} else if ((parseInt(timeNow) <= parseInt(meetingEnd[currentMeeting].substring(34, 38))) && (parseInt(timeNow) > parseInt(meetingStart[currentMeeting].substring(36, 40)))) {
return meetingStates.INPROGRESS;
} else if (parseInt(timeNow) > parseInt(meetingEnd[currentMeeting].substring(34, 38))) {
return meetingStates.NONE;
}
console.log('Time now: ' + timeNow);
console.log('Meeting start time: ' + meetingStart[currentMeeting].substring(36, 40));
console.log('Meeting end time: ' + meetingEnd[currentMeeting].substring(34, 38));
console.log('Projector Status: ' + projectorStatus);
console.log('');
//Return projectorStatus;
//module.exports.pStatus = projectorStatus;
}
}
});
};
module.exports.calendarInterface = calendarInterface;