Content
On this page:
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Content
Themes and user preferences
Content
Intellisense support
Content
Using the TK Toolkit Namespace
Content
Content
Incremental compiling
Content
Incorporating external assemblies
Content
Content
Leveraging the .NET Debugger
Content
Content
Content
Project Objects and Namespaces
Content
Displays, Code Behind
Content
Alarms, Notifications Event Handler
Content
Historian, Custom Retrieval and Archiving
Content
Utilizing ServerMain Class Entry Points
Content
Content
Content
Content
Content
New Organization to this module:
About the Script module
Purpose and Key Concepts
* Script Task
* Script Class
* Script Expressions
How the Script module Works
* Features highlights
* Supported Scripting Languages
* Tasks Execution Process
* Function Libraries in Script Classes
* Expressions Evaluation and Execution
Configuring the Script module
* Configuration Workflow
* Creating and Managing Script Tasks
** Pre-defined Tasks
* Adding and Editing Script Classes
* Pre-defined Classes
* Creating and Utilizing Expressions
* Managing External References
Programming with the Code Editor
* Productivity Enhancements
** Themes and User Preferences
** Intellisense Support
** Using the TK Toolkit Namespace
* Scripts Compilation
** Incremental Compiling
** Incorporating External Assemblies
* Debugging and Error Handling
** Leveraging the .NET Debugger
Working with the Script module
* Runtime Execution
* Integration with Other modules
** Project Objects and Namespaces
** Displays, Code Behind
** Alarms, Notifications Event Handler
** Historian, Custom Retrieval and Archiving
** Utilizing ServerMain Class Entry Points
* Advanced Features and Options
Troubleshooting and Best Practices
* Common Issues and Solutions
* Best Practices and Recommendations
* Script module Runtime Attributes
Text for about:
The Script Module is a powerful feature in Tatsoft FrameworX and FactoryStudio that enables users to execute custom code and extend the functionality of the entire platform. With support for multiple programming languages, including C#, VB.NET, Python, and JavaScript, the Script Module allows users to develop high-performance, server-side, and client-side scripts that can be easily integrated into their projects.
What is the Script Module
<< NOTE: When doing the new rogation this WHAT IS content shall be moved the section title Features Highlights, under the section How the script module Works
The Script Module brings to the Project configuration the ability to execute custom code and extend the functionality of the entire platform.
Programming languages included are C#, VB.NET, Python and JavaScript.
The Script Module feature set includes:
Delivers high-performance server-side and client-side scripting capability. Programming languages include C#, VB .NET, Python, and JavaScript. Easily create multi-threading applications with parallel execution for the task and add references to externally developed classes.
Key Concepts
Script Task
A Script Task defines a code that executes in reaction to an event (trigger) or periodically at specified intervals.
For example, you can create a Task to execute every second, or when any specific Tag in the Project has a new Value.
Script Class
A Script Class is a library of functions and methods that you can consume on Script Tasks, Expressions or from the Code Behind on the graphical Displays.
The use of Scripts Classes allows a more efficient and clean organization of Project logic and calculations.
Script Expressions
When you need to execute one line of code in a reaction to an event, instead of creating a Script Task, you can configure just one line at the Script Expressions table.
For example, when one tag changes its value, you need to set another tag value, or call a Script Class, it is easier to define just the one line Expression instead of a Task.
Configuring Tasks
Tasks are program units, written in VB.Net or C#, that execute either when a trigger event occurs or periodocally at specified intervals.
Project Built-in Tasks
All Projects includes the following built-in tasks:
Buit-in Scripts Tasks | |
---|---|
Task | When is Executed |
ServerStartup | Executed when the project starts running. Runs on the server (computer running TServer.exe). |
ServerShutdown | Executed when the project shuts down. Runs on the server. |
ClientStartup | Executed on each client when the Displays module starts running. |
ClientShutdown | Executed on each client when the Displays module close. |
Creating and Editing Tasks
To configure tasks:
Script Task Configuration Properties | |
---|---|
Column / Field | Description |
Name | Enter a name for the task. The system allows you to know if the name is not valid. |
Code | Read-only. This specifies the language used for the code for this task. By default, this is the language you selected when you created the project. From the Code Editor tab, you can change the code language. To change the project default, go to Info → Settings. |
Trigger | Enter or select the event (tag or other object) that should trigger the task execution, if any. The task executes when the value of the object changes. |
Period | Enter the time interval that should occur between executions of the task, if there is no trigger. |
Domain | Select where the script executes:
|
InitialState | Select the task's initial state:
|
BuildStatus | Read-only. Status of the task code from the continuous compiling process.
|
BuildErrors | Read-only. Displays any errors encountered during the last build. |
EditSecurity | Set the security to enable who can edit the tasks. |
Description | Enter a description of this task. |
Configuring Script Classes
Classes allows you to create a repository of class libraries, methods, and functions that you can use across the application. You can call them from tasks, other classes, and displays (code behind).
All Projects come with the following built-in classes:
Buit-in Scripts Tasks | |
---|---|
Task | When is Executed |
ServerMain | Methods library that executes on the server computer (computer running TServer.exe). |
ClientMain | Methods library available for .NET clients. |
Creating and Editing Script Classes
To configure Script Classes:
Script Class Configuration Properties | |
---|---|
Field | Description |
Name | Enter a name for the class. The system lets you know if the name is not valid. |
Code | Read-only. This specifies the language used for the code for this task. By default, this is the language you selected when you created the project. From the Code Editor tab, you can change the code language. To change the project default, go to Project → Settings. |
Domain | Select where the class executes:
|
Content | Read-only. Shows the type of the content in the class (e.g.: method or namespace) |
EditSecurity | Set the security to enable who can edit the tasks |
BuildOrder | Set the order to build the classes |
BuildStatus | Read-only. Status of the class code from the continuous compiling process.
|
BuildErrors | Read-only. Displays any errors encountered during the last build. |
Description | Enter a description of this class. |
Working with the Code Editor
When Selecting Edit → Scripts → CodeEditor, the Code Editor environment is displayed with the contents of the last selected Script Task or Script Class.
While in that environment, you can change the task or class you are editing using the ComboBox at the top area.
IntelliSense
InteliSense is a code-completion aid that features listing the available properties and methods for the objects and auto completing their names.
Additionally to the Properties and Methods of the .NET Framework, it also supports all Project entities, like Tags and Displays. Just type a dot "." after an element to show its available properties.
Editing Code
You can write code in either standard VB.Net or C#. You can also switch between both. If you change your language selection in the code editor, the system automatically converts existing code to the selected language.
For Python code, the editing is executed at the Python editor and a reference the program created is added.
To edit code:
Using the .NET Debugger
Creating debugger Information
The platform has an integrated .NET code debugger. In order to use it, the local computer must have a cache with the necessary files to run the debugger. The steps to enable the debugger are:
Attaching the .NET debugger
In order to have a .NET debugging session, the engineering environment must be connected to the runtime, and the .NET debugger must be attached to server or client process. Follow these steps.
Breakpoints, Steps, and Watch
In order to setup a breakpoint, open the desired code, select the line, and press Insert Breakpoint on the toolbar.
When the system stops on a breakpoint, you can perform step by step execution or click the continue button.
In order to inspect local .NET variables, tags, or project objects, select the text in the script editor. When the execution is stopped on a breakpoint, the toolkit will show the current value of the variable.
You can also add .NET variables or project objects to the Watch window. When adding tags or project objects, you need to use the at symbols @, like @tag.tag1
. This will allow the system to know it is a project object and not a local .NET variable. Keep in mind that the Watch display is only updated when execution is stopped. If you want to have real-time values for tags and objects, open the PropertyWatch diagnostics tool.
Configuring Expressions
Overview
Besides the Script → Expressions table, you can use expressions in several other places by using the syntax described here. This can be done to determine a value or to configure a condition.
Expressions are plain VB.Net expressions, such as arithmetic expressions or calls to script.class methods from the project. IntelliSense only shows tags and application objects, but the expressions are compiled using the standard VB.Net compiler. Whatever you would write in the code editor should be accepted in expressions as well.
Our platform implements automated replacements, such as "==" to "=". The syntax of an expression is close to a C# statement but without a need to add the semicolon “;” at the end.
Therefore, both VB.Net and C# programmers are able to use the expression fields seamlessly.
In expressions, you do not need to put an "@" before tag names. You need the "@" in the code editor to differentiate project tags from .NET variables. However, expressions do not have local .NET variables, so you use the project object directly.
For arithmetic operands, use the standard operands as described in the .NET documentation.
To allow single-line evaluations, the .NET language has the IIF command, which currently is used only with VB.Net. The IIF command has three parameters.
IIF (A, B, C) |
The first parameter is a condition. This method will return B if condition A is true, and it returns C if the condition is false.
IIF (tag.A = 1, "True", "False") will return the string "True" if the value of tag.A is 1, or "False" if tag.A has a different value. |
In this .NET method, all three parameters are evaluated regardless of the condition. For instance, if you have IIF (tag.A = 1, script.class.client.Func1(), script.class.client.Func2())
, both Func1 and Func2 will always be executed. Only the return value will be different based on the value of tag.A.
The IF or IIF methods need to evaluate the parameters before calling the method. There are many scenarios where you may want to execute only the function according to the value. For these scenarios, FactoryStudio has a method called TIF.
Use the expression:
TIF (tag.A = 1, script.class.client.Func1(), script.class.client.Func2())
Only the Func1() or Func2() will be executed, according the value of Tag.A
The TIF method is defined in the class library that is automatically exposed to expressions that are in the toolkit function library.
For more complex calculations, you can call a class that you create on the Classes tab. See Configuring Classes earlier in this chapter.
Adding or editing Script Expression
Script Expressions Configuration Properties | |
---|---|
Field | Description |
Object | Select an existing tag or object. |
Expression | Enter the expression. The expression can be a basic mathematical expression, a class, or a conditional expression. |
Domain | Select where the expression executes:
|
Execution | Select when the expression executes: OnChange The expression executes when the value of any tag in the expression changes. TriggerOrTimeInterval The expression executes when the trigger event occurs or when the interval set in the period elapses. ChangeOrStartup The expression executes when the value of any tag in the expression changes or at startup. TriggerOrTimeOfDay The expression executes when a trigger event occurs or on a specific time of day |
Trigger | Enter or select the tag or object that triggers the expression execution. The expression executes when the value of the object changes. |
DisableCondition | Enter or select the tag or object that disables the expression execution. |
Time | Specify the time when the expression runs. |
Label | Set a label to the specified class. |
Build Messages | Return the message status after the expression runs |
BuildStatus | Read-only. Set after you click Verify.
|
BuildErrors | Read-only. Displays any errors encountered during the last build. |
Scripts Compiling
Incremental Compiling
All C# and VB.NET scripts are automatically compiled in background as you create them, without the needs of expressly request it.
For performance optimization, only the scripts you just changed are compiled, not the entire project. This method of compiling only the affected parts - not the entire system again - is called Increment Compiling.
This concept is Important to understand why their the command of Build for a Project. The Build Command will recompile all the scripts on the entire Project. <<<<. please check this paragraph. the first info is a little confusing >>>>
It is recommend to execute a Build before deploying the project for Production. When upgrading or Importing Projects it is mandatory to perform a Project Build Operation.
When you use the code editor, the system constantly compiles code in the background. You can also build the whole project, as needed. For more information, see Running the Application. |
Using External Assemblies
If you need references to your own assemblies, you can use Project → Build → References.
As a fully compliant .NET application, you can find free source code to use, including .NET components, products, and libraries. Plus, you can use your own libraries.
The TK Toolkit namespace
Our platform exposes many .NET libraries to the application, but there are a few methods that are frequently required, such as type conversion, copying tags to DataTables and .NET objects, or dynamically changing the communication settings, and others.
Those methods included in a library toolkit. To use these methods, you need to put TK.
in the code editor. IntelliSense will respond with a list of all available methods and provide a summary documentation for any method you select.
double x = TK.ConvertTo<double>("123"); |
The Script Namespace
The namespace Script is the entry point for all objects related to the Scripts Module.
The Script.Task object lists all configured database connections and their runtime properties.
The Script.Class object lists all configured tables and its runtime properties.
Examples:
Script Module properties examples | ||
---|---|---|
Property | Type | Description |
Script.IsStarted | Boolean | Flag indicating if the Module Dataset has started. |
Script.OpenStatusMessage | String | Message OK or error when initiating the Module. |
Script Task properties examples | ||
---|---|---|
Property | Type | Description |
Script.Task.Task1.ExecutionCount | Integer | Number of times the Script Task <Task1> was executed. |
Script.Task.Task1.LastCPUTime | TimeSpan | The Amount of CPU used the last time the Script Task Task1 was executed. |