Skip to content

Commit c7b8cb2

Browse files
author
Alvaro Vega
authored
Merge pull request #403 from telefonicaid/task/prepareBugfix2.1.1
Task/prepare bugfix2.1.1
2 parents 7ab023e + a1590dc commit c7b8cb2

File tree

4 files changed

+216
-188
lines changed

4 files changed

+216
-188
lines changed

lib/services/devices/deviceService.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,32 @@ function setDefaultAttributeIds(attribute) {
199199
return attribute;
200200
}
201201

202+
/**
203+
* Merge array of attributes coming from the device with another one coming from a configuration. The latter will
204+
* complete the information in the former if the attribute in the configuration:
205+
* - does not have a conflicting object_id with any attribute of the device.
206+
* - does not have a conflicting name with any other attribute in the device
207+
*
208+
* @param {Array} original List of attributes of the device.
209+
* @param {Array} newArray List of attributes of the configuration.
210+
* @return {Array} Merge of the attributes of the device and those of the configuration.
211+
*/
202212
function mergeArrays(original, newArray) {
203-
var originalKeys = _.pluck(original, 'name'),
204-
newKeys = _.pluck(newArray, 'name'),
213+
/* jshint camelcase: false */
214+
var originalKeys = _.pluck(original, 'object_id'),
215+
newKeys = _.pluck(newArray, 'object_id'),
205216
addedKeys = _.difference(newKeys, originalKeys),
206217
differenceArray = newArray.filter(function(item) {
207-
return addedKeys.indexOf(item.name) >= 0;
218+
return addedKeys.indexOf(item.object_id) >= 0;
219+
}),
220+
originalNames = _.pluck(original, 'name'),
221+
newNames = _.pluck(newArray, 'name'),
222+
addedNames = _.difference(newNames, originalNames),
223+
differenceNamesArray = newArray.filter(function(item) {
224+
return addedNames.indexOf(item.name) >= 0 && (!item.object_id || newKeys.indexOf(item.object_id) < 0);
208225
});
209226

210-
return original.concat(differenceArray);
227+
return original.concat(differenceArray).concat(differenceNamesArray);
211228
}
212229

213230
/**

lib/services/devices/registrationUtils.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
var request = require('request'),
2727
errors = require('../../errors'),
2828
logger = require('logops'),
29+
_ = require('underscore'),
2930
intoTrans = require('../common/domain').intoTrans,
3031
config = require('../../commonConfig'),
3132
ngsiParser = require('./../ngsi/ngsiParser'),
@@ -126,14 +127,24 @@ function sendRegistrations(unregister, deviceData, callback) {
126127
return attributeList;
127128
}
128129

130+
function mergeWithSameName(old, current) {
131+
var keys = _.pluck(old, 'name');
132+
133+
if (keys.indexOf(current.name) < 0) {
134+
old.push(current);
135+
}
136+
137+
return old;
138+
}
139+
129140
if (deviceData.registrationId) {
130141
options.json.registrationId = deviceData.registrationId;
131142
}
132143

133144
options.json.contextRegistrations[0].attributes = options.json.contextRegistrations[0].attributes.concat(
134-
formatAttributes(deviceData.lazy),
135-
formatAttributes(deviceData.commands)
136-
);
145+
formatAttributes(deviceData.lazy),
146+
formatAttributes(deviceData.commands)
147+
).reduce(mergeWithSameName, []);
137148

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

0 commit comments

Comments
 (0)