Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const ERROR_EXIT_CODE = -1;
const OK_EXIT_CODE = 0;
const hubConnectionString = process.env.IOTHUB_CONNECTION_STRING;
const registry = Registry.fromConnectionString(hubConnectionString);
const deviceId = 'node-longhaul-sak-' + uuid.v4();
let protocol;
/* tslint:disable:no-var-requires */
switch (process.env.DEVICE_PROTOCOL) {
case 'amqp':
protocol = require('azure-iot-device-amqp').Amqp;
break;
case 'amqp-ws':
protocol = require('azure-iot-device-amqp').AmqpWs;
break;
case 'mqtt':
protocol = require('azure-iot-device-mqtt').Mqtt;
break;
case 'mqtt-ws':
protocol = require('azure-iot-device-mqtt').MqttWs;
break;
case 'http':
protocol = require('azure-iot-device-mqtt').Http;
break;
default:
debug('unknown protocol: ' + process.env.DEVICE_PROTOCOL);
process.exit(ERROR_EXIT_CODE);
}
/* tslint:enable:no-var-requires */
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
/*jshint esversion: 6 */
var respondWithCode = require('../utils/writer').respondWithCode;
var debug = require('debug')('azure-iot-e2e:node')
var Mqtt = require('azure-iot-device-mqtt').Mqtt;
var MqttWs = require('azure-iot-device-mqtt').MqttWs;
var Amqp = require('azure-iot-device-amqp').Amqp;
var AmqpWs = require('azure-iot-device-amqp').AmqpWs;
var Http = require('azure-iot-device-http').Http;
/**
* return failure to the caller, passing the appropriate information back.
*
* @param {function} reject reject function from Promise
* @param {Error} err error to return
* @param {string} functionName name of function with failure
*/
var returnFailure = function(reject, err, functionName) {
var errorText = '';
if (functionName) {
errorText = 'failure from ' + functionName + ': ';
}
if (err.responseBody) {
errorText += err.responseBody;
function AzureIoTHubNode(config) {
// Store node for further use
var node = this;
var nodeConfig = config;
var Client = require('azure-iot-device').Client;
var Protocols = {
amqp: require('azure-iot-device-amqp').Amqp,
mqtt: require('azure-iot-device-mqtt').Mqtt,
http: require('azure-iot-device-http').Http,
amqpWs: require('azure-iot-device-amqp').AmqpWs
};
var Message = require('azure-iot-device').Message;
var client = null;
var clientConnectionString = "";
var clientProtocol = "";
var statusEnum = {
disconnected: { color: "red", text: "Disconnected" },
connected: { color: "green", text: "Connected" },
sent: { color: "blue", text: "Sent message" },
received: { color: "yellow", text: "Received" },
error: { color: "grey", text: "Error" }
};
function amqpWsTest(deviceConnectionString, done) {
runTest(deviceConnectionString, require('azure-iot-device-amqp').AmqpWs, 'AMQP/WS', done);
}
if(!program.deviceConnectionString && !program.args[0]) {
inputError('You must specify either a device connection string (--device-connection-string) or the IoT Hub connection string and a device id as first argument');
}
if (!program.send && !program.receive && !program.uploadFile) {
inputError('Nothing to do: please use --send, --receive or --uploadFile');
}
var settleMethod = program.settle || 'complete';
var protocolArg = program.protocol || 'amqp';
var Protocol;
switch(protocolArg) {
case 'amqp-ws':
Protocol = require('azure-iot-device-amqp').AmqpWs;
break;
case 'http':
Protocol = require('azure-iot-device-http').Http;
break;
case 'mqtt':
Protocol = require('azure-iot-device-mqtt').Mqtt;
if (settleMethod !== 'complete') {
inputError('Cannot ' + settleMethod + ' messages with MQTT: messages are automatically completed.');
}
break;
case 'mqtt-ws':
Protocol = require('azure-iot-device-mqtt').MqttWs;
if (settleMethod !== 'complete') {
inputError('Cannot ' + settleMethod + ' messages with MQTT: messages are automatically completed.');
}
break;