ageorgios / homebridge-sonoff-tasmota-http

Control Sonoff Basic devices with Tasmota firmware through Homebridge
Other
15 stars 19 forks source link

JSON Parsing error causes homebridge to restart #18

Open irdeto-hackwestern opened 4 years ago

irdeto-hackwestern commented 4 years ago

I am getting the following error:

SyntaxError: Unexpected token N in JSON at position 0 at JSON.parse () at Request._callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/index.js:34:29) at Request.self.callback (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:185:22) at Request.emit (events.js:210:5) at Request. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1161:10) at Request.emit (events.js:210:5) at IncomingMessage. (/homebridge/node_modules/homebridge-sonoff-tasmota-http/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:299:28) at IncomingMessage.emit (events.js:215:7) at endReadableNT (_stream_readable.js:1183:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)

I added a fix to wrap the JSON parse which fixes the issue:

SonoffTasmotaHTTPAccessory.prototype.getState = function(callback) {
var that = this
request("http://" + that.hostname + "/cm?user=admin&password=" + that.password + "&cmnd=Power" + that.relay, function(error, res if (error) return callback(error);
that.log("Sonoff HTTP: " + that.hostname + "body: " + body);
try{
var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
that.log("Sonoff HTTP: " + that.hostname + ", Relay " + that.relay + ", Get State: " + JSON.stringify(sonoff_reply));
switch (sonoff_reply["POWER" + that.relay]) {
case "ON":
callback(null, 1);
break;
case "OFF":
callback(null, 0);
break;
}
}catch(e){
return callback(error);
}
})
}

juliangorge commented 3 years ago

same issue, what's your firmware version?