Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove mergeDeviceWithConfiguration #644

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is underscore used elsewhere? In negative case, we can remove it from packages.json also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 09f8199

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
Loading