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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Methods for Data Storage Part 2

No ratings

 

 

Step 3: Streams

Mass data is typically stored in a structure functionally similar to a spreadsheet.

Each separate column of the spreadsheet is exclusively one type of data (with each field directly beneath it holding different entries of that same type), while the rows determine the specific entry.

Combine a column’s “type” with the row’s “index”, and you have the particular piece of data about which you care.

ThingWorx mass data storage functions in roughly the same way. However, ThingWorx does have one differentiation from many spreadsheet implementations.

In ThingWorx, the type that defines each column is not held internally to the data structure itself. Instead, it is abstracted out as a Data Shape.

A Data Shape is the definition of what each column contains.

A Stream requires a Data Shape in order to define its structure.

Create Data Shape

  1. On the ThingWorx Composer Browse tab, click Modeling -> Data Shapes, + New.

    Data17.png
     
  2. In the Name field, enter Test_Data_Shape.

    Data18.png
     
  3. If Project is not already set, search for and select PTCDefaultProject

  4. At the top, click Field Definitions.

    data19.png
     
  5. Click + Add.

  6. On the right in the Name field, enter Index_Field
    .
  7. Change the Base Type to INTEGER.

  8. Check the Is Primary Key checkbox.

    • All Data Shapes must have a Primary Key, and Index_Field is an appropriate choice, as it will keep track of the number of entries.

      Data20.png
     
  9. At the top-right, click the "Check with a +" button for Done and Add.

  10. In the Name field, enter Value_Field.

  11. Change the Base Type to NUMBER.

    Data21.png
     
  12. At the top-right, click the "Check" button for Done.

  13. At the top, click Save.

    Data22.png

Once again, the Data Shape does nothing by itself.

It simply defines that some other mass data structure (such as a Stream) follows the particular format of the Data Shape.

So a Stream assigned the Test_Data_Shape above would have two fields per entry, i.e. an Index and a Value.

Create Stream

Streams are a mass data storage option optimized for Time-Series data.

They are utilized in much the same way as Value Streams, but possess additional functionality.

 

For one, Streams do not have to be tied to Things in the way that Value Streams are. They can be entirely independent, or even accept inputs from multiple different Things.

This can be useful when you want to have a master list of what every similar Thing was doing at particular points throughout some timeframe.

  1. On the ThingWorx Composer Browse tab, click DATA STORAGE -> Streams, + New.

    Stream1.png
     
  2. On the Choose Template pop-up, select Stream and click OK.

    Stream2.png
     
  3. In the Name field, enter Test_Stream.

    Stream3.png
     
  4. If Project is not already set, search for and select PTCDefaultProject.
  5. In the Data Shape field, search for and select Test_Data_Shape.
  6. At the top, click Save.

    Stream4.png
     

Create Thing

Now that the Stream is created, you'll create a Thing from which you can log some Properties.

  1. On the ThingWorx Composer Browse tab, click MODELING -> Things, + New.

    Stream5.png
     
  2. In the Name field, enter Stream_Test_Thing.

  3. If Project is not already set, search for and select PTCDefaultProject.

  4. In the Thing Template field, search for and select GenericThing.

    Stream6.png
     
  5. At the top, click Properties and Alerts.

    Stream7.png
     
  6. Click + Add.

  7. In the Name field, enter Index_Property.

  8. Change the Base Type to Integer.

  9. Check the Persistent checkbox.

    Stream8.png
     
  10. At the top-right, click the "Check with a +" button for Done and Add.

  11. In the Name field, enter Value_Property.

  12. Change the Base Type to Number.

  13. Check the Persistent checkbox.

    Stream9.png
     
  14. At the top-right, click the "Check" button for Done.

Add Service

Since you now have both a Thing and a Stream, we can write a Service which logs the Properties' values when the Service is executed.

  1. At the top, click Services.

    Stream10.png
     
  2. At the top, click + Add.

  3. In the Name field, enter Add_Stream_Entry_Service.

    Stream11.png
     
  4. On the left under New Service, click the Snippets tab.

    Stream12.png
     
  5. In the Filter field, type add str.

    Stream13.png
     
  6. Expand the Stream, Blog, Data Table section to reveal the Add Stream Entry Snippet.

    Stream14.png
     
  7. Click the right arrow beside Add Stream Entry.

    • An Add Stream Entry pop-up will open.

      Stream15.png
     
  8. In the Search Streams field, enter test.

    Stream16.png
     
  9. Select Test_Stream.

    Stream17.png
     
  10. Click the Insert Code Snippet button.

    • Note that a section of Javascript code has now been added to the Script window.

      Stream18.png
     

     

Modify Snippet

The inserted Javascript Snippet has already done most of the work of creating a custom Service for us.

All which you need to do now is modify the code Snippet to store the particular Property values from your Thing.

  1. On the 10th line of code, double-click undefined to select it.

    Stream19.png
     
  2. On the left, click the Me/Entities tab.

    Stream20.png
     
  3. Under the Me/Entities tab, expand Properties.

    • Note that this is not the Properties and Alerts at the top of Composer.

      Stream21.png
     
  4. Click the right arrow beside Value_Property.

    • Note that undefined has been replaced by me.Value_Property.

      Stream22.png
     
  5. On the 11th line of code, double-click the remaining undefined to select it.

    Stream24.png
     
  6. Click the right arrow beside Index_Property.

    • Note that the second “undefined” has been replaced by “me.Index_Property”.

      Stream25.png
     
  7. At the top, click Done to close the Add_Stream_Entry_Service editing window.

    Stream26.png
     
  8. At the top, click Save.

 

 

Click here to view Part 3 of this guide.

Version history
Last update:
‎Mar 07, 2023 01:48 PM
Updated by:
Labels (1)
Contributors