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';
const logger = require('../lib').logger;
const config = require('../config');
const EventHubClient = require('@azure/event-hubs').EventHubClient;
const EventPosition = require('@azure/event-hubs').EventPosition;
const ServiceClient = require('azure-iothub').Client;
var serviceConnection = null;
var eventHubConnection = null;
// Called whenever an error occurs in either the message callback or the eventhub connection setup
function errorCb(err) {
logger.crit(err.message);
};
// Called whenever we receive a telemetry message from the client
function messageReceivedCb(message) {
logger.debug('Service successfully received telemetry from client.');
logger.trace(JSON.stringify(message));
var targetDevice = message.body.toString();
if(!targetDevice) {
const client = await event_hubs_1.EventHubClient.createFromIotHubConnectionString(IOT_HUB_CONNECTION_STRING);
const hubInfo = await client.getHubRuntimeInformation();
console.log(`Connected to IoT Hub at ${hubInfo.path}`);
client.receive('1', (eventData) => {
if (eventData.annotations) {
const enqueuedTime = eventData.annotations['x-opt-enqueued-time'];
console.debug(`Received message from IoT Hub, enqueued at ${enqueuedTime}`);
}
else {
console.debug(`Received message from IoT Hub`);
}
db_1.updateState(eventData.body);
}, (error) => {
console.error(`Error receiving message from Event Hubs: ${error}`);
}, {
eventPosition: event_hubs_1.EventPosition.fromEnqueuedTime(Date.now())
});
}
function getConfig(deviceId, cb) {