Skip to content

Instantly share code, notes, and snippets.

@koteq
Last active February 5, 2018 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koteq/3a28f4990d1ab9883777d87e7e143e15 to your computer and use it in GitHub Desktop.
Save koteq/3a28f4990d1ab9883777d87e7e143e15 to your computer and use it in GitHub Desktop.
/**
* DIR-655 Network Monitor.
* Based on Network Monitor v2.0 for Yahoo! Widgets.
*/
let lan_tx_bytes;
let lan_rx_bytes;
let last_timestamp;
const readValues = () => {
fetch('http://192.168.0.1/router_info.xml?section=lan_stats')
.then((response) => response.text())
.then((str) => (new DOMParser()).parseFromString(str, 'text/xml'))
.then(xml => {
const c_last_timestamp = Date.now();
const c_lan_tx_bytes = parseInt(xml.querySelector('lan_tx_bytes').textContent);
const c_lan_rx_bytes = parseInt(xml.querySelector('lan_rx_bytes').textContent);
if (last_timestamp) {
const d_last_timestamp = c_last_timestamp - last_timestamp;
const d_lan_tx_bytes = c_lan_tx_bytes - lan_tx_bytes;
const d_lan_rx_bytes = c_lan_rx_bytes - lan_rx_bytes;
const d_lan_tx_Mbytes_ps = (d_lan_tx_bytes * 8 / 1024 / 1024) / (d_last_timestamp / 1000);
const d_lan_rx_Mbytes_ps = (d_lan_rx_bytes * 8 / 1024 / 1024) / (d_last_timestamp / 1000);
console.clear();
console.log(`UL ${d_lan_rx_Mbytes_ps.toFixed(2)} Mbit/s`);
console.log(`DL ${d_lan_tx_Mbytes_ps.toFixed(2)} Mbit/s`);
}
last_timestamp = c_last_timestamp;
lan_tx_bytes = c_lan_tx_bytes;
lan_rx_bytes = c_lan_rx_bytes;
});
};
readValues();
setInterval(readValues, 1000);
console.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment