Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'azure-iot-device-amqp' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 */
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"
});
var led = new five.Led(8);
create(broker, configuration) {
this.broker = broker;
this.configuration = configuration;
if(this.configuration && this.configuration.connection_string) {
// open a connection to the IoT Hub
this.iothub_client = Client.fromConnectionString(this.configuration.connection_string, Protocol);
this.iothub_client.open(this.on_connect.bind(this));
return true;
}
else {
console.error('This module requires the connection string to be passed in via configuration.');
return false;
}
}
'use strict';
var Protocol = require('azure-iot-device-amqp').Amqp;
// Uncomment one of these transports and then change it in fromConnectionString to test other transports
// var Protocol = require('azure-iot-device-amqp-ws').AmqpWs;
// var Protocol = require('azure-iot-device-http').Http;
// var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
// String containing Hostname, Device Id & Device Key in the following formats:
// "HostName=;DeviceId=;SharedAccessKey="
var connectionString = process.env.IOTHUB_DEVICE_CONN || 'YOUR IOT HUB DEVICE-SPECIFIC CONNECTION STRING HERE';
// fromConnectionString must specify a transport constructor, coming from any transport package.
var client = Client.fromConnectionString(connectionString, Protocol);
var connectCallback = function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
client.on('message', function (msg) {
console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
client.complete(msg, printResultFor('completed'));
// reject and abandon follow the same pattern.
// /!\ reject and abandon are not available with MQTT
});
// Create a message and send it to the IoT Hub every second
var sendInterval = setInterval(function () {
var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
// Define the protocol that will be used to send messages to Azure IoT Hub
// For this lab we will use AMQP over Web Sockets.
// If you want to use a different protocol, comment out the protocol you want to replace,
// and uncomment one of the other transports.
// var Protocol = require('azure-iot-device-amqp-ws').AmqpWs;
var Protocol = require('azure-iot-device-amqp').Amqp;
// var Protocol = require('azure-iot-device-http').Http;
// var Protocol = require('azure-iot-device-mqtt').Mqtt;
// The device-specific connection string to your Azure IoT Hub
var connectionString = process.env.IOTHUB_DEVICE_CONN || 'YOUR IOT HUB DEVICE-SPECIFIC CONNECTION STRING HERE';
// Create the client instanxe that will manage the connection to your IoT Hub
// The client is created in the context of an Azure IoT device.
var client = Client.fromConnectionString(connectionString, Protocol);
// Extract the Azure IoT Hub device ID from the connection string
var deviceId = device.ConnectionString.parse(connectionString).DeviceId;
// location is simply a string that you can filter on later
var location = process.env.DEVICE_LOCATION || 'GIVE A NAME TO THE LOCATION OF THE THING';
// Define the sensors you will use
var th02, lcd, led, button;
// Define some variable for holding sensor values
var tempC, tempF, humidity, r, g, b = 0;
// Define the board, which is an abstraction of the Intel Edison
var board = new five.Board({
io: new Edison()
// 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" }
};
/*
// PARTICLE PHOTON USERS
// Add the following definition for the Particle plugin for Johnny-Five
var Particle = require("particle-io");
var token = 'YOUR PARTICLE ACCESS TOKEN HERE';
*/
var location = process.env.DEVICE_LOCATION || 'GIVE A NAME TO THE LOCATION OF THE THING';
var connectionString = process.env.IOTHUB_CONN || 'YOUR IOT HUB DEVICE-SPECIFIC CONNECTION STRING HERE';
// Create an Azure IoT client that will manage the connection to your IoT Hub
// The client is created in the context of an Azure IoT device, which is why
// you use a device-specific connection string.
var client = clientFromConnectionString(connectionString);
var deviceId = device.ConnectionString.parse(connectionString).DeviceId;
// Create a Johnny-Five board instance to represent your Particle Photon
// Board is simply an abstraction of the physical hardware, whether is is a
// Photon, Arduino, Raspberry Pi or other boards.
var board = new five.Board();
/*
// PARTICLE PHOTON USERS
// When creating a Board instance for the Photon you must specify the token and device ID
// for your Photon using the Particle-IO Plugin for Johnny-five.
// Replace the Board instantiation with the following:
var board = new five.Board({
io: new Particle({
token: token,
deviceId: 'YOUR PARTICLE PHOTON DEVICE IS OR ALIAS'
})
function amqpWsTest(deviceConnectionString, done) {
runTest(deviceConnectionString, require('azure-iot-device-amqp').AmqpWs, 'AMQP/WS', done);
}
describe('Over AMQP/WS', function () {
this.timeout(30000);
runTests(AmqpWs, connectionString, badConnStrings);
});