无法为SyncResponse有效负载创建Device对象

时间:2019-06-10 16:15:58

标签: java actions-on-google

据我了解,SyncResponse的有效负载是设备类型的数组。

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

当我import com.google.actions.api.smarthome.SyncResponse.Payload.Device;收到“无法解析”的错误时,结果是设备引用出现“无法将设备解析为类型”错误。

如果我使用com.google.api.services.actions_fulfillment.v2.model.Device,则在screenshot左侧显示的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;
    }
}

我应该如何创建或投射设备对象?

P.S .:很抱歉,以前不清楚。

2 个答案:

答案 0 :(得分:0)

目前尚不清楚您的问题特别重要。该文档包含许多示例,这些示例说明如何使用SyncResponse个对象数组来创建合适的Device

来自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);
}

答案 1 :(得分:0)

是JDT核心bug,并且在Eclipse版本2019-09中已修复。