view services/commands/src/main/resources/index.html @ 250:2d924d7904a7

Add optional arguments to gateway commands client Reviewed-by: jerboaa, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024839.html
author Chris Lessard <clessard@redhat.com>
date Wed, 06 Sep 2017 13:11:55 -0400
parents 1a1e29c83c7b
children 1428b14f642f
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 Client (HTML)</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 = {};

        CmdChan.socket = null;
        CmdChan.sequence = 1;

        CmdChan.connect = (function(host, message) {
            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.send(JSON.stringify(message));
            };

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

            CmdChan.socket.onmessage = function (message) {
                Console.log(message.data);
                CmdChan.socket.close();
                CmdChan.socket = null;
            };
        });

        CmdChan.initialize = (function() {
            document.getElementById('cmd-chan').onkeydown = function(event) {
                if (event.keyCode == 13) {
                    CmdChan.sendMessage();
                }
            };
            document.getElementById('agent').onkeydown = function(event) {
                if (event.keyCode == 13) {
                    CmdChan.sendMessage();
                }
            };
        });

        CmdChan.sendMessage = (function() {
            var action = document.getElementById('cmd-chan').value;
            var agent = document.getElementById('agent').value;
            var argumentKeys = document.getElementsByClassName('optionArgument');
            var argumentValues = document.getElementsByClassName('optionArgumentVal');

            if (action != '' && agent != '') {
                var clientRequest = {};
                var url = null;
                clientRequest.type = 2;
                clientRequest.payload = {};

                // Parse command arguments
                for (var i = 0; i < argumentValues.length; i++) {
                    var key = argumentKeys[i].value.trim();
                    var val = argumentValues[i].value.trim();

                    if (val != '' && key != '') {
                        clientRequest.payload[key] = val;
                    }
                }

                if (window.location.protocol == 'http:') {
                  url = 'ws://' + window.location.host;
                } else {
                  url = 'wss://' + window.location.host;
                }
                url = url + '/commands/v1/actions/' + action + '/systems/ignored_system/agents/' + agent + '/jvms/ignored_jvm/sequence/' + CmdChan.sequence++;
                CmdChan.connect( url, clientRequest );
                document.getElementById('cmd-chan').value = '';
            }
        });

        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;
        });

        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);

    //]]>
    </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: HTML Client
    </p>
    <p>
        <input type="text" placeholder="Type the action and press enter in order to send a message to the agent below." id="cmd-chan" />
        <input type="text" placeholder="The agent to send messages to. Eg. 'testAgent' or 'otherAgent'" id="agent" /> <br>
        <h3>Arguments: (blank fields will be omitted)</h3><br>
        <input type="text" placeholder="Argument 1" class="optionArgument"/><input type="text" placeholder="Value 1" class="optionArgumentVal"/><br>
        <input type="text" placeholder="Argument 2" class="optionArgument"/><input type="text" placeholder="Value 2" class="optionArgumentVal"/><br>
        <input type="text" placeholder="Argument 3" class="optionArgument"/><input type="text" placeholder="Value 3" class="optionArgumentVal"/><br>

    </p>
    <div id="console-container">
        <div id="console"/>
    </div>
    <script>
        CmdChan.initialize();
    </script>
</div>
</body>
</html>