Skip to content

Commit

Permalink
Merge pull request #644 from telefonicaid/task/remove_local_mergeDevi…
Browse files Browse the repository at this point in the history
…ceWithConfiguration

remove mergeDeviceWithConfiguration
  • Loading branch information
fgalan authored Oct 16, 2023
2 parents 077d111 + 35d5f7f commit 12cc063
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 78 deletions.
2 changes: 1 addition & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -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)

82 changes: 5 additions & 77 deletions lib/iotaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
);
Expand Down

0 comments on commit 12cc063

Please sign in to comment.