Skip to content

Commit

Permalink
FIX Use the context broker configured in the generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoranj committed Jan 21, 2015
1 parent bfd0016 commit 0e4acd2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/services/ngsiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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));

Expand Down
39 changes: 39 additions & 0 deletions test/unit/active-devices-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
});
});
});

});

0 comments on commit 0e4acd2

Please sign in to comment.