program-manager.js 3.34 KB
"use strict";
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 = {};
        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;