By setting the property type as "script", you can define set/get functions to perform operations as another property value (persistent or remote) is accessed. Typical operations include validation of incoming values, sending an email if a value is over a predefined level, etc.
Script properties Property scripts are created by following these simple steps:
Table of Contents |
---|
1. Set property type as "script"
...
Open the property create/edit dialog by clicking "New" or "Edit" in the "Properties" section of the "thing"
- set type to "script"
- Select the Data Type which is to be used for the argument (function input) and/or return value (output)
- check "Readonly" if you want your property to be readable but not settable (only the get<PropertyName> function is required in the script)
- Check "Private" if you want your property to be unreadable (except for script functions of properties/methods/triggers within the thing)
- Check "Nullable" if the "null" is acceptable as property value
2. Define get/set property access functions
...
All script properties must be provided with a script with the get/set access functions defined. These two functions have the form: "get<Name>" and "set<Name>", where:
To enter the script, the "Edit Script" dialog box must be opened:
The example shows the formatting for the get/set functions of a "Temperature" script property:
|
3. Comply to declared data type
...
The set/get functions must accept/return the data type that was predefined in the other property being accessed. This was declared in its Create/Edit Property dialog box. This same data type must be selected when defining the data type for the script property in step 1.2. If another data type is selected, an error message will be shown.
-
Whenever primitive types are specified (boolean, string, int, long, float, double, string), standard Java Script types are used
For example, the following code shows a primitive type as a return argument, and then as a function input argument:
Code Block language javascript //returns float function getTemperature(){ return 2.8; } //expects float function setTemperature(value){ var example=value*value; }
-
When arrays are specified as a data type, Java Script arrays are used
For example, the following code shows how an array is implemented as a return argument, and then as a function input argument:
Code Block language javascript //returns float[] function getTemperatures(){ return new Array(2.8, 3.4, 5); } //expects float[] function setTemperatures(value){ var len=value; var example=value[0]; }
-
When structures are specified as a data type, JavaScript objects are used.
For example, the following code shows how a structured data type of "Weather" with fields "temperature" (float) and "condition" (string) is implemented as a return argument, and then as a function input argument:
Code Block language javascript //returns Weather function getWeather(){ var w=new Object(); w.condition="Scattered Clouds"; w.temperature=30; return w; } //expects Whether function setWeather(value){ if(value.condition=="Sunny" && value.temperature==20){ log.debug("Oh Happy Day!"); } }
4. Code property logic
...
You have a lot of options. See our Scripting Recipes for more details.
...