cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Get Started with ThingWorx for IoT Guide Part 2

67% helpful (2/3)

Get Started with ThingWorx for IoT Guide Part 2

 

Step 4: Create Thing

 

Thing is used to digitally represent a specific component of your application in ThingWorx. In Java programming terms, a Thing is similar to an instance of a class. In this step, you will create a Thing that represents an individual house using the Thing Template we created in the previous step. Using a Thing Template allows you to increase development velocity by creating multiple Things without re-entering the same information each time.

  1. Start on the Browse, folder icon tab on the far left of ThingWorx Composer.

    Step 4 Step 1.png

  2. Under the Modeling tab, hover over Things then click the + button.
  3. Type MyHouse in the Name field.

NOTE: This name, with matching capitalization, is required for the data simulator which will be imported in a later step.

IoTProductMgmt_1-1659621957739.png

4. If Project is not already set, click the + in the Project text box and select the PTCDefaultProject.

5. In the Base Thing Template text box, click the + and select the recently created BuildingTemplate.
IoTProductMgmt_2-1659621957746.png

6. In the Implemented Shapes text box, click the + and select the recently created ThermostatShape.
IoTProductMgmt_3-1659621957747.png

7. Click Save.

 

 

Step 5: Store Data in Value Stream

 

Now that you have created the MyHouse Thing to model your application in ThingWorx, you need to create a storage entity to record changing property values. This guide shows ways to store data in ThingWorx Foundation. This exercise uses a Value Stream which is a quick and easy way to save time-series data.

 

Create Value Stream

 

  1. Start on the Browse, folder icon tab on the far left of ThingWorx Composer.

    Step 5 Create Value Stream Step 1.png

  2. Under the Data Storage section of the left-hand navigation panel, hover over Value Streams and click the + button.
  3. Select the ValueStream template option, then click OK.

    Step 5 Create Value Stream Step 3.png

  4. Enter Foundation_Quickstart_ValueStream in the Name field.
  5. If Project is not already set, click the + in the Project text box and select the PTCDefaultProject.IoTProductMgmt_6-1659621957757.png

     

  6. Click Save.

 

Update Thing Template

 

  1. Navigate to the BuildingTemplate Thing Template.

TIP: You can use the Search box at the top if the tab is closed.

      2. Confirm you are on the General Information tab.

      3. Click Edit button if it is visible, then, in the Value Stream text entry box, click the + and select Foundation_Quickstart_ValueStream 

      Step 5 Update Thing Template Step 3.png

      4. Click Save

 

 

Step 6: Create Custom Service

 

The ThingWorx Foundation server provides the ability to create and execute custom Services written in Javascript. Expedite your development with sample code snippets, code-completion, and linting in the Services editor for ThingsThing Templates, and Thing Shapes.

In this section, you will create a custom Service in the Electric Meter Thing Shape that will calculate the current hourly cost of electricity based on both the simulated live data, and the electricity rate saved in your model. You will create a JavaScript that multiplies the current meter reading by the cost per hour and stores it in a property that tracks the current cost.

  1. Click Thing Shapes under the Modeling tab on the left navigation pane; then click on MeterShape in the list.

    Step 6 Step 1.png

  2. Click Services tab, then click + Add and select Local (Javascript).

    Step 6 Step 2.png

  3. Type calculateCost into the Name field.

    Step 6 Step 3.png

  4. Click Me/Entities to open the tab.
  5. Click Properties.

    Step 6 Step 5.png

NOTE: There are a number of properties including costPerKWhcurrentCost and currentPower. These come from the Thing Shape you defined earlier in this tutorial.

6. Click the arrow next to the currentCost property. This will add the Javascript code to the script box for accessing the currentCost property.

7. Reproduce the code below by typing in the script box or clicking on the other required properties under the Me tab:

 

 

 

 

 

me.currentCost = me.costPerKWh * me.currentPower;

 

 

 

 

 

Step 6 Step 7.png

8. Click Done.

9. Click Save.

NOTE: There is a new ThingWorx 9.3 feature that allows users to easily Execute tests for ‘Services’ right from where they are defined so users can quickly test solution code. 

FINAL FINAL.png

 

Click here to view Part 3 of this guide. 

Version history
Last update:
‎Nov 15, 2022 01:37 PM
Updated by:
Contributors