| 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
153
154
155
156
157
158
159
160
161
162 |
1
1
1
57
57
57
171
171
171
1
57
57
1
58
58
58
7
7
1
58
1
1
57
57
57
58
58
58
58
4
4
54
54
58
58
58
58
58
1
1
1
1
1
1
1
1
1
1
1
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 coapRouter = require('./services/coapRouter'),
errors = require('./errors'),
registry,
deviceManagement = require('./services/server/deviceManagement'),
informationReporting = require('./services/server/informationReporting'),
async = require('async'),
logger = require('logops'),
context = {
op: 'LWM2MLib.Server'
},
apply = async.apply;
/**
* Load the internal handlers for each kind of operation. Each handler is implemented in a separated module. This
* module will be, in time, in charge of executing the user handler for that operation with all the data extracted
* from the request (and completed with internal data if needed).
*
* @param {Object} serverInfo Object containing all the information of the current server.
*/
function loadDefaultHandlers(serverInfo, config) {
logger.info(context, 'Loading default handlers');
serverInfo.handlers = {
registration: {
module: require('./services/server/registration'),
user: coapRouter.defaultHandler
},
unregistration: {
module: require('./services/server/unregistration'),
user: coapRouter.defaultHandler
},
updateRegistration: {
module: require('./services/server/updateRegistration'),
user: coapRouter.defaultHandler
}
};
for (var i in serverInfo.handlers) {
Eif (serverInfo.handlers.hasOwnProperty(i)) {
serverInfo.handlers[i].module.init(registry, config);
serverInfo.handlers[i].lib = serverInfo.handlers[i].module.handle;
}
}
}
/**
* Load the tables of available routes. For each route, the method, a regexp for the path and the name of the operation
* is indicated (the name of the operation will be used to select the internal and user handlers to execute for each
* route).
*
* @param {Object} serverInfo Object containing all the information of the current server.
*/
function loadRoutes(serverInfo) {
logger.info(context, 'Loading routes');
serverInfo.routes = [
['POST', /\/rd/, 'registration'],
['DELETE', /\/rd\/.*/, 'unregistration'],
['PUT', /\/rd\/.*/, 'updateRegistration']
];
}
function validateTypes(config, callback) {
var error;
logger.info(context, 'Validating configuration types');
if (config.types) {
for (var i in config.types) {
if (config.types[i].url.match(/^\/rd.*/)) {
error = new errors.IllegalTypeUrl(config.types[i].url);
}
}
}
callback(error);
}
function start(config, startCallback) {
function loadDefaults(serverInfo, callback) {
loadRoutes(serverInfo);
loadDefaultHandlers(serverInfo, config);
callback(null, serverInfo);
}
Eif (config.logLevel) {
logger.setLevel(config.logLevel);
}
logger.info(context, 'Starting Lightweight M2M Server');
if (config.deviceRegistry && config.deviceRegistry.type === 'mongodb') {
logger.info(context, 'Mongo DB Device registry selected for Lightweight M2M Library');
registry = require('./services/server/mongodbDeviceRegistry');
} else {
logger.info(context, 'Memory Device registry selected for Lightweight M2M Library');
registry = require('./services/server/inMemoryDeviceRegistry');
}
deviceManagement.init(registry);
informationReporting.init(registry);
exports.listDevices = registry.list;
exports.getDevice = registry.getByName;
async.waterfall([
apply(validateTypes, config),
apply(registry.init, config),
registry.clean,
apply(coapRouter.start, config),
loadDefaults
], startCallback);
}
function getRegistry() {
return registry;
}
exports.start = start;
exports.setHandler = coapRouter.setHandler;
exports.stop = coapRouter.stop;
exports.read = deviceManagement.read;
exports.write = deviceManagement.write;
exports.execute = deviceManagement.execute;
exports.writeAttributes = deviceManagement.writeAttributes;
exports.discover = deviceManagement.discover;
exports.create = deviceManagement.create;
exports.remove = deviceManagement.remove;
exports.observe = informationReporting.observe;
exports.listObservers = informationReporting.list;
exports.cleanObservers = informationReporting.clean;
exports.cancelObserver = informationReporting.cancel;
exports.buildObserverId = informationReporting.buildId;
exports.parseObserverId = informationReporting.parseId;
exports.getRegistry = getRegistry;
|