az iot device
Note
This reference is part of the azure-iot extension for the Azure CLI (version 2.59.0 or higher). 该扩展会在首次运行 az iot device 命令时自动安装。 Learn more about extensions.
利用设备模拟和其他以设备为中心的操作,例如设备到云或云到设备的消息传送功能。
命令
名称 | 说明 | 类型 | Status |
---|---|---|---|
az iot device c2d-message |
云到设备的消息传送命令。 |
Extension | GA |
az iot device c2d-message abandon |
放弃云到设备的消息。 |
Extension | GA |
az iot device c2d-message complete |
完成云到设备的消息。 |
Extension | GA |
az iot device c2d-message purge |
清除目标设备的云到设备消息队列。 |
Extension | GA |
az iot device c2d-message receive |
接收云到设备的消息。 |
Extension | GA |
az iot device c2d-message reject |
拒绝或终止云到设备的消息。 |
Extension | GA |
az iot device c2d-message send |
发送云到设备的消息。 |
Extension | GA |
az iot device registration |
管理 IoT 设备预配服务的 IoT 设备注册。 |
Extension | Preview |
az iot device registration create |
将 IoT 设备注册到 IoT 设备预配服务。 |
Extension | Preview |
az iot device send-d2c-message |
发送 mqtt 设备到云的消息。 |
Extension | GA |
az iot device simulate |
在Azure IoT 中心中模拟设备。 |
Extension | Experimental |
az iot device upload-file |
将本地文件作为设备上传到预配置的 Blob 存储容器。 |
Extension | GA |
az iot device send-d2c-message
发送 mqtt 设备到云的消息。
该命令支持以 unicode 字符串或二进制格式发送具有自定义有效负载的消息。 如果打算发送二进制文件,数据应来自文件(通过 --data-file-path
),内容类型应设置为 application/octet-stream
。
注意:该命令仅适用于基于对称密钥身份验证(SAS)的设备。 若要在消息路由中启用对消息正文的查询,内容类型系统属性必须是应用程序/JSON,内容编码系统属性必须是该系统属性支持的 UTF 编码值之一(UTF-8、UTF-16 或 UTF-32)。 如果在Azure 存储用作路由终结点时未设置内容编码,则IoT 中心以 base 64 编码格式写入消息。 如果使用 x509 身份验证方法,则必须提供证书和密钥文件(如果需要)密码。
az iot device send-d2c-message --device-id
[--certificate-file-path --cp]
[--da --data]
[--data-file-path --dfp]
[--dtmi --model-id]
[--hub-name]
[--key --symmetric-key]
[--key-file-path --kp]
[--login]
[--mc --msg-count]
[--pass --passphrase]
[--properties --props]
[--resource-group]
示例
Basic usage
az iot device send-d2c-message -n {iothub_name} -d {device_id}
注册“dtmi:com:example:Thermostat”模型 ID 的设备的基本用法;连接时为 1'
az iot device send-d2c-message -n {iothub_name} -d {device_id} --model-id 'dtmi:com:example:Thermostat;1'
使用 x509 身份验证的设备的基本用法
az iot device send-d2c-message -n {iothub_name} -d {device_id} --cp {certificate_file_path} --kp {key_file_path}
具有 x509 身份验证的设备的基本用法,其中密钥文件具有通行短语
az iot device send-d2c-message -n {iothub_name} -d {device_id} --cp {certificate_file_path} --kp {key_file_path} --pass {passphrase}
自定义数据的基本用法
az iot device send-d2c-message -n {iothub_name} -d {device_id} --data {message_body}
发送应用程序属性
az iot device send-d2c-message -n {iothub_name} -d {device_id} --props 'key0=value0;key1=value1'
发送系统属性(消息 ID 和相关 ID)
az iot device send-d2c-message -n {iothub_name} -d {device_id} --props '$.mid={id};$.cid={id}'
通过在系统属性中指定内容类型和内容编码来发送自定义数据
az iot device send-d2c-message -n {iothub_name} -d {device_id} --props '$.ct={content-type};$.ce={content-encoding}' --data {message_body}
通过在系统属性中指定内容编码,以二进制格式发送自定义数据
az iot device send-d2c-message -n {iothub_name} -d {device_id} --props '$.ct=application/octet-stream' --data-file-path {file_path}
通过在系统属性中指定内容类型和内容编码,以 JSON 格式发送自定义数据
az iot device send-d2c-message -n {iothub_name} -d {device_id} --props '$.ct=application/json;$.ce=utf-8' --data-file-path {file_path}
必需参数
目标设备 ID。
可选参数
The following parameters are optional, but depending on the context, one or more might become required for the command to execute successfully.
证书文件的路径。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
Message body. 提供文本或原始 json。
属性 | 值 |
---|---|
默认值: | Ping from Az CLI IoT Extension |
提供消息正文有效负载的文件路径。 请注意,当需要以二进制格式发送有效负载时,请将内容类型设置为 application/octet-stream。
设备在连接到中心时将报告的数字孪生模型 ID。 有关详细信息,请参阅 https://learn.microsoft.com/en-us/azure/iot-develop/overview-iot-plug-and-play。
属性 | 值 |
---|---|
Parameter group: | Digital Twin Arguments |
IoT 中心名称或主机名。 如果未提供 --login,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
要用于设备的对称密钥。 如果提供了对称密钥和其他设备身份验证参数,则对称密钥优先。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
密钥文件的路径。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
此命令支持具有执行操作权限的实体连接字符串。 用于避免通过“az login”进行会话登录。 如果同时提供实体连接字符串和名称,则连接字符串优先。 如果未提供 --hub-name,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
要发送到 IoT 中心的设备消息数。
属性 | 值 |
---|---|
默认值: | 1 |
密钥文件的通行短语。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
键值对中的消息属性包采用以下格式:a=b;c=d。 对于 mqtt 消息传送 - 可以使用 $发送系统属性。{name}=value。 例如 $.cid=12345 设置系统关联 ID 属性。 其他系统属性标识符示例包括内容类型的 $.ct、消息 ID $.mid和内容编码的 $.ce。
资源组的名称。 可以使用 az configure --defaults group=<name>
配置默认组。
全局参数
提高日志记录详细程度以显示所有调试日志。
属性 | 值 |
---|---|
默认值: | False |
显示此帮助消息并退出。
仅显示错误,禁止显示警告。
属性 | 值 |
---|---|
默认值: | False |
Output format.
属性 | 值 |
---|---|
默认值: | json |
接受的值: | json, jsonc, none, table, tsv, yaml, yamlc |
JMESPath 查询字符串。 有关更多信息和示例,请参阅 http://jmespath.org/。
订阅的名称或 ID。 可以使用 az account set -s NAME_OR_ID
配置默认订阅。
增加日志记录详细程度。 使用 --debug 获取完整的调试日志。
属性 | 值 |
---|---|
默认值: | False |
az iot device simulate
此命令是实验性的,正在开发中。 参考和支持级别:https://aka.ms/CLI_refstatus
在Azure IoT 中心中模拟设备。
设备模拟运行时,设备将自动接收并确认云到设备(c2d)消息。 对于 mqtt 模拟,所有 c2d 消息都将在完成时得到确认。 对于 http 模拟 c2d 确认,基于可完成、拒绝或放弃的用户选择。 mqtt 模拟还支持直接方法调用,可以通过响应状态代码和响应有效负载进行确认。 注意:默认情况下,该命令会将内容类型设置为 application/json,并将内容编码设置为 utf-8。 这可以重写。 注意:如果使用 x509 身份验证方法,则必须提供证书和密钥文件(如果需要)密码。
az iot device simulate --device-id
[--certificate-file-path --cp]
[--da --data]
[--dtmi --model-id]
[--hub-name]
[--init-reported-properties --irp]
[--key --symmetric-key]
[--key-file-path --kp]
[--login]
[--mc --msg-count]
[--method-response-code --mrc]
[--method-response-payload --mrp]
[--mi --msg-interval]
[--pass --passphrase]
[--properties --props]
[--proto --protocol {http, mqtt}]
[--receive-settle --rs {abandon, complete, reject}]
[--resource-group]
示例
基本用法(mqtt)
az iot device simulate -n {iothub_name} -d {device_id}
注册“dtmi:com:example:Thermostat”模型 ID 的设备的基本用法;连接时为 1' (mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --model-id 'dtmi:com:example:Thermostat;1'
使用 x509 身份验证的设备的基本用法(mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --cp {certificate_file_path} --kp {key_file_path}
具有 x509 身份验证(mqtt)的设备的基本用法,其中密钥文件具有通行短语
az iot device simulate -n {iothub_name} -d {device_id} --cp {certificate_file_path} --kp {key_file_path} --pass {passphrase}
发送混合属性 (mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --properties "myprop=myvalue;$.ct=application/json"
将直接方法响应状态代码和直接方法响应有效负载作为原始 json 发送(仅 mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --method-response-code 201 --method-response-payload '{"result":"Direct method successful"}'
将直接方法响应状态代码和直接方法响应有效负载作为本地文件的路径(仅 mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --method-response-code 201 --method-response-payload '../my_direct_method_payload.json'
将设备孪生报告属性的初始状态作为目标设备的原始 json 发送(仅 mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --init-reported-properties '{"reported_prop_1":"val_1", "reported_prop_2":val_2}'
将设备孪生报告属性的初始状态作为目标设备的本地文件的路径(仅 mqtt)
az iot device simulate -n {iothub_name} -d {device_id} --init-reported-properties '../my_device_twin_reported_properties.json'
基本用法(http)
az iot device simulate -n {iothub_name} -d {device_id} --protocol http
发送混合属性 (http)
az iot device simulate -n {iothub_name} -d {device_id} --protocol http --properties "iothub-app-myprop=myvalue;content-type=application/json;iothub-correlationid=12345"
选择消息之间的总消息计数和间隔
az iot device simulate -n {iothub_name} -d {device_id} --msg-count 1000 --msg-interval 5
拒绝 c2d 消息(仅限 http)
az iot device simulate -n {iothub_name} -d {device_id} --rs reject --protocol http
放弃 c2d 消息(仅限 http)
az iot device simulate -n {iothub_name} -d {device_id} --rs abandon --protocol http
必需参数
目标设备 ID。
可选参数
The following parameters are optional, but depending on the context, one or more might become required for the command to execute successfully.
证书文件的路径。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
Message body. 提供文本或原始 json。
属性 | 值 |
---|---|
默认值: | Ping from Az CLI IoT Extension |
设备在连接到中心时将报告的数字孪生模型 ID。 有关详细信息,请参阅 https://learn.microsoft.com/en-us/azure/iot-develop/overview-iot-plug-and-play。
属性 | 值 |
---|---|
Parameter group: | Digital Twin Arguments |
IoT 中心名称或主机名。 如果未提供 --login,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
在运行模拟器时,目标设备的孪生报告属性的初始状态。 可选参数,仅支持 mqtt。
要用于设备的对称密钥。 如果提供了对称密钥和其他设备身份验证参数,则对称密钥优先。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
密钥文件的路径。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
此命令支持具有执行操作权限的实体连接字符串。 用于避免通过“az login”进行会话登录。 如果同时提供实体连接字符串和名称,则连接字符串优先。 如果未提供 --hub-name,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
要发送到 IoT 中心的设备消息数。
属性 | 值 |
---|---|
默认值: | 100 |
在设备上执行直接方法时要返回的状态代码。 可选参数,仅支持 mqtt。
在设备上执行直接方法时要返回的有效负载。 提供文件路径或原始 json。 可选参数,仅支持 mqtt。
设备到云消息之间的延迟(以秒为单位)。
属性 | 值 |
---|---|
默认值: | 3 |
密钥文件的通行短语。
属性 | 值 |
---|---|
Parameter group: | Device Authentication Arguments |
键值对中的消息属性包采用以下格式:a=b;c=d。 对于 mqtt 消息传送 - 可以使用 $发送系统属性。{name}=value。 例如 $.cid=12345 设置系统关联 ID 属性。 其他系统属性标识符示例包括内容类型的 $.ct、消息 ID $.mid和内容编码的 $.ce。 对于 http 消息传送 - 使用 iothub-app-{name}=value 发送应用程序属性,例如 iothub-app-myprop=myvalue。 系统属性通常以 iothub-{name} 作为前缀,如 iothub-correlationid,但存在内容类型和内容编码等异常。
指示设备到云的消息协议。
属性 | 值 |
---|---|
默认值: | mqtt |
接受的值: | http, mqtt |
指示如何解决收到的云到设备的消息。 仅支持 HTTP。
属性 | 值 |
---|---|
默认值: | complete |
接受的值: | abandon, complete, reject |
资源组的名称。 可以使用 az configure --defaults group=<name>
配置默认组。
全局参数
提高日志记录详细程度以显示所有调试日志。
属性 | 值 |
---|---|
默认值: | False |
显示此帮助消息并退出。
仅显示错误,禁止显示警告。
属性 | 值 |
---|---|
默认值: | False |
Output format.
属性 | 值 |
---|---|
默认值: | json |
接受的值: | json, jsonc, none, table, tsv, yaml, yamlc |
JMESPath 查询字符串。 有关更多信息和示例,请参阅 http://jmespath.org/。
订阅的名称或 ID。 可以使用 az account set -s NAME_OR_ID
配置默认订阅。
增加日志记录详细程度。 使用 --debug 获取完整的调试日志。
属性 | 值 |
---|---|
默认值: | False |
az iot device upload-file
将本地文件作为设备上传到预配置的 Blob 存储容器。
az iot device upload-file --content-type --ct
--device-id
--file-path --fp
[--hub-name]
[--login]
[--resource-group]
必需参数
MIME 文件类型。
目标设备 ID。
要上传的文件的路径。
可选参数
The following parameters are optional, but depending on the context, one or more might become required for the command to execute successfully.
IoT 中心名称或主机名。 如果未提供 --login,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
此命令支持具有执行操作权限的实体连接字符串。 用于避免通过“az login”进行会话登录。 如果同时提供实体连接字符串和名称,则连接字符串优先。 如果未提供 --hub-name,则为必需。
属性 | 值 |
---|---|
Parameter group: | IoT Hub Identifier Arguments |
资源组的名称。 可以使用 az configure --defaults group=<name>
配置默认组。
全局参数
提高日志记录详细程度以显示所有调试日志。
属性 | 值 |
---|---|
默认值: | False |
显示此帮助消息并退出。
仅显示错误,禁止显示警告。
属性 | 值 |
---|---|
默认值: | False |
Output format.
属性 | 值 |
---|---|
默认值: | json |
接受的值: | json, jsonc, none, table, tsv, yaml, yamlc |
JMESPath 查询字符串。 有关更多信息和示例,请参阅 http://jmespath.org/。
订阅的名称或 ID。 可以使用 az account set -s NAME_OR_ID
配置默认订阅。
增加日志记录详细程度。 使用 --debug 获取完整的调试日志。
属性 | 值 |
---|---|
默认值: | False |