Actions On Google 在 SYNC 调用后不添加测试设备

问题描述

我已经建立了一个关于 Actions On Google 的项目,我应该可以通过它向我的 Google 助理添加恒温器。将其添加到 Google Home 不起作用,但是,知道为什么吗?

在开发选项卡中,我添加了:

  • 履行网址
  • 客户端 ID 和密码
  • 授权和令牌 URL

通过授权 URL,我能够(存根)登录,并返回所需的令牌,如下所示: {"token_type":"bearer","access_token":"123access","expires_in":86400}

返回令牌后,我收到了带有请求 ID 的 SYNC 调用,正如预期的那样。我返回以下内容

{"payload":{"agentUserId":"sampleClientId","devices":[{"traits":["action.devices.traits.TemperatureSetting"],"willReportState":true,"attributes":{"availableThermostatModes":["off","heat","cool","heatcool","on"],"thermostatTemperatureRange":{"maxThresholdCelsius":30,"minThresholdCelsius":15},"thermostatTemperatureUnit":"F"},"id":"123","type":"action.devices.types.THERMOSTAT","deviceInfo":{"swVersion":"11.4","model":"hs1234","manufacturer":"smart-home-inc","hwVersion":"3.2"}}]},"requestId":"16241058020468313677"}

这个 json 结构来自恒温器上的 documentation。但是,一旦请求完成,我只会再收到 3 个具有完全相同结果的 SYNC 调用。我的 Google Home 和智能助理应用程序首先告诉我添加设备有效,3 秒后通知我毕竟出了点问题。我没有从谷歌那里得到错误代码,所以我不确定我做错了什么。 'agentUserId' 属性是我添加到 Actions on Google 控制台的客户端 ID,因为我不确定我必须在那里添加什么。

我将在下面添加最重要的代码以使其完整。

onSync(..) 方法

@NotNull
    @Override
    public SyncResponse onSync(@NotNull SyncRequest syncRequest,@Nullable Map<?,?> headers) {
        SyncResponse res = new SyncResponse();
        res.setRequestId(syncRequest.requestId);
        res.setPayload(new SyncResponse.Payload());

        String token = (String) headers.get("authorization");
        String user = TokenServlet.user.getUserId();
        res.payload.agentUserId = user;

        List<DevicesItem> devices = TokenServlet.user.getDevices();

        int numOfDevices = devices.size();
        res.payload.devices = new SyncResponse.Payload.Device[numOfDevices];
        for (int i = 0; i < numOfDevices; i++) {
            DevicesItem device = devices.get(i);
            SyncResponse.Payload.Device.Builder deviceBuilder =
                    new SyncResponse.Payload.Device.Builder()
                            .setId(device.getId())
                            .setType(device.getType())
                            .setTraits(device.getTraits())
                            .setwillReportState(device.isWillReportState())
                            .setDeviceInfo(
                                    DeviceProto.DeviceInfo.newBuilder()
                                            .setManufacturer(device.getDeviceInfo().getManufacturer())
                                            .setModel(device.getDeviceInfo().getModel())
                                            .setHwVersion(device.getDeviceInfo().getHwVersion())
                                            .setSwVersion(device.getDeviceInfo().getSwVersion())
                                            .build())
                    ;
            if (!Objects.isNull(device.getAttributes())) {
                String attributesJson = new Gson().toJson(device.getAttributes());
                Struct.Builder attributeBuilder = Struct.newBuilder();
                try {
                    JsonFormat.parser().ignoringUnkNownFields().merge(attributesJson,attributeBuilder);
                } catch (Exception e) {
                    LOGGER.error("Failed TO BUILD");
                }
                deviceBuilder.setAttributes(attributeBuilder.build());
            }
            res.payload.devices[i] = deviceBuilder.build();
        }

        return res;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...