diff --git a/README.md b/README.md index 381ca10..8b48a0f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,18 @@ Alternatively, use the Palette Manager in Node-RED. Drag this node on to a worksheet and double click it. Enter the IP address of the plug on your network. Save and deploy. -To turn the HS100 on, send a message a message with the topic or payload set to `on`. -To turn the HS100 off, send a message a message with the topic or payload set to `off`. +# Actuation + +## Turn on + +To turn the HS100 on, send a message to this node's input with the topic or payload set to `on`. + +## Turn off + +To turn the HS100 off, send a message to this node's input with the topic or payload set to `off`. + +## Obtain power consumption data + +To obtain the power consumption, send a message to this node's input with the topic or payload set to `consumption`. +The consumption data will be sent via this node's output in `msg.payload`. diff --git a/index.html b/index.html index df53179..537670a 100644 --- a/index.html +++ b/index.html @@ -31,7 +31,7 @@ host: {value: ''} }, inputs: 1, - outputs: 0, + outputs: 1, icon: 'hs100.png', label: function () { return this.name || 'hs100'; @@ -67,8 +67,19 @@

Configuration

Drag this node on to a worksheet and double click it. Enter the IP address of the plug on your network. Save and deploy.

-

To turn the HS100 on, send a message a message with the topic or payload set to on.

+

Actuation

-

To turn the HS100 off, send a message a message with the topic or payload set to off.

+

Turn on

+ +

To turn the HS100 on, send a message to this node's input with the topic or payload set to on.

+ +

Turn off

+ +

To turn the HS100 off, send a message to this node's input with the topic or payload set to off.

+ +

Obtain power consumption data

+ +

To obtain the power consumption, send a message to this node's input with the topic or payload set to `consumption`. The consumption data will +be sent via this node's output in msg.payload.

\ No newline at end of file diff --git a/index.js b/index.js index e35a0df..8f4855e 100644 --- a/index.js +++ b/index.js @@ -36,17 +36,41 @@ module.exports = function (RED) { var plug = client.getPlug({host: config.host}); node.on('input', function (msg) { - var state = (msg.payload === 'on' || msg.topic === 'on'); - plug.setPowerState(state); - node.status({ - fill: 'green', - shape: state ? 'dot' : 'circle', - text: state ? 'on' : 'off' - }); + if (msg.payload === 'consumption' || msg.topic === 'consumption') { + plug.getConsumption().then(function (data) { + node.send({payload: data}); + }).catch(errorHandler); + } else if (msg.payload === 'on' || msg.topic === 'on') { + setPowerState(true); + } else if (msg.payload === 'off' || msg.topic === 'off') { + setPowerState(false); + } else { + errorHandler(new Error('Actuation must be one of [on, off, consumption]')); + } }); node.on('close', function () { client.socket.close(); }); + + function setPowerState(on) { + node.status({ + fill: 'orange', + shape: on ? 'dot' : 'circle', + text: 'Turning ' + ( on ? 'on' : 'off') + }); + plug.setPowerState(on).then(function () { + node.status({ + fill: 'green', + shape: on ? 'dot' : 'circle', + text: on ? 'on' : 'off' + }); + }).catch(errorHandler); + } + + function errorHandler(err) { + node.error(err); + node.status({fill: 'red', shape: 'dot', text: err.message}); + } }); }; \ No newline at end of file diff --git a/package.json b/package.json index 0824b4e..d5e758b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-hs100", - "version": "0.2.1", + "version": "0.3.0", "description": "", "main": "index.js", "keywords": ["node-red", "tp-link", "tplink", "hs100"],