Code coverage report for lib/services/server/inMemoryDeviceRegistry.js

Statements: 95.92% (47 / 49)      Branches: 78.57% (11 / 14)      Functions: 100% (8 / 8)      Lines: 95.92% (47 / 49)     

All files » lib/services/server/ » inMemoryDeviceRegistry.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                              1   1                   1 48 48 48 48                 1 23   23 22   22   1             1 66   66               1 36 36                       1 1 1 1                 1 1   1 2 2       1                 1 3   3 5 2       3 2   1                   1 53     1 1 1 1 1 1 1 1
/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib 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-lwm2m-lib 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-lwm2m-lib.
 * 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]
 */
 
'use strict';
 
var registry = {},
    idCounter = 1,
    errors = require('../../errors'),
    _ = require('underscore');
 
/**
 * Inserts the given object in the registry. The generated ID is returned through the callback.
 *
 * @param {Object} object       Object to insert into the registry.
 */
function register(object, callback) {
    var id = idCounter++;
    registry[id] = object;
    registry[id].id = id;
    callback(null, id);
}
 
/**
 * Removes the object identified by this id from the registry. The removed object is passed as the first callback
 * parameter.
 *
 * @param {Integer} id          Identifier of the object to be removed.
 */
function unregister(id, callback) {
    var obj = registry[id];
 
    if (obj) {
        delete registry[id];
 
        callback(null, obj);
    } else {
        callback(new errors.DeviceNotFound(id));
    }
}
 
/**
 * Remove all the objects from the registry.
 */
function clean(callback) {
    registry = {};
 
    callback();
}
 
/**
 * Retrieves from the registry the object identified by the given id.
 *
 * @param {String} id       Id of the object to be retrieved.
 */
function getObject(id, callback) {
    Eif (registry[id]) {
        callback(null, _.clone(registry[id]));
    } else {
        callback(new errors.DeviceNotFound(id));
    }
}
 
/**
 * Update the object identified with the given id with the object value passed as a parameter.
 *
 * @param {String} id       Id of the object to update.
 * @param {Object} obj      New object value to insert in the registry.
 */
function update(id, obj, callback) {
    Eif (registry[id]) {
        registry[id] = obj;
        callback(null, registry[id]);
    } else {
        callback(new errors.DeviceNotFound(id));
    }
}
 
/**
 * Returns an array of all the devices as the parameter of the callback.
 */
function list(callback) {
    var result = [];
 
    for(var key in registry) {
        Eif (registry.hasOwnProperty(key)) {
            result.push(registry[key]);
        }
    }
 
    callback(null, result);
}
 
/**
 * Gets the device that has the device name passed as a parameter (should be unique) or return a DeviceNotFound error
 * in case none exist.
 *
 * @param {String} deviceName       Name of the device to retrieve.
 */
function getByName(deviceName, callback) {
    var result;
 
    for(var key in registry) {
        if (registry[key] && registry[key].name === deviceName) {
            result = registry[key];
        }
    }
 
    if (result) {
        callback(null, result);
    } else {
        callback(new errors.DeviceNotFound(deviceName));
    }
}
 
/**
 * Initializes the device registry based on the parameter found in the configuration. For this in memory registry this
 * function doesn't do anything.
 *
 * @param {Object} config           Configuration object.
 */
function init(config, callback) {
    callback(null);
}
 
exports.register = register;
exports.unregister = unregister;
exports.get = getObject;
exports.update = update;
exports.clean = clean;
exports.list = list;
exports.getByName = getByName;
exports.init = init;