How to use the tuyapi/lib/message-parser.js.CommandType.UDP_NEW function in tuyapi

To help you get started, we’ve selected a few tuyapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Apollon77 / ioBroker.tuya / main.js View on Github external
const foundIps = Object.keys(discoveredEncryptedDevices);
    adapter.log.debug(deviceId + ': Try to initialize encrypted device with received UDP messages (#IPs: ' + foundIps.length + '): version=' + knownDevices[deviceId].version + ', key=' + knownDevices[deviceId].localKey);
    const parser = new MessageParser({version: knownDevices[deviceId].version || 3.3, key: knownDevices[deviceId].localKey});

    for (let ip of foundIps) {
        if (discoveredEncryptedDevices[ip] === true) continue;

        let data;
        try {
            data = parser.parse(discoveredEncryptedDevices[ip])[0];
        }
        catch (err) {
            adapter.log.debug(deviceId + ': Error on decrypt try: ' + err);
            continue;
        }
        if (!data.payload || !data.payload.gwId || (data.commandByte !== CommandType.UDP && data.commandByte !== CommandType.UDP_NEW)) {
            adapter.log.debug(deviceId + ': No relevant Data for decrypt try: ' + JSON.stringify(data));
            continue;
        }
        if (data.payload.gwId === deviceId) {
            discoveredEncryptedDevices[data.payload.ip] = true;
            initDevice(data.payload.gwId, data.payload.productKey, data.payload, ['name'], callback);
            return true;
        }
    }
    adapter.log.info(deviceId + ': None of the discovered devices matches :-(');
    callback && callback();
}

tuyapi

An easy-to-use API for devices that use Tuya's cloud services

MIT
Latest version published 2 years ago

Package Health Score

66 / 100
Full package analysis