summaryrefslogtreecommitdiff
path: root/www/js/utils.js
diff options
context:
space:
mode:
authorPeter Grassberger <petertheone@gmail.com>2015-07-31 21:02:12 (GMT)
committerPeter Grassberger <petertheone@gmail.com>2015-07-31 21:02:12 (GMT)
commitc6fde48e1deb0016f9f8d1e0d6ae6584da7e2851 (patch)
tree657f40632390f2fd2a19f21b95c986e27b0225c1 /www/js/utils.js
parent0ee64178d1967772701058c00601cbe069f2e47c (diff)
move clock from utils.js to clock.js
Diffstat (limited to 'www/js/utils.js')
-rw-r--r--www/js/utils.js117
1 files changed, 0 insertions, 117 deletions
diff --git a/www/js/utils.js b/www/js/utils.js
index e2c494b..2c69b73 100644
--- a/www/js/utils.js
+++ b/www/js/utils.js
@@ -96,124 +96,7 @@ function get_rd_week(msEpoch) {
return week;
}
-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.redraw = function() {
- var rdtime_ms = (+new Date()) + (this.last_message.tz_offset * 1000) + this.clock_offset;
-
- 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();
-
-function clock_init() {
- clock.start();
-}
-
-function clock_add_callback(cb) {
- clock.addCallback(cb);
-}
-
function locationHashValue() {
var hash = window.location.hash.match(/#([a-z]+)\/?.*/);
return hash ? hash[1] : '';
}
-
-function drawClock(date, time, week) {
- $('#clock span.clock-date').text(date);
- $('#clock span.clock-time').text(time);
- var weekspan = $('#clock span.current-week').removeClass().addClass('current-week').addClass('label');
- switch(week) {
- case 1:
- weekspan.addClass('label-info').text('Woche 1');
- break;
- case 2:
- weekspan.addClass('label-warning').text('Woche 2');
- break;
- case 3:
- weekspan.addClass('label-success').text('Woche 3');
- break;
- case 4:
- weekspan.addClass('label-important').text('Woche 4');
- break;
- default:
- weekspan.addClass('label-inverse').text('Fehler');
- }
-}