From e2695df9659afe9fd8285b6159a2d22fee73c0b6 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 5 Oct 2023 11:31:40 +0200 Subject: [PATCH] the same fix for http, mqtt and amqp bindings --- lib/bindings/AMQPBinding.js | 13 +++++++------ lib/bindings/HTTPBinding.js | 6 ++++-- lib/bindings/MQTTBinding.js | 8 ++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/bindings/AMQPBinding.js b/lib/bindings/AMQPBinding.js index fd868a651..0778e1f92 100644 --- a/lib/bindings/AMQPBinding.js +++ b/lib/bindings/AMQPBinding.js @@ -23,12 +23,14 @@ /* eslint-disable consistent-return */ +const iotAgentLib = require('iotagent-node-lib'); const config = require('../configService'); const constants = require('../constants'); const amqp = require('amqplib/callback_api'); const commons = require('./../commonBindings'); +const fillService = iotAgentLib.fillService; const async = require('async'); -const context = { +let context = { op: 'IOTAJSON.AMQP.Binding' }; let amqpConn; @@ -70,6 +72,7 @@ function queueListener(msg) { function start(callback) { let exchange; let queue; + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); const amqpConfig = config.getConfig().amqp; if (!amqpConfig) { return config.getLogger().error(context, 'Error AMPQ is not configured'); @@ -92,9 +95,7 @@ function start(callback) { let durable; - if ( - amqpConfig.options && amqpConfig.options.durable - ) { + if (amqpConfig.options && amqpConfig.options.durable) { durable = amqpConfig.options.durable; } else { durable = constants.AMQP_DEFAULT_DURABLE; @@ -115,7 +116,7 @@ function start(callback) { } let uri = 'amqp://'; - + if (amqpConfig.username && amqpConfig.password) { uri += amqpConfig.username + ':' + amqpConfig.password + '@'; } @@ -125,7 +126,7 @@ function start(callback) { uri += ':' + amqpConfig.port; } } - + let isConnecting = false; let numRetried = 0; diff --git a/lib/bindings/HTTPBinding.js b/lib/bindings/HTTPBinding.js index cc41a69d0..7e95ce99f 100644 --- a/lib/bindings/HTTPBinding.js +++ b/lib/bindings/HTTPBinding.js @@ -27,6 +27,7 @@ const fs = require('fs'); const iotAgentLib = require('iotagent-node-lib'); const regenerateTransid = iotAgentLib.regenerateTransid; const finishSouthBoundTransaction = iotAgentLib.finishSouthBoundTransaction; +const fillService = iotAgentLib.fillService; const intoTrans = iotAgentLib.intoTrans; const _ = require('underscore'); const commandHandler = require('../commandHandler'); @@ -41,7 +42,7 @@ const https = require('https'); const commonBindings = require('../commonBindings'); const bodyParser = require('body-parser'); const constants = require('../constants'); -const context = { +let context = { op: 'IOTAJSON.HTTP.Binding' }; const config = require('../configService'); @@ -252,7 +253,7 @@ function addTimestamp(req, res, next) { function handleIncomingMeasure(req, res, next) { let values; - + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); config .getLogger() .debug(context, 'Processing multiple HTTP measures for device [%s] with apiKey [%s]', req.deviceId, req.apiKey); @@ -498,6 +499,7 @@ function deviceUpdatingHandler(device, callback) { */ function returnCommands(req, res, next) { function updateCommandStatus(device, commandList) { + context = fillService(context, device); function createCommandUpdate(command) { return apply( iotAgentLib.setCommandResult, diff --git a/lib/bindings/MQTTBinding.js b/lib/bindings/MQTTBinding.js index 4ed4a3fba..87eb17526 100644 --- a/lib/bindings/MQTTBinding.js +++ b/lib/bindings/MQTTBinding.js @@ -28,10 +28,11 @@ const fs = require('fs'); const iotAgentLib = require('iotagent-node-lib'); const mqtt = require('mqtt'); const commonBindings = require('../commonBindings'); +const fillService = iotAgentLib.fillService; const async = require('async'); const iotaUtils = require('../iotaUtils'); const constants = require('../constants'); -const context = { +let context = { op: 'IOTAJSON.MQTT.Binding' }; let mqttClient; @@ -132,6 +133,7 @@ function generateTopics(callback) { * Recreate the MQTT subscriptions for all the registered devices. */ function recreateSubscriptions(callback) { + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); config.getLogger().debug(context, 'Recreating subscriptions for all devices'); function subscribeToTopics(topics, callback) { @@ -166,6 +168,7 @@ function recreateSubscriptions(callback) { function sendConfigurationToDevice(apiKey, deviceId, results, callback) { const configurations = iotaUtils.createConfigurationNotification(results); const options = {}; + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); if (config.getConfig().mqtt.qos) { options.qos = parseInt(config.getConfig().mqtt.qos) || 0; } @@ -228,6 +231,7 @@ function unsubscribeAll(callback) { * Start the binding. */ function start(callback) { + context = fillService(context, { service: 'n/a', subservice: 'n/a' }); const mqttConfig = config.getConfig().mqtt; if (!mqttConfig) { return config.getLogger().error(context, 'Error MQTT is not configured'); @@ -371,7 +375,7 @@ function stop(callback) { function executeCommand(apiKey, device, cmdName, serializedPayload, contentType, callback) { const options = {}; // retrieve command mqtt options from device - var commands = Object.assign({}, ...device.commands.map((c) => ({ [c.name]: c }))); + const commands = Object.assign({}, ...device.commands.map((c) => ({ [c.name]: c }))); options.qos = commands[cmdName].mqtt && commands[cmdName].mqtt.qos