rudders / homebridge-http

HTTP Plugin for Homebridge
Apache License 2.0
178 stars 109 forks source link

temperature decimal parse error #16

Closed bwired-nl closed 8 years ago

bwired-nl commented 8 years ago

Hi Mr Rudders

Adding more sensors to your http plugin :-) getting a parse error with the temp status? if i send back '15' all is OK and the temp displays 15, no errors. if i send back '15.1' or '15,1' I get a parse error?

Hope you can help

{ "accessory" : "Http", "name": "Kamertemperatuur", "switchHandling": "yes", "http_method": "GET", "tempvalue_url": "http://10.0.0.47:3031/temp", "sendimmediately": "yes", "username" : "", "password" : "",
"service": "Temperature Sensor" }

part of the index.js

getState: function(callback) { if (!this.tempvalue_url) { this.log.warn("Ignoring request; No temp level url defined."); callback(new Error("No temp level url defined.")); return; }
var url = this.tempvalue_url; this.log("Getting temp level"); this.log(url);
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function(error, response, responseBody) { if (error) { this.log('HTTP get temp function failed: %s', error.message); callback(error); } else {
this.log(responseBody);
var binaryState = parseInt(responseBody); var level = binaryState; this.log("temp state is currently %s", binaryState); callback(null, level); } this.log(responseBody);
}.bind(this)); },

if (this.service == "Temperature Sensor") { var tempService = new Service.TemperatureSensor(this.name); tempService .getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this)); return [tempService];

And this is the error i get, when i send back '15.1'

Accessory [HomebridgeBwired] Getting value for Characteristic "Current Temperature" +3ms [Kamertemperatuur] Getting temp level [Kamertemperatuur] http://10.0.0.47:3031/temp [Kamertemperatuur] HTTP get temp function failed: Parse Error Accessory [HomebridgeBwired] Got Characteristic "Current Temperature" value: undefined +144ms Accessory [HomebridgeBwired] Error getting value for Characteristic "Current Temperature": Parse Error +0ms [Kamertemperatuur] undefined

rudders commented 8 years ago

What's in response and responsebody at the time of the error - I expect that's what it can't parse - probably malformed json or xml or the decimal in the response is upsetting httprequest

bwired-nl commented 8 years ago

If I do this.log(resonse) or reponse body its undefined. And only when I use the decimal? If I click on the link from within the config it gives me the right value with decimal in chrome

rudders commented 8 years ago

If you wrote the called function try wrapping the result in double quotes to see if that will parse okay

bwired-nl commented 8 years ago

You mean like this res.sendStatus("15.1"); instead of res.sendStatus('15.1');

see same result below, the undefined is the value

[Kamertemperatuur] Getting temp level [Kamertemperatuur] http://10.0.0.47:3031/temp [Kamertemperatuur] HTTP get temp function failed: Parse Error Accessory [HomebridgeBwired] Got Characteristic "Current Temperature" value: undefined +96ms Accessory [HomebridgeBwired] Error getting value for Characteristic "Current Temperature": Parse Error +1ms [Kamertemperatuur] undefined

rudders commented 8 years ago

I'm confused you're using a sendStatus to pass data back? can you share more code please?

bwired-nl commented 8 years ago

my http server is on nodejs and is using Node express this is what i send back Works great on all other devices like switches etc and like i told if i put in '15' below it works...its making me crazy :)

app.get('/temp', function (req, res) { console.log('temp info') res.sendStatus("15.1"); });

rudders commented 8 years ago

sendStatus is for the response status (refer http://expressjs.com/en/api.html) - it only expects a few http defended values - a decimal would freak it out. Don't know express personally. But their hello world example returns data via res,send. http://expressjs.com/en/starter/hello-world.html

bwired-nl commented 8 years ago

wtf:

i always use res.send before but got a message with other devices i need to use res.sendStatus() and it worked. but not with this temp values. with res,send its working :) many thanks!

res.send() Send a response of various types. res.sendStatus() Set the response status code and send its string representation as the response body.

rudders commented 8 years ago

Note the last line - it does set the vale as the return value, but also attempts to set the value as the "http status" - which a decimal value would break i expect.

res.sendStatus(statusCode)

Set the response HTTP status code to statusCode and send its string representation as the response body.

res.sendStatus(200); // equivalent to res.status(200).send('OK')
res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
If an unsupported status code is specified, the HTTP status is still set to statusCode and the string version of the code is sent as the response body.

res.sendStatus(2000); // equivalent to res.status(2000).send('2000')