mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-21 13:23:05 +01:00
Release v1.6.0 (#15)
* added new method to listen for websocket messages * version bump * updated README
This commit is contained in:
18
README.md
18
README.md
@@ -1,5 +1,12 @@
|
||||
# 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
|
||||
@@ -9,11 +16,4 @@
|
||||
|
||||
|
||||
## Usage
|
||||
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.
|
||||
Check docs at https://ewelink-api.now.sh
|
||||
5
main.js
5
main.js
@@ -142,6 +142,9 @@ const getDeviceMixin = require('./mixins/devices/getDeviceMixin');
|
||||
const getDeviceChannelCountMixin = require('./mixins/devices/getDeviceChannelCountMixin');
|
||||
const getFirmwareVersionMixin = require('./mixins/devices/getFirmwareVersionMixin');
|
||||
|
||||
/* LOAD MIXINS: websocket */
|
||||
const openWebSocketMixin = require('./mixins/websocket/openWebSocketMixin');
|
||||
|
||||
Object.assign(
|
||||
eWeLink.prototype,
|
||||
getDevicePowerStateMixin,
|
||||
@@ -169,4 +172,6 @@ Object.assign(
|
||||
getFirmwareVersionMixin
|
||||
);
|
||||
|
||||
Object.assign(eWeLink.prototype, openWebSocketMixin);
|
||||
|
||||
module.exports = eWeLink;
|
||||
|
||||
37
mixins/websocket/openWebSocketMixin.js
Normal file
37
mixins/websocket/openWebSocketMixin.js
Normal 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
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ewelink-api",
|
||||
"version": "1.5.1",
|
||||
"version": "1.6.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ewelink-api",
|
||||
"version": "1.5.1",
|
||||
"version": "1.6.0",
|
||||
"description": "eWeLink API for Node.js",
|
||||
"author": "Martín M.",
|
||||
"license": "MIT",
|
||||
|
||||
Reference in New Issue
Block a user