diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-04-12 09:42:39 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-07-10 23:59:53 +0200 |
commit | d67d4335f05635d06b433f7d3fa0f6a4e401ec92 (patch) | |
tree | b5d57ea1620278f65008f8faf34648f58d04da42 /htdocs |
Initial commit
Change-Id: Ia434e5b4869ea19b0b78b1c586c44c15cb93c7e8
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'htdocs')
-rw-r--r-- | htdocs/AFB-websock.js | 177 | ||||
-rw-r--r-- | htdocs/CMakeLists.txt | 33 | ||||
-rw-r--r-- | htdocs/assets/background_iot_bzh_light.jpg | bin | 0 -> 40718 bytes | |||
-rw-r--r-- | htdocs/assets/favicon.ico | bin | 0 -> 1150 bytes | |||
-rw-r--r-- | htdocs/assets/iot-bzh-logo-small.png | bin | 0 -> 14449 bytes | |||
-rw-r--r-- | htdocs/index.html | 48 | ||||
-rw-r--r-- | htdocs/iotbzh-Binding.css | 59 | ||||
-rw-r--r-- | htdocs/iotbzh-Binding.js | 149 |
8 files changed, 466 insertions, 0 deletions
diff --git a/htdocs/AFB-websock.js b/htdocs/AFB-websock.js new file mode 100644 index 0000000..3d4831f --- /dev/null +++ b/htdocs/AFB-websock.js @@ -0,0 +1,177 @@ +var urlWS; +var urlhttp; + +AFB = function(base, initialtoken){ + +urlWS = "ws://"+window.location.host+"/"+base; +urlhttp = "http://"+window.location.host+"/"+base; + +/*********************************************/ +/**** ****/ +/**** AFB_context ****/ +/**** ****/ +/*********************************************/ +var AFB_context; +{ + var UUID = undefined; + var TOKEN = initialtoken; + + var context = function(token, uuid) { + this.token = token; + this.uuid = uuid; + } + + context.prototype = { + get token() {return TOKEN;}, + set token(tok) {if(tok) TOKEN=tok;}, + get uuid() {return UUID;}, + set uuid(id) {if(id) UUID=id;} + }; + + AFB_context = new context(); +} +/*********************************************/ +/**** ****/ +/**** AFB_websocket ****/ +/**** ****/ +/*********************************************/ +var AFB_websocket; +{ + var CALL = 2; + var RETOK = 3; + var RETERR = 4; + var EVENT = 5; + + var PROTO1 = "x-afb-ws-json1"; + + AFB_websocket = function(onopen, onabort) { + var u = urlWS; + if (AFB_context.token) { + u = u + '?x-afb-token=' + AFB_context.token; + if (AFB_context.uuid) + u = u + '&x-afb-uuid=' + AFB_context.uuid; + } + this.ws = new WebSocket(u, [ PROTO1 ]); + this.pendings = {}; + this.awaitens = {}; + this.counter = 0; + this.ws.onopen = onopen.bind(this); + this.ws.onerror = onerror.bind(this); + this.ws.onclose = onclose.bind(this); + this.ws.onmessage = onmessage.bind(this); + this.onopen = onopen; + this.onabort = onabort; + this.onclose = onabort; + } + + function onerror(event) { + var f = this.onabort; + if (f) { + delete this.onopen; + delete this.onabort; + f && f(this); + } + this.onerror && this.onerror(this); + } + + function onopen(event) { + var f = this.onopen; + delete this.onopen; + delete this.onabort; + f && f(this); + } + + function onclose(event) { + for (var id in this.pendings) { + var ferr = this.pendings[id].onerror; + ferr && ferr(null, this); + } + this.pendings = {}; + this.onclose && this.onclose(); + } + + function fire(awaitens, name, data) { + var a = awaitens[name]; + if (a) + a.forEach(function(handler){handler(data);}); + var i = name.indexOf("/"); + if (i >= 0) { + a = awaitens[name.substring(0,i)]; + if (a) + a.forEach(function(handler){handler(data);}); + } + a = awaitens["*"]; + if (a) + a.forEach(function(handler){handler(data);}); + } + + function reply(pendings, id, ans, offset) { + if (id in pendings) { + var p = pendings[id]; + delete pendings[id]; + var f = p[offset]; + f(ans); + } + } + + function onmessage(event) { + var obj = JSON.parse(event.data); + var code = obj[0]; + var id = obj[1]; + var ans = obj[2]; + AFB_context.token = obj[3]; + switch (code) { + case RETOK: + reply(this.pendings, id, ans, 0); + break; + case RETERR: + reply(this.pendings, id, ans, 1); + break; + case EVENT: + default: + fire(this.awaitens, id, ans); + break; + } + } + + function close() { + this.ws.close(); + this.onabort(); + } + + function call(method, request) { + return new Promise((function(resolve, reject){ + var id, arr; + do { + id = String(this.counter = 4095 & (this.counter + 1)); + } while (id in this.pendings); + this.pendings[id] = [ resolve, reject ]; + arr = [CALL, id, method, request ]; + if (AFB_context.token) arr.push(AFB_context.token); + this.ws.send(JSON.stringify(arr)); + }).bind(this)); + } + + function onevent(name, handler) { + var id = name; + var list = this.awaitens[id] || (this.awaitens[id] = []); + list.push(handler); + } + + AFB_websocket.prototype = { + close: close, + call: call, + onevent: onevent + }; +} +/*********************************************/ +/**** ****/ +/**** ****/ +/**** ****/ +/*********************************************/ +return { + context: AFB_context, + ws: AFB_websocket +}; +}; + diff --git a/htdocs/CMakeLists.txt b/htdocs/CMakeLists.txt new file mode 100644 index 0000000..7aa0ce1 --- /dev/null +++ b/htdocs/CMakeLists.txt @@ -0,0 +1,33 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll <fulup@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + + + +################################################## +# HTML Testing Files +################################################## +PROJECT_TARGET_ADD(htdocs) + + file(GLOB SOURCE_FILES "*.html" "*.js" "*.jpg" "*.css" "assets") + + add_input_files("${SOURCE_FILES}") + + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + LABELS "HTDOCS" + OUTPUT_NAME ${TARGET_NAME} + ) diff --git a/htdocs/assets/background_iot_bzh_light.jpg b/htdocs/assets/background_iot_bzh_light.jpg Binary files differnew file mode 100644 index 0000000..f47d2ee --- /dev/null +++ b/htdocs/assets/background_iot_bzh_light.jpg diff --git a/htdocs/assets/favicon.ico b/htdocs/assets/favicon.ico Binary files differnew file mode 100644 index 0000000..eeb7ab7 --- /dev/null +++ b/htdocs/assets/favicon.ico diff --git a/htdocs/assets/iot-bzh-logo-small.png b/htdocs/assets/iot-bzh-logo-small.png Binary files differnew file mode 100644 index 0000000..2c3b2ae --- /dev/null +++ b/htdocs/assets/iot-bzh-logo-small.png diff --git a/htdocs/index.html b/htdocs/index.html new file mode 100644 index 0000000..28d58ab --- /dev/null +++ b/htdocs/index.html @@ -0,0 +1,48 @@ +<html> + +<head> + <title>XDS data collector binding Test</title> + <link rel="stylesheet" href="iotbzh-Binding.css"> + <link rel="icon" type="image/x-icon" href="assets/favicon.ico"> + <script type="text/javascript" src="AFB-websock.js"></script> + <script type="text/javascript" src="iotbzh-Binding.js"></script> +</head> + +<body class="page-content" onload="init()"> + + <img src="assets/iot-bzh-logo-small.png"> + + <h1>XDS data collector prototype Test</h1> + + <button id="connected" onclick="init()">Binder WS Fail</button> + <button id="monitoring" onclick="window.open('/monitoring/monitor.html','_monitor_ctl')">Debug/Monitoring</a> + </button> + + <br> + <br> + + <div> + <button onclick="callbinder('xds-service','list' ,{});">List</button> + </li> + + <button onclick="callbinder('xds-service','trace' ,{'ws': 'unix:ave'});">Trace ws unix:ave</button> + </li> + + </div> + + <br> + + <div id="main" style="visibility:hidden"> + <ol> + <li>Question + <pre id="question"></pre> + </li> + <li>Response + <pre id="output"></pre> + </li> + <li>Events: + <pre id="outevt"></pre> + </li> + </ol> + </div> +</body> diff --git a/htdocs/iotbzh-Binding.css b/htdocs/iotbzh-Binding.css new file mode 100644 index 0000000..96a04cc --- /dev/null +++ b/htdocs/iotbzh-Binding.css @@ -0,0 +1,59 @@ +body.page-content { + height: 100%; + width: auto; + margin-top: 20px; + background: url("assets/background_iot_bzh_light.jpg") 0 0 no-repeat; + background-size: cover; + background-position: center; +} + +img { + float: right; +} + +#question, +#output, +#outevt { + white-space: pre-wrap; +} + +button { + margin-right: 10px; + padding: 6px 8px; + font-size: large; +} + +pre { + outline: 1px solid #ccc; + padding: 5px; + margin: 5px; + background-color: white; + opacity: 0.85; + min-height: 5pc; + max-height: 10pc; + overflow: auto; +} + +pre#output { + max-height: 30pc; +} + +.string { + color: green; +} + +.number { + color: darkorange; +} + +.boolean { + color: blue; +} + +.null { + color: magenta; +} + +.key { + color: red; +} diff --git a/htdocs/iotbzh-Binding.js b/htdocs/iotbzh-Binding.js new file mode 100644 index 0000000..ae32f83 --- /dev/null +++ b/htdocs/iotbzh-Binding.js @@ -0,0 +1,149 @@ +var afb = new AFB("api", "mysecret"); +var ws; +var evtIdx = 0; +var count = 0; + + +//********************************************** +// Logger +//********************************************** +var log = { + command: function (api, verb, query) { + console.log("subscribe api=" + api + " verb=" + verb + " query=", query); + var question = urlWS + "/" + api + "/" + verb + "?query=" + JSON.stringify(query); + log._write("question", count + ": " + log.syntaxHighlight(question)); + }, + + event: function (obj) { + console.log("gotevent:" + JSON.stringify(obj)); + log._write("outevt", (evtIdx++) + ": " + JSON.stringify(obj)); + }, + + reply: function (obj) { + console.log("replyok:" + JSON.stringify(obj)); + log._write("output", count + ": OK: " + log.syntaxHighlight(obj)); + }, + + error: function (obj) { + console.log("replyerr:" + JSON.stringify(obj)); + log._write("output", count + ": ERROR: " + log.syntaxHighlight(obj)); + }, + + _write: function (element, msg) { + var el = document.getElementById(element); + el.innerHTML += msg + '\n'; + + // auto scroll down + setTimeout(function () { + el.scrollTop = el.scrollHeight; + }, 100); + }, + + syntaxHighlight: function (json) { + if (typeof json !== 'string') { + json = JSON.stringify(json, undefined, 2); + } + json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); + return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { + var cls = 'number'; + if (/^"/.test(match)) { + if (/:$/.test(match)) { + cls = 'key'; + } else { + cls = 'string'; + } + } else if (/true|false/.test(match)) { + cls = 'boolean'; + } else if (/null/.test(match)) { + cls = 'null'; + } + return '<span class="' + cls + '">' + match + '</span>'; + }); + }, +}; + +//********************************************** +// Generic function to call binder +//*********************************************** +function callbinder(api, verb, query) { + log.command(api, verb, query); + + // ws.call return a Promise + return ws.call(api + '/' + verb, query) + .then(function (res) { + log.reply(res); + count++; + return res; + }) + .catch(function (err) { + log.reply(err); + count++; + throw err; + }); +}; + +//********************************************** +// +//********************************************** +var topo = null; + +function getTopo() { + callbinder('supervisor', 'list', null) + .then(list => { + var reqs = []; + for (pid in list.response) { + this.pid = pid; + reqs.push( + callbinder('supervisor', 'config', { + "pid": pid + }) + .then((res) => { + return { + 'pid': this.pid, + 'ws_servers': res.response.ws_servers, + 'ws_clients': res.response.ws_clients + } + }) + ); + }; + return Promise.all(reqs); + }) + .then(daemons => { + console.log("daemons count ", daemons.length); + daemons.forEach(dd => { + console.log(dd); + }); + topo = daemons; + }) + .catch(err => { + console.log() + }); +}; + + + + + +//********************************************** +// Init - establish Websocket connection +//********************************************** +function init(elemID, api, verb, query) { + + function onopen() { + // Request initial authorization + callbinder('xds-service', 'auth', ''); + + document.getElementById("main").style.visibility = "visible"; + document.getElementById("connected").innerHTML = "Binder WS Active"; + document.getElementById("connected").style.background = "lightgreen"; + ws.onevent("*", log.event); + } + + function onabort() { + document.getElementById("main").style.visibility = "hidden"; + document.getElementById("connected").innerHTML = "Connected Closed"; + document.getElementById("connected").style.background = "red"; + } + + ws = new afb.ws(onopen, onabort); +} |