Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
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);
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);
// 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"
// 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());
};
}
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);
});
}
};
}
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() {
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();
}