Added some sorting to the calendar. Using async! :)

This commit is contained in:
Martin Donnelly 2017-10-26 10:16:50 +01:00
parent 62cdf35e60
commit 3917e0aeae
4 changed files with 94 additions and 22 deletions

View File

@ -25,7 +25,7 @@ for (var t = 0; t < calHandler.calendars.length;t++)
});
*/
for (const item of calHandler.calendars)
/* for (const item of calHandler.calendars)
calHandler.getAdvancedCalV3(item)
.then((d) => {
'use strict';
@ -33,14 +33,14 @@ for (const item of calHandler.calendars)
// cal.tomorrow = cal.tomorrow.concat(d.tomorrow);
cal.week = cal.week.concat(d.week);
logger.info(cal);
console.dir(cal);
})
.catch((e) => {
'use strict';
logger.error(e);
});
});*/
/*calHandler.getSimpleCalV3(
/* calHandler.getSimpleCalV3(
'http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc')
.then((d) => {
'use strict';
@ -53,7 +53,7 @@ for (const item of calHandler.calendars)
logger.error(e);
});*/
/*calHandler.getAdvancedCalV3(calHandler.calendars[4])
/* calHandler.getAdvancedCalV3(calHandler.calendars[4])
.then((d) => {
'use strict';
// cal.today = cal.today.concat(d.today);
@ -66,3 +66,36 @@ for (const item of calHandler.calendars)
'use strict';
logger.error(e);
});*/
async function AsyncTest() {
function compare(a, b) {
if (a.ts < b.ts) {
return -1;
}
if (a.ts > b.ts) {
return 1;
}
return 0;
}
for (const item of calHandler.calendars)
await calHandler.getAdvancedCalV3(item)
.then((d) => {
'use strict';
// cal.today = cal.today.concat(d.today);
// cal.tomorrow = cal.tomorrow.concat(d.tomorrow);
cal.week = cal.week.concat(d.week);
// console.dir(cal);
})
.catch((e) => {
'use strict';
logger.error(e);
});
cal.week = cal.week.sort(compare);
console.log('COMPLETE!!');
console.log(cal.week);
}
AsyncTest();

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,7 @@ const Sugar = require('sugar');
Sugar.extend();
logger.level = 'debug';
logger.level = 'error';
function processICAL(ical) {
'use strict';
@ -139,7 +139,7 @@ function processICAL(ical) {
newBlock = Object.assign({}, _workBlock);
newBlock.dtstart = new Date(_dstart);
newBlock.dtend = new Date(_dend);
logger.info(newBlock.dtstart.medium());
// logger.info(newBlock.dtstart.medium());
recurArray.push(newBlock);
_dstart.advance({ 'year':1 });
@ -162,7 +162,7 @@ function processICAL(ical) {
newBlock = Object.assign({}, _workBlock);
newBlock.dtstart = new Date(_dstart);
newBlock.dtend = new Date(_dend);
logger.info(newBlock.dtstart.medium());
// logger.info(newBlock.dtstart.medium());
recurArray.push(newBlock);
_dstart.addMonths(interval);
@ -193,7 +193,7 @@ function processICAL(ical) {
newBlock = Object.assign({}, _workBlock);
newBlock.dtstart = new Date(_findSun).addMilliseconds(_workBlock.timeStartMS);
newBlock.dtend = new Date(_findSun).addMilliseconds(_workBlock.timeEndMS);
logger.info(newBlock.dtstart.medium());
// logger.info(newBlock.dtstart.medium());
recurArray.push(newBlock);
// all done, next.
_dstart.reset('month').addDays(32);
@ -216,7 +216,7 @@ function processICAL(ical) {
newBlock = Object.assign({}, _workBlock);
newBlock.dtstart = new Date(_dstart);
newBlock.dtend = new Date(_dend);
logger.info(newBlock.dtstart.medium());
// logger.info(newBlock.dtstart.medium());
recurArray.push(newBlock);
_dstart.addWeeks(interval);
@ -224,7 +224,7 @@ function processICAL(ical) {
}
while(_dstart < dateLimit);
}
return recurArray;
}
@ -246,7 +246,8 @@ function processICAL(ical) {
'combined': '',
'recur': null,
'timeStartMS': null,
'timeEndMS': null
'timeEndMS': null,
'ts' : null
};
let alarmFlag = false, ws, blockStep;
for (let step = 0; step < block.length; step++) {
@ -320,7 +321,6 @@ function processICAL(ical) {
workBlock.timeEnd = Sugar.Date(workBlock.dtend).format('{24hr}:{mm}:{ss}').raw;
workBlock.timeEndMS = ((workBlock.dtend.getHours() * 60 * 60 ) + (workBlock.dtend.getMinutes() * 60 ) + workBlock.dtend.getSeconds()) * 1000;
}
if (workBlock.dtstart !== null && workBlock.dtend !== null) {
const elapsedTime = new Elapsed(workBlock.dtstart, workBlock.dtend);
@ -372,13 +372,24 @@ function processICAL(ical) {
if (Array.isArray(b))
logger.error('!returned an array...');
else
if (b.dtstart !== null)
if (b.dtstart !== null) {
b.ts = b.dtstart.format('{X}');
workingBlock.push(b);
}
}
else {
// logger.warn('We need to spread the recurrance!!');
// logger.debug(b);
const recurBlocks = buildRecurranceArray(b);
let recurBlocks = buildRecurranceArray(b);
if (recurBlocks)
recurBlocks = recurBlocks.map((item) => {
if (item.dtstart)
item.ts = item.dtstart.format('{X}');
return item;
});
if (recurBlocks && recurBlocks.length > 0)
workingBlock = workingBlock.concat(recurBlocks);
}
@ -519,7 +530,7 @@ module.exports = {
});
}
catch (e) {
console.log(e);
logger.error(e);
}
}, 'getSimpleCalV3': function(url) {
'use strict';
@ -549,7 +560,7 @@ module.exports = {
});
}
catch (e) {
console.log(e);
logger.error(e);
return reject(e);
}
@ -591,7 +602,7 @@ module.exports = {
});
}
catch (e) {
console.log(e);
logger.error(e);
return reject(e);
}

View File

@ -325,7 +325,17 @@ module.exports = {
this.refreshTrain();
this.refreshWeather();
},
'preLoadToday': function () {
'preLoadToday': async function () {
function compare(a, b) {
if (a.ts < b.ts) {
return -1;
}
if (a.ts > b.ts) {
return 1;
}
return 0;
}
module.exports.getTodayDate();
todayCache.data.cal = { 'today': [], 'tomorrow': [], 'week': [] };
weather.newDoGetWeather()
@ -384,7 +394,7 @@ module.exports = {
logger.error(e);
});
for (let t = 0; t < calHandler.calendars.length; t++)
/*for (let t = 0; t < calHandler.calendars.length; t++)
calHandler.getAdvancedCalV3(calHandler.calendars[t])
.then((d) => {
'use strict';
@ -395,7 +405,25 @@ module.exports = {
.catch((e) => {
'use strict';
logger.error(e);
});
});*/
for (const item of calHandler.calendars)
await calHandler.getAdvancedCalV3(item)
.then((d) => {
todayCache.data.cal.today = todayCache.data.cal.today.concat(d.today);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.concat(d.tomorrow);
todayCache.data.cal.week = todayCache.data.cal.week.concat(d.week);
})
.catch((e) => {
'use strict';
logger.error(e);
});
console.log('>> SORT!!');
todayCache.data.cal.today = todayCache.data.cal.today.sort(compare);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.sort(compare);
todayCache.data.cal.week = todayCache.data.cal.week.sort(compare);
swedishWord.getSwedishWord()
.then((d) => {