2017-09-30 13:48:40 +02:00
|
|
|
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
|
|
|
|
import { Subscription } from 'rxjs/Subscription';
|
2017-09-26 18:13:00 +02:00
|
|
|
import { OpenviduRestService } from '../../services/openvidu-rest.service';
|
2017-09-30 13:48:40 +02:00
|
|
|
import { OpenviduParamsService } from '../../services/openvidu-params.service';
|
2017-09-27 16:24:39 +02:00
|
|
|
|
|
|
|
import * as colormap from 'colormap';
|
|
|
|
const numColors = 64;
|
|
|
|
|
2017-09-26 18:13:00 +02:00
|
|
|
@Component({
|
2017-09-30 13:48:40 +02:00
|
|
|
selector: 'app-test-apirest',
|
|
|
|
templateUrl: './test-apirest.component.html',
|
|
|
|
styleUrls: ['./test-apirest.component.css']
|
2017-09-26 18:13:00 +02:00
|
|
|
})
|
2017-09-30 13:48:40 +02:00
|
|
|
export class TestApirestComponent implements OnInit, OnDestroy {
|
2017-09-26 18:13:00 +02:00
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
openviduUrl: string;
|
|
|
|
openviduSecret: string;
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
paramsSubscription: Subscription;
|
2017-09-28 19:13:29 +02:00
|
|
|
|
|
|
|
// API REST params
|
2017-09-27 16:24:39 +02:00
|
|
|
serverData = 'data_test';
|
|
|
|
selectedRadioIndex = 0;
|
2017-09-28 19:13:29 +02:00
|
|
|
selectedRole = 'PUBLISHER';
|
2017-09-27 16:24:39 +02:00
|
|
|
openViduRoles = ['SUBSCRIBER', 'PUBLISHER', 'MODERATOR'];
|
|
|
|
|
|
|
|
// API REST data collected
|
|
|
|
data = [];
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-27 16:24:39 +02:00
|
|
|
cg;
|
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
constructor(private openviduRestService: OpenviduRestService, private openviduParamsService: OpenviduParamsService) {
|
2017-09-27 16:24:39 +02:00
|
|
|
const options = {
|
|
|
|
colormap: [
|
|
|
|
{ 'index': 0, 'rgb': [135, 196, 213] },
|
|
|
|
{ 'index': 1, 'rgb': [255, 230, 151] }],
|
|
|
|
nshades: numColors,
|
|
|
|
format: 'hex'
|
|
|
|
};
|
|
|
|
this.cg = colormap(options);
|
2017-09-26 18:13:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
ngOnInit() {
|
|
|
|
const openviduParams = this.openviduParamsService.getParams();
|
|
|
|
this.openviduUrl = openviduParams.openviduUrl;
|
|
|
|
this.openviduSecret = openviduParams.openviduSecret;
|
2017-09-26 18:13:00 +02:00
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
this.paramsSubscription = this.openviduParamsService.newParams$.subscribe(
|
|
|
|
params => {
|
|
|
|
this.openviduUrl = params.openviduUrl;
|
|
|
|
this.openviduSecret = params.openviduSecret;
|
|
|
|
});
|
2017-09-26 18:13:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
ngOnDestroy() {
|
2018-03-01 11:25:25 +01:00
|
|
|
if (!!this.paramsSubscription) { this.paramsSubscription.unsubscribe(); }
|
2017-09-30 13:48:40 +02:00
|
|
|
}
|
2017-09-27 18:42:11 +02:00
|
|
|
|
2017-09-26 18:13:00 +02:00
|
|
|
private getSessionId() {
|
2017-09-30 13:48:40 +02:00
|
|
|
this.openviduRestService.getSessionId(this.openviduUrl, this.openviduSecret)
|
2017-09-26 18:13:00 +02:00
|
|
|
.then((sessionId) => {
|
2017-09-27 16:24:39 +02:00
|
|
|
this.updateData();
|
2017-09-26 18:13:00 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error getting a sessionId', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private getToken() {
|
2017-09-27 16:24:39 +02:00
|
|
|
const sessionId = this.data[this.selectedRadioIndex][0];
|
|
|
|
|
2017-09-30 13:48:40 +02:00
|
|
|
this.openviduRestService.getToken(this.openviduUrl, this.openviduSecret, sessionId, this.selectedRole, this.serverData)
|
2017-09-26 18:13:00 +02:00
|
|
|
.then((token) => {
|
2017-09-27 16:24:39 +02:00
|
|
|
this.updateData();
|
2017-09-26 18:13:00 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error getting a token', error);
|
|
|
|
});
|
|
|
|
}
|
2017-09-27 16:24:39 +02:00
|
|
|
|
|
|
|
private updateData() {
|
|
|
|
this.data = Array.from(this.openviduRestService.getAvailableParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
private getTokenDisabled(): boolean {
|
|
|
|
return ((this.data.length === 0) || this.selectedRadioIndex === undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBackgroundColor(index: number) {
|
|
|
|
return this.cg[((index + 1) * 15) % numColors];
|
|
|
|
}
|
|
|
|
|
|
|
|
private cleanAllSessions() {
|
|
|
|
this.data = [];
|
|
|
|
this.openviduRestService.sessionIdSession.clear();
|
|
|
|
this.openviduRestService.sessionIdTokenOpenViduRole.clear();
|
|
|
|
}
|
2017-09-27 18:42:11 +02:00
|
|
|
|
2017-09-26 18:13:00 +02:00
|
|
|
}
|