You're currently browsing the Ignition 8.0 docs. Click here to view the latest docs.

Gateway Event Scripts Overview

Gateway Event Scripts are scripts that run directly on the Gateway. They are useful because they always run, regardless if any sessions or clients are open. They also provide a guaranteed way to make sure only a single execution of a particular script occurs at a time, as opposed to placing a script in a window, as there could be multiple instances of the window open at a given point of time.

Note that even though Gateway Event Scripts run on the Gateway, they're still considered a project resource. Project backups will include any Gateway Event Scripts.  

The Gateway Event scripting workspace is located in the Scripting menu of the Designer or in the Project Browser under Scripting > Gateway Events.


On this page ...

Other Event Scripts

The content on this page will focus primarily on Gateway Event Scripts. However, there is some overlap with Client Event Scripts, as they have similar events. More information can be found on the Client Event Scripts and Perspective Session Event Scripts pages.

System functions are available for both Client Event Scripts and Gateway Event Scripts, but some system functions are specific to either one or the other. When you're writing event scripts, it's important to remember the scope of where you're writing the script: Client or Gateway. You can check Scripting Functions in the Appendix to see list of all system functions, their descriptions, and what scope they run in. 


Gateway vs Client Event Scripts

Startup Script

The Startup Script event runs at the startup of the Gateway. Additionally, if the project is restarted in someway, such as by making a change to a Gateway Event Script and saving, then the Startup Script will be called. This means that while editing scripts frequently in the Designer, the startup and shutdown events may happen frequently.

Gateway Startup Behavior

There is a specific order to when the various startup scripts are run. When troubleshooting your Gateway startup times, consider the following:

  1. Gateway starts - The Gateway will start as an OS service, and start the context. No startup scripts can run before this is complete.
  2. Projects are started - This includes all of the Gateway scoped items in the projects such as Transaction Groups, SFCs, etc. This does not refer to launching clients, and no clients can be automatically launched at this time. All Gateway Startup Scripts are run at this time for each project. Note: if you copied a project, always check for Gateway scoped events such as these. You generally don't want a Gateway Startup Script to run twice because it is in two projects.


Startup Scripts

The following feature is new in Ignition version 8.0.5
Click here to check out the other new features

Update Script

The Update Script event runs after a project is saved or updated on the Gateway. This enables you to insert a script that will run every time a project is saved.

Shutdown Script

The Shutdown Script event runs at the shutdown of the project, which means it can be used as a way to trigger a script when the Gateway has to be restarted. It allows you to run a piece of code as the shutdown is occurring. After the script completes, the shutdown will finish.

Note that the Shutdown Script event only gets called if the Gateway is requested to shut down: if the computer power is lost abruptly (power outage, hard restart, etc.) this shutdown script will not run.

Gateway Shutdown Behavior

Similar to how a Startup Script behaves, "Shutdown" in the context of this event means "project shutdown", so shutting down the Gateway would trigger this event, as well as disabling the project containing a script on this event. Additionally, making a change to a Gateway Event Script in the Designer, and then saving the project will cause the project to restart, which means this event can get called by simply making changes in the Designer and saving.


Shutdown Scripts

Timer Scripts

The Timer Scripts execute periodically on a timer at a fixed delay or rate. This allows you to set up a sort of heartbeat that can run on the Gateway. This is the ideal event to use if you need the Gateway to periodically perform some scripting task.


Timer Scripts

Since multiple Timer Scripts can be added, there are separate buttons that allow you to manage each Timer Script.

  •  Add Timer Script - Adds a new Timer Script.
  •  Remove Timer Script - Will delete the selected Timer Script.
  •  Modify Timer Script - Will modify the settings for the selected Timer Script.

Timer Script Settings

Below is an overview of the settings for a Timer Script.

  • Name: The name of the Timer script. Names must be unique per project, so two timer scripts in the same project cannot have the same name. 
  • Delay: The delay period in milliseconds. The meaning of this setting is dependent on the Delay Type setting.
  • Enabled: Allows you disable the Timer Script when set to false. 
  • Delay Type: Determines how the Delay setting is utilized. 
    • Fixed Delay timer script (the default) waits for the given Delay between each script invocation. This means that the script's rate will actually be the delay plus the amount of time it takes to execute the script. This is the safest option since it prevents a script from mistakenly running continuously because it takes longer to execute the script than the delay.
    • Fixed Rate scripts attempt to run the script at a fixed rate relative to the first execution. If the script takes too long, or there is too much background process, this may not be possible. See the documentation for java.util.Timer.scheduleAtFixedRate() for more details.
  • Threading: Determines which thread this script should run in. In other words, this setting allows you to specify if you want this timer script to share execution resources or not. The rule of thumb here is that quick-running tasks should run in the shared thread, and long-running tasks should get their own dedicated thread.
    • The Shared setting means that all timer scripts will share a thread. This is usually desirable, as it prevents creating lots of unnecessary threads: threads have some overhead, so a small amount of resources are used per thread. However, if your script takes a long time to run, it will block other timer tasks on the shared thread. 
    • The Dedicated setting will create a separate thread specifically for the timer script to use. This setting is desirable when your scripts executions must be as consistent as possible, as other timer scripts can't slowdown or otherwise impact the execution of a script in a separate thread. 

Tag Change Scripts

The Tag Change Script event allows you to specify any number of Tags, and trigger a script when one of them change. Since these execute based on a Tag changing value, Tag Change Scripts are ideal when you need a script to run based on some some signal from a PLC. 

Having the Tag Change Scripts run in the Gateway Scope means that the scripts are active as long as the Gateway is running. Thus, you do not need a client or session to be open for a Gateway Tag Change Script to execute. When executing, each Tag Change Script runs in a separate thread. This prevents long running scripts from blocking the execution of other Tag Change Scripts. 

Due to their nature, Client Tags may not trigger Gateway Tag Change Scripts. However, there are other similar events, such as a Client Event Script, that enable you to trigger a script based on a Client Tag changing value. 

Tag Change Script Interface

Tag List

Lists all of the available Tag Change scripts in the project. Two icons are available below the list:

  •  Add Script - Adds a new Tag Change Script to the list.
  •  Remove Script - Removes the currently selected script from the list.

Tags Tab

The Tags tab contains settings for the script. See the Script Settings description below

  • Script Name - The name of the script. Script names must be unique per project. 
  • Enabled - Determines if the script is active or not. Set to false to disable the script. 
  • Change Triggers - When the Tag changes, the script can trigger based on the Value, Quality, and/or Timestamp. Note that regardless of how many triggers changed, the script only executes once per tag, so leaving all triggers enabled will not trigger three executions each time the Tag changes.
  • Tag Paths - A list of Tag paths to monitor. When any of the Tags listed in this area change, the script will trigger. Note that the list is not comma separated: new paths are specified each line.
    •   Tag Browser - Opens a Tag Browser window, allowing you to quickly lookup and add Tag paths to the Tag Change Script. 
    • Path Diagnostic - Click this icon to verify the paths specified under the Tag Paths list. This is useful for checking for typos in the list. 

      The following feature is new in Ignition version 8.0.5
      Click here to check out the other new features

    • Wildcards can be used at the end of configured tag paths. Using a wildcard will execute the script for all tags within the same folder. In the below example, the script "New Script" will run when the value of any tag in the "Test" folder changes.

      Note: Wildcards can only be used at the folder level and cannot be used at the tag level. For example, configuring a tag path like [provider]folder/* will execute a script on all tags within the folder, but [provider]folder/ramp* will not.

Script Tab

The Script tab is where the Python script associated with this event will be placed. Two icons are available below the list:

 Add Script - Adds a new Tag Change Script to the list.

 Remove Script - Removes the currently selected script from the list.

Tag Change Objects

Tag Change Scripts contain several built-in objects that are useful for inspecting the event, such as seeing what value the Tag changed to. These objects are listed below: 

The initialChange Value

A variable that is a flag (0 or 1) which indicates whether or not the event is due to the initial subscription or not. This is useful as you can filter out the event that is the inital subscription, preventing a script from running when the values haven't actually changed.

if not initialChange:

    # Do something useful here    

The newValue Object

A QualifiedValue object that contains the following methods

MethodDescriptionUsage Example
Returns the new value on the tag
Returns the new quality on the tag
Returns the new timestamp value

The event Object

This object offers some additional utility, such as accessing the previous values on the tag.

MethodDescriptionUsage Example
Returns a QualifiedValue object (similar to the newValue object), representing the new values on the tag.
Returns a QualifiedValue object, representing the values on the tag before the change.
Returns a TagPath object, that can be further examened for details about the path on the tag that changed. See the TagPath Object table below. Additionally, the TagPath object can easily be turned into a string, providing quick access to the path of the tag that changed value.
Returns the new value on the tag, similar to getCurrentValue(). This method is a convenient way to retrieve just the new value, without needing to interact with the QualifiedValue object returned by getCurrentValue().

The TagPath Object

These methods are available on the TagPath object returned by event.getTagPath() calls

MethodDescriptionUsage Example
Returns the name of the item at the end of the path, which can be used to get the name of the tag that changed.
Returns the path to the Tag's parent folder.


Tag Change Scripts

Message Scripts

Message Handlers allow you to write a script that will run in the scope they are located in, but they can be invoked by making a call from other projects or even other Gateways. They can be called using three different scripting functions: system.util.sendMessagesystem.util.sendRequest, and system.util.sendRequestAsync.

Under the list of handlers, three small buttons allow you to add, remove and manage your handlers.

 Add Message Handler - Will add a message handler.

 Remove Message Handler - Will delete the highlighted message handler.

 Modify Message Handler - Will modify the settings for the highlighted message handler.

Gateway Message Handler Settings

When adding or modifying a message handler, a Message Handler settings window will popup.

The following settings are available:

Name - The name of the message handler. Each message handler must have a unique name per project. 

Threading - Determines the threading for the message handler. Contains the following options:

Shared - The default way of running a message handler. Will execute the handler on a shared pool of threads in the order that they are invoked. If too many message handlers are called all at once and they take long periods of time to execute, there may be delays before each message handler gets to execute.

Dedicated - The message handler will run on its own dedicated thread. This is useful when a message handler will take a long time to execute, so that it does not hinder the execution of other message handlers. Threads have a bit of overhead, so this option uses more of the Gateway's resources, but is desirable if you want the message handler to not be impeded by the execution of other message handlers. 

Security - Allows you to specify security zone and role combinations that are allow to request this message handler. 

The Payload

Inside the message handler is your script. The script will have a single object available to it, the payload. The payload is a dictionary containing the objects that were passed into it. In essence, the payload is the mechanism that allows you to pass the message handler values. 

The payload is simply a python dictionary, so extracting values involves specifying the key:

Pseudocode - Payload Values
value1 = payload["MyFirstValue"] #"MyFirstValue" is the key that is associated with a value. We are taking the value associated with MyFirstValue, and assigning it to value1.
value2 = payload["MySecondValue"] #Similarly, we are taking the value associated with MySecondValue and assigning it to value2.

Calling Message Handlers

Once you have your message handlers created, you can then call them from a script using one of three scripting functions: system.util.sendMessagesystem.util.sendRequest, and system.util.sendRequestAsync. These functions allow you to call a message handler in any project, even if the project that the message handler resides on is different from the one you are calling it from. The message handler will then execute in the scope in which it was created, and will use any parameters that you pass in through the payload.

Pseudocode - Calling a Message Handler
messageHandler="My Message Handler"
myDict = {'MyFirstValue': "Hello", 'MySecondValue': "World"}
results=system.util.sendMessage(project, messageHandler, myDict)


Script Messaging

Troubleshooting Gateway Scripts

While they are technically project resources, remember that Gateway Event Scripts technically run on the Gateway. Thus the Status section of the Gateway is useful for diagnosing issues with Gateway Event Scripts. 

  • No labels