Stefan Huber

added types and transpiled scripts

"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./player"));
__export(require("./program-item/program-item"));
__export(require("./program-item/program-item-factory"));
__export(require("./program-manager"));
__export(require("./util"));
"use strict";
var __extends = (this && this.__extends) || function (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 events_1 = require("events");
var util_1 = require("./util");
var STATE_START = "start";
var STATE_STOP = "stop";
var Player = (function (_super) {
__extends(Player, _super);
function Player() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._minutesReplication = 5;
_this._replicationRetry = 10000;
_this._currentProgramItemId = '';
_this._currentReplicationCounter = 0;
_this._state = STATE_STOP;
return _this;
}
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;
return this.programRepository.replicate()
.then(function () {
_this._currentReplicationCounter = 0;
_this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
})
.catch(function () {
_this.trigger(_this.triggerReplication, _this.replicationRetry);
});
};
Player.prototype.triggerProgramItemId = function () {
var _this = this;
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 != _this._currentProgramItemId) {
_this._currentProgramItemId = programItemId;
_this.emit('play', programItemId);
}
else if (_this._currentReplicationCounter >= _this._minutesReplication) {
_this.triggerReplication();
}
else {
_this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
}
});
};
Player.prototype.trigger = function (func, milliseconds) {
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_1.EventEmitter));
exports.Player = Player;
"use strict";
var program_item_1 = require("./program-item");
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 program_item_1.ProgramItem();
programItem.type = type;
if (type === program_item_1.PROGRAM_ITEM_TYPE_VIDEO) {
return this.prepareVideoItem(programItem, data);
}
else if (type === program_item_1.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,
effect: data.settings.effect,
images: []
};
for (var _i = 0, images_1 = images; _i < images_1.length; _i++) {
var image = images_1[_i];
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.ProgramItemFactory = ProgramItemFactory;
"use strict";
exports.PROGRAM_ITEM_TYPE_SLIDESHOW = "slideshow";
exports.PROGRAM_ITEM_TYPE_VIDEO = "video";
var ProgramItem = (function () {
function ProgramItem() {
}
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;
}());
exports.ProgramItem = ProgramItem;
"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;
"use strict";
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;
}());
exports.Util = Util;
export * from './player';
export * from './program-item/program-item';
export * from './program-item/program-item-factory';
export * from './program-manager';
export * from './program-repository';
export * from './util';
/// <reference types="node" />
/// <reference types="es6-promise" />
import { EventEmitter } from 'events';
import { ProgramRepository } from './program-repository';
import { ProgramManager } from './program-manager';
export declare class Player extends EventEmitter {
protected _programRepository: ProgramRepository;
protected _programManager: ProgramManager;
protected _minutesReplication: number;
protected _replicationRetry: number;
protected _currentProgramItemId: string;
protected _currentReplicationCounter: number;
protected _state: string;
state: string;
programManager: ProgramManager;
programRepository: ProgramRepository;
minutesReplication: number;
replicationRetry: number;
triggerReplication(): Promise<void>;
triggerProgramItemId(): void;
trigger(func: Function, milliseconds: number): void;
start(): void;
stop(): void;
}
/// <reference types="es6-promise" />
import { ProgramItem } from './program-item';
import { ProgramRepository } from './../program-repository';
export declare class ProgramItemFactory {
protected _programRepository: ProgramRepository;
protected _basePath: string;
basePath: string;
programRepository: ProgramRepository;
getProgramItem(programItemId: string): Promise<ProgramItem>;
prepareProgramItem(type: string, data: any): Promise<ProgramItem>;
prepareSlideshowItem(programItem: ProgramItem, data: any): Promise<ProgramItem>;
prepareVideoItem(programItem: ProgramItem, data: any): Promise<ProgramItem>;
}
export declare const PROGRAM_ITEM_TYPE_SLIDESHOW = "slideshow";
export declare const PROGRAM_ITEM_TYPE_VIDEO = "video";
export declare class ProgramItem {
protected _type: string;
protected _data: any;
type: string;
data: any;
}
/// <reference types="es6-promise" />
import { ProgramRepository } from './program-repository';
export declare class ProgramManager {
protected _programRepository: ProgramRepository;
programRepository: ProgramRepository;
getCurrentProgramItemId(): Promise<string>;
/**
* find program item in schedule, which fits
* according to current hh:mm
*/
findCurrentProgramItem(schedule: any, dateInMinutes: number): string;
/**
* Find the program segment
* This is dependent on the date set on the device
*/
findCurrentProgramSegment(): Promise<any>;
}
/// <reference types="es6-promise" />
export interface ProgramRepository {
findById(id: string): Promise<any>;
findByIds(ids: Array<string>): Promise<Array<any>>;
findByType(type: string): Promise<Array<any>>;
replicate(): Promise<void>;
}
export declare class Util {
static getISODate(): string;
static getDateInMinutes(): number;
/**
* convert a time input to minutes
* e.g. 23:59 = 1439
*/
static convertToMinutes(time: string): number;
static calculateNextMinute(): number;
}