Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
iotHubClient.open(function(err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else { // {"Name":"TurnFanOn","Parameters":""}
var data = JSON.stringify({ "Name":command,"Parameters":null });
var message = new Message (data);
console.log('Sending message: ' + data);
iotHubClient.send(deviceId, message, printResultFor('send'));
}
});
static toMessage(amqpMessage: AmqpMessage): Message {
/*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_001: [The `toMessage` method shall throw if the `amqpMessage` argument is falsy.]*/
if (!amqpMessage) {
throw new ReferenceError('amqpMessage cannot be \'' + amqpMessage + '\'');
}
/*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_009: [The `toMessage` method shall set the `Message.data` of the message to the content of the `AmqpMessage.body.content` property.]*/
let msg: Message = ( amqpMessage.body ) ? ( new Message(amqpMessage.body.content) ) : ( new Message(undefined) );
/*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_005: [The `toMessage` method shall set the `Message.to` property to the `AmqpMessage.to` value if it is present.]*/
if (amqpMessage.to) {
msg.to = amqpMessage.to;
}
/*Codes_SRS_NODE_IOTHUB_AMQPMSG_16_006: [The `toMessage` method shall set the `Message.expiryTimeUtc` property to the `AmqpMessage.absolute_expiry_time` value if it is present.]*/
if (amqpMessage.absolute_expiry_time) {
msg.expiryTimeUtc = amqpMessage.absolute_expiry_time;
}
//
// The rhea library will de-serialize an encoded uuid (0x98) as a 16 byte buffer.
// Since common messages should only have type string it is safe to decode
// these as strings.
//
it('can receive a C2D message', (testCallback) => {
let testMessage = new Message('testMessage');
testMessage.messageId = uuid.v4();
let sendOK = false;
let receiveOK = false;
deviceClient.on('error', (err) => {
debug('DEVICE CLIENT ERROR: ' + err.toString());
});
deviceClient.on('message', (msg) => {
debug('Device Client: Message Received');
if (msg.messageId === testMessage.messageId) {
debug('Device Client: Message OK');
deviceClient.complete(msg, (err) => {
if (err) throw err;
debug('Device Client: Message Completed');
deviceClient.close((err) => {
if (err) throw err;
debug('Device Client: Closed');
private _send(): void {
const id = uuid.v4();
let msg = new Message(id);
msg.messageId = id;
debug('sending message with id: ' + id);
timeout(this._client.sendEvent.bind(this._client), this._sendTimeout)(msg, (err) => {
if (err) {
debug('error sending message: ' + id + ': ' + err.message);
this.emit('error', err);
} else {
debug('sent message with id: ' + id);
this._timer = setTimeout(this._send.bind(this), this._sendInterval);
this.emit('sent', id);
}
});
}
}
serviceClient.open((err) => {
if (err) {
this.outputLine(Constants.IoTHubC2DMessageLabel, err.message);
} else {
let message = new Message(messageBody);
serviceClient.send(deviceId, message.getData(),
this.sendEventDone(serviceClient, Constants.IoTHubC2DMessageLabel, deviceId, Constants.IoTHubAIC2DMessageDoneEvent));
}
});
}
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
tweet = (info.statuses && info.statuses.length) ? `@${truncateByDot(info.statuses[0].user.name, 13)}:\n${info.statuses[0].text}` : "No new tweet.";
context.log(tweet);
const message = new Message(tweet);
cloudClient.send(deviceId, message, function (err, res) {
cloudClient.close();
if (err) {
completeContextWithError(context, `error in send C2D message: ${err}`);
} else {
context.log(`send status: ${res.constructor.name}`);
context.done();
}
});
}
else {
cloudClient.close();
completeContextWithError(context, `fail to call twitter API: ${error}`);
}
});
}
Client.prototype.send = function (deviceId, message, done) {
var _this = this;
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_013: [The send method shall throw ReferenceError if the deviceId or message arguments are falsy.]*/
if (!deviceId) {
throw new ReferenceError('deviceId is \'' + deviceId + '\'');
}
if (!message) {
throw new ReferenceError('message is \'' + message + '\'');
}
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_014: [The send method shall convert the message object to type azure-iot-common.Message if necessary.]*/
if (message.constructor.name !== 'Message') {
message = new azure_iot_common_1.Message(message);
}
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_015: [If the connection has not already been opened (e.g., by a call to open), the send method shall open the connection before attempting to send the message.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_016: [When the send method completes, the callback function (indicated by the done argument) shall be invoked with the following arguments:
err - standard JavaScript Error object (or subclass)
response - an implementation-specific response object returned by the underlying protocol, useful for logging and troubleshooting]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_017: [The argument err passed to the callback done shall be null if the protocol operation was successful.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_018: [Otherwise the argument err shall have a transport property containing implementation-specific response information for use in logging and troubleshooting.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_019: [If the deviceId has not been registered with the IoT Hub, send shall return an instance of DeviceNotFoundError.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_020: [If the queue which receives messages on behalf of the device is full, send shall return and instance of DeviceMaximumQueueDepthExceededError.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_023: [The `send` method shall use the retry policy defined either by default or by a call to `setRetryPolicy` if necessary to send the message.]*/
var retryOp = new azure_iot_common_2.RetryOperation(this._retryPolicy, MAX_RETRY_TIMEOUT);
retryOp.retry(function (retryCallback) {
_this._transport.send(deviceId, message, retryCallback);
}, function (err, result) {
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_030: [The `send` method shall not throw if the `done` callback is falsy.]*/
if (done) {
AmqpReceiver.prototype._onAmqpMessage = function (amqpMessage) {
/**
* @event module:azure-iot-amqp-base.AmqpReceiver#message
* @type {Message}
*/
var msg = new Message();
if (amqpMessage.properties.to) {
msg.to = amqpMessage.properties.to;
}
if (amqpMessage.properties.absoluteExpiryTime) {
msg.expiryTimeUtc = amqpMessage.properties.absoluteExpiryTime;
}
if (amqpMessage.properties.messageId) {
msg.messageId = amqpMessage.properties.messageId;
}
if (amqpMessage.body) {
msg.data = amqpMessage.body;
}
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
tweet = (info.statuses && info.statuses.length) ? `@${truncateByDot(info.statuses[0].user.name, 13)}:\n${info.statuses[0].text}` : "No new tweet.";
context.log(tweet);
const message = new Message(tweet);
cloudClient.send(deviceId, message, function (err, res) {
if (err) {
context.log(`Error in send C2D message: ${err}`);
} else {
context.log(`send status: ${res.constructor.name}`);
}
cloudClient.close();
});
}
else {
cloudClient.close();
}
});
}