module.exports = function (RED) { function dashboardapi(config) { // create node RED.nodes.createNode(this, config); //call this => node so whenver you want to call a node function type node and the function behind it var node = this; 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); } } RED.nodes.registerType("dashboardapi", dashboardapi); };