Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_onEnter: (disconnectedCallback, err, result) => {
if (disconnectedCallback) {
if (err) {
/*Codes_SRS_NODE_DEVICE_MQTT_16_019: [The `connect` method shall calls its callback with an `Error` that has been translated from the `MqttBase` error using the `translateError` method if it fails to establish a connection.]*/
disconnectedCallback(translateError(err));
} else {
disconnectedCallback(undefined, result);
}
} else {
/* Codes_SRS_NODE_DEVICE_MQTT_18_026: When MqttTransport fires the close event, the Mqtt object shall emit a disconnect event */
this.emit('disconnect', err);
}
},
/*Codes_SRS_NODE_DEVICE_MQTT_16_021: [The `disconnect` method shall call its callback immediately with a `null` argument and a `results.Disconnected` second argument if `MqttBase` is already disconnected.]*/
this._config = config;
this._topicTelemetryPublish = 'devices/' + this._config.deviceId + '/messages/events/';
this._topicMessageSubscribe = 'devices/' + this._config.deviceId + '/messages/devicebound/#';
this._topicMethodSucbscribe = '$iothub/methods/POST/#';
debug('topic publish: ' + this._topicTelemetryPublish);
debug('topic subscribe: ' + this._topicMessageSubscribe);
const sdkVersionString = encodeURIComponent('azure-iot-device/' + packageJson.version);
/*Codes_SRS_NODE_DEVICE_MQTT_16_016: [The Mqtt constructor shall initialize the `uri` property of the `config` object to `mqtts://`.]*/
(this._config as any).uri = 'mqtts://' + config.host;
/* Codes_SRS_NODE_DEVICE_MQTT_18_025: [ If the Mqtt constructor receives a second parameter, it shall be used as a provider in place of mqtt.js ]*/
if (provider) {
this._mqtt = provider;
} else {
this._mqtt = new MqttBase(sdkVersionString);
}
/* Codes_SRS_NODE_DEVICE_MQTT_18_026: When MqttTransport fires the close event, the Mqtt object shall emit a disconnect event */
this._mqtt.on('error', (err) => {
debug('on close');
this._fsm.handle('disconnect', () => {
this.emit('disconnect', err);
});
});
this._mqtt.on('message', this._dispatchMqttMessage.bind(this));
// MQTT topics to subscribe to
this._topics = {
'message': {
name: this._topicMessageSubscribe,
this._mqtt.publish(topic, body.toString(), { qos: 0, retain: false }, (err, puback) => {
if (err) {
/* Codes_SRS_NODE_DEVICE_MQTT_18_016: [** If an error occurs in the `sendTwinRequest` method, the `done` callback shall be called with the error as the first parameter. **]** */
/* Codes_SRS_NODE_DEVICE_MQTT_18_024: [** If an error occurs, the `sendTwinRequest` shall use the MQTT `translateError` module to convert the mqtt-specific error to a transport agnostic error before passing it into the `done` callback. **]** */
callback(translateError(err));
} else {
/* Codes_SRS_NODE_DEVICE_MQTT_18_004: [** If a `done` callback is passed as an argument, The `sendTwinRequest` method shall call `done` after the body has been published. **]** */
/* Codes_SRS_NODE_DEVICE_MQTT_18_017: [** If the `sendTwinRequest` method is successful, the first parameter to the `done` callback shall be null and the second parameter shall be a MessageEnqueued object. **]** */
callback(null, new results.MessageEnqueued(puback));
}
});
},
constructor(mqttBase?: MqttBase) {
super();
this._mqttBase = mqttBase || new MqttBase();
this._config.pollingInterval = ProvisioningDeviceConstants.defaultPollingInterval;
const responseHandler = (topic: string, payload: any) => {
let payloadString: string = payload.toString('ascii');
debug('message received on ' + topic);
debug('request payload is: ' + payloadString);
/* Codes_SRS_NODE_PROVISIONING_MQTT_18_010: [ When waiting for responses, `registrationRequest` shall watch for messages with a topic named $dps/registrations/res//?$rid=.] */
/* Codes_SRS_NODE_PROVISIONING_MQTT_18_024: [ When waiting for responses, `queryOperationStatus` shall watch for messages with a topic named $dps/registrations/res//?$rid=.] */
let match = topic.match(/^\$dps\/registrations\/res\/(.*)\/\?(.*)$/);
if (!!match && match.length === 3) {
let queryParameters = queryString.parse(match[2]);
if (queryParameters.$rid) {
let rid: string = queryParameters.$rid as string;
if (this._operations[rid]) {
_onEnter: (callback, err) => {
this._mqtt.removeListener('message', messageHandler);
if (callback) {
callback(err);
} else if (err) {
this.emit('error', translateError(err));
}
},
subscribe: (callback) => this._fsm.transition('subscribingToResponseTopic', callback),
this._fsm.handle('connect', (err) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_MQTT_16_024: [The `sendEvent` method shall call its callback with an `Error` that has been translated using the `translateError` method if the `MqttBase` object fails to establish a connection.]*/
sendEventCallback(translateError(err));
} else {
this._fsm.handle('sendEvent', topic, payload, options, sendEventCallback);
}
});
},
this._mqtt.publish(topic, body.toString(), { qos: 0, retain: false }, (err, puback) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_MQTT_TWIN_CLIENT_16_008: [If an error happen while publishing the request message, the `callback` shall be called with the translated version of this error obtained by using the `translateError` method of the `azure-iot-mqtt-base` package.]*/
/*Codes_SRS_NODE_DEVICE_MQTT_TWIN_CLIENT_16_018: [If an error happen while publishing the request message, the `callback` shall be called with the translated version of this error obtained by using the `translateError` method of the `azure-iot-mqtt-base` package.]*/
delete this._pendingTwinRequests[requestId];
callback(translateError(err));
} else {
debug('twin request sent: ' + puback);
}
});
}