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', [ |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame^] | 13 | '$scope', '$window', 'APIUtils', '$route', '$q', |
| 14 | function($scope, $window, APIUtils, $route, $q) { |
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 = {}; |
| 17 | $scope.set_power_cap_error = false; |
| 18 | $scope.set_power_cap_success = false; |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 19 | $scope.loading = false; |
| 20 | loadPowerData(); |
| 21 | |
| 22 | function loadPowerData() { |
| 23 | $scope.loading = true; |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame^] | 24 | |
| 25 | var getPowerCapPromise = APIUtils.getPowerCap().then( |
| 26 | function(data) { |
| 27 | $scope.power_cap = data.data; |
| 28 | // TODO: openbmc/openbmc#3154 Rest server should return a proper |
| 29 | // JSON bool. Convert 0/1 to boolean in meantime. |
| 30 | if ($scope.power_cap.PowerCapEnable) { |
| 31 | $scope.power_cap.PowerCapEnable = true; |
| 32 | } else { |
| 33 | $scope.power_cap.PowerCapEnable = false; |
| 34 | } |
| 35 | }, |
| 36 | function(error) { |
| 37 | console.log(JSON.stringify(error)); |
| 38 | }); |
| 39 | |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 40 | var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then( |
| 41 | function(data) { |
| 42 | $scope.power_consumption = data; |
| 43 | }, |
| 44 | function(error) { |
| 45 | console.log(JSON.stringify(error)); |
| 46 | }); |
| 47 | |
| 48 | var promises = [ |
| 49 | getPowerConsumptionPromise, |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame^] | 50 | getPowerCapPromise, |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 51 | ]; |
| 52 | |
| 53 | $q.all(promises).finally(function() { |
| 54 | $scope.loading = false; |
| 55 | }); |
| 56 | } |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame^] | 57 | |
| 58 | $scope.setPowerCap = function() { |
| 59 | $scope.set_power_cap_error = false; |
| 60 | $scope.set_power_cap_success = false; |
| 61 | // The power cap value will be undefined if outside range |
| 62 | if (!$scope.power_cap.PowerCap) { |
| 63 | $scope.set_power_cap_error = true; |
| 64 | return; |
| 65 | } |
| 66 | $scope.loading = true; |
| 67 | var promises = [ |
| 68 | setPowerCapValue(), |
| 69 | setPowerCapEnable(), |
| 70 | ]; |
| 71 | |
| 72 | $q.all(promises).finally(function() { |
| 73 | $scope.loading = false; |
| 74 | if (!$scope.set_power_cap_error) { |
| 75 | $scope.set_power_cap_success = true; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | }; |
| 80 | $scope.refresh = function() { |
| 81 | $route.reload(); |
| 82 | }; |
| 83 | |
| 84 | function setPowerCapValue() { |
| 85 | return APIUtils.setPowerCap($scope.power_cap.PowerCap) |
| 86 | .then( |
| 87 | function(data) {}, |
| 88 | function(error) { |
| 89 | $scope.set_power_cap_error = true; |
| 90 | console.log(JSON.stringify(error)); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | function setPowerCapEnable() { |
| 95 | return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable) |
| 96 | .then( |
| 97 | function(data) {}, |
| 98 | function(error) { |
| 99 | $scope.set_power_cap_error = true; |
| 100 | console.log(JSON.stringify(error)); |
| 101 | }); |
| 102 | } |
| 103 | |
Gunnar Mills | 52b8bde | 2018-06-21 13:16:54 -0500 | [diff] [blame] | 104 | } |
| 105 | ]); |
| 106 | |
| 107 | })(angular); |