Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var sendInterval = setInterval(function () {
var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
var temperature = 20 + (Math.random() * 10); // range: [20, 30]
var humidity = 60 + (Math.random() * 20); // range: [60, 80]
var data = JSON.stringify({ deviceId: 'myFirstDevice', windSpeed: windSpeed, temperature: temperature, humidity: humidity });
var message = new Message(data);
message.properties.add('temperatureAlert', (temperature > 28) ? 'true' : 'false');
console.log('Sending message: ' + message.getData());
client.sendEvent(message, printResultFor('send'));
}, 2000);
setInterval(function(){
// Simulate telemetry.
var temperature = 20 + (Math.random() * 15);
var message = new Message(JSON.stringify({
temperature: temperature,
humidity: 60 + (Math.random() * 20)
}));
// Add a custom application property to the message.
// An IoT hub can filter on these properties without access to the message body.
message.properties.add('temperatureAlert', (temperature > 30) ? 'true' : 'false');
console.log('Sending message: ' + message.getData());
// Send the message.
client.sendEvent(message, function (err) {
if (err) {
console.error('send error: ' + err.toString());
} else {
console.log('message sent');
} else {
throw new StatusError('Invalid format: a device specification must be provided.', 400);
}
if (!validateMeasurements(measurements)) {
throw new StatusError('Invalid format: invalid measurement list.', 400);
}
if (timestamp && isNaN(Date.parse(timestamp))) {
throw new StatusError('Invalid format: if present, timestamp must be in ISO format (e.g., YYYY-MM-DDTHH:mm:ss.sssZ)', 400);
}
const client = Device.Client.fromConnectionString(await getDeviceConnectionString(context, device), DeviceTransport.Http);
try {
const message = new Device.Message(JSON.stringify(measurements));
if (timestamp) {
message.properties.add('iothub-creation-time-utc', timestamp);
}
await util.promisify(client.open.bind(client))();
context.log('[HTTP] Sending telemetry for device', device.deviceId);
await util.promisify(client.sendEvent.bind(client))(message);
await util.promisify(client.close.bind(client))();
} catch (e) {
// If the device was deleted, we remove its cached connection string
if (e.name === 'DeviceNotFoundError' && deviceCache[device.deviceId]) {
delete deviceCache[device.deviceId].connectionString;
}
throw new Error(`Unable to send telemetry for device ${device.deviceId}: ${e.message}`);
function sendMessage() {
if (!sendingMessage) { return; }
var tempature = HTS221.getTempature();
var humidity = HTS221.getHumidity();
var messageObject = JSON.stringify({
messageId: messageId,
deviceId: 'Devkit playground',
temperature: tempature,
humidity: humidity
});
var message = new Message(messageObject);
message.properties.add('temperatureAlert', (tempature > 30).toString());
console.log("Sending message: " + messageObject);
client.sendEvent(message, async function (err) {
if (err) {
console.error('Failed to send message to Azure IoT Hub');
} else {
await blinkSendConfirmation();
console.log('Message sent to Azure IoT Hub');
}
});
console.log("IoTHubClient accepted the message for delivery");
}
function onStart(request, response) {
const generateImportantMessage = messageId => new Message(
JSON.stringify({
messageId : messageId,
data : 'beep boop bopity bop',
})
);
return new Promise((resolve, reject) => {
let device = require('azure-iot-device');
let message = new device.Message(content);
client.sendEvent(message, (err, res) => {
if (err) reject(err);
resolve(res);
});
});
}
.then(result => {
fs.unlinkSync('picture.png'); //delete the picture
//sending message to iot hub
log('sending message to iot hub...');
let message = new device.Message(JSON.stringify({ deviceId: 'device1', tags: ['foo', 'baz', 'bar'] }));
hubClient.sendEvent(message, (err, res) => {
if (err) log(err.message);
else {
log(`Sent ${JSON.stringify(result.tags)} to your IoT Hub`);
log('READY');
}
led.stop().off();
});
})
.catch(err => {
private async sendD2CMessageCore(
client: Client,
message: any,
status: SendStatus,
totalStatus: SendStatus,
) {
let stringify = Utility.getConfig(
Constants.IoTHubD2CMessageStringifyKey,
);
await client.sendEvent(
new Message(stringify ? JSON.stringify(message) : message),
this.sendEventDoneCallback(
client,
Constants.IoTHubAIMessageDoneEvent,
status,
totalStatus,
),
);
}
data.forEach(function (value) {
messages.push(new Message(JSON.stringify(value)));
});