Stefan Huber

added types and transpiled scripts

1 +"use strict";
2 +function __export(m) {
3 + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4 +}
5 +__export(require("./player"));
6 +__export(require("./program-item/program-item"));
7 +__export(require("./program-item/program-item-factory"));
8 +__export(require("./program-manager"));
9 +__export(require("./util"));
1 +"use strict";
2 +var __extends = (this && this.__extends) || function (d, b) {
3 + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4 + function __() { this.constructor = d; }
5 + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6 +};
7 +var events_1 = require("events");
8 +var util_1 = require("./util");
9 +var STATE_START = "start";
10 +var STATE_STOP = "stop";
11 +var Player = (function (_super) {
12 + __extends(Player, _super);
13 + function Player() {
14 + var _this = _super !== null && _super.apply(this, arguments) || this;
15 + _this._minutesReplication = 5;
16 + _this._replicationRetry = 10000;
17 + _this._currentProgramItemId = '';
18 + _this._currentReplicationCounter = 0;
19 + _this._state = STATE_STOP;
20 + return _this;
21 + }
22 + Object.defineProperty(Player.prototype, "state", {
23 + set: function (st) {
24 + this._state = st;
25 + },
26 + enumerable: true,
27 + configurable: true
28 + });
29 + Object.defineProperty(Player.prototype, "programManager", {
30 + get: function () {
31 + return this._programManager;
32 + },
33 + set: function (pm) {
34 + this._programManager = pm;
35 + },
36 + enumerable: true,
37 + configurable: true
38 + });
39 + Object.defineProperty(Player.prototype, "programRepository", {
40 + get: function () {
41 + return this._programRepository;
42 + },
43 + set: function (pr) {
44 + this._programRepository = pr;
45 + },
46 + enumerable: true,
47 + configurable: true
48 + });
49 + Object.defineProperty(Player.prototype, "minutesReplication", {
50 + get: function () {
51 + return this._minutesReplication;
52 + },
53 + set: function (mr) {
54 + this._minutesReplication = mr;
55 + },
56 + enumerable: true,
57 + configurable: true
58 + });
59 + Object.defineProperty(Player.prototype, "replicationRetry", {
60 + get: function () {
61 + return this._replicationRetry;
62 + },
63 + set: function (rr) {
64 + this._replicationRetry = rr;
65 + },
66 + enumerable: true,
67 + configurable: true
68 + });
69 + Player.prototype.triggerReplication = function () {
70 + var _this = this;
71 + return this.programRepository.replicate()
72 + .then(function () {
73 + _this._currentReplicationCounter = 0;
74 + _this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
75 + })
76 + .catch(function () {
77 + _this.trigger(_this.triggerReplication, _this.replicationRetry);
78 + });
79 + };
80 + Player.prototype.triggerProgramItemId = function () {
81 + var _this = this;
82 + this.programManager.getCurrentProgramItemId()
83 + .then(function (programItemId) {
84 + _this._currentReplicationCounter++;
85 + // if there is a new program item id trigger play
86 + // else (1) calculate next potential program change point
87 + // or (2) trigger replication
88 + if (programItemId != _this._currentProgramItemId) {
89 + _this._currentProgramItemId = programItemId;
90 + _this.emit('play', programItemId);
91 + }
92 + else if (_this._currentReplicationCounter >= _this._minutesReplication) {
93 + _this.triggerReplication();
94 + }
95 + else {
96 + _this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
97 + }
98 + });
99 + };
100 + Player.prototype.trigger = function (func, milliseconds) {
101 + if (this._state === STATE_START) {
102 + setTimeout(function () { func(); }, milliseconds);
103 + }
104 + };
105 + Player.prototype.start = function () {
106 + if (this._state === STATE_STOP) {
107 + this.triggerReplication();
108 + this._state = STATE_START;
109 + }
110 + };
111 + Player.prototype.stop = function () {
112 + this._state = STATE_STOP;
113 + };
114 + return Player;
115 +}(events_1.EventEmitter));
116 +exports.Player = Player;
1 +"use strict";
2 +var program_item_1 = require("./program-item");
3 +var ProgramItemFactory = (function () {
4 + function ProgramItemFactory() {
5 + }
6 + Object.defineProperty(ProgramItemFactory.prototype, "basePath", {
7 + get: function () {
8 + return this._basePath;
9 + },
10 + set: function (bp) {
11 + this._basePath = bp;
12 + },
13 + enumerable: true,
14 + configurable: true
15 + });
16 + Object.defineProperty(ProgramItemFactory.prototype, "programRepository", {
17 + get: function () {
18 + return this._programRepository;
19 + },
20 + set: function (pr) {
21 + this._programRepository = pr;
22 + },
23 + enumerable: true,
24 + configurable: true
25 + });
26 + ProgramItemFactory.prototype.getProgramItem = function (programItemId) {
27 + var _this = this;
28 + return this.programRepository
29 + .findById(programItemId)
30 + .then(function (programItem) {
31 + return _this.prepareProgramItem(programItem.program_item_type, programItem);
32 + });
33 + };
34 + ProgramItemFactory.prototype.prepareProgramItem = function (type, data) {
35 + var programItem = new program_item_1.ProgramItem();
36 + programItem.type = type;
37 + if (type === program_item_1.PROGRAM_ITEM_TYPE_VIDEO) {
38 + return this.prepareVideoItem(programItem, data);
39 + }
40 + else if (type === program_item_1.PROGRAM_ITEM_TYPE_SLIDESHOW) {
41 + return this.prepareSlideshowItem(programItem, data);
42 + }
43 + else {
44 + return null;
45 + }
46 + };
47 + ProgramItemFactory.prototype.prepareSlideshowItem = function (programItem, data) {
48 + var _this = this;
49 + return this._programRepository.findByIds(data.images)
50 + .then(function (images) {
51 + programItem.data = {
52 + speed: data.settings.speed,
53 + effect: data.settings.effect,
54 + images: []
55 + };
56 + for (var _i = 0, images_1 = images; _i < images_1.length; _i++) {
57 + var image = images_1[_i];
58 + programItem.data.images.push(_this.basePath + image.filename);
59 + }
60 + return programItem;
61 + });
62 + };
63 + ProgramItemFactory.prototype.prepareVideoItem = function (programItem, data) {
64 + var _this = this;
65 + return this._programRepository.findById(data.video)
66 + .then(function (data) {
67 + programItem.data = {
68 + video: _this.basePath + data['filename']
69 + };
70 + return programItem;
71 + });
72 + };
73 + return ProgramItemFactory;
74 +}());
75 +exports.ProgramItemFactory = ProgramItemFactory;
1 +"use strict";
2 +exports.PROGRAM_ITEM_TYPE_SLIDESHOW = "slideshow";
3 +exports.PROGRAM_ITEM_TYPE_VIDEO = "video";
4 +var ProgramItem = (function () {
5 + function ProgramItem() {
6 + }
7 + Object.defineProperty(ProgramItem.prototype, "type", {
8 + get: function () {
9 + return this._type;
10 + },
11 + set: function (t) {
12 + this._type = t;
13 + },
14 + enumerable: true,
15 + configurable: true
16 + });
17 + Object.defineProperty(ProgramItem.prototype, "data", {
18 + get: function () {
19 + return this._data;
20 + },
21 + set: function (d) {
22 + this._data = d;
23 + },
24 + enumerable: true,
25 + configurable: true
26 + });
27 + return ProgramItem;
28 +}());
29 +exports.ProgramItem = ProgramItem;
1 +"use strict";
2 +var util_1 = require("./util");
3 +var ProgramManager = (function () {
4 + function ProgramManager() {
5 + }
6 + Object.defineProperty(ProgramManager.prototype, "programRepository", {
7 + get: function () {
8 + return this._programRepository;
9 + },
10 + set: function (pr) {
11 + this._programRepository = pr;
12 + },
13 + enumerable: true,
14 + configurable: true
15 + });
16 + ProgramManager.prototype.getCurrentProgramItemId = function () {
17 + var _this = this;
18 + return new Promise(function (resolve, reject) {
19 + _this.findCurrentProgramSegment().then(function (programSegment) {
20 + var currentProgramItemId = programSegment.default;
21 + if (programSegment.schedule) {
22 + currentProgramItemId = _this.findCurrentProgramItem(programSegment.schedule, util_1.Util.getDateInMinutes());
23 + }
24 + resolve(currentProgramItemId);
25 + });
26 + });
27 + };
28 + /**
29 + * find program item in schedule, which fits
30 + * according to current hh:mm
31 + */
32 + ProgramManager.prototype.findCurrentProgramItem = function (schedule, dateInMinutes) {
33 + var timeList = [];
34 + var tmpSchedule = {};
35 + for (var startTime in schedule) {
36 + if (schedule.hasOwnProperty(startTime)) {
37 + var minutes = util_1.Util.convertToMinutes(startTime);
38 + timeList.push(minutes);
39 + tmpSchedule[minutes] = schedule[startTime];
40 + }
41 + }
42 + // sort ascending (-)
43 + timeList.sort(function (a, b) { return a - b; });
44 + var last = 0;
45 + for (var i = 0; i < timeList.length; i++) {
46 + if (timeList[i] <= dateInMinutes) {
47 + last = timeList[i];
48 + }
49 + else {
50 + break;
51 + }
52 + }
53 + return tmpSchedule[last];
54 + };
55 + /**
56 + * Find the program segment
57 + * This is dependent on the date set on the device
58 + */
59 + ProgramManager.prototype.findCurrentProgramSegment = function () {
60 + var _this = this;
61 + return new Promise(function (resolve, reject) {
62 + var today = util_1.Util.getISODate();
63 + _this.programRepository.findByType('program')
64 + .then(function (programs) {
65 + if (programs.length > 0) {
66 + var program = programs[0];
67 + var programSegmentId = void 0;
68 + // if there is a program_segment for today else default
69 + if (program.schedule && program.schedule[today]) {
70 + programSegmentId = program.schedule[today];
71 + }
72 + else {
73 + programSegmentId = program.default;
74 + }
75 + _this.programRepository
76 + .findById(programSegmentId)
77 + .then(function (programSegment) {
78 + resolve(programSegment);
79 + }).catch(function (error) {
80 + reject("program segment not found");
81 + });
82 + }
83 + else {
84 + reject('No Program found');
85 + }
86 + }).catch(function (error) {
87 + reject(error);
88 + });
89 + });
90 + };
91 + return ProgramManager;
92 +}());
93 +exports.ProgramManager = ProgramManager;
1 +"use strict";
2 +var Util = (function () {
3 + function Util() {
4 + }
5 + Util.getISODate = function () {
6 + return (new Date()).toISOString().slice(0, 10);
7 + };
8 + Util.getDateInMinutes = function () {
9 + var now = new Date();
10 + return (now.getHours() * 60) + now.getMinutes();
11 + };
12 + /**
13 + * convert a time input to minutes
14 + * e.g. 23:59 = 1439
15 + */
16 + Util.convertToMinutes = function (time) {
17 + var times = time.split(":");
18 + var convered = (parseInt(times[0]) * 60) + parseInt(times[1]);
19 + return (convered >= 0 && convered <= 1439) ? convered : 0;
20 + };
21 + Util.calculateNextMinute = function () {
22 + return (60 - (Math.round((new Date()).getTime() / 1000) % 60)) * 1000;
23 + };
24 + return Util;
25 +}());
26 +exports.Util = Util;
1 +export * from './player';
2 +export * from './program-item/program-item';
3 +export * from './program-item/program-item-factory';
4 +export * from './program-manager';
5 +export * from './program-repository';
6 +export * from './util';
1 +/// <reference types="node" />
2 +/// <reference types="es6-promise" />
3 +import { EventEmitter } from 'events';
4 +import { ProgramRepository } from './program-repository';
5 +import { ProgramManager } from './program-manager';
6 +export declare class Player extends EventEmitter {
7 + protected _programRepository: ProgramRepository;
8 + protected _programManager: ProgramManager;
9 + protected _minutesReplication: number;
10 + protected _replicationRetry: number;
11 + protected _currentProgramItemId: string;
12 + protected _currentReplicationCounter: number;
13 + protected _state: string;
14 + state: string;
15 + programManager: ProgramManager;
16 + programRepository: ProgramRepository;
17 + minutesReplication: number;
18 + replicationRetry: number;
19 + triggerReplication(): Promise<void>;
20 + triggerProgramItemId(): void;
21 + trigger(func: Function, milliseconds: number): void;
22 + start(): void;
23 + stop(): void;
24 +}
1 +/// <reference types="es6-promise" />
2 +import { ProgramItem } from './program-item';
3 +import { ProgramRepository } from './../program-repository';
4 +export declare class ProgramItemFactory {
5 + protected _programRepository: ProgramRepository;
6 + protected _basePath: string;
7 + basePath: string;
8 + programRepository: ProgramRepository;
9 + getProgramItem(programItemId: string): Promise<ProgramItem>;
10 + prepareProgramItem(type: string, data: any): Promise<ProgramItem>;
11 + prepareSlideshowItem(programItem: ProgramItem, data: any): Promise<ProgramItem>;
12 + prepareVideoItem(programItem: ProgramItem, data: any): Promise<ProgramItem>;
13 +}
1 +export declare const PROGRAM_ITEM_TYPE_SLIDESHOW = "slideshow";
2 +export declare const PROGRAM_ITEM_TYPE_VIDEO = "video";
3 +export declare class ProgramItem {
4 + protected _type: string;
5 + protected _data: any;
6 + type: string;
7 + data: any;
8 +}
1 +/// <reference types="es6-promise" />
2 +import { ProgramRepository } from './program-repository';
3 +export declare class ProgramManager {
4 + protected _programRepository: ProgramRepository;
5 + programRepository: ProgramRepository;
6 + getCurrentProgramItemId(): Promise<string>;
7 + /**
8 + * find program item in schedule, which fits
9 + * according to current hh:mm
10 + */
11 + findCurrentProgramItem(schedule: any, dateInMinutes: number): string;
12 + /**
13 + * Find the program segment
14 + * This is dependent on the date set on the device
15 + */
16 + findCurrentProgramSegment(): Promise<any>;
17 +}
1 +/// <reference types="es6-promise" />
2 +export interface ProgramRepository {
3 + findById(id: string): Promise<any>;
4 + findByIds(ids: Array<string>): Promise<Array<any>>;
5 + findByType(type: string): Promise<Array<any>>;
6 + replicate(): Promise<void>;
7 +}
1 +export declare class Util {
2 + static getISODate(): string;
3 + static getDateInMinutes(): number;
4 + /**
5 + * convert a time input to minutes
6 + * e.g. 23:59 = 1439
7 + */
8 + static convertToMinutes(time: string): number;
9 + static calculateNextMinute(): number;
10 +}