Added target and terminal support in Dashboard
[src/xds/xds-agent.git] / webapp / src / app / pages / targets / terminals / terminals.component.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, OnInit } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { Subject } from 'rxjs/Subject';
22 import { Subscription } from 'rxjs/Subscription';
23
24 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
25
26 import { TargetService, TargetType, ITarget, ITerminal, TerminalType, ITerminalOutput } from '../../../@core-xds/services/target.service';
27 import { AlertService } from '../../../@core-xds/services/alert.service';
28
29 @Component({
30   selector: 'xds-terminals',
31   styleUrls: ['./terminals.component.scss'],
32   templateUrl: './terminals.component.html',
33 })
34 export class TerminalsComponent implements OnInit {
35
36   public curTarget: ITarget;
37   public xTermStdout: string;
38   public xTermDisable: boolean;
39
40   protected curTermID: string;
41
42   private termOut$: Subject<ITerminalOutput>;
43   private termSubs: Subscription;
44
45   constructor(
46     private modalService: NgbModal,
47     private targetSvr: TargetService,
48     private alert: AlertService,
49   ) {
50     this.xTermStdout = '';
51     this.xTermDisable = true;
52     this.curTarget = null;
53     this.curTermID = '';
54   }
55
56   ngOnInit() {
57     this.targetSvr.curTarget$.subscribe(p => this.curTarget = p);
58   }
59
60   openTerm() {
61     if (this.curTarget == null || this.curTarget.id === '') {
62       return;
63     }
64
65     // FIXME: don't always use 1st terminal
66     if (this.curTarget.terms.length > 0) {
67       this.curTermID = this.curTarget.terms[0].id;
68     }
69
70     this.targetSvr.terminalOpen(this.curTarget.id, this.curTermID)
71       .subscribe(
72         res => {
73           this.termOut$ = this.targetSvr.terminalOutput$;
74
75           this.termSubs = this.termOut$.subscribe(termOut => {
76             this.xTermStdout = termOut.stdout + termOut.stderr;
77           });
78
79           this.xTermDisable = false;
80         },
81         err => {
82           this.alert.error(err);
83         },
84     );
85   }
86
87   closeTerm() {
88     if (this.curTarget == null || this.curTarget.id === '' || this.curTermID === '') {
89       return;
90     }
91     this.targetSvr.terminalClose(this.curTarget.id, this.curTermID)
92       .subscribe(res => {
93         this.curTermID = '';
94         this.xTermStdout = '\r\n*** Terminal closed ***\n\n\r';
95         if (this.termSubs !== undefined) {
96           this.termSubs.unsubscribe();
97           this.termOut$ = undefined;
98         }
99         this.xTermDisable = true;
100       });
101   }
102
103   onXTermData(data: string) {
104     if (this.termOut$ !== undefined && !this.termOut$.closed) {
105       this.targetSvr.terminalWrite(data);
106     }
107   }
108
109   onResize(sz) {
110     if (this.termOut$ !== undefined && !this.termOut$.closed) {
111       this.targetSvr.terminalResize(this.curTarget.id, this.curTermID, sz.cols, sz.rows).subscribe();
112     }
113   }
114
115 }