PunchThrough / node-red-contrib-bean

Node-Red nodes for the LightBlue Bean
23 stars 16 forks source link

Doesn't pass async reads to HTTP Response #18

Open webxl opened 8 years ago

webxl commented 8 years ago

According to https://github.com/node-red/node-red/issues/714#issuecomment-217383660, the original msg object needs to be modified and sent back for it to work. Not sure if this is the best fix (using .once() in the input event handler), but I got beanTemp.js working properly like this:

        // respond to inputs....
        this.on('input', function (msg) {
          if (this.beanConfig && this.beanConfig.device){

            this.beanConfig.device.once('temp', function(temp, valid){
              msg.topic = "temp";
              msg.payload = (temp);
              this.send(msg);
            }.bind(this));

            this.beanConfig.requestTemp();  
          } else {
            this.error('Bean not connected');
            this.send(msg);
          }
        });

        // this.beanConfig.on("connected", function() {
        //     this.beanConfig.device.on('temp', tempDataReceived);
        // }.bind(this));

At first I tried a persistent msg variable in the closure that tempDataReceived could access, but that could lead to race conditions I think. See https://node-red.slack.com/archives/general/p1476118415001635