152b8bde9SGunnar Mills/** 252b8bde9SGunnar Mills * Controller for power-usage 352b8bde9SGunnar Mills * 452b8bde9SGunnar Mills * @module app/serverControl 552b8bde9SGunnar Mills * @exports powerUsageController 652b8bde9SGunnar Mills * @name powerUsageController 752b8bde9SGunnar Mills */ 852b8bde9SGunnar Mills 952b8bde9SGunnar Millswindow.angular && (function(angular) { 1052b8bde9SGunnar Mills 'use strict'; 1152b8bde9SGunnar Mills 1252b8bde9SGunnar Mills angular.module('app.serverControl').controller('powerUsageController', [ 13*27ce84d2Sbeccabroek '$scope', '$window', 'APIUtils', '$route', '$q', 'toastService', 14*27ce84d2Sbeccabroek function($scope, $window, APIUtils, $route, $q, toastService) { 1552b8bde9SGunnar Mills $scope.power_consumption = ''; 16006aaa0fSGunnar Mills $scope.power_cap = {}; 1752b8bde9SGunnar Mills $scope.loading = false; 1852b8bde9SGunnar Mills loadPowerData(); 1952b8bde9SGunnar Mills 2052b8bde9SGunnar Mills function loadPowerData() { 2152b8bde9SGunnar Mills $scope.loading = true; 22006aaa0fSGunnar Mills 23006aaa0fSGunnar Mills var getPowerCapPromise = APIUtils.getPowerCap().then( 24006aaa0fSGunnar Mills function(data) { 25006aaa0fSGunnar Mills $scope.power_cap = data.data; 26006aaa0fSGunnar Mills }, 27006aaa0fSGunnar Mills function(error) { 28006aaa0fSGunnar Mills console.log(JSON.stringify(error)); 29006aaa0fSGunnar Mills }); 30006aaa0fSGunnar Mills 3152b8bde9SGunnar Mills var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then( 3252b8bde9SGunnar Mills function(data) { 3352b8bde9SGunnar Mills $scope.power_consumption = data; 3452b8bde9SGunnar Mills }, 3552b8bde9SGunnar Mills function(error) { 3652b8bde9SGunnar Mills console.log(JSON.stringify(error)); 3752b8bde9SGunnar Mills }); 3852b8bde9SGunnar Mills 3952b8bde9SGunnar Mills var promises = [ 4052b8bde9SGunnar Mills getPowerConsumptionPromise, 41006aaa0fSGunnar Mills getPowerCapPromise, 4252b8bde9SGunnar Mills ]; 4352b8bde9SGunnar Mills 4452b8bde9SGunnar Mills $q.all(promises).finally(function() { 4552b8bde9SGunnar Mills $scope.loading = false; 4652b8bde9SGunnar Mills }); 4752b8bde9SGunnar Mills } 48006aaa0fSGunnar Mills 49006aaa0fSGunnar Mills $scope.setPowerCap = function() { 50006aaa0fSGunnar Mills // The power cap value will be undefined if outside range 51006aaa0fSGunnar Mills if (!$scope.power_cap.PowerCap) { 52*27ce84d2Sbeccabroek toastService.error( 53*27ce84d2Sbeccabroek 'Power cap value between 100 and 10,000 is required'); 54006aaa0fSGunnar Mills return; 55006aaa0fSGunnar Mills } 56006aaa0fSGunnar Mills $scope.loading = true; 57006aaa0fSGunnar Mills var promises = [ 58006aaa0fSGunnar Mills setPowerCapValue(), 59006aaa0fSGunnar Mills setPowerCapEnable(), 60006aaa0fSGunnar Mills ]; 61006aaa0fSGunnar Mills 622264b42eSbeccabroek $q.all(promises) 632264b42eSbeccabroek .then( 642264b42eSbeccabroek function() { 65*27ce84d2Sbeccabroek toastService.success('Power cap settings saved'); 662264b42eSbeccabroek }, 672264b42eSbeccabroek function(errors) { 68*27ce84d2Sbeccabroek toastService.error('Power cap settings could not be saved'); 692264b42eSbeccabroek }) 702264b42eSbeccabroek .finally(function() { 71006aaa0fSGunnar Mills $scope.loading = false; 72006aaa0fSGunnar Mills }); 73006aaa0fSGunnar Mills }; 74006aaa0fSGunnar Mills $scope.refresh = function() { 75006aaa0fSGunnar Mills $route.reload(); 76006aaa0fSGunnar Mills }; 77006aaa0fSGunnar Mills 78006aaa0fSGunnar Mills function setPowerCapValue() { 79006aaa0fSGunnar Mills return APIUtils.setPowerCap($scope.power_cap.PowerCap) 80006aaa0fSGunnar Mills .then( 81006aaa0fSGunnar Mills function(data) {}, 82006aaa0fSGunnar Mills function(error) { 83006aaa0fSGunnar Mills console.log(JSON.stringify(error)); 842264b42eSbeccabroek return $q.reject(); 85006aaa0fSGunnar Mills }); 86006aaa0fSGunnar Mills } 87006aaa0fSGunnar Mills 88006aaa0fSGunnar Mills function setPowerCapEnable() { 89006aaa0fSGunnar Mills return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable) 90006aaa0fSGunnar Mills .then( 91006aaa0fSGunnar Mills function(data) {}, 92006aaa0fSGunnar Mills function(error) { 93006aaa0fSGunnar Mills console.log(JSON.stringify(error)); 942264b42eSbeccabroek return $q.reject(); 95006aaa0fSGunnar Mills }); 96006aaa0fSGunnar Mills } 9752b8bde9SGunnar Mills } 9852b8bde9SGunnar Mills ]); 9952b8bde9SGunnar Mills})(angular); 100