Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native promises for Bridge/Controller #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 11 additions & 22 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
- [.mount(zApp, callback)](#mountzapp-callback)
- [.list([ieeeAddrs])](#listieeeaddrs)
- [.find(addr, epId)](#findaddr-epid)
- [.lqi(ieeeAddr, callback)](#lqiieeeaddr-callback)
- [.remove(ieeeAddr[, cfg][, callback])](#removeieeeaddr-cfg-callback)
- [.lqi(ieeeAddr)](#lqiieeeaddr)
- [.remove(ieeeAddr[, cfg])](#removeieeeaddr-cfg)
- [.acceptDevIncoming(devInfo, callback)](#acceptdevincomingdevinfo-callback)
- [Event: 'ready'](#event-ready)
- [Event: 'error'](#event-error)
Expand Down Expand Up @@ -369,7 +369,6 @@ Query the link quality index from a certain device by its ieee address.
**Arguments:**

1. `ieeeAddr` (_String_): Ieee address of the device.
2. `callback` (_Function_): `function (err, data) { }`. This method returns you the link quality index via `data`. An error occurs if device not found.

**Returns:**

Expand All @@ -378,10 +377,9 @@ Query the link quality index from a certain device by its ieee address.
**Examples:**

```js
bridge.lqi('0x00124b0001ce4beb', (err, data) => {
if (!err) {
console.log(data);
/*
bridge.lqi('0x00124b0001ce4beb')
.then(() => console.log(data));
/*
[
{
ieeeAddr: '0x00124b0001ce3631',
Expand All @@ -392,14 +390,12 @@ bridge.lqi('0x00124b0001ce4beb', (err, data) => {
lqi: 70
}
]
*/
}
});
*/
```

********************************************

### .remove(ieeeAddr[, cfg][, callback])
### .remove(ieeeAddr[, cfg])

Remove the device from the network.

Expand All @@ -409,7 +405,6 @@ Remove the device from the network.
2. `cfg` (_Object_):
- `reJoin` (_Boolean_): Set to `true` if device is allowed for re-joining, otherwise `false`. Default is `true`.
- `rmChildren` (_Boolean_): Set to `true` will remove all children of this device as well. Default is `false`.
3. `callback` (_Function_): `function (err) { }`.

**Returns:**

Expand All @@ -418,18 +413,12 @@ Remove the device from the network.
**Examples:**

```js
bridge.remove('0x00124b0001ce4beb', (err) => {
if (!err) {
console.log('Successfully removed!');
}
});
bridge.remove('0x00124b0001ce4beb')
.then(() => console.log('Successfully removed!'));

// remove and ban [TODO: how to unban???]
bridge.remove('0x00124b0001ce4beb', { reJoin: false }, (err) => {
if (!err) {
console.log('Successfully removed!');
}
});
bridge.remove('0x00124b0001ce4beb', { reJoin: false })
.then(() => console.log('Successfully removed!'));
```

********************************************
Expand Down
80 changes: 39 additions & 41 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ module.exports = class Bridge extends EventEmitter {
throw new Error('Coordinator has not been initialized yet.');
}
}).then(() => {
return this.controller.registerEp(loEp).then(function() {
debug.bridge('Register zApp, epId: %s, profId: %s ', loEp.getEpId(), loEp.getProfId());
});
return this.controller.registerEp(loEp)
.then(() => {
debug.bridge('Register zApp, epId: %s, profId: %s ', loEp.getEpId(), loEp.getProfId());
});
}).then(() => {
return this.controller.querie.coordInfo()
.then((coordInfo) => {
Expand Down Expand Up @@ -430,52 +431,49 @@ module.exports = class Bridge extends EventEmitter {
: undefined;
}

lqi(ieeeAddr, callback) {
proving.string(ieeeAddr, 'ieeeAddr should be a string.');

const dev = this._findDevByAddr(ieeeAddr);

return Q.fcall(() => {
if (dev) {
return this.controller.request('ZDO', 'mgmtLqiReq', {
dstaddr: dev.getNwkAddr(),
startindex: 0,
});
}
lqi(ieeeAddr) {
return Promise.resolve()
.then(() => {
proving.string(ieeeAddr, 'ieeeAddr should be a string.');
})
.then(() => this._findDevByAddr(ieeeAddr))
.then((dev) => {
if (!dev) {
throw new Error('device is not found.');
}

return Q.reject(new Error('device is not found.'));
}).then(function(rsp) {
// { srcaddr, status, neighbortableentries, startindex, neighborlqilistcount, neighborlqilist }
if (rsp.status !== 0) return;
return dev;
})
.then((dev) => this.controller.request('ZDO', 'mgmtLqiReq', {
dstaddr: dev.getNwkAddr(),
startindex: 0,
}))
.then((rsp) => {
// { srcaddr, status, neighbortableentries, startindex, neighborlqilistcount, neighborlqilist }
if (rsp.status !== 0) return;

// success
return _.map(rsp.neighborlqilist, (neighbor) => {
return {
// success
return rsp.neighborlqilist.map((neighbor) => ({
ieeeAddr: neighbor.extAddr,
lqi: neighbor.lqi,
};
}));
});
}).nodeify(callback);
}

remove(ieeeAddr, cfg, callback) {
proving.string(ieeeAddr, 'ieeeAddr should be a string.');

const dev = this._findDevByAddr(ieeeAddr);

if (_.isFunction(cfg) && !_.isFunction(callback)) {
callback = cfg;
cfg = {};
} else {
cfg = cfg || {};
}

if (!dev) {
return Q.reject(new Error('device is not found.'))
.nodeify(callback);
}
remove(ieeeAddr, cfg = {}) {
return Promise.resolve()
.then(() => {
proving.string(ieeeAddr, 'ieeeAddr should be a string.');
})
.then(() => this._findDevByAddr(ieeeAddr))
.then((dev) => {
if (!dev) {
throw new Error('device is not found.');
}

return this.controller.remove(dev, cfg, callback);
return dev;
})
.then((dev) => this.controller.remove(dev, cfg));
}

/*
Expand Down
133 changes: 71 additions & 62 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,86 +494,95 @@ module.exports = class Controller extends EventEmitter {
});
}

remove(dev, cfg, callback) {
remove(dev, cfg) {
// cfg: { reJoin, rmChildren }
let reqArgObj;
let rmChildren = 0x00;

if (!(dev instanceof Device)) {
throw new TypeError('dev should be an instance of Device class.');
} else if (!_.isPlainObject(cfg)) {
throw new TypeError('cfg should be an object.');
}

// defaults to true
cfg.reJoin = cfg.hasOwnProperty('reJoin')
? !!cfg.reJoin
: true;

// defaults to false
cfg.rmChildren = cfg.hasOwnProperty('rmChildren')
? !!cfg.rmChildren
: false;

rmChildren = cfg.reJoin
? (rmChildren | 0x01)
: rmChildren;

rmChildren = cfg.rmChildren
? (rmChildren | 0x02)
: rmChildren;

reqArgObj = {
dstaddr: dev.getNwkAddr(),
deviceaddress: dev.getIeeeAddr(),
removechildren_rejoin: rmChildren,
};
return Promise.resolve()
.then(() => {
if (!(dev instanceof Device)) {
throw new TypeError('dev should be an instance of Device class.');
}

return this.request('ZDO', 'mgmtLeaveReq', reqArgObj)
if (!_.isPlainObject(cfg)) {
throw new TypeError('cfg should be an object.');
}
})
.then(() => {
// defaults to true
cfg.reJoin = cfg.hasOwnProperty('reJoin')
? !!cfg.reJoin
: true;

// defaults to false
cfg.rmChildren = cfg.hasOwnProperty('rmChildren')
? !!cfg.rmChildren
: false;

rmChildren = cfg.reJoin
? (rmChildren | 0x01)
: rmChildren;

rmChildren = cfg.rmChildren
? (rmChildren | 0x02)
: rmChildren;

return {
dstaddr: dev.getNwkAddr(),
deviceaddress: dev.getIeeeAddr(),
removechildren_rejoin: rmChildren,
};
})
.then((reqArgObj) => this.request('ZDO', 'mgmtLeaveReq', reqArgObj))
.then((rsp) => {
if (rsp.status !== 0 && rsp.status !== 'SUCCESS') {
return Q.reject(rsp.status);
return Promise.reject(rsp.status);
}
})
.nodeify(callback);
});
}

registerEp(loEp, callback) {
if (!(loEp instanceof Coordpoint)) {
throw new TypeError('loEp should be an instance of Coordpoint class.');
}

return this.request('AF', 'register', makeRegParams(loEp))
.then((rsp) => rsp)
.fail((err) => (err.message === 'rsp error: 184') ? this.reRegisterEp(loEp) : Q.reject(err))
.nodeify(callback);
registerEp(loEp) {
return Promise.resolve()
.then(() => {
if (!(loEp instanceof Coordpoint)) {
throw new TypeError('loEp should be an instance of Coordpoint class.');
}
})
.then(() => this.request('AF', 'register', makeRegParams(loEp)))
.catch((err) => {
return err.message === 'rsp error: 184'
? this.reRegisterEp(loEp)
: Promise.reject(err);
});
}

deregisterEp(loEp, callback) {
deregisterEp(loEp) {
const coordEps = this.getCoord().endpoints;

if (!(loEp instanceof Coordpoint)) {
throw new TypeError('loEp should be an instance of Coordpoint class.');
}

return Q.fcall(() => {
if (!_.includes(coordEps, loEp)) {
return Q.reject(new Error('Endpoint not maintained by Coordinator, cannot be removed.'));
}
return Promise.resolve()
.then(() => {
if (!(loEp instanceof Coordpoint)) {
throw new TypeError('loEp should be an instance of Coordpoint class.');
}
})
.then(() => {
if (!_.includes(coordEps, loEp)) {
throw new Error('Endpoint not maintained by Coordinator, cannot be removed.');
}

return this.request('AF', 'delete', {
endpoint: loEp.getEpId(),
return this.request('AF', 'delete', {
endpoint: loEp.getEpId(),
});
})
.then((rsp) => {
delete coordEps[loEp.getEpId()];
return rsp;
});
}).then((rsp) => {
delete coordEps[loEp.getEpId()];
return rsp;
}).nodeify(callback);
}

reRegisterEp(loEp, callback) {
reRegisterEp(loEp) {
return this.deregisterEp(loEp)
.then(() => this.request('AF', 'register', makeRegParams(loEp)))
.nodeify(callback);
.then(() => this.request('AF', 'register', makeRegParams(loEp)));
}

simpleDescReq(nwkAddr, ieeeAddr, callback) {
Expand Down
Loading