无法为 SyncResponse 负载创建设备对象

No able to create Device object for SyncResponse Payload

据我所知,SyncResponse 的有效载荷是一个 Device 类型的数组。

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

当我 import com.google.actions.api.smarthome.SyncResponse.Payload.Device; 收到 "cannot be resolved" 错误时,设备引用出现 "Device cannot be resolved to a type" 错误。

如果我用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 对象?

P.S.: 抱歉之前没说清楚

尚不清楚您的具体问题是什么。该文档包含许多有关如何使用 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);
}

是一个 JDT 核心 bug 并且在 Eclipse 版本 2019-09 上得到修复。