forked from felixls/node-red-contrib-tplink-smarthome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-plug.html
155 lines (150 loc) · 6.75 KB
/
smart-plug.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<script type="text/javascript">
RED.nodes.registerType('smart-plug', {
category: 'devices',
color: '#4acbd6',
defaults: {
name: { value: '' },
device: { value: '', required: true},
interval: {
value: 10000,
required: true,
validate: function(v) {
return (/^[0-9]+/.test(v) && parseInt(v,10) >= 500);
}
},
eventInterval: {
value: 1000,
required: true,
validate: function(v) {
return (/^[0-9]+/.test(v) && parseInt(v,10) >= 500);
}
}
},
inputs: 1,
outputs: 1,
icon: 'icon.png',
align: 'right',
paletteLabel: 'smart plug',
label: function () {
return this.name || 'smart_plug';
},
oneditprepare: function() {
function manualDeviceIP () {
var current = $('#node-input-device').val();
$('#node-input-device').replaceWith('<input type="text" id="node-input-device" style="width: 60%;"/>');
$('#node-input-device').val(current);
}
function searchAndSelectDevice() {
var current = $('#node-input-device').val();
$('#node-input-device').replaceWith('<select id="node-input-device" style="width: 60%;"></select>');
$('#node-input-device').append('<option selected="selected" value="null">searching…</option>');
$.get('smarthome/plugs')
.done( function(data) {
var devices = JSON.parse(data);
if(devices.length <= 0) {
RED.notify("No TP-Link smart plug found.", "error");
}
$('#node-input-device').empty();
devices.forEach(function(ip) {
$('#node-input-device').append('<option value="' + ip + '">' + ip + '</option>');
});
$('#node-input-device').val(current);
})
.fail(function() {
RED.notify("Something went wrong. Please retry.", "error");
});
}
$(document).on('change', '#node-input-device', function() {
if ($('#node-input-name').val().trim().length === 0) {
var currentDeviceIP = $('#node-input-device').val();
if(currentDeviceIP.length > 6) {
$.get('smarthome/plug', { ip: currentDeviceIP } )
.done( function(data) {
$('#node-input-name').val(data);
})
.fail(function(err) {
RED.notify("Sorry. Your selected device is invalid!", "error");
});
}
}
});
$('#node-input-scan').click(function() {
if($('#node-input-device').prop("tagName") === "INPUT") {
searchAndSelectDevice();
} else {
manualDeviceIP();
}
});
}
});
</script>
<script type="text/x-red" data-template-name="smart-plug">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-device"><i class="fa fa-server"></i> Device IP</label>
<input type="text" id="node-input-device" placeholder="192.168.100.1" style="width: 60%;">
<a id="node-input-scan" class="editor-button"><i class="fa fa-search"></i></a>
</div>
<div class="form-row">
<label for="node-input-interval"><i class="fa fa-clock-o"></i> Poll interval</label>
<input type="text" placeholder="7500" id="node-input-interval">
</div>
<div class="form-row">
<label for="node-input-eventInterval"><i class="fa fa-clock-o"></i> Event poll interval</label>
<input type="text" placeholder="2000" id="node-input-eventInterval">
</div>
</script>
<script type="text/x-red" data-help-name="smart-plug">
<p>This node controls a <b>TP-Link Smart Plug</b>.</p>
<p>Full support for <a href="https://www.tp-link.com/us/products/details/cat-5516_HS100.html" target="_blank" rel="external">HS100</a> and <a href="https://www.tp-link.com/us/products/details/cat-5516_HS110.html" target="_blank" rel="external">HS110</a>.</p>
<h3>Parameters</h3>
<p>Configurable properties of the node required for succesfull deploy.</p>
<ul>
<li><code>Name</code> - Type in the name of the host manually or keep the default device name.</li>
<li><code>Device IP</code> - Type in the Device IP address manually or press the button to retreive all locally available plug devices.</li>
<li><code>Poll interval</code> - Interval that is used to poll availability of devices <em>(Min. 500ms / Recommended 5000-10000ms.)</em></li>
<li><code>Event poll interval</code> - Interval that is used to poll active devices for events <em>(Min. 500ms / Recommended 1000ms-3000ms.)</em></li>
</ul>
<p>If any "TCP Timeout" console errors occur raise the timeout limit to at least 15s <em>(15000ms)</em> in your Node-RED's <b>settings.js</b>.</p>
<h3>Inputs</h3>
<p>Control the plug by setting it's properties.</p>
<dl class="message-properties">
<dt>msg.payload<span class="property-type">string | boolean</span></dt>
<ul>
<li><code>true</code> - Turn on the device.</li>
<li><code>false</code> - Turn off the device.</li>
<li><code>switch</code> - Switch the power state of the device.</li>
</ul>
</dl>
<h3>Commands</h3>
<p>Force the plug node to output when injected as strings.</p>
<dl class="message-properties">
<dt>msg.payload<span class="property-type">string</span></dt>
<ul>
<li><code>getInfo</code> - Fetch the device proprieties, such as brightness, temperature and many more.</li>
<li><code>getCloudInfo</code> - Fetch the device proprieties, such as username, bind status and many more, from the cloud.</li>
<li><code>getQuickInfo</code> - Fetch most popular proprieties, such as username, device consumption, mode, lighting state, and many more.</li>
<li><code>getMeterInfo</code> - Fetch the current device consumption.</li>
<li><code>eraseStats</code> - Clear all the meter statistics.</li>
<li><code>clearEvents</code> - Unsubscribe from events.</li>
</ul>
</dl>
<h3>Events</h3>
<p>Subscribe to outputs based on plug's events. Frequency set by <code>Event poll interval</code>.</p>
<dl class="message-properties">
<dt>msg.payload<span class="property-type">string</span></dt>
<ul>
<li><code>getInfoEvents</code> - Subscribe to proprieties` events.</li>
<li><code>getPowerUpdateEvents</code> - Subscribe just to power ON/OFF events.</li>
<li><code>getMeterEvents</code> - Subscribe to meter information events.</li>
<li><code>getInUseEvents</code> - Subscribe to device usage events.</li>
<li><code>getOnlineEvents</code> - Subscribe just to online/offline events.</li>
</ul>
</dl>
<p>Multiple events can be used as a list separated with the <code>|</code> character.</p>
<h3>Get involved</h3>
<p>Fork and help the node on <a href="https://github.com/mental05/node-red-contrib-tplink-iot" target="_blank" rel="external">GIT Hub</a>.<br>
</script>