1/**
2 * Controller for firmware
3 *
4 * @module app/configuration
5 * @exports firmwareController
6 * @name firmwareController
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11    'use strict';
12
13    angular
14        .module('app.configuration')
15        .controller('firmwareController', [
16                '$scope',
17                '$window',
18                'APIUtils',
19                'dataService',
20                '$location',
21                '$anchorScroll',
22                'Constants',
23                function ($scope, $window, APIUtils, dataService, $location, $anchorScroll, Constants) {
24                    $scope.dataService = dataService;
25
26                    //Scroll to target anchor
27                    $scope.gotoAnchor = function () {
28                        $location.hash('upload');
29                        $anchorScroll();
30                    };
31
32                    $scope.firmwares = [];
33                    $scope.bmcActiveVersion = "";
34                    $scope.hostActiveVersion = "";
35                    $scope.display_error = false;
36                    $scope.reboot_confirm = false;
37                    $scope.preserve_settings_confirm = false;
38                    $scope.delete_image_id = "";
39                    $scope.delete_image_version = "";
40                    $scope.activate_image_id = "";
41                    $scope.priority_image_id = "";
42                    $scope.priority_image_version = "";
43                    $scope.priority_from = -1;
44                    $scope.priority_to = -1;
45                    $scope.confirm_priority = false;
46                    $scope.file_empty = true;
47                    $scope.uploading = false;
48
49                    $scope.error = {
50                        modal_title: "",
51                        title: "",
52                        desc: "",
53                        type: "warning"
54                    };
55
56                    $scope.activateImage = function(imageId){
57                        $scope.activate_image_id = imageId;
58                        $scope.preserve_settings_confirm = true;
59                    }
60
61                    $scope.displayError = function(data){
62                        $scope.error = data;
63                        $scope.display_error = true;
64                    }
65
66                    $scope.preserveSettingsConfirmed = function(){
67                        $scope.uploading = true;
68                        APIUtils.activateImage($scope.activate_image_id).then(function(response){
69                            $scope.uploading = false;
70                            if(response.status == 'error'){
71                                $scope.displayError({
72                                    modal_title: response.data.description,
73                                    title: response.data.description,
74                                    desc: response.data.exception,
75                                    type: 'Error'
76                                });
77                            }else{
78                                $scope.loadFirmwares();
79                            }
80                        });
81                        $scope.preserve_settings_confirm = false;
82                    }
83
84                    $scope.confirmWarmReboot = function(){
85                        $scope.reboot_confirm = false;
86                    }
87
88                    $scope.upload = function(){
89                        if($scope.file) {
90                            $scope.uploading = true;
91                            APIUtils.uploadImage($scope.file).then(function(response){
92                                $scope.uploading = false;
93                                if(response.status == 'error'){
94                                    $scope.displayError({
95                                        modal_title: response.data.description,
96                                        title: response.data.description,
97                                        desc: response.data.exception,
98                                        type: 'Error'
99                                    });
100                                }else{
101                                    $scope.loadFirmwares();
102                                }
103                            });
104                        }
105                    }
106
107                    $scope.download = function(){
108                        $scope.downloading = true;
109                        APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
110                            var data = response.data;
111                            $scope.downloading = false;
112                            var headers = response.headers();
113
114                            var filename = headers['x-filename'];
115                            var contentType = headers['content-type'];
116
117                            if(!headers['x-filename']){
118                                filename = $scope.download_filename;
119                            }
120
121                            var linkElement = document.createElement('a');
122                            try {
123                                var blob = new Blob([data], { type: contentType });
124                                var url = window.URL.createObjectURL(blob);
125
126                                linkElement.setAttribute('href', url);
127                                linkElement.setAttribute("download", filename);
128
129                                var clickEvent = new MouseEvent("click", {
130                                    "view": window,
131                                    "bubbles": true,
132                                    "cancelable": false
133                                });
134                                linkElement.dispatchEvent(clickEvent);
135                            } catch (ex) {
136                                console.log(ex);
137                            }
138                        });
139                    }
140
141                    $scope.changePriority = function(imageId, imageVersion, from, to){
142                        $scope.priority_image_id = imageId;
143                        $scope.priority_image_version = imageVersion;
144                        $scope.priority_from = from;
145                        $scope.priority_to = to;
146
147                        if((from + to) == 1){
148                            $scope.confirm_priority = true;
149                        }else{
150                            $scope.confirmChangePriority();
151                        }
152                    }
153
154                    $scope.confirmChangePriority = function(){
155                        $scope.loading = true;
156                        APIUtils.changePriority($scope.priority_image_id, $scope.priority_to).then(function(response){
157                            $scope.loading = false;
158                            if(response.status == 'error'){
159                                $scope.displayError({
160                                    modal_title: response.data.description,
161                                    title: response.data.description,
162                                    desc: response.data.exception,
163                                    type: 'Error'
164                                });
165                            }else{
166                                $scope.loadFirmwares();
167                            }
168                        });
169                        $scope.confirm_priority = false;
170                    }
171                    $scope.deleteImage = function(imageId, imageVersion){
172                        $scope.delete_image_id = imageId;
173                        $scope.delete_image_version = imageVersion;
174                        $scope.confirm_delete = true;
175                    }
176                    $scope.confirmDeleteImage = function(){
177                        $scope.loading = true;
178                        APIUtils.deleteImage($scope.delete_image_id).then(function(response){
179                            $scope.loading = false;
180                            if(response.status == 'error'){
181                                $scope.displayError({
182                                    modal_title: response.data.description,
183                                    title: response.data.description,
184                                    desc: response.data.exception,
185                                    type: 'Error'
186                                });
187                            }else{
188                                $scope.loadFirmwares();
189                            }
190                        });
191                        $scope.confirm_delete = false;
192                    }
193                    $scope.fileNameChanged = function(){
194                        $scope.file_empty = false;
195                    }
196
197                    $scope.uploading = false;
198                    $scope.filters = {
199                        bmc: {
200                            imageType: 'BMC'
201                        },
202                        host: {
203                            imageType: 'Host'
204                        }
205                    };
206
207                    $scope.loadFirmwares = function(){
208                        APIUtils.getFirmwares().then(function(result){
209                           $scope.firmwares = result.data;
210                           $scope.bmcActiveVersion = result.bmcActiveVersion;
211                           $scope.hostActiveVersion = result.hostActiveVersion;
212                        });
213                    }
214
215                    $scope.loadFirmwares();
216                }
217            ]
218        );
219
220})(angular);
221