-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathdeviceRegistryMongoDB.js
390 lines (343 loc) · 12.3 KB
/
deviceRegistryMongoDB.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-iotagent-lib
*
* fiware-iotagent-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.
*
* fiware-iotagent-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 fiware-iotagent-lib.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::daniel.moranjimenez@telefonica.com
*/
const logger = require('logops');
const dbService = require('../../model/dbConn');
const config = require('../../commonConfig');
const fillService = require('./../common/domain').fillService;
const alarmsInt = require('../common/alarmManagement').intercept;
const errors = require('../../errors');
const constants = require('../../constants');
const Device = require('../../model/Device');
const async = require('async');
let context = {
op: 'IoTAgentNGSI.MongoDBDeviceRegister'
};
const attributeList = [
'id',
'type',
'name',
'service',
'subservice',
'lazy',
'commands',
'staticAttributes',
'active',
'registrationId',
'internalId',
'internalAttributes',
'resource',
'apikey',
'protocol',
'endpoint',
'transport',
'polling',
'timestamp',
'explicitAttrs',
'ngsiVersion',
'subscriptions',
'payloadType'
];
/**
* Generates a handler for the save device operations. The handler will take the customary error and the saved device
* as the parameters (and pass the serialized DAO as the callback value).
*
* @return {Function} The generated handler.
*/
function saveDeviceHandler(callback) {
return function saveHandler(error, deviceDAO) {
if (error) {
logger.debug(fillService(context, deviceDAO), 'Error storing device information: %s', error);
callback(new errors.InternalDbError(error));
} else {
callback(null, deviceDAO.toObject());
}
};
}
/**
* Create a new register for a device. The device object should contain the id, type and registrationId
*
* @param {Object} newDevice Device object to be stored
*/
function storeDevice(newDevice, callback) {
/* eslint-disable-next-line new-cap */
const deviceObj = new Device.model();
attributeList.forEach((key) => {
deviceObj[key] = newDevice[key];
});
// Ensure protocol is in newDevice
if (!newDevice.protocol && config.getConfig().iotManager && config.getConfig().iotManager.protocol) {
deviceObj.protocol = config.getConfig().iotManager.protocol;
}
logger.debug(context, 'Storing device with id [%s] and type [%s]', newDevice.id, newDevice.type);
deviceObj.save(function saveHandler(error, deviceDAO) {
if (error) {
if (error.code === 11000) {
logger.debug(context, 'Tried to insert a device with duplicate ID in the database: %s', error);
callback(new errors.DuplicateDeviceId(newDevice.id));
} else {
logger.debug(context, 'Error storing device information: %s', error);
callback(new errors.InternalDbError(error));
}
} else {
callback(null, deviceDAO.toObject());
}
});
}
/**
* Remove the device identified by its id and service.
*
* @param {String} id Device ID of the device to remove.
* @param {String} service Service of the device to remove.
* @param {String} subservice Subservice inside the service for the removed device.
*/
function removeDevice(id, apikey, service, subservice, callback) {
const condition = {
id,
service,
subservice
};
if (apikey) {
condition.apikey = apikey;
}
logger.debug(context, 'Removing device with id [%s]', id);
Device.model.deleteOne(condition, function (error) {
if (error) {
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
callback(new errors.InternalDbError(error));
} else {
logger.debug(context, 'Device [%s] successfully removed.', id);
callback(null);
}
});
}
/**
* Return the list of currently registered devices (via callback).
*
* @param {String} tyoe Type for which the devices are requested.
* @param {String} service Service for which the devices are requested.
* @param {String} subservice Subservice inside the service for which the devices are requested.
* @param {Number} limit Maximum number of entries to return.
* @param {Number} offset Number of entries to skip for pagination.
*/
function listDevices(type, service, subservice, limit, offset, callback) {
const condition = {};
if (type) {
condition.type = type;
}
if (service) {
condition.service = service;
}
if (subservice) {
condition.subservice = subservice;
}
const query = Device.model.find(condition).sort();
if (limit) {
query.limit(parseInt(limit, 10));
}
if (offset) {
query.skip(parseInt(offset, 10));
}
async.series(
[query.exec.bind(query), Device.model.countDocuments.bind(Device.model, condition)],
function (error, results) {
callback(error, {
count: results[1],
devices: results[0]
});
}
);
}
function findOneInMongoDB(queryParams, id, callback) {
const query = Device.model.findOne(queryParams);
query.select({ __v: 0 });
query.lean().exec(function handleGet(error, data) {
if (error) {
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
callback(new errors.InternalDbError(error));
} else if (data) {
context = fillService(context, data);
logger.debug(context, 'Device data found: %j', data);
callback(null, data);
} else {
logger.debug(context, 'Device [%s] not found.', id);
callback(new errors.DeviceNotFound(id));
}
});
}
/**
* Internal function used to find a device in the DB.
*
* @param {String} id ID of the Device to find.
* @param {String} Apikey Apikey of the Device to find.
* @param {String} service Service the device belongs to (optional).
* @param {String} subservice Division inside the service (optional).
*/
function getDeviceById(id, apikey, service, subservice, callback) {
let queryParams = {
id,
service,
subservice
};
if (apikey) {
queryParams.apikey = apikey;
}
context = fillService(context, queryParams);
logger.debug(context, 'Looking for device with id [%s] and queryParams [%j].', id, queryParams);
findOneInMongoDB(queryParams, id, callback);
}
/**
* Retrieves a device using it ID, converting it to a plain Object before calling the callback.
*
* @param {String} id ID of the Device to find.
* @param {String} service Service the device belongs to.
* @param {String} subservice Division inside the service.
*/
function getDevice(id, apikey, service, subservice, callback) {
getDeviceById(id, apikey, service, subservice, function (error, data) {
if (error) {
// Try without apikey: apikey will be added to device later
getDeviceById(id, null, service, subservice, function (error, data) {
if (error) {
callback(error);
} else {
callback(null, data);
}
});
} else {
callback(null, data);
}
});
}
function getByNameAndType(name, type, service, servicepath, callback) {
context = fillService(context, { service, subservice: servicepath });
let optionsQuery = {
name: name,
service: service,
subservice: servicepath
};
if (type) {
optionsQuery.type = type;
}
logger.debug(context, 'Looking for device with [%j].', optionsQuery);
const query = Device.model.findOne(optionsQuery);
query.select({ __v: 0 });
query.lean().exec(function handleGet(error, data) {
if (error) {
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
callback(new errors.InternalDbError(error));
} else if (data) {
callback(null, data);
} else {
logger.debug(context, 'Device [%s] not found.', name);
callback(new errors.DeviceNotFound(name));
}
});
}
function getByName(name, service, servicepath, callback) {
getByNameAndType(name, null, service, servicepath, callback);
}
/**
* Updates the given device into the database.
* updated.
*
* @param {Object} device Device object with the new values to write.
*/
function update(device, callback) {
logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4));
getDevice(device.id, device.apikey, device.service, device.subservice, function (error, data) {
if (error) {
callback(error);
} else {
data.lazy = device.lazy;
data.active = device.active;
data.internalId = device.internalId;
data.staticAttributes = device.staticAttributes;
data.internalAttributes = device.internalAttributes;
data.commands = device.commands;
data.endpoint = device.endpoint;
data.transport = device.transport;
data.polling = device.polling;
data.name = device.name;
data.type = device.type;
data.apikey = device.apikey;
data.registrationId = device.registrationId;
data.explicitAttrs = device.explicitAttrs;
data.ngsiVersion = device.ngsiVersion;
data.timestamp = device.timestamp;
data.subscriptions = device.subscriptions;
data.payloadType = device.payloadType;
/* eslint-disable-next-line new-cap */
const deviceObj = new Device.model(data);
deviceObj.isNew = false;
deviceObj.save(saveDeviceHandler(callback));
}
});
}
/**
* Cleans all the information in the database, leaving it in a clean state.
*/
function clear(callback) {
dbService.db.db.dropDatabase(callback);
}
function itemToObject(i) {
if (i.toObject) {
return i.toObject();
} else {
return i;
}
}
function getDevicesByAttribute(name, value, service, subservice, callback) {
const filter = {};
if (service) {
filter.service = service;
}
if (subservice) {
filter.subservice = subservice;
}
filter[name] = value;
context = fillService(context, filter);
logger.debug(context, 'Looking for device with filter [%j].', filter);
const query = Device.model.find(filter);
query.select({ __v: 0 });
query.exec(function handleGet(error, devices) {
if (error) {
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
callback(new errors.InternalDbError(error));
} else if (devices) {
callback(null, devices.map(itemToObject));
} else {
logger.debug(context, 'Device [%s] not found.', name);
callback(new errors.DeviceNotFound(name));
}
});
}
exports.getDevicesByAttribute = alarmsInt(constants.MONGO_ALARM, getDevicesByAttribute);
exports.store = alarmsInt(constants.MONGO_ALARM, storeDevice);
exports.update = alarmsInt(constants.MONGO_ALARM, update);
exports.remove = alarmsInt(constants.MONGO_ALARM, removeDevice);
exports.list = alarmsInt(constants.MONGO_ALARM, listDevices);
exports.get = alarmsInt(constants.MONGO_ALARM, getDevice);
exports.getSilently = getDevice;
exports.getByName = alarmsInt(constants.MONGO_ALARM, getByName);
exports.getByNameAndType = alarmsInt(constants.MONGO_ALARM, getByNameAndType);
exports.clear = alarmsInt(constants.MONGO_ALARM, clear);