1 var afb = new AFB("api", "mysecret");
3 var ucs2_config="ConfigNotSelected";
7 function syntaxHighlight(json) {
8 if (typeof json !== 'string') {
9 json = JSON.stringify(json, undefined, 2);
11 json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
12 return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
14 if (/^"/.test(match)) {
15 if (/:$/.test(match)) {
20 } else if (/true|false/.test(match)) {
22 } else if (/null/.test(match)) {
25 return '<span class="' + cls + '">' + match + '</span>';
29 function basename(path) {
30 return path.split('/').reverse()[0];
33 function getParameterByName(name, url) {
35 url = window.location.href;
37 name = name.replace(/[\[\]]/g, "\\$&");
38 var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
39 results = regex.exec(url);
40 if (!results) return null;
41 if (!results[2]) return '';
42 return decodeURIComponent(results[2].replace(/\+/g, " "));
45 // default soundcard is "PCH"
46 var devid=getParameterByName("devid");
47 if (!devid) devid="hw:1";
49 var haldev=getParameterByName("haldev");
50 if (!haldev) haldev="scarlett-usb";
52 var sndname=getParameterByName("sndname");
53 if (!sndname) sndname="PCH";
55 var mode=getParameterByName("mode");
61 function replyok(obj) {
62 console.log("replyok:" + JSON.stringify(obj));
63 document.getElementById("output").innerHTML = "OK: "+ syntaxHighlight(obj);
66 function replyerr(obj) {
67 console.log("replyerr:" + JSON.stringify(obj));
68 document.getElementById("output").innerHTML = "ERROR: "+ syntaxHighlight(obj);
71 function gotevent(obj) {
72 console.log("gotevent:" + JSON.stringify(obj));
73 document.getElementById("outevt").innerHTML = (evtidx++) +": "+JSON.stringify(obj);
76 function send(message) {
77 var api = document.getElementById("api").value;
78 var verb = document.getElementById("verb").value;
79 document.getElementById("question").innerHTML = "subscribe: "+api+"/"+verb + " (" + JSON.stringify(message) +")";
80 ws.call(api+"/"+verb, {data:message}).then(replyok, replyerr);
84 // On button click from HTML page
85 function callbinder(api, verb, query) {
86 console.log ("subscribe api="+api+" verb="+verb+" query=" +query);
87 var question = urlws +"/" +api +"/" +verb + "?query=" + JSON.stringify(query);
88 document.getElementById("question").innerHTML = syntaxHighlight(question);
89 ws.call(api+"/"+verb, query).then(replyok, replyerr);
93 // Retreive Select value and Text from the binder
94 // Note: selection of value/text for a given context is huggly!!!
95 function querySelectList (elemid, api, verb, query) {
97 console.log("querySelectList elemid=%s api=%s verb=%s query=%s", elemid, api, verb, query);
99 var selectobj = document.getElementById(elemid);
101 console.log ("****** elemid=%s does not exit in HTML page ****", elemid);
105 // onlick update selected HAL api
106 selectobj.onclick=function(){
107 ucs2_config= this.value;
108 console.log ("Default Selection=" + ucs2_config);
111 function gotit (result) {
113 // display response as for normal onclick action
115 var response=result.response;
117 // fulfill select with avaliable active HAL
118 for (idx=0; idx<response.length; idx++) {
119 var opt = document.createElement('option');
120 var basename = response[idx].basename;
121 var dirpath = response[idx].dirpath;
122 var ext= basename.split('.').pop();
124 // Only propose XML files
125 if (ext.toLowerCase() === "xml") {
127 opt.value = dirpath + "/" + basename;
128 selectobj.appendChild(opt);
132 ucs2_config= selectobj.value;
135 var question = urlws +"/"+api +"/" +verb + "?query=" + JSON.stringify(query);
136 document.getElementById("question").innerHTML = syntaxHighlight(question);
138 // request lowlevel ALSA to get API list
139 ws.call(api+"/"+verb, query).then(gotit, replyerr);
143 function init(elemid, api, verb, query) {
146 // check for active HALs
147 querySelectList (elemid, api, verb, query);
149 document.getElementById("main").style.visibility = "visible";
150 document.getElementById("connected").innerHTML = "Binder WS Active";
151 document.getElementById("connected").style.background = "lightgreen";
152 ws.onevent("*", gotevent);
156 document.getElementById("main").style.visibility = "hidden";
157 document.getElementById("connected").innerHTML = "Connected Closed";
158 document.getElementById("connected").style.background = "red";
161 ws = new afb.ws(onopen, onabort);