Scenario:
- Structured data types can be selected as a data type for a property in the same way as for the primitive data types, such as int, float, string, etc.
- Structured data types can contain any number of data elements, each of which can be a primitive type, an array, or even another structure
- The data elements are organized as name/value pairs, according to the JSON format
- Arrays, data structures, and other types of data elements can be nested within a JSON data structure
- We'll be creating a persistent “_settings” property for our “Thermometer” thing, which contains named values based on a new structured data type
- The data structure will be defined with the following elements of data:
- unitOfMeasure - string, accepts only "celsius" or "fahrenheit”
- ledColor - string, accepts only "red", "green", or "blue"
- A script property called “settings” will then be created to provide the logic for getting the current "_settings" property values or setting new ones if they are validated
Prerequisites:
- A "Thermometer" thing was created in the Create, Read And Write Property scenario
- All new "_settings" property values must be validated before storing as a persistent property
- If any of the "_settings" property values don't match one of the acceptable strings, an error will be indicated
- The UI or API can be used with the "settings" property to execute the code for getting/setting property values for “_settings” (with validation)
Steps:
1. Creating a "ThermometerSettings" structured data type
- Click "Data Types" in the main menu to open a tab listing the data types
- Click "+" to open "Create Structure Type" dialog
- Enter "ThermometerSettings" as the name for the new structured data type
- Click "+" button to add the data elements to the data structure
- Set the name of the first data element to "unitOfMeasure"
- Check "Required"
- Select "string" in the drop-down menu as the data type for this element (clicking "..." opens the menu)
- Click "+" button again
- Set the name of the second data element to "ledColor"
- Check "Required"
- Select "string" again in the drop-down menu as the data type for this element
- Click "Save" to save the new structured data type
"ThermometerSettings" has now been added to the list in the "Data Types" tab, and can be selected as a data type for a property
2. Creating a persistent "_settings" property
- Open "Properties" in the "Thing: Thermometer" tab
- Click "New" to open "Create Property" dialog
- Enter "_settings" as the name for this property
- Select "persistent" as the property type
- To select "DataType", click the "..." button to open a drop-down menu
- Click "more types" in this menu to open the "Select Type" dialog
- Type "Therm" in the search field to locate the new structured data type
- Select "ThermometerSettings" in the search results
- Click "Select" to enter the new data type in the "Data Type" field
- Check "Private" (property value in GK can only be set internally by methods or script property logic)
- Save the new property
The persistent "_settings" property now contains values based on the "ThermometerSettings" structured data type, and has been set to "Private"
3. Creating a script "settings" property
- Open "Properties"
- Click to open "Create Property" dialog
- Enter "settings" as the name for this property
- Select "script" as the property type
- Follow steps 5 - 9 of the previous section to select "ThermometerSettings" as "Data Type" for this property
- Save the new property
A script for the new "settings" property will be coded in the next step based on the "ThermometerSettings" structured data type:
4. Coding the get/setSettings script logic for "settings"
- Open "Properties"
- Select the new "settings" script property
- Click to open "Edit Script" dialog
-
Insert the following JavaScript code for both getSettings( ) and setSettings( ) functions:
- Save script by clicking "Save"
The script "settings" property contains code to get/set "_settings" property values with the "ThermometerSettings" structured data type
The code for setting values includes a validation check for allowable values
Current "_settings" property values can be retrieved or new values can be set (if validated) by using UI or API with the "settings" property as follows:
5. Get/set property values using UI
- Select "Properties"
- Select the "settings" script property
- Click "Edit Value" to open a dialog
-
Insert the following code in the "Value" text area (values correspond with the new "ThermometerSettings" structured data type):
- Click "Set" to retain the structured data values in the persistent memory locations for "_settings"
- Click "Get" to retrieve the current "_settings" property values
Clicking "Get" or "Set" executes the corresponding code for the script "settings" property, which will either get or set data values for "_settings" (with validation)
6. Get/set property values using REST API
- Check out the GadgetKeeper API - Thing Properties page for details on how to perform basic method operations on GadgetKeeper API using cURL or a REST API tool
- Obtain required parameters:
- Use API Explorer to perform REST calls using UI
- Use cURL to perform REST calls using command lines:
To get property value:
To set property value: