blob: f3042f9da205188f7ee8502a022a8a609947ec9e [file] [log] [blame]
Gunnar Mills52b8bde2018-06-21 13:16:54 -05001/**
2 * Controller for power-usage
3 *
4 * @module app/serverControl
5 * @exports powerUsageController
6 * @name powerUsageController
7 */
8
9window.angular && (function(angular) {
10 'use strict';
11
12 angular.module('app.serverControl').controller('powerUsageController', [
Gunnar Mills006aaa02018-06-26 16:08:59 -050013 '$scope', '$window', 'APIUtils', '$route', '$q',
14 function($scope, $window, APIUtils, $route, $q) {
Gunnar Mills52b8bde2018-06-21 13:16:54 -050015 $scope.power_consumption = '';
Gunnar Mills006aaa02018-06-26 16:08:59 -050016 $scope.power_cap = {};
17 $scope.set_power_cap_error = false;
18 $scope.set_power_cap_success = false;
Gunnar Mills52b8bde2018-06-21 13:16:54 -050019 $scope.loading = false;
20 loadPowerData();
21
22 function loadPowerData() {
23 $scope.loading = true;
Gunnar Mills006aaa02018-06-26 16:08:59 -050024
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 Mills52b8bde2018-06-21 13:16:54 -050040 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 Mills006aaa02018-06-26 16:08:59 -050050 getPowerCapPromise,
Gunnar Mills52b8bde2018-06-21 13:16:54 -050051 ];
52
53 $q.all(promises).finally(function() {
54 $scope.loading = false;
55 });
56 }
Gunnar Mills006aaa02018-06-26 16:08:59 -050057
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 Mills52b8bde2018-06-21 13:16:54 -0500104 }
105 ]);
106
107})(angular);