program-manager.ts
3.31 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
97
98
99
import {ProgramRepository} from './program-repository';
import {Util} from './util';
import {ProgramItem, PROGRAM_ITEM_TYPE_SLIDESHOW, PROGRAM_ITEM_TYPE_VIDEO } from './program-item/program-item'
export class ProgramManager {
protected _programRepository:ProgramRepository;
set programRepository(pr:ProgramRepository) {
this._programRepository = pr;
}
get programRepository() : ProgramRepository {
return this._programRepository;
}
getCurrentProgramItemId() : Promise<string> {
return new Promise<string> ((resolve, reject) => {
this.findCurrentProgramSegment().then(programSegment => {
let currentProgramItemId = programSegment.default;
if (programSegment.schedule) {
currentProgramItemId = this.findCurrentProgramItem(programSegment.schedule, Util.getDateInMinutes());
}
resolve(currentProgramItemId);
});
});
}
/**
* find program item in schedule, which fits
* according to current hh:mm
*/
findCurrentProgramItem(schedule:any, dateInMinutes:number) : string {
let timeList:any = [];
let tmpSchedule:any = {};
for (let startTime in schedule) {
if (schedule.hasOwnProperty(startTime)) {
let minutes = Util.convertToMinutes(startTime);
timeList.push(minutes);
tmpSchedule[minutes] = schedule[startTime];
}
}
// sort ascending (-)
timeList.sort((a,b) => { return a-b; });
let last = 0;
for (let 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
*/
findCurrentProgramSegment() : Promise<any> {
return new Promise<any>((resolve, reject) => {
let today = Util.getISODate();
this.programRepository.findByType('program')
.then(programs => {
if (programs.length > 0) {
let program:any = programs[0];
let programSegmentId;
// 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(programSegment => {
resolve(programSegment);
}).catch(error => {
reject("program segment not found");
});
} else {
reject('No Program found');
}
}).catch(error => {
reject(error);
});
});
}
}