Funktionen

Framework Control Funktionen, die in der Description.json definiert werden, müssen in der Source.js 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 für die Source.js:

/**
* @description This is the call of the api function 'customFunction'.
* @returns {void}
*/
FrameworkControlJs1.prototype.customFunction = function () {
   /**
   * 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 Rückgabewert wird in der Description.json unter „type“ definiert.

"type": "tchmi:general#/definitions/Boolean"

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

/**
* @description This is the call of the api function 'customFunction'.
* @returns {Boolean}
*/
FrameworkControlJs1.prototype.customFunction = function () {
   /**
   * 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
   return true;
};

Funktion mit Übergabeparameter

Eine Funktion mit Ü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 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 Source.js 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.

/**
* @description This is the call of the api function 'customFunction'.
* @returns {void}
*/
FrameworkControlJs1.prototype.customFunction = function (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 enginnering in the twincat
   */
   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
};