/** * Controller for power-usage * * @module app/serverControl * @exports powerUsageController * @name powerUsageController */ window.angular && (function(angular) { 'use strict'; angular.module('app.serverControl').controller('powerUsageController', [ '$scope', '$window', 'APIUtils', '$route', '$q', 'ngToast', function($scope, $window, APIUtils, $route, $q, ngToast) { $scope.power_consumption = ''; $scope.power_cap = {}; $scope.loading = false; loadPowerData(); function loadPowerData() { $scope.loading = true; var getPowerCapPromise = APIUtils.getPowerCap().then( function(data) { $scope.power_cap = data.data; }, function(error) { console.log(JSON.stringify(error)); }); var getPowerConsumptionPromise = APIUtils.getPowerConsumption().then( function(data) { $scope.power_consumption = data; }, function(error) { console.log(JSON.stringify(error)); }); var promises = [ getPowerConsumptionPromise, getPowerCapPromise, ]; $q.all(promises).finally(function() { $scope.loading = false; }); } $scope.setPowerCap = function() { // The power cap value will be undefined if outside range if (!$scope.power_cap.PowerCap) { ngToast.danger('Power cap value between 100 and 10,000 is required'); return; } $scope.loading = true; var promises = [ setPowerCapValue(), setPowerCapEnable(), ]; $q.all(promises) .then( function() { ngToast.success('Power cap settings saved'); }, function(errors) { ngToast.danger('Power cap settings could not be saved'); }) .finally(function() { $scope.loading = false; }); }; $scope.refresh = function() { $route.reload(); }; function setPowerCapValue() { return APIUtils.setPowerCap($scope.power_cap.PowerCap) .then( function(data) {}, function(error) { console.log(JSON.stringify(error)); return $q.reject(); }); } function setPowerCapEnable() { return APIUtils.setPowerCapEnable($scope.power_cap.PowerCapEnable) .then( function(data) {}, function(error) { console.log(JSON.stringify(error)); return $q.reject(); }); } } ]); })(angular);