Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
req.on('error', function(err) {
if (!_.isFunction(callback))
self.emit('error', err);
else if (err.retransmitTimeout)
callback(null, { code: RSP.timeout });
else
callback(err);
});
Device.prototype.ping = function (callback) {
validate.fn(callback);
var self = this,
nc = this.get('netcore'),
ping = nc ? nc.ping : undefined,
err = anyEnableError.call(this),
permAddr = this.get('permAddr');
if (err)
return setImmediate(callback, err);
else if (!_.isFunction(ping))
return setImmediate(callback, new Error('Net driver ping is not implemented'));
ping.call(nc, permAddr, callback);
};
args = undefined;
}
validate.argTypes({ attrName: attrName, callback: callback });
validate.array(args, 'args should be an array.');
var self = this,
nc = this.get('netcore'),
exec = nc ? nc._findDriver('gad', 'exec') : undefined,
err = anyEnableError.call(this),
permAddr = this.get('permAddr'),
auxId = this.get('auxId');
if (err)
return setImmediate(callback, err);
else if (!_.isFunction(exec))
return setImmediate(callback, 'Gadget driver exec is not implemented');
exec(permAddr, auxId, attrName, args, function (err, data) {
var result;
if (!err) {
result = _.set({}, attrName, data);
self._handoff(EVTS.GadExec, { permAddr: permAddr, auxId: auxId, data: result });
}
callback(err, data);
});
};
Gadget.prototype.write = function (attrName, val, callback) {
validate.argTypes({ attrName: attrName, val: val, callback: callback });
var self = this,
nc = this.get('netcore'),
write = nc ? nc._findDriver('gad', 'write') : undefined,
err = anyEnableError.call(this),
permAddr = this.get('permAddr'),
auxId = this.get('auxId');
if (err)
return setImmediate(callback, err);
else if (!_.isFunction(write))
return setImmediate(callback, 'Gadget driver write is not implemented');
write(permAddr, auxId, attrName, val, function (err, data) {
var result;
if (!err) {
result = _.set({}, attrName, _.isNil(data) ? val : data);
self.set('attrs', result);
self._handoff(EVTS.GadWrite, { permAddr: permAddr, auxId: auxId, data: result });
}
callback(err, _.isNil(data) ? val : data);
});
};
Gadget.prototype.read = function (attrName, callback) {
validate.argTypes({ attrName: attrName, callback: callback });
var self = this,
nc = this.get('netcore'),
read = nc ? nc._findDriver('gad', 'read') : undefined,
err = anyEnableError.call(this),
permAddr = this.get('permAddr'),
auxId = this.get('auxId');
if (err)
return setImmediate(callback, err);
else if (!_.isFunction(read))
return setImmediate(callback, 'Gadget driver read is not implemented');
read(permAddr, auxId, attrName, function (err, data) {
var result;
if (!err) {
result = _.set({}, attrName, data);
self.set('attrs', result);
self._handoff(EVTS.GadRead, { permAddr: permAddr, auxId: auxId, data: result });
}
callback(err, data);
});
};
Device.prototype.read = function (attrName, callback) {
validate.argTypes({ attrName: attrName, callback: callback });
var self = this,
nc = this.get('netcore'),
read = nc ? nc._findDriver('dev', 'read') : undefined,
err = anyEnableError.call(this),
permAddr = this.get('permAddr');
if (err)
return setImmediate(callback, err);
else if (!_.isFunction(read))
return setImmediate(callback, new Error('Device driver read is not implemented'));
read(permAddr, attrName, function (err, data) {
var result;
if (!err) {
result = _.set({}, attrName, data);
self._handoff(BTM_EVTS.DevRead, { permAddr: permAddr, data: result });
self.set('attrs', result);
}
callback(err, data);
});
};
checkout (duration, callback) {
if (_.isFunction(duration)) {
callback = duration
duration = undefined
}
if (!_.isFunction(callback)) throw new TypeError('callback should be given and should be a function.')
duration = duration || 0
reporter.clear(this)
this._sleep = true
return request.checkout(this, duration, callback)
}
checkin (callback) {
if (!_.isFunction(callback)) throw new TypeError('callback should be given and should be a function.')
this._sleep = false
return request.checkin(this, callback)
}
ping (callback) {
if (!_.isFunction(callback)) throw new TypeError('callback should be given and should be a function.')
return request.ping(this, callback)
}
}