Create SOL console directive

Created serial-console directive in order to avoid
duplicate code between remote-console-controller
and remote-console-window-controller.

Tested: Verified that SOL console functions as
expected after refactor

Change-Id: I8cfc8e78cc2325c813e5bef608886859df6b3ab8
Signed-off-by: beccabroek <beccabroek@gmail.com>
diff --git a/app/common/directives/serial-console.html b/app/common/directives/serial-console.html
new file mode 100644
index 0000000..0927f0a
--- /dev/null
+++ b/app/common/directives/serial-console.html
@@ -0,0 +1,3 @@
+<div class="serial-lan__wrapper">
+    	<div id="terminal" class="serial-lan__terminal"></div>
+</div>
\ No newline at end of file
diff --git a/app/common/directives/serial-console.js b/app/common/directives/serial-console.js
new file mode 100644
index 0000000..7ce2e62
--- /dev/null
+++ b/app/common/directives/serial-console.js
@@ -0,0 +1,63 @@
+import {hterm, lib} from 'hterm-umdjs';
+
+window.angular && (function(angular) {
+  'use strict';
+
+  angular.module('app.common.directives').directive('serialConsole', [
+    function() {
+      return {
+        'restrict': 'E',
+        'template': require('./serial-console.html'),
+        'scope': {'path': '='},
+        'controller': [
+          '$scope', '$window', 'dataService',
+          function($scope, $window, dataService) {
+            $scope.dataService = dataService;
+
+            // See https://github.com/macton/hterm for available hterm options
+
+            hterm.defaultStorage = new lib.Storage.Local();
+            var term = new hterm.Terminal('host-console');
+            term.decorate(document.querySelector('#terminal'));
+            // Set cursor color
+            term.prefs_.set('cursor-color', 'rgba(83, 146, 255, .5)');
+            // Set background color
+            term.prefs_.set('background-color', '#19273c');
+            // Allows keyboard input
+            term.installKeyboard();
+
+            // The BMC exposes a websocket at /console0. This can be read
+            // or written to access the host serial console.
+            var hostname = dataService.getHost().replace('https://', '');
+            var host = 'wss://' + hostname + '/console0';
+            var ws = new WebSocket(host);
+            ws.onmessage = function(evt) {
+              // websocket -> terminal
+              term.io.print(evt.data);
+            };
+
+            // terminal -> websocket
+            term.onTerminalReady = function() {
+              var io = term.io.push();
+              io.onVTKeystroke = function(str) {
+                ws.send(str);
+              };
+              io.sendString = function(str) {
+                ws.send(str);
+              };
+            };
+
+            ws.onopen = function() {
+              console.log('websocket opened');
+            };
+            ws.onclose = function(event) {
+              console.log(
+                  'websocket closed. code: ' + event.code +
+                  ' reason: ' + event.reason);
+            };
+          }
+        ]
+      };
+    }
+  ]);
+})(window.angular);