要使AWS IoT核心设备仅显示来自控制台的消息,你可以使用IoT设备SDK中的策略来限制设备的订阅权限。以下是一个示例代码,展示了如何使用AWS SDK for Python(Boto3)来创建设备的策略:
import boto3
iot = boto3.client('iot')
# 创建设备策略
policy_name = 'DevicePolicy'
policy_document = '''
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Receive",
"Resource": "*",
"Condition": {
"StringEquals": {
"iot:Connection.Thing.ThingName": "YOUR_DEVICE_THING_NAME"
}
}
},
{
"Effect": "Deny",
"Action": "iot:Receive",
"Resource": "*"
}
]
}
'''
response = iot.create_policy(
policyName=policy_name,
policyDocument=policy_document
)
# 创建设备
thing_name = 'YourDevice'
response = iot.create_thing(
thingName=thing_name
)
# 附加设备到策略
response = iot.attach_thing_principal(
thingName=thing_name,
principal='YOUR_DEVICE_CERTIFICATE_ARN'
)
在上面的代码中,你需要将YOUR_DEVICE_THING_NAME替换为你的设备的Thing名字,将YOUR_DEVICE_CERTIFICATE_ARN替换为你的设备的证书ARN。
这将创建一个设备策略,其中允许设备接收来自自己的连接,并拒绝来自其他资源的接收。这样,设备将只能接收来自控制台的消息。