// Copyright (C) 2018 Avatao.com Innovative Learning Kft. // All Rights Reserved. See LICENSE file for details. import { Component, OnInit } from '@angular/core'; import { WebSocketService } from '../services/websocket.service'; import { WebSocketMessage } from '../message-types/websocket-message'; import { ConsoleReadMessage, ConsoleWriteMessage, ConsoleLiveLogsMessage, ConsoleRewriteContentMessage } from '../message-types/console-messages'; import { config } from '../config'; import { ProcessLogService } from '../services/processlog.service'; import { LogMessage } from '../message-types/log-message'; @Component({ selector: 'app-console', templateUrl: './console.component.html', styleUrls: ['./console.component.scss'] }) export class ConsoleComponent implements OnInit { console_content: string = config.console.defaultContent; rewriteContentWithProcessLogsOnDeploy: string = config.console.rewriteContentWithProcessLogsOnDeploy; command_handlers = { 'console.read': this.readHandler.bind(this), 'console.write': this.writeHandler.bind(this), 'console.showLiveLogs': this.showLiveLogsHandler.bind(this), 'console.rewriteContentWithProcessLogsOnDeploy': this.rewriteContentWithProcessLogsOnDeployHandler.bind(this) }; constructor(private webSocketService: WebSocketService, private processLogService: ProcessLogService) {} ngOnInit() { this.webSocketService.connect(); this.webSocketService.observeKey('console').subscribe( message => this.command_handlers[message.key](message) ); this.processLogService.newLogs.subscribe((data) => this.newLogsHandler(data)); } readHandler(message: ConsoleReadMessage) { this.sendContent(this.console_content); } writeHandler(message: ConsoleWriteMessage) { this.setContent(message.value); } newLogsHandler(logs: LogMessage) { if (this.rewriteContentWithProcessLogsOnDeploy !== '') { const log = logs[this.rewriteContentWithProcessLogsOnDeploy]; if (log) { this.setContent(log); } } } showLiveLogsHandler(message: ConsoleLiveLogsMessage) { this.processLogService.showLiveLogs = message.value; } rewriteContentWithProcessLogsOnDeployHandler(message: ConsoleRewriteContentMessage) { this.rewriteContentWithProcessLogsOnDeploy = message.value; } setContent(content: string) { this.console_content = content; } sendContent(content: string) { this.webSocketService.sendJSON({ 'key': 'console.content', 'value': content }); } }