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 the Community Ranking System, a fun gamification element of the PTC Community. X

Event Listener for Thingworx Events

BenAtLmco
12-Amethyst

Event Listener for Thingworx Events

Hello!  I was wondering what the correct syntax is to listen for events that get fired in Thingworx from Vuforia Studio.  I know that I can add Events in the external data pane, but I'm unsure of how to listen for them.  I'm assuming it will be something similar to listening for the event that fires when a service completes:

$scope.$root.$on('servicename-complete', function(evt, arg){

// Do something 

});

 

For a Thing w/ name "thingname" and an event "eventname", what would be the correct syntax to create an event listener?  Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions

I was able to get this resolved with some help from a contact at PTC and wanted to post the solution in case anyone else was interested.  To start, you need to add the event to the external data pane.  The event's data is then stored on $scope at a location that can be determined from binding the event to a widget and looking at the bindings tab.  You then use the $watch function to execute a function when the value of the event data on scope changes.  Syntax for an event "eventName" and a Thing "thingName" is shown below:

 

$scope.$watch("app.mdl['thingName'].events['eventName']['eventData']", function(){

var eventData = $scope.app.mdl['thingName'].events['eventName']['eventData'];

// Do something with event data

});

View solution in original post

4 REPLIES 4
BenAtLmco
12-Amethyst
(To:Jimwang)

Hey Jim!

Thank you for the response!  Unfortunately, I think the reference material you provided doesn't cover exactly what I was after.  I'm not so much interested in triggering a service or listening for a service completion or failure.  I'll lay out my use case to give a little more context:

 

Basically, I have a laptop running a MatLab script that will be intermittently sending data to Thingworx.  When that data is sent, I would like to forward it along to all devices which are currently in an AR experience that I am designing to interact w/ that data.  

I am already familiar with the flow in the reference material you have provided where the AR experience runs a service and listens for a response.  The catch here is that I would like to push from Thingworx to the devices running the AR experience as the devices would have no idea when the MatLab script will be sending data to Thingworx.

 

My best guess at how I should implement this is to fire an event in Thingworx when data is received from the MatLab script.  I would then need to be listening in my Vuforia AR experience for that event to then manipulate the data as needed.  I'm under the impression that this is possible since I can add events to the external data pane, but am unsure on the syntax to listen for an event that is NOT tied to a specific service completing or failing.

 

Any additional documentation or guidance you could provide would be greatly appreciated!  Thanks!    

I was able to get this resolved with some help from a contact at PTC and wanted to post the solution in case anyone else was interested.  To start, you need to add the event to the external data pane.  The event's data is then stored on $scope at a location that can be determined from binding the event to a widget and looking at the bindings tab.  You then use the $watch function to execute a function when the value of the event data on scope changes.  Syntax for an event "eventName" and a Thing "thingName" is shown below:

 

$scope.$watch("app.mdl['thingName'].events['eventName']['eventData']", function(){

var eventData = $scope.app.mdl['thingName'].events['eventName']['eventData'];

// Do something with event data

});

you can use the following

 

$scope.app.registerOnNamedEvent = function(thingname, propname, eventname, callback) {
    var twxConnectorCtrl = $injector.get('ThingworxConnector');
    twxConnectorCtrl.subscribe(thingname, "Things", eventname, propname, callback);
}

 

and then do something like this

 

$scope.app.registerOnNamedEvent("myThing", "temperature", "DataChange", function(event) {

    console.log(event.source,  event.sourceProperty, event.eventData[0].newValue[0].value);

});

 

to listen to DataChange events on the temperature property of myThing.

Top Tags