plasticrake / tplink-smarthome-api

TP-Link Smarthome WiFi API
MIT License
1.02k stars 141 forks source link

Suppress Error Messages #128

Closed ssmoss closed 3 years ago

ssmoss commented 3 years ago

I want to start by saying thank you so much for making this awesome library. It makes scripting TP-Link devices extremely straightforward.

I am seeking some help on an issue I encountered using the following code:

async function getPlugRealTimeUsage(host, port) {
    try {
        //Fetch TP Device
        const device = await new tplink.Client().getDevice(
            {
                host : host,
                port : port
            },
            {
                timeout : 2500
            }
        );

        //Check If Plug
        if (device.deviceType != 'plug') {
            throw new Error('Device is not a plug.');
        }

        //Get Real Time Usage Stats
        const emeter = await device.emeter.getRealtime({
            timeout : 2500
        });

        //Return Plug Usage Data
        return {
            id            : device.deviceId,
            voltage       : emeter.voltage,
            current       : emeter.current,
            power_current : emeter.power,
            power_total   : emeter.total,
            error_code    : emeter.err_code
        };
    }
    catch (error) {}
}

getPlugRealTimeUsage('192.168.1.10', 9999)

Despite catching the error it still seems to generate the following log message:

TCP 192.168.1.10:9999 Error: TCP Timeout after 2500ms
192.168.1.10:9999 {"system":{"get_sysinfo":{}}}
    at Timeout._onTimeout (.../node_modules/tplink-smarthome-api/lib/network/tcp-socket.js:59:36)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

Is there a way to prevent that message from being generated? I would like to be able to do my own error handling and associated logging.

plasticrake commented 3 years ago

try new tplink.Client({ logLevel: 'silent' }), you can also pass in your own custom logger with { logger: yourCustomLogger }.

ssmoss commented 3 years ago

try new tplink.Client({ logLevel: 'silent' }), you can also pass in your own custom logger with { logger: yourCustomLogger }.

Worked like a charm, thanks.