Skip to end of metadata
Go to start of metadata

Scenario:

  1.  Creating an "airconditioner" thing with methods "start" and "stop"
  2. A persistent property "_started" is used to record the current state of "airconditioner" in GadgetKeeper
  3. Two script methods called "start" and "stop" will be created to perform the start/stop functions
  4. The script logic will start or stop the "Airconditioner" thing if it is not already started/stopped
  5. A notification email is to be sent whenever the air-conditioner is started/stopped
  6. This example shows how the UI or API can be used to execute the script logic for the "start" or "stop" method

Steps

For a complete description of all the method settings used for the dialog boxes in this tutorial, see Thing Method Settings and Method Script.

1. Create "airconditioner" thing


  1. In the "Things" tab, open "Create Thing" dialog
  2. Set name of the thing
  3. Save thing by clicking "Save"

A new "Airconditioner" thing is added to the list in the "Things" tab. Clicking it will open the "Thing: Airconditioner" tab

2. Create "_started" property


  1. In the "Thing: Airconditioner" tab, open "Properties"
  2. Click to open "Create Property" dialog
  3. Set name of the property
  4. Select "persistent" as "Type" (property value would be persisted by the GK)
  5. Select "boolean" as "Data Type" (property value can be one of two states - started or stopped)
  6. Check "private" (property value in GK would be visible only to the thing)
  7. Save property

3. Create "start" method


  1. In the "Thing: Airconditioner" tab, click "Methods"
  2. Click to open "Create Method" dialog
  3. Set name of the method
  4. Select "script" as the type of method
  5. Click to save new method

3. Create "stop" method


  Repeat the same steps (1 through 5) for creating "start" method, but specify "stop" as the method name in step 3

Two new methods, "start" and "stop", are added to the list, and clicking on either one will show its details in the pane below it

A script for new "start" and "stop methods will be coded in next step, which will start or stop the "Airconditioner" thing based on the state of the persistent "_started" boolean variable

4. Code methods logic


  1. In the "Methods" section of "Thing: Airconditioner", select "start" method
  2. Click to open "Edit Script" dialog
  3. Insert the following JavaScript code for both "start" and "stop" methods:

  4. Save script by clicking "Save"

      5. Repeat the same steps (1 through 4) to code the "stop" method logic, but select the "stop" method in step 1

The script logic will start or stop the "Airconditioner" thing as needed and send an email whenever it changes state

Read more about how the context object provides information on the script parameters for thing

5. Call method using UI


  1.  Select "start" method 
  2. Click to open "Call method" dialog
  3. Click "Call" to call the chosen method (no arguments needed)

This will start the "airconditioner" thing by setting the "_started" property as "true" if it is not already set as "true"

 

     4. To stop the "airconditioner" thing, repeat steps 1-3, but select the "stop" method in step 1

This will stop the "airconditioner" thing by setting the "_started" property as "false" if it is not already set as "false"

The name of the method determines the function that will be called in the script

A notification email is sent whenever the "_started" property changes state

6. Call method or get/set method details using REST API


  1. Check out the GadgetkeeperAPI - Methods page for details on how to perform basic method operations on GadgetKeeper API using cURL or a REST API tool
  2. Obtain required parameters:
    • To obtain the Thing Id, see instructions here.
    • To obtain "start" and "stop" method Ids, see instructions here.
    • To create an API Key, see instructions here.
  3. Use cURL to perform REST calls using command lines:

          To execute method:

    

 

 

  • No labels