Bootstrap Switch setState Via Socket.io

94 views
Skip to first unread message

Neil Mueller

unread,
Oct 24, 2015, 10:36:59 PM10/24/15
to nodejs
Here is a section of code im using. i can log the result to the browser console and it returns true or false just fine but when i try to use the bootstrap switch to set the state it doesn't work.. im new to writing code so it might be something simple but i have tried all sorts of things and cant seem to figure it out.

here is a link to the full code.
https://github.com/neileum/Pool-Automation


Client Side Code:
var socket = io.connect();

// Checkbox elements
var checkboxes = {
red
: $('#checkbox-red'),
green
: $('#checkbox-green'),
blue
: $('#checkbox-blue'),
yellow
: $('#checkbox-yellow')
}

for(var color in checkboxes) {
checkboxes
[color].bootstrapSwitch();
}

checkboxes
['red'].on('switchChange.bootstrapSwitch', function(event,  state) {
socket
.emit('red', { state: state });
console
.log(callback);
});


socket
.on ('red', function (state) {
 $
('#checkbox-red').bootstrapSwitch('setState', state);
 console
.log(state);
});
Server Side Code:
Enter
    var mraa = require('mraa');
var m = require('mraa'); //require mraa
console
.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
// Map GPIO block pins to MRAA pin numbers
// Reference: https://learn.sparkfun.com/tutorials/installing-libmraa-on-ubilinux-for-edison
var pins = {
  GP44
: 31,
  GP45
: 45,
  GP46
: 32,
  GP47
: 46
};

// Initialize LED controls
var leds = {
  red
: new mraa.Gpio(6),
  green
: new mraa.Gpio(6),
  blue
: new mraa.Gpio(6),
  yellow
: new mraa.Gpio(6)
};

// Set direction of LED controls to out
for(var color in leds) {
  leds
[color].dir(mraa.DIR_OUT);
}

function toggleLed(led, state) {
  led
.write(state ? 1 : 0);
}

function toggleLeds(leds, state) {
 
for(var color in leds) {
    leds
[color].write(state ? 1 : 0);
 
}
}

function printLedState(color, state) {
  console
.log('color: ' + color + ', state: ' + state);
}

    socket
.on('red', function(data, state, callback) {
     toggleLed
(leds['red'], data.state);
     printLedState
('red', data.state);
     socket
.broadcast.emit ('red', state);
   
});
code here...



Reply all
Reply to author
Forward
0 new messages