Skip to end of metadata
Go to start of metadata

By setting the method type as "script", you can define a logic function to perform operations as the method's arguments are accessed. Typical operations include data validation, sending an email if a value is over a predefined level, etc.

Method scripts are created by following these simple steps:

1. Set method type as "script"


In method create/edit dialog:

  1. set type to "script"
  2. If there is an incoming argument value (or values) to the method function, select the data type for "Request Data Type"
  3. If there is a return argument from the method function, select the data type for "Response Data Type"
  4. Check "Private" if you don't want your script method to be accessible by script functions of properties/methods/triggers of other "things")

2. Define method function


All methods must be provided with a script that has a "<name>" function defined, where <name> is the same method name predefined in the Create/Edit Method dialog box:

So for example, you need to provide a function for the "echo" method as follows:

If method has no "Request Data Type" set (incoming argument to the method function), then the method can be declared without arguments:

To enter the script, the "Edit Script" dialog box must be opened:

  1. Select the method in the "Methods" section of the thing
  2. Click "Edit Script"

The formatting for an example function is shown on the right:

Note that the Request/Response Data Types can be left blank when defining the script method because there are no incoming/return arguments in the function

 

3. Comply to declared argument and result type


The method function must accept/return values with the Request/Response data types that were predefined in the Create/Edit Method dialog box:

  • Whenever primitive types are specified (boolean, string, int, long, float, double, string), standard Java Script types are used:

    the following code shows a primitive type as a function input (Request) argument and as a return (Response) argument:

  • When arrays are specified as a data type, Java Script arrays are used.

    For example, the following code shows how an array can be either an input argument or a return (output) argument when "array" is specified as the Request/Response Data Type:

  • When structures are specified as a data type, JavaScript objects are used.

    For example, the following code shows how a Response Data Type of "UserDetails" with fields "id" (long) and "name" (string) is implemented as a return argument. Note that the input argument (Request Data Type) is "long":

    And a Request Data Type of "Weather" with fields "temperature" (float) and "condition" (string) becomes a function input argument in the following code. Note that the return argument (Response Data Type) is "string":

4. Code method logic


You have a lot of options. See our Scripting Recipes for more details.

Just some ideas:

  • validate incoming values and save the value as an internal persistent property
  • validate incoming value and pass it to the device via remote property
  • send an email
  • send the value using a REST service
  • retrieve value using a REST service
  • retrieve value from device via remote property 
  • fire an event

 

 

  • No labels