Release v1.6.0 (#15)

* added new method to listen for websocket messages

* version bump

* updated README
This commit is contained in:
Martin M
2019-09-15 01:48:13 -03:00
committed by GitHub
parent 1cd87a8d90
commit 4a21470978
5 changed files with 53 additions and 11 deletions

View File

@@ -1,5 +1,12 @@
# ewelink-api # ewelink-api
> eWeLink API for Node.js > eWeLink API for JavaScript
## Key features
* can run on browsers, node scripts or serverless environment
* set on/off devices
* get power consumption on devices like Sonoff POW
* listen for devices events
## Installation ## Installation
@@ -9,11 +16,4 @@
## Usage ## Usage
Check docs at https://ewelink-api.now.sh. Check docs at https://ewelink-api.now.sh
## Testing
1. open `test/_setup/credentials.json` and update parameters.
2. in a terminal, `npm run test`
* Tests needs to be performed serially, so if run jest manually, add `--runInBand` parameter.

View File

@@ -142,6 +142,9 @@ const getDeviceMixin = require('./mixins/devices/getDeviceMixin');
const getDeviceChannelCountMixin = require('./mixins/devices/getDeviceChannelCountMixin'); const getDeviceChannelCountMixin = require('./mixins/devices/getDeviceChannelCountMixin');
const getFirmwareVersionMixin = require('./mixins/devices/getFirmwareVersionMixin'); const getFirmwareVersionMixin = require('./mixins/devices/getFirmwareVersionMixin');
/* LOAD MIXINS: websocket */
const openWebSocketMixin = require('./mixins/websocket/openWebSocketMixin');
Object.assign( Object.assign(
eWeLink.prototype, eWeLink.prototype,
getDevicePowerStateMixin, getDevicePowerStateMixin,
@@ -169,4 +172,6 @@ Object.assign(
getFirmwareVersionMixin getFirmwareVersionMixin
); );
Object.assign(eWeLink.prototype, openWebSocketMixin);
module.exports = eWeLink; module.exports = eWeLink;

View File

@@ -0,0 +1,37 @@
const W3CWebSocket = require('websocket').w3cwebsocket;
const WebSocketAsPromised = require('websocket-as-promised');
const payloads = require('../../lib/payloads');
const openWebSocketMixin = {
/**
* Open a socket connection to eWeLink
* and execute callback function with server message as argument
*
* @param callback
*
* @returns {Promise<WebSocketAsPromised>}
*/
async openWebSocket(callback) {
const payloadLogin = await payloads.wssLoginPayload({
at: this.at,
apiKey: this.apiKey,
});
const wsp = new WebSocketAsPromised(this.getApiWebSocket(), {
createWebSocket: wss => new W3CWebSocket(wss),
});
wsp.onMessage.addListener(message => {
const data = JSON.parse(message);
callback(data);
});
await wsp.open();
await wsp.send(payloadLogin);
return wsp;
},
};
module.exports = openWebSocketMixin;

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "ewelink-api", "name": "ewelink-api",
"version": "1.5.1", "version": "1.6.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "ewelink-api", "name": "ewelink-api",
"version": "1.5.1", "version": "1.6.0",
"description": "eWeLink API for Node.js", "description": "eWeLink API for Node.js",
"author": "Martín M.", "author": "Martín M.",
"license": "MIT", "license": "MIT",