Code Snippets

Code snippets are small piece of code that illustrate a single function or piece of data.  Complete applications, most with source code in C# or Flash, are available in the Sample Applications section.

There are three kinds of code snippets available:

  • Expression Rules, which are triggered by timers or changes in asset state
  • Groovy scripts that are called from Expression Rules
  • Groovy scripts that are called from web services via Scripto

There is one snippet, Expression Rule Patterns for Alarms, illustrating how stacking of Expression Rules can do more than a single rule in isolation.

For scripts called by Expression Rules, the article on Groovy's Implicit Objects provides a good introduction to the variables which are supplied to your script by the platform.  Depending on what kind of Expression Rule called the script, these variables can include the current alarm or location.  The article includes pointers back to code snippets illustrating each case.

An important application of scripts is to call out to third-party web services.  This is illustrated by two snippets:  Send a Tweet, and Get Precipitation.

A good place to start with scripts called from web services via Scripto is the simplest code snippet, Retrieving a List of Models.  You could also look at the HTML, Ajax and Scripto tutorial.  If you have Flash Builder, the simple Hello Scripto sample application is a great introduction to calling Scripto scripts from Flash, as well as a simple way to test your Scripto scripts.

Rule to Associate Asset

Asset associations are a powerful concept, and they can be managed in a few ways. This example shows how an association can be made from a dataitem reported by a device. This would happen when a device can detect that it's near or connected to another device. So this consists of an Expression Rule...

Expression Rule Patterns for Alarms

It's common to want Expression Rules to detect and do something when an alarm has been unacknowledged for some time, like an hour. One way to accomplish this is to have a timer that looks for unacknowledged alarms for every asset. This would be a bad solution because it's a huge number of queries to...

Groovy Script : Getting Users from a User Group and filtering by username

This Groovy Script will retrieve a list of users in a specified Usergroup. There is also code that allows you to filter the search by Username.

Groovy Script: Add an asset to an asset group

This code snippet shows how to add an existing Device to an existing DeviceGroup using a custom Groovy script executed by the Scripto web service. To call the script create a URL of the following form: http://<HOST>/services/v1/rest/Scripto/execute/addDeviceToDeviceGroup?us... Text in angled...

Groovy Script: alarm Object

When an Expression Rule of type Alarm, AlarmExtendedDataChange, AlarmSeverityChange or AlarmStateChange calls a Groovy script, the script is provided with the implicit object 'alarm'. This example shows how the alarm object can be used.

Groovy Script: Checking alarm history

Here's an example Groovy script that you can call from an Expression Rule which retrieves the history of a particular alarm.

Groovy Script: Close Alarms on a condition

Sometimes you want to ack or close an alarm due to some condition like a dataitem value. The actions in an expression rule pertain to the event that triggered the expression rule, so calling SetAlarmState doesn't work because it acts on the alarm that caused the rule to run. A rule like this runs a...

Groovy Script: Copy Data Item value to Asset Property value

This Groovy script takes any dataitem values and writes them to properties of the same name - if they exist.

Groovy Script: Get Precipitation by a given latitude and Longitude

This Groovy script gets the weather forecast for a given lat/long by calling an external web service.

Groovy Script: Getting a list of device groups based on a deviceID

Here's an example Groovy script that returns a list of Device groups for a given Asset.