program-manager.js
3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var util_1 = require("./util");
var ProgramManager = (function () {
function ProgramManager() {
}
Object.defineProperty(ProgramManager.prototype, "programRepository", {
get: function () {
return this._programRepository;
},
set: function (pr) {
this._programRepository = pr;
},
enumerable: true,
configurable: true
});
ProgramManager.prototype.getCurrentProgramItemId = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.findCurrentProgramSegment().then(function (programSegment) {
var currentProgramItemId = programSegment.default;
if (programSegment.schedule) {
currentProgramItemId = _this.findCurrentProgramItem(programSegment.schedule, util_1.Util.getDateInMinutes());
}
resolve(currentProgramItemId);
});
});
};
/**
* find program item in schedule, which fits
* according to current hh:mm
*/
ProgramManager.prototype.findCurrentProgramItem = function (schedule, dateInMinutes) {
var timeList = [];
var tmpSchedule = {};
dateInMinutes--; // make it not so strict, which will start one minute earlier
for (var startTime in schedule) {
if (schedule.hasOwnProperty(startTime)) {
var minutes = util_1.Util.convertToMinutes(startTime);
timeList.push(minutes);
tmpSchedule[minutes] = schedule[startTime];
}
}
// sort ascending (-)
timeList.sort(function (a, b) { return a - b; });
var last = 0;
for (var i = 0; i < timeList.length; i++) {
if (timeList[i] <= dateInMinutes) {
last = timeList[i];
}
else {
break;
}
}
return tmpSchedule[last];
};
/**
* Find the program segment
* This is dependent on the date set on the device
*/
ProgramManager.prototype.findCurrentProgramSegment = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var today = util_1.Util.getISODate();
_this.programRepository.findByType('program')
.then(function (programs) {
if (programs.length > 0) {
var program = programs[0];
var programSegmentId = void 0;
// if there is a program_segment for today else default
if (program.schedule && program.schedule[today]) {
programSegmentId = program.schedule[today];
}
else {
programSegmentId = program.default;
}
_this.programRepository
.findById(programSegmentId)
.then(function (programSegment) {
resolve(programSegment);
}).catch(function (error) {
reject("program segment not found");
});
}
else {
reject('No Program found');
}
}).catch(function (error) {
reject(error);
});
});
};
return ProgramManager;
}());
exports.ProgramManager = ProgramManager;
//# sourceMappingURL=program-manager.js.map