2018-07-15 22:47:05 +02:00
|
|
|
import config from './config';
|
|
|
|
|
2019-06-05 16:04:51 +02:00
|
|
|
const list = {},
|
2019-06-10 13:38:21 +02:00
|
|
|
pingState = {},
|
|
|
|
storeMaxPing = 10;
|
2017-05-16 16:00:32 +02:00
|
|
|
|
2018-07-15 21:09:39 +02:00
|
|
|
// Returns the node with specified id (or null if node doesn't exist).
|
2018-06-30 16:20:54 +02:00
|
|
|
export function getNode (nodeid) {
|
2018-07-15 21:09:39 +02:00
|
|
|
if (!list[nodeid]) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-16 19:18:35 +02:00
|
|
|
|
2018-07-15 21:09:39 +02:00
|
|
|
let node = list[nodeid];
|
2018-08-10 13:46:18 +02:00
|
|
|
// keep structur for pings later
|
2019-06-10 13:38:21 +02:00
|
|
|
if(pingState[nodeid]) {
|
|
|
|
node.pingstate = pingState[nodeid];
|
|
|
|
}else{
|
|
|
|
node.pingstate = [];
|
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
return node;
|
|
|
|
};
|
2017-05-30 14:35:11 +02:00
|
|
|
|
2018-07-15 21:09:39 +02:00
|
|
|
// Creates an empty node, but doesn't add it to the list.
|
|
|
|
export function createNode (nodeid) {
|
|
|
|
return {
|
|
|
|
'node_id': nodeid,
|
2018-07-21 18:50:08 +02:00
|
|
|
'hostname': '',
|
|
|
|
'owner': '',
|
|
|
|
'lastseen': null,
|
2018-07-15 22:47:05 +02:00
|
|
|
'wireless': {
|
|
|
|
'channel24': config.node.channel24,
|
|
|
|
'channel5': config.node.channel5,
|
|
|
|
},
|
2019-06-05 16:04:51 +02:00
|
|
|
'location': {},
|
2018-07-15 21:09:39 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Overwrites the values for the specified node (identified by its node_id) with new values.
|
2018-08-10 13:46:18 +02:00
|
|
|
export function updateNode (node) {
|
|
|
|
list[node.node_id] = node;
|
2019-06-10 13:38:21 +02:00
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
};
|
2017-05-30 14:35:11 +02:00
|
|
|
|
2019-06-05 16:04:51 +02:00
|
|
|
function updateNodePingTo(value){
|
|
|
|
return (nodeid) => {
|
|
|
|
if (!list[nodeid]) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-10 13:38:21 +02:00
|
|
|
if(pingState[nodeid] === undefined) {
|
|
|
|
pingState[nodeid] = [value];
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pingState[nodeid].unshift(value);
|
|
|
|
if (pingState[nodeid].length > storeMaxPing) {
|
|
|
|
pingState[nodeid].length = storeMaxPing;
|
2019-06-05 16:04:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateNodePing(ping) {
|
2019-06-10 13:38:21 +02:00
|
|
|
if(ping['true']) {
|
|
|
|
ping['true'].forEach(updateNodePingTo(true));
|
|
|
|
}
|
|
|
|
if(ping['false']) {
|
|
|
|
ping['false'].forEach(updateNodePingTo(false));
|
|
|
|
}
|
2019-06-05 16:04:51 +02:00
|
|
|
};
|
|
|
|
|
2018-07-15 21:09:39 +02:00
|
|
|
// Returns a list of all known nodes.
|
2018-06-30 16:20:54 +02:00
|
|
|
export function getNodes () {
|
|
|
|
return Object.keys(list).map(getNode);
|
|
|
|
};
|