forked from HealsCodes/node-red-contrib-homegear-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homegear-mqtt.html
399 lines (355 loc) · 14.6 KB
/
homegear-mqtt.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!-- Copyright (c) 2015 René Köcher
*- See the file LICENSE for copying permission.
-->
<!-- start of homegear-mqtt in -->
<script type="text/javascript">
RED.nodes.registerType('homegear-mqtt in',{
category: 'input',
color: '#c0deed',
defaults: {
broker: {type:'mqtt-broker', required:true},
homegearId: {value:'1234-5678-9abc', required:true},
peerId: {value:1, required:true, validate:RED.validators.number()},
deviceType: {required:true},
publishUpdates: {value:true, required:true},
publishComplete: {value:true, required:true},
name: {value:''},
genName: {value:''}
},
inputs: 0,
outputs: 1,
icon: 'homegear-mqtt.png',
label: function() {
return this.name || this.genName || 'homegear-mqtt in';
},
labelStyle: function() {
return this.name ? 'node_label_italic' : '';
},
paletteLabel: 'homegear',
oneditprepare: function() {
$('#node-input-deviceType').hide();
$.getScript('homegear-mqtt/families/homematic')
.done(function(data, textStatus, jqxhr) {
$('#node-input-deviceTypeList option').remove();
var deviceTypes = JSON.parse(data);
var deviceOpts = [];
var selected = $('#node-input-deviceType').val();
for(var i in deviceTypes) {
for(var j in deviceTypes[i].names) {
var name = deviceTypes[i].names[j];
var value = i.toString() + ':' + j.toString();
deviceOpts.push([name, value]);
}
}
deviceOpts.sort();
for(i = 0; i < deviceOpts.length; ++i) {
var opt = $('<option></option>').val(deviceOpts[i][1])
.text(deviceOpts[i][0]);
opt.appendTo('#node-input-deviceTypeList');
if(selected === opt.val()) {
opt.attr('selected', 'selected');
}
}
$('#node-input-deviceTypeList').change(function() {
var value = $('#node-input-deviceTypeList option:selected').val();
var name = $('#node-input-deviceTypeList option:selected').text();
$('#node-input-deviceType').val(value);
}).show();
});
},
oneditsave: function() {
var name = $('#node-input-name').val();
var peer = $('#node-input-peerId').val();
var homegear = $('#node-input-homegearId').val() || '1234-5678-9abc';
var genName = homegear + '.' + peer + '.' + $('#node-input-deviceTypeList option:selected').text();
$('#node-input-genName').val((name === '') ? genName : '');
}
});
</script>
<script type="text/x-red" data-template-name="homegear-mqtt in">
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-globe"></i><span data-i18n="homegear-mqtt.label.broker"></span></label>
<input type="text" id="node-input-broker">
</div>
<div class="form-row">
<label for="node-input-homegearId"><i class="fa fa-tag"></i><span data-i18n="homegear-mqtt.label.homegearId"></span></label>
<input type="text" id="node-input-homegearId" placeholder="1234-5678-9abc">
</div>
<div class="form-row">
<label for="node-input-peerId"><i class="fa fa-tag"></i><span data-i18n="homegear-mqtt.label.peerId"></span></label>
<input type="text" id="node-input-peerId" placeholder="1">
</div>
<div class="form-row">
<label for="node-input-deviceType"><i class="fa fa-tasks"></i><span data-i18n="homegear-mqtt.label.deviceType"></span></label>
<select id="node-input-deviceTypeList"></select>
<input type="text" id="node-input-deviceType" style="display: none;">
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-publishUpdates" style="display: inline-block; width: auto; vertial-align: top;">
<label for="node-input-publishUpdates" style="width: 70%;"><span data-i18n="homegear-mqtt.label.publishUpdates"></span></label>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-publishComplete" style="display: inline-block; width: auto; vertial-align: top;">
<label for="node-input-publishComplete" style="width: 70%;"><span data-i18n="homegear-mqtt.label.publishComplete"></span></label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i><span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
<input type="text" id="node-input-genName" style="display: none;">
</div>
</script>
<script type="text/x-red" data-help-name="homegear-mqtt in">
<p>Collects data from <b>one device</b> connected to the configured Homegear server.</p>
<p>
The device is identified by it's <i><b>peerId</b></i> which an be queried from Homegear
using the <i>"peers list"</i> command or any suitable web interface.
</p>
<p>
On first connect the node will request all current device values and collect and publish
them. From this point on updates will be based on the received change events.
</p>
<p>
This node will subscribe to events via <b>mqtt</b>.
Make sure you're using Homegear <b>0.6 or later</b> with <i>"enabled = true"</i> set
inside your <i>/etc/homegear/mqtt.conf</i>.
</p>
<p>
(This file is also the location of the <b>homegearId</b> parameter)
</p>
</script>
<!-- end of homegear-mqtt in -->
<!-- start of homegear-mqtt out -->
<script type="text/javascript">
var homegear = null;
RED.nodes.registerType('homegear-mqtt out', {
category: 'output',
color: '#c0deed',
defaults: {
broker: {type:'mqtt-broker', required:true},
homegearId: {value:'1234-5678-9abc', required:true},
peerId: {value:1, required:true, validate:RED.validators.number()},
deviceType: {required:true},
paramName: {required:true},
paramValue: {value:''},
publishResult: {value:false},
name: {value:''},
genName: {value:''},
outputs: {value:0}
},
inputs: 1,
outputs: 0,
icon: 'homegear-mqtt.png',
align: 'right',
label: function() {
return this.name || this.genName || 'homegear-mqtt out';
},
labelStyle: function() {
return this.name ? 'node_label_italic' : '';
},
paletteLabel: 'homegear',
oneditprepare: function() {
$('#node-input-paramName').hide();
$('#node-input-paramNameList').hide();
$('#node-input-paramValue').hide();
$('#node-input-paramValueList').hide();
$('#node-input-deviceType').hide();
$.getScript('homegear-mqtt/common')
.done(function(data, textStatus, jqxhr) {
$.getScript('homegear-mqtt/families/homematic')
.done(function(data, textStatus, jqxhr) {
$('#node-input-deviceTypeList option').remove();
var deviceTypes = JSON.parse(data);
var deviceOpts = [];
var selected = $('#node-input-deviceType').val();
for(var i in deviceTypes) {
for(var j in deviceTypes[i].names) {
var name = deviceTypes[i].names[j];
var value = i.toString() + ':' + j.toString();
deviceOpts.push([name, value]);
}
}
deviceOpts.sort();
for(i = 0; i < deviceOpts.length; ++i) {
var opt = $('<option></option>').val(deviceOpts[i][1])
.text(deviceOpts[i][0]);
opt.appendTo('#node-input-deviceTypeList');
if(selected === opt.val()) {
opt.attr('selected', 'selected');
}
}
$('#node-input-deviceTypeList').change(function() {
var value = $('#node-input-deviceTypeList option:selected').val();
var name = $('#node-input-deviceTypeList option:selected').text();
$('#node-input-deviceType').val(value);
updateDeviceParamLists(deviceTypes[Number(value.split(':')[0])]);
}).show();
updateDeviceParamLists(deviceTypes[Number((selected || '0:0').split(':')[0])]);
});
});
function updateDeviceParamLists(device) {
/* update paramValue / paramValueList */
var params = homegear.writeableParamsForDevice(device);
$('#node-input-paramNameList option').remove();
params.forEach(function(param){
var opt = $('<option></option>').val(param.name).text(param.name);
opt.appendTo('#node-input-paramNameList');
if($('#node-input-paramName').val() === opt.val()){
opt.attr('selected', 'selected');
updateDeviceParamValueList(device);
}
});
$('#node-input-paramNameList').change(function(){
var selected = $('#node-input-paramNameList option:selected').val();
$('#node-input-paramName').val(selected);
updateDeviceParamValueList(device, selected);
}).show();
var selected = $('#node-input-paramNameList option:selected').val();
updateDeviceParamValueList(device, selected);
}
function updateDeviceParamValueList(device, name) {
/* update possible choinces for paramValue */
var params = homegear.writeableParamsForDevice(device);
$('#node-input-paramActionNotice').hide();
$('#node-input-paramValueList option').remove();
$('#node-input-paramValueList').hide();
$('#node-input-paramValueRow').hide();
$('#node-input-paramValue').hide();
params.forEach(function(param){
if(param.name === name) {
switch(param.type) {
case 'float':
case 'string':
case 'integer':
$('#node-input-paramValueRow').show();
$('#node-input-paramValue').show();
break;
case 'action':
$('#node-input-paramValue').val('1');
$('#node-input-paramActionNotice').show();
break;
case 'boolean':
var select = Number($('#node-input-paramValue').val()) || 0;
$('<option></option>').val(0).text('false')
.appendTo('#node-input-paramValueList');
$('<option></option>').val(1).text('true')
.appendTo('#node-input-paramValueList');
$('#node-input-paramValueList').show();
$('#node-input-paramValueRow').show();
break;
case 'option':
if(param.mapping === undefined) {
$('#node-input-paramValue').show();
} else {
for(var i = 0; i < param.mapping.length; ++i) {
var opt = $('<option></option>').val(i).text(param.mapping[i]);
if(Number($('#node-input-paramValue').val()) == i) {
opt.attr('selected', 'selected');
}
opt.appendTo('#node-input-paramValueList');
}
$('#node-input-paramValueList').show();
}
$('#node-input-paramValueRow').show();
break;
}
}
});
$('#node-input-paramValueList').change(function(){
var value = $('#node-input-paramValueList option:selected').val();
$('#node-input-paramValue').val(value);
});
}
},
oneditsave: function() {
$('#node-input-paramName').val($('#node-input-paramNameList option:selected').val());
var name = $('#node-input-name').val();
var peer = $('#node-input-peerId').val();
var homegear = $('#node-input-homegearId').val() || '1234-5678-9abc';
var device = $('#node-input-deviceTypeList option:selected').text();
var param = $('#node-input-paramName').val();
var value = $('#node-input-paramValue').val();
var genName = homegear + '.' + peer + '.' + device + '.' + param + (value ? '.' : '') + value;
$('#node-input-genName').val((name === '') ? genName : '');
if($('#node-input-publishResult').prop('checked') === true) {
$('#node-input-outputs').val(true);
this.outputs = 1;
} else {
$('#node-input-outputs').val(false);
this.outputs = 0;
}
}
});
</script>
<script type="text/x-red" data-template-name="homegear-mqtt out">
<div class="form-row">
<label for="node-input-broker"><i class="fa fa-globe"></i><span data-i18n="homegear-mqtt.label.broker"></span></label>
<input type="text" id="node-input-broker">
</div>
<div class="form-row">
<label for="node-input-homegearId"><i class="fa fa-tag"></i><span data-i18n="homegear-mqtt.label.homegearId"></span></label>
<input type="text" id="node-input-homegearId" placeholder="1234-5678-9abc">
</div>
<div class="form-row">
<label for="node-input-peerId"><i class="fa fa-tag"></i><span data-i18n="homegear-mqtt.label.peerId"></span></label>
<input type="text" id="node-input-peerId" placeholder="1">
</div>
<div class="form-row">
<label for="node-input-deviceType"><i class="fa fa-tasks"></i><span data-i18n="homegear-mqtt.label.deviceType"></span></label>
<select id="node-input-deviceTypeList"></select>
<input type="text" id="node-input-deviceType" style="display: none;">
</div>
<div class="form-row">
<label for="node-input-paramName"><i class="fa fa-wrench"></i><span data-i18n="homegear-mqtt.label.paramName"></span></label>
<select id="node-input-paramNameList"></select>
<input type="text" id="node-input-paramName" style="display: none;">
</div>
<div class="form-row" id="node-input-paramValueRow">
<label for="node-input-paramValue"><i class="fa fa-pencil"></i><span data-i18n="homegear-mqtt.label.paramValue"></span></label>
<select id="node-input-paramValueList" style="display: none;"></select>
<input type="text" id="node-input-paramValue" data-i18n="[placeholder]homegear-mqtt.placeholder.paramValue" style="display: none;">
</div>
<div class="form-row" id="node-input-paramActionNotice" style="display: none">
<i><span data-i18n="homegear-mqtt.label.paramActionNotice" style="display: block; width: auto; text-align: center;"></span></i>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-publishResult" style="display: inline-block; width: auto; vertial-align: top;">
<label for="node-input-publishResult" style="width: 70%;"><span data-i18n="homegear-mqtt.label.publishResult"></span></label>
<input type="text" id="node-input-outputs" style="display: none;">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i><span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
<input type="text" id="node-input-genName" style="display: none;">
</div>
</script>
<script type="text/x-red" data-help-name="homegear-mqtt out">
<p>Sends change requests for <b>one value</b> of <b>one device</b> connected to the configured Homegear server.</p>
<p>
Use this node to change values of the configured device.</br>
By default it will send a <i>setValue</i> RPC request for the configured variable / value pair.
</p>
<p>
<ul>
<li>if <b>msg.value</b> is specified it will be used as the new value</li>
<li>if <b>msg.name</b> is specified it will be used as the parameter name</li>
</ul>
</p>
<p>
The device is identified by it's <i><b>peerId</b></i> which an be queried from Homegear
using the <i>"peers list"</i> command or by using any suitable web interface.</br>
</p>
<p>
This node will subscribe to events via <b>mqtt</b>.
Make sure you're using Homegear <b>0.6 or later</b> with <i>"enabled = true"</i> set
inside your <i>/etc/homegear/mqtt.conf</i>.
</p>
<p>
(This file is also the location of the <b>homegearId</b> parameter)
</p>
</script>
<!-- end of homegear-mqtt out -->
<!-- vim: ts=4 sw=4 sts=4 noet :
-->