Skip to main content

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 Hubmessage/events终结点,最多保留7天。
  • D2C消息最大大小是256KB, 所以可以发送批量消息,只要最大大小不超过256KB。
  • D2C消息只能根据deviceid进行分区。
  • 可以使用Message Enrichments的特性标记消息。

D2C消息包括如下的系统属性:

PropertyDescriptionUser Settable?Keyword for routing query
message-idA 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 + {'-', ':', '.', '+', '%', '_', '#', '*', '?', '!', '(', ')', ',', '=', '@', ';', '$', '''}.YesmessageId
iothub-enqueuedtimeDate and time the Device-to-Cloud message was received by IoT Hub.NoenqueuedTime
user-idAn ID used to specify the origin of messages. When messages are generated by IoT Hub, it is set to {iot hub name}.YesuserId
iothub-connection-device-idAn ID set by IoT Hub on device-to-cloud messages. It contains the deviceId of the device that sent the message.NoconnectionDeviceId
iothub-connection-module-idAn ID set by IoT Hub on device-to-cloud messages. It contains the moduleId of the device that sent the message.NoconnectionModuleId
iothub-connection-auth-generation-idAn 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.NoconnectionDeviceGenerationId
iothub-connection-auth-methodAn 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.NoconnectionAuthMethod
dt-dataschemaThis 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-subjectThe 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 nameEvent HubsAzure StorageService BusEvent Grid
Message IDmessage-idmessageIdMessageIdmessage-id
User iduser-iduserIdUserIduser-id
Connection device idiothub-connection-device-idconnectionDeviceIdiothub-connection-device-idiothub-connection-device-id
Connection module idiothub-connection-module-idconnectionModuleIdiothub-connection-module-idiothub-connection-module-id
Connection auth generation idiothub-connection-auth-generation-idconnectionDeviceGenerationIdiothub-connection-auth-generation-idiothub-connection-auth-generation-id
Connection auth methodiothub-connection-auth-methodconnectionAuthMethodiothub-connection-auth-methodiothub-connection-auth-method
contentTypecontent-typecontentTypeContentTypeiothub-content-type
contentEncodingcontent-encodingcontentEncodingContentEncodingiothub-content-encoding
iothub-enqueuedtimeiothub-enqueuedtimeenqueuedTimeN/Aiothub-enqueuedtime
CorrelationIdcorrelation-idcorrelationIdCorrelationIdcorrelation-id
dt-dataschemadt-dataschemadt-dataschemadt-dataschemadt-dataschema
dt-subjectdt-subjectdt-subjectdt-subjectdt-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,我们之后会通过教程来一一说明。