Skip to content

Handling multiple connections fails #21

@nospam2k

Description

@nospam2k

I have a setup using Node Red. How do I handle multiple requests? This code seems to work ok for 2 requests but has problems with more than 2.

if(msg.topic.indexOf('zigbee_light') !== -1)
{
    var lightify = global.get('lightify');
    var connection = new lightify.lightify('192.168.0.41');
    connection.connect().then(function()
    {
        return connection.discover();
    })
    .then(function(data)
    {
        var devices = data.result;
        
        if(msg.topic.endsWith('/set'))
        {
            var device_no = (msg.topic.substring(
                    msg.topic.indexOf('zigbee_light') + 12,
                    msg.topic.indexOf('zigbee_light') + 13));
        
            var state = parseInt(msg.payload, 10);
            
            devices.forEach(
                device =>
                {
                    var check = 'A19 W 60 0' + device_no;
                    if(device.name === check)
                    {
                        new Promise(function(resolve, reject) {
                            resolve(connection.nodeSoftOnOff(device.mac,
                            state, 0))
                            reject(function()
                            {
                                node.warn('Rejected');
                                connection.dispose();
                                flow.set('connection', undefined);
                            });
                        }).then(function(data)
                        {
                            return connection.nodeOnOff(device.mac, state,
                            0);
                        })
                        .then(function(data)
                        {
                            connection.dispose();
                            flow.set('connection', undefined);
                        })
                        .catch(function(error)
                        {
                        	node.warn(error);
                        	flow.set('connection', undefined)
                        });
                    }
                }
            );
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions