mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-02 13:33:40 +01:00
working blueiris widget
This commit is contained in:
parent
6ba7e38d67
commit
6761a9e756
@ -864,5 +864,10 @@
|
|||||||
"notifications": "Notifications",
|
"notifications": "Notifications",
|
||||||
"issues": "Issues",
|
"issues": "Issues",
|
||||||
"pulls": "Pull Requests"
|
"pulls": "Pull Requests"
|
||||||
|
},
|
||||||
|
"blueiris": {
|
||||||
|
"serverName": "Server Name",
|
||||||
|
"numberOfActiveCams": "Active Cameras",
|
||||||
|
"numberOfAlerts": "Total Alerts"
|
||||||
}
|
}
|
||||||
}
|
}
|
37
src/widgets/blueiris/component.jsx
Normal file
37
src/widgets/blueiris/component.jsx
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { useTranslation } from "next-i18next";
|
||||||
|
|
||||||
|
import Block from "components/services/widget/block";
|
||||||
|
import Container from "components/services/widget/container";
|
||||||
|
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||||
|
|
||||||
|
export default function Component({ service }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const { widget } = service;
|
||||||
|
|
||||||
|
const { data: blueirisData, error: blueirisAPIError } = useWidgetAPI(widget, "unified", {
|
||||||
|
refreshInterval: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (blueirisAPIError) {
|
||||||
|
return <Container service={service} error={blueirisAPIError} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!blueirisData) {
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="blueiris.serverName" />
|
||||||
|
<Block label="blueiris.numberOfActiveCams" />
|
||||||
|
<Block label="blueiris.numberOfAlerts" />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="blueiris.serverName" value= {blueirisData.serverName} />
|
||||||
|
<Block label="blueiris.numberOfActiveCams" value={t("common.number", { value: blueirisData.numberOfActiveCams })} />
|
||||||
|
<Block label="blueiris.numberOfAlerts" value={t("common.number", { value: blueirisData.totalNumberOfAlerts })} />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
89
src/widgets/blueiris/proxy.js
Normal file
89
src/widgets/blueiris/proxy.js
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/* eslint-disable no-underscore-dangle */
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
|
const saltedMd5 = require('salted-md5');
|
||||||
|
|
||||||
|
const proxyName = "blueirisProxyHandler";
|
||||||
|
|
||||||
|
const logger = createLogger(proxyName);
|
||||||
|
let globalUserData = null;
|
||||||
|
|
||||||
|
const executeCMD = async (widgetUrl, body) => {
|
||||||
|
const url = `${widgetUrl}/json`
|
||||||
|
const params = {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body
|
||||||
|
};
|
||||||
|
const [status, , data] = await httpProxy(url, params);
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.debug(`HTTP ${status} performing Request`, data);
|
||||||
|
throw new Error(`Failed fetching`);
|
||||||
|
}
|
||||||
|
let jsonData
|
||||||
|
try {
|
||||||
|
jsonData = JSON.parse(data.toString());
|
||||||
|
} catch (e) {
|
||||||
|
logger.debug(`Failed parsing response:`, data);
|
||||||
|
throw new Error(`Failed parsing response`);
|
||||||
|
}
|
||||||
|
return jsonData;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getWidget = async (req) => {
|
||||||
|
const { group, service } = req.query;
|
||||||
|
if (!group || !service) {
|
||||||
|
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
if (!widget) {
|
||||||
|
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userLogin = async (widget) => {
|
||||||
|
let res = await executeCMD(widget.url, "{\"cmd\":\"login\"}");
|
||||||
|
const response = await saltedMd5(`${widget.user}:${res.session}:${widget.password}`, '', true);
|
||||||
|
res = await executeCMD(widget.url, `{"cmd":"login", "session":"${res.session}", "response":"${response}"}`);
|
||||||
|
return {"session": res.session, "serverName":res.data["system name"], "url": widget.url};
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeCamCount = async (widget, session) => {
|
||||||
|
const res = await executeCMD(widget.url, `{"cmd":"camlist", "session":"${session}"}`);
|
||||||
|
const cameraList = res.data.filter(item => !item.optionDisplay.includes('+') && item.isEnabled);
|
||||||
|
return cameraList;
|
||||||
|
};
|
||||||
|
|
||||||
|
const numberOfAlerts = async (widget, session) => {
|
||||||
|
const res = await activeCamCount(widget, session);
|
||||||
|
const promises = res.map(item => executeCMD(widget.url, `{"cmd":"alertlist", "camera":"${item.optionDisplay}", "session":"${session}"}`));
|
||||||
|
const results = await Promise.all(promises);
|
||||||
|
const alerts = results.reduce((acc, r) => acc + r.data.length, 0);
|
||||||
|
return alerts;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export default async function blueirisProxyHandler(req, res) {
|
||||||
|
const widget = await getWidget(req);
|
||||||
|
|
||||||
|
if (!globalUserData) {
|
||||||
|
globalUserData = await userLogin(widget);
|
||||||
|
}
|
||||||
|
const activeCams = await activeCamCount(widget, globalUserData.session);
|
||||||
|
const alerts = await numberOfAlerts(widget, globalUserData.session);
|
||||||
|
const data = {
|
||||||
|
"serverName":globalUserData.serverName,
|
||||||
|
"numberOfActiveCams":activeCams.length,
|
||||||
|
"totalNumberOfAlerts":alerts,
|
||||||
|
};
|
||||||
|
return res.status(200).send(data);
|
||||||
|
|
||||||
|
}
|
14
src/widgets/blueiris/widget.js
Normal file
14
src/widgets/blueiris/widget.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import blueirisProxyHandler from "./proxy";
|
||||||
|
|
||||||
|
const widget = {
|
||||||
|
api: "{url}/json",
|
||||||
|
proxyHandler: blueirisProxyHandler,
|
||||||
|
|
||||||
|
mappings: {
|
||||||
|
unified: {
|
||||||
|
endpoint: "/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default widget;
|
@ -8,6 +8,7 @@ const components = {
|
|||||||
autobrr: dynamic(() => import("./autobrr/component")),
|
autobrr: dynamic(() => import("./autobrr/component")),
|
||||||
azuredevops: dynamic(() => import("./azuredevops/component")),
|
azuredevops: dynamic(() => import("./azuredevops/component")),
|
||||||
bazarr: dynamic(() => import("./bazarr/component")),
|
bazarr: dynamic(() => import("./bazarr/component")),
|
||||||
|
blueiris: dynamic(() => import("./blueiris/component")),
|
||||||
caddy: dynamic(() => import("./caddy/component")),
|
caddy: dynamic(() => import("./caddy/component")),
|
||||||
calendar: dynamic(() => import("./calendar/component")),
|
calendar: dynamic(() => import("./calendar/component")),
|
||||||
calibreweb: dynamic(() => import("./calibreweb/component")),
|
calibreweb: dynamic(() => import("./calibreweb/component")),
|
||||||
|
@ -111,6 +111,7 @@ import xteve from "./xteve/widget";
|
|||||||
import jdrssdownloader from "./jdrssdownloader/widget";
|
import jdrssdownloader from "./jdrssdownloader/widget";
|
||||||
import urbackup from "./urbackup/widget";
|
import urbackup from "./urbackup/widget";
|
||||||
import wled from "./wled/widget";
|
import wled from "./wled/widget";
|
||||||
|
import blueiris from "./blueiris/widget";
|
||||||
|
|
||||||
const widgets = {
|
const widgets = {
|
||||||
adguard,
|
adguard,
|
||||||
@ -120,6 +121,7 @@ const widgets = {
|
|||||||
autobrr,
|
autobrr,
|
||||||
azuredevops,
|
azuredevops,
|
||||||
bazarr,
|
bazarr,
|
||||||
|
blueiris,
|
||||||
caddy,
|
caddy,
|
||||||
calibreweb,
|
calibreweb,
|
||||||
changedetectionio,
|
changedetectionio,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user