frontend-tutorial-framework/src/app/services/processmanager.service.ts
2018-05-31 11:31:22 +02:00

56 lines
1.8 KiB
TypeScript

// Copyright (C) 2018 Avatao.com Innovative Learning Kft.
// All Rights Reserved. See LICENSE file for details.
import { Injectable } from '@angular/core';
import { WebSocketService } from './websocket.service';
import { ProcessCommand } from '../message.types/process.command';
import { filter } from 'rxjs/operators';
import { WebSocketMessage } from '../message.types/websocket.message';
@Injectable()
export class ProcessManagerService {
key = 'processmanager';
process_name: string;
constructor(private webSocketService: WebSocketService) {}
init() {
this.webSocketService.connect();
}
subscribeCallback(process_name: string, callback: (event: WebSocketMessage<ProcessCommand>) => void) {
this.observeProcessMessage(process_name).subscribe(callback);
}
subscribeSuccessCallback(process_name: string, callback: (event: WebSocketMessage<ProcessCommand>) => void) {
this.observeProcessMessage(process_name).pipe(filter(message => !('error' in message.data))).subscribe(callback);
}
subscribeErrorCallback(process_name: string, callback: (event: WebSocketMessage<ProcessCommand>) => void) {
this.observeProcessMessage(process_name).pipe(filter(message => 'error' in message.data)).subscribe(callback);
}
observeProcessMessage(process_name: string) {
return this.webSocketService.observeKey<ProcessCommand>(this.key)
.pipe(filter(message => message.data.process_name === process_name));
}
sendCommand(command: string, process_name: string) {
this.webSocketService.send(this.key, {'command': command, 'process_name': process_name});
}
startProcess(process_name: string) {
this.sendCommand('start', process_name);
}
stopProcess(process_name: string) {
this.sendCommand('stop', process_name);
}
restartProcess(process_name: string) {
this.sendCommand('restart', process_name);
}
}