Funktionen

Framework Control Funktionen, die in der Description.json definiert werden, müssen in der Control.js/ts definiert und implementiert werden. Die Funktion muss wie der „name“ der Funktion in der Description.json benannt werden.

Funktion ohne Übergabeparameter

Eine Funktion ohne Übergabeparameter wird in der Description.json wie folgt definiert:

"functions": [
   {
      "name": "customFunction",
      "displayName": "customFunction",
      "visible": true,
      "description": "This is a sample for a function that can be called.",
      "category": "Actions",
      "params": [],
      "type": null,
      "heritable": true,
      "searchTerms": []
   }
]

Daraus ergibt sich der folgende Code:

TypeScript:

/**
* This is the call of the api function 'customFunction'.
*/
public customFunction(): void {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the engineering in Visual Studio
   */

   // implement function logic here
}

JavaScript:

/**
* This is the call of the api function 'customFunction'.
* @returns {void}
*/
customFunction() {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the engineering in Visual Studio
   */

   // implement function logic here
}

Innerhalb der Funktion kann die beliebige Ausführungslogik der Funktion implementiert werden.

Funktion mit Rückgabewert

Bei den Funktionen besteht die Möglichkeit, einen Rückgabewert an die aufrufende Stelle im Engineering zurückzugeben. Der Datentyp des Rückgabewertes wird in der Description.json unter „type“ definiert.

"functions": [
   {
      "name": "customFunction2",
      "displayName": "customFunction2",
      "visible": true,
      "description": "This is a sample for a function that can be called by the engineering.",
      "category": "Actions",
      "params": [ ],
      "type": "tchmi:general#/definitions/Boolean",
      "heritable": false,
      "searchterms": []
   }
]

Innerhalb der Funktion in der Control.js/ts besteht dann die Möglichkeit, einen Wert des entsprechenden Typs zurückzugeben.

TypeScript:

/**
* This is the call of the api function 'customFunction2'.
*/
public customFunction2 (): boolean {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the enginnering in the twincat
   */

   // implement function logic here
   return true;
}

JavaScript:

/**
* This is the call of the api function 'customFunction2'.
* @returns {Boolean}
*/
customFunction2 () {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the enginnering in the twincat
   */

   // implement function logic here
   return true;
}

Funktion mit Übergabeparameter

Eine Funktion mit Übergabeparameter wird in der Description.json wie folgt definiert:

"functions": [
   {
      "name": "customFunction3",
      "displayName": "customFunction3",
      "visible": true,
      "description": "This is a sample for a function that can be called by the engineering.",
      "category": "Actions",
      "params": [
         {
            "name": "functionParameter1",
            "displayName": "functionParameter1",
            "description": "This is a sample for a function parameter.",
            "type": "tchmi:general#/definitions/Number",
            "visible": true
         },
         {
            "name": "functionParameter2",
            "displayName": "functionParameter2",
            "description": "This is a sample for a function parameter.",
            "type": "tchmi:general#/definitions/Boolean",
            "visible": true
         }
      ],
      "type": null,
      "heritable": false,
      "searchterms": []
   }
]

Die Funktion wird in der Control.js/ts wie eine normale Funktion implementiert, wobei die Übergabeparameter bei dem Funktionsaufruf mit übergeben werden. Die Parameter müssen vor der Verwendung auf Gültigkeit überprüft werden, ähnlich wie bei den Attribut-Settern.

TypeScript:

/**
* This is the call of the api function 'customFunction3'.
*/
public customFunction3(functionParameter1: number, functionParameter2: boolean): void {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the enginnering in the twincat
   */
   let convertedParameter1 = TcHmi.ValueConverter.toNumber(functionParameter1);
   if (convertedParameter1 === null) {
      // parameter is null: define what to do in this case

      // exit function call if the execution without this parameter is not possible
      return;
   }

   let convertedParameter2 = TcHmi.ValueConverter.toBoolean(functionParameter2);
   if (convertedParameter2 === null) {
      // parameter is null: define what to do in this case

      // exit function call if the execution without this parameter is not possible
      return;
   }

   // implement function logic with the evaluation of the parameters here
}

JavaScript:

/**
* This is the call of the api function 'customFunction3'.
* @param {number} functionParameter1
* @param {boolean} functionParameter2
* @returns {void}
*/
customFunction3(functionParameter1, functionParameter2) {
   /**
   * customFunction is defined in the description.json and called from outside.
   * The api functions are shown in the graphical user interface of the engineering in Visual Studio
   */
   var convertedParameter1 = TcHmi.ValueConverter.toNumber(functionParameter1);
   if (convertedParameter1 === null) {
      // parameter is null: define what to do in this case

      // exit function call if the execution without this parameter is not possible
      return;
   }

   var convertedParameter2 = TcHmi.ValueConverter.toBoolean(functionParameter2);
   if (convertedParameter2 === null) {
      // parameter is null: define what to do in this case

      // exit function call if the execution without this parameter is not possible
      return;
   }

   // implement function logic with the evaluation of the parameters here
}