Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for server |
| 3 | * |
| 4 | * @module app/serverControl |
| 5 | * @exports remoteConsoleController |
| 6 | * @name remoteConsoleController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 9 | import {hterm, lib} from 'hterm-umdjs'; |
| 10 | |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | window.angular && (function (angular) { |
| 12 | 'use strict'; |
| 13 | |
| 14 | angular |
| 15 | .module('app.serverControl') |
| 16 | .controller('remoteConsoleController', [ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 17 | '$scope', |
| 18 | '$window', |
| 19 | 'APIUtils', |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 20 | 'dataService', |
| 21 | function($scope, $window, APIUtils, dataService){ |
| 22 | $scope.dataService = dataService; |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 23 | |
| 24 | // See https://github.com/macton/hterm for available hterm options |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 25 | |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 26 | hterm.defaultStorage = new lib.Storage.Local(); |
Deepak Kodihalli | 15374c8 | 2018-04-17 05:49:46 -0500 | [diff] [blame] | 27 | var term = new hterm.Terminal("host-console"); |
| 28 | term.decorate(document.querySelector('#terminal')); |
| 29 | //Set cursor color |
| 30 | term.prefs_.set('cursor-color', 'rgba(83, 146, 255, .5)'); |
| 31 | //Set background color |
| 32 | term.prefs_.set('background-color', '#19273c'); |
| 33 | //Allows keyboard input |
| 34 | term.installKeyboard(); |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 35 | |
Deepak Kodihalli | 15374c8 | 2018-04-17 05:49:46 -0500 | [diff] [blame] | 36 | //The BMC exposes a websocket at /console0. This can be read |
| 37 | //or written to access the host serial console. |
| 38 | var hostname = dataService.getHost().replace("https://", ''); |
| 39 | var host = "wss://" + hostname + "/console0"; |
| 40 | var ws = new WebSocket(host); |
| 41 | ws.onmessage = function (evt) { |
| 42 | //websocket -> terminal |
| 43 | term.io.print(evt.data); |
| 44 | }; |
| 45 | |
| 46 | //terminal -> websocket |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 47 | term.onTerminalReady = function() { |
| 48 | var io = term.io.push(); |
| 49 | io.onVTKeystroke = function(str) { |
Deepak Kodihalli | 15374c8 | 2018-04-17 05:49:46 -0500 | [diff] [blame] | 50 | ws.send(str); |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 51 | }; |
| 52 | io.sendString = function(str) { |
Deepak Kodihalli | 15374c8 | 2018-04-17 05:49:46 -0500 | [diff] [blame] | 53 | ws.send(str); |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 54 | }; |
| 55 | }; |
Michael Davis | 8b52206 | 2017-05-05 09:21:07 -0500 | [diff] [blame] | 56 | |
Deepak Kodihalli | 15374c8 | 2018-04-17 05:49:46 -0500 | [diff] [blame] | 57 | ws.onopen = function() { |
| 58 | console.log("websocket opened"); |
| 59 | }; |
| 60 | ws.onclose = function() { |
| 61 | console.log("websocket closed"); |
| 62 | }; |
| 63 | $scope.$on("$destroy", function() { |
| 64 | if(ws) { |
| 65 | ws.close(); |
| 66 | } |
| 67 | }); |
Iftekharul Islam | c4172b5 | 2017-09-06 10:43:20 -0500 | [diff] [blame] | 68 | |
| 69 | $scope.openTerminalWindow = function(){ |
| 70 | dataService.setRemoteWindowActive(); |
| 71 | $window.open('#/server-control/remote-console-window','Remote Console Window','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=600,height=400'); |
| 72 | } |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 73 | } |
| 74 | ] |
| 75 | ); |
| 76 | |
| 77 | })(angular); |