function processDeviceList()

in source/services/systems/lib/refresh-system.js [10:96]


function processDeviceList(deviceListSpec, deviceList) {
    const tag = `${lib}(processDeviceList):`;

    function deviceByRef(ref) {
        return deviceList[
            deviceListSpec.findIndex(device => {
                return device.ref === ref;
            })
        ];
    }

    return deviceListSpec.reduce((previousSystemDevice, currentSystemDevice, index, array) => {
        return previousSystemDevice.then(reduceResultChain => {
            console.log(tag, 'CurrentValue:', index, JSON.stringify(currentSystemDevice));
            console.log(tag, 'reduceResultChain:', index, JSON.stringify(reduceResultChain));

            let occurencesOfGetAtt = JSON.stringify(currentSystemDevice).split('!GetAtt[');

            if (occurencesOfGetAtt.length > 1) {
                // Found at least 1 occurence of !GetAtt in our spec.

                console.log(tag, `Found ${occurencesOfGetAtt.length} GetAtts`);

                occurencesOfGetAtt.forEach((occurence, i) => {
                    if (i !== 0) {
                        // Skip first one (which is everything before 1st occurence)

                        const split = occurence.split(']');
                        const queryString = split[0];
                        const attributes = queryString.split('.');

                        const value = attributes.reduce((pv, cv, j) => {
                            if (j === 0) {
                                // This is the reference to the device
                                return deviceByRef(cv);
                            } else {
                                if (pv) {
                                    return pv[cv]
                                } else {
                                    return null;
                                }
                            }
                        }, '');

                        console.log(tag, `!GetAtt[${queryString}]: ${value}`);
                        split.shift();
                        occurencesOfGetAtt[i] = '' + value + split.join(']');
                        console.log(tag, `GetAtt: occurencesOfGetAtt[${i}]:`, occurencesOfGetAtt[i]);
                    }
                });
            }

            currentSystemDevice = JSON.parse(occurencesOfGetAtt.join(''));
            currentSystemDevice.device = deviceList[index];

            console.log(tag, 'Updating device', currentSystemDevice.spec);

            if (currentSystemDevice.device) {
                return documentClient
                    .update({
                        TableName: process.env.TABLE_DEVICES,
                        Key: {
                            thingId: currentSystemDevice.device.thingId
                        },
                        UpdateExpression: 'set #ua = :ua, #spec = :spec',
                        ExpressionAttributeNames: {
                            '#ua': 'updatedAt',
                            '#spec': 'spec'
                        },
                        ExpressionAttributeValues: {
                            ':ua': moment()
                                .utc()
                                .format(),
                            ':spec': currentSystemDevice.spec || {}
                        }
                    })
                    .promise()
                    .then(result => {
                        currentSystemDevice.device.spec = currentSystemDevice.spec;
                        return [...reduceResultChain, currentSystemDevice];
                    });
            } else {
                return [...reduceResultChain, currentSystemDevice];
            }
        });
    }, Promise.resolve([]).then(arrayOfResults => arrayOfResults));
}