Skip to content

Commit f11c9c9

Browse files
authored
Merge pull request #1153 from telefonicaid/task/add_updating_device_handler
Task/add updating device handler
2 parents 082c8bf + dd7fe13 commit f11c9c9

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
- ADD: allow update polling devide field (iota-json#602)
2+
- ADD: support for add and update device handler (iota-json#602)
13
- ADD: support both WARN and WARNING log levels (#1146)

lib/fiware-iotagent-lib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ exports.setDataQueryHandler = contextServer.setQueryHandler;
325325
exports.setConfigurationHandler = contextServer.setConfigurationHandler;
326326
exports.setRemoveConfigurationHandler = contextServer.setRemoveConfigurationHandler;
327327
exports.setProvisioningHandler = contextServer.setProvisioningHandler;
328+
exports.setUpdatingHandler = contextServer.setUpdatingHandler;
328329
exports.setRemoveDeviceHandler = contextServer.setRemoveDeviceHandler;
329330
exports.setNotificationHandler = contextServer.setNotificationHandler;
330331
exports.addUpdateMiddleware = ngsi.addUpdateMiddleware;

lib/services/devices/deviceRegistryMongoDB.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ function update(device, callback) {
289289
data.internalAttributes = device.internalAttributes;
290290
data.commands = device.commands;
291291
data.endpoint = device.endpoint;
292+
data.polling = device.polling;
292293
data.name = device.name;
293294
data.type = device.type;
294295
data.apikey = device.apikey;

lib/services/northBound/deviceProvisioningServer.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const context = {
3434
};
3535
const apply = async.apply;
3636
let provisioningHandler;
37+
let updatingHandler;
3738
let removeDeviceHandler;
3839
let updateDeviceTemplate;
3940
let createDeviceTemplate;
@@ -53,6 +54,7 @@ const provisioningAPITranslation = {
5354
protocol: 'protocol',
5455
transport: 'transport',
5556
endpoint: 'endpoint',
57+
polling: 'polling',
5658
attributes: 'active',
5759
commands: 'commands',
5860
lazy: 'lazy',
@@ -204,6 +206,7 @@ function toProvisioningAPIFormat(device) {
204206
timezone: device.timezone,
205207
timestamp: device.timestamp,
206208
endpoint: device.endpoint,
209+
polling: device.polling,
207210
transport: device.transport,
208211
attributes: device.active ? device.active.map(attributeToProvisioningAPIFormat) : undefined,
209212
lazy: device.lazy ? device.lazy.map(attributeToProvisioningAPIFormat) : undefined,
@@ -317,6 +320,14 @@ function handleRemoveDevice(req, res, next) {
317320
* This middleware handles updates in the provisioning devices. The only attribute
318321
*/
319322
function handleUpdateDevice(req, res, next) {
323+
function applyUpdatingHandler(device, callback) {
324+
if (updatingHandler) {
325+
updatingHandler(device, callback);
326+
} else {
327+
callback(null, device);
328+
}
329+
}
330+
320331
if (req.body.device_id) {
321332
next(new errors.BadRequest("Can't change the ID of a preprovisioned device"));
322333
} else {
@@ -338,12 +349,19 @@ function handleUpdateDevice(req, res, next) {
338349
if (req.body.entity_name || req.body.entity_type) {
339350
isTypeOrNameUpdated = true;
340351
}
341-
deviceService.updateRegister(newDevice, isTypeOrNameUpdated, function handleDeviceUpdate(error) {
342-
if (error) {
343-
next(error);
344-
} else {
345-
res.status(204).json({});
346-
}
352+
async.waterfall([apply(applyUpdatingHandler, newDevice)], function handleUpdating(
353+
error,
354+
newDeviceUpdated
355+
) {
356+
deviceService.updateRegister(newDeviceUpdated, isTypeOrNameUpdated, function handleDeviceUpdate(
357+
error
358+
) {
359+
if (error) {
360+
next(error);
361+
} else {
362+
res.status(204).json({});
363+
}
364+
});
347365
});
348366
} else {
349367
next(new errors.DeviceNotFound(req.params.deviceId));
@@ -392,6 +410,10 @@ function setProvisioningHandler(newHandler) {
392410
provisioningHandler = newHandler;
393411
}
394412

413+
function setUpdatingHandler(newHandler) {
414+
updatingHandler = newHandler;
415+
}
416+
395417
function setRemoveDeviceHandler(newHandler) {
396418
removeDeviceHandler = newHandler;
397419
}
@@ -409,6 +431,7 @@ function clear(callback) {
409431
exports.setConfiguration = setConfiguration;
410432
exports.loadContextRoutes = intoTrans(context, loadContextRoutes);
411433
exports.setProvisioningHandler = intoTrans(context, setProvisioningHandler);
434+
exports.setUpdatingHandler = intoTrans(context, setUpdatingHandler);
412435
exports.setRemoveDeviceHandler = intoTrans(context, setRemoveDeviceHandler);
413436
exports.addDeviceProvisionMiddleware = addDeviceProvisionMiddleware;
414437
exports.clear = clear;

lib/services/northBound/northboundServer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const domainUtils = require('../common/domain');
3131
const middlewares = require('../common/genericMiddleware');
3232
const intoTrans = domainUtils.intoTrans;
3333
const deviceProvisioning = require('./deviceProvisioningServer');
34+
const deviceUpdating = require('./deviceProvisioningServer');
3435
const groupProvisioning = require('./deviceGroupAdministrationServer');
3536
const logger = require('logops');
3637
const context = {
@@ -116,6 +117,7 @@ exports.setNotificationHandler = intoTrans(context, contextServer.setNotificatio
116117
exports.setConfigurationHandler = intoTrans(context, groupProvisioning.setConfigurationHandler);
117118
exports.setRemoveConfigurationHandler = intoTrans(context, groupProvisioning.setRemoveConfigurationHandler);
118119
exports.setProvisioningHandler = intoTrans(context, deviceProvisioning.setProvisioningHandler);
120+
exports.setUpdatingHandler = intoTrans(context, deviceUpdating.setUpdatingHandler);
119121
exports.setRemoveDeviceHandler = intoTrans(context, deviceProvisioning.setRemoveDeviceHandler);
120122
exports.addDeviceProvisionMiddleware = deviceProvisioning.addDeviceProvisionMiddleware;
121123
exports.addConfigurationProvisionMiddleware = groupProvisioning.addConfigurationProvisionMiddleware;

0 commit comments

Comments
 (0)