How to use the azure-iothub.Client.fromSharedAccessSignature function in azure-iothub

To help you get started, we’ve selected a few azure-iothub examples, based on popular ways it is used in public projects.

github Azure / iothub-explorer / iothub-explorer-monitor-uploads.js View on Github external
var getSas = require('./common.js').getSas;

// Azure Event Hubs dependencies
var ServiceClient = require('azure-iothub').Client;
var showDeprecationText = require('./common.js').showDeprecationText;

showDeprecationText('There is no equivalent command in the Azure CLI - if this feature is important to you please open an issue on the Azure CLI IoT Extension repository (https://aka.ms/iotcli)');

program
  .description('Monitor file upload notifications emitted by devices')
  .option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .parse(process.argv);

var sas = getSas(program.login);
var client = ServiceClient.fromSharedAccessSignature(sas.toString());
client.open(function (err) {
  if (err) {
    inputError('Could not open the connection to the service: ' + err.message);
  } else {
    client.getFileNotificationReceiver(function (err, receiver) {
      if (err) serviceError(err);
      if (!program.raw) {
        console.log(chalk.yellow('Waiting for file notifications...') + ' (Ctrl-C to quit)');
      }

      receiver.on('errorReceived', function (err) { serviceError(err); });
      receiver.on('message', function (fileNotification) {
        var notif = JSON.parse(fileNotification.data.toString());
        var rendered = program.raw ?
          JSON.stringify(notif) :
          prettyjson.render(notif) + '\n----------------------------\n';
github Azure / iothub-explorer / iothub-explorer-monitor-feedback.js View on Github external
var getSas = require('./common.js').getSas;

// Azure Event Hubs dependencies
var ServiceClient = require('azure-iothub').Client;
var showDeprecationText = require('./common.js').showDeprecationText;

showDeprecationText('az iot hub monitor-feedback');

program
  .description('Monitor feedback messages sent by devices when they receive a cloud-to-device (c2d) message.')
  .option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .parse(process.argv);

var sas = getSas(program.login);
var client = ServiceClient.fromSharedAccessSignature(sas.toString());
client.open(function (err) {
  if (err) {
    inputError('Could not open the connection to the service: ' + err.message);
  } else {
    client.getFeedbackReceiver(function (err, receiver) {
      if (err) serviceError(err);
      if (!program.raw) {
        console.log(chalk.yellow('Waiting for feedback...') + ' (Ctrl-C to quit)');
      }

      receiver.on('errorReceived', function (err) { serviceError(err); });
      receiver.on('message', function (feedbackRecords) {
        var records = JSON.parse(feedbackRecords.data);
        var output = {
          originalMessageId: records[0].originalMessageId,
          'iothub-enqueuedtime': records[0].enqueuedTimeUtc,
github Azure / iothub-explorer / iothub-explorer-send.js View on Github external
program
  .description('Send a message to device (cloud-to-device/C2D).')
  .option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .option('-a, --ack <ack-type>', 'set the type of feedback that you would like to receive: none|positive|negative|full')
  .parse(process.argv);

if(!program.args[0]) inputError('You need to specify a device id.');
if(!program.args[1]) inputError('You need to specify a message.');

var deviceId = program.args[0];
var messageArg = program.args[1];
var ack = program.ack;

var sas = getSas(program.login);
var client = ServiceClient.fromSharedAccessSignature(sas.toString());
client.open(function (err) {
  if (err) {
    inputError('Could not open the connection to the service: ' + err.message);
  } else {
    var message = createMessageFromArgument(messageArg, ack);
    client.send(deviceId, message, function (err) {
      if (err) serviceError(err);
      if(program.raw) {
        console.log(message.messageId);
      } else {
        var successMessage = chalk.green('Message sent with id: ') + message.messageId;
        if (program.ack) successMessage += chalk.grey('. Acknowledgement requested: ' + program.ack);
        console.log(successMessage);
      }
      client.close(function(err) {
        if(err) serviceError(err);
github Azure / iothub-explorer / iothub-explorer-device-method.js View on Github external
.parse(process.argv);

if(!program.args[0]) inputError('You must specify a deviceId.');
if(!program.args[1]) inputError('You must specify the JSON to update the twin with.');

var deviceId = program.args[0];
var methodName = program.args[1];
var methodPayload = program.args[2];
var methodTimeout = program.args[3];

if(!deviceId) inputError('Please provide a valid device id');
if(!methodName) inputError('Please provide a valid method name');

var sas = getSas(program.login);

var client = Client.fromSharedAccessSignature(sas);

var actualPayload = methodPayload;

if (methodPayload) {
  try {
    actualPayload = JSON.parse(methodPayload);
  } catch (err) {
    if (!(err instanceof SyntaxError)) {
      throw err;
    }
  }
}

var methodParams = {
  methodName: methodName,
  payload: actualPayload || null,