summaryrefslogtreecommitdiff
path: root/www/js/rhctl.js
blob: 616042ecbdd4d0c56ee4a97c8842de89bae5ed49 (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
/*
 *  rhrdweb
 *
 *  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
 *
 *  This file is part of rhrdweb.
 *
 *  rhrdweb is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  rhrdweb is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with rhrdweb. If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

function Rhctl() {
  this.draw_callbacks = $.Callbacks('unique');
  this.state = 'NEW';

  this.sock_onmessage = function(event) {
    var msg = JSON.parse(event.data);
    if (msg.RESPONSE_CODE != 200) {
      console.log("got error message: ", event.data)
    } else {
      switch(msg.TYPE) {
      case 'state':
        this.draw_callbacks.fireWith(window, [ msg.STATE ]);
        break;
      case 'ack':
        break;
      default:
        console.log("rhctl: got unexpected message type '" + msg.TYPE + "'")
      }
    }
  }

  this.addCallback = function(cb) {
    this.draw_callbacks.add(cb);
  }

  this.sock_onopen = function() {
    this.state = 'CONNECTED';
    console.log('rhctl: ' + this.state)
    this.sock.onmessage = this.sock_onmessage.bind(this);
    this.sock.send(JSON.stringify({ COMMAND: 'subscribe', ARGS: [ 'state' ] }));
    this.sock.send(JSON.stringify({ COMMAND: 'state' }));
  }

  this.sock_onclose = function(event) {
    this.draw_callbacks.fireWith(window, []);
    this.sock.close();
    delete this.sock;
    setTimeout(this.connect.bind(this), 1000);
    this.state = 'RECONNECTING';
    console.log('rhctl: ' + this.state)
  }

  this.connect = function() {
    this.sock = new WebSocket('wss://' + window.location.host + '/rhctl/socket');
    this.sock.onopen = this.sock_onopen.bind(this);
    this.sock.onclose = this.sock_onclose.bind(this);
    this.state = 'CONNECTING';
    console.log('rhctl: ' + this.state)
  }
}

var rhctl = new Rhctl();

function rhctl_init() {
  rhctl.connect();
  rhctl_draw_state()
  rhctl.addCallback(rhctl_draw_state);
}

function rhctl_draw_state(state) {
  var raw = $('#rhctlraw');
  var mood = $('#rhctlmood img').removeClass();
  var srv = $('#rhctlactiveserver span').removeClass().addClass('label');
  var srvTable = $('#rhctlservertable table');
  srvTable.find("tr:gt(0)").remove();;
  var audioTable = $('#rhctlaudiotable table');
  audioTable.find("tr:gt(0)").remove();;

  if(!state) {
    raw.text("connecting...");
    mood.attr('src', '/img/mood-undefined.png');
    srv.addClass('label-default').text('none');
    return;
  }

    // Raw message
  raw.text(JSON.stringify(state, null, '\t'));

    // Mood
  mood.attr('src', '/img/mood-' + state.Mood + '.png');
  if(state.Settled) {
    mood.attr('title', state.Mood);
  } else {
    mood.addClass('blink').attr('title', state.Mood + ' (Settling)');
  }

    // Active Server
  srv.text(state.ActiveServer);
  switch(state.ActiveServer) {
  case 'master':
    srv.addClass('label-success');
    break;
  case 'standby':
    srv.addClass('label-warning');
    break;
  default:
    if(!state.ActiveServer) {
      srv.text('none');
    }
    srv.addClass('label-default');
  }

    // Server Table
  $.each(state.Server, function(key, value) {
     var entry = $('#hiddenTemplates .servertableEntryTemplate').clone().removeClass('servertableEntryTemplate');
      entry.find('.server-name').text(key);
      if(value.Channel) {
        entry.find('.server-channel').text(value.Channel);
      }
      var h = entry.find('.server-health img');
      if(value.ResurrectionLevel > 0 && value.ResurrectionLevel < 1) {
        h.addClass('blink').attr('src', '/img/server-resurrecting.png').attr('title', 'resurrecting');
      } else {
        switch(value.Health) {
        case 'alive':
        case 'dead':
          h.attr('src', '/img/server-' + value.Health + '.png').attr('title', value.Health);
          break;
        default:
        }
      }
      srvTable.append(entry)
  });


    // Audio Outputs
  $.each(state.Switch.Audio, function(index, value) {
     var entry = $('#hiddenTemplates .audiotableEntryTemplate').clone().removeClass('audiotableEntryTemplate');
     entry.find('.audio-out').text(index+1);
     $.each(value.Inputs, function(index, value) {
         if(value) {
             entry.find('.audio-in' + (index+1) + ' span').removeClass('glyphicon-unchecked').addClass('glyphicon-check');
         }
         });
     if(!value.Silence)  {
       entry.find('.audio-silence span').removeClass('glyphicon-volume-off').addClass('glyphicon-volume-up');
     }

     audioTable.append(entry)
  });
}