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                        }else{
72                        }
73                    });
74                };
75                $scope.testState = function(){
76                    $timeout(function(){
77                        dataService.setPowerOffState();
78                        $timeout(function(){
79                            dataService.setPowerOnState();
80                        }, 2000);
81                    }, 1000);
82                };
83                $scope.warmRebootConfirm = function(){
84                    if($scope.confirm) {
85                        return;
86                    }
87                    $scope.confirm = true;
88                    $scope.warmboot_confirm = true;
89                };
90
91                $scope.coldReboot = function(){
92                    $scope.warmReboot();
93                };
94                $scope.coldRebootConfirm = function(){
95                    if($scope.confirm) {
96                        return;
97                    }
98                    $scope.confirm = true;
99                    $scope.coldboot_confirm = true;
100                };
101
102                $scope.orderlyShutdown = function(){
103                    //@TODO:show progress
104                    APIUtils.hostPowerOff(function(response){
105                        if(response){
106                            dataService.setPowerOffState();
107                        }else{
108                            //@TODO:hide progress & show error message
109                        }
110                    });
111                };
112                $scope.orderlyShutdownConfirm = function(){
113                    if($scope.confirm) {
114                        return;
115                    }
116                    $scope.confirm = true;
117                    $scope.orderly_confirm = true;
118                };
119
120                $scope.immediateShutdown = function(){
121                    $scope.orderlyShutdown();
122                };
123                $scope.immediateShutdownConfirm = function(){
124                    if($scope.confirm) {
125                        return;
126                    }
127                    $scope.confirm = true;
128                    $scope.immediately_confirm = true;
129                };
130            }
131        ]
132    );
133
134})(angular);
135