TheThingSystem / node-thing-client

A node.js module to interface with TheThingSystem steward, as a thing
MIT License
2 stars 4 forks source link

thingID not defined: Invoking perform request #6

Closed GuySawyer closed 10 years ago

GuySawyer commented 10 years ago

I have altered the code in node-thing-client and node-taas-client to be able to toggle the value of a global variable, and thereafter, the output of a GPIO to a switch.

The perform section of thing-client looks like this:

            , '/api/v1/thing/perform':
                function() {
                  var task, taskID, response;

                  response = { path: '/api/v1/thing/report', requestID: message.requestID, tasks: {} };

                  for (taskID in message.tasks) if (message.tasks.hasOwnProperty(taskID)) {
                    task = message.tasks[taskID];

                    if (task.perform !== 'toggle') {
                      response.tasks[taskID] = { error: { permanent: true, diagnostic: 'invalid perform value' }};
                      continue;
                    }
                    if ((!task.parameter) || (typeof task.parameter !== 'string') || (task.parameter.length === 0)) {
                      response.tasks[taskID] = { error: { permanent: true, diagnostic: 'invalid parameter value' }};
                      continue;
                    }
                    if (task.testOnly) {
                      response.tasks[taskID] = { success: true };
                      continue;
                    }

                    console.log('>>> light: ' + task.parameter);

                    if (switch1Temp === 0)
                    {
                        switch1Temp = 1;
                    }
                    else if (switch1Temp ===1)
                    {
                        switch1Temp = 0;
                    }

                  }

                  thing.reply(response);

                }
            }[message.path];
    if (!f) return console.log ('invalid message: ' + JSON.stringify(message));
    f(thing, thingID, message);

The idea is to toggle the value of a global variable which will then be used inside the interval function to set the value of the output GPIO.

The relevant section in taas-client looks like this:

}).on('ready', function(channel, data) {
  console.log('ready ' + channel + ' data='+ JSON.stringify(data));

  if (channel !== 'management') return;

  console.log('ready, set, go!');
  steward.performActors('device/switch/rs/light', 'toggle', 'switch toggled', function(message) {
    console.log(require('util').inspect(message, { depth: null }));
  });

One thing I am unsure about in this code is the parameter value "switch toggled". I put that in there just because there was something there before I eddited it, but I don't know where this value is used or where it get received.

Anyway, the problem comes when I try to run taas-client after successfully running thing-client.

This is the ouput of the thing-client:

>>> recv: {"path":"/api/v1/thing/perform","requestID":"6","tasks":{"39512110":{"perform":"toggle","parameter":"\"switch toggled\"","testOnly":false}}}
{ path: '/api/v1/thing/perform',
  requestID: '6',
  tasks: 
   { '39512110': 
      { perform: 'toggle',
        parameter: '"switch toggled"',
        testOnly: false } } }

/home/debian/node_modules/thing-client/beagle1.js:533
    f(thing, thingID, message);   
             ^
ReferenceError: thingID is not defined
    at null.<anonymous> (/home/debian/node_modules/thing-client/beagle1.js:533:14)
    at EventEmitter.emit (events.js:117:20)
    at WebSocket.<anonymous> (/home/debian/node_modules/thing-client/thing-client.js:183:37)
    at WebSocket.EventEmitter.emit (events.js:98:17)
    at Receiver.self._receiver.ontext (/home/debian/node_modules/thing-client/node_modules/ws/lib/WebSocket.js:647:10)
    at Receiver.opcodes.1.finish (/home/debian/node_modules/thing-client/node_modules/ws/lib/Receiver.js:397:14)
    at Receiver.expectHandler (/home/debian/node_modules/thing-client/node_modules/ws/lib/Receiver.js:384:31)
    at Receiver.add (/home/debian/node_modules/thing-client/node_modules/ws/lib/Receiver.js:93:24)
    at CleartextStream.firstHandler (/home/debian/node_modules/thing-client/node_modules/ws/lib/WebSocket.js:627:22)
    at CleartextStream.EventEmitter.emit (events.js:95:17)

This is the output of the taas-client:

debian@steward:~/node_modules/taas-client$ sudo node test3.js
*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>
*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>
console
{ event: 'establish', url: 'wss://steward.local.:8888/console' }
open console
ready console data=["developer"]
open management
ready management data={"role":"monitor"}
ready, set, go!
{ requestID: '2',
  actors: { 'device/4': { status: 'success' } } }

And this is the Steward log from immediately before running the taas-client:

info: [server] wss 192.168.2.103 34209 /console loopback=false, subnet=true, local=true, remoteAddress=192.168.2.103, remotePort=34209, secure=true, clientSerialNo=20, event=connection
info: [server] wss 192.168.2.103 34210 /manage loopback=false, subnet=true, local=true, remoteAddress=192.168.2.103, remotePort=34210, secure=true, clientSerialNo=21, event=connection
info: [server] wss 192.168.2.102 54243 /manage loopback=false, subnet=true, local=true, remoteAddress=192.168.2.102, remotePort=54243, secure=true, clientSerialNo=19, event=close, userID=2, clientID=.things/3, code=1000, message=

I tested with the original thing-client and taas-client. It works fine. But with mine, I get this problem with thingID. I'm sure its probably a small syntax error, but I've been searching for a while and can't see anything.

Thanks in advance.

GuySawyer commented 10 years ago

Nevermind, finally found it. It was just a syntax error. The function getToWork was passing variable (thingIDswitch) "my own code". so it "wasn't defined".