Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
This project will introduce you to a variety of features designed to expedite the IoT application development workflow. In particular, there are several features that make the creation of custom Services quicker and easier when writing and testing your code.
Following the steps in this guide, you will learn how to access features that will help you throughout your software development lifecycle: development, execution, and testing.
We will teach you how to became a faster and more efficient IoT application developer.
NOTE: This guide's content aligns with ThingWorx 9.3. The estimated time to complete ALL parts of this guide is 30 minutes.
Download and extract the completed files for this tutorial attached to this article: ToolsTipsTricks.zip file.
The file provided to you contains a completed example of the Entities covered in this guide. Utilize this file to see a finished example and return to it as a reference if you become stuck during this guide and need some extra help or clarification.
Keep in mind, this download uses the exact names for Entities used in this tutorial. If you would like to import this example and also create Entities on your own, change the names of the Entities you create.
Expedite your application development process by utilizing code Snippets provided in ThingWorx. The JavaScript code snippets will give you insight into best practices for performing common development tasks.
Follow the steps below to create a Service that will:
Utilizing a Thing Template expedites your development process because the Things inherit properties from the Template. In the steps below, it can relate to a real-world scenario where Things might represent parts that belong to an assembly line.
In the top left of the Home screen of Composer, click the + button.
In the dropdown list, click Thing Template.
Select GenericThing as the Thing Template and select a Project (ie, PTCDefaultProject).
Create a string Property named PartName.
Now that you've saved the Template, you can create Things that inherit the properties PartTemperature and PartName. Create a couple new Things with LinePartTemplate as the Thing Template. For testing purposes, set a different value for the PartTemperature Property of each Thing.
NOTE: When you run the Service later, you'll be able to see the different Things with values - some within a temperature threshold based on the conditions we set.
Code Snippets help shorten time to develop Services, but you are also provided with a way to call Services from an entity plus the default input parameters needed to make the call.
In the top left of the Home screen of Composer, click the + button.
In the dropdown list, click Thing.
Select GenericThing as the Thing Template and select a Project (ie, PTCDefaultProject).
You should now see a Snippet of JavaScript inserted into the Service code window. The GetImplementingThingsWithData Service is used to get all the Things with a specific Thing Template as it's base template. The return of this call will provide you with an InfoTable of all the Things you created on your own in the last section. Refer to LineTemplateThing1 in the completed download for an example.
Lets's update the Snippet inserted in the last section.
Update the variable name from result to partsList.
Expand the Info Table section and click the arrow next to Infotable for loop.
All places where yourInfotableHere is located, replace it with the name of your list, partsList.
At this point, you have created a loop that will iterate through the set of Things created with LinePartTemplate set as the Thing Template.
Since the JavaScript snippets were provided and all you had to do was update the variable names, now you must retrieve the input from the user on what the temperature threshold will be. This will be handled in the next section.
You can set the threshold to what is considered a "Hot" part in two different ways. Use a static value in order to keep things standardized OR create an input variable to make things more flexible. In the steps below, we are adding an input variable to finish off our Service.
Add an input parameter called TemperatureThreshold that is a Number. Keep the other settings clear.
Go to the Outputs tab, select INFOTABLE in the dropdown.
In the Data Shape search field, use the + New Data Shape sign to create a new DataShape. An example is shown below. I’ve named this Data Shape LinePartDataShape.
In the JavaScript code, put your cursor on a new line before the for loop (ie, line 1).
if(row.PartTemperature > TemperatureThreshold){ }
In the search bar, filter and select Create infotable entry from datashape in the Info Table section by clicking the arrow next to it.
Select the newly created LinePartDataShape Data Shape when a popup appears and click Insert Code Snippet. You will see the newly inserted snippet that creates an entry for the Info Table.
var newEntry = new Object(); newEntry.PartName = row.PartName; // STRING newEntry.PartTemperature = row.PartTemperature; // NUMBER
Your entire code thus far should match the following:
var params = { infoTableName : "InfoTable", dataShapeName : "LinePartDataShape" }; // CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(LinePartDataShape) var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params); // result: INFOTABLE dataShape: "RootEntityList" var partsList = ThingTemplates["LinePartTemplate"].GetImplementingThingsWithData(); var tableLength = partsList.rows.length; for (var x=0; x < tableLength; x++) { var row = partsList.rows[x]; if(row.PartTemperature > TemperatureThreshold){ var newEntry = new Object(); newEntry.PartName = row.PartName; // STRING [Primary Key] newEntry.PartTemperature = row.PartTemperature; // NUMBER } }
result.AddRow(newEntry);
Click here to view Part 2 of this guide.