commitpull/704/headad3e664b56
Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 19:54:54 2022 +0100 Add .idea to .gitignore commit7e51a09384
Merge:93d8035
7dd0b0e
Author: Benoit SERRA <oupsman@oupsman.fr> Date: Tue Dec 13 18:38:51 2022 +0100 Merge branch 'benphelps:main' into main commit93d80350b1
Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 18:15:20 2022 +0100 Omada widget : One widget, shows only the number alerts, the number of connected AP, the number of connected devices to Wifi, the number of connected switches and gatewawys. commita1babd860c
Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 13 09:33:50 2022 +0100 Omada widget : spliting widget between WLAN and LAN/WAN fields to have no more than 5 fields per widget. commite12cc65c77
Merge:331f31f
146326f
Author: Benoit SERRA <oupsman@oupsman.fr> Date: Sun Dec 11 14:39:27 2022 +0100 Merge branch 'benphelps:main' into main commit331f31fc2b
Merge:37154e3
ccc1229
Author: Benoit SERRA <oupsman@oupsman.fr> Date: Sat Dec 10 17:56:44 2022 +0100 Merge branch 'benphelps:main' into main commit37154e327a
Author: Benoit <oupsman@oupsman.fr> Date: Sat Dec 10 17:11:30 2022 +0100 Omada widget : Improved error handling Omada widget: handling power as common.power in translation commit1f48491406
Author: Benoit <oupsman@oupsman.fr> Date: Sat Dec 10 10:24:55 2022 +0100 Omada widget : adding stats for isolated aps, connected gateways, connected switches, available ports, power consumption commitf375f0b815
Merge:467b678
775b511
Author: Benoit <oupsman@oupsman.fr> Date: Fri Dec 9 21:06:38 2022 +0100 Merge branch 'main' of https://github.com/Oupsman/homepage into main commit467b67802a
Author: Benoit <oupsman@oupsman.fr> Date: Fri Dec 9 21:06:09 2022 +0100 Omada widget : v3 v4 and v5 versions don't use the same fields for the same stats, I've corrected the code to make it more reliable commit775b5111e1
Merge:8d66756
88c4375
Author: Benoit SERRA <oupsman@oupsman.fr> Date: Thu Dec 8 15:38:20 2022 +0100 Merge branch 'benphelps:main' into main commit8d66756a7d
Author: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:45:44 2022 +0100 Omada Widget : code cleanup commit282a6d0592
Author: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:42:41 2022 +0100 Omada Widget : code cleanup commitc3e9b8f870
Author: Benoit <oupsman@oupsman.fr> Date: Thu Dec 8 12:37:10 2022 +0100 Omada Widget : No more legacy variable, the code detects the controller version and adapts the requests. Logic is not duplicated anymore commiteafcc20597
Author: Benoit <oupsman@oupsman.fr> Date: Wed Dec 7 15:46:00 2022 +0100 V2 API is working commitbcc2864ee2
Author: Benoit <oupsman@oupsman.fr> Date: Wed Dec 7 10:01:26 2022 +0100 Code fore v2 API is not working but V1 code is. commitea8e297e84
Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 6 14:28:05 2022 +0100 Errors handling commitab6d51a88c
Author: Benoit <oupsman@oupsman.fr> Date: Tue Dec 6 09:50:14 2022 +0100 Adding alerts commit047db2cce8
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:53:43 2022 +0100 Fixed translation system commit42c5a3e665
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:34:34 2022 +0100 Translation system is still * up commitc80eac9d5b
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:33:50 2022 +0100 Translation system is still * up commitf8ba6b0245
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:32:22 2022 +0100 Translation system is still * up commitdec7eec6de
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 22:16:13 2022 +0100 Translation system is * up commitcc840cf7cc
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 21:33:00 2022 +0100 First working version commit54b65e619e
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 18:59:09 2022 +0100 Using getGlobalStat method commit7ebc8500da
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 14:33:37 2022 +0100 Working on Omada Widget : NOT WORKING FOR NOW commit04eaf28cae
Merge:61065ac
826fe15
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 10:32:30 2022 +0100 Merge branch 'main' of https://github.com/Oupsman/homepage into main commit61065ace28
Author: Benoit <oupsman@oupsman.fr> Date: Mon Dec 5 10:24:57 2022 +0100 Working on Omada Widget remove idea Co-Authored-By: Benoit SERRA <11260343+oupsman@users.noreply.github.com>
parent
d16fd0cd05
commit
a9cc0100f6
@ -0,0 +1,41 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import useWidgetAPI from "../../utils/proxy/use-widget-api";
|
||||
import Container from "../../components/services/widget/container";
|
||||
import Block from "../../components/services/widget/block";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: omadaData, error: omadaAPIError } = useWidgetAPI(widget, "stats", {
|
||||
refreshInterval: 5000,
|
||||
});
|
||||
|
||||
if (omadaAPIError) {
|
||||
return <Container error={omadaAPIError} />;
|
||||
}
|
||||
|
||||
if (!omadaData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="omada.connectedAp" />
|
||||
<Block label="omada.activeUser" />
|
||||
<Block label="omada.alerts" />
|
||||
<Block label="omada.connectedGateway" />
|
||||
<Block label="omada.connectedSwitches" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="omada.connectedAp" value={t( "common.number", { value: omadaData.connectedAp})} />
|
||||
<Block label="omada.activeUser" value={t( "common.number", { value: omadaData.activeUser })} />
|
||||
<Block label="omada.alerts" value={t( "common.number", { value: omadaData.alerts })} />
|
||||
{ omadaData.connectedGateways > 0 && <Block label="omada.connectedGateway" value={t("common.number", { value: omadaData.connectedGateways})} /> }
|
||||
{ omadaData.connectedSwitches > 0 && <Block label="omada.connectedSwitches" value={t("common.number", { value: omadaData.connectedSwitches})} /> }
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,272 @@
|
||||
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const proxyName = "omadaProxyHandler";
|
||||
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
|
||||
async function login(loginUrl, username, password, cversion) {
|
||||
let params;
|
||||
if (cversion < "4.0.0") {
|
||||
// change the parameters of the query string
|
||||
params = JSON.stringify({
|
||||
"method": "login",
|
||||
"params": {
|
||||
"name": username,
|
||||
"password": password
|
||||
}
|
||||
});
|
||||
} else {
|
||||
params = JSON.stringify({
|
||||
"username": username,
|
||||
"password": password
|
||||
});
|
||||
}
|
||||
const authResponse = await httpProxy(loginUrl, {
|
||||
method: "POST",
|
||||
body: params,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
const data = JSON.parse(authResponse[2]);
|
||||
const status = authResponse[0];
|
||||
let token;
|
||||
if (data.errorCode === 0) {
|
||||
token = data.result.token;
|
||||
} else {
|
||||
token = null;
|
||||
}
|
||||
return [status, token ?? data];
|
||||
}
|
||||
|
||||
|
||||
export default async function omadaProxyHandler(req, res) {
|
||||
const { group, service } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (!widgets?.[widget.type]?.api) {
|
||||
return res.status(403).json({ error: "Service does not support API calls" });
|
||||
}
|
||||
|
||||
if (widget) {
|
||||
let cid;
|
||||
let cversion;
|
||||
let connectedAp;
|
||||
let activeuser;
|
||||
let connectedSwitches;
|
||||
let connectedGateways;
|
||||
|
||||
let alerts;
|
||||
let loginUrl;
|
||||
let siteName;
|
||||
let requestresponse;
|
||||
|
||||
const {url} = widget;
|
||||
|
||||
const controllerInfoUrl = `${widget.url}/api/info`;
|
||||
|
||||
const cInfoResponse = await httpProxy(controllerInfoUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
if (cInfoResponse[0] === 500) {
|
||||
logger.debug("Getting controller version ends with Error 500");
|
||||
return res.status(cInfoResponse[0]).json({error: {message: "HTTP Error", controllerInfoUrl, data: cInfoResponse[2]}});
|
||||
|
||||
}
|
||||
const cidresult = cInfoResponse[2];
|
||||
|
||||
try {
|
||||
cid = JSON.parse(cidresult).result.omadacId;
|
||||
cversion = JSON.parse(cidresult).result.controllerVer;
|
||||
} catch (e) {
|
||||
cversion = "3.2.17"
|
||||
}
|
||||
if (cversion < "4.0.0") {
|
||||
loginUrl = `${widget.url}/api/user/login?ajax`;
|
||||
} else if (cversion < "5.0.0") {
|
||||
loginUrl = `${widget.url}/api/v2/login`;
|
||||
} else {
|
||||
loginUrl = `${widget.url}/${cid}/api/v2/login`;
|
||||
}
|
||||
requestresponse = await login(loginUrl, widget.username, widget.password, cversion);
|
||||
|
||||
if (requestresponse[1].errorCode) {
|
||||
return res.status(requestresponse[0]).json({error: {message: "Error logging in", url, data: requestresponse[1]}});
|
||||
}
|
||||
|
||||
const token = requestresponse[1];
|
||||
// Switching to the site we want to gather stats from
|
||||
// First, we get the list of sites
|
||||
let sitesUrl;
|
||||
let body;
|
||||
let params;
|
||||
let headers;
|
||||
let method;
|
||||
let sitetoswitch;
|
||||
if (cversion < "4.0.0") {
|
||||
sitesUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
|
||||
body = JSON.stringify({
|
||||
"method": "getUserSites",
|
||||
"params": {
|
||||
"userName": widget.username
|
||||
}});
|
||||
params = { "token": token };
|
||||
headers = { };
|
||||
method = "POST";
|
||||
} else if (cversion < "5.0.0") {
|
||||
sitesUrl = `${widget.url}/api/v2/sites?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
body = {};
|
||||
params = {"token": token};
|
||||
headers = {"Csrf-Token": token };
|
||||
method = "GET";
|
||||
|
||||
} else {
|
||||
sitesUrl = `${widget.url}/${cid}/api/v2/sites?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
body = {};
|
||||
headers = { "Csrf-Token": token };
|
||||
method = "GET";
|
||||
params = { };
|
||||
}
|
||||
requestresponse = await httpProxy(sitesUrl, {
|
||||
method,
|
||||
params,
|
||||
body: body.toString(),
|
||||
headers,
|
||||
});
|
||||
const listresult = JSON.parse(requestresponse[2]);
|
||||
if (listresult.errorCode !== 0) {
|
||||
logger.debug(`HTTTP ${requestresponse[0]} getting sites list: ${requestresponse[2].msg}`);
|
||||
return res.status(requestresponse[0]).json({error: {message: "Error getting sites list", url, data: requestresponse[2]}});
|
||||
}
|
||||
|
||||
// Switching site is really needed only for Omada 3.x.x controllers
|
||||
|
||||
let switchUrl;
|
||||
|
||||
if (cversion < "4.0.0") {
|
||||
sitetoswitch = listresult.result.siteList.filter(site => site.name === widget.site);
|
||||
siteName = sitetoswitch[0].siteName;
|
||||
switchUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`;
|
||||
method = "POST";
|
||||
body = JSON.stringify({
|
||||
"method": "switchSite",
|
||||
"params": {
|
||||
"siteName": siteName,
|
||||
"userName": widget.username
|
||||
}
|
||||
});
|
||||
headers = { "Content-Type": "application/json" };
|
||||
params = { "token": token };
|
||||
requestresponse = await httpProxy(switchUrl, {
|
||||
method,
|
||||
params,
|
||||
body: body.toString(),
|
||||
headers,
|
||||
});
|
||||
const switchresult = JSON.parse(requestresponse[2]);
|
||||
if (switchresult.errorCode !== 0) {
|
||||
logger.debug(`HTTTP ${requestresponse[0]} getting sites list: ${requestresponse[2]}`);
|
||||
return res.status(requestresponse[0]).json({error: {message: "Error switching site", url, data: requestresponse[2]}});
|
||||
}
|
||||
}
|
||||
|
||||
// OK now we are on the correct site. Let's get the stats
|
||||
// on modern controller, we need to call two different endpoints
|
||||
// on older controller, we can call one endpoint
|
||||
if (cversion < "4.0.0") {
|
||||
const statsUrl = `${widget.url}/web/v1/controller?getGlobalStat=&token=${token}`;
|
||||
const statResponse = await httpProxy(statsUrl, {
|
||||
method: "POST",
|
||||
params: { "token": token },
|
||||
body: JSON.stringify({
|
||||
"method": "getGlobalStat",
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
const data = JSON.parse(statResponse[2]);
|
||||
|
||||
if (data.errorCode !== 0) {
|
||||
return res.status(statResponse[0]).json({error: {message: "Error getting stats", url, data: statResponse[2]}});
|
||||
}
|
||||
connectedAp = data.result.connectedAp;
|
||||
activeuser = data.result.activeUser;
|
||||
alerts = data.result.alerts;
|
||||
|
||||
} else {
|
||||
let siteStatsUrl;
|
||||
let response;
|
||||
sitetoswitch = listresult.result.data.filter(site => site.name === widget.site);
|
||||
|
||||
if (sitetoswitch.length === 0) {
|
||||
return res.status(requestresponse[0]).json({error: {message: `Site ${widget.site} is not found`, url, data: requestresponse[2]}});
|
||||
}
|
||||
|
||||
// On 5.0.0, the field we need is id, on 4.x.x, it's key ...
|
||||
siteName = sitetoswitch[0].id ?? sitetoswitch[0].key;
|
||||
if (cversion < "5.0.0") {
|
||||
siteStatsUrl = `${url}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
} else {
|
||||
siteStatsUrl = `${url}/${cid}/api/v2/sites/${siteName}/dashboard/overviewDiagram?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
}
|
||||
response = await httpProxy(siteStatsUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Csrf-Token": token,
|
||||
},
|
||||
});
|
||||
|
||||
const clientresult = JSON.parse(response[2]);
|
||||
if (clientresult.errorCode !== 0) {
|
||||
logger.debug(`HTTTP ${listresult.errorCode} getting clients stats for site ${widget.site} with message ${listresult.msg}`);
|
||||
return res.status(500).send(response[2]);
|
||||
}
|
||||
|
||||
activeuser = clientresult.result.totalClientNum;
|
||||
connectedAp = clientresult.result.connectedApNum;
|
||||
connectedGateways = clientresult.result.connectedGatewayNum;
|
||||
connectedSwitches = clientresult.result.connectedSwitchNum;
|
||||
|
||||
|
||||
let alertUrl;
|
||||
if (cversion >= "5.0.0") {
|
||||
alertUrl = `${url}/${cid}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
} else {
|
||||
alertUrl = `${url}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000`;
|
||||
}
|
||||
response = await httpProxy(alertUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Csrf-Token": token,
|
||||
},
|
||||
});
|
||||
const alertresult = JSON.parse(response[2]);
|
||||
alerts = alertresult.result.alertNum;
|
||||
}
|
||||
|
||||
return res.send(JSON.stringify({
|
||||
"connectedAp": connectedAp,
|
||||
"activeUser": activeuser,
|
||||
"alerts": alerts,
|
||||
"connectedGateways": connectedGateways,
|
||||
"connectedSwitches": connectedSwitches,
|
||||
}));
|
||||
}
|
||||
}
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import omadaProxyHandler from "./proxy";
|
||||
// import genericProxyHandler from "../../utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/web/v1/{endpoint}",
|
||||
proxyHandler: omadaProxyHandler,
|
||||
|
||||
mappings: {
|
||||
stats: {
|
||||
endpoint: "controller",
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue