低代码开发物联网app(4.1)-将Azure IoT Service SDK 集成到 Azure Function并发布

131 篇文章 10 订阅
121 篇文章 5 订阅

本文介绍:

将IoT Hub Service SDK 调用Direct Method 封装为Azure Functions Http Trigger(C#)

 

 

视频介绍:

https://www.51azure.cloud/post/2020/6/15/azure-iot-hub-14-iot-hub-service-sdk-direct-method-azure-functions-http-trigger

图文介绍:

本文内容比较简单,不做图文介绍了,直接创建functions,采用http 触发,添加如下代码,发布后即可通过调用http请求的方式,向iot hub 发送 direct method 调用。

注意:

1.引用包:using Microsoft.Azure.Devices;

2.本例调用的客户端案例为\azure-iot-samples-node-master\iot-hub\Quickstarts\simulated-device-2

3. 其中iot hub 连接字符串写入functions 配置文件,deviceid 和payload 通过query string 传递

例如,http://localhost:7071/api/CallDierctMethod?deviceId=device-0001&payload=10

表示将名称为device-0001的设备的上报数据频率改为10秒

 

 

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Devices;

namespace Company.Function
{
    public static class CallDierctMethod
    {

        private static ServiceClient s_serviceClient;


        // The function will called like :
        // http://localhost:7071/api/CallDierctMethod?deviceId=device-0001&payload=1

        [FunctionName("CallDierctMethod")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");         

          

            string deviceId = req.Query["deviceId"];

            string payload = req.Query["payload"];

             log.LogInformation($"parameter deviceId:{deviceId},payload:{payload}");

            string s_connectionString = System.Environment.GetEnvironmentVariable("IoTHubConnectionString");
                  
            s_serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString);
            var cloudToDeviceMethodResult= InvokeMethod(deviceId,payload).GetAwaiter().GetResult();
      
             log.LogInformation($"Functions Result:{cloudToDeviceMethodResult.GetPayloadAsJson()}");

            return new OkObjectResult(cloudToDeviceMethodResult);
        }



          private static  async Task<CloudToDeviceMethodResult> InvokeMethod(string deviceId,string  payload)
        {
            var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval") { ResponseTimeout = TimeSpan.FromSeconds(30) };
            methodInvocation.SetPayloadJson(payload);

            // Invoke the direct method asynchronously and get the response from the simulated device.
            var response =  await s_serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation);

            return response;

           
        }
    }
}

 

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "IoTHubConnectionString":"{your iot hub connection string}"
  }
}


 


声明:

 

点击可查阅本站文章目录 《文章分类目录》

本站所有内容仅代表个人观点,如与官文档冲突,请以官方文档为准。

可在本页面下方留言或通过下方联系方式联系我:

微信:wxyusz;邮箱:shuzhen.yu@foxmail.com

欢迎关注公众号“云计算实战”,接收最新文章推送。

知识共享许可协议

本作品由Sean Yu 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章链接:https://www.51azure.cloud,且不得用于商业目的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值