DataStore.prototype.getNodeByAddress16 = function(address16, cb) { if (typeof address16 == 'Array') { // make sure address is hex-string format address16 = Tools.bArr2HexStr(address16); } this.db.nodes.find({ address16: address16}, function(err, docs) { if (docs.length > 0) { cb(null, docs[0]); } else { cb(new Error("not found")); } }); }
DataStore.prototype.persistApplication = function(application, cb) { var timestamp = new Date(); var self = this; var id = application.address64 + "." + Tools.dec2Hex(application.endpoint, 2); self.db.devices.find({ id: id }, function(err, docs) { if (err) { console.log("!!! error persisting endpoint: " + err); } var applicationDoc = application; if (application.node) { applicationDoc = application.toSpec(); } //application.id = id; //application.updated = timestamp; applicationDoc.id = id; applicationDoc.updated = timestamp; // { // id : id, // address16 : application.address16, // node address // endpoint : application.endpoint, // profileId : application.profileId, // deviceId : application.deviceId, // deviceVersion : application.deviceVersion, // inputClusters : application.inputClusters, // outputClusters : application.outputClusters, // updated : timestamp, // }; if (docs.length == 0) { self.db.devices.insert(applicationDoc, function (err, newDoc) { if (typeof cb != 'undefined') { cb(err, newDoc); } }); } else { //var doc = docs[0]; self.db.devices.update({ id: id }, { $set: applicationDoc }, {}, function(err, numReplaced, upsert) { if (typeof cb != 'undefined') { cb(err, applicationDoc); } }); } }); }