Sean Yu

IoT Specialty/Microsoft MVP/MCT

Azure IoT Hub入门系列 (13)- 实现Azure Function 通过IoT Hub Trigger将遥测消息写入SQL数据库(C#)

China Standard Time
China Standard Time - ... Reads

Azure IoT Hub入门系列 (13)- 实现Azure Function 通过IoT Hub Trigger将遥测消息写入SQL数据库(C#)

 

本文介绍:

本文介绍如下:

1.C# 语言和VS Code 开发Azure Functions的准备工作;

   安装.net core sdk;

   安装配置 vs code 和Azue Functions Core Tools;

   在portal中准备SQL数据库;

2.设备发送遥测消息到 IoT Hub后使用Azure Function 的IoT Hub Trigger 处理遥测消息;

    新建C#  iot hub trigger functons;

3. 使用VS Code Nuget package manager 添加数据库访问用的package(dll引用);

    安装nuget package manager 扩展;

    安装数据库需要用的dll;

    修改源代码,添加数据库保存的相关内容;

4. 使用VS Code 发布 Azure functions并配置参数;

5. 观察云端Functions运行状态;

 

 

视频介绍:

 

图文介绍:

 

 

 

 

1.C# 语言和VS Code 开发Azure Functions的准备工作;

.Net Core SDK 安装:

下载地址:https://dotnet.microsoft.com/download

双击下一步下一步进行安装:

 

安装vs code 及扩展:

下载安装Visual Studio Code:https://code.visualstudio.com/

下载相关的扩展:

设置Azure 扩展为中国区Azure(如果使用世纪互联版本,则需要本步骤):

登录Azure 账号:

 

 

 

 在本地手动安装 Azure Functions Core Tools:

安装过程可能需要使用VPN。

安装3.0版本Azure Functions Core Tools:https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#install-the-azure-functions-core-tools

npm install -g azure-functions-core-tools@3

 

如果上述npm 安装过程太慢,则可以使用如下方式,下载安装最新的版本即可:

https://github.com/Azure/azure-functions-core-tools/releases

或者使用本站加速下载地址:https://videos.51azure.cloud/files/func-cli-3.0.2534-x64.msi

本地Function 运行过程中遇到 func.ps1 cannot be loaded because running scripts is disabled on this system.

参照官网:https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code?pivots=programming-language-java#configure-your-environment

 

如果遇到错误func.ps1 cannot be loaded because running scripts is disabled on this system.

在power shell中运行如下命令:

Set-ExecutionPolicy unrestricted

 

创建SQL 数据库:

 

创建过程中,如果没有服务器,则新建服务器:

在本例中,我们将数据库调整为较低规格的SKU,将计算+存储 调整为”基本“,如下图所示:

 

 

设置防火墙规则:

本例中需要设置:

1.允许azure 服务访问;2. 添加客户端IP已实现本地PC 访问;

使用SSMS 连接到数据库,通过如下脚本新建表:

注意,如下脚本仅作为演示,未进行索引等优化。

CREATE TABLE [dbo].[telemetry](
 [temperature] [numeric](18, 2) NULL,
 [humidity] [numeric](18, 2) NULL,
 [deviceid] [nvarchar](50) NULL,
 [funcsavedt] [datetime] NULL
) ON [PRIMARY]
GO

 

 

2.设备发送遥测消息到 IoT Hub后使用Azure Function 的IoT Hub Trigger 处理遥测消息;

CTRL+SHIFT+P :

选择一个文件夹:

选择开发语言:

选择IoT Hub Trigger 模板:

 

注意,此处有几个步骤没有截图,保持默认即可。

 

创建本地配置文件:

跳过Event Hub Namespage:

Endpoint 保持不变:

选择已有的Storage 或新建 storage,该Storage是Azure Functions环境必须的参数;

 

执行结束,可看到如下页面:

 

需要修改Connection 的名称和local.settings.json 文件:

3. 使用VS Code Nuget package manager 添加数据库访问用的package(dll引用);

 

安装nuget package manager;

安装C#访问数据库用的dll(System.Data.SqlClient)

输入 System.Data.SqlClient

 

注意,以上步骤,可能需要访问外网,否则可能出现,列举出 包名后,无法列举具体版本的情况:

同时VS Code右下角会提示如下:

 

此时,可以修改项目文件csproj,手动添加上需要引用的package 及其版本号。

保存后运行项目,VS Code会提示你需要安装包,点击右下角的Restore 按钮,VS Code会自动安装这个包。

修改代码,将遥测消息接入数据库;

修改后的完整源代码如下:

using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;

using System.Data;
using System.Data.SqlClient;
using System;
using Newtonsoft.Json;


namespace Company.Function
{
    public static class iothubtriggertodb
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("iothubtriggertodb")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "IotHubEventHubString")]EventData message, ILogger log)
        {
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");

            var deviceid=message.SystemProperties["iothub-connection-device-id"].ToString();

            
            var dbstring=System.Environment.GetEnvironmentVariable("SQLConn");
            

            try
            {
                 Telemetry tmsg = JsonConvert.DeserializeObject<Telemetry>(Encoding.UTF8.GetString(message.Body.Array));

                
                using (SqlConnection con = new SqlConnection(dbstring))
                {
                    con.Open();
                    if (con.State == ConnectionState.Open)
                    {
                        string strCmd = $"insert into dbo.Telemetry(temperature,humidity,funcsavedt,deviceid) values ({tmsg.temperature},{tmsg.humidity},'{System.DateTime.Now}','{deviceid}' )";


                        SqlCommand sqlcmd = new SqlCommand(strCmd, con);
                        int   n = sqlcmd.ExecuteNonQuery();
                        if (n > 0)
                        {
                            log.LogInformation("save to db successfully");
                        }
                        else
                        {
                            log.LogError("save to db error");
                        }
                      
                }  
               con.Close();
               }
            }
            catch (Exception ex)
            {
               log.LogInformation(ex.Message);
            }


        }
    }


      public class Telemetry
        {
            public string deviceid{get;set;}

            public DateTime funcsavedt{get;set; }
            public double temperature { get; set; }

            public double humidity { get; set; }
        }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "your account storage connection string",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "IotHubEventHubString":"your iot hub built-in endpoint",
    "SQLConn":"Data Source =XXX.database.windows.net;Initial Catalog =XXXDB;User Id = XXX;Password =XXX;"
  }
}

4. 使用VS Code 发布 Azure functions并配置参数;

发布Functions: ctrl+shift+p

 

在云中的Functions 添加IoT Hub的内置Event Hub终结点的配置文件:

具体配置 需与local.settings.json 保持一致:

IoTHub内置终结点配置:

 

数据库配置:

 

5. 观察云端Functions运行状态;

可以在Functions监视页面观察日志或者可以直接访问到数据库中,查询数据保存结果

请参照文档《10分钟实现Azure Function 通过IoT Hub Trigger处理设备到云的消息(C#)

 

 

 


Comments 0