0

i am trying to make a function in my app that is supposed to update the status of my hue lamps when the states of one of them changed.

For now, i have done this :

async.waterfall([
function getLightsId(callback) {
    args = {
        path: {
            "username": "webjeremie"
        }
    };

    client.registerMethod("getLightState", "http://hue.jeremieledieu.fr/api/${username}/lights/", "GET");

    client.methods.getLightState(args, function (data, response) {
        var ids = [];
        for (key in data) {
            ids.push(key);
        }
        callback(null, ids);
    });
},
function getLightsState(ids, callback) {
    var lightsState = new Object();
    async.reduce(ids, {}, function(lightsState, id, rcallback) {
      getLightState(id, function(state) {
        lightsState[id] = state;
        rcallback(null, lightsState);
        });
    }, callback);
},


], function (err, currentState) {

        io.sockets.on('connection', function (socket) {
            socket.emit('updateLightsState', currentState);
            previousState = currentState;
        });
});

This get me the states of all the lamps in the currenState but im stuck at how should i constantly compare the previous state with the current state, and only emit when the currentState != previousState

Any ideas ?

Abhinav
  • 7,550
  • 10
  • 40
  • 79
Jeremie4zw
  • 616
  • 1
  • 7
  • 17

2 Answers2

0

I can give you a suggestion.

Set a value to a variable.

var origVal = "Original";
var yourVariable = "Original";

setInterval(function(){
   if(yourVariable !== origVal){
     // do something.
   }
},1000)
Amit Joki
  • 53,955
  • 7
  • 67
  • 89
0

Take a look at answer #1180790

I think, object.watch will make the work here.

Community
  • 1
  • 1
  • At linked answer, you can find linked Eli Grey`s [implementation](https://gist.github.com/eligrey/384583) to work on all major browsers. –  Apr 05 '14 at 14:36