Update ping component via DOM manipulation
This commit is contained in:
1 parent
306015f022
commit
f966194e22
2 files changed
+47
-42
No files matched your search
+45
-41
@@ -17,58 +17,62 @@
|
|||||||
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ClosureComponent, Component } from "mithril";
|
import { ClosureComponent, Component, VnodeDOM } from "mithril";
|
||||||
import { m } from "../components";
|
import { m } from "../components";
|
||||||
import * as store from "../store";
|
import * as store from "../store";
|
||||||
|
|
||||||
const REFRESH_INTERVAL = 3000;
|
const REFRESH_INTERVAL = 3000;
|
||||||
|
|
||||||
const component: ClosureComponent = (): Component => {
|
const component: ClosureComponent = (vn): Component => {
|
||||||
|
let interval: ReturnType<typeof setInterval>;
|
||||||
|
let host: string;
|
||||||
|
|
||||||
|
const refresh = async (): Promise<void> => {
|
||||||
|
let status = "";
|
||||||
|
if (host) {
|
||||||
|
try {
|
||||||
|
const res = await store.ping(host);
|
||||||
|
if (res["avg"] != null) status = `${Math.trunc(res["avg"])} ms`;
|
||||||
|
else status = "Unreachable";
|
||||||
|
} catch (err) {
|
||||||
|
setTimeout(() => {
|
||||||
|
throw err;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dom = (vn as VnodeDOM).dom;
|
||||||
|
|
||||||
|
if (dom) dom.innerHTML = `Pinging ${host}: ${status}`;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
oninit: vnode => {
|
onremove: () => {
|
||||||
vnode.state["timestamp"] = 0;
|
clearInterval(interval);
|
||||||
vnode.state["ping"] = null;
|
|
||||||
vnode.state["host"] = null;
|
|
||||||
vnode.state["timeout"] = null;
|
|
||||||
},
|
},
|
||||||
view: vnode => {
|
view: vnode => {
|
||||||
const refresh = (): void => {
|
const device = vnode.attrs["device"];
|
||||||
const device = vnode.attrs["device"];
|
let param =
|
||||||
let param =
|
device["InternetGatewayDevice.ManagementServer.ConnectionRequestURL"];
|
||||||
device["InternetGatewayDevice.ManagementServer.ConnectionRequestURL"];
|
if (!param)
|
||||||
if (!param)
|
param = device["Device.ManagementServer.ConnectionRequestURL"];
|
||||||
param = device["Device.ManagementServer.ConnectionRequestURL"];
|
|
||||||
if (!param || !param.value) return;
|
|
||||||
|
|
||||||
|
let h;
|
||||||
|
if (param && param.value) {
|
||||||
const url = new URL(param.value[0]);
|
const url = new URL(param.value[0]);
|
||||||
vnode.state["host"] = url.hostname;
|
h = url.hostname;
|
||||||
vnode.state["timestamp"] = Date.now();
|
|
||||||
store
|
|
||||||
.ping(vnode.state["host"])
|
|
||||||
.then(res => {
|
|
||||||
vnode.state["ping"] = res;
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
// Do nothing
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const t = vnode.state["timestamp"] + REFRESH_INTERVAL - Date.now();
|
|
||||||
clearTimeout(vnode.state["timeout"]);
|
|
||||||
if (t <= 0) refresh();
|
|
||||||
else vnode.state["timeout"] = setTimeout(refresh, t);
|
|
||||||
|
|
||||||
if (vnode.state["host"]) {
|
|
||||||
let status = "";
|
|
||||||
if (vnode.state["ping"]) {
|
|
||||||
if (vnode.state["ping"].avg != null)
|
|
||||||
status = `${Math.trunc(vnode.state["ping"].avg)} ms`;
|
|
||||||
else status = "Unreachable";
|
|
||||||
}
|
|
||||||
|
|
||||||
return m("div", `Pinging ${vnode.state["host"]}: ${status}`);
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
if (host !== h) {
|
||||||
|
host = h;
|
||||||
|
clearInterval(interval);
|
||||||
|
if (host) {
|
||||||
|
refresh();
|
||||||
|
interval = setInterval(refresh, REFRESH_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m("div", `Pinging ${host}:`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-1
@@ -693,6 +693,7 @@ export function logOut(): Promise<void> {
|
|||||||
|
|
||||||
export function ping(host): Promise<{}> {
|
export function ping(host): Promise<{}> {
|
||||||
return xhrRequest({
|
return xhrRequest({
|
||||||
url: `/api/ping/${encodeURIComponent(host)}`
|
url: `/api/ping/${encodeURIComponent(host)}`,
|
||||||
|
background: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user