stevegw (39) [Avatar] Offline
#1
I might be missing something but I have used my PI to run the examples as per Chapter 7 I have the simulator running (This took a little time but its running)

See attached image or the text below which show running PI command window with output

Simulated LED 1 actuator started!
Simulated Temperature & Humidity sensor started!
HTTP server started...
WebSocket server started...
Your WoT Pi is up and running on port 8484
not anymore!
there is someone!
not anymore!
there is someone!
not anymore!
Change detected by plugin for LED 1...
Temperature: 27 C, humidity 61 %
there is someone!

Now if I try http://raspberrypi.local:8484/pi/actuators I see nothing in the browser - I expected to see the output from the actuators - maybe its becuase I'm in simulation I not sure.

any thoughts
domguinard (69) [Avatar] Offline
#2
Hi Steven,

Thanks for your post! When you say that you see nothing do you mean that you get an error or just an empty page?

To keep the code simple, the HTML Converter Middleware works only on the leaves of the resource tree (e.g., works for /pi/actuators/leds/1 but not for /pi/actuators/). Extending it to support non-leaves resources is left as an exercise (the HTML Middleware Converter in Chapter 8 does support all resources).


Try: http://raspberrypi.local:8484/pi/actuators/leds/1
or http://raspberrypi.local:8484/pi/sensors/temperature
does that work?

Let us know!
33831 (8) [Avatar] Offline
#3
I have a similar problem. I get Cannot GET /pi/sensors/pir when I type in http://localhost:8484/pi/sensors/pir address into Chrome on a simulator running on my mac. The console shows the converter is being called but the website gives the error. When i comment out all the COAP and websockets calls in the http.js file in the book version, the web pages doesn't convert to hml. In this case it displays the json with no error. Please look at this. Attached the file from the book where I commented out the other calls so you can try it yourselves.

In fact, shouldn't I be able to go into the Chapter 7 implementation folder on the code examples, run npm install and node wot.server and everything should work on desktop simulater, right? Well it doesn't when I do that. I get an error around the Object.observe from the LED plugins.

Object.observe(what, function (changes) {
^

TypeError: Object.observe is not a function
at observe (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/plugins/internal/ledsPlugin.js:29:10)
at Object.exports.start (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/plugins/internal/ledsPlugin.js:10:3)
at Object.<anonymous> (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/wot-server.js:14:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
33831 (8) [Avatar] Offline
#4
domguinard wrote:Hi Steven,

Thanks for your post! When you say that you see nothing do you mean that you get an error or just an empty page?

To keep the code simple, the HTML Converter Middleware works only on the leaves of the resource tree (e.g., works for /pi/actuators/leds/1 but not for /pi/actuators/). Extending it to support non-leaves resources is left as an exercise (the HTML Middleware Converter in Chapter 8 does support all resources).


Try: http://raspberrypi.local:8484/pi/actuators/leds/1
or http://raspberrypi.local:8484/pi/sensors/temperature
does that work?

Let us know!

Dom, could you look at my issue in this thread. I'm pulling my hair out and i don't have much to spare.
domguinard (69) [Avatar] Offline
#5

In fact, shouldn't I be able to go into the Chapter 7 implementation folder on the code examples, run npm install and node wot.server and everything should work on desktop simulater, right? Well it doesn't when I do that. I get an error around the Object.observe from the LED plugins.

Object.observe(what, function (changes) {
^

TypeError: Object.observe is not a function
at observe (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/plugins/internal/ledsPlugin.js:29:10)
at Object.exports.start (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/plugins/internal/ledsPlugin.js:10:3)
at Object.<anonymous> (/Projects/wot-book/chapter7-implementation/part1-2-direct-gateway/wot-server.js:14:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)


Worry no more, you are not doing anything wrong! The only issue is that you are using the wrong version of Node (see https://forums.manning.com/posts/list/38200.page). We are working on fixing that but currently you have to stick with Node version 4.X as suggested in the book. The Observe function has been removed since Node 5 :-/

If you are unsure which version you are running do:
node --version

This should give you a 4.X version. If not then install 4.X from: https://nodejs.org/dist/latest-v4.x/. Note that you can also use the great NVM tool (see https://github.com/creationix/nvm) to install several version and switch back and forth between them.

Hope that helps, let us know if there are any more issues.

33831 (8) [Avatar] Offline
#6
Unfortunately, the suggestion that we use a 4.x version doesn't fix the converter issue. As I mentioned previously, I used the attach wot-server code,(commented out all calls that are not relevant to http server running the converter) and the book code displays the same error I get. If you use this URL http://localhost/pi/sensors/pir, you get JSON results in the browser window. If you try to specify an html url i.e. http://localhost/pi/sensors/pir.html, it says Cannot GET /pi/sensors/pir.html.

I used NVM to install node 4.8.0 and updated all the NPM dependencies and it still does the same thing. Your code does the same thing as well when i try and run it.
domguinard (69) [Avatar] Offline
#7
Hi,

Okay, now we understand what was going on, very sorry for the slow reply but it was hard to reproduce the issue. What happened was that some browsers are sending
*/*
or
application/json
in the accept header (see: http://stackoverflow.com/questions/18902293/nodejs-validating-request-type-checking-for-json-or-html). Since we first checked for JSON in the code this would mean always returning JSON.

The easiest way to fix this is simply by removing the check for JSON but defaulting to JSON ( see: https://github.com/webofthings/wot-book/commit/b83756fc4c6ce67a212e8c64615efa4cd6e80998 ).

However, as we learned in Chapter 6.1.4 when talking about content-negotiation clients might specify several types they accept with a preference (expressed a q={0-1}). The simple modification would still mean that:
Accept: text/html,application/xhtml+xml,application/xml;q=0.7,image/webp,application/json;q=0.8

would return HTML instead of JSON (as HTML is in the Accept header, nevertheless with a lower priority than JSON: 0.7 vs 0.8 ).

To fix this the right way we used the
req.accepts(['json', 'html', 'application/x-msgpack'])

Express function which takes into account q and returns the best type for the client among the ones specified in the array.

You can find the diff here: https://github.com/webofthings/wot-book/commit/9ab17edeaa4284290cd6563d81336503cea470e7
and the full code here: https://github.com/webofthings/wot-book/blob/master/chapter7-implementation/part1-2-direct-gateway/middleware/converter.js

Thanks again for your patience on this one and for reporting it. We will make sure we modify the code in the errata version.