How to use azure-iot-device - 10 common examples

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 / azure-iot-sdk-node / ts-e2e / src / device_twin.tests.ts View on Github external
describe('Over ' + transportCtor.name, () => {
      // tslint:disable:no-invalid-this
      this.timeout(60000);

      const testDevice = testUtils.createTestDevice();
      const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
      const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

      beforeEach((beforeEachCallback) => {
        testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
      });

      afterEach((afterEachCallback) => {
        testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
      });

      it('device can get its device twin and modify reported properties', (testCallback) => {
        const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
        const twinPatch = { twinKey: 'twinValue' };

        deviceClient.open((err) => {
          if (err) throw err;
          debug('Device Client: Opened');
github Azure / azure-iot-sdk-node / ts-e2e / src / device_methods.tests.ts View on Github external
describe('Over ' + transportCtor.name, () => {
      // tslint:disable:no-invalid-this
      this.timeout(60000);

      const testDevice = testUtils.createTestDevice();
      const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
      const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

      beforeEach((beforeEachCallback) => {
        testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
      });

      afterEach((afterEachCallback) => {
        testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
      });

      [null, '', 'foo', { k1: 'v1' }, {}].forEach((testPayload) => {
      it('device can receive a method call with a payload of ' + JSON.stringify(testPayload) + ' and send a response', (testCallback) => {
        const methodName = 'testMethod';
        const requestPayload = testPayload;
        const responsePayload = { responseKey: uuid.v4() };
        const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
        let sendOK = false;
github Azure / iothub-diagnostics / iothubtests / iotClient.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';

const logger = require('../lib').logger;
const config = require('../config')
const iothub = require('azure-iothub');
const Message = require('azure-iot-device').Message;
const DeviceConnectionString = require('azure-iot-device').ConnectionString;
const DeviceClient = require('azure-iot-device').Client;

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');
github Azure / azure-iot-sdk-node / ts-e2e / src / c2d.tests.ts View on Github external
describe('C2D', () => {
  // tslint:disable:no-invalid-this
  this.timeout(60000);
  const testDevice2 = testUtils.createTestDevice();

  const hostName = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
  const testDeviceCS2 = DeviceConnectionString.createWithSharedAccessKey(hostName, testDevice2.deviceId, testDevice2.authentication.symmetricKey.primaryKey);

  before((beforeCallback) => {
    testUtils.addTestDeviceToRegistry(testDevice2, beforeCallback);
  });

  after((afterCallback) => {
    testUtils.removeTestDeviceFromRegistry(testDevice2, afterCallback);
  });

  [DeviceAmqp, DeviceAmqpWs, DeviceMqtt, DeviceMqttWs, DeviceHttp].forEach((transportCtor: any) => {
    describe('Over ' + transportCtor.name, () => {
      let deviceClient: DeviceClient;

      beforeEach((beforeEachCallback) => {
        deviceClient = DeviceClient.fromConnectionString(testDeviceCS2, transportCtor);
        deviceClient.open((err) => {
github Azure / azure-iot-sdk-node / ts-e2e / src / file_upload.tests.ts View on Github external
describe('File upload', () => {
  // tslint:disable:no-invalid-this
  this.timeout(60000);
  const testDevice = testUtils.createTestDevice();

  const hostName = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
  const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(hostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

  const fileConfig = {
    fileName: 'smallFile',
    fileSizeInKb: 10,
  };

  before((beforeCallback) => {
    let fileContent = Buffer.alloc(fileConfig.fileSizeInKb * 1024);
    fileContent.fill(uuid.v4());
    fs.writeFileSync(fileConfig.fileName, fileContent);
    testUtils.addTestDeviceToRegistry(testDevice, beforeCallback);
  });

  after((afterCallback) => {
    fs.unlinkSync(fileConfig.fileName);
    testUtils.removeTestDeviceFromRegistry(testDevice, afterCallback);
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-node-ping / devicesample / node / node_device.js View on Github external
function main() {
  // receive the IoT Hub connection string as a command line parameter
  if(process.argv.length < 3) {
    console.error('Usage: node_device.js <>');
    process.exit(1);
  }

  // open a connection to the device
  deviceConnectionString = process.argv[2];

  client = Client.fromConnectionString(deviceConnectionString, Protocol);
  client.open(onConnect);
}
github Azure / azure-iot-sdk-node / device / samples / simple_sample_module_method.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 ModuleClient = require('azure-iot-device').ModuleClient;

ModuleClient.fromEnvironment(Protocol, function (err, client) {
  if (err) {
    console.error("Could not create client: " + err.toString());
    process.exit(-1);
  } else {
    console.log('got client');

    client.on('error', function (err) {
      console.error(err.message);
    });

    // connect to the edge instance
    client.open(function (err) {
      if (err) {
        console.error('Could not connect: ' + err.message);
      } else {
        console.log('Client connected');
github Azure / azure-iot-sdk-node / device / samples / simple_sample_module_twin.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 ModuleClient = require('azure-iot-device').ModuleClient;
var Protocol = require('azure-iot-device-mqtt').Mqtt;

ModuleClient.fromEnvironment(Protocol, function (err, client) {
  if (err) {
    console.error("Could not create client: " + err.toString());
    process.exit(-1);
  } else {
    console.log('got client');

    client.on('error', function (err) {
      console.error(err.message);
    });

    // connect to the edge instance
    client.open(function(err) {
      if (err) {
        console.error('could not open IotHub client');
      }  else {
        console.log('client opened');
github Azure / azure-iot-sdk-node / device / samples / simple_sample_module.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 ModuleClient = require('azure-iot-device').ModuleClient;
var Message = require('azure-iot-device').Message;

ModuleClient.fromEnvironment(Protocol, function (err, client) {
  if (err) {
    console.error("Could not create client: " + err.toString());
    process.exit(-1);
  } else {
    console.log('got client');

    client.on('error', function (err) {
      console.error(err.message);
    });

    client.open(function (err) {
      if (err) {
        console.error('Could not connect: ' + err.message);
      } else {
        console.log('Client connected');