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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Implement Services, Events, and Subscriptions Guide Part 2

No ratings

 

Step 5: Create Services

 

Below shows how we can create the GetCustomerPackages Service for the PTCDeliversBusinessLogic Thing.

 

Create Service Definition

 

  1. On the home page, filter and select the PTCDeliversBusinessLogic Thing.

    Step 5 Create Service Definition Step 1.png

  2. Switch to the Services tab.
  3. Click + Add.

    Step 5 Create Service Definition Step 3.png

     

  4. Enter the name of the Service, GetCustomerPackages.
  5. Switch to the Output tab of the Service.
  6. Select InfoTable from the list as the Base Type. When prompted for the DataShape, select CustomerDataShape.

    Step 5 Create Service Definition Step 6.png

  7. Switch to the Inputs tab of the Service.
  8. Click the + Add button.

    Step 5 Create Service Definition Step 8.png

     

  9. Enter the name CustomerId.
  10. Check the Required checkbox.

    Step 5 Create Service Definition Step 10.png

  11. Click Done.

 

Add Service Functionality

 

  1. Switch to the Me/Entities tab.
  2. Switch the radio option to Other Entity.

    Step 5 Add Service Functionality Step 2.png

  3. Filter and select PackageDataTable as the Entity. A list of all accessible Services for the PackageDataTable will appear.
  4. In the search bar for Services, enter QueryDataTableEntries in the Services section and click the arrow next to it.

    Step 5 Add Service Functionality Step 5.png

  5. Update the inserted code to use the input parameter, CustomerId, in the query. An example is below and more information can be found on queries on our Query Help Page.
  6. Click Done and save your work.

 

 

 

 

 

 

/ Provide your filter using the format as described in the help topic "Query Parameter for Query Services" var query = { "filters": { "type": "EQ", "fieldName": "CustomerId", "value": CustomerId } }; // result: INFOTABLE dataShape: "" var result = Things["PackageDataTable"].QueryDataTableEntries({ maxItems: undefined /* NUMBER */, values: undefined /* INFOTAB

 

 

 

 

 

 

After saving changes and closing the Service edit view, you can test the method by selecting the Execute play button.

 

The Service we created will query PackageDataTable for any packages with the CustomerId you entered. If no data has been added to the DataTable as yet, open PackageDataTable's Services tab and execute the AddDataTableEntries Service with test data.

 

 

 

Step 6: Create Subscriptions

 

Subscriptions are based on tracking the firing of an Event. When the Event is triggered or fired, all entities with a Subscription to the Event will perform the script as defined. The JavaScript interface and script tabs are the same as those utilized for the Services interface.

 

Subscriptions are a great resource for making updates in other Entities, Databases, or even just logging this information to your liking.

  1. On the home page, filter and select the PTCDeliversBusinessLogic Thing.

    Step 6 Step 1.png

  2. Switch to the Subscriptions tab.
  3. Click + Add.

    Step 6 Step 3.png

     

  4. For Source, select *Other Entity.
  5. Filter and select PackageStream.
  6. Check the Enabled checkbox.

    Step 6 Step 6.png

    Using a Stream to track events in the application allows for one source to watch for activity. The source for a Subscription can be other Entities if a single Entity is wanted. In order to capture all source data from the PackageStream, you will need to set it as the Stream for the Entity you would like to track.
  7. Switch to the Inputs tab.
  8. Select the Event drop-down and pick the PackageDelivered Event. This PackageDelivered Event only exists in the completed download. If you are not using that download, create your own Event based on the PackageDataShape.

    Step 6 Step 8.png

  9. Update the script area of the Subscription using the below code. This JavaScript code will take the information from the triggered Event and add it to the DeliveryDataTable.

 

 

 

 

 

/ tags:TAGS

var tags = new Array();



// values:INFOTABLE(Datashape: PackageDataShape)

var values = Things["DeliveryDataTable"].CreateValues();

values.Customer = eventData.Customer; // THINGNAME

values.Content = eventData.Content; // STRING

values.ID = eventData.ID; // INTEGER [Primary Key]

values.Weight = eventData.Weight; // NUMBER



// location:LOCATION

var location = new Object();

location.latitude = 0;

location.longitude = 0;

location.elevation = 0;

location.units ="WGS84";



var params = {

    tags : tags,

    source : me.name,

    values : values,

    location : location

};



// AddOrUpdateDataTableEntry(tags:TAGS, source:STRING("me.name"), values:INFOTABLE(DeliveryDataTable), location:LOCATION):STRING

var id = Things["DeliveryDataTable"].AddOrUpdateDataTableEntry(params);

 

 

 

 

 

 

 

 

Step 7: Next Steps

 

Congratulations! You've successfully completed the Implement Services, Events and Subscriptions guide.

 

In this guide you learned how to to create Events, Services and Subscriptions you can utilize to monitor and optimize your IoT applications.

 

The next guide in the Design and Implement Data Models to Enable Predictive Analytics learning path is Build a Predictive Analytics Model

 

The next guide in the Monitor Factory Supplies and Consumables learning path is Build a Predictive Analytics Model.

 

Learn More

 

We recommend the following resources to continue your learning experience:

 

Capability Guide

BuildCreate Custom Business Logic Guide
SecureConfigure Permissions
ConnectSDK Reference

 

Additional Resources

 

If you have questions, issues, or need additional information, refer to:

 

Resource Link

CommunityDeveloper Community Forum
SupportHelp Center

 

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