Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function buildDevAttr(req) {
var devAttr = {},
query = req.url ? req.url.split('?')[1] : undefined,
queryParams = query ? query.split('&') : undefined,
invalidAttrs = [];
_.forEach(queryParams, function (queryParam, idx) {
queryParams[idx] = queryParam.split('=');
});
_.forEach(queryParams, function(queryParam) { // 'ep=clientName<=86400&lwm2m=1.0.0'
if(queryParam[0] === 'ep') {
devAttr.clientName = queryParam[1];
} else if (queryParam[0] === 'lt') {
devAttr.lifetime = parseInt(queryParam[1]);
} else if (queryParam[0] === 'lwm2m') {
devAttr.version = queryParam[1];
} else if (queryParam[0] === 'mac') {
devAttr.mac = queryParam[1];
} else {
invalidAttrs.push(queryParam[0]);
}
});
.removeAllListeners(EVT_BTM.NcBannedDevIncoming)
.removeAllListeners(EVT_BTM.NcBannedDevReporting)
.removeAllListeners(EVT_BTM.NcBannedGadIncoming)
.removeAllListeners(EVT_BTM.NcBannedGadReporting)
.removeAllListeners(EVT_BTM.DevError)
.removeAllListeners(EVT_BTM.DevNetChanged)
.removeAllListeners(EVT_BTM.DevPropsChanged)
.removeAllListeners(EVT_BTM.DevAttrsChanged)
.removeAllListeners(EVT_BTM.GadError)
.removeAllListeners(EVT_BTM.GadPanelChanged)
.removeAllListeners(EVT_BTM.GadPropsChanged)
.removeAllListeners(EVT_BTM.GadAttrsChanged)
.removeAllListeners(EVT_BTM.GadAttrsAppend);
// Firstly bind the handlers to freebird
_.forEach(handlers, function (lsn, key) {
ncLsns[key] = lsn.bind(freebird);
});
// Then, attach the handlers to events // VVVVVVVVVVVVVVVVVVVVVV [TODO] all kvps need to be checked
freebird.on(EVT_BTM.NcError, ncLsns.ncError); // { ncName, error: err }
freebird.on(EVT_BTM.NcEnabled, ncLsns.ncEnabled);
freebird.on(EVT_BTM.NcDisabled, ncLsns.ncDisabled);
freebird.on(EVT_BTM.NcStarted, ncLsns.ncStarted);
freebird.on(EVT_BTM.NcStopped, ncLsns.ncStopped);
freebird.on(EVT_BTM.NcReady, ncLsns.ncReady); // { ncName }
freebird.on(EVT_BTM.NcPermitJoin, ncLsns.ncPermitJoin); // { ncName, timeLeft: self._joinTicks }
freebird.on(EVT_BTM.NcDevIncoming, ncLsns.ncDevIncoming); // { ncName, permAddr: permAddr, raw: rawDev }
freebird.on(EVT_BTM.NcDevLeaving, ncLsns.ncDevLeaving); // { ncName, permAddr: permAddr }
freebird.on(EVT_BTM.NcDevNetChanging, ncLsns.ncDevNetChanging); // { ncName, permAddr: permAddr, data: changes }
freebird.on(EVT_BTM.NcDevReporting, ncLsns.ncDevReporting); // { ncName, permAddr: permAddr, data: devAttrs }
module.exports = function attachEventListeners(freebird) {
var ncLsns;
freebird._ncEventListeners = freebird._ncEventListeners || {};
ncLsns = freebird._ncEventListeners;
// Firstly bind the handlers to freebird
_.forEach(listeners, function (lsn, key) {
if (key !== 'attachEventListeners')
ncLsns[key] = lsn.bind(freebird);
});
// Then, attach the handlers to events
freebird.on(LLEVT.NETCORE.ready, ncLsns.ncReady); // {}
freebird.on(LLEVT.NETCORE.error, ncLsns.ncError); // { error: err }
freebird.on(LLEVT.NETCORE.permitJoin, ncLsns.ncPermitJoin); // { timeLeft: self._joinTicks }
freebird.on(LLEVT.NETCORE.started, ncLsns.ncStarted);
freebird.on(LLEVT.NETCORE.stopped, ncLsns.ncStopped);
freebird.on(LLEVT.NETCORE.enabled, ncLsns.ncEnabled);
freebird.on(LLEVT.NETCORE.disabled, ncLsns.ncDisabled);
freebird.on(LLEVT.NETCORE.devNetChanging, ncLsns.ncDevNetChanging); // { permAddr: permAddr, data: changes }
freebird.on(LLEVT.NETCORE.devIncoming, ncLsns.ncDevIncoming); // { permAddr: permAddr, raw: rawDev }
freebird.on(LLEVT.NETCORE.bannedDevIncoming, ncLsns.ncBannedDevIncoming); // { permAddr: permAddr, raw: rawDev }
freebird.on(LLEVT.NETCORE.devLeaving, ncLsns.ncDevLeaving); // { permAddr: permAddr }
_.forEach(self.objList, (iids, oid) => {
_.forEach(iids, (iid) => {
const path = mutils.createPath('/', oid, iid)
readAllProms.push(self.readReq(path)) // readReq will update the database
})
})
return Q.all(readAllProms)
function registerDevice(freebird, dev, callback) {
var devId, oldDev;
if (!validate.isDevice(dev)) {
setImmediate(callback, new TypeError('Input dev is not a valid device instance'));
return freebird;
}
devId = dev.get('id');
oldDev = _.isNil(devId) ? undefined : freebird.findById('device', devId);
if (oldDev) {
setImmediate(callback, new Error('Device with id: ' + devId + ' already exists, unregister it first'));
return freebird;
}
if (dev._recovering) { // recovered from database (when at booting up or restarting stage)
freebird._devbox.set(devId, dev, function (err, id) {
if (!err)
dev._recovering = false;
callback(err, id);
});
} else {
dev._poke();
dev.set('net', {
subscribe (topics, opts, callback) {
if (_.isFunction(opts)) {
callback = opts
opts = { qos: 0 }
}
if (!_.isFunction(callback)) throw new TypeError('callback should be given and should be a function.')
this.mc.subscribe(topics, opts, callback) // function (err, granted)
return this
}
function getAttrQuery(attr) {
var query = '';
_.forEach(attr, function (value, key) {
if (key === 'step')
query = query + 'stp=' + value + '&';
else
query = query + key + '=' + value + '&';
});
return query.slice(0, query.length - 1); // take off the last '&'
}
function getAttrQuery(attr) {
var query = '';
_.forEach(attr, function (value, key) {
if (key === 'step')
query = query + 'stp=' + value + '&';
else
query = query + key + '=' + value + '&';
});
return query.slice(0, query.length - 1); // take off the last '&'
}
CoapNode.prototype._disableAllReport = function () {
var self = this;
helper.heartbeat(this, false);
_.forEach(this._reporters, function (rpt, key) {
var oid = key.split('/')[0],
iid = key.split('/')[1],
rid = key.split('/')[2];
self._disableReport(oid, iid, rid, function (err, result) {
if (err)
self.emit('error', err);
});
});
};
rRpt.max = setInterval(() => {
rAttrs.mute = true
dumper((err, val) => {
if (err) console.warn(err)
rAttrs.mute = false
notifier.data = val
qn.notify(notifier, () => {})
})
if (!_.isNil(rRpt.min)) clearTimeout(rRpt.min)
rRpt.min = null
rRpt.min = setTimeout(() => {
if (pmin === 0) {
rAttrs.mute = false
} else {
dumper((err, val) => {
if (err) console.warn(err)
rAttrs.mute = false
notifier.data = val
qn.notify(notifier, () => {})
})
}
}, pmin)
}, pmax)