Code snippet for Direct Methods
This commit is contained in:
Родитель
523b9ab376
Коммит
4805730787
|
@ -23,7 +23,7 @@
|
|||
"\t\tconsole.log('Could not connect: ' + err);",
|
||||
"\t} else {",
|
||||
"\t\tconsole.log('Client connected');",
|
||||
"\t\tvar data = 'Hello IoT Hub!';",
|
||||
"\t\tvar data = '${1:Hello IoT Hub!}';",
|
||||
"\t\tvar message = new Message(data);",
|
||||
"\t\tconsole.log('Sending message: ' + message.getData());",
|
||||
"\t\tclient.sendEvent(message, printResultFor('send', client));",
|
||||
|
@ -79,13 +79,14 @@
|
|||
"var Client = require('azure-iothub').Client;",
|
||||
"var Message = require('azure-iot-common').Message;",
|
||||
"var connectionString = '<iotHubConnectionString>';",
|
||||
"var targetDevice = '<targetDeviceId>';",
|
||||
"var targetDevice = '${1:<targetDeviceId>}';",
|
||||
"var serviceClient = Client.fromConnectionString(connectionString);",
|
||||
"",
|
||||
"function printResultFor(op) {",
|
||||
"\treturn function printResult(err, res) {",
|
||||
"\t\tif (err) console.log(op + ' error: ' + err.toString());",
|
||||
"\t\tif (res) console.log(op + ' status: ' + res.constructor.name);",
|
||||
"\t\tserviceClient.close();",
|
||||
"\t};",
|
||||
"}",
|
||||
"",
|
||||
|
@ -102,7 +103,7 @@
|
|||
"\t} else {",
|
||||
"\t\tconsole.log('Service client connected');",
|
||||
"\t\tserviceClient.getFeedbackReceiver(receiveFeedback);",
|
||||
"\t\tvar message = new Message('Cloud to device message.');",
|
||||
"\t\tvar message = new Message('${2:Cloud to device message.}');",
|
||||
"\t\tmessage.ack = 'full';",
|
||||
"\t\tmessage.messageId = 'My Message ID ';",
|
||||
"\t\tconsole.log('Sending message: ' + message.getData());",
|
||||
|
@ -143,5 +144,67 @@
|
|||
"",
|
||||
"client.open(connectCallback);"
|
||||
]
|
||||
},
|
||||
"Call direct methods": {
|
||||
"prefix": "iotCallDirectMethods",
|
||||
"description": "Call direct methods",
|
||||
"body": [
|
||||
"'use strict';",
|
||||
"",
|
||||
"var Client = require('azure-iothub').Client;",
|
||||
"var connectionString = '<iotHubConnectionString>';",
|
||||
"",
|
||||
"var methodName = '${2:writeLine}';",
|
||||
"var deviceId = '${1:<targetDeviceId>}';",
|
||||
"var client = Client.fromConnectionString(connectionString);",
|
||||
"",
|
||||
"var methodParams = {",
|
||||
"\tmethodName: methodName,",
|
||||
"\tpayload: 'a line to be written',",
|
||||
"\ttimeoutInSeconds: 30",
|
||||
"};",
|
||||
"",
|
||||
"client.invokeDeviceMethod(deviceId, methodParams, function (err, result) {",
|
||||
"\tif (err) {",
|
||||
"\t\tconsole.error('Failed to invoke method \\'' + methodName + '\\': ' + err.message);",
|
||||
"\t} else {",
|
||||
"\t\tconsole.log(methodName + ' on ' + deviceId + ':');",
|
||||
"\t\tconsole.log(JSON.stringify(result, null, 2));",
|
||||
"\t}",
|
||||
"});"
|
||||
]
|
||||
},
|
||||
"Receive direct methods": {
|
||||
"prefix": "iotReceiveDirectMethods",
|
||||
"description": "Receive direct methods",
|
||||
"body": [
|
||||
"'use strict';",
|
||||
"",
|
||||
"var Mqtt = require('azure-iot-device-mqtt').Mqtt;",
|
||||
"var DeviceClient = require('azure-iot-device').Client;",
|
||||
"var connectionString = '<deviceConnectionString>';",
|
||||
"var client = DeviceClient.fromConnectionString(connectionString, Mqtt);",
|
||||
"",
|
||||
"function onWriteLine(request, response) {",
|
||||
"\tconsole.log(request.payload);",
|
||||
"",
|
||||
"\tresponse.send(200, 'Input was written to log.', function (err) {",
|
||||
"\t\tif (err) {",
|
||||
"\t\t\tconsole.error('An error ocurred when sending a method response:\\n' + err.toString());",
|
||||
"\t\t} else {",
|
||||
"\t\t\tconsole.log('Response to method \\'' + request.methodName + '\\' sent successfully.');",
|
||||
"\t\t}",
|
||||
"\t});",
|
||||
"}",
|
||||
"",
|
||||
"client.open(function (err) {",
|
||||
"\tif (err) {",
|
||||
"\t\tconsole.error('could not open IotHub client');",
|
||||
"\t} else {",
|
||||
"\t\tconsole.log('client opened');",
|
||||
"\t\tclient.onDeviceMethod('${1:writeLine}', onWriteLine);",
|
||||
"\t}",
|
||||
"});"
|
||||
]
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче