private _onResponseMessage()

in device/transport/mqtt/src/mqtt_twin_client.ts [233:273]


  private _onResponseMessage(topic: string, message: any): void {
    let urlObject: url.Url;
    let path: string[];
    let query: any;

    try {
      urlObject = url.parse(topic);
      path = urlObject.path.split('/');
      query = querystring.parse(urlObject.query as string);
    } catch (err) {
      return;
    }

    if ((path[0] === '$iothub') &&
        (path[1] === 'twin') &&
        (path[2] === 'res') &&
        (path[3]) &&
        (path[3].toString().length > 0) &&
        (query.$rid) &&
        (query.$rid.toString().length > 0)) {
      if (this._pendingTwinRequests[query.$rid]) {
        const requestCallback = this._pendingTwinRequests[query.$rid];
        delete this._pendingTwinRequests[query.$rid];
        // should we really ignore the status code?
        const responseBody = message.toString();
        const parsedMessage = responseBody ? JSON.parse(responseBody) : undefined;
        /*Codes_SRS_NODE_DEVICE_MQTT_TWIN_CLIENT_16_007: [When a message is received on the response topic with an `$rid` property in the query string of the topic matching the one that was sent on the request topic, the `callback` shall be called with a `null` error object and the parsed content of the response message.]*/
        /*Codes_SRS_NODE_DEVICE_MQTT_TWIN_CLIENT_16_017: [When a message is received on the response topic with an `$rid` property in the query string of the topic matching the one that was sent on the request topic, the `callback` shall be called with a `null` error object.]*/
        /*Codes_SRS_NODE_DEVICE_MQTT_TWIN_CLIENT_41_001: [The `callback` shall be called with a Error object containing the error message if the `parsedMessage` contains an errorCode.]*/
        if (parsedMessage && parsedMessage.errorCode) {
          requestCallback(new Error(parsedMessage.message));
        } else {
          requestCallback(null, parsedMessage);
        }
      } else {
        debug('received a response for a request we do not know about: ' + query.$rid);
      }
    } else {
      debugErrors('received a response with a malformed topic property: ' + topic);
    }
  }