Update the server power state dynamically in GUI

This uses websocket mechanism to retrieve the server power state and
update the same.

Resolves openbmc/openbmc#3102

Tested: Running GUI locally and verified the following scenarios:
- Power on operation from GUI and host command line
- Power off operation from host command line
- Warm reboot operation from GUI
- Cold reboot operation from GUI
- Immediate shutdown operation from GUI
- Orderly shutdown operation from GUI
In all of the above scenarios state change is notified to GUI at right time.

Change-Id: I1c97ae10419078dfe16a1d097082580c29827fb7
Signed-off-by: Jayashankar Padath <jayashankar.padath@in.ibm.com>
diff --git a/app/common/directives/app-header.js b/app/common/directives/app-header.js
index d9fc54c..6be458a 100644
--- a/app/common/directives/app-header.js
+++ b/app/common/directives/app-header.js
@@ -15,6 +15,49 @@
               $rootScope, $scope, dataService, userModel, $location, $route) {
             $scope.dataService = dataService;
 
+            // Create a secure websocket with URL as /subscribe
+            // TODO: Need to put in a generic APIUtils to avoid duplicate
+            // controller
+            var ws =
+                new WebSocket('wss://' + dataService.server_id + '/subscribe');
+
+            // Specify the required event details as JSON dictionary
+            var data = JSON.stringify({
+              'paths': ['/xyz/openbmc_project/state/host0'],
+              'interfaces': ['xyz.openbmc_project.State.Host']
+            });
+
+            // Send the JSON dictionary data to host
+            ws.onopen = function() {
+              ws.send(data);
+              console.log('host0 ws opened');
+            };
+
+            // Close the web socket
+            ws.onclose = function() {
+              console.log('host0 ws closed');
+            };
+
+            // Websocket event handling function which catches the
+            // current host state
+            ws.onmessage = function(evt) {
+              // Parse the response (JSON dictionary data)
+              var content = JSON.parse(evt.data);
+
+              // Fetch the current server power state
+              if (content.hasOwnProperty('properties') &&
+                  content['properties'].hasOwnProperty('CurrentHostState')) {
+                // Refresh the host state and status
+                // TODO: As of now not using the current host state
+                // value for updating the data Service state rather
+                // using it to detect the command line state change.
+                // Tried different methods like creating a separate
+                // function, addding ws under $scope etc.. but auto
+                // refresh is not happening.
+                $scope.loadServerStatus();
+              }
+            };
+
             $scope.loadServerHealth = function() {
               APIUtils.getLogs().then(function(result) {
                 dataService.updateServerHealth(result.data);
diff --git a/app/server-control/controllers/power-operations-controller.js b/app/server-control/controllers/power-operations-controller.js
index 4ae732f..f3955e6 100644
--- a/app/server-control/controllers/power-operations-controller.js
+++ b/app/server-control/controllers/power-operations-controller.js
@@ -68,16 +68,6 @@
         $scope.power_confirm = true;
       };
 
-      function setHostState(state) {
-        if (state == Constants.HOST_STATE_TEXT.off_code) {
-          dataService.setPowerOffState();
-        } else if (state == Constants.HOST_STATE_TEXT.on_code) {
-          dataService.setPowerOnState();
-        } else {
-          dataService.setErrorState();
-        }
-      }
-
       function pollChassisStatusTillOff() {
         var deferred = $q.defer();
         pollChassisStatusTimer = $interval(function() {
@@ -119,7 +109,6 @@
           }
           APIUtils.getHostState()
               .then(function(state) {
-                setHostState(state);
                 if (state === Constants.HOST_STATE_TEXT.on_code) {
                   $interval.cancel(pollHostStatusTimer);
                   pollHostStatusTimer = undefined;
@@ -154,7 +143,6 @@
           }
           APIUtils.getHostState()
               .then(function(state) {
-                setHostState(state);
                 if (state === Constants.HOST_STATE_TEXT.off_code) {
                   $interval.cancel(pollHostStatusTimer);
                   pollHostStatusTimer = undefined;