1import {hterm, lib} from 'hterm-umdjs';
2
3window.angular && (function(angular) {
4  'use strict';
5
6  angular.module('app.common.directives').directive('serialConsole', [
7    function() {
8      return {
9        'restrict': 'E',
10        'template': require('./serial-console.html'),
11        'scope': {'path': '='},
12        'controller': [
13          '$scope', '$window', 'dataService',
14          function($scope, $window, dataService) {
15            $scope.dataService = dataService;
16
17            // See https://github.com/macton/hterm for available hterm options
18
19            hterm.defaultStorage = new lib.Storage.Local();
20            var term = new hterm.Terminal('host-console');
21            term.decorate(document.querySelector('#terminal'));
22            // Set cursor color
23            term.prefs_.set('cursor-color', 'rgba(83, 146, 255, .5)');
24            // Set background color
25            term.prefs_.set('background-color', '#19273c');
26            // Allows keyboard input
27            term.installKeyboard();
28
29            // The BMC exposes a websocket at /console0. This can be read
30            // or written to access the host serial console.
31            var hostname = dataService.getHost().replace('https://', '');
32            var host = 'wss://' + hostname + '/console0';
33            var ws = new WebSocket(host);
34            ws.onmessage = function(evt) {
35              // websocket -> terminal
36              term.io.print(evt.data);
37            };
38
39            // terminal -> websocket
40            term.onTerminalReady = function() {
41              var io = term.io.push();
42              io.onVTKeystroke = function(str) {
43                ws.send(str);
44              };
45              io.sendString = function(str) {
46                ws.send(str);
47              };
48            };
49
50            ws.onopen = function() {
51              console.log('websocket opened');
52            };
53            ws.onclose = function(event) {
54              console.log(
55                  'websocket closed. code: ' + event.code +
56                  ' reason: ' + event.reason);
57            };
58          }
59        ]
60      };
61    }
62  ]);
63})(window.angular);
64