jvmahon / Homebridge-HomeSeer4

Homebridge Plugin for HomeSeer 3 and 4
28 stars 8 forks source link

Addition of device in HomeSeer caused plugin/HomeBridge to crash #129

Closed mikeleo closed 3 years ago

mikeleo commented 3 years ago

In HomeSeer, I rescanned a device that caused an additional child to get added. When I triggered the event, Motion Sensor on a Switch, it appears this correlated with a crash in the plugin.

...
[1/3/2021, 5:48:44 PM] TypeError: Cannot read property 'notifyObjects' of undefined
    at Socket.<anonymous> (/home/homebridge/.nvm/versions/node/v15.2.0/lib/node_modules/homebridge-homeseer4/lib/Setup Listener.js:76:51)
    at Socket.emit (node:events:329:20)
    at addChunk (node:internal/streams/readable:304:12)
    at readableAddChunk (node:internal/streams/readable:279:9)
    at Socket.Readable.push (node:internal/streams/readable:218:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:192:23)
[1/3/2021, 5:48:44 PM] Got SIGTERM, shutting down Homebridge...
[1/3/2021, 5:48:49 PM] [HB Supervisor] Homebridge Process Ended. Code: 143, Signal: null
[1/3/2021, 5:48:54 PM] [HB Supervisor] Restarting Homebridge...
...

Looking at the line in question,

// Setup Listener.js
...
            case(thisUpdate.startsWith("DC")): // Handle received data
                {   
                myData = new HSData( thisUpdate.split(","));
                    //Only need to do an update if there is HomeKit data associated with it!, Which occurs if the notifyObjects array is not undefined.
                    if( HomeSeerData.HomeSeerDevices[myData.ref].notifyObjects !== undefined)   
                    {
                        globals.log(`HomeSeer device: ${cyan(myData.ref)}: new value ${cyan(myData.newValue)}, had old value ${cyan(myData.oldValue)}` );

                        HomeSeerData.processReceivedData(myData.ref, parseFloat(myData.newValue))
                    } 
                    break;
                };
...         

the code is not checking if the myData.ref is present in the HomeSeerData.HomeSeerDevices array before further checking if the notifyObjects is present. An option to address issue might be to reload the HomeSeerData.HomeSeerDevices if the reference is not present. In the odd case it isn't still present after reload, there might need to be consideration of logging error and stubbing the device to prevent continuous reloading.

jvmahon commented 3 years ago

I'm not doing much to this code anymore, but if I understand the issue, it sounds like this can be fixed by changing the "if" to something like

if( (HomeSeerData.HomeSeerDevices[myData.ref]) && (HomeSeerData.HomeSeerDevices[myData.ref].notifyObjects !== undefined))

Can you give this a try and see if it works (I am no longer running HomeSeer). If so, I'll add it into the main code and re-publish.

jvmahon commented 3 years ago

I added the change that I proposed in my prior post to version 1.0.13. If you are still having an issue, re-open this comment and explain further.