Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const run = async() => {
try {
console.log('Initializing Module Client.');
const moduleClient = ModuleClient.fromConnectionString(process.env.DEVICE_CONNECTION_STRING, Mqtt);
// We use the iot hub connection string to create the client rather than the actual event hub end point
console.log('Initializing Event Hub Client.');
const eventHubClient = await EventHubClient.createFromIotHubConnectionString(process.env.IOTHUB_CONNECTION_STRING);
console.log('Initializing HTTP Device Client.');
const httpClient = HttpClientFromConnectionString(process.env.DEVICE_CONNECTION_STRING);
console.log('Initialized clients!');
console.log('Getting information from Event Hub.');
const partitionIds = await eventHubClient.getPartitionIds(); //read more about partitions https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-features#partitions
const startingPosition = EventPosition.fromEnqueuedTime(Date.now() - 1000); //subtracting a second to account for delay
const eventLabel = 'beepBoop'; //label our events
const sendDummyMessage = m => moduleClient.sendOutputEvent(eventLabel, m); //send helper
console.log('Creating messages to send to via Module Client');
//generate some messages
const messageCount = 10;
const importantMessages = [...Array(messageCount).keys()].map(generateImportantMessage);
console.log('Sending messages via Module Client.');
//iterative send approach
// 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 clientFromConnectionString = require('azure-iot-device-http').clientFromConnectionString;
var Message = require('azure-iot-device').Message;
// String containing Hostname, Device Id & Device Key in the following formats:
// "HostName=;DeviceId=;SharedAccessKey="
var connectionString = '[IoT Hub device connection string]';
var client = clientFromConnectionString(connectionString);
// Create a message and send it to the IoT hub every second
setInterval(function () {
var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
var data = JSON.stringify({
deviceId: 'myFirstDevice',
windSpeed: windSpeed
});
var message = new Message(data);
message.properties.add('myproperty', 'myvalue');
console.log('Sending message: ' + message.getData());
client.sendEvent(message, printResultFor('send'));
}, 1000);
// Monitor messages from IoT Hub and print them in the console.
// Note: In this sample, the polling interval is set to 1 second
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;
// Sensor data
var temperature = 0;
var humidity = 0;
var externalTemperature = 0;
// Create IoT Hub client
var client = Client.fromConnectionString(connectionString, Protocol);
// Helper function to print results for an operation
function printErrorFor(op) {
return function printError(err) {
if (err) console.log(op + ' error: ' + err.toString());
};
}
// Send device meta data
var deviceMetaData = {
'ObjectType': 'DeviceInfo',
'IsSimulatedDevice': 0,
'Version': '1.0',
'DeviceProperties': {
'DeviceID': deviceId,
'HubEnabledState': 1,
vscode.window.showInputBox({ prompt: 'Enter message to send' }).then((message: string) => {
if (message !== undefined) {
try {
let client = clientFromConnectionString(deviceConnectionString);
client.sendEvent(new Message(JSON.stringify(message)), this.sendEventDone(true, client, label));
}
catch (e) {
this.outputLine(label, e);
}
}
});
}