bundle.js 11.9 KB
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var events = require('events');

function __extends(d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

var Util = (function () {
    function Util() {
    }
    Util.getISODate = function () {
        return (new Date()).toISOString().slice(0, 10);
    };
    Util.getDateInMinutes = function () {
        var now = new Date();
        return (now.getHours() * 60) + now.getMinutes();
    };
    /**
     * convert a time input to minutes
     * e.g. 23:59 = 1439
     */
    Util.convertToMinutes = function (time) {
        var times = time.split(":");
        var convered = (parseInt(times[0]) * 60) + parseInt(times[1]);
        return (convered >= 0 && convered <= 1439) ? convered : 0;
    };
    Util.calculateNextMinute = function () {
        return (60 - (Math.round((new Date()).getTime() / 1000) % 60)) * 1000;
    };
    return Util;
}());

var STATE_START = "start";
var STATE_STOP = "stop";
var Player = (function (_super) {
    __extends(Player, _super);
    function Player() {
        _super.call(this);
        this._minutesReplication = 3;
        this._replicationRetry = 10000;
        this._currentProgramItemId = '';
        this._currentReplicationCounter = 0;
        this._state = STATE_STOP;
    }
    Object.defineProperty(Player.prototype, "state", {
        set: function (st) {
            this._state = st;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Player.prototype, "programManager", {
        get: function () {
            return this._programManager;
        },
        set: function (pm) {
            this._programManager = pm;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Player.prototype, "programRepository", {
        get: function () {
            return this._programRepository;
        },
        set: function (pr) {
            this._programRepository = pr;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Player.prototype, "minutesReplication", {
        get: function () {
            return this._minutesReplication;
        },
        set: function (mr) {
            this._minutesReplication = mr;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Player.prototype, "replicationRetry", {
        get: function () {
            return this._replicationRetry;
        },
        set: function (rr) {
            this._replicationRetry = rr;
        },
        enumerable: true,
        configurable: true
    });
    Player.prototype.triggerReplication = function () {
        var _this = this;
        console.info("digsig-player-service: trigger replication");
        return this.programRepository.replicate()
            .then(function (changes) {
            _this._currentReplicationCounter = 0;
            _this.trigger(function () { _this.triggerProgramItemId(changes); });
        })
            .catch(function (error) {
            _this.trigger(function () { _this.triggerReplication(); }, _this.replicationRetry);
            _this.emit('error', error);
        });
    };
    Player.prototype.triggerProgramItemId = function (changes) {
        var _this = this;
        if (changes === void 0) { changes = false; }
        console.info("digsig-player-service: trigger program item id");
        this.programManager.getCurrentProgramItemId()
            .then(function (programItemId) {
            _this._currentReplicationCounter++;
            // if there is a new program item id trigger play
            // else (1) calculate next potential program change point
            // or (2) trigger replication
            if (programItemId && (programItemId != _this._currentProgramItemId || changes)) {
                _this._currentProgramItemId = programItemId;
                _this.emit('play', programItemId);
            }
            if (_this._currentReplicationCounter >= _this._minutesReplication) {
                _this.triggerReplication();
            }
            else {
                _this.trigger(function () { _this.triggerProgramItemId(); }, Util.calculateNextMinute());
            }
        });
    };
    Player.prototype.trigger = function (func, milliseconds) {
        if (milliseconds === void 0) { milliseconds = 0; }
        if (this._state === STATE_START) {
            setTimeout(function () { func(); }, milliseconds);
        }
    };
    Player.prototype.start = function () {
        if (this._state === STATE_STOP) {
            this.triggerReplication();
            this._state = STATE_START;
        }
    };
    Player.prototype.stop = function () {
        this._state = STATE_STOP;
    };
    return Player;
}(events.EventEmitter));

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.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.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.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;
}());

var PROGRAM_ITEM_TYPE_SLIDESHOW = "slideshow";
var PROGRAM_ITEM_TYPE_VIDEO = "video";
var ProgramItem = (function () {
    function ProgramItem() {
        this._type = "";
        this._data = {};
    }
    Object.defineProperty(ProgramItem.prototype, "type", {
        get: function () {
            return this._type;
        },
        set: function (t) {
            this._type = t;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ProgramItem.prototype, "data", {
        get: function () {
            return this._data;
        },
        set: function (d) {
            this._data = d;
        },
        enumerable: true,
        configurable: true
    });
    return ProgramItem;
}());

var ProgramItemFactory = (function () {
    function ProgramItemFactory() {
    }
    Object.defineProperty(ProgramItemFactory.prototype, "basePath", {
        get: function () {
            return this._basePath;
        },
        set: function (bp) {
            this._basePath = bp;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ProgramItemFactory.prototype, "programRepository", {
        get: function () {
            return this._programRepository;
        },
        set: function (pr) {
            this._programRepository = pr;
        },
        enumerable: true,
        configurable: true
    });
    ProgramItemFactory.prototype.getProgramItem = function (programItemId) {
        var _this = this;
        return this.programRepository
            .findById(programItemId)
            .then(function (programItem) {
            return _this.prepareProgramItem(programItem.program_item_type, programItem);
        });
    };
    ProgramItemFactory.prototype.prepareProgramItem = function (type, data) {
        var programItem = new ProgramItem();
        programItem.type = type;
        if (data.infoboxes) {
            programItem.data.infoboxes = data.infoboxes;
        }
        if (type === PROGRAM_ITEM_TYPE_VIDEO) {
            return this.prepareVideoItem(programItem, data);
        }
        else if (type === PROGRAM_ITEM_TYPE_SLIDESHOW) {
            return this.prepareSlideshowItem(programItem, data);
        }
        else {
            return null;
        }
    };
    ProgramItemFactory.prototype.prepareSlideshowItem = function (programItem, data) {
        var _this = this;
        return this._programRepository.findByIds(data.images)
            .then(function (images) {
            programItem.data.speed = data.settings.speed;
            programItem.data.effect = data.settings.effect;
            programItem.data.images = [];
            if (images && images.length > 0) {
                for (var _i = 0, images_1 = images; _i < images_1.length; _i++) {
                    var image = images_1[_i];
                    if (image && image.filename) {
                        programItem.data.images.push(_this.basePath + image.filename);
                    }
                }
            }
            return programItem;
        });
    };
    ProgramItemFactory.prototype.prepareVideoItem = function (programItem, data) {
        var _this = this;
        return this._programRepository.findById(data.video)
            .then(function (data) {
            programItem.data.video = _this.basePath + data['filename'];
            return programItem;
        });
    };
    return ProgramItemFactory;
}());

exports.Player = Player;
exports.ProgramManager = ProgramManager;
exports.PROGRAM_ITEM_TYPE_SLIDESHOW = PROGRAM_ITEM_TYPE_SLIDESHOW;
exports.PROGRAM_ITEM_TYPE_VIDEO = PROGRAM_ITEM_TYPE_VIDEO;
exports.ProgramItem = ProgramItem;
exports.ProgramItemFactory = ProgramItemFactory;
//# sourceMappingURL=bundle.js.map