我尝试使用 HomeGraphService api 为我的 google 操作实现 StateReporting。
为此,我遵循了谷歌state-report文档。
问题是reportStateAndNotification函数似乎什么也没做。 它仅返回 com.google.api.services.homegraph.v1.HomeGraphService$Devices$ReportStateAndNotification 的空列表。 HomeGraphViewer 中的状态也不会改变。
当我使用来自credentials.accessToken的访问令牌和curl时,一切正常。 任何帮助将不胜感激。
val stream = Base64.getDecoder().decode(homeGraphServiceKey)
val credentials = GoogleCredentials.fromStream(stream.inputStream())
.createScoped("https://www.googleapis.com/auth/homegraph")
credentials.refreshIfExpired()
val token = credentials.accessToken
val service = HomeGraphService.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
GsonFactory.getDefaultInstance(),
HttpCredentialsAdapter(credentials)
)
.setApplicationName("ff")
.build()
val states = mutableMapOf<String, MutableMap<String, Any>>()
val state = mutableMapOf<String, Any>()
state["on"] = true
states["hue:cE1"] = state
val request =
ReportStateAndNotificationRequest().setRequestId(
kotlin.random.Random.nextLong().toString()
)
.setAgentUserId("7b1d52e3-e68f-46cd-a8b9-6ce1fdfe5b62").setPayload(
StateAndNotificationPayload().setDevices(
ReportStateAndNotificationDevice().setStates(
states.toMap()
)
)
)
val response = service?.devices()?.reportStateAndNotification(request)
卷曲:
{
"requestId": "123ABC",
"agentUserId": "7b1d52e3-e68f-46cd-a8b9-6ce1fdfe5b62",
"payload": {
"devices": {
"states": {
"hue:cE1": {
"on": true
}
}
}
}
}
curl -X POST -H "Authorization: Bearer token” -H "Content-Type: application/json" -d @request "https://homegraph.googleapis.com/v1/devices:reportStateAndNotification"
感谢您提出这个问题。当您实施报告状态时,每次设备状态发生变化时您都会向我们发送数据。如果您发现发送给我们的设备状态与Homegraph查看器工具中的设备状态有任何差异,您可以打开一份公共问题跟踪票,我们可以查看。 我们强烈建议您参加我们的智能家居代码实验室,获得有关如何实施报告状态、请求同步等的实践经验。