Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for power-usage |
| 3 | * |
| 4 | * @module app/serverControl |
| 5 | * @exports powerUsageController |
| 6 | * @name powerUsageController |
| 7 | */ |
| 8 | |
| 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular.module('app.serverControl').controller('powerUsageController', [ |
beccabroek | 2264b42 | 2019-01-07 11:43:13 -0600 | [diff] [blame^] | 13 | '$scope', '$window', 'APIUtils', '$route', '$q', 'ngToast', |
| 14 | function($scope, $window, APIUtils, $route, $q, ngToast) { |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 15 | $scope.power_consumption = ''; |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 16 | $scope.power_cap = {}; |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 17 | $scope.loading = false; |
| 18 | loadPowerData(); |
| 19 | |
| 20 | function loadPowerData() { |
| 21 | $scope.loading = true; |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 22 | |
| 23 | var getPowerCapPromise = APIUtils.getPowerCap().then( |
| 24 | function(data) { |
| 25 | $scope.power_cap = data.data; |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 26 | }, |
| 27 | function(error) { |
| 28 | console.log(JSON.stringify(error)); |
| 29 | }); |
| 30 | |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 31 | var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then( |
| 32 | function(data) { |
| 33 | $scope.power_consumption = data; |
| 34 | }, |
| 35 | function(error) { |
| 36 | console.log(JSON.stringify(error)); |
| 37 | }); |
| 38 | |
| 39 | var promises = [ |
| 40 | getPowerConsumptionPromise, |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 41 | getPowerCapPromise, |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 42 | ]; |
| 43 | |
| 44 | $q.all(promises).finally(function() { |
| 45 | $scope.loading = false; |
| 46 | }); |
| 47 | } |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 48 | |
| 49 | $scope.setPowerCap = function() { |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 50 | // The power cap value will be undefined if outside range |
| 51 | if (!$scope.power_cap.PowerCap) { |
beccabroek | 2264b42 | 2019-01-07 11:43:13 -0600 | [diff] [blame^] | 52 | ngToast.danger('Power cap value between 100 and 10,000 is required'); |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | $scope.loading = true; |
| 56 | var promises = [ |
| 57 | setPowerCapValue(), |
| 58 | setPowerCapEnable(), |
| 59 | ]; |
| 60 | |
beccabroek | 2264b42 | 2019-01-07 11:43:13 -0600 | [diff] [blame^] | 61 | $q.all(promises) |
| 62 | .then( |
| 63 | function() { |
| 64 | ngToast.success('Power cap settings saved'); |
| 65 | }, |
| 66 | function(errors) { |
| 67 | ngToast.danger('Power cap settings could not be saved'); |
| 68 | }) |
| 69 | .finally(function() { |
| 70 | $scope.loading = false; |
| 71 | }); |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 72 | }; |
| 73 | $scope.refresh = function() { |
| 74 | $route.reload(); |
| 75 | }; |
| 76 | |
| 77 | function setPowerCapValue() { |
| 78 | return APIUtils.setPowerCap($scope.power_cap.PowerCap) |
| 79 | .then( |
| 80 | function(data) {}, |
| 81 | function(error) { |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 82 | console.log(JSON.stringify(error)); |
beccabroek | 2264b42 | 2019-01-07 11:43:13 -0600 | [diff] [blame^] | 83 | return $q.reject(); |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 84 | }); |
| 85 | } |
| 86 | |
| 87 | function setPowerCapEnable() { |
| 88 | return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable) |
| 89 | .then( |
| 90 | function(data) {}, |
| 91 | function(error) { |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 92 | console.log(JSON.stringify(error)); |
beccabroek | 2264b42 | 2019-01-07 11:43:13 -0600 | [diff] [blame^] | 93 | return $q.reject(); |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 94 | }); |
| 95 | } |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 96 | } |
| 97 | ]); |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 98 | })(angular); |