Skip to content

Logic File

Akın Şibay edited this page Oct 15, 2024 · 5 revisions

This file has a .node.ts extension and defines what the node will do when it starts running. It is the functional part of the node.

There are some important built-in functions you should know when you are creating node class:

  • getParameter(name: string)

    It is used to get the data in a desired input of the node. When you give the name of the value input as a parameter, you can get the relevant input value.

  • getAllParameters()

    It is used to get all input parameters. You get them all as an object.

  • getAllNodeConfig()

    It is used to get all configuration parameters. You get them all as an object.

  • execute()

    The function where the main function of the node is defined. You can create any methods you want in the node class you create, but the main method that the system will use to run the node is the execute method.

  • registerEvent(eventName: string, customEventFunc: CustomEventFunc)

    All nodes can have event inputs and outputs. start, end, error are built-in event handles. You may need to create custom event handles depending on the function of your node. For example, if you are writing a Counter node, you can write a clear event handle and have the counter stop when triggered. This function is used to introduce similar custom event outputs to the node.

  • returnOutput(data: OutputData)

    It is used to send values ​​to the output ends of the node. After the function written inside the execute method is finished, it will transfer the result of the function to the node outputs. At this point, the desired value can be transferred to the desired output by using the returnOutput method and giving the node's output object as a parameter.

Note: All nodes are derived from the BaseNode class. Any class that does not derive from the BaseNode class is not a node. The above built-in methods are derived from the BaseNode class. If they do not derive from here, these methods will not be accessible.

Samples:

  • HTTP Request Node:
import axios from "axios";
import BaseNode from "../../BaseNode";

export default class HttpRequest extends BaseNode {
  async execute() {
    const url = this.getNodeConfig("url") as string;
    const { data } = await axios.get(url);
    this.returnOutput({
      response: data,
    });
  }
}
  • PID Node:
import BaseNode from "../../BaseNode";
import { Node } from "../../types";

export default class PID extends BaseNode {
  constructor(self: Node) {
    super(self);
    this.registerEvent("stop", this.stopEvent);
  }

  stopEvent() {
    console.log("PID STOP EVENT RUN !!!");
    const parameters = this.getAllNodeConfig();
    const stopParameters = {
      ...parameters,
      on_off: "off",
    };
    this.returnOutput({
      pid: { ...stopParameters },
    });
  }

  async execute() {
    const setPoint = this.getParameter("SetPoint");
    const parameters = this.getAllNodeConfig();
    this.sendStatus({ message: "worked", color: "success" });
    this.returnOutput({
      pid: { setPoint, ...parameters },
    });
  }
}
  • Combine Node:
import BaseNode from "../../BaseNode";

export default class Combine extends BaseNode {
  async execute() {
    let combined = "";
    const spaceCharacter = this.getNodeConfig("space_character");

    Object.values(this.getAllParameters()).forEach((param: string) => {
      combined = `${combined}${spaceCharacter}${param}`;
    });
    this.returnOutput({
      result: combined,
    });
  }
}
  • Split Node:
import BaseNode from "../../BaseNode";

export default class Split extends BaseNode {
  async execute() {
    const splitCharacter = this.getNodeConfig("split_character");
    const splitted = this.getParameter("value").split(splitCharacter);

    this.returnOutput({
      result: splitted,
    });
  }
}

The above examples are examples of using methods inherited from the BaseClass class and creating nodes that do meaningful work with it. The .node.ts file focuses only on the function of the node. When you call this class in the class property of the object in the .ui.ts file, you combine these functions with the appearance of that node.

The logic of creating a new node is like this. For development suggestions and discussions, please contact us via Github discussion or discord server.

Clone this wiki locally