in device/transport/mqtt/src/mqtt.ts [1080:1144]
function _parseMessage(topic: string, body: any): MethodMessage {
let url: URL.Url;
let path: string[];
let query: any;
try {
url = URL.parse(topic);
path = url.path.split('/');
query = querystring.parse(url.query as string);
} catch (err) {
debugErrors('Could not parse topic for received message: ' + topic);
debugErrors('Error is ' + err);
return undefined;
}
// if the topic has a querystring then 'path' will include it; so
// we strip it out
const lastPathComponent = path[path.length - 1];
if (lastPathComponent.indexOf('?') !== -1) {
path[path.length - 1] = lastPathComponent.substr(
0, lastPathComponent.indexOf('?')
);
}
if (path.length > 0 && path[0] === '$iothub') {
const message = new MethodMessageImpl();
if (path.length > 1 && path[1].length > 0) {
// create an object for the module; for example, $iothub/twin/...
// would result in there being a message.twin object
const mod = message[path[1]] = new MethodDescription();
// populates the request ID if there is one
if (query.$rid) {
message.requestId = query.$rid;
}
// parse the other properties properties (excluding $rid)
message.properties = query;
delete message.properties.$rid;
// save the body
message.body = body;
// parse the verb
if (path.length > 2 && path[2].length > 0) {
mod.verb = path[2];
// This is a topic that looks like this:
// $iothub/methods/POST/{method name}?$rid={request id}&{serialized properties}
// We parse the method name out.
if (path.length > 3 && path[3].length > 0) {
mod.methodName = path[3];
} else {
// The service published a message on a strange topic name. This is
// probably a service bug. At any rate we don't know what to do with
// this strange topic so we throw.
throw new Error('Device method call\'s MQTT topic name does not include the method name.');
}
}
}
return message;
}
return undefined;
}