From 952305492cf28fe523a6afac121bbfdd2d320608 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 21 Dec 2022 13:04:14 -0800 Subject: [PATCH] More Omada widget cleanup --- public/locales/en/common.json | 13 +++---- src/widgets/omada/component.jsx | 4 +-- src/widgets/omada/proxy.js | 61 ++++++++++++++++----------------- src/widgets/omada/widget.js | 8 ----- 4 files changed, 35 insertions(+), 51 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 78c5dce86..9f5637bf7 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -89,14 +89,11 @@ "no_active": "No Active Streams" }, "omada": { - "activeUser": "Active devices", - "alerts": "Alerts", - "connectedAp": "Connected APs", - "isolatedAp": "Isolated APs", - "powerConsumption": "Power consumption", - "availablePorts" : "Available ports", - "connectedGateway": "Connected gateways", - "connectedSwitches": "Connected switches" + "connectedAp": "Connected APs", + "activeUser": "Active devices", + "alerts": "Alerts", + "connectedGateway": "Connected gateways", + "connectedSwitches": "Connected switches" }, "nzbget": { "rate": "Rate", diff --git a/src/widgets/omada/component.jsx b/src/widgets/omada/component.jsx index d499da363..dee60842f 100644 --- a/src/widgets/omada/component.jsx +++ b/src/widgets/omada/component.jsx @@ -9,7 +9,7 @@ export default function Component({ service }) { const { widget } = service; - const { data: omadaData, error: omadaAPIError } = useWidgetAPI(widget, "stats", { + const { data: omadaData, error: omadaAPIError } = useWidgetAPI(widget, { refreshInterval: 5000, }); @@ -23,8 +23,6 @@ export default function Component({ service }) { - - ); } diff --git a/src/widgets/omada/proxy.js b/src/widgets/omada/proxy.js index d867e112b..e89ad81db 100644 --- a/src/widgets/omada/proxy.js +++ b/src/widgets/omada/proxy.js @@ -2,27 +2,26 @@ 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, controllerVersionMajor) { const params = { - username: username, - password: password + username, + password } if (controllerVersionMajor === 3) { - params["method"] = "login"; - params["params"] = { + params.method = "login"; + params.params = { name: username, password }; } - + + // eslint-disable-next-line no-unused-vars const [status, contentType, data] = await httpProxy(loginUrl, { method: "POST", body: JSON.stringify(params), @@ -41,10 +40,6 @@ export default async function omadaProxyHandler(req, res) { 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) { const {url} = widget; @@ -59,7 +54,7 @@ export default async function omadaProxyHandler(req, res) { if (status !== 200) { logger.error("Unable to retrieve Omada controller info"); - return res.status(status).json({error: {message: `HTTP Error ${status}`, url: controllerInfoURL, data: data}}); + return res.status(status).json({error: {message: `HTTP Error ${status}`, url: controllerInfoURL, data}}); } let cId; @@ -97,12 +92,11 @@ export default async function omadaProxyHandler(req, res) { const [loginStatus, loginResponseData] = await login(loginUrl, widget.username, widget.password, controllerVersionMajor); if (loginStatus !== 200 || loginResponseData.errorCode > 0) { - return res.status(requestresponse[0]).json({error: {message: "Error logging in to Oamda controller", url: loginUrl, data: loginResponseData}}); + return res.status(status).json({error: {message: "Error logging in to Oamda controller", url: loginUrl, data: loginResponseData}}); } - const token = loginResponseData.result.token; + const { token } = loginResponseData.result; - // List sites let sitesUrl; let body = {}; let params = { token }; @@ -126,6 +120,8 @@ export default async function omadaProxyHandler(req, res) { case 5: sitesUrl = `${widget.url}/${cId}/api/v2/sites?token=${token}¤tPage=1¤tPageSize=1000`; break; + default: + break; } [status, contentType, data] = await httpProxy(sitesUrl, { @@ -138,16 +134,16 @@ export default async function omadaProxyHandler(req, res) { const sitesResponseData = JSON.parse(data); if (sitesResponseData.errorCode > 0) { - logger.debug(`HTTTP ${status} getting sites list: ${requestresponse[2].msg}`); - return res.status(status).json({error: {message: "Error getting sites list", url, data: requestresponse[2]}}); + logger.debug(`HTTTP ${status} getting sites list: ${sitesResponseData.msg}`); + return res.status(status).json({error: {message: "Error getting sites list", url, data: sitesResponseData}}); } const site = (controllerVersionMajor === 3) ? - sitesResponseData.result.siteList.find(site => site.name === widget.site): - sitesResponseData.result.data.find(site => site.name === widget.site); + sitesResponseData.result.siteList.find(s => s.name === widget.site): + sitesResponseData.result.data.find(s => s.name === widget.site); if (!site) { - return res.status(requestresponse[0]).json({error: {message: `Site ${widget.site} is not found`, url, data}}); + return res.status(status).json({error: {message: `Site ${widget.site} is not found`, url, data}}); } let siteResponseData; @@ -159,18 +155,18 @@ export default async function omadaProxyHandler(req, res) { let alerts; if (controllerVersionMajor === 3) { - // Omada 3.x.x controller requires switching site + // Omada v3 controller requires switching site const switchUrl = `${widget.url}/web/v1/controller?ajax=&token=${token}`; method = "POST"; body = { - "method": "switchSite", - "params": { - "siteName": site.siteName, - "userName": widget.username + method: "switchSite", + params: { + siteName: site.siteName, + userName: widget.username } }; headers = { "Content-Type": "application/json" }; - params = { "token": token }; + params = { token }; [status, contentType, data] = await httpProxy(switchUrl, { method, @@ -219,25 +215,26 @@ export default async function omadaProxyHandler(req, res) { siteResponseData = JSON.parse(data); if (status !== 200 || siteResponseData.errorCode > 0) { - logger.debug(`HTTP ${status} getting stats for site ${widget.site} with message ${listresult.msg}`); + logger.debug(`HTTP ${status} getting stats for site ${widget.site} with message ${siteResponseData.msg}`); return res.status(500).send(data); } - activeUser = siteResponseData.result.totalClientNum; - connectedAp = siteResponseData.result.connectedApNum; - connectedGateways = siteResponseData.result.connectedGatewayNum; - connectedSwitches = siteResponseData.result.connectedSwitchNum; - const alertUrl = (controllerVersionMajor === 4) ? `${url}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000` : `${url}/${cId}/api/v2/sites/${siteName}/alerts/num?token=${token}¤tPage=1¤tPageSize=1000`; + // eslint-disable-next-line no-unused-vars [status, contentType, data] = await httpProxy(alertUrl, { headers: { "Csrf-Token": token, }, }); const alertResponseData = JSON.parse(data); + + activeUser = siteResponseData.result.totalClientNum; + connectedAp = siteResponseData.result.connectedApNum; + connectedGateways = siteResponseData.result.connectedGatewayNum; + connectedSwitches = siteResponseData.result.connectedSwitchNum; alerts = alertResponseData.result.alertNum; } diff --git a/src/widgets/omada/widget.js b/src/widgets/omada/widget.js index 0ef4177e0..5e32edad7 100644 --- a/src/widgets/omada/widget.js +++ b/src/widgets/omada/widget.js @@ -1,15 +1,7 @@ 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;