1/**
2 * Controller for power-operations
3 *
4 * @module app/serverControl
5 * @exports powerOperationsController
6 * @name powerOperationsController
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11    'use strict';
12
13    angular
14        .module('app.serverControl')
15        .controller('powerOperationsController', [
16            '$scope',
17            'APIUtils',
18            'dataService',
19            '$timeout',
20            function($scope, APIUtils, dataService, $timeout){
21                $scope.dataService = dataService;
22                $scope.confirm = false;
23                $scope.power_confirm = false;
24                $scope.warmboot_confirm = false;
25                $scope.coldboot_confirm = false;
26                $scope.orderly_confirm = false;
27                $scope.immediately_confirm = false;
28
29                //@TODO: call api and get proper state
30                $scope.toggleState = function(){
31                    dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
32                }
33
34                $scope.togglePower = function(){
35                    var method = (dataService.server_state == 'Running') ? 'hostPowerOff' : 'hostPowerOn';
36                     //@TODO: show progress or set class orange
37                    APIUtils[method](function(response){
38                        //update state based on response
39                        //error case?
40                        if(response == null){
41                            console.log("Failed request.");
42                        }else{
43                            //@TODO::need to get the server status
44                            if(dataService.server_state == 'Running'){
45                                dataService.setPowerOffState();
46                            }else{
47                                dataService.setPowerOnState();
48                            }
49                        }
50                    });
51                }
52                $scope.powerOnConfirm = function(){
53                    if($scope.confirm) {
54                        return;
55                    }
56                    $scope.confirm = true;
57                    $scope.power_confirm = true;
58                };
59                $scope.warmReboot = function(){
60                    //@TODO:show progress
61                    dataService.setBootingState();
62                    APIUtils.hostPowerOff(function(response){
63                        if(response){
64                            APIUtils.hostPowerOn(function(response){
65                                if(response){
66                                    dataService.setPowerOnState();
67                                }else{
68                                    //@TODO:show error message
69                                }
70                            });
71                        }
72                    });
73                };
74                $scope.testState = function(){
75                    $timeout(function(){
76                        dataService.setPowerOffState();
77                        $timeout(function(){
78                            dataService.setPowerOnState();
79                        }, 2000);
80                    }, 1000);
81                };
82                $scope.warmRebootConfirm = function(){
83                    if($scope.confirm) {
84                        return;
85                    }
86                    $scope.confirm = true;
87                    $scope.warmboot_confirm = true;
88                };
89
90                $scope.coldReboot = function(){
91                    $scope.warmReboot();
92                };
93                $scope.coldRebootConfirm = function(){
94                    if($scope.confirm) {
95                        return;
96                    }
97                    $scope.confirm = true;
98                    $scope.coldboot_confirm = true;
99                };
100
101                $scope.orderlyShutdown = function(){
102                    //@TODO:show progress
103                    APIUtils.hostPowerOff(function(response){
104                        if(response){
105                            dataService.setPowerOffState();
106                        }else{
107                            //@TODO:hide progress & show error message
108                        }
109                    });
110                };
111                $scope.orderlyShutdownConfirm = function(){
112                    if($scope.confirm) {
113                        return;
114                    }
115                    $scope.confirm = true;
116                    $scope.orderly_confirm = true;
117                };
118
119                $scope.immediateShutdown = function(){
120                    $scope.orderlyShutdown();
121                };
122                $scope.immediateShutdownConfirm = function(){
123                    if($scope.confirm) {
124                        return;
125                    }
126                    $scope.confirm = true;
127                    $scope.immediately_confirm = true;
128                };
129            }
130        ]
131    );
132
133})(angular);
134