Skip to content

Commit

Permalink
Merge pull request #834 from telefonicaid/task/update_handler_with_ol…
Browse files Browse the repository at this point in the history
…d_device

uses previous device in device update handler
  • Loading branch information
fgalan committed Jun 5, 2024
2 parents aaab9ff + e040dfe commit 9aad482
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Fix: update device using previous device apikey to avoid error when apikey is updated (#833)
- Fix: allow send multiple measures to CB in a batch (POST /v2/op/update) and sorted by TimeInstant when possible, instead of using multiples single request (#825, iotagent-node-lib#1612)
- Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#827)
2 changes: 1 addition & 1 deletion lib/bindings/AMQPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function deviceProvisioningHandler(device, callback) {
*
* @param {Object} device Device object containing all the information about the provisioned device.
*/
function deviceUpdatingHandler(device, callback) {
function deviceUpdatingHandler(device, oldDevice, callback) {
callback(null, device);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/ARGOBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function deviceProvisioningHandler(device, callback) {
*
* @param {Object} device Device object containing all the information about the updated device.
*/
function deviceUpdatingHandler(device, callback) {
function deviceUpdatingHandler(device, oldDevice, callback) {
return callback(null, device);
}

Expand Down
10 changes: 6 additions & 4 deletions lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,18 +539,20 @@ function deviceProvisioningHandler(device, callback) {
*
* @param {Object} device Device object containing all the information about the updated device.
*/
function deviceUpdatingHandler(device, callback) {
config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler device %j', device);
function deviceUpdatingHandler(newDevice, oldDevice, callback) {
config
.getLogger()
.debug(context, 'httpbinding.deviceUpdatingHandler newDevice %j oldDevice %j', newDevice, oldDevice);
let group = {};
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', device.apikey, function (
iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', oldDevice.apikey, function (
error,
foundGroup
) {
if (!error) {
group = foundGroup;
}
config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler group %j', group);
setPollingAndDefaultTransport(device, group, callback);
setPollingAndDefaultTransport(newDevice, group, callback);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/MQTTBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function deviceProvisioningHandler(device, callback) {
*
* @param {Object} device Device object containing all the information about the provisioned device.
*/
function deviceUpdatingHandler(device, callback) {
function deviceUpdatingHandler(device, oldDevice, callback) {
callback(null, device);
}

Expand Down
11 changes: 8 additions & 3 deletions lib/iotagent-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function updateHandler(id, type, attributes, service, subservice, callback) {
* @param {Object} device Device provisioning information.
*/
function deviceProvisioningHandler(device, callback) {
config.getLogger().debug(context, 'deviceProvisioningHandler for device %j', device);
transportSelector.applyFunctionFromBinding([device], 'deviceProvisioningHandler', null, function (error, devices) {
if (error) {
callback(error);
Expand All @@ -111,13 +112,17 @@ function deviceProvisioningHandler(device, callback) {
}

/**
* Calls all the device updating handlers for each transport protocol binding whenever a new device is updated
* Calls all the device updating handlers for each transport protocol binding whenever a device is updated
* in the Agent.
*
* @param {Object} device Device updating information.
*/
function deviceUpdatingHandler(device, callback) {
transportSelector.applyFunctionFromBinding([device], 'deviceUpdatingHandler', null, function (error, devices) {
function deviceUpdatingHandler(newDevice, oldDevice, callback) {
config.getLogger().debug(context, 'deviceUpdatingHandler for newDevice %j oldDevice %j', newDevice, oldDevice);
transportSelector.applyFunctionFromBinding([newDevice, oldDevice], 'deviceUpdatingHandler', null, function (
error,
devices
) {
if (error) {
callback(error);
} else {
Expand Down
122 changes: 122 additions & 0 deletions test/unit/ngsiv2/HTTP_update_device_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent-json
*
* iotagent-json is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent-json is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with iotagent-json.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*
* Modified by: Daniel Calvo - ATOS Research & Innovation
*/

/* eslint-disable no-unused-vars */

const iotagentMqtt = require('../../../');
const config = require('./config-test.js');
const nock = require('nock');
const should = require('should');
const iotAgentLib = require('iotagent-node-lib');
const async = require('async');
const request = require('request');
const utils = require('../../utils');
let mockedClientServer;
let contextBrokerMock;

describe('HTTP binding - Update provisioned devices with a new apikey', function () {
const provisionOptions = {
url: 'http://localhost:' + config.iota.server.port + '/iot/devices',
method: 'POST',
json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json'),
headers: {
'fiware-service': 'smartgondor',
'fiware-servicepath': '/gardens'
}
};

beforeEach(function (done) {
config.logLevel = 'FATAL';
nock.cleanAll();

iotagentMqtt.start(config, function () {
request(provisionOptions, function (error, response, body) {
done();
});
});
});

it('should have provisioned with APIKEY1', function (done) {
const options = {
url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4',
headers: {
'fiware-service': 'smartgondor',
'fiware-servicepath': '/gardens'
},
method: 'GET'
};
request(options, function (error, response, body) {
/* jshint camelcase:false */
const parsedBody = JSON.parse(body);
parsedBody.apikey.should.equal('APIKEY1');
done();
});
});

afterEach(function (done) {
nock.cleanAll();
async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done);
});

describe('When a request to update a provision device arrives', function () {
const optionsUpdate = {
url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4',
method: 'PUT',
headers: {
'fiware-service': 'smartgondor',
'fiware-servicepath': '/gardens'
},
json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json')
};

it('should return a 200 OK and no errors', function (done) {
request(optionsUpdate, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(204);
done();
});
});

it('should have updated device apikey', function (done) {
request(optionsUpdate, function (error, response, body) {
const options = {
url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4',
headers: {
'fiware-service': 'smartgondor',
'fiware-servicepath': '/gardens'
},
method: 'GET'
};

request(options, function (error, response, body) {
/* jshint camelcase:false */
const parsedBody = JSON.parse(body);
parsedBody.apikey.should.equal('APIKEY2');
done();
});
});
});
});
});
47 changes: 47 additions & 0 deletions test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"devices": [
{
"device_id": "MQTT_4",
"protocol": "GENERIC_PROTO",
"apikey": "APIKEY1",
"entity_name": "Second MQTT Device",
"timezone": "America/Santiago",
"entity_type": "AnMQTTDevice",
"attributes": [
{
"name": "temperature",
"type": "celsius"
},
{
"name": "humidity",
"type": "degrees"
},
{
"name": "luminosity",
"type": "Integer"

},
{
"name": "pollution",
"type": "Float"
},
{
"name": "configuration",
"type": "Object"
},
{
"name": "tags",
"type": "Array"
},
{
"name": "enabled",
"type": "Boolean"
},
{
"name": "alive",
"type": "Null"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"apikey": "APIKEY2"
}

0 comments on commit 9aad482

Please sign in to comment.