Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
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]) {