3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
24 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
26 import { TargetService, TargetType, ITarget, ITerminal, TerminalType, ITerminalOutput } from '../../../@core-xds/services/target.service';
27 import { AlertService } from '../../../@core-xds/services/alert.service';
30 selector: 'xds-terminals',
31 styleUrls: ['./terminals.component.scss'],
32 templateUrl: './terminals.component.html',
34 export class TerminalsComponent implements OnInit {
36 public curTarget: ITarget;
37 public xTermStdout: string;
38 public xTermDisable: boolean;
40 protected curTermID: string;
42 private termOut$: Subject<ITerminalOutput>;
43 private termSubs: Subscription;
46 private modalService: NgbModal,
47 private targetSvr: TargetService,
48 private alert: AlertService,
50 this.xTermStdout = '';
51 this.xTermDisable = true;
52 this.curTarget = null;
57 this.targetSvr.curTarget$.subscribe(p => this.curTarget = p);
61 if (this.curTarget == null || this.curTarget.id === '') {
65 // FIXME: don't always use 1st terminal
66 if (this.curTarget.terms.length > 0) {
67 this.curTermID = this.curTarget.terms[0].id;
70 this.targetSvr.terminalOpen(this.curTarget.id, this.curTermID)
73 this.termOut$ = this.targetSvr.terminalOutput$;
75 this.termSubs = this.termOut$.subscribe(termOut => {
76 this.xTermStdout = termOut.stdout + termOut.stderr;
79 this.xTermDisable = false;
82 this.alert.error(err);
88 if (this.curTarget == null || this.curTarget.id === '' || this.curTermID === '') {
91 this.targetSvr.terminalClose(this.curTarget.id, 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;
99 this.xTermDisable = true;
103 onXTermData(data: string) {
104 if (this.termOut$ !== undefined && !this.termOut$.closed) {
105 this.targetSvr.terminalWrite(data);
110 if (this.termOut$ !== undefined && !this.termOut$.closed) {
111 this.targetSvr.terminalResize(this.curTarget.id, this.curTermID, sz.cols, sz.rows).subscribe();