update dashboardAPI -AGENT

This commit is contained in:
znetsixe
2026-01-13 14:29:43 +01:00
parent c99a93f73b
commit 1ea4788848
16 changed files with 1202 additions and 8393 deletions

View File

@@ -1,108 +1,41 @@
const fs = require('node:fs');
const path = require('node:path');
const nameOfNode = 'dashboardapi';
const nodeClass = require('./src/nodeClass.js');
const { MenuManager } = require('generalFunctions');
module.exports = function (RED) {
function dashboardapi(config) {
// create node
RED.nodes.registerType(nameOfNode, function (config) {
RED.nodes.createNode(this, config);
this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
});
//call this => node so whenver you want to call a node function type node and the function behind it
var node = this;
const menuMgr = new MenuManager();
RED.httpAdmin.get(`/${nameOfNode}/menu.js`, (req, res) => {
try {
//fetch obj
const Dashboardapi = require("./dependencies/dashboardapi/dashboardapi_class");
//load user defined config in the node-red UI
const dConfig = {
general: {
name: config.name,
id: node.id,
logging: {
logLevel: config.logLevel,
enabled: config.enableLog,
},
},
grafanaConnector: {
host: config.host,
port: config.port,
bearerToken: config.bearerToken,
},
};
//make new measurement on creation to work with.
const d = new Dashboardapi(dConfig);
// put m on node memory as source
node.source = d;
function updateNodeStatus(val) {
if (val && val.grafanaResponse) {
// Check for a successful response from the Grafana API call
if (val.grafanaResponse.status === 200) {
node.status({
fill: "green",
shape: "dot",
text: "Grafana API: Success",
});
node.log("Grafana API call completed successfully.");
} else {
node.status({
fill: "red",
shape: "ring",
text: "Grafana API: Error",
});
node.error(
"Grafana API call failed with status: " +
val.grafanaResponse.status
);
}
}
}
//-------------------------------------------------------------------->>what to do on input
node.on("input", async function (msg, send, done) {
try {
switch(msg.topic) {
//on start make dashboard
case 'registerChild':
const childId = msg.payload;
const childObj = RED.nodes.getNode(childId);
if (!childObj || !childObj.source) {
throw new Error("Missing or invalid child node");
}
const child = childObj.source;
msg.payload = await d.generateDashB(child.config);
msg.topic = "create";
msg.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer glsa_gI7fOMEd844p1gZt9iaDeEFpeYtejRj7_cf1c41f8'// + config.bearerToken
};
console.log(`Child registered: ${childId}`);
send(msg);
break;
}
done();
} catch (err) {
node.status({ fill: "red", shape: "ring", text: "Bad request data" });
node.error("Bad request data: " + err.message, msg);
done(err);
}
});
// tidy up any async code here - shutdown connections and so on.
node.on("close", function () {
});
} catch (e) {
console.log(e);
const script = menuMgr.createEndpoint(nameOfNode, ['logger']);
res.type('application/javascript').send(script);
} catch (err) {
res.status(500).send(`// Error generating menu: ${err.message}`);
}
}
});
RED.nodes.registerType("dashboardapi", dashboardapi);
// Provide config metadata for the editor (local, no dependency on generalFunctions configs).
RED.httpAdmin.get(`/${nameOfNode}/configData.js`, (req, res) => {
try {
const configPath = path.join(__dirname, 'dependencies', 'dashboardapi', 'dashboardapiConfig.json');
const json = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const script = `
window.EVOLV = window.EVOLV || {};
window.EVOLV.nodes = window.EVOLV.nodes || {};
window.EVOLV.nodes.${nameOfNode} = window.EVOLV.nodes.${nameOfNode} || {};
window.EVOLV.nodes.${nameOfNode}.config = ${JSON.stringify(json, null, 2)};
`;
res.type('application/javascript').send(script);
} catch (err) {
res.status(500).send(`// Error generating configData: ${err.message}`);
}
});
};