From 0e4acd20e6edd8ab45812214f362cb2362c9f221 Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Wed, 21 Jan 2015 17:47:33 +0100 Subject: [PATCH] FIX Use the context broker configured in the generic type --- lib/services/ngsiService.js | 42 ++++++++++++++++++++------------ test/unit/active-devices-test.js | 39 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/lib/services/ngsiService.js b/lib/services/ngsiService.js index 61a88b8ff..1dc3c2f11 100644 --- a/lib/services/ngsiService.js +++ b/lib/services/ngsiService.js @@ -181,8 +181,32 @@ function registerDevice(deviceObj, callback) { * @param {String} token User token to identify against the PEP Proxies (optional). */ function sendUpdateValue(deviceId, deviceType, attributes, token, callback) { - var options = { - url: 'http://' + config.contextBroker.host + ':' + config.contextBroker.port + '/NGSI10/updateContext', + var brokerHost = config.contextBroker.host, + brokerPort = config.contextBroker.port, + options, + headers = { + 'fiware-service': config.service, + 'fiware-servicepath': config.subservice, + 'X-Auth-Token': token + }; + + if (config.types[deviceType]) { + if (config.types[deviceType].service) { + headers['fiware-service'] = config.types[deviceType].service; + } + + if (config.types[deviceType].subservice) { + headers['fiware-servicepath'] = config.types[deviceType].subservice; + } + + if (config.types[deviceType].contextBroker) { + brokerHost = config.types[deviceType].contextBroker.host; + brokerPort = config.types[deviceType].contextBroker.port; + } + } + + options = { + url: 'http://' + brokerHost + ':' + brokerPort + '/NGSI10/updateContext', method: 'POST', json: { contextElements: [ @@ -195,21 +219,9 @@ function sendUpdateValue(deviceId, deviceType, attributes, token, callback) { ], updateAction: 'APPEND' }, - headers: { - 'fiware-service': config.service, - 'fiware-servicepath': config.subservice, - 'X-Auth-Token': token - } + headers: headers }; - if (config.types[deviceType] && config.types[deviceType].service) { - options.headers['fiware-service'] = config.types[deviceType].service; - } - - if (config.types[deviceType] && config.types[deviceType].subservice) { - options.headers['fiware-servicepath'] = config.types[deviceType].subservice; - } - logger.debug(context, 'Updating device value in the Context Broker at [%s]', options.url); logger.debug(context, 'Using the following request:\n\n%s\n\n', JSON.stringify(options, null, 4)); diff --git a/test/unit/active-devices-test.js b/test/unit/active-devices-test.js index 3a82851b0..fcec11e80 100644 --- a/test/unit/active-devices-test.js +++ b/test/unit/active-devices-test.js @@ -62,6 +62,20 @@ var iotAgentLib = require('../../'), ], active: [ ] + }, + 'Humidity': { + contextBroker: { + host: '192.168.1.1', + port: '3024' + }, + commands: [], + lazy: [], + active: [ + { + name: 'humidity', + type: 'percentage' + } + ] } }, service: 'smartGondor', @@ -141,4 +155,29 @@ describe('Active attributes test', function() { }); }); }); + + describe('When the IoT Agent recieves information for a type with a configured Context Broker', function() { + beforeEach(function(done) { + nock.cleanAll(); + + contextBrokerMock = nock('http://192.168.1.1:3024') + .matchHeader('fiware-service', 'smartGondor') + .matchHeader('fiware-servicepath', 'gardens') + .post('/NGSI10/updateContext', + utils.readExampleFile('./test/unit/contextRequests/updateContext2.json')) + .reply(200, + utils.readExampleFile('./test/unit/contextResponses/updateContext1Success.json')); + + iotAgentLib.activate(iotAgentConfig, done); + }); + + it('should use the Context Broker defined by the type', function(done) { + iotAgentLib.update('humSensor', 'Humidity', values, function(error) { + should.not.exist(error); + contextBrokerMock.done(); + done(); + }); + }); + }); + });