wailuen / homebridge-sensibo-sky

Homebridge Sensibo Sky
33 stars 25 forks source link

homebridge-sensibo-sky crashes Homebridge #33

Open zork17 opened 5 years ago

zork17 commented 5 years ago

I tried this plugin a year ago and it didn't work. I tried it now (after installing the latest versions of everything) and I get the same error. This is the config file:

{
            "platform": "SensiboSky",
            "name": "Sensibo",
            "apiKey": "XXX"
}

This is the error I get:

Feb 25 10:29:42 RSBPI homebridge[9573]: /usr/lib/node_modules/homebridge-sensibo-sky/lib/sensiboapi.js:117
Feb 25 10:29:42 RSBPI homebridge[9573]: callback(data.result[i].acState);
Feb 25 10:29:42 RSBPI homebridge[9573]: ^
Feb 25 10:29:42 RSBPI homebridge[9573]: TypeError: Cannot read property 'acState' of undefined
Feb 25 10:29:42 RSBPI homebridge[9573]: at /usr/lib/node_modules/homebridge-sensibo-sky/lib/sensiboapi.js:117:28
Feb 25 10:29:42 RSBPI homebridge[9573]: at IncomingMessage.<anonymous> (/usr/lib/node_modules/homebridge-sensibo-sky/lib/sensiboapi.js:50:21)
Feb 25 10:29:42 RSBPI homebridge[9573]: at emitNone (events.js:91:20)
Feb 25 10:29:42 RSBPI homebridge[9573]: at IncomingMessage.emit (events.js:185:7)
Feb 25 10:29:42 RSBPI homebridge[9573]: at endReadableNT (_stream_readable.js:974:12)
Feb 25 10:29:42 RSBPI homebridge[9573]: at _combinedTickCallback (internal/process/next_tick.js:74:11)
Feb 25 10:29:42 RSBPI homebridge[9573]: at process._tickCallback (internal/process/next_tick.js:98:9)
Feb 25 10:29:42 RSBPI systemd[1]: homebridge.service: main process exited, code=exited, status=1/FAILURE
Feb 25 10:29:42 RSBPI systemd[1]: Unit homebridge.service entered failed state.

The only difference I can think of from a simple homebridge system is that I have 2 homebridge services since in total I have more than 100 devices. I tried putting the sensibo-sky platform in each of them and it crashes at the same line.

Is there a way to debug or fix this?

zork17 commented 5 years ago

Any help?

zork17 commented 5 years ago

I can see in the lines just above the crash:

        //We get the last 10 items in case the first one failed.
        if (debug) {
                    console.log("**[Sensibo API Debug] DeviceID :\n", deviceID.trim());
        }
        GET({ path: 'pods/'+deviceID+'/acStates?fields=status,reason,acState&limit=10&apiKey='+this.apiKey },function(data){

that the number 10 is mentioned. I have 10 Sensibo devices - maybe this causes the problem?

zork17 commented 5 years ago

I managed to fix it with the following change in getState: Old:

getState: function(deviceID, callback) {
        //We get the last 10 items in case the first one failed.
        GET({ path: 'pods/'+deviceID+'/acStates?fields=status,reason,acState&limit=10&apiKey='+this.apiKey },function(data){
            if (data && data.status && data.status == 'success' && data.result && data.result instanceof Array) {
                var i=0;
                for (i=0; i<data.result.length; i++) {
                    if (data.result[i].status=="Success") break;
                }
                if (i==data.result.length) i=0;
                callback(data.result[i].acState);
            } else {
                callback();
            }
        })      
    },

New

getState: function(deviceID, callback) {
        GET({ path: 'pods/'+deviceID+'/acStates?fields=status,reason,acState&limit=20&apiKey='+this.apiKey },function(data){
            if (data && data.status && data.status == 'success' && data.result && data.result instanceof Array) {
                var i=0;
                for (i=0; i<data.result.length; i++) {
                    if (data.result[i].status=="Success") break;
                }
                if (i==0 && data.result.length==0)
                    callback();
                else
                {
                    if (i==data.result.length) i=0;
                    callback(data.result[i].acState);
                }
            } else {
                callback();
            }
        })      
    },

So basically adding:

if (i==0 && data.result.length==0)
    callback();

I do not know what is the effecting of this, but it works. I found out that in many cases in my system both i and data.result.length are 0 after the loop. Any chance of adding this to the code?

HFi6QhEqLVxy2V commented 5 years ago

Thank you!