Added host power on/off functionality to button.

Change-Id: I8942444c7686851c1cf2db9a6125654a5fa399d9
Signed-off-by: Iftekharul Islam <iislam@us.ibm.com>
diff --git a/src/js/controllers.js b/src/js/controllers.js
index 1b5df0f..1c2dd82 100644
--- a/src/js/controllers.js
+++ b/src/js/controllers.js
@@ -1,185 +1,158 @@
-angular
-    .module('app.controllers', [])
-    .controller('loginController', ['$scope', '$window', 'APIUtils', 'dataService', 'userModel', function ($scope, $window, APIUtils, dataService, userModel) {
-        $scope.dataService = dataService;
-
-        $scope.tryLogin = function (username, password, event) {
-            if (event.keyCode === 13) {
-                $scope.login(username, password);
-            }
-        };
-        $scope.login = function (username, password) {
-            $scope.error = false;
-            if (!username || username == "" || !password || password == "") {
-                return false;
-            } else {
-                if (userModel.login(username, password)) {
-                    $window.location.hash = '#/system-overview';
-                } else {
-                    $scope.error = true;
-                }
+ angular
+ .module('app.controllers', [])
+   .controller('loginController', ['$scope', '$window', 'APIUtils', 'dataService', 'userModel', function($scope, $window, APIUtils, dataService, userModel){
+    $scope.dataService = dataService;
+    
+    $scope.tryLogin = function(username, password, event){
+        if(event.keyCode === 13){
+            $scope.login(username, password);
+        }
+    }; 
+    $scope.login = function(username, password){
+        $scope.error = false;
+        if(!username || username == "" ||
+           !password || password == ""){
+            return false;
+        }else{
+            if(userModel.login(username, password)){
+                $window.location.hash = '#/system-overview';
+            }else{
+                $scope.error = true;
             }
         }
-    }])
-    .controller('dashboardController', ['$scope', 'dataService', function ($scope, dataService) {
-        $scope.dataService = dataService;
-    }])
-    .controller('systemOverviewController', ['$scope', 'dataService', function ($scope, dataService) {
-        $scope.dataService = dataService;
-    }])
-    .controller('unitIDController', ['$scope', 'dataService', function ($scope, dataService) {
-        $scope.dataService = dataService;
-    }])
-    .controller('bmcRebootController', ['$scope', 'dataService', function ($scope, dataService) {
-        $scope.dataService = dataService;
-        $scope.bmcReboot_confirm = false;
+    }
+ }])
+ .controller('dashboardController', ['$scope', 'dataService', function($scope, dataService){
+    $scope.dataService = dataService;
+ }])
+ .controller('systemOverviewController', ['$scope', 'dataService', function($scope, dataService){
+    $scope.dataService = dataService;
+ }])
+ .controller('unitIDController', ['$scope', 'dataService', function($scope, dataService){
+    $scope.dataService = dataService;
+ }])
+ .controller('bmcRebootController', ['$scope', 'dataService', function($scope, dataService){
+    $scope.dataService = dataService;
+ }])
+  .controller('powerOperationsController', ['$scope', 'APIUtils', 'dataService', '$timeout', function($scope, APIUtils, dataService, $timeout){
+    $scope.dataService = dataService;
+    $scope.confirm = false;
+    $scope.power_confirm = false;
+    $scope.warmboot_confirm = false;
+    $scope.coldboot_confirm = false;
+    $scope.orderly_confirm = false;
+    $scope.immediately_confirm = false;
 
-        // BMC reboot
-        $scope.bmcReboot = function () {
-            //@TODO:show progress
-            APIUtils.bmcReboot(function (response) {
-                if (response) {
-                    //@TODO: set proper server state
-                } else {
-                    //@TODO:show error message
+    //@TODO: call api and get proper state
+    $scope.toggleState = function(){
+        dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
+    }
+
+    $scope.togglePower = function(){
+        var method = (dataService.server_state == 'Running') ? 'hostPowerOff' : 'hostPowerOn';
+         //@TODO: show progress or set class orange
+        APIUtils[method](function(response){
+            //update state based on response
+            //error case?
+            if(response == null){
+                console.log("Failed request.");
+            }else{
+                //@TODO::need to get the server status
+                if(dataService.server_state == 'Running'){
+                    dataService.setPowerOffState();
+                }else{
+                    dataService.setPowerOnState();
                 }
-                //@TODO:hide progress
-            });
-        };
-        $scope.bmcRebootConfirm = function () {
-            if ($scope.confirm) {
-                return;
             }
-            $scope.confirm = true;
-            $scope.bmcReboot_confirm = true;
-        };
-    }])
-    .controller('powerOperationsController', ['$scope', 'APIUtils', 'dataService', function ($scope, APIUtils, dataService) {
-        $scope.dataService = dataService;
-        $scope.confirm = false;
-        $scope.power_confirm = false;
-        $scope.warmboot_confirm = false;
-        $scope.coldboot_confirm = false;
-        $scope.orderly_confirm = false;
-        $scope.immediately_confirm = false;
-
-        //@TODO: call api and get proper state
-        $scope.toggleState = function () {
-            dataService.server_state = (dataService.server_state == 'On') ? 'Off' : 'On';
-        };
-
-        $scope.togglePower = function () {
-            var method = (dataService.server_state == 'On') ? 'chassisPowerOff' : 'chassisPowerOn';
-            //@TODO: show progress or set class orange
-            APIUtils[method](function (response) {
-                //update state based on response
-                //error case?
-                if (response == null) {
-                    console.log("Failed request.");
-                } else {
-                    dataService.server_state = response;
-                }
-            });
+        });
+    }
+    $scope.powerOnConfirm = function(){
+        if($scope.confirm) {
+            return;
         }
-        $scope.powerOnConfirm = function () {
-            if ($scope.confirm) {
-                return;
+        $scope.confirm = true;
+        $scope.power_confirm = true;
+    };
+    $scope.warmReboot = function(){
+        //@TODO:show progress
+        dataService.loading = true;
+        APIUtils.hostPowerOff(function(response){
+            if(response){
+                dataService.setPowerOffState();
+                APIUtils.hostPowerOn(function(response){
+                    if(response){
+                        dataService.setPowerOnState();
+                    }else{
+                        //@TODO:show error message
+                    }
+                    //@TODO:hide progress, set proper server state
+                    dataService.loading = false;
+                });
+            }else{
+                //@TODO:hide progress & show error message
+                dataService.loading = false;
             }
-            $scope.confirm = true;
-            $scope.power_confirm = true;
-        };
-        $scope.warmReboot = function () {
-            //@TODO:show progress
-            APIUtils.hostPowerOff(function (response) {
-                if (response) {
-                    APIUtils.hostPowerOn(function (response) {
-                        if (response) {
+        });
+    };
+    $scope.testState = function(){
+        //@TODO:show progress
+        dataService.loading = true;
 
-                        } else {
-                            //@TODO:show error message
-                        }
-                        //@TODO:hide progress, set proper server state
-                    });
-                } else {
-                    //@TODO:hide progress & show error message
-                }
-            });
-        };
-        $scope.warmRebootConfirm = function () {
-            if ($scope.confirm) {
-                return;
+        $timeout(function(){
+            dataService.setPowerOffState();
+            $timeout(function(){
+                dataService.setPowerOnState();
+                dataService.loading = false;
+            }, 2000);
+        }, 1000);
+    };
+    $scope.warmRebootConfirm = function(){
+        if($scope.confirm) {
+            return;
+        }
+        $scope.confirm = true;
+        $scope.warmboot_confirm = true;
+    };
+
+    $scope.coldReboot = function(){
+        $scope.warmReboot();
+    };
+    $scope.coldRebootConfirm = function(){
+        if($scope.confirm) {
+            return;
+        }
+        $scope.confirm = true;
+        $scope.coldboot_confirm = true;
+    };
+
+    $scope.orderlyShutdown = function(){
+        //@TODO:show progress
+        dataService.loading = true;
+        APIUtils.hostPowerOff(function(response){
+            if(response){
+                dataService.setPowerOffState();
+            }else{
+                //@TODO:hide progress & show error message
             }
-            $scope.confirm = true;
-            $scope.warmboot_confirm = true;
-        };
+            dataService.loading = false;
+        });
+    };
+    $scope.orderlyShutdownConfirm = function(){
+        if($scope.confirm) {
+            return;
+        }
+        $scope.confirm = true;
+        $scope.orderly_confirm = true;
+    };
 
-        $scope.coldReboot = function () {
-            //@TODO:show progress
-            APIUtils.chassisPowerOff(function (response) {
-                if (response) {
-                    APIUtils.chassisPowerOn(function (response) {
-                        if (response) {
-
-                        } else {
-                            //@TODO:show error message
-                        }
-                        //@TODO:hide progress, set proper server state
-                    });
-                } else {
-                    //@TODO:hide progress & show error message
-                }
-            });
-        };
-        $scope.coldRebootConfirm = function () {
-            if ($scope.confirm) {
-                return;
-            }
-            $scope.confirm = true;
-            $scope.coldboot_confirm = true;
-        };
-
-        $scope.orderlyShutdown = function () {
-            //@TODO:show progress
-            APIUtils.hostPowerOff(function (response) {
-                if (response) {
-                    APIUtils.chassisPowerOff(function (response) {
-                        if (response) {
-
-                        } else {
-                            //@TODO:show error message
-                        }
-                        //@TODO:hide progress, set proper server state
-                    });
-                } else {
-                    //@TODO:hide progress & show error message
-                }
-            });
-        };
-        $scope.orderlyShutdownConfirm = function () {
-            if ($scope.confirm) {
-                return;
-            }
-            $scope.confirm = true;
-            $scope.orderly_confirm = true;
-        };
-
-        $scope.immediateShutdown = function () {
-            //@TODO:show progress
-            APIUtils.chassisPowerOff(function (response) {
-                if (response) {
-                    //@TODO: set proper server state
-                } else {
-                    //@TODO:show error message
-                }
-                //@TODO:hide progress
-            });
-        };
-        $scope.immediateShutdownConfirm = function () {
-            if ($scope.confirm) {
-                return;
-            }
-            $scope.confirm = true;
-            $scope.immediately_confirm = true;
-        };
-
-    }]);
+    $scope.immediateShutdown = function(){
+        $scope.orderlyShutdown();
+    };
+    $scope.immediateShutdownConfirm = function(){
+        if($scope.confirm) {
+            return;
+        }
+        $scope.confirm = true;
+        $scope.immediately_confirm = true;
+    };
+ }]);