2020-04-19 20:10:32 +00:00
|
|
|
Js8Thread = function(el){
|
|
|
|
this.messages = [];
|
|
|
|
this.el = el;
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Thread.prototype.getAverageFrequency = function(){
|
|
|
|
var total = this.messages.map(function(message){
|
|
|
|
return message.freq;
|
|
|
|
}).reduce(function(t, f){
|
|
|
|
return t + f;
|
|
|
|
}, 0);
|
|
|
|
return total / this.messages.length;
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Thread.prototype.pushMessage = function(message) {
|
|
|
|
this.messages.push(message);
|
|
|
|
this.render();
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Thread.prototype.render = function() {
|
|
|
|
this.el.html(
|
|
|
|
'<td>' + this.renderTimestamp(this.getLatestTimestamp()) + '</td>' +
|
|
|
|
'<td class="decimal freq">' + Math.round(this.getAverageFrequency()) + '</td>' +
|
2020-04-20 20:07:21 +00:00
|
|
|
'<td class="message"><div>' + this.renderMessages() + '</div></td>'
|
2020-04-19 20:10:32 +00:00
|
|
|
);
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
2020-04-19 21:35:06 +00:00
|
|
|
Js8Thread.prototype.getLatestTimestamp = function() {
|
|
|
|
return this.messages[0].timestamp;
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Js8Thread.prototype.isOpen = function() {
|
|
|
|
if (!this.messages.length) return true;
|
|
|
|
var last_message = this.messages[this.messages.length - 1];
|
|
|
|
return (last_message.thread_type & 2) === 0;
|
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Thread.prototype.renderMessages = function() {
|
2020-04-20 16:31:45 +00:00
|
|
|
var res = [];
|
2020-04-19 20:10:32 +00:00
|
|
|
for (var i = 0; i < this.messages.length; i++) {
|
|
|
|
var msg = this.messages[i];
|
|
|
|
if (msg.thread_type & 1) {
|
|
|
|
res.push('[ ');
|
2020-04-20 16:31:45 +00:00
|
|
|
} else if (i === 0 || msg.timestamp - this.messages[i - 1].timestamp > 15000) {
|
2020-04-19 20:10:32 +00:00
|
|
|
res.push(' ... ');
|
|
|
|
}
|
|
|
|
res.push(msg.msg);
|
|
|
|
if (msg.thread_type & 2) {
|
|
|
|
res.push(' ]');
|
2020-04-20 16:31:45 +00:00
|
|
|
} else if (i === this.messages.length -1) {
|
|
|
|
res.push(' ... ');
|
2020-04-19 20:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res.join('');
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Thread.prototype.renderTimestamp = function(timestamp) {
|
|
|
|
var t = new Date(timestamp);
|
|
|
|
var pad = function (i) {
|
|
|
|
return ('' + i).padStart(2, "0");
|
|
|
|
};
|
|
|
|
return pad(t.getUTCHours()) + pad(t.getUTCMinutes()) + pad(t.getUTCSeconds());
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
2020-04-19 21:35:06 +00:00
|
|
|
Js8Thread.prototype.purgeOldMessages = function() {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
this.messages = this.messages.filter(function(m) {
|
|
|
|
// keep messages around for 20 minutes
|
|
|
|
return now - m.timestamp < 20 * 60 * 1000;
|
|
|
|
});
|
|
|
|
if (!this.messages.length) {
|
|
|
|
this.el.remove();
|
|
|
|
} else {
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
return this.messages.length;
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 21:35:06 +00:00
|
|
|
|
2020-04-19 20:10:32 +00:00
|
|
|
Js8Threader = function(el){
|
|
|
|
this.threads = [];
|
|
|
|
this.tbody = $(el).find('tbody');
|
2020-04-19 21:35:06 +00:00
|
|
|
var me = this;
|
|
|
|
this.interval = setInterval(function(){
|
|
|
|
me.purgeOldMessages();
|
|
|
|
}, 15000);
|
|
|
|
};
|
|
|
|
|
|
|
|
Js8Threader.prototype.purgeOldMessages = function() {
|
|
|
|
this.threads = this.threads.filter(function(t) {
|
|
|
|
return t.purgeOldMessages();
|
|
|
|
});
|
2020-04-19 20:10:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Js8Threader.prototype.findThread = function(freq) {
|
|
|
|
var matching = this.threads.filter(function(thread) {
|
2020-04-19 21:35:06 +00:00
|
|
|
// max frequency deviation: 5 Hz. this may be a little tight.
|
2020-04-20 16:31:45 +00:00
|
|
|
return thread.isOpen() && Math.abs(thread.getAverageFrequency() - freq) <= 5;
|
2020-04-19 20:10:32 +00:00
|
|
|
});
|
2020-04-21 19:00:16 +00:00
|
|
|
matching.sort(function(a, b){
|
|
|
|
return b.getLatestTimestamp() - a.getLatestTimestamp();
|
|
|
|
});
|
2020-04-19 20:10:32 +00:00
|
|
|
return matching[0] || false;
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
Js8Threader.prototype.pushMessage = function(message) {
|
2020-04-21 16:11:07 +00:00
|
|
|
var thread;
|
|
|
|
// only look for exising threads if the message is not a starting message
|
|
|
|
if ((message.thread_type & 1) === 0) {
|
|
|
|
thread = this.findThread(message.freq);
|
|
|
|
}
|
2020-04-19 20:10:32 +00:00
|
|
|
if (!thread) {
|
2020-04-20 16:31:45 +00:00
|
|
|
var line = $("<tr></tr>");
|
2020-04-19 20:10:32 +00:00
|
|
|
this.tbody.append(line);
|
2020-04-20 16:31:45 +00:00
|
|
|
thread = new Js8Thread(line);
|
2020-04-19 20:10:32 +00:00
|
|
|
this.threads.push(thread);
|
|
|
|
}
|
|
|
|
thread.pushMessage(message);
|
2020-04-19 21:35:06 +00:00
|
|
|
this.tbody.scrollTop(this.tbody[0].scrollHeight);
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|
2020-04-19 20:10:32 +00:00
|
|
|
|
|
|
|
$.fn.js8 = function() {
|
|
|
|
if (!this.data('threader')) {
|
|
|
|
this.data('threader', new Js8Threader(this));
|
|
|
|
}
|
|
|
|
return this.data('threader');
|
2020-04-20 16:31:45 +00:00
|
|
|
};
|