首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SyncResponse需要哪个jar来创建设备

SyncResponse需要哪个jar来创建设备
EN

Stack Overflow用户
提问于 2020-07-17 15:35:30
回答 1查看 30关注 0票数 0
代码语言:javascript
复制
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;


public class GoogleDeviceSync extends SmartHomeApp {
    
    @NotNull
    @Override
    public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> map) {
      Payload payload = new Payload();
      payload.setAgentUserId("1836.15267389");
      payload.setDevices(
          new Device[] {
            new Device.Builder()
                .setId("123")
                .setType("action.devices.types.OUTLET")
                .addTrait("action.devices.traits.OnOff")
                .setName(
                    Collections.singletonList("My Outlet 1234"),
                    "Night light",
                    Collections.singletonList("Wall plug"))
                .setWillReportState(true)
                .setDeviceInfo("lights-out-inc", "hs1234", "3.2", "11.4")
                .setCustomData(
                    new JSONObject()
                        .put("fooValue", 74)
                        .put("barValue", true)
                        .put("bazValue", "foo"))
                .build(),
            new Device.Builder()
                .setId("456")
                .setType("action.devices.types.LIGHT")
                .addTrait("action.devices.traits.OnOff")
                .addTrait("action.devices.traits.Brightness")
                .addTrait("action.devices.traits.ColorTemperature")
                .addTrait("action.devices.traits.ColorSpectrum")
                .setName(
                    Collections.singletonList("Lights Out Inc. bulb A19 color hyperglow"),
                    "Lamp",
                    Collections.singletonList("Reading lamp"))
                .setWillReportState(true)
                .setDeviceInfo("Lights Out Inc.", "hg11", "1.2", "5.4")
                .setCustomData(
                    new JSONObject()
                        .put("fooValue", 12)
                        .put("barValue", false)
                        .put("bazValue", "bar"))
                .build(),
          });
      return new SyncResponse(syncRequest.getRequestId(), payload);
    }   
}

payload.setDevices(new Device[])Device是从哪个jar引入的?在https://developers.google.com/assistant/smarthome/develop/process-intents文档中,只有代码,没有显示jar的介绍。那么‘’device‘需要引入哪个jar呢?

EN

回答 1

Stack Overflow用户

发布于 2020-07-17 15:48:52

添加依赖项(jars)

需要在gradle.build中添加的依赖项( jar)应该是:

代码语言:javascript
复制
implementation 'com.google.actions:actions-on-google:1.8.0'

具体版本将取决于您的配置和环境。您可以查看完整的gradle.build示例。在同一文件夹中还有其他配置示例。

导入类

我还添加了一些关于PayloadDevice类所在位置的信息。Payload嵌套在SyncResponse中,Device嵌套在Payload中。

使用以下import语句导入Payload

代码语言:javascript
复制
import com.google.actions.api.smarthome.SyncResponse.Payload

可以使用以下import语句导入Device

代码语言:javascript
复制
import com.google.actions.api.smarthome.SyncResponse.Payload.Device

Creating Objects

要从SyncResponse中的有效负载创建新有效负载,请执行以下操作:

代码语言:javascript
复制
new SyncResponse.Payload()

你可以尝试从它的构建器创建一个设备:

代码语言:javascript
复制
SyncResponse.Payload.Device.Builder deviceBuilder = new syncResponse.Payload.Device.Builder()

有关“有效负载”和“设备”的用法,请参阅此example

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62949267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档