diff --git a/webroot/js/socket.js b/webroot/js/socket.js index f8e0fe0..29d8ab0 100644 --- a/webroot/js/socket.js +++ b/webroot/js/socket.js @@ -202,5 +202,7 @@ addEvent('channels_wifi24', (msg) => { addEvent('channels_wifi5', (msg) => { store.channelsWifi5 = msg.body.sort((a, b) => a - b); }); - +addEvent('ping', (msg) => { + store.updateNodePing(msg.body); +}); connect(); diff --git a/webroot/js/store.js b/webroot/js/store.js index a43e8fc..5c384a2 100644 --- a/webroot/js/store.js +++ b/webroot/js/store.js @@ -1,6 +1,7 @@ import config from './config'; -const list = {}; +const list = {}, + storeMaxPing = 5, // Returns the node with specified id (or null if node doesn't exist). export function getNode (nodeid) { @@ -24,7 +25,8 @@ export function createNode (nodeid) { 'channel24': config.node.channel24, 'channel5': config.node.channel5, }, - 'location': {} + 'location': {}, + 'pingstate':[] }; }; @@ -33,6 +35,23 @@ export function updateNode (node) { list[node.node_id] = node; }; +function updateNodePingTo(value){ + return (nodeid) => { + if (!list[nodeid]) { + return; + } + list[nodeid]['pingstate'].unshift(value); + if (list[nodeid]['pingstate'].length > storeMaxPing) { + list[nodeid]['pingstate'].length = storeMaxPing; + } + } +} + +export function updateNodePing(ping) { + ping["true"].forEach(updateNodePingTo(true)); + ping["false"].forEach(updateNodePingTo(false)); +}; + // Returns a list of all known nodes. export function getNodes () { return Object.keys(list).map(getNode);