1/**
2 * Controller for server LED
3 *
4 * @module app/serverControl
5 * @exports serverLEDController
6 * @name serverLEDController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.serverControl').controller('serverLEDController', [
13    '$scope', '$window', '$route', 'APIUtils', 'dataService', 'toastService',
14    function($scope, $window, $route, APIUtils, dataService, toastService) {
15      $scope.dataService = dataService;
16
17      APIUtils.getLEDState().then(function(state) {
18        $scope.displayLEDState(state);
19      });
20
21      $scope.displayLEDState = function(state) {
22        if (state == APIUtils.LED_STATE.on) {
23          dataService.LED_state = APIUtils.LED_STATE_TEXT.on;
24        } else {
25          dataService.LED_state = APIUtils.LED_STATE_TEXT.off;
26        }
27      };
28
29      $scope.toggleLED = function() {
30        var toggleState =
31            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
32            APIUtils.LED_STATE.off :
33            APIUtils.LED_STATE.on;
34        dataService.LED_state =
35            (dataService.LED_state == APIUtils.LED_STATE_TEXT.on) ?
36            APIUtils.LED_STATE_TEXT.off :
37            APIUtils.LED_STATE_TEXT.on;
38        APIUtils.setLEDState(toggleState)
39            .then(
40                function(response) {},
41                function(errors) {
42                  toastService.error(
43                      'Failed to turn LED light ' +
44                      (toggleState ? 'on' : 'off'));
45                  console.log(JSON.stringify(errors));
46                  // Reload to get correct current LED state
47                  $route.reload();
48                })
49      };
50    }
51  ]);
52})(angular);
53