How to use the tuyapi/lib/message-parser.js.MessageParser 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
function discoverLocalDevices() {
    server = dgram.createSocket('udp4');
    server.on('listening', function () {
        //const address = server.address();
        adapter.log.info('Listen for local Tuya devices on port 6666');
    });
    const normalParser = new MessageParser({version: 3.1});
    server.on('message', function (message, remote) {
        adapter.log.debug('Discovered device: ' + remote.address + ':' + remote.port + ' - ' + message);
        let data;
        try {
            data = normalParser.parse(message)[0];
        } catch (err) {
            return;
        }
        if (!data.payload || !data.payload.gwId || data.commandByte !== CommandType.UDP) return;
        if (knownDevices[data.payload.gwId] && knownDevices[data.payload.gwId].device) return;
        initDevice(data.payload.gwId, data.payload.productKey, data.payload, ['name']);
    });
    server.on('error', err => {
        adapter.log.warn('Can not Listen for Encrypted UDP packages: ' + err);
    });
    server.bind(6666);
github Apollon77 / ioBroker.tuya / main.js View on Github external
function checkDiscoveredEncryptedDevices(deviceId, callback) {
    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;
        }

tuyapi

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

MIT
Latest version published 1 year ago

Package Health Score

67 / 100
Full package analysis