Synology widget : working on code stability
This commit is contained in:
parent
2401312869
commit
a57ec06a30
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
@ -7,38 +8,36 @@ const proxyName = "synologyProxyHandler";
|
|||||||
|
|
||||||
const logger = createLogger(proxyName);
|
const logger = createLogger(proxyName);
|
||||||
|
|
||||||
const authApi = "{url}/webapi/entry.cgi?api=SYNO.API.Auth&version=7&method=login&account={username}&passwd={password}&format=cookie"
|
|
||||||
|
|
||||||
function formatUptime(uptime) {
|
function formatUptime(uptime) {
|
||||||
const [hour, minutes, seconds] = uptime.split(":");
|
const [hour, minutes, seconds] = uptime.split(":");
|
||||||
const days = Math.floor(hour/24);
|
const days = Math.floor(hour/24);
|
||||||
const hours = hour % 24;
|
const hours = hour % 24;
|
||||||
|
|
||||||
return `${days} days ${hours}:${minutes}:${seconds}`
|
return `${days} d ${hours}h${minutes}m${seconds}s`
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getApiInfo(api, widget) {
|
async function getApiInfo(api, widget) {
|
||||||
const infoAPI = "{url}/webapi/entry.cgi?api=SYNO.API.Info&version=1&method=query"
|
const infoAPI = "{url}/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query"
|
||||||
|
|
||||||
const infoUrl = formatApiCall(infoAPI, widget);
|
const infoUrl = formatApiCall(infoAPI, widget);
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [status, contentType, data] = await httpProxy(infoUrl);
|
const [status, contentType, data] = await httpProxy(infoUrl);
|
||||||
|
|
||||||
let path = "Method unavailable";
|
|
||||||
let minVersion = 0;
|
|
||||||
let maxVersion = 0;
|
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
const json = JSON.parse(data.toString());
|
const json = JSON.parse(data.toString());
|
||||||
if (json.data[api]) {
|
if (json.data[api]) {
|
||||||
path = json.data[api].path;
|
const { path, minVersion, maxVersion } = json.data[api];
|
||||||
minVersion = json.data[api].minVersion;
|
return [ path, minVersion, maxVersion ];
|
||||||
maxVersion = json.data[api].maxVersion;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [null, null, null];
|
||||||
return [path, minVersion, maxVersion];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(widget) {
|
async function login(widget) {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
const [path, minVersion, maxVersion] = await getApiInfo("SYNO.API.Auth", widget);
|
||||||
|
const authApi = `{url}/webapi/${path}?api=SYNO.API.Auth&version=${maxVersion}&method=login&account={username}&passwd={password}&format=cookie`
|
||||||
const loginUrl = formatApiCall(authApi, widget);
|
const loginUrl = formatApiCall(authApi, widget);
|
||||||
const [status, contentType, data] = await httpProxy(loginUrl);
|
const [status, contentType, data] = await httpProxy(loginUrl);
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
@ -69,11 +68,15 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
let [status, contentType, data] = await login(widget);
|
let [status, contentType, data] = await login(widget);
|
||||||
|
|
||||||
const { sid }=JSON.parse(data.toString()).data;
|
const { sid }=JSON.parse(data.toString()).data;
|
||||||
const api = "SYNO.Core.System";
|
let api = "SYNO.Core.System";
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
let [ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
||||||
|
|
||||||
const storageUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&type="storage"&_sid=${sid}`;
|
const storageUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&type="storage"&_sid=${sid}`;
|
||||||
|
|
||||||
[status, contentType, data] = await httpProxy(storageUrl );
|
[status, contentType, data] = await httpProxy(storageUrl );
|
||||||
|
|
||||||
|
|
||||||
let usedVolume = 0;
|
let usedVolume = 0;
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
return res.status(status).set("Content-Type", contentType).send(data);
|
return res.status(status).set("Content-Type", contentType).send(data);
|
||||||
@ -86,6 +89,7 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
|
|
||||||
const healthUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&_sid=${sid}`;
|
const healthUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&_sid=${sid}`;
|
||||||
[status, contentType, data] = await httpProxy(healthUrl);
|
[status, contentType, data] = await httpProxy(healthUrl);
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
return res.status(status).set("Content-Type", contentType).send(data);
|
return res.status(status).set("Content-Type", contentType).send(data);
|
||||||
}
|
}
|
||||||
@ -94,10 +98,20 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
return res.status(401).json({ error: "Error getting uptime" });
|
return res.status(401).json({ error: "Error getting uptime" });
|
||||||
}
|
}
|
||||||
const uptime = formatUptime(json.data.up_time);
|
const uptime = formatUptime(json.data.up_time);
|
||||||
|
api = "SYNO.Core.System.Utilization";
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
[ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
||||||
|
const sysUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=get&_sid=${sid}`;
|
||||||
|
[status, contentType, data] = await httpProxy(sysUrl );
|
||||||
|
|
||||||
|
const memoryUsage = 100 - (100 * (parseFloat(JSON.parse(data.toString()).data.memory.avail_real) + parseFloat(JSON.parse(data.toString()).data.memory.cached)) / parseFloat(JSON.parse(data.toString()).data.memory.total_real));
|
||||||
|
const cpuLoad = parseFloat(JSON.parse(data.toString()).data.cpu.user_load) + parseFloat(JSON.parse(data.toString()).data.cpu.system_load);
|
||||||
|
|
||||||
const resdata = {
|
const resdata = {
|
||||||
uptime,
|
uptime,
|
||||||
usedVolume
|
usedVolume,
|
||||||
|
memoryUsage,
|
||||||
|
cpuLoad,
|
||||||
}
|
}
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
return res.status(status).send(JSON.stringify(resdata));
|
return res.status(status).send(JSON.stringify(resdata));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user