diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index b7b6b9b..0f8d6fd 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -6,4 +6,5 @@ - FIX Client handler after connection update (#51) - FIX Remove old device registration on the new registration (#52) - Add Travis CI files and changes to enable it in the package.json (#55). -- Add Check lifetime and unregister devices (#56) \ No newline at end of file +- Add Check lifetime and unregister devices (#56) +- Add Lifetime unregistration handler (#59) \ No newline at end of file diff --git a/lib/lwm2m-server.js b/lib/lwm2m-server.js index ff69265..b779e6e 100644 --- a/lib/lwm2m-server.js +++ b/lib/lwm2m-server.js @@ -35,6 +35,7 @@ var coapRouter = require('./services/coapRouter'), op: 'LWM2MLib.Server' }, apply = async.apply, + config, status = 'STOPPED'; /** @@ -103,13 +104,14 @@ function validateTypes(config, callback) { callback(error); } -function start(config, startCallback) { +function start(serverConfig, startCallback) { function loadDefaults(serverInfo, callback) { loadRoutes(serverInfo); loadDefaultHandlers(serverInfo, config); callback(null, serverInfo); } + config = serverConfig; if (config.logLevel) { logger.setLevel(config.logLevel); } @@ -165,8 +167,26 @@ function isRunning() { return status === 'RUNNING'; } +/** + * Sets the handler callback for a given type of operation. + * + * The signature of the handler will depend on the operation being handled. The complete list of operations and the + * signature of its handlers can be found in the online documentation. + * + * @param {Object} serverInfo Object containing all the information of the current server. + * @param {String} type Name of the operation to be handled. + * @param {Function} handler Operation handler. + */ +function setHandler(serverInfo, type, handler) { + coapRouter.setHandler(serverInfo, type, handler); + + if (type === 'unregistration') { + registry.checkLifetime(config.lifetimeCheckInterval, handler); + } +} + exports.start = start; -exports.setHandler = coapRouter.setHandler; +exports.setHandler = setHandler; exports.stop = stop; exports.read = deviceManagement.read; exports.write = deviceManagement.write; diff --git a/lib/services/server/inMemoryDeviceRegistry.js b/lib/services/server/inMemoryDeviceRegistry.js index 8a46980..16e608f 100644 --- a/lib/services/server/inMemoryDeviceRegistry.js +++ b/lib/services/server/inMemoryDeviceRegistry.js @@ -153,13 +153,24 @@ function list(callback) { } +/** + * Stops checking for device lifetime. + */ +function stopLifetimeCheck() { + clearInterval(checkLifetimeInterval); + checkLifetimeInterval = null; +} + /** * If Lifetime Resource exists, then the registration SHOULD be removed by the Server if a new registration or update * is not received within this lifetime. * - * @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms + * @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms + * @param {Function} unregistrationHandler Unregistration device handler */ -function checkLifetime(lifetimeCheckInterval) { +function checkLifetime(lifetimeCheckInterval, unregistrationHandler) { + stopLifetimeCheck(); + checkLifetimeInterval = setInterval(function(){ list(function(error, deviceList){ if (!error && deviceList) { @@ -176,6 +187,12 @@ function checkLifetime(lifetimeCheckInterval) { logger.debug(context, 'Lifetime unregistration for device [%s] ended successfully', obj.name); + + if (unregistrationHandler) { + unregistrationHandler(obj, function(){ + + }); + } } }); } @@ -185,14 +202,6 @@ function checkLifetime(lifetimeCheckInterval) { }, lifetimeCheckInterval); } -/** - * Stops checking for device lifetime. - */ -function stopLifetimeCheck() { - clearInterval(checkLifetimeInterval); - checkLifetimeInterval = null; -} - /** * Initializes the device registry based on the parameter found in the configuration. For this in memory registry this * function doesn't do anything. diff --git a/lib/services/server/mongodbDeviceRegistry.js b/lib/services/server/mongodbDeviceRegistry.js index ae507f8..1467694 100644 --- a/lib/services/server/mongodbDeviceRegistry.js +++ b/lib/services/server/mongodbDeviceRegistry.js @@ -200,13 +200,25 @@ function list(callback) { query.exec(callback); } + +/** + * Stops checking for device lifetime. + */ +function stopLifetimeCheck() { + clearInterval(checkLifetimeInterval); + checkLifetimeInterval = null; +} + /** * If Lifetime Resource exists, then the registration SHOULD be removed by the Server if a new registration or update * is not received within this lifetime. * - * @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms + * @param {Object} lifetimeCheckInterval Minimum interval between lifetime checks in ms + * @param {Function} unregistrationHandler Unregistration device handler */ -function checkLifetime(lifetimeCheckInterval) { +function checkLifetime(lifetimeCheckInterval, unregistrationHandler) { + stopLifetimeCheck(); + checkLifetimeInterval = setInterval(function(){ list(function(error, deviceList){ if (!error && deviceList) { @@ -223,6 +235,12 @@ function checkLifetime(lifetimeCheckInterval) { logger.debug(context, 'Lifetime unregistration for device [%s] ended successfully', obj.name); + + if (unregistrationHandler) { + unregistrationHandler(obj, function(){ + + }); + } } }); } @@ -232,14 +250,6 @@ function checkLifetime(lifetimeCheckInterval) { }, lifetimeCheckInterval); } -/** - * Stops checking for device lifetime. - */ -function stopLifetimeCheck() { - clearInterval(checkLifetimeInterval); - checkLifetimeInterval = null; -} - /** * Initializes the device registry based on the parameter found in the configuration. The MongoDB config object should * contain at least the host string needed to connect to MongoDB and the database name where to store the device info.