blob: f44bcc0fba9e4566ddc3b5b1c9b37a94ac9971c4 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
Gunnar Mills4129f402018-07-11 16:40:58 -05002 * Controller for server LED
Iftekharul Islamcd789502017-04-19 14:37:55 -05003 *
Gunnar Mills4b1f24c2018-07-11 16:10:56 -05004 * @module app/serverControl
Gunnar Mills4129f402018-07-11 16:40:58 -05005 * @exports serverLEDController
6 * @name serverLEDController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Gunnar Mills4129f402018-07-11 16:40:58 -050012 angular.module('app.serverControl').controller('serverLEDController', [
beccabroek27ce84d2019-02-05 15:43:17 -060013 '$scope', '$window', '$route', 'APIUtils', 'dataService', 'toastService',
14 function($scope, $window, $route, APIUtils, dataService, toastService) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 $scope.dataService = dataService;
Michael Davis11dbb822017-08-11 16:42:47 -050016
Andrew Geisslerd27bb132018-05-24 11:07:27 -070017 APIUtils.getLEDState().then(function(state) {
18 $scope.displayLEDState(state);
19 });
Iftekharul Islam96bbf312017-08-22 13:44:38 -050020
Andrew Geisslerd27bb132018-05-24 11:07:27 -070021 $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 };
Iftekharul Islam96bbf312017-08-22 13:44:38 -050028
Andrew Geisslerd27bb132018-05-24 11:07:27 -070029 $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;
beccabroekb7f0ee12019-01-10 11:15:58 -060038 APIUtils.setLEDState(toggleState)
39 .then(
40 function(response) {},
41 function(errors) {
beccabroek27ce84d2019-02-05 15:43:17 -060042 toastService.error(
beccabroekb7f0ee12019-01-10 11:15:58 -060043 '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 })
Andrew Geisslerd27bb132018-05-24 11:07:27 -070049 };
50 }
51 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -050052})(angular);