Skip to content
Closed
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
87 changes: 54 additions & 33 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,29 @@ function init (env, ctx, server) {
}
// if not found create new record
console.log(LOG_DEDUP + 'Adding new record');
ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, doc) {
if (err != null && err.message) {
console.log('treatments data insertion error: ', err.message);
return;
}

ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
});

if (callback) {
callback(doc.ops);
}
ctx.bus.emit('data-received');
if (!err) {
ctx.store.collection(collection).findOne({ '_id': doc.insertedId }
, function(err, results) {
if (!err && results !== null) {
ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([results])
});

if (callback) {
callback([results]);
}
ctx.bus.emit('data-received');
}
});
}
});
});
});
Expand Down Expand Up @@ -451,40 +458,54 @@ function init (env, ctx, server) {

});

ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, doc) {
if (err != null && err.message) {
console.log('devicestatus insertion error: ', err.message);
return;
}

ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
});

if (callback) {
callback(doc.ops);
}
ctx.bus.emit('data-received');
if (!err) {
ctx.store.collection(collection).findOne({ '_id': doc.insertedId }
, function(err, results) {
if (!err && results !== null) {
ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([results])
});

if (callback) {
callback([results]);
}
ctx.bus.emit('data-received');
}
});
}
});
} else {
ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, doc) {
if (err != null && err.message) {
console.log(data.collection + ' insertion error: ', err.message);
return;
}

ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
});

if (callback) {
callback(doc.ops);
}
ctx.bus.emit('data-received');
if (!err) {
ctx.store.collection(collection).findOne({ '_id': doc.insertedId }
, function(err, results) {
if (!err && results !== null) {
ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([results])
});

if (callback) {
callback([results]);
}
ctx.bus.emit('data-received');
}
});
}
});
}
});
Expand Down