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

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Implement Services, Events, and Subscriptions Guide Part 1

No ratings

 

 

Automate business processes with Services, Events and Subscriptions.

 

Guide Concept

 

This project will introduce Services, Events, and Subscriptions inside of the ThingWorx platform.

Following the steps in this guide, you will be able to expand your data model using Services, Events, and Subscriptions.

 

We will teach you how to make a more robust and enjoyable experience for users simply by using the resources inside of the ThingWorx Composer.

 

You'll learn how to

 

  • Create Events, Services and Subscriptions in Composer
  • Utilize the ThingWorx Edge SDK platforms with Services, Subscriptions, and Events

 

NOTE:  The estimated time to complete this guide is 60 minutes

 

 

Step 1: Completed Example

 

This guide references the attached EventsServicesSubscription.zip. The sample application is based on a company needing to make deliveries. The rules engine will handle much of the work outside of the transportation workflow.

 

Unzip and 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.

 

What's Inside

 

 Name                                            Type
PTCDeliversThing
PTCDeliversBusinessLogicThing
PackageStreamStream
OrdersDatabaseDatabase
DeliveryDataTableDatabase
PackageDataTableDatabase
MerchantDatabaseDatabase
PlaneThingTemplateThingTemplate
MerchantThingTemplateThingTemplate
TruckThingTemplateThiingTemplate
ClientThingShapeThingShape
VehicleDeliveryShapeThingShape
PackageDataShapeDataShape
CustomerDataShapeDataShape
OrderDataShapeDataShape
PackageDeliveryDataShapeDataShape
MerchantOrderDataShapeDataShape
MerchantDataShapeDataShape
default_userUser
TestDashboardMashup1Mashup
TestGadgetMashup

 

 

Step 2: Functionality

 

You can combine Services, Subscriptions, and Events to automate business processes or trigger notifications. See the definitions and examples before to gain a better understanding as to the role each plays.

 

 Name                 Function
ServicesMethods and functions to be used within a ThingWorx application. There are Services provided by the system, but custom services need to be made to create the application's functional requirements. All services in the ThingWorx Composer can be accessed by a user with the appropriate permissions. Remote services can be created with the use of the ThingWorx SDKs.
EventsAn Event represent a change in state of a property or changes in a running application. Changes can be handled in different ways based on the method used to fire or queue them. They are a great resource for property value changes and alerts.
SubscriptionsThe implementation for a subscription is the same as that of a service. Subscriptions are activated when the Event they are listened for is triggered.

 

 

 

Step 3: Create Events

 

Events are used to mark situations that can occur within an application. They can be used for scenarios ranging from a dangerous situation that is imminent and needs to be checked by personnel to just a system notification. Each Event is based on a DataShape that will hold the necessary information about the Event.

 

For example, it wouldn’t be enough to just know that a rain Event has occurred. It might be helpful to also know the location, the time the rain started and expectations of the amount of rain to fall.

 

Follow the steps below to create an Event for the TruckThingTemplate Entity.

 

  1. Open the TruckThingTemplate Entity and click the Events tab.
  2. Click Add.

    open_truck_template.png

     

  3. Enter an Event name, such as PackageDelivered.
  4. For the DataShape, in the Search Data Shape field, filter and select PackageDataShape.

    set_truck_event.png

     

Events can be queued or fired based on updates done in Composer, property changes of a running application, or programmatically. Triggering or firing an Event can be done in Services utilizing JavaScript.

 

 

 

Step 4: Services Interface

 

To create Services within ThingWorx, go to the Services tab of the Entity that will house the Service. Our PTCDeliversBusinessLogic Thing will contain several Services for us.

 

Within the Services tab under the Entity Information section of a Thing, you will see built in functionality for all the ThingTemplates and ThingShapes the Thing inherits from. You will also see the section that allows you to create new Services.

 

Service Info Tab

 

This tab is for the general information of the Service. This is where you will add the Service name, description, category, whether the service is asynchronous, and whether the Service can be overridden.

 

tab_service_info.png

 

Inputs Tab

 

This tab is for the parameters for the Service. For input parameters, a parameter can be required and default values are allowed.

 

tab_service_input.png

 

Outputs Tab

 

This tab is for the return type of the Service. The resulting output of the Service can stretch from being nothing to user defined Entities.

 

tab_service_output.png

 

Snippets Tab

 

This tab allows users to pick from reusable JavaScript code that is specific to ThingWorx. These snippets are grouped by resources used, functionality, and type. You are also provided a search bar to look for keywords or titles.

 

When a snippet is found, click the arrow. This will insert the JavaScript code into the Script section wherever the cursor is located. From here, edit the inserted snippet to work with the rest of your code. This section is often helpful in getting to know how to perform service calls and code for the ThingWorx environment.

 

tab_service_snippet.png

 

Me/Entities Tab

 

A listing of all Properties, Events, and Services belonging to custom and provided Entities in ThingWorx. As with the Snippets section, clicking the blue and white arrow will insert the reusable code to wherever the cursor is located. After the code is inserted, update the code to your liking. This is often a great resource for buildiung up payload for Service calls.

 

tab_service_entities.png

 
 
Click here to view Part 2 of this guide.
Version history
Last update:
‎Nov 14, 2022 09:36 AM
Updated by:
Labels (1)
Attachments
Contributors