Azure IoT Hub消息指南
分类: Azure物联网 ◆ 标签: #Azure #IoT Hub # ◆ 发布于: 2023-06-12 21:46:00

Azure IoT Hub
允许通过消息进行双向通讯,关于消息的通讯,您可以参考我们之前的文章(D2C, C2D), 为了更好的适应各种协议,Azure IoT Hub
专门为消息指定了统一的格式,包括D2C和C2D都使用统一的消息格式。虽然消息的内容格式是一样的,但是D2C和C2D在和服务器之间的通讯方式由于选择协议不一样而不一样,D2C消息使用流模式,并且它的实现类似于Event Hub的时间模式。
Azure IoT Hub
消息主要包括如下几个部分:
- 预定义的系统属性
- 应用属性
- 二进制的消息体
属性名和值只能包括ASCII
字符串 + !
, #
, $
, %
, &
, '
, *
, +
, -
, .
, ^
, _
, '',
|,
~`。
D2C 消息有如下的特征
- D2C消息是持久的消息类型,默认会保存在
Azure IoT Hub
的message/events
终结点,最多保留7天。 - D2C消息最大大小是256KB, 所以可以发送批量消息,只要最大大小不超过256KB。
- D2C消息只能根据
deviceid
进行分区。 - 可以使用
Message Enrichments
的特性标记消息。
D2C消息包括如下的系统属性:
Property | Description | User Settable? | Keyword for routing query |
---|---|---|---|
message-id | A user-settable identifier for the message used for request-reply patterns. Format: A case-sensitive string (up to 128 characters long) of ASCII 7-bit alphanumeric characters + {'-', ':', '.', '+', '%', '_', '#', '*', '?', '!', '(', ')', ',', '=', '@', ';', '$', '''}. | Yes | messageId |
iothub-enqueuedtime | Date and time the Device-to-Cloud message was received by IoT Hub. | No | enqueuedTime |
user-id | An ID used to specify the origin of messages. When messages are generated by IoT Hub, it is set to {iot hub name}. | Yes | userId |
iothub-connection-device-id | An ID set by IoT Hub on device-to-cloud messages. It contains the deviceId of the device that sent the message. | No | connectionDeviceId |
iothub-connection-module-id | An ID set by IoT Hub on device-to-cloud messages. It contains the moduleId of the device that sent the message. | No | connectionModuleId |
iothub-connection-auth-generation-id | An ID set by IoT Hub on device-to-cloud messages. It contains the connectionDeviceGenerationId (as per Device identity properties) of the device that sent the message. | No | connectionDeviceGenerationId |
iothub-connection-auth-method | An authentication method set by IoT Hub on device-to-cloud messages. This property contains information about the authentication method used to authenticate the device sending the message. | No | connectionAuthMethod |
dt-dataschema | This value is set by IoT hub on device-to-cloud messages. It contains the device model ID set in the device connection. | No | $dt-dataschema |
dt-subject | The name of the component that is sending the device-to-cloud messages. | Yes | $dt-subject |
D2C 消息的应用属性
应用的属性一般都包括一个通用的属性即:发送消息的TimeStamp
, 一般使用属性名:iothub-creation-time-utc
实例如下:
{ "applicationId":"5782ed70-b703-4f13-bda3-1f5f0f5c678e", "messageSource":"telemetry", "deviceId":"sample-device-01", "schema":"default@v1", "templateId":"urn:modelDefinition:mkuyqxzgea:e14m1ukpn", "enqueuedTime":"2021-01-29T16:45:39.143Z", "telemetry":{ "temperature":8.341033560421833 }, "messageProperties":{ "iothub-creation-time-utc":"2021-01-29T16:45:39.021Z" }, "enrichments":{} }
C2D消息的系统属性
| Property | Description | User Settable? |
| message-id | A user-settable identifier for the message used for request-reply patterns. Format: A case-sensitive string (up to 128 characters long) of ASCII 7-bit alphanumeric characters + {'-', ':', '.', '+', '%', '_', '#', '*', '?', '!', '(', ')', ',', '=', '@', ';', '$', '''}. | Yes |
| sequence-number | A number (unique per device-queue) assigned by IoT Hub to each cloud-to-device message.| No |
| to | A destination specified in Cloud-to-Device messages. | No |
| absolute-expiry-time | Date and time of message expiration. | Yes |
| correlation-id | A string property in a response message that typically contains the MessageId of the request, in request-reply patterns. | Yes |
| user-id | An ID used to specify the origin of messages. When messages are generated by IoT Hub, it is set to {iot hub name}. | Yes |
| iothub-ack | A feedback message generator. This property is used in cloud-to-device messages to request IoT Hub to generate feedback messages as a result of the consumption of the message by the device. Possible values: none (default): no feedback message is generated, positive: receive a feedback message if the message was completed, negative: receive a feedback message if the message expired (or maximum delivery count was reached) without being completed by the device, or full: both positive and negative. | |
系统属性名称
系统属性名称基于消息被路由的endpoint, 不同的endpoint 系统属性名称会稍微有些不一致,详细的情况可以对比下表:
System property name | Event Hubs | Azure Storage | Service Bus | Event Grid |
---|---|---|---|---|
Message ID | message-id | messageId | MessageId | message-id |
User id | user-id | userId | UserId | user-id |
Connection device id | iothub-connection-device-id | connectionDeviceId | iothub-connection-device-id | iothub-connection-device-id |
Connection module id | iothub-connection-module-id | connectionModuleId | iothub-connection-module-id | iothub-connection-module-id |
Connection auth generation id | iothub-connection-auth-generation-id | connectionDeviceGenerationId | iothub-connection-auth-generation-id | iothub-connection-auth-generation-id |
Connection auth method | iothub-connection-auth-method | connectionAuthMethod | iothub-connection-auth-methodi | othub-connection-auth-method |
contentType | content-type | contentType | ContentType | iothub-content-type |
contentEncoding | content-encoding | contentEncoding | ContentEncoding | iothub-content-encoding |
iothub-enqueuedtime | iothub-enqueuedtime | enqueuedTime | N/A | iothub-enqueuedtime |
CorrelationId | correlation-id | correlationId | CorrelationId | correlation-id |
dt-dataschema | dt-dataschema | dt-dataschema | dt-dataschema | dt-dataschema |
dt-subject | dt-subject | dt-subject | dt-subject | dt-subject |
消息的大小
Azure IoT Hub
消息的大小和使用什么协议无关,消息的大小根据如下的规则来计算:
- 消息体的大小(in KB)
- 消息系统属性所有值的大小(不统计名称大小)
- 所有用户属性的名称和值的大小(in KB)
所有这些值加起来就是消息总共的大小。
其他属性
为了防止欺诈,IoT Hub
会标记所有的D2C消息三个属性:
iothub-connection-device-id
iothub-connection-auth-generation-id
iothub-connection-auth-method
iothub-connection-auth-method
属性包含一个系列化的对象,这个对象包含如下的属性:
{ "scope": "{ hub | device }", "type": "{ symkey | sas | x509 }", "issuer": "iothub" }
Message Enrichments
Message Enrichments
是指利用IoT Hub
根据用户的配置给D2C消息添加额外的信息,使得下一个处理消息的组件可以使用这些消息进行业务处理,例如如果下一个组件需要查询Device Twin
, 可以利用IoT hub
给消息添加一些从Device Twin
里自读读入的信息,并填充到消息里,下一个处理组件就无需再次查询Device Twin
。
需要注意的是:
- 每个hub最多只能添加10个
enrichments
- 更新了
device twin
, 一般需要5分钟才会起作用。 - 消息体的大小包括添加到消息里的
enrichments
信息。不能超过256KB。 如果消息超过了256KB, 那么Hub就会丢掉该消息,可以通过metrics
:d2c.telemetry.egress.invalid
来观察这种情况的发生。 message enrichments
不能应用于device twin
的数字值的变化。
使用Message Enrichments
的一般步骤
使用如下的步骤来应用Message Enrichments
:
- 创建一个自定义的终结点:例如event hub终结点,storage终结点等等。
- 创建消息自定义路由
- 创建
Message enrichments
, 给消息添加更多的信息。
Message Enrichments
主要元素:
Enrichment
名称或者Key
- 值
- 一个或者多个终结点,消息路由到这些终结点上都会应用本
Message Enrichments
Enrichment
值得说明:
- 可以包含任何静态字符串。
- 转发消息得IoT Hub名字,它得值是:
$iothubname
- 从孪生设备中读取得信息,目前仅仅支持如下几个值:
$twin.tags
,$twin.properties.desired
,$twin.properties.reported
关于如何应用Message Enrichment
,我们之后会通过教程来一一说明。