diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 6f3e6de7..675777ab 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ +- Remove: local mergeDeviceWithConfiguration redefined from iota-node-lib (#642) - Fix: ensure service and subservice from device in logs about error proccesing message -- Fix: use but not store timestamp and entityNameExp from group with autoprovisioned devices (iotagent-node-lib#1504, partially) - Fix: remove autocast (iotagent-node-lib#1498) diff --git a/lib/iotaUtils.js b/lib/iotaUtils.js index 4bdb3b50..66f20bd5 100644 --- a/lib/iotaUtils.js +++ b/lib/iotaUtils.js @@ -24,7 +24,6 @@ const iotAgentLib = require('iotagent-node-lib'); const errors = require('./errors'); const dateFormat = require('dateformat'); -const _ = require('underscore'); const context = { op: 'IOTAUL.IoTUtils' }; @@ -133,81 +132,6 @@ function findOrCreate(deviceId, transport, apikey, group, callback) { }); } -function mergeArrays(original, newArray) { - const originalKeys = _.pluck(original, 'object_id'); - const newKeys = _.pluck(newArray, 'object_id'); - const addedKeys = _.difference(newKeys, originalKeys); - const differenceArray = newArray.filter(function (item) { - return addedKeys.indexOf(item.object_id) >= 0; - }); - - return original.concat(differenceArray); -} - -/** - * If the object_id or the name of the attribute is missing, complete it with the other piece of data. - * - * @param {Object} attribute Device attribute - * @return {*} Completed attribute - */ -function setDefaultAttributeIds(attribute) { - if (!attribute.object_id && attribute.name) { - attribute.object_id = attribute.name; - } - - if (!attribute.name && attribute.object_id) { - attribute.name = attribute.object_id; - } - - return attribute; -} - -/** - * Complete the information of the device with the information in the configuration group (with precedence of the - * device). - * - * @param {Object} deviceData Device data. - * @param {Object} configuration Configuration data. - */ -function mergeDeviceWithConfiguration(deviceData, configuration, callback) { - const fields = ['lazy', 'internalAttributes', 'active', 'staticAttributes', 'commands', 'subscriptions']; - const defaults = [null, null, [], [], [], [], []]; - config.getLogger().debug(context, 'deviceData before merge with conf: %j', deviceData); - for (let i = 0; i < fields.length; i++) { - const confField = fields[i] === 'active' ? 'attributes' : fields[i]; - - if (deviceData[fields[i]] && configuration && configuration[confField]) { - deviceData[fields[i]] = mergeArrays(deviceData[fields[i]], configuration[confField]); - } else if (!deviceData[fields[i]] && configuration && configuration[confField]) { - deviceData[fields[i]] = configuration[confField]; - } else if (!deviceData[fields[i]] && (!configuration || !configuration[confField])) { - deviceData[fields[i]] = defaults[i]; - } - - if (deviceData[fields[i]] && ['active', 'lazy', 'commands'].indexOf(fields[i]) >= 0) { - deviceData[fields[i]] = deviceData[fields[i]].map(setDefaultAttributeIds); - } - } - - if (configuration && configuration.cbHost) { - deviceData.cbHost = configuration.cbHost; - } - if (configuration && configuration.ngsiVersion) { - deviceData.ngsiVersion = configuration.ngsiVersion; - } - if (configuration && configuration.explicitAttrs !== undefined && deviceData.explicitAttrs === undefined) { - deviceData.explicitAttrs = configuration.explicitAttrs; - } - if (configuration && configuration.entityNameExp !== undefined) { - deviceData.entityNameExp = configuration.entityNameExp; - } - if (configuration && configuration.timestamp !== undefined && deviceData.timestamp === undefined) { - deviceData.timestamp = configuration.timestamp; - } - config.getLogger().debug(context, 'deviceData after merge with conf: %j', deviceData); - callback(null, deviceData); -} - /** * Retrieve a device from the device repository based on the given APIKey and DeviceID, creating one if none is * found for the given data. @@ -240,7 +164,11 @@ function retrieveDevice(deviceId, apiKey, transport, callback) { [ apply(iotAgentLib.getConfigurationSilently, config.getConfig().iota.defaultResource, apiKey), apply(findOrCreate, deviceId, transport, apiKey), // group.apikey and apikey are the same - mergeDeviceWithConfiguration + apply( + iotAgentLib.mergeDeviceWithConfiguration, + ['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'], + [null, null, [], [], [], [], []] + ) ], callback );