freifunkmanager/webroot/js/store.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

import config from './config';
2019-06-05 16:04:51 +02:00
const list = {},
2019-06-05 16:23:53 +02:00
storeMaxPing = 5;
2017-05-16 16:00:32 +02:00
// Returns the node with specified id (or null if node doesn't exist).
export function getNode (nodeid) {
if (!list[nodeid]) {
return null;
}
2017-05-16 19:18:35 +02:00
let node = list[nodeid];
2018-08-10 13:46:18 +02:00
// keep structur for pings later
return node;
};
2017-05-30 14:35:11 +02:00
// Creates an empty node, but doesn't add it to the list.
export function createNode (nodeid) {
return {
'node_id': nodeid,
'hostname': '',
'owner': '',
'lastseen': null,
'wireless': {
'channel24': config.node.channel24,
'channel5': config.node.channel5,
},
2019-06-05 16:04:51 +02:00
'location': {},
'pingstate':[]
};
};
// 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;
};
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;
}
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);
};