Azure IoT Hub DPS custom allocation policies by functions- 在DPS中启用设备自定义分配策略

视频介绍:

azure iot hub DPS 自定义策略

图文介绍:

本问参考官网文档:https://docs.microsoft.com/zh-cn/azure/iot-dps/how-to-use-custom-allocation-policies

在实际的业务中,可能存在某个DPS服务根据特定的策略将设备分配到不同的Azure IoT Hub 的情况,比如:

1. 大型项目中,将不同客户的设备分配到该客户独有的IoT Hub中;

2. 根据硬件版本号,将V1.1的版本号分配到中国东部2的IoT Hub,将V1.2的分配到中国北部的IoT Hub;

3. 根据硬件种类,将冰箱分配到冰箱专用的IoT Hub, 将空调分配到空调专用的IoT Hub;

4. 其他场景

这时候的这个分配规则,就是自定义规则,通过Azure functions实现,示意图如下:

本例中,

1. 创建了两个IoT Hub,一个是冰箱专用的,Hub名称包含 fridge字样,一个是空调专用的,hub名称包含conditioner字样;

2. 1个DPS服务,链接了上述两个IoT Hub, 配置了一个Function 自定义分配规则;

3. 1个function app,通过http触发,规则如上述图片所示;

4. 使用示例代码(C#)模拟设备进行测试,下载链接:https://codeload.github.com/Azure-Samples/azure-iot-samples-csharp/zip/master

重点步骤:

1.创建两个IoT Hub, 一个是冰箱专用的,Hub名称包含 fridge字样,一个是空调专用的,hub名称包含conditioner字样

2. 创建1个DPS服务

3.链接两个IoT Hub:

4. 准备Functions, Http 触发:

引用如下包:

<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Service" Version="1.5.0" />
<PackageReference Include="Microsoft.Azure.Devices.Shared" Version="1.16.0" />  

代码如下:

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.Shared;               // For TwinCollection
using Microsoft.Azure.Devices.Provisioning.Service; // For TwinStatenamespace Company.Function
{public static class HttpTriggerCSharp1{[FunctionName("HttpTriggerCSharp1")]public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,ILogger log){log.LogInformation("C# HTTP trigger function processed a request.");// Get request bodystring requestBody = await new StreamReader(req.Body).ReadToEndAsync();dynamic data = JsonConvert.DeserializeObject(requestBody);log.LogInformation("Request.Body:...");log.LogInformation(requestBody);// Get registration ID of the devicestring regId = data?.deviceRuntimeContext?.registrationId;string message = "Uncaught error";bool fail = false;ResponseObj obj = new ResponseObj();if (regId == null){message = "Registration ID not provided for the device.";log.LogInformation("Registration ID : NULL");fail = true;}else{string[] hubs = data?.linkedHubs.ToObject<string[]>();// Must have hubs selected on the enrollmentif (hubs == null){message = "No hub group defined for the enrollment.";log.LogInformation("linkedHubs : NULL");fail = true;}else{// This is a air-conditionerif (regId.Contains("-conditioner")){//Find the "conditioner" IoT hub configured on the enrollmentforeach(string hubString in hubs){if (hubString.Contains("-conditioner"))obj.iotHubHostName = hubString;}if (obj.iotHubHostName == null){message = "No conditioner hub found for the enrollment.";log.LogInformation(message);fail = true;}else{// Specify the initial tags for the device.TwinCollection tags = new TwinCollection();tags["deviceType"] = "conditioner";// Specify the initial desired properties for the device.TwinCollection properties = new TwinCollection();properties["state"] = "code";// Add the initial twin state to the response.TwinState twinState = new TwinState(tags, properties);obj.initialTwin = twinState;}}// This is fridgeelse if (regId.Contains("-fridge")){//Find the "-heatpumps-" IoT hub configured on the enrollmentforeach(string hubString in hubs){if (hubString.Contains("-fridge"))obj.iotHubHostName = hubString;}if (obj.iotHubHostName == null){message = "No fridge hub found for the enrollment.";log.LogInformation(message);fail = true;}else{// Specify the initial tags for the device.TwinCollection tags = new TwinCollection();tags["deviceType"] = "fridge";// Specify the initial desired properties for the device.TwinCollection properties = new TwinCollection();properties["state"] = "on";properties["temperatureSetting"] = "-18";// Add the initial twin state to the response.TwinState twinState = new TwinState(tags, properties);obj.initialTwin = twinState;}}// Unrecognized device.else{fail = true;message = "Unrecognized device registration.";log.LogInformation("Unknown device registration");}}}log.LogInformation("\nResponse");log.LogInformation((obj.iotHubHostName != null) ? JsonConvert.SerializeObject(obj) : message);return (fail)? new BadRequestObjectResult(message) : (ActionResult)new OkObjectResult(obj);}    }public class ResponseObj{public string iotHubHostName {get; set;}public TwinState initialTwin {get; set;}}
}

发布Function:

5. 在DPS中创建组注册,策略选择自定义function,IoT Hub 选择冰箱和空调两个

6. 运行设备示例代码:

修改idScope, primarykey,secondarykey, 可参见《DPS 组注册示例》

其中registrationId 也就是注册到iot hub后的设备ID,如果包含“fridge”则注册到冰箱iot hub,如果包含“conditioner” 则注册到空调iot hub,

依次修改为包含fridge和conditioner的字符串后运行程序:

7. 在iot hub中检查结果:

同时验证 device twin也由Function 写好:

Azure IoT Hub DPS custom allocation policies by functions- 在DPS中启用设备自定义分配策略相关推荐

  1. Azure IoT Hub 十分钟入门系列 (2)- 使用模拟设备发送设备到云(d2c)的消息

    本文主要分享一个案例: 10分钟- 使用Python 示例代码和SDK向IoT Hub 发送遥测消息 本文主要有如下内容: 了解C2D/D2C消息: 了解IoT Hub中Device的概念 了解并下载 ...

  2. Azure IoT Hub 十分钟入门系列 (1)- 10分钟带你了解Azure IoT Hub 并创建IoT Hub

    建议您先对<Azure 上 IoT 整体解决方案概览 >进行了解. 本文主要分享一个案例: 10分钟-了解Azure IoT Hub并创建Azure IoT Hub 本文主要有如下内容: ...

  3. Microsoft Azure IoT Hub应用 – 第一部分:向云端发送数据

    By Toradex Leonardo Graboski Veiga 1). 简介 物联网(Internet of Things)概念的本质其实就是关于发送数据到网络,所以称为云服务.随着时代发展和技 ...

  4. Azure IoT 中级(1)-Device Provisioning Service(DPS)概览

    视频讲解: 为什么需要DPS(Device Provisioning Service)及DPS的工作原理 您可以在B站观看视频或在本站观看: Azure IoT之 DPS 设备预配服务 实战:创建Io ...

  5. 【物联网云端对接-1】 通过HTTP协议与微软Azure IoT hub进行云端通信

    在2015年曾写过一篇文章<从微软build 2015.展望微软未来发展>,提到了微软的Azure和Windows 10 IoT,那算是初次接触微软物联网技术.比較幸运的是在兴许的时间里. ...

  6. Azure IoT Hub和Event Hub相关的技术系列-索引篇

    Azure IoT Hub和Event Hub相关的技术系列,最近已经整理了不少了,统一做一个索引链接,置顶. Azure IoT 技术研究系列1-入门篇 Azure IoT 技术研究系列2-设备注册 ...

  7. Azure IoT Hub入门 - 接口介绍

    Azure IoT Hub作为多租户服务,对不同的用户暴露了不同的接口(如下图所示). 下面对各类接口进行详细解释: 设备接口: Send device-to-cloud messages: 通过该接 ...

  8. 针对世纪互联Azure IoT Hub的一种设备下线通知方案

    本文介绍: 世纪互联Azure IoT Hub的一种获取设备下线通知方案 视频介绍: 您可在B站观看视频介绍:https://www.bilibili.com/video/BV1RK4y1b7Zp/ ...

  9. Microsoft正式发布Azure IoT Hub与Event Grid的集成

    在经历为期六个月的公开预览后,微软宣布正式发布IoT Hub与Azure Event Grid的集成.组合使用两者可以提高对客户设备事件的支持,实现数据库更新.工单(ticket)创建和定价管理等操作 ...

最新文章

  1. 对linux文件权限的理解,理解linux文件权限2
  2. C# 引用类型的对象克隆(深拷贝)。
  3. HDU-4527 小明系列故事——玩转十滴水 模拟
  4. leetcode242. 有效的字母异位词
  5. 拜托!程序员的工作不能用时间来衡量
  6. oracle pdb还原为no-cdb,oracle 12c中CDB和PDB的备份还原实验
  7. hexo -d 部署的时候报错 FATAL Something's wrong Template render error: expected variable
  8. UVa 642 - Word Amalgamation
  9. 记一次Springboot 启动错误(三) xxx.jar中没有主清单属性
  10. PDF 报告生成器:用 reportlab 和 pdfrw 生成自定义 PDF 报告
  11. 全球水储量分布图matlab代码,中国水能资源储量及分布特点分析(图)
  12. python json对比差异,更新json数据
  13. adb shell settings(系统服务:settings)
  14. 4g模块Linux拨号ppp脚本,在ARM-linux上实现4G模块PPP拨号上网【转】
  15. Ubuntu安装日常踩坑——Ubuntu安装过程中分区时出现空闲空间不可用的情况
  16. python如何打开npy文件_python实现npy格式文件转换为txt文件操作
  17. 基于链表和禁忌搜索启发式算法实现非一刀切二维矩形排样算法
  18. Nexus私服安装以及使用教程
  19. Goland中在文件模板中为go文件添加个人声明
  20. java基础-常用快捷键及基本dos命令

热门文章

  1. windows下C与C++执行cmd命令并实时获取输出
  2. 【调剂】3.16计算机考研其余调剂信息
  3. SIM7920G-M2介绍
  4. 我家云折腾之配置文件共享
  5. python模拟天猫商城
  6. 为什么 Mozilla 要固守 Gecko 内核
  7. android listview添加footview,有的手机可以正常显示,有的则不显示
  8. 《EDIUS 6.5快刀手高效剪辑技法》——2.3 EDIUS的菜单命令
  9. 学习笔记a——物联网安全
  10. ORDER BY 排序子句 | 零基础自学SQL课程系列Day6