使用具有 HTTP 网桥的网关时,您必须将设备绑定到该网关。开始之前,请参阅使用 HTTP 网桥,了解有关在 Cloud IoT Core 中使用 HTTP 网桥的一般信息。
如需将网关与 HTTP 网桥搭配使用,请执行以下操作:
通过网关发布设备的遥测事件
将设备绑定到网关后,请使用 publishEvent
方法代表设备将遥测事件发布到 Cloud IoT Core。二进制载荷数据必须采用 base64 编码。
为了提高安全性,您可以使用设备的 JWT(而不是网关的 JWT)发送消息。您提供的任何 JWT 都必须与请求的设备路径相匹配。
使用网关的 JWT:
curl -X POST -H 'authorization: Bearer GATEWAY_JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "gateway_info": {"delegated_device_id: "device-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{gateway-id}:publishEvent'
使用设备的 JWT:
curl -X POST -H 'authorization: Bearer DEVICE_JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "gateway_info": {"gateway_id: "gateway-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}:publishEvent'
遥测事件会转发到 Google Cloud Console 中指定的 Cloud Pub/Sub 主题,或者转发到设备注册表资源中的 eventNotificationConfigs[i].pubsubTopicName
字段。publishEvent
方法提供了一个可选的 subFolder
字段,用于对遥测事件进行分类。如需了解如何将数据从子文件夹发布到单独的 Pub/Sub 主题,请参阅使用 HTTP 网桥。
通过网关更新设备配置
使用 HTTP 网桥时,网关必须明确请求连接到它的设备的新配置。
如需获取 Cloud IoT Core 中当前可用的设备配置,请使用 getConfig
请求。您可以在授权不记名标头中添加来自网关或设备的 JWT。
使用网关的 JWT:
curl -X POST -H 'authorization: Bearer GATEWAY_JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "gateway_info": {"delegated_device_id: "device-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{gateway-id}/config?local_version=0'
使用设备的 JWT:
curl -X POST -H 'authorization: Bearer DEVICE_JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "gateway_info": {"gateway_id: "gateway-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}/config?local_version=0'
通过网关设置设备状态
使用 setState
请求向 Cloud IoT Core 报告设备状态。状态数据必须采用 base64 编码。您可以在授权不记名标头中添加来自网关或设备的 JWT。
使用网关的 JWT:
curl -X POST -H 'authorization: Bearer GATEWAY_JWT' -H 'content-type: application/json' --data '{"state": {"binary_data": "DATA"}, "gateway_info": {"delegated_device_id: "device-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{gateway-id}:setState'
使用设备的 JWT:
curl -X POST -H 'authorization: Bearer DEVICE_JWT' -H 'content-type: application/json' --data '{"state": {"binary_data": "DATA"}, "gateway_info": {"gateway_id: "gateway-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}:setState'
获取设备状态数据
如需检索设备状态数据,请在 Cloud Console 中查看设备详细信息或使用 API。如需了解详情,请参阅获取设备状态数据。