diff --git a/aprsd/stats.py b/aprsd/stats.py index 3c2517e..5c8380a 100644 --- a/aprsd/stats.py +++ b/aprsd/stats.py @@ -246,7 +246,6 @@ class APRSDStats: }, "plugins": plugin_stats, } - LOG.info("APRSD Stats: DONE") return stats def __str__(self): diff --git a/aprsd/web/chat/static/js/send-message.js b/aprsd/web/chat/static/js/send-message.js index 3888e1f..dff2483 100644 --- a/aprsd/web/chat/static/js/send-message.js +++ b/aprsd/web/chat/static/js/send-message.js @@ -4,6 +4,10 @@ var message_list = {}; var from_msg_list = {}; const socket = io("/sendmsg"); +MSG_TYPE_TX = "tx"; +MSG_TYPE_RX = "rx"; +MSG_TYPE_ACK = "ack"; + function size_dict(d){c=0; for (i in d) ++c; return c} function init_chat() { @@ -16,24 +20,28 @@ function init_chat() { }); socket.on("sent", function(msg) { - if (cleared == false) { + if (cleared === false) { + console.log("CLEARING #msgsTabsDiv"); var msgsdiv = $("#msgsTabsDiv"); - msgsdiv.html('') - cleared = true + msgsdiv.html(''); + cleared = true; } + msg["type"] = MSG_TYPE_TX; sent_msg(msg); }); socket.on("ack", function(msg) { - update_msg(msg); + msg["type"] = MSG_TYPE_ACK; + ack_msg(msg); }); socket.on("new", function(msg) { - if (cleared == false) { + if (cleared === false) { var msgsdiv = $("#msgsTabsDiv"); msgsdiv.html('') - cleared = true + cleared = true; } + msg["type"] = MSG_TYPE_RX; from_msg(msg); }); @@ -47,32 +55,124 @@ function init_chat() { }); init_gps(); + // Try and load any existing chat threads from last time + init_messages(); } -function add_callsign(callsign) { - /* Ensure a callsign exists in the left hand nav */ +function message_ts_id(msg) { + //Create a 'id' from the message timestamp + ts_str = msg["ts"].toString(); + ts = ts_str.split(".")[0]*1000; + id = ts_str.split('.')[0]; + return {'timestamp': ts, 'id': id}; +} - if (callsign in callsign_list) { - return false - } +function time_ack_from_msg(msg) { + // Return the time and ack_id from a message + ts_id = message_ts_id(msg); + ts = ts_id['timestamp']; + id = ts_id['id']; + ack_id = "ack_" + id + var d = new Date(ts).toLocaleDateString("en-US") + var t = new Date(ts).toLocaleTimeString("en-US") + return {'time': t, 'date': d, 'ack_id': ack_id}; +} + +function save_data() { + // Save the relevant data to local storage + localStorage.setItem('callsign_list', JSON.stringify(callsign_list)); + localStorage.setItem('message_list', JSON.stringify(message_list)); +} + +function init_messages() { + // This tries to load any previous conversations from local storage + callsign_list = JSON.parse(localStorage.getItem('callsign_list')); + message_list = JSON.parse(localStorage.getItem('message_list')); + console.log("init_messages"); + if (callsign_list == null) { + callsign_list = {}; + } + if (message_list == null) { + message_list = {}; + } + console.log(callsign_list); + console.log(message_list); + + // Now loop through each callsign and add the tabs + first_callsign = null; + for (callsign in callsign_list) { + console.log("Adding callsign " + callsign); + if (first_callsign === null) { + first_callsign = callsign; + console.log("first_callsign " + first_callsign) + } + create_callsign_tab(callsign); + } + // and then populate the messages in order + for (callsign in message_list) { + new_callsign = true; + cleared = true; + for (id in message_list[callsign]) { + msg = message_list[callsign][id]; + info = time_ack_from_msg(msg); + t = info['time']; + ack_id = false; + acked = false; + if (msg['type'] == MSG_TYPE_TX) { + ack_id = info['ack_id']; + acked = msg['ack']; + } + msg_html = create_message_html(t, msg['from'], msg['to'], msg['message'], ack_id, msg, acked); + append_message_html(callsign, msg_html, new_callsign); + new_callsign = false; + } + } + + //Click on the very first tab + if (first_callsign !== null) { + click_div = '#'+tab_string(first_callsign); + var click_timer = setTimeout(function() { + console.log("Click on first tab " + click_div); + $(click_div).click(); + clearTimeout(click_timer); + }, 500); + } +} + +function create_callsign_tab(callsign) { + //Create the html for the callsign tab and insert it into the DOM + console.log("create_callsign_tab " + callsign) var callsignTabs = $("#callsignTabs"); tab_name = tab_string(callsign); tab_content = tab_content_name(callsign); divname = content_divname(callsign); item_html = '