in jobs/index.js [152:204]
this._handleMessages = function(topic, payload) {
var topicTokens = topic.split('/');
// If not a job topic emit to application and return
if (!isJobTopic(topicTokens)) {
that.emit('message', topic, payload);
return;
}
var thingName = topicTokens[2];
var thing = jobSubscriptions.find(function(elem) {
return elem.thingName === thingName;
});
// Do nothing if thing not found in job subscriptions
if (isUndefined(thing)) {
return;
}
var jobExecutionData = {};
try {
jobExecutionData = JSON.parse(payload.toString());
} catch (err) {
if (options.debug === true) {
console.error('failed parsing JSON \'' + payload.toString() + '\', ' + err);
}
return;
}
if (isUndefined(jobExecutionData.execution) ||
isUndefined(jobExecutionData.execution.jobId) ||
isUndefined(jobExecutionData.execution.jobDocument)) {
return;
}
var operationName = jobExecutionData.execution.jobDocument.operation;
var operation = thing.operations.find(function(elem) {
return (isUndefined(operationName) ? isUndefined(elem.operationName) : operationName === elem.operationName);
});
// If operation subscription not found by operation name then look for default operation subscription
if (isUndefined(operation)) {
operation = thing.operations.find(function(elem) { return (isUndefined(elem.operationName)); });
if (isUndefined(operation)) {
return;
}
}
operation.callback(null, that._buildJobObject(thingName, jobExecutionData.execution));
}