Writing a Basic Trigger

ARDI Trigger makes use of function decorators to make each of your triggered functions easy to read.

Creating the File

First, we start off with a basic Python file that imports the arditrigger module. We will call this file triggers.py.

import arditrigger
from arditrigger import trigger,data,when,timer,risingedge

#Content Here

arditrigger.Start("localhost","default")

You'll notice that we have two imports. The second set imports the individual decarators that apply to our triggers. We will cover these shortly.

At the end of our file, we call arditrigger.Start, passing the name of the server and the site we want to connect to.

Writing a Function

Next, we write a function that does something in response to a change in data.

In this case, we'd like to measure some key factors about the last product batch we produced. We will call it BatchReporting.

def BatchReporting(o):
   print("This batch is now complete!")

The function above announces that a particular batch of product is complete.

Each trigger function takes a single parameter, which is a Trigger object that will be sent by the framework when your trigger occurs.

But how to we set it up to be called when a change in the batch number occurs?

Adding Decorators and Conditions

@trigger
@data('Batch System.Batch Number')
@risingedge
@when(lambda o: o.Value() != o.OldValue() )
def BatchReporting(o):
   print("This batch is now complete!")

We've added several decorators in front of our function.

Trigger

Trigger marks the function as a trigger function (a function that gets called when a certain condition is met). This must be the first decorator.

Data

Data indicates which ARDI data points are needed. These are given as <asset name>.<property name», so in the example above we are reading the Batch Number property from the Batch System.

RisingEdge

RisingEdge instructs the system that the function should only be called on the rising edge (where the value of the When condition goes from False to True).

It should not be called when the function goes from True to False. See edge modes for more information.

When

When describes the condition function that triggers our event.

This is a lambda-expression that takes one parameter - the Trigger object.

In this case, the Value function gets the latest value of our data point, and the OldValue function gets the previous value the last time our trigger was run. So we are going to call the function whenever the batch number changes.

Trigger Data

@trigger
@data('Batch System.Batch Number')
@risingedge
@when(lambda o: o.Value() != o.OldValue() )
def BatchReporting(o):
   range = o.Range(utc=True)
   print("Reporting From " + str(range[0]) + " to " + str(range[1]))

The Range function asks for the time between now and the previous time the event was triggered. In this case, we are showing the full UTC time range of the event.

This is where we could request history to create a report, animation, summary values or anything else you'd care to create at the end of a batch.

For a complete script, see our Batch Reporting Example or the Event Alerting Example, and for details you can see the decorator and object reference.

There are also a couple of methods of launching your triggers.