Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active March 9, 2016 21:23
Show Gist options
  • Save LarsBergqvist/db93ee938fae2c163964 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/db93ee938fae2c163964 to your computer and use it in GitHub Desktop.
WebRelay Controller
var myApp = angular.module('myApp', [])
.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('[[').endSymbol(']]');
});
var addImageToRelayObjects = function(relayObjects) {
for (var i=0; i < relayObjects.length; i++) {
var relay = relayObjects[i];
if (relay.state == 'on') {
relay.image = '/static/on_button.gif';
}
else {
relay.image = '/static/off_button.gif';
}
}
return relayObjects;
};
myApp.controller('RelaysController', ['$scope', '$http', function($scope, $http) {
$scope.header = 'Relay status';
var getRelayInfo = function() {
$http.get("api/relays").then(function(response) {
var relays = response.data.relays;
addImageToRelayObjects(relays)
$scope.relays = relays;
}, function(error) {}
);
};
$scope.toggleRelay = function(relay) {
var newState = 'off';
if (relay.state == 'off') {
newState = 'on';
}
$http.put("/WebRelay/api/relays/"+relay.id, { state : newState}).then(function(response) {
relay = response.data.relay;
if (relay.state == 'on') {
relay.image = '/static/on_button.gif';
}
else {
relay.image = '/static/off_button.gif';
}
for (var i=0; i < $scope.relays.length; i++) {
if ($scope.relays[i].id == relay.id) {
$scope.relays[i] = relay;
break;
}
}
}, function(error) {}
);
}
getRelayInfo();
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment