Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- ADD: index for Device model based on {service: 1, subservice: 1, id: 1, apikey: 1} (#1576)
- Fix: modified JEXL transformations (toisostring, gettime, parseint, etc.) to return null instead of NaN when some unexpected situation occurs (#1701)
1 change: 1 addition & 0 deletions lib/model/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const Device = new Schema({
});

function load() {
Device.index({ service: 1, subservice: 1, apikey: 1, id: 1 }, { unique: true });
module.exports.model = mongoose.model('Device', Device);
module.exports.internalSchema = Device;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function storeDevice(newDevice, callback) {
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));
callback(new errors.DuplicateDeviceId(newDevice), newDevice);
} else {
logger.debug(context, 'Error storing device information: %s', error);

Expand Down
13 changes: 11 additions & 2 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const registrationUtils = require('./registrationUtils');
const subscriptions = require('../ngsi/subscriptionService');
const expressionPlugin = require('./../../plugins/expressionPlugin');
const pluginUtils = require('./../../plugins/pluginUtils');
const alarms = require('../common/alarmManagement');
const constants = require('../../constants');
const _ = require('underscore');
const context = {
op: 'IoTAgentNGSI.DeviceService'
Expand Down Expand Up @@ -250,7 +252,7 @@ function registerDevice(deviceObj, callback) {
/* eslint-disable-next-line no-unused-vars */
function (error, device) {
if (!error) {
innerCb(new errors.DuplicateDeviceId(deviceObj));
innerCb(new errors.DuplicateDeviceId(deviceObj), device);
} else {
innerCb();
}
Expand Down Expand Up @@ -664,7 +666,14 @@ function findOrCreate(deviceId, apikey, group, callback) {
if (group.autoprovision === undefined || group.autoprovision === true) {
logger.debug(context, 'Registering autoprovision of Device %j for its conf %j', newDevice, group);
registerDevice(newDevice, function (error, device) {
callback(error, device, group);
if (error && error.name === 'DUPLICATE_DEVICE_ID') {
alarms.release(constants.MONGO_ALARM, error);
logger.warn(context, 'Error %j already registered autoprovisioned device: %j ', error, device);
callback(null, device, group);
} else {
logger.debug(context, 'registered autoprovisioned device: %j ', device);
callback(error, device, group);
}
});
} else {
logger.info(
Expand Down