How to use the azure-iot-device.ConnectionString.parse function in azure-iot-device

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

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / iothub-diagnostics / iothubtests / iotClient.js View on Github external
var runTest = function(deviceConnectionString, protocol, done) {
  var testDeviceId = DeviceConnectionString.parse(deviceConnectionString).DeviceId;
  var client = DeviceClient.fromConnectionString(deviceConnectionString, protocol);

  logger.trace('Opening the client for ' + testDeviceId);

  // Connect to the service
  client.open(function (err) {
    if (err) {
      logger.fatal('Could not connect to IOT HUB: ' + err);
      return done(err);
    } else {
      logger.debug('Client connected, sending message');

      // Listen for messages
      client.on('message', function (msg) {
        if(JSON.parse(msg.data) === config.testCommand) {
          client.complete(msg, function(err, res) {
github Azure-Samples / iot-hub-c-huzzah-getstartedkit / command_center_node / server.js View on Github external
var Message = require('azure-iot-common').Message;
var EventHubClient = require('azure-event-hubs').Client;
var DeviceConnectionString = require('azure-iot-device').ConnectionString;
var azure = require('azure-storage');
var nconf = require('nconf');

nconf.argv().env().file('./config.json');
var eventHubName = nconf.get('eventHubName');
var ehConnString = nconf.get('ehConnString');
var deviceConnString = nconf.get('deviceConnString');
var storageAcountName = nconf.get('storageAcountName');
var storageAccountKey = nconf.get('storageAccountKey');
var storageTable = nconf.get('storageTable');
var iotHubConnString = nconf.get('iotHubConnString');

var deviceId = DeviceConnectionString.parse(deviceConnString).DeviceId;
var iotHubClient = ServiceClient.fromConnectionString(iotHubConnString);

// event hub alerts
var alerts = [];
var ehclient = EventHubClient.fromConnectionString(ehConnString, eventHubName)
ehclient.createReceiver('$Default', '0', { startAfterTime: Date.now() })
    .then(function(rx) {
        rx.on('errorReceived', function(err) { console.log(err); });
        rx.on('message', function(message) {
            alerts.push(message.body);
            alerts = alerts.slice(-5); // keep last 5
        });
    });

// table storage
var tableSvc = azure.createTableService(storageAcountName, storageAccountKey);
github Azure-Samples / iot-hub-c-m0wifi-getstartedkit / command_center_node / server.js View on Github external
var Message = require('azure-iot-common').Message;
var EventHubClient = require('azure-event-hubs').Client;
var DeviceConnectionString = require('azure-iot-device').ConnectionString;
var azure = require('azure-storage');
var nconf = require('nconf');

nconf.argv().env().file('./config.json');
var eventHubName = nconf.get('eventHubName');
var ehConnString = nconf.get('ehConnString');
var deviceConnString = nconf.get('deviceConnString');
var storageAcountName = nconf.get('storageAcountName');
var storageAccountKey = nconf.get('storageAccountKey');
var storageTable = nconf.get('storageTable');
var iotHubConnString = nconf.get('iotHubConnString');

var deviceId = DeviceConnectionString.parse(deviceConnString).DeviceId;
var iotHubClient = ServiceClient.fromConnectionString(iotHubConnString);

// event hub alerts
var alerts = [];
var ehclient = EventHubClient.fromConnectionString(ehConnString, eventHubName)
ehclient.createReceiver('$Default', '0', { startAfterTime: Date.now() })
    .then(function(rx) {
        rx.on('errorReceived', function(err) { console.log(err); });
        rx.on('message', function(message) {
            alerts.push(message.body);
            alerts = alerts.slice(-5); // keep last 5
        });
    });

// table storage
var tableSvc = azure.createTableService(storageAcountName, storageAccountKey);
github Azure-Samples / iot-hub-node-intel-edison-getstartedkit / command_center / command_center.js View on Github external
// Edison packages
var five = require("johnny-five");
var Edison = require("edison-io");
var board = new five.Board({
  io: new Edison()
});

var hostName = '';
var deviceId = '';
var sharedAccessKey = '';

// String containing Hostname, Device Id & Device Key in the following formats:
//  "HostName=;DeviceId=;SharedAccessKey="
var connectionString = 'HostName=' + hostName + ';DeviceId=' + deviceId + ';SharedAccessKey=' + sharedAccessKey;

var deviceId = ConnectionString.parse(connectionString)["DeviceId"];

// fromConnectionString must specify a transport constructor, coming from any transport package.
var client = Client.fromConnectionString(connectionString, Protocol);

// Helper function to print results in the console
function printResultFor(op) {
  return function printResult(err, res) {
    if (err) console.log(op + ' error: ' + err.toString());
    if (res) console.log(op + ' status: ' + res.constructor.name);
  };
}

board.on("ready", function() {
  var temp = new five.Temperature({
    pin: "A0",
    controller: "GROVE"
github Azure / azure-iot-sdk-node / device / samples / remote_monitoring.js View on Github external
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-device').ConnectionString;
var Message = require('azure-iot-device').Message;

// String containing Hostname, Device Id & Device Key in the following formats:
//  "HostName=;DeviceId=;SharedAccessKey="
var deviceConnectionString = process.env.DEVICE_CONNECTION_STRING;
var deviceId = ConnectionString.parse(connectionString).DeviceId;

// Sensors data
var temperature = 50;
var humidity = 50;
var externalTemperature = 55;

// Create IoT Hub client
var client = Client.fromConnectionString(deviceConnectionString, Protocol);

// Helper function to print results for an operation
function printErrorFor(op) {
  return function printError(err) {
    if (err) console.log(op + ' error: ' + err.toString());
  };
}
github microsoft / vscode-azure-iot-toolkit / src / iotHubC2DMessageExplorer.ts View on Github external
return (err) => {
            if (err) {
                this.outputLine(Constants.IoTHubC2DMessageMonitorLabel, err);
                TelemetryClient.sendEvent(Constants.IoTHubAIStartMonitorC2DEvent, { Result: "Exception", Message: err });
            } else {
                this.updateMonitorStatus(true);
                let deviceId = ConnectionString.parse(deviceConnectionString).DeviceId;
                this.outputLine(Constants.IoTHubC2DMessageMonitorLabel, `Start receiving C2D message for [${deviceId}]...`);
                TelemetryClient.sendEvent(Constants.IoTHubAIStartMonitorC2DEvent);
                this._deviceClient.on("message", (msg) => {
                    this.outputLine(Constants.IoTHubC2DMessageMonitorLabel, "Message Received: " + msg.getData());
                    if (msg.properties && msg.properties.propertyList && msg.properties.propertyList.length > 0) {
                        this._outputChannel.appendLine("Properties:");
                        this._outputChannel.appendLine(JSON.stringify(msg.properties.propertyList, null, 2));
                    }
                    this._deviceClient.complete(msg, this.printResult);
                });
            }
        };
    }
github Azure / iothub-explorer / iothub-explorer-simulate-device.js View on Github external
break;
}

var sendInterval = program.sendInterval || 1000;
var sendCount = program.sendCount || Number.MAX_SAFE_INTEGER;
var receiveCount = program.receiveCount || Number.MAX_SAFE_INTEGER;
var uploadFilePath = program.uploadFile;

var deviceConnectionString;
var deviceId = program.args[0];
if (!deviceId) {
  deviceConnectionString = program.deviceConnectionString;
  if(!deviceConnectionString) {
    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');
  } else {
    deviceId = DeviceConnectionString.parse(deviceConnectionString).DeviceId;
    simulateDevice();
  }
} else {
  var registry = Registry.fromSharedAccessSignature(sas.toString());
  registry.get(deviceId, function(err, deviceInfo) {
    if (err) serviceError(err);
    else {
      var host = getHostFromSas(sas.toString());
      deviceConnectionString = createDeviceConnectionString(deviceInfo, host);
    }

    simulateDevice();
  });
}

function simulateDevice() {
github microsoft / vscode-azure-iot-toolkit / src / utility.ts View on Github external
public static generateSasTokenForDevice(deviceConnectionString: string, expiryInHours = 1): string {
        const connectionString = DeviceConnectionString.parse(deviceConnectionString);
        const expiry = Math.floor(Date.now() / 1000) + expiryInHours * 60 * 60;
        return DeviceSharedAccessSignature.create(connectionString.HostName, connectionString.DeviceId, connectionString.SharedAccessKey, expiry).toString();
    }