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 JobClient = require('azure-iothub').JobClient;
var connectionString = process.env.IOTHUB_CONNECTION_STRING;
var jobClient = JobClient.fromConnectionString(connectionString);
var query = jobClient.createQuery();
var onResults = function(err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function(job) {
console.log(JSON.stringify(job, null, 2));
});
if (query.hasMoreResults) {
query.next(onResults);
}
}
};
// 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 uuid = require('uuid');
var JobClient = require('azure-iothub').JobClient;
var connectionString = process.env.IOTHUB_CONNECTION_STRING;
var queryCondition = "deviceId IN ['', '']"; // example queryCondition = "deviceId IN ['MyDevice1', 'MyDevice2']"
// For a single device you can also set queryCondition as "deviceId = ''" . Example, "deviceId = 'MyDevice1'"
var startTime = new Date();
var maxExecutionTimeInSeconds = 3600;
var jobClient = JobClient.fromConnectionString(connectionString);
// Schedule a device method call.
var methodParams = {
methodName: 'methodName',
payload: null,
responseTimeoutInSeconds: 15 // set response timeout as 15 seconds
};
var methodJobId = uuid.v4();
console.log('scheduling Device Method job with id: ' + methodJobId);
jobClient.scheduleDeviceMethod(methodJobId,
queryCondition,
methodParams,
startTime,
maxExecutionTimeInSeconds,
function(err) {
it('schedules a twin update job with a sql query and succeeds', (testCallback) => {
const jobClient = JobClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
const testJobId = uuid.v4();
const testTwinPatch = {
etag: '*',
tags: {
key: 'value2'
},
properties: { desired: {}, reported: {} }
};
debug('scheduling a twin update job with a sql query with the id: ' + testJobId);
debug('Query: ' + 'deviceId = \'' + testDeviceId + '\'');
jobClient.scheduleTwinUpdate(testJobId, 'deviceId = \'' + testDeviceId + '\'', testTwinPatch, new Date(Date.now()), 120, continueWith(() => {
waitForJobStatus(jobClient, testJobId, 'completed', continueWith(() => {
const registry = Registry.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
registry.getTwin(testDeviceId, continueWith((twin) => {
assert.equal(twin.tags.key, testTwinPatch.tags.key);
it('schedules a device method job with a sql query and succeeds', (testCallback) => {
const jobClient = JobClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
const testJobId = uuid.v4();
let methodResponseWasSent = false;
const deviceClient = DeviceClient.fromConnectionString(testDeviceConnectionString, DeviceMqtt);
deviceClient.open(continueWith(() => {
deviceClient.onDeviceMethod(testDeviceMethod.methodName, (request, response) => {
response.send(200, continueWith(() => {
methodResponseWasSent = true;
}));
});
debug('scheduling a device method job with a sql query with the id: ' + testJobId);
debug('Query: ' + 'deviceId = \'' + testDeviceId + '\'');
jobClient.scheduleDeviceMethod(testJobId, 'deviceId = \'' + testDeviceId + '\'', testDeviceMethod, new Date(Date.now()), 120, continueWith(() => {
waitForJobStatus(jobClient, testJobId, 'completed', continueWith(() => {
const registry = Registry.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
registry.getTwin(testDeviceId, continueWith(() => {
it('schedules a twin update job and cancels it', (testCallback) => {
const jobClient = JobClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
const testJobId = uuid.v4();
const fakePatch = {
etag: '*',
tags: {},
properties: {
desired: {},
reported: {}
}
};
jobClient.scheduleTwinUpdate(testJobId, 'deviceId = \'' + testDeviceId + '\'', fakePatch, new Date(Date.now() + 3600000), 120, continueWith((job) => {
assert.strictEqual(job.jobId, testJobId);
assert.strictEqual(job.status, 'queued');
debug('cancelling job ' + testJobId);
jobClient.cancelJob(testJobId, continueWith(() => {
waitForJobStatus(jobClient, testJobId, 'cancelled', continueWith(() => {
testCallback();
it('schedules a device method job and cancels it', (testCallback) => {
const jobClient = JobClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);
const testJobId = uuid.v4();
jobClient.scheduleDeviceMethod(testJobId, 'deviceId = \'' + testDeviceId + '\'', testDeviceMethod, new Date(Date.now() + 3600000), 120, continueWith((job) => {
assert.strictEqual(job.jobId, testJobId);
assert.strictEqual(job.status, 'queued');
debug('cancelling job ' + testJobId);
jobClient.cancelJob(testJobId, continueWith(() => {
waitForJobStatus(jobClient, testJobId, 'cancelled', continueWith(testCallback));
}));
}));
});
});