Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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').ModuleClient;
var Message = require('azure-iot-device').Message;
var fs = require('fs');
var connectionString = process.env.EdgeHubConnectionString;
var client = Client.fromConnectionString(connectionString, Protocol);
console.log('got client');
client.on('error', function (err) {
console.error(err.message);
});
client.setOptions({
ca: fs.readFileSync(process.env.EdgeModuleCACertificateFile).toString('ascii')
}, function(err) {
if (err) {
console.log('error:' + err);
} else {
// connect to the edge instance
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
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
return glueUtils.makePromise('moduleConnectPut', function(callback) {
var client = ModuleClient.fromConnectionString(connectionString, glueUtils.transportFromType(transportType));
var connectionId = objectCache.addObject('moduleClient', client);
glueUtils.setOptionalCert(client, caCertificate, function(err) {
glueUtils.debugFunctionResult('glueUtils.setOptionalCert', err);
if (err) {
callback(err);
} else {
debug('calling moduleClient.open');
client.open(function(err) {
glueUtils.debugFunctionResult('client.open', err);
if (err) {
objectCache.removeObject(connectionId);
callback(err);
} else {
callback(null, {connectionId: connectionId});
}
});