1 | //@ sourceURL=J_CPLUS.js |
---|
2 | //------------------------------------------------------------- |
---|
3 | // CPLUS Plugin javascript Tabs |
---|
4 | //------------------------------------------------------------- |
---|
5 | var cplus_Svs = 'urn:upnp-org:serviceId:cplus1'; |
---|
6 | var ip_address = data_request_url; |
---|
7 | |
---|
8 | //------------------------------------------------------------- |
---|
9 | // Utilities Javascript |
---|
10 | //------------------------------------------------------------- |
---|
11 | if (typeof String.prototype.format == 'undefined') { |
---|
12 | String.prototype.format = function() |
---|
13 | { |
---|
14 | var content = this; |
---|
15 | for (var i=0; i < arguments.length; i++) |
---|
16 | { |
---|
17 | var replacement = new RegExp('\\{' + i + '\\}', 'g'); // regex requires \ and assignment into string requires \\, |
---|
18 | // if ($.type(arguments[i]) === "string") |
---|
19 | // arguments[i] = arguments[i].replace(/\$/g,'$'); |
---|
20 | content = content.replace(replacement, arguments[i]); |
---|
21 | } |
---|
22 | return content; |
---|
23 | }; |
---|
24 | }; |
---|
25 | function isFunction(x) { |
---|
26 | return Object.prototype.toString.call(x) == '[object Function]'; |
---|
27 | } |
---|
28 | |
---|
29 | //------------------------------------------------------------- |
---|
30 | // Utilities for searching Vera devices |
---|
31 | //------------------------------------------------------------- |
---|
32 | function findDeviceIdx(deviceID) |
---|
33 | { |
---|
34 | //jsonp.ud.devices |
---|
35 | for(var i=0; i<jsonp.ud.devices.length; i++) { |
---|
36 | if (jsonp.ud.devices[i].id == deviceID) |
---|
37 | return i; |
---|
38 | } |
---|
39 | return null; |
---|
40 | } |
---|
41 | |
---|
42 | function findRootDeviceIdx(deviceID) |
---|
43 | { |
---|
44 | var idx = findDeviceIdx(deviceID); |
---|
45 | while (jsonp.ud.devices[idx].id_parent != 0) |
---|
46 | { |
---|
47 | idx = findDeviceIdx(jsonp.ud.devices[idx].id_parent); |
---|
48 | } |
---|
49 | return idx; |
---|
50 | } |
---|
51 | |
---|
52 | function findRootDevice(deviceID) |
---|
53 | { |
---|
54 | var idx = findRootDeviceIdx(deviceID) ; |
---|
55 | return jsonp.ud.devices[idx].id; |
---|
56 | } |
---|
57 | |
---|
58 | function findDeviceIP(deviceID) |
---|
59 | { |
---|
60 | var idx = findRootDeviceIdx(deviceID) ; |
---|
61 | return jsonp.ud.devices[idx].ip; |
---|
62 | } |
---|
63 | |
---|
64 | //------------------------------------------------------------- |
---|
65 | // Device TAB : Settings |
---|
66 | //------------------------------------------------------------- |
---|
67 | function cplus_sendKeyStream(deviceID,value) |
---|
68 | { |
---|
69 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"SendKey",{keyStream:value}); |
---|
70 | jQuery.get( url ,function( data ) { |
---|
71 | //alert("Success:"+data); |
---|
72 | }) |
---|
73 | .fail(function() { |
---|
74 | alert("Internal failure, could not run the VERA action"); |
---|
75 | }); |
---|
76 | } |
---|
77 | |
---|
78 | //------------------------------------------------------------- |
---|
79 | // Device TAB : Donate |
---|
80 | //------------------------------------------------------------- |
---|
81 | function cplus_Donate(deviceID) { |
---|
82 | var htmlDonate='For those who really like this plugin and feel like it, you can donate what you want here on Paypal. It will not buy you more support not any garantee that this can be maintained or evolve in the future but if you want to show you are happy and would like my kids to transform some of the time I steal from them into some <i>concrete</i> returns, please feel very free ( and absolutely not forced to ) to donate whatever you want. thank you ! '; |
---|
83 | htmlDonate+='<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"><input type="hidden" name="cmd" value="_donations"><input type="hidden" name="business" value="alexis.mermet@free.fr"><input type="hidden" name="lc" value="FR"><input type="hidden" name="item_name" value="Alexis Mermet"><input type="hidden" name="item_number" value="CPLUS"><input type="hidden" name="no_note" value="0"><input type="hidden" name="currency_code" value="EUR"><input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest"><input type="image" src="https://www.paypalobjects.com/en_US/FR/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/fr_FR/i/scr/pixel.gif" width="1" height="1"></form>'; |
---|
84 | var html = '<div>'+htmlDonate+'</div>'; |
---|
85 | set_panel_html(html); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | //------------------------------------------------------------- |
---|
90 | // Device TAB : Settings |
---|
91 | //------------------------------------------------------------- |
---|
92 | function cplus_hideControls( bRegistered ) |
---|
93 | { |
---|
94 | if (bRegistered) { |
---|
95 | jQuery( "hr.cplus_hr" ).show(); |
---|
96 | jQuery( "#cplus_table_registered" ).show(); |
---|
97 | jQuery( "#cplus_UnregisterSmartphone" ).show(); |
---|
98 | jQuery( "#cplus_RegisterSmartphone" ).hide(); |
---|
99 | } |
---|
100 | else { |
---|
101 | jQuery( "hr.cplus_hr" ).hide(); |
---|
102 | jQuery( "#cplus_table_registered" ).hide(); |
---|
103 | jQuery( "#cplus_UnregisterSmartphone" ).hide(); |
---|
104 | jQuery( "#cplus_RegisterSmartphone" ).show(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | function cplus_checkRegistration(deviceID) |
---|
109 | { |
---|
110 | var url = ip_address +'id=lr_CPLUS_Handler&command=isregistered&DeviceNum='+deviceID; |
---|
111 | jQuery.ajax({ |
---|
112 | type: "GET", |
---|
113 | url: url, |
---|
114 | cache: false |
---|
115 | }).done(function(data, textStatus, jqXHR) { |
---|
116 | // then get the new ones |
---|
117 | // text result is a json encoding |
---|
118 | var bRegistered = JSON.parse(data) |
---|
119 | cplus_hideControls( bRegistered ); |
---|
120 | }).fail(function() { |
---|
121 | alert('isregistered Failed!'); |
---|
122 | }); |
---|
123 | } |
---|
124 | |
---|
125 | function cplus_Settings(deviceID) { |
---|
126 | // first determine if it is a child device or not |
---|
127 | //var device = findDeviceIdx(deviceID); |
---|
128 | //var debug = get_device_state(deviceID, cplus_Svs, 'Debug',1); |
---|
129 | //var root = (device!=null) && (jsonp.ud.devices[device].id_parent==0); |
---|
130 | var present = get_device_state(deviceID, cplus_Svs, 'Present',1); |
---|
131 | var pairing = get_device_state(deviceID, cplus_Svs, 'Pairing',1); |
---|
132 | var power = get_device_state(deviceID, cplus_Svs, 'PowerScene',1); |
---|
133 | var ipaddr = findDeviceIP(deviceID); |
---|
134 | |
---|
135 | var htmlipaddr = 'ip(:port) :</td><td><input readonly id="cplus_IpAddress" type="text" size="15" value="'+ipaddr+'" onchange="cplus_SetIpAddress(' + deviceID + ', \'IpxIpAddress\', this.value);"> <button id="cplus_TestRefresh">Test</button>'; |
---|
136 | var style=' <style>\ |
---|
137 | table.cplus_table td:first-child{\ |
---|
138 | background-color: #E0E0E0;\ |
---|
139 | width: 140px;\ |
---|
140 | }\ |
---|
141 | hr.cplus_hr {\ |
---|
142 | border: 0;\ |
---|
143 | color: grey;\ |
---|
144 | margin-top: 5px;\ |
---|
145 | margin-bottom: 5px;\ |
---|
146 | background-color: grey;\ |
---|
147 | height: 1px;\ |
---|
148 | }\ |
---|
149 | </style>'; |
---|
150 | var htmlpairing ='<button id="cplus_RequestPairing">Request Pairing</button>'; |
---|
151 | htmlregister = '<input id="cplus_pairing" type="text" size="5" value="'+pairing+'" onchange="cplus_SetPairing(' + deviceID + ', \'Pairing\', this.value);"> '; |
---|
152 | htmlregister += '<button id="cplus_RegisterSmartphone">Register</button> <button id="cplus_UnregisterSmartphone">Unregister</button>'; |
---|
153 | htmlpower = '<input id="cplus_power" type="text" size="5" value="'+power+'" onchange="cplus_SetPowerScene(' + deviceID + ', \'PowerScene\', this.value);">'; |
---|
154 | htmlstream = '<input id="cplus_keystream" type="text" size="20" value=""> ' + '<button id="cplus_SendKeyStream">Send Keys</button>'; |
---|
155 | htmlkeys = '<div id="cplus_keys"></div>'; |
---|
156 | htmlchannels = '<select id="cplus_channels"></select>'; |
---|
157 | htmltest = '<button id="cplus_Test">Test</button>'; |
---|
158 | var html = |
---|
159 | style+ |
---|
160 | '<div class="pane" id="pane"> '+ |
---|
161 | '<table class="cplus_table" id="cplus_table">'+ |
---|
162 | '<tr><td>'+htmlipaddr+'</td></tr>' + |
---|
163 | '<tr><td>Pairing</td><td>'+htmlpairing+'</td></tr>' + |
---|
164 | '<tr><td>Registration</td><td>'+htmlregister+'</td></tr>' + |
---|
165 | '<tr><td>On/Off Power Scene:</td><td>'+htmlpower+' (optional scene number to toggle On/Off state of the decoder)</td></tr>' + |
---|
166 | // '<tr><td>Test</td><td>'+htmltest+'</td></tr>' + |
---|
167 | '</table><hr class="cplus_hr">'+ |
---|
168 | '<table class="cplus_table" id="cplus_table_registered">'+ |
---|
169 | '<tr><td>Key Stream</td><td>'+htmlstream+' (csv list of key names)</td></tr>' + |
---|
170 | '<tr><td>Keys</td><td>'+htmlkeys+'</td></tr>' + |
---|
171 | '<tr><td>Channels</td><td>'+htmlchannels+'</td></tr>' + |
---|
172 | '</table>'+ |
---|
173 | '</div>' ; |
---|
174 | |
---|
175 | //html = html + '<button id="button_save" type="button">Save</button>' |
---|
176 | set_panel_html(html); |
---|
177 | |
---|
178 | // |
---|
179 | // test isregistered |
---|
180 | // |
---|
181 | jQuery( "#cplus_Test" ).click(function() { |
---|
182 | }); |
---|
183 | |
---|
184 | // click handler to get pattern value and add it to target names |
---|
185 | jQuery( "#cplus_TestRefresh" ).click(function() { |
---|
186 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"Refresh"); |
---|
187 | jQuery.get( url ,function( data ) { |
---|
188 | //alert("Success:"+data); |
---|
189 | }) |
---|
190 | .fail(function() { |
---|
191 | alert("Internal failure, could not run the VERA action"); |
---|
192 | }); |
---|
193 | }); |
---|
194 | |
---|
195 | // click handler to request pairing |
---|
196 | jQuery( "#cplus_RequestPairing" ).click(function() { |
---|
197 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"RequestPairing"); |
---|
198 | jQuery.get( url ,function( data ) { |
---|
199 | //alert("Success:"+data); |
---|
200 | }) |
---|
201 | .fail(function() { |
---|
202 | alert("Internal failure, could not run the VERA action"); |
---|
203 | }); |
---|
204 | }); |
---|
205 | |
---|
206 | // click handler to register phone |
---|
207 | jQuery( "#cplus_RegisterSmartphone" ).click(function() { |
---|
208 | var value = jQuery("#cplus_pairing").val(); |
---|
209 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"RegisterSmartphone",{newPairing:value}); |
---|
210 | jQuery.get( url ,function( data ) { |
---|
211 | cplus_checkRegistration(deviceID); |
---|
212 | }) |
---|
213 | .fail(function() { |
---|
214 | alert("Internal failure, could not run the VERA action"); |
---|
215 | }); |
---|
216 | }); |
---|
217 | |
---|
218 | // click handler to unregister phone |
---|
219 | jQuery( "#cplus_UnregisterSmartphone" ).click(function() { |
---|
220 | var value = jQuery("#cplus_pairing").val(); |
---|
221 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"UnregisterSmartphone"); |
---|
222 | jQuery.get( url ,function( data ) { |
---|
223 | cplus_checkRegistration(deviceID); |
---|
224 | }) |
---|
225 | .fail(function() { |
---|
226 | alert("Internal failure, could not run the VERA action"); |
---|
227 | }); |
---|
228 | }); |
---|
229 | |
---|
230 | // click handler to unregister phone |
---|
231 | jQuery( "#cplus_SendKeyStream" ).click(function() { |
---|
232 | var value = jQuery("#cplus_keystream").val(); |
---|
233 | cplus_sendKeyStream(deviceID,value); |
---|
234 | }); |
---|
235 | |
---|
236 | jQuery( "#cplus_channels" ).change(function() { |
---|
237 | //alert( jQuery( this ).val() ); |
---|
238 | var url = buildUPnPActionUrl(deviceID,cplus_Svs,"SetChannel",{newLocator:jQuery( this ).val() }); |
---|
239 | jQuery.get( url ,function( data ) { |
---|
240 | //alert("Success:"+data); |
---|
241 | }) |
---|
242 | .fail(function() { |
---|
243 | alert("Internal failure, could not run the VERA action"); |
---|
244 | }); |
---|
245 | }); |
---|
246 | |
---|
247 | // execute and get the result (table of names) - synchronous |
---|
248 | var url = ip_address +'id=lr_CPLUS_Handler&command=getkeys&DeviceNum='+deviceID; |
---|
249 | jQuery.ajax({ |
---|
250 | type: "GET", |
---|
251 | url: url, |
---|
252 | cache: false |
---|
253 | }).done(function(data, textStatus, jqXHR) { |
---|
254 | // then get the new ones |
---|
255 | // text result is a json encoding of array of iCloud names |
---|
256 | var keynames = JSON.parse(data); |
---|
257 | var keydiv = jQuery('#cplus_keys') |
---|
258 | jQuery.each( keynames , function (index,value) { |
---|
259 | var button = '<button onclick="cplus_PressKey('+deviceID+',\''+value+'\')">'+value+'</button>'; |
---|
260 | keydiv.append(button); |
---|
261 | }); |
---|
262 | }).fail(function() { |
---|
263 | alert('getkeys Failed!'); |
---|
264 | }); |
---|
265 | |
---|
266 | // execute and get the channels |
---|
267 | var url = ip_address +'id=lr_CPLUS_Handler&command=getchannels&DeviceNum='+deviceID; |
---|
268 | jQuery.ajax({ |
---|
269 | type: "GET", |
---|
270 | url: url, |
---|
271 | cache: false |
---|
272 | }).done(function(data, textStatus, jqXHR) { |
---|
273 | var channels = JSON.parse(data); |
---|
274 | jQuery( "#cplus_channels" ).data( "listid", channels.listid ); |
---|
275 | channels.list.sort( function(a,b) { |
---|
276 | if (a.name<b.name) |
---|
277 | return -1; |
---|
278 | if (a.name>b.name) |
---|
279 | return 1; |
---|
280 | return 0; |
---|
281 | } ); |
---|
282 | jQuery.each( channels.list, function( index, value ) { |
---|
283 | var option = new Option(value.name, value.loc); |
---|
284 | jQuery('#cplus_channels').append(option); |
---|
285 | }); |
---|
286 | }).fail(function() { |
---|
287 | alert('getchannels Failed!'); |
---|
288 | }); |
---|
289 | |
---|
290 | cplus_checkRegistration(deviceID); |
---|
291 | } |
---|
292 | |
---|
293 | //------------------------------------------------------------- |
---|
294 | // Save functions |
---|
295 | //------------------------------------------------------------- |
---|
296 | function cplus_PressKey(deviceID,value) |
---|
297 | { |
---|
298 | cplus_sendKeyStream(deviceID,value); |
---|
299 | } |
---|
300 | |
---|
301 | function cplus_SetIpAddress(deviceID, varName, varVal) { |
---|
302 | //return save(deviceID, varName, varVal, goodip); |
---|
303 | } |
---|
304 | |
---|
305 | function cplus_SetPowerScene(deviceID, varName, varVal) { |
---|
306 | return save(deviceID, varName, varVal, goodscene); |
---|
307 | } |
---|
308 | |
---|
309 | function cplus_SetPairing(deviceID, varName, varVal) { |
---|
310 | return save(deviceID, varName, varVal, goodpairing); |
---|
311 | } |
---|
312 | |
---|
313 | function goodip(ip) |
---|
314 | { |
---|
315 | // @duiffie contribution |
---|
316 | var reg = new RegExp('^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:\\d{1,5})?$', 'i'); |
---|
317 | return(reg.test(ip)); |
---|
318 | } |
---|
319 | function goodscene(v) |
---|
320 | { |
---|
321 | var reg = new RegExp('^\\d*$', 'i'); |
---|
322 | return(reg.test(v)); |
---|
323 | } |
---|
324 | function goodpairing(v) |
---|
325 | { |
---|
326 | var reg = new RegExp('^\\d\\d\\d\\d$', 'i'); |
---|
327 | return(reg.test(v)); |
---|
328 | } |
---|
329 | function save(deviceID, varName, varVal, func, reload) { |
---|
330 | // reload is optional parameter and defaulted to false |
---|
331 | if (typeof reload === "undefined" || reload === null) { |
---|
332 | reload = false; |
---|
333 | } |
---|
334 | |
---|
335 | if ((!func) || func(varVal)) { |
---|
336 | //set_device_state(deviceID, cplus_Svs, varName, varVal); |
---|
337 | saveVar(deviceID, cplus_Svs, varName, varVal, reload) |
---|
338 | jQuery('#cplus_' + varName).css('color', 'black'); |
---|
339 | } else { |
---|
340 | jQuery('#cplus_' + varName).css('color', 'red'); |
---|
341 | alert(varName+':'+varVal+' is not correct'); |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | //------------------------------------------------------------- |
---|
346 | // Pattern Matching functions |
---|
347 | //------------------------------------------------------------- |
---|
348 | |
---|
349 | //------------------------------------------------------------- |
---|
350 | // Variable saving ( log , then full save ) |
---|
351 | //------------------------------------------------------------- |
---|
352 | function saveVar(deviceID, cplus_Svs, varName, varVal, reload) |
---|
353 | { |
---|
354 | // set_device_state (deviceID, cplus_Svs, varName, varVal, 0); // only updated at time of luup restart |
---|
355 | set_device_state (deviceID, cplus_Svs, varName, varVal, (reload==true) ? 0 : 1); // lost in case of luup restart |
---|
356 | |
---|
357 | // 3rd method : updated immediately but not reflected ! |
---|
358 | var url = buildVariableSetUrl( deviceID, varName, varVal) |
---|
359 | var jqxhr = jQuery.ajax({ |
---|
360 | url:url, |
---|
361 | async:false // important to be in synchronous mode in that case |
---|
362 | }) |
---|
363 | .done(function() { |
---|
364 | // success, remove pending save for this variable |
---|
365 | |
---|
366 | }) |
---|
367 | .fail(function() { |
---|
368 | // error, keep track of error, keep entry in DB for next save |
---|
369 | |
---|
370 | }); |
---|
371 | } |
---|
372 | |
---|
373 | |
---|
374 | //------------------------------------------------------------- |
---|
375 | // Helper functions to build URLs to call VERA code from JS |
---|
376 | //------------------------------------------------------------- |
---|
377 | function buildVeraURL( deviceID, fnToUse, varName, varValue) |
---|
378 | { |
---|
379 | var urlHead = '' + ip_address + 'id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunLua&Code='; |
---|
380 | if (varValue != null) |
---|
381 | return urlHead + fnToUse + '("' + cplus_Svs + '", "' + varName + '", "' + varValue + '", ' + deviceID + ')'; |
---|
382 | |
---|
383 | return urlHead + fnToUse + '("' + cplus_Svs + '", "' + varName + '", "", ' + deviceID + ')'; |
---|
384 | } |
---|
385 | |
---|
386 | function buildVariableSetUrl( deviceID, varName, varValue) |
---|
387 | { |
---|
388 | var urlHead = '' + ip_address + 'id=variableset&DeviceNum='+deviceID+'&serviceId='+cplus_Svs+'&Variable='+varName+'&Value='+varValue; |
---|
389 | return urlHead; |
---|
390 | } |
---|
391 | |
---|
392 | function buildUPnPActionUrl(deviceID,service,action,params) |
---|
393 | { |
---|
394 | var urlHead = ip_address +'id=action&output_format=json&DeviceNum='+deviceID+'&serviceId='+service+'&action='+action;//'&newTargetValue=1'; |
---|
395 | if (params != undefined) { |
---|
396 | jQuery.each(params, function(index,value) { |
---|
397 | urlHead = urlHead+"&"+index+"="+value; |
---|
398 | }); |
---|
399 | } |
---|
400 | return urlHead; |
---|
401 | } |
---|
402 | |
---|
403 | |
---|