Azure IoT Hub 十分钟入门系列 (10)- 实现云端接收设备文件上传通知(file upload notification,Node.js示例)

本文主要分享一个案例:

通过Service SDK获取文件上传通知;

本文的前提是《Azure IoT Hub 十分钟入门系列 (4)- 实现从设备上传日志文件/图片到 Azure Storage(Node.js示例)》。

 

本文主要有如下内容:

1. 在IoT Hub中打开文件上传通知

2. 使用Node.js Service SDK 接受文件上传通知

 

视频介绍:https://www.51azure.cloud/post/2020/6/7/azure-iot-hub-10-file-upload-notification-nodejs

 

 

图文介绍:

1. 在IoT Hub中打开文件上传通知:

2. 使用如下示例代码:

3.安装SDK:

npm init

回车->回车确认,直到出现如下的package.json界面:

 

执行

npm install azure-iothub --save

4.修改Service 侧代码中的连接字符串:

修改后的Service 侧代码如下:

'use strict';
​
var Client = require('azure-iothub').Client;
​
//var connectionString = process.env.IOTHUB_CONNECTION_STRING;
​
var connectionString = "your iot hub connection string";
if (!connectionString) {
  console.log('Please set the IOTHUB_CONNECTION_STRING environment variable.');
  process.exit(-1);
}
​
var client = Client.fromConnectionString(connectionString);
​
client.open(function (err) {
  if (err) {
    console.error('Could not connect: ' + err.message);
    process.exit(-1);
  } else {
    console.log('Client connected');
​
    client.getFileNotificationReceiver(function(err, receiver) {
      if(err) {
        console.error('Could not get file notification receiver: ' + err.message);
        process.exit(-1);
      } else {
        receiver.on('message', function(msg) {
          console.log('File uploaded: ');
          console.log(msg.data.toString());
          receiver.complete(msg, function(err) {
            if (err) {
              console.error('Could not complete the message: ' + err.message);
              process.exit(-1);
            } else {
              console.log('Message completed');
              process.exit(0);
            }
          });
        });
      }
    });
  }
});

5.运行Service 侧代码:

node receive_file_notifications.js

6.运行Device 侧代码,上传文件,从Service 侧接收文件上传通知:

注意,device侧代码运行参照文档《Azure IoT Hub 十分钟入门系列 (4)- 实现从设备上传日志文件/图片到 Azure Storage(Node.js示例)》

执行:

node upload_to_blob.js

Service 侧立刻收到通知:

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章