Skip to content

Commit

Permalink
Merge pull request #403 from telefonicaid/task/prepareBugfix2.1.1
Browse files Browse the repository at this point in the history
Task/prepare bugfix2.1.1
  • Loading branch information
Alvaro Vega authored Jun 24, 2016
2 parents 7ab023e + a1590dc commit c7b8cb2
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 188 deletions.
25 changes: 21 additions & 4 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,32 @@ function setDefaultAttributeIds(attribute) {
return attribute;
}

/**
* Merge array of attributes coming from the device with another one coming from a configuration. The latter will
* complete the information in the former if the attribute in the configuration:
* - does not have a conflicting object_id with any attribute of the device.
* - does not have a conflicting name with any other attribute in the device
*
* @param {Array} original List of attributes of the device.
* @param {Array} newArray List of attributes of the configuration.
* @return {Array} Merge of the attributes of the device and those of the configuration.
*/
function mergeArrays(original, newArray) {
var originalKeys = _.pluck(original, 'name'),
newKeys = _.pluck(newArray, 'name'),
/* jshint camelcase: false */
var originalKeys = _.pluck(original, 'object_id'),
newKeys = _.pluck(newArray, 'object_id'),
addedKeys = _.difference(newKeys, originalKeys),
differenceArray = newArray.filter(function(item) {
return addedKeys.indexOf(item.name) >= 0;
return addedKeys.indexOf(item.object_id) >= 0;
}),
originalNames = _.pluck(original, 'name'),
newNames = _.pluck(newArray, 'name'),
addedNames = _.difference(newNames, originalNames),
differenceNamesArray = newArray.filter(function(item) {
return addedNames.indexOf(item.name) >= 0 && (!item.object_id || newKeys.indexOf(item.object_id) < 0);
});

return original.concat(differenceArray);
return original.concat(differenceArray).concat(differenceNamesArray);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions lib/services/devices/registrationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
var request = require('request'),
errors = require('../../errors'),
logger = require('logops'),
_ = require('underscore'),
intoTrans = require('../common/domain').intoTrans,
config = require('../../commonConfig'),
ngsiParser = require('./../ngsi/ngsiParser'),
Expand Down Expand Up @@ -126,14 +127,24 @@ function sendRegistrations(unregister, deviceData, callback) {
return attributeList;
}

function mergeWithSameName(old, current) {
var keys = _.pluck(old, 'name');

if (keys.indexOf(current.name) < 0) {
old.push(current);
}

return old;
}

if (deviceData.registrationId) {
options.json.registrationId = deviceData.registrationId;
}

options.json.contextRegistrations[0].attributes = options.json.contextRegistrations[0].attributes.concat(
formatAttributes(deviceData.lazy),
formatAttributes(deviceData.commands)
);
formatAttributes(deviceData.lazy),
formatAttributes(deviceData.commands)
).reduce(mergeWithSameName, []);

if (options.json.contextRegistrations[0].attributes.length === 0) {
logger.debug('No Context Provider registrations found for unregister');
Expand Down
Loading

0 comments on commit c7b8cb2

Please sign in to comment.