-
Notifications
You must be signed in to change notification settings - Fork 5
/
J_WeMo1_UI7.js
483 lines (463 loc) · 15.5 KB
/
J_WeMo1_UI7.js
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Plugin for Belkin WeMo
* Copyright (C) 2009-2011 Deborah Pickett
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**********
*
* Configuration tab
*
**********/
/*
* Replaces prototype string.escapeHTML
*/
var WeMo = (function(api)
{
// unique identifier for this plugin...
var uuid = 'E451565B-B468-4E9E-8981-30DB4FD16F70';
var myModule = {};
var device = api.getCpanelDeviceId();
function onBeforeCpanelClose(args)
{
// do some cleanup...
console.log('handler for before cpanel close');
}
function init()
{
// register to events...
api.registerEventHandler('on_ui_cpanel_before_close', myModule,
'onBeforeCpanelClose');
}
function wemoEscapeHtml(string)
{
if(typeof string === 'boolean') {
string = string.toString();
}
if(typeof string !== 'string') {
throw new Error('only string parameter supported!');
}
return string.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function ShowStatus(text, error)
{
var html = '';
html =
'<input type="button" value="Reload Luup" onClick="WeMo.doReload()"/>';
if (!error)
{
document.getElementById("wemo_saveChanges_text")
.style.backgroundColor = "#00A652";
document.getElementById("wemo_saveChanges_text")
.innerHTML = text;
document.getElementById("wemo_saveChanges_button")
.style.backgroundColor = "#00A652";
document.getElementById("wemo_saveChanges_button")
.innerHTML = html;
}
else
{
document.getElementById("wemo_saveChanges_text")
.style.backgroundColor = "#FF9090";
document.getElementById("wemo_saveChanges_text")
.innerHTML = text;
document.getElementById("wemo_saveChanges_button")
.style.backgroundColor = "#FF9090";
document.getElementById("wemo_saveChanges_button")
.innerHTML = html;
}
}
function setDeviceStateVariable(DEVICE, SID, VARIABLE, VALUE, TRASH)
{
api.setDeviceStatePersistent(DEVICE, SID, VARIABLE, VALUE,
{
'onSuccess': function()
{
ShowStatus('Data updated, Reload LuuP Engine to commit changes.',
false);
},
'onFailure': function()
{
ShowStatus(
'Failed to update data, Reload LuuP Engine and try again.',
true);
}
});
}
// Remove an existing device.
function configurationRemoveChildDevice(device, index, button)
{
var btn = jQuery(button);
btn.attr('disabled', 'disabled');
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1", "Child" +
index + "Type", "", 0);
btn.val("Removed");
jQuery('#wemo_saveChanges')
.show();
}
function setEnableMulticast(device, button)
{
var btn = jQuery(button);
var newState = btn.is(':checked');
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1",
"EnableMulticast", newState ? "1" : "0", 0);
if (!btn.is(':checked'))
{
jQuery('#wemo_scanResults')
.hide();
}
jQuery('#wemo_saveChanges')
.show();
}
function setManualSubscription(device, button)
{
var btn = jQuery(button);
var newState = btn.is(':checked');
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1",
"Report", newState ? "1" : "0", 0);
jQuery('#wemo_saveChanges');
}
function addManualRow(device, node)
{
var html = '<p>';
html += 'Name <input type="text" class="wemo_name" size="16"/> ';
html += 'Type <select class="wemo_type">;';
html += '<option value="urn:Belkin:device:controllee:1">Appliance Switch</option>;';
html += '<option value="urn:Belkin:device:sensor:1">Sensor</option>;';
html += '<option value="urn:Belkin:device:lightswitch:1">Light Switch</option>;';
html += '<option value="urn:Belkin:device:insight:1">Insight Switch</option></select> ';
html += 'IP Address <input type="text" class="wemo_host" size="15"/> ';
html += '<input type="button" value="Add Static" onClick="WeMo.configurationAddManualDevice(' + device + ',this)"/>';
html += '</p>';
jQuery('#' + node)
.append(html);
}
// Add a found or manual device.
function configurationAddDevice(device, name, type, usn, host)
{
var deviceCount = api.getDeviceState(device,
"urn:futzle-com:serviceId:WeMo1", "ChildCount", 0) - 0;
deviceCount++;
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1", "Child" +
deviceCount + "Name", name || "");
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1", "Child" +
deviceCount + "Type", type || "");
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1", "Child" +
deviceCount + "USN", usn || "");
if (host != undefined)
{
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1", "Child" +
deviceCount + "Host", host);
}
setDeviceStateVariable(device, "urn:futzle-com:serviceId:WeMo1",
"ChildCount", deviceCount);
}
// Add a found device.
function configurationAddFoundDevice(device, index, button, isStatic)
{
var btn = jQuery(button);
btn.parent('input')
.attr('disabled', 'disabled');
var unknownDeviceName = api.getDeviceState(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + index + "Name", 0);
var unknownDeviceType = api.getDeviceState(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + index + "Type", 0);
var unknownDeviceUSN = api.getDeviceState(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + index + "USN", 0);
var unknownDeviceHost = api.getDeviceState(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + index + "Host", 0);
if (isStatic)
{
configurationAddDevice(device, unknownDeviceName, unknownDeviceType,
unknownDeviceUSN, unknownDeviceHost);
}
else
{
configurationAddDevice(device, unknownDeviceName, unknownDeviceType,
unknownDeviceUSN, undefined);
}
btn.val("Added");
jQuery('#wemo_saveChanges')
.show();
}
// Add a manual device.
function configurationAddManualDevice(device, button)
{
var btn = jQuery(button);
var name = btn.parent()
.find('.wemo_name')
.val();
var type = btn.parent()
.find('.wemo_type')
.val();
var host = btn.parent()
.find('.wemo_host')
.val();
if (name === "")
{
name = "Manual WeMo device";
}
if (host === "")
{
return;
}
btn.attr('disabled', 'disabled');
configurationAddDevice(device, name, type, "", host);
btn.val("Added");
jQuery('#wemo_saveChanges')
.show();
addManualRow(device, 'wemo_addManual');
}
function doReload(device)
{
var requestURL = data_request_url + 'id=lu_action';
requestURL +=
'&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1×tamp=' +
new Date()
.getTime() + '&action=Reload';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", requestURL, false);
xmlHttp.send(null);
}
function configuration(device)
{
try
{
var html = '';
html +=
'<table width="100%" style="border-collapse: collapse"><tbody><tr><td id="wemo_saveChanges_text" style= "font-weight: bold; text-align: center;"></td><td id="wemo_saveChanges_button" style= "text-align: center;"></td></tr></tbody>';
html +=
'</table>';
// List known child devices, with option to delete them.
var childDevices = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "ChildCount",
{
'dynamic': false
}) - 0;
var actualChildDevices = 0;
var childHtml = '';
var dynamicCount = 0;
childHtml +=
'<div style="border: black 1px solid; padding: 5px; margin: 5px;">';
childHtml +=
'<div style="font-weight: bold; text-align: center;">Existing WeMo devices</div>';
childHtml +=
'<table width="100%"><thead><th>Name</th><th>Type</th><th>IP Address</th><th>Room</th><th>Action</th></thead>';
var i;
for (i = 1; i <= childDevices; i++)
{
// Find the child in the device list (requires exhaustive search).
var childDeviceType = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "Child" + i + "Type",
{
'dynamic': false
}) || "";
if (childDeviceType === "")
{
continue;
}
var childDeviceHost = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "Child" + i + "Host",
{
'dynamic': false
});
var childDeviceUSN = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "Child" + i + "USN",
{
'dynamic': false
});
var childRoom;
var checkDevice;
var childFound = false;
for (checkDevice in jsonp.ud.devices)
{
if (jsonp.ud.devices[checkDevice].id_parent === device &&
(jsonp.ud.devices[checkDevice].altid === childDeviceUSN ||
jsonp.ud.devices[checkDevice].altid === childDeviceHost))
{
childName = jsonp.ud.devices[checkDevice].name;
var childRoomId = jsonp.ud.devices[checkDevice].room;
if (childRoomId === "0")
{
// Room 0 is unassigned, would break get_room_by_id().
childRoom = "Unassigned";
}
else
{
childRoom = api.getRoomObject(childRoomId)
.name;
}
childFound = true;
}
}
if (!childFound)
{
continue;
}
childHtml += '<tr>';
childHtml += '<td>' + wemoEscapeHtml(childName) + '</td>';
childHtml += '<td>' + (childDeviceType === "urn:Belkin:device:sensor:1" ? "Sensor" :
childDeviceType === "urn:Belkin:device:controllee:1" ? "Appliance Switch" :
childDeviceType === "urn:Belkin:device:insight:1" ? "Insight Switch" :
childDeviceType === "urn:Belkin:device:lightswitch:1" ? "Light Switch" :
wemoEscapeHtml(childDeviceType)) + '</td>';
if (childDeviceHost === undefined)
{
childDeviceHost = "Dynamic";
dynamicCount++;
}
childHtml += '<td>' + wemoEscapeHtml(childDeviceHost) + '</td>';
childHtml += '<td>' + wemoEscapeHtml(childRoom) + '</td>';
childHtml +=
'<td><input type="button" value="Remove" onClick="WeMo.configurationRemoveChildDevice(' +
device + ',' + i + ',this)"/></td>';
childHtml += '</tr>';
actualChildDevices++;
}
childHtml += '</table>';
childHtml += '</div>';
if (actualChildDevices)
{
html += childHtml;
}
// Scan for WeMo devices on the network. Requires Multicast to be enabled.
var enableMulticast = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "EnableMulticast",
{
'dynamic': false
});
if (enableMulticast === undefined)
{
enableMulticast = "1";
}
html +=
'<div style="border: black 1px solid; padding: 5px; margin: 5px;">';
html +=
'<div style="font-weight: bold; text-align: center;">Scan for WeMo devices</div>';
html += '<p><input type="checkbox"' +
(enableMulticast === "1" ? ' checked="checked"' : '') +
(dynamicCount ? ' disabled="disabled"' : '') +
' onclick="WeMo.setEnableMulticast(' + device +
', this)"> Enable scan for WeMo devices on LAN</p>';
// List unknown devices as candidates to add.
if (enableMulticast === "1")
{
var unknownDevices = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDeviceCount",
{
'dynamic': false
}) - 0;
html +=
'<table id="wemo_scanResults" width="100%"><thead><th>Name (Serial number)</th><th>Type</th><th>IP Address</th><th>Action</th></thead>';
var i;
for (i = 1; i <= unknownDevices; i++)
{
html += '<tr>';
var unknownDeviceName = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + i + "Name",
{
'dynamic': false
});
html += '<td>' + wemoEscapeHtml(unknownDeviceName) + '</td>';
var unknownDeviceType = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + i + "Type",
{
'dynamic': false
});
html += '<td>' + (unknownDeviceType === "urn:Belkin:device:sensor:1" ? "Sensor" :
unknownDeviceType === "urn:Belkin:device:controllee:1" ? "Appliance Switch" :
unknownDeviceType === "urn:Belkin:device:insight:1" ? "Insight Switch" :
unknownDeviceType === "urn:Belkin:device:lightswitch:1" ? "Light Switch" :
wemoEscapeHtml(unknownDeviceType)) + '</td>';
var unknownDeviceAddress = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "UnknownDevice" + i + "Host",
{
'dynamic': false
});
html += '<td>' + wemoEscapeHtml(unknownDeviceAddress) + '</td>';
html +=
'<td><input type="button" value="Add Dynamic" onClick="WeMo.configurationAddFoundDevice(' +
device + ',' + i + ',this,false)"/> ';
html +=
'<input type="button" value="Add Static" onClick="WeMo.configurationAddFoundDevice(' +
device + ',' + i + ',this,true)"/></td>';
html += '</tr>';
}
html += '</table>';
}
html += '</div>';
// Allow manual adding of device at static address.
html +=
'<div id="wemo_addManual" style="border: black 1px solid; padding: 5px; margin: 5px;">';
html +=
'<div style="font-weight: bold; text-align: center;">Manually add WeMo device</div>';
html += '</div>';
// Allow manual subscription.
html +=
'<div id="wemo_enableManual" style="border: black 1px solid; padding: 5px; margin: 5px;">';
html +=
'<div style="font-weight: bold; text-align: center;">Subscribe to Insight parameters for devices</div>';
var enableInsightSubscription = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "Report",
{
'dynamic': false
});
if (enableInsightSubscription === undefined)
{
enableInsightSubscription = "1";
}
html += '<p><input type="checkbox"' +
(enableInsightSubscription === "1" ? ' checked="checked"' : '') +
(dynamicCount ? ' disabled="disabled"' : '') +
' onclick="WeMo.setManualSubscription(' + device +
', this)"> Enable subscription to Insight parameter changes</p>';
html += '</div>';
// Notify user if the UPnP Proxy is not answering.
var proxyApiVersion = api.getDeviceStateVariable(device,
"urn:futzle-com:serviceId:WeMo1", "ProxyApiVersion",
{
'dynamic': false
});
if (proxyApiVersion === undefined || proxyApiVersion === "")
{
html +=
'<div style="margin: 5px;"><p>UPnP Proxy is not running. Instant updates will not happen. More information <a target="_new" href="http://code.mios.com/trac/mios_upnp-event-proxy/">here</a>.</p></div>';
}
else
{
html += '<div style=" margin: 5px;"><p>UPnP Proxy running (API version ' +
wemoEscapeHtml(proxyApiVersion) + ')</p></div>';
}
api.setCpanelContent(html);
addManualRow(device, 'wemo_addManual');
}
catch (e)
{
Utils.logError('Error in WeMo.configuration(): ' + e);
}
}
myModule = {
uuid: uuid,
init: init,
onBeforeCpanelClose: onBeforeCpanelClose,
configurationAddManualDevice: configurationAddManualDevice,
configurationRemoveChildDevice: configurationRemoveChildDevice,
configurationAddFoundDevice: configurationAddFoundDevice,
setEnableMulticast: setEnableMulticast,
setManualSubscription: setManualSubscription,
doReload: doReload,
configuration: configuration
};
return myModule;
})(api);