view services/commands/src/main/resources/agent.html @ 297:8ecf97835acd

[commands] Bring version in line with others. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025543.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 26 Oct 2017 16:07:31 +0200
parents 51841a9b5f81
children
line wrap: on
line source

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Thermostat Commands Agent (HTML Receiver)</title>
    <style type="text/css"><![CDATA[
        input#cmd-chan {
            width: 410px
        }

        #console-container {
            width: 400px;
        }

        #console {
            border: 1px solid #CCCCCC;
            border-right-color: #999999;
            border-bottom-color: #999999;
            height: 170px;
            overflow-y: scroll;
            padding: 5px;
            width: 100%;
        }

        #console p {
            padding: 0;
            margin: 0;
        }
    ]]></style>
    <script type="application/javascript">
    //<![CDATA[
        var CmdChan = {};

        var Response = {};

        Response.asJson = (function(sequence, resp) {
            var r = {};
            r.type = 100;
            r.sequence = sequence;
            r.payload = {};
            r.payload.respType = resp;
            return JSON.stringify(r);
        });

        var Msg = {};

        Msg.fromRequest = (function(rawMsg) {
            var jsonObj = JSON.parse(rawMsg);
            Msg.sequence = jsonObj.sequence;
            Msg.payload = jsonObj.payload;
        });

        CmdChan.socket = null;

        CmdChan.getTokenParam = (function(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return null;
        });

        CmdChan.connect = (function(host) {
            if ('WebSocket' in window) {
                CmdChan.socket = new WebSocket(host);
            } else if ('MozWebSocket' in window) {
                CmdChan.socket = new MozWebSocket(host);
            } else {
                Console.log('Error: WebSocket is not supported by this browser.');
                return;
            }

            CmdChan.socket.onopen = function () {
                Console.log('Info: WebSocket connection opened.');
            };

            CmdChan.socket.onclose = function () {
                Console.log('Info: WebSocket closed.');
            };

            CmdChan.socket.onmessage = function (message) {
                var data = JSON.parse(message.data);
                var jvmId = escapeHtml(data.jvmId);
                var systemId = escapeHtml(data.systemId);
                var payload = data.payload;

                var requestArguments = {}
                for (var key in payload) {
                    // Check for an actual key, and not a prototype value
                    if (payload.hasOwnProperty(key)) {
                        requestArguments[escapeHtml(key)] = escapeHtml(payload[key]);
                    }
                }

                // Print message to screen
                data.payload = requestArguments;
                data.jvmId = jvmId;
                data.systemId = systemId;
                Console.log('Got: ' + JSON.stringify(data));

                // parse message
                Msg.fromRequest(message.data);

                // send the always-ok reply
                CmdChan.socket.send(Response.asJson(Msg.sequence, 'OK'));
                Console.log('Info: OK reply sent.');
            };
        });

        CmdChan.initialize = function() {
            var query_string = "";
            var token = CmdChan.getTokenParam("access_token");
            if (token != null) {
                query_string = "?access_token=" + token;
            }
            if (window.location.protocol == 'http:') {
                CmdChan.connect('ws://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/testAgent' + query_string);
            } else {
                CmdChan.connect('wss://' + window.location.host + '/commands/0.0.1/systems/ignored_system/agents/testAgent' + query_string);
            }
        };

        var Console = {};

        Console.log = (function(message) {
            var console = document.getElementById('console');
            var p = document.createElement('p');
            p.style.wordWrap = 'break-word';
            p.innerHTML = message;
            console.appendChild(p);
            while (console.childNodes.length > 25) {
                console.removeChild(console.firstChild);
            }
            console.scrollTop = console.scrollHeight;
        });

        CmdChan.initialize();

        document.addEventListener("DOMContentLoaded", function() {
            // Remove elements with "noscript" class - <noscript> is not allowed in XHTML
            var noscripts = document.getElementsByClassName("noscript");
            for (var i = 0; i < noscripts.length; i++) {
                noscripts[i].parentNode.removeChild(noscripts[i]);
            }
        }, false);

        var escapeHtml = function(unsafeStr) {
            return unsafeStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
        }

    //]]>
    </script>
</head>
<body>
<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
    Javascript and reload this page!</h2></div>
<div>
    <p>
    Thermostat Commands: Agent Receiver
    </p>
    <div id="console-container">
        <div id="console"/>
    </div>
</div>
</body>
</html>