gpt4 book ai didi

java - 无法为 SyncResponse Payload 创 build 备对象

转载 作者:行者123 更新时间:2023-12-02 04:39:01 27 4
gpt4 key购买 nike

据我了解,SyncResponse 的有效负载是一个 Device 类型的数组。

我的问题是我无法创建在 SyncResponse - Payload - Device 内部定义的设备类型实例(无权访问)。

当我 import com.google.actions.api.smarthome.SyncResponse.Payload.Device; 时,我收到一条“无法解析”的错误,因此,设备引用显示“设备无法解析”被解析为“类型”错误。

如果我使用 com.google.api.services.actions_fulfillment.v2.model.Device,则 SyncResponse.Payload.Device 将不可见,如 screenshot 左侧所示(我无法上传图片),我无法转换。

由于我之前错过了添加代码,让我们使用 OnOff 引用页面,其中可以复制错误。

package com.example;

import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import com.google.actions.api.smarthome.DisconnectRequest;
import com.google.actions.api.smarthome.ExecuteRequest;
import com.google.actions.api.smarthome.ExecuteResponse;
import com.google.actions.api.smarthome.QueryRequest;
import com.google.actions.api.smarthome.QueryResponse;
import com.google.actions.api.smarthome.SmartHomeApp;
import com.google.actions.api.smarthome.SyncRequest;
import com.google.actions.api.smarthome.SyncResponse;
import com.google.actions.api.smarthome.SyncResponse.Payload;
import com.google.actions.api.smarthome.SyncResponse.Payload.Device;

public class MyActionsApp extends SmartHomeApp {

@NotNull
@Override
public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> headers) {
Payload payload = new Payload();
payload.setAgentUserId("1836.15267389");
payload.setDevices(new Device[] {
new Device.Builder().setId("123")
.setType("action.devices.types.LIGHT")
.addTrait("action.devices.traits.OnOff")
.setName(
Collections.singletonList("AAA bulb A19 color hyperglow"),
"lamp1",
Collections.singletonList("reading lamp")
)
.setWillReportState(true)
.setAttributes(new JSONObject()
.put("commandOnlyOnOff", false)
)
.setDeviceInfo("BrandX", "hg11", "1.2", "5.4")
.setCustomData(new JSONObject()
.put("fooValue", 12)
.put("barValue", false)
.put("bazValue", "dancing alpaca")
.toString()
)
.build() });
return new SyncResponse(syncRequest.getRequestId(), payload);
}

@Override
public void onDisconnect(DisconnectRequest request, Map<?, ?> headers) {
}

@Override
public ExecuteResponse onExecute(ExecuteRequest request, Map<?, ?> headers) {
return null;
}

@Override
public QueryResponse onQuery(QueryRequest request, Map<?, ?> headers) {
return null;
}
}

我应该如何创建或转换 Device 对象?

PS:抱歉之前没说清楚。

最佳答案

目前尚不清楚您的问题具体是什么。该文档包含许多有关如何使用 Device 对象数组创建正确的 SyncResponse 的示例。

来自OnOff reference page :

@NotNull
@Override
public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> headers) {
Payload payload = new Payload();
payload.setAgentUserId("1836.15267389");
payload.setDevices(new Device[] {
new Device.Builder()
.setId("123")
.setType("action.devices.types.LIGHT")
.addTrait("action.devices.traits.OnOff")
.setName(
Collections.singletonList("AAA bulb A19 color hyperglow"),
"lamp1",
Collections.singletonList("reading lamp")
)
.setWillReportState(true)
.setAttributes(new JSONObject()
.put("commandOnlyOnOff", false)
)
.setDeviceInfo("BrandX", "hg11", "1.2", "5.4")
.setCustomData(new JSONObject()
.put("fooValue", 12)
.put("barValue", false)
.put("bazValue", "dancing alpaca")
.toString()
)
.build()
});
return new SyncResponse(syncRequest.getRequestId(), payload);
}

关于java - 无法为 SyncResponse Payload 创 build 备对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com