summaryrefslogtreecommitdiff
path: root/www/js/clock.js
blob: a4cba2dbd1d482faa3ecf3d91e04554e562e7706 (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
/*
 *  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 Clock() {
  this.draw_callbacks = $.Callbacks('unique');

  this.last_message = { t1: 0, t2: 0, t3: 0, t4: 0, tz_offset: 3600 };
  this.clock_offset = 0;
  this.clock_rtt = 0;
  this.state = 'NEW';

  this.now = function() {
    return new Date(this.getRDTimeMS());
  }

  this.getRDTimeMS = function() {
    return (+new Date()) + (this.last_message.tz_offset * 1000) + this.clock_offset;
  }

  this.redraw = function() {
    var rdtime_ms = this.getRDTimeMS()

    var rdtime = new Date(rdtime_ms);
    var date_str = weekday_short[rdtime.getUTCDay()]  + ', ';
    date_str += rdtime.getUTCDate() + '.' + (rdtime.getUTCMonth() + 1) + '.' + rdtime.getUTCFullYear();

    var time_str = (rdtime.getUTCHours() > 9 ? ' ' : ' 0') + rdtime.getUTCHours();
    time_str += (rdtime.getUTCMinutes() > 9 ? ':' : ':0') + rdtime.getUTCMinutes();
    time_str += (rdtime.getUTCSeconds() > 9 ? ':' : ':0') + rdtime.getUTCSeconds();

    this.draw_callbacks.fireWith(window, [date_str, time_str, get_rd_week(rdtime_ms)]);
  }

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

  this.ntp_update = function(event) {
    var t4 = (+new Date());

    var msg = JSON.parse(event.data);
    msg.t4 = t4;
    this.last_message = msg;
    this.clock_offset = ((msg.t2 - msg.t1) + (msg.t3 - msg.t4)) / 2;
    this.clock_rtt = (msg.t4 - msg.t1) - (msg.t3 - msg.t2);
//    console.log('got new ntp message from rhrdtime (rtt=' + this.clock_rtt + ' ms): new offset = ' + this.clock_offset + ' ms');
  }

  this.ntp_request = function() {
    this.sock.send(JSON.stringify({ t1: (+new Date()), t2: 0, t3: 0, t4: 0, tz_offset: 0, week: 0 }));
  }

  this.sock_onopen = function() {
//    console.log('clock websocket connection established');
    this.state = 'CONNECTED';
    this.ntp_request();
    this.interval_request = setInterval(this.ntp_request.bind(this), 2000);
  }

  this.sock_onclose = function(event) {
    if(this.state == 'STOPPED') {
      delete this.sock;
    } else {
//      console.log('clock websocket closed with code ' + event.code + ', trying reconnect...');
      clearInterval(this.interval_request);
      delete this.interval_request;
      this.sock.close();
      delete this.sock;
      setTimeout(this.connect.bind(this), 1000);
      this.state = 'RECONNECTING';
    }
  }

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

  this.start = function() {
    this.connect();
    this.interval_redraw = setInterval(this.redraw.bind(this), 200);
  }

  this.stop = function() {
    this.state = 'STOPPED';
    clearInterval(this.interval_redraw);
    delete this.interval_redraw;
    clearInterval(this.interval_request);
    delete this.interval_request;
    this.sock.close();
  }
}

var clock = new Clock();
clock.start();