summaryrefslogtreecommitdiffstats
path: root/app/server-control/controllers/server-led-controller.js
blob: f44bcc0fba9e4566ddc3b5b1c9b37a94ac9971c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Controller for server LED
 *
 * @module app/serverControl
 * @exports serverLEDController
 * @name serverLEDController
 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.serverControl').controller('serverLEDController', [
    '$scope', '$window', '$route', 'APIUtils', 'dataService', 'toastService',
    function($scope, $window, $route, APIUtils, dataService, toastService) {
      $scope.dataService = dataService;

      APIUtils.getLEDState().then(function(state) {
        $scope.displayLEDState(state);
      });

      $scope.displayLEDState = function(state) {
        if (state == APIUtils.LED_STATE.on) {
          dataService.LED_state = APIUtils.LED_STATE_TEXT.on;
        } else {
          dataService.LED_state = APIUtils.LED_STATE_TEXT.off;
        }
      };

      $scope.toggleLED = function() {
        var toggleState =
            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
            APIUtils.LED_STATE.off :
            APIUtils.LED_STATE.on;
        dataService.LED_state =
            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
            APIUtils.LED_STATE_TEXT.off :
            APIUtils.LED_STATE_TEXT.on;
        APIUtils.setLEDState(toggleState)
            .then(
                function(response) {},
                function(errors) {
                  toastService.error(
                      'Failed to turn LED light ' +
                      (toggleState ? 'on' : 'off'));
                  console.log(JSON.stringify(errors));
                  // Reload to get correct current LED state
                  $route.reload();
                })
      };
    }
  ]);
})(angular);
OpenPOWER on IntegriCloud