mirror of
https://github.com/UiO-Gaming/infoscreen3.git
synced 2026-11-05 22:04:33 +01:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
import plugin from "../../modules/plugin.js";
|
|
import exec from 'child_process';
|
|
import config from './settings.js';
|
|
|
|
function ping_getTime() {
|
|
let d = new Date();
|
|
return d.getTime();
|
|
}
|
|
|
|
|
|
export default class myTestPlugin extends plugin {
|
|
|
|
onInit() {
|
|
this.cache = 0;
|
|
this.lastStamp = ping_getTime()-30000;
|
|
}
|
|
|
|
onPluginRouter(router) {
|
|
let self = this;
|
|
router.get("/ajax/ping", function (req, res) {
|
|
|
|
if ((ping_getTime() - self.lastStamp) <= 5 * 1000) {
|
|
return res.json({ ping: self.cache });
|
|
}
|
|
|
|
let cmd = "ping -c 1 ";
|
|
let regex = /=([0-9.]+?) ms/;
|
|
if (/^win/.test(process.platform)) {
|
|
cmd = "ping -n 1 ";
|
|
regex = /[><=]([0-9.]+?)ms/;
|
|
}
|
|
|
|
exec(cmd + config.host, function (err, stdout, stdErr) {
|
|
var ms = stdout.match(regex);
|
|
ms = (ms && ms[1]) ? Number(ms[1]) : ms;
|
|
self.lastStamp = ping_getTime();
|
|
self.cache = ms;
|
|
res.json({ ping: ms });
|
|
});
|
|
})
|
|
}
|
|
|
|
setDisplayViewAdditions() {
|
|
return this.renderPluginView("pingWidget");
|
|
}
|
|
}
|