forked from nidayand/node-red-contrib-tradfri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradfri-out.html
346 lines (320 loc) · 17.2 KB
/
tradfri-out.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<script type="text/javascript">
RED.nodes.registerType('tradfri-get', {
category: 'function',
defaults: {
name: {value: ""},
hub : {type: "tradfri-config", required: true}
},
color: "#E9967A",
icon: "light.png",
align: 'left',
inputs: 1,
outputs: 1,
label : function(){ return this.name || "tradfri get"},
oneditprepare : function(){
var setHover = function(obj, txt){
obj.hover(function(){
$('#tradfri-help').html(txt);
},function(){
$('#tradfri-help').html('');
});
}
setHover($('#node-input-hub'),'Select IKEA hub configuration to use');
}
});
</script>
<script type="text/x-red" data-template-name="tradfri-get">
<div class="form-row" style="display:none">
<input type="text" id="node-input-name"><input type="text" id="node-input-dtype">
</div>
<div class="form-row">
<label for="node-input-config"><i class="fa fa-bookmark"></i> Config</label>
<input type="text" id="node-input-hub">
</div>
<div class="form-row">
<i style="font-size:95%;color:#E9967A"><span id="tradfri-help"></span></i>
</div>
</script>
<script type="text/x-red" data-help-name="tradfri-get">
<p>Retrieves IKEA TRÅDFRI device information from your IKEA hub.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">string | integer | optional</span>
</dt>
<dd> the device or group id to lookup</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<li>Standard output
<dl class="message-properties">
<dt>payload <span class="property-type">object | array</span></dt>
<dd>Device, group object or an array with all groups and devices</dd>
</dl>
</li>
</ol>
<h3>Details</h3>
<p>If <code>msg.payload</code> is a number the details of that device or group will be retrieved and returned in msg.payload as an object. If <code>msg.payload</code> is not set or is not a number the complete structure will be retrieved from the defined IKEA Hub.</p>
<p>The <code>Config</code> setting requires a number of parameters:</p>
<ul>
<li><code>Security Code</code> - As found on the sticker on the bottom of the IKEA TRÅDFRI hub</li>
<li><code>IP address</code> - The IP address of the IKEA TRÅDFRI hub</li>
<li><code>CoAP path</code> - The file system path to the CoAP library that communicates with the IKEA TRÅDFRI hub. A set of precompiled versions of libcoap is available to choose from. If they don’t work or you want to be sure to use the latest version, please follow the instructions to compile from the Github repo of libcoap.</li>
</ul>
<p>Look at <a href="https://github.com/nidayand/node-tradfri-argon">node-tradfri-argon</a> on instructions on how to get or compile the libcoap library. The executable will be in the [examples] directory when it has finished the compilation. E.g. <code>/home/pi/libcoap/examples/coap-client</code></p>
</script>
<script type="text/javascript">
RED.nodes.registerType('tradfri-out', {
category: 'output',
defaults: {
name: {value: ""},
dtype: {value: "device"},
tradfri_id: {value: "0", required:true},
hub : {type: "tradfri-config", required: true},
output: {value: false, required:true }
},
color: "#E9967A",
icon: "light.png",
align: 'right',
inputs: 1,
outputs: 0,
label : function(){ return this.name || "tradfri"},
oneditsave: function(){
$('#node-input-name').val($('#node-input-tradfri_id option:selected').text());
$('#node-input-dtype').val($('#node-input-tradfri_id option:selected').attr('dtype'));
var node=this;
if ($("#node-input-output").is(":checked")){
node.outputs=1;
} else {
node.outputs=0;
}
},
oneditprepare: function(){
//Register help
var setHover = function(obj, txt){
obj.hover(function(){
$('#tradfri-help').html(txt);
},function(){
$('#tradfri-help').html('');
});
}
setHover($('#tradfri-custom-refresh'),'Click to refresh list of groups and devices from the IKEA hub');
setHover($('#node-input-hub'),'Select IKEA hub configuration to use');
setHover($('#node-input-tradfri_id'),'Select the light to control or choose [By msg.tradfri_id] to define the light to control via the inbound message');
setHover($('#node-input-output'),'Enable output to get the result from the request in msg.payload in boolean');
// Show name and id in select box
if (parseInt(this.tradfri_id) !== 0){
$('#node-input-tradfri_id').append($('<option>', {
value: this.tradfri_id,
text : this.name,
dtype : this.dtype
}));
}
//Select the saved value
$('#node-input-tradfri_id').val(this.tradfri_id);
// Register onchange to clear the list if config is changed
$('#node-input-config').on('change',function(){
$('#node-input-tradfri_id').find('option').remove();
$('#node-input-tradfri_id').append('<option value="0">By msg.tradfri_id</option>');
});
// Register onclick to get data
$('#tradfri-custom-refresh').click(function(){
// Get the selected configuration
if ($('#node-input-hub option:selected').val() === "_ADD_"){
alert("Please select a hub configuration");
return;
}
//Start spinner
$('#tradfri-spinner').addClass('fa-pulse');
// Get the config node
var c = RED.nodes.node($('#node-input-hub option:selected').val());
// Try to connect to hub to get device and group data
$.get('/tradfri/controls',{sid:c.sid, coap:c.coap, hubip:c.hubip, identity:c.identity, preshared_key:c.preshared_key}, function(data){
// Stop spinner
$('#tradfri-spinner').removeClass('fa-pulse');
if (data.status === "ok"){
//Store the currently selected id and name
var tradfri_id = $('#node-input-tradfri_id option:selected').val();
var name = $('#node-input-tradfri_id option:selected').text();
// Remove old options
$('#node-input-tradfri_id').find('option').remove();
$('#node-input-tradfri_id').append('<option value="0">By msg.tradfri_id</option>');
data.items.forEach(function(item){
$('#node-input-tradfri_id').append($('<option>', {
value: item.id,
text : item.name + (item.type==='group'? ' (group)':''),
dtype: item.type
}));
});
$('#node-input-tradfri_id').val(tradfri_id);
//Indicate update
$("#node-input-tradfri_id").fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
} else {
alert('Please verify your hub configuration settings or try again!');
}
});
});
}
});
</script>
<script type="text/x-red" data-template-name="tradfri-out">
<div class="form-row" style="display:none">
<input type="text" id="node-input-name"><input type="text" id="node-input-dtype">
</div>
<div class="form-row">
<label for="node-input-config"><i class="fa fa-bookmark"></i> Config</label>
<input type="text" id="node-input-hub">
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-output" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-output" style="width: 70%;">Use output</label>
</div>
<div class="form-row">
<label for="node-input-tradfri_id"><i class="icon-tag"></i> Device</label>
<select id="node-input-tradfri_id" style="smargin-right:5px;">
<option value="0">By msg.tradfri_id</option>
</select>
<a id="tradfri-custom-refresh" class="editor-button" style=""><i id="tradfri-spinner" class="fa fa-refresh"></i></a>
</div>
<div class="form-row">
<i style="font-size:95%;color:#E9967A"><span id="tradfri-help"></span></i>
</div>
</script>
<script type="text/x-red" data-help-name="tradfri-out">
<p>Set state (on/off/brightness) on an IKEA TRÅDFRI device or group</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">object | string</span>
</dt>
<dd>either a string with `on` or `off` or an object describing the action</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<li>Standard output
<dl class="message-properties">
<dt>payload <span class="property-type">boolean | optional</span></dt>
<dd>if output is enabled success or failure is described by true or false</dd>
</dl>
</li>
</ol>
<h3>Details</h3>
<p>If <code>msg.payload</code> is a a string (on/off) a device or group must have been defined in the node. If <code>msg.payload</code> is an object any information will override defined attributes in the node. <br>
<strong>The following attributes of the object are available:</strong> <br>
- <code>tradfri_id</code> (Integer) - optional - Not needed if target has been specified in the node <br>
- <code>type</code> (String) - optional - [‘group’, ‘device’] - specifies if the call is for a group or device. Not needed if target has been specified in the node <br>
- <code>state</code> (String) - [‘on’, ‘off’, ‘toggle’] - specifies the state instruction <br>
- <code>brightness</code> (Integer) - [0-255] - specifies the brightness of the instruction <br>
- <code>mired</code> (Integer) - [40-600] for RGB bulbs, [250-454] for white spectrum bulbs - specifies the color temperature of the instruction <br>
- <code>color</code> (String) - [hex color value, ‘cool’, ‘normal’, ‘warm’] - color code (HEX - e.g. ‘ffffff’ or cool/normal/warm)<br>
- <code>transitiontime</code> (Integer) - time in seconds to transition from one state to another</p>
<p><strong>Examples:</strong> <code>msg.payload = 'on';</code> or <code>msg.payload = { tradfri_id: 1234, type:'device', state: 'on', color: 'cool'}</code></p>
<p>The <code>Config</code> setting requires a number of parameters:</p>
<ul>
<li><code>Security Code</code> - As found on the sticker on the bottom of the IKEA TRÅDFRI hub</li>
<li><code>IP address</code> - The IP address of the IKEA TRÅDFRI hub</li>
<li><code>CoAP path</code> - The file system path to the CoAP library that communicates with the IKEA TRÅDFRI hub. A set of precompiled versions of libcoap is available to choose from. If they don’t work or you want to be sure to use the latest version, please follow the instructions to compile from the Github repo of libcoap.</li>
</ul>
<p>Look at <a href="https://github.com/nidayand/node-tradfri-argon">node-tradfri-argon</a> on instructions on how to get or compile the libcoap library. The executable will be in the [examples] directory when it has finished the compilation. E.g. <code>/home/pi/libcoap/examples/coap-client</code></p>
</script>
<script type="text/javascript">
RED.nodes.registerType('tradfri-config', {
category: 'config',
defaults: {
name: {value: ""},
hubip : {value: "", required:true},
sid: {value:"", required:true},
coap: {value:"", required:true},
identity: {value:"", required:true},
preshared_key: {value:"", required:false}
},
label: function () {
return this.name || "IKEA Hub";
},
oneditprepare : function(){
var setHover = function(obj, txt){
obj.hover(function(){
$('#tradfri-help').html(txt);
},function(){
$('#tradfri-help').html('');
});
}
setHover($('#node-config-input-hubip'),'The IP address of the IKEA TRÅDFRI hub');
setHover($('#node-config-input-sid'),'The Security code as found on a sticker under the IKEA TRÅDFRI hub');
setHover($('#node-config-input-coap'),'A C program, libcoap, is used to talk to the IKEA TRÅDFRI hub and the path to program should be specified here. In the node-tradfri-argon node module are compilations included that could be used here (in the lib folder e.g. named coap-client-raspbian). If not visit <a href="https://github.com/nidayand/node-tradfri-argon" target=_blank>node-tradfri-argon</a> Github repo to get instructions on how to compile the library (for OSX and Linux)');
setHover($('#node-config-input-identity'),'A string that identifies the app. Once entered a registration can only happen once and the preshared key returned by the hub must be saved for future reference');
setHover($('#node-config-input-preshared_key'),'After a registration is made on a new App Identity the value needs to be stored here. If not empty the Register button will be disabled');
$('#node-config-button').on("click", function(){
var tsid = $('#node-config-input-sid').val(),
tcoap = $('#node-config-input-coap').val(),
thubip=$('#node-config-input-hubip').val(),
tidentity = $('#node-config-input-identity').val();
var vtst = function(val){
return val.trim().length>0;
};
if (vtst(tsid) && vtst(tcoap) && vtst(thubip)){
$.get('/tradfri/register',{sid: tsid, coap:tcoap, hubip:thubip}, function(data){
if (data.status == 'ok'){
$('#node-config-input-preshared_key').val(data.preshared_key);
$('#node-config-input-identity').val(data.identity);
alert("New preshared key was received and the field has been updated")
} else {
alert("Registration did not work. Verify your settings and possibly try another identity. Note that an identity can only be used once and the response preshared key must be stored for usage with the identity");
}
});
} else {
alert("Enter all mandatory information prior to requesting a preshared key!");
}
});
$.get('/tradfri/libs', function(data){
data.forEach(function(item){
var t = $("<a href='#'>"+item.file+"</a>");
t.on('click', function(){
$("#node-config-input-coap").val(item.path);
});
$("#tradfri-libs-prebuilt").append(t).append(" ");
});
$("#tradfri-libs-prebuilt").append(" or build yourself. See libcoap compilation instructions at <a href='https://github.com/nidayand/node-tradfri-argon' target=_blank>node-tradfri-argon</a>");
});
}
});
</script>
<script type="text/x-red" data-template-name="tradfri-config">
<div class="form-row" style="width:500px">
<p></p>
</div>
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-config-input-hubip"><i class="fa fa-tag"></i> Hub IP address</label>
<input type="text" id="node-config-input-hubip" placeholder="Hub IP address">
</div>
<div class="form-row">
<label for="node-config-input-sid"><i class="fa fa-tag"></i> Security Code</label>
<input type="text" id="node-config-input-sid" placeholder="As found on IKEA hub device">
</div>
<div class="form-row">
<label for="node-config-input-coap"><i class="fa fa-tag"></i> CoAP path</label>
<input type="text" id="node-config-input-coap" placeholder="CoAP path">
<div>
Click to select path to pre-built library: <span id="tradfri-libs-prebuilt"></span>
</div>
</div>
<div class="form-row">
<button type="button" id="node-config-button">Get Preshared key on App Identity</button>
</div>
<div class="form-row">
<label for="node-config-input-identity"><i class="fa fa-tag"></i> App Identity</label>
<input type="text" id="node-config-input-identity" placeholder="Custom value that is identifying the connection">
</div>
<div class="form-row">
<label for="node-config-input-preshared_key"><i class="fa fa-tag"></i> Preshared key</label>
<input type="text" id="node-config-input-preshared_key" placeholder="Generated by the hub post registration of identity">
</div>
<div class="form-row">
<i style="font-size:95%;color:#E9967A"><span id="tradfri-help"></span></i>
</div>
</script>