| 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 |
1
1
1
31
1
1
15
21
21
1
15
15
15
15
15
15
15
15
15
15
1
1
15
15
15
15
1
2
1
5
5
1
1
1
1
1
1
58
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 async = require('async'),
apply = async.apply,
coapUtils = require('./coapUtils'),
registry,
logger = require('logops'),
context = {
op: 'LWM2MLib.InformationReporting'
},
subscriptions = {},
_ = require('underscore');
/**
* Generates an observer ID from its parts. The ID has the following pattern:
*
* <deviceId>:/<objectType>/<objectId>/<resourceId>
*
* @param {Number} deviceId ID of the device, provided by the LWM2M server.
* @param {Number} objectType ID of the object type.
* @param {Number} objectId ID of the object instance.
* @param {Number} resourceId Observed resource of the object.
* @returns {string} The generated ID.
*/
function buildId(deviceId, objectType, objectId, resourceId) {
return deviceId + ':/' + objectType + '/' + objectId + '/' + resourceId;
}
/**
* Parse an observer ID, returning its parts encapsulated in an object.
*
* @param {String} idString Observer ID.
* @returns {{deviceId: string, objectType: string, objectId: string, resourceId: string}}
*/
function parseId(idString) {
var fieldList = idString.substring(idString.indexOf(':') + 2).split('/');
return {
deviceId: idString.substring(0, idString.indexOf(':')),
objectType: fieldList[0],
objectId: fieldList[1],
resourceId: fieldList[2]
};
}
/**
* Constructs an object representing the subscription for the changes in the value of a particular resource of a
* device.
*
* @param {Number} deviceId Internal ID of the device of the subscription (not the endpoint ID).
* @param {Number} objectType Number identifying the object type.
* @param {Number} objectId Number identifying the object instance.
* @param {Number} resourceId Resource of the object to be subscribed to.
* @param {Object} observeStream Stream object for writing into.
* @param {Function} handler Handler to be called each time a new piece of data arrives.
* @returns {{id: string, resource: string, deviceId: *, stream: *, dataHandler: Function, finishHandler: Function}}
* @constructor
*/
function ResourceListener(deviceId, objectType, objectId, resourceId, observeStream, handler) {
return {
id: buildId(deviceId, objectType, objectId, resourceId),
resource: '/' + objectType + '/' + objectId + '/' + resourceId,
deviceId: deviceId,
stream: observeStream,
dataHandler: function (chunk) {
logger.debug(context, 'New data on resource /%s/%s/%s in device [%d]',
objectType, objectId, resourceId, deviceId);
handler(chunk.toString('utf8'));
},
finishHandler: function(chunk) {
logger.debug(context, 'Finished observing value of resource /%s/%s/%s in device [%d]',
objectType, objectId, resourceId, deviceId);
delete subscriptions[this.id];
}
};
}
/**
* Creates the subscription object and establish the handlers for the incoming data. The first piece of data has a
* special treatment, as it has to be returned with the callback.
*
* @param {Number} deviceId Internal ID of the device of the subscription (not the endpoint ID).
* @param {Number} objectType Number identifying the object type.
* @param {Number} objectId Number identifying the object instance.
* @param {Number} resourceId Resource of the object to be subscribed to.
* @param {Function} handler Handler to be called each time a new piece of data arrives.
* @param {Object} observeStream Stream object for writing into.
*/
function processStream(deviceId, objectType, objectId, resourceId, handler, observeStream, callback) {
var subscriptionId = buildId(deviceId, objectType, objectId, resourceId);
observeStream.pause();
subscriptions[subscriptionId] =
new ResourceListener(deviceId, objectType, objectId, resourceId, observeStream, handler);
observeStream.on('data', function (chunk) {
logger.debug(context, 'Got first piece of data on resource /%s/%s/%s in device [%d]',
objectType, objectId, resourceId, deviceId);
observeStream.removeAllListeners('data');
observeStream.on('data', subscriptions[subscriptionId].dataHandler);
callback(null, chunk.toString('utf8'));
});
observeStream.on('finish', subscriptions[subscriptionId].finishHandler);
observeStream.resume();
}
/**
* Creates a subscription to a resource in a device, executing the given handler each time new data arrives to the
* server. As part of the the subscription process, the first piece of data will be automatically resolved in the
* same way as a Read action (returning the current resource data in the callback). Subsquent data will be processed
* by the handler.
*
* @param {Number} deviceId Internal ID of the device of the subscription (not the endpoint ID).
* @param {Number} objectType Number identifying the object type.
* @param {Number} objectId Number identifying the object instance.
* @param {Number} resourceId Resource of the object to be subscribed to.
* @param {Function} handler Handler to be called each time a new piece of data arrives.
*/
function observe(deviceId, objectType, objectId, resourceId, handler, callback) {
function createObserveRequest(obj, callback) {
var request = {
host: obj.address,
port: 5683,
method: 'GET',
pathname: '/' + objectType + '/' + objectId + '/' + resourceId,
observe: true
};
callback(null, request);
}
logger.debug(context, 'Observing value from resource /%s/%s/%s in device [%d]',
objectType, objectId, resourceId, deviceId);
async.waterfall([
apply(registry.get, deviceId),
createObserveRequest,
coapUtils.sendRequest,
apply(processStream, deviceId, objectType, objectId, resourceId, handler)
], callback);
}
/**
* Gets a list of all the subscriptions to client resources actually in use.
*
*/
function list(callback) {
callback(null, _.values(subscriptions));
}
/**
* Remove all the observations the server is currently performing.
*
* TODO: completely close the streams instead of simply removing them.
*/
function clean(callback) {
subscriptions = {};
callback();
}
/**
* Cancel an observation for the specified resource in a particular device.
*
* @param {Number} deviceId
* @param {Number} objectType
* @param {Number} objectId
* @param {Number} resourceId
*/
function cancel(deviceId, objectType, objectId, resourceId, callback) {
var subscriptionId = buildId(deviceId, objectType, objectId, resourceId);
subscriptions[subscriptionId].stream.close();
delete subscriptions[subscriptionId];
callback();
}
/**
* Initializes the Information Reporting interface, specifiying the current device registry to use.
*
* @param {Object} deviceRegistry Device registry in use by the application.
*/
function init(deviceRegistry) {
registry = deviceRegistry;
}
exports.buildId = buildId;
exports.parseId = parseId;
exports.observe = observe;
exports.list = list;
exports.clean = clean;
exports.cancel = cancel;
exports.init = init;
|