Skip to content

Commit

Permalink
Merge 679545a into 8bdba96
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Dec 13, 2023
2 parents 8bdba96 + 679545a commit 8876375
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/bindings/AMQPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let amqpChannel;
* @param {Object} device Data object for the device receiving the command.
* @param {String} serializedPayload String payload in JSON format for the command.
*/
function executeCommand(apiKey, device, cmdName, serializedPayload, contentType, callback) {
function executeCommand(apiKey, group, device, cmdName, serializedPayload, contentType, callback) {
config
.getLogger()
.debug(
Expand Down Expand Up @@ -258,7 +258,7 @@ function deviceUpdatingHandler(device, callback) {
* @param {String} deviceId ID of the Device.
* @param {Object} results Context Broker response.
*/
function sendConfigurationToDevice(apiKey, deviceId, results, callback) {
function sendConfigurationToDevice(apiKey, group, deviceId, results, callback) {
callback();
}

Expand Down
26 changes: 15 additions & 11 deletions lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ function parseDataMultipleMeasure(req, res, next) {
}
}

function executeCommand(apiKey, device, cmdName, serializedPayload, contentType, callback) {
function executeCommand(apiKey, group, device, cmdName, serializedPayload, contentType, callback) {
const options = {
url: device.endpoint,
url: device.endpoint || group.endpoint,
method: 'POST',
body: serializedPayload,
headers: {
Expand All @@ -213,8 +213,8 @@ function executeCommand(apiKey, device, cmdName, serializedPayload, contentType,
'content-type': contentType
}
};
if (device.endpoint) {
// device.endpoint or another field like device.endpointExp ?
if (options.endpoint) {
// endpoint could be an expression
const parser = iotAgentLib.dataPlugins.expressionTransformation;
let attrList = iotAgentLib.dataPlugins.utils.getIdTypeServSubServiceFromDevice(device);
attrList = device.staticAttributes ? attrList.concat(device.staticAttributes) : attrList.concat([]);
Expand All @@ -223,11 +223,11 @@ function executeCommand(apiKey, device, cmdName, serializedPayload, contentType,
// expression result will be the full command payload
let endpointRes = null;
try {
endpointRes = parser.applyExpression(device.endpoint, ctxt, device);
endpointRes = parser.applyExpression(options.endpoint, ctxt, device);
} catch (e) {
// no error should be reported
}
options.url = endpointRes ? endpointRes : device.endpoint;
options.url = endpointRes ? endpointRes : options.endpoint;
}
if (config.getConfig().http.timeout) {
options.timeout = config.getConfig().http.timeout;
Expand Down Expand Up @@ -418,7 +418,7 @@ function isCommand(req, res, next) {
next();
}

function sendConfigurationToDevice(apiKey, deviceId, results, callback) {
function sendConfigurationToDevice(apiKey, group, deviceId, results, callback) {
function handleDeviceResponse(innerCallback) {
return function (error, response, body) {
if (error) {
Expand All @@ -431,9 +431,10 @@ function sendConfigurationToDevice(apiKey, deviceId, results, callback) {
};
}

function sendRequest(device, results, innerCallback) {
function sendRequest(device, group, results, innerCallback) {
const resultRequest = {
url: device.endpoint + constants.HTTP_CONFIGURATION_PATH,
url: (device.endpoint || group.endpoint) + constants.HTTP_CONFIGURATION_PATH,

method: 'POST',
json: iotaUtils.createConfigurationNotification(results),
headers: {
Expand All @@ -445,13 +446,16 @@ function sendConfigurationToDevice(apiKey, deviceId, results, callback) {

request(resultRequest, handleDeviceResponse(innerCallback));
}
iotaUtils.retrieveDevice(deviceId, apiKey, transport, function (error, device) {
iotaUtils.retrieveDevice(deviceId, apiKey, group ? group.transport : undefined || transport, function (
error,
device
) {
if (error) {
callback(error);
} else if (!device.endpoint) {
callback(new errors.EndpointNotFound(device.id));
} else {
sendRequest(device, results, callback);
sendRequest(device, group, results, callback);
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions lib/bindings/MQTTBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function recreateSubscriptions(callback) {
* @param {String} deviceId ID of the Device.
* @param {Object} results Context Broker response.
*/
function sendConfigurationToDevice(apiKey, deviceId, results, callback) {
function sendConfigurationToDevice(apiKey, group, deviceId, results, callback) {
const configurations = iotaUtils.createConfigurationNotification(results);
const options = {};
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
Expand Down Expand Up @@ -369,10 +369,11 @@ function stop(callback) {
* JSON payload (already containing the command information).
*
* @param {String} apiKey APIKey of the device that will be receiving the command.
* @param {Object} group Data object for the group receiving the command.
* @param {Object} device Data object for the device receiving the command.
* @param {String} serializedPayload String payload in JSON format for the command.
*/
function executeCommand(apiKey, device, cmdName, serializedPayload, contentType, callback) {
function executeCommand(apiKey, group, device, cmdName, serializedPayload, contentType, callback) {
const options = {};
// retrieve command mqtt options from device
const commands = Object.assign({}, ...device.commands.map((c) => ({ [c.name]: c })));
Expand Down
13 changes: 11 additions & 2 deletions lib/commandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ function serializedPayloadCommand(payload, command) {
function generateCommandExecution(apiKey, device, attribute) {
let payload = {};
let command = device && device.commands.find((att) => att.name === attribute.name);
let group;
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function (
error,
foundGroup
) {
if (!error) {
group = foundGroup;
}
});
if (command && command.expression) {
let parser = iotAgentLib.dataPlugins.expressionTransformation;
// The context for the JEXL expression should be the ID, TYPE, S, SS
Expand Down Expand Up @@ -111,9 +120,9 @@ function generateCommandExecution(apiKey, device, attribute) {
);

const executions = transportSelector.createExecutionsForBinding(
[apiKey, device, attribute.name, serialized, contentType],
[apiKey, group, device, attribute.name, serialized, contentType],
'executeCommand',
device.transport || config.getConfig().defaultTransport
device.transport || group.transport || config.getConfig().defaultTransport
);

return executions;
Expand Down
16 changes: 13 additions & 3 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,21 @@ function extractAttributes(device, current, payloadType) {
return values;
}

function sendConfigurationToDevice(device, apiKey, deviceId, results, callback) {
function sendConfigurationToDevice(device, apiKey, group, deviceId, results, callback) {
//let group;
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function (
error,
foundGroup
) {
if (!error) {
group = foundGroup;
}
});

transportSelector.applyFunctionFromBinding(
[apiKey, deviceId, results],
[apiKey, group, deviceId, results],
'sendConfigurationToDevice',
device.transport || config.getConfig().defaultTransport,
device.transport || group.transport || config.getConfig().defaultTransport,
callback
);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/iotaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function manageConfiguration(apiKey, deviceId, device, objMessage, sendFunction,
async.waterfall(
[
apply(iotAgentLib.query, device.name, device.type, '', objMessage.fields, device),
apply(sendFunction, apiKey, deviceId)
apply(sendFunction, apiKey, {}, deviceId)
],
handleSendConfigurationError
);
Expand Down Expand Up @@ -171,10 +171,14 @@ function findOrCreate(deviceId, transport, apikey, group, callback) {
) {
newDevice.protocol = config.getConfig().iota.iotManager.protocol;
}
// Fix transport depending on binding
if (!newDevice.transport) {
newDevice.transport = transport;
}
// // Fix transport depending on binding and group
// if (!newDevice.transport) {
// if ('transport' in group && group.transport !== undefined) {
// newDevice.transport = group.transport;
// } else {
// newDevice.transport = transport;
// }
// }
if ('ngsiVersion' in group && group.ngsiVersion !== undefined) {
newDevice.ngsiVersion = group.ngsiVersion;
}
Expand Down
13 changes: 11 additions & 2 deletions lib/iotagent-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ const config = require('./configService');
*/
function configurationNotificationHandler(device, updates, callback) {
function invokeConfiguration(apiKey, callback) {
let group;
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function (
error,
foundGroup
) {
if (!error) {
group = foundGroup;
}
});
transportSelector.applyFunctionFromBinding(
[apiKey, device.id, updates],
[apiKey, group, device.id, updates],
'sendConfigurationToDevice',
device.transport || config.getConfig().defaultTransport,
device.transport || group.transport || config.getConfig().defaultTransport,
callback
);
}
Expand Down

0 comments on commit 8876375

Please sign in to comment.