Search code examples
javaactions-on-google

No able to create Device object for SyncResponse Payload


As I understood, the Payload for SyncResponse was an array of type Device.

My problem is that I am not able to create an instance (don't have access) of type Device defined inside of SyncResponse - Payload - Device.

When I import com.google.actions.api.smarthome.SyncResponse.Payload.Device; I receive an error that "cannot be resolved" and as result, the Device references has "Device cannot be resolved to a type" error.

If I use com.google.api.services.actions_fulfillment.v2.model.Device, following that SyncResponse.Payload.Device is not visible as shown on the left side of the screenshot (I can't upload pictures), I can't cast.

As I missed the add code before, let's use from OnOff reference page, where the errors are possible to replicate.

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;
    }
}

How should I create a Device object, or cast it?

P.S.: Sorry for not be clear before.


Solution

  • Was a JDT Core bug and is fixed on the Eclipse version 2019-09.