Joery-M / Govee-LAN-Control

An Node.js package to control Govee devices with LAN
https://joery.com/govee-lan-control
MIT License
21 stars 4 forks source link

Cannot read properties of undefined (reading 'send') #2

Closed JustJoostNL closed 1 year ago

JustJoostNL commented 1 year ago

Hey! First I want to thank you for creating this! This is all I needed!

Unfortunately, I get an error when I am using the govee.discover() function, this is the error I get back: (node:16484) UnhandledPromiseRejectionWarning: TypeError: Cannot read properties of undefined (reading 'send')

The error seems something to do with line 420 in your code (index.js)

Line 420 in your code: udpSocket.send(message, 0, message.length, 4001, "239.255.255.250");

I tried both govee.discover() and await govee.discover() and both give the same error.

I also tried calling it when the ready event was emitted:

async function goveeControl(){
    const Govee = require("govee-lan-control");
    const govee = new Govee.default();

    console.log("Alright!")
    win.webContents.send('log', "Alright!");
    govee.on('ready', async () => {
        console.log("Server and client are ready to go!");
        console.log("Connecting to Govee...");
        win.webContents.send('log', "Connecting to Govee...");
        await govee.discover().then(r => {
            console.log(r);
        })
    });

}

But when I use this, only the text "Alright!" is getting outputted, and that one is outside of the 'ready' event, so I guess the 'ready' event is never emitted. Maybe that is a part of the problem.

Again, I really appreciate your work!

Thanks for helping in advance!

Joery-M commented 1 year ago

The ready event not being fired was indeed my mistake, but that is fixed now in v2.1.0.

You typically don't have to call discover if you just created the instance. By default it automatically starts discovering when you create the class.

If you want to disable this, you can disable it (in 2.1.0) by doing this:

new Govee.default({
    startDiscover: false
});

So this is how your script would look like:

async function goveeControl() {
    const Govee = require("govee-lan-control");
    const govee = new Govee.default();

    console.log("Alright!");
    win.webContents.send("log", "Alright!");
    govee.on("ready", () => {
        console.log("Server and client are ready to go!");
        console.log("Connecting to Govee...");
        win.webContents.send("log", "Connecting to Govee...");

        // You could also put this outside of the ready listener
        govee.on("deviceAdded", (device) => {
            console.log(device.model);
        });
    });
}

Thanks for making the issue!