Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
64 views
in Technique[技术] by (71.8m points)

java - Actions On Google doesn't add test device after SYNC call

I have set up a project on Actions On Google with which I should be able to add a thermostat to my Google Assistant. Adding it to Google Home doesn't work, however, any idea why?

In the develop-tab, I have added:

  • a fulfillment URL
  • Client ID & secret
  • Authorization & Token URL

Via the authorization URL, I was able to (stub) log on, and return the required tokens as such: {"token_type":"bearer","access_token":"123access","expires_in":86400}

Upon returning the tokens, I receive a SYNC call with the request ID, as expected. I return the following.

{"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"}

This json structure came from the documentation on thermostats. However, once the request finishes, I just receive 3 more SYNC calls with the exact same result. My Google Home and Assistant apps first tell me adding the device worked, and 3 seconds later notify me that something went wrong after all. I don't get an error code from Google, so I'm not sure what I'm doing wrong. The 'agentUserId' property is the client ID I added to the Actions on Google console, since I wasn't sure what I had to add there.

I'll add the most important code below to be complete.

The onSync(..) method:

@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;
    }
question from:https://stackoverflow.com/questions/65598941/actions-on-google-doesnt-add-test-device-after-sync-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...