无法为同步响应有效负载创建设备对象



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

我的问题是我无法创建在 SyncResponse - 有效负载 - 设备中定义的设备类型的实例(无访问权限(。

当我import com.google.actions.api.smarthome.SyncResponse.Payload.Device;时,我收到"无法解析"的错误,因此,设备引用出现"设备无法解析为类型"错误。

如果我使用 com.google.api.services.actions_fulfillment.v2.model.Device ,之后 SyncResponse.Payload.Device 不可见,如屏幕截图左侧所示(我无法上传图片(,我无法投射。

由于我之前错过了添加代码,让我们从 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;
    }
}

应如何创建或强制转换设备对象?

PS:对不起,之前不清楚。

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

从 OnOff 参考页面:

@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 Core 错误,在 Eclipse 版本 2019-09 中修复。

最新更新