summaryrefslogtreecommitdiff
path: root/web-static/socket.html
blob: 9781e8189b16c7ac66697ad6ac94ab0c184eb11a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE HTML>
<html>
  <head>
    <title>rhimportd Websocket Testclient</title>
    <meta charset="utf-8">
    <style type="text/css">
      body {
        background-color: #555;
      }

      div.data {
        background-color: white;
        border: 1px solid;
        padding: 1em;
        font-family: monospace;
        margin-top: 1em;
        margin-bottom: 1em;
      }

      #progress {
        background-color: white;
        border: 1px solid;
        padding: 1em;
        font-family: monospace;
        margin-top: 2em;
        margin-bottom: 0em;
      }

      #progress span.caption {
        font-weight: bold;
      }

      td {
        text-align: right;
      }
    </style>
    <script src="jquery.min.js"></script>
    <script type="text/javascript">
      function SessionList(req) {
        this.req = req
        this.sock = new WebSocket("ws://localhost:4080/public/socket");
        this.sock_onmessage = function (event) {
          $('#listmsg').text(event.data);
        }
        this.sock.onmessage = this.sock_onmessage.bind(this);

        this.sock_onopen = function() {
          this.sock.send(JSON.stringify(this.req));
          $('#buttonlist').attr('disabled','disabled')
        }
        this.sock.onopen = this.sock_onopen.bind(this);
      }

      var sl;

      function list() {
        req = { COMMAND: "list",
                LOGIN_NAME: $('#username').val(),
	              PASSWORD: $('#token').val()};
        sl = new SessionList(req);
      }


      function Session(req) {
        this.req = req
        $('#rawmsg').text("");
        this.sock = new WebSocket("ws://localhost:4080/public/socket");
        this.sock_onmessage = function (event) {
          $('#rawmsg').append(event.data + "\n");
          msg = $.parseJSON(event.data)
          switch (msg.TYPE) {
            case "ack":
               $('#sessionid').val(msg.ID);
               buttonsRunning();
               break;
            case "progress":
               repaintProgress(msg.PROGRESS_STEP_NAME, msg.PROGRESS, msg.TITLE, msg.CART_NUMBER, msg.CUT_NUMBER);
               break;
            case "done":
            case "error":
               this.sock.close();
               init();
               break;
          }
        }
        this.sock.onmessage = this.sock_onmessage.bind(this);

        this.sock_onopen = function() {
          this.sock.send(JSON.stringify(this.req));
        }
        this.sock.onopen = this.sock_onopen.bind(this);

        this.cancel = function() {
          this.sock.send(JSON.stringify({COMMAND: "cancel"}));
        }
      }

      var s;

      function run() {
        req = { COMMAND: "new",
                TIMEOUT: 7200,
                REFERENCE_ID: $('#refid').val(),
                LOGIN_NAME: $('#username').val(),
	              PASSWORD: $('#token').val(),
	              SHOW_ID: 10002,
	              CLEAR_SHOW_CARTS: true,
	              SOURCE_URI: $('#source').val() };
        s = new Session(req);
      }

      function reconnect() {
        req = { COMMAND: "reconnect",
                LOGIN_NAME: $('#username').val(),
	              ID: $('#sessionid').val() };
        s = new Session(req);
      }

      function cancel() {
        s.cancel();
      }

      function detach() {
        s.sock.close();
        buttonsIdle();
      }

      function buttonsIdle() {
        $('#buttonrun').removeAttr('disabled');
        $('#buttonreconnect').removeAttr('disabled');
        $('#buttondetach').attr('disabled','disabled');
        $('#buttoncancel').attr('disabled','disabled');
      }

      function buttonsRunning() {
        $('#buttonrun').attr('disabled','disabled');
        $('#buttonreconnect').attr('disabled','disabled');
        $('#buttondetach').removeAttr('disabled');
        $('#buttoncancel').removeAttr('disabled');
      }

      function repaintProgress(step, value, title, cart, cut) {
        $('#progress_step').text("'" + step + "'");
        $('#progress_value').text(Math.round(value*100)/100 + "%");
        $('#progress_title').text("'" + title + "'");
        $('#progress_cart').text(cart);
        $('#progress_cut').text(cut);
      }

      function init() {
        $('#sessionid').val("");
        buttonsIdle();
        repaintProgress("", 0, "", 0, 0);
        $('#buttonlist').removeAttr('disabled','disabled');
      }
    </script>
  </head>
  <body onload="init()">

    <h1>rhimportd Websocket Testclient</h1>

    <div>
      <table>
        <tr><td>Username</td><td><input id="username" type="text" size="20" value="heslinki" /></td></tr>
        <tr><td>Token</td><td><input id="token" type="text" size="20" /></td></tr>
      </table>
    </div>

    <div>
      <button id="buttonlist" onclick="list()">list</button>
      <div id="listmsg" class="data"></div>
    </div>

    <div>
      <input id="source" type="text" size="30" value="fake://10000">
      <input id="refid" type="text" size="15" value="test-reference-id">
      <button id="buttonrun" onclick="run()">start</button>
      <button id="buttondetach" onclick="detach()">detach</button>
      <input id="sessionid" type="text" size="45">
      <button id="buttonreconnect" onclick="reconnect()">reconnect</button>
      <button id="buttoncancel" onclick="cancel()">cancel</button>
      <div id="progress">
        <span class="caption">Step/Value:</span>
        <span id="progress_step"></span> / <span id="progress_value"></span>
        <span class="caption">Title</span>
        <span id="progress_title"></span>
        <span class="caption">Cart/Cut</span>
        <span id="progress_cart"></span> / <span id="progress_cut"></span>
      </div>
      <div id="rawmsg" class="data"></div>
    </div>
  </body>
</html>