This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
smart-bulb.html
154 lines (132 loc) · 4.79 KB
/
smart-bulb.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
<script type="text/javascript">
RED.nodes.registerType('smart-bulb', {
category: 'output',
color: '#FFF0F0',
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 bulb',
label: function () {
return this.name || 'smart_bulb';
},
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/bulbs')
.done( function(data) {
var devices = JSON.parse(data);
if(devices.length <= 0) {
RED.notify("No TP-Link smart bulb found.", "error");
}
// reset options
$('#node-input-device').empty();
// Set devices as options
devices.forEach(function(ip) {
$('#node-input-device').append('<option value="' + ip + '">' + ip + '</option>');
});
// select current value
$('#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/bulb', { 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-bulb">
<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.0.100" 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="10000" 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="1000" id="node-input-eventInterval">
</div>
</script>
<script type="text/x-red" data-help-name="smart-bulb">
<p>This node controls a TP-Link Smart Bulb</p>
<p>Parameters:</p>
<h3>Name</h3>
<p>Type in the name of the host manually or keep the default device name</p>
<h3>Device IP</h3>
<p>Type in the Device IP address manually or press the button to retreive all locally available bulb devices</p>
<h3>Poll interval</h3>
<p>Interval that is used to poll availability of devices (min 500ms / recommended between 5000ms and 10000ms)</p>
<h3>Event poll interval</h3>
<p>Interval that is used to poll active devices for events (min 500ms / recommended between 1000ms and 3000ms)</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">string | boolean</span>
</dt>
<p>
<p><b>On/Off</b></p>
<p>true: Turn on the device.</p>
<p>false: Turn off the device.</p>
<p><b>Commands</b></p>
<p>getInfo: Fetch the device information.</p>
<p>clearEvents: Unsubscribe events.</p>
<p><b>Events</b></p>
<p>getInfoEvents: Subscribe to information events.</p>
<p>getPowerUpdateEvents: Subscribe to power on/off events.</p>
<p>getOnlineEvents: Subscribe to online/offline events.</p>
<p>Multiple events can be used as a list separated with the `|` character.</p>
</p>
</script>