Skip to content

Commit

Permalink
send to CB using multimeasures
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed May 21, 2024
1 parent 932bc41 commit 494aed9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/bindings/HTTPBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function returnCommands(req, res, next) {
}

function handleIncomingMeasure(req, res, next) {
let updates = [];
let update = [];
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
// prettier-ignore
config.getLogger().debug(context, 'Processing multiple HTTP measures for device %s with apiKey %j',
Expand All @@ -223,10 +223,10 @@ function handleIncomingMeasure(req, res, next) {
function processHTTPWithDevice(device) {
context = fillService(context, device);
if (req.ulPayload) {
updates = req.ulPayload.reduce(commonBindings.processMeasureGroup.bind(null, device, req.apiKey), []);
update = [req.ulPayload].reduce(commonBindings.processMeasureGroup.bind(null, device, req.apiKey), []);
}

async.series(updates, function (error) {
async.series(update, function (error) {
if (error) {
next(error);
// prettier-ignore
Expand Down
36 changes: 20 additions & 16 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,36 @@ function manageConfigurationRequest(apiKey, deviceId, device, objMessage) {

/* eslint-disable-next-line no-unused-vars */
function processMeasureGroup(device, apikey, previous, current, index) {
const values = [];
if (current.command) {
if (current[0] && current[0].command) {
previous.push(
iotAgentLib.setCommandResult.bind(
null,
device.name,
config.getConfig().iota.defaultResource,
apikey,
current.command,
current.value,
current[0].command,
current[0].value,
constants.COMMAND_STATUS_COMPLETED,
device
)
);
} else {
for (const k in current) {
if (current.hasOwnProperty(k)) {
values.push({
name: k,
type: guessType(k, device),
value: current[k]
});
const val = [];
for (let curr of current) {
const values = [];
for (const k in curr) {
if (curr.hasOwnProperty(k)) {
values.push({
name: k,
type: guessType(k, device),
value: curr[k]
});
}
}
val.push(values);
}

previous.push(iotAgentLib.update.bind(null, device.name, device.type, '', values, device));
config.getLogger().debug(context, 'VAL %j', val);
previous.push(iotAgentLib.update.bind(null, device.name, device.type, '', val, device));
}

return previous;
Expand All @@ -168,7 +172,7 @@ function processMeasureGroup(device, apikey, previous, current, index) {
* @param {String} messageStr UL payload parsed to string.
*/
function multipleMeasures(apiKey, device, messageStr) {
let updates = [];
let update = [];
let parsedMessage;
context = fillService(context, device);
config.getLogger().debug(context, 'Processing multiple measures for device %s with apiKey %s', device.id, apiKey);
Expand All @@ -181,9 +185,9 @@ function multipleMeasures(apiKey, device, messageStr) {
return;
}
config.getLogger().debug(context, 'stringMessage: %s parsedMessage: %s', messageStr, parsedMessage);
updates = parsedMessage.reduce(processMeasureGroup.bind(null, device, apiKey), []);
update = [parsedMessage].reduce(processMeasureGroup.bind(null, device, apiKey), []);

async.series(updates, function (error) {
async.series(update, function (error) {
if (error) {
config.getLogger().error(
context,
Expand Down

0 comments on commit 494aed9

Please sign in to comment.